miner.php 54 KB

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