]> arthur.barton.de Git - netdata.git/commitdiff
freeradius plugin added
authorlgz <lgz@loled2>
Tue, 20 Dec 2016 06:23:59 +0000 (15:23 +0900)
committerlgz <lgz@loled2>
Tue, 20 Dec 2016 06:23:59 +0000 (15:23 +0900)
python.d/freeradius.chart.py [new file with mode: 0644]

diff --git a/python.d/freeradius.chart.py b/python.d/freeradius.chart.py
new file mode 100644 (file)
index 0000000..cbf3be4
--- /dev/null
@@ -0,0 +1,89 @@
+# -*- coding: utf-8 -*-
+# Description: freeradius netdata python.d module
+# Author: l2isbad
+
+from base import SimpleService
+from os.path import isfile
+from re import findall
+from subprocess import Popen, PIPE
+
+# default module values (can be overridden per job in `config`)
+priority = 60000
+retries = 60
+update_every = 10
+directories = ['/bin/', '/usr/bin/', '/sbin/', '/usr/sbin/']
+
+# charts order (can be overridden if you want less charts, or different order)
+ORDER = ['authentication', 'accounting']
+
+CHARTS = {
+    'authentication': {
+        'options': [None, "Authentication", "packets/s", 'Authentication', 'freerad.auth', 'line'],
+        'lines': [
+            ['access-accepts', None, 'incremental'], ['access-rejects', None, 'incremental'],
+            ['auth-dropped-requests', None, 'incremental'], ['auth-duplicate-requests', None, 'incremental'],
+            ['auth-invalid-requests', None, 'incremental'], ['auth-malformed-requests', None, 'incremental'],
+            ['auth-unknown-types', None, 'incremental']
+        ]},
+     'accounting': {
+        'options': [None, "Accounting", "packets/s", 'Accounting', 'freerad.acct', 'line'],
+        'lines': [
+            ['accounting-requests', None, 'incremental'], ['accounting-responses', None, 'incremental'],
+            ['acct-dropped-requests', None, 'incremental'], ['acct-duplicate-requests', None, 'incremental'],
+            ['acct-invalid-requests', None, 'incremental'], ['acct-malformed-requests', None, 'incremental'],
+            ['acct-unknown-types', None, 'incremental']
+        ]}
+}
+
+
+class Service(SimpleService):
+    def __init__(self, configuration=None, name=None):
+        SimpleService.__init__(self, configuration=configuration, name=name)
+        self.order = ORDER
+        self.definitions = CHARTS
+        self.host = self.configuration.get('host', 'localhost')
+        self.port = self.configuration.get('port', '18121')
+        self.secret = self.configuration.get('secret', 'adminsecret')
+        self.echo = [''.join([directory, 'echo']) for directory in directories if isfile(''.join([directory, 'echo']))][0]
+        self.radclient = [''.join([directory, 'radclient']) for directory in directories if isfile(''.join([directory, 'radclient']))][0]
+        self.sub_echo = [self.echo, 'Message-Authenticator = 0x00, FreeRADIUS-Statistics-Type = 3, Response-Packet-Type = Access-Accept']
+        self.sub_radclient = [self.radclient, '-r', '1', '-t', '1', ':'.join([self.host, self.port]), 'status', self.secret]
+    
+    def check(self):
+        if not all([self.echo, self.radclient]):
+            self.error('Command radclient not found')
+            return False
+        if self._get_raw_data():
+            self.info('plugin was started succesfully')
+            return True
+        else:
+            self.error('Request returned no data. Is server alive? Used options: host {}, port {}, secret {}'.format(self.host, self.port, self.secret))
+            return False
+        
+
+    def _get_data(self):
+        """
+        Format data received from shell command
+        :return: dict
+        """
+        result = self._get_raw_data()
+        return {k.lower():int(v) for k, v in findall(r'((?<=-)[AP][a-zA-Z-]+) = (\d+)', result)}
+        
+    def _get_raw_data(self):
+        """
+        The following code is equivalent to
+        'echo "Message-Authenticator = 0x00, FreeRADIUS-Statistics-Type = 3, Response-Packet-Type = Access-Accept" | radclient -t 1 -r 1 host:port status secret'
+        :return: str
+        """
+        try:
+            process_echo = Popen(self.sub_echo,  stdout=PIPE, shell=False)
+            process_rad = Popen(self.sub_radclient, stdin=process_echo.stdout, stdout=PIPE,  shell=False)
+            process_echo.stdout.close()
+            raw_result = process_rad.communicate()[0]
+        except Exception:
+            return None
+        else:
+            if process_rad.returncode is 0:
+                return raw_result.decode()
+            else:
+                return None