|
| 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() |
0 commit comments