forked from inigoflores/lora-packet-forwarder-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processlogs.php
executable file
·359 lines (289 loc) · 11.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
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
#!/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/lora-packet-forwarder-analyzer
*/
$logsPath = '/var/log/packet-forwarder/';
$startDate = "2000-01-01";
$endDate = "2030-01-01";
$includeDataPackets = false;
// Command line options
$options = ["d","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':
$logsPath = $opts['p'];
if (substr($logsPath,strlen($logsPath)-1) != "/" && is_dir($logsPath)){
$logsPath.="/";
};
break;
case 'd':
$includeDataPackets = true;
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 {$logsPath}\n\n";
$packets = extractData($logsPath,$startDate,$endDate);
echo generateStats($packets);
exit(1);
case 'l':
echo "\nUsing logs in {$logsPath}\n\n";
$packets = extractData($logsPath,$startDate,$endDate);
echo generateList($packets,$includeDataPackets);
exit(1);
case 'c':
$packets = extractData($logsPath,$startDate,$endDate);
$filename = $opts['c'];
echo generateCSV($packets,$filename,$includeDataPackets);
exit(1);
}
/*
* -------------------------------------------------------------------------------------------------
* Functions
* -------------------------------------------------------------------------------------------------
*/
/**
* @param $logsPath
* @return array
*/
function extractData($logsPath, $startDate = "", $endDate = ""){
if (is_dir($logsPath)) {
$filenames = glob("{$logsPath}packet_forwarder*.log*");
} else if (is_file($logsPath)) {
$filenames = [$logsPath];
} else {
exit ("Path is not a valid folder or file.\n");
}
if (empty($filenames)){
exit ("No logs found. Install the service and let it run for some time before running this command again.\n");
}
rsort($filenames); //Order is important, from older to more recent.
$packets = [];
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 (!strstr($line,'rxpk')) { //empty line
continue;
}
$temp = explode('{"rxpk":', $line);
$temp1 = explode(" ",$temp[0]);
$datetime = "{$temp1[0]} $temp1[1]";
if ($datetime < $startDate || $datetime > $endDate) {
continue;
}
$packet = json_decode('{"rxpk":' . $temp[1]);
if (empty($packet)) {
continue;
}
$packet = $packet->rxpk[0];
if (isset($packet->rssis)) {
$rssi = $packet->rssis;
} else {
$rssi = $packet->rssi;
}
if (substr($packet->data,0,3)=="QDD") {
$type = "witness";
} else {
$type = "data";
}
$snr = $packet->lsnr;
$freq = $packet->freq;
$packets[] = compact('datetime', 'freq', 'rssi', 'snr', 'type');
}
}
return $packets;
}
/**
* @param $packets
* @return string
*/
function generateStats($packets) {
if (empty($packets)) {
exit("No packets found\n");
}
$startTime = DateTime::createFromFormat('Y-m-d H:i:s',explode('.',$packets[0]['datetime'])[0]);
$endTime = DateTime::createFromFormat('Y-m-d H:i:s',explode('.',end($packets)['datetime'])[0]);
$intervalInHours = ($endTime->getTimestamp() - $startTime->getTimestamp())/3600;
$totalWitnesses = 0;
$totalPackets = sizeOf($packets);
$lowestWitnessRssi = $lowestPacketRssi = 0;
foreach ($packets as $packet){
//echo $packet['freq'] . "\n";
//@$freqs["{$packet['freq']}"]++;
$packetDataByFrequency["{$packet['freq']}"]['rssi'][] = $packet['rssi'];
$packetDataByFrequency["{$packet['freq']}"]['snr'][] = $packet['snr'];
if ($packet['rssi'] < $lowestPacketRssi) {
$lowestPacketRssi = $packet['rssi'];
}
if ($packet['type']=='witness') {
$totalWitnesses++;
$witnessDataByFrequency["{$packet['freq']}"]['rssi'][] = $packet['rssi'];
$witnessDataByFrequency["{$packet['freq']}"]['snr'][] = $packet['snr'];
if ($packet['rssi'] < $lowestWitnessRssi) {
$lowestWitnessRssi = $packet['rssi'];
}
}
}
foreach ($packetDataByFrequency as $freq => $rssifreq) {
$packetRssiAverages["{$freq}"] = number_format(getMean($packetDataByFrequency["{$freq}"]['rssi']),2);
$packetRssiMins["{$freq}"] = number_format(min($packetDataByFrequency["{$freq}"]['rssi']),2);
$packetSnrAverages["{$freq}"] = number_format(getMean($packetDataByFrequency["{$freq}"]['snr']),2);
}
foreach ($witnessDataByFrequency as $freq => $rssifreq) {
$witnessRssiAverages["{$freq}"] = number_format(getMean($witnessDataByFrequency["{$freq}"]['rssi']) ,2);
$witnessRssiMins["{$freq}"] = number_format(min($witnessDataByFrequency["{$freq}"]['rssi']) ,2);
$witnessSnrsAverages["{$freq}"] = number_format(getMean($witnessDataByFrequency["{$freq}"]['snr']),2);
}
$freqs = array_keys($packetDataByFrequency);
sort($freqs);
$totalPacketsPerHour = number_format(round($totalPackets / $intervalInHours,2),2,".","");
$totalWitnessesPerHour = number_format(round($totalWitnesses / $intervalInHours,2), 2,".","");
$totalPacketsPerHour = str_pad("($totalPacketsPerHour",9, " ", STR_PAD_LEFT);;
$totalWitnessesPerHour = str_pad("($totalWitnessesPerHour",9, " ", STR_PAD_LEFT);;
$totalWitnesses = str_pad($totalWitnesses,7, " ", STR_PAD_LEFT);
$totalPackets = str_pad($totalPackets,7, " ", STR_PAD_LEFT);
$lowestPacketRssi = str_pad($lowestPacketRssi,7," ",STR_PAD_LEFT);
$lowestWitnessRssi = str_pad($lowestWitnessRssi,7," ",STR_PAD_LEFT);
$output = "";
$output.= "Total Witnesses: $totalWitnesses $totalWitnessesPerHour/hour)\n";
$output.= "Total Packets: $totalPackets $totalPacketsPerHour/hour)\n";
$output.= "Lowest Witness RSSI: $lowestWitnessRssi dBm\n";
$output.= "Lowest Packet RSSI: $lowestPacketRssi dBm\n";
$output.= "\n";
$output.= " ----------------------------------------------------------------------------- " . PHP_EOL;
$output.= " | Witnesses | All Packets " . PHP_EOL;
$output.= " ----------------------------------------------------------------------------- " . PHP_EOL;
$output.= "Freq | Num | RSSI Avg | RSSI Min | SNR | Num | RSSI Avg | RSSI Min | SNR " . PHP_EOL;
$output.= "----------------------------------------------------------------------------------- " . PHP_EOL;
foreach ($freqs as $freq) {
$numberOfWitnesses = @str_pad(count($witnessDataByFrequency[$freq]['rssi']), 4, " ", STR_PAD_LEFT);
$witnessRssi = @str_pad($witnessRssiAverages["{$freq}"] , 7, " ", STR_PAD_LEFT);
$witnessSnr = @str_pad($witnessSnrsAverages["{$freq}"] , 6, " ", STR_PAD_LEFT);
$witnessRssiMin = @str_pad($witnessRssiMins["{$freq}"] , 7, " ", STR_PAD_LEFT);
$numberOfPackets = str_pad(count($packetDataByFrequency[$freq]['rssi']), 6, " ", STR_PAD_LEFT);
$packetRssi = str_pad($packetRssiAverages["{$freq}"] , 7, " ", STR_PAD_LEFT);
$packetSnr = str_pad($packetSnrAverages["{$freq}"] , 6, " ", STR_PAD_LEFT);
$packetRssiMin = str_pad($packetRssiMins["{$freq}"] , 7, " ", STR_PAD_LEFT);
$output.= "$freq | $numberOfWitnesses | $witnessRssi | $witnessRssiMin | $witnessSnr | $numberOfPackets | $packetRssi | $packetRssiMin | $packetSnr " . PHP_EOL;
};
$output.= "------------------------------------------------------------------------------------ " . PHP_EOL;
echo $output;
}
/**
* @param $packets
* @param $includeDataPackets
* @return string
*/
function generateList($packets, $includeDataPackets = false) {
//Sort packets by datetime
usort($packets, function($a, $b) {
return $a['datetime'] <=> $b['datetime'];
});
$output = "Date | RSSI | Freq | SNR | Noise | Type \n";
$output.= "------------------------------------------------------------- \n";
foreach ($packets as $packet){
if ($packet['type']!="witness" && !$includeDataPackets){
continue;
}
$rssi = str_pad($packet['rssi'], 4, " ", STR_PAD_LEFT);
$snr = str_pad($packet['snr'], 5, " ", STR_PAD_LEFT);
$noise = str_pad(number_format((float) ($packet['rssi'] - $packet['snr']),1),6, " ", STR_PAD_LEFT);
$type = str_pad($packet['type'],6, " ", STR_PAD_LEFT);
//$challenger = @str_pad($packet['challenger'],52, " ", STR_PAD_RIGHT);
$challenger="";
$output.=@"{$packet['datetime']} | {$rssi} | {$packet['freq']} | {$snr} | {$noise} | $type \n";
}
return $output;
}
/**
* @param $packets
* @param $includeDataPackets
* @return string
*/
function generateCSV($packets, $filename = false, $includeDataPackets = false) {
//Sort packets by datetime
usort($packets, function($a, $b) {
return $a['datetime'] <=> $b['datetime'];
});
$columns = ['Date','Freq','RSSI','SNR','Noise','Type'];
$data = array2csv($columns);
foreach ($packets as $packet){
if ($packet['type']!="witness" && !$includeDataPackets){
continue;
}
$noise = number_format((float) ($packet['rssi'] - $packet['snr']),1);
$data.= @array2csv([
$packet['datetime'], $packet['freq'], $packet['rssi'], $packet['snr'], $noise, $packet['type']]
);
}
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;
}
function getMedian($arr) {
sort($arr);
$count = count($arr);
$middleval = floor(($count-1)/2);
if ($count % 2) {
$median = $arr[$middleval];
} else {
$low = $arr[$middleval];
$high = $arr[$middleval+1];
$median = (($low+$high)/2);
}
return $median;
}
function getMean($arr) {
$count = count($arr);
return array_sum($arr)/$count;
}