Dejan Bodiroga
Dejan Bodiroga

Reputation: 153

Send email alert if Jenkins agent is Offline - script

I have Jenkins agent nodes and need to set email alert if some of the nodes are down. Manage to get the list of offline nodes, but struggling with email part.

int exitcode = 0
for (slave in hudson.model.Hudson.instance.slaves) {
    if (slave.getComputer().isOffline().toString() == "true"){
        println('Slave ' + slave.name + " is offline!");
        exitcode++;
    }
}

if (exitcode > 0){
    println("We have a Slave down, failing the build!");
    return 1;
    send_email_notification()
}

def send_email_notification(String body) {
    emailext body: "$body",
            to: "[email protected]",
            subject: "Offline Slave",
            presendScript : "msg.addHeader('X-Priority', '1 (Highest)')"
}

No Errors inside of Jenkins Script Console:

Result Slave NODE01 is offline! We have a Slave down, failing the build! Result: 1

But I am not receiving an email.

Upvotes: 0

Views: 1261

Answers (1)

Don
Don

Reputation: 161

Do you have any errors? Are you able to receive any emails? Is the email server configured in https://{JENKINS_URL}/configure?

What happens if you run this:

emailext body: "Testing",
            to: "[email protected]",
            subject: "Offline Slave",
            presendScript : "msg.addHeader('X-Priority', '1 (Highest)')"

It looks to me like you aren't passing a body to the send_email_notification() call. Try this:

def send_email_notification(String body) {
    emailext body: "${body}",
            to: "[email protected]",
            subject: "Offline Slave",
            presendScript : "msg.addHeader('X-Priority', '1 (Highest)')"
}
for (slave in hudson.model.Hudson.instance.slaves) {
    if (slave.getComputer().isOffline().toString() == "true"){
        msg = "Slave " + slave.name + " is offline!";
        send_email_notification("${msg}")
    }
}

Upvotes: 0

Related Questions