From 3fbcb1be0ff7dd667a082990efc3ee4f22d622c2 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 10 Jan 2013 21:50:47 +0100 Subject: replace ok, warning, critical subs with generic msg sub --- net_if | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/net_if b/net_if index 3ca31a8..62d1dab 100755 --- a/net_if +++ b/net_if @@ -6,19 +6,24 @@ 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; +# 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 { @@ -30,7 +35,7 @@ sub get_if_status { $if = $1; my $pid = open(IFCONFIG, "-|"); - die "$0: fork: $!" unless defined $pid; + msg('crit', "fork: $!") unless defined $pid; if ($pid == 0) { # child @@ -92,19 +97,19 @@ sub print_stats { } } - ok("$count interfaces |$perfdata"); + msg('ok', "$count interfaces |$perfdata"); } my ($up, $down, $error) = get_if_status(@ARGV); if (@$error) { $error = join ", ", @$error; - critical("Some interfaces does not exists: $error"); + msg('crit', "Some interfaces does not exists: $error"); } if (@$down) { $down = join ", ", @$down; - warning("Some interfaces are down: $down"); + msg('warn', "Some interfaces are down: $down"); } my $stats = {}; -- cgit v1.2.3-1-g7c22