miner.php 45 KB

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