-
Notifications
You must be signed in to change notification settings - Fork 6
/
fritz.pl
executable file
·265 lines (213 loc) · 6.7 KB
/
fritz.pl
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/perl
#
# RRD script to display FRITZ!Box connection stats
#
# Copyright (C) 2017 Christian Garbs <[email protected]>
# Licensed under GNU GPL v3 or later.
#
# This file is part of my rrd scripts (https://github.com/mmitch/rrd).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# This script should be run every 5 minutes.
#
use strict;
use warnings;
use RRDs;
use Net::Fritz::Box 0.0.8;
# parse configuration file
my %conf;
eval(`cat ~/.rrd-conf.pl`);
die $@ if $@;
# set variables
my $datafile = "$conf{DBPATH}/fritz.rrd";
my $picbase = "$conf{OUTPATH}/connecttime-"; # supersedes old 'connecttime.pl' script!
my $picbase2 = "$conf{OUTPATH}/fritz-";
my $ipfile = "$conf{FRITZ_IP_FILE}";
# global error variable
my $ERR;
# global Net::Fritz::Box instance
my $fritz;
############################### subroutines ###############################
sub fritz_connect() {
# get credentials
my ($user, $pass);
my $rcfile = $ENV{HOME}.'/.fritzrc';
if (-r $rcfile) {
open FRITZRC, '<', $rcfile or die $!;
while (my $line = <FRITZRC>) {
chomp $line;
if ($line =~ /^(\S+)\s*=\s*(.*?)$/) {
if ($1 eq 'username') {
$user = $2;
}
elsif ($1 eq 'password') {
$pass = $2
}
}
}
close FRITZRC or die $!;
}
# connect to FRITZ!Box
my $box = Net::Fritz::Box->new(
username => $user,
password => $pass
);
$box->errorcheck;
return $box;
}
sub get_link_type() {
my $dsl_link_info = $fritz->call(':WANDSLLinkConfig:', 'GetDSLLinkInfo');
return 0 if $dsl_link_info->error;
return 0 unless $dsl_link_info->data->{'NewLinkStatus'} eq 'Up';
return $dsl_link_info->data->{'NewLinkType'};
}
sub get_wan_type() {
my $link_type = get_link_type();
return 0 unless $link_type;
my $service_name = {
'PPPoE' => ':WANPPPConnection:',
'IP' => ':WANIPConnection:',
}->{$link_type};
return 0 unless defined $service_name;
return $service_name;
}
sub get_external_ip($) {
my ($wan_type) = @_;
my $external_ip = $fritz->call($wan_type, 'GetExternalIPAddress');
return '' if $external_ip->error;
return $external_ip->data->{'NewExternalIPAddress'};
}
sub get_uptime($) {
my ($wan_type) = @_;
my $uptime = $fritz->call($wan_type, 'GetStatusInfo');
return 0 if $uptime->error;
return $uptime->data->{'NewUptime'};
}
sub get_bytes_in() {
my $response = $fritz->call(':WANCommonInterfaceConfig:', 'GetTotalBytesReceived');
return 0 if $response->error;
return $response->data->{'NewTotalBytesReceived'};
}
sub get_bytes_out() {
my $response = $fritz->call(':WANCommonInterfaceConfig:', 'GetTotalBytesSent');
return 0 if $response->error;
return $response->data->{'NewTotalBytesSent'};
}
sub write_to_file($$) {
my ($file, $content) = @_;
open FILE, '>', $file or die "error opening to `$file': $!";
print FILE $content;
close FILE or die "error opening to `$file': $!";
}
############################### subroutines ###############################
# get graph minimum time ($DETAIL_TIME in rrd_runall.sh)
my $MINTIME = 1;
if (defined $ARGV[0])
{
$MINTIME = shift @ARGV;
}
# whoami?
my $hostname = `/bin/hostname`;
chomp $hostname;
# generate database if absent
if (! -e $datafile ) {
RRDs::create($datafile,
'DS:connecttime:GAUGE:600:0:31536000', # 1 year
'DS:input:DERIVE:600:0:15000000', # about 100MBit
'DS:output:DERIVE:600:0:15000000', # about 100MBit (symmetric)
'RRA:AVERAGE:0.5:1:600',
'RRA:AVERAGE:0.5:6:700',
'RRA:AVERAGE:0.5:24:775',
'RRA:AVERAGE:0.5:288:797',
'RRA:MAX:0.5:1:600',
'RRA:MAX:0.5:6:700',
'RRA:MAX:0.5:24:775',
'RRA:MAX:0.5:288:797'
);
$ERR=RRDs::error;
die "ERROR while creating $datafile: $ERR\n" if $ERR;
print "created $datafile\n";
}
# gather data
my ($connecttime, $input, $output) = (0, 0, 0);
$fritz = fritz_connect();
my $wan_type = get_wan_type();
if ($wan_type) {
write_to_file($ipfile, get_external_ip($wan_type));
$connecttime = get_uptime($wan_type);
}
$input = get_bytes_in();
$output = get_bytes_out();
# update database
RRDs::update($datafile,
"N:$connecttime:$input:$output"
);
die "ERROR while adding $datafile $connecttime: $ERR\n" if $ERR;
# draw pictures (connecttime)
foreach ( [3600, "hour"], [86400, "day"], [604800, "week"], [31536000, "year"] ) {
my ($time, $scale) = @{$_};
next if $time < $MINTIME;
RRDs::graph($picbase . $scale . ".png",
"--start=-${time}",
'--lazy',
'--imgformat=PNG',
"--title=${hostname} time since last connect/IP change (last $scale)",
'--base=1024',
"--width=$conf{GRAPH_WIDTH}",
"--height=$conf{GRAPH_HEIGHT}",
'--color=BACK#f3f3f3f3',
'--color=SHADEA#f3f3f3f3',
'--color=SHADEB#f3f3f3f3',
"DEF:seconds=${datafile}:connecttime:AVERAGE",
"DEF:oldseconds=${datafile}:connecttime:AVERAGE:end=now-${time}s:start=end-${time}s",
"SHIFT:oldseconds:$time",
'CDEF:hours=seconds,3600,/',
'CDEF:oldhours=oldseconds,3600,/',
'AREA:hours#00D000:connection time [h]',
"LINE:oldhours#D0D0D0:connection time [h] previous $scale",
'COMMENT:\n',
'COMMENT:\n',
);
$ERR=RRDs::error;
die "ERROR while drawing $datafile $time: $ERR\n" if $ERR;
}
# draw pictures (I/O)
foreach ( [3600, "hour"], [86400, "day"], [604800, "week"], [31536000, "year"] ) {
my ($time, $scale) = @{$_};
next if $time < $MINTIME;
RRDs::graph($picbase2 . $scale . ".png",
"--start=-${time}",
'--lazy',
'--imgformat=PNG',
"--title=${hostname} FRITZ!Box network traffic (last $scale)",
"--width=$conf{GRAPH_WIDTH}",
"--height=$conf{GRAPH_HEIGHT}",
'--color=BACK#f3f3f3f3',
'--color=SHADEA#f3f3f3f3',
'--color=SHADEB#f3f3f3f3',
'--alt-autoscale',
'--logarithmic',
'--units=si',
"DEF:input=${datafile}:input:AVERAGE",
"DEF:output=${datafile}:output:AVERAGE",
'AREA:input#00D000:input [bytes/sec]',
'AREA:output#0000D0:output [bytes/sec]',
'LINE1:input#00D000:',
'COMMENT:\n',
);
$ERR=RRDs::error;
die "ERROR while drawing $datafile $time: $ERR\n" if $ERR;
}