#!/usr/bin/python --
## datalogfile.py
##
## Dru Nelson <dru@egroups.com>
## 3-25-99
## 07-08-99 - Major change to add headers
'''
datalogfile.py -- object for handling log data files
A data-logfile will automatically check the date and remember what
logfile it was using. This module relies on being run from a single
process, it cannot handle concurrency
''' # '
import os, sys, socket, string
import time
class DataLogFile:
def __init__ (self, report, moduleVersion, fields):
self.version = '1.0'
self.host = report['host']
self.entity = report['entity']
self.subentity = report['subEntity']
self.module = report['entity']
self.moduleversion = moduleVersion
self.fields = fields
self.file = None
self.yearMonthDay = ()
self.doLogCheck()
def doLogCheck(self):
# Get YearMonthDay and open a log file for this entity
year, month, day, hour, min, sec, dayofweek, dayofyear, x = time.localtime(time.time())
mydate = year, month, day,
if self.yearMonthDay != mydate:
self.rotate()
def rotate (self):
if self.file != None:
self.file.close()
# Get time info
year, month, day, hour, min, sec, dayofweek, dayofyear, x = time.localtime(time.time())
# Insure 'yearMonth' directory exists
if not os.path.exists('CollectedData/%s%02d' %(year, month)):
os.mkdir('CollectedData/%s%02d' %(year,month))
# Insure 'day' directory exists
if not os.path.exists('CollectedData/%s%02d/%02d' %(year, month, day)):
os.mkdir('CollectedData/%s%02d/%02d' %(year, month, day))
# Insure 'host' directory exists
if not os.path.exists('CollectedData/%s%02d/%02d/%s' %(year, month, day, self.host)):
os.mkdir('CollectedData/%s%02d/%02d/%s' %(year, month, day, self.host))
# Define the filename for convenience
z = 'CollectedData/%s%02d/%02d/%s/%s%02d%02d.%s_%s_%s' %(year, month, day, self.host, year, month, day, self.host, self.entity, self.subentity)
# If the file doesn't exist
if not os.path.exists('%s.data' %(z)):
self.file = open('%s.tmp' %(z), 'w')
self.file.write('#QOS %s\n' %self.version)
self.file.write('#Module: %s %s\n' %(self.module, self.moduleversion))
self.file.write('#Fields: ')
for f in self.fields:
self.file.write('%s, ' %f)
self.file.write('\n')
self.file.close()
os.rename('%s.tmp' %z, '%s.data' %z)
# Now open the file for writes
self.file = open('%s.data' %(z), 'a')
self.yearMonthDay = year, month, day
def writeState (self, state, line):
year, month, day, hour, min, sec, dayofweek, dayofyear, x = time.localtime(time.time())
# Y M D H M S ST L
self.file.write('%s %02d %02d %02d %02d %02d %s %s\n' % (year, month, day, hour, min, sec, state, line) )
self.file.close()