1 # -*- coding: utf-8 -*-
2 # Description: openvpn status log netdata python.d module
5 from base import SimpleService
6 from re import compile, findall, search, subn
11 ORDER = ['users', 'traffic']
14 'options': [None, 'OpenVPN active users', 'active users', 'Users', 'openvpn_status.users', 'line'],
16 ["users", None, "absolute"],
19 'options': [None, 'OpenVPN traffic', 'kilobit/s', 'Traffic', 'openvpn_status.traffic', 'area'],
21 ["in", None, "incremental", 8, 1000], ["out", None, "incremental", 8, -1000]
26 class Service(SimpleService):
27 def __init__(self, configuration=None, name=None):
28 SimpleService.__init__(self, configuration=configuration, name=name)
30 self.definitions = CHARTS
31 self.log_path = self.configuration.get('log_path')
32 self.regex_data_inter = compile(r'(?<=Since ).*?(?=.ROUTING)')
33 self.regex_data_final = compile(r'\d{1,3}(?:\.\d{1,3}){3}[:0-9,. ]*')
34 self.regex_users = compile(r'\d{1,3}(?:\.\d{1,3}){3}:\d+')
35 self.regex_traffic = compile(r'(?<=(?:,| ))\d+(?=(?:,| ))')
38 if not self._get_raw_data():
39 self.error('Make sure that the openvpn status log file exists and netdata has permission to read it')
42 self.info('Plugin was started succesfully')
45 def _get_raw_data(self):
51 with open(self.log_path, 'rt') as log:
60 Parse openvpn-status log file.
61 Current regex version is ok for status-version 1, 2 and 3. Both users and bytes in/out are collecting.
64 raw_data = self._get_raw_data()
66 data_inter = self.regex_data_inter.search(' '.join(raw_data.splitlines())).group()
67 except AttributeError:
70 data_final = ' '.join(self.regex_data_final.findall(data_inter))
71 users = self.regex_users.subn('', data_final)[1]
72 traffic = self.regex_traffic.findall(data_final)
74 bytes_in = sum([int(traffic[i]) for i in range(len(traffic)) if (i + 1) % 2 is 1])
75 bytes_out = sum([int(traffic[i]) for i in range(len(traffic)) if (i + 1) % 2 is 0])
77 return {'users': users, 'in': bytes_in, 'out': bytes_out}