I was assigned to establish some health check on some of Jenkins' works. The idea is to get the job status and its associated IP address through the Jenkins Rest API, so I can use this information to interact with another supporting API. I created a groovy script that successfully parses Jenkins jobs and gets their status (regardless of whether it works or not), but I still have to find a way to associate these jobs with their IP addresses. Is there a way to get the IP address of a slave in Jenkins via the rest of the API, and if not, is there another way to get the specified IP address?
Here is the code that still works like a charm for me:
@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 IP addresses of Jenkins subordinates.
Can I do this through the rest of the API? And if not, is there any other way? Perhaps through the CLI? I have not seen the getIP () method anywhere in the Jenkins API documentation, but I'm pretty new to this, so I can just skip something simple.
source share