forked from inigoflores/helium-miner-log-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processlogs.php
executable file
·331 lines (275 loc) · 12.5 KB
/
processlogs.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
#!/usr/bin/php
<?php
/**
* processlogs.php
*
* Extracts witness data from Helium miner logs
*
* @author Iñigo Flores
* @copyright 2022 Iñigo Flores
* @license https://opensource.org/licenses/MIT MIT License
* @version 0.01
* @link https://github.com/inigoflores/helium-miner-log-analyzer
*/
$logsFolder = './';
if (is_dir("/home/pi/hnt/miner/log")) { //Pisces P100
$logsFolder = '/home/pi/hnt/miner/log/';
}
$startDate = "2000-01-01";
$endDate = "2030-01-01";
// Command line options
$options = ["p:","s:","e:","a","l"];
$opts = getopt(implode("",$options));
if (!isset($opts['l'])) {
$opts['a']=false;
}
foreach ($options as $key=>$val){
$options[$key] = str_replace(":","",$val);
}
uksort($opts, function ($a, $b) use ($options) {
$pos_a = array_search($a, $options);
$pos_b = array_search($b, $options);
return $pos_a - $pos_b;
});
// Handle command line arguments
foreach (array_keys($opts) as $opt) switch ($opt) {
case 'p':
$logsFolder = $opts['p'];
if (substr($logsFolder,strlen($logsFolder)-1) != "/"){
$logsFolder.="/";
};
break;
case 's':
if (!DateTime::createFromFormat('Y-m-d', $opts['s'])){
exit("Wrong date format");
}
$startDate = $opts['s'];
break;
case 'e':
if (!DateTime::createFromFormat('Y-m-d', $opts['e'])){
exit("Wrong date format");
}
$endDate = $opts['e'];
break;
case 'a':
echo "\nUsing logs in folder {$logsFolder}\n\n";
$beacons = extractData($logsFolder,$startDate,$endDate);
echo generateStats($beacons);
exit(1);
case 'l':
echo "\nUsing logs in folder {$logsFolder}\n\n";
$beacons = extractData($logsFolder,$startDate,$endDate);
echo generateList($beacons);
exit(1);
}
/*
* -------------------------------------------------------------------------------------------------
* Functions
* -------------------------------------------------------------------------------------------------
*/
/**
* @param $beacons
* @return string
*/
function generateStats($beacons) {
if (empty($beacons)) {
exit("No witnesses found\n");
}
$sucessful = 0;
$failedMaxRetry = 0;
$failedIncomplete = 0;
$failedUnkown = 0;
$failedNotFound = 0;
$failedTimeout = 0;
$failedNoListenAddress = 0;
$failedConRefused = 0;
$failedHostUnreach = 0;
$relayed = 0;
$notRelayed = 0;
foreach ($beacons as $beacon){
// General Witnesses Overview
if ($beacon['status']=='successfully sent') {
$sucessful++;
} else if ($beacon['status']=='failed max retry') {
$failedMaxRetry++;
} else if ($beacon['status']=='failed to dial' || $beacon['status']=='incomplete') {
$failedIncomplete++;
} else {
$failedUnkown++;
}
// Failure Reasons
if ($beacon['status']=='failed max retry') {
if ($beacon['reasonShort']=='not found') {
$failedNotFound++;
} else if ($beacon['reasonShort']=='timeout') {
$failedTimeout++;
} else if ($beacon['reasonShort']=='no listen address') {
$failedNoListenAddress++;
} else if ($beacon['reasonShort']=='connection refused') {
$failedConRefused++;
} else if ($beacon['reasonShort']=='host unreachable') {
$failedHostUnreach++;
}
}
//Relayed Challengers
if (@$beacon['relayed'] == "yes") {
$relayed++;
} else if (@$beacon['relayed'] == "no") {
$notRelayed++;
}
}
$total = sizeOf($beacons);
$totalFailed = $total - $sucessful;
//$totalFailedMaxRetry = $failedMaxRetryNotFound + $failedMaxRetryTimeout + $failedMaxRetryHostUnreach + $failedMaxRetryConRefused + $failedMaxRetryUnkown;
$totalFailedOther = $failedNoListenAddress + $failedConRefused + $failedHostUnreach;
$percentageSuccessful = round($sucessful/$total*100,2);
$percentageFailed = round($totalFailed/$total*100,2);
$percentageFailedMaxRetry = round($failedMaxRetry/$total*100,2);
$percentageFailedIncomplete = round($failedIncomplete/$total*100,2);
$percentageFailedNotFound = round($failedNotFound/$failedMaxRetry*100,2);
$percentageFailedTimeout = round($failedTimeout/$failedMaxRetry*100,2);
$percentageFailedOther = round($totalFailedOther/$failedMaxRetry*100,2);
$percentageNotRelayed = round($notRelayed/$total*100,2);
$percentageRelayed = round($relayed/$total*100,2);
$percentageRelayUnknown = round(($total-$notRelayed-$relayed)/$total*100,2);
$output = "\nGeneral Witnesses Overview \n";
$output.= "----------------------------------\n";
$output.= "Total witnesses = ". str_pad($total, 5, " ", STR_PAD_LEFT) . "\n";
$output.= "Succesfully delivered = ". str_pad($sucessful, 5, " ", STR_PAD_LEFT) .
str_pad("({$percentageSuccessful}%)", 9, " ", STR_PAD_LEFT) . "\n";
$output.= "Failed = ". str_pad($totalFailed, 5, " ", STR_PAD_LEFT) .
str_pad("({$percentageFailed}%)", 9, " ", STR_PAD_LEFT) . " \n";
$output.= " ├── Max retry = ". str_pad($failedMaxRetry, 4, " ", STR_PAD_LEFT) .
str_pad("({$percentageFailedMaxRetry}%)", 9, " ", STR_PAD_LEFT) . " \n";
$output.= " └── Crash/reboot = ". str_pad($failedIncomplete, 4, " ", STR_PAD_LEFT) .
str_pad("({$percentageFailedIncomplete}%)", 9, " ", STR_PAD_LEFT) . " \n";
$output.= "\nMax Retry Failure Reasons \n";
$output.= "----------------------------------\n";
$output.= "Timeout = ". str_pad($failedTimeout, 5, " ", STR_PAD_LEFT) .
str_pad("({$percentageFailedTimeout}%)", 9, " ", STR_PAD_LEFT) . " \n";
$output.= "Not Found = ". str_pad($failedNotFound, 5, " ", STR_PAD_LEFT) .
str_pad("({$percentageFailedNotFound}%)", 9, " ", STR_PAD_LEFT) . " \n";
$output.= "Other challenger issues = ". str_pad($totalFailedOther, 5, " ", STR_PAD_LEFT) .
str_pad("({$percentageFailedOther}%)", 9, " ", STR_PAD_LEFT) . " \n";
$output.= "\nChallengers \n";
$output.= "----------------------------------\n";
$output.= "Not Relayed = ". str_pad($notRelayed, 5, " ", STR_PAD_LEFT) .
str_pad("({$percentageNotRelayed}%)", 9, " ", STR_PAD_LEFT) . " \n";
$output.= "Relayed = ". str_pad($relayed, 5, " ", STR_PAD_LEFT) .
str_pad("({$percentageRelayed}%)", 9, " ", STR_PAD_LEFT) . " \n";
$output.= "Unknown Relay Status = ". str_pad($total-$notRelayed-$relayed, 5, " ", STR_PAD_LEFT) .
str_pad("({$percentageRelayUnknown}%)", 9, " ", STR_PAD_LEFT) . " \n";
return $output;
}
/**
* @param $beacons
* @return string
*/
function generateList($beacons) {
$output = "Date | Session | RSSI | Freq | SNR | Challenger | Relay | Status | Fails | Reason \n";
$output.= "-------------------------------------------------------------------------------------------------------------------------------------------------------------- \n";
foreach ($beacons as $beacon){
$rssi = str_pad($beacon['rssi'], 4, " ", STR_PAD_LEFT);
$snr = str_pad($beacon['snr'], 5, " ", STR_PAD_LEFT);
$status = str_pad($beacon['status'], 17, " ", STR_PAD_RIGHT);
$failures = str_pad(empty($beacon['failures'])?0:$beacon['failures'], 5, " ", STR_PAD_LEFT);
$challenger = @str_pad($beacon['challenger'],52, " ", STR_PAD_RIGHT);
$relayed = @str_pad($beacon['relayed'],5, " ", STR_PAD_RIGHT);
$reasonShort = @$beacon['reasonShort'];
$reason = @$beacon['reason'];
$session = str_pad($beacon['session'],10, " ", STR_PAD_LEFT);;
$output.=@"{$beacon['datetime']} | {$session} | {$rssi} | {$beacon['freq']} | {$snr} | {$challenger} | $relayed | {$status} | {$failures} | {$reasonShort} \n";
}
return $output;
}
/**
* @param $logsFolder
* @return array
*/
function extractData($logsFolder, $startDate, $endDate){
$beacons = [];
$filenames = glob("{$logsFolder}console.log*");
if (empty($filenames)){
exit ("No logs found. Please chdir to the Helium miner logs folder or specify a path.\n");
}
rsort($filenames); //Order is important, from older to more recent.
foreach ($filenames as $filename) {
$lines = file( $filename, FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
if (preg_match('/miner_onion_server:send_witness:{[0-9]+,[0-9]+} (?:re-)?sending witness at RSSI/', $line) ||
preg_match('/miner_onion_server:send_witness:{[0-9]+,[0-9]+} failed to dial challenger/', $line) ||
preg_match('/miner_onion_server:send_witness:{[0-9]+,[0-9]+} successfully sent witness to challenger/', $line) ||
preg_match('/miner_onion_server:send_witness:{[0-9]+,[0-9]+} failed to send witness, max retry/', $line
))
{
$fields = explode(' ', $line);
$datetime = $fields[0] . " " . $fields[1];
if ($datetime<$startDate || $datetime>$endDate) {
continue;
}
$session = explode('>',explode('<', $fields[4])[1])[0];
} else {
continue;
}
if (preg_match('/sending witness at RSSI/', $line)){
$rssi = str_pad(substr($fields[9], 0, -1), 4, " ", STR_PAD_LEFT);
$freq = substr($fields[11], 0, -1);
$snr = $fields[13];
$status = "incomplete";
$beacons[$session] = array_merge((array)@$beacons[$session], compact('datetime', 'session', 'rssi', 'freq', 'snr', 'status'));
}
if (preg_match('/failed to dial challenger/', $line)) {
$challenger = substr($fields[9], 6, -2);
$reason = $fields[10];
if (strpos($line,'p2p-circuit')){
$relayed = 'yes';
} else if ($reason!='not_found') {
$relayed = 'no';
} else {
$relayed = '';
}
switch (true) {
case strpos($reason,'not_found') !== FALSE:
$reasonShort = "not found";
break;
case strpos($reason,'timeout') !== FALSE:
$reasonShort = "timeout";
break;
case strpos($reason,'econnrefused') !== FALSE:
$reasonShort = "connection refused";
break;
case strpos($reason,',ehostunreach') !== FALSE:
$reasonShort = "host unreachable";
break;
case strpos($reason,'no_listen_addr') !== FALSE:
$reasonShort = "no listen address";
break;
default:
$reasonShort = "";
};
$failures = @$beacons[$session]['failures'] + 1;
$status = "failed to dial";
$beacons[$session] = array_merge((array)@$beacons[$session], compact('datetime', 'session', 'challenger', 'status', 'reason','reasonShort', 'failures','relayed'));
}
if (preg_match('/successfully sent witness to challenger/', $line)) {
$challenger = substr($fields[10], 6, -1);
$relayed = strpos($line,'p2p-circuit')?'yes':'no';
$rssi = str_pad(substr($fields[13], 0, -1), 4, " ", STR_PAD_LEFT);
$freq = substr($fields[15], 0, -1);
$snr = $fields[17];
$status = "successfully sent";
$reason = "";
$reasonShort = "";
$beacons[$session] = array_merge((array)@$beacons[$session], compact('datetime', 'session', 'challenger', 'relayed', 'rssi', 'freq', 'snr', 'status', 'reason','reasonShort'));
}
if (preg_match('/failed to send witness, max retry/', $line)) {
$status = "failed max retry";
$beacons[$session] = array_merge((array)@$beacons[$session], compact('datetime', 'session', 'status'));
}
}
}
usort($beacons, function($a, $b) {
return $a['datetime'] <=> $b['datetime'];
});
return $beacons;
}