openvpn status log plugin added
lgz committed
Dec 21, 2016 at 22:04 UTC
041015a8c97f9b47612518009988f7b2041d302c
1 file changed
+77
python.d/ovpn_status_log.chart.py
new
+77
@@ -0,0 +1,77 @@
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, findall, search, subn
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', 'kilobit/s', 'Traffic', 'openvpn_status.traffic', 'area'],
20
+ 'lines': [
21
+ ["in", None, "incremental", 8, 1000], ["out", None, "incremental", 8, -1000]
22
+ ]},
23
+
24
+}
25
+
26
+class Service(SimpleService):
27
+ def __init__(self, configuration=None, name=None):
28
+ SimpleService.__init__(self, configuration=configuration, name=name)
29
+ self.order = ORDER
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+(?=(?:,| ))')
36
+
37
+ def check(self):
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')
40
+ return False
41
+ else:
42
+ self.info('Plugin was started succesfully')
43
+ return True
44
+
45
+ def _get_raw_data(self):
46
+ """
47
+ Open log file
48
+ :return: str
49
+ """
50
+ try:
51
+ with open(self.log_path, 'rt') as log:
52
+ result = log.read()
53
+ except Exception:
54
+ return None
55
+ else:
56
+ return result
57
+
58
+ def _get_data(self):
59
+ """
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.
62
+ """
63
+
64
+ raw_data = self._get_raw_data()
65
+ try:
66
+ data_inter = self.regex_data_inter.search(' '.join(raw_data.splitlines())).group()
67
+ except AttributeError:
68
+ string = ''
69
+
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)
73
+
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])
76
+
77
+ return {'users': users, 'in': bytes_in, 'out': bytes_out}