Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / jenkins / jenkins_ @ 09b88141

Historique | Voir | Annoter | Télécharger (5,21 ko)

1
#!/usr/bin/env perl
2
# -*- perl -*-
3

    
4
=head1 NAME
5

    
6
jenkins_ - Plugin for displaying Jenkins Stats
7

    
8
=head1 INTERPRETATION
9

    
10
This plugin displays the following charts:
11

    
12
1) The Status of each Build
13
2) Number of Jobs in the Build Queue
14
3) Number of Builds, currently running
15

    
16
You can set the modes with naming the symlink:
17

    
18
1) jenkins_results
19
2) jenkins_queue
20
3) jenkins_running
21

    
22
=head1 CONFIGURATION
23

    
24
This plugin is configurable via environment variables.
25

    
26
env.url         Jenkins Host
27
env.port        Jenkins Port
28
env.context     Jenkins Context path
29
env.user        User for the API Tokent
30
env.apiToken    Jenkins API Token (see https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients)
31
env.jobDepth    How far into job "folders" should the plugin check for jobs
32

    
33
Example:
34

    
35
 [jenkins_*]
36
 env.url localhost
37
 env.port 4040
38
 env.context /jenkins
39
 env.user user
40
 env.apiToken aaaa0f6e48b92cbbbbddecdb72dc1dad
41

    
42

    
43
=head1 AUTHOR
44

    
45
Philipp Haussleiter <philipp@haussleiter.de> (email)
46

    
47
=head1 LICENSE
48

    
49
GPLv2
50

    
51
=cut
52

    
53
# MAIN
54
use warnings;
55
use strict;
56
use JSON;
57
use File::Basename;
58
use URI;
59

    
60
# VARS
61
my $url = ($ENV{'url'} || 'localhost');
62
my $port = ($ENV{'port'} || '4040');
63
my $user = ($ENV{'user'} || '');
64
my $apiToken = ($ENV{'apiToken'} || '');
65
my $context = ($ENV{'context'} || '');
66
my $jobDepth = ($ENV{'jobDepth'} || 1);
67
my $wgetBin = "/usr/bin/wget";
68

    
69
my $type = basename($0);
70
$type =~ s/jenkins_//;
71

    
72
my %states = (
73
	'blue' =>'stable',
74
	'blue_anime' =>'stable',
75
	'yellow'=>'unstable',
76
	'yellow_anime'=>'unstable',
77
	'red'=>'failing',
78
	'red_anime'=>'failing',
79
	'disabled'=>'disabled',
80
	'notbuilt' => 'disabled',
81
	'notbuilt_anime' =>'disabled',
82
	'aborted'=>'failing',
83
	'aborted_anime'=>'failing'
84
);
85
my $auth = ( $user ne "" and $apiToken ne "" ? " --auth-no-challenge --user=$user --password=$apiToken" : "" );
86

    
87
if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
88
	if( $type eq "results" ) {
89
		print "graph_args --base 1000 -l 0\n";
90
		print "graph_title Jenkins Build Results\n";
91
		print "graph_vlabel Build Results\n";
92
		print "graph_category devel\n";
93
		print "graph_info The Graph shows the Status of each Build\n";
94
		print "build_disabled.draw AREA\n";
95
		print "build_disabled.label disabled\n";
96
		print "build_disabled.type GAUGE\n";
97
		print "build_disabled.colour 8A8A8A\n";
98
		print "build_failing.draw STACK\n";
99
		print "build_failing.label failing\n";
100
		print "build_failing.type GAUGE\n";
101
		print "build_failing.colour E61217\n";
102
		print "build_unstable.draw STACK\n";
103
		print "build_unstable.label unstable\n";
104
		print "build_unstable.type GAUGE\n";
105
		print "build_unstable.colour F3E438\n";
106
		print "build_stable.draw STACK\n";
107
		print "build_stable.label stable\n";
108
		print "build_stable.type GAUGE\n";
109
		print "build_stable.colour 294D99\n";
110
	} elsif( $type eq "queue" ) {
111
		print "graph_args --base 1000 -l 0\n";
112
		print "graph_title Jenkins Queue Length\n";
113
		print "graph_vlabel Number of Jobs in Queue\n";
114
		print "graph_category devel\n";
115
		print "graph_info The Graph shows the Number of Jobs in the Build Queue\n";
116
		print "build_count.label Jobs in Queue\n";
117
		print "build_count.type GAUGE\n";
118
	} elsif( $type eq "running" ) {
119
		print "graph_args --base 1000 -l 0\n";
120
		print "graph_title Jenkins Builds Running\n";
121
		print "graph_vlabel Builds currently running\n";
122
		print "graph_category devel\n";
123
		print "graph_info The Graph shows the Number of Builds, currently running\n";
124
		print "build_running.label running Builds\n";
125
		print "build_running.type GAUGE\n";
126
	} else {
127
		warn "Unknown mode requested: $type\n";
128
	}
129
} else {
130
	my $cmd = "$wgetBin $auth -qO- $url:$port$context";
131

    
132
	my $tree = 'jobs[name,color]';
133
	for (2..$jobDepth) {
134
		$tree = "jobs[name,color,$tree]";
135
	}
136

    
137
	if( $type eq "results" ) {
138
		my $result = `$cmd'/api/json?depth=$jobDepth&tree=$tree'`;
139
		my $parsed = decode_json($result);
140
		my $counts = parse_results($parsed->{'jobs'});
141

    
142
		foreach my $status (keys %{$counts}) {
143
			print "build_$status.value $counts->{$status}\n";
144
		}
145
	} elsif( $type eq "running" ) {
146
		my $result = `$cmd'/api/json?depth=$jobDepth&tree=$tree'`;
147
		my $parsed = decode_json($result);
148
		my $count = parse_running_builds($parsed->{'jobs'});
149
		print "build_running.value ", $count, "\n";
150
	} elsif( $type eq "queue" ) {
151
		my $result = `$cmd/queue/api/json`;
152
		my $parsed = decode_json($result);
153
		print "build_count.value ", scalar( @{$parsed->{'items'}} ), "\n";
154
	} else {
155
		warn "Unknown mode requested: $type\n";
156
	}
157
}
158

    
159
sub parse_running_builds {
160
	my $builds = shift;
161
	my $count = 0;
162
	foreach my $cur (@{$builds}) {
163
		if( defined($cur->{'jobs'}) ) {
164
			$count += parse_running_builds($cur->{'jobs'});
165
		} elsif( defined ($cur->{'color'}) and $cur->{'color'} =~ /anime$/ ) {
166
			$count += 1;
167
		}
168
	}
169
	return $count;
170
}
171

    
172
sub parse_results {
173
	my $builds = shift;
174
	my %counts = ('stable' => 0, 'unstable' => 0, 'failing' => 0, 'disabled' => 0);
175

    
176
	foreach my $cur(@{$builds}) {
177
		if( defined($cur->{'jobs'}) ) {
178
			my $new_counts = parse_results($cur->{'jobs'});
179
			foreach my $new_count_key (keys %{$new_counts}) {
180
				$counts{$new_count_key} += $new_counts->{$new_count_key};
181
			}
182
		} elsif (defined($cur->{'color'})) {
183
			if (defined($states{$cur->{'color'}})) {
184
				$counts{$states{$cur->{'color'}}} += 1;
185
			} else {
186
				warn "Ignoring unknown color " . $cur->{'color'} . "\n"
187
			}
188
		}
189
	}
190
	return \%counts;
191
}