Getting IP address through Jenkins REST API?

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.

+4
source share
1 answer

You can execute the groovy script on your slave device through the REST API, so you can get the slave IP address. Here is an example with curl, but you can configure it for use in your code:

 $ curl -u username:password -d "script=println InetAddress.localHost.hostAddress" jenkins_url/computer/node_name/scriptText # 192.168.0.104 

Node : in order to get the ip address of a specific slave, you need to know its name. These are the simple jenkins_url/computer/api/json names of the nodes requesting jenkins_url/computer/api/json

I will try to clear the node HTML pages to capture the IP from the description of the subordinate swarm

This does not always work, since the slave can be connected via JNLP and you will not have the IP address on this HTML page.

+2
source

All Articles