miner.php 46 KB

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