forked from Ericbla/check_jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_jenkins_job_time.pl
More file actions
341 lines (290 loc) · 9.31 KB
/
check_jenkins_job_time.pl
File metadata and controls
341 lines (290 loc) · 9.31 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/perl
#
# Check Jenkins job build time using the JSON API
#
# Author: Eric Blanchard
#
#
# This Nagios plugin check that running Jenkins jobs are not taking unusual time to complete.
# You can specify one critical and/or one warning threshold express in % of the expected
# building time (estimated by Jenkins with statistics on past builds).
# You can select a given job with -j <job-name> option or check all jobs defined on the master Jenkins
# specified by its url.
use strict;
use LWP::UserAgent;
use JSON;
use Getopt::Long
qw(GetOptions HelpMessage VersionMessage :config no_ignore_case bundling);
use Pod::Usage qw(pod2usage);
# Nagios return values
use constant {
OK => 0,
WARNING => 1,
CRITICAL => 2,
UNKNOWN => 3,
};
use constant API_SUFFIX => "/api/json";
our $VERSION = '1.5';
my %args;
my $ciMasterUrl;
my $jobName = '(All)';
my $warn = -1;
my $crit = -1;
my $awarn = -1;
my $acrit = -1;
my $status = UNKNOWN;
my $debug = 0;
my $timeout = 10;
# Functions prototypes
sub test_job(\%);
sub print_duration($);
sub trace(@);
# Main
GetOptions(
\%args,
'version|v' => sub { VersionMessage( { '-exitval' => UNKNOWN } ) },
'help|h' => sub { HelpMessage( { '-exitval' => UNKNOWN } ) },
'man' => sub { pod2usage( { '-verbose' => 2, '-exitval' => UNKNOWN } ) },
'debug|d' => \$debug,
'timeout|t=i' => \$timeout,
'proxy=s',
'noproxy',
'noperfdata',
'warning|w=i' => \$warn,
'critical|c=i' => \$crit,
'absoluteWarn=i' => \$awarn,
'absoluteCrit=i' => \$acrit,
'job|j=s' => \$jobName
)
or pod2usage( { '-exitval' => UNKNOWN } );
HelpMessage(
{ '-msg' => 'Missing Jenkins url parameter', '-exitval' => UNKNOWN } )
if scalar(@ARGV) != 1;
$ciMasterUrl = $ARGV[0];
$ciMasterUrl =~ s/\/$//;
my $ua = LWP::UserAgent->new();
$ua->timeout($timeout);
if ( defined( $args{proxy} ) ) {
$ua->proxy( 'http', $args{proxy} );
}
else {
if ( !defined( $args{noproxy} ) ) {
# Use HTTP_PROXY environment variable
$ua->env_proxy;
}
}
my $url =
$ciMasterUrl
. API_SUFFIX
. '?tree=jobs[name,url,buildable,lastBuild[number,building,timestamp,estimatedDuration]]';
my $req = HTTP::Request->new( GET => $url );
trace( "Get ", $url, " ...\n" );
my $res = $ua->request($req);
if ( !$res->is_success ) {
print( "UNKNOWN: can't get ", $url, " ($res->{status_line})\n" );
exit UNKNOWN;
}
my $json = new JSON;
my $content = {};
eval {
$content = $json->decode( $res->content );
1;
}
or do {
$@ =~ s/\n//m;
print( "UNKNOWN: can't parse JSON content (error $@) from master api: ",
$url, "\n" );
return UNKNOWN;
};
my $jobs = $content->{'jobs'}; # ref to array
my $jobs_count = scalar(@$jobs);
trace( "Got ", $jobs_count, " jobs\n" );
if ( $jobs_count == 0 ) {
print("OK: job: All (No Job)\n");
exit OK;
}
for ( my $i = 0 ; $i < $jobs_count ; $i++ ) {
if ( $jobName eq '(All)' || $jobs->[$i]->{'name'} eq $jobName ) {
$status = test_job( %{ $jobs->[$i] } );
}
last if ( $status != OK && !$debug );
}
if ( $status == OK ) {
print( "OK: job: ", $jobName, "\n" );
}
exit $status;
sub test_job(\%) {
my $job = shift;
if ( !defined($job) || ref($job) ne 'HASH' ) {
trace("test_job: No job\n");
}
my $job_name = $job->{'name'};
my $job_url = $job->{'url'};
trace( "Job: ", $job_name );
if ( !$job->{'buildable'} ) {
# Disabled job
trace(", disabled \n");
return OK;
}
if ( !defined( $job->{'lastBuild'} ) ) {
# No build for this job
trace(", no available build\n");
return OK;
}
my $build_number = $job->{'lastBuild'}->{'number'};
if ( !$job->{'lastBuild'}->{'building'} ) {
# This build is not currently running
trace( ", build number=", $build_number, " is not running \n" );
return OK;
}
my $stamp = $job->{'lastBuild'}->{'timestamp'};
my $current_stamp = time() * 1000; # in ms
my $duration = $current_stamp - $stamp;
my $usual_duration = 1000 * 60 * 30; # 30 minutes
if ( defined( $job->{'lastBuild'}->{'estimatedDuration'} )
&& $job->{'lastBuild'}->{'estimatedDuration'} > 0 )
{
$usual_duration = $job->{'lastBuild'}->{'estimatedDuration'};
}
trace( ", duration=", $duration, ", usual duration=",
$usual_duration, "\n" );
if ( $crit != -1
&& $duration > $usual_duration + ( $usual_duration * $crit / 100 ) )
{
print(
'CRITICAL: job: <a href="', $job_url,
'">', $job_name,
'</a>, build=', $build_number,
', duration=', print_duration($duration),
' exeeds critical threshold =(', $crit,
'%) of usual duration=', print_duration($usual_duration),
"\n"
);
return CRITICAL;
}
if ( $acrit != -1 && $duration > $acrit * 60 * 1000 ) {
print(
'CRITICAL: job: <a href="',
$job_url,
'">',
$job_name,
'</a>, build=',
$build_number,
', duration=',
print_duration($duration),
' exeeds critical absolute threshold =(',
$acrit,
'm)',
"\n"
);
return CRITICAL;
}
if ( $warn != -1
&& $duration > $usual_duration + ( $usual_duration * $warn / 100 ) )
{
print(
'WARNING: job: <a href="', $job_url,
'">', $job_name,
'</a>, build=', $build_number,
', duration=', print_duration($duration),
' exeeds warning threshold =(', $warn,
'%) of usual duration=', print_duration($usual_duration),
"\n"
);
return WARNING;
}
if ( $awarn != -1 && $duration > $awarn * 60 * 1000 ) {
print(
'WARNING: job: <a href="',
$job_url,
'">',
$job_name,
'</a>, build=',
$build_number,
', duration=',
print_duration($duration),
' exeeds warning absolute threshold =(',
$awarn,
'm)',
"\n"
);
return WARNING;
}
return OK;
}
sub print_duration($) {
my $duration = shift;
return 'NaN' if !defined($duration);
$duration /= 1000; # in sec
if ( $duration < 60 ) {
return sprintf( '%ds', $duration );
}
if ( $duration < 3600 ) {
return sprintf( '%.1fm', $duration / 60 );
}
return sprintf( '%.1fh', $duration / 3600 );
}
sub trace(@) {
if ($debug) {
print @_;
}
}
__END__
=head1 NAME
check_jenkins_job_time - A Nagios plugin that check that Jenkin job(s) has no unusual duration
=head1 SYNOPSIS
check_jenkins_job_time.pl --version
check_jenkins_job_time.pl --help
check_jenkins_job_time.pl --man
check_jenkins_job_time.pl [options] <jenkins-url>
Options:
-d --debug turns on debug traces
-t --timeout=<timeout> the timeout in seconds to wait for the
request (default 30)
--proxy=<url> the http proxy url (default from
HTTP_PROXY env)
--noproxy do not use HTTP_PROXY env
--noperfdata do not output perdata
-w --warning=<percent> the percentage of usual duration for the WARNING threshold
-c --critical=<percent> the percentage of usual duration for the CRITICAL threshold
--absoluteWarn=<minutes> the duration in minutes for the WARNING threshold
--absoluteCrit=<minutes> the duration in minutes for the CRITICAL threshold
-j --job=<job-name> the name of the job to monitor (default all jobs)
=head1 OPTIONS
=over 8
=item B<--help>
Print a brief help message and exits.
=item B<--version>
Prints the version of this tool and exits.
=item B<--man>
Prints manual and exits.
=item B<-d> B<--debug>
Turns on debug traces
=item B<-t> B<--timeout=>timeout
The timeout in seconds to wait for the request (default 30)
=item B<--proxy=>url
The http proxy url (default from HTTP_PROXY env)
=item B<--noproxy>
Do not use HTTP_PROXY env
=item B<--noperfdata>
Do not output perdata
=item B<-w> B<--warning=>percent
The percentage of usual duration for the WARNING threshold
=item B<-c> B<--critical=>percent
The percentage of usual duration for the CRITICAL threshold
=item B<--absoluteWarn=>minutes
The duration in minutes for the WARNING threshold
=item B<--absoluteCrit=>minutes
The duration in minutes for the CRITICAL threshold
=item B<-j> B<--job=>job-name
The name of the job to monitor (default all jobs)
=back
=head1 DESCRIPTION
B<check_jenkins_job_time.pl> is a Nagios plugin check that running Jenkins jobs
are not taking unusual time to complete.
You can specify one critical and/or one warning threshold expressed in % of the
expected building time (estimated by Jenkins with statistics on past builds)
or expressed in absolute minutes.
You can select a given job with -j <job-name> option or check all jobs defined
on the master Jenkins specified by its url.
=cut