miner.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  1. <?php
  2. session_start();
  3. #
  4. global $title, $miner, $port, $readonly, $notify, $rigs;
  5. global $rigtotals, $forcerigtotals;
  6. global $socksndtimeoutsec, $sockrcvtimeoutsec;
  7. global $checklastshare, $poolinputs, $hidefields;
  8. global $ignorerefresh, $changerefresh, $autorefresh;
  9. global $allowcustompages, $customsummarypages;
  10. global $miner_font_family, $miner_font_size;
  11. global $colouroverride, $placebuttons;
  12. #
  13. # See API-README for more details of these variables and how
  14. # to configure miner.php
  15. #
  16. # Web page title
  17. $title = 'Mine';
  18. #
  19. # Set $readonly to true to force miner.php to be readonly
  20. # Set $readonly to false then it will check BFGMiner 'privileged'
  21. $readonly = false;
  22. #
  23. # Set $notify to false to NOT attempt to display the notify command
  24. # Set $notify to true to attempt to display the notify command
  25. $notify = true;
  26. #
  27. # Set $checklastshare to true to do the following checks:
  28. # If a device's last share is 12x expected ago then display as an error
  29. # If a device's last share is 8x expected ago then display as a warning
  30. # If either of the above is true, also display the whole line highlighted
  31. # This assumes shares are 1 difficulty shares
  32. $checklastshare = true;
  33. #
  34. # Set $poolinputs to true to show the input fields for adding a pool
  35. # and changing the pool priorities
  36. # N.B. also if $readonly is true, it will not display the fields
  37. $poolinputs = false;
  38. #
  39. # Set $rigs to an array of your BFGMiner rigs that are running
  40. # format: 'IP:Port' or 'Host:Port' or 'Host:Port:Name'
  41. $rigs = array('127.0.0.1:4028');
  42. #
  43. # Set $rigtotals to true to display totals on the single rig page
  44. # 'false' means no totals (and ignores $forcerigtotals)
  45. # You can force it to always show rig totals when there is only
  46. # one line by setting $forcerigtotals = true;
  47. $rigtotals = true;
  48. $forcerigtotals = false;
  49. #
  50. # These should be OK for most cases
  51. $socksndtimeoutsec = 10;
  52. $sockrcvtimeoutsec = 40;
  53. #
  54. # List of fields NOT to be displayed
  55. # This example would hide the slightly more sensitive pool information
  56. #$hidefields = array('POOL.URL' => 1, 'POOL.User' => 1);
  57. $hidefields = array();
  58. #
  59. # Auto-refresh of the page (in seconds) - integers only
  60. # $ignorerefresh = true/false always ignore refresh parameters
  61. # $changerefresh = true/false show buttons to change the value
  62. # $autorefresh = default value, 0 means dont auto-refresh
  63. $ignorerefresh = false;
  64. $changerefresh = true;
  65. $autorefresh = 0;
  66. #
  67. # Should we allow custom pages?
  68. # (or just completely ignore them and don't display the buttons)
  69. $allowcustompages = true;
  70. #
  71. # OK this is a bit more complex item: Custom Summary Pages
  72. # As mentioned above, see API-README
  73. # see the example below (if there is no matching data, no total will show)
  74. $mobilepage = array(
  75. 'DATE' => null,
  76. 'RIGS' => null,
  77. 'SUMMARY' => array('Elapsed', 'MHS av', 'Found Blocks=Blks', 'Accepted', 'Rejected=Rej', 'Utility'),
  78. 'DEVS+NOTIFY' => array('DEVS.Name=Name', 'DEVS.ID=ID', 'DEVS.Status=Status', 'DEVS.Temperature=Temp',
  79. 'DEVS.MHS av=MHS av', 'DEVS.Accepted=Accept', 'DEVS.Rejected=Rej',
  80. 'DEVS.Utility=Utility', 'NOTIFY.Last Not Well=Not Well'),
  81. 'POOL' => array('POOL', 'Status', 'Accepted', 'Rejected=Rej', 'Last Share Time'));
  82. $mobilesum = array(
  83. 'SUMMARY' => array('MHS av', 'Found Blocks', 'Accepted', 'Rejected', 'Utility'),
  84. 'DEVS+NOTIFY' => array('DEVS.MHS av', 'DEVS.Accepted', 'DEVS.Rejected', 'DEVS.Utility'),
  85. 'POOL' => array('Accepted', 'Rejected'));
  86. #
  87. $statspage = array(
  88. 'DATE' => null,
  89. 'RIGS' => null,
  90. 'SUMMARY' => array('Elapsed', 'MHS av', 'Found Blocks=Blks',
  91. 'Accepted', 'Rejected=Rej', 'Utility',
  92. 'Hardware Errors=HW Errs', 'Network Blocks=Net Blks',
  93. 'Work Utility'),
  94. 'COIN' => array('*'),
  95. 'STATS' => array('*'));
  96. #
  97. $statssum = array(
  98. 'SUMMARY' => array('MHS av', 'Found Blocks', 'Accepted',
  99. 'Rejected', 'Utility', 'Hardware Errors',
  100. 'Work Utility'));
  101. #
  102. # customsummarypages is an array of these Custom Summary Pages
  103. $customsummarypages = array('Mobile' => array($mobilepage, $mobilesum),
  104. 'Stats' => array($statspage, $statssum));
  105. #
  106. $here = $_SERVER['PHP_SELF'];
  107. #
  108. global $tablebegin, $tableend, $warnfont, $warnoff, $dfmt;
  109. #
  110. $tablebegin = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  111. $tableend = '</table></td></tr>';
  112. $warnfont = '<font color=red><b>';
  113. $warnoff = '</b></font>';
  114. $dfmt = 'H:i:s j-M-Y \U\T\CP';
  115. #
  116. $miner_font_family = 'verdana,arial,sans';
  117. $miner_font_size = '13pt';
  118. #
  119. # Edit this or redefine it in myminer.php to change the colour scheme
  120. # See $colourtable below for the list of names
  121. $colouroverride = array();
  122. #
  123. # Where to place the buttons: 'top' 'bot' 'both'
  124. # anything else means don't show them - case sensitive
  125. $placebuttons = 'top';
  126. #
  127. # This below allows you to put your own settings into a seperate file
  128. # so you don't need to update miner.php with your preferred settings
  129. # every time a new version is released
  130. # Just create the file 'myminer.php' in the same directory as
  131. # 'miner.php' - and put your own settings in there
  132. if (file_exists('myminer.php'))
  133. include_once('myminer.php');
  134. #
  135. # This is the system default that must always contain all necessary
  136. # colours so it must be a constant
  137. # You can override these values with $colouroverride
  138. # The only one missing is in $warnfont
  139. # - which you can override directly anyway
  140. global $colourtable;
  141. $colourtable = array(
  142. 'body bgcolor' => '#ecffff',
  143. 'td color' => 'blue',
  144. 'td.two color' => 'blue',
  145. 'td.two background' => '#ecffff',
  146. 'td.h color' => 'blue',
  147. 'td.h background' => '#c4ffff',
  148. 'td.err color' => 'black',
  149. 'td.err background' => '#ff3050',
  150. 'td.warn color' => 'black',
  151. 'td.warn background' => '#ffb050',
  152. 'td.sta color' => 'green',
  153. 'td.tot color' => 'blue',
  154. 'td.tot background' => '#fff8f2',
  155. 'td.lst color' => 'blue',
  156. 'td.lst background' => '#ffffdd',
  157. 'td.hi color' => 'blue',
  158. 'td.hi background' => '#f6ffff',
  159. 'td.lo color' => 'blue',
  160. 'td.lo background' => '#deffff'
  161. );
  162. #
  163. # Don't touch these 2
  164. $miner = null;
  165. $port = null;
  166. #
  167. # Ensure it is only ever shown once
  168. global $showndate;
  169. $showndate = false;
  170. #
  171. # For summary page to stop retrying failed rigs
  172. global $rigerror;
  173. $rigerror = array();
  174. #
  175. global $rownum;
  176. $rownum = 0;
  177. #
  178. function getcss($cssname, $dom = false)
  179. {
  180. global $colourtable, $colouroverride;
  181. $css = '';
  182. foreach ($colourtable as $cssdata => $value)
  183. {
  184. $cssobj = explode(' ', $cssdata, 2);
  185. if ($cssobj[0] == $cssname)
  186. {
  187. if (isset($colouroverride[$cssdata]))
  188. $value = $colouroverride[$cssdata];
  189. if ($dom == true)
  190. $css .= ' '.$cssobj[1].'='.$value;
  191. else
  192. $css .= $cssobj[1].':'.$value.'; ';
  193. }
  194. }
  195. return $css;
  196. }
  197. #
  198. function getdom($domname)
  199. {
  200. return getcss($domname, true);
  201. }
  202. #
  203. function htmlhead($checkapi, $rig, $pg = null)
  204. {
  205. global $title, $miner_font_family, $miner_font_size;
  206. global $error, $readonly, $poolinputs, $here;
  207. global $ignorerefresh, $autorefresh;
  208. $extraparams = '';
  209. if ($rig != null && $rig != '')
  210. $extraparams = "&rig=$rig";
  211. else
  212. if ($pg != null && $pg != '')
  213. $extraparams = "&pg=$pg";
  214. if ($ignorerefresh == true || $autorefresh == 0)
  215. $refreshmeta = '';
  216. else
  217. {
  218. $url = "$here?ref=$autorefresh$extraparams";
  219. $refreshmeta = "\n<meta http-equiv='refresh' content='$autorefresh;url=$url'>";
  220. }
  221. if ($readonly === false && $checkapi === true)
  222. {
  223. $error = null;
  224. $access = api('privileged');
  225. if ($error != null
  226. || !isset($access['STATUS']['STATUS'])
  227. || $access['STATUS']['STATUS'] != 'S')
  228. $readonly = true;
  229. }
  230. $miner_font = "font-family:$miner_font_family; font-size:$miner_font_size;";
  231. echo "<html><head>$refreshmeta
  232. <title>$title</title>
  233. <style type='text/css'>
  234. td { $miner_font ".getcss('td')."}
  235. td.two { $miner_font ".getcss('td.two')."}
  236. td.h { $miner_font ".getcss('td.h')."}
  237. td.err { $miner_font ".getcss('td.err')."}
  238. td.warn { $miner_font ".getcss('td.warn')."}
  239. td.sta { $miner_font ".getcss('td.sta')."}
  240. td.tot { $miner_font ".getcss('td.tot')."}
  241. td.lst { $miner_font ".getcss('td.lst')."}
  242. td.hi { $miner_font ".getcss('td.hi')."}
  243. td.lo { $miner_font ".getcss('td.lo')."}
  244. </style>
  245. </head><body".getdom('body').">
  246. <script type='text/javascript'>
  247. function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location='$here?ref=$autorefresh'+a}\n";
  248. if ($ignorerefresh == false)
  249. echo "function prr(a){if(a){v=document.getElementById('refval').value}else{v=0}window.location='$here?ref='+v+'$extraparams'}\n";
  250. if ($readonly === false && $checkapi === true)
  251. {
  252. echo "function prc(a,m){pr('&arg='+a,m)}
  253. 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)}
  254. 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";
  255. if ($poolinputs === true)
  256. echo "function cbs(s){var t=s.replace(/\\\\/g,'\\\\\\\\'); return t.replace(/,/g, '\\\\,')}\nfunction pla(r){var u=document.getElementById('purl').value;var w=document.getElementById('pwork').value;var p=document.getElementById('ppass').value;pr('&rig='+r+'&arg=addpool|'+cbs(u)+','+cbs(w)+','+cbs(p),'Add Pool '+u)}\nfunction psp(r){var p=document.getElementById('prio').value;pr('&rig='+r+'&arg=poolpriority|'+p,'Set Pool Priorities to '+p)}\n";
  257. }
  258. ?>
  259. </script>
  260. <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
  261. <tr><td align=center valign=top>
  262. <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
  263. <?php
  264. }
  265. #
  266. global $haderror, $error;
  267. $haderror = false;
  268. $error = null;
  269. #
  270. function getsock($addr, $port)
  271. {
  272. global $haderror, $error, $socksndtimeoutsec, $sockrcvtimeoutsec;
  273. $error = null;
  274. $socket = null;
  275. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  276. if ($socket === false || $socket === null)
  277. {
  278. $haderror = true;
  279. $error = socket_strerror(socket_last_error());
  280. $msg = "socket create(TCP) failed";
  281. $error = "ERR: $msg '$error'\n";
  282. return null;
  283. }
  284. // Ignore if this fails since the socket connect may work anyway
  285. // and nothing is gained by aborting if the option cannot be set
  286. // since we don't know in advance if it can connect
  287. socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $socksndtimeoutsec, 'usec' => 0));
  288. socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockrcvtimeoutsec, 'usec' => 0));
  289. $res = socket_connect($socket, $addr, $port);
  290. if ($res === false)
  291. {
  292. $haderror = true;
  293. $error = socket_strerror(socket_last_error());
  294. $msg = "socket connect($addr,$port) failed";
  295. $error = "ERR: $msg '$error'\n";
  296. socket_close($socket);
  297. return null;
  298. }
  299. return $socket;
  300. }
  301. #
  302. function readsockline($socket)
  303. {
  304. $line = '';
  305. while (true)
  306. {
  307. $byte = socket_read($socket, 1);
  308. if ($byte === false || $byte === '')
  309. break;
  310. if ($byte === "\0")
  311. break;
  312. $line .= $byte;
  313. }
  314. return $line;
  315. }
  316. #
  317. function api_convert_escape($str)
  318. {
  319. $res = '';
  320. $len = strlen($str);
  321. for ($i = 0; $i < $len; $i++)
  322. {
  323. $ch = substr($str, $i, 1);
  324. if ($ch != '\\' || $i == ($len-1))
  325. $res .= $ch;
  326. else
  327. {
  328. $i++;
  329. $ch = substr($str, $i, 1);
  330. switch ($ch)
  331. {
  332. case '|':
  333. $res .= "\1";
  334. break;
  335. case '\\':
  336. $res .= "\2";
  337. break;
  338. case '=':
  339. $res .= "\3";
  340. break;
  341. case ',':
  342. $res .= "\4";
  343. break;
  344. default:
  345. $res .= $ch;
  346. }
  347. }
  348. }
  349. return $res;
  350. }
  351. #
  352. function revert($str)
  353. {
  354. return str_replace(array("\1", "\2", "\3", "\4"), array("|", "\\", "=", ","), $str);
  355. }
  356. #
  357. function api($cmd)
  358. {
  359. global $haderror, $error;
  360. global $miner, $port, $hidefields;
  361. $socket = getsock($miner, $port);
  362. if ($socket != null)
  363. {
  364. socket_write($socket, $cmd, strlen($cmd));
  365. $line = readsockline($socket);
  366. socket_close($socket);
  367. if (strlen($line) == 0)
  368. {
  369. $haderror = true;
  370. $error = "WARN: '$cmd' returned nothing\n";
  371. return $line;
  372. }
  373. # print "$cmd returned '$line'\n";
  374. $line = api_convert_escape($line);
  375. $data = array();
  376. $objs = explode('|', $line);
  377. foreach ($objs as $obj)
  378. {
  379. if (strlen($obj) > 0)
  380. {
  381. $items = explode(',', $obj);
  382. $item = $items[0];
  383. $id = explode('=', $items[0], 2);
  384. if (count($id) == 1 or !ctype_digit($id[1]))
  385. $name = $id[0];
  386. else
  387. $name = $id[0].$id[1];
  388. if (strlen($name) == 0)
  389. $name = 'null';
  390. $sectionname = preg_replace('/\d/', '', $name);
  391. if (isset($data[$name]))
  392. {
  393. $num = 1;
  394. while (isset($data[$name.$num]))
  395. $num++;
  396. $name .= $num;
  397. }
  398. $counter = 0;
  399. foreach ($items as $item)
  400. {
  401. $id = explode('=', $item, 2);
  402. if (isset($hidefields[$sectionname.'.'.$id[0]]))
  403. continue;
  404. if (count($id) == 2)
  405. $data[$name][$id[0]] = revert($id[1]);
  406. else
  407. $data[$name][$counter] = $id[0];
  408. $counter++;
  409. }
  410. }
  411. }
  412. return $data;
  413. }
  414. return null;
  415. }
  416. #
  417. function getparam($name, $both = false)
  418. {
  419. $a = null;
  420. if (isset($_POST[$name]))
  421. $a = $_POST[$name];
  422. if (($both === true) and ($a === null))
  423. {
  424. if (isset($_GET[$name]))
  425. $a = $_GET[$name];
  426. }
  427. if ($a == '' || $a == null)
  428. return null;
  429. // limit to 1K just to be safe
  430. return substr($a, 0, 1024);
  431. }
  432. #
  433. function newtable()
  434. {
  435. global $tablebegin, $rownum;
  436. echo $tablebegin;
  437. $rownum = 0;
  438. }
  439. #
  440. function newrow()
  441. {
  442. echo '<tr>';
  443. }
  444. #
  445. function otherrow($row)
  446. {
  447. echo "<tr>$row</tr>";
  448. }
  449. #
  450. function endrow()
  451. {
  452. global $rownum;
  453. echo '</tr>';
  454. $rownum++;
  455. }
  456. #
  457. function endtable()
  458. {
  459. global $tableend;
  460. echo $tableend;
  461. }
  462. #
  463. function classlastshare($when, $alldata, $warnclass, $errorclass)
  464. {
  465. global $checklastshare;
  466. if ($checklastshare === false)
  467. return '';
  468. if ($when == 0)
  469. return '';
  470. if (!isset($alldata['MHS av']))
  471. return '';
  472. if ($alldata['MHS av'] == 0)
  473. return '';
  474. if (!isset($alldata['Last Share Time']))
  475. return '';
  476. $expected = pow(2, 32) / ($alldata['MHS av'] * pow(10, 6));
  477. $howlong = $when - $alldata['Last Share Time'];
  478. if ($howlong < 1)
  479. $howlong = 1;
  480. if ($howlong > ($expected * 12))
  481. return $errorclass;
  482. if ($howlong > ($expected * 8))
  483. return $warnclass;
  484. return '';
  485. }
  486. #
  487. function fmt($section, $name, $value, $when, $alldata)
  488. {
  489. global $dfmt, $rownum;
  490. if ($alldata == null)
  491. $alldata = array();
  492. $errorclass = ' class=err';
  493. $warnclass = ' class=warn';
  494. $lstclass = ' class=lst';
  495. $hiclass = ' class=hi';
  496. $loclass = ' class=lo';
  497. $c2class = ' class=two';
  498. $totclass = ' class=tot';
  499. $b = '&nbsp;';
  500. $ret = $value;
  501. $class = '';
  502. $nams = explode('.', $name);
  503. if (count($nams) > 1)
  504. $name = $nams[count($nams)-1];
  505. if ($value === null)
  506. $ret = $b;
  507. else
  508. switch ($section.'.'.$name)
  509. {
  510. case 'GPU.Last Share Time':
  511. case 'PGA.Last Share Time':
  512. case 'DEVS.Last Share Time':
  513. if ($value == 0
  514. || (isset($alldata['Last Share Pool']) && $alldata['Last Share Pool'] == -1))
  515. {
  516. $ret = 'Never';
  517. $class = $warnclass;
  518. }
  519. else
  520. {
  521. $ret = date('H:i:s', $value);
  522. $class = classlastshare($when, $alldata, $warnclass, $errorclass);
  523. }
  524. break;
  525. case 'POOL.Last Share Time':
  526. if ($value == 0)
  527. $ret = 'Never';
  528. else
  529. $ret = date('H:i:s d-M', $value);
  530. break;
  531. case 'GPU.Last Share Pool':
  532. case 'PGA.Last Share Pool':
  533. case 'DEVS.Last Share Pool':
  534. if ($value == -1)
  535. {
  536. $ret = 'None';
  537. $class = $warnclass;
  538. }
  539. break;
  540. case 'SUMMARY.Elapsed':
  541. $s = $value % 60;
  542. $value -= $s;
  543. $value /= 60;
  544. if ($value == 0)
  545. $ret = $s.'s';
  546. else
  547. {
  548. $m = $value % 60;
  549. $value -= $m;
  550. $value /= 60;
  551. if ($value == 0)
  552. $ret = sprintf("%dm$b%02ds", $m, $s);
  553. else
  554. {
  555. $h = $value % 24;
  556. $value -= $h;
  557. $value /= 24;
  558. if ($value == 0)
  559. $ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  560. else
  561. {
  562. if ($value == 1)
  563. $days = '';
  564. else
  565. $days = 's';
  566. $ret = sprintf("%dday$days$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  567. }
  568. }
  569. }
  570. break;
  571. case 'NOTIFY.Last Well':
  572. if ($value == '0')
  573. {
  574. $ret = 'Never';
  575. $class = $warnclass;
  576. }
  577. else
  578. $ret = date('H:i:s', $value);
  579. break;
  580. case 'NOTIFY.Last Not Well':
  581. if ($value == '0')
  582. $ret = 'Never';
  583. else
  584. {
  585. $ret = date('H:i:s', $value);
  586. $class = $errorclass;
  587. }
  588. break;
  589. case 'NOTIFY.Reason Not Well':
  590. if ($value != 'None')
  591. $class = $errorclass;
  592. break;
  593. case 'GPU.Utility':
  594. case 'PGA.Utility':
  595. case 'DEVS.Utility':
  596. case 'SUMMARY.Utility':
  597. case 'total.Utility':
  598. $ret = $value.'/m';
  599. if ($value == 0)
  600. $class = $errorclass;
  601. else
  602. if (isset($alldata['MHS av']))
  603. {
  604. $expected = 60 * $alldata['MHS av'] * (pow(10, 6) / pow(2, 32));
  605. if ($expected == 0)
  606. $expected = 0.000001; // 1 H/s
  607. $ratio = $value / $expected;
  608. if ($ratio < 0.9)
  609. $class = $loclass;
  610. else
  611. if ($ratio > 1.1)
  612. $class = $hiclass;
  613. }
  614. break;
  615. case 'PGA.Temperature':
  616. case 'GPU.Temperature':
  617. case 'DEVS.Temperature':
  618. $ret = $value.'&deg;C';
  619. if (!isset($alldata['GPU']))
  620. break;
  621. case 'GPU.GPU Clock':
  622. case 'DEVS.GPU Clock':
  623. case 'GPU.Memory Clock':
  624. case 'DEVS.Memory Clock':
  625. case 'GPU.GPU Voltage':
  626. case 'DEVS.GPU Voltage':
  627. case 'GPU.GPU Activity':
  628. case 'DEVS.GPU Activity':
  629. if ($value == 0)
  630. $class = $warnclass;
  631. break;
  632. case 'GPU.Fan Percent':
  633. case 'DEVS.Fan Percent':
  634. if ($value == 0)
  635. $class = $warnclass;
  636. else
  637. {
  638. if ($value == 100)
  639. $class = $errorclass;
  640. else
  641. if ($value > 85)
  642. $class = $warnclass;
  643. }
  644. break;
  645. case 'GPU.Fan Speed':
  646. case 'DEVS.Fan Speed':
  647. if ($value == 0)
  648. $class = $warnclass;
  649. else
  650. if (isset($alldata['Fan Percent']))
  651. {
  652. $test = $alldata['Fan Percent'];
  653. if ($test == 100)
  654. $class = $errorclass;
  655. else
  656. if ($test > 85)
  657. $class = $warnclass;
  658. }
  659. break;
  660. case 'GPU.MHS av':
  661. case 'PGA.MHS av':
  662. case 'DEVS.MHS av':
  663. case 'SUMMARY.MHS av':
  664. case 'total.MHS av':
  665. $parts = explode('.', $value, 2);
  666. if (count($parts) == 1)
  667. $dec = '';
  668. else
  669. $dec = '.'.$parts[1];
  670. $ret = number_format($parts[0]).$dec;
  671. if ($value == 0)
  672. $class = $errorclass;
  673. else
  674. if (isset($alldata['Utility']))
  675. {
  676. $expected = 60 * $value * (pow(10, 6) / pow(2, 32));
  677. $utility = $alldata['Utility'];
  678. $ratio = $utility / $expected;
  679. if ($ratio < 0.9)
  680. $class = $hiclass;
  681. else
  682. if ($ratio > 1.1)
  683. $class = $loclass;
  684. }
  685. break;
  686. case 'GPU.Total MH':
  687. case 'PGA.Total MH':
  688. case 'DEVS.Total MH':
  689. case 'SUMMARY.Total MH':
  690. case 'total.Total MH':
  691. case 'SUMMARY.Getworks':
  692. case 'POOL.Getworks':
  693. case 'total.Getworks':
  694. case 'GPU.Accepted':
  695. case 'PGA.Accepted':
  696. case 'DEVS.Accepted':
  697. case 'SUMMARY.Accepted':
  698. case 'POOL.Accepted':
  699. case 'total.Accepted':
  700. case 'GPU.Rejected':
  701. case 'PGA.Rejected':
  702. case 'DEVS.Rejected':
  703. case 'SUMMARY.Rejected':
  704. case 'POOL.Rejected':
  705. case 'total.Rejected':
  706. case 'SUMMARY.Local Work':
  707. case 'total.Local Work':
  708. case 'SUMMARY.Discarded':
  709. case 'POOL.Discarded':
  710. case 'total.Discarded':
  711. $parts = explode('.', $value, 2);
  712. if (count($parts) == 1)
  713. $dec = '';
  714. else
  715. $dec = '.'.$parts[1];
  716. $ret = number_format($parts[0]).$dec;
  717. break;
  718. case 'GPU.Status':
  719. case 'PGA.Status':
  720. case 'DEVS.Status':
  721. case 'POOL.Status':
  722. if ($value != 'Alive')
  723. $class = $errorclass;
  724. break;
  725. case 'GPU.Enabled':
  726. case 'PGA.Enabled':
  727. case 'DEVS.Enabled':
  728. if ($value != 'Y')
  729. $class = $warnclass;
  730. break;
  731. case 'STATUS.When':
  732. case 'COIN.Current Block Time':
  733. $ret = date($dfmt, $value);
  734. break;
  735. case 'BUTTON.Rig':
  736. case 'BUTTON.Pool':
  737. case 'BUTTON.GPU':
  738. $ret = $value;
  739. break;
  740. }
  741. if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
  742. $class = $errorclass;
  743. if ($class == '' && $section != 'POOL')
  744. $class = classlastshare($when, $alldata, $lstclass, $lstclass);
  745. if ($class == '' && $section == 'total')
  746. $class = $totclass;
  747. if ($class == '' && ($rownum % 2) == 0)
  748. $class = $c2class;
  749. if ($ret === '')
  750. $ret = $b;
  751. return array($ret, $class);
  752. }
  753. #
  754. global $poolcmd;
  755. $poolcmd = array( 'Switch to' => 'switchpool',
  756. 'Enable' => 'enablepool',
  757. 'Disable' => 'disablepool',
  758. 'Remove' => 'removepool' );
  759. #
  760. function showhead($cmd, $values, $justnames = false)
  761. {
  762. global $poolcmd, $readonly;
  763. newrow();
  764. foreach ($values as $name => $value)
  765. {
  766. if ($name == '0' or $name == '')
  767. $name = '&nbsp;';
  768. echo "<td valign=bottom class=h>$name</td>";
  769. }
  770. if ($justnames === false && $cmd == 'pools' && $readonly === false)
  771. foreach ($poolcmd as $name => $pcmd)
  772. echo "<td valign=bottom class=h>$name</td>";
  773. endrow();
  774. }
  775. #
  776. function showdatetime()
  777. {
  778. global $dfmt;
  779. otherrow('<td class=sta>Date: '.date($dfmt).'</td>');
  780. }
  781. #
  782. global $singlerigsum;
  783. $singlerigsum = array(
  784. 'devs' => array('MHS av' => 1, 'MHS 5s' => 1, 'Accepted' => 1, 'Rejected' => 1,
  785. 'Hardware Errors' => 1, 'Utility' => 1, 'Total MH' => 1),
  786. 'pools' => array('Getworks' => 1, 'Accepted' => 1, 'Rejected' => 1, 'Discarded' => 1,
  787. 'Stale' => 1, 'Get Failures' => 1, 'Remote Failures' => 1),
  788. 'notify' => array('*' => 1));
  789. #
  790. function showtotal($total, $when, $oldvalues)
  791. {
  792. global $rigtotals;
  793. list($showvalue, $class) = fmt('total', '', 'Total:', $when, null);
  794. echo "<td$class align=right>$showvalue</td>";
  795. $skipfirst = true;
  796. foreach ($oldvalues as $name => $value)
  797. {
  798. if ($skipfirst === true)
  799. {
  800. $skipfirst = false;
  801. continue;
  802. }
  803. if (isset($total[$name]))
  804. $newvalue = $total[$name];
  805. else
  806. $newvalue = '';
  807. list($showvalue, $class) = fmt('total', $name, $newvalue, $when, null);
  808. echo "<td$class";
  809. if ($rigtotals === true)
  810. echo ' align=right';
  811. echo ">$showvalue</td>";
  812. }
  813. }
  814. #
  815. function details($cmd, $list, $rig)
  816. {
  817. global $dfmt, $poolcmd, $readonly, $showndate;
  818. global $rownum, $rigtotals, $forcerigtotals, $singlerigsum;
  819. $when = 0;
  820. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  821. newtable();
  822. if ($showndate === false)
  823. {
  824. showdatetime();
  825. endtable();
  826. newtable();
  827. $showndate = true;
  828. }
  829. if (isset($list['STATUS']))
  830. {
  831. newrow();
  832. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  833. if (isset($list['STATUS']['When']))
  834. {
  835. echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
  836. $when = $list['STATUS']['When'];
  837. }
  838. $sta = $list['STATUS']['STATUS'];
  839. echo '<td>Status: '.$stas[$sta].'</td>';
  840. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  841. endrow();
  842. }
  843. if ($rigtotals === true && isset($singlerigsum[$cmd]))
  844. $dototal = $singlerigsum[$cmd];
  845. else
  846. $dototal = array();
  847. $total = array();
  848. $section = '';
  849. $oldvalues = null;
  850. foreach ($list as $item => $values)
  851. {
  852. if ($item == 'STATUS')
  853. continue;
  854. $sectionname = preg_replace('/\d/', '', $item);
  855. // Handle 'devs' possibly containing >1 table
  856. if ($sectionname != $section)
  857. {
  858. if ($oldvalues != null && count($total) > 0
  859. && ($rownum > 2 || $forcerigtotals === true))
  860. showtotal($total, $when, $oldvalues);
  861. endtable();
  862. newtable();
  863. showhead($cmd, $values);
  864. $section = $sectionname;
  865. }
  866. newrow();
  867. foreach ($values as $name => $value)
  868. {
  869. list($showvalue, $class) = fmt($section, $name, $value, $when, $values);
  870. echo "<td$class";
  871. if ($rigtotals === true)
  872. echo ' align=right';
  873. echo ">$showvalue</td>";
  874. if (isset($dototal[$name])
  875. || (isset($dototal['*']) and substr($name, 0, 1) == '*'))
  876. {
  877. if (isset($total[$name]))
  878. $total[$name] += $value;
  879. else
  880. $total[$name] = $value;
  881. }
  882. }
  883. if ($cmd == 'pools' && $readonly === false)
  884. {
  885. reset($values);
  886. $pool = current($values);
  887. foreach ($poolcmd as $name => $pcmd)
  888. {
  889. list($ignore, $class) = fmt('BUTTON', 'Pool', '', $when, $values);
  890. echo "<td$class>";
  891. if ($pool === false)
  892. echo '&nbsp;';
  893. else
  894. {
  895. echo "<input type=button value='Pool $pool'";
  896. echo " onclick='prc(\"$pcmd|$pool&rig=$rig\",\"$name Pool $pool\")'>";
  897. }
  898. echo '</td>';
  899. }
  900. }
  901. endrow();
  902. $oldvalues = $values;
  903. }
  904. if ($oldvalues != null && count($total) > 0
  905. && ($rownum > 2 || $forcerigtotals === true))
  906. showtotal($total, $when, $oldvalues);
  907. endtable();
  908. }
  909. #
  910. global $devs;
  911. $devs = null;
  912. #
  913. function gpubuttons($count, $rig)
  914. {
  915. global $devs;
  916. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  917. $options = array( 'intensity' => 'Intensity',
  918. 'fan' => 'Fan Percent',
  919. 'engine' => 'GPU Clock',
  920. 'mem' => 'Memory Clock',
  921. 'vddc' => 'GPU Voltage' );
  922. newtable();
  923. newrow();
  924. foreach ($basic as $head)
  925. echo "<td class=h>$head</td>";
  926. foreach ($options as $name => $des)
  927. echo "<td class=h nowrap>$des</td>";
  928. $n = 0;
  929. for ($c = 0; $c < $count; $c++)
  930. {
  931. endrow();
  932. newrow();
  933. foreach ($basic as $name)
  934. {
  935. list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
  936. echo "<td$class>";
  937. if ($name == 'GPU')
  938. echo $c;
  939. else
  940. {
  941. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  942. echo strtolower($name);
  943. echo "|$c\",$rig)'>";
  944. }
  945. echo '</td>';
  946. }
  947. foreach ($options as $name => $des)
  948. {
  949. list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
  950. echo "<td$class>";
  951. if (!isset($devs["GPU$c"][$des]))
  952. echo '&nbsp;';
  953. else
  954. {
  955. $value = $devs["GPU$c"][$des];
  956. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n,$rig)'>";
  957. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  958. $n++;
  959. }
  960. echo '</td>';
  961. }
  962. }
  963. endrow();
  964. endtable();
  965. }
  966. #
  967. function processgpus($rig)
  968. {
  969. global $error;
  970. global $warnfont, $warnoff;
  971. $gpus = api('gpucount');
  972. if ($error != null)
  973. otherrow("<td>Error getting GPU count: $warnfont$error$warnoff</td>");
  974. else
  975. {
  976. if (!isset($gpus['GPUS']['Count']))
  977. {
  978. $rw = '<td>No GPU count returned: '.$warnfont;
  979. $rw .= $gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'];
  980. $rw .= $warnoff.'</td>';
  981. otherrow($rw);
  982. }
  983. else
  984. {
  985. $count = $gpus['GPUS']['Count'];
  986. if ($count == 0)
  987. otherrow('<td>No GPUs</td>');
  988. else
  989. gpubuttons($count, $rig);
  990. }
  991. }
  992. }
  993. #
  994. function showpoolinputs($rig, $ans)
  995. {
  996. global $readonly, $poolinputs;
  997. if ($readonly === true || $poolinputs === false)
  998. return;
  999. newtable();
  1000. newrow();
  1001. $inps = array('Pool URL' => array('purl', 20),
  1002. 'Worker Name' => array('pwork', 10),
  1003. 'Worker Password' => array('ppass', 10));
  1004. $b = '&nbsp;';
  1005. echo "<td align=right class=h> Add a pool: </td><td>";
  1006. foreach ($inps as $text => $name)
  1007. echo "$text: <input name='".$name[0]."' id='".$name[0]."' value='' type=text size=".$name[1]."> ";
  1008. echo "</td><td align=middle><input type=button value='Add' onclick='pla($rig)'></td>";
  1009. endrow();
  1010. if (count($ans) > 1)
  1011. {
  1012. newrow();
  1013. echo '<td align=right class=h> Set pool priorities: </td>';
  1014. echo "<td> Comma list of pool numbers: <input type=text name=prio id=prio size=20>";
  1015. echo "</td><td align=middle><input type=button value='Set' onclick='psp($rig)'></td>";
  1016. endrow();
  1017. }
  1018. endtable();
  1019. }
  1020. #
  1021. function process($cmds, $rig)
  1022. {
  1023. global $error, $devs;
  1024. global $warnfont, $warnoff;
  1025. $count = count($cmds);
  1026. foreach ($cmds as $cmd => $des)
  1027. {
  1028. $process = api($cmd);
  1029. if ($error != null)
  1030. {
  1031. otherrow("<td colspan=100>Error getting $des: $warnfont$error$warnoff</td>");
  1032. break;
  1033. }
  1034. else
  1035. {
  1036. details($cmd, $process, $rig);
  1037. if ($cmd == 'devs')
  1038. $devs = $process;
  1039. if ($cmd == 'pools')
  1040. showpoolinputs($rig, $process);
  1041. # Not after the last one
  1042. if (--$count > 0)
  1043. otherrow('<td><br><br></td>');
  1044. }
  1045. }
  1046. }
  1047. #
  1048. function rigbutton($rig, $rigname, $when, $row)
  1049. {
  1050. global $rigs;
  1051. if (isset($rigs[$rig]))
  1052. {
  1053. $parts = explode(':', $rigs[$rig], 3);
  1054. if (count($parts) == 3)
  1055. $rigname = $parts[2];
  1056. }
  1057. list($value, $class) = fmt('BUTTON', 'Rig', '', $when, $row);
  1058. $button = "<td align=middle$class><input type=button value='$rigname'";
  1059. $button .= " onclick='pr(\"&rig=$rig\",null)'></td>";
  1060. return $button;
  1061. }
  1062. #
  1063. function showrigs($anss, $headname, $rigname)
  1064. {
  1065. $dthead = array($headname => 1, 'STATUS' => 1, 'Description' => 1, 'When' => 1, 'API' => 1, 'CGMiner' => 1);
  1066. showhead('', $dthead);
  1067. foreach ($anss as $rig => $ans)
  1068. {
  1069. if ($ans == null)
  1070. continue;
  1071. newrow();
  1072. $when = 0;
  1073. if (isset($ans['STATUS']['When']))
  1074. $when = $ans['STATUS']['When'];
  1075. foreach ($ans as $item => $row)
  1076. {
  1077. if ($item != 'STATUS' && $item != 'VERSION')
  1078. continue;
  1079. foreach ($dthead as $name => $x)
  1080. {
  1081. if ($item == 'STATUS' && $name == $headname)
  1082. echo rigbutton($rig, $rigname.$rig, $when, null);
  1083. else
  1084. {
  1085. if (isset($row[$name]))
  1086. {
  1087. list($showvalue, $class) = fmt('STATUS', $name, $row[$name], $when, null);
  1088. echo "<td$class align=right>$showvalue</td>";
  1089. }
  1090. }
  1091. }
  1092. }
  1093. endrow();
  1094. }
  1095. }
  1096. #
  1097. # $head is a hack but this is just a demo anyway :)
  1098. function doforeach($cmd, $des, $sum, $head, $datetime)
  1099. {
  1100. global $miner, $port;
  1101. global $error, $readonly, $notify, $rigs;
  1102. global $warnfont, $warnoff, $dfmt;
  1103. global $rigerror;
  1104. $when = 0;
  1105. $header = $head;
  1106. $anss = array();
  1107. $count = 0;
  1108. $preverr = count($rigerror);
  1109. foreach ($rigs as $num => $rig)
  1110. {
  1111. $anss[$num] = null;
  1112. if (isset($rigerror[$rig]))
  1113. continue;
  1114. $parts = explode(':', $rig, 3);
  1115. if (count($parts) >= 2)
  1116. {
  1117. $miner = $parts[0];
  1118. $port = $parts[1];
  1119. if (count($parts) > 2)
  1120. $name = $parts[2];
  1121. else
  1122. $name = $num;
  1123. $ans = api($cmd);
  1124. if ($error != null)
  1125. {
  1126. $rw = "<td colspan=100>Error on rig $name getting ";
  1127. $rw .= "$des: $warnfont$error$warnoff</td>";
  1128. otherrow($rw);
  1129. $rigerror[$rig] = $error;
  1130. $error = null;
  1131. }
  1132. else
  1133. {
  1134. $anss[$num] = $ans;
  1135. $count++;
  1136. }
  1137. }
  1138. }
  1139. if ($count == 0)
  1140. {
  1141. $rw = '<td>Failed to access any rigs successfully';
  1142. if ($preverr > 0)
  1143. $rw .= ' (or rigs had previous errors)';
  1144. $rw .= '</td>';
  1145. otherrow($rw);
  1146. return;
  1147. }
  1148. if ($datetime)
  1149. {
  1150. showdatetime();
  1151. endtable();
  1152. newtable();
  1153. showrigs($anss, '', 'Rig ');
  1154. endtable();
  1155. otherrow('<td><br><br></td>');
  1156. newtable();
  1157. return;
  1158. }
  1159. $total = array();
  1160. foreach ($anss as $rig => $ans)
  1161. {
  1162. if ($ans == null)
  1163. continue;
  1164. foreach ($ans as $item => $row)
  1165. {
  1166. if ($item == 'STATUS')
  1167. continue;
  1168. if (count($row) > count($header))
  1169. {
  1170. $header = $head;
  1171. foreach ($row as $name => $value)
  1172. if (!isset($header[$name]))
  1173. $header[$name] = '';
  1174. }
  1175. if ($sum != null)
  1176. foreach ($sum as $name)
  1177. {
  1178. if (isset($row[$name]))
  1179. {
  1180. if (isset($total[$name]))
  1181. $total[$name] += $row[$name];
  1182. else
  1183. $total[$name] = $row[$name];
  1184. }
  1185. }
  1186. }
  1187. }
  1188. if ($sum != null)
  1189. $anss['total']['total'] = $total;
  1190. showhead('', $header);
  1191. foreach ($anss as $rig => $ans)
  1192. {
  1193. if ($ans == null)
  1194. continue;
  1195. $when = 0;
  1196. if (isset($ans['STATUS']['When']))
  1197. $when = $ans['STATUS']['When'];
  1198. foreach ($ans as $item => $row)
  1199. {
  1200. if ($item == 'STATUS')
  1201. continue;
  1202. newrow();
  1203. $section = preg_replace('/\d/', '', $item);
  1204. foreach ($header as $name => $x)
  1205. {
  1206. if ($name == '')
  1207. {
  1208. if ($rig === 'total')
  1209. {
  1210. list($ignore, $class) = fmt($rig, '', '', $when, $row);
  1211. echo "<td align=right$class>Total:</td>";
  1212. }
  1213. else
  1214. echo rigbutton($rig, "Rig $rig", $when, $row);
  1215. }
  1216. else
  1217. {
  1218. if (isset($row[$name]))
  1219. $value = $row[$name];
  1220. else
  1221. $value = null;
  1222. list($showvalue, $class) = fmt($section, $name, $value, $when, $row);
  1223. echo "<td$class align=right>$showvalue</td>";
  1224. }
  1225. }
  1226. endrow();
  1227. }
  1228. }
  1229. }
  1230. #
  1231. function refreshbuttons()
  1232. {
  1233. global $readonly;
  1234. global $ignorerefresh, $changerefresh, $autorefresh;
  1235. if ($ignorerefresh == false && $changerefresh == true)
  1236. {
  1237. echo '&nbsp;&nbsp;&nbsp;&nbsp;';
  1238. echo "<input type=button value='Auto Refresh:' onclick='prr(true)'>";
  1239. echo "<input type=text name='refval' id='refval' size=2 value='$autorefresh'>";
  1240. echo "<input type=button value='Off' onclick='prr(false)'>";
  1241. }
  1242. }
  1243. #
  1244. function pagebuttons($rig, $pg)
  1245. {
  1246. global $readonly, $rigs;
  1247. global $allowcustompages, $customsummarypages;
  1248. if ($rig === null)
  1249. {
  1250. if ($pg === null)
  1251. $refresh = '';
  1252. else
  1253. $refresh = "&pg=$pg";
  1254. }
  1255. else
  1256. $refresh = "&rig=$rig";
  1257. echo '<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td nowrap>';
  1258. echo "<input type=button value='Refresh' onclick='pr(\"$refresh\",null)'>&nbsp;";
  1259. if (count($rigs) > 1)
  1260. echo "<input type=button value='Summary' onclick='pr(\"\",null)'>&nbsp;";
  1261. if ($allowcustompages === true)
  1262. foreach ($customsummarypages as $pagename => $data)
  1263. echo "<input type=button value='$pagename' onclick='pr(\"&pg=$pagename\",null)'>&nbsp;";
  1264. echo '</td><td width=100%>&nbsp;</td><td nowrap>';
  1265. if ($rig !== null && $readonly === false)
  1266. {
  1267. $rg = '';
  1268. if (count($rigs) > 1)
  1269. $rg = " Rig $rig";
  1270. echo "<input type=button value='Restart' onclick='prc(\"restart&rig=$rig\",\"Restart BFGMiner$rg\")'>";
  1271. echo "&nbsp;<input type=button value='Quit' onclick='prc(\"quit&rig=$rig\",\"Quit BFGMiner$rg\")'>";
  1272. }
  1273. refreshbuttons();
  1274. echo "</td></tr></table></td></tr>";
  1275. }
  1276. #
  1277. function doOne($rig, $preprocess)
  1278. {
  1279. global $haderror, $readonly, $notify, $rigs;
  1280. global $placebuttons;
  1281. htmlhead(true, $rig);
  1282. if ($placebuttons == 'top' || $placebuttons == 'both')
  1283. pagebuttons($rig, null);
  1284. if ($preprocess != null)
  1285. process(array($preprocess => $preprocess), $rig);
  1286. $cmds = array( 'devs' => 'device list',
  1287. 'summary' => 'summary information',
  1288. 'pools' => 'pool list');
  1289. if ($notify)
  1290. $cmds['notify'] = 'device status';
  1291. $cmds['config'] = 'BFGMiner config';
  1292. process($cmds, $rig);
  1293. if ($haderror == false && $readonly === false)
  1294. processgpus($rig);
  1295. if ($placebuttons == 'bot' || $placebuttons == 'both')
  1296. pagebuttons($rig, null);
  1297. }
  1298. #
  1299. global $sectionmap;
  1300. # map sections to their api command
  1301. # DEVS is a special case that will match GPU or PGA
  1302. # so you can have a single table with both in it
  1303. # DATE is hard coded so not in here
  1304. $sectionmap = array(
  1305. 'RIGS' => 'version',
  1306. 'SUMMARY' => 'summary',
  1307. 'POOL' => 'pools',
  1308. 'DEVS' => 'devs',
  1309. 'GPU' => 'devs', // You would normally use DEVS
  1310. 'PGA' => 'devs', // You would normally use DEVS
  1311. 'NOTIFY' => 'notify',
  1312. 'DEVDETAILS' => 'devdetails',
  1313. 'STATS' => 'stats',
  1314. 'CONFIG' => 'config',
  1315. 'COIN' => 'coin');
  1316. #
  1317. function joinfields($section1, $section2, $join, $results)
  1318. {
  1319. global $sectionmap;
  1320. $name1 = $sectionmap[$section1];
  1321. $name2 = $sectionmap[$section2];
  1322. $newres = array();
  1323. // foreach rig in section1
  1324. foreach ($results[$name1] as $rig => $result)
  1325. {
  1326. $status = null;
  1327. // foreach answer section in the rig api call
  1328. foreach ($result as $name1b => $fields1b)
  1329. {
  1330. if ($name1b == 'STATUS')
  1331. {
  1332. // remember the STATUS from section1
  1333. $status = $result[$name1b];
  1334. continue;
  1335. }
  1336. // foreach answer section in the rig api call (for the other api command)
  1337. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1338. {
  1339. if ($name2b == 'STATUS')
  1340. continue;
  1341. // If match the same field values of fields in $join
  1342. $match = true;
  1343. foreach ($join as $field)
  1344. if ($fields1b[$field] != $fields2b[$field])
  1345. {
  1346. $match = false;
  1347. break;
  1348. }
  1349. if ($match === true)
  1350. {
  1351. if ($status != null)
  1352. {
  1353. $newres[$rig]['STATUS'] = $status;
  1354. $status = null;
  1355. }
  1356. $subsection = $section1.'+'.$section2;
  1357. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1358. foreach ($fields1b as $nam => $val)
  1359. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1360. foreach ($fields2b as $nam => $val)
  1361. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1362. }
  1363. }
  1364. }
  1365. }
  1366. return $newres;
  1367. }
  1368. #
  1369. function joinall($section1, $section2, $results)
  1370. {
  1371. global $sectionmap;
  1372. $name1 = $sectionmap[$section1];
  1373. $name2 = $sectionmap[$section2];
  1374. $newres = array();
  1375. // foreach rig in section1
  1376. foreach ($results[$name1] as $rig => $result)
  1377. {
  1378. // foreach answer section in the rig api call
  1379. foreach ($result as $name1b => $fields1b)
  1380. {
  1381. if ($name1b == 'STATUS')
  1382. {
  1383. // copy the STATUS from section1
  1384. $newres[$rig][$name1b] = $result[$name1b];
  1385. continue;
  1386. }
  1387. // foreach answer section in the rig api call (for the other api command)
  1388. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1389. {
  1390. if ($name2b == 'STATUS')
  1391. continue;
  1392. $subsection = $section1.'+'.$section2;
  1393. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1394. foreach ($fields1b as $nam => $val)
  1395. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1396. foreach ($fields2b as $nam => $val)
  1397. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1398. }
  1399. }
  1400. }
  1401. return $newres;
  1402. }
  1403. #
  1404. function joinsections($sections, $results, $errors)
  1405. {
  1406. global $sectionmap;
  1407. #echo "results['pools']=".print_r($results['pools'],true)."<br>";
  1408. // GPU's don't have Name,ID fields - so create them
  1409. foreach ($results as $section => $res)
  1410. foreach ($res as $rig => $result)
  1411. foreach ($result as $name => $fields)
  1412. {
  1413. $subname = preg_replace('/[0-9]/', '', $name);
  1414. if ($subname == 'GPU' and isset($result[$name]['GPU']))
  1415. {
  1416. $results[$section][$rig][$name]['Name'] = 'GPU';
  1417. $results[$section][$rig][$name]['ID'] = $result[$name]['GPU'];
  1418. }
  1419. }
  1420. foreach ($sections as $section => $fields)
  1421. if ($section != 'DATE' && !isset($sectionmap[$section]))
  1422. {
  1423. $both = explode('+', $section, 2);
  1424. if (count($both) > 1)
  1425. {
  1426. switch($both[0])
  1427. {
  1428. case 'SUMMARY':
  1429. switch($both[1])
  1430. {
  1431. case 'POOL':
  1432. case 'DEVS':
  1433. case 'CONFIG':
  1434. case 'COIN':
  1435. $sectionmap[$section] = $section;
  1436. $results[$section] = joinall($both[0], $both[1], $results);
  1437. break;
  1438. default:
  1439. $errors[] = "Error: Invalid section '$section'";
  1440. break;
  1441. }
  1442. break;
  1443. case 'DEVS':
  1444. $join = array('Name', 'ID');
  1445. switch($both[1])
  1446. {
  1447. case 'NOTIFY':
  1448. case 'DEVDETAILS':
  1449. $sectionmap[$section] = $section;
  1450. $results[$section] = joinfields($both[0], $both[1], $join, $results);
  1451. break;
  1452. default:
  1453. $errors[] = "Error: Invalid section '$section'";
  1454. break;
  1455. }
  1456. break;
  1457. default:
  1458. $errors[] = "Error: Invalid section '$section'";
  1459. break;
  1460. }
  1461. }
  1462. else
  1463. $errors[] = "Error: Invalid section '$section'";
  1464. }
  1465. return array($results, $errors);
  1466. }
  1467. #
  1468. function secmatch($section, $field)
  1469. {
  1470. if ($section == $field)
  1471. return true;
  1472. if ($section == 'DEVS'
  1473. && ($field == 'GPU' || $field == 'PGA'))
  1474. return true;
  1475. return false;
  1476. }
  1477. #
  1478. function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total)
  1479. {
  1480. foreach ($result as $sec => $row)
  1481. {
  1482. $secname = preg_replace('/\d/', '', $sec);
  1483. if ($sec != 'total')
  1484. if (!secmatch($section, $secname))
  1485. continue;
  1486. newrow();
  1487. $when = 0;
  1488. if (isset($result['STATUS']['When']))
  1489. $when = $result['STATUS']['When'];
  1490. if ($isbutton)
  1491. echo rigbutton($rig, $rig, $when, $row);
  1492. else
  1493. {
  1494. list($ignore, $class) = fmt('total', '', '', $when, $row);
  1495. echo "<td align=middle$class>$rig</td>";
  1496. }
  1497. foreach ($showfields as $name => $one)
  1498. {
  1499. if (isset($row[$name]))
  1500. {
  1501. $value = $row[$name];
  1502. if (isset($sum[$section][$name]))
  1503. {
  1504. if (isset($total[$name]))
  1505. $total[$name] += $value;
  1506. else
  1507. $total[$name] = $value;
  1508. }
  1509. }
  1510. else
  1511. {
  1512. if ($sec == 'total' && isset($total[$name]))
  1513. $value = $total[$name];
  1514. else
  1515. $value = null;
  1516. }
  1517. if (strpos($secname, '+') === false)
  1518. list($showvalue, $class) = fmt($secname, $name, $value, $when, $row);
  1519. else
  1520. {
  1521. $parts = explode('.', $name, 2);
  1522. list($showvalue, $class) = fmt($parts[0], $parts[1], $value, $when, $row);
  1523. }
  1524. echo "<td$class align=right>$showvalue</td>";
  1525. }
  1526. endrow();
  1527. }
  1528. return $total;
  1529. }
  1530. #
  1531. function processcustompage($pagename, $sections, $sum, $namemap)
  1532. {
  1533. global $sectionmap;
  1534. global $miner, $port;
  1535. global $rigs, $error;
  1536. global $warnfont, $warnoff;
  1537. global $dfmt;
  1538. global $readonly, $showndate;
  1539. $cmds = array();
  1540. $errors = array();
  1541. foreach ($sections as $section => $fields)
  1542. {
  1543. $all = explode('+', $section);
  1544. foreach ($all as $section)
  1545. {
  1546. if (isset($sectionmap[$section]))
  1547. {
  1548. $cmd = $sectionmap[$section];
  1549. if (!isset($cmds[$cmd]))
  1550. $cmds[$cmd] = 1;
  1551. }
  1552. else
  1553. if ($section != 'DATE')
  1554. $errors[] = "Error: unknown section '$section' in custom summary page '$pagename'";
  1555. }
  1556. }
  1557. $results = array();
  1558. foreach ($rigs as $num => $rig)
  1559. {
  1560. $parts = explode(':', $rig, 3);
  1561. if (count($parts) >= 2)
  1562. {
  1563. $miner = $parts[0];
  1564. $port = $parts[1];
  1565. if (count($parts) > 2)
  1566. $name = $parts[2];
  1567. else
  1568. $name = $rig;
  1569. foreach ($cmds as $cmd => $one)
  1570. {
  1571. $process = api($cmd);
  1572. if ($error != null)
  1573. {
  1574. $errors[] = "Error getting $cmd for $name $warnfont$error$warnoff";
  1575. break;
  1576. }
  1577. else
  1578. $results[$cmd][$num] = $process;
  1579. }
  1580. }
  1581. }
  1582. $shownsomething = false;
  1583. if (count($results) > 0)
  1584. {
  1585. list($results, $errors) = joinsections($sections, $results, $errors);
  1586. $first = true;
  1587. foreach ($sections as $section => $fields)
  1588. {
  1589. if ($section === 'DATE')
  1590. {
  1591. if ($shownsomething)
  1592. otherrow('<td>&nbsp;</td>');
  1593. newtable();
  1594. showdatetime();
  1595. endtable();
  1596. // On top of the next table
  1597. $shownsomething = false;
  1598. continue;
  1599. }
  1600. if ($section === 'RIGS')
  1601. {
  1602. if ($shownsomething)
  1603. otherrow('<td>&nbsp;</td>');
  1604. newtable();
  1605. showrigs($results['version'], 'Rig', '');
  1606. endtable();
  1607. $shownsomething = true;
  1608. continue;
  1609. }
  1610. if (isset($results[$sectionmap[$section]]))
  1611. {
  1612. $rigresults = $results[$sectionmap[$section]];
  1613. $showfields = array();
  1614. $showhead = array();
  1615. foreach ($fields as $field)
  1616. foreach ($rigresults as $result)
  1617. foreach ($result as $sec => $row)
  1618. {
  1619. $secname = preg_replace('/\d/', '', $sec);
  1620. if (secmatch($section, $secname))
  1621. {
  1622. if ($field === '*')
  1623. {
  1624. foreach ($row as $f => $v)
  1625. {
  1626. $showfields[$f] = 1;
  1627. $map = $section.'.'.$f;
  1628. if (isset($namemap[$map]))
  1629. $showhead[$namemap[$map]] = 1;
  1630. else
  1631. $showhead[$f] = 1;
  1632. }
  1633. }
  1634. elseif (isset($row[$field]))
  1635. {
  1636. $showfields[$field] = 1;
  1637. $map = $section.'.'.$field;
  1638. if (isset($namemap[$map]))
  1639. $showhead[$namemap[$map]] = 1;
  1640. else
  1641. $showhead[$field] = 1;
  1642. }
  1643. }
  1644. }
  1645. if (count($showfields) > 0)
  1646. {
  1647. if ($shownsomething)
  1648. otherrow('<td>&nbsp;</td>');
  1649. newtable();
  1650. showhead('', array('Rig'=>1)+$showhead, true);
  1651. $total = array();
  1652. $add = array('total' => array());
  1653. foreach ($rigresults as $num => $result)
  1654. $total = customset($showfields, $sum, $section, $num, true, $result, $total);
  1655. if (count($total) > 0)
  1656. customset($showfields, $sum, $section, '&Sigma;', false, $add, $total);
  1657. $first = false;
  1658. endtable();
  1659. $shownsomething = true;
  1660. }
  1661. }
  1662. }
  1663. }
  1664. if (count($errors) > 0)
  1665. {
  1666. if (count($results) > 0)
  1667. otherrow('<td>&nbsp;</td>');
  1668. foreach ($errors as $err)
  1669. otherrow("<td colspan=100>$err</td>");
  1670. }
  1671. }
  1672. #
  1673. function showcustompage($pagename)
  1674. {
  1675. global $customsummarypages;
  1676. global $placebuttons;
  1677. htmlhead(false, null, $pagename);
  1678. if ($placebuttons == 'top' || $placebuttons == 'both')
  1679. pagebuttons(null, $pagename);
  1680. if (!isset($customsummarypages[$pagename]))
  1681. {
  1682. otherrow("<td colspan=100>Unknown custom summary page '$pagename'</td>");
  1683. return;
  1684. }
  1685. if (count($customsummarypages[$pagename]) != 2)
  1686. {
  1687. $rw = "<td colspan=100>Invalid custom summary page '$pagename' (";
  1688. $rw .= count($customsummarypages[$pagename]).')</td>';
  1689. otherrow($rw);
  1690. return;
  1691. }
  1692. $page = $customsummarypages[$pagename][0];
  1693. $namemap = array();
  1694. foreach ($page as $name => $fields)
  1695. {
  1696. if ($fields === null)
  1697. $page[$name] = array();
  1698. else
  1699. foreach ($fields as $num => $field)
  1700. {
  1701. $pos = strpos($field, '=');
  1702. if ($pos !== false)
  1703. {
  1704. $names = explode('=', $field, 2);
  1705. if (strlen($names[1]) > 0)
  1706. $namemap[$name.'.'.$names[0]] = $names[1];
  1707. $page[$name][$num] = $names[0];
  1708. }
  1709. }
  1710. }
  1711. $sum = $customsummarypages[$pagename][1];
  1712. if ($sum === null)
  1713. $sum = array();
  1714. // convert them to searchable via isset()
  1715. foreach ($sum as $section => $fields)
  1716. {
  1717. $newfields = array();
  1718. foreach ($fields as $field)
  1719. $newfields[$field] = 1;
  1720. $sum[$section] = $newfields;
  1721. }
  1722. if (count($page) <= 1)
  1723. {
  1724. otherrow("<td colspan=100>Invalid custom summary page '$pagename' no content </td>");
  1725. return;
  1726. }
  1727. processcustompage($pagename, $page, $sum, $namemap);
  1728. if ($placebuttons == 'bot' || $placebuttons == 'both')
  1729. pagebuttons(null, $pagename);
  1730. }
  1731. #
  1732. function display()
  1733. {
  1734. global $miner, $port;
  1735. global $readonly, $notify, $rigs;
  1736. global $ignorerefresh, $autorefresh;
  1737. global $allowcustompages;
  1738. global $placebuttons;
  1739. if ($ignorerefresh == false)
  1740. {
  1741. $ref = trim(getparam('ref', true));
  1742. if ($ref != null && $ref != '')
  1743. $autorefresh = intval($ref);
  1744. }
  1745. $rig = trim(getparam('rig', true));
  1746. $arg = trim(getparam('arg', true));
  1747. $preprocess = null;
  1748. if ($arg != null and $arg != '')
  1749. {
  1750. $num = null;
  1751. if ($rig != null and $rig != '')
  1752. {
  1753. if ($rig >= 0 and $rig < count($rigs))
  1754. $num = $rig;
  1755. }
  1756. else
  1757. if (count($rigs) == 0)
  1758. $num = 0;
  1759. if ($num != null)
  1760. {
  1761. $parts = explode(':', $rigs[$num], 3);
  1762. if (count($parts) >= 2)
  1763. {
  1764. $miner = $parts[0];
  1765. $port = $parts[1];
  1766. if ($readonly !== true)
  1767. $preprocess = $arg;
  1768. }
  1769. }
  1770. }
  1771. if ($rigs == null or count($rigs) == 0)
  1772. {
  1773. otherrow("<td>No rigs defined</td>");
  1774. return;
  1775. }
  1776. if ($allowcustompages === true)
  1777. {
  1778. $pg = trim(getparam('pg', true));
  1779. if ($pg != null && $pg != '')
  1780. {
  1781. showcustompage($pg);
  1782. return;
  1783. }
  1784. }
  1785. if (count($rigs) == 1)
  1786. {
  1787. $parts = explode(':', $rigs[0], 3);
  1788. if (count($parts) >= 2)
  1789. {
  1790. $miner = $parts[0];
  1791. $port = $parts[1];
  1792. doOne(0, $preprocess);
  1793. }
  1794. else
  1795. otherrow('<td>Invalid "$rigs" array</td>');
  1796. return;
  1797. }
  1798. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  1799. {
  1800. $parts = explode(':', $rigs[$rig], 3);
  1801. if (count($parts) >= 2)
  1802. {
  1803. $miner = $parts[0];
  1804. $port = $parts[1];
  1805. doOne($rig, $preprocess);
  1806. }
  1807. else
  1808. otherrow('<td>Invalid "$rigs" array</td>');
  1809. return;
  1810. }
  1811. htmlhead(false, null);
  1812. if ($placebuttons == 'top' || $placebuttons == 'both')
  1813. pagebuttons(null, null);
  1814. if ($preprocess != null)
  1815. process(array($preprocess => $preprocess), $rig);
  1816. newtable();
  1817. doforeach('version', 'rig summary', array(), array(), true);
  1818. $sum = array('MHS av', 'Getworks', 'Found Blocks', 'Accepted', 'Rejected', 'Discarded', 'Stale', 'Utility', 'Local Work', 'Total MH');
  1819. doforeach('summary', 'summary information', $sum, array(), false);
  1820. endtable();
  1821. otherrow('<td><br><br></td>');
  1822. newtable();
  1823. doforeach('devs', 'device list', $sum, array(''=>'','ID'=>'','Name'=>''), false);
  1824. endtable();
  1825. otherrow('<td><br><br></td>');
  1826. newtable();
  1827. doforeach('pools', 'pool list', $sum, array(''=>''), false);
  1828. endtable();
  1829. if ($placebuttons == 'bot' || $placebuttons == 'both')
  1830. pagebuttons(null, null);
  1831. }
  1832. #
  1833. display();
  1834. #
  1835. ?>
  1836. </table></td></tr></table>
  1837. </body></html>