forked from jonlives/nagios-jenkins-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_jenkins_nodes.pl
154 lines (118 loc) · 4.23 KB
/
check_jenkins_nodes.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
#!/usr/bin/perl
# Author: Dave Stern ([email protected])
use lib '/usr/lib/nagios/plugins';
use utils qw(%ERRORS $TIMEOUT);
use strict;
use warnings;
use JSON;
use LWP::UserAgent;
use Getopt::Std;
sub main {
my %options=();
getopts("c:hs:p:u:vw:", \%options);
my $ciHost = $options{'s'};
my $username = $options{'u'};
my $password = $options{'p'};
my $warning = $options{'w'};
my $critical = $options{'c'};
my $retStr;
my $exitCode;
if($options{'h'})
{
$retStr = usage();
}
elsif ($ciHost && ! ($warning || $critical))
{
# Fail if no CI host specified or no alert threshold values
$retStr = usage();
}
else
{
# Continue if input checks pass
my $nodeStatusUrl = $ciHost . "/computer/api/json";
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => $nodeStatusUrl);
if ($username)
{
$req->authorization_basic($username, $password);
}
my $res = $ua->request($req);
if ($res->is_success)
{
my $json = new JSON;
my $obj = $json->decode($res->content);
my @offline;
my $totalNodes = 0;
for my $computer (@{$obj->{'computer'} or []})
{
++$totalNodes;
if($computer->{'offline'} eq 'true')
{
push(@offline, {
'displayName' => $computer->{'displayName'},
'offlineCauseReason' => $computer->{'offlineCauseReason'}
});
}
}
my $offlineNodes = scalar(@offline);
my $offlineNodeNames = join(", ", map($_->{'displayName'}, @offline));
my $criticalThreshold = $critical;
# Convert critical threshold to a number if it's a %
if ($critical =~ s/\%$//)
{
$criticalThreshold = $critical/100 * $totalNodes;
}
my $warningThreshold = $warning;
# Convert warning threshold to a number if it's a %
if ($warning =~ s/\%$//)
{
$warningThreshold = $warning/100 * $totalNodes;
}
if ($offlineNodes >= $criticalThreshold)
{
$retStr = "$offlineNodes/$totalNodes nodes offline >= $options{'c'} critical ($offlineNodeNames).\n";
$exitCode = $ERRORS{'CRITICAL'};
}
# Continue to check warning threshold if not critical
if (! $exitCode && $offlineNodes >= $warningThreshold)
{
$retStr = "$offlineNodes/$totalNodes nodes offline >= $options{'w'} warning ($offlineNodeNames).\n";
$exitCode = $ERRORS{'WARNING'};
}
# Set success output if not warning
if (! $exitCode)
{
$retStr = qq|$offlineNodes/$totalNodes nodes offline < $options{'w'} warning and < $options{'c'} critical ($offlineNodeNames).\n|;
$exitCode = $ERRORS{'OK'};
}
}
else
{
# Failure connecting to CI server
$retStr = "Failed retrieving node status via API (API status line: " . $res->status_line . ")";
}
}
if (! defined $exitCode)
{
$exitCode = $ERRORS{'UNKNOWN'}
}
print $retStr;
exit $exitCode;
}
sub usage
{
my $usage = qq|\nUsage: check_jenkins_nodes.pl -s [jenkins server hostname & path] -w [integer or %] -c [integer or %] [-h this help message] [-u username] [-p password] [-v]
Required Arguments:
-s <server hostname> : jenkins CI server hostname
-c <threshold> : integer or percentage (ex: 2 or 50%)
CRITICAL if <threshold> nodes or greater are offline
-w <threshold> : integer or percentage (ex: 2 or 50%)
WARNING if <threshold> nodes or greater are offline
Optional arguments
-h This help message
-p <password> : password to the jenkins CI server
-u <username> : username to the jenkins CI server
-v verbose output\n\n|;
return $usage;
}
main();