Skip to content

Commit 2c9dfbc

Browse files
committed
add cli to show liquid cooling leakage status
Signed-off-by: Yuanzhe, Liu <[email protected]>
1 parent 1e9d04c commit 2c9dfbc

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

scripts/leakageshow

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Script to show leakage status.
5+
"""
6+
7+
import os
8+
import sys
9+
from tabulate import tabulate
10+
from natsort import natsorted
11+
12+
# mock the redis for unit test purposes #
13+
try:
14+
if os.environ["UTILITIES_UNIT_TESTING"] == "1":
15+
modules_path = os.path.join(os.path.dirname(__file__), "..")
16+
test_path = os.path.join(modules_path, "tests")
17+
sys.path.insert(0, modules_path)
18+
sys.path.insert(0, test_path)
19+
import mock_tables.dbconnector
20+
except KeyError:
21+
pass
22+
23+
from swsscommon.swsscommon import SonicV2Connector
24+
25+
header = ['Name', 'Leak']
26+
27+
LIQUID_COOLING_TABLE_NAME = 'LIQUID_COOLING_INFO'
28+
STATUS_FIELD_NAME = 'leak_status'
29+
30+
class LeakageShow(object):
31+
def __init__(self):
32+
self.db = SonicV2Connector(host="127.0.0.1")
33+
self.db.connect(self.db.STATE_DB)
34+
35+
def show(self):
36+
keys = self.db.keys(self.db.STATE_DB, LIQUID_COOLING_TABLE_NAME + '*')
37+
if not keys:
38+
print('Leakage sensor Not detected\n')
39+
return
40+
41+
table = []
42+
for key in natsorted(keys):
43+
key_list = key.split('|')
44+
if len(key_list) != 2: # error data in DB, log it and ignore
45+
print('Warn: Invalid key in table LIQUID_COOLING_INFO: {}'.format(key))
46+
continue
47+
48+
name = key_list[1]
49+
data_dict = self.db.get_all(self.db.STATE_DB, key)
50+
status = data_dict[STATUS_FIELD_NAME]
51+
52+
table.append((name, status))
53+
54+
if table:
55+
print(tabulate(table, header, tablefmt='simple', stralign='right'))
56+
else:
57+
print('No leakage status data available\n')
58+
59+
60+
if __name__ == "__main__":
61+
leakageShow = LeakageShow()
62+
leakageShow.show()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
'scripts/intfstat',
145145
'scripts/ipintutil',
146146
'scripts/lag_keepalive.py',
147+
'scripts/leakageshow',
147148
'scripts/lldpshow',
148149
'scripts/log_ssd_health',
149150
'scripts/mellanox_buffer_migrator.py',

show/platform.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,17 @@ def firmware(args):
198198
subprocess.check_call(cmd)
199199
except subprocess.CalledProcessError as e:
200200
sys.exit(e.returncode)
201+
202+
203+
# 'leakage' subcommand ("show platform leakage status")
204+
@platform.group()
205+
def leakage():
206+
"""Show platform leakage information"""
207+
pass
208+
209+
210+
@leakage.command()
211+
def status():
212+
"""Show platform leakage status"""
213+
cmd = ["leakageshow"]
214+
clicommon.run_command(cmd)

tests/leakage_status_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
import os
3+
from click.testing import CliRunner
4+
import show.main as show
5+
6+
test_path = os.path.dirname(os.path.abspath(__file__))
7+
modules_path = os.path.dirname(test_path)
8+
scripts_path = os.path.join(modules_path, "scripts")
9+
sys.path.insert(0, modules_path)
10+
11+
12+
class TestFan(object):
13+
@classmethod
14+
def setup_class(cls):
15+
print("SETUP")
16+
os.environ["PATH"] += os.pathsep + scripts_path
17+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
18+
19+
def test_show_platform_leakage_status(self):
20+
runner = CliRunner()
21+
result = runner.invoke(show.cli.commands["platform"].commands["leakage"].commands["status"])
22+
print(result.output)
23+
expected = """\
24+
Name Leak
25+
-------- ------
26+
leakage1 No
27+
leakage2 No
28+
leakage3 Yes
29+
"""
30+
31+
assert result.output == expected
32+
33+
@classmethod
34+
def teardown_class(cls):
35+
print("TEARDOWN")
36+
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
37+
os.environ["UTILITIES_UNIT_TESTING"] = "0"

tests/mock_tables/state_db.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,15 @@
12491249
"14": "200:200:200:200::5@Vlan1000",
12501250
"15": "200:200:200:200::5@Vlan1000"
12511251
},
1252+
"LIQUID_COOLING_INFO|leakage1": {
1253+
"leak_status": "No"
1254+
},
1255+
"LIQUID_COOLING_INFO|leakage2": {
1256+
"leak_status": "No"
1257+
},
1258+
"LIQUID_COOLING_INFO|leakage3": {
1259+
"leak_status": "Yes"
1260+
},
12521261
"FG_ROUTE_TABLE|100.50.25.12/32": {
12531262
"0": "200.200.200.4@Vlan1000",
12541263
"1": "200.200.200.4@Vlan1000",

0 commit comments

Comments
 (0)