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