-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_upstart_status.pl
executable file
·157 lines (138 loc) · 3.24 KB
/
check_upstart_status.pl
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/perl -w
#
# check_upstart_status.pl
# by Jeremy Goldstein
#
# This nagios check checks the status of upstart jobs
#
# Copyright (c) 2012 Jeremy Goldstein
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# 0 = ok
# 1 = warn
# 2 = crit
# 3 = unknown
#configurable options:
my $debug = 0;
# No changes below, unless you want to change exit codes
use lib "/usr/lib/nagios/plugins";
use Getopt::Std;
use strict;
use warnings;
use utils qw( %ERRORS);
%ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3);
my $exit_value = $ERRORS{'UNKNOWN'} ;
my %options=();
getopts("j:", \%options);
if (scalar(keys(%options))<1 )
{
show_usage();
}
my $jobName = $options{'j'};
my $exit_value_human="";
my $retval;
my $goal;
my $state;
#Run command and get result
my $cmd = "status $jobName";
$retval = `$cmd`;
#Get return code
my $retnum = $?>>8;
#Check return code to see if job was found
if ($retnum > 0)
{
$exit_value = $ERRORS{'UNKNOWN'};
$retval = 'Error - Job name may not have been found';
}
else
{
#Remove trailing \n and replace with trailing comma as quick hack
$retval =~ s/\n/,/;
if ($retval =~ m/$jobName (.*)\/.*/)
{
$goal = $1;
}
if ($retval =~ m/.*\/(.*),/)
{
$state = $1;
}
if ($debug)
{
print "Command run: $cmd\n";
print "Result: $retval\n";
}
if ($goal eq "stop")
{
$exit_value = $ERRORS{'CRITICAL'};
}
elsif ($state eq "pre-stop")
{
$exit_value = $ERRORS{'WARNING'};
}
elsif ($state eq "stopping")
{
$exit_value = $ERRORS{'WARNING'};
}
elsif ($state eq "killed")
{
$exit_value = $ERRORS{'WARNING'};
}
elsif ($state eq "post-stop")
{
$exit_value = $ERRORS{'WARNING'};
}
else
{
$exit_value = $ERRORS{'OK'};
}
}
if ($exit_value == 0)
{
$exit_value_human = "OK";
}
elsif ($exit_value == 1)
{
$exit_value_human = "WARNING";
}
elsif ($exit_value == 2)
{
$exit_value_human = "CRITICAL";
}
elsif ($exit_value == 3)
{
$exit_value_human = "UNKNOWN";
}
#Remove trailing comma hack
$retval =~ s/,$//;
print $retval . ". STATUS: $exit_value_human\n";
exit $exit_value;
sub show_usage
{
print "\n\n";
print "check_upstart_status.pl\n";
print "by Jeremy Goldstein\n";
print "\n";
print "This nagios check checks the status of upstart jobs\n";
print "and alerts when the job is not running.\n";
print "\n";
print "usage:\n";
print "./check_upstart_status.pl -j jobName\n";
print "\n";
print "-j jobName\t\t The name of the upstart job you want to check\n";
print "\n\n";
exit $exit_value;
}