Filter:   InfoImg
download rangefind.py
Language: Python
LOC: 73
Project Info
QOS
Server: SourceForge
Type: cvs
[Show Code]



[Show Code]
...ge\q\qos\qos\qos\qosserver\
   auth_handler.py
   BaseObject.py
   collector.py
   ConfData.db.orig
   config_server.py
   counter.py
   DataLogFile.py
   default_handler.py
   downtimer.py
   englishUnits.py
   entityHeaders.py
   entityProps.py
   event_loop.py
   http_date.py
   http_server.py
   log.py
   logger.py
   m_syslog.py
   medusa_gif.py
   monitor.py
   normalDate.py
   pop.py
   Process.py
   producers.py
   qos.db.seed
   qos_server.py
   qosreport.py
   qosserver.init
   rangefind.py
   report_server.py
   reportData.py
   reportDowntime.py
   reportMultiDowntime.py
   reportMultiplot.py
   rpc_server.py
   sendmail.py
   StateDb.py
   status_handler.py

#!/usr/bin/python
#
# rangefind.py: QOS log range extractor
#
# Doug White
import sys
import os
import string
import time
import normalDate

QOS_COLLECTEDDATA = '/usr/local/qosserver/CollectedData'

class RangeFind:
    def __init__(self, agent='', entity='', startdate=None, starttime=None, enddate=None, endtime=None, datadir=QOS_COLLECTEDDATA):
        # note that starttime and endtime are normalDate objects
        self.startdate = startdate
        self.starttime = starttime
        self.enddate = enddate
        self.endtime = endtime
        self.agent = agent
        self.entity = entity
        self.datadir = datadir
        self.timezone = time.strftime("%Z", time.localtime(time.time()))
        print "Using timezone: %s" % self.timezone

    # Gank the log lines as spec'd above
    def extract(self):
        daylist = []
        daylist = self.startdate.range(self.enddate - self.startdate)
        daylist.append(self.enddate)

        if len(daylist) == 0:
            raise ValueError, "Why is daylist empty?"

        # At this point we're ready to slurp files. We special-case the
        # first and last files since these need to be cut down to the date
        # range given. The files for the days in between don't need filtering.
        # This speeds things up dramatically :)

        datalines = []

        # Slurp the start day file
        stime = time.time()
        datalines.extend(self.date_filter_file(daylist[0]))
        etime = time.time()
        print "First date filter split: %s" % (etime - stime)
        stime = time.time()
        if len(daylist) > 2:
            # Grab the unfiltered files in the middle of the range
            for day in daylist[1:-1]:
                try:
                    datafile = open(self.build_log_name(day))
                    datalines.extend(datafile.readlines())
                    datafile.close()
                except:
                    print "Error opening file %s" % self.build_log_name(day)
                    pass
        etime = time.time()
        print "Center file slurp split: %s" % (etime-stime)
        # Filter the final file
        stime = time.time()
        if len(daylist) > 1:
            datalines.extend(self.date_filter_file(daylist[-1]))
        etime = time.time()
        print "Last date filter split: %s" % (etime - stime)
        # datalines now has all the QOS log files for the given range.
        return datalines


    ###### UTILITY
    # Given a NormalDate object and the entity, construct the path to the QOS
    # logfile. Could be rewritten as a string construction.
    def build_log_name(self, day):
        return os.path.join(QOS_COLLECTEDDATA,
                            str(day.year()) + repr(day)[-4:-2],
                            repr(day)[-2:],
                            self.agent,
                            repr(day) + '.' + self.agent + '_' + self.entity + '.data')

    def date_filter_file(self, day):

        datalines = []

        dayfile = self.build_log_name(day)
        try:
            datafile = open(dayfile)
            inlines = datafile.readlines()
        except:
            print "Error opening file %s" % dayfile
            return datalines

        # could probably write our own comment stripper in readlines()
        for ent in inlines :
            if ent[0] == '#' :
                continue
            checktime = self.log_time(ent)
            if checktime <= self.starttime or checktime >= self.endtime :
                continue
            datalines.append(ent)

        datafile.close()
        print "Done with %s" % dayfile
        return datalines

    def log_time(self, line):
        # line=line[:19] + " " + time.strftime("%Z", time.localtime(time.time()))
        #print line
        #return time.mktime(time.strptime("%s %s %s %s %s %s" % tuple(string.split(line)[:6]), "%Y %m %d %H %M %S %Z"))
        #return time.mktime(time.strptime(line[:19] + " " + self.timezone, "%Y %m %d %H %M %S %Z"))
        return time.mktime(time.strptime(line[:19], "%Y %m %d %H %M %S"))