Как проверить состояние соединения с узлом jenkins с помощью jenkins-cli

Я пытаюсь проверить, подключен ли конкретный узел Jenkins с помощью Jenkins CLI. Чтобы получить детали узла я могу использовать 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>

Но это не включает статус узла. У любого есть идея, как проверить состояние узла Дженкинсом

1 ответ

Решение

Поскольку я не знаю метода, использующего jenkins-cli, я предлагаю использовать REST API:

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

возвращает данные JSON, включая offline поле.

Вы можете получить обзор всех агентов (называемых здесь компьютерами) по следующему URL:

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

Мы можем реализовать это с помощью конвейера. Я использовал Groovy скрипт для машин Windows VDI, ниже скрипт.

pipeline {agent none stage {

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

}

Другие вопросы по тегам