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
|
#!/usr/bin/perl -T
use warnings;
use strict;
$ENV{'PATH'} = '/bin:/usr/bin:/sbin:/usr/sbin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
sub critical {
print "CRITICAL - @_\n";
exit 2;
}
sub warning {
print "WARNING - @_\n";
exit 1;
}
sub ok {
print "OK - @_\n";
exit 0;
}
sub get_if_status {
my (@up, @down, @error);
while (@_) {
my $if = shift @_;
next unless ($if =~ m/^([-\w]+)$/);
$if = $1;
my $pid = open(IFCONFIG, "-|");
die "$0: fork: $!" unless defined $pid;
if ($pid == 0) {
# child
open(STDERR, ">", "/dev/null");
exec("ifconfig", $if)
}
while (<IFCONFIG>) {
if ($_ =~ m/^$if: flags=([0-9]+)</) {
if ($1 & 1) {
push(@up, $if);
}
else {
push (@down, $if);
}
}
}
close(IFCONFIG) or push(@error, $if);
}
return (\@up, \@down, \@error);
}
sub get_if_stats {
my $if = shift @_;
my $stats = {};
open(PFCTL, "-|", "pfctl", "-vvsI", "-i", $if);
while (<PFCTL>) {
if ($_ =~ m{In4/Pass.*Bytes: ([0-9]+)}) {
$stats->{'in4'} = $1;
}
elsif ($_ =~ m{Out4/Pass.*Bytes: ([0-9]+)}) {
$stats->{'out4'} = $1;
}
elsif ($_ =~ m{In6/Pass.*Bytes: ([0-9]+)}) {
$stats->{'in6'} = $1;
}
elsif ($_ =~ m{Out6/Pass.*Bytes: ([0-9]+)}) {
$stats->{'out6'} = $1;
}
}
close(PFCTL);
return $stats;
}
sub print_stats {
my ($stats, $count, $perfdata, $if);
$stats = shift @_;
$count = keys %$stats;
$perfdata = "";
for (keys %$stats) {
$if = $_;
for (qw( in4 out4 in6 out6 )) {
$perfdata = "$perfdata $if-$_=$stats->{$if}->{$_}";
}
}
ok("$count interfaces | $perfdata");
}
my ($up, $down, $error) = get_if_status(@ARGV);
if (@$error) {
$error = join ", ", @$error;
critical("Some interfaces does not exists: $error");
}
if (@$down) {
$down = join ", ", @$down;
warning("Some interfaces are down: $down");
}
my $stats = {};
while (@$up) {
my $if = shift @$up;
$stats->{$if} = get_if_stats($if);
}
print_stats($stats)
|