miner.php 7.8 KB

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