Project

General

Profile

Download (4.28 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-2016 Electric Sheep Fencing, LLC
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12
 *
13
 * 1. Redistributions of source code must retain the above copyright notice,
14
 *    this list of conditions and the following disclaimer.
15
 *
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in
18
 *    the documentation and/or other materials provided with the
19
 *    distribution.
20
 *
21
 * 3. All advertising materials mentioning features or use of this software
22
 *    must display the following acknowledgment:
23
 *    "This product includes software developed by the pfSense Project
24
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
25
 *
26
 * 4. The names "pfSense" and "pfSense Project" must not be used to
27
 *    endorse or promote products derived from this software without
28
 *    prior written permission. For written permission, please contact
29
 *    coreteam@pfsense.org.
30
 *
31
 * 5. Products derived from this software may not be called "pfSense"
32
 *    nor may "pfSense" appear in their names without prior written
33
 *    permission of the Electric Sheep Fencing, LLC.
34
 *
35
 * 6. Redistributions of any form whatsoever must retain the following
36
 *    acknowledgment:
37
 *
38
 * "This product includes software developed by the pfSense Project
39
 * for use in the pfSense software distribution (http://www.pfsense.org/).
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 * OF THE POSSIBILITY OF SUCH DAMAGE.
53
 */
54

    
55
ini_set("max_execution_time", "0");
56

    
57
if(empty($argv[1])) {
58
	echo "No modem device given \n";
59
	exit(0);
60
}
61

    
62
/* Huawei example */
63
$device = "/dev/{$argv[1]}";
64
$statfile = "/tmp/3gstats.{$argv[2]}";
65
/* mode is a comma separated value, thus submode is born */
66
$header = "#seconds,rssi,mode,submode,upstream,downstream,sentbytes,receivedbyts,bwupstream,bwdownstream,simstate,service\n";
67

    
68
$i = 0;
69

    
70
$record = array();
71
$handle = fopen($device, "r");
72
if(! $handle) {
73
	echo "Can not open modem stats device\n";
74
	exit(1);
75
}
76

    
77
$record['time'] = 0;
78
$record['rssi'] = 0;
79
$record['mode'] = "0,0";
80
$record['upstream'] = 0;
81
$record['downstream'] = 0;
82
$record['sent'] = 0;
83
$record['received'] = 0;
84
$record['bwupstream'] = 0;
85
$record['bwdownstream'] = 0;
86
$record['simstate'] = 0;
87
$record['service'] = 0;
88

    
89
while (true) {
90
	$string = "";
91
	$string = fgets($handle, 256);
92

    
93
	$elements = array();
94
	$elements = explode(':', $string);
95
	$elements[0] = trim($elements[0]);
96
	$elements[1] = trim($elements[1]);
97

    
98
	switch ($elements[0]) {
99
		case "^MODE":
100
			$record['mode'] = $elements[1];
101
			break;
102
		case "^SRVST":
103
			$record['service'] = $elements[1];
104
			break;
105
		case "^SIMST":
106
			$record['simstate'] = $elements[1];
107
			break;
108
		case "^RSSI":
109
			$record['rssi'] = $elements[1];
110
			break;
111
		case "^DSFLOWRPT":
112
			$items = array();
113
			$items = explode(',', $elements[1]);
114
			$record['time'] = hexdec($items[0]);
115
			$record['upstream'] = round((floatval(hexdec($items[1])) * 8) /1024);
116
			$record['downstream'] = round((floatval(hexdec($items[2])) * 8) /1024);
117
			$record['sent'] = hexdec($items[3]);
118
			$record['received'] = hexdec($items[4]);
119
			$record['bwupstream'] = round((floatval(hexdec($items[5])) * 8) /1024);
120
			$record['bwdownstream'] = round((floatval(hexdec($items[6])) * 8) /1024);
121
			break;
122
	}
123

    
124
	if ($i > 10) {
125
		$csv = $header;
126
		$csv .= implode(",", $record);
127
		$csv .= "\n";
128
		file_put_contents($statfile, $csv);
129
		$i = 0;
130
	}
131
	$i++;
132
}
133
fclose($handle);
134
?>
(1-1/10)