]> arthur.barton.de Git - netdata.git/commitdiff
add nginx_log module
authorpaulfantom <paulfantom@gmail.com>
Sun, 14 Aug 2016 13:17:20 +0000 (15:17 +0200)
committerpaulfantom <paulfantom@gmail.com>
Sun, 14 Aug 2016 13:17:20 +0000 (15:17 +0200)
conf.d/Makefile.am
conf.d/python.d.conf
conf.d/python.d/nginx_log.conf [new file with mode: 0644]
python.d/Makefile.am
python.d/nginx_log.chart.py [new file with mode: 0644]

index 6be4945e6c440189345c98af54af5d5ba4d7568b..f628a042bee0049dbe36583cdfb9206b19fea0ce 100644 (file)
@@ -30,6 +30,7 @@ dist_pythonconfig_DATA = \
        python.d/memcached.conf \
        python.d/mysql.conf \
        python.d/nginx.conf \
+       python.d/nginx_log.conf \
        python.d/phpfpm.conf \
        python.d/postfix.conf \
        python.d/redis.conf \
index a0c809776ca5838180a5dbf02b5e0891e520ce74..940bd918345b6c9cdcae2ac1e6fc14760dace610 100644 (file)
@@ -33,6 +33,7 @@ example: no
 # memcached: yes
 # mysql: yes
 # nginx: yes
+# nginx_log: yes
 # phpfpm: yes
 # postfix: yes
 # redis: yes
diff --git a/conf.d/python.d/nginx_log.conf b/conf.d/python.d/nginx_log.conf
new file mode 100644 (file)
index 0000000..6a53c52
--- /dev/null
@@ -0,0 +1,72 @@
+# netdata python.d.plugin configuration for nginx log
+#
+# This file is in YaML format. Generally the format is:
+#
+# name: value
+#
+# There are 2 sections:
+#  - global variables
+#  - one or more JOBS
+#
+# JOBS allow you to collect values from multiple sources.
+# Each source will have its own set of charts.
+#
+# JOB parameters have to be indented (using spaces only, example below).
+
+# ----------------------------------------------------------------------
+# Global Variables
+# These variables set the defaults for all JOBs, however each JOB
+# may define its own, overriding the defaults.
+
+# update_every sets the default data collection frequency.
+# If unset, the python.d.plugin default is used.
+# update_every: 1
+
+# priority controls the order of charts at the netdata dashboard.
+# Lower numbers move the charts towards the top of the page.
+# If unset, the default for python.d.plugin is used.
+# priority: 60000
+
+# retries sets the number of retries to be made in case of failures.
+# If unset, the default for python.d.plugin is used.
+# Attempts to restore the service are made once every update_every
+# and only if the module has collected values in the past.
+# retries: 5
+
+# ----------------------------------------------------------------------
+# JOBS (data collection sources)
+#
+# The default JOBS share the same *name*. JOBS with the same name
+# are mutually exclusive. Only one of them will be allowed running at
+# any time. This allows autodetection to try several alternatives and
+# pick the one that works.
+#
+# Any number of jobs is supported.
+#
+# All python.d.plugin JOBS (for all its modules) support a set of
+# predefined parameters. These are:
+#
+# job_name:
+#     name: myname     # the JOB's name as it will appear at the
+#                      # dashboard (by default is the job_name)
+#                      # JOBs sharing a name are mutually exclusive
+#     update_every: 1  # the JOB's data collection frequency
+#     priority: 60000  # the JOB's order on the dashboard
+#     retries: 5       # the JOB's number of restoration attempts
+#
+# Additionally to the above, nginx_log also supports the following:
+#
+#     path: 'PATH'     # the path to nginx's access.log
+#
+
+# ----------------------------------------------------------------------
+# AUTO-DETECTION JOBS
+# only one of them will run (they have the same name)
+
+nginx_log:
+  name: 'local'
+  path: '/var/log/nginx/access.log'
+
+nginx_log2:
+  name: 'local'
+  path: '/var/log/nginx/nginx-access.log'
index 57d89bad251ed1510bcd6d573f83c92db5d68052..8bccba3780d706fce07c682c030b311ff484dd72 100644 (file)
@@ -19,6 +19,7 @@ dist_python_SCRIPTS = \
        memcached.chart.py \
        mysql.chart.py \
        nginx.chart.py \
+       nginx_log.chart.py \
        phpfpm.chart.py \
        postfix.chart.py \
        redis.chart.py \
diff --git a/python.d/nginx_log.chart.py b/python.d/nginx_log.chart.py
new file mode 100644 (file)
index 0000000..ea9f10b
--- /dev/null
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+# Description: nginx log netdata python.d module
+# Author: Pawel Krupa (paulfantom)
+
+from base import LogService
+import re
+
+priority = 60000
+retries = 60
+# update_every = 3
+
+ORDER = ['codes']
+CHARTS = {
+    'codes': {
+        'options': [None, 'nginx status codes', 'requests/s', 'requests', 'nginx_log.codes', 'stacked'],
+        'lines': [
+            ["20X", None, "incremental"],
+            ["30X", None, "incremental"],
+            ["40X", None, "incremental"],
+            ["50X", None, "incremental"]
+        ]}
+}
+
+
+class Service(LogService):
+    def __init__(self, configuration=None, name=None):
+        LogService.__init__(self, configuration=configuration, name=name)
+        if len(self.log_path) == 0:
+            self.log_path = "/var/log/nginx/access.log"
+        self.order = ORDER
+        self.definitions = CHARTS
+        pattern = r'" ([0-9]{3}) ?'
+        #pattern = r'(?:" )([0-9][0-9][0-9]) ?'
+        self.regex = re.compile(pattern)
+
+    def _get_data(self):
+        """
+        Parse new log lines
+        :return: dict
+        """
+        data = {'20X': 0,
+                '30X': 0,
+                '40X': 0,
+                '50X': 0}
+        try:
+            raw = self._get_raw_data()
+            if raw is None:
+                return None
+            elif not raw:
+                return data
+        except (ValueError, AttributeError):
+            return None
+
+        regex = self.regex
+        for line in raw:
+            code = regex.search(line)
+            beginning = code.group(1)[0]
+
+            if beginning == '2':
+                data["20X"] += 1
+            elif beginning == '3':
+                data["30X"] += 1
+            elif beginning == '4':
+                data["40X"] += 1
+            elif beginning == '5':
+                data["50X"] += 1
+
+        return data
+