miner.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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="<? 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. $poolcmd = array( 'Switch to' => 'switchpool',
  209. 'Enable' => 'enablepool',
  210. 'Disable' => 'disablepool' );
  211. foreach ($list as $item => $values)
  212. {
  213. if ($item != 'STATUS')
  214. {
  215. $section = $item;
  216. echo '<tr>';
  217. foreach ($values as $name => $value)
  218. {
  219. if ($name == '0')
  220. $name = '&nbsp;';
  221. echo "<td valign=bottom class=h>$name</td>";
  222. }
  223. if ($cmd == 'pools')
  224. foreach ($poolcmd as $name => $pcmd)
  225. echo "<td valign=bottom class=h>$name</td>";
  226. echo '</tr>';
  227. break;
  228. }
  229. }
  230. foreach ($list as $item => $values)
  231. {
  232. if ($item == 'STATUS')
  233. continue;
  234. echo '<tr>';
  235. foreach ($values as $name => $value)
  236. echo '<td>'.fmt($section, $name, $value).'</td>';
  237. if ($cmd == 'pools')
  238. {
  239. reset($values);
  240. $pool = current($values);
  241. foreach ($poolcmd as $name => $pcmd)
  242. {
  243. echo '<td>';
  244. if ($pool === false)
  245. echo '&nbsp;';
  246. else
  247. {
  248. echo "<input type=button value='Pool $pool'";
  249. echo " onclick='prc(\"$pcmd|$pool\",\"$name Pool $pool\")'>";
  250. }
  251. echo '</td>';
  252. }
  253. }
  254. echo '</tr>';
  255. }
  256. echo $te;
  257. }
  258. #
  259. global $devs;
  260. $devs = null;
  261. #
  262. function gpubuttons($count, $info)
  263. {
  264. global $devs;
  265. $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  266. $options = array( 'intensity' => 'Intensity',
  267. 'fan' => 'Fan Percent',
  268. 'engine' => 'GPU Clock',
  269. 'mem' => 'Memory Clock',
  270. 'vddc' => 'GPU Voltage' );
  271. $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
  272. $te = '</table></td></tr>';
  273. echo $tb.'<tr>';
  274. foreach ($basic as $head)
  275. echo "<td>$head</td>";
  276. foreach ($options as $name => $des)
  277. echo "<td nowrap>$des</td>";
  278. $n = 0;
  279. for ($c = 0; $c < $count; $c++)
  280. {
  281. echo '</tr><tr>';
  282. foreach ($basic as $name)
  283. {
  284. echo '<td>';
  285. if ($name == 'GPU')
  286. echo $c;
  287. else
  288. {
  289. echo "<input type=button value='$name $c' onclick='prs(\"gpu";
  290. echo strtolower($name);
  291. echo "|$c\")'>";
  292. }
  293. echo '</td>';
  294. }
  295. foreach ($options as $name => $des)
  296. {
  297. echo '<td>';
  298. if (!isset($devs["GPU$c"][$des]))
  299. echo '&nbsp;';
  300. else
  301. {
  302. $value = $devs["GPU$c"][$des];
  303. echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n)'>";
  304. echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
  305. $n++;
  306. }
  307. echo '</td>';
  308. }
  309. }
  310. echo '</tr>'.$te;
  311. }
  312. #
  313. function processgpus($rd, $ro)
  314. {
  315. global $error;
  316. $gpus = api('gpucount');
  317. if ($error != null)
  318. echo '<tr><td>Error getting GPU count: '.$rd.$error.$ro.'</td></tr>';
  319. else
  320. {
  321. if (!isset($gpus['GPUS']['Count']))
  322. echo '<tr><td>No GPU count returned: '.$rd.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
  323. else
  324. {
  325. $count = $gpus['GPUS']['Count'];
  326. if ($count == 0)
  327. echo '<tr><td>No GPUs</td></tr>';
  328. else
  329. gpubuttons($count);
  330. }
  331. }
  332. }
  333. #
  334. function process($cmds, $rd, $ro)
  335. {
  336. global $error, $devs;
  337. foreach ($cmds as $cmd => $des)
  338. {
  339. $process = api($cmd);
  340. if ($error != null)
  341. {
  342. echo "<tr><td>Error getting $des: ";
  343. echo $rd.$error.$ro.'</td></tr>';
  344. break;
  345. }
  346. else
  347. {
  348. details($cmd, $process);
  349. echo '<tr><td><br><br></td></tr>';
  350. if ($cmd == 'devs')
  351. $devs = $process;
  352. }
  353. }
  354. }
  355. #
  356. function display()
  357. {
  358. global $error;
  359. $error = null;
  360. $rd = '<font color=red><b>';
  361. $ro = '</b></font>';
  362. echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  363. echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
  364. echo "</td><td width=100%>&nbsp;</td><td>";
  365. echo "<input type=button value='Quit' onclick='prc(\"quit\",\"Quit CGMiner\")'>";
  366. echo "</td></tr></table></td></tr>";
  367. $arg = trim(getparam('arg', true));
  368. if ($arg != null and $arg != '')
  369. process(array($arg => $arg), $rd, $ro);
  370. $cmds = array( 'devs' => 'device list',
  371. 'summary' => 'summary information',
  372. 'pools' => 'pool list',
  373. 'config' => 'cgminer config');
  374. process($cmds, $rd, $ro);
  375. if ($error == null)
  376. processgpus($rd, $ro);
  377. }
  378. #
  379. display();
  380. #
  381. ?>
  382. </table></td></tr></table>
  383. </body></html>