axmol/tools/jenkins-scripts/watchdog.py

101 lines
2.9 KiB
Python

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):
#get jenkins-job-watchdog-threshold
#Get last build running
build = _job.get_last_build()
running = build.is_running()
print 'build_job:',_job,'running:',running
if not running:
return False
#Get numerical ID of the last build.
buildnu = _job.get_last_buildnumber()
print "buildnumber:#",buildnu
#get nowtime
nowtime = int(time.time())
print 'nowtime:', time.ctime(nowtime)
#get build start time
timestamp = build._poll()['timestamp']
buildtime = int(timestamp)/1000
print 'buildtime:', time.ctime(buildtime)
subtime = (nowtime - buildtime)/60
print 'subtime:', subtime, _threshold
if subtime > _threshold:
#print 'subtime',subtime
#kill dead buid
build.stop()
def main():
username = os.environ['JENKINS_ADMIN']
password = os.environ['JENKINS_ADMIN_PW']
J = Jenkins('http://115.28.134.83:8000',username,password)
#get all jenkins jobs
for key,job in J.iteritems():
threshold = 0
if(os.environ.has_key(key+'-threshold')):
threshold = int(os.environ[key+'-threshold'])
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)
# -------------- main --------------
if __name__ == '__main__':
sys_ret = 0
try:
sys_ret = main()
except:
traceback.print_exc()
sys_ret = 1
finally:
sys.exit(sys_ret)