1616
1717VERSION = '2.0'
1818
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
2223
23- # Global platform-specific fanutil class instance
24- platform_fanutil = None
24+ # Global platform-specific chassis class instance
2525platform_chassis = None
2626
27+ # Load the helper class
28+ helper = UtilHelper ()
2729
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 ====================
9831
9932
100- # This is our main entrypoint - the main 'fanutil ' command
33+ # This is our main entrypoint - the main 'pddf_fanutil ' command
10134@click .group ()
10235def cli ():
10336 """pddf_fanutil - Command line utility for providing FAN information"""
10437
105- global platform_fanutil
10638 global platform_chassis
10739
10840 if os .geteuid () != 0 :
10941 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 )
11443
11544 if not helper .check_pddf_mode ():
11645 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 )
12547
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 )
12652
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 )
13453
13554# 'version' subcommand
13655@cli .command ()
13756def version ():
13857 """Display version info"""
13958 click .echo ("PDDF fanutil version {0}" .format (VERSION ))
14059
60+
14161# 'numfans' subcommand
14262@cli .command ()
14363def numfans ():
14464 """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+
14668
14769# 'status' subcommand
14870@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) " )
15072def status (index ):
15173 """Display FAN status"""
152- supported_fan = list (range (1 , _wrapper_get_num_fans ()+ 1 ))
153- fan_ids = []
74+ fan_list = []
15475 if (index < 0 ):
155- fan_ids = supported_fan
76+ fan_list = platform_chassis .get_all_fans ()
77+ default_index = 0
15678 else :
157- fan_ids = [index ]
79+ fan_list = platform_chassis .get_fan (index - 1 )
80+ default_index = index - 1
15881
15982 header = ['FAN' , 'Status' ]
16083 status_table = []
16184
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 ])
17698
17799 if status_table :
178100 click .echo (tabulate (status_table , header , tablefmt = "simple" ))
179101
102+
180103# 'direction' subcommand
181104@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) " )
183106def direction (index ):
184107 """Display FAN airflow direction"""
185- supported_fan = list (range (1 , _wrapper_get_num_fans () + 1 ))
186- fan_ids = []
108+ fan_list = []
187109 if (index < 0 ):
188- fan_ids = supported_fan
110+ fan_list = platform_chassis .get_all_fans ()
111+ default_index = 0
189112 else :
190- fan_ids = [index ]
113+ fan_list = platform_chassis .get_fan (index - 1 )
114+ default_index = index - 1
191115
192116 header = ['FAN' , 'Direction' ]
193- status_table = []
117+ dir_table = []
194118
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" ))
203126
204- if status_table :
205- click .echo (tabulate (status_table , header , tablefmt = "simple" ))
206127
207128# 'speed' subcommand
208129@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) " )
210131def getspeed (index ):
211132 """Display FAN speed in RPM"""
212- supported_fan = list (range (1 , _wrapper_get_num_fans () + 1 ))
213- fan_ids = []
133+ fan_list = []
214134 if (index < 0 ):
215- fan_ids = supported_fan
135+ fan_list = platform_chassis .get_all_fans ()
136+ default_index = 0
216137 else :
217- fan_ids = [index ]
138+ fan_list = platform_chassis .get_fan (index - 1 )
139+ default_index = index - 1
218140
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 = []
225143
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 ])
234148
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" ))
239151
240- if status_table :
241- click .echo (tabulate (status_table , header , tablefmt = "simple" ))
242152
243153# 'setspeed' subcommand
244154@cli .command ()
@@ -249,30 +159,38 @@ def setspeed(speed):
249159 click .echo ("speed value is required" )
250160 raise click .Abort ()
251161
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+
254170 if not status :
255171 click .echo ("Failed" )
256172 sys .exit (1 )
257173
258174 click .echo ("Successful" )
259175
176+
260177@cli .group ()
261178def debug ():
262179 """pddf_fanutil debug commands"""
263180 pass
264181
182+
265183@debug .command ()
266184def dump_sysfs ():
267185 """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 ()
270189
271190 if status :
272191 for i in status :
273192 click .echo (i )
274193
275194
276-
277195if __name__ == '__main__' :
278196 cli ()
0 commit comments