miner.php 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. <?php
  2. session_start();
  3. #
  4. global $miner, $port, $readonly, $notify, $rigs, $socktimeoutsec;
  5. global $checklastshare, $hidefields;
  6. #
  7. # Don't touch these 2 - see $rigs below
  8. $miner = null;
  9. $port = null;
  10. #
  11. # Set $readonly to true to force miner.php to be readonly
  12. # Set $readonly to false then it will check BFGMiner 'privileged'
  13. $readonly = false;
  14. #
  15. # Set $notify to false to NOT attempt to display the notify command
  16. # Set $notify to true to attempt to display the notify command
  17. # If your older version of BFGMiner returns an 'Invalid command'
  18. # coz it doesn't have notify - it just shows the error status table
  19. $notify = true;
  20. #
  21. # set $checklastshare to true to do the following checks:
  22. # If a device's last share is 12x expected ago then display as an error
  23. # If a device's last share is 8x expected ago then display as a warning
  24. # If either of the above is true, also display the whole line highlighted
  25. # This assumes shares are 1 difficulty shares
  26. $checklastshare = true;
  27. #
  28. # Set $rigs to an array of your BFGMiner rigs that are running
  29. # format: 'IP:Port' or 'Host:Port'
  30. # If you only have one rig, it will just show the detail of that rig
  31. # If you have more than one rig it will show a summary of all the rigs
  32. # with buttons to show the details of each rig
  33. # e.g. $rigs = array('127.0.0.1:4028','myrig.com:4028');
  34. $rigs = array('127.0.0.1:4028');
  35. #
  36. # This should be OK for most cases
  37. # However, the longer it is the longer you have to wait while php
  38. # hangs if the target cgminer isn't runnning or listening
  39. # Feel free to increase it if your network is very slow
  40. # Also, on some windows PHP, apparently the $usec is ignored
  41. $socktimeoutsec = 10;
  42. #
  43. # List of fields NOT to be displayed
  44. # You can use this to hide data you don't want to see or don't want
  45. # shown on a public web page
  46. # The list of sections are: SUMMARY, POOL, PGA, GPU, NOTIFY, CONFIG
  47. # See the web page for the list of field names (the table headers)
  48. # It is an array of 'SECTION.Field Name' => 1
  49. # This example would hide the slightly more sensitive pool information
  50. #$hidefields = array('POOL.URL' => 1, 'POOL.User' => 1);
  51. $hidefields = array();
  52. #
  53. $here = $_SERVER['PHP_SELF'];
  54. #
  55. global $tablebegin, $tableend, $warnfont, $warnoff, $dfmt;
  56. #
  57. $tablebegin = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  58. $tableend = '</table></td></tr>';
  59. $warnfont = '<font color=red><b>';
  60. $warnoff = '</b></font>';
  61. $dfmt = 'H:i:s j-M-Y \U\T\CP';
  62. #
  63. global $miner_font_family, $miner_font_size;
  64. #
  65. $miner_font_family = 'verdana,arial,sans';
  66. $miner_font_size = '13pt';
  67. #
  68. # This below allows you to put your own settings into a seperate file
  69. # so you don't need to update miner.php with your preferred settings
  70. # every time a new version is released
  71. # Just create the file 'myminer.php' in the same directory as
  72. # 'miner.php' - and put your own settings in there
  73. if (file_exists('myminer.php'))
  74. include_once('myminer.php');
  75. #
  76. # Ensure it is only ever shown once
  77. global $showndate;
  78. $showndate = false;
  79. #
  80. # For summary page to stop retrying failed rigs
  81. global $rigerror;
  82. $rigerror = array();
  83. #
  84. function htmlhead($checkapi)
  85. {
  86. global $miner_font_family, $miner_font_size;
  87. global $error, $readonly, $here;
  88. if ($readonly === false && $checkapi === true)
  89. {
  90. $access = api('privileged');
  91. if ($error != null
  92. || !isset($access['STATUS']['STATUS'])
  93. || $access['STATUS']['STATUS'] != 'S')
  94. $readonly = true;
  95. }
  96. $miner_font = "font-family:$miner_font_family; font-size:$miner_font_size;";
  97. echo "<html><head><title>Mine</title>
  98. <style type='text/css'>
  99. td { color:blue; $miner_font }
  100. td.h { color:blue; $miner_font background:#d0ffff }
  101. td.err { color:black; $miner_font background:#ff3050 }
  102. td.warn { color:black; $miner_font background:#ffb050 }
  103. td.sta { color:green; $miner_font }
  104. td.tot { color:blue; $miner_font background:#fff8f2 }
  105. td.lst { color:blue; $miner_font background:#ffffdd }
  106. </style>
  107. </head><body bgcolor=#ecffff>
  108. <script type='text/javascript'>
  109. function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location=\"$here\"+a}\n";
  110. if ($readonly === false && $checkapi === true)
  111. {
  112. ?>
  113. function prc(a,m){pr('?arg='+a,m)}
  114. function prs(a,r){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+'&rig='+r,m)}
  115. function prs2(a,n,r){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+'&rig='+r,m)}
  116. <?php
  117. }
  118. ?>
  119. </script>
  120. <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
  121. <tr><td align=center valign=top>
  122. <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
  123. <?php
  124. }
  125. #
  126. global $error;
  127. $error = null;
  128. #
  129. function getsock($addr, $port)
  130. {
  131. global $error, $socktimeoutsec;
  132. $socket = null;
  133. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  134. if ($socket === false || $socket === null)
  135. {
  136. $error = socket_strerror(socket_last_error());
  137. $msg = "socket create(TCP) failed";
  138. $error = "ERR: $msg '$error'\n";
  139. return null;
  140. }
  141. // Ignore if this fails since the socket connect may work anyway
  142. // and nothing is gained by aborting if the option cannot be set
  143. // since we don't know in advance if it can connect
  144. socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $socktimeoutsec, 'usec' => 0));
  145. $res = socket_connect($socket, $addr, $port);
  146. if ($res === false)
  147. {
  148. $error = socket_strerror(socket_last_error());
  149. $msg = "socket connect($addr,$port) failed";
  150. $error = "ERR: $msg '$error'\n";
  151. socket_close($socket);
  152. return null;
  153. }
  154. return $socket;
  155. }
  156. #
  157. function readsockline($socket)
  158. {
  159. $line = '';
  160. while (true)
  161. {
  162. $byte = socket_read($socket, 1);
  163. if ($byte === false || $byte === '')
  164. break;
  165. if ($byte === "\0")
  166. break;
  167. $line .= $byte;
  168. }
  169. return $line;
  170. }
  171. #
  172. function api($cmd)
  173. {
  174. global $miner, $port, $hidefields;
  175. $socket = getsock($miner, $port);
  176. if ($socket != null)
  177. {
  178. socket_write($socket, $cmd, strlen($cmd));
  179. $line = readsockline($socket);
  180. socket_close($socket);
  181. if (strlen($line) == 0)
  182. {
  183. $error = "WARN: '$cmd' returned nothing\n";
  184. return $line;
  185. }
  186. # print "$cmd returned '$line'\n";
  187. $data = array();
  188. $objs = explode('|', $line);
  189. foreach ($objs as $obj)
  190. {
  191. if (strlen($obj) > 0)
  192. {
  193. $items = explode(',', $obj);
  194. $item = $items[0];
  195. $id = explode('=', $items[0], 2);
  196. if (count($id) == 1 or !ctype_digit($id[1]))
  197. $name = $id[0];
  198. else
  199. $name = $id[0].$id[1];
  200. if (strlen($name) == 0)
  201. $name = 'null';
  202. $sectionname = preg_replace('/\d/', '', $name);
  203. if (isset($data[$name]))
  204. {
  205. $num = 1;
  206. while (isset($data[$name.$num]))
  207. $num++;
  208. $name .= $num;
  209. }
  210. $counter = 0;
  211. foreach ($items as $item)
  212. {
  213. $id = explode('=', $item, 2);
  214. if (isset($hidefields[$sectionname.'.'.$id[0]]))
  215. continue;
  216. if (count($id) == 2)
  217. $data[$name][$id[0]] = $id[1];
  218. else
  219. $data[$name][$counter] = $id[0];
  220. $counter++;
  221. }
  222. }
  223. }
  224. return $data;
  225. }
  226. return null;
  227. }
  228. #
  229. function getparam($name, $both = false)
  230. {
  231. $a = null;
  232. if (isset($_POST[$name]))
  233. $a = $_POST[$name];
  234. if (($both === true) and ($a === null))
  235. {
  236. if (isset($_GET[$name]))
  237. $a = $_GET[$name];
  238. }
  239. if ($a == '' || $a == null)
  240. return null;
  241. // limit to 1K just to be safe
  242. return substr($a, 0, 1024);
  243. }
  244. #
  245. function classlastshare($when, $alldata, $warnclass, $errorclass)
  246. {
  247. global $checklastshare;
  248. if ($checklastshare === false)
  249. return '';
  250. if ($when == 0)
  251. return '';
  252. if (!isset($alldata['MHS av']))
  253. return '';
  254. if (!isset($alldata['Last Share Time']))
  255. return '';
  256. $expected = pow(2, 32) / ($alldata['MHS av'] * pow(10, 6));
  257. $howlong = $when - $alldata['Last Share Time'];
  258. if ($howlong < 1)
  259. $howlong = 1;
  260. if ($howlong > ($expected * 12))
  261. return $errorclass;
  262. if ($howlong > ($expected * 8))
  263. return $warnclass;
  264. return '';
  265. }
  266. #
  267. function fmt($section, $name, $value, $when, $alldata)
  268. {
  269. global $dfmt;
  270. if ($alldata == null)
  271. $alldata = array();
  272. $errorclass = ' class=err';
  273. $warnclass = ' class=warn';
  274. $lstclass = ' class=lst';
  275. $b = '&nbsp;';
  276. $ret = $value;
  277. $class = '';
  278. if ($value === null)
  279. $ret = $b;
  280. else
  281. switch ($section.'.'.$name)
  282. {
  283. case 'GPU.Last Share Time':
  284. case 'PGA.Last Share Time':
  285. if ($value == 0
  286. || (isset($alldata['Last Share Pool']) && $alldata['Last Share Pool'] == -1))
  287. {
  288. $ret = 'Never';
  289. $class = $warnclass;
  290. }
  291. else
  292. {
  293. $ret = date('H:i:s', $value);
  294. $class = classlastshare($when, $alldata, $warnclass, $errorclass);
  295. }
  296. break;
  297. case 'POOL.Last Share Time':
  298. if ($value == 0)
  299. $ret = 'Never';
  300. else
  301. $ret = date('H:i:s d-M', $value);
  302. break;
  303. case 'GPU.Last Share Pool':
  304. case 'PGA.Last Share Pool':
  305. if ($value == -1)
  306. {
  307. $ret = 'None';
  308. $class = $warnclass;
  309. }
  310. break;
  311. case 'SUMMARY.Elapsed':
  312. $s = $value % 60;
  313. $value -= $s;
  314. $value /= 60;
  315. if ($value == 0)
  316. $ret = $s.'s';
  317. else
  318. {
  319. $m = $value % 60;
  320. $value -= $m;
  321. $value /= 60;
  322. if ($value == 0)
  323. $ret = sprintf("%dm$b%02ds", $m, $s);
  324. else
  325. {
  326. $h = $value % 24;
  327. $value -= $h;
  328. $value /= 24;
  329. if ($value == 0)
  330. $ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  331. else
  332. {
  333. if ($value == 1)
  334. $days = '';
  335. else
  336. $days = 's';
  337. $ret = sprintf("%dday$days$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  338. }
  339. }
  340. }
  341. break;
  342. case 'NOTIFY.Last Well':
  343. if ($value == '0')
  344. {
  345. $ret = 'Never';
  346. $class = $warnclass;
  347. }
  348. else
  349. $ret = date('H:i:s', $value);
  350. break;
  351. case 'NOTIFY.Last Not Well':
  352. if ($value == '0')
  353. $ret = 'Never';
  354. else
  355. {
  356. $ret = date('H:i:s', $value);
  357. $class = $errorclass;
  358. }
  359. break;
  360. case 'NOTIFY.Reason Not Well':
  361. if ($value != 'None')
  362. $class = $errorclass;
  363. break;
  364. case 'GPU.Utility':
  365. case 'PGA.Utility':
  366. case 'SUMMARY.Utility':
  367. $ret = $value.'/m';
  368. if ($value == 0)
  369. $class = $warnclass;
  370. break;
  371. case 'PGA.Temperature':
  372. $ret = $value.'&deg;C';
  373. break;
  374. case 'GPU.Temperature':
  375. $ret = $value.'&deg;C';
  376. case 'GPU.GPU Clock':
  377. case 'GPU.Memory Clock':
  378. case 'GPU.GPU Voltage':
  379. case 'GPU.GPU Activity':
  380. if ($value == 0)
  381. $class = $warnclass;
  382. break;
  383. case 'GPU.Fan Percent':
  384. if ($value == 0)
  385. $class = $warnclass;
  386. else
  387. {
  388. if ($value == 100)
  389. $class = $errorclass;
  390. else
  391. if ($value > 85)
  392. $class = $warnclass;
  393. }
  394. break;
  395. case 'GPU.Fan Speed':
  396. if ($value == 0)
  397. $class = $warnclass;
  398. else
  399. if (isset($alldata['Fan Percent']))
  400. {
  401. $test = $alldata['Fan Percent'];
  402. if ($test == 100)
  403. $class = $errorclass;
  404. else
  405. if ($test > 85)
  406. $class = $warnclass;
  407. }
  408. break;
  409. case 'GPU.MHS av':
  410. case 'PGA.MHS av':
  411. case 'SUMMARY.MHS av':
  412. case 'GPU.Total MH':
  413. case 'PGA.Total MH':
  414. case 'SUMMARY.Total MH':
  415. case 'SUMMARY.Getworks':
  416. case 'GPU.Accepted':
  417. case 'PGA.Accepted':
  418. case 'SUMMARY.Accepted':
  419. case 'GPU.Rejected':
  420. case 'PGA.Rejected':
  421. case 'SUMMARY.Rejected':
  422. case 'SUMMARY.Local Work':
  423. case 'POOL.Getworks':
  424. case 'POOL.Accepted':
  425. case 'POOL.Rejected':
  426. case 'POOL.Discarded':
  427. $parts = explode('.', $value, 2);
  428. if (count($parts) == 1)
  429. $dec = '';
  430. else
  431. $dec = '.'.$parts[1];
  432. $ret = number_format($parts[0]).$dec;
  433. break;
  434. case 'GPU.Status':
  435. case 'PGA.Status':
  436. case 'POOL.Status':
  437. if ($value != 'Alive')
  438. $class = $errorclass;
  439. break;
  440. case 'GPU.Enabled':
  441. case 'PGA.Enabled':
  442. if ($value != 'Y')
  443. $class = $warnclass;
  444. break;
  445. case 'STATUS.When':
  446. $ret = date($dfmt, $value);
  447. break;
  448. }
  449. if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
  450. $class = $errorclass;
  451. if ($class == '' && $section != 'POOL')
  452. $class = classlastshare($when, $alldata, $lstclass, $lstclass);
  453. return array($ret, $class);
  454. }
  455. #
  456. global $poolcmd;
  457. $poolcmd = array( 'Switch to' => 'switchpool',
  458. 'Enable' => 'enablepool',
  459. 'Disable' => 'disablepool' );
  460. #
  461. function showhead($cmd, $item, $values)
  462. {
  463. global $poolcmd, $readonly;
  464. echo '<tr>';
  465. foreach ($values as $name => $value)
  466. {
  467. if ($name == '0' or $name == '')
  468. $name = '&nbsp;';
  469. echo "<td valign=bottom class=h>$name</td>";
  470. }
  471. if ($cmd == 'pools' && $readonly === false)
  472. foreach ($poolcmd as $name => $pcmd)
  473. echo "<td valign=bottom class=h>$name</td>";
  474. echo '</tr>';
  475. }
  476. #
  477. function details($cmd, $list, $rig)
  478. {
  479. global $tablebegin, $tableend, $dfmt;
  480. global $poolcmd, $readonly;
  481. global $showndate;
  482. $when = 0;
  483. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  484. echo $tablebegin;
  485. if ($showndate === false)
  486. {
  487. echo '<tr><td class=sta>Date: '.date($dfmt).'</td></tr>';
  488. echo $tableend.$tablebegin;
  489. $showndate = true;
  490. }
  491. if (isset($list['STATUS']))
  492. {
  493. echo '<tr>';
  494. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  495. if (isset($list['STATUS']['When']))
  496. {
  497. echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
  498. $when = $list['STATUS']['When'];
  499. }
  500. $sta = $list['STATUS']['STATUS'];
  501. echo '<td>Status: '.$stas[$sta].'</td>';
  502. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  503. echo '</tr>';
  504. }
  505. $section = '';
  506. foreach ($list as $item => $values)
  507. {
  508. if ($item == 'STATUS')
  509. continue;
  510. $sectionname = preg_replace('/\d/', '', $item);
  511. if ($sectionname != $section)
  512. {
  513. echo $tableend.$tablebegin;
  514. showhead($cmd, $item, $values);
  515. $section = $sectionname;
  516. }
  517. echo '<tr>';
  518. foreach ($values as $name => $value)
  519. {
  520. list($showvalue, $class) = fmt($section, $name, $value, $when, $values);
  521. echo "<td$class>$showvalue</td>";
  522. }
  523. if ($cmd == 'pools' && $readonly === false)
  524. {
  525. reset($values);
  526. $pool = current($values);
  527. foreach ($poolcmd as $name => $pcmd)
  528. {
  529. echo '<td>';
  530. if ($pool === false)
  531. echo '&nbsp;';
  532. else
  533. {
  534. echo "<input type=button value='Pool $pool'";
  535. echo " onclick='prc(\"$pcmd|$pool&rig=$rig\",\"$name Pool $pool\")'>";
  536. }
  537. echo '</td>';
  538. }
  539. }
  540. echo '</tr>';
  541. }
  542. echo $tableend;
  543. }
  544. #
  545. global $devs;
  546. $devs = null;
  547. #
  548. function gpubuttons($count, $rig)
  549. {
  550. global $tablebegin, $tableend;
  551. global $devs;
  552. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  553. $options = array( 'intensity' => 'Intensity',
  554. 'fan' => 'Fan Percent',
  555. 'engine' => 'GPU Clock',
  556. 'mem' => 'Memory Clock',
  557. 'vddc' => 'GPU Voltage' );
  558. echo $tablebegin.'<tr>';
  559. foreach ($basic as $head)
  560. echo "<td>$head</td>";
  561. foreach ($options as $name => $des)
  562. echo "<td nowrap>$des</td>";
  563. $n = 0;
  564. for ($c = 0; $c < $count; $c++)
  565. {
  566. echo '</tr><tr>';
  567. foreach ($basic as $name)
  568. {
  569. echo '<td>';
  570. if ($name == 'GPU')
  571. echo $c;
  572. else
  573. {
  574. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  575. echo strtolower($name);
  576. echo "|$c\",$rig)'>";
  577. }
  578. echo '</td>';
  579. }
  580. foreach ($options as $name => $des)
  581. {
  582. echo '<td>';
  583. if (!isset($devs["GPU$c"][$des]))
  584. echo '&nbsp;';
  585. else
  586. {
  587. $value = $devs["GPU$c"][$des];
  588. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n,$rig)'>";
  589. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  590. $n++;
  591. }
  592. echo '</td>';
  593. }
  594. }
  595. echo '</tr>'.$tableend;
  596. }
  597. #
  598. function processgpus($rig)
  599. {
  600. global $error;
  601. global $warnfont, $warnoff;
  602. $gpus = api('gpucount');
  603. if ($error != null)
  604. echo '<tr><td>Error getting GPU count: '.$warnfont.$error.$warnoff.'</td></tr>';
  605. else
  606. {
  607. if (!isset($gpus['GPUS']['Count']))
  608. echo '<tr><td>No GPU count returned: '.$warnfont.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
  609. else
  610. {
  611. $count = $gpus['GPUS']['Count'];
  612. if ($count == 0)
  613. echo '<tr><td>No GPUs</td></tr>';
  614. else
  615. gpubuttons($count, $rig);
  616. }
  617. }
  618. }
  619. #
  620. function process($cmds, $rig)
  621. {
  622. global $error, $devs;
  623. global $warnfont, $warnoff;
  624. foreach ($cmds as $cmd => $des)
  625. {
  626. $process = api($cmd);
  627. if ($error != null)
  628. {
  629. echo "<tr><td colspan=100>Error getting $des: ";
  630. echo $warnfont.$error.$warnoff.'</td></tr>';
  631. break;
  632. }
  633. else
  634. {
  635. details($cmd, $process, $rig);
  636. echo '<tr><td><br><br></td></tr>';
  637. if ($cmd == 'devs')
  638. $devs = $process;
  639. }
  640. }
  641. }
  642. #
  643. # $head is a hack but this is just a demo anyway :)
  644. function doforeach($cmd, $des, $sum, $head, $datetime)
  645. {
  646. global $miner, $port;
  647. global $error, $readonly, $notify, $rigs;
  648. global $tablebegin, $tableend, $warnfont, $warnoff, $dfmt;
  649. global $rigerror;
  650. $when = 0;
  651. $header = $head;
  652. $anss = array();
  653. $count = 0;
  654. $preverr = count($rigerror);
  655. foreach ($rigs as $rig)
  656. {
  657. if (isset($rigerror[$rig]))
  658. continue;
  659. $parts = explode(':', $rig, 2);
  660. if (count($parts) == 2)
  661. {
  662. $miner = $parts[0];
  663. $port = $parts[1];
  664. $ans = api($cmd);
  665. if ($error != null)
  666. {
  667. echo "<tr><td colspan=100>Error on rig $count getting $des: ";
  668. echo $warnfont.$error.$warnoff.'</td></tr>';
  669. $rigerror[$rig] = $error;
  670. $error = null;
  671. }
  672. else
  673. $anss[$count] = $ans;
  674. }
  675. $count++;
  676. }
  677. if (count($anss) == 0)
  678. {
  679. echo '<tr><td>Failed to access any rigs successfully';
  680. if ($preverr > 0)
  681. echo ' (or rigs had previous errors)';
  682. echo '</td></tr>';
  683. return;
  684. }
  685. if ($datetime)
  686. {
  687. echo '<tr><td class=sta>Date: '.date($dfmt).'</td></tr>';
  688. echo $tableend.$tablebegin;
  689. $dthead = array('' => 1, 'STATUS' => 1, 'Description' => 1, 'When' => 1, 'API' => 1, 'CGMiner' => 1);
  690. showhead('', null, $dthead);
  691. foreach ($anss as $rig => $ans)
  692. {
  693. echo '<tr>';
  694. foreach ($ans as $item => $row)
  695. {
  696. if ($item != 'STATUS' && $item != 'VERSION')
  697. continue;
  698. foreach ($dthead as $name => $x)
  699. {
  700. if ($item == 'STATUS' && $name == '')
  701. echo "<td align=right><input type=button value='Rig $rig' onclick='pr(\"?rig=$rig\",null)'></td>";
  702. else
  703. {
  704. if (isset($row[$name]))
  705. {
  706. list($showvalue, $class) = fmt('STATUS', $name, $row[$name], $when, null);
  707. echo "<td$class align=right>$showvalue</td>";
  708. }
  709. }
  710. }
  711. }
  712. echo '</tr>';
  713. }
  714. echo $tableend;
  715. echo '<tr><td><br><br></td></tr>';
  716. echo $tablebegin;
  717. return;
  718. }
  719. $total = array();
  720. foreach ($anss as $rig => $ans)
  721. {
  722. foreach ($ans as $item => $row)
  723. {
  724. if ($item == 'STATUS')
  725. continue;
  726. if (count($row) > count($header))
  727. {
  728. $header = $head;
  729. foreach ($row as $name => $value)
  730. if (!isset($header[$name]))
  731. $header[$name] = '';
  732. }
  733. if ($sum != null)
  734. foreach ($sum as $name)
  735. {
  736. if (isset($row[$name]))
  737. {
  738. if (isset($total[$name]))
  739. $total[$name] += $row[$name];
  740. else
  741. $total[$name] = $row[$name];
  742. }
  743. }
  744. }
  745. }
  746. if ($sum != null)
  747. $anss['total']['total'] = $total;
  748. showhead('', null, $header);
  749. $section = '';
  750. foreach ($anss as $rig => $ans)
  751. {
  752. $when = 0;
  753. if (isset($ans['STATUS']['When']))
  754. $when = $ans['STATUS']['When'];
  755. foreach ($ans as $item => $row)
  756. {
  757. if ($item == 'STATUS')
  758. continue;
  759. echo '<tr>';
  760. $newsection = preg_replace('/\d/', '', $item);
  761. if ($newsection != 'total')
  762. $section = $newsection;
  763. foreach ($header as $name => $x)
  764. {
  765. if ($name == '')
  766. {
  767. if ($rig === 'total')
  768. echo "<td align=right class=tot>Total:</td>";
  769. else
  770. echo "<td align=right><input type=button value='Rig $rig' onclick='pr(\"?rig=$rig\",null)'></td>";
  771. }
  772. else
  773. {
  774. if (isset($row[$name]))
  775. $value = $row[$name];
  776. else
  777. $value = null;
  778. list($showvalue, $class) = fmt($section, $name, $value, $when, $row);
  779. if ($rig === 'total' and $class == '')
  780. $class = ' class=tot';
  781. echo "<td$class align=right>$showvalue</td>";
  782. }
  783. }
  784. echo '</tr>';
  785. }
  786. }
  787. }
  788. #
  789. function doOne($rig, $preprocess)
  790. {
  791. global $error, $readonly, $notify;
  792. global $rigs;
  793. htmlhead(true);
  794. $error = null;
  795. echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  796. echo "<input type=button value='Refresh' onclick='pr(\"?rig=$rig\",null)'></td>";
  797. if (count($rigs) > 1)
  798. echo "<td><input type=button value='Summary' onclick='pr(\"\",null)'></td>";
  799. echo "<td width=100%>&nbsp;</td><td>";
  800. if ($readonly === false)
  801. {
  802. $msg = 'Quit BFGMiner';
  803. if (count($rigs) > 1)
  804. $msg .= " Rig $rig";
  805. echo "<input type=button value='Quit' onclick='prc(\"quit&rig=$rig\",\"$msg\")'>";
  806. }
  807. echo "</td></tr></table></td></tr>";
  808. if ($preprocess != null)
  809. process(array($preprocess => $preprocess), $rig);
  810. $cmds = array( 'devs' => 'device list',
  811. 'summary' => 'summary information',
  812. 'pools' => 'pool list');
  813. if ($notify)
  814. $cmds['notify'] = 'device status';
  815. $cmds['config'] = 'BFGMiner config';
  816. process($cmds, $rig);
  817. if ($error == null && $readonly === false)
  818. processgpus($rig);
  819. }
  820. #
  821. function display()
  822. {
  823. global $tablebegin, $tableend;
  824. global $miner, $port;
  825. global $error, $readonly, $notify, $rigs;
  826. $rig = trim(getparam('rig', true));
  827. $arg = trim(getparam('arg', true));
  828. $preprocess = null;
  829. if ($arg != null and $arg != '')
  830. {
  831. $num = null;
  832. if ($rig != null and $rig != '')
  833. {
  834. if ($rig >= 0 and $rig < count($rigs))
  835. $num = $rig;
  836. }
  837. else
  838. if (count($rigs) == 0)
  839. $num = 0;
  840. if ($num != null)
  841. {
  842. $parts = explode(':', $rigs[$num], 2);
  843. if (count($parts) == 2)
  844. {
  845. $miner = $parts[0];
  846. $port = $parts[1];
  847. $preprocess = $arg;
  848. }
  849. }
  850. }
  851. if ($rigs == null or count($rigs) == 0)
  852. {
  853. echo "<tr><td>No rigs defined</td></tr>";
  854. return;
  855. }
  856. if (count($rigs) == 1)
  857. {
  858. $parts = explode(':', $rigs[0], 2);
  859. if (count($parts) == 2)
  860. {
  861. $miner = $parts[0];
  862. $port = $parts[1];
  863. doOne(0, $preprocess);
  864. }
  865. else
  866. echo '<tr><td>Invalid "$rigs" array</td></tr>';
  867. return;
  868. }
  869. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  870. {
  871. $parts = explode(':', $rigs[$rig], 2);
  872. if (count($parts) == 2)
  873. {
  874. $miner = $parts[0];
  875. $port = $parts[1];
  876. doOne($rig, $preprocess);
  877. }
  878. else
  879. echo '<tr><td>Invalid "$rigs" array</td></tr>';
  880. return;
  881. }
  882. htmlhead(false);
  883. echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  884. echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
  885. echo "</td></tr></table></td></tr>";
  886. if ($preprocess != null)
  887. process(array($preprocess => $preprocess), $rig);
  888. echo $tablebegin;
  889. doforeach('version', 'rig summary', array(), array(), true);
  890. $sum = array('MHS av', 'Getworks', 'Found Blocks', 'Accepted', 'Rejected', 'Discarded', 'Stale', 'Utility', 'Local Work', 'Total MH');
  891. doforeach('summary', 'summary information', $sum, array(), false);
  892. echo $tableend;
  893. echo '<tr><td><br><br></td></tr>';
  894. echo $tablebegin;
  895. doforeach('devs', 'device list', $sum, array(''=>'','ID'=>'','Name'=>''), false);
  896. echo $tableend;
  897. echo '<tr><td><br><br></td></tr>';
  898. echo $tablebegin;
  899. doforeach('pools', 'pool list', $sum, array(''=>''), false);
  900. echo $tableend;
  901. }
  902. #
  903. display();
  904. #
  905. ?>
  906. </table></td></tr></table>
  907. </body></html>