Jenkins: Monitoring external jobs
Jenkins CI is the leading open-source continuous integration server. It’s primarily used for:
- Building/testing software projects continuously
- 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
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
(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:
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/).
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
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.
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.
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
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.
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.
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.