-
Notifications
You must be signed in to change notification settings - Fork 799
Expand file tree
/
Copy pathmain.py
More file actions
211 lines (168 loc) · 6.15 KB
/
main.py
File metadata and controls
211 lines (168 loc) · 6.15 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
#
# main.py
#
# Command-line utility for interacting with PSU Controller 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 'pddf_psuutil' command
@click.group()
def cli():
"""pddf_psuutil - Command line utility for providing PSU status for a platform using PDDF"""
global platform_chassis
if os.geteuid() != 0:
click.echo("Root privileges are required for this operation")
sys.exit(ERROR_PERMISSIONS)
if not helper.check_pddf_mode():
click.echo("PDDF mode should be supported and enabled for this platform for this operation")
sys.exit(ERROR_PDDF_NOT_SUPPORTED)
# 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 psuutil version {0}".format(VERSION))
# 'numpsus' subcommand
@cli.command()
def numpsus():
"""Display number of supported PSUs on device"""
num_psus = platform_chassis.get_num_psus()
click.echo(str(num_psus))
# 'status' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="Index of the PSU (1-based)")
def status(index):
"""Display PSU status"""
psu_list = []
if (index < 0):
psu_list = platform_chassis.get_all_psus()
default_index = 0
else:
psu_list = platform_chassis.get_psu(index-1)
default_index = index-1
header = ['PSU', 'Status']
status_table = []
for idx, psu in enumerate(psu_list, default_index):
psu_name = helper.try_get(psu.get_name, "PSU {}".format(idx+1))
status = 'NOT PRESENT'
if psu.get_presence():
oper_status = helper.try_get(psu.get_powergood_status, 'UNKNOWN')
if oper_status is True:
status = 'OK'
elif oper_status is False:
status = 'NOT OK'
else:
status = oper_status
status_table.append([psu_name, status])
if status_table:
click.echo(tabulate(status_table, header, tablefmt='simple'))
# 'mfrinfo' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="Index of the PSU (1-based)")
def mfrinfo(index):
"""Display PSU manufacturer info"""
psu_list = []
if (index < 0):
psu_list = platform_chassis.get_all_psus()
default_index = 0
else:
psu_list = platform_chassis.get_psu(index-1)
default_index = index-1
header = ['PSU', 'Status', 'Manufacturer ID', 'Model', 'Serial', 'Fan Airflow Direction']
mfrinfo_table = []
for idx, psu in enumerate(psu_list, default_index):
psu_name = helper.try_get(psu.get_name, "PSU {}".format(idx+1))
status = 'NOT PRESENT'
model_name = 'N/A'
mfr_id = 'N/A'
serial_num = 'N/A'
airflow_dir = 'N/A'
if psu.get_presence():
oper_status = helper.try_get(psu.get_powergood_status, 'UNKNOWN')
if oper_status is True:
status = 'OK'
elif oper_status is False:
status = 'NOT OK'
else:
status = oper_status
model_name = helper.try_get(psu.get_model, 'N/A')
mfr_id = helper.try_get(psu.get_mfr_id, 'N/A')
serial_num = helper.try_get(psu.get_serial, 'N/A')
airflow_dir = helper.try_get(psu._fan_list[0].get_direction, 'N/A')
mfrinfo_table.append([psu_name, status, mfr_id, model_name, serial_num, airflow_dir])
if mfrinfo_table:
click.echo(tabulate(mfrinfo_table, header, tablefmt='simple'))
# 'seninfo' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="the index of PSU")
def seninfo(index):
"""Display PSU sensor info"""
psu_list = []
if (index < 0):
psu_list = platform_chassis.get_all_psus()
default_index = 0
else:
psu_list = platform_chassis.get_psu(index-1)
default_index = index-1
header = ['PSU', 'Status', 'Output Voltage (V)', 'Output Current (A)',
'Output Power (W)', 'Temperature1 (C)', 'Fan1 Speed (RPM)']
seninfo_table = []
for idx, psu in enumerate(psu_list, default_index):
psu_name = helper.try_get(psu.get_name, "PSU {}".format(idx+1))
status = 'NOT PRESENT'
v_out = 'N/A'
i_out = 'N/A'
p_out = 'N/A'
temp1 = 'N/A'
fan1_rpm = 'N/A'
if psu.get_presence():
oper_status = helper.try_get(psu.get_powergood_status, 'UNKNOWN')
if oper_status is True:
status = 'OK'
elif oper_status is False:
status = 'NOT OK'
else:
status = oper_status
v_out = helper.try_get(psu.get_voltage, 'N/A')
i_out = helper.try_get(psu.get_current, 'N/A')
p_out = helper.try_get(psu.get_power, 'N/A')
temp1 = helper.try_get(psu.get_temperature, 'N/A')
fan1_rpm = helper.try_get(psu._fan_list[0].get_speed_rpm, 'N/A')
seninfo_table.append([psu_name, status, v_out, i_out, p_out, temp1, fan1_rpm])
if seninfo_table:
click.echo(tabulate(seninfo_table, header, tablefmt='simple', floatfmt='.2f'))
@cli.group()
def debug():
"""pddf_psuutil debug commands"""
pass
@debug.command()
def dump_sysfs():
"""Dump all PSU related SysFS paths"""
psu_list = platform_chassis.get_all_psus()
for psu in psu_list:
status = psu.dump_sysfs()
if status:
for i in status:
click.echo(i)
if __name__ == '__main__':
cli()