miner.php 30 KB

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