-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathprocesslogs.php
executable file
·295 lines (239 loc) · 8.81 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
#!/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.02
* @link https://github.com/inigoflores/helium-miner-log-analyzer
*/
$logsFolder = './';
$logsFolders = [
'Docker' => '/var/log/miner/', //Also Controllino
'Pisces P100' => '/home/pi/hnt/miner/log/',
'Milesight UG65' => '/mnt/mmcblk0p1/miner_data/log/', //adding it for future use,as PHP and opkg are missing (OpenWrt)
'Panther X2' => '/opt/panther-x2/miner_data/log/',
'RHF2S308' => '/opt/helium/miner_data/log/',
'HeliumDIY' => '/home/pi/miner_data/log/'
];
foreach ($logsFolders as $folder){
if (is_dir($folder)) {
$logsFolder = $folder;
break;
}
}
$startDate = "2000-01-01";
$endDate = "2030-01-01";
// Command line options
$options = ["p:","s:","e:","a","l","c::"];
$opts = getopt(implode("",$options));
// Defaults to stats when called
if (!(isset($opts['l']) || isset($opts['c']))) {
$opts['a']=true;
}
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);
case 'c':
$beacons = extractData($logsFolder,$startDate,$endDate);
$filename = $opts['c'];
echo generateCSV($beacons,$filename);
exit(1);
}
/*
* -------------------------------------------------------------------------------------------------
* Functions
* -------------------------------------------------------------------------------------------------
*/
/**
* @param $beacons
* @return string
*/
function generateStats($beacons) {
if (empty($beacons)) {
exit("No witnesses found\n");
}
$startTime = DateTime::createFromFormat('Y-m-d H:i:s',explode('.',$beacons[0]['datetime'])[0]);
$endTime = DateTime::createFromFormat('Y-m-d H:i:s',explode('.',end($beacons)['datetime'])[0]);
$intervalInHours = ($endTime->getTimestamp() - $startTime->getTimestamp())/3600;
$successful = 0;
foreach ($beacons as $beacon){
// General Witnesses Overview
if ($beacon['status']=='successfully sent') {
$successful++;
}
}
$total = sizeOf($beacons);
$totalFailed = $total - $successful;
$totalPerHour = round($total / $intervalInHours,2);
$percentageSuccessful = round($successful/$total*100,2);
$percentageFailed = round($totalFailed/$total*100,2);
$output = "\nGeneral Witnesses Overview \n";
$output.= "----------------------------------\n";
$output.= "Total witnesses = ". str_pad($total, 5, " ", STR_PAD_LEFT) .
str_pad(" ({$totalPerHour}/hour)", 13, " ", STR_PAD_LEFT) . "\n";
$output.= "Succesfully delivered = ". str_pad($successful, 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";
return $output;
}
/**
* @param $beacons
* @return string
*/
function generateList($beacons) {
$systemDate = new DateTime();
$output = "Date | Freq | RSSI | SNR | Noise | Status \n";
$output.= "------------------------------------------------------------- \n";
foreach ($beacons as $beacon){
$datetime = DateTime::createFromFormat('Y-m-d H:i:s.u',$beacon['datetime'], new DateTimeZone( 'UTC' ));
$datetime->setTimezone($systemDate->getTimezone());
$rssi = str_pad($beacon['rssi'], 4, " ", STR_PAD_LEFT);
$snr = str_pad($beacon['snr'], 5, " ", STR_PAD_LEFT);
$noise = str_pad(number_format((float) ($beacon['rssi'] - $beacon['snr']),1),6, " ", STR_PAD_LEFT);
$status = str_pad($beacon['status'], 17, " ", STR_PAD_RIGHT);
$reasonShort = @$beacon['reasonShort'];
$datetimeStr = $datetime->format("d-m-Y H:i:s");
//$reason = @$beacon['reason'];
$output.=@"{$datetimeStr} | {$beacon['freq']} | {$rssi} | {$snr} | {$noise} | {$status} \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) {
$buf = file_get_contents($filename,);
if(substr($filename, -3) == '.gz') {
$buf = gzdecode($buf);
}
$lines = explode("\n", $buf);
unset($buf);
foreach ($lines as $line) {
if (preg_match('/miner_onion_server_light:decrypt:{[0-9]+,[0-9]+} (?:re-)?sending witness at RSSI/', $line) ||
(preg_match('/@miner_poc_grpc_client_statem:send_report:{[0-9]+,[0-9]+} failed to submit report/', $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 = substr($fields[9], 0, -1);
$freq = substr($fields[11], 0, -1);
$snr = $fields[13];
$status = "successfully sent";
$beacons[$rssi.$freq.$snr] = array_merge((array)@$beacons[$rssi.$freq.$snr], compact('datetime', 'rssi', 'freq', 'snr', 'status'));
continue;
}
if (preg_match('/failed to submit report/', $line)){
$temp = explode('<<',$fields[13]);
$temp1 = explode("," , $temp[1]);
$temp3 = explode("," , $temp[3]);
$rssi = $temp1[sizeof($temp1)-2];
$freq = $temp3[2];
$snr = $temp3[1];
$status = "failed";
$beacons[$rssi.$freq.$snr] = array_merge((array)@$beacons[$rssi.$freq.$snr], compact('datetime', 'rssi', 'freq', 'snr', 'status'));
continue;
}
}
}
//
foreach ($beacons as $session => $beacon) {
if (empty(@$beacon['rssi'])) {
unset($beacons[$session]);
}
}
usort($beacons, function($a, $b) {
return $a['datetime'] <=> $b['datetime'];
});
return $beacons;
}
/**
* @param $beacons
* @return string
*/
function generateCSV($beacons, $filename=false) {
$columns = ['Date','Freq','RSSI','SNR','Noise','Status'];
$data = array2csv($columns);
foreach ($beacons as $beacon){
$noise = number_format((float) ($beacon['rssi'] - $beacon['snr']),1);
$data.= @array2csv([
$beacon['datetime'],$beacon['freq'],$beacon['rssi'],$beacon['snr'],$noise, $beacon['status']]);
}
if ($filename) {
$data = "SEP=;" . $data;
file_put_contents($filename,$data);
return "Data saved to $filename\n";
}
return $data;
}
/**
* @param $fields
* @param string $delimiter
* @param string $enclosure
* @param string $escape_char
* @return false|string
*/
function array2csv($fields, $delimiter = ",", $enclosure = '"', $escape_char = '\\')
{
$buffer = fopen('php://temp', 'r+');
fputcsv($buffer, $fields, $delimiter, $enclosure, $escape_char);
rewind($buffer);
$csv = fgets($buffer);
fclose($buffer);
return $csv;
}