]> arthur.barton.de Git - netdata.git/blob - python.d/gunicorn_log.chart.py
Merge pull request #1589 from l2isbad/varnish_plugin
[netdata.git] / python.d / gunicorn_log.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: nginx log netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
4 # Modified for Gunicorn by: Jeff Willette (deltaskelta)
5
6 from base import LogService 
7 import re
8
9 priority = 60000
10 retries = 60
11 # update_every = 3
12
13 ORDER = ['codes']
14 CHARTS = {
15     'codes': {
16         'options': [None, 'gunicorn status codes', 'requests/s', 'requests', 'gunicorn_log.codes', 'stacked'],
17         'lines': [
18             ["2xx", None, "incremental"],
19             ["3xx", None, "incremental"],
20             ["4xx", None, "incremental"],
21             ["5xx", None, "incremental"]
22         ]}
23 }
24
25
26 class Service(LogService):
27     def __init__(self, configuration=None, name=None):
28         LogService.__init__(self, configuration=configuration, name=name)
29         if len(self.log_path) == 0:
30             self.log_path = "/var/log/gunicorn/access.log"
31         self.order = ORDER
32         self.definitions = CHARTS
33         pattern = r'" ([0-9]{3}) '
34         #pattern = r'(?:" )([0-9][0-9][0-9]) ?'
35         self.regex = re.compile(pattern)
36
37     def _get_data(self):
38         """
39         Parse new log lines
40         :return: dict
41         """
42         data = {'2xx': 0,
43                 '3xx': 0,
44                 '4xx': 0,
45                 '5xx': 0}
46         try:
47             raw = self._get_raw_data()
48             if raw is None:
49                 return None
50             elif not raw:
51                 return data
52         except (ValueError, AttributeError):
53             return None
54
55         regex = self.regex
56         for line in raw:
57             code = regex.search(line)
58             try:
59                 beginning = code.group(1)[0]
60             except AttributeError:
61                 continue
62
63             if beginning == '2':
64                 data["2xx"] += 1
65             elif beginning == '3':
66                 data["3xx"] += 1
67             elif beginning == '4':
68                 data["4xx"] += 1
69             elif beginning == '5':
70                 data["5xx"] += 1
71
72         return data