ovpn_status_log plugin: minor fixes
Ilya committed
Mar 26, 2017 at 11:51 UTC
ed84bb8a762f62a4e20065038b23d043c6213ea3
1 file changed
+28
-17
python.d/ovpn_status_log.chart.py
+28
-17
@@ -3,7 +3,7 @@
3
# Author: l2isbad
4
5
from base import SimpleService
6
-from re import compile, findall, search, subn
6
+from re import compile as r_compile
7
priority = 60000
8
retries = 60
9
update_every = 10
@@ -11,35 +11,41 @@ update_every = 10
11
ORDER = ['users', 'traffic']
12
CHARTS = {
13
'users': {
14
- 'options': [None, 'OpenVPN active users', 'active users', 'Users', 'openvpn_status.users', 'line'],
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'],
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')
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+(?=(?:,| ))')
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):
38
- if not self._get_raw_data():
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:
42
- self.info('Plugin was started succesfully')
48
+ self._data_from_check = data
49
return True
50
51
def _get_raw_data(self):
@@ -47,13 +53,14 @@ class Service(SimpleService):
53
Open log file
54
:return: str
55
"""
56
+
57
try:
58
with open(self.log_path, 'rt') as log:
59
result = log.read()
53
- except Exception:
60
+ except OSError:
61
return None
62
else:
56
- return result
63
+ return result or None
64
65
def _get_data(self):
66
"""
@@ -62,16 +69,20 @@ class Service(SimpleService):
69
"""
70
71
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
- data_inter = ''
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
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])
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}