1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | use strict; |
---|
4 | use Getopt::Long; |
---|
5 | |
---|
6 | my $verbose=0; |
---|
7 | my $help; |
---|
8 | my $hostname; |
---|
9 | my $metric; |
---|
10 | my $state; |
---|
11 | my $debug; |
---|
12 | |
---|
13 | my $TIMEOUT = 10; |
---|
14 | |
---|
15 | GetOptions("help" => \$help, |
---|
16 | "host:s" => \$hostname, |
---|
17 | "metric:s" => \$metric, |
---|
18 | "state:s" => \$state, |
---|
19 | "debug" => \$debug, |
---|
20 | "verbose" => \$verbose ); |
---|
21 | |
---|
22 | my %ERRORS=(OK=>0, |
---|
23 | WARNING=>1, |
---|
24 | CRITICAL=>2, |
---|
25 | UNKNOWN=>3, |
---|
26 | DEPENDENT=>4); |
---|
27 | |
---|
28 | my $ZGREP = "/usr/bin/zgrep"; |
---|
29 | my $SORT = "/bin/sort"; |
---|
30 | my $TAIL = "/usr/bin/tail"; |
---|
31 | |
---|
32 | my $result = $ERRORS{"UNKNOWN"}; |
---|
33 | |
---|
34 | # Just in case of problems, let's not hang Nagios |
---|
35 | $SIG{'ALRM'} = sub { |
---|
36 | print ("ERROR: No response from $hostname (alarm timeout)\n"); |
---|
37 | exit $ERRORS{"UNKNOWN"}; |
---|
38 | }; |
---|
39 | |
---|
40 | alarm($TIMEOUT); |
---|
41 | |
---|
42 | ############################################################################## |
---|
43 | # output of ncm-ncd |
---|
44 | # |
---|
45 | my $timestamp = 'NOT FOUND'; |
---|
46 | my $errors = 0; |
---|
47 | my $warnings = 0; |
---|
48 | |
---|
49 | my $message; |
---|
50 | |
---|
51 | my $cmd = "$ZGREP -h 'warnings executing configure' /var/log/ncm/ncd.log* | $SORT | $TAIL -1"; |
---|
52 | |
---|
53 | $_ = `$cmd`; |
---|
54 | |
---|
55 | if (/^(.+)\s+\[\w+\]\s+(\d+) errors, (\d+) warnings executing configure$/) { |
---|
56 | $timestamp = $1; |
---|
57 | $errors = $2; |
---|
58 | $warnings = $3; |
---|
59 | |
---|
60 | if ( $errors > 0 ) { |
---|
61 | $message = "ERROR: $errors error(s)"; |
---|
62 | $message .= ", $warnings warning(s)" if ( $warnings > 0 ); |
---|
63 | $message .= " [$timestamp]"; |
---|
64 | $result = $ERRORS{"CRITICAL"}; |
---|
65 | } |
---|
66 | elsif ( $warnings > 0 ) { |
---|
67 | $message = "WARNING: $warnings warning(s) [$timestamp]"; |
---|
68 | $result = $ERRORS{"WARNING"}; |
---|
69 | } |
---|
70 | else { |
---|
71 | $message = "OK: [$timestamp]"; |
---|
72 | $result = $ERRORS{"OK"}; |
---|
73 | } |
---|
74 | } |
---|
75 | else { |
---|
76 | $message = "UNKNOWN: could not determine result of last ncm-ncd run"; |
---|
77 | $result = $ERRORS{"UNKNOWN"}; |
---|
78 | } |
---|
79 | |
---|
80 | alarm(0); |
---|
81 | |
---|
82 | print "$message\n"; |
---|
83 | exit($result); |
---|