miner.php 8.4 KB

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