miner.php 8.2 KB

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