1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 02:51:03 +00:00

Merge pull request #918 from sumpfralle/jenkins_multilevel_jobs2

Jenkins multilevel jobs
This commit is contained in:
Lars Kruse 2019-07-22 00:00:32 +02:00 committed by GitHub
commit f5d393ce51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,7 +13,7 @@ This plugin displays the following charts:
2) Number of Jobs in the Build Queue
3) Number of Builds, currently running
You can set the modes with naming the softlink:
You can set the modes with naming the symlink:
1) jenkins_results
2) jenkins_queue
@ -21,13 +21,14 @@ You can set the modes with naming the softlink:
=head1 CONFIGURATION
This plugin is configurable environment variables.
This plugin is configurable via environment variables.
env.url Jenkins Host
env.port Jenkins Port
env.context Jenkins Context path
env.user User for the API Tokent
env.apiToken Jenkins API Token (see https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients)
env.jobDepth How far into job "folders" should the plugin check for jobs
Example:
@ -62,6 +63,7 @@ my $port = ($ENV{'port'} || '4040');
my $user = ($ENV{'user'} || '');
my $apiToken = ($ENV{'apiToken'} || '');
my $context = ($ENV{'context'} || '');
my $jobDepth = ($ENV{'jobDepth'} || 1);
my $wgetBin = "/usr/bin/wget";
my $type = basename($0);
@ -105,9 +107,7 @@ if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
print "build_stable.label stable\n";
print "build_stable.type GAUGE\n";
print "build_stable.colour 294D99\n";
exit;
}
if( $type eq "queue" ) {
} elsif( $type eq "queue" ) {
print "graph_args --base 1000 -l 0\n";
print "graph_title Jenkins Queue Length\n";
print "graph_vlabel Number of Jobs in Queue\n";
@ -115,9 +115,7 @@ if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_info The Graph shows the Number of Jobs in the Build Queue\n";
print "build_count.label Jobs in Queue\n";
print "build_count.type GAUGE\n";
exit;
}
if( $type eq "running" ) {
} elsif( $type eq "running" ) {
print "graph_args --base 1000 -l 0\n";
print "graph_title Jenkins Builds Running\n";
print "graph_vlabel Builds currently running\n";
@ -125,81 +123,69 @@ if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
print "graph_info The Graph shows the Number of Builds, currently running\n";
print "build_running.label running Builds\n";
print "build_running.type GAUGE\n";
exit;
} else {
warn "Unknown mode requested: $type\n";
}
} else {
my $cmd = "$wgetBin $auth -qO- $url:$port$context";
my $tree = 'jobs[name,color]';
for (2..$jobDepth) {
$tree = "jobs[name,color,$tree]";
}
if( $type eq "results" ) {
my $counts = get_results('');
my $result = `$cmd'/api/json?depth=$jobDepth&tree=$tree'`;
my $parsed = decode_json($result);
my $counts = parse_results($parsed->{'jobs'});
foreach my $status (keys %{$counts}) {
print "build_$status.value $counts->{$status}\n";
}
exit;
}
if( $type eq "running" ) {
my $running_count = get_running('');
print "build_running.value ", $running_count, "\n";
exit;
}
if( $type eq "queue" ) {
} elsif( $type eq "running" ) {
my $result = `$cmd'/api/json?depth=$jobDepth&tree=$tree'`;
my $parsed = decode_json($result);
my $count = parse_running_builds($parsed->{'jobs'});
print "build_running.value ", $count, "\n";
} elsif( $type eq "queue" ) {
my $result = `$cmd/queue/api/json`;
my $parsed = decode_json($result);
print "build_count.value ", scalar( @{$parsed->{'items'}} ), "\n";
exit;
} else {
warn "Unknown mode requested: $type\n";
}
}
{
my %counts;
sub get_results{
my $query_context = shift;
die "get_results requires an argument" unless defined $query_context;
unless (%counts) {
# initialise
%counts = ('stable' => 0, 'unstable'=>0, 'failing'=>0, 'disabled'=>0);
sub parse_running_builds {
my $builds = shift;
my $count = 0;
foreach my $cur (@{$builds}) {
if( defined($cur->{'jobs'}) ) {
$count += parse_running_builds($cur->{'jobs'});
} elsif( defined ($cur->{'color'}) and $cur->{'color'} =~ /anime$/ ) {
$count += 1;
}
my $cmd = "$wgetBin $auth -qO- $url:$port$context$query_context/api/json";
my $result = `$cmd`;
my $parsed = decode_json($result);
foreach my $cur(@{$parsed->{'jobs'}}) {
if (exists $cur->{'color'} && defined $states{$cur->{'color'}}) {
}
return $count;
}
sub parse_results {
my $builds = shift;
my %counts = ('stable' => 0, 'unstable' => 0, 'failing' => 0, 'disabled' => 0);
foreach my $cur(@{$builds}) {
if( defined($cur->{'jobs'}) ) {
my $new_counts = parse_results($cur->{'jobs'});
foreach my $new_count_key (keys %{$new_counts}) {
$counts{$new_count_key} += $new_counts->{$new_count_key};
}
} elsif (defined($cur->{'color'})) {
if (defined($states{$cur->{'color'}})) {
$counts{$states{$cur->{'color'}}} += 1;
} elsif ($cur->{'_class'} eq 'com.cloudbees.hudson.plugins.folder.Folder'){
my $uri = URI->new($cur->{'url'});
my $folder = $uri->path;
get_results($folder);
} else {
warn "Ignoring unknown color " . $cur->{'color'} . "\n"
}
}
}
return \%counts;
}
}
{
my $running_count;
sub get_running{
my $query_context = shift;
die "get_results requires an argument" unless defined $query_context;
$running_count //= 0;
my $cmd = "$wgetBin $auth -qO- $url:$port$context$query_context/api/json";
my $result = `$cmd`;
my $parsed = decode_json($result);
foreach my $cur(@{$parsed->{'jobs'}}) {
if( exists $cur->{'color'} && $cur->{'color'} =~ /anime$/ ) {
$running_count += 1;
} elsif ($cur->{'_class'} eq 'com.cloudbees.hudson.plugins.folder.Folder'){
my $uri = URI->new($cur->{'url'});
my $folder = $uri->path;
get_running($folder);
}
}
return $running_count;
}
}