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

How to check jenkins node connection status using jenkins-cli

我正在尝试使用 Jenkins CLI 检查特定的 Jenkins 节点是否已连接 .要获取节点详细信息,我可以使用 get-node 命令,它的 returns 详细信息如下 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

检查节点状态

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

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

returns JSON 数据包括 offline 字段。

您可以在以下 URL:

上获得所有代理(此处称为 计算机)的概览
http://jenkins.example.com/computer/api/json?pretty=true

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

管道{ 代理 none 阶段 {

  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"
            }
        }
    }
    }
}

}