Commit d1a0393e authored by Claudemir Todo Bom's avatar Claudemir Todo Bom
Browse files

* fix issues with "alone" values

parent 87b02268
......@@ -6,28 +6,33 @@ import time
import os.path
from math import ceil,floor
from MarkerOnTimeCounter import *
from datetime import datetime
def getTimeStamp( timestamp , secs ):
return int( (floor(time.mktime(time.strptime(timestamp, "%Y-%m-%d %H:%M:%S"))/secs)+1) * secs)
def loadStatus( filename ):
if not os.path.isfile(filename):
status={'position': 0, 'lastupdate': 0}
else:
with open( self.statusFile , 'r' ) as file:
status = simplejson.loads(file.read())
return status
def loadConfig( filename ):
with open( filename , 'r' ) as file:
config = simplejson.loads(file.read().replace('\n', ' ').replace('\t', ' '))
return config
class LogDigest:
def __init__ ( self , confFile , statusFile ):
self.statusFile = statusFile
self.confFile = confFile
def __init__ ( self , config , status ):
self.compiledMatchers={}
self.status={}
with open( self.confFile , 'r' ) as file:
self.conf = simplejson.loads(file.read().replace('\n', ' ').replace('\t', ' '))
for matcherKey in self.conf["matchers"]:
self.compiledMatchers[matcherKey] = re.compile(self.conf["matchers"][matcherKey])
self.status=status
self.conf=config
if not os.path.isfile(statusFile):
self.status={'position': 0}
else:
with open( self.statusFile , 'r' ) as file:
self.status = simplejson.loads(file.read())
for matcherKey in self.conf["matchers"]:
self.compiledMatchers[matcherKey] = re.compile(self.conf["matchers"][matcherKey])
self.counters=MarkerOnTimeCounter()
self.nowLimit = self.conf["secsloop"]*floor(time.mktime(time.localtime())/self.conf["secsloop"])
......@@ -70,8 +75,9 @@ class LogDigest:
def execute ( self ):
try:
if 'lastupdate' in self.status and os.path.getmtime(self.conf["filename"]+".1") > self.status["lastupdate"]:
logFile = self.openfile(self.conf["filename"]+".1", self.status["position"])
rotatedFile=self.conf["filename"]+".1"
if 'lastupdate' in self.status and os.path.isfile(rotatedFile) and os.path.getmtime(rotatedFile) > self.status["lastupdate"]:
logFile = self.openfile(rotatedFile, self.status["position"])
self.readfile(logFile)
self.status["position"] = 0
finally:
......@@ -80,6 +86,6 @@ class LogDigest:
self.status["lastupdate"] = self.nowLimit
def savestatus( self ):
with open (self.statusFile , 'w') as file:
def savestatus( self , statusFile ):
with open (statusFile , 'w') as file:
file.write(simplejson.dumps(self.status, sort_keys=True))
......@@ -4,6 +4,7 @@ from pyrrd import rrd
from pyrrd.graph import Graph , DEF , CDEF , AREA , GPRINT , LINE
from LogDigest import *
import os.path
from datetime import datetime
def prepareRRD( filename , conf , start = None , create = False ):
dataSources=[]
......@@ -28,10 +29,14 @@ if len(sys.argv) < 3:
sys.stderr.write("invalid arguments")
sys.exit(1)
confile = sys.argv[1]
statusfile = sys.argv[2]
configFile = sys.argv[1]
statusFile = sys.argv[2]
ld = LogDigest(confile,statusfile)
status = loadStatus(statusFile)
config = loadConfig(configFile)
lastupdate = status["lastupdate"]
ld = LogDigest(config,status)
rrdfile = ld.conf["rrd"]
start = None
......@@ -47,19 +52,25 @@ ld.execute()
templateArray = ld.conf["matchers"].keys()
templateString = ""
tstarted = False
zeroes = ""
divider = ""
secsloop = int(config["secsloop"])
for t in templateArray:
if tstarted:
templateString = templateString + ":"
else:
tstarted = True
templateString = templateString + t
templateString = templateString + divider + t
zeroes = zeroes + divider + "0"
divider = ":"
haveUpdate = False
for k in ld.counters:
buffervalues = ""
divider = ""
if int(k)-secsloop > int(lastupdate):
myRRD.bufferValue( int(k)-secsloop , zeroes )
lastupdate = k
for ds in templateArray:
if ds in ld.counters[k]:
value = str(ld.counters[k][ds])
......@@ -72,9 +83,12 @@ for k in ld.counters:
haveUpdate = True
if haveUpdate:
myRRD.update( template = templateString )
ret = myRRD.update( template = templateString )
if ret:
print rrdtool.error()
ld.savestatus()
ld.savestatus(statusFile)
if "graph" in ld.conf:
defs = []
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment