@cryptotaxi247 / netdata-1 / commits / 6e8bf817e

"ovpn_status_log" module update

lgz committed Oct 14, 2017 at 23:29 UTC 6e8bf817eb4bacb3445718523eadb6a7c384bdbb
1 file changed +23 -18
python.d/ovpn_status_log.chart.py
+23 -18
@@ -3,8 +3,8 @@
3 # Author: l2isbad
4
5 from re import compile as r_compile
6 -from collections import defaultdict
7 -from base import SimpleService
6 +
7 +from bases.FrameworkServices.SimpleService import SimpleService
8
9 priority = 60000
10 retries = 60
@@ -34,24 +34,30 @@ class Service(SimpleService):
34 self.log_path = self.configuration.get('log_path')
35 self.regex = dict(tls=r_compile(r'\d{1,3}(?:\.\d{1,3}){3}(?::\d+)? (?P<bytes_in>\d+) (?P<bytes_out>\d+)'),
36 static_key=r_compile(r'TCP/[A-Z]+ (?P<direction>(?:read|write)) bytes,(?P<bytes>\d+)'))
37 - self.to_netdata = dict(bytes_in=0, bytes_out=0)
37
38 def check(self):
39 if not (self.log_path and isinstance(self.log_path, str)):
41 - self.error('\'log_path\' is not defined')
40 + self.error("'log_path' is not defined")
41 return False
42
44 - data = False
45 - for method in (self._get_data_tls, self._get_data_static_key):
46 - data = method()
47 - if data:
48 - self._get_data = method
49 - self._data_from_check = data
50 - break
43 + data = self._get_raw_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 None
47
52 - if data:
48 + found = None
49 + for row in data:
50 + if 'ROUTING' in row:
51 + self.get_data = self.get_data_tls
52 + found = True
53 + break
54 + elif 'STATISTICS' in row:
55 + self.get_data = self.get_data_static_key
56 + found = True
57 + break
58 + if found:
59 return True
54 - self.error('Make sure that the openvpn status log file exists and netdata has permission to read it')
60 + self.error("Failed to parse ovpenvpn log file")
61 return False
62
63 def _get_raw_data(self):
@@ -68,7 +74,7 @@ class Service(SimpleService):
74 else:
75 return raw_data
76
71 - def _get_data_static_key(self):
77 + def get_data_static_key(self):
78 """
79 Parse openvpn-status log file.
80 """
@@ -77,7 +83,7 @@ class Service(SimpleService):
83 if not raw_data:
84 return None
85
80 - data = defaultdict(lambda: 0)
86 + data = dict(bytes_in=0, bytes_out=0)
87
88 for row in raw_data:
89 match = self.regex['static_key'].search(row)
@@ -90,7 +96,7 @@ class Service(SimpleService):
96
97 return data or None
98
93 - def _get_data_tls(self):
99 + def get_data_tls(self):
100 """
101 Parse openvpn-status log file.
102 """
@@ -99,7 +105,7 @@ class Service(SimpleService):
105 if not raw_data:
106 return None
107
102 - data = defaultdict(lambda: 0)
108 + data = dict(users=0, bytes_in=0, bytes_out=0)
109 for row in raw_data:
110 row = ' '.join(row.split(',')) if ',' in row else ' '.join(row.split())
111 match = self.regex['tls'].search(row)
@@ -110,4 +116,3 @@ class Service(SimpleService):
116 data['bytes_out'] += int(match['bytes_out'])
117
118 return data or None
113 -