summaryrefslogtreecommitdiff
path: root/src/conf_mode/service_monitoring.py
blob: e14ad78fcd7905db429c8de84d4509f6d8d62817 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
#
# Copyright (C) 2021 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import json

from sys import exit

from vyos.config import Config
from vyos.template import render
from vyos.util import call
from vyos.util import chown
from vyos.util import cmd
from vyos import ConfigError
from vyos import airbag
airbag.enable()


cache_dir = '/run/telegraf/.cache'
config_telegraf = '/run/telegraf/vyos-telegraf.conf'
custom_scripts_dir = '/etc/telegraf/custom_scripts'
syslog_telegraf = '/etc/rsyslog.d/50-telegraf.conf'
systemd_telegraf_service = '/etc/systemd/system/vyos-telegraf.service'
systemd_telegraf_override_dir = '/etc/systemd/system/vyos-telegraf.service.d'
systemd_override = f'{systemd_telegraf_override_dir}/10-override.conf'


def get_nft_filter_chains():
    """
    Get nft chains for table filter
    """
    nft = cmd('nft --json list table ip filter')
    nft = json.loads(nft)
    chain_list = []

    for output in nft['nftables']:
        if 'chain' in output:
            chain = output['chain']['name']
            chain_list.append(chain)

    return chain_list

def get_config(config=None):

    if config:
        conf = config
    else:
        conf = Config()
    base = ['service', 'monitoring']
    monitoring = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True,
                                    no_tag_node_value_mangle=True)

    if monitoring:
        # Set default value for bucket if not configured
        if 'bucket' not in monitoring:
            monitoring['bucket'] = cmd('cat /etc/hostname')

        # Get nftables chains
        monitoring['nft_chains'] = get_nft_filter_chains()
        monitoring['custom_scripts_dir'] = custom_scripts_dir

    return monitoring

def verify(monitoring):
    # bail out early - looks like removal from running config
    if not monitoring:
        return None

    if 'login' not in monitoring['authentication'] or 'token' not in monitoring['authentication']:
        raise ConfigError(f'Authentication "login and token" are mandatory!')

    if 'url' not in monitoring:
        raise ConfigError(f'Monitoring "url" is mandatory!')

    return None

def generate(monitoring):
    if not monitoring:
        # Delete config file
        if os.path.isfile(config_telegraf):
            os.unlink(config_telegraf)
        if os.path.isfile(systemd_override):
            os.unlink(systemd_override)
        if os.path.isfile(syslog_telegraf):
            os.unlink(syslog_telegraf)


        return None

    # Create telegraf cache dir
    if not os.path.exists(cache_dir):
        os.mkdir(cache_dir)

    chown(cache_dir, 'telegraf', 'vyattacfg')

    # Create systemd override dir
    if not os.path.exists(systemd_telegraf_override_dir):
        os.mkdir(systemd_telegraf_override_dir)

    # Create custome scripts dir
    if not os.path.exists(custom_scripts_dir):
        os.mkdir(custom_scripts_dir)

    # Render telegraf configuration and systemd override
    render(config_telegraf, 'monitoring/telegraf.tmpl', monitoring)
    render(systemd_telegraf_service, 'monitoring/systemd_vyos_telegraf_service.tmpl', monitoring)
    render(systemd_override, 'monitoring/override.conf.tmpl', monitoring, permission=0o640)
    render(syslog_telegraf, 'monitoring/syslog_telegraf.tmpl', monitoring)

    return None

def apply(monitoring):
    # Reload systemd manager configuration
    call('systemctl daemon-reload')
    if monitoring:
        call('systemctl restart vyos-telegraf.service')
    else:
        call('systemctl stop vyos-telegraf.service')
    # Telegraf include custom rsyslog config changes
    call('systemctl restart rsyslog')

if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        exit(1)