check_junos, Nagios::Plugin::JUNOS: Call checks from the plugin.

This commit is contained in:
Sebastian Harl 2012-01-03 09:16:15 +01:00
parent 1ef30b92b6
commit 8e12d7e44c
2 changed files with 103 additions and 40 deletions

View file

@ -47,8 +47,6 @@ use Nagios::Plugin::JUNOS;
binmode STDOUT, ":utf8"; binmode STDOUT, ":utf8";
my $valid_checks = "interfaces|chassis_environment|system_storage";
# TODO: # TODO:
# * chassis_routing_engine: show chassis routing-engine (-> number and status) # * 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; my $junos = undef;
foreach my $arg (@args) { foreach my $arg (@args) {
$plugin->add_arg($arg); $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->configure();
$plugin->set_checks($valid_checks, 'chassis_environment', @ARGV); $plugin->set_checks(@ARGV);
$junos = $plugin->connect(); $junos = $plugin->connect();
foreach my $check ($plugin->get_checks()) { $plugin->run_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);
}
}
my ($code, $msg) = $plugin->check_messages(join => ', '); my ($code, $msg) = $plugin->check_messages(join => ', ');

View file

@ -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 sub configure
{ {
my $self = shift; my $self = shift;
@ -155,13 +208,12 @@ sub _get_conf
sub _add_single_check sub _add_single_check
{ {
my $self = shift; my $self = shift;
my $valid_checks = shift; my @check = split(m/,/, shift);
my @check = split(m/,/, shift);
my %c = (); my %c = ();
if ($check[0] !~ m/\b(?:$valid_checks)\b/) { if (! $self->is_valid_check($check[0])) {
return "ERROR: invalid check '$check[0]'"; return "ERROR: invalid check '$check[0]'";
} }
@ -188,10 +240,8 @@ sub _add_single_check
sub set_checks sub set_checks
{ {
my $self = shift; my $self = shift;
my $valid_checks = shift; my @checks = @_;
my $default = shift;
my @checks = @_;
my $err_str = "ERROR:"; my $err_str = "ERROR:";
@ -200,12 +250,14 @@ sub set_checks
} }
if (scalar(@checks) == 0) { if (scalar(@checks) == 0) {
$self->{'conf'}->{'checks'}[0] = { if ($self->{'default_check'}) {
name => $default, $self->{'conf'}->{'checks'}->[0] = {
target => [], name => $self->{'default_check'},
warning => undef, target => [],
critical => undef, warning => undef,
}; critical => undef,
};
}
return 1; return 1;
} }
@ -214,7 +266,7 @@ sub set_checks
foreach my $check (@checks) { foreach my $check (@checks) {
my $e; my $e;
$e = $self->_add_single_check($valid_checks, $check); $e = $self->_add_single_check($check);
if ($e =~ m/^ERROR: (.*)$/) { if ($e =~ m/^ERROR: (.*)$/) {
$err_str .= " $1,"; $err_str .= " $1,";
} }
@ -277,6 +329,27 @@ sub connect
return $junos; 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 sub verbose
{ {
my $self = shift; my $self = shift;