From 61dc6720867b17adbf0b58d53723e082a7193115 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 11 Jan 2013 04:05:57 +0100 Subject: allways print performance data (even on WARNING or CRITICAL) --- net_if | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/net_if b/net_if index 265ad80..6f25770 100755 --- a/net_if +++ b/net_if @@ -105,7 +105,9 @@ sub get_if_stats ($) { # nagios status code # additional message # interface statistics -sub print_stats ($) { +sub print_stats ($$$) { + my $status = shift; + my $msg = shift; my $stats = shift; my ($count, $perfdata); @@ -117,26 +119,56 @@ sub print_stats ($) { } } - msg('ok', "$count interfaces |$perfdata"); + msg($status, "$count interfaces$msg |$perfdata"); } +# This function generate a nagios status code and an additional +# message, based on the counts of the interfaces, that are down +# or does not exist. +# +# parameter: +# list of down interfaces +# list of interfaces, that does not exists +# returns: +# (status, msg) for nagios +sub get_status ($$) { + my $down = shift; + my $error = shift; + my ($status, $msg) = ('ok', ''); + + # Order matters! LAST MATCH WINS. + if (@$down) { + # Chech whether some interfaces are down and we have to + # issue a warning. + my $count = @$down; + $down = join ", ", @$down; + + $status = 'warn'; + $msg = ", $count interface(s) are down ($down)"; + } -my ($interfaces, $down, $error) = get_if_status(@ARGV); + if (@$error) { + # Chech whether some interface or interface group, specified + # on the command line, does not exists and we should return a + # critical error. + my $count = @$error; + $error = join ", ", @$error; -if (@$error) { - $error = join ", ", @$error; - msg('crit', "Some interfaces does not exists: $error"); -} + $status = 'crit'; + $msg = ", $count interface(s) does not exists ($error)"; + } -if (@$down) { - $down = join ", ", @$down; - msg('warn', "Some interfaces are down: $down"); + return ($status, $msg); } + +my ($interfaces, $down, $error) = get_if_status(@ARGV); +my ($status, $msg) = get_status($down, $error); + my $stats = {}; for my $if (@$interfaces) { $stats->{$if} = get_if_stats($if); } -print_stats($stats); +print_stats($status, $msg, $stats); msg('crit', "End of script"); -- cgit v1.2.3-1-g7c22