]> arthur.barton.de Git - netdata.git/blob - python.d/nginx_log.chart.py
Merge pull request #1184 from facetoe/postgres_plugin_error_message
[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             ["3xx", None, "incremental"],
19             ["4xx", None, "incremental"],
20             ["5xx", None, "incremental"]
21         ]}
22 }
23
24
25 class Service(LogService):
26     def __init__(self, configuration=None, name=None):
27         LogService.__init__(self, configuration=configuration, name=name)
28         if len(self.log_path) == 0:
29             self.log_path = "/var/log/nginx/access.log"
30         self.order = ORDER
31         self.definitions = CHARTS
32         pattern = r'" ([0-9]{3}) ?'
33         #pattern = r'(?:" )([0-9][0-9][0-9]) ?'
34         self.regex = re.compile(pattern)
35
36     def _get_data(self):
37         """
38         Parse new log lines
39         :return: dict
40         """
41         data = {'2xx': 0,
42                 '3xx': 0,
43                 '4xx': 0,
44                 '5xx': 0}
45         try:
46             raw = self._get_raw_data()
47             if raw is None:
48                 return None
49             elif not raw:
50                 return data
51         except (ValueError, AttributeError):
52             return None
53
54         regex = self.regex
55         for line in raw:
56             code = regex.search(line)
57             try:
58                 beginning = code.group(1)[0]
59             except AttributeError:
60                 continue
61
62             if beginning == '2':
63                 data["2xx"] += 1
64             elif beginning == '3':
65                 data["3xx"] += 1
66             elif beginning == '4':
67                 data["4xx"] += 1
68             elif beginning == '5':
69                 data["5xx"] += 1
70
71         return data
72