#!/usr/bin/perl -T use warnings; use strict; $ENV{'PATH'} = '/bin:/usr/bin:/sbin:/usr/sbin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; # define nagios status codes my %nagios = ( 'ok' => ["OK", 0], 'warn' => ["WARNING", 1], 'crit' => ["CRITICAL", 2], 'unknown' => ["UNKNOWN", 3] ); # Print nagios compatible status message and terminat script # with apropiate return code. # # parameter: type of message sub msg ($@) { my $type = shift; my ($msg, $exit) = @{$nagios{$type}}; print $msg, " - ", @_, "\n"; exit $exit; } sub get_if_status (@) { my (@up, @down, @error); while (@_) { my $if = shift @_; next unless ($if =~ m/^([-\w]+)$/); $if = $1; my $pid = open(IFCONFIG, "-|"); msg('crit', "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 = shift; my ($count, $perfdata); $count = keys %$stats; $perfdata = ""; for my $if (keys %$stats) { for (qw( in4 out4 in6 out6 )) { $perfdata = "$perfdata $if-$_=$stats->{$if}->{$_};"; } } msg('ok', "$count interfaces |$perfdata"); } my ($up, $down, $error) = get_if_status(@ARGV); if (@$error) { $error = join ", ", @$error; msg('crit', "Some interfaces does not exists: $error"); } if (@$down) { $down = join ", ", @$down; msg('warn', "Some interfaces are down: $down"); } my $stats = {}; for my $if (@$up) { $stats->{$if} = get_if_stats($if); } print_stats($stats);