Project

General

Profile

Download (2.72 KB) Statistics
| Branch: | Tag: | Revision:
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-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
ini_set("max_execution_time", "0");
26

    
27
if (empty($argv[1])) {
28
	echo "No modem device given \n";
29
	exit(0);
30
}
31

    
32
/* Huawei example */
33
$device = "/dev/{$argv[1]}";
34
$statfile = "/tmp/3gstats.{$argv[2]}";
35
/* mode is a comma separated value, thus submode is born */
36
$header = "#seconds,rssi,mode,submode,upstream,downstream,sentbytes,receivedbyts,bwupstream,bwdownstream,simstate,service\n";
37

    
38
$i = 0;
39

    
40
$record = array();
41
$handle = fopen($device, "r");
42
if (!$handle) {
43
	echo "Can not open modem stats device\n";
44
	exit(1);
45
}
46

    
47
$record['time'] = 0;
48
$record['rssi'] = 0;
49
$record['mode'] = "0,0";
50
$record['upstream'] = 0;
51
$record['downstream'] = 0;
52
$record['sent'] = 0;
53
$record['received'] = 0;
54
$record['bwupstream'] = 0;
55
$record['bwdownstream'] = 0;
56
$record['simstate'] = 255;
57
$record['service'] = 255;
58

    
59
while (true) {
60
	$string = "";
61
	$string = fgets($handle, 256);
62

    
63
	$elements = array();
64
	$elements = explode(':', $string);
65
	$elements[0] = trim($elements[0]);
66
	$elements[1] = trim($elements[1]);
67

    
68
	switch ($elements[0]) {
69
		case "^MODE":
70
			$record['mode'] = $elements[1];
71
			break;
72
		case "^SRVST":
73
			$record['service'] = $elements[1];
74
			break;
75
		case "^SIMST":
76
			$record['simstate'] = $elements[1];
77
			break;
78
		case "^RSSI":
79
			$record['rssi'] = $elements[1];
80
			break;
81
		case "^DSFLOWRPT":
82
			$items = array();
83
			$items = explode(',', $elements[1]);
84
			$record['time'] = hexdec($items[0]);
85
			$record['upstream'] = round((floatval(hexdec($items[1])) * 8) /1024);
86
			$record['downstream'] = round((floatval(hexdec($items[2])) * 8) /1024);
87
			$record['sent'] = hexdec($items[3]);
88
			$record['received'] = hexdec($items[4]);
89
			$record['bwupstream'] = round((floatval(hexdec($items[5])) * 8) /1024);
90
			$record['bwdownstream'] = round((floatval(hexdec($items[6])) * 8) /1024);
91
			break;
92
	}
93

    
94
	if ($i > 10) {
95
		$csv = $header;
96
		$csv .= implode(",", $record);
97
		$csv .= "\n";
98
		file_put_contents($statfile, $csv);
99
		$i = 0;
100
	}
101
	$i++;
102
}
103
fclose($handle);
104
?>
(1-1/15)