#!/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 () { if ($_ =~ m/^([-a-v0-9]+): flags=([0-9]+)) { 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)