|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | + Script to show system ready status. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import argparse |
| 10 | +from tabulate import tabulate |
| 11 | +from natsort import natsorted |
| 12 | + |
| 13 | +# mock the redis for unit test purposes # |
| 14 | +try: |
| 15 | + if os.environ["UTILITIES_UNIT_TESTING"] == "1": |
| 16 | + modules_path = os.path.join(os.path.dirname(__file__), "..") |
| 17 | + test_path = os.path.join(modules_path, "tests") |
| 18 | + sys.path.insert(0, modules_path) |
| 19 | + sys.path.insert(0, test_path) |
| 20 | + import mock_tables.dbconnector #lgtm [py/unused-import] |
| 21 | +except KeyError: |
| 22 | + pass |
| 23 | + |
| 24 | +from swsscommon.swsscommon import SonicV2Connector |
| 25 | + |
| 26 | +header = ['Service-Name', 'Service-Status', 'App-Ready-Status', 'Down-Reason'] |
| 27 | +header_detail = ['Service-Name', 'Service-Status', 'App-Ready-Status', 'Down-Reason', 'AppStatus-UpdateTime'] |
| 28 | + |
| 29 | +SERVICE_STATUS_TABLE = 'ALL_SERVICE_STATUS' |
| 30 | +SYSREADY_TABLE = "SYSTEM_READY|SYSTEM_STATE" |
| 31 | +SERVICE_STATUS = 'service_status' |
| 32 | +APP_READY_STATUS = 'app_ready_status' |
| 33 | +FAIL_REASON = 'fail_reason' |
| 34 | +UPDATE_TIME = 'update_time' |
| 35 | + |
| 36 | +class SysreadyShow(object): |
| 37 | + def __init__(self): |
| 38 | + self.db = SonicV2Connector(host="127.0.0.1") |
| 39 | + self.db.connect(self.db.STATE_DB) |
| 40 | + |
| 41 | + def show(self, detailed_info): |
| 42 | + keys = self.db.keys(self.db.STATE_DB, SERVICE_STATUS_TABLE + '*') |
| 43 | + if not keys: |
| 44 | + print('No system ready status data available - system-health service might be down\n') |
| 45 | + return |
| 46 | + |
| 47 | + sysready_state = self.db.get(self.db.STATE_DB, SYSREADY_TABLE, "Status") |
| 48 | + if sysready_state == "UP": |
| 49 | + print("System is ready\n") |
| 50 | + else: |
| 51 | + print("System is not ready - one or more services are not up\n") |
| 52 | + |
| 53 | + #When brief option is specified, return here. |
| 54 | + if detailed_info == False: |
| 55 | + return |
| 56 | + |
| 57 | + if detailed_info is None: |
| 58 | + header_info = header |
| 59 | + else: |
| 60 | + header_info = header_detail |
| 61 | + |
| 62 | + table = [] |
| 63 | + for key in natsorted(keys): |
| 64 | + key_list = key.split('|') |
| 65 | + if len(key_list) != 2: # error data in DB, log it and ignore |
| 66 | + print('Warn: Invalid key in table {}: {}'.format(SERVICE_STATUS_TABLE, key)) |
| 67 | + continue |
| 68 | + |
| 69 | + name = key_list[1] |
| 70 | + data_dict = self.db.get_all(self.db.STATE_DB, key) |
| 71 | + try: |
| 72 | + service_status = data_dict[SERVICE_STATUS] |
| 73 | + app_ready_status = data_dict[APP_READY_STATUS] |
| 74 | + fail_reason = data_dict[FAIL_REASON] |
| 75 | + update_time = data_dict[UPDATE_TIME] |
| 76 | + except ValueError as e: |
| 77 | + print('Error in data_dict') |
| 78 | + |
| 79 | + if detailed_info is None: |
| 80 | + table.append((name, service_status, app_ready_status, fail_reason)) |
| 81 | + else: |
| 82 | + table.append((name, service_status, app_ready_status, fail_reason, update_time)) |
| 83 | + |
| 84 | + |
| 85 | + if table: |
| 86 | + print(tabulate(table, header_info, tablefmt='simple', stralign='left')) |
| 87 | + else: |
| 88 | + print('No sysready status data available\n') |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + parser = argparse.ArgumentParser(description='Display the System Ready status', |
| 93 | + formatter_class=argparse.RawTextHelpFormatter, |
| 94 | + epilog=""" |
| 95 | + Examples: |
| 96 | + sysreadyshow |
| 97 | + sysreadyshow --brief |
| 98 | + sysreadyshow --detail |
| 99 | + """) |
| 100 | + |
| 101 | + parser.add_argument('-b', '--brief', action='store_true', help='brief system ready status', default=False) |
| 102 | + parser.add_argument('-d', '--detail', action='store_true', help='detailed system ready status', default=False) |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + try: |
| 106 | + sysready = SysreadyShow() |
| 107 | + if args.detail: |
| 108 | + detailed_info = True |
| 109 | + elif args.brief: |
| 110 | + detailed_info = False |
| 111 | + else: |
| 112 | + detailed_info = None |
| 113 | + sysready.show(detailed_info) |
| 114 | + except Exception as e: |
| 115 | + print(str(e), file=sys.stderr) |
| 116 | + sys.exit(1) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments