miner.php 9.3 KB

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