diff options
-rw-r--r-- | check.py | 108 |
1 files changed, 70 insertions, 38 deletions
@@ -9,22 +9,16 @@ class GardCheck: def __init__(self, url): self.url = url - # Overridde in child classes - def check(self): - return True - - def humanize_time(self, secs): - 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) - - def timestamp_to_secs(self, ts): - return rfc822.mktime_tz(rfc822.parsedate_tz(ts)) + def check_file_exists(self, url): + ret = True + try: + f = urllib2.urlopen(url) + if len(f.read()) == 0: + raise IOError + except: + ret = False -class DistCheck(GardCheck): - # can only be used from Python 2.6 onwards - HTTP_TIMEOUT = 30 + return ret # Takes the URL to a timestamp.{chk|x} file and returns the # corresponding time stamp in seconds @@ -42,48 +36,86 @@ class DistCheck(GardCheck): # timestamp.x format? ts = float(date.split(' ')[0]) except: - print 'ERROR: Could not get timestamp from ' + url return None return ts - def _check_file_exists(self, url): - ret = True - try: - f = urllib2.urlopen(url) - if len(f.read()) == 0: - raise IOError - except: - ret = False + def get_lag(self, path, verbose=False): + ts = self._get_timestamp_from_url(self.url + path) + now = time.mktime(time.gmtime()) + if ts is None or now < ts: + return None + return now - ts - return ret + def humanize_time(self, secs): + 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) + + def timestamp_to_secs(self, ts): + return rfc822.mktime_tz(rfc822.parsedate_tz(ts)) + + # Override these in child classes + + def check(self): + return True - def check(self, maxdlag, maxrlag, verbose=False): + def lag(self): + return None + +# Check distfiles mirrors +class DistfilesCheck(GardCheck): + def lag(self): + path = '/distfiles/timestamp.chk' + return self.get_lag(path) + + def check(self, maxlag, verbose=False): # XXX: Replace 'verbose' with a logger object - ret = True - # Verify that distfiles aren't lagging - now = time.mktime(time.gmtime()) - ts = self._get_timestamp_from_url(self.url+'/distfiles/timestamp.chk') - if ts is not None and ((now - ts) > maxdlag or (now - ts) < 0): + lag = self.lag() + + if lag is None: + print '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 - delta is %s' \ - % (self.url, self.humanize_time(now - ts)) + print 'ERROR: distfiles at %s is lagging\n\tdelta is %s' \ + % (self.url, self.humanize_time(lag)) ret = False + else: + ret = True + return ret + +# Check releases mirrors +class ReleasesCheck(GardCheck): + def lag(self): + path = '/releases/.test/timestamp.x' + return self.get_lag(path) + + def check(self, maxlag, verbose=False): # Verify that releases aren't lagging - ts = self._get_timestamp_from_url(self.url+'/releases/.test/timestamp.x') - if ts is not None and ((now - ts) > maxrlag or (now - ts) < 0): + + lag = self.lag() + + if lag is None: + print '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 - delta is %s' \ - % (self.url, self.humanize_time(now - ts)) + print 'ERROR: releases at %s is lagging\n\tdelta is %s' \ + % (self.url, self.humanize_time(lag)) ret = False + else: + ret = True # 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 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 ret = False return ret + |