forked from etsy/logster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleGraphiteLogster.py
More file actions
91 lines (75 loc) · 3.32 KB
/
Copy pathSampleGraphiteLogster.py
File metadata and controls
91 lines (75 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
### A sample logster parser file that can be used to count the number
### of response codes found in an Apache access log.
###
### For example:
### sudo ./logster --dry-run SampleGraphiteLogster /var/log/httpd/access_log
###
###
### Copyright 2011, Etsy, Inc.
###
### This file is part of Logster.
###
### Logster is free software: you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation, either version 3 of the License, or
### (at your option) any later version.
###
### Logster is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with Logster. If not, see <http://www.gnu.org/licenses/>.
###
import time
import re
from logster_helper import GraphiteMetricObject, GraphiteLogster
from logster_helper import LogsterParsingException
class SampleGraphiteLogster(GraphiteLogster):
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.http_1xx = 0
self.http_2xx = 0
self.http_3xx = 0
self.http_4xx = 0
self.http_5xx = 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('.*HTTP/1.\d\" (?P<http_status_code>\d{3}) .*')
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()
status = int(linebits['http_status_code'])
if (status < 200):
self.http_1xx += 1
elif (status < 300):
self.http_2xx += 1
elif (status < 400):
self.http_3xx += 1
elif (status < 500):
self.http_4xx += 1
else:
self.http_5xx += 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 [
GraphiteMetricObject("http_1xx", (self.http_1xx / self.duration)),
GraphiteMetricObject("http_2xx", (self.http_2xx / self.duration)),
GraphiteMetricObject("http_3xx", (self.http_3xx / self.duration)),
GraphiteMetricObject("http_4xx", (self.http_4xx / self.duration)),
GraphiteMetricObject("http_5xx", (self.http_5xx / self.duration)),
]