From 1d5748c505c0da062b0985d02649b06b3d1b858e Mon Sep 17 00:00:00 2001 From: lm Date: Mon, 19 May 2014 18:03:21 +0800 Subject: [PATCH] [Jenkins][ci skip] Add check slave online status to watchdog --- tools/jenkins-scripts/watchdog.py | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tools/jenkins-scripts/watchdog.py b/tools/jenkins-scripts/watchdog.py index 99c2e7447f..61d5588fb0 100644 --- a/tools/jenkins-scripts/watchdog.py +++ b/tools/jenkins-scripts/watchdog.py @@ -1,8 +1,39 @@ import jenkinsapi from jenkinsapi.jenkins import Jenkins +from jenkinsapi.node import Node import sys import time import os +from email.mime.text import MIMEText +import smtplib + +def send_mail(sub,title,content): + to_list = os.environ['EMAIL_LIST'].split(' ') + mail_user = os.environ['EMAIL_USER'] + mail_pass = os.environ['EMAIL_PWD'] + mail_postfix = 'gmail.com' + me = mail_user + "<" + mail_user + "@" + mail_postfix + ">" + msg = MIMEText(content, _subtype='plain', _charset='gb2312') + msg['Subject'] = sub + msg['From'] = me + msg['To'] = " ".join(to_list) + print 'to users:', msg['To'] + msg['Content'] = 'test' + try: + s = smtplib.SMTP('smtp.gmail.com', 587) + s.ehlo() + s.starttls() + s.login(mail_user,mail_pass) + s.sendmail(me, to_list, str(msg)) + print 'info:', me, to_list, str(msg) + s.close() + return True + except Exception, e: + print str(e) + return False + +def sendEmail(msg): + send_mail("Jenkins node " + msg + " is offline", 'for offline.', msg + ' is offline') #check & kill dead buid def build_time(_job,_threshold): @@ -43,6 +74,16 @@ def main(): else: threshold = int(os.environ['jenkins-job-watchdog-threshold']) build_time(job,threshold) + + #check node status + node_names = os.environ['NODE_LIST'].split(' ') + for node_name in node_names: + node = J.get_node(node_name) + if node.is_online(): + print node_name, ' : is online' + else: + sendEmail(node_name) + print node_name, ' : is offline' return(0)