1 # -*- coding: utf-8 -*-
2 # Description: freeradius netdata python.d module
5 from base import SimpleService
6 from os.path import isfile
8 from subprocess import Popen, PIPE
10 # default module values (can be overridden per job in `config`)
14 directories = ['/bin/', '/usr/bin/', '/sbin/', '/usr/sbin/']
16 # charts order (can be overridden if you want less charts, or different order)
17 ORDER = ['authentication', 'accounting']
21 'options': [None, "Authentication", "packets/s", 'Authentication', 'freerad.auth', 'line'],
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']
29 'options': [None, "Accounting", "packets/s", 'Accounting', 'freerad.acct', 'line'],
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']
39 class Service(SimpleService):
40 def __init__(self, configuration=None, name=None):
41 SimpleService.__init__(self, configuration=configuration, name=name)
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]
53 if not all([self.echo, self.radclient]):
54 self.error('Command radclient not found')
56 if self._get_raw_data():
57 self.info('plugin was started succesfully')
60 self.error('Request returned no data. Is server alive? Used options: host {}, port {}, secret {}'.format(self.host, self.port, self.secret))
66 Format data received from shell command
69 result = self._get_raw_data()
70 return {k.lower():int(v) for k, v in findall(r'((?<=-)[AP][a-zA-Z-]+) = (\d+)', result)}
72 def _get_raw_data(self):
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'
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]
86 if process_rad.returncode is 0:
87 return raw_result.decode()