From 041015a8c97f9b47612518009988f7b2041d302c Mon Sep 17 00:00:00 2001 From: lgz Date: Wed, 21 Dec 2016 22:04:14 +0900 Subject: [PATCH] openvpn status log plugin added --- python.d/ovpn_status_log.chart.py | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 python.d/ovpn_status_log.chart.py diff --git a/python.d/ovpn_status_log.chart.py b/python.d/ovpn_status_log.chart.py new file mode 100644 index 00000000..2480dce9 --- /dev/null +++ b/python.d/ovpn_status_log.chart.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +# Description: openvpn status log netdata python.d module +# Author: l2isbad + +from base import SimpleService +from re import compile, findall, search, subn +priority = 60000 +retries = 60 +update_every = 10 + +ORDER = ['users', 'traffic'] +CHARTS = { + 'users': { + 'options': [None, 'OpenVPN active users', 'active users', 'Users', 'openvpn_status.users', 'line'], + 'lines': [ + ["users", None, "absolute"], + ]}, + 'traffic': { + 'options': [None, 'OpenVPN traffic', 'kilobit/s', 'Traffic', 'openvpn_status.traffic', 'area'], + 'lines': [ + ["in", None, "incremental", 8, 1000], ["out", None, "incremental", 8, -1000] + ]}, + +} + +class Service(SimpleService): + def __init__(self, configuration=None, name=None): + SimpleService.__init__(self, configuration=configuration, name=name) + self.order = ORDER + self.definitions = CHARTS + self.log_path = self.configuration.get('log_path') + self.regex_data_inter = compile(r'(?<=Since ).*?(?=.ROUTING)') + self.regex_data_final = compile(r'\d{1,3}(?:\.\d{1,3}){3}[:0-9,. ]*') + self.regex_users = compile(r'\d{1,3}(?:\.\d{1,3}){3}:\d+') + self.regex_traffic = compile(r'(?<=(?:,| ))\d+(?=(?:,| ))') + + def check(self): + if not self._get_raw_data(): + self.error('Make sure that the openvpn status log file exists and netdata has permission to read it') + return False + else: + self.info('Plugin was started succesfully') + return True + + def _get_raw_data(self): + """ + Open log file + :return: str + """ + try: + with open(self.log_path, 'rt') as log: + result = log.read() + except Exception: + return None + else: + return result + + def _get_data(self): + """ + Parse openvpn-status log file. + Current regex version is ok for status-version 1, 2 and 3. Both users and bytes in/out are collecting. + """ + + raw_data = self._get_raw_data() + try: + data_inter = self.regex_data_inter.search(' '.join(raw_data.splitlines())).group() + except AttributeError: + string = '' + + data_final = ' '.join(self.regex_data_final.findall(data_inter)) + users = self.regex_users.subn('', data_final)[1] + traffic = self.regex_traffic.findall(data_final) + + bytes_in = sum([int(traffic[i]) for i in range(len(traffic)) if (i + 1) % 2 is 1]) + bytes_out = sum([int(traffic[i]) for i in range(len(traffic)) if (i + 1) % 2 is 0]) + + return {'users': users, 'in': bytes_in, 'out': bytes_out} -- 2.39.2