summaryrefslogtreecommitdiffstats
path: root/python.d/ovpn_status_log.chart.py
blob: c5fca002af67d143089594bcb45b33eab58cd672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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:
            data_inter = ''

        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}