I've been tasked with instituting some health checking on some Jenkins jobs. The idea is to get the job's status and an associated IP address through the Jenkins rest API, so I can use that information to interface with another restful API. I have created a groovy script that successfully parses through the Jenkins jobs and gets their status (whether or not they are running) but I have yet to find a way to associate these jobs with their IP addresses. Is there any way to get the IP address of a slave in Jenkins through the rest API, and if not, is there another way to get said IP address?
Here's the code I've got so far that works like a charm:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
import groovyx.net.http.RESTClient
import groovy.json.JsonSlurper
def jenkinsClient = new RESTClient( 'myJenkinsURL' )
def monitorClient = new RESTClient( 'myOtherRestfulAPIURL' )
monitorClient.auth.basic "<username>", "<pass>"
jenkinsClient.setHeaders(Accept: 'application/json')
monitorClient.setHeaders(Accept: 'application/json')
def jobs = []
def jenkinsGetJobs = jenkinsClient.get( path: 'view/Events/api/json', contentType: 'text/plain' )
def jenkinsGetJobsSlurp = new JsonSlurper().parse(jenkinsGetJobs.data)
for (def j in jenkinsGetJobsSlurp.jobs ){
jobs.add(j.name)
}
//Can we get a list of IPS?
for(def job in jobs){
def jenkinsResp = jenkinsClient.get( path : 'view/Events/job/' + job + '/api/json', contentType: 'text/plain', query: [depth:"1"])
def jenkinsSlurp = new JsonSlurper().parse(jenkinsResp.data)
// println slurp
if (jenkinsSlurp.builds[0].building == true){
println "The " + job + " job is running."
//Make a call to other Restful API here
}
if (jenkinsSlurp.builds[0].building == false){
println "The " + job + " job is not running."
}
}
In the commented section labeled //can we get a list of IPS? I would like to somehow use the Jenkins Rest API to get a list of the IPs of the Jenkins slaves.
Can I do this through the rest API? And if not, is there another way? Through the CLI, perhaps? I haven't seen a getIP() method anywhere in the Jenkins API documentation but I am fairly new to this so I might just be missing something simple.