]> arthur.barton.de Git - netdata.git/blob - python.d/ovpn_status_log.chart.py
b3cc6723fff2878251468db596d25871837a1e24
[netdata.git] / python.d / ovpn_status_log.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: openvpn status log netdata python.d module
3 # Author: l2isbad
4
5 from base import SimpleService
6 from re import compile as r_compile
7 priority = 60000
8 retries = 60
9 update_every = 10
10
11 ORDER = ['users', 'traffic']
12 CHARTS = {
13     'users': {
14         'options': [None, 'OpenVPN Active Users', 'active users', 'users', 'openvpn_status.users', 'line'],
15         'lines': [
16             ["users", None, "absolute"],
17         ]},
18     'traffic': {
19         'options': [None, 'OpenVPN Traffic', 'Kb/s', 'traffic', 'openvpn_status.traffic', 'area'],
20         'lines': [
21             ["in", None, "incremental", 8, 1000], ["out", None, "incremental", 8, -1000]
22         ]},
23
24 }
25
26
27 class Service(SimpleService):
28     def __init__(self, configuration=None, name=None):
29         SimpleService.__init__(self, configuration=configuration, name=name)
30         self.order = ORDER
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+(?=(?:,| ))')
37
38     def check(self):
39         if not (self.log_path and isinstance(self.log_path, str)):
40             self.error('\'log_path\' is not defined')
41             return False
42
43         data = self._get_data()
44         if not data:
45             self.error('Make sure that the openvpn status log file exists and netdata has permission to read it')
46             return False
47         else:
48             self._data_from_check = data
49             return True
50
51     def _get_raw_data(self):
52         """
53         Open log file
54         :return: str
55         """
56
57         try:
58             with open(self.log_path, 'rt') as log:
59                 result = log.read()
60         except OSError:
61             return None
62         else:
63             return result or None
64
65     def _get_data(self):
66         """
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.
69         """
70
71         raw_data = self._get_raw_data()
72         if not raw_data:
73             return None
74
75         data_inter = self.regex_data_inter.search(' '.join(raw_data.splitlines()))
76         if not data_inter:
77             return None
78         else:
79             data_inter = data_inter.group()
80
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)
84
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)
87
88         return {'users': users, 'in': bytes_in, 'out': bytes_out}