-1

Could someone help me on writting a perl script which will check health of each jenkins job on different servers. Here are the steps I want to use for plugin creation:

  1. Using jenkins REST API I want to found jenkins jobs and stored them into an array.

    http://my-host:8080/api/xml

    Test_Job1 http://jenkins-host:8080/job/Test_job1/ red Test_job2 http://jenkins-host:8080/job/Test_job2/ red Test_View http://jenkins-host:8080/

  2. For each jenkins job stored in an array I want to monitor HealthReport of each job using REST API.

    http://my-host:8080/Job_name/api/xml

    Test Result: 1 test failing out of a total of 78 tests.
    health-80plus.png 98

  3. According to the <score>98</score> of jenkins job, I want to display OK,CRITICAL,WARNING on nagios GUI.

  4. if <score>98</score> is less than 50 it should show CRITICAL on nagios GUI.

Shoe
  • 74,840
  • 36
  • 166
  • 272
MangeshBiradar
  • 3,820
  • 1
  • 23
  • 41
  • If you havent made any attempt to do it, or if your question isnt about a problem you are having.. this question is likely to be closed. – Karthik T Jan 02 '13 at 08:00
  • Thanks @KarthikT...I have tried myself using jenkins REST API.would that be the final way to do monitoring? Here is the way I found job list: `http://my-host:8080/api/xml` ` Test_Job1 http://jenkins-host:8080/job/Test_job1/ red Test_job2 http://jenkins-host:8080/job/Test_job2/ red ` – MangeshBiradar Jan 02 '13 at 08:05
  • 1
    You might want to update the question, then, with what you have tried and where you are stuck. Others may be able to help, I myself have no experience with jenkins – Karthik T Jan 02 '13 at 08:07

2 Answers2

1

This is still quite vague and broad.. But basically you know what you need to do I expect? Get some XML parser (I suggest XML::Simple for its simplicity), use Nagios::Plugin to help you write the plugin. You can also get REST::Client to make it easier to use the rest API of jenkins

p.s. is this your first attempt at perl?

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • Thanks!!Yes I am newbie to perl. – MangeshBiradar Jan 02 '13 at 08:55
  • @Maverick143 I was afraid of that. In that case you might want to learn atleast a bit of perl first. This should become quite easy then, and besides we cannot write your code for you here. Here are some decent resources, a good book would work too, [perlintro](http://perldoc.perl.org/perlintro.html), [A Beginner's Introduction to Perl 5.10](http://www.perl.com/pub/2008/04/23/a-beginners-introduction-to-perl-510.html), [Beginning Perl](http://www.perl.org/books/beginning-perl/) – Karthik T Jan 02 '13 at 09:02
  • thanks for your valuable help.. – MangeshBiradar Jan 02 '13 at 09:05
0

Here is the code what I wrote.

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use XML::Twig;
use HTTP::Request;
use Getopt::Long;
use Nagios::Plugin;
#use File::stat;

use File::Basename;
my $PROGNAME = basename($0);

my $p = Nagios::Plugin->new(
    usage => "Usage: %s [ -H|--host ] [ -p|--port ]",
    extra => "
    Examples:
    $PROGNAME --host myhost -port 8080
    Check Host name and port.
");

$p->add_arg(
    spec => 'host|f=s',
    required => 1,
    help => "-H, --host=Hostname. REQUIRED.");

$p->add_arg(
    spec => 'port|a=i',
    default => 8080,
    help => "-p, --port=Portnumber. Default 8080.");

$p->getopts;

my $o_host = $p->opts->host ;
my $o_port = $p->opts->port;
my $protocol = 'http';
my $o_url = '/api/xml';
my @jobs;

my $url = $protocol . "://" . $o_host . ":" . $o_port . $o_url ;
#print $url,"\n";
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get($url);
if ($response->is_success) {
    my $content = $response->decoded_content;  # or whatever
    XML::Twig->new( twig_roots => { 'job/name' => sub { push @jobs, $_->text; } }) ->parseurl( $url);
}
else {
    $p->nagios_die( CRITICAL, "Bad page found" );
}
#print @jobs;
foreach my $job_name (@jobs) {
        #print $job_name;
        my $job_url = $protocol . "://" . $o_host . ":" . $o_port . "/" . "job" . "/" . $job_name . $o_url ;
        #print $job_url;
        my $response2 = $ua->get($job_url);
        #print $job_url;
        if ($response2->is_success) {
            $p->nagios_die( OK, "Job link valid" );
        }
        else {
            $p->nagios_die( CRITICAL, "Bad page found" );
        }
}
MangeshBiradar
  • 3,820
  • 1
  • 23
  • 41