miner.php 45 KB

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