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 – 
ourgroupdistributionl...@mydomain.com
toAddress = "emailaddressfornotificati...@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: bmat...@gmail.com [mailto:bmat...@gmail.com] On Behalf Of Baptiste Mathus
Sent: Tuesday, April 17, 2018 5:26 AM
To: jenkinsci-users@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.nag...@gmail.com<mailto:saranya.nag...@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-users+unsubscr...@googlegroups.com<mailto:jenkinsci-users%2bunsubscr...@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.

--
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+unsubscr...@googlegroups.com<mailto:jenkinsci-users+unsubscr...@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<https://groups.google.com/d/msgid/jenkinsci-users/CANWgJS4M-fxAS6%3D%3DwMGCVRPczzHTWYsK7S8wiS%3DMY0LNs_cx6w%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
________________________________
[https://www.aciworldwide.com/-/media/aci-footer]<http://www.aciworldwide.com>
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.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/DM2PR0801MB074787C751346A97A6EA46E8F2B70%40DM2PR0801MB0747.namprd08.prod.outlook.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to