miner.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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:13pt; }
  14. td.h { color:blue; font-family:verdana,arial,sans; font-size:13pt; background:#d0ffff }
  15. td.sta { color:green; font-family:verdana,arial,sans; font-size:13pt; }
  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="<?php 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. <?php
  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 'GPU.Last Share Time':
  147. case 'PGA.Last Share Time':
  148. return date('H:i:s', $value);
  149. break;
  150. case 'SUMMARY.Elapsed':
  151. $s = $value % 60;
  152. $value -= $s;
  153. $value /= 60;
  154. if ($value == 0)
  155. {
  156. return $s.'s';
  157. }
  158. else
  159. {
  160. $m = $value % 60;
  161. $value -= $m;
  162. $value /= 60;
  163. if ($value == 0)
  164. {
  165. return sprintf("%dm$b%02ds", $m, $s);
  166. }
  167. else
  168. {
  169. $h = $value % 24;
  170. $value -= $h;
  171. $value /= 24;
  172. if ($value == 0)
  173. return sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
  174. else
  175. return sprintf("%ddays$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
  176. }
  177. }
  178. break;
  179. case 'GPU.Utility':
  180. case 'PGA.Utility':
  181. case 'SUMMARY.Utility':
  182. return $value.'/m';
  183. break;
  184. case 'GPU.Temperature':
  185. case 'PGA.Temperature':
  186. return $value.'&deg;C';
  187. break;
  188. }
  189. return $value;
  190. }
  191. #
  192. global $poolcmd;
  193. $poolcmd = array( 'Switch to' => 'switchpool',
  194. 'Enable' => 'enablepool',
  195. 'Disable' => 'disablepool' );
  196. #
  197. function showhead($cmd, $item, $values)
  198. {
  199. global $poolcmd;
  200. echo '<tr>';
  201. foreach ($values as $name => $value)
  202. {
  203. if ($name == '0')
  204. $name = '&nbsp;';
  205. echo "<td valign=bottom class=h>$name</td>";
  206. }
  207. if ($cmd == 'pools')
  208. foreach ($poolcmd as $name => $pcmd)
  209. echo "<td valign=bottom class=h>$name</td>";
  210. echo '</tr>';
  211. }
  212. #
  213. function details($cmd, $list)
  214. {
  215. global $poolcmd;
  216. $dfmt = 'H:i:s j-M-Y \U\T\CP';
  217. $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
  218. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  219. $te = '</table></td></tr>';
  220. echo $tb;
  221. echo '<tr><td class=sta>Date: '.date($dfmt).'</td></tr>';
  222. echo $te.$tb;
  223. if (isset($list['STATUS']))
  224. {
  225. echo '<tr>';
  226. echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
  227. if (isset($list['STATUS']['When']))
  228. echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
  229. $sta = $list['STATUS']['STATUS'];
  230. echo '<td>Status: '.$stas[$sta].'</td>';
  231. echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
  232. echo '</tr>';
  233. }
  234. $section = '';
  235. foreach ($list as $item => $values)
  236. {
  237. if ($item == 'STATUS')
  238. continue;
  239. $sectionname = ereg_replace('[0-9]', '', $item);
  240. if ($sectionname != $section)
  241. {
  242. echo $te.$tb;
  243. showhead($cmd, $item, $values);
  244. $section = $sectionname;
  245. }
  246. echo '<tr>';
  247. foreach ($values as $name => $value)
  248. echo '<td>'.fmt($section, $name, $value).'</td>';
  249. if ($cmd == 'pools')
  250. {
  251. reset($values);
  252. $pool = current($values);
  253. foreach ($poolcmd as $name => $pcmd)
  254. {
  255. echo '<td>';
  256. if ($pool === false)
  257. echo '&nbsp;';
  258. else
  259. {
  260. echo "<input type=button value='Pool $pool'";
  261. echo " onclick='prc(\"$pcmd|$pool\",\"$name Pool $pool\")'>";
  262. }
  263. echo '</td>';
  264. }
  265. }
  266. echo '</tr>';
  267. }
  268. echo $te;
  269. }
  270. #
  271. global $devs;
  272. $devs = null;
  273. #
  274. function gpubuttons($count, $info)
  275. {
  276. global $devs;
  277. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  278. $options = array( 'intensity' => 'Intensity',
  279. 'fan' => 'Fan Percent',
  280. 'engine' => 'GPU Clock',
  281. 'mem' => 'Memory Clock',
  282. 'vddc' => 'GPU Voltage' );
  283. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  284. $te = '</table></td></tr>';
  285. echo $tb.'<tr>';
  286. foreach ($basic as $head)
  287. echo "<td>$head</td>";
  288. foreach ($options as $name => $des)
  289. echo "<td nowrap>$des</td>";
  290. $n = 0;
  291. for ($c = 0; $c < $count; $c++)
  292. {
  293. echo '</tr><tr>';
  294. foreach ($basic as $name)
  295. {
  296. echo '<td>';
  297. if ($name == 'GPU')
  298. echo $c;
  299. else
  300. {
  301. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  302. echo strtolower($name);
  303. echo "|$c\")'>";
  304. }
  305. echo '</td>';
  306. }
  307. foreach ($options as $name => $des)
  308. {
  309. echo '<td>';
  310. if (!isset($devs["GPU$c"][$des]))
  311. echo '&nbsp;';
  312. else
  313. {
  314. $value = $devs["GPU$c"][$des];
  315. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n)'>";
  316. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  317. $n++;
  318. }
  319. echo '</td>';
  320. }
  321. }
  322. echo '</tr>'.$te;
  323. }
  324. #
  325. function processgpus($rd, $ro)
  326. {
  327. global $error;
  328. $gpus = api('gpucount');
  329. if ($error != null)
  330. echo '<tr><td>Error getting GPU count: '.$rd.$error.$ro.'</td></tr>';
  331. else
  332. {
  333. if (!isset($gpus['GPUS']['Count']))
  334. echo '<tr><td>No GPU count returned: '.$rd.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
  335. else
  336. {
  337. $count = $gpus['GPUS']['Count'];
  338. if ($count == 0)
  339. echo '<tr><td>No GPUs</td></tr>';
  340. else
  341. gpubuttons($count);
  342. }
  343. }
  344. }
  345. #
  346. function process($cmds, $rd, $ro)
  347. {
  348. global $error, $devs;
  349. foreach ($cmds as $cmd => $des)
  350. {
  351. $process = api($cmd);
  352. if ($error != null)
  353. {
  354. echo "<tr><td>Error getting $des: ";
  355. echo $rd.$error.$ro.'</td></tr>';
  356. break;
  357. }
  358. else
  359. {
  360. details($cmd, $process);
  361. echo '<tr><td><br><br></td></tr>';
  362. if ($cmd == 'devs')
  363. $devs = $process;
  364. }
  365. }
  366. }
  367. #
  368. function display()
  369. {
  370. global $error;
  371. $error = null;
  372. $rd = '<font color=red><b>';
  373. $ro = '</b></font>';
  374. echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  375. echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
  376. echo "</td><td width=100%>&nbsp;</td><td>";
  377. echo "<input type=button value='Quit' onclick='prc(\"quit\",\"Quit CGMiner\")'>";
  378. echo "</td></tr></table></td></tr>";
  379. $arg = trim(getparam('arg', true));
  380. if ($arg != null and $arg != '')
  381. process(array($arg => $arg), $rd, $ro);
  382. $cmds = array( 'devs' => 'device list',
  383. 'summary' => 'summary information',
  384. 'pools' => 'pool list',
  385. 'config' => 'cgminer config');
  386. process($cmds, $rd, $ro);
  387. if ($error == null)
  388. processgpus($rd, $ro);
  389. }
  390. #
  391. display();
  392. #
  393. ?>
  394. </table></td></tr></table>
  395. </body></html>