1 # -*- coding: utf-8 -*-
2 # Description: openvpn status log netdata python.d module
5 from base import SimpleService
6 from re import compile as r_compile
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', 'Kb/s', 'traffic', 'openvpn_status.traffic', 'area'],
21 ["in", None, "incremental", 8, 1000], ["out", None, "incremental", 8, -1000]
27 class Service(SimpleService):
28 def __init__(self, configuration=None, name=None):
29 SimpleService.__init__(self, configuration=configuration, name=name)
31 self.definitions = CHARTS
32 self.log_path = self.configuration.get('log_path')
33 self.regex_data_inter = r_compile(r'(?<=Since ).*?(?=.ROUTING)')
34 self.regex_data_final = r_compile(r'\d{1,3}(?:\.\d{1,3}){3}[:0-9,. ]*')
35 self.regex_users = r_compile(r'\d{1,3}(?:\.\d{1,3}){3}:\d+')
36 self.regex_traffic = r_compile(r'(?<=(?:,| ))\d+(?=(?:,| ))')
39 if not (self.log_path and isinstance(self.log_path, str)):
40 self.error('\'log_path\' is not defined')
43 data = self._get_data()
45 self.error('Make sure that the openvpn status log file exists and netdata has permission to read it')
48 self._data_from_check = data
51 def _get_raw_data(self):
58 with open(self.log_path, 'rt') as log:
67 Parse openvpn-status log file.
68 Current regex version is ok for status-version 1, 2 and 3. Both users and bytes in/out are collecting.
71 raw_data = self._get_raw_data()
75 data_inter = self.regex_data_inter.search(' '.join(raw_data.splitlines()))
79 data_inter = data_inter.group()
81 data_final = ' '.join(self.regex_data_final.findall(data_inter))
82 users = self.regex_users.subn('', data_final)[1]
83 traffic = self.regex_traffic.findall(data_final)
85 bytes_in = sum(int(traffic[i[0]]) for i in enumerate(traffic) if (i[0] + 1) % 2 is 1)
86 bytes_out = sum(int(traffic[i[0]]) for i in enumerate(traffic) if (i[0] + 1) % 2 is 0)
88 return {'users': users, 'in': bytes_in, 'out': bytes_out}