miner.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. session_start();
  3. #
  4. global $miner, $port, $readonly, $notify;
  5. $miner = '127.0.0.1'; # hostname or IP address
  6. $port = 4028;
  7. #
  8. # Set $readonly to true to force miner.php to be readonly
  9. # Set $readonly to false then it will check cgminer 'privileged'
  10. $readonly = false;
  11. #
  12. # Set $notify to false to NOT attempt to display the notify command
  13. # Set $notify to true to attempt to display the notify command
  14. # If your older version of cgminer returns an 'Invalid command'
  15. # coz it doesn't have notify - it just shows the error status table
  16. $notify = true;
  17. #
  18. $here = $_SERVER['PHP_SELF'];
  19. #
  20. function htmlhead()
  21. {
  22. global $error, $readonly, $here;
  23. if ($readonly === false)
  24. {
  25. $access = api('privileged');
  26. if ($error != null
  27. || !isset($access['STATUS']['STATUS'])
  28. || $access['STATUS']['STATUS'] != 'S')
  29. $readonly = true;
  30. }
  31. ?>
  32. <html><head><title>Mine</title>
  33. <style type='text/css'>
  34. td { color:blue; font-family:verdana,arial,sans; font-size:13pt; }
  35. td.h { color:blue; font-family:verdana,arial,sans; font-size:13pt; background:#d0ffff }
  36. td.err { color:black; font-family:verdana,arial,sans; font-size:13pt; background:#ff3050 }
  37. td.warn { color:black; font-family:verdana,arial,sans; font-size:13pt; background:#ffb050 }
  38. td.sta { color:green; font-family:verdana,arial,sans; font-size:13pt; }
  39. </style>
  40. </head><body bgcolor=#ecffff>
  41. <script type='text/javascript'>
  42. function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location="<?php echo $here ?>"+a}
  43. <?php
  44. if ($readonly === false)
  45. {
  46. ?>
  47. function prc(a,m){pr('?arg='+a,m)}
  48. function prs(a){var c=a.substr(3);var z=c.split('|',2);var m=z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' GPU '+z[1];prc(a,m)}
  49. function prs2(a,n){var v=document.getElementById('gi'+n).value;var c=a.substr(3);var z=c.split('|',2);var m='Set GPU '+z[1]+' '+z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' to '+v;prc(a+','+v,m)}
  50. <?php
  51. }
  52. ?>
  53. </script>
  54. <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
  55. <tr><td align=center valign=top>
  56. <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
  57. <?php
  58. }
  59. #
  60. global $error;
  61. $error = null;
  62. #
  63. function getsock($addr, $port)
  64. {
  65. global $error;
  66. $socket = null;
  67. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  68. if ($socket === false || $socket === null)
  69. {
  70. $error = socket_strerror(socket_last_error());
  71. $msg = "socket create(TCP) failed";
  72. $error = "ERR: $msg '$error'\n";
  73. return null;
  74. }
  75. $res = socket_connect($socket, $addr, $port);
  76. if ($res === false)
  77. {
  78. $error = socket_strerror(socket_last_error());
  79. $msg = "socket connect($addr,$port) failed";
  80. $error = "ERR: $msg '$error'\n";
  81. socket_close($socket);
  82. return null;
  83. }
  84. return $socket;
  85. }
  86. #
  87. function readsockline($socket)
  88. {
  89. $line = '';
  90. while (true)
  91. {
  92. $byte = socket_read($socket, 1);
  93. if ($byte === false || $byte === '')
  94. break;
  95. if ($byte === "\0")
  96. break;
  97. $line .= $byte;
  98. }
  99. return $line;
  100. }
  101. #
  102. function api($cmd)
  103. {
  104. global $miner, $port;
  105. $socket = getsock($miner, $port);
  106. if ($socket != null)
  107. {
  108. socket_write($socket, $cmd, strlen($cmd));
  109. $line = readsockline($socket);
  110. socket_close($socket);
  111. if (strlen($line) == 0)
  112. {
  113. $error = "WARN: '$cmd' returned nothing\n";
  114. return $line;
  115. }
  116. # print "$cmd returned '$line'\n";
  117. $data = array();
  118. $objs = explode('|', $line);
  119. foreach ($objs as $obj)
  120. {
  121. if (strlen($obj) > 0)
  122. {
  123. $items = explode(',', $obj);
  124. $item = $items[0];
  125. $id = explode('=', $items[0], 2);
  126. if (count($id) == 1 or !ctype_digit($id[1]))
  127. $name = $id[0];
  128. else
  129. $name = $id[0].$id[1];
  130. if (strlen($name) == 0)
  131. $name = 'null';
  132. if (isset($data[$name]))
  133. {
  134. $num = 1;
  135. while (isset($data[$name.$num]))
  136. $num++;
  137. $name .= $num;
  138. }
  139. $counter = 0;
  140. foreach ($items as $item)
  141. {
  142. $id = explode('=', $item, 2);
  143. if (count($id) == 2)
  144. $data[$name][$id[0]] = $id[1];
  145. else
  146. $data[$name][$counter] = $id[0];
  147. $counter++;
  148. }
  149. }
  150. }
  151. return $data;
  152. }
  153. return null;
  154. }
  155. #
  156. function getparam($name, $both = false)
  157. {
  158. $a = null;
  159. if (isset($_POST[$name]))
  160. $a = $_POST[$name];
  161. if (($both === true) and ($a === null))
  162. {
  163. if (isset($_GET[$name]))
  164. $a = $_GET[$name];
  165. }
  166. if ($a == '' || $a == null)
  167. return null;
  168. // limit to 1K just to be safe
  169. return substr($a, 0, 1024);
  170. }
  171. #
  172. function fmt($section, $name, $value)
  173. {
  174. $errorclass = ' class=err';
  175. $warnclass = ' class=warn';
  176. $b = '&nbsp;';
  177. $ret = $value;
  178. $class = '';
  179. switch ($section.'.'.$name)
  180. {
  181. case 'GPU.Last Share Time':
  182. case 'PGA.Last Share Time':
  183. $ret = date('H:i:s', $value);
  184. break;
  185. case 'SUMMARY.Elapsed':
  186. $s = $value % 60;
  187. $value -= $s;
  188. $value /= 60;
  189. if ($value == 0)
  190. $ret = $s.'s';
  191. else
  192. {
  193. $m = $value % 60;
  194. $value -= $m;
  195. $value /= 60;
  196. if ($value == 0)
  197. $ret = sprintf("%dm$b%02ds", $m, $s);
  198. else
  199. {
  200. $h = $value % 24;
  201. $value -= $h;
  202. $value /= 24;
  203. if ($value == 0)
  204. $ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  205. else
  206. $ret = sprintf("%ddays$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  207. }
  208. }
  209. break;
  210. case 'NOTIFY.Last Well':
  211. if ($value == '0')
  212. {
  213. $ret = 'Never';
  214. $class = $warnclass;
  215. }
  216. else
  217. $ret = date('H:i:s', $value);
  218. break;
  219. case 'NOTIFY.Last Not Well':
  220. if ($value == '0')
  221. $ret = 'Never';
  222. else
  223. {
  224. $ret = date('H:i:s', $value);
  225. $class = $errorclass;
  226. }
  227. break;
  228. case 'NOTIFY.Reason Not Well':
  229. if ($value != 'None')
  230. $class = $errorclass;
  231. break;
  232. case 'GPU.Utility':
  233. case 'PGA.Utility':
  234. case 'SUMMARY.Utility':
  235. $ret = $value.'/m';
  236. break;
  237. case 'GPU.Temperature':
  238. case 'PGA.Temperature':
  239. $ret = $value.'&deg;C';
  240. break;
  241. }
  242. if ($section == 'NOTIFY')
  243. {
  244. $code = preg_split('/ /', $name);
  245. if (count($code) > 1)
  246. if ($code[0] == 'Thread' || $code[0] == 'Dev')
  247. if ($value != '0')
  248. $class = $errorclass;
  249. }
  250. return array($ret, $class);
  251. }
  252. #
  253. global $poolcmd;
  254. $poolcmd = array( 'Switch to' => 'switchpool',
  255. 'Enable' => 'enablepool',
  256. 'Disable' => 'disablepool' );
  257. #
  258. function showhead($cmd, $item, $values)
  259. {
  260. global $poolcmd, $readonly;
  261. echo '<tr>';
  262. foreach ($values as $name => $value)
  263. {
  264. if ($name == '0')
  265. $name = '&nbsp;';
  266. echo "<td valign=bottom class=h>$name</td>";
  267. }
  268. if ($cmd == 'pools' && $readonly === false)
  269. foreach ($poolcmd as $name => $pcmd)
  270. echo "<td valign=bottom class=h>$name</td>";
  271. echo '</tr>';
  272. }
  273. #
  274. function details($cmd, $list)
  275. {
  276. global $poolcmd, $readonly;
  277. $dfmt = 'H:i:s j-M-Y \U\T\CP';
  278. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  279. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  280. $te = '</table></td></tr>';
  281. echo $tb;
  282. echo '<tr><td class=sta>Date: '.date($dfmt).'</td></tr>';
  283. echo $te.$tb;
  284. if (isset($list['STATUS']))
  285. {
  286. echo '<tr>';
  287. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  288. if (isset($list['STATUS']['When']))
  289. echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
  290. $sta = $list['STATUS']['STATUS'];
  291. echo '<td>Status: '.$stas[$sta].'</td>';
  292. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  293. echo '</tr>';
  294. }
  295. $section = '';
  296. foreach ($list as $item => $values)
  297. {
  298. if ($item == 'STATUS')
  299. continue;
  300. $sectionname = preg_replace('/\d/', '', $item);
  301. if ($sectionname != $section)
  302. {
  303. echo $te.$tb;
  304. showhead($cmd, $item, $values);
  305. $section = $sectionname;
  306. }
  307. echo '<tr>';
  308. foreach ($values as $name => $value)
  309. {
  310. list($showvalue, $class) = fmt($section, $name, $value);
  311. echo "<td$class>$showvalue</td>";
  312. }
  313. if ($cmd == 'pools' && $readonly === false)
  314. {
  315. reset($values);
  316. $pool = current($values);
  317. foreach ($poolcmd as $name => $pcmd)
  318. {
  319. echo '<td>';
  320. if ($pool === false)
  321. echo '&nbsp;';
  322. else
  323. {
  324. echo "<input type=button value='Pool $pool'";
  325. echo " onclick='prc(\"$pcmd|$pool\",\"$name Pool $pool\")'>";
  326. }
  327. echo '</td>';
  328. }
  329. }
  330. echo '</tr>';
  331. }
  332. echo $te;
  333. }
  334. #
  335. global $devs;
  336. $devs = null;
  337. #
  338. function gpubuttons($count)
  339. {
  340. global $devs;
  341. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  342. $options = array( 'intensity' => 'Intensity',
  343. 'fan' => 'Fan Percent',
  344. 'engine' => 'GPU Clock',
  345. 'mem' => 'Memory Clock',
  346. 'vddc' => 'GPU Voltage' );
  347. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  348. $te = '</table></td></tr>';
  349. echo $tb.'<tr>';
  350. foreach ($basic as $head)
  351. echo "<td>$head</td>";
  352. foreach ($options as $name => $des)
  353. echo "<td nowrap>$des</td>";
  354. $n = 0;
  355. for ($c = 0; $c < $count; $c++)
  356. {
  357. echo '</tr><tr>';
  358. foreach ($basic as $name)
  359. {
  360. echo '<td>';
  361. if ($name == 'GPU')
  362. echo $c;
  363. else
  364. {
  365. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  366. echo strtolower($name);
  367. echo "|$c\")'>";
  368. }
  369. echo '</td>';
  370. }
  371. foreach ($options as $name => $des)
  372. {
  373. echo '<td>';
  374. if (!isset($devs["GPU$c"][$des]))
  375. echo '&nbsp;';
  376. else
  377. {
  378. $value = $devs["GPU$c"][$des];
  379. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n)'>";
  380. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  381. $n++;
  382. }
  383. echo '</td>';
  384. }
  385. }
  386. echo '</tr>'.$te;
  387. }
  388. #
  389. function processgpus($rd, $ro)
  390. {
  391. global $error;
  392. $gpus = api('gpucount');
  393. if ($error != null)
  394. echo '<tr><td>Error getting GPU count: '.$rd.$error.$ro.'</td></tr>';
  395. else
  396. {
  397. if (!isset($gpus['GPUS']['Count']))
  398. echo '<tr><td>No GPU count returned: '.$rd.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
  399. else
  400. {
  401. $count = $gpus['GPUS']['Count'];
  402. if ($count == 0)
  403. echo '<tr><td>No GPUs</td></tr>';
  404. else
  405. gpubuttons($count);
  406. }
  407. }
  408. }
  409. #
  410. function process($cmds, $rd, $ro)
  411. {
  412. global $error, $devs;
  413. foreach ($cmds as $cmd => $des)
  414. {
  415. $process = api($cmd);
  416. if ($error != null)
  417. {
  418. echo "<tr><td>Error getting $des: ";
  419. echo $rd.$error.$ro.'</td></tr>';
  420. break;
  421. }
  422. else
  423. {
  424. details($cmd, $process);
  425. echo '<tr><td><br><br></td></tr>';
  426. if ($cmd == 'devs')
  427. $devs = $process;
  428. }
  429. }
  430. }
  431. #
  432. function display()
  433. {
  434. global $error, $readonly, $notify;
  435. $error = null;
  436. $rd = '<font color=red><b>';
  437. $ro = '</b></font>';
  438. echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  439. echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
  440. echo "</td><td width=100%>&nbsp;</td><td>";
  441. if ($readonly === false)
  442. echo "<input type=button value='Quit' onclick='prc(\"quit\",\"Quit CGMiner\")'>";
  443. echo "</td></tr></table></td></tr>";
  444. $arg = trim(getparam('arg', true));
  445. if ($arg != null and $arg != '')
  446. process(array($arg => $arg), $rd, $ro);
  447. $cmds = array( 'devs' => 'device list',
  448. 'summary' => 'summary information',
  449. 'pools' => 'pool list');
  450. if ($notify)
  451. $cmds['notify'] = 'device status';
  452. $cmds['config'] = 'cgminer config';
  453. process($cmds, $rd, $ro);
  454. if ($error == null && $readonly === false)
  455. processgpus($rd, $ro);
  456. }
  457. #
  458. htmlhead();
  459. display();
  460. #
  461. ?>
  462. </table></td></tr></table>
  463. </body></html>