]> arthur.barton.de Git - netdata.git/blob - python.d/freeradius.chart.py
added IPMI alarms
[netdata.git] / python.d / freeradius.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: freeradius netdata python.d module
3 # Author: l2isbad
4
5 from base import SimpleService
6 from re import findall
7 from subprocess import Popen, PIPE
8
9 # default module values (can be overridden per job in `config`)
10 priority = 60000
11 retries = 60
12 update_every = 15
13
14 # charts order (can be overridden if you want less charts, or different order)
15 ORDER = ['authentication', 'accounting', 'proxy-auth', 'proxy-acct']
16
17 CHARTS = {
18     'authentication': {
19         'options': [None, "Authentication", "packets/s", 'Authentication', 'freerad.auth', 'line'],
20         'lines': [
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']
25         ]},
26      'accounting': {
27         'options': [None, "Accounting", "packets/s", 'Accounting', 'freerad.acct', 'line'],
28         'lines': [
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']
33         ]},
34     'proxy-auth': {
35         'options': [None, "Proxy Authentication", "packets/s", 'Authentication', 'freerad.proxy.auth', 'line'],
36         'lines': [
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']
41         ]},
42      'proxy-acct': {
43         'options': [None, "Proxy Accounting", "packets/s", 'Accounting', 'freerad.proxy.acct', 'line'],
44         'lines': [
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']
49         ]}
50
51 }
52
53
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]
67     
68     def check(self):
69         if not all([self.echo, self.radclient]):
70             self.error('Can\'t locate \'radclient\' binary or binary is not executable by netdata')
71             return False
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')
77             return True
78         else:
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))
80             return False
81         
82
83     def _get_data(self):
84         """
85         Format data received from shell command
86         :return: dict
87         """
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)])
90         
91     def _get_raw_data(self):
92         """
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'
95         :return: str
96         """
97         try:
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]
102         except Exception:
103             return None
104         else:
105             if process_rad.returncode is 0:
106                 return raw_result.decode()
107             else:
108                 return None