forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (108 loc) · 3.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
#
# main.py
#
# Command-line utility for interacting with Thermal sensors in PDDF mode in SONiC
#
try:
import sys
import os
import click
from tabulate import tabulate
from utilities_common.util_base import UtilHelper
except ImportError as e:
raise ImportError("%s - required module not found" % str(e))
VERSION = '2.0'
ERROR_PERMISSIONS = 1
ERROR_CHASSIS_LOAD = 2
ERROR_NOT_IMPLEMENTED = 3
ERROR_PDDF_NOT_SUPPORTED = 4
# Global platform-specific chassis class instance
platform_chassis = None
# Load the helper class
helper = UtilHelper()
# ==================== CLI commands and groups ====================
# This is our main entrypoint - the main 'thermalutil' command
@click.group()
def cli():
"""pddf_thermalutil - Command line utility for providing Temp Sensors information"""
global platform_chassis
if os.geteuid() != 0:
click.echo("Root privileges are required for this operation")
sys.exit(1)
if not helper.check_pddf_mode():
click.echo("PDDF mode should be supported and enabled for this platform for this operation")
sys.exit(1)
# Load platform-specific chassis 2.0 api class
platform_chassis = helper.load_platform_chassis()
if not platform_chassis:
sys.exit(ERROR_CHASSIS_LOAD)
# 'version' subcommand
@cli.command()
def version():
"""Display version info"""
click.echo("PDDF thermalutil version {0}".format(VERSION))
# 'numthermals' subcommand
@cli.command()
def numthermals():
"""Display number of Thermal Sensors installed """
num_thermals = platform_chassis.get_num_thermals()
click.echo(num_thermals)
# 'gettemp' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="Index of Temp Sensor (1-based)")
def gettemp(index):
"""Display Temperature values of thermal sensors"""
thermal_list = []
if (index < 0):
thermal_list = platform_chassis.get_all_thermals()
default_index = 0
else:
thermal_list = platform_chassis.get_thermal(index-1)
default_index = index-1
header = []
temp_table = []
for idx, thermal in enumerate(thermal_list, default_index):
thermal_name = helper.try_get(thermal.get_name, "TEMP{}".format(idx+1))
# TODO: Provide a wrapper API implementation for the below function
try:
temp = thermal.get_temperature()
if temp:
value = "temp1\t %+.1f C (" % temp
high = thermal.get_high_threshold()
if high:
value += "high = %+.1f C" % high
crit = thermal.get_high_critical_threshold()
if high and crit:
value += ", "
if crit:
value += "crit = %+.1f C" % crit
label = thermal.get_temp_label()
value += ")"
except NotImplementedError:
pass
if label is None:
temp_table.append([thermal_name, value])
else:
temp_table.append([thermal_name, label, value])
if temp_table:
if label is None:
header = ['Temp Sensor', 'Value']
else:
header = ['Temp Sensor', 'Label', 'Value']
click.echo(tabulate(temp_table, header, tablefmt="simple"))
@cli.group()
def debug():
"""pddf_thermalutil debug commands"""
pass
@debug.command()
def dump_sysfs():
"""Dump all Temp Sensor related SysFS paths"""
thermal_list = platform_chassis.get_all_thermals()
for idx, thermal in enumerate(thermal_list):
status = thermal.dump_sysfs()
if status:
for i in status:
click.echo(i)
if __name__ == '__main__':
cli()