miner.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. td.hi { color:blue; $miner_font background:#99ff99 }
  159. td.lo { color:blue; $miner_font background:#ff9999 }
  160. </style>
  161. </head><body bgcolor=#ecffff>
  162. <script type='text/javascript'>
  163. function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location='$here?ref=$autorefresh'+a}\n";
  164. if ($ignorerefresh == false)
  165. echo "function prr(a){if(a){v=document.getElementById('refval').value}else{v=0}window.location='$here?ref='+v+'$extraparams'}\n";
  166. if ($readonly === false && $checkapi === true)
  167. {
  168. echo "function prc(a,m){pr('&arg='+a,m)}
  169. 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)}
  170. 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";
  171. }
  172. ?>
  173. </script>
  174. <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
  175. <tr><td align=center valign=top>
  176. <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
  177. <?php
  178. }
  179. #
  180. global $haderror, $error;
  181. $haderror = false;
  182. $error = null;
  183. #
  184. function getsock($addr, $port)
  185. {
  186. global $haderror, $error, $socktimeoutsec;
  187. $error = null;
  188. $socket = null;
  189. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  190. if ($socket === false || $socket === null)
  191. {
  192. $haderror = true;
  193. $error = socket_strerror(socket_last_error());
  194. $msg = "socket create(TCP) failed";
  195. $error = "ERR: $msg '$error'\n";
  196. return null;
  197. }
  198. // Ignore if this fails since the socket connect may work anyway
  199. // and nothing is gained by aborting if the option cannot be set
  200. // since we don't know in advance if it can connect
  201. socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $socktimeoutsec, 'usec' => 0));
  202. $res = socket_connect($socket, $addr, $port);
  203. if ($res === false)
  204. {
  205. $haderror = true;
  206. $error = socket_strerror(socket_last_error());
  207. $msg = "socket connect($addr,$port) failed";
  208. $error = "ERR: $msg '$error'\n";
  209. socket_close($socket);
  210. return null;
  211. }
  212. return $socket;
  213. }
  214. #
  215. function readsockline($socket)
  216. {
  217. $line = '';
  218. while (true)
  219. {
  220. $byte = socket_read($socket, 1);
  221. if ($byte === false || $byte === '')
  222. break;
  223. if ($byte === "\0")
  224. break;
  225. $line .= $byte;
  226. }
  227. return $line;
  228. }
  229. #
  230. function api($cmd)
  231. {
  232. global $haderror, $error;
  233. global $miner, $port, $hidefields;
  234. $socket = getsock($miner, $port);
  235. if ($socket != null)
  236. {
  237. socket_write($socket, $cmd, strlen($cmd));
  238. $line = readsockline($socket);
  239. socket_close($socket);
  240. if (strlen($line) == 0)
  241. {
  242. $haderror = true;
  243. $error = "WARN: '$cmd' returned nothing\n";
  244. return $line;
  245. }
  246. # print "$cmd returned '$line'\n";
  247. $data = array();
  248. $objs = explode('|', $line);
  249. foreach ($objs as $obj)
  250. {
  251. if (strlen($obj) > 0)
  252. {
  253. $items = explode(',', $obj);
  254. $item = $items[0];
  255. $id = explode('=', $items[0], 2);
  256. if (count($id) == 1 or !ctype_digit($id[1]))
  257. $name = $id[0];
  258. else
  259. $name = $id[0].$id[1];
  260. if (strlen($name) == 0)
  261. $name = 'null';
  262. $sectionname = preg_replace('/\d/', '', $name);
  263. if (isset($data[$name]))
  264. {
  265. $num = 1;
  266. while (isset($data[$name.$num]))
  267. $num++;
  268. $name .= $num;
  269. }
  270. $counter = 0;
  271. foreach ($items as $item)
  272. {
  273. $id = explode('=', $item, 2);
  274. if (isset($hidefields[$sectionname.'.'.$id[0]]))
  275. continue;
  276. if (count($id) == 2)
  277. $data[$name][$id[0]] = $id[1];
  278. else
  279. $data[$name][$counter] = $id[0];
  280. $counter++;
  281. }
  282. }
  283. }
  284. return $data;
  285. }
  286. return null;
  287. }
  288. #
  289. function getparam($name, $both = false)
  290. {
  291. $a = null;
  292. if (isset($_POST[$name]))
  293. $a = $_POST[$name];
  294. if (($both === true) and ($a === null))
  295. {
  296. if (isset($_GET[$name]))
  297. $a = $_GET[$name];
  298. }
  299. if ($a == '' || $a == null)
  300. return null;
  301. // limit to 1K just to be safe
  302. return substr($a, 0, 1024);
  303. }
  304. #
  305. function classlastshare($when, $alldata, $warnclass, $errorclass)
  306. {
  307. global $checklastshare;
  308. if ($checklastshare === false)
  309. return '';
  310. if ($when == 0)
  311. return '';
  312. if (!isset($alldata['MHS av']))
  313. return '';
  314. if (!isset($alldata['Last Share Time']))
  315. return '';
  316. $expected = pow(2, 32) / ($alldata['MHS av'] * pow(10, 6));
  317. $howlong = $when - $alldata['Last Share Time'];
  318. if ($howlong < 1)
  319. $howlong = 1;
  320. if ($howlong > ($expected * 12))
  321. return $errorclass;
  322. if ($howlong > ($expected * 8))
  323. return $warnclass;
  324. return '';
  325. }
  326. #
  327. function fmt($section, $name, $value, $when, $alldata)
  328. {
  329. global $dfmt;
  330. if ($alldata == null)
  331. $alldata = array();
  332. $errorclass = ' class=err';
  333. $warnclass = ' class=warn';
  334. $lstclass = ' class=lst';
  335. $hiclass = ' class=hi';
  336. $loclass = ' class=lo';
  337. $b = '&nbsp;';
  338. $ret = $value;
  339. $class = '';
  340. if ($value === null)
  341. $ret = $b;
  342. else
  343. switch ($section.'.'.$name)
  344. {
  345. case 'GPU.Last Share Time':
  346. case 'PGA.Last Share Time':
  347. if ($value == 0
  348. || (isset($alldata['Last Share Pool']) && $alldata['Last Share Pool'] == -1))
  349. {
  350. $ret = 'Never';
  351. $class = $warnclass;
  352. }
  353. else
  354. {
  355. $ret = date('H:i:s', $value);
  356. $class = classlastshare($when, $alldata, $warnclass, $errorclass);
  357. }
  358. break;
  359. case 'POOL.Last Share Time':
  360. if ($value == 0)
  361. $ret = 'Never';
  362. else
  363. $ret = date('H:i:s d-M', $value);
  364. break;
  365. case 'GPU.Last Share Pool':
  366. case 'PGA.Last Share Pool':
  367. if ($value == -1)
  368. {
  369. $ret = 'None';
  370. $class = $warnclass;
  371. }
  372. break;
  373. case 'SUMMARY.Elapsed':
  374. $s = $value % 60;
  375. $value -= $s;
  376. $value /= 60;
  377. if ($value == 0)
  378. $ret = $s.'s';
  379. else
  380. {
  381. $m = $value % 60;
  382. $value -= $m;
  383. $value /= 60;
  384. if ($value == 0)
  385. $ret = sprintf("%dm$b%02ds", $m, $s);
  386. else
  387. {
  388. $h = $value % 24;
  389. $value -= $h;
  390. $value /= 24;
  391. if ($value == 0)
  392. $ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  393. else
  394. {
  395. if ($value == 1)
  396. $days = '';
  397. else
  398. $days = 's';
  399. $ret = sprintf("%dday$days$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  400. }
  401. }
  402. }
  403. break;
  404. case 'NOTIFY.Last Well':
  405. if ($value == '0')
  406. {
  407. $ret = 'Never';
  408. $class = $warnclass;
  409. }
  410. else
  411. $ret = date('H:i:s', $value);
  412. break;
  413. case 'NOTIFY.Last Not Well':
  414. if ($value == '0')
  415. $ret = 'Never';
  416. else
  417. {
  418. $ret = date('H:i:s', $value);
  419. $class = $errorclass;
  420. }
  421. break;
  422. case 'NOTIFY.Reason Not Well':
  423. if ($value != 'None')
  424. $class = $errorclass;
  425. break;
  426. case 'GPU.Utility':
  427. case 'PGA.Utility':
  428. case 'SUMMARY.Utility':
  429. $ret = $value.'/m';
  430. if ($value == 0)
  431. $class = $errorclass;
  432. else
  433. if (isset($alldata['MHS av']))
  434. {
  435. $expected = 60 * $alldata['MHS av'] * (pow(10, 6) / pow(2, 32));
  436. if ($expected == 0)
  437. $expected = 0.000001; // 1 H/s
  438. $ratio = $value / $expected;
  439. if ($ratio < 0.9)
  440. $class = $loclass;
  441. else
  442. if ($ratio > 1.1)
  443. $class = $hiclass;
  444. }
  445. break;
  446. case 'PGA.Temperature':
  447. $ret = $value.'&deg;C';
  448. break;
  449. case 'GPU.Temperature':
  450. $ret = $value.'&deg;C';
  451. case 'GPU.GPU Clock':
  452. case 'GPU.Memory Clock':
  453. case 'GPU.GPU Voltage':
  454. case 'GPU.GPU Activity':
  455. if ($value == 0)
  456. $class = $warnclass;
  457. break;
  458. case 'GPU.Fan Percent':
  459. if ($value == 0)
  460. $class = $warnclass;
  461. else
  462. {
  463. if ($value == 100)
  464. $class = $errorclass;
  465. else
  466. if ($value > 85)
  467. $class = $warnclass;
  468. }
  469. break;
  470. case 'GPU.Fan Speed':
  471. if ($value == 0)
  472. $class = $warnclass;
  473. else
  474. if (isset($alldata['Fan Percent']))
  475. {
  476. $test = $alldata['Fan Percent'];
  477. if ($test == 100)
  478. $class = $errorclass;
  479. else
  480. if ($test > 85)
  481. $class = $warnclass;
  482. }
  483. break;
  484. case 'GPU.MHS av':
  485. case 'PGA.MHS av':
  486. case 'SUMMARY.MHS av':
  487. $parts = explode('.', $value, 2);
  488. if (count($parts) == 1)
  489. $dec = '';
  490. else
  491. $dec = '.'.$parts[1];
  492. $ret = number_format($parts[0]).$dec;
  493. if ($value == 0)
  494. $class = $errorclass;
  495. else
  496. if (isset($alldata['Utility']))
  497. {
  498. $expected = 60 * $value * (pow(10, 6) / pow(2, 32));
  499. $utility = $alldata['Utility'];
  500. $ratio = $utility / $expected;
  501. if ($ratio < 0.9)
  502. $class = $hiclass;
  503. else
  504. if ($ratio > 1.1)
  505. $class = $loclass;
  506. }
  507. break;
  508. case 'GPU.Total MH':
  509. case 'PGA.Total MH':
  510. case 'SUMMARY.Total MH':
  511. case 'SUMMARY.Getworks':
  512. case 'GPU.Accepted':
  513. case 'PGA.Accepted':
  514. case 'SUMMARY.Accepted':
  515. case 'GPU.Rejected':
  516. case 'PGA.Rejected':
  517. case 'SUMMARY.Rejected':
  518. case 'SUMMARY.Local Work':
  519. case 'POOL.Getworks':
  520. case 'POOL.Accepted':
  521. case 'POOL.Rejected':
  522. case 'POOL.Discarded':
  523. $parts = explode('.', $value, 2);
  524. if (count($parts) == 1)
  525. $dec = '';
  526. else
  527. $dec = '.'.$parts[1];
  528. $ret = number_format($parts[0]).$dec;
  529. break;
  530. case 'GPU.Status':
  531. case 'PGA.Status':
  532. case 'POOL.Status':
  533. if ($value != 'Alive')
  534. $class = $errorclass;
  535. break;
  536. case 'GPU.Enabled':
  537. case 'PGA.Enabled':
  538. if ($value != 'Y')
  539. $class = $warnclass;
  540. break;
  541. case 'STATUS.When':
  542. $ret = date($dfmt, $value);
  543. break;
  544. }
  545. if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
  546. $class = $errorclass;
  547. if ($class == '' && $section != 'POOL')
  548. $class = classlastshare($when, $alldata, $lstclass, $lstclass);
  549. return array($ret, $class);
  550. }
  551. #
  552. global $poolcmd;
  553. $poolcmd = array( 'Switch to' => 'switchpool',
  554. 'Enable' => 'enablepool',
  555. 'Disable' => 'disablepool' );
  556. #
  557. function showhead($cmd, $values, $justnames = false)
  558. {
  559. global $poolcmd, $readonly;
  560. echo '<tr>';
  561. foreach ($values as $name => $value)
  562. {
  563. if ($name == '0' or $name == '')
  564. $name = '&nbsp;';
  565. echo "<td valign=bottom class=h>$name</td>";
  566. }
  567. if ($justnames === false && $cmd == 'pools' && $readonly === false)
  568. foreach ($poolcmd as $name => $pcmd)
  569. echo "<td valign=bottom class=h>$name</td>";
  570. echo '</tr>';
  571. }
  572. #
  573. function details($cmd, $list, $rig)
  574. {
  575. global $tablebegin, $tableend, $dfmt;
  576. global $poolcmd, $readonly;
  577. global $showndate;
  578. $when = 0;
  579. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  580. echo $tablebegin;
  581. if ($showndate === false)
  582. {
  583. echo '<tr><td class=sta>Date: '.date($dfmt).'</td></tr>';
  584. echo $tableend.$tablebegin;
  585. $showndate = true;
  586. }
  587. if (isset($list['STATUS']))
  588. {
  589. echo '<tr>';
  590. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  591. if (isset($list['STATUS']['When']))
  592. {
  593. echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
  594. $when = $list['STATUS']['When'];
  595. }
  596. $sta = $list['STATUS']['STATUS'];
  597. echo '<td>Status: '.$stas[$sta].'</td>';
  598. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  599. echo '</tr>';
  600. }
  601. $section = '';
  602. foreach ($list as $item => $values)
  603. {
  604. if ($item == 'STATUS')
  605. continue;
  606. $sectionname = preg_replace('/\d/', '', $item);
  607. if ($sectionname != $section)
  608. {
  609. echo $tableend.$tablebegin;
  610. showhead($cmd, $values);
  611. $section = $sectionname;
  612. }
  613. echo '<tr>';
  614. foreach ($values as $name => $value)
  615. {
  616. list($showvalue, $class) = fmt($section, $name, $value, $when, $values);
  617. echo "<td$class>$showvalue</td>";
  618. }
  619. if ($cmd == 'pools' && $readonly === false)
  620. {
  621. reset($values);
  622. $pool = current($values);
  623. foreach ($poolcmd as $name => $pcmd)
  624. {
  625. echo '<td>';
  626. if ($pool === false)
  627. echo '&nbsp;';
  628. else
  629. {
  630. echo "<input type=button value='Pool $pool'";
  631. echo " onclick='prc(\"$pcmd|$pool&rig=$rig\",\"$name Pool $pool\")'>";
  632. }
  633. echo '</td>';
  634. }
  635. }
  636. echo '</tr>';
  637. }
  638. echo $tableend;
  639. }
  640. #
  641. global $devs;
  642. $devs = null;
  643. #
  644. function gpubuttons($count, $rig)
  645. {
  646. global $tablebegin, $tableend;
  647. global $devs;
  648. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  649. $options = array( 'intensity' => 'Intensity',
  650. 'fan' => 'Fan Percent',
  651. 'engine' => 'GPU Clock',
  652. 'mem' => 'Memory Clock',
  653. 'vddc' => 'GPU Voltage' );
  654. echo $tablebegin.'<tr>';
  655. foreach ($basic as $head)
  656. echo "<td>$head</td>";
  657. foreach ($options as $name => $des)
  658. echo "<td nowrap>$des</td>";
  659. $n = 0;
  660. for ($c = 0; $c < $count; $c++)
  661. {
  662. echo '</tr><tr>';
  663. foreach ($basic as $name)
  664. {
  665. echo '<td>';
  666. if ($name == 'GPU')
  667. echo $c;
  668. else
  669. {
  670. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  671. echo strtolower($name);
  672. echo "|$c\",$rig)'>";
  673. }
  674. echo '</td>';
  675. }
  676. foreach ($options as $name => $des)
  677. {
  678. echo '<td>';
  679. if (!isset($devs["GPU$c"][$des]))
  680. echo '&nbsp;';
  681. else
  682. {
  683. $value = $devs["GPU$c"][$des];
  684. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n,$rig)'>";
  685. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  686. $n++;
  687. }
  688. echo '</td>';
  689. }
  690. }
  691. echo '</tr>'.$tableend;
  692. }
  693. #
  694. function processgpus($rig)
  695. {
  696. global $error;
  697. global $warnfont, $warnoff;
  698. $gpus = api('gpucount');
  699. if ($error != null)
  700. echo '<tr><td>Error getting GPU count: '.$warnfont.$error.$warnoff.'</td></tr>';
  701. else
  702. {
  703. if (!isset($gpus['GPUS']['Count']))
  704. echo '<tr><td>No GPU count returned: '.$warnfont.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
  705. else
  706. {
  707. $count = $gpus['GPUS']['Count'];
  708. if ($count == 0)
  709. echo '<tr><td>No GPUs</td></tr>';
  710. else
  711. gpubuttons($count, $rig);
  712. }
  713. }
  714. }
  715. #
  716. function process($cmds, $rig)
  717. {
  718. global $error, $devs;
  719. global $warnfont, $warnoff;
  720. foreach ($cmds as $cmd => $des)
  721. {
  722. $process = api($cmd);
  723. if ($error != null)
  724. {
  725. echo "<tr><td colspan=100>Error getting $des: ";
  726. echo $warnfont.$error.$warnoff.'</td></tr>';
  727. break;
  728. }
  729. else
  730. {
  731. details($cmd, $process, $rig);
  732. echo '<tr><td><br><br></td></tr>';
  733. if ($cmd == 'devs')
  734. $devs = $process;
  735. }
  736. }
  737. }
  738. #
  739. # $head is a hack but this is just a demo anyway :)
  740. function doforeach($cmd, $des, $sum, $head, $datetime)
  741. {
  742. global $miner, $port;
  743. global $error, $readonly, $notify, $rigs;
  744. global $tablebegin, $tableend, $warnfont, $warnoff, $dfmt;
  745. global $rigerror;
  746. $when = 0;
  747. $header = $head;
  748. $anss = array();
  749. $count = 0;
  750. $preverr = count($rigerror);
  751. foreach ($rigs as $rig)
  752. {
  753. if (isset($rigerror[$rig]))
  754. continue;
  755. $parts = explode(':', $rig, 2);
  756. if (count($parts) == 2)
  757. {
  758. $miner = $parts[0];
  759. $port = $parts[1];
  760. $ans = api($cmd);
  761. if ($error != null)
  762. {
  763. echo "<tr><td colspan=100>Error on rig $count getting $des: ";
  764. echo $warnfont.$error.$warnoff.'</td></tr>';
  765. $rigerror[$rig] = $error;
  766. $error = null;
  767. }
  768. else
  769. $anss[$count] = $ans;
  770. }
  771. $count++;
  772. }
  773. if (count($anss) == 0)
  774. {
  775. echo '<tr><td>Failed to access any rigs successfully';
  776. if ($preverr > 0)
  777. echo ' (or rigs had previous errors)';
  778. echo '</td></tr>';
  779. return;
  780. }
  781. if ($datetime)
  782. {
  783. echo '<tr><td class=sta>Date: '.date($dfmt).'</td></tr>';
  784. echo $tableend.$tablebegin;
  785. $dthead = array('' => 1, 'STATUS' => 1, 'Description' => 1, 'When' => 1, 'API' => 1, 'CGMiner' => 1);
  786. showhead('', $dthead);
  787. foreach ($anss as $rig => $ans)
  788. {
  789. echo '<tr>';
  790. foreach ($ans as $item => $row)
  791. {
  792. if ($item != 'STATUS' && $item != 'VERSION')
  793. continue;
  794. foreach ($dthead as $name => $x)
  795. {
  796. if ($item == 'STATUS' && $name == '')
  797. echo "<td align=right><input type=button value='Rig $rig' onclick='pr(\"&rig=$rig\",null)'></td>";
  798. else
  799. {
  800. if (isset($row[$name]))
  801. {
  802. list($showvalue, $class) = fmt('STATUS', $name, $row[$name], $when, null);
  803. echo "<td$class align=right>$showvalue</td>";
  804. }
  805. }
  806. }
  807. }
  808. echo '</tr>';
  809. }
  810. echo $tableend;
  811. echo '<tr><td><br><br></td></tr>';
  812. echo $tablebegin;
  813. return;
  814. }
  815. $total = array();
  816. foreach ($anss as $rig => $ans)
  817. {
  818. foreach ($ans as $item => $row)
  819. {
  820. if ($item == 'STATUS')
  821. continue;
  822. if (count($row) > count($header))
  823. {
  824. $header = $head;
  825. foreach ($row as $name => $value)
  826. if (!isset($header[$name]))
  827. $header[$name] = '';
  828. }
  829. if ($sum != null)
  830. foreach ($sum as $name)
  831. {
  832. if (isset($row[$name]))
  833. {
  834. if (isset($total[$name]))
  835. $total[$name] += $row[$name];
  836. else
  837. $total[$name] = $row[$name];
  838. }
  839. }
  840. }
  841. }
  842. if ($sum != null)
  843. $anss['total']['total'] = $total;
  844. showhead('', $header);
  845. $section = '';
  846. foreach ($anss as $rig => $ans)
  847. {
  848. $when = 0;
  849. if (isset($ans['STATUS']['When']))
  850. $when = $ans['STATUS']['When'];
  851. foreach ($ans as $item => $row)
  852. {
  853. if ($item == 'STATUS')
  854. continue;
  855. echo '<tr>';
  856. $newsection = preg_replace('/\d/', '', $item);
  857. if ($newsection != 'total')
  858. $section = $newsection;
  859. foreach ($header as $name => $x)
  860. {
  861. if ($name == '')
  862. {
  863. if ($rig === 'total')
  864. echo "<td align=right class=tot>Total:</td>";
  865. else
  866. echo "<td align=right><input type=button value='Rig $rig' onclick='pr(\"&rig=$rig\",null)'></td>";
  867. }
  868. else
  869. {
  870. if (isset($row[$name]))
  871. $value = $row[$name];
  872. else
  873. $value = null;
  874. list($showvalue, $class) = fmt($section, $name, $value, $when, $row);
  875. if ($rig === 'total' and $class == '')
  876. $class = ' class=tot';
  877. echo "<td$class align=right>$showvalue</td>";
  878. }
  879. }
  880. echo '</tr>';
  881. }
  882. }
  883. }
  884. #
  885. function refreshbuttons()
  886. {
  887. global $readonly;
  888. global $ignorerefresh, $changerefresh, $autorefresh;
  889. if ($ignorerefresh == false && $changerefresh == true)
  890. {
  891. echo '&nbsp;&nbsp;&nbsp;&nbsp;';
  892. echo "<input type=button value='Auto Refresh:' onclick='prr(true)'>";
  893. echo "<input type=text name='refval' id='refval' size=2 value='$autorefresh'>";
  894. echo "<input type=button value='Off' onclick='prr(false)'>";
  895. }
  896. }
  897. #
  898. function pagetop($rig, $pg)
  899. {
  900. global $readonly, $rigs;
  901. global $allowcustompages, $customsummarypages;
  902. if ($rig === null)
  903. {
  904. if ($pg === null)
  905. $refresh = '';
  906. else
  907. $refresh = "&pg=$pg";
  908. }
  909. else
  910. $refresh = "&rig=$rig";
  911. echo '<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td nowrap>';
  912. echo "<input type=button value='Refresh' onclick='pr(\"$refresh\",null)'>&nbsp;";
  913. if (count($rigs) > 1)
  914. echo "<input type=button value='Summary' onclick='pr(\"\",null)'>&nbsp;";
  915. if ($allowcustompages === true)
  916. foreach ($customsummarypages as $pagename => $data)
  917. echo "<input type=button value='$pagename' onclick='pr(\"&pg=$pagename\",null)'>&nbsp;";
  918. echo '</td><td width=100%>&nbsp;</td><td nowrap>';
  919. if ($rig !== null && $readonly === false)
  920. {
  921. $rg = '';
  922. if (count($rigs) > 1)
  923. $rg = " Rig $rig";
  924. echo "<input type=button value='Restart' onclick='prc(\"restart&rig=$rig\",\"Restart CGMiner$rg\")'>";
  925. echo "&nbsp;<input type=button value='Quit' onclick='prc(\"quit&rig=$rig\",\"Quit CGMiner$rg\")'>";
  926. }
  927. refreshbuttons();
  928. echo "</td></tr></table></td></tr>";
  929. }
  930. #
  931. function doOne($rig, $preprocess)
  932. {
  933. global $haderror, $readonly, $notify, $rigs;
  934. htmlhead(true, $rig);
  935. pagetop($rig, null);
  936. if ($preprocess != null)
  937. process(array($preprocess => $preprocess), $rig);
  938. $cmds = array( 'devs' => 'device list',
  939. 'summary' => 'summary information',
  940. 'pools' => 'pool list');
  941. if ($notify)
  942. $cmds['notify'] = 'device status';
  943. $cmds['config'] = 'cgminer config';
  944. process($cmds, $rig);
  945. if ($haderror == false && $readonly === false)
  946. processgpus($rig);
  947. }
  948. #
  949. global $sectionmap;
  950. # map sections to their api command
  951. $sectionmap = array(
  952. 'SUMMARY' => 'summary',
  953. 'POOL' => 'pools',
  954. 'GPU' => 'devs',
  955. 'PGA' => 'devs',
  956. 'NOTIFY' => 'notify',
  957. 'CONFIG' => 'config');
  958. #
  959. function customset($showfields, $sum, $section, $num, $result, $total)
  960. {
  961. foreach ($result as $sec => $row)
  962. {
  963. $secname = preg_replace('/\d/', '', $sec);
  964. if ($sec != 'total')
  965. {
  966. if ($secname != $section)
  967. continue;
  968. }
  969. echo '<tr>';
  970. $when = 0;
  971. if (isset($result['STATUS']['When']))
  972. $when = $result['STATUS']['When'];
  973. if ($sec === 'total')
  974. $class = ' class=tot';
  975. else
  976. $class = '';
  977. echo "<td align=middle$class>$num</td>";
  978. foreach ($showfields as $name => $one)
  979. {
  980. if (isset($row[$name]))
  981. {
  982. $value = $row[$name];
  983. if (isset($sum[$secname][$name]))
  984. {
  985. if (isset($total[$name]))
  986. $total[$name] += $value;
  987. else
  988. $total[$name] = $value;
  989. }
  990. }
  991. else
  992. {
  993. if ($sec == 'total' && isset($total[$name]))
  994. $value = $total[$name];
  995. else
  996. $value = null;
  997. }
  998. list($showvalue, $class) = fmt($section, $name, $value, $when, $row);
  999. if ($sec === 'total' and $class == '')
  1000. $class = ' class=tot';
  1001. echo "<td$class align=right>$showvalue</td>";
  1002. }
  1003. echo '</tr>';
  1004. }
  1005. return $total;
  1006. }
  1007. #
  1008. function processcustompage($pagename, $sections, $sum)
  1009. {
  1010. global $sectionmap;
  1011. global $miner, $port;
  1012. global $rigs, $error;
  1013. global $warnfont, $warnoff;
  1014. global $tablebegin, $tableend, $dfmt;
  1015. global $readonly, $showndate;
  1016. $cmds = array();
  1017. $errors = array();
  1018. foreach ($sections as $section => $fields)
  1019. {
  1020. if (isset($sectionmap[$section]))
  1021. {
  1022. $cmd = $sectionmap[$section];
  1023. if (!isset($cmds[$cmd]))
  1024. $cmds[$cmd] = 1;
  1025. }
  1026. else
  1027. $errors[] = "Error: unknown section '$section' in custom summary page '$pagename'";
  1028. }
  1029. $results = array();
  1030. foreach ($rigs as $num => $rig)
  1031. {
  1032. $parts = explode(':', $rig, 2);
  1033. if (count($parts) == 2)
  1034. {
  1035. $miner = $parts[0];
  1036. $port = $parts[1];
  1037. foreach ($cmds as $cmd => $one)
  1038. {
  1039. $process = api($cmd);
  1040. if ($error != null)
  1041. {
  1042. $errors[] = "Error getting $cmd for $rig $warnfont$error$warnoff";
  1043. break;
  1044. }
  1045. else
  1046. $results[$cmd][$num] = $process;
  1047. }
  1048. }
  1049. }
  1050. if (count($results) > 0)
  1051. {
  1052. $first = true;
  1053. foreach ($sections as $section => $fields)
  1054. {
  1055. if (isset($results[$sectionmap[$section]]))
  1056. {
  1057. $rigresults = $results[$sectionmap[$section]];
  1058. $showfields = array();
  1059. foreach ($fields as $field)
  1060. foreach ($rigresults as $result)
  1061. foreach ($result as $sec => $row)
  1062. {
  1063. $secname = preg_replace('/\d/', '', $sec);
  1064. if ($secname == $section && isset($row[$field]))
  1065. $showfields[$field] = 1;
  1066. }
  1067. if (count($showfields) > 0)
  1068. {
  1069. if ($first === false)
  1070. echo '<tr><td>&nbsp;</td></tr>';
  1071. echo $tablebegin;
  1072. showhead('', array('Rig'=>1)+$showfields, true);
  1073. $total = array();
  1074. $add = array('total' => array());
  1075. foreach ($rigresults as $num => $result)
  1076. {
  1077. $rg = "<input type=button value='$num' onclick='pr(\"&rig=$num\",null)'>";
  1078. $total = customset($showfields, $sum, $section, $rg, $result, $total);
  1079. }
  1080. if (count($total) > 0)
  1081. customset($showfields, $sum, $section, '&Sigma;', $add, $total);
  1082. $first = false;
  1083. echo $tableend;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. if (count($errors) > 0)
  1089. {
  1090. if (count($results) > 0)
  1091. echo '<tr><td>&nbsp;</td></tr>';
  1092. foreach ($errors as $err)
  1093. echo "<tr><td colspan=100>$err</td></tr>";
  1094. }
  1095. }
  1096. #
  1097. function showcustompage($pagename)
  1098. {
  1099. global $customsummarypages;
  1100. htmlhead(false, null, $pagename);
  1101. pagetop(null, $pagename);
  1102. if (!isset($customsummarypages[$pagename]))
  1103. {
  1104. echo "<tr><td colspan=100>Unknown custom summary page '$pagename'</td></tr>";
  1105. return;
  1106. }
  1107. if (count($customsummarypages[$pagename]) != 2)
  1108. {
  1109. echo "<tr><td colspan=100>Invalid custom summary page '$pagename' (".count($customsummarypages[$pagename]).")</td></tr>";
  1110. return;
  1111. }
  1112. $page = $customsummarypages[$pagename][0];
  1113. $sum = $customsummarypages[$pagename][1];
  1114. if ($sum === null)
  1115. $sum = array();
  1116. if (count($page) <= 1)
  1117. {
  1118. echo "<tr><td colspan=100>Invalid custom summary page '$pagename' no content </td></tr>";
  1119. return;
  1120. }
  1121. processcustompage($pagename, $page, $sum);
  1122. }
  1123. #
  1124. function display()
  1125. {
  1126. global $tablebegin, $tableend;
  1127. global $miner, $port;
  1128. global $readonly, $notify, $rigs;
  1129. global $ignorerefresh, $autorefresh;
  1130. global $allowcustompages;
  1131. if ($ignorerefresh == false)
  1132. {
  1133. $ref = trim(getparam('ref', true));
  1134. if ($ref != null && $ref != '')
  1135. $autorefresh = intval($ref);
  1136. }
  1137. $rig = trim(getparam('rig', true));
  1138. $arg = trim(getparam('arg', true));
  1139. $preprocess = null;
  1140. if ($arg != null and $arg != '')
  1141. {
  1142. $num = null;
  1143. if ($rig != null and $rig != '')
  1144. {
  1145. if ($rig >= 0 and $rig < count($rigs))
  1146. $num = $rig;
  1147. }
  1148. else
  1149. if (count($rigs) == 0)
  1150. $num = 0;
  1151. if ($num != null)
  1152. {
  1153. $parts = explode(':', $rigs[$num], 2);
  1154. if (count($parts) == 2)
  1155. {
  1156. $miner = $parts[0];
  1157. $port = $parts[1];
  1158. $preprocess = $arg;
  1159. }
  1160. }
  1161. }
  1162. if ($rigs == null or count($rigs) == 0)
  1163. {
  1164. echo "<tr><td>No rigs defined</td></tr>";
  1165. return;
  1166. }
  1167. if ($allowcustompages === true)
  1168. {
  1169. $pg = trim(getparam('pg', true));
  1170. if ($pg != null && $pg != '')
  1171. {
  1172. showcustompage($pg);
  1173. return;
  1174. }
  1175. }
  1176. if (count($rigs) == 1)
  1177. {
  1178. $parts = explode(':', $rigs[0], 2);
  1179. if (count($parts) == 2)
  1180. {
  1181. $miner = $parts[0];
  1182. $port = $parts[1];
  1183. doOne(0, $preprocess);
  1184. }
  1185. else
  1186. echo '<tr><td>Invalid "$rigs" array</td></tr>';
  1187. return;
  1188. }
  1189. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  1190. {
  1191. $parts = explode(':', $rigs[$rig], 2);
  1192. if (count($parts) == 2)
  1193. {
  1194. $miner = $parts[0];
  1195. $port = $parts[1];
  1196. doOne($rig, $preprocess);
  1197. }
  1198. else
  1199. echo '<tr><td>Invalid "$rigs" array</td></tr>';
  1200. return;
  1201. }
  1202. htmlhead(false, null);
  1203. pagetop(null, null);
  1204. if ($preprocess != null)
  1205. process(array($preprocess => $preprocess), $rig);
  1206. echo $tablebegin;
  1207. doforeach('version', 'rig summary', array(), array(), true);
  1208. $sum = array('MHS av', 'Getworks', 'Found Blocks', 'Accepted', 'Rejected', 'Discarded', 'Stale', 'Utility', 'Local Work', 'Total MH');
  1209. doforeach('summary', 'summary information', $sum, array(), false);
  1210. echo $tableend;
  1211. echo '<tr><td><br><br></td></tr>';
  1212. echo $tablebegin;
  1213. doforeach('devs', 'device list', $sum, array(''=>'','ID'=>'','Name'=>''), false);
  1214. echo $tableend;
  1215. echo '<tr><td><br><br></td></tr>';
  1216. echo $tablebegin;
  1217. doforeach('pools', 'pool list', $sum, array(''=>''), false);
  1218. echo $tableend;
  1219. }
  1220. #
  1221. display();
  1222. #
  1223. ?>
  1224. </table></td></tr></table>
  1225. </body></html>