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

如何使用jenkins cli检查jenkins节点连接状态,jenkins,jenkins-cli,Jenkins,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:\\je

我正在尝试检查特定的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>

20170602_jenkins_slave_002
10.49.82.46
c:\\jenkins\u根
1.
独家
20170602_jenkins_slave_002
管理

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

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

http://jenkins.example.com/computer/:name/api/json
返回JSON数据,包括一个
脱机
字段

您可以通过以下URL获得所有代理(此处称为计算机)的概览:

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

我们可以使用管道来实现这一点。我确实为windows VDI机器使用了groovy脚本,下面是脚本

管道{ 一号特工 舞台{


}

我从来不知道Jenkins有Json API。我也可以得到节点列表。非常感谢&这正是我想要的
  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"
            }
        }
    }
    }
}