From 8e12d7e44c944821c03524696c7defc92fc774a1 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 3 Jan 2012 09:16:15 +0100 Subject: [PATCH] check_junos, Nagios::Plugin::JUNOS: Call checks from the plugin. --- check_junos.pl | 40 +++++-------- perl/lib/Nagios/Plugin/JUNOS.pm | 103 +++++++++++++++++++++++++++----- 2 files changed, 103 insertions(+), 40 deletions(-) diff --git a/check_junos.pl b/check_junos.pl index 4366b3a..8629763 100755 --- a/check_junos.pl +++ b/check_junos.pl @@ -47,8 +47,6 @@ use Nagios::Plugin::JUNOS; binmode STDOUT, ":utf8"; -my $valid_checks = "interfaces|chassis_environment|system_storage"; - # TODO: # * chassis_routing_engine: show chassis routing-engine (-> number and status) @@ -119,38 +117,30 @@ my @args = ( }, ); +my %checks = ( + interfaces => \&check_interfaces, + chassis_environment => \&check_chassis_environment, + system_storage => \&check_system_storage, +); + my $junos = undef; foreach my $arg (@args) { $plugin->add_arg($arg); } +foreach my $check (keys %checks) { + $plugin->add_check_impl($check, $checks{$check}); +} + +$plugin->set_default_check('chassis_environment'); + +# configure removes any options from @ARGV $plugin->configure(); -$plugin->set_checks($valid_checks, 'chassis_environment', @ARGV); +$plugin->set_checks(@ARGV); $junos = $plugin->connect(); -foreach my $check ($plugin->get_checks()) { - my @targets = (); - - if (defined $check->{'target'}) { - @targets = @{$check->{'target'}}; - } - - $plugin->set_thresholds( - warning => $check->{'warning'}, - critical => $check->{'critical'}, - ); - - if ($check->{'name'} eq 'interfaces') { - check_interfaces(@targets); - } - elsif ($check->{'name'} eq 'chassis_environment') { - check_chassis_environment(@targets); - } - elsif ($check->{'name'} eq 'system_storage') { - check_system_storage(@targets); - } -} +$plugin->run_checks(); my ($code, $msg) = $plugin->check_messages(join => ', '); diff --git a/perl/lib/Nagios/Plugin/JUNOS.pm b/perl/lib/Nagios/Plugin/JUNOS.pm index 4cad1a0..9475518 100644 --- a/perl/lib/Nagios/Plugin/JUNOS.pm +++ b/perl/lib/Nagios/Plugin/JUNOS.pm @@ -107,6 +107,59 @@ sub add_arg ); } +sub add_check_impl +{ + my $self = shift; + my $name = shift; + my $sub = shift; + + if ((! $name) || (! $sub) || (ref($sub) ne "CODE")) { + carp "Invalid check specification: $name -> $sub"; + return; + } + + if (! defined($self->{'check_impls'})) { + $self->{'check_impls'} = {}; + } + + $self->{'check_impls'}->{$name} = $sub; +} + +sub get_check_impl +{ + my $self = shift; + my $name = shift; + + if (! defined($self->{'check_impls'}->{$name})) { + return; + } + return $self->{'check_impls'}->{$name}; +} + +sub is_valid_check +{ + my $self = shift; + my $name = shift; + + if (defined $self->{'check_impls'}->{$name}) { + return 1; + } + return; +} + +sub set_default_check +{ + my $self = shift; + my $def = shift; + + if (! $self->is_valid_check($def)) { + carp "set_default_check: Check '$def' does not exist"; + return; + } + + $self->{'default_check'} = $def; +} + sub configure { my $self = shift; @@ -155,13 +208,12 @@ sub _get_conf sub _add_single_check { - my $self = shift; - my $valid_checks = shift; - my @check = split(m/,/, shift); + my $self = shift; + my @check = split(m/,/, shift); my %c = (); - if ($check[0] !~ m/\b(?:$valid_checks)\b/) { + if (! $self->is_valid_check($check[0])) { return "ERROR: invalid check '$check[0]'"; } @@ -188,10 +240,8 @@ sub _add_single_check sub set_checks { - my $self = shift; - my $valid_checks = shift; - my $default = shift; - my @checks = @_; + my $self = shift; + my @checks = @_; my $err_str = "ERROR:"; @@ -200,12 +250,14 @@ sub set_checks } if (scalar(@checks) == 0) { - $self->{'conf'}->{'checks'}[0] = { - name => $default, - target => [], - warning => undef, - critical => undef, - }; + if ($self->{'default_check'}) { + $self->{'conf'}->{'checks'}->[0] = { + name => $self->{'default_check'}, + target => [], + warning => undef, + critical => undef, + }; + } return 1; } @@ -214,7 +266,7 @@ sub set_checks foreach my $check (@checks) { my $e; - $e = $self->_add_single_check($valid_checks, $check); + $e = $self->_add_single_check($check); if ($e =~ m/^ERROR: (.*)$/) { $err_str .= " $1,"; } @@ -277,6 +329,27 @@ sub connect return $junos; } +sub run_checks +{ + my $self = shift; + + foreach my $check ($self->get_checks()) { + my @targets = (); + + if (defined $check->{'target'}) { + @targets = @{$check->{'target'}}; + } + + $self->set_thresholds( + warning => $check->{'warning'}, + critical => $check->{'critical'}, + ); + + my $sub = $self->get_check_impl($check->{'name'}); + $sub->(@targets); + } +} + sub verbose { my $self = shift;