check_junos: Added -C option to cache interface information.

If used, this option will let the interfaces check query *all* interface
information on the first API call. This will then be used when further
querying interfaces (e.g. physical interfaces of aggregates).
This commit is contained in:
Sebastian Harl 2012-04-03 16:35:27 +02:00
parent e1da92dff9
commit 1cfdcd3667

View file

@ -58,7 +58,7 @@ my $plugin = Nagios::Plugin::JUNOS->new(
blurb => 'Monitor Juniper™ Switches.', blurb => 'Monitor Juniper™ Switches.',
usage => usage =>
"Usage: %s [-v|--verbose] [-H <host>] [-p <port>] [-t <timeout] "Usage: %s [-v|--verbose] [-H <host>] [-p <port>] [-t <timeout]
[-U <user>] [-P <password] check-tuple [...]", [-C] [-U <user>] [-P <password] check-tuple [...]",
license => license =>
"This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. "This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY.
It may be used, redistributed and/or modified under the terms of the 3-Clause It may be used, redistributed and/or modified under the terms of the 3-Clause
@ -110,7 +110,15 @@ my %checks = (
my $junos = undef; my $junos = undef;
my $cache = {};
$plugin->add_common_args(); $plugin->add_common_args();
$plugin->add_arg({
spec => 'caching|C',
usage => '-C, --caching',
desc => 'Enabling caching of API results',
default => 0,
});
foreach my $check (keys %checks) { foreach my $check (keys %checks) {
$plugin->add_check_impl($check, $checks{$check}); $plugin->add_check_impl($check, $checks{$check});
@ -184,12 +192,17 @@ sub get_interfaces
my @ifaces = (); my @ifaces = ();
my @ret = (); my @ret = ();
if (defined($cache->{'interfaces'})) {
@ifaces = @{$cache->{'interfaces'}};
}
else {
my $cmd = 'get_interface_information'; my $cmd = 'get_interface_information';
my $res; my $res;
my $args = { detail => 1 }; my $args = { detail => 1 };
if ((scalar(@targets) == 1) && (! $opts->{'with_description'})) { if ((scalar(@targets) == 1) && (! $opts->{'with_description'})
&& (! $plugin->{'conf'}->{'caching'})) {
$args->{'interface_name'} = $targets[0]; $args->{'interface_name'} = $targets[0];
} }
$res = $plugin->send_query($cmd, $args); $res = $plugin->send_query($cmd, $args);
@ -199,6 +212,11 @@ sub get_interfaces
} }
@ifaces = $res->getElementsByTagName('physical-interface'); @ifaces = $res->getElementsByTagName('physical-interface');
}
if ($plugin->{'conf'}->{'caching'}) {
$cache->{'interfaces'} = \@ifaces;
}
@targets = map { s/\*/\.\*/g; s/\?/\./g; $_; } @targets; @targets = map { s/\*/\.\*/g; s/\?/\./g; $_; } @targets;