Browse Source

api: add a sample rig web ui (php)

master
Tanguy Pruvot 10 years ago
parent
commit
3dcfa5d84f
  1. 4
      README.txt
  2. 130
      api/index.php
  3. 230
      api/local-sample.php

4
README.txt

@ -156,8 +156,8 @@ features.
>>> RELEASE HISTORY <<< >>> RELEASE HISTORY <<<
Nov. 13th 2014 v1.4.9 Nov. 13th 2014 v1.4.9
Add nvml unit to monitor nvidia cards (api) Support of nvml and nvapi to monitor gpus
API: small changes, bump v1.1 api: Fix multi gpus stats, bump API v1.1
Nov. 12th 2014 v1.4.8 Nov. 12th 2014 v1.4.8
Add a basic API and sample php json wrapper Add a basic API and sample php json wrapper

130
api/index.php

@ -0,0 +1,130 @@
<?php
/* ccminer API sample UI */
$host = 'http://localhost/api/'; // 'http://'.$_SERVER['SERVER_NAME'].'/api/';
$configs = array(
'LOCAL'=>'local-sample.php',
//'EPSYTOUR'=>'epsytour.php', /* copy local.php file and edit target IP:PORT */
);
function getdataFromPears()
{
global $host, $configs;
$data = array();
foreach ($configs as $name => $conf) {
$json = file_get_contents($host.$conf);
$data[$name] = json_decode($json, TRUE);
}
return $data;
}
function translateField($key)
{
$intl = array();
$intl['NAME'] = 'Software';
$intl['VER'] = 'Version';
$intl['ALGO'] = 'Algorithm';
$intl['KHS'] = 'Hashrate (kH/s)';
$intl['ACC'] = 'Accepted shares';
$intl['ACCMN'] = 'Accepted / mn';
$intl['REJ'] = 'Rejected';
$intl['UPTIME'] = 'Miner uptime';
$intl['TEMP'] = 'T°c';
$intl['FAN'] = 'Fan %';
if (isset($intl[$key]))
return $intl[$key];
else
return $key;
}
function displayData($data)
{
$htm = '';
foreach ($data as $name => $stats) {
$htm .= '<table id="tb_'.$name.'" class="stats">'."\n";
$htm .= '<tr><th class="machine" colspan="2">'.$name."</th></tr>\n";
foreach ($stats['summary'] as $key=>$val) {
if (!empty($val))
$htm .= '<tr><td class="key">'.translateField($key).'</td>'.
'<td class="val">'.$val."</td></tr>\n";
}
foreach ($stats['stats'] as $g=>$gpu) {
$htm .= '<tr><th class="gpu" colspan="2">'.$g."</th></tr>\n";
foreach ($gpu as $key=>$val) {
if (!empty($val))
$htm .= '<tr><td class="key">'.translateField($key).'</td>'.
'<td class="val">'.$val."</td></tr>\n";
}
}
$htm .= "</table>\n";
}
return $htm;
}
$data = getdataFromPears();
?>
<html>
<head>
<title>ccminer rig api sample</title>
<style type="text/css">
body { color:#cccccc; background:#1d1d1d; margin:30px 30px 0px 30px; padding:0px; font-size:.8em; font-family:Arial,Helvetica,sans-serif; }
a { color:#aaaaaa; text-decoration: none; }
a:focus { outline-style:none; }
.clear { clear: both; }
div#page, div#header, div#footer {
margin: auto;
width: 950px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.15);
}
div#page {
padding-top: 8px;
background: #252525;
min-height: 820px;
}
div#header {
background: rgba(65, 65, 65, 0.85);
height: 50px;
margin-bottom: 24px;
padding-left: 8px;
}
div#footer {
background: rgba(25, 25, 25, 0.85);
height: 0px;
margin-bottom: 40px;
text-align: center;
color: #666666;
text-shadow: rgba(0, 0, 0, 0.8) 0px 1px 0px;
padding-top: 8px;
}
#header h1 { padding: 12px; font-size: 20px; }
#footer p { margin: 12px 24px; }
table.stats { width: 280px; margin: 4px 16px; display: inline-block; }
th.machine { color: darkcyan; padding: 16px 0px 0px 0px; text-align: left; border-bottom: 1px solid gray; }
th.gpu { color: white; padding: 3px 3px; font: bolder; text-align: left; background: rgba(65, 65, 65, 0.85); }
td.key { width: 40px; max-width: 120px; }
td.val { width: 70px; max-width: 180px; color: white; }
</style>
</head>
<body>
<div id="header">
<h1>ccminer monitoring API RIG sample</h1>
</div>
<div id="page">
<?=displayData($data)?>
</div>
<div id="footer">
<p>&copy; 2014 <a href="http://github.com/tpruvot/ccminer">tpruvot@github</a></p>
</div>
</body>
</html>

230
api-example.php → api/local-sample.php

@ -1,115 +1,115 @@
<?php <?php
/** /**
* Sample Request API to ccminer * Sample Request API to ccminer
*/ */
defined('API_HOST') || define('API_HOST', '127.0.0.1'); defined('API_HOST') || define('API_HOST', '127.0.0.1');
defined('API_PORT') || define('API_PORT', 4068); defined('API_PORT') || define('API_PORT', 4068);
function getsock($port) function getsock($port)
{ {
$socket = null; $socket = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null) { if ($socket === false || $socket === null) {
$error = socket_strerror(socket_last_error()); $error = socket_strerror(socket_last_error());
$msg = "socket create($port) failed"; $msg = "socket create($port) failed";
echo "ERR: $msg '$error'\n"; echo "ERR: $msg '$error'\n";
return NULL; return NULL;
} }
$res = socket_connect($socket, API_HOST, $port); $res = socket_connect($socket, API_HOST, $port);
if ($res === false) { if ($res === false) {
$error = socket_strerror(socket_last_error()); $error = socket_strerror(socket_last_error());
$msg = "socket connect($port) failed"; $msg = "socket connect($port) failed";
echo "ERR: $msg '$error'\n"; echo "ERR: $msg '$error'\n";
socket_close($socket); socket_close($socket);
return NULL; return NULL;
} }
return $socket; return $socket;
} }
function readsockline($socket) function readsockline($socket)
{ {
$line = ''; $line = '';
while (true) { while (true) {
$byte = socket_read($socket, 1); $byte = socket_read($socket, 1);
if ($byte === false || $byte === '') if ($byte === false || $byte === '')
break; break;
if ($byte === "\0") if ($byte === "\0")
break; break;
$line .= $byte; $line .= $byte;
} }
return $line; return $line;
} }
function request($cmd) function request($cmd)
{ {
$socket = getsock(API_PORT); $socket = getsock(API_PORT);
if ($socket == null) if ($socket == null)
return NULL; return NULL;
socket_write($socket, $cmd, strlen($cmd)); socket_write($socket, $cmd, strlen($cmd));
$line = readsockline($socket); $line = readsockline($socket);
socket_close($socket); socket_close($socket);
if (strlen($line) == 0) { if (strlen($line) == 0) {
echo "WARN: '$cmd' returned nothing\n"; echo "WARN: '$cmd' returned nothing\n";
return $line; return $line;
} }
echo "$cmd returned '$line'\n"; echo "$cmd returned '$line'\n";
$data = array(); $data = array();
$objs = explode('|', $line); $objs = explode('|', $line);
foreach ($objs as $obj) foreach ($objs as $obj)
{ {
if (strlen($obj) > 0) if (strlen($obj) > 0)
{ {
$items = explode(';', $obj); $items = explode(';', $obj);
$item = $items[0]; $item = $items[0];
$id = explode('=', $items[0], 2); $id = explode('=', $items[0], 2);
if (count($id) == 1) if (count($id) == 1)
$name = $id[0]; $name = $id[0];
else else
$name = $id[0].$id[1]; $name = $id[0].$id[1];
if (strlen($name) == 0) if (strlen($name) == 0)
$name = 'null'; $name = 'null';
if (isset($data[$name])) { if (isset($data[$name])) {
$num = 1; $num = 1;
while (isset($data[$name.$num])) while (isset($data[$name.$num]))
$num++; $num++;
$name .= $num; $name .= $num;
} }
$counter = 0; $counter = 0;
foreach ($items as $item) foreach ($items as $item)
{ {
$id = explode('=', $item, 2); $id = explode('=', $item, 2);
if (count($id) == 2) if (count($id) == 2)
$data[$name][$id[0]] = $id[1]; $data[$name][$id[0]] = $id[1];
else else
$data[$name][$counter] = $id[0]; $data[$name][$counter] = $id[0];
$counter++; $counter++;
} }
} }
} }
if ($cmd == 'summary') if ($cmd == 'summary')
return array_pop($data); return array_pop($data);
else else
return $data; return $data;
} }
ob_start(); ob_start();
$summary = request('summary'); $summary = request('summary');
$stats = request('stats'); $stats = request('stats');
ob_end_clean(); ob_end_clean();
//echo ob_get_clean()."\n"; //echo ob_get_clean()."\n";
header("Content-Type: application/json"); header("Content-Type: application/json");
echo json_encode(compact('summary', 'stats'))."\n"; echo json_encode(compact('summary', 'stats'))."\n";
?> ?>
Loading…
Cancel
Save