Post
Topic
Merits 2 from 2 users
Re: Antminer Z9 mini overclocked
by
scott85213
on 08/09/2018, 04:42:03 UTC
⭐ Merited by grinbuck (1) ,vapourminer (1)
Since my batch 2 units came, and they seem to be quite a bit more finicky on overclocking, I've written a php script that keeps an eye on them and restarts them if they end up failing.

To configure, just change the miners IP's, user name, password, and threshold.

Requirements:
  • PHP webserver with sockets enabled and phpseclib library
  • Z9s need to be on your network or locally accessable

Other fun stuff included: There's a couple logging functions that will dump your hashrate stats into some CSV's.  I use them to make pretty charts. https://i.imgur.com/JAviVIA.png

Random notes... this restarts any machine with a 'failed' ASIC chain or one that has been powered on for more than 2 minutes and doesn't meet your threshold value, it will then restart it via SSH with the phpseclib library.  It pulls all stats with the default enabled CGMiner API, so you don't need to enable anything for that to work.  It actually pulls all the API info, so there's much more info available than what I'm using.  

And if you do it right, you'll end up with something like:
https://i.imgur.com/T3jRoWt.png

Code:
set_include_path(get_include_path() . PATH_SEPARATOR 'phpseclib');
include(
'phpseclib/Net/SSH2.php');
header('refresh:600; url=index.php'); //change this to change the refresh in seconds (how often you want it to check on your machines)
date_default_timezone_set('America/Los_Angeles');
$machines = array
(
    
=> array(
'name' => 'Mini-1',
'ip' => '192.168.1.175',
'user' => 'root',
'password' => 'root',
'threshold' => 14,
),
=> array(
"name" => "Mini-2",
"ip" => "192.168.1.160",
'user' => 'root',
'password' => 'root',
"threshold" => 12,
),
=> array(
"name" => "Mini-3",
"ip" => "192.168.1.51",
'user' => 'root',
'password' => 'root',
"threshold" => 12,
),
=> array(
"name" => "Mini-4",
"ip" => "192.168.1.52",
'user' => 'root',
'password' => 'root',
"threshold" => 12,
)
);

foreach (
$machines as &$machine
{
createEmptyLogs($machine);
getstats($machine);
if(checkFails($machine)) restart($machine);
if(!file_exists"logs/overallhashrate.csv" )) file_put_contents("logs/overallhashrate.csv""time,total hashrate\r\n"FILE_APPEND);
}

printTotalHashrate($machines);
echo 
'';

function 
createEmptyLogs($machine)
{
if(!file_exists'logs/'.$machine['name']."hashrate.csv" )) file_put_contents('logs/'.$machine['name']."hashrate.csv""time,".$machine['name']." hashrate\r\n"FILE_APPEND);
}

function 
logging($machine)
{
file_put_contents('logs/'.$machine['name']."hashrate.csv"date('j M H:i:s'time()).",".$machine['ghs5s']."\r\n"FILE_APPEND);
}

function 
restart($machine)
{
$ssh = new Net_SSH2($machine['ip']);
if (!$ssh->login($machine['user'], $machine['password'])) {
exit('Login Failed');
}

$ssh->exec('/etc/init.d/cgminer.sh restart >/dev/null 2>&1');
}

function 
checkFails(&$machine)
{
if (($machine['chain_acs1'] == 'xxxx' or $machine['chain_acs2'] == 'xxxx' or $machine['chain_acs3'] == 'xxxx' or intval($machine['ghs5s']) < intval($machine['threshold'])) and intval($machine['elapsed']) > 120)
return true//this restarts any machine with a 'failed' ASIC chain or one that has been powered on for more than 2 minutes and doesn't meet your threshold value
else return false;
}

function 
getstats(&$machine)
{
// create a socket
$socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
$result socket_connect($socket$machine['ip'], 4028);
if ($socket === false || $result === false) {
echo $machine['name']. " appears to be offline!";
$machine['ghs5s'] = 0;
$machine['ghsav'] = 0;
$machine['chain_rate1'] = 0;
$machine['chain_rate2'] = 0;
$machine['chain_rate3'] = 0;
return;
}
else
{
// send a 'summary' command to antminer
$in '{"command":"stats"}';
$out '';
socket_write($socket$instrlen($in));

// read output from antminer
$output="";
while ($out socket_read($socket2048)) {
$output=$output.$out;
}
socket_close($socket);

$output strtolower($output);
$output str_replace(" """$output);
$output str_replace(","":"$output);
$output str_replace("\""""$output);
$output str_replace("}"""$output);
$output str_replace("]"""$output);
$output explode(":"substr($output,strpos($output,"elapsed"),strpos($output,"id:1")));

foreach($output as $i => $item//create the rest of the item entries
{
if ($i == 0)
$machine[$output[$i]] = $output[$i+1];
}
printmachinestats($machine); //Don't print stats or logs if they aren't online
logging($machine);
}

//print_r($machine);

/* old method of parsing string manually
$machine['totalhashrate']=substr($output,strpos($output,"GHS 5s")+9);

$machine['totalhashrate']=floatval(substr($machine['totalhashrate'],0, strpos($machine['totalhashrate'],"GHS av")));
$machine['uptime']=substr($output,strpos($output,"Elapsed")+9);
$machine['uptime']=floatval(substr($machine['uptime'],0, strpos($machine['uptime'],"GHS 5s")));
$machine['board1hash']=substr($output,strpos($output,"chain_rate1")+14);
$machine['board1hash']=floatval(substr($machine['board1hash'],0, strpos($machine['board1hash'],"chain_rate2")));
$machine['board2hash']=substr($output,strpos($output,"chain_rate2")+14);
$machine['board2hash']=floatval(substr($machine['board2hash'],0, strpos($machine['board2hash'],"chain_rate3")));
$machine['board3hash']=substr($output,strpos($output,"chain_rate3")+14);
$machine['board3hash']=floatval(substr($machine['board3hash'],0, strpos($machine['board3hash'],"}],")));
$machine['board1status']=substr($output,strpos($output,"chain_acs1")+13);
$machine['board1status']=substr($machine['board1status'],0, strpos($machine['board1status'],"chain_acs2")-3);
$machine['board2status']=substr($output,strpos($output,"chain_acs2")+13);
$machine['board2status']=substr($machine['board2status'],0, strpos($machine['board2status'],"chain_acs3")-3);
$machine['board3status']=substr($output,strpos($output,"chain_acs3")+13);
$machine['board3status']=substr($machine['board3status'],0, strpos($machine['board3status'],"chain_hw1")-3);
*/
}

function 
printmachinestats(&$machine)
{
print $machine['name'];
echo '';
print "IP Address = ".$machine['ip'];
echo '';
print "Hashrate = ".$machine['ghsav']. " KSol/S";
echo '';
print "Uptime = ".secondsToTime($machine['elapsed']);
echo '';
print "Board 1 Hash = ".$machine['chain_rate1']. " KSol/S";
echo '';
print "Board 2 Hash = ".$machine['chain_rate2']. " KSol/S";
echo '';
print "Board 3 Hash = ".$machine['chain_rate3']. " KSol/S";
echo '';
print "Board 1 Status = ".$machine['chain_acs1'];
echo '';
print "Board 2 Status = ".$machine['chain_acs2'];
echo '';
print "Board 3 Status = ".$machine['chain_acs3'];
echo '';

echo '';
}

function 
printTotalHashrate($machines)
{
$total 0;
foreach ($machines as &$machine
{
$total += $machine['ghs5s'];
}
print "Total Hashrate = " . $total" KSol/S";
//['2004',  1000,      400]
file_put_contents("logs/overallhashrate.csv"date('j M H:i:s'time()).",".$total."\r\n"FILE_APPEND); //totals Hashrate

}

function 
secondsToTime($seconds
{
    
$dtF = new \DateTime('@0');
    
$dtT = new \DateTime("@$seconds");
    return 
$dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
?>

I won't be providing any support for this, but I can answer any simple questions.

Donations:
LTC: MKKDJpk74UjUWXEPM2v7x7QfM2RFZxyQUa
ETH: 0xD3c330bdc5b5f964a45D20dBC3e71F6b15E442eb