Projet

Général

Profil

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

root / plugins / network / vnstat_month @ f14628ad

Historique | Voir | Annoter | Télécharger (2,05 ko)

1 ec5b5bd4 ?var Arnfj?r? Bjarmason
#!/usr/bin/env perl
2
use 5.010;
3
use strict;
4
use warnings;
5
use autodie;
6
use List::Util 'sum';
7
8
=head1 NAME
9
10
vnstat_month - Run C<vnstat --dumpdb> and report usage this month
11
12
=head1 SYNOPSIS
13
14
Optionally, in In F</etc/munin/plugin-conf.d/munin-node>:
15
16
    # Set the max bandwidth to 800 GB per month
17
    [vnstat_month]
18
    env.limittt 800000
19
20
=head1 AUTHOR
21
22
E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org>
23
24
=head1 LICENSE
25
26
This program is in the public domain.
27
28
=head1 MAGIC MARKERS
29
30
  #%# family=auto
31
32
=cut
33
34
my $limittt = $ENV{limittt};
35
36
given ($ARGV[0]) {
37
    when ("config") {
38
        print <<END;
39
graph_title Total traffic this month
40
graph_args --base 1000 --lower-limit 0
41
graph_vlabel Monthly traffic
42
graph_category network
43
graph_info Total network traffic in bytes.
44
totaltx.label Sent
45
totaltx.info Total data sent.
46
totaltx.cdef totaltx,1000000,*
47
totalrx.label Received
48
totalrx.info Total data received.
49
totalrx.cdef totalrx,1000000,*
50
totaltt.label Total
51
totaltt.info Total data sent & received.
52
totaltt.cdef totaltt,1000000,*
53
END
54
        if ($limittt) {
55
            print <<END;
56
limittt.label Limit
57
limittt.info The data transfer limit for this month
58
limittt.cdef limittt,1000000,*
59
END
60
        }
61
    }
62
    default {
63
        my @days = get_daylines();
64
        my @relevant = grep {
65
            my ($this_month) = localtime =~ /^\S+ (\S+)\b/;
66
            localtime($_->{time}) =~ /^\S+ $this_month\b/;
67
        } @days;
68
69
        my $rx = sum( map { $_->{rx_mib} } @relevant );
70
        my $tx = sum( map { $_->{tx_mib} } @relevant );
71
        my $tt = $rx + $tx;
72
73
        print <<"END";
74
totalrx.value $rx
75
totaltx.value $tx
76
totaltt.value $tt
77
END
78
        if ($limittt) {
79
            print <<"END";
80
limittt.value $limittt
81
END
82
        }
83
    }
84
}
85
86
sub get_daylines {
87
    open my $vnstat, "vnstat --dumpdb |";
88
    my @lines;
89
    while (my $line = <$vnstat>) {
90
        chomp $line;
91
        my $ns = qr/[^:]+/;
92
        next unless $line =~ /^d;(?<day>$ns);(?<time>$ns);(?<rx_mib>$ns);(?<tx_mib>$ns);(?<rx_kib>$ns);(?<tx_kib>$ns);(?<in_use>$ns)$/;
93
        push @lines => { %+ };
94
    }
95
    return @lines;
96
}