axmol/tools/jenkins-scripts/master-scripts/watchdog.py

114 lines
3.4 KiB
Python
Raw Normal View History

2014-03-12 15:36:09 +08:00
import jenkinsapi
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.node import Node
2014-03-12 15:36:09 +08:00
import sys
import time
import os
from email.mime.text import MIMEText
import smtplib
import requests
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')
2014-03-12 15:36:09 +08:00
def rebuild_jobs(build):
rebuild_jobs = os.environ['REBUILD_JOBS'].split(' ')
trigger_urls = os.environ['JOB_TRIGGER_URLS'].split(' ')
for i, rebuild_job in enumerate(rebuild_jobs):
if rebuild_job in build.__str__():
payload = build._poll()['actions'][0]['parameters'][0]['value']
job_trigger_url = trigger_urls[i]
post_data = {'payload': payload}
requests.post(job_trigger_url, data=post_data)
print 'build_job:', rebuild_job, 'rebuild : TRUE'
2014-03-13 11:16:21 +08:00
#check & kill dead buid
def build_time(_job,_threshold):
#get jenkins-job-watchdog-threshold
2014-03-13 11:16:21 +08:00
#Get last build running
build = _job.get_last_build()
2014-03-12 15:36:09 +08:00
running = build.is_running()
2014-03-13 11:16:21 +08:00
print 'build_job:',_job,'running:',running
2014-03-12 15:36:09 +08:00
if not running:
return False
2014-03-13 11:16:21 +08:00
#Get numerical ID of the last build.
buildnu = _job.get_last_buildnumber()
2014-03-12 15:36:09 +08:00
print "buildnumber:#",buildnu
2014-03-12 17:18:16 +08:00
#get nowtime
nowtime = int(time.time())
print 'nowtime:', time.ctime(nowtime)
2014-03-13 11:16:21 +08:00
#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:
rebuild_jobs(build)
2014-03-12 15:36:09 +08:00
#print 'subtime',subtime
2014-03-13 11:16:21 +08:00
#kill dead buid
2014-03-12 15:36:09 +08:00
build.stop()
2014-03-13 11:16:21 +08:00
def main():
username = os.environ['JENKINS_ADMIN']
password = os.environ['JENKINS_ADMIN_PW']
2014-03-13 11:16:21 +08:00
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'
2014-03-12 15:36:09 +08:00
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)
2014-03-13 11:16:21 +08:00