-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfritzbw_.php
executable file
·134 lines (120 loc) · 3.6 KB
/
fritzbw_.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
#!/usr/bin/php
<?php
// Configure me hard!
// Please configure Fritz!Box Login Data in /lib/fritzbox_user.conf.php
$tmpFile="/tmp/fritzbw.db";
$tmpFileMaxAge="60";
// NO NEED TO EDIT BELOW HERE!
require_once(__DIR__."/lib/fritzbox_api.class.php");
require_once(__DIR__."/lib/lua_parser.class.php");
require_once(__DIR__."/lib/sbLib.php");
function lowhigh($l,$h)
{
$res=$l+($h*pow(65536,2));
$res=round($res/1000,0);
return($res);
}
// Check if we have to do a new DOCSIS query
if(!file_exists($tmpFile) || (filemtime($tmpFile) < time() - $tmpFileMaxAge)) {
// Get us a new fritzbox handler
$fritz = new fritzbox_api();
// Get the "Onlien-Monitor" details page
$params = array('getpage' => '/internet/inetstat_counter.lua');
$output = $fritz->doGetRequest($params);
// Disconnect from the Webinterface
$fritz = null;
$dat = formatLua($output);
// Heute empfangen
$high=$dat["QUERIES"]["inetstat:status/Today/BytesReceivedHigh"];
$low=$dat["QUERIES"]["inetstat:status/Today/BytesReceivedLow"];
$bwstats['received']=lowhigh($low,$high);
$high=$dat["QUERIES"]["inetstat:status/Today/BytesSentHigh"];
$low=$dat["QUERIES"]["inetstat:status/Today/BytesSentLow"];
$bwstats['sent']=lowhigh($low,$high);
// Alte Daten lesen
$bwstats['receivedDelta'] = 0;
$bwstats['sentDelta'] = 0;
if(file_exists($tmpFile)) {
$bwstatsOld = unserialize(file_get_contents($tmpFile));
$tmpDelta = ($bwstats['received'] - $bwstatsOld['received']);
if($tmpDelta > 0) {
$bwstats['receivedDelta'] = $tmpDelta;
}
$tmpDelta = ($bwstats['sent'] - $bwstatsOld['sent']);
if($tmpDelta > 0) {
$bwstats['sentDelta'] = $tmpDelta;
}
}
// Serialize what we have
file_put_contents($tmpFile,serialize($bwstats));
} else {
// Unserialize from file
$bwstats = unserialize(file_get_contents($tmpFile));
}
// Munin plugin part
// Check if the user wants a config suggestions
if(isset($argv[1])) {
if($argv[1]=="suggest") {
echo "ln -s ".$argv[0]." /etc/munin/plugins/fritzbw_sent\n";
echo "ln -s ".$argv[0]." /etc/munin/plugins/fritzbw_received\n";
echo "ln -s ".$argv[0]." /etc/munin/plugins/fritzbw_both\n";
exit(0);
}
}
// Detect our function derived from our name:
preg_match("/.*_(sent|received|both)/",$argv[0],$selector);
// up or down?
$mode=$selector[1];
/*
* MUNIN fritzbw_received
* Shows Received Data
*/
if($mode=="received" && !empty($argv[1])){
if($argv[1] == "config") {
echo "graph_title FritzBox Bandwidth - Received\n";
echo "graph_vlabel Bandwidth"."\n";
echo "graph_scale no"."\n";
echo "graph_category fritzbox-bandwidth"."\n";
echo "bw.label data received [kB]"."\n";
exit(0);
}
}
if($mode=="received") {
echo "bw.value ".($bwstats["receivedDelta"])."\n";
}
/*
* MUNIN fritzbw_sent
* Shows Sent Data
*/
if($mode=="sent" && !empty($argv[1])){
if($argv[1] == "config") {
echo "graph_title FritzBox Bandwidth - Sent\n";
echo "graph_vlabel Bandwidth"."\n";
echo "graph_scale no"."\n";
echo "graph_category fritzbox-bandwidth"."\n";
echo "bw.label data sent [kB]"."\n";
exit(0);
}
}
if($mode=="sent") {
echo "bw.value ".($bwstats["sentDelta"])."\n";
}
/*
* MUNIN fritzbw combined
*/
if($mode=="both" && !empty($argv[1])){
if($argv[1] == "config") {
echo "graph_title FritzBox Bandwidth\n";
echo "graph_vlabel Bandwidth"."\n";
echo "graph_scale yes"."\n";
echo "graph_category fritzbox-bandwidth"."\n";
echo "bwrecv.label data received [kB]"."\n";
echo "bwsent.label data sent [kB]"."\n";
exit(0);
}
}
if($mode=="both") {
echo "bwrecv.value ".$bwstats["receivedDelta"]."\n";
echo "bwsent.value ".$bwstats["sentDelta"]."\n";
}
?>