Skip to content

Instantly share code, notes, and snippets.

@bobpattersonjr
Created May 5, 2011 19:06
Show Gist options
  • Save bobpattersonjr/957672 to your computer and use it in GitHub Desktop.
Save bobpattersonjr/957672 to your computer and use it in GitHub Desktop.
php error and warning logging parsing script to use with logster
import time
import re
from logster_helper import MetricObject, LogsterParser
from logster_helper import LogsterParsingException
class phplogster(LogsterParser):
def __init__(self):
'''Initialize any data structures or variables needed for keeping track
of the tasty bits we find in the log we are parsing.'''
self.php_error = 0
self.php_warning = 0
# Regular expression for matching lines we are interested in, and capturing
# fields from the line (in this case, http_status_code).
self.reg = re.compile('.*error.*')
self.reg_warn = re.compile('.*warning.*')
def parse_line(self, line):
'''This function should digest the contents of one line at a time, updating
object's state variables. Takes a single argument, the line to be parsed.'''
try:
# Apply regular expression to each line and extract interesting bits.
regMatch = self.reg.match(line)
if regMatch:
linebits = regMatch.groupdict()
self.php_error += 1
else:
regMatchWarn = self.reg_warn(line)
if regMatchWarn:
self.php_warning += 1
else:
raise LogsterParsingException, "regmatch failed to match"
except Exception, e:
raise LogsterParsingException, "regmatch or contents failed with %s" % e
def get_state(self, duration):
'''Run any necessary calculations on the data collected from the logs
and return a list of metric objects.'''
self.duration = duration
# Return a list of metrics objects
return [
MetricObject("php_error", (self.php_error / self.duration), "Responses per sec"),
MetricObject("php_warning", (self.php_warning / self.duration), "Responses per sec"),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment