Doc/Monitoring/NagiosProbes: check_service.NIKHEF

File check_service.NIKHEF, 2.3 KB (added by /C=GR/O=HellasGrid/OU=auth.gr/CN=Christos Triantafyllidis, 13 years ago)

check_service by NIKHEF

Line 
1#!/usr/bin/perl -w
2
3use strict;
4use Getopt::Long;
5
6my $verbose=0;
7my $help;
8my $hostname;
9my $services;
10my $initdir = '/etc/init.d';
11my $debug;
12
13my $TIMEOUT = 10;
14
15GetOptions("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
22my %ERRORS=(OK=>0,
23            WARNING=>1,
24            CRITICAL=>2,
25            UNKNOWN=>3,
26            DEPENDENT=>4);
27
28if ( ! 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
39alarm($TIMEOUT);
40
41my ($result, $msg) = &check_services($services, $initdir);
42
43alarm(0);
44
45print "$msg\n";
46exit($result);
47
48
49
50sub 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