summaryrefslogtreecommitdiffstats
path: root/web/attachments/163636-check_ntp.pl.patch
blob: 3ce6b2ab34b4405244de87462de8a02cf7858c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
--- check_ntp.pl.dist	2006-01-16 17:56:25.835790000 +0100
+++ check_ntp.pl	2006-01-16 20:17:39.345787000 +0100
@@ -56,6 +56,12 @@
 # changed ntpdc to ntpq - jitter/dispersion is in milliseconds
 #
 # Patch for for regex for stratum1 refid.
+#
+# Modified by Joachim Schrod <jschrod@acm.org>, 16 Jan 06:
+# Support multiple peers, don't overwrite jitter value if one was found.
+# ntpdate and ntpq may exit with non-zero status, then close() returns false.
+# Support FQDN hostnames with one-letter components.
+# Support unknown hosts.
 
 require 5.004;
 use POSIX;
@@ -112,7 +118,7 @@
 }
 
 $opt_H = shift unless ($opt_H);
-my $host = $1 if ($opt_H && $opt_H =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0-9]+(\.[a-zA-Z][-a-zA-Z0-9]+)*)$/);
+my $host = $1 if ($opt_H && $opt_H =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0-9]*(\.[a-zA-Z][-a-zA-Z0-9]*)*)$/);
 unless ($host) {
 	print "No target host specified\n";
 	print_usage();
@@ -242,11 +248,15 @@
 		}
 	}
 
+	if (/can\'t find host/) {
+		$ntpdate_error = $ERRORS{"CRITICAL"};
+		$msg = "Host not found - ";
+	}
+
 }
 $out =~ s/\n//g;
-close (NTPDATE) || 
-    die $! ? "$out - Error closing $ntpdate pipe: $!"
-           : "$out - Exit status: $? from $ntpdate\n";
+close (NTPDATE)  or
+    $!  and  die "$out - Error closing $ntpdate pipe: $!\n";
 
 # declare an error if we also get a non-zero return code from ntpdate
 # unless already set to critical
@@ -314,13 +324,11 @@
 				}
 			} else {
 				print "No match!\n" if $verbose;
-				$jitter = '(not parsed)';
 			}
 			
 		}
-		close NTPQ ||
-            die $! ? "Error closing $ntpq pipe: $!"
-                   : "Exit status: $? from $ntpq\n";
+		close NTPQ  or
+	            $! and die "Error closing $ntpq pipe: $!\n";
 
 		# if we did not match sys.peer or pps.peer but matched # candidates only
 		# generate a warning 
@@ -428,7 +436,14 @@
 foreach my $key (keys %ERRORS) {
 	if ($state==$ERRORS{$key}) {
 #		print ("NTP $key: $answer");
-		print ("NTP $key: $answer|offset=$offset, jitter=" . $jitter/1000 .	",peer_stratum=$stratum\n");
+		defined($offset) or $offset = '-';
+		defined($stratum) or $stratum = '-';
+		if (defined($jitter)) {
+			$jitter /= 1000;
+		} else {
+			$jitter = '-';
+		}
+		print ("NTP $key: $answer|offset=$offset, jitter=$jitter, peer_stratum=$stratum\n");
 		last;
 	}
 }