1
|
#!/usr/local/bin/php-cgi -f
|
2
|
<?php
|
3
|
/*
|
4
|
* 3gstats.php
|
5
|
*
|
6
|
* part of pfSense (https://www.pfsense.org)
|
7
|
* Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
|
8
|
* All rights reserved.
|
9
|
*
|
10
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
11
|
* you may not use this file except in compliance with the License.
|
12
|
* You may obtain a copy of the License at
|
13
|
*
|
14
|
* http://www.apache.org/licenses/LICENSE-2.0
|
15
|
*
|
16
|
* Unless required by applicable law or agreed to in writing, software
|
17
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
18
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
19
|
* See the License for the specific language governing permissions and
|
20
|
* limitations under the License.
|
21
|
*/
|
22
|
|
23
|
ini_set("max_execution_time", "0");
|
24
|
|
25
|
if (empty($argv[1])) {
|
26
|
echo "No modem device given \n";
|
27
|
exit(0);
|
28
|
}
|
29
|
|
30
|
/* Huawei example */
|
31
|
$device = "/dev/{$argv[1]}";
|
32
|
$statfile = "/tmp/3gstats.{$argv[2]}";
|
33
|
/* mode is a comma separated value, thus submode is born */
|
34
|
$header = "#seconds,rssi,mode,submode,upstream,downstream,sentbytes,receivedbyts,bwupstream,bwdownstream,simstate,service\n";
|
35
|
|
36
|
$i = 0;
|
37
|
|
38
|
$record = array();
|
39
|
$handle = fopen($device, "r");
|
40
|
if (!$handle) {
|
41
|
echo "Can not open modem stats device\n";
|
42
|
exit(1);
|
43
|
}
|
44
|
|
45
|
$record['time'] = 0;
|
46
|
$record['rssi'] = 0;
|
47
|
$record['mode'] = "0,0";
|
48
|
$record['upstream'] = 0;
|
49
|
$record['downstream'] = 0;
|
50
|
$record['sent'] = 0;
|
51
|
$record['received'] = 0;
|
52
|
$record['bwupstream'] = 0;
|
53
|
$record['bwdownstream'] = 0;
|
54
|
$record['simstate'] = 255;
|
55
|
$record['service'] = 255;
|
56
|
|
57
|
while (true) {
|
58
|
$string = "";
|
59
|
$string = fgets($handle, 256);
|
60
|
|
61
|
$elements = array();
|
62
|
$elements = explode(':', $string);
|
63
|
$elements[0] = trim($elements[0]);
|
64
|
$elements[1] = trim($elements[1]);
|
65
|
|
66
|
switch ($elements[0]) {
|
67
|
case "^MODE":
|
68
|
$record['mode'] = $elements[1];
|
69
|
break;
|
70
|
case "^SRVST":
|
71
|
$record['service'] = $elements[1];
|
72
|
break;
|
73
|
case "^SIMST":
|
74
|
$record['simstate'] = $elements[1];
|
75
|
break;
|
76
|
case "^RSSI":
|
77
|
$record['rssi'] = $elements[1];
|
78
|
break;
|
79
|
case "^DSFLOWRPT":
|
80
|
$items = array();
|
81
|
$items = explode(',', $elements[1]);
|
82
|
$record['time'] = hexdec($items[0]);
|
83
|
$record['upstream'] = round((floatval(hexdec($items[1])) * 8) /1024);
|
84
|
$record['downstream'] = round((floatval(hexdec($items[2])) * 8) /1024);
|
85
|
$record['sent'] = hexdec($items[3]);
|
86
|
$record['received'] = hexdec($items[4]);
|
87
|
$record['bwupstream'] = round((floatval(hexdec($items[5])) * 8) /1024);
|
88
|
$record['bwdownstream'] = round((floatval(hexdec($items[6])) * 8) /1024);
|
89
|
break;
|
90
|
}
|
91
|
|
92
|
if ($i > 10) {
|
93
|
$csv = $header;
|
94
|
$csv .= implode(",", $record);
|
95
|
$csv .= "\n";
|
96
|
file_put_contents($statfile, $csv);
|
97
|
$i = 0;
|
98
|
}
|
99
|
$i++;
|
100
|
}
|
101
|
fclose($handle);
|
102
|
?>
|