title: Check if Agent Node is online
body: <p>I'm trying to connect to couple of my Jenkins agents and run some commands on each of them. After researching, <a href="https://stackoverflow.com/questions/60789539/how-to-connect-to-list-of-servers-using-a-loop-in-jenkin-pipeline">follow the answer from this link</a>, this Jenkinsfile code works fine:</p>
  <pre><code>pipeline {
      agent none
      stages {
          stage('Check') {            
              matrix {
                  agent {
                      label &quot;${SLAVE}&quot;
                  }
     
                  axes {
                      axis {
                          name 'SLAVE'
                          values &quot;slv1&quot;, &quot;slv2&quot;,
                          &quot;slv3&quot;
                      }
                  }
                  stages {
                      stage('do something') {
                          steps {
                              sh 'hostname'
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  <p>But I want to check each of nodes is online or not before doing anything.
  I haven't had any luck with what I've tried. This is my latest trying:</p>
  <pre><code>Boolean finalResult = true
  
  def checkStatus(String nodeName){
      Node cleanUpNode = Jenkins.instance.getNode(nodeName)
          Computer computer = cleanUpNode.toComputer()
  
          if (cleanUpNode == null) {
              println(&quot;ERROR: Node ${nodeName} doesn't exist&quot;)
              finalResult = false
              continue
          }
  
          if (computer.countBusy()) {
              println(&quot;WARNING: Ignore ${nodeName} as it is busy&quot;)
              continue
          }
          if (computer.isOffline())
          {
              println &quot;Error! Node ${nodeName} is offline.&quot;;
              finalResult = false
              continue
          }
      return finalResult    
  }
  
  pipeline {
      agent none
      stages {
          stage('Check') {            
              matrix {
                  agent {
                      label &quot;${SLAVE}&quot;
                  }
                  when {
                        expression { checkStatus(${SLAVE}) == true }
                  }
                  axes {
                      axis {
                          name 'SLAVE'
                          values &quot;slv1&quot;, &quot;slv2&quot;,
                          &quot;slv3&quot;
                      }
                  }
                  stages {
                      stage('do something') {
                          steps {
                              sh 'hostname'
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  <p>My first idea is create an array to store all the nodes, then check it and assign variable into it through <code>values</code> in <code>axis</code>. But this idea <a href="https://stackoverflow.com/questions/59844846/declarative-pipeline-with-dynamic-matrix-axis-values">is not supported</a></p>
  <p>Can anyone help?
  Thanks in advance!</p>