From 003105fc5ec82596add3587aa9d9d4f71c19d6bb Mon Sep 17 00:00:00 2001 From: Arun Raghavan Date: Sun, 21 Dec 2008 18:10:22 +0530 Subject: Make GARD uses the GardCheck classes for checking. Switched the classes over to 'logging' so there shouldn't be any stray prints around now. --- check.py | 44 ++++++++++++---------- gard-v2.py | 124 ++++++++++++++++--------------------------------------------- 2 files changed, 57 insertions(+), 111 deletions(-) diff --git a/check.py b/check.py index bf76a90..30a548c 100644 --- a/check.py +++ b/check.py @@ -1,13 +1,26 @@ #!/usr/bin/python +import logging import urllib2 import time import rfc822 class GardCheck: # Base class which provides some helper functions - def __init__(self, url): + def __init__(self, url, log=True): self.url = url + if log: + self.logger = logging.getLogger('') + else: + self.logger = None + + def log_info(self, msg): + if self.logger is not None: + self.logger.info(msg) + + def log_error(self, msg): + if self.logger is not None: + self.logger.error(msg) def check_file_exists(self, url): ret = True @@ -40,7 +53,7 @@ class GardCheck: return ts - def get_lag(self, path, verbose=False): + def get_lag(self, path): ts = self._get_timestamp_from_url(self.url + path) now = time.mktime(time.gmtime()) if ts is None or now < ts: @@ -51,7 +64,7 @@ class GardCheck: mins, secs = divmod(secs, 60) hours, mins = divmod(mins, 60) days, hours = divmod(hours, 24) - return '%02d d %02dh %02d m %02d s' % (days, hours, mins, secs) + return '%02dd %02dh %02dm %02ds' % (days, hours, mins, secs) def timestamp_to_secs(self, ts): return rfc822.mktime_tz(rfc822.parsedate_tz(ts)) @@ -70,18 +83,15 @@ class DistfilesCheck(GardCheck): path = '/distfiles/timestamp.chk' return self.get_lag(path) - def check(self, maxlag, verbose=False): - # XXX: Replace 'verbose' with a logger object - + def check(self, maxlag): lag = self.lag() if lag is None: - print 'ERROR: Could not get distfiles timestamp for ' + self.url + self.log_error('Could not get distfiles timestamp for ' + self.url) ret = False elif lag > maxlag: - if verbose is True: - print 'ERROR: distfiles at %s is lagging\n\tdelta is %s' \ - % (self.url, self.humanize_time(lag)) + self.log_error('Distfiles at %s is lagging (delta is %s)' \ + % (self.url, self.humanize_time(lag))) ret = False else: ret = True @@ -94,18 +104,15 @@ class ReleasesCheck(GardCheck): path = '/releases/.test/timestamp.x' return self.get_lag(path) - def check(self, maxlag, verbose=False): - # Verify that releases aren't lagging - + def check(self, maxlag): lag = self.lag() if lag is None: - print 'ERROR: Could not get releases timestamp for ' + self.url + self.log_error('Could not get releases timestamp for ' + self.url) ret = False elif lag > maxlag: - if verbose is True: - print 'ERROR: releases at %s is lagging\n\tdelta is %s' \ - % (self.url, self.humanize_time(lag)) + self.log_error('Releases at %s is lagging (delta is %s)' \ + % (self.url, self.humanize_time(lag))) ret = False else: ret = True @@ -113,8 +120,7 @@ class ReleasesCheck(GardCheck): # Verify that releases/.test/THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt # is not world readable if self.check_file_exists(self.url+'releases/.test/THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt'): - if verbose is True: - print 'ERROR: THIS-FILE-SHOULD-NOT-BE-PUBLIC.txt is visible on %s' % self.url + self.log_error('ERROR: releases permission check failed on %s' % self.url) ret = False return ret diff --git a/gard-v2.py b/gard-v2.py index fa8f443..85991a3 100755 --- a/gard-v2.py +++ b/gard-v2.py @@ -13,9 +13,11 @@ import datetime import subprocess import logging +import check + rmaxlag=45 -dmaxlag=5.5 -relmaxlag=2 +dmaxlag = 5.5 * 3600 +relmaxlag = 2 * 3600 HOME = os.getenv('HOME') reportpath = HOME + "/gentoo/mirrors/gard/reports" @@ -36,84 +38,6 @@ def add_handler(logfile): def remove_handler(handler): logging.getLogger('').removeHandler(handler) -def mirrorcheck(target): - retcode1=subprocess.call(['/usr/bin/wget','-q','-T 30',target]) - if retcode1 > 0: - logging.error("return value of wget during mirrorcheck was "+str(retcode1)) - return - 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*60) - gmt=time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) - if lag > dmaxlag: - 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" - tstamp1 = "Timestamp on Mirror : "+str(ots) - 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: - 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: - logging.error("return value of wget for release timestamp check was "+str(retcode3)) - return - else: - temp=open('timestamp.x','r') - ots=temp.readline() - temp.close() - ts=ots.split(' ') - nots=ts[1]+" "+ts[2]+" "+ts[3]+" "+ts[4]+" "+ts[5]+" " - #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(nots, "%a, %d %b %Y %H:%M:%S ")) - 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 = "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" - 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') logging.debug("Initializing G(entoo)A(utomated)R(sync)D(istfiles) checker script, version 2 (Katrina) - kicking mirror ass for Gentoo") @@ -203,12 +127,20 @@ for line in file: 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) + + 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) @@ -221,12 +153,20 @@ for line in file: 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) + + 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) -- cgit v1.2.3-65-gdbad