简体   繁体   English

通过Jenkins REST API获取IP地址?

[英]Getting an IP Address through Jenkins REST API?

I've been tasked with instituting some health checking on some Jenkins jobs. 我的任务是对Jenkins的一些工作进行一些健康检查。 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. 我们的想法是通过Jenkins rest API获取作业的状态和相关的IP地址,这样我就可以使用该信息与另一个安静的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. 我创建了一个groovy脚本,成功解析Jenkins作业并获取其状态(无论它们是否正在运行),但我还没有找到将这些作业与其IP地址相关联的方法。 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? 有没有办法通过其余的API获取Jenkins中的从站的IP地址,如果没有,是否有其他方法来获取所述IP地址?

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? 在标有//的评论部分,我们可以获得IPS列表吗? I would like to somehow use the Jenkins Rest API to get a list of the IPs of the Jenkins slaves. 我想以某种方式使用Jenkins Rest API来获取Jenkins从属的IP列表。

Can I do this through the rest API? 我可以通过其余API执行此操作吗? And if not, is there another way? 如果没有,还有另一种方式吗? Through the CLI, perhaps? 通过CLI,也许吧? 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. 我没有在Jenkins API文档中的任何地方看到过getIP()方法,但我对此很新,所以我可能只是遗漏了一些简单的东西。

You can execute groovy script on your slave via REST API, thus can get slave's ip address. 您可以通过REST API在您的slave上执行groovy脚本,从而可以获得slave的ip地址。 Here is an example with curl, but you can adjust it to use in your code: 以下是curl的示例,但您可以调整它以在代码中使用:

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

Node : to get an ip-address of a particular slave, you have to know it's name. 节点 :要获取特定从站的IP地址,您必须知道它的名称。 That's easy to grad nodes names querying jenkins_url/computer/api/json 查询jenkins_url/computer/api/json grad节点名称很容易

I am going to try scraping the HTML of the node page to grab the IP from the swarm slave description 我将尝试抓取节点页面的HTML以从swarm slave描述中获取IP

This will not always work as slave may be connected via JNLP and you will not have an IP on that HTML page. 这并不总是有效,因为奴隶可能通过JNLP连接,你不会在该HTML页面上有IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM