summaryrefslogtreecommitdiff
path: root/python/vyos/ifconfig/vrrp.py
blob: d3d31cc0734b3b1f731a87542e99075657e46587 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright 2019-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see <http://www.gnu.org/licenses/>.

import os
import json
import signal

from time import time
from tabulate import tabulate

from vyos.configquery import ConfigTreeQuery
from vyos.utils.convert import seconds_to_human
from vyos.utils.file import read_file
from vyos.utils.file import wait_for_file_write_complete
from vyos.utils.process import process_running

class VRRPError(Exception):
    pass

class VRRPNoData(VRRPError):
    pass

class VRRP(object):
    _vrrp_prefix = '00:00:5E:00:01:'
    location = {
        'pid':      '/run/keepalived/keepalived.pid',
        'fifo':     '/run/keepalived/keepalived_notify_fifo',
        'state':    '/tmp/keepalived.data',
        'stats':    '/tmp/keepalived.stats',
        'json':     '/tmp/keepalived.json',
        'daemon':   '/etc/default/keepalived',
        'config':   '/run/keepalived/keepalived.conf',
    }

    _signal = {
        'state':  signal.SIGUSR1,
        'stats':  signal.SIGUSR2,
        'json':   signal.SIGRTMIN + 2,
    }

    _name = {
        'state': 'information',
        'stats': 'statistics',
        'json':  'data',
    }

    state = {
        0: 'INIT',
        1: 'BACKUP',
        2: 'MASTER',
        3: 'FAULT',
        # UNKNOWN
    }

    def __init__(self,ifname):
        self.ifname = ifname

    def enabled(self):
        return self.ifname in self.active_interfaces()

    @classmethod
    def active_interfaces(cls):
        if not os.path.exists(cls.location['pid']):
            return []
        data = cls.collect('json')
        return [group['data']['ifp_ifname'] for group in json.loads(data)]

    @classmethod
    def decode_state(cls, code):
        return cls.state.get(code,'UNKNOWN')

    # used in conf mode
    @classmethod
    def is_running(cls):
        if not os.path.exists(cls.location['pid']):
            return False
        return process_running(cls.location['pid'])

    @classmethod
    def collect(cls, what):
        fname = cls.location[what]
        try:
            # send signal to generate the configuration file
            pid = read_file(cls.location['pid'])
            wait_for_file_write_complete(fname,
              pre_hook=(lambda: os.kill(int(pid), cls._signal[what])),
              timeout=30)

            return read_file(fname)
        except FileNotFoundError:
            raise VRRPNoData("VRRP data is not available (process not running or no active groups)")
        except OSError:
            # raised by vyos.utils.file.read_file
            raise VRRPNoData("VRRP data is not available (wait time exceeded)")
        except Exception:
            name = cls._name[what]
            raise VRRPError(f'VRRP {name} is not available')
        finally:
            if os.path.exists(fname):
                os.remove(fname)

    @classmethod
    def disabled(cls):
        disabled = []
        base = ['high-availability', 'vrrp']
        conf = ConfigTreeQuery()
        if conf.exists(base):
            # Read VRRP configuration directly from CLI
            vrrp_config_dict = conf.get_config_dict(base, key_mangling=('-', '_'),
                                                    get_first_key=True)

            # add disabled groups to the list
            if 'group' in vrrp_config_dict:
                for group, group_config in vrrp_config_dict['group'].items():
                    if 'disable' not in group_config:
                        continue
                    disabled.append([group, group_config['interface'], group_config['vrid'], 'DISABLED', ''])

        # return list with disabled instances
        return disabled

    @classmethod
    def format(cls, data):
        headers = ["Name", "Interface", "VRID", "State", "Priority", "Last Transition"]
        groups = []

        data = json.loads(data) if isinstance(data, str) else data
        for group in data:
            data = group['data']

            name = data['iname']
            intf = data['ifp_ifname']
            vrid = data['vrid']
            state = cls.decode_state(data["state"])
            priority = data['effective_priority']

            since = int(time() - float(data['last_transition']))
            last = seconds_to_human(since)

            groups.append([name, intf, vrid, state, priority, last])

        # add to the active list disabled instances
        groups.extend(cls.disabled())
        return(tabulate(groups, headers))