1 # -*- coding: utf-8 -*-
2 # Description: freeradius netdata python.d module
5 from base import SimpleService
7 from subprocess import Popen, PIPE
9 # default module values (can be overridden per job in `config`)
14 # charts order (can be overridden if you want less charts, or different order)
15 ORDER = ['authentication', 'accounting', 'proxy-auth', 'proxy-acct']
19 'options': [None, "Authentication", "packets/s", 'Authentication', 'freerad.auth', 'line'],
21 ['access-accepts', None, 'incremental'], ['access-rejects', None, 'incremental'],
22 ['auth-dropped-requests', None, 'incremental'], ['auth-duplicate-requests', None, 'incremental'],
23 ['auth-invalid-requests', None, 'incremental'], ['auth-malformed-requests', None, 'incremental'],
24 ['auth-unknown-types', None, 'incremental']
27 'options': [None, "Accounting", "packets/s", 'Accounting', 'freerad.acct', 'line'],
29 ['accounting-requests', None, 'incremental'], ['accounting-responses', None, 'incremental'],
30 ['acct-dropped-requests', None, 'incremental'], ['acct-duplicate-requests', None, 'incremental'],
31 ['acct-invalid-requests', None, 'incremental'], ['acct-malformed-requests', None, 'incremental'],
32 ['acct-unknown-types', None, 'incremental']
35 'options': [None, "Proxy Authentication", "packets/s", 'Authentication', 'freerad.proxy.auth', 'line'],
37 ['proxy-access-accepts', None, 'incremental'], ['proxy-access-rejects', None, 'incremental'],
38 ['proxy-auth-dropped-requests', None, 'incremental'], ['proxy-auth-duplicate-requests', None, 'incremental'],
39 ['proxy-auth-invalid-requests', None, 'incremental'], ['proxy-auth-malformed-requests', None, 'incremental'],
40 ['proxy-auth-unknown-types', None, 'incremental']
43 'options': [None, "Proxy Accounting", "packets/s", 'Accounting', 'freerad.proxy.acct', 'line'],
45 ['proxy-accounting-requests', None, 'incremental'], ['proxy-accounting-responses', None, 'incremental'],
46 ['proxy-acct-dropped-requests', None, 'incremental'], ['proxy-acct-duplicate-requests', None, 'incremental'],
47 ['proxy-acct-invalid-requests', None, 'incremental'], ['proxy-acct-malformed-requests', None, 'incremental'],
48 ['proxy-acct-unknown-types', None, 'incremental']
54 class Service(SimpleService):
55 def __init__(self, configuration=None, name=None):
56 SimpleService.__init__(self, configuration=configuration, name=name)
57 self.host = self.configuration.get('host', 'localhost')
58 self.port = self.configuration.get('port', '18121')
59 self.secret = self.configuration.get('secret', 'adminsecret')
60 self.acct = self.configuration.get('acct', False)
61 self.proxy_auth = self.configuration.get('proxy_auth', False)
62 self.proxy_acct = self.configuration.get('proxy_acct', False)
63 self.echo = self.find_binary('echo')
64 self.radclient = self.find_binary('radclient')
65 self.sub_echo = [self.echo, 'Message-Authenticator = 0x00, FreeRADIUS-Statistics-Type = 15, Response-Packet-Type = Access-Accept']
66 self.sub_radclient = [self.radclient, '-r', '1', '-t', '1', ':'.join([self.host, self.port]), 'status', self.secret]
69 if not all([self.echo, self.radclient]):
70 self.error('Can\'t locate \'radclient\' binary or binary is not executable by netdata')
72 if self._get_raw_data():
73 chart_choice = [True, bool(self.acct), bool(self.proxy_auth), bool(self.proxy_acct)]
74 self.order = [chart for chart, choice in zip(ORDER, chart_choice) if choice]
75 self.definitions = dict([chart for chart in CHARTS.items() if chart[0] in self.order])
76 self.info('Plugin was started succesfully')
79 self.error('Request returned no data. Is server alive? Used options: host {0}, port {1}, secret {2}'.format(self.host, self.port, self.secret))
85 Format data received from shell command
88 result = self._get_raw_data()
89 return dict([(elem[0].lower(), int(elem[1])) for elem in findall(r'((?<=-)[AP][a-zA-Z-]+) = (\d+)', result)])
91 def _get_raw_data(self):
93 The following code is equivalent to
94 'echo "Message-Authenticator = 0x00, FreeRADIUS-Statistics-Type = 15, Response-Packet-Type = Access-Accept" | radclient -t 1 -r 1 host:port status secret'
98 process_echo = Popen(self.sub_echo, stdout=PIPE, stderr=PIPE, shell=False)
99 process_rad = Popen(self.sub_radclient, stdin=process_echo.stdout, stdout=PIPE, stderr=PIPE, shell=False)
100 process_echo.stdout.close()
101 raw_result = process_rad.communicate()[0]
105 if process_rad.returncode is 0:
106 return raw_result.decode()