miner.php 24 KB

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