#!/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"))