-
Notifications
You must be signed in to change notification settings - Fork 9
/
eth_log_gen.php
461 lines (331 loc) · 17.9 KB
/
eth_log_gen.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/php
<?php
define('DEBUG_MODE', false);
define('SERVICE_URL', 'http://localhost'); // Claymore API
define('SERVICE_PORT', 3333); // Claymore API port
define('RUN_INDEFINITELY', false); // If set to true script will loop indefinitely and continuously append to logs
define('UPDATE_FREQ', 10); // Scan and generate log every 10s if RUN_INDEFINITELY=true
define('MACHINE_NAME', 'minr');
define('FILE_LOG_GPUS', './gpu.log');
define('FILE_LOG_TOTALS', './totals.log');
define('DUAL_CURRENCY', 'DCR'); // Just leave as-is if not dual mining
define('DUAL_CURRENCY_LOWER', strtolower(DUAL_CURRENCY)); // Leave as-is
define('INFLUXDB_ENABLED', true);
define('INFLUXDB_DB', 'telegraf');
define('INFLUXDB_SERVICE_URL', 'http://localhost');
define('INFLUXDB_SERVICE_PORT', 8086);
define('INFLUXDB_USERNAME', '');
define('INFLUXDB_PASSWORD', '');
define('INFLUXDB_USE_CREDENTIALS', false);
date_default_timezone_set('UTC');
while(true) {
$dual_mining_detected = false;
// Fetch data from Claymore API via cURL:
$curl_url = SERVICE_URL.':'.SERVICE_PORT;
$arr_curl_result = fetch_via_curl($curl_url);
$arr_log_line = array();
if ($arr_curl_result['quick_status'] == 'ok' && strlen($arr_curl_result['curl_exec']) > 0) {
// cURL-result ok. Strip HTML-tags:
$clean = strip_tags($arr_curl_result['curl_exec']);
// Reverse array
$arr_lines = array_reverse(explode("\n", $clean));
// Instantiate arrays that holds output data
$arr_gpu_info = $arr_totals_info = array();
$i = 0;
foreach ($arr_lines as $key => $line) {
if (substr($line, 0, 4) == 'GPU ') {
// If we encounter 'GPU ' we are done and can stop
break;
}
# ------------------------------------------------------------------ #
# Individual GPU's: id, temperatures, and fan speed
# ------------------------------------------------------------------ #
// Get temp. and fans of individual cards - GPU0 t=57C fan=23%, GPU1 t=67C fan=40%, GPU2 t=64C fan=70%, GPU3 t=73C fan=70%
if (substr($line, 0, 4) == 'GPU0') {
$arr_gpu_temp_fan = explode(', ', $line);
$gpu_number = 0;
foreach ($arr_gpu_temp_fan as $k_gpu => $gpu_info) {
$arr_gpu = explode(' ', $gpu_info);
$arr_gpu_info[$gpu_number] = array(
'id' => $arr_gpu[0],
'temp' => str_replace(array('t=', 'C'), "", $arr_gpu[1]),
'fan' => str_replace(array('fan=', '%'), "", $arr_gpu[2]),
);
$gpu_number++;
}
}
# ------------------------------------------------------------------ #
# DUAL CURRENCY: Get mining speed of individual cards
# ------------------------------------------------------------------ #
// DCR: GPU0 336.523 Mh/s, GPU1 352.541 Mh/s, GPU2 off, GPU3 off
if (stristr($line, DUAL_CURRENCY.': GPU') !== false) {
// It seems we are dual mining
$dual_mining_detected = true;
$clean_line = substr($line, 12);
$arr_gpu_speed = explode(', ', $clean_line);
$gpu_number = 0;
foreach ($arr_gpu_speed as $k_gpu => $gpu_info) {
$arr_gpu = explode(' ', $gpu_info);
if ($arr_gpu[1] == 'off') {
$arr_gpu[1] = 0;
}
$arr_gpu_info[$gpu_number][DUAL_CURRENCY_LOWER.'_speed'] = $arr_gpu[1];
$gpu_number++;
}
}
# ------------------------------------------------------------------ #
# DUAL CURRENCY: Get totals
# ------------------------------------------------------------------ #
// DCR - Total Speed: 685.462 Mh/s, Total Shares: 4831, Rejected: 69
if (stristr($line, DUAL_CURRENCY.' - Total Speed:') !== false) {
// It seems we are dual mining
$dual_mining_detected = true;
$clean_line = substr($line, 12);
$arr_totals = explode(' ', $clean_line);
$shares = str_replace(array('),', '('), array('', '+'), $arr_totals[7]);
$arr_shares = explode('+', $shares);
$arr_totals_info[DUAL_CURRENCY_LOWER.'_total_speed'] = $arr_totals[3];
$arr_totals_info[DUAL_CURRENCY_LOWER.'_total_shares'] = intval(str_replace(",", "", $arr_shares[0]));
$arr_totals_info[DUAL_CURRENCY_LOWER.'_total_rejected'] = intval($arr_totals[9]);
// Shares for individual cards
$arr_card_shares = array_slice($arr_shares, 1, count($arr_shares));
if (!empty($arr_card_shares)) {
foreach ($arr_card_shares as $k_gpu => $gpu_shares) {
$arr_gpu_info[$k_gpu][DUAL_CURRENCY_LOWER.'_shares'] = $gpu_shares;
}
}
// Let's calculate some averages for the totals array and set share percentage per card
if (!empty($arr_gpu_info)) {
$total_cards = count($arr_gpu_info);
$arr_totals_info[DUAL_CURRENCY_LOWER.'_avg_speed_per_card'] = round($arr_totals_info[DUAL_CURRENCY_LOWER.'_total_speed'] / $total_cards, 2);
$arr_totals_info[DUAL_CURRENCY_LOWER.'_avg_shares_per_card'] = round($arr_totals_info[DUAL_CURRENCY_LOWER.'_total_shares'] / $total_cards, 2);
$temp_total = $fan_total = 0;
foreach ($arr_gpu_info as $k_gpu => $gpu) {
if ($arr_totals_info[DUAL_CURRENCY_LOWER.'_total_shares'] > 0) {
$arr_gpu_info[$k_gpu][DUAL_CURRENCY_LOWER.'_shares_pct'] = round(100 * ($gpu[DUAL_CURRENCY_LOWER.'_shares'] ?? 0 / $arr_totals_info[DUAL_CURRENCY_LOWER.'_total_shares']), 2);
} else {
$arr_gpu_info[$k_gpu][DUAL_CURRENCY_LOWER.'_shares_pct'] = 0;
}
}
}
}
# ------------------------------------------------------------------ #
# ETH: Get mining speed of individual cards
# ------------------------------------------------------------------ #
// ETH: GPU0 28.780 Mh/s, GPU1 29.179 Mh/s, GPU2 24.029 Mh/s, GPU3 28.896 Mh/s
if (substr($line, 0, 8) == 'ETH: GPU') {
$clean_line = substr($line, 5);
$arr_gpu_speed = explode(', ', $clean_line);
$gpu_number = 0;
foreach ($arr_gpu_speed as $k_gpu => $gpu_info) {
$arr_gpu = explode(' ', $gpu_info);
if ($arr_gpu[1] == 'off') {
$arr_gpu[1] = 0;
}
$arr_gpu_info[$gpu_number]['eth_speed'] = $arr_gpu[1];
$gpu_number++;
}
}
// TODO "Incorrect ETH shares: GPU0 6, GPU1 6, GPU2 3, GPU3 23" + "Incorrect ETH shares: none"
# ------------------------------------------------------------------ #
# ETH: Get totals and also calculate avg. temperature and avg. fan
# ------------------------------------------------------------------ #
// ETH - Total Speed: 110.884 Mh/s, Total Shares: 271(78+84+57+57), Rejected: 0, Time: 02:47
if (substr($line, 0, strlen('ETH - Total Speed')) == 'ETH - Total Speed') {
$clean_line = substr($line, 5);
$arr_totals = explode(' ', $clean_line);
$shares = str_replace(array('),', '('), array('', '+'), $arr_totals[7]);
$arr_shares = explode('+', $shares);
$arr_totals_info['eth_total_speed'] = $arr_totals[3];
$arr_totals_info['eth_total_shares'] = intval(str_replace(",", "", $arr_shares[0]));
$arr_totals_info['eth_total_rejected'] = intval($arr_totals[9]);
// Shares for individual cards
$arr_card_shares = array_slice($arr_shares, 1, count($arr_shares));
if (!empty($arr_card_shares)) {
foreach ($arr_card_shares as $k_gpu => $gpu_shares) {
$arr_gpu_info[$k_gpu]['eth_shares'] = $gpu_shares;
}
}
// Let's calculate some averages for the totals array and set share percentage per card
if (!empty($arr_gpu_info)) {
$total_cards = count($arr_gpu_info);
$arr_totals_info['eth_avg_speed_per_card'] = round($arr_totals_info['eth_total_speed'] / $total_cards, 2);
$arr_totals_info['eth_avg_shares_per_card'] = round($arr_totals_info['eth_total_shares'] / $total_cards, 2);
$temp_total = $fan_total = 0;
foreach ($arr_gpu_info as $k_gpu => $gpu) {
$temp_total += $gpu['temp'] ?? 0;
$fan_total += $gpu['fan'] ?? 0;
if ($arr_totals_info['eth_total_shares'] > 0) {
$arr_gpu_info[$k_gpu]['eth_shares_pct'] = round(100 * ($gpu['eth_shares'] ?? 0 / $arr_totals_info['eth_total_shares']), 2);
if (count($arr_gpu_info) === 1) {
// Fix: If there is only one card in rig
$arr_gpu_info[$k_gpu]['eth_shares_pct'] = 100;
}
} else {
$arr_gpu_info[$k_gpu]['eth_shares_pct'] = 0;
}
}
$arr_totals_info['avg_temp'] = round($temp_total / $total_cards, 2);
$arr_totals_info['avg_fan'] = round($fan_total / $total_cards, 2);
}
}
$i++;
}
# ------------------------------------------------------------------ #
# Sorting totals array before output
# ------------------------------------------------------------------ #
// Sort order
$arr_totals_order = array('avg_temp', 'avg_fan', 'eth_total_speed', 'eth_total_shares', 'eth_total_rejected', 'eth_avg_speed_per_card', 'eth_avg_shares_per_card');
if ($dual_mining_detected === true) {
// We are doing dual mining, so there are a few more keys we need to sort
$arr_totals_order = array_merge($arr_totals_order, array(DUAL_CURRENCY_LOWER.'_total_speed', DUAL_CURRENCY_LOWER.'_total_shares', DUAL_CURRENCY_LOWER.'_total_rejected', DUAL_CURRENCY_LOWER.'_avg_speed_per_card', DUAL_CURRENCY_LOWER.'_avg_shares_per_card'));
}
// Now sort the totals array:
$arr_totals_info = array_merge(array_flip($arr_totals_order), $arr_totals_info);
# ------------------------------------------------------------------ #
# Sorting GPU arrays before output
# ------------------------------------------------------------------ #
// Sort order
$arr_gpu_order = array('id', 'temp', 'fan', 'eth_speed', 'eth_shares', 'eth_shares_pct');
if ($dual_mining_detected === true) {
// We are doing dual mining, so there are a few more keys we need to sort
$arr_gpu_order = array_merge($arr_gpu_order, array(DUAL_CURRENCY_LOWER.'_speed', DUAL_CURRENCY_LOWER.'_shares', DUAL_CURRENCY_LOWER.'_shares_pct'));
}
foreach ($arr_gpu_info as $gpu_k => $gpu) {
// Sort each GPU array
$arr_gpu_info[$gpu_k] = array_merge(array_flip($arr_gpu_order), $gpu);
}
# ------------------------------------------------------------------ #
# Log time and machine name
# ------------------------------------------------------------------ #
$objDateTime = new DateTime('NOW');
$time = str_replace('+', 'Z', $objDateTime->format(DateTime::RFC3339));
# ------------------------------------------------------------------ #
# Write to totals log file
# ------------------------------------------------------------------ #
$log_entry_total = $time.', machine: '.MACHINE_NAME;
foreach ($arr_totals_info as $label => $v) {
$log_entry_total .= ', '.$label.': '.$v;
}
file_put_contents(FILE_LOG_TOTALS, $log_entry_total."\n", FILE_APPEND);
# ------------------------------------------------------------------ #
# Write to gpu log file
# ------------------------------------------------------------------ #
$log_entry_gpu = '';
foreach ($arr_gpu_info as $gpu_k => $gpu) {
$log_entry_gpu .= $time.', machine: '.MACHINE_NAME;
foreach ($gpu as $label => $v) {
$log_entry_gpu .= ', '.$label.': '.$v;
}
$log_entry_gpu .= "\n";
}
file_put_contents(FILE_LOG_GPUS, $log_entry_gpu, FILE_APPEND);
if (INFLUXDB_ENABLED === true) {
/**
* Influx line format looks something like this:
* curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
* See: https://docs.influxdata.com/influxdb/v1.3/guides/writing_data/
*/
$influxdb_url = INFLUXDB_SERVICE_URL.':'.INFLUXDB_SERVICE_PORT.'/write?db='.INFLUXDB_DB;
// Totals for influxdb
$binary_data_totals = 'eth_totals,host='.MACHINE_NAME.' ';
foreach ($arr_totals_info as $label => $v) {
$binary_data_totals.= $label.'='.$v.',';
}
$binary_data_totals = substr($binary_data_totals, 0, -1);
$binary_data_totals.= ' '.nano_sec_since_unix_epoc(); // Add time in nano seconds
// GPU's for influxdb
$arr_string_fields = array('id');
$binary_data_gpu = '';
foreach ($arr_gpu_info as $gpu_k => $gpu) {
$binary_data_gpu.= 'eth_gpu,host='.MACHINE_NAME.',gpu='.$gpu['id'].' ';
foreach ($gpu as $label => $v) {
if (in_array($label, $arr_string_fields)) {
// String-fields needs to be in qoutes
$binary_data_gpu.= $label.'="'.$v.'",';
} else {
$binary_data_gpu.= $label.'='.$v.',';
}
}
$binary_data_gpu = substr($binary_data_gpu, 0, -1);
$binary_data_gpu.= "\n";
}
$binary_data_gpu = substr($binary_data_gpu, 0, -1); // Remove last line break
$binary_data_gpu.= ' '.nano_sec_since_unix_epoc(); // Add time in nano seconds
$influx_binary_data = $binary_data_totals."\n".$binary_data_gpu;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $influxdb_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $influx_binary_data);
curl_setopt($ch, CURLOPT_POST, 1);
if (DEBUG_MODE === true) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
}
if (INFLUXDB_USE_CREDENTIALS === true) {
curl_setopt($ch, CURLOPT_USERPWD, INFLUXDB_USERNAME . ":" . INFLUXDB_PASSWORD);
}
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
}
# ------------------------------------------------------------------ #
# Output debug-info. if enabled
# ------------------------------------------------------------------ #
if (DEBUG_MODE === true) {
print "===========================================================================\n\n";
print $log_entry_total . "\n";
print $log_entry_gpu . "\n";
print "===========================================================================\n\n";
print_r($arr_totals_info) . "\n";
print "===========================================================================\n\n";
print_r($arr_gpu_info) . "\n";
print "===========================================================================\n\n";
print_r($arr_lines) . "\n";
}
} else if (DEBUG_MODE === true) {
// Error
print 'Could not reach Claymore API on '.SERVICE_URL.' port '.SERVICE_PORT;
}
if (RUN_INDEFINITELY === false) {
exit;
}
sleep(UPDATE_FREQ);
}
function nano_sec_since_unix_epoc() {
return intval(array_sum(explode(' ', microtime()))).'000000000';
}
/**
* This function fetches data via curl
* @param $url
* @return array
*/
function fetch_via_curl($url) {
$arr_curl_result = array();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
$arr_curl_result['curl_exec'] = curl_exec($ch);
$arr_curl_result['curl_getinfo'] = curl_getinfo($ch);
if (array_key_exists('http_code', $arr_curl_result['curl_getinfo']) === true) {
if ($arr_curl_result['curl_getinfo']['http_code'] == '200') {
$quick_status = 'ok';
} else {
$quick_status = 'error';
}
$http_code = $arr_curl_result['curl_getinfo']['http_code'];
} else {
$quick_status = 'error';
$http_code = 'None';
}
$arr_curl_result['quick_status'] = $quick_status;
$arr_curl_result['http_code'] = $http_code;
curl_close($ch);
return $arr_curl_result;
}
?>