miner.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. session_start();
  3. #
  4. global $miner, $port, $readonly, $notify;
  5. $miner = '127.0.0.1'; # hostname or IP address
  6. $port = 4028;
  7. #
  8. # Set $readonly to true to force miner.php to be readonly
  9. # Set $readonly to false then it will check cgminer 'privileged'
  10. $readonly = false;
  11. #
  12. # Set $notify to false to NOT attempt to display the notify command
  13. # Set $notify to true to attempt to display the notify command
  14. # If your older version of cgminer returns an 'Invalid command'
  15. # coz it doesn't have notify - it just shows the error status table
  16. $notify = true;
  17. #
  18. $here = $_SERVER['PHP_SELF'];
  19. #
  20. function htmlhead()
  21. {
  22. global $error, $readonly, $here;
  23. if ($readonly === false)
  24. {
  25. $access = api('privileged');
  26. if ($error != null
  27. || !isset($access['STATUS']['STATUS'])
  28. || $access['STATUS']['STATUS'] != 'S')
  29. $readonly = true;
  30. }
  31. ?>
  32. <html><head><title>Mine</title>
  33. <style type='text/css'>
  34. td { color:blue; font-family:verdana,arial,sans; font-size:13pt; }
  35. td.h { color:blue; font-family:verdana,arial,sans; font-size:13pt; background:#d0ffff }
  36. td.err { color:black; font-family:verdana,arial,sans; font-size:13pt; background:#ff3050 }
  37. td.warn { color:black; font-family:verdana,arial,sans; font-size:13pt; background:#ffb050 }
  38. td.sta { color:green; font-family:verdana,arial,sans; font-size:13pt; }
  39. </style>
  40. </head><body bgcolor=#ecffff>
  41. <script type='text/javascript'>
  42. function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location="<?php echo $here ?>"+a}
  43. <?php
  44. if ($readonly === false)
  45. {
  46. ?>
  47. function prc(a,m){pr('?arg='+a,m)}
  48. function prs(a){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,m)}
  49. function prs2(a,n){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,m)}
  50. <?php
  51. }
  52. ?>
  53. </script>
  54. <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
  55. <tr><td align=center valign=top>
  56. <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
  57. <?php
  58. }
  59. #
  60. global $error;
  61. $error = null;
  62. #
  63. function getsock($addr, $port)
  64. {
  65. global $error;
  66. $socket = null;
  67. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  68. if ($socket === false || $socket === null)
  69. {
  70. $error = socket_strerror(socket_last_error());
  71. $msg = "socket create(TCP) failed";
  72. $error = "ERR: $msg '$error'\n";
  73. return null;
  74. }
  75. $res = socket_connect($socket, $addr, $port);
  76. if ($res === false)
  77. {
  78. $error = socket_strerror(socket_last_error());
  79. $msg = "socket connect($addr,$port) failed";
  80. $error = "ERR: $msg '$error'\n";
  81. socket_close($socket);
  82. return null;
  83. }
  84. return $socket;
  85. }
  86. #
  87. function readsockline($socket)
  88. {
  89. $line = '';
  90. while (true)
  91. {
  92. $byte = socket_read($socket, 1);
  93. if ($byte === false || $byte === '')
  94. break;
  95. if ($byte === "\0")
  96. break;
  97. $line .= $byte;
  98. }
  99. return $line;
  100. }
  101. #
  102. function api($cmd)
  103. {
  104. global $miner, $port;
  105. $socket = getsock($miner, $port);
  106. if ($socket != null)
  107. {
  108. socket_write($socket, $cmd, strlen($cmd));
  109. $line = readsockline($socket);
  110. socket_close($socket);
  111. if (strlen($line) == 0)
  112. {
  113. $error = "WARN: '$cmd' returned nothing\n";
  114. return $line;
  115. }
  116. # print "$cmd returned '$line'\n";
  117. $data = array();
  118. $objs = explode('|', $line);
  119. foreach ($objs as $obj)
  120. {
  121. if (strlen($obj) > 0)
  122. {
  123. $items = explode(',', $obj);
  124. $item = $items[0];
  125. $id = explode('=', $items[0], 2);
  126. if (count($id) == 1 or !ctype_digit($id[1]))
  127. $name = $id[0];
  128. else
  129. $name = $id[0].$id[1];
  130. if (strlen($name) == 0)
  131. $name = 'null';
  132. if (isset($data[$name]))
  133. {
  134. $num = 1;
  135. while (isset($data[$name.$num]))
  136. $num++;
  137. $name .= $num;
  138. }
  139. $counter = 0;
  140. foreach ($items as $item)
  141. {
  142. $id = explode('=', $item, 2);
  143. if (count($id) == 2)
  144. $data[$name][$id[0]] = $id[1];
  145. else
  146. $data[$name][$counter] = $id[0];
  147. $counter++;
  148. }
  149. }
  150. }
  151. return $data;
  152. }
  153. return null;
  154. }
  155. #
  156. function getparam($name, $both = false)
  157. {
  158. $a = null;
  159. if (isset($_POST[$name]))
  160. $a = $_POST[$name];
  161. if (($both === true) and ($a === null))
  162. {
  163. if (isset($_GET[$name]))
  164. $a = $_GET[$name];
  165. }
  166. if ($a == '' || $a == null)
  167. return null;
  168. // limit to 1K just to be safe
  169. return substr($a, 0, 1024);
  170. }
  171. #
  172. function fmt($section, $name, $value)
  173. {
  174. $errorclass = ' class=err';
  175. $warnclass = ' class=warn';
  176. $b = '&nbsp;';
  177. $ret = $value;
  178. $class = '';
  179. switch ($section.'.'.$name)
  180. {
  181. case 'GPU.Last Share Time':
  182. case 'PGA.Last Share Time':
  183. $ret = date('H:i:s', $value);
  184. break;
  185. case 'SUMMARY.Elapsed':
  186. $s = $value % 60;
  187. $value -= $s;
  188. $value /= 60;
  189. if ($value == 0)
  190. $ret = $s.'s';
  191. else
  192. {
  193. $m = $value % 60;
  194. $value -= $m;
  195. $value /= 60;
  196. if ($value == 0)
  197. $ret = sprintf("%dm$b%02ds", $m, $s);
  198. else
  199. {
  200. $h = $value % 24;
  201. $value -= $h;
  202. $value /= 24;
  203. if ($value == 0)
  204. $ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  205. else
  206. $ret = sprintf("%ddays$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  207. }
  208. }
  209. break;
  210. case 'NOTIFY.Last Well':
  211. if ($value == '0')
  212. {
  213. $ret = 'Never';
  214. $class = $warnclass;
  215. }
  216. else
  217. $ret = date('H:i:s', $value);
  218. break;
  219. case 'NOTIFY.Last Not Well':
  220. if ($value == '0')
  221. $ret = 'Never';
  222. else
  223. {
  224. $ret = date('H:i:s', $value);
  225. $class = $errorclass;
  226. }
  227. break;
  228. case 'NOTIFY.Reason Not Well':
  229. if ($value != 'None')
  230. $class = $errorclass;
  231. break;
  232. case 'GPU.Utility':
  233. case 'PGA.Utility':
  234. case 'SUMMARY.Utility':
  235. $ret = $value.'/m';
  236. break;
  237. case 'GPU.Temperature':
  238. case 'PGA.Temperature':
  239. $ret = $value.'&deg;C';
  240. break;
  241. }
  242. if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
  243. $class = $errorclass;
  244. return array($ret, $class);
  245. }
  246. #
  247. global $poolcmd;
  248. $poolcmd = array( 'Switch to' => 'switchpool',
  249. 'Enable' => 'enablepool',
  250. 'Disable' => 'disablepool' );
  251. #
  252. function showhead($cmd, $item, $values)
  253. {
  254. global $poolcmd, $readonly;
  255. echo '<tr>';
  256. foreach ($values as $name => $value)
  257. {
  258. if ($name == '0')
  259. $name = '&nbsp;';
  260. echo "<td valign=bottom class=h>$name</td>";
  261. }
  262. if ($cmd == 'pools' && $readonly === false)
  263. foreach ($poolcmd as $name => $pcmd)
  264. echo "<td valign=bottom class=h>$name</td>";
  265. echo '</tr>';
  266. }
  267. #
  268. function details($cmd, $list)
  269. {
  270. global $poolcmd, $readonly;
  271. $dfmt = 'H:i:s j-M-Y \U\T\CP';
  272. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  273. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  274. $te = '</table></td></tr>';
  275. echo $tb;
  276. echo '<tr><td class=sta>Date: '.date($dfmt).'</td></tr>';
  277. echo $te.$tb;
  278. if (isset($list['STATUS']))
  279. {
  280. echo '<tr>';
  281. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  282. if (isset($list['STATUS']['When']))
  283. echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
  284. $sta = $list['STATUS']['STATUS'];
  285. echo '<td>Status: '.$stas[$sta].'</td>';
  286. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  287. echo '</tr>';
  288. }
  289. $section = '';
  290. foreach ($list as $item => $values)
  291. {
  292. if ($item == 'STATUS')
  293. continue;
  294. $sectionname = preg_replace('/\d/', '', $item);
  295. if ($sectionname != $section)
  296. {
  297. echo $te.$tb;
  298. showhead($cmd, $item, $values);
  299. $section = $sectionname;
  300. }
  301. echo '<tr>';
  302. foreach ($values as $name => $value)
  303. {
  304. list($showvalue, $class) = fmt($section, $name, $value);
  305. echo "<td$class>$showvalue</td>";
  306. }
  307. if ($cmd == 'pools' && $readonly === false)
  308. {
  309. reset($values);
  310. $pool = current($values);
  311. foreach ($poolcmd as $name => $pcmd)
  312. {
  313. echo '<td>';
  314. if ($pool === false)
  315. echo '&nbsp;';
  316. else
  317. {
  318. echo "<input type=button value='Pool $pool'";
  319. echo " onclick='prc(\"$pcmd|$pool\",\"$name Pool $pool\")'>";
  320. }
  321. echo '</td>';
  322. }
  323. }
  324. echo '</tr>';
  325. }
  326. echo $te;
  327. }
  328. #
  329. global $devs;
  330. $devs = null;
  331. #
  332. function gpubuttons($count)
  333. {
  334. global $devs;
  335. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  336. $options = array( 'intensity' => 'Intensity',
  337. 'fan' => 'Fan Percent',
  338. 'engine' => 'GPU Clock',
  339. 'mem' => 'Memory Clock',
  340. 'vddc' => 'GPU Voltage' );
  341. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  342. $te = '</table></td></tr>';
  343. echo $tb.'<tr>';
  344. foreach ($basic as $head)
  345. echo "<td>$head</td>";
  346. foreach ($options as $name => $des)
  347. echo "<td nowrap>$des</td>";
  348. $n = 0;
  349. for ($c = 0; $c < $count; $c++)
  350. {
  351. echo '</tr><tr>';
  352. foreach ($basic as $name)
  353. {
  354. echo '<td>';
  355. if ($name == 'GPU')
  356. echo $c;
  357. else
  358. {
  359. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  360. echo strtolower($name);
  361. echo "|$c\")'>";
  362. }
  363. echo '</td>';
  364. }
  365. foreach ($options as $name => $des)
  366. {
  367. echo '<td>';
  368. if (!isset($devs["GPU$c"][$des]))
  369. echo '&nbsp;';
  370. else
  371. {
  372. $value = $devs["GPU$c"][$des];
  373. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n)'>";
  374. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  375. $n++;
  376. }
  377. echo '</td>';
  378. }
  379. }
  380. echo '</tr>'.$te;
  381. }
  382. #
  383. function processgpus($rd, $ro)
  384. {
  385. global $error;
  386. $gpus = api('gpucount');
  387. if ($error != null)
  388. echo '<tr><td>Error getting GPU count: '.$rd.$error.$ro.'</td></tr>';
  389. else
  390. {
  391. if (!isset($gpus['GPUS']['Count']))
  392. echo '<tr><td>No GPU count returned: '.$rd.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
  393. else
  394. {
  395. $count = $gpus['GPUS']['Count'];
  396. if ($count == 0)
  397. echo '<tr><td>No GPUs</td></tr>';
  398. else
  399. gpubuttons($count);
  400. }
  401. }
  402. }
  403. #
  404. function process($cmds, $rd, $ro)
  405. {
  406. global $error, $devs;
  407. foreach ($cmds as $cmd => $des)
  408. {
  409. $process = api($cmd);
  410. if ($error != null)
  411. {
  412. echo "<tr><td>Error getting $des: ";
  413. echo $rd.$error.$ro.'</td></tr>';
  414. break;
  415. }
  416. else
  417. {
  418. details($cmd, $process);
  419. echo '<tr><td><br><br></td></tr>';
  420. if ($cmd == 'devs')
  421. $devs = $process;
  422. }
  423. }
  424. }
  425. #
  426. function display()
  427. {
  428. global $error, $readonly, $notify;
  429. $error = null;
  430. $rd = '<font color=red><b>';
  431. $ro = '</b></font>';
  432. echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  433. echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
  434. echo "</td><td width=100%>&nbsp;</td><td>";
  435. if ($readonly === false)
  436. echo "<input type=button value='Quit' onclick='prc(\"quit\",\"Quit CGMiner\")'>";
  437. echo "</td></tr></table></td></tr>";
  438. $arg = trim(getparam('arg', true));
  439. if ($arg != null and $arg != '')
  440. process(array($arg => $arg), $rd, $ro);
  441. $cmds = array( 'devs' => 'device list',
  442. 'summary' => 'summary information',
  443. 'pools' => 'pool list');
  444. if ($notify)
  445. $cmds['notify'] = 'device status';
  446. $cmds['config'] = 'cgminer config';
  447. process($cmds, $rd, $ro);
  448. if ($error == null && $readonly === false)
  449. processgpus($rd, $ro);
  450. }
  451. #
  452. htmlhead();
  453. display();
  454. #
  455. ?>
  456. </table></td></tr></table>
  457. </body></html>