-
Notifications
You must be signed in to change notification settings - Fork 799
Expand file tree
/
Copy pathmain.py
More file actions
196 lines (152 loc) · 5.11 KB
/
main.py
File metadata and controls
196 lines (152 loc) · 5.11 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
#!/usr/bin/env python3
#
# main.py
#
# Command-line utility for interacting with FAN 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_fanutil' command
@click.group()
def cli():
"""pddf_fanutil - Command line utility for providing FAN information"""
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 fanutil version {0}".format(VERSION))
# 'numfans' subcommand
@cli.command()
def numfans():
"""Display number of FANs installed on device"""
num_fans = platform_chassis.get_num_fans()
click.echo(num_fans)
# 'status' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="Index of FAN (1-based)")
def status(index):
"""Display FAN status"""
fan_list = []
if (index < 0):
fan_list = platform_chassis.get_all_fans()
default_index = 0
else:
fan_list = platform_chassis.get_fan(index-1)
default_index = index-1
header = ['FAN', 'Status']
status_table = []
for idx, fan in enumerate(fan_list, default_index):
fan_name = helper.try_get(fan.get_name, "Fan {}".format(idx+1))
status = 'NOT PRESENT'
if fan.get_presence():
oper_status = helper.try_get(fan.get_status, 'UNKNOWN')
if oper_status is True:
status = 'OK'
elif oper_status is False:
status = 'NOT OK'
else:
status = oper_status
status_table.append([fan_name, status])
if status_table:
click.echo(tabulate(status_table, header, tablefmt="simple"))
# 'direction' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="Index of FAN (1-based)")
def direction(index):
"""Display FAN airflow direction"""
fan_list = []
if (index < 0):
fan_list = platform_chassis.get_all_fans()
default_index = 0
else:
fan_list = platform_chassis.get_fan(index-1)
default_index = index-1
header = ['FAN', 'Direction']
dir_table = []
for idx, fan in enumerate(fan_list, default_index):
fan_name = helper.try_get(fan.get_name, "Fan {}".format(idx+1))
direction = helper.try_get(fan.get_direction, 'N/A')
dir_table.append([fan_name, direction.capitalize()])
if dir_table:
click.echo(tabulate(dir_table, header, tablefmt="simple"))
# 'speed' subcommand
@cli.command()
@click.option('-i', '--index', default=-1, type=int, help="Index of FAN (1-based)")
def getspeed(index):
"""Display FAN speed in RPM"""
fan_list = []
if (index < 0):
fan_list = platform_chassis.get_all_fans()
default_index = 0
else:
fan_list = platform_chassis.get_fan(index-1)
default_index = index-1
header = ['FAN', 'SPEED (RPM)']
speed_table = []
for idx, fan in enumerate(fan_list, default_index):
fan_name = helper.try_get(fan.get_name, "Fan {}".format(idx+1))
rpm = helper.try_get(fan.get_speed_rpm, 'N/A')
speed_table.append([fan_name, rpm])
if speed_table:
click.echo(tabulate(speed_table, header, tablefmt="simple"))
# 'setspeed' subcommand
@cli.command()
@click.argument('speed', type=int)
def setspeed(speed):
"""Set FAN speed in percentage"""
if speed is None:
click.echo("speed value is required")
raise click.Abort()
fan_list = platform_chassis.get_all_fans()
for idx, fan in enumerate(fan_list):
try:
status = fan.set_speed(speed)
except NotImplementedError:
click.echo("Set speed API not implemented")
sys.exit(0)
if not status:
click.echo("Failed")
sys.exit(1)
click.echo("Successful")
@cli.group()
def debug():
"""pddf_fanutil debug commands"""
pass
@debug.command()
def dump_sysfs():
"""Dump all Fan related SysFS paths"""
fan_list = platform_chassis.get_all_fans()
for idx, fan in enumerate(fan_list):
status = fan.dump_sysfs()
if status:
for i in status:
click.echo(i)
if __name__ == '__main__':
cli()