-
Notifications
You must be signed in to change notification settings - Fork 3
/
freebox_
executable file
·231 lines (167 loc) · 5.87 KB
/
freebox_
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
#!/usr/bin/env perl
=head1 NAME
freebox - Plugin for monitoring Freebox parameters.
=head1 APPLICABLE SYSTEMS
Any computer on the same LAN as a Freebox v6. Perl module L<WebService::Freebox>
is required for this plugin to function and can be installed from CPAN.
=head1 CONFIGURATION
Prior to using this plugin, an authentication token allowing it connect to
Freebox must be generated. Do this by running the following command:
freebox-authorize $app_id "Munin Freebox plugin" $app_version "Your server"
to obtain the app token and add a section
[freebox_*]
env.app_token = value-of-the-token
to the munin-node configuration file.
=head1 USAGE
This is a wildcard plugin which can be used to monitor several different
families of Freebox parameters. Run it with C<suggest> command line argument
to see all the possible operation modes and create a symlink called
C<freebox_mode> to this plugin from the Munin plugins directory, e.g.
C<freebox_temp>.
=head1 AUTHOR
Copyright 2014 Vadim Zeitlin <vz-cpan@zeitlins.org>
=head1 LICENSE
GPLv2
=head1 HISTORY
=over 4
=item 2014-07-19 Initial version.
=back
=head1 MAGIC MARKERS
#%# family=manual
#%# capabilities=autoconf suggest
=cut
use strict;
use warnings;
use Munin::Plugin;
my ($app_id, $app_version) = ('org.munin.plugin.freebox', '1.0');
sub get_freebox {
if (!defined($ENV{app_token})) {
die <<EOF
Use the following command:
freebox-authorize $app_id "Munin Freebox plugin" $app_version "Your server"
to obtain the app token and add a section
[freebox_*]
env.app_token = value-of-the-token
to use this plugin.
EOF
}
use WebService::Freebox;
my $fb = WebService::Freebox->new(
app_id => $app_id,
app_version => $app_version,
app_token => $ENV{app_token}
);
set_state_name('freebox_session');
my @state = restore_state();
my $session_token = $state[0] if @state;
$session_token = $fb->login($session_token);
save_state($session_token);
return $fb;
}
if (defined $ARGV[0] and $ARGV[0] eq 'autoconf') {
my $error;
if (!eval 'require WebService::Freebox;') {
$error = 'WebService::Freebox not found';
} else {
eval {
WebService::Freebox->new(
app_id => $app_id,
app_version => $app_version
);
};
$error = $@ if $@;
}
print defined $error ? "no ($error)\n" : "yes\n";
exit 0
}
my @modes = ('temp', 'bandwidth', 'freeplug', );
if (defined $ARGV[0] and $ARGV[0] eq 'suggest') {
print "$_\n" for @modes;
exit 0
}
if ($0 !~ /freebox_(.+)*$/ or !grep $1, @modes) {
die "Must be called freebox_{" . join('|',@modes) . "}.\n"
}
my $mode = $1;
my %temps = (
cpum => 'CPU (Marvell)',
cpub => 'CPU (Broadcom)',
sw => 'switch',
);
if (defined $ARGV[0] and $ARGV[0] eq 'config') {
print "graph_category freebox\n";
if ($mode eq 'temp') {
print "graph_args --base 1000 -l 0\n";
print "graph_title Freebox temperatures\n";
print "graph_vlabel degrees Celsius\n";
for my $t (keys %temps) {
print "$t.label $temps{$t}\n";
print "$t.warning 80\n";
print "$t.critical 90\n";
}
} elsif ($mode eq 'bandwidth') {
print "graph_title Bandwidth\n";
print "graph_vlabel Byte / second\n";
for my $what (qw(bandwidth rate)) {
for my $dir (qw(up down)) {
print "${what}_${dir}.label $what $dir\n"
}
}
} elsif ($mode eq 'freeplug') {
print "graph_title Connection speed\n";
print "graph_vlabel Mbit / second\n";
my $fps = get_freebox()->get_all_freeplugs();
# Take just the stations as coordinator freeplugs don't have tx/rx stats.
my @stations = grep { $_->{net_role} eq 'sta' } @$fps;
if (@stations <= 1) {
print "freeplug_rx.label receive speed\n";
print "freeplug_tx.label transmit speed\n";
} else {
# If we have more than one station, number them to allow
# distinguishing between them.
for (my $fpnum = 1; $fpnum <= @stations; $fpnum++) {
print "freeplug${fpnum}_rx.label receive speed for station $fpnum\n";
print "freeplug${fpnum}_tx.label transmit speed for station $fpnum\n";
}
}
}
exit 0
}
die "Usage: $0 [autoconf|config]\n" if @ARGV;
my $fb = get_freebox();
if ($mode eq 'temp') {
my $sc = $fb->get_system_config();
for my $t (keys %temps) {
print "$t.value " . $sc->{"temp_$t"} . "\n";
}
} elsif ($mode eq 'bandwidth') {
my $cs = $fb->get_connection_status();
for my $dir (qw(up down)) {
print "rate_$dir.value " . $cs->{"rate_$dir"} . "\n";
# Those are in bit/s, translate to bytes.
print "bandwidth_$dir.value " . $cs->{"bandwidth_$dir"}/8 . "\n";
}
} elsif ($mode eq 'freeplug') {
GET_FREEPLUGS: {
my $fps = get_freebox()->get_all_freeplugs();
my @stations = grep { $_->{net_role} eq 'sta' } @$fps;
last if !@stations;
# Freeplug stats are invalid if it's not used, so query it once
# again if it was inactive for some time, this is typically enough
# to wake it up and get something meaningful from it.
my $fp = $stations[0];
redo GET_FREEPLUGS if $fp->{rx_rate} == -1 or $fp->{tx_rate} == -1;
if (@stations == 1) {
print "freeplug_rx.value " . $fp->{rx_rate} . "\n";
print "freeplug_tx.value " . $fp->{tx_rate} . "\n";
} else {
my $fpnum = 1;
for $fp (@stations) {
print "freeplug${fpnum}_rx.value " . $fp->{rx_rate} . "\n";
print "freeplug${fpnum}_tx.value " . $fp->{tx_rate} . "\n";
$fpnum++;
}
}
}
}
exit 0