miner.php 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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 cgminer '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 cgminer 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 cgminer 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 nowrap>";
  800. if ($readonly === false)
  801. {
  802. $rg = '';
  803. if (count($rigs) > 1)
  804. $rg .= " Rig $rig";
  805. echo "<input type=button value='Restart' onclick='prc(\"restart&rig=$rig\",\"Restart CGMiner$rg\")'>";
  806. echo "&nbsp;<input type=button value='Quit' onclick='prc(\"quit&rig=$rig\",\"Quit CGMiner$rg\")'>";
  807. }
  808. echo "</td></tr></table></td></tr>";
  809. if ($preprocess != null)
  810. process(array($preprocess => $preprocess), $rig);
  811. $cmds = array( 'devs' => 'device list',
  812. 'summary' => 'summary information',
  813. 'pools' => 'pool list');
  814. if ($notify)
  815. $cmds['notify'] = 'device status';
  816. $cmds['config'] = 'cgminer config';
  817. process($cmds, $rig);
  818. if ($error == null && $readonly === false)
  819. processgpus($rig);
  820. }
  821. #
  822. function display()
  823. {
  824. global $tablebegin, $tableend;
  825. global $miner, $port;
  826. global $error, $readonly, $notify, $rigs;
  827. $rig = trim(getparam('rig', true));
  828. $arg = trim(getparam('arg', true));
  829. $preprocess = null;
  830. if ($arg != null and $arg != '')
  831. {
  832. $num = null;
  833. if ($rig != null and $rig != '')
  834. {
  835. if ($rig >= 0 and $rig < count($rigs))
  836. $num = $rig;
  837. }
  838. else
  839. if (count($rigs) == 0)
  840. $num = 0;
  841. if ($num != null)
  842. {
  843. $parts = explode(':', $rigs[$num], 2);
  844. if (count($parts) == 2)
  845. {
  846. $miner = $parts[0];
  847. $port = $parts[1];
  848. $preprocess = $arg;
  849. }
  850. }
  851. }
  852. if ($rigs == null or count($rigs) == 0)
  853. {
  854. echo "<tr><td>No rigs defined</td></tr>";
  855. return;
  856. }
  857. if (count($rigs) == 1)
  858. {
  859. $parts = explode(':', $rigs[0], 2);
  860. if (count($parts) == 2)
  861. {
  862. $miner = $parts[0];
  863. $port = $parts[1];
  864. doOne(0, $preprocess);
  865. }
  866. else
  867. echo '<tr><td>Invalid "$rigs" array</td></tr>';
  868. return;
  869. }
  870. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  871. {
  872. $parts = explode(':', $rigs[$rig], 2);
  873. if (count($parts) == 2)
  874. {
  875. $miner = $parts[0];
  876. $port = $parts[1];
  877. doOne($rig, $preprocess);
  878. }
  879. else
  880. echo '<tr><td>Invalid "$rigs" array</td></tr>';
  881. return;
  882. }
  883. htmlhead(false);
  884. echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  885. echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
  886. echo "</td></tr></table></td></tr>";
  887. if ($preprocess != null)
  888. process(array($preprocess => $preprocess), $rig);
  889. echo $tablebegin;
  890. doforeach('version', 'rig summary', array(), array(), true);
  891. $sum = array('MHS av', 'Getworks', 'Found Blocks', 'Accepted', 'Rejected', 'Discarded', 'Stale', 'Utility', 'Local Work', 'Total MH');
  892. doforeach('summary', 'summary information', $sum, array(), false);
  893. echo $tableend;
  894. echo '<tr><td><br><br></td></tr>';
  895. echo $tablebegin;
  896. doforeach('devs', 'device list', $sum, array(''=>'','ID'=>'','Name'=>''), false);
  897. echo $tableend;
  898. echo '<tr><td><br><br></td></tr>';
  899. echo $tablebegin;
  900. doforeach('pools', 'pool list', $sum, array(''=>''), false);
  901. echo $tableend;
  902. }
  903. #
  904. display();
  905. #
  906. ?>
  907. </table></td></tr></table>
  908. </body></html>