Browse Source

Nagios::Plugin::JUNOS, check_junos: Added basic support for excluding targets.

This may be done by prefixing the target name with '~' rather than '+'. For
example, this may be combined with @with_description when checking interfaces
to exclude some specific interface(s): interfaces,@with_description~ge-0/0/0.
Sebastian Harl 12 years ago
parent
commit
864a0e6dcd
2 changed files with 52 additions and 21 deletions
  1. 21 17
      check_junos.pl
  2. 31 4
      perl/lib/Nagios/Plugin/JUNOS.pm

+ 21 - 17
check_junos.pl

@@ -252,19 +252,20 @@ sub check_interfaces
 {
 	my $plugin  = shift;
 	my $junos   = shift;
-	my @targets = @_;
+	my $targets = shift || [];
+	my $exclude = shift || [];
 
 	my $opts = {
 		with_description => 0,
 	};
 
-	if (grep { m/^\@with_description$/; } @targets) {
+	if (grep { m/^\@with_description$/; } @$targets) {
 		$opts->{'with_description'} = 1;
 
-		@targets = grep { ! m/^\@with_description$/; } @targets;
+		@$targets = grep { ! m/^\@with_description$/; } @$targets;
 	}
 
-	my @interfaces = get_interfaces($plugin, $opts, @targets);;
+	my @interfaces = get_interfaces($plugin, $opts, @$targets);;
 
 	my $down_count = 0;
 	my @down_ifaces = ();
@@ -277,7 +278,7 @@ sub check_interfaces
 	foreach my $iface (@interfaces) {
 		my $name = $plugin->get_query_object_value($iface, 'name');
 		my $desc = $plugin->get_query_object_value($iface, 'description');
-		my $status = check_interface($plugin, $iface, $opts, @targets);
+		my $status = check_interface($plugin, $iface, $opts, @$targets);
 
 		my $tmp;
 
@@ -339,15 +340,15 @@ sub check_interfaces
 	}
 
 	if ((! $down_count) && (! $phys_down_count)) {
-		if ((! scalar(@targets)) || $opts->{'with_description'}) {
+		if ((! scalar(@$targets)) || $opts->{'with_description'}) {
 			$plugin->add_message(OK, "all interfaces up"
 				. ($have_lag_ifaces
 					? " (including all LAG member interfaces)" : ""));
 		}
 		else {
 			$plugin->add_message(OK, "interface"
-				. (scalar(@targets) == 1 ? " " : "s ")
-				. join(", ", @targets) . " up"
+				. (scalar(@$targets) == 1 ? " " : "s ")
+				. join(", ", @$targets) . " up"
 				. ($have_lag_ifaces
 					? " (including all LAG member interfaces)" : ""));
 		}
@@ -358,7 +359,8 @@ sub check_interface_forwarding
 {
 	my $plugin  = shift;
 	my $junos   = shift;
-	my @targets = @_;
+	my $targets = shift || [];
+	my $exclude = shift || [];
 
 	my $res = $plugin->send_query('show ethernet-switching interfaces brief');
 
@@ -369,7 +371,7 @@ sub check_interface_forwarding
 		'Storm control in effect'  => 1,
 	);
 
-	my %targets = map { my @t = split(':', $_); $t[0] => $t[1]; } @targets;
+	my %targets = map { my @t = split(':', $_); $t[0] => $t[1]; } @$targets;
 
 	my @failed = ();
 
@@ -377,7 +379,7 @@ sub check_interface_forwarding
 		my $name = $plugin->get_query_object_value($iface, 'interface-name');
 		my $failed_status = undef;
 
-		if (scalar(@targets) && (! exists($targets{$name}))) {
+		if (scalar(@$targets) && (! exists($targets{$name}))) {
 			next;
 		}
 
@@ -418,7 +420,8 @@ sub check_chassis_environment
 {
 	my $plugin  = shift;
 	my $junos   = shift;
-	my @targets = @_;
+	my $targets = shift || [];
+	my $exclude = shift || [];
 
 	my $res = $plugin->send_query('get_environment_information');
 
@@ -437,7 +440,7 @@ sub check_chassis_environment
 	foreach my $item ($plugin->get_query_object($res, 'environment-item')) {
 		my $name = $plugin->get_query_object_value($item, 'name');
 
-		if (scalar(@targets) && (! grep { m/^$name$/ } @targets)) {
+		if (scalar(@$targets) && (! grep { m/^$name$/ } @$targets)) {
 			next;
 		}
 
@@ -448,7 +451,7 @@ sub check_chassis_environment
 		my $status = $plugin->get_query_object_value($item, 'status');
 
 		if ($status eq "Absent") {
-			if (! scalar(@targets)) {
+			if (! scalar(@$targets)) {
 				next;
 			}
 			# else: check this component
@@ -515,7 +518,8 @@ sub check_system_storage
 {
 	my $plugin  = shift;
 	my $junos   = shift;
-	my @targets = @_;
+	my $targets = shift || [];
+	my $exclude = shift || [];
 
 	my $res = $plugin->send_query('get_system_storage');
 
@@ -530,8 +534,8 @@ sub check_system_storage
 			my $name = $plugin->get_query_object_value($fs, 'filesystem-name');
 			my $mnt_pt = $plugin->get_query_object_value($fs, 'mounted-on');
 
-			if (scalar(@targets) && (! grep { m/^$name$/ } @targets)
-					&& (! grep { m/^$mnt_pt$/ } @targets)) {
+			if (scalar(@$targets) && (! grep { m/^$name$/ } @$targets)
+					&& (! grep { m/^$mnt_pt$/ } @$targets)) {
 				next;
 			}
 

+ 31 - 4
perl/lib/Nagios/Plugin/JUNOS.pm

@@ -257,7 +257,28 @@ sub _add_single_check
 
 	$c{'target'} = undef;
 	if (defined($check[1])) {
-		$c{'target'} = [ split(m/\+/, $check[1]) ];
+		my @tmp = split(m/(\+|\~)/, $check[1]);
+
+		$c{'target'}  = [];
+		$c{'exclude'} = [];
+
+		for (my $i = 0; $i < scalar(@tmp); ++$i) {
+			my $t = $tmp[$i];
+
+			if ((($t ne "+") && ($t ne "~")) || ($i == $#tmp)) {
+				push @{$c{'target'}}, $t;
+				next;
+			}
+
+			++$i;
+
+			if ($t eq "+") {
+				push @{$c{'target'}}, $tmp[$i];
+			}
+			else {
+				push @{$c{'exclude'}}, $tmp[$i];
+			}
+		}
 	}
 
 	$c{'warning'}    = $check[2];
@@ -290,6 +311,7 @@ sub set_checks
 			$self->{'conf'}->{'checks'}->[0] = {
 				name     => $self->{'default_check'},
 				target   => [],
+				exclude  => [],
 				warning  => undef,
 				critical => undef,
 			};
@@ -371,10 +393,15 @@ sub run_checks
 	my $self = shift;
 
 	foreach my $check ($self->get_checks()) {
-		my @targets = ();
+		my $targets = [];
+		my $exclude = [];
 
 		if (defined $check->{'target'}) {
-			@targets = @{$check->{'target'}};
+			$targets = $check->{'target'};
+		}
+
+		if (defined $check->{'exclude'}) {
+			$exclude = $check->{'exclude'};
 		}
 
 		$self->set_thresholds(
@@ -383,7 +410,7 @@ sub run_checks
 		);
 
 		my $sub = $self->get_check_impl($check->{'name'});
-		$sub->($self, $self->{'junos'}, @targets);
+		$sub->($self, $self->{'junos'}, $targets, $exclude);
 	}
 }