Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / network / zenus_ @ 9eeaa526

Historique | Voir | Annoter | Télécharger (6,96 ko)

1 6f0e6960 Paul Saunders
#!/usr/bin/perl
2
# -*- cperl -*-
3
4
=head1 NAME
5
6
zenus_ - Munin plugin to monitor the usage of a Zen Internet Broadband account
7
8
=head1 APPLICABLE SYSTEMS
9
10
Any (Though most likely those connected to a Zen Internet Broadband connection)
11
12
=head1 CONFIGURATION
13
14
This plugin requires the C<zenus> module to be installed. This can be fetched
15
from L<http://www.rachaelandtom.info/zenus>.
16
17
Tip: To see if it's already setup correctly, just run this plugin
18
with the parameter 'autoconf'. If you get a "yes", everything should
19
work like a charm already.
20
21
This configuration section shows the defaults of the plugin:
22
23
	[zenus_*]
24
		env.user
25
		env.pass
26
		env.account
27
		env.tick	60
28
29
You must supply C<env.user> and C<env.pass>. These will be your PORTAL username and password, NOT your ADSL account details.
30
31
You may either specify the account to report through C<env.account> (e.g. zen12345@zen) or by naming the symlink in your plugins directory (e.g. zenus_zen12345_zen). If no account is specified, the default account will be chosen.
32
33
C<env.tick> specifies how often (in minutes) the data will be refreshed from upstream. This is to avoid hitting the Zen servers every five minutes. Data is cached between runs.
34
35
=head1 INTERPRETATION
36
37
The plugin shows the amount of data downloaded and uploaded since the beginning of the chargable period (i.e. the calendar month). A thick line shows the current download allowance (planned plus banked). A thinner line shows the estimated amount remaining at the end of the month. If you are estimated to exceed your allowance before the end of the month, a second line appears showing the rate at which you're downloading beyond the sustainable rate.
38
39
Warnings are sent if your estimated allowance drops below 25% of your cap and if the overspeed rate rises above zero. Critical warnings are sent if your estimated allowance drops below 10% of your cap.
40
41
=head1 MAGIC MARKERS
42
43
	#%# family=auto contrib
44
	#%# capabilities=autoconf suggest
45
46
=head1 AUTHOR
47
48 13a585f2 Paul Saunders
Paul Saunders L<darac+munin@darac.org.uk>
49 6f0e6960 Paul Saunders
50
=cut
51
52
use strict;
53
use warnings;
54
55
use lib $ENV{'MUNIN_LIBDIR'};
56
use Munin::Plugin;
57
use Data::Dump qw(pp);
58
use Time::Local;
59
60
my $ret = undef;
61
62
# Load modules like so
63
if ( !eval "require zenus;" ) {
64
    $ret =
65
      "Could not load zenus. Get it from http://www.rachaelandtom.info/zenus\n";
66
    $ret .= "(BTW, \@INC is: " . join( ', ', @INC ) . ")\n";
67
}
68
69
my $USER = $ENV{user};
70
my $PASS = $ENV{pass};
71
my $ACCT = $ENV{account} || "";
72
my $TICK = $ENV{tick} || 60;      # minutes
73
74
my @name_fields = split /_/, $0;
75
if ( scalar @name_fields == 3 ) {
76
    if ( $name_fields[1] eq '' or $name_fields[2] eq '' ) {
77
        print "Misconfigured symlink. See Documentation\n";
78
        exit 1;
79
    }
80
    else {
81
        $ACCT = $name_fields[1] . '@' . $name_fields[2];
82
    }
83
}
84
85
# If there are more or less than 3 components to the filename,
86
# we just carry on with the default account
87
88
my $lastread;
89
90
sub save_data {
91
    my $hashref = shift;
92
93
    # Do we need to save this info
94
    if ( time > $lastread + ( $TICK * 60 ) ) {
95
96
        $lastread = time;
97
98
        my @save_vector;
99
        push @save_vector, $lastread;
100
101
        # Push the hash values on to the array
102
        foreach ( keys %$hashref ) {
103
            push @save_vector, $_ . '?' . $hashref->{$_};
104
        }
105
106
        #Go!
107
        save_state(@save_vector);
108
    }
109
}
110
111
sub load_data {
112
113
    # Bring the data back in
114
    my @save_vector = restore_state();
115
116
    # Read the timestamp. Do we need to refresh the data?
117
    $lastread = shift @save_vector;
118
119
    my $hashref;
120
    foreach (@save_vector) {
121
        my ( $key, $value ) = split /?/;
122
        $hashref->{$key} = $value;
123
    }
124
    if ( !defined $lastread or time >= ( $lastread + ( $TICK * 60 ) ) ) {
125
126
        # Data is stale
127
128
        #print STDERR "REFRESHING DATA\n";
129
        my $temphash;
130
        eval {
131
            zenus::login( $USER, $PASS );
132
133
            (
134
                $temphash->{name},  $temphash->{used},
135
                $temphash->{avail}, $temphash->{per},
136
                $temphash->{uploadAmount}
137
            ) = zenus::getUsage($ACCT);
138
            (
139
                $temphash->{dayofmonth}, $temphash->{permonth},
140
                $temphash->{avedaily},   $temphash->{maxdaily},
141
                $temphash->{daysremain}, $temphash->{estusage},
142
                $temphash->{daysinmonth}
143
              )
144
              = zenus::estimateRemaining( $temphash->{used},
145
                $temphash->{avail} );
146
            $hashref = $temphash;
147
148
            # If zenus threw an error we won't copy the data over,
149
            #  so we still use the cached data.
150
        };
151
    }
152
    return $hashref;
153
154
}
155
156
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) {
157
    if ($ret) {
158
        print "no ($ret)\n";
159
        exit 1;
160
    }
161
    print "yes\n";
162
    exit 0;
163
}
164
165
if ( defined $ARGV[0] and $ARGV[0] eq "suggest" ) {
166
    if ( defined $USER and defined $PASS ) {
167
        zenus::login( $USER, $PASS );
168
        for my $account ( zenus::getAccounts() ) {
169
            if ( defined $account ) {
170
                $account =~ s/\@/_/g;
171
                print "$account\n";
172
            }
173
        }
174
    }
175
    exit 0;
176
}
177
178
if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
179
    if ($ret) {
180
        print $ret;
181
        exit 1;
182
    }
183
    my $data    = load_data();
184
    my $cap     = sprintf( "%.3f", $data->{avail} );
185
    my $warn    = sprintf( "%.3f", $cap * 0.25 );
186
    my $crit    = sprintf( "%.3f", $cap * 0.1 );
187
    my $datestr = scalar( localtime($lastread) );
188
    print <<EOF;
189
graph_args --base 1000
190
graph_vlabel GBytes out (-) / in (+)
191
graph_category Network
192
graph_title Zen Broadband Usage
193
graph_info Usage of your Zen Broadband account: $data->{name}
194
upload.label Uploaded
195
upload.type GAUGE
196
upload.info Amount of data uploaded (No upper limit)
197
upload.draw AREA
198
upload.graph no
199
upload.colour 00CC00EE
200
download.label Transfer
201
download.type GAUGE
202
download.info Amount of data downloaded (Limit is $cap GB)
203
download.draw AREA
204
download.extinfo Last Read was $datestr
205
download.negative upload
206
download.colour 00CC00EE
207
allowance.label Allowance
208
allowance.type GAUGE
209
allowance.info Amount of data you are allowed to download this month
210
allowance.draw LINE2
211
remaining.label Est'd Remaining
212
remaining.type GAUGE
213
remaining.info Estimated amount of data transfer remaining at the end of the month
214
remaining.draw LINE1
215
remaining.min 0
216
remaining.max $cap
217
remaining.warning $warn:
218
remaining.critical $crit:
219
overrate.label Overspeed Rate
220
overrate.type GAUGE
221
overrate.info Rate at which you're downloading beyond the sustainable rate
222
overrate.draw LINE1
223
overrate.min 0
224
overrate.warning 0:
225
#graph_order download upload allowance remaining overrate
226
EOF
227
228
    save_data($data);
229
    exit 0;
230
}
231
232
my $data = load_data();
233
print "upload.value " . $data->{uploadAmount} . "\n";
234
print "download.value " . $data->{used} . "\n";
235
print "allowance.value " . $data->{avail} . "\n";
236
my $remain = $data->{avail} - $data->{estusage};
237
$remain = 0 if $remain < 0;
238
print "remaining.value " . $remain . "\n";
239
my $overrate = $data->{avedaily} - $data->{maxdaily};
240
$overrate = 0 if $overrate < 0;
241
print "overrate.value " . $overrate . "\n";
242
save_data($data);
243
exit 0;