miner.php 46 KB

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