diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2013-01-10 21:50:47 +0100 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2013-01-10 21:50:47 +0100 |
commit | 3fbcb1be0ff7dd667a082990efc3ee4f22d622c2 (patch) | |
tree | 07352164eb59997400e55e0559fb6e2ce13830e4 | |
parent | 730a4b5a2e490733f4734c87acf7ce064d1384fe (diff) | |
download | net_if-3fbcb1be0ff7dd667a082990efc3ee4f22d622c2.tar.gz net_if-3fbcb1be0ff7dd667a082990efc3ee4f22d622c2.tar.bz2 net_if-3fbcb1be0ff7dd667a082990efc3ee4f22d622c2.zip |
replace ok, warning, critical subs with generic msg sub
-rwxr-xr-x | net_if | 39 |
1 files changed, 22 insertions, 17 deletions
@@ -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 = {}; |