|
| 1 | +#!/usr/bin/python |
| 2 | +""" |
| 3 | + Script to show fan status. |
| 4 | +""" |
| 5 | +from __future__ import print_function |
| 6 | + |
| 7 | +import argparse |
| 8 | + |
| 9 | +from tabulate import tabulate |
| 10 | +from swsssdk import SonicV2Connector |
| 11 | + |
| 12 | + |
| 13 | +header = ['NAME', 'Temperature', 'High Threshold', 'Low Threshold', 'Critical High Threshold', 'Critical Low Threshold', 'Warning Status', 'Timestamp'] |
| 14 | + |
| 15 | +TEMPER_TABLE_NAME = 'TEMPERATURE_INFO' |
| 16 | +TEMPER_FIELD_NAME = 'temperature' |
| 17 | +TIMESTAMP_FIELD_NAME = 'timestamp' |
| 18 | +HIGH_THRESH_FIELD_NAME = 'high_threshold' |
| 19 | +LOW_THRESH_FIELD_NAME = 'low_threshold' |
| 20 | +CRIT_HIGH_THRESH_FIELD_NAME = 'critical_high_threshold' |
| 21 | +CRIT_LOW_THRESH_FIELD_NAME = 'critical_low_threshold' |
| 22 | +WARNING_STATUS_FIELD_NAME = 'warning_status' |
| 23 | + |
| 24 | + |
| 25 | +class TemperShow(object): |
| 26 | + def __init__(self): |
| 27 | + self.db = SonicV2Connector(host="127.0.0.1") |
| 28 | + self.db.connect(self.db.STATE_DB) |
| 29 | + |
| 30 | + def show(self): |
| 31 | + keys = self.db.keys(self.db.STATE_DB, TEMPER_TABLE_NAME + '*') |
| 32 | + if not keys: |
| 33 | + print('Thermal Not detected\n') |
| 34 | + return |
| 35 | + |
| 36 | + table = [] |
| 37 | + for key in keys: |
| 38 | + key_list = key.split('|') |
| 39 | + if len(key_list) != 2: # error data in DB, log it and ignore |
| 40 | + print('Warn: Invalid key in table {}: {}'.format(TEMPER_TABLE_NAME, key)) |
| 41 | + continue |
| 42 | + |
| 43 | + name = key_list[1] |
| 44 | + data_dict = self.db.get_all(self.db.STATE_DB, key) |
| 45 | + table.append((name, |
| 46 | + data_dict[TEMPER_FIELD_NAME], |
| 47 | + data_dict[HIGH_THRESH_FIELD_NAME], |
| 48 | + data_dict[LOW_THRESH_FIELD_NAME], |
| 49 | + data_dict[CRIT_HIGH_THRESH_FIELD_NAME], |
| 50 | + data_dict[CRIT_LOW_THRESH_FIELD_NAME], |
| 51 | + data_dict[WARNING_STATUS_FIELD_NAME], |
| 52 | + data_dict[TIMESTAMP_FIELD_NAME] |
| 53 | + )) |
| 54 | + |
| 55 | + if table: |
| 56 | + table.sort() |
| 57 | + print(tabulate(table, header, tablefmt='simple', stralign='right')) |
| 58 | + else: |
| 59 | + print('No tempeature data available\n') |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + temperShow = TemperShow() |
| 64 | + temperShow.show() |
0 commit comments