File: if.in

package info (click to toggle)
munin 2.0.76-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,064 kB
  • sloc: perl: 11,684; java: 1,924; sh: 1,632; makefile: 636; javascript: 365; python: 267
file content (219 lines) | stat: -rw-r--r-- 5,208 bytes parent folder | download | duplicates (4)
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
#!@@PERL@@
# -*-  perl -*-

=head1 NAME

if - plugin to monitor network interfaces

=head1 APPLICABLE SYSTEMS

Every Linux system with network interfaces configured.

=head1 CONFIGURATION

This plugin automatically detects the network interfaces that have seen traffic by using /proc/net/dev.

To specify the location of the /proc/net/dev, set the PROCNETDEV environment
variable.  For example:

  [if]
  env.PROCNETDEV /proc/net/dev

=head1 USAGE

Link this plugin to @@CONFDIR@@/plugins/ and restart the munin-node.

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 BUGS

None known.

=head1 AUTHOR

Brian De Wolf

=head1 LICENSE

GPLv2

=cut


use warnings;
use strict;

use Munin::Plugin;

my $PROCNETDEV = ($ENV{procnetdev} or
            (-r "/proc/net/dev" and "/proc/net/dev"));
my $mode = ($ARGV[0] or "print");

if($mode eq "autoconf") {
	if($PROCNETDEV and -r $PROCNETDEV) {
		print "yes\n";
	} else {
		print "no\n";
	}
	exit 0;
}


# Collect data
#
open(FH, $PROCNETDEV)
    or die "Failed to open $PROCNETDEV: $!";

my %nics;

# Skip the header
<FH>;<FH>;
while(<FH>) {
    chomp;
    my @fields = split(/:?\s+/);

    # This regex is from if_ autoconf
    next if $fields[1] !~ /^(eth|tap|bond|wlan|ath|ra|sw|vlan|venet|veth|msh)[0-9]+(\.[0-9]+)?$/;
    # Skip this interface if all counters are zero
    next if !grep { /^\d+$/ and $_ != 0 } @fields;

    my $nic = $fields[1];
    $nic =~ s/\./_/;

    $nics{$nic}{in_bytes} = $fields[2];
    $nics{$nic}{out_bytes} = $fields[10];
    $nics{$nic}{in_packets} = $fields[3];
    $nics{$nic}{out_packets} = $fields[11];
    $nics{$nic}{in_errors} = $fields[4];
    $nics{$nic}{out_errors} = $fields[12];

}

close FH
    or die "Failed to close $PROCNETDEV: $!";


# Print data
#
foreach my $type (qw(bytes packets errors)) {
    my ($unit);
    if($type eq "bytes") {
        $unit = "bits";
    } elsif($type eq "packets") {
        $unit = "packets";
    } elsif($type eq "errors") {
        $unit = "errors";
    }
    print "\nmultigraph if_$type\n";
    if($mode eq 'config') {
        print <<EOF;
graph_title Network $unit per interface
graph_args --base 1000
graph_vlabel $unit in (-) / out (+) per second
graph_category network
EOF
        print "graph_order";
        foreach my $dir (qw(in out)) {
            foreach my $nic (keys %nics) {
                print " $type\_$nic\_$dir=if_$type.if_$type\_$nic.if_$type\_$nic\_$dir.$type\_$nic\_$dir";
            }
        }
        print "\n\n";
    }
    # Print overview graphs
    foreach my $nic (keys %nics) {
        foreach my $dir (qw(in out)) {
            my $fname = "$type\_$nic\_$dir";
            if($mode eq 'config') {
                print <<EOF;
$fname.label $nic
$fname.type DERIVE
$fname.min 0
$fname.info $unit $dir by $nic
$fname.draw LINE1
$fname.update no
EOF
                print "$fname.cdef $fname,8,*\n"
                    if($type eq 'bytes');
                print "$fname.negative $type\_$nic\_in\n"
                    if($dir eq 'out');
                print "$fname.graph no\n"
                    if($dir eq 'in');
                print_thresholds($fname);
            } else {
                print "$fname.value " . $nics{$nic}{"${dir}_$type"} . "\n";
            }
        }
    }

    # Per-interface graphs
    foreach my $nic (keys %nics) {
        print "\nmultigraph if_$type.if_$type\_$nic\n";
        if($mode eq 'config') {
            print <<EOF;
graph_title $nic $unit per second
graph_args --base 1000
graph_vlabel $unit per second
graph_category network
EOF
            print "graph_order";
            foreach my $dir (qw(in out)) {
                print " $type\_$nic\_$dir=if_$type\_$nic\_$dir.$type\_$nic\_$dir";
            }
            print "\n\n";
        }

        foreach my $dir (qw(in out)) {
            my $fname = "$type\_$nic\_$dir";
            if($mode eq 'config') {
                print <<EOF;
$fname.label $nic
$fname.type DERIVE
$fname.min 0
$fname.update no
EOF
                print "$fname.cdef $fname,8,*\n"
                    if($type eq 'bytes');
                print "$fname.negative $type\_$nic\_in\n"
                    if($dir eq 'out');
                print "$fname.graph no\n"
                    if($dir eq 'in');
            } else {
                print "$fname.value " . $nics{$nic}{"${dir}_$type"} . "\n";
            }
        }
    }
    # Per-interface-direction graphs
    foreach my $nic (keys %nics) {
        foreach my $dir (qw(in out)) {
            print "\nmultigraph if_$type.if_$type\_$nic.if_$type\_$nic\_$dir\n";
            if($mode eq 'config') {
                print <<EOF;
graph_title $nic $unit $dir per second
graph_args --base 1000
graph_vlabel $unit per second
graph_category network
EOF
            }

            my $fname = "$type\_$nic\_$dir";
            if($mode eq 'config') {
                print <<EOF;
$fname.label $nic
$fname.type DERIVE
$fname.min 0
$fname.draw AREA
EOF
                print "$fname.cdef $fname,8,*\n"
                    if($type eq 'bytes');
            } else {
                print "$fname.value " . $nics{$nic}{"${dir}_$type"} . "\n";
            }
        }
    }
}

# vim: ft=perl sw=4 ts=4 et