16
16
17
17
VERSION = '2.0'
18
18
19
- SYSLOG_IDENTIFIER = "fanutil"
20
- PLATFORM_SPECIFIC_MODULE_NAME = "fanutil"
21
- PLATFORM_SPECIFIC_CLASS_NAME = "FanUtil"
19
+ ERROR_PERMISSIONS = 1
20
+ ERROR_CHASSIS_LOAD = 2
21
+ ERROR_NOT_IMPLEMENTED = 3
22
+ ERROR_PDDF_NOT_SUPPORTED = 4
22
23
23
- # Global platform-specific fanutil class instance
24
- platform_fanutil = None
24
+ # Global platform-specific chassis class instance
25
25
platform_chassis = None
26
26
27
+ # Load the helper class
28
+ helper = UtilHelper ()
27
29
28
- def _wrapper_get_num_fans ():
29
- if platform_chassis is not None :
30
- try :
31
- return platform_chassis .get_num_fans ()
32
- except NotImplementedError :
33
- pass
34
- return platform_fanutil .get_num_fans ()
35
-
36
- def _wrapper_get_fan_name (idx ):
37
- if platform_chassis is not None :
38
- try :
39
- return platform_chassis .get_fan (idx - 1 ).get_name ()
40
- except NotImplementedError :
41
- pass
42
- return "FAN {}" .format (idx )
43
-
44
- def _wrapper_get_fan_presence (idx ):
45
- if platform_chassis is not None :
46
- try :
47
- return platform_chassis .get_fan (idx - 1 ).get_presence ()
48
- except NotImplementedError :
49
- pass
50
- return platform_fanutil .get_presence (idx )
51
-
52
- def _wrapper_get_fan_status (idx ):
53
- if platform_chassis is not None :
54
- try :
55
- return platform_chassis .get_fan (idx - 1 ).get_status ()
56
- except NotImplementedError :
57
- pass
58
- return platform_fanutil .get_status (idx )
59
-
60
- def _wrapper_get_fan_direction (idx ):
61
- if platform_chassis is not None :
62
- try :
63
- return platform_chassis .get_fan (idx - 1 ).get_direction ()
64
- except NotImplementedError :
65
- pass
66
- return platform_fanutil .get_direction (idx )
67
-
68
- def _wrapper_get_fan_speed (idx ):
69
- if platform_chassis is not None :
70
- try :
71
- return platform_chassis .get_fan (idx - 1 ).get_speed_rpm ()
72
- except NotImplementedError :
73
- pass
74
- return platform_fanutil .get_speed (idx )
75
-
76
- def _wrapper_get_fan_speed_rear (idx ):
77
- if platform_chassis is not None :
78
- # This wrapper API is invalid for Pl API 2.0 as every fan
79
- # is treated as a separate fan
80
- return 0
81
- return platform_fanutil .get_speed_rear (idx )
82
-
83
- def _wrapper_set_fan_speed (idx , percent ):
84
- if platform_chassis is not None :
85
- try :
86
- return platform_chassis .get_fan (idx - 1 ).set_speed (percent )
87
- except NotImplementedError :
88
- pass
89
- return platform_fanutil .set_speed (percent )
90
-
91
- def _wrapper_dump_sysfs (idx ):
92
- if platform_chassis is not None :
93
- try :
94
- return platform_chassis .get_fan (idx ).dump_sysfs ()
95
- except NotImplementedError :
96
- pass
97
- return platform_fanutil .dump_sysfs ()
30
+ # ==================== CLI commands and groups ====================
98
31
99
32
100
- # This is our main entrypoint - the main 'fanutil ' command
33
+ # This is our main entrypoint - the main 'pddf_fanutil ' command
101
34
@click .group ()
102
35
def cli ():
103
36
"""pddf_fanutil - Command line utility for providing FAN information"""
104
37
105
- global platform_fanutil
106
38
global platform_chassis
107
39
108
40
if os .geteuid () != 0 :
109
41
click .echo ("Root privileges are required for this operation" )
110
- sys .exit (1 )
111
-
112
- # Load the helper class
113
- helper = UtilHelper ()
42
+ sys .exit (ERROR_PERMISSIONS )
114
43
115
44
if not helper .check_pddf_mode ():
116
45
click .echo ("PDDF mode should be supported and enabled for this platform for this operation" )
117
- sys .exit (1 )
118
-
119
- # Load new platform api class
120
- try :
121
- import sonic_platform .platform
122
- platform_chassis = sonic_platform .platform .Platform ().get_chassis ()
123
- except Exception as e :
124
- click .echo ("Failed to load chassis due to {}" .format (str (e )))
46
+ sys .exit (ERROR_PDDF_NOT_SUPPORTED )
125
47
48
+ # Load platform-specific chassis 2.0 api class
49
+ platform_chassis = helper .load_platform_chassis ()
50
+ if not platform_chassis :
51
+ sys .exit (ERROR_CHASSIS_LOAD )
126
52
127
- # Load platform-specific fanutil class if new platform object class is not found
128
- if platform_chassis is None :
129
- try :
130
- platform_fanutil = helper .load_platform_util (PLATFORM_SPECIFIC_MODULE_NAME , PLATFORM_SPECIFIC_CLASS_NAME )
131
- except Exception as e :
132
- click .echo ("Failed to load {}: {}" .format (PLATFORM_SPECIFIC_MODULE_NAME , str (e )))
133
- sys .exit (2 )
134
53
135
54
# 'version' subcommand
136
55
@cli .command ()
137
56
def version ():
138
57
"""Display version info"""
139
58
click .echo ("PDDF fanutil version {0}" .format (VERSION ))
140
59
60
+
141
61
# 'numfans' subcommand
142
62
@cli .command ()
143
63
def numfans ():
144
64
"""Display number of FANs installed on device"""
145
- click .echo (_wrapper_get_num_fans ())
65
+ num_fans = platform_chassis .get_num_fans ()
66
+ click .echo (num_fans )
67
+
146
68
147
69
# 'status' subcommand
148
70
@cli .command ()
149
- @click .option ('-i' , '--index' , default = - 1 , type = int , help = "the index of FAN" )
71
+ @click .option ('-i' , '--index' , default = - 1 , type = int , help = "Index of FAN (1-based) " )
150
72
def status (index ):
151
73
"""Display FAN status"""
152
- supported_fan = list (range (1 , _wrapper_get_num_fans ()+ 1 ))
153
- fan_ids = []
74
+ fan_list = []
154
75
if (index < 0 ):
155
- fan_ids = supported_fan
76
+ fan_list = platform_chassis .get_all_fans ()
77
+ default_index = 0
156
78
else :
157
- fan_ids = [index ]
79
+ fan_list = platform_chassis .get_fan (index - 1 )
80
+ default_index = index - 1
158
81
159
82
header = ['FAN' , 'Status' ]
160
83
status_table = []
161
84
162
- for fan in fan_ids :
163
- msg = ""
164
- fan_name = _wrapper_get_fan_name (fan )
165
- if fan not in supported_fan :
166
- click .echo ("Error! The {} is not available on the platform.\n " \
167
- "Number of supported FAN - {}." .format (fan_name , len (supported_fan )))
168
- continue
169
- presence = _wrapper_get_fan_presence (fan )
170
- if presence :
171
- oper_status = _wrapper_get_fan_status (fan )
172
- msg = 'OK' if oper_status else "NOT OK"
173
- else :
174
- msg = 'NOT PRESENT'
175
- status_table .append ([fan_name , msg ])
85
+ for idx , fan in enumerate (fan_list , default_index ):
86
+ fan_name = helper .try_get (fan .get_name , "Fan {}" .format (idx + 1 ))
87
+ status = 'NOT PRESENT'
88
+ if fan .get_presence ():
89
+ oper_status = helper .try_get (fan .get_status , 'UNKNOWN' )
90
+ if oper_status is True :
91
+ status = 'OK'
92
+ elif oper_status is False :
93
+ status = 'NOT OK'
94
+ else :
95
+ status = oper_status
96
+
97
+ status_table .append ([fan_name , status ])
176
98
177
99
if status_table :
178
100
click .echo (tabulate (status_table , header , tablefmt = "simple" ))
179
101
102
+
180
103
# 'direction' subcommand
181
104
@cli .command ()
182
- @click .option ('-i' , '--index' , default = - 1 , type = int , help = "the index of FAN" )
105
+ @click .option ('-i' , '--index' , default = - 1 , type = int , help = "Index of FAN (1-based) " )
183
106
def direction (index ):
184
107
"""Display FAN airflow direction"""
185
- supported_fan = list (range (1 , _wrapper_get_num_fans () + 1 ))
186
- fan_ids = []
108
+ fan_list = []
187
109
if (index < 0 ):
188
- fan_ids = supported_fan
110
+ fan_list = platform_chassis .get_all_fans ()
111
+ default_index = 0
189
112
else :
190
- fan_ids = [index ]
113
+ fan_list = platform_chassis .get_fan (index - 1 )
114
+ default_index = index - 1
191
115
192
116
header = ['FAN' , 'Direction' ]
193
- status_table = []
117
+ dir_table = []
194
118
195
- for fan in fan_ids :
196
- fan_name = _wrapper_get_fan_name (fan )
197
- if fan not in supported_fan :
198
- click .echo ("Error! The {} is not available on the platform.\n " \
199
- "Number of supported FAN - {}." .format (fan_name , len (supported_fan )))
200
- continue
201
- direction = _wrapper_get_fan_direction (fan )
202
- status_table .append ([fan_name , direction .capitalize ()])
119
+ for idx , fan in enumerate (fan_list , default_index ):
120
+ fan_name = helper .try_get (fan .get_name , "Fan {}" .format (idx + 1 ))
121
+ direction = helper .try_get (fan .get_direction , 'N/A' )
122
+ dir_table .append ([fan_name , direction .capitalize ()])
123
+
124
+ if dir_table :
125
+ click .echo (tabulate (dir_table , header , tablefmt = "simple" ))
203
126
204
- if status_table :
205
- click .echo (tabulate (status_table , header , tablefmt = "simple" ))
206
127
207
128
# 'speed' subcommand
208
129
@cli .command ()
209
- @click .option ('-i' , '--index' , default = - 1 , type = int , help = "the index of FAN" )
130
+ @click .option ('-i' , '--index' , default = - 1 , type = int , help = "Index of FAN (1-based) " )
210
131
def getspeed (index ):
211
132
"""Display FAN speed in RPM"""
212
- supported_fan = list (range (1 , _wrapper_get_num_fans () + 1 ))
213
- fan_ids = []
133
+ fan_list = []
214
134
if (index < 0 ):
215
- fan_ids = supported_fan
135
+ fan_list = platform_chassis .get_all_fans ()
136
+ default_index = 0
216
137
else :
217
- fan_ids = [index ]
138
+ fan_list = platform_chassis .get_fan (index - 1 )
139
+ default_index = index - 1
218
140
219
- if platform_chassis is not None :
220
- header = ['FAN' , 'SPEED (RPM)' ]
221
- else :
222
- header = ['FAN' , 'Front Fan RPM' , 'Rear Fan RPM' ]
223
-
224
- status_table = []
141
+ header = ['FAN' , 'SPEED (RPM)' ]
142
+ speed_table = []
225
143
226
- for fan in fan_ids :
227
- fan_name = _wrapper_get_fan_name (fan )
228
- if fan not in supported_fan :
229
- click .echo ("Error! The {} is not available on the platform.\n " \
230
- "Number of supported FAN - {}." .format (fan_name , len (supported_fan )))
231
- continue
232
- front = _wrapper_get_fan_speed (fan )
233
- rear = _wrapper_get_fan_speed_rear (fan )
144
+ for idx , fan in enumerate (fan_list , default_index ):
145
+ fan_name = helper .try_get (fan .get_name , "Fan {}" .format (idx + 1 ))
146
+ rpm = helper .try_get (fan .get_speed_rpm , 'N/A' )
147
+ speed_table .append ([fan_name , rpm ])
234
148
235
- if platform_chassis is not None :
236
- status_table .append ([fan_name , front ])
237
- else :
238
- status_table .append ([fan_name , front , rear ])
149
+ if speed_table :
150
+ click .echo (tabulate (speed_table , header , tablefmt = "simple" ))
239
151
240
- if status_table :
241
- click .echo (tabulate (status_table , header , tablefmt = "simple" ))
242
152
243
153
# 'setspeed' subcommand
244
154
@cli .command ()
@@ -249,30 +159,38 @@ def setspeed(speed):
249
159
click .echo ("speed value is required" )
250
160
raise click .Abort ()
251
161
252
- for fan in range (_wrapper_get_num_fans ()):
253
- status = _wrapper_set_fan_speed (fan , speed )
162
+ fan_list = platform_chassis .get_all_fans ()
163
+ for idx , fan in enumerate (fan_list ):
164
+ try :
165
+ status = fan .set_speed (speed )
166
+ except NotImplementedError :
167
+ click .echo ("Set speed API not implemented" )
168
+ sys .exit (0 )
169
+
254
170
if not status :
255
171
click .echo ("Failed" )
256
172
sys .exit (1 )
257
173
258
174
click .echo ("Successful" )
259
175
176
+
260
177
@cli .group ()
261
178
def debug ():
262
179
"""pddf_fanutil debug commands"""
263
180
pass
264
181
182
+
265
183
@debug .command ()
266
184
def dump_sysfs ():
267
185
"""Dump all Fan related SysFS paths"""
268
- for fan in range (_wrapper_get_num_fans ()):
269
- status = _wrapper_dump_sysfs (fan )
186
+ fan_list = platform_chassis .get_all_fans ()
187
+ for idx , fan in enumerate (fan_list ):
188
+ status = fan .dump_sysfs ()
270
189
271
190
if status :
272
191
for i in status :
273
192
click .echo (i )
274
193
275
194
276
-
277
195
if __name__ == '__main__' :
278
196
cli ()
0 commit comments