miner.php 45 KB

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