miner.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  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. if (!isset($alldata['Last Share Difficulty']))
  492. return '';
  493. $expected = pow(2, 32) / ($alldata['MHS av'] * pow(10, 6));
  494. // If the share difficulty changes while waiting on a share,
  495. // this calculation will of course be incorrect
  496. $expected *= $alldata['Last Share Difficulty'];
  497. $howlong = $when - $alldata['Last Share Time'];
  498. if ($howlong < 1)
  499. $howlong = 1;
  500. if ($howlong > ($expected * 12))
  501. return $errorclass;
  502. if ($howlong > ($expected * 8))
  503. return $warnclass;
  504. return '';
  505. }
  506. #
  507. function fmt($section, $name, $value, $when, $alldata)
  508. {
  509. global $dfmt, $rownum;
  510. if ($alldata == null)
  511. $alldata = array();
  512. $errorclass = ' class=err';
  513. $warnclass = ' class=warn';
  514. $lstclass = ' class=lst';
  515. $hiclass = ' class=hi';
  516. $loclass = ' class=lo';
  517. $c2class = ' class=two';
  518. $totclass = ' class=tot';
  519. $b = '&nbsp;';
  520. $ret = $value;
  521. $class = '';
  522. $nams = explode('.', $name);
  523. if (count($nams) > 1)
  524. $name = $nams[count($nams)-1];
  525. if ($value === null)
  526. $ret = $b;
  527. else
  528. switch ($section.'.'.$name)
  529. {
  530. case 'GPU.Last Share Time':
  531. case 'PGA.Last Share Time':
  532. case 'DEVS.Last Share Time':
  533. if ($value == 0
  534. || (isset($alldata['Last Share Pool']) && $alldata['Last Share Pool'] == -1))
  535. {
  536. $ret = 'Never';
  537. $class = $warnclass;
  538. }
  539. else
  540. {
  541. $ret = date('H:i:s', $value);
  542. $class = classlastshare($when, $alldata, $warnclass, $errorclass);
  543. }
  544. break;
  545. case 'POOL.Last Share Time':
  546. if ($value == 0)
  547. $ret = 'Never';
  548. else
  549. $ret = date('H:i:s d-M', $value);
  550. break;
  551. case 'GPU.Last Share Pool':
  552. case 'PGA.Last Share Pool':
  553. case 'DEVS.Last Share Pool':
  554. if ($value == -1)
  555. {
  556. $ret = 'None';
  557. $class = $warnclass;
  558. }
  559. break;
  560. case 'SUMMARY.Elapsed':
  561. $s = $value % 60;
  562. $value -= $s;
  563. $value /= 60;
  564. if ($value == 0)
  565. $ret = $s.'s';
  566. else
  567. {
  568. $m = $value % 60;
  569. $value -= $m;
  570. $value /= 60;
  571. if ($value == 0)
  572. $ret = sprintf("%dm$b%02ds", $m, $s);
  573. else
  574. {
  575. $h = $value % 24;
  576. $value -= $h;
  577. $value /= 24;
  578. if ($value == 0)
  579. $ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  580. else
  581. {
  582. if ($value == 1)
  583. $days = '';
  584. else
  585. $days = 's';
  586. $ret = sprintf("%dday$days$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  587. }
  588. }
  589. }
  590. break;
  591. case 'NOTIFY.Last Well':
  592. if ($value == '0')
  593. {
  594. $ret = 'Never';
  595. $class = $warnclass;
  596. }
  597. else
  598. $ret = date('H:i:s', $value);
  599. break;
  600. case 'NOTIFY.Last Not Well':
  601. if ($value == '0')
  602. $ret = 'Never';
  603. else
  604. {
  605. $ret = date('H:i:s', $value);
  606. $class = $errorclass;
  607. }
  608. break;
  609. case 'NOTIFY.Reason Not Well':
  610. if ($value != 'None')
  611. $class = $errorclass;
  612. break;
  613. case 'GPU.Utility':
  614. case 'PGA.Utility':
  615. case 'DEVS.Utility':
  616. case 'SUMMARY.Utility':
  617. case 'total.Utility':
  618. $ret = $value.'/m';
  619. if ($value == 0)
  620. $class = $errorclass;
  621. else
  622. if (isset($alldata['Difficulty Accepted'])
  623. && isset($alldata['Accepted'])
  624. && isset($alldata['MHS av'])
  625. && ($alldata['Difficulty Accepted'] > 0)
  626. && ($alldata['Accepted'] > 0))
  627. {
  628. $expected = 60 * $alldata['MHS av'] * (pow(10, 6) / pow(2, 32));
  629. if ($expected == 0)
  630. $expected = 0.000001; // 1 H/s
  631. $da = $alldata['Difficulty Accepted'];
  632. $a = $alldata['Accepted'];
  633. $expected /= ($da / $a);
  634. $ratio = $value / $expected;
  635. if ($ratio < 0.9)
  636. $class = $loclass;
  637. else
  638. if ($ratio > 1.1)
  639. $class = $hiclass;
  640. }
  641. break;
  642. case 'PGA.Temperature':
  643. case 'GPU.Temperature':
  644. case 'DEVS.Temperature':
  645. $ret = $value.'&deg;C';
  646. if (!isset($alldata['GPU']))
  647. break;
  648. case 'GPU.GPU Clock':
  649. case 'DEVS.GPU Clock':
  650. case 'GPU.Memory Clock':
  651. case 'DEVS.Memory Clock':
  652. case 'GPU.GPU Voltage':
  653. case 'DEVS.GPU Voltage':
  654. case 'GPU.GPU Activity':
  655. case 'DEVS.GPU Activity':
  656. if ($value == 0)
  657. $class = $warnclass;
  658. break;
  659. case 'GPU.Fan Percent':
  660. case 'DEVS.Fan Percent':
  661. if ($value == 0)
  662. $class = $warnclass;
  663. else
  664. {
  665. if ($value == 100)
  666. $class = $errorclass;
  667. else
  668. if ($value > 85)
  669. $class = $warnclass;
  670. }
  671. break;
  672. case 'GPU.Fan Speed':
  673. case 'DEVS.Fan Speed':
  674. if ($value == 0)
  675. $class = $warnclass;
  676. else
  677. if (isset($alldata['Fan Percent']))
  678. {
  679. $test = $alldata['Fan Percent'];
  680. if ($test == 100)
  681. $class = $errorclass;
  682. else
  683. if ($test > 85)
  684. $class = $warnclass;
  685. }
  686. break;
  687. case 'GPU.MHS av':
  688. case 'PGA.MHS av':
  689. case 'DEVS.MHS av':
  690. case 'SUMMARY.MHS av':
  691. case 'total.MHS av':
  692. $parts = explode('.', $value, 2);
  693. if (count($parts) == 1)
  694. $dec = '';
  695. else
  696. $dec = '.'.$parts[1];
  697. $ret = number_format((float)$parts[0]).$dec;
  698. if ($value == 0)
  699. $class = $errorclass;
  700. else
  701. if (isset($alldata['Difficulty Accepted'])
  702. && isset($alldata['Accepted'])
  703. && isset($alldata['Utility'])
  704. && ($alldata['Difficulty Accepted'] > 0)
  705. && ($alldata['Accepted'] > 0))
  706. {
  707. $expected = 60 * $value * (pow(10, 6) / pow(2, 32));
  708. if ($expected == 0)
  709. $expected = 0.000001; // 1 H/s
  710. $da = $alldata['Difficulty Accepted'];
  711. $a = $alldata['Accepted'];
  712. $expected /= ($da / $a);
  713. $ratio = $alldata['Utility'] / $expected;
  714. if ($ratio < 0.9)
  715. $class = $hiclass;
  716. else
  717. if ($ratio > 1.1)
  718. $class = $loclass;
  719. }
  720. break;
  721. case 'GPU.Total MH':
  722. case 'PGA.Total MH':
  723. case 'DEVS.Total MH':
  724. case 'SUMMARY.Total MH':
  725. case 'total.Total MH':
  726. case 'SUMMARY.Getworks':
  727. case 'POOL.Getworks':
  728. case 'total.Getworks':
  729. case 'GPU.Accepted':
  730. case 'PGA.Accepted':
  731. case 'DEVS.Accepted':
  732. case 'SUMMARY.Accepted':
  733. case 'POOL.Accepted':
  734. case 'total.Accepted':
  735. case 'GPU.Rejected':
  736. case 'PGA.Rejected':
  737. case 'DEVS.Rejected':
  738. case 'SUMMARY.Rejected':
  739. case 'POOL.Rejected':
  740. case 'total.Rejected':
  741. case 'SUMMARY.Local Work':
  742. case 'total.Local Work':
  743. case 'SUMMARY.Discarded':
  744. case 'POOL.Discarded':
  745. case 'total.Discarded':
  746. $parts = explode('.', $value, 2);
  747. if (count($parts) == 1)
  748. $dec = '';
  749. else
  750. $dec = '.'.$parts[1];
  751. $ret = number_format((float)$parts[0]).$dec;
  752. break;
  753. case 'GPU.Status':
  754. case 'PGA.Status':
  755. case 'DEVS.Status':
  756. case 'POOL.Status':
  757. if ($value != 'Alive')
  758. $class = $errorclass;
  759. break;
  760. case 'GPU.Enabled':
  761. case 'PGA.Enabled':
  762. case 'DEVS.Enabled':
  763. if ($value != 'Y')
  764. $class = $warnclass;
  765. break;
  766. case 'STATUS.When':
  767. case 'COIN.Current Block Time':
  768. $ret = date($dfmt, $value);
  769. break;
  770. case 'BUTTON.Rig':
  771. case 'BUTTON.Pool':
  772. case 'BUTTON.GPU':
  773. $ret = $value;
  774. break;
  775. }
  776. if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
  777. $class = $errorclass;
  778. if ($class == '' && $section != 'POOL')
  779. $class = classlastshare($when, $alldata, $lstclass, $lstclass);
  780. if ($class == '' && $section == 'total')
  781. $class = $totclass;
  782. if ($class == '' && ($rownum % 2) == 0)
  783. $class = $c2class;
  784. if ($ret === '')
  785. $ret = $b;
  786. return array($ret, $class);
  787. }
  788. #
  789. global $poolcmd;
  790. $poolcmd = array( 'Switch to' => 'switchpool',
  791. 'Enable' => 'enablepool',
  792. 'Disable' => 'disablepool',
  793. 'Remove' => 'removepool' );
  794. #
  795. function showhead($cmd, $values, $justnames = false)
  796. {
  797. global $poolcmd, $readonly;
  798. newrow();
  799. foreach ($values as $name => $value)
  800. {
  801. if ($name == '0' or $name == '')
  802. $name = '&nbsp;';
  803. echo "<td valign=bottom class=h>$name</td>";
  804. }
  805. if ($justnames === false && $cmd == 'pools' && $readonly === false)
  806. foreach ($poolcmd as $name => $pcmd)
  807. echo "<td valign=bottom class=h>$name</td>";
  808. endrow();
  809. }
  810. #
  811. function showdatetime()
  812. {
  813. global $dfmt;
  814. otherrow('<td class=sta>Date: '.date($dfmt).'</td>');
  815. }
  816. #
  817. global $singlerigsum;
  818. $singlerigsum = array(
  819. 'devs' => array('MHS av' => 1, 'MHS 5s' => 1, 'Accepted' => 1, 'Rejected' => 1,
  820. 'Hardware Errors' => 1, 'Utility' => 1, 'Total MH' => 1),
  821. 'pools' => array('Getworks' => 1, 'Accepted' => 1, 'Rejected' => 1, 'Discarded' => 1,
  822. 'Stale' => 1, 'Get Failures' => 1, 'Remote Failures' => 1),
  823. 'notify' => array('*' => 1));
  824. #
  825. function showtotal($total, $when, $oldvalues)
  826. {
  827. global $rigtotals;
  828. list($showvalue, $class) = fmt('total', '', 'Total:', $when, null);
  829. echo "<td$class align=right>$showvalue</td>";
  830. $skipfirst = true;
  831. foreach ($oldvalues as $name => $value)
  832. {
  833. if ($skipfirst === true)
  834. {
  835. $skipfirst = false;
  836. continue;
  837. }
  838. if (isset($total[$name]))
  839. $newvalue = $total[$name];
  840. else
  841. $newvalue = '';
  842. list($showvalue, $class) = fmt('total', $name, $newvalue, $when, null);
  843. echo "<td$class";
  844. if ($rigtotals === true)
  845. echo ' align=right';
  846. echo ">$showvalue</td>";
  847. }
  848. }
  849. #
  850. function details($cmd, $list, $rig)
  851. {
  852. global $dfmt, $poolcmd, $readonly, $showndate;
  853. global $rownum, $rigtotals, $forcerigtotals, $singlerigsum;
  854. $when = 0;
  855. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  856. newtable();
  857. if ($showndate === false)
  858. {
  859. showdatetime();
  860. endtable();
  861. newtable();
  862. $showndate = true;
  863. }
  864. if (isset($list['STATUS']))
  865. {
  866. newrow();
  867. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  868. if (isset($list['STATUS']['When']))
  869. {
  870. echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
  871. $when = $list['STATUS']['When'];
  872. }
  873. $sta = $list['STATUS']['STATUS'];
  874. echo '<td>Status: '.$stas[$sta].'</td>';
  875. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  876. endrow();
  877. }
  878. if ($rigtotals === true && isset($singlerigsum[$cmd]))
  879. $dototal = $singlerigsum[$cmd];
  880. else
  881. $dototal = array();
  882. $total = array();
  883. $section = '';
  884. $oldvalues = null;
  885. foreach ($list as $item => $values)
  886. {
  887. if ($item == 'STATUS')
  888. continue;
  889. $sectionname = preg_replace('/\d/', '', $item);
  890. // Handle 'devs' possibly containing >1 table
  891. if ($sectionname != $section)
  892. {
  893. if ($oldvalues != null && count($total) > 0
  894. && ($rownum > 2 || $forcerigtotals === true))
  895. showtotal($total, $when, $oldvalues);
  896. endtable();
  897. newtable();
  898. showhead($cmd, $values);
  899. $section = $sectionname;
  900. }
  901. newrow();
  902. foreach ($values as $name => $value)
  903. {
  904. list($showvalue, $class) = fmt($section, $name, $value, $when, $values);
  905. echo "<td$class";
  906. if ($rigtotals === true)
  907. echo ' align=right';
  908. echo ">$showvalue</td>";
  909. if (isset($dototal[$name])
  910. || (isset($dototal['*']) and substr($name, 0, 1) == '*'))
  911. {
  912. if (isset($total[$name]))
  913. $total[$name] += $value;
  914. else
  915. $total[$name] = $value;
  916. }
  917. }
  918. if ($cmd == 'pools' && $readonly === false)
  919. {
  920. reset($values);
  921. $pool = current($values);
  922. foreach ($poolcmd as $name => $pcmd)
  923. {
  924. list($ignore, $class) = fmt('BUTTON', 'Pool', '', $when, $values);
  925. echo "<td$class>";
  926. if ($pool === false)
  927. echo '&nbsp;';
  928. else
  929. {
  930. echo "<input type=button value='Pool $pool'";
  931. echo " onclick='prc(\"$pcmd|$pool&rig=$rig\",\"$name Pool $pool\")'>";
  932. }
  933. echo '</td>';
  934. }
  935. }
  936. endrow();
  937. $oldvalues = $values;
  938. }
  939. if ($oldvalues != null && count($total) > 0
  940. && ($rownum > 2 || $forcerigtotals === true))
  941. showtotal($total, $when, $oldvalues);
  942. endtable();
  943. }
  944. #
  945. global $devs;
  946. $devs = null;
  947. #
  948. function gpubuttons($count, $rig)
  949. {
  950. global $devs;
  951. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  952. $options = array( 'intensity' => 'Intensity',
  953. 'fan' => 'Fan Percent',
  954. 'engine' => 'GPU Clock',
  955. 'mem' => 'Memory Clock',
  956. 'vddc' => 'GPU Voltage' );
  957. newtable();
  958. newrow();
  959. foreach ($basic as $head)
  960. echo "<td class=h>$head</td>";
  961. foreach ($options as $name => $des)
  962. echo "<td class=h nowrap>$des</td>";
  963. $n = 0;
  964. for ($c = 0; $c < $count; $c++)
  965. {
  966. endrow();
  967. newrow();
  968. foreach ($basic as $name)
  969. {
  970. list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
  971. echo "<td$class>";
  972. if ($name == 'GPU')
  973. echo $c;
  974. else
  975. {
  976. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  977. echo strtolower($name);
  978. echo "|$c\",$rig)'>";
  979. }
  980. echo '</td>';
  981. }
  982. foreach ($options as $name => $des)
  983. {
  984. list($ignore, $class) = fmt('BUTTON', 'GPU', '', 0, null);
  985. echo "<td$class>";
  986. if (!isset($devs["GPU$c"][$des]))
  987. echo '&nbsp;';
  988. else
  989. {
  990. $value = $devs["GPU$c"][$des];
  991. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n,$rig)'>";
  992. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  993. $n++;
  994. }
  995. echo '</td>';
  996. }
  997. }
  998. endrow();
  999. endtable();
  1000. }
  1001. #
  1002. function processgpus($rig)
  1003. {
  1004. global $error;
  1005. global $warnfont, $warnoff;
  1006. $gpus = api($rig, 'gpucount');
  1007. if ($error != null)
  1008. otherrow("<td>Error getting GPU count: $warnfont$error$warnoff</td>");
  1009. else
  1010. {
  1011. if (!isset($gpus['GPUS']['Count']))
  1012. {
  1013. $rw = '<td>No GPU count returned: '.$warnfont;
  1014. $rw .= $gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'];
  1015. $rw .= $warnoff.'</td>';
  1016. otherrow($rw);
  1017. }
  1018. else
  1019. {
  1020. $count = $gpus['GPUS']['Count'];
  1021. if ($count == 0)
  1022. otherrow('<td>No GPUs</td>');
  1023. else
  1024. gpubuttons($count, $rig);
  1025. }
  1026. }
  1027. }
  1028. #
  1029. function showpoolinputs($rig, $ans)
  1030. {
  1031. global $readonly, $poolinputs;
  1032. if ($readonly === true || $poolinputs === false)
  1033. return;
  1034. newtable();
  1035. newrow();
  1036. $inps = array('Pool URL' => array('purl', 20),
  1037. 'Worker Name' => array('pwork', 10),
  1038. 'Worker Password' => array('ppass', 10));
  1039. $b = '&nbsp;';
  1040. echo "<td align=right class=h> Add a pool: </td><td>";
  1041. foreach ($inps as $text => $name)
  1042. echo "$text: <input name='".$name[0]."' id='".$name[0]."' value='' type=text size=".$name[1]."> ";
  1043. echo "</td><td align=middle><input type=button value='Add' onclick='pla($rig)'></td>";
  1044. endrow();
  1045. if (count($ans) > 1)
  1046. {
  1047. newrow();
  1048. echo '<td align=right class=h> Set pool priorities: </td>';
  1049. echo "<td> Comma list of pool numbers: <input type=text name=prio id=prio size=20>";
  1050. echo "</td><td align=middle><input type=button value='Set' onclick='psp($rig)'></td>";
  1051. endrow();
  1052. }
  1053. endtable();
  1054. }
  1055. #
  1056. function process($cmds, $rig)
  1057. {
  1058. global $error, $devs;
  1059. global $warnfont, $warnoff;
  1060. $count = count($cmds);
  1061. foreach ($cmds as $cmd => $des)
  1062. {
  1063. $process = api($rig, $cmd);
  1064. if ($error != null)
  1065. {
  1066. otherrow("<td colspan=100>Error getting $des: $warnfont$error$warnoff</td>");
  1067. break;
  1068. }
  1069. else
  1070. {
  1071. details($cmd, $process, $rig);
  1072. if ($cmd == 'devs')
  1073. $devs = $process;
  1074. if ($cmd == 'pools')
  1075. showpoolinputs($rig, $process);
  1076. # Not after the last one
  1077. if (--$count > 0)
  1078. otherrow('<td><br><br></td>');
  1079. }
  1080. }
  1081. }
  1082. #
  1083. function rigbutton($rig, $rigname, $when, $row)
  1084. {
  1085. global $rigs;
  1086. if (isset($rigs[$rig]))
  1087. {
  1088. $parts = explode(':', $rigs[$rig], 3);
  1089. if (count($parts) == 3)
  1090. $rigname = $parts[2];
  1091. }
  1092. list($value, $class) = fmt('BUTTON', 'Rig', '', $when, $row);
  1093. $button = "<td align=middle$class><input type=button value='$rigname'";
  1094. $button .= " onclick='pr(\"&rig=$rig\",null)'></td>";
  1095. return $button;
  1096. }
  1097. #
  1098. function showrigs($anss, $headname, $rigname)
  1099. {
  1100. $dthead = array($headname => 1, 'STATUS' => 1, 'Description' => 1, 'When' => 1, 'API' => 1, 'CGMiner' => 1);
  1101. showhead('', $dthead);
  1102. foreach ($anss as $rig => $ans)
  1103. {
  1104. if ($ans == null)
  1105. continue;
  1106. newrow();
  1107. $when = 0;
  1108. if (isset($ans['STATUS']['When']))
  1109. $when = $ans['STATUS']['When'];
  1110. foreach ($ans as $item => $row)
  1111. {
  1112. if ($item != 'STATUS' && $item != 'VERSION')
  1113. continue;
  1114. foreach ($dthead as $name => $x)
  1115. {
  1116. if ($item == 'STATUS' && $name == $headname)
  1117. echo rigbutton($rig, $rigname.$rig, $when, null);
  1118. else
  1119. {
  1120. if (isset($row[$name]))
  1121. {
  1122. list($showvalue, $class) = fmt('STATUS', $name, $row[$name], $when, null);
  1123. echo "<td$class align=right>$showvalue</td>";
  1124. }
  1125. }
  1126. }
  1127. }
  1128. endrow();
  1129. }
  1130. }
  1131. #
  1132. # $head is a hack but this is just a demo anyway :)
  1133. function doforeach($cmd, $des, $sum, $head, $datetime)
  1134. {
  1135. global $miner, $port;
  1136. global $error, $readonly, $notify, $rigs;
  1137. global $warnfont, $warnoff, $dfmt;
  1138. global $rigerror;
  1139. $when = 0;
  1140. $header = $head;
  1141. $anss = array();
  1142. $count = 0;
  1143. $preverr = count($rigerror);
  1144. foreach ($rigs as $num => $rig)
  1145. {
  1146. $anss[$num] = null;
  1147. if (isset($rigerror[$rig]))
  1148. continue;
  1149. $parts = explode(':', $rig, 3);
  1150. if (count($parts) >= 2)
  1151. {
  1152. $miner = $parts[0];
  1153. $port = $parts[1];
  1154. if (count($parts) > 2)
  1155. $name = $parts[2];
  1156. else
  1157. $name = $num;
  1158. $ans = api($name, $cmd);
  1159. if ($error != null)
  1160. {
  1161. $rw = "<td colspan=100>Error on rig $name getting ";
  1162. $rw .= "$des: $warnfont$error$warnoff</td>";
  1163. otherrow($rw);
  1164. $rigerror[$rig] = $error;
  1165. $error = null;
  1166. }
  1167. else
  1168. {
  1169. $anss[$num] = $ans;
  1170. $count++;
  1171. }
  1172. }
  1173. }
  1174. if ($count == 0)
  1175. {
  1176. $rw = '<td>Failed to access any rigs successfully';
  1177. if ($preverr > 0)
  1178. $rw .= ' (or rigs had previous errors)';
  1179. $rw .= '</td>';
  1180. otherrow($rw);
  1181. return;
  1182. }
  1183. if ($datetime)
  1184. {
  1185. showdatetime();
  1186. endtable();
  1187. newtable();
  1188. showrigs($anss, '', 'Rig ');
  1189. endtable();
  1190. otherrow('<td><br><br></td>');
  1191. newtable();
  1192. return;
  1193. }
  1194. $total = array();
  1195. foreach ($anss as $rig => $ans)
  1196. {
  1197. if ($ans == null)
  1198. continue;
  1199. foreach ($ans as $item => $row)
  1200. {
  1201. if ($item == 'STATUS')
  1202. continue;
  1203. if (count($row) > count($header))
  1204. {
  1205. $header = $head;
  1206. foreach ($row as $name => $value)
  1207. if (!isset($header[$name]))
  1208. $header[$name] = '';
  1209. }
  1210. if ($sum != null)
  1211. foreach ($sum as $name)
  1212. {
  1213. if (isset($row[$name]))
  1214. {
  1215. if (isset($total[$name]))
  1216. $total[$name] += $row[$name];
  1217. else
  1218. $total[$name] = $row[$name];
  1219. }
  1220. }
  1221. }
  1222. }
  1223. if ($sum != null)
  1224. $anss['total']['total'] = $total;
  1225. showhead('', $header);
  1226. foreach ($anss as $rig => $ans)
  1227. {
  1228. if ($ans == null)
  1229. continue;
  1230. $when = 0;
  1231. if (isset($ans['STATUS']['When']))
  1232. $when = $ans['STATUS']['When'];
  1233. foreach ($ans as $item => $row)
  1234. {
  1235. if ($item == 'STATUS')
  1236. continue;
  1237. newrow();
  1238. $section = preg_replace('/\d/', '', $item);
  1239. foreach ($header as $name => $x)
  1240. {
  1241. if ($name == '')
  1242. {
  1243. if ($rig === 'total')
  1244. {
  1245. list($ignore, $class) = fmt($rig, '', '', $when, $row);
  1246. echo "<td align=right$class>Total:</td>";
  1247. }
  1248. else
  1249. echo rigbutton($rig, "Rig $rig", $when, $row);
  1250. }
  1251. else
  1252. {
  1253. if (isset($row[$name]))
  1254. $value = $row[$name];
  1255. else
  1256. $value = null;
  1257. list($showvalue, $class) = fmt($section, $name, $value, $when, $row);
  1258. echo "<td$class align=right>$showvalue</td>";
  1259. }
  1260. }
  1261. endrow();
  1262. }
  1263. }
  1264. }
  1265. #
  1266. function refreshbuttons()
  1267. {
  1268. global $readonly;
  1269. global $ignorerefresh, $changerefresh, $autorefresh;
  1270. if ($ignorerefresh == false && $changerefresh == true)
  1271. {
  1272. echo '&nbsp;&nbsp;&nbsp;&nbsp;';
  1273. echo "<input type=button value='Auto Refresh:' onclick='prr(true)'>";
  1274. echo "<input type=text name='refval' id='refval' size=2 value='$autorefresh'>";
  1275. echo "<input type=button value='Off' onclick='prr(false)'>";
  1276. }
  1277. }
  1278. #
  1279. function pagebuttons($rig, $pg)
  1280. {
  1281. global $readonly, $rigs;
  1282. global $allowcustompages, $customsummarypages;
  1283. if ($rig === null)
  1284. {
  1285. if ($pg === null)
  1286. $refresh = '';
  1287. else
  1288. $refresh = "&pg=$pg";
  1289. }
  1290. else
  1291. $refresh = "&rig=$rig";
  1292. echo '<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td nowrap>';
  1293. echo "<input type=button value='Refresh' onclick='pr(\"$refresh\",null)'>&nbsp;";
  1294. if (count($rigs) > 1)
  1295. echo "<input type=button value='Summary' onclick='pr(\"\",null)'>&nbsp;";
  1296. if ($allowcustompages === true)
  1297. foreach ($customsummarypages as $pagename => $data)
  1298. echo "<input type=button value='$pagename' onclick='pr(\"&pg=$pagename\",null)'>&nbsp;";
  1299. echo '</td><td width=100%>&nbsp;</td><td nowrap>';
  1300. if ($rig !== null && $readonly === false)
  1301. {
  1302. $rg = '';
  1303. if (count($rigs) > 1)
  1304. $rg = " Rig $rig";
  1305. echo "<input type=button value='Restart' onclick='prc(\"restart&rig=$rig\",\"Restart BFGMiner$rg\")'>";
  1306. echo "&nbsp;<input type=button value='Quit' onclick='prc(\"quit&rig=$rig\",\"Quit BFGMiner$rg\")'>";
  1307. }
  1308. refreshbuttons();
  1309. echo "</td></tr></table></td></tr>";
  1310. }
  1311. #
  1312. function doOne($rig, $preprocess)
  1313. {
  1314. global $haderror, $readonly, $notify, $rigs;
  1315. global $placebuttons;
  1316. htmlhead(true, $rig);
  1317. if ($placebuttons == 'top' || $placebuttons == 'both')
  1318. pagebuttons($rig, null);
  1319. if ($preprocess != null)
  1320. process(array($preprocess => $preprocess), $rig);
  1321. $cmds = array( 'devs' => 'device list',
  1322. 'summary' => 'summary information',
  1323. 'pools' => 'pool list');
  1324. if ($notify)
  1325. $cmds['notify'] = 'device status';
  1326. $cmds['config'] = 'BFGMiner config';
  1327. process($cmds, $rig);
  1328. if ($haderror == false && $readonly === false)
  1329. processgpus($rig);
  1330. if ($placebuttons == 'bot' || $placebuttons == 'both')
  1331. pagebuttons($rig, null);
  1332. }
  1333. #
  1334. global $sectionmap;
  1335. # map sections to their api command
  1336. # DEVS is a special case that will match GPU or PGA
  1337. # so you can have a single table with both in it
  1338. # DATE is hard coded so not in here
  1339. $sectionmap = array(
  1340. 'RIGS' => 'version',
  1341. 'SUMMARY' => 'summary',
  1342. 'POOL' => 'pools',
  1343. 'DEVS' => 'devs',
  1344. 'GPU' => 'devs', // You would normally use DEVS
  1345. 'PGA' => 'devs', // You would normally use DEVS
  1346. 'NOTIFY' => 'notify',
  1347. 'DEVDETAILS' => 'devdetails',
  1348. 'STATS' => 'stats',
  1349. 'CONFIG' => 'config',
  1350. 'COIN' => 'coin');
  1351. #
  1352. function joinfields($section1, $section2, $join, $results)
  1353. {
  1354. global $sectionmap;
  1355. $name1 = $sectionmap[$section1];
  1356. $name2 = $sectionmap[$section2];
  1357. $newres = array();
  1358. // foreach rig in section1
  1359. foreach ($results[$name1] as $rig => $result)
  1360. {
  1361. $status = null;
  1362. // foreach answer section in the rig api call
  1363. foreach ($result as $name1b => $fields1b)
  1364. {
  1365. if ($name1b == 'STATUS')
  1366. {
  1367. // remember the STATUS from section1
  1368. $status = $result[$name1b];
  1369. continue;
  1370. }
  1371. // foreach answer section in the rig api call (for the other api command)
  1372. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1373. {
  1374. if ($name2b == 'STATUS')
  1375. continue;
  1376. // If match the same field values of fields in $join
  1377. $match = true;
  1378. foreach ($join as $field)
  1379. if ($fields1b[$field] != $fields2b[$field])
  1380. {
  1381. $match = false;
  1382. break;
  1383. }
  1384. if ($match === true)
  1385. {
  1386. if ($status != null)
  1387. {
  1388. $newres[$rig]['STATUS'] = $status;
  1389. $status = null;
  1390. }
  1391. $subsection = $section1.'+'.$section2;
  1392. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1393. foreach ($fields1b as $nam => $val)
  1394. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1395. foreach ($fields2b as $nam => $val)
  1396. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1397. }
  1398. }
  1399. }
  1400. }
  1401. return $newres;
  1402. }
  1403. #
  1404. function joinall($section1, $section2, $results)
  1405. {
  1406. global $sectionmap;
  1407. $name1 = $sectionmap[$section1];
  1408. $name2 = $sectionmap[$section2];
  1409. $newres = array();
  1410. // foreach rig in section1
  1411. foreach ($results[$name1] as $rig => $result)
  1412. {
  1413. // foreach answer section in the rig api call
  1414. foreach ($result as $name1b => $fields1b)
  1415. {
  1416. if ($name1b == 'STATUS')
  1417. {
  1418. // copy the STATUS from section1
  1419. $newres[$rig][$name1b] = $result[$name1b];
  1420. continue;
  1421. }
  1422. // foreach answer section in the rig api call (for the other api command)
  1423. foreach ($results[$name2][$rig] as $name2b => $fields2b)
  1424. {
  1425. if ($name2b == 'STATUS')
  1426. continue;
  1427. $subsection = $section1.'+'.$section2;
  1428. $subsection .= preg_replace('/[^0-9]/', '', $name1b.$name2b);
  1429. foreach ($fields1b as $nam => $val)
  1430. $newres[$rig][$subsection]["$section1.$nam"] = $val;
  1431. foreach ($fields2b as $nam => $val)
  1432. $newres[$rig][$subsection]["$section2.$nam"] = $val;
  1433. }
  1434. }
  1435. }
  1436. return $newres;
  1437. }
  1438. #
  1439. function joinsections($sections, $results, $errors)
  1440. {
  1441. global $sectionmap;
  1442. #echo "results['pools']=".print_r($results['pools'],true)."<br>";
  1443. // GPU's don't have Name,ID fields - so create them
  1444. foreach ($results as $section => $res)
  1445. foreach ($res as $rig => $result)
  1446. foreach ($result as $name => $fields)
  1447. {
  1448. $subname = preg_replace('/[0-9]/', '', $name);
  1449. if ($subname == 'GPU' and isset($result[$name]['GPU']))
  1450. {
  1451. $results[$section][$rig][$name]['Name'] = 'GPU';
  1452. $results[$section][$rig][$name]['ID'] = $result[$name]['GPU'];
  1453. }
  1454. }
  1455. foreach ($sections as $section => $fields)
  1456. if ($section != 'DATE' && !isset($sectionmap[$section]))
  1457. {
  1458. $both = explode('+', $section, 2);
  1459. if (count($both) > 1)
  1460. {
  1461. switch($both[0])
  1462. {
  1463. case 'SUMMARY':
  1464. switch($both[1])
  1465. {
  1466. case 'POOL':
  1467. case 'DEVS':
  1468. case 'CONFIG':
  1469. case 'COIN':
  1470. $sectionmap[$section] = $section;
  1471. $results[$section] = joinall($both[0], $both[1], $results);
  1472. break;
  1473. default:
  1474. $errors[] = "Error: Invalid section '$section'";
  1475. break;
  1476. }
  1477. break;
  1478. case 'DEVS':
  1479. $join = array('Name', 'ID');
  1480. switch($both[1])
  1481. {
  1482. case 'NOTIFY':
  1483. case 'DEVDETAILS':
  1484. $sectionmap[$section] = $section;
  1485. $results[$section] = joinfields($both[0], $both[1], $join, $results);
  1486. break;
  1487. default:
  1488. $errors[] = "Error: Invalid section '$section'";
  1489. break;
  1490. }
  1491. break;
  1492. default:
  1493. $errors[] = "Error: Invalid section '$section'";
  1494. break;
  1495. }
  1496. }
  1497. else
  1498. $errors[] = "Error: Invalid section '$section'";
  1499. }
  1500. return array($results, $errors);
  1501. }
  1502. #
  1503. function secmatch($section, $field)
  1504. {
  1505. if ($section == $field)
  1506. return true;
  1507. if ($section == 'DEVS'
  1508. && ($field == 'GPU' || $field == 'PGA'))
  1509. return true;
  1510. return false;
  1511. }
  1512. #
  1513. function customset($showfields, $sum, $section, $rig, $isbutton, $result, $total)
  1514. {
  1515. foreach ($result as $sec => $row)
  1516. {
  1517. $secname = preg_replace('/\d/', '', $sec);
  1518. if ($sec != 'total')
  1519. if (!secmatch($section, $secname))
  1520. continue;
  1521. newrow();
  1522. $when = 0;
  1523. if (isset($result['STATUS']['When']))
  1524. $when = $result['STATUS']['When'];
  1525. if ($isbutton)
  1526. echo rigbutton($rig, $rig, $when, $row);
  1527. else
  1528. {
  1529. list($ignore, $class) = fmt('total', '', '', $when, $row);
  1530. echo "<td align=middle$class>$rig</td>";
  1531. }
  1532. foreach ($showfields as $name => $one)
  1533. {
  1534. if (isset($row[$name]))
  1535. {
  1536. $value = $row[$name];
  1537. if (isset($sum[$section][$name]))
  1538. {
  1539. if (isset($total[$name]))
  1540. $total[$name] += $value;
  1541. else
  1542. $total[$name] = $value;
  1543. }
  1544. }
  1545. else
  1546. {
  1547. if ($sec == 'total' && isset($total[$name]))
  1548. $value = $total[$name];
  1549. else
  1550. $value = null;
  1551. }
  1552. if (strpos($secname, '+') === false)
  1553. list($showvalue, $class) = fmt($secname, $name, $value, $when, $row);
  1554. else
  1555. {
  1556. $parts = explode('.', $name, 2);
  1557. list($showvalue, $class) = fmt($parts[0], $parts[1], $value, $when, $row);
  1558. }
  1559. echo "<td$class align=right>$showvalue</td>";
  1560. }
  1561. endrow();
  1562. }
  1563. return $total;
  1564. }
  1565. #
  1566. function processcustompage($pagename, $sections, $sum, $namemap)
  1567. {
  1568. global $sectionmap;
  1569. global $miner, $port;
  1570. global $rigs, $error;
  1571. global $warnfont, $warnoff;
  1572. global $dfmt;
  1573. global $readonly, $showndate;
  1574. $cmds = array();
  1575. $errors = array();
  1576. foreach ($sections as $section => $fields)
  1577. {
  1578. $all = explode('+', $section);
  1579. foreach ($all as $section)
  1580. {
  1581. if (isset($sectionmap[$section]))
  1582. {
  1583. $cmd = $sectionmap[$section];
  1584. if (!isset($cmds[$cmd]))
  1585. $cmds[$cmd] = 1;
  1586. }
  1587. else
  1588. if ($section != 'DATE')
  1589. $errors[] = "Error: unknown section '$section' in custom summary page '$pagename'";
  1590. }
  1591. }
  1592. $results = array();
  1593. foreach ($rigs as $num => $rig)
  1594. {
  1595. $parts = explode(':', $rig, 3);
  1596. if (count($parts) >= 2)
  1597. {
  1598. $miner = $parts[0];
  1599. $port = $parts[1];
  1600. if (count($parts) > 2)
  1601. $name = $parts[2];
  1602. else
  1603. $name = $rig;
  1604. foreach ($cmds as $cmd => $one)
  1605. {
  1606. $process = api($name, $cmd);
  1607. if ($error != null)
  1608. {
  1609. $errors[] = "Error getting $cmd for $name $warnfont$error$warnoff";
  1610. break;
  1611. }
  1612. else
  1613. $results[$cmd][$num] = $process;
  1614. }
  1615. }
  1616. }
  1617. $shownsomething = false;
  1618. if (count($results) > 0)
  1619. {
  1620. list($results, $errors) = joinsections($sections, $results, $errors);
  1621. $first = true;
  1622. foreach ($sections as $section => $fields)
  1623. {
  1624. if ($section === 'DATE')
  1625. {
  1626. if ($shownsomething)
  1627. otherrow('<td>&nbsp;</td>');
  1628. newtable();
  1629. showdatetime();
  1630. endtable();
  1631. // On top of the next table
  1632. $shownsomething = false;
  1633. continue;
  1634. }
  1635. if ($section === 'RIGS')
  1636. {
  1637. if ($shownsomething)
  1638. otherrow('<td>&nbsp;</td>');
  1639. newtable();
  1640. showrigs($results['version'], 'Rig', '');
  1641. endtable();
  1642. $shownsomething = true;
  1643. continue;
  1644. }
  1645. if (isset($results[$sectionmap[$section]]))
  1646. {
  1647. $rigresults = $results[$sectionmap[$section]];
  1648. $showfields = array();
  1649. $showhead = array();
  1650. foreach ($fields as $field)
  1651. foreach ($rigresults as $result)
  1652. foreach ($result as $sec => $row)
  1653. {
  1654. $secname = preg_replace('/\d/', '', $sec);
  1655. if (secmatch($section, $secname))
  1656. {
  1657. if ($field === '*')
  1658. {
  1659. foreach ($row as $f => $v)
  1660. {
  1661. $showfields[$f] = 1;
  1662. $map = $section.'.'.$f;
  1663. if (isset($namemap[$map]))
  1664. $showhead[$namemap[$map]] = 1;
  1665. else
  1666. $showhead[$f] = 1;
  1667. }
  1668. }
  1669. elseif (isset($row[$field]))
  1670. {
  1671. $showfields[$field] = 1;
  1672. $map = $section.'.'.$field;
  1673. if (isset($namemap[$map]))
  1674. $showhead[$namemap[$map]] = 1;
  1675. else
  1676. $showhead[$field] = 1;
  1677. }
  1678. }
  1679. }
  1680. if (count($showfields) > 0)
  1681. {
  1682. if ($shownsomething)
  1683. otherrow('<td>&nbsp;</td>');
  1684. newtable();
  1685. showhead('', array('Rig'=>1)+$showhead, true);
  1686. $total = array();
  1687. $add = array('total' => array());
  1688. foreach ($rigresults as $num => $result)
  1689. $total = customset($showfields, $sum, $section, $num, true, $result, $total);
  1690. if (count($total) > 0)
  1691. customset($showfields, $sum, $section, '&Sigma;', false, $add, $total);
  1692. $first = false;
  1693. endtable();
  1694. $shownsomething = true;
  1695. }
  1696. }
  1697. }
  1698. }
  1699. if (count($errors) > 0)
  1700. {
  1701. if (count($results) > 0)
  1702. otherrow('<td>&nbsp;</td>');
  1703. foreach ($errors as $err)
  1704. otherrow("<td colspan=100>$err</td>");
  1705. }
  1706. }
  1707. #
  1708. function showcustompage($pagename)
  1709. {
  1710. global $customsummarypages;
  1711. global $placebuttons;
  1712. htmlhead(false, null, $pagename);
  1713. if ($placebuttons == 'top' || $placebuttons == 'both')
  1714. pagebuttons(null, $pagename);
  1715. if (!isset($customsummarypages[$pagename]))
  1716. {
  1717. otherrow("<td colspan=100>Unknown custom summary page '$pagename'</td>");
  1718. return;
  1719. }
  1720. if (count($customsummarypages[$pagename]) != 2)
  1721. {
  1722. $rw = "<td colspan=100>Invalid custom summary page '$pagename' (";
  1723. $rw .= count($customsummarypages[$pagename]).')</td>';
  1724. otherrow($rw);
  1725. return;
  1726. }
  1727. $page = $customsummarypages[$pagename][0];
  1728. $namemap = array();
  1729. foreach ($page as $name => $fields)
  1730. {
  1731. if ($fields === null)
  1732. $page[$name] = array();
  1733. else
  1734. foreach ($fields as $num => $field)
  1735. {
  1736. $pos = strpos($field, '=');
  1737. if ($pos !== false)
  1738. {
  1739. $names = explode('=', $field, 2);
  1740. if (strlen($names[1]) > 0)
  1741. $namemap[$name.'.'.$names[0]] = $names[1];
  1742. $page[$name][$num] = $names[0];
  1743. }
  1744. }
  1745. }
  1746. $sum = $customsummarypages[$pagename][1];
  1747. if ($sum === null)
  1748. $sum = array();
  1749. // convert them to searchable via isset()
  1750. foreach ($sum as $section => $fields)
  1751. {
  1752. $newfields = array();
  1753. foreach ($fields as $field)
  1754. $newfields[$field] = 1;
  1755. $sum[$section] = $newfields;
  1756. }
  1757. if (count($page) <= 1)
  1758. {
  1759. otherrow("<td colspan=100>Invalid custom summary page '$pagename' no content </td>");
  1760. return;
  1761. }
  1762. processcustompage($pagename, $page, $sum, $namemap);
  1763. if ($placebuttons == 'bot' || $placebuttons == 'both')
  1764. pagebuttons(null, $pagename);
  1765. }
  1766. #
  1767. function display()
  1768. {
  1769. global $miner, $port;
  1770. global $readonly, $notify, $rigs;
  1771. global $ignorerefresh, $autorefresh;
  1772. global $allowcustompages;
  1773. global $placebuttons;
  1774. if ($ignorerefresh == false)
  1775. {
  1776. $ref = trim(getparam('ref', true));
  1777. if ($ref != null && $ref != '')
  1778. $autorefresh = intval($ref);
  1779. }
  1780. $rig = trim(getparam('rig', true));
  1781. $arg = trim(getparam('arg', true));
  1782. $preprocess = null;
  1783. if ($arg != null and $arg != '')
  1784. {
  1785. $num = null;
  1786. if ($rig != null and $rig != '')
  1787. {
  1788. if ($rig >= 0 and $rig < count($rigs))
  1789. $num = $rig;
  1790. }
  1791. else
  1792. if (count($rigs) == 0)
  1793. $num = 0;
  1794. if ($num != null)
  1795. {
  1796. $parts = explode(':', $rigs[$num], 3);
  1797. if (count($parts) >= 2)
  1798. {
  1799. $miner = $parts[0];
  1800. $port = $parts[1];
  1801. if ($readonly !== true)
  1802. $preprocess = $arg;
  1803. }
  1804. }
  1805. }
  1806. if ($rigs == null or count($rigs) == 0)
  1807. {
  1808. otherrow("<td>No rigs defined</td>");
  1809. return;
  1810. }
  1811. if ($allowcustompages === true)
  1812. {
  1813. $pg = trim(getparam('pg', true));
  1814. if ($pg != null && $pg != '')
  1815. {
  1816. showcustompage($pg);
  1817. return;
  1818. }
  1819. }
  1820. if (count($rigs) == 1)
  1821. {
  1822. $parts = explode(':', $rigs[0], 3);
  1823. if (count($parts) >= 2)
  1824. {
  1825. $miner = $parts[0];
  1826. $port = $parts[1];
  1827. doOne(0, $preprocess);
  1828. }
  1829. else
  1830. otherrow('<td>Invalid "$rigs" array</td>');
  1831. return;
  1832. }
  1833. if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
  1834. {
  1835. $parts = explode(':', $rigs[$rig], 3);
  1836. if (count($parts) >= 2)
  1837. {
  1838. $miner = $parts[0];
  1839. $port = $parts[1];
  1840. doOne($rig, $preprocess);
  1841. }
  1842. else
  1843. otherrow('<td>Invalid "$rigs" array</td>');
  1844. return;
  1845. }
  1846. htmlhead(false, null);
  1847. if ($placebuttons == 'top' || $placebuttons == 'both')
  1848. pagebuttons(null, null);
  1849. if ($preprocess != null)
  1850. process(array($preprocess => $preprocess), $rig);
  1851. newtable();
  1852. doforeach('version', 'rig summary', array(), array(), true);
  1853. $sum = array('MHS av', 'Getworks', 'Found Blocks', 'Accepted', 'Rejected', 'Discarded', 'Stale', 'Utility', 'Local Work', 'Total MH');
  1854. doforeach('summary', 'summary information', $sum, array(), false);
  1855. endtable();
  1856. otherrow('<td><br><br></td>');
  1857. newtable();
  1858. doforeach('devs', 'device list', $sum, array(''=>'','ID'=>'','Name'=>''), false);
  1859. endtable();
  1860. otherrow('<td><br><br></td>');
  1861. newtable();
  1862. doforeach('pools', 'pool list', $sum, array(''=>''), false);
  1863. endtable();
  1864. if ($placebuttons == 'bot' || $placebuttons == 'both')
  1865. pagebuttons(null, null);
  1866. }
  1867. #
  1868. display();
  1869. #
  1870. ?>
  1871. </table></td></tr></table>
  1872. </body></html>