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 $services; |
---|
10 | my $initdir = '/etc/init.d'; |
---|
11 | my $debug; |
---|
12 | |
---|
13 | my $TIMEOUT = 10; |
---|
14 | |
---|
15 | GetOptions("help" => \$help, |
---|
16 | "host:s" => \$hostname, |
---|
17 | "services|service|s:s" => \$services, |
---|
18 | "initd|i:s" => \$initdir, |
---|
19 | "debug" => \$debug, |
---|
20 | "verbose|v+" => \$verbose ); |
---|
21 | |
---|
22 | my %ERRORS=(OK=>0, |
---|
23 | WARNING=>1, |
---|
24 | CRITICAL=>2, |
---|
25 | UNKNOWN=>3, |
---|
26 | DEPENDENT=>4); |
---|
27 | |
---|
28 | if ( ! defined $services ) { |
---|
29 | print "UNKNOWN: missing argument service name (--service)\n"; |
---|
30 | exit $ERRORS{"UNKNOWN"}; |
---|
31 | } |
---|
32 | |
---|
33 | # Just in case of problems, let's not hang Nagios |
---|
34 | $SIG{'ALRM'} = sub { |
---|
35 | print ("ERROR: No response from $hostname (alarm timeout)\n"); |
---|
36 | exit $ERRORS{"UNKNOWN"}; |
---|
37 | }; |
---|
38 | |
---|
39 | alarm($TIMEOUT); |
---|
40 | |
---|
41 | my ($result, $msg) = &check_services($services, $initdir); |
---|
42 | |
---|
43 | alarm(0); |
---|
44 | |
---|
45 | print "$msg\n"; |
---|
46 | exit($result); |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | sub check_services { |
---|
51 | my %status; |
---|
52 | |
---|
53 | foreach my $servicename ( split(/,/, $_[0]) ) { |
---|
54 | ($verbose > 1) and print "Checking service $servicename\n"; |
---|
55 | |
---|
56 | my $res; |
---|
57 | if ( -f "$initdir/$servicename" && |
---|
58 | -x "$initdir/$servicename" ) { |
---|
59 | my $cmd = "$initdir/$servicename status > /dev/null 2>&1"; |
---|
60 | system($cmd); |
---|
61 | my $status = $? >> 8; |
---|
62 | |
---|
63 | if ( $status == 0 ) { |
---|
64 | $res = "OK"; |
---|
65 | } |
---|
66 | elsif ( $status == 3 ) { |
---|
67 | $res = "WARNING"; |
---|
68 | } |
---|
69 | elsif ( $status == 1 || $status == 2 ) { |
---|
70 | $res = "CRITICAL"; |
---|
71 | } |
---|
72 | else { |
---|
73 | $res = "UNKNOWN"; |
---|
74 | } |
---|
75 | } |
---|
76 | else { |
---|
77 | # script is not found or not executable -> status UNKNOWN |
---|
78 | $res = 'UNKNOWN'; |
---|
79 | } |
---|
80 | $status{$res} .= "$servicename "; |
---|
81 | $debug and print STDERR "$servicename: $res\n"; |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | my $retval = $ERRORS{'UNKNOWN'}; |
---|
86 | my $msg = ''; |
---|
87 | foreach my $res ('OK', 'UNKNOWN', 'WARNING', 'CRITICAL') { |
---|
88 | if ( $status{$res} ) { |
---|
89 | $retval = $ERRORS{$res}; |
---|
90 | chomp $status{$res}; |
---|
91 | $status{$res} =~ tr![ ]!,!; |
---|
92 | $status{$res} =~ s!,$!!g; |
---|
93 | $msg = "$res: $status{$res}; " . $msg; |
---|
94 | } |
---|
95 | } |
---|
96 | $msg ||= 'No services found'; |
---|
97 | |
---|
98 | return ($retval, $msg); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|