diff options
-rwxr-xr-x | gard-v2.py | 226 |
1 files changed, 110 insertions, 116 deletions
@@ -11,22 +11,35 @@ import time import os import datetime import subprocess +import logging rmaxlag=45 dmaxlag=5.5 relmaxlag=2 -reportpath="/home/fox2mike/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/fox2mike/open-mirror-bugs" +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) def mirrorcheck(target): retcode1=subprocess.call(['/usr/bin/wget','-q','-T 30',target]) if retcode1 > 0: - error1 = "*ERROR* : return value of wget during mirrorcheck was "+str(retcode1) - report.write(error1) - #report.close() - print error1 + logging.error("return value of wget during mirrorcheck was "+str(retcode1)) return else: temp=open('timestamp.chk','r') @@ -37,47 +50,37 @@ def mirrorcheck(target): lag=(time.mktime(time.gmtime())-t1)/(60*60) gmt=time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) if lag > dmaxlag: - error1 = "*ERROR* : distfiles is over 5.5 hours behind, lag is : "+str(round(lag,2))+" hours"+'\n' - tstamp1 = "Timestamp on Mirror : "+str(ots) - curtime1 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))+'\n' - bugurl1 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate+'\n' - report.write(error1) - report.write(tstamp1) - report.write(curtime1) - report.write(bugurl1) - #report.close() - print error1,tstamp1,curtime1,bugurl1 + error1 = "distfiles is over 5.5 hours behind, lag is : "+str(round(lag,2))+" hours" + tstamp1 = "Timestamp on Mirror : "+str(ots).strip() + curtime1 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) + bugurl1 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate + logging.error(error1) + logging.info(tstamp1) + logging.info(curtime1) + logging.info(bugurl1) else: - info1 = addy+" is in sync, current delta : "+str(round(lag,2))+" hours"+'\n' + info1 = addy+" is in sync, current delta : "+str(round(lag,2))+" hours" tstamp1 = "Timestamp on Mirror : "+str(ots) - curtime1 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))+'\n' - bugurl1 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate+'\n' - report.write(info1) - report.write(tstamp1) - report.write(curtime1) - report.write(bugurl1) - #report.close() - print info1,tstamp1,curtime1,bugurl1 + curtime1 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) + bugurl1 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate + logging.info(info1) + logging.info(tstamp1) + logging.info(curtime1) + logging.info(bugurl1) os.unlink('timestamp.chk') return def relpermcheck(target): retcode2=subprocess.call(['/usr/bin/wget','-q','-T 30',target]) if retcode2 < 1: - error2 = "*ERROR* : return value of wget for /releases file perm check was "+str(retcode2)+" which means the file is world accessible!"+'\n' - report.write(error2) - report.close() - print error2 + logging.error("return value of wget for /releases file perm check was "+str(retcode2)+" which means the file is world accessible!") os.unlink('THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt') return def reltscheck(target): retcode3=subprocess.call(['/usr/bin/wget','-q','-T 30',target]) if retcode3 > 0: - error3 = "*ERROR* : return value of wget for release timestamp check was "+str(retcode3)+'\n' - report.write(error3) - #report.close() - print error3 + logging.error("return value of wget for release timestamp check was "+str(retcode3)) return else: temp=open('timestamp.x','r') @@ -90,34 +93,30 @@ def reltscheck(target): lag=(time.mktime(time.gmtime())-t1)/(60*60*24) gmt=time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) if lag > relmaxlag: - error3 = "*ERROR* : releases is over 2 days behind, lag is : "+str(round(lag,2))+" days"+'\n' - tstamp3 = "Timestamp on Mirror : "+nots+'\n' - curtime3 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))+'\n' - bugurl3 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate+'\n' - report.write(error3) - report.write(tstamp3) - report.write(curtime3) - report.write(bugurl3) - #report.close() - print error3,tstamp3,curtime3,bugurl3 + error3 = "releases is over 2 days behind, lag is : "+str(round(lag,2))+" days" + tstamp3 = "Timestamp on Mirror : "+nots + curtime3 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) + bugurl3 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate + logging.error(error3) + logging.info(tstamp3) + logging.info(curtime3) + logging.info(bugurl3) else: - info3 = addy+"'s releases dir is in sync, current delta : "+str(round(lag,2))+" days"+'\n' - tstamp3 = "Timestamp on Mirror : "+nots+'\n' - curtime3 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))+'\n' - bugurl3 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate+'\n' - report.write(info3) - report.write(tstamp3) - report.write(curtime3) - report.write(bugurl3) - #report.close() - print info3,tstamp3,curtime3,bugurl3 + info3 = addy+"'s releases dir is in sync, current delta : "+str(round(lag,2))+" days" + tstamp3 = "Timestamp on Mirror : "+nots + curtime3 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())) + bugurl3 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate + logging.info(info3) + logging.info(tstamp3) + logging.info(curtime3) + logging.info(bugurl3) os.unlink('timestamp.x') return retcode5=subprocess.call(['/usr/bin/wget','-q','-T 30', '-O', fname, bugurl]) file=open(fname,'r') -print "\nInitializing G(entoo)A(utomated)R(sync)D(istfiles) checker script, version 2 (Katrina) - kicking mirror ass for Gentoo \n" +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" @@ -159,17 +158,13 @@ for line in file: pass else: os.makedirs(reportpath) - report=open(fname,'a+') - info4=grsync+" wants to be a gentoo-portage mirror and is being checked as per Bug #"+bugnum+'\n' - report.write(info4) - print info4 + 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: - error4="*ERROR* : return value of rsync during gentoo-portage check was "+str(retcode) - report.write(error4) - report.close() - print error4 + logging.error("return value of rsync during gentoo-portage check was "+str(retcode)) else: temp=open('timestamp.chk','r') ots=temp.readline() @@ -178,76 +173,76 @@ for line in file: 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 = "*ERROR* : gentoo-portage mirror is lagging by over 40 mins, lag is : "+str(round(lag,2))+" mins"+'\n' - tstamp4 = "Timestamp on Mirror : "+str(ots) - curtime4 = "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))+'\n' - bugurl4 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate+'\n' - report.write(error4) - report.write(tstamp4) - report.write(curtime4) - report.write(bugurl4) - report.close() - print error4,tstamp4,curtime4,bugurl4 + 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"+'\n' + 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()))+'\n' - bugurl4 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate+'\n' - report.write(info4) - report.write(tstamp4) - report.write(curtime4) - report.write(bugurl4) - report.close() - print info4,tstamp4,curtime4,bugurl4 + 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) - report=open(fname,'a+') - info5 = addy+" wants to be distfiles mirror and is being checked as per Bug #"+bugnum+ " over http"+'\n' - report.write(info5) - print info5 + handler = add_handler(fname) + info5 = addy+" wants to be distfiles mirror and is being checked as per Bug #"+bugnum+ " over http" + logging.info(info5) disttsdest=http+"/distfiles/timestamp.chk" mirrorcheck(disttsdest) reltsdest=http+"/releases/.test/timestamp.x" reltscheck(reltsdest) relpermdest=http+"/releases/.test/THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt" relpermcheck(relpermdest) + # 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) - report=open(fname,'a+') - info6 = addy+" wants to be an distfiles mirror and is being checked as per Bug #"+bugnum+ " over ftp"+'\n' - report.write(info6) - print info6 + add_handler(fname) + info6 = addy+" wants to be an distfiles mirror and is being checked as per Bug #"+bugnum+ " over ftp" + logging.info(info6) ftpdest=ftp+"/distfiles/timestamp.chk" mirrorcheck(ftpdest) reltsdest=ftp+"/releases/.test/timestamp.x" reltscheck(reltsdest) relpermdest=ftp+"/releases/.test/THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt" relpermcheck(relpermdest) + # 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) - report=open(fname,'a+') - info7 = addy+" wants to be an distfiles mirror and is being checked as per Bug #"+bugnum+ " over rsync"+'\n' - report.write(info7) - print info7 + 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: - error7 = "*ERROR* : return value of rsync during distfiles check was "+str(retcode4) - report.write(error7) - print error7 + logging.error("return value of rsync during distfiles check was "+str(retcode4)) else: temp=open('timestamp.chk','r') ots=temp.readline() @@ -255,25 +250,24 @@ for line in file: 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 = "*ERROR* : Mirror is lagging by over 5.5 hours, difference is : "+str(round(lag,2))+" hours"+'\n' - tstamp7 = "Timestamp on Mirror : "+str(ots) - curtime7= "Current time in UTC : "+str(time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()))+'\n' - bugurl7 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate+'\n' - report.write(error7) - report.write(tstamp7) - report.write(curtime7) - report.write(bugurl7) - report.close() - print error7,tstamp7,curtime7,bugurl7 + 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"+'\n' + 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()))+'\n' - bugurl7 = "Bug URL : https://bugs.gentoo.org/"+bugnum+" , "+duedate+'\n' - report.write(info7) - report.write(tstamp7) - report.write(curtime7) - report.write(bugurl7) - report.close() - print info7,tstamp7,curtime7,bugurl7 + 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) |