From e9f416e431daf16c86f62b589fd0d98aef31d6c9 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Mon, 14 Jan 2013 04:06:56 +0100 Subject: net_if: use Nagios::Plugin for output --- net_if | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/net_if b/net_if index 6f25770..702de79 100755 --- a/net_if +++ b/net_if @@ -2,30 +2,12 @@ use warnings; use strict; +use Nagios::Plugin; $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; -} +my $n = Nagios::Plugin->new; # Check the status of the given network interface and return # three lists of the interfaces sorted by status (all, down, error). @@ -48,7 +30,7 @@ sub get_if_status (@) { # necessary to pipe stderr of the child process away, while # using the list syntax for open and exec. my $pid = open(IFCONFIG, "-|"); - msg('crit', "fork: $!") unless defined $pid; + $n->nagois_exit(CRITICAL, "fork: $!") unless defined $pid; if ($pid == 0) { # redirect stderr @@ -119,7 +101,7 @@ sub print_stats ($$$) { } } - msg($status, "$count interfaces$msg |$perfdata"); + $n->nagios_exit($status, "$count interfaces$msg |$perfdata"); } # This function generate a nagios status code and an additional @@ -134,7 +116,7 @@ sub print_stats ($$$) { sub get_status ($$) { my $down = shift; my $error = shift; - my ($status, $msg) = ('ok', ''); + my ($status, $msg) = (OK, ''); # Order matters! LAST MATCH WINS. if (@$down) { @@ -143,7 +125,7 @@ sub get_status ($$) { my $count = @$down; $down = join ", ", @$down; - $status = 'warn'; + $status = WARNING; $msg = ", $count interface(s) are down ($down)"; } @@ -154,7 +136,7 @@ sub get_status ($$) { my $count = @$error; $error = join ", ", @$error; - $status = 'crit'; + $status = CRITICAL; $msg = ", $count interface(s) does not exists ($error)"; } @@ -171,4 +153,4 @@ for my $if (@$interfaces) { } print_stats($status, $msg, $stats); -msg('crit', "End of script"); +$n->nagios_exit(CRITICAL, "End of script"); -- cgit v1.2.3-1-g7c22