Преглед на файлове

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).
Sebastian Harl преди 13 години
родител
ревизия
1cfdcd3667
променени са 1 файла, в които са добавени 29 реда и са изтрити 11 реда
  1. 29 11
      check_junos.pl

+ 29 - 11
check_junos.pl

@@ -58,7 +58,7 @@ my $plugin = Nagios::Plugin::JUNOS->new(
 	blurb     => 'Monitor Juniper™ Switches.',
 	usage     =>
 "Usage: %s [-v|--verbose] [-H <host>] [-p <port>] [-t <timeout]
-[-U <user>] [-P <password] check-tuple [...]",
+[-C] [-U <user>] [-P <password] check-tuple [...]",
 	license   =>
 "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
@@ -110,7 +110,15 @@ my %checks = (
 
 my $junos = undef;
 
+my $cache = {};
+
 $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) {
 	$plugin->add_check_impl($check, $checks{$check});
@@ -184,21 +192,31 @@ sub get_interfaces
 	my @ifaces  = ();
 	my @ret     = ();
 
-	my $cmd = 'get_interface_information';
-	my $res;
+	if (defined($cache->{'interfaces'})) {
+		@ifaces = @{$cache->{'interfaces'}};
+	}
+	else {
+		my $cmd = 'get_interface_information';
+		my $res;
 
-	my $args = { detail => 1 };
+		my $args = { detail => 1 };
 
-	if ((scalar(@targets) == 1) && (! $opts->{'with_description'})) {
-		$args->{'interface_name'} = $targets[0];
-	}
-	$res = $plugin->send_query($cmd, $args);
+		if ((scalar(@targets) == 1) && (! $opts->{'with_description'})
+				&& (! $plugin->{'conf'}->{'caching'})) {
+			$args->{'interface_name'} = $targets[0];
+		}
+		$res = $plugin->send_query($cmd, $args);
+
+		if (! ref $res) {
+			$plugin->die($res);
+		}
 
-	if (! ref $res) {
-		$plugin->die($res);
+		@ifaces = $res->getElementsByTagName('physical-interface');
 	}
 
-	@ifaces = $res->getElementsByTagName('physical-interface');
+	if ($plugin->{'conf'}->{'caching'}) {
+		$cache->{'interfaces'} = \@ifaces;
+	}
 
 	@targets = map { s/\*/\.\*/g; s/\?/\./g; $_; } @targets;