Skip to content

Instantly share code, notes, and snippets.

@tperalta82
Last active May 3, 2018 23:43
Show Gist options
  • Save tperalta82/6e11253cd4b9cf9c5c6fa15bbf046d4f to your computer and use it in GitHub Desktop.
Save tperalta82/6e11253cd4b9cf9c5c6fa15bbf046d4f to your computer and use it in GitHub Desktop.
Reboot D3 on fail board
#!/usr/bin/php
<?php
$hosts = array("antminer1","antminer2");
$minercreds = array( "user" => "root", "pass" => "becauseIAmHigh");
$failhashboard = "oxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxx";
/* I'm too stoned to try and detect how many hashboards are there*/
$possiblehashboards = 4;
foreach($hosts as $host)
{
$r = request("stats",$host);
for( $i = 1; $i <= $possiblehashboards; $i++ )
{
//var_dump(array_keys($r));
if( isset( $r["STATS0"]["chain_acs".$i]) && trim($r["STATS0"]["chain_acs".$i]) === $failhashboard )
{
echo "errors on chain $i on $host, sending reboot".PHP_EOL;
$out = rebootMinerWget($host, $minercreds['user'], $minercreds['pass']);
echo $out.PHP_EOL;
break;
}
}
}
function rebootMinerWget( $host, $user, $pass )
{
$cmd = "/usr/bin/wget -qO- --http-user=".$user." --http-password=".$pass." http://".$host."/cgi-bin/reboot.cgi";
return shell_exec($cmd);
}
function rebootMinerCurl( $host, $user, $pass )
{
$url = "http://".$host."/cgi-bin/reboot.cgi";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass" );
$reboot = curl_exec($ch);
return $reboot;
}
function rebootMiner($host, $user, $pass)
{
$conn = ssh2_connect($host, 22);
if(!$conn )
{
echo "Could not connect to $host".PHP_EOL;
return false;
}
if(!ssh2_auth_password( $conn, $user, $pass ) )
{
echo "Failed to authenticate on ssh $host".PHP_EOL;
return false;
}
$out = ssh2_exec($conn , "reboot");
stream_set_blocking($out, true);
$result = stream_get_contents($out);
fclose($out);
return $result;
}
function getsock($addr, $port)
{
$socket = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null) {
$error = socket_strerror(socket_last_error());
$msg = "socket create(TCP) failed";
echo "ERR: $msg '$error'\n";
return null;
}
$res = socket_connect($socket, $addr, $port);
if ($res === false) {
$error = socket_strerror(socket_last_error());
$msg = "socket connect($addr,$port) failed";
echo "ERR: $msg '$error'\n";
socket_close($socket);
return null;
}
return $socket;
}
#
# Slow ...
function readsockline($socket)
{
$line = '';
while (true) {
$byte = socket_read($socket, 1);
if ($byte === false || $byte === '') break;
if ($byte === "\0") break;
$line .= $byte;
}
return $line;
}
#
function request($cmd, $host)
{
$socket = getsock($host, 4028);
if ($socket != null) {
socket_write($socket, $cmd, strlen($cmd));
$line = readsockline($socket);
socket_close($socket);
if (strlen($line) == 0) {
echo "WARN: '$cmd' returned nothing\n";
return $line;
}
//print "$cmd returned '$line'\n";
if (substr($line, 0, 1) == '{') {
return json_decode($line, true);
}
$data = array();
$objs = explode('|', $line);
foreach ($objs as $obj) {
if (strlen($obj) > 0) {
$items = explode(',', $obj);
$item = $items[0];
$id = explode('=', $items[0], 2);
if (count($id) == 1 or ! ctype_digit($id[1])) {
$name = $id[0];
} else {
$name = $id[0].$id[1];
}
if (strlen($name) == 0) {
$name = 'null';
}
if (isset($data[$name])) {
$num = 1;
while (isset($data[$name.$num])) {
$num++;
}
$name .= $num;
}
$counter = 0;
foreach ($items as $item) {
$id = explode('=', $item, 2);
if (count($id) == 2) {
$data[$name][$id[0]] = $id[1];
} else {
$data[$name][$counter] = $id[0];
}
$counter++;
}
}
}
return $data;
}
return null;
}
@besoeasy
Copy link

i wish you had included a module to scan all subnets for D3 miners, i was working on some kind of script but your is better so i will improve it

@Jalapan
Copy link

Jalapan commented Nov 1, 2017

Hi. Why does it say $failhashboard = "oxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxx";****

Theres other ways hashboards can be displayed and not working.. it can be oxxxooo xxxxxxx oooo and so on. and also
$possiblehashboards = 4; the D3 has 3 hashboards and L3+ 4 ... does it matter?

I would like to use this script but i have 0 experience.. i have a linux server i can add this to crontab but what values to change i dont understand..

THx

@gnanet
Copy link

gnanet commented Nov 5, 2017

STATS gives you a "miner_count" which holds the amount how much time you need to check "chain_acs" and the failed count is simply
substr_count($current_chain_acs_string, 'x') which is ideally ZERO, but i have to mention, lots of time, the restart of cgminer.sh is enough, to get the working chains back(this works only over SSH with /etc/init.d/cgminer.sh restart (shame on me, you already use SSH)). But sometimes the chains fail, then you have a hardware failure....

already done this in php

also you can have an issue with the chips per chain. Which is 60 for D3, and maybe 72 for L3+, so if you start to get less chips than the specs, that is also a misteriuos issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment