@cryptotaxi247 / netdata-1 / commits / 693797bf3

freeradius plugin added

lgz committed Dec 20, 2016 at 15:23 UTC 693797bf39e923300078621882c60da860290d4a
1 file changed +89
python.d/freeradius.chart.py new
+89
@@ -0,0 +1,89 @@
1 +# -*- coding: utf-8 -*-
2 +# Description: freeradius netdata python.d module
3 +# Author: l2isbad
4 +
5 +from base import SimpleService
6 +from os.path import isfile
7 +from re import findall
8 +from subprocess import Popen, PIPE
9 +
10 +# default module values (can be overridden per job in `config`)
11 +priority = 60000
12 +retries = 60
13 +update_every = 10
14 +directories = ['/bin/', '/usr/bin/', '/sbin/', '/usr/sbin/']
15 +
16 +# charts order (can be overridden if you want less charts, or different order)
17 +ORDER = ['authentication', 'accounting']
18 +
19 +CHARTS = {
20 + 'authentication': {
21 + 'options': [None, "Authentication", "packets/s", 'Authentication', 'freerad.auth', 'line'],
22 + 'lines': [
23 + ['access-accepts', None, 'incremental'], ['access-rejects', None, 'incremental'],
24 + ['auth-dropped-requests', None, 'incremental'], ['auth-duplicate-requests', None, 'incremental'],
25 + ['auth-invalid-requests', None, 'incremental'], ['auth-malformed-requests', None, 'incremental'],
26 + ['auth-unknown-types', None, 'incremental']
27 + ]},
28 + 'accounting': {
29 + 'options': [None, "Accounting", "packets/s", 'Accounting', 'freerad.acct', 'line'],
30 + 'lines': [
31 + ['accounting-requests', None, 'incremental'], ['accounting-responses', None, 'incremental'],
32 + ['acct-dropped-requests', None, 'incremental'], ['acct-duplicate-requests', None, 'incremental'],
33 + ['acct-invalid-requests', None, 'incremental'], ['acct-malformed-requests', None, 'incremental'],
34 + ['acct-unknown-types', None, 'incremental']
35 + ]}
36 +}
37 +
38 +
39 +class Service(SimpleService):
40 + def __init__(self, configuration=None, name=None):
41 + SimpleService.__init__(self, configuration=configuration, name=name)
42 + self.order = ORDER
43 + self.definitions = CHARTS
44 + self.host = self.configuration.get('host', 'localhost')
45 + self.port = self.configuration.get('port', '18121')
46 + self.secret = self.configuration.get('secret', 'adminsecret')
47 + self.echo = [''.join([directory, 'echo']) for directory in directories if isfile(''.join([directory, 'echo']))][0]
48 + self.radclient = [''.join([directory, 'radclient']) for directory in directories if isfile(''.join([directory, 'radclient']))][0]
49 + self.sub_echo = [self.echo, 'Message-Authenticator = 0x00, FreeRADIUS-Statistics-Type = 3, Response-Packet-Type = Access-Accept']
50 + self.sub_radclient = [self.radclient, '-r', '1', '-t', '1', ':'.join([self.host, self.port]), 'status', self.secret]
51 +
52 + def check(self):
53 + if not all([self.echo, self.radclient]):
54 + self.error('Command radclient not found')
55 + return False
56 + if self._get_raw_data():
57 + self.info('plugin was started succesfully')
58 + return True
59 + else:
60 + self.error('Request returned no data. Is server alive? Used options: host {}, port {}, secret {}'.format(self.host, self.port, self.secret))
61 + return False
62 +
63 +
64 + def _get_data(self):
65 + """
66 + Format data received from shell command
67 + :return: dict
68 + """
69 + result = self._get_raw_data()
70 + return {k.lower():int(v) for k, v in findall(r'((?<=-)[AP][a-zA-Z-]+) = (\d+)', result)}
71 +
72 + def _get_raw_data(self):
73 + """
74 + The following code is equivalent to
75 + 'echo "Message-Authenticator = 0x00, FreeRADIUS-Statistics-Type = 3, Response-Packet-Type = Access-Accept" | radclient -t 1 -r 1 host:port status secret'
76 + :return: str
77 + """
78 + try:
79 + process_echo = Popen(self.sub_echo, stdout=PIPE, shell=False)
80 + process_rad = Popen(self.sub_radclient, stdin=process_echo.stdout, stdout=PIPE, shell=False)
81 + process_echo.stdout.close()
82 + raw_result = process_rad.communicate()[0]
83 + except Exception:
84 + return None
85 + else:
86 + if process_rad.returncode is 0:
87 + return raw_result.decode()
88 + else:
89 + return None