miner.php 46 KB

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