#
global $error;
$error = null;
#
function getsock($addr, $port)
{
global $error;
$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";
$error = "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";
$error = "ERR: $msg '$error'\n";
socket_close($socket);
return null;
}
return $socket;
}
#
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 api($cmd)
{
global $miner, $port;
$socket = getsock($miner, $port);
if ($socket != null)
{
socket_write($socket, $cmd, strlen($cmd));
$line = readsockline($socket);
socket_close($socket);
if (strlen($line) == 0)
{
$error = "WARN: '$cmd' returned nothing\n";
return $line;
}
# print "$cmd returned '$line'\n";
$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;
}
#
function getparam($name, $both = false)
{
$a = null;
if (isset($_POST[$name]))
$a = $_POST[$name];
if (($both === true) and ($a === null))
{
if (isset($_GET[$name]))
$a = $_GET[$name];
}
if ($a == '' || $a == null)
return null;
// limit to 1K just to be safe
return substr($a, 0, 1024);
}
#
function details($list)
{
$stas = array('S' => 'Succees', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
$tb = ' | ';
echo $tb;
echo 'Date: '.date('H:i:s j-M-Y \U\T\CP').' | ';
echo $te.$tb;
if (isset($list['STATUS']))
{
echo '';
echo 'Computer: '.$list['STATUS']['Description'].' | ';
$sta = $list['STATUS']['STATUS'];
echo 'Status: '.$stas[$sta].' | ';
echo 'Message: '.$list['STATUS']['Msg'].' | ';
echo ' ';
}
echo $te.$tb;
foreach ($list as $item => $values)
{
if ($item != 'STATUS')
{
echo '';
foreach ($values as $name => $value)
{
if ($name == '0')
$name = ' ';
echo "$name | ";
}
echo ' ';
break;
}
}
foreach ($list as $item => $values)
{
if ($item == 'STATUS')
continue;
foreach ($values as $name => $value)
echo "$value | ";
echo '';
}
echo $te;
}
#
function gpubuttons($count)
{
$tb = ' | ';
echo $tb.'';
for ($i = 0; $i < 4; $i++)
{
echo '';
if ($i == 0)
echo 'GPU';
else
echo ' ';
echo ' | ';
}
for ($c = 0; $c < $count; $c++)
{
echo ' ';
echo "$c | ";
echo " | ";
echo " | ";
echo " | ";
}
echo ' '.$te;
}
#
function processgpus($rd, $ro)
{
global $error;
$gpus = api('gpucount');
if ($error != null)
echo 'Error getting GPU count: '.$rd.$error.$ro.' | ';
else
{
if (!isset($gpus['GPUS']['Count']))
echo 'No GPU count returned: '.$rd.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.' | ';
else
{
$count = $gpus['GPUS']['Count'];
if ($count == 0)
echo 'No GPUs | ';
else
gpubuttons($count);
}
}
}
#
function process($cmds, $rd, $ro)
{
global $error;
foreach ($cmds as $cmd => $des)
{
$process = api($cmd);
if ($error != null)
{
echo "Error getting $des: ";
echo $rd.$error.$ro.' | ';
break;
}
else
{
details($process);
echo '
| ';
}
}
}
#
function display()
{
global $error;
$error = null;
$rd = '';
$ro = '';
echo " | ";
$arg = trim(getparam('arg', true));
if ($arg != null and $arg != '')
process(array($arg => $arg), $rd, $ro);
$cmds = array( 'devs' => 'device list',
'summary' => 'summary information',
'pools' => 'pool list');
process($cmds, $rd, $ro);
if ($error == null)
processgpus($rd, $ro);
}
#
display();
#
?>
|