We can implement this using pipeline. I did using the groovy script for the windows VDI machines, Below is the script.
pipeline {
agent none
stages {
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"
}
}
}
}
}
}