فهرست منبع

change check_file_age to use long plugin output; make sure check_file_age uses the 'worst' result instead of the most recent one, make sure check_file_age doesn't exit on the first file-not-found

madlohe 6 سال پیش
والد
کامیت
a40d702ab3
1فایلهای تغییر یافته به همراه53 افزوده شده و 11 حذف شده
  1. 53 11
      plugins-scripts/check_file_age.pl

+ 53 - 11
plugins-scripts/check_file_age.pl

@@ -35,7 +35,7 @@ sub print_help ();
 sub print_usage ();
 
 my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $opt_i);
-my ($result, $message, $age, $size, $st, $perfdata, $output, @filelist, $filename);
+my ($result, $message, $age, $size, $st, $perfdata, $output, @filelist, $filename, $counter, $summary, $high_water_mark, $this_level, $this_result);
 
 $PROGNAME="check_file_age";
 
@@ -83,18 +83,38 @@ $opt_f = '"' . $opt_f . '"' if $opt_f =~ / /;
 $perfdata = "";
 $output = "";
 @filelist = glob($opt_f);
+$counter = 0;
+$high_water_mark = 0;
 
+$result = "OK";
 foreach $filename (@filelist) {
 	unless (-e $filename) {
 		if ($opt_i) {
-			$result = 'OK';
-			print "FILE_AGE $result: $filename doesn't exist, but ignore-missing was set\n";
-			exit $ERRORS{$result};
+			if ($output) {
+				$output = $output . "\n";
+			}
+			$output = $output . "FILE_AGE OK: $filename doesn't exist, but ignore-missing was set\n";
+			$this_result = "OK";
+			$this_level = 0;
 
 		} else {
-			print "FILE_AGE CRITICAL: File not found - $filename\n";
-			exit $ERRORS{'CRITICAL'};
+			if ($output) {
+				$output = $output . "\n";
+			}
+			$output = $output . "FILE_AGE CRITICAL: File not found - $filename\n";
+			$this_result = "CRITICAL";
+			$this_level = 2;
 		}
+
+		if ($high_water_mark < $this_level) {
+			$high_water_mark = $this_level;
+			$counter = 1;
+			$result = $this_result;
+		}
+		elsif($high_water_mark = $this_level) {
+			$counter = $counter + 1;
+		}
+		next;
 	}
 
 	$st = File::stat::stat($filename);
@@ -102,20 +122,42 @@ foreach $filename (@filelist) {
 	$size = $st->size;
 	$perfdata = $perfdata . "age=${age}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0 ";
 
-	$result = 'OK';
+	$this_result = 'OK';
+	$this_level = 0;
 
 	if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {
-		$result = 'CRITICAL';
+		$this_result = 'CRITICAL';
+		$this_level = 2;
 	}
 	elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {
-		$result = 'WARNING';
+		$this_result = 'WARNING';
+		$this_level = 1;
+	}
+
+	if ($high_water_mark < $this_level) {
+		$high_water_mark = $this_level;
+		$counter = 1;
+		$result = $this_result;
+	}
+	elsif ($high_water_mark == $this_level) {
+		$counter = $counter + 1;
 	}
 
-	$output = $output . "FILE_AGE $result: $filename is $age seconds old and $size bytes ";
+	if ($output) {
+		$output = $output . "\n";
+	}
+	$output = $output . "FILE_AGE $this_result: $filename is $age seconds old and $size bytes ";
+}
 
+$summary = "$result: $counter files are $result";
+
+if (scalar @filelist == 1) {
+	print "$output | $perfdata \n";
+}
+else {
+	print "$summary \n$output | $perfdata \n";
 }
 
-print "$output | $perfdata\n";
 
 exit $ERRORS{$result};