Project

General

Profile

Download (2.63 KB) Statistics
| Branch: | Tag: | Revision:
1 cb7d18d5 Renato Botelho
#!/usr/local/bin/php-cgi -f
2 5e589685 smos
<?php
3 ac24dc24 Renato Botelho
/*
4
 * 3gstats.php
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7 b8f91b7c Luiz Souza
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
8 ac24dc24 Renato Botelho
 * All rights reserved.
9
 *
10 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
22 5e589685 smos
23 dc3e3acb smos
ini_set("max_execution_time", "0");
24
25 2e4f5048 doktornotor
if (empty($argv[1])) {
26 5e589685 smos
	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 5aa68a55 Renato Botelho
/* mode is a comma separated value, thus submode is born */
34 d535507a smos
$header = "#seconds,rssi,mode,submode,upstream,downstream,sentbytes,receivedbyts,bwupstream,bwdownstream,simstate,service\n";
35 5e589685 smos
36
$i = 0;
37
38
$record = array();
39
$handle = fopen($device, "r");
40 2e4f5048 doktornotor
if (!$handle) {
41 73ce6909 smos
	echo "Can not open modem stats device\n";
42
	exit(1);
43
}
44 99f95f7d smos
45 7efe5ac5 smos
$record['time'] = 0;
46 99f95f7d smos
$record['rssi'] = 0;
47
$record['mode'] = "0,0";
48 7efe5ac5 smos
$record['upstream'] = 0;
49
$record['downstream'] = 0;
50
$record['sent'] = 0;
51
$record['received'] = 0;
52
$record['bwupstream'] = 0;
53
$record['bwdownstream'] = 0;
54 2e4f5048 doktornotor
$record['simstate'] = 255;
55
$record['service'] = 255;
56 99f95f7d smos
57 7d61beba Phil Davis
while (true) {
58 5e589685 smos
	$string = "";
59
	$string = fgets($handle, 256);
60
61
	$elements = array();
62 f99f51a9 Ermal LUÇI
	$elements = explode(':', $string);
63 5e589685 smos
	$elements[0] = trim($elements[0]);
64
	$elements[1] = trim($elements[1]);
65
66 7d61beba Phil Davis
	switch ($elements[0]) {
67 7efe5ac5 smos
		case "^MODE":
68
			$record['mode'] = $elements[1];
69
			break;
70 d535507a smos
		case "^SRVST":
71
			$record['service'] = $elements[1];
72
			break;
73
		case "^SIMST":
74
			$record['simstate'] = $elements[1];
75
			break;
76 5e589685 smos
		case "^RSSI":
77
			$record['rssi'] = $elements[1];
78
			break;
79
		case "^DSFLOWRPT":
80
			$items = array();
81 f99f51a9 Ermal LUÇI
			$items = explode(',', $elements[1]);
82 5e589685 smos
			$record['time'] = hexdec($items[0]);
83 b45d6db6 smos
			$record['upstream'] = round((floatval(hexdec($items[1])) * 8) /1024);
84
			$record['downstream'] = round((floatval(hexdec($items[2])) * 8) /1024);
85 5e589685 smos
			$record['sent'] = hexdec($items[3]);
86
			$record['received'] = hexdec($items[4]);
87 b45d6db6 smos
			$record['bwupstream'] = round((floatval(hexdec($items[5])) * 8) /1024);
88
			$record['bwdownstream'] = round((floatval(hexdec($items[6])) * 8) /1024);
89 5e589685 smos
			break;
90
	}
91
92 7d61beba Phil Davis
	if ($i > 10) {
93 d535507a smos
		$csv = $header;
94
		$csv .= implode(",", $record);
95
		$csv .= "\n";
96 5e589685 smos
		file_put_contents($statfile, $csv);
97
		$i = 0;
98
	}
99
	$i++;
100
}
101
fclose($handle);
102
?>