Skip to content

Commit cc8f6d5

Browse files
committed
wlb: T4470: Support WLB op-mode commands
1 parent fd748e5 commit cc8f6d5

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

data/op-mode-standardized.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"evpn.py",
1414
"interfaces.py",
1515
"ipsec.py",
16+
"load-balancing_wan.py",
1617
"lldp.py",
1718
"log.py",
1819
"memory.py",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<interfaceDefinition>
3+
<node name="restart">
4+
<children>
5+
<node name="wan-load-balance">
6+
<properties>
7+
<help>Restart Wide Area Network (WAN) load-balancing daemon</help>
8+
</properties>
9+
<command>sudo ${vyos_op_scripts_dir}/restart.py restart_service --name load-balancing_wan</command>
10+
</node>
11+
</children>
12+
</node>
13+
<node name="show">
14+
<children>
15+
<node name="wan-load-balance">
16+
<properties>
17+
<help>Show Wide Area Network (WAN) load-balancing information</help>
18+
</properties>
19+
<command>${vyos_op_scripts_dir}/load-balancing_wan.py show_summary</command>
20+
<children>
21+
<node name="connection">
22+
<properties>
23+
<help>Show Wide Area Network (WAN) load-balancing flow</help>
24+
</properties>
25+
<command>${vyos_op_scripts_dir}/load-balancing_wan.py show_connection</command>
26+
</node>
27+
<node name="status">
28+
<properties>
29+
<help>Show WAN load-balancing statistics</help>
30+
</properties>
31+
<command>${vyos_op_scripts_dir}/load-balancing_wan.py show_status</command>
32+
</node>
33+
</children>
34+
</node>
35+
</children>
36+
</node>
37+
</interfaceDefinition>

src/op_mode/load-balancing_wan.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (C) 2024 VyOS maintainers and contributors
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License version 2 or later as
7+
# published by the Free Software Foundation.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
import json
18+
import re
19+
import sys
20+
21+
from datetime import datetime
22+
23+
from vyos.config import Config
24+
from vyos.utils.process import cmd
25+
26+
import vyos.opmode
27+
28+
wlb_status_file = '/run/wlb_status.json'
29+
30+
status_format = '''Interface: {ifname}
31+
Status: {status}
32+
Last Status Change: {last_change}
33+
Last Interface Success: {last_success}
34+
Last Interface Failure: {last_failure}
35+
Interface Failures: {failures}
36+
'''
37+
38+
def _verify(func):
39+
"""Decorator checks if WLB config exists"""
40+
from functools import wraps
41+
42+
@wraps(func)
43+
def _wrapper(*args, **kwargs):
44+
config = Config()
45+
if not config.exists(['load-balancing', 'wan']):
46+
unconf_message = 'WAN load-balancing is not configured'
47+
raise vyos.opmode.UnconfiguredSubsystem(unconf_message)
48+
return func(*args, **kwargs)
49+
return _wrapper
50+
51+
def _get_raw_data():
52+
with open(wlb_status_file, 'r') as f:
53+
data = json.loads(f.read())
54+
if not data:
55+
return {}
56+
return data
57+
58+
def _get_formatted_output(raw_data):
59+
for ifname, if_data in raw_data.items():
60+
latest_change = if_data['last_success'] if if_data['last_success'] > if_data['last_failure'] else if_data['last_failure']
61+
62+
change_dt = datetime.fromtimestamp(latest_change) if latest_change > 0 else None
63+
success_dt = datetime.fromtimestamp(if_data['last_success']) if if_data['last_success'] > 0 else None
64+
failure_dt = datetime.fromtimestamp(if_data['last_failure']) if if_data['last_failure'] > 0 else None
65+
now = datetime.utcnow()
66+
67+
fmt_data = {
68+
'ifname': ifname,
69+
'status': "active" if if_data['state'] else "failed",
70+
'last_change': change_dt.strftime("%Y-%m-%d %H:%M:%S") if change_dt else 'N/A',
71+
'last_success': str(now - success_dt) if success_dt else 'N/A',
72+
'last_failure': str(now - failure_dt) if failure_dt else 'N/A',
73+
'failures': if_data['failure_count']
74+
}
75+
print(status_format.format(**fmt_data))
76+
77+
@_verify
78+
def show_summary(raw: bool):
79+
data = _get_raw_data()
80+
81+
if raw:
82+
return data
83+
else:
84+
return _get_formatted_output(data)
85+
86+
@_verify
87+
def show_connection(raw: bool):
88+
res = cmd('sudo conntrack -L -n')
89+
lines = res.split("\n")
90+
filtered_lines = [line for line in lines if re.search(r' mark=[1-9]', line)]
91+
92+
if raw:
93+
return filtered_lines
94+
95+
for line in lines:
96+
print(line)
97+
98+
@_verify
99+
def show_status(raw: bool):
100+
res = cmd('sudo nft list chain ip vyos_wanloadbalance wlb_mangle_prerouting')
101+
lines = res.split("\n")
102+
filtered_lines = [line.replace("\t", "") for line in lines[3:-2] if 'meta mark set' not in line]
103+
104+
if raw:
105+
return filtered_lines
106+
107+
for line in filtered_lines:
108+
print(line)
109+
110+
if __name__ == "__main__":
111+
try:
112+
res = vyos.opmode.run(sys.modules[__name__])
113+
if res:
114+
print(res)
115+
except (ValueError, vyos.opmode.Error) as e:
116+
print(e)
117+
sys.exit(1)

src/op_mode/restart.py

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
'systemd_service': 'strongswan',
5454
'path': ['vpn', 'ipsec'],
5555
},
56+
'load-balancing_wan': {
57+
'systemd_service': 'vyos-wan-load-balance',
58+
'path': ['load-balancing', 'wan'],
59+
},
5660
'mdns_repeater': {
5761
'systemd_service': 'avahi-daemon',
5862
'path': ['service', 'mdns', 'repeater'],
@@ -86,6 +90,7 @@
8690
'haproxy',
8791
'igmp_proxy',
8892
'ipsec',
93+
'load-balancing_wan',
8994
'mdns_repeater',
9095
'router_advert',
9196
'snmp',

0 commit comments

Comments
 (0)