Skip to content

Commit f201240

Browse files
committedOct 8, 2024
wlb: T4470: Support WLB op-mode commands
1 parent 1514a4e commit f201240

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
 
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 systemctl restart vyos-wan-load-balance.service</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

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
def show_connection(raw: bool):
87+
res = cmd('sudo conntrack -L -n')
88+
lines = res.split("\n")
89+
filtered_lines = [line for line in lines if re.search(r' mark=[1-9]', line)]
90+
91+
if raw:
92+
return filtered_lines
93+
94+
for line in lines:
95+
print(line)
96+
97+
def show_status(raw: bool):
98+
res = cmd('sudo nft list chain ip vyos_wanloadbalance wlb_mangle_prerouting')
99+
lines = res.split("\n")
100+
filtered_lines = [line.replace("\t", "") for line in lines[3:-2] if 'meta mark set' not in line]
101+
102+
if raw:
103+
return filtered_lines
104+
105+
for line in filtered_lines:
106+
print(line)
107+
108+
if __name__ == "__main__":
109+
try:
110+
res = vyos.opmode.run(sys.modules[__name__])
111+
if res:
112+
print(res)
113+
except (ValueError, vyos.opmode.Error) as e:
114+
print(e)
115+
sys.exit(1)

0 commit comments

Comments
 (0)