miner.php 6.1 KB

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