miner.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. session_start();
  3. #
  4. global $miner, $port;
  5. $miner = '127.0.0.1'; # hostname or IP address
  6. $port = 4028;
  7. #
  8. $here = $_SERVER['PHP_SELF'];
  9. #
  10. ?>
  11. <html><head><title>Mine</title>
  12. <style type='text/css'>
  13. td { color:blue; font-family:verdana,arial,sans; font-size:14pt; }
  14. td.h { color:blue; font-family:verdana,arial,sans; font-size:14pt; background:#d0ffff }
  15. td.sta { color:green; font-family:verdana,arial,sans; font-size:14pt; }
  16. </style>
  17. </head><body bgcolor=#ecffff>
  18. <script type='text/javascript'>
  19. function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location="<? echo $here ?>"+a}
  20. 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];pr('?arg='+a,m)}
  21. </script>
  22. <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
  23. <tr><td align=center valign=top>
  24. <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
  25. <?
  26. #
  27. global $error;
  28. $error = null;
  29. #
  30. function getsock($addr, $port)
  31. {
  32. global $error;
  33. $socket = null;
  34. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  35. if ($socket === false || $socket === null)
  36. {
  37. $error = socket_strerror(socket_last_error());
  38. $msg = "socket create(TCP) failed";
  39. $error = "ERR: $msg '$error'\n";
  40. return null;
  41. }
  42. $res = socket_connect($socket, $addr, $port);
  43. if ($res === false)
  44. {
  45. $error = socket_strerror(socket_last_error());
  46. $msg = "socket connect($addr,$port) failed";
  47. $error = "ERR: $msg '$error'\n";
  48. socket_close($socket);
  49. return null;
  50. }
  51. return $socket;
  52. }
  53. #
  54. function readsockline($socket)
  55. {
  56. $line = '';
  57. while (true)
  58. {
  59. $byte = socket_read($socket, 1);
  60. if ($byte === false || $byte === '')
  61. break;
  62. if ($byte === "\0")
  63. break;
  64. $line .= $byte;
  65. }
  66. return $line;
  67. }
  68. #
  69. function api($cmd)
  70. {
  71. global $miner, $port;
  72. $socket = getsock($miner, $port);
  73. if ($socket != null)
  74. {
  75. socket_write($socket, $cmd, strlen($cmd));
  76. $line = readsockline($socket);
  77. socket_close($socket);
  78. if (strlen($line) == 0)
  79. {
  80. $error = "WARN: '$cmd' returned nothing\n";
  81. return $line;
  82. }
  83. # print "$cmd returned '$line'\n";
  84. $data = array();
  85. $objs = explode('|', $line);
  86. foreach ($objs as $obj)
  87. {
  88. if (strlen($obj) > 0)
  89. {
  90. $items = explode(',', $obj);
  91. $item = $items[0];
  92. $id = explode('=', $items[0], 2);
  93. if (count($id) == 1 or !ctype_digit($id[1]))
  94. $name = $id[0];
  95. else
  96. $name = $id[0].$id[1];
  97. if (strlen($name) == 0)
  98. $name = 'null';
  99. if (isset($data[$name]))
  100. {
  101. $num = 1;
  102. while (isset($data[$name.$num]))
  103. $num++;
  104. $name .= $num;
  105. }
  106. $counter = 0;
  107. foreach ($items as $item)
  108. {
  109. $id = explode('=', $item, 2);
  110. if (count($id) == 2)
  111. $data[$name][$id[0]] = $id[1];
  112. else
  113. $data[$name][$counter] = $id[0];
  114. $counter++;
  115. }
  116. }
  117. }
  118. return $data;
  119. }
  120. return null;
  121. }
  122. #
  123. function getparam($name, $both = false)
  124. {
  125. $a = null;
  126. if (isset($_POST[$name]))
  127. $a = $_POST[$name];
  128. if (($both === true) and ($a === null))
  129. {
  130. if (isset($_GET[$name]))
  131. $a = $_GET[$name];
  132. }
  133. if ($a == '' || $a == null)
  134. return null;
  135. // limit to 1K just to be safe
  136. return substr($a, 0, 1024);
  137. }
  138. #
  139. function fmt($section, $name, $value)
  140. {
  141. $b = '&nbsp;';
  142. switch ($section.'.'.$name)
  143. {
  144. case 'SUMMARY.Elapsed':
  145. $s = $value % 60;
  146. $value -= $s;
  147. $value /= 60;
  148. if ($value == 0)
  149. {
  150. return $s.'s';
  151. }
  152. else
  153. {
  154. $m = $value % 60;
  155. $value -= $m;
  156. $value /= 60;
  157. if ($value == 0)
  158. {
  159. return sprintf("%dm$b%02ds", $m, $s);
  160. }
  161. else
  162. {
  163. $h = $value % 24;
  164. $value -= $h;
  165. $value /= 24;
  166. if ($value == 0)
  167. return sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  168. else
  169. return sprintf("%ddays$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  170. }
  171. }
  172. break;
  173. case 'GPU0.Utility':
  174. case 'SUMMARY.Utility':
  175. return $value.'/m';
  176. break;
  177. case 'GPU0.Temperature':
  178. return $value.'&deg;C';
  179. break;
  180. }
  181. return $value;
  182. }
  183. #
  184. function details($list)
  185. {
  186. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  187. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  188. $te = '</table></td></tr>';
  189. echo $tb;
  190. echo '<tr><td class=sta>Date: '.date('H:i:s j-M-Y \U\T\CP').'</td></tr>';
  191. echo $te.$tb;
  192. if (isset($list['STATUS']))
  193. {
  194. echo '<tr>';
  195. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  196. $sta = $list['STATUS']['STATUS'];
  197. echo '<td>Status: '.$stas[$sta].'</td>';
  198. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  199. echo '</tr>';
  200. }
  201. echo $te.$tb;
  202. $section = '';
  203. foreach ($list as $item => $values)
  204. {
  205. if ($item != 'STATUS')
  206. {
  207. $section = $item;
  208. echo '<tr>';
  209. foreach ($values as $name => $value)
  210. {
  211. if ($name == '0')
  212. $name = '&nbsp;';
  213. echo "<td valign=bottom class=h>$name</td>";
  214. }
  215. echo '</tr>';
  216. break;
  217. }
  218. }
  219. foreach ($list as $item => $values)
  220. {
  221. if ($item == 'STATUS')
  222. continue;
  223. foreach ($values as $name => $value)
  224. echo '<td>'.fmt($section, $name, $value).'</td>';
  225. echo '</tr>';
  226. }
  227. echo $te;
  228. }
  229. #
  230. function gpubuttons($count)
  231. {
  232. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  233. $te = '</table></td></tr>';
  234. echo $tb.'<tr>';
  235. for ($i = 0; $i < 4; $i++)
  236. {
  237. echo '<td>';
  238. if ($i == 0)
  239. echo 'GPU';
  240. else
  241. echo '&nbsp;';
  242. echo '</td>';
  243. }
  244. for ($c = 0; $c < $count; $c++)
  245. {
  246. echo '</tr><tr>';
  247. echo "<td>$c</td>";
  248. echo "<td><input type=button value='Enable $c' onclick='prs(\"gpuenable|$c\")'></td>";
  249. echo "<td><input type=button value='Disable $c' onclick='prs(\"gpudisable|$c\")'></td>";
  250. echo "<td><input type=button value='Restart $c' onclick='prs(\"gpurestart|$c\")'></td>";
  251. }
  252. echo '</tr>'.$te;
  253. }
  254. #
  255. function processgpus($rd, $ro)
  256. {
  257. global $error;
  258. $gpus = api('gpucount');
  259. if ($error != null)
  260. echo '<tr><td>Error getting GPU count: '.$rd.$error.$ro.'</td></tr>';
  261. else
  262. {
  263. if (!isset($gpus['GPUS']['Count']))
  264. echo '<tr><td>No GPU count returned: '.$rd.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
  265. else
  266. {
  267. $count = $gpus['GPUS']['Count'];
  268. if ($count == 0)
  269. echo '<tr><td>No GPUs</td></tr>';
  270. else
  271. gpubuttons($count);
  272. }
  273. }
  274. }
  275. #
  276. function process($cmds, $rd, $ro)
  277. {
  278. global $error;
  279. foreach ($cmds as $cmd => $des)
  280. {
  281. $process = api($cmd);
  282. if ($error != null)
  283. {
  284. echo "<tr><td>Error getting $des: ";
  285. echo $rd.$error.$ro.'</td></tr>';
  286. break;
  287. }
  288. else
  289. {
  290. details($process);
  291. echo '<tr><td><br><br></td></tr>';
  292. }
  293. }
  294. }
  295. #
  296. function display()
  297. {
  298. global $error;
  299. $error = null;
  300. $rd = '<font color=red><b>';
  301. $ro = '</b></font>';
  302. echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  303. echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
  304. echo "</td><td width=100%>&nbsp;</td><td>";
  305. echo "<input type=button value='Quit' onclick='pr(\"?arg=quit\",\"Quit CGMiner\")'>";
  306. echo "</td></tr></table></td></tr>";
  307. $arg = trim(getparam('arg', true));
  308. if ($arg != null and $arg != '')
  309. process(array($arg => $arg), $rd, $ro);
  310. $cmds = array( 'devs' => 'device list',
  311. 'summary' => 'summary information',
  312. 'pools' => 'pool list');
  313. process($cmds, $rd, $ro);
  314. if ($error == null)
  315. processgpus($rd, $ro);
  316. }
  317. #
  318. display();
  319. #
  320. ?>
  321. </table></td></tr></table>
  322. </body></html>