Alert if master gets disconnected or if any shared slave goes offline.

318 views
Skip to first unread message

Sunshine

unread,
Apr 17, 2018, 5:02:09 AM4/17/18
to Jenkins Users
Hi all ,

Is there a way via groovy scipt to check if a master gets disconnected from the JOC ?

Also ,

To check for any offline shared slave that's connected at JOC level ,
I used com.cloudbees.opscenter.server.model.Sharedslaves to display offline slaves ,
But it doesn't seem to work.

Baptiste Mathus

unread,
Apr 17, 2018, 5:26:51 AM4/17/18
to jenkins...@googlegroups.com
Hello,

This question is specific to CloudBees products. Please open a ticket on the usual commercial support https://support.cloudbees.com/hc/en-us/requests/new

Thanks


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/a38c93b2-57bd-475c-8d17-f02cc67c89e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Burrows, John

unread,
Apr 17, 2018, 9:05:46 AM4/17/18
to jenkins...@googlegroups.com

Sunshine (Saranya),

 

We use a simple Groovy (Scriptler) script setup to run as a Job on the Jenkins Master every 15 minutes, that  checks to see if Jenkins Nodes/Slaves respond or it can get an environment setting from them, and if not, sends an email to us alerting that the Node/Slave is disconnected or down.

 

Id:                           monitor.slave.nodes.groovy

Name:                   Monitor Slave Nodes

Comment:           Monitors Jenkins Nodes and sends out email if it finds a node down.

Permission: X

Restriction: X (only run on Master)

No Parameters

 

----------------------Script Start--------------------

/*** BEGIN META {

  "name" : "Monitor Slave Nodes",

  "comment" : "Monitors Jenkins Nodes and sends out email if it finds a node down.",

} END META**/

 

import hudson.model.*

import hudson.node_monitors.*

import hudson.slaves.*

import java.util.concurrent.*

 

jenkins = Hudson.instance

 

import javax.mail.internet.*;

import javax.mail.*

import javax.activation.*

 

import jenkins.model.JenkinsLocationConfiguration

 

//SendMail process to send emails for notification purposes.

def sendMail (slave, cause) {

 

jlc = new jenkins.model.JenkinsLocationConfiguration()

message = slave + " slave is down. Check " + jlc.getUrl() + "/computer/" + slave + "\nBecause " + cause

subject = slave + " slave is offline"

//The Email Address(es) you want Notifications Sent to, separated by semi-colons(;). We usually put an Email Distribution List here, example – OurGroupDist...@MyDomain.com

toAddress = "EmailAddressFo...@YourDomain.com"

fromAddress = jlc.getAdminAddress()

//Your SMTP Server Fully Qualified Domain Name, example – smtp.yahoo.com

host = "SMTPServerFQDN"

//Port number to use for SMTP Server

port = "25"

 

//Set properties to support the email process

Properties mprops = new Properties();

mprops.setProperty("mail.transport.protocol","smtp");

mprops.setProperty("mail.host",host);

mprops.setProperty("mail.smtp.port",port);

 

Session lSession = Session.getDefaultInstance(mprops,null);

MimeMessage msg = new MimeMessage(lSession);

 

 

//tokenize out the recipients in case they came in as a list

StringTokenizer tok = new StringTokenizer(toAddress,";");

ArrayList emailTos = new ArrayList();

while(tok.hasMoreElements()){

emailTos.add(new InternetAddress(tok.nextElement().toString()));

}

InternetAddress[] to = new InternetAddress[emailTos.size()];

to = (InternetAddress[]) emailTos.toArray(to);

msg.setRecipients(MimeMessage.RecipientType.TO,to);

InternetAddress fromAddr = new InternetAddress(fromAddress);

msg.setFrom(fromAddr);

msg.setFrom(new InternetAddress(fromAddress));

msg.setSubject(subject);

msg.setText(message)

 

Transport transporter = lSession.getTransport("smtp");

transporter.connect();

transporter.send(msg);

}

 

//Get Environment process for getting each Jenkins Node

def getEnviron(computer) {

   def env

   def thread = Thread.start("Getting env from ${computer.name}", { env = computer.environment })

   thread.join(2000)

   if (thread.isAlive()) thread.interrupt()

   env

}

 

//Check to see if the Jenkins Node is accessible (try to get the PATH setting from the Node)

def slaveAccessible(computer) {

    getEnviron(computer)?.get('PATH') != null

}

 

 

def numberOfflineNodes = 0

def numberNodes = 0

for (slave in jenkins.slaves) {

   def computer = slave.computer

   numberNodes ++

   println ""

   println "Checking computer ${computer.name}:"

 

   //Can you get the PATH and the Node is not OffLine

   def isOK = (slaveAccessible(computer) && !computer.offline)

 

   //If it responds, it is OK, so just print some info about it

   if (isOK) {

     println "\t\tOK, got PATH back from slave ${computer.name}."

     println('\tcomputer.isOffline: ' + slave.getComputer().isOffline());

     println('\tcomputer.isTemporarilyOffline: ' + slave.getComputer().isTemporarilyOffline());

     println('\tcomputer.getOfflineCause: ' + slave.getComputer().getOfflineCause());

     println('\tcomputer.offline: ' + computer.offline);

 

 

   } else {

     //The Node doesn’t respond or is set OffLine

     numberOfflineNodes ++

     println "  ERROR: can't get PATH from slave ${computer.name}."

     println('\tcomputer.isOffline: ' + slave.getComputer().isOffline());

     println('\tcomputer.isTemporarilyOffline: ' + slave.getComputer().isTemporarilyOffline());

     println('\tcomputer.getOfflineCause: ' + slave.getComputer().getOfflineCause());

     println('\tcomputer.offline: ' + computer.offline);

     if ((slave.getComputer().getOfflineCause().toString().contains("Disconnected by")) | (computer.name.contains("SetANodeNameHereifYouWantToIgnoreItSpecifically"))) {

        //The Node was purposely taken OffLine (disconnected) or is explicitly set to be ignored, don’t send anything out

     } else {

       //Send out an email notification for each Node that is OffLine/Down

       sendMail(computer.name, slave.getComputer().getOfflineCause().toString())

     }

     if (slave.getComputer().isTemporarilyOffline()) {

      if (!slave.getComputer().getOfflineCause().toString().contains("Disconnected by")) {

         computer.setTemporarilyOffline(false, slave.getComputer().getOfflineCause())

      }

     } else {

         computer.connect(true)

     }

   }

}

println ("Number of Offline Nodes: " + numberOfflineNodes)

println ("Number of Nodes: " + numberNodes)

----------------------Script End--------------------

 

I hope this helps.

 

Thanks,

John

 

---------------

John Burrows

 

 

From: bma...@gmail.com [mailto:bma...@gmail.com] On Behalf Of Baptiste Mathus
Sent: Tuesday, April 17, 2018 5:26 AM
To: jenkins...@googlegroups.com
Subject: Re: Alert if master gets disconnected or if any shared slave goes offline.

 

Hello,

 

This question is specific to CloudBees products. Please open a ticket on the usual commercial support https://support.cloudbees.com/hc/en-us/requests/new

 

Thanks

2018-04-17 11:02 GMT+02:00 Sunshine <saranya...@gmail.com>:

Hi all ,

Is there a way via groovy scipt to check if a master gets disconnected from the JOC ?

Also ,

To check for any offline shared slave that's connected at JOC level ,
I used com.cloudbees.opscenter.server.model.Sharedslaves to display offline slaves ,
But it doesn't seem to work.


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

 

--

You received this message because you are subscribed to the Google Groups "Jenkins Users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CANWgJS4M-fxAS6%3D%3DwMGCVRPczzHTWYsK7S8wiS%3DMY0LNs_cx6w%40mail.gmail.com.


For more options, visit https://groups.google.com/d/optout.



This email message and any attachments may contain confidential, proprietary or non-public information. The information is intended solely for the designated recipient(s). If an addressing or transmission error has misdirected this email, please notify the sender immediately and destroy this email. Any review, dissemination, use or reliance upon this information by unintended recipients is prohibited. Any opinions expressed in this email are those of the author personally.

Sunshine

unread,
Apr 17, 2018, 9:14:28 AM4/17/18
to Jenkins Users
Hi John ,

This definitely works for dedicated individual slaves connected on Masters.

But I have shared-slaves on JOC and this doesn't seem to help :(

Reply all
Reply to author
Forward
0 new messages