#!/usr/bin/python # Header : $Header: /usr/local/cvsroot/shyam/scripts/gard-v2.py,v 1.2 2008/08/27 10:43:01 shyam Exp $ # Purpose : Script to automatically check new Gentoo rsync and distfile mirrors # Author : Shyam Mani # Created : 05 Jul 2008 # Dedicated to robbat2 aka Robin, for sparking off the idea ;) import sys import time import os import datetime import subprocess import logging import check rmaxlag=45 dmaxlag = 5.5 * 3600 relmaxlag = 2 * 3600 HOME = os.getenv('HOME') reportpath = HOME + "/gentoo/mirrors/gard/reports" bugurl = "http://bugs.gentoo.org/buglist.cgi?query_format=advanced&product=Mirrors&status_whiteboard_type=allwordssubstr&status_whiteboard=due&bug_status=ASSIGNED&columnlist=bug_id,opendate,bug_status,resolution,short_desc,status_whiteboard&ctype=csv" fname = HOME + "/gentoo/open-mirror-bugs" logformat = '%(levelname)s:\t%(message)s' logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=logformat) def add_handler(logfile): file = logging.FileHandler(logfile) file.setLevel(logging.INFO) formatter = logging.Formatter(logformat) file.setFormatter(formatter) logging.getLogger('').addHandler(file) return file def remove_handler(handler): logging.getLogger('').removeHandler(handler) retcode5=subprocess.call(['/usr/bin/wget','-q','-T 30', '-O', fname, bugurl]) file=open(fname,'r') logging.debug("Initializing G(entoo)A(utomated)R(sync)D(istfiles) checker script, version 2 (Katrina) - kicking mirror ass for Gentoo") # Format of CSV = 234083,"2008-08-06 13:01:32","ASSIGNED",,"[rsync,distfiles|all] New mirror Cambrium BV (The Netherlands)","due:2008/09/01;dist:http://mirror.cambrium.nl/pub/os/linux/gentoo/|ftp://mirror.cambrium.nl/pub/os/linux/gentoo/|rsync://mirror.cambrium.nl/gentoo/;portage:mirror.cambrium.nl" for line in file: line=line.replace("\n","") data=line.split(',') bugnum=data[0] swboard=data[5] if bugnum == "bug_id": continue else: details=swboard.split(';') duedate=details[0] duedate=duedate.replace("\"","") #print duedate urls=details[1].replace("dist:","") port=details[2].split(':') grsync=port[1].replace("\"","") temp=urls.split('|') http=temp[0] ftp=temp[1] drsync=temp[2] #print http #print ftp #print drsync #print grsync if len(http) > 0: boo=http.split('/') addy=boo[2] elif len(ftp) > 0: boo=ftp.split('/') addy=boo[2] else: addy=grsync #hdomain='/'+addy[2] if len(grsync) > 0: fname=reportpath+"/"+grsync if os.path.isdir(reportpath): pass else: os.makedirs(reportpath) handler = add_handler(fname) info4=grsync+" wants to be a gentoo-portage mirror and is being checked as per Bug #"+bugnum logging.info(info4) target=grsync+"::gentoo-portage/metadata/timestamp.chk" retcode=subprocess.call(['rsync','-aqP','--no-motd','--contimeout=30',target,'.']) if retcode > 0: logging.error("return value of rsync during gentoo-portage check was "+str(retcode)) else: temp=open('timestamp.chk','r') ots=temp.readline() temp.close() #Keep timestamp as Fri, 04 Jul 2008 20:45:01, remove the +0000 since there doesn't seem to be a %z in python t1=time.mktime(time.strptime(ots.replace(" +0000",""), "%a, %d %b %Y %H:%M:%S ")) lag=(time.mktime(time.gmtime())-t1)/60 if lag > rmaxlag: error4 = "gentoo-portage mirror is lagging by over 40 mins, lag is : "+str(round(lag,2))+" mins" tstamp4 = "Timestamp on Mirror : "+str(ots).strip() curtime4 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) bugurl4 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate logging.error(error4) logging.info(tstamp4) logging.info(curtime4) logging.info(bugurl4) else: info4 = grsync+" is in sync, current delta : "+str(round(lag,2))+" mins" tstamp4 = "Timestamp on Mirror : "+str(ots) curtime4= "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) bugurl4 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate logging.info(info4) logging.info(tstamp4) logging.info(curtime4) logging.info(bugurl4) os.unlink('timestamp.chk') # New line for the prettiness logging.info('') remove_handler(handler) if len(http) > 0: fname=reportpath+"/"+addy if os.path.isdir(reportpath): pass else: os.makedirs(reportpath) handler = add_handler(fname) info5 = addy+" wants to be distfiles mirror and is being checked as per Bug #"+bugnum+ " over http" logging.info(info5) synced = True if check.DistfilesCheck(http).check(dmaxlag) is not True: synced = False if check.ReleasesCheck(http).check(relmaxlag) is not True: synced = False if synced: logging.info(addy + ' is in sync') else: logging.info(addy + ' is out of sync') bugurl1 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate logging.info(bugurl1) # New line for the prettiness logging.info('') remove_handler(handler) if len(ftp) > 0: fname=reportpath+"/"+addy if os.path.isdir(reportpath): pass else: os.makedirs(reportpath) add_handler(fname) info6 = addy+" wants to be an distfiles mirror and is being checked as per Bug #"+bugnum+ " over ftp" logging.info(info6) synced = True if check.DistfilesCheck(ftp).check(dmaxlag) is not True: synced = False if check.ReleasesCheck(ftp).check(relmaxlag) is not True: synced = False if synced: logging.info(addy + ' is in sync') else: logging.info(addy + ' is out of sync') bugurl1 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate logging.info(bugurl1) # New line for the prettiness logging.info('') remove_handler(handler) if len(drsync) > 0: fname=reportpath+"/"+addy if os.path.isdir(reportpath): pass else: os.makedirs(reportpath) add_handler(fname) info7 = addy+" wants to be an distfiles mirror and is being checked as per Bug #"+bugnum+ " over rsync" logging.info(info7) target=drsync+"/distfiles/timestamp.chk" retcode4=subprocess.call(['rsync','-aqP','--no-motd','--contimeout=30',target,'.']) if retcode4 > 0: logging.error("return value of rsync during distfiles check was "+str(retcode4)) else: temp=open('timestamp.chk','r') ots=temp.readline() temp.close() t1=time.mktime(time.strptime(ots.replace(" +0000",""), "%a, %d %b %Y %H:%M:%S ")) lag=(time.mktime(time.gmtime())-t1)/(60*60) if lag > dmaxlag: error7 = "Mirror is lagging by over 5.5 hours, difference is : "+str(round(lag,2))+" hours" tstamp7 = "Timestamp on Mirror : "+str(ots).strip() curtime7= "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) bugurl7 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate logging.error(error7) logging.info(tstamp7) logging.info(curtime7) logging.info(bugurl7) else: info7 = addy+" is in sync, current lag : "+str(round(lag,2))+" hours" tstamp7 = "Timestamp on Mirror : "+str(ots) curtime7 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) bugurl7 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate logging.info(info7) logging.info(tstamp7) logging.info(curtime7) logging.info(bugurl7) os.unlink('timestamp.chk') # New line for the prettiness logging.info('') remove_handler(handler)