Home > Continuous Integration > Jenkins: Monitoring external jobs

Jenkins: Monitoring external jobs

Jenkins CI is the leading open-source continuous integration server.  It’s primarily used for:

  1. Building/testing software projects continuously
  2. Monitoring executions of externally-run jobs

Today we will discuss about using Jenkins CI for Monitoring executions of externally-run jobs. I used to receive various daily reports via cron in my mailbox from different servers. Then I had to fill an excel sheet with all these to produce a final report on server health status. This was an annoying task. I found Jenkins a great tool to overcome this boring task without any extra cost and with very little effort. Here are the basic steps on how this can be done.

These steps were performed on “SunOS 5.10 Generic_142900-02 sun4u sparc SUNW,Sun-Fire-V440” platform. However, these can be executed on any LINUX/UNIX platform without much changes.

Make sure you have java present on your hosts. You may check this by

$which java

/usr/jdk/jdk1.6.0_21/bin/java

$java -version

java version “1.6.0_21”
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) Server VM (build 17.0-b16, mixed mode)

If java is missing then install java

1. Download Jenkins

$wget http://jenkins.mirror.isppower.de/war/1.451/jenkins.war

(In no wget or server behind a firewall blocking internet access, the download on your local PC and scp/ftp to your host)

2. Install and start jenkins by running below command

$nohup java -jar jenkins.war > jenkins.log 2>&1 &

The new build server should now be available at:

http://host1:8080/

If everything has gone well you would be able to see the Jenkins Dashboard. You may wish to configure few basic things as per your need by clicking “Manage Jenkins” –> “Configure System”.

Next, lets configure a job which monitors an external job, on a remote server. There are 2 ways of doing this and we will discuss both.

Method 1(using crontab)

1.1. On Jenkins Dashboard Click “New Job

1.2. Put a Job name e.g. Report1

1.3. Check “Monitor an external job” and click “OK

1.4. Put a brief Description(optional) about the job on next page, click “Save” and return back to the dashboard. You will notice that this Job cannot be executed from Jenkins Dashboard (the build button against this job is missing)!

Next steps(1.5 to 1.9) have to be executed on remote server, host2 (which you want to monitor via jenkins)

Logon to host2, make sure you have java installed on this host too.

1.5a. $export JENKINS_HOME=http://host1:8080

If you have enabled security on Jenkins server and only authorized users can run a Job, then JENKINS_HOME should be:

1.5b. $ export JENKINS_HOME=http://user_id:password@host1:8080

where user_id and password are that of an account in Jenkins, authorized to execute the build.

1.6. SCP/FTP below files from host1 ~/.jenkins/war/WEB-INF/lib/ directory to, a new directory preferably, on host2(say ~/jenkins/).

jenkins-core-1.451.jar

xstream-1.3.1-jenkins-9.jar

ant-1.8.0.jar

commons-lang-2.4.jar

jenkins-core-1.451.jar

remoting-2.12.jar

xstream-1.3.1-jenkins-9.jar

commons-io-1.4.jar

jna-posix-1.0.3.jar

1.7. If your report is generated by a script myreport.sh then this can be done by executing

$ java -jar /path_to/jenkins/jenkins-core-*.jar “Report1” /path_to/myreport.sh 2>&1 > /dev/null

Job “Report1” should now be executed and details of execution can now be seen on Jenkins dashboard. This was a manual execution which you do not want to do daily. You want this to happen automatically. So lets put this in crontab on host2.

1.8. Add below 2 lines to a new script say, jenkins_report.sh

export JENKINS_HOME=http://user_id:password@host1:8080

java -jar /path_to/jenkins/jenkins-core-*.jar “Report1” /path_to/myreport.sh 2>&1 > /dev/null

Save the script and grant execute permissions to the script by

$ chmod 755 jenkins_report.sh

1.9. Add below entry in your crontab by executing

$ crontab -e

15 06 * * * /path_to/jenkins_report.sh

This should now execute the script for the build job “Report1” at 06:15 am daily and same can be monitored via Jenkins Dashboard.

Method 2

2.1. On Jenkins Dashboard click “Manage Jenkins” –> “Manage Plugins” and from “Available” plugins tab, install “Jenkins SSH Plugin“. This plugin executes shell commands remotely using SSH protocol.

2.2. On Jenkins Dashboard click “Manage Jenkins” –> “Configure System” and under “SSH remote hosts” section click “Add“. Fill in the details(fields listed below) of your remote server where you wish to execute a command or script.  You only need to fill either Password/Passphrase or Keyfile.

Hostname

Port

User Name

Password/Passphrase

Keyfile

Click save at the the bottom of the page and return to Jenkins Dashboard.

2.3. On Jenkins Dashboard click “New Job

2.4. Put a Job name e.g. Report2

2.5. Check “Build a free-style software project” and click “OK

2.6. Now configure below sections:

Description: <any meaningful description. this is optional>

Source Code Management

Select None

Build Triggers

Check Build periodically and enter as below(this would execute the build to 06:15 AM every day, you can set this to any desired value)

15 06 * * *

You can configure (if you desire) this job to be executed after another job, depending upon your requirement. But most of us would go with the option explained above.

Build

Click Add build step and select Execute shell script on remote host using SSH

Select the SSH site from drop down that was added in step 2.2

Paste the contents of script e.g. myreport.sh(mentioned in step 1.7) in Command section or any command/script you wish to execute on remote server.

Post-build Actions

Select an option that suits your requirements the best. A good practice would be to send an E-mail Notification –> Send e-mail for every unstable build. Fill the Receipients e-mail addresses seperated by a space(in case of multiple receipients) and click Save.

This completes the job setup which would build the job daily at and same can be monitored via the Jenkins dashboard. Run the build manually once to check if all works as expected.

Have fun!

Feedback welcome.

Advertisements
  1. joe
    March 15, 2012 at 5:15 PM

    Thank you. Very helpful. Works perfectly.

  2. March 17, 2012 at 4:13 PM

    Tons of Thanks to you Vikram, i just followed the step-by-step and its works perfectly.

  3. Mayur
    August 13, 2012 at 7:17 PM

    Vikram, Nice instructions. I was facing issues in connecting to remote job and this helped in fixing my issues. Thank you

  4. Krunal Shah
    September 25, 2013 at 8:37 AM

    Good article. It also covers how to install jenkins. Nicely drafted too.

  5. February 16, 2014 at 7:28 PM

    Nice tutorial, but I still cannot see any significant difference between monitoring an external job and running a shell script in a free-form job. As for me, second approach seems to be easier to setup and maintain.

  6. May 7, 2015 at 4:43 AM

    Is this feature still available on the latest release ?
    I cant seem to let it work.

    $ java -jar /path_to/jenkins/jenkins-core-*.jar “Report1″ /path_to/myreport.sh 2>&1 > /dev/null

  7. Udaya
    May 22, 2015 at 11:13 PM

    thanks a lot.. you saved me a lot of time!

  1. November 3, 2014 at 7:06 AM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: