如何使用jenkins-cli检查jenkins节点连接状态

时间:2017-06-05 05:32:10

标签: jenkins jenkins-cli

我尝试使用Jenkins CLI检查特定的Jenkins节点是否已连接 。要获取节点详细信息,我可以使用get-node命令,它将返回详细信息,如此xml

<?xml version="1.0" encoding="UTF-8"?>
<slave>
<name>20170602_jenkins_slave_002</name>
<description>10.49.82.46</description>
<remoteFS>c:\\jenkins_root</remoteFS>
<numExecutors>1</numExecutors>
<mode>EXCLUSIVE</mode>
<retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
<launcher class="hudson.slaves.JNLPLauncher"/>
<label>20170602_jenkins_slave_002</label>
<nodeProperties/>
<userId>admin</userId>
</slave>

但它不包括节点状态。任何人都知道如何通过Jenkins cli检查节点状态

2 个答案:

答案 0 :(得分:3)

由于我不知道使用的方法,我的建议是使用REST API:

http://jenkins.example.com/computer/:name/api/json

返回包含offline字段的JSON数据。

您可以在以下网址上查看所有代理(此处称为计算机)的概述:

http://jenkins.example.com/computer/api/json?pretty=true

答案 1 :(得分:0)

我们可以使用管道来实现。我确实在Windows VDI机器上使用了Groovy脚本,下面是该脚本。

管道{ 代理商无 阶段{

  stage('Agent Health check step') {
        options {
            timeout(time:5, unit: 'MINUTES')
        }
        agent {
            label "master"
        }
    steps { 
    
        script {
    
            def agStatusList = []
            
            def agentsMap = [VDI1:"XYZ@gmail.com", VDI2:"XYZ2@gmail.com", VDI3:"XYZ@gmail.com", VDI4:"himesh.patel@gmail.com"]
            
            String agEmlTo=''
            
            for (agSlave in hudson.model.Hudson.instance.slaves) {
                    def agName=agSlave.name
                    def agOwner=agentsMap.get(agName)
                    def agStatus=agSlave.getComputer().isOnline()
                    
                    
                    
                    if (agStatus != true ){
                        
                        echo "Node is offline"
                        echo " "    
                        agStatusList += agName + " node is offline" + " and owner is " + agOwner
                        
                        if(agStatus != true && agOwner != null){
                        
                            if (agEmlTo == null || agEmlTo.trim().isEmpty()){ 
                                agEmlTo = agOwner
                            } else{
                                agEmlTo += (','+agOwner) 
                            }
                        

                        
                        }
                        
                    }else{
                    
                        echo "Node is online"
                        echo " "
                        agStatusList += agName + " node is online" + " and owner is " + agOwner
                        
                    }
                            
            }
            
             println agStatusList
             
             println agEmlTo
             
            if (agEmlTo != null && !agEmlTo.trim().isEmpty()) {
                
                echo "Sending email to the owners of offline Nodes"
                
                string agEmlCc='123@gmail.com'
                string agEmlFrom='JenkinsServer@gmail.com'
                String agEmlHdr='[Notification] Your Windows VDI agent is offline'
                
                String agEmlBody='Hello, \n \nThe Windlows VDI agent assigned to you seems offline now. Please check the Windows VDI machine and get it connected ASAP.\n \n *This E-mail is from Automated process. Please do not respond directly to this e-mail as the originating e-mail account is not monitored*'
                    
                emailext body: "${agEmlBody}",
                to: "${agEmlTo}",
                subject: "${agEmlHdr}"
                from: "JenkinsServer@gmail.com"
            }
        }
    }
    }
}

}