-
Notifications
You must be signed in to change notification settings - Fork 157
/
thermalctld
867 lines (724 loc) · 33.7 KB
/
thermalctld
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
#!/usr/bin/env python3
"""
thermalctld
Thermal control daemon for SONiC
"""
import signal
import sys
import threading
import time
from datetime import datetime
import sonic_platform
from sonic_py_common import daemon_base, logger
from sonic_py_common.task_base import ProcessTaskBase
from swsscommon import swsscommon
# TODO: Once we no longer support Python 2, we can eliminate this and get the
# name using the 'name' field (e.g., `signal.SIGINT.name`) starting with Python 3.5
SIGNALS_TO_NAMES_DICT = dict((getattr(signal, n), n)
for n in dir(signal) if n.startswith('SIG') and '_' not in n)
SYSLOG_IDENTIFIER = 'thermalctld'
NOT_AVAILABLE = 'N/A'
CHASSIS_INFO_KEY = 'chassis 1'
PHYSICAL_ENTITY_INFO_TABLE = 'PHYSICAL_ENTITY_INFO'
INVALID_SLOT = -1
ERR_UNKNOWN = 1
# Thermal control daemon is designed to never exit, it must always
# return non-zero exit code when exiting and so that supervisord will
# restart it automatically.
exit_code = ERR_UNKNOWN
# utility functions
def try_get(callback, default=NOT_AVAILABLE):
"""
Handy function to invoke the callback and catch NotImplementedError
:param callback: Callback to be invoked
:param default: Default return value if exception occur
:return: Default return value if exception occur else return value of the callback
"""
try:
ret = callback()
if ret is None:
ret = default
except NotImplementedError:
ret = default
return ret
def update_entity_info(table, parent_name, key, device, device_index):
fvs = swsscommon.FieldValuePairs(
[('position_in_parent', str(try_get(device.get_position_in_parent, device_index))),
('parent_name', parent_name)])
table.set(key, fvs)
class FanStatus(logger.Logger):
absent_fan_count = 0
faulty_fan_count = 0
def __init__(self, fan=None, is_psu_fan=False):
"""
Initializer of FanStatus
"""
super(FanStatus, self).__init__(SYSLOG_IDENTIFIER)
self.fan = fan
self.is_psu_fan = is_psu_fan
self.presence = True
self.status = True
self.under_speed = False
self.over_speed = False
self.invalid_direction = False
self.led_initialized = False
@classmethod
def get_bad_fan_count(cls):
return cls.absent_fan_count + cls.faulty_fan_count
@classmethod
def reset_fan_counter(cls):
cls.absent_fan_count = 0
cls.faulty_fan_count = 0
def set_presence(self, presence):
"""
Set and cache Fan presence status
:param presence: Fan presence status
:return: True if status changed else False
"""
if not presence and not self.is_psu_fan:
FanStatus.absent_fan_count += 1
if presence == self.presence:
return False
self.presence = presence
return True
def set_fault_status(self, status):
"""
Set and cache Fan fault status
:param status: Fan fault status, False indicate Fault
:return: True if status changed else False
"""
if not status:
FanStatus.faulty_fan_count += 1
if status == self.status:
return False
self.status = status
return True
def _check_speed_value_available(self, speed, target_speed, tolerance, current_status):
if speed == NOT_AVAILABLE or target_speed == NOT_AVAILABLE or tolerance == NOT_AVAILABLE:
if isinstance(tolerance, int) and (tolerance > 100 or tolerance < 0):
self.log_warning('Invalid tolerance value: {}'.format(tolerance))
return False
if current_status is True:
self.log_warning('Fan speed or target_speed or tolerance became unavailable, '
'speed={}, target_speed={}, tolerance={}'.format(speed, target_speed, tolerance))
return False
return True
def set_under_speed(self, speed, target_speed, tolerance):
"""
Set and cache Fan under speed status
:param speed: Fan speed
:param target_speed: Fan target speed
:param tolerance: Threshold between Fan speed and target speed
:return: True if status changed else False
"""
if not self._check_speed_value_available(speed, target_speed, tolerance, self.under_speed):
old_status = self.under_speed
self.under_speed = False
return old_status != self.under_speed
status = speed < target_speed * (1 - float(tolerance) / 100)
if status == self.under_speed:
return False
self.under_speed = status
return True
def set_over_speed(self, speed, target_speed, tolerance):
"""
Set and cache Fan over speed status
:param speed: Fan speed
:param target_speed: Fan target speed
:param tolerance: Threshold between Fan speed and target speed
:return: True if status changed else False
"""
if not self._check_speed_value_available(speed, target_speed, tolerance, self.over_speed):
old_status = self.over_speed
self.over_speed = False
return old_status != self.over_speed
status = speed > target_speed * (1 + float(tolerance) / 100)
if status == self.over_speed:
return False
self.over_speed = status
return True
def is_ok(self):
"""
Indicate the Fan works as expect
:return: True if Fan works normal else False
"""
return (self.presence and
self.status and
not self.under_speed and
not self.over_speed and
not self.invalid_direction)
#
# FanUpdater ===================================================================
#
class FanUpdater(logger.Logger):
# Fan information table name in database
FAN_INFO_TABLE_NAME = 'FAN_INFO'
FAN_DRAWER_INFO_TABLE_NAME = 'FAN_DRAWER_INFO'
def __init__(self, chassis, task_stopping_event):
"""
Initializer for FanUpdater
:param chassis: Object representing a platform chassis
"""
super(FanUpdater, self).__init__(SYSLOG_IDENTIFIER)
self.chassis = chassis
self.task_stopping_event = task_stopping_event
self.fan_status_dict = {}
state_db = daemon_base.db_connect("STATE_DB")
self.table = swsscommon.Table(state_db, FanUpdater.FAN_INFO_TABLE_NAME)
self.drawer_table = swsscommon.Table(state_db, FanUpdater.FAN_DRAWER_INFO_TABLE_NAME)
self.phy_entity_table = swsscommon.Table(state_db, PHYSICAL_ENTITY_INFO_TABLE)
def deinit(self):
"""
Deinitializer of FanUpdater
:return:
"""
for name in self.fan_status_dict.keys():
self.table._del(name)
def _log_on_status_changed(self, normal_status, normal_log, abnormal_log):
"""
Log when any status changed
:param normal_status: Expected status.
:param normal_log: Log string for expected status.
:param abnormal_log: Log string for unexpected status
:return:
"""
if normal_status:
self.log_notice(normal_log)
else:
self.log_warning(abnormal_log)
def update(self):
"""
Update all Fan information to database
:return:
"""
self.log_debug("Start fan updating")
old_bad_fan_count = FanStatus.get_bad_fan_count()
FanStatus.reset_fan_counter()
for drawer_index, drawer in enumerate(self.chassis.get_all_fan_drawers()):
if self.task_stopping_event.is_set():
return
self._refresh_fan_drawer_status(drawer, drawer_index)
for fan_index, fan in enumerate(drawer.get_all_fans()):
if self.task_stopping_event.is_set():
return
try:
self._refresh_fan_status(drawer, drawer_index, fan, fan_index)
except Exception as e:
self.log_warning('Failed to update fan status - {}'.format(repr(e)))
for psu_index, psu in enumerate(self.chassis.get_all_psus()):
for fan_index, fan in enumerate(psu.get_all_fans()):
if self.task_stopping_event.is_set():
return
try:
self._refresh_fan_status(psu, psu_index, fan, fan_index, True)
except Exception as e:
self.log_warning('Failed to update PSU fan status - {}'.format(repr(e)))
self._update_led_color()
bad_fan_count = FanStatus.get_bad_fan_count()
if bad_fan_count > 0 and old_bad_fan_count != bad_fan_count:
self.log_warning("Insufficient number of working fans warning: {} fan{} not working".format(
bad_fan_count, " is" if bad_fan_count == 1 else "s are"
))
elif old_bad_fan_count > 0 and bad_fan_count == 0:
self.log_notice("Insufficient number of working fans warning cleared: all fans are back to normal")
self.log_debug("End fan updating")
def _refresh_fan_drawer_status(self, fan_drawer, drawer_index):
drawer_name = try_get(fan_drawer.get_name)
if drawer_name == NOT_AVAILABLE:
return
update_entity_info(self.phy_entity_table, CHASSIS_INFO_KEY, drawer_name, fan_drawer, drawer_index)
fvs = swsscommon.FieldValuePairs(
[('presence', str(try_get(fan_drawer.get_presence, False))),
('model', str(try_get(fan_drawer.get_model))),
('serial', str(try_get(fan_drawer.get_serial))),
('status', str(try_get(fan_drawer.get_status))),
('is_replaceable', str(try_get(fan_drawer.is_replaceable, False))),
])
self.drawer_table.set(drawer_name, fvs)
def _refresh_fan_status(self, parent, parent_index, fan, fan_index, is_psu_fan=False):
"""
Get Fan status by platform API and write to database for a given Fan
:param parent: Parent device of this fan
:param parent_index: Parent device index
:param fan: Object representing a platform Fan
:param fan_index: Index of the Fan object in its parent device
:param name_prefix: name prefix of Fan object if Fan.get_name not presented
:return:
"""
drawer_name = NOT_AVAILABLE if is_psu_fan else str(try_get(parent.get_name))
if is_psu_fan:
parent_name = 'PSU {}'.format(parent_index + 1)
else:
parent_name = drawer_name if drawer_name != NOT_AVAILABLE else CHASSIS_INFO_KEY
fan_name = try_get(fan.get_name, '{} fan {}'.format(parent_name, fan_index + 1))
update_entity_info(self.phy_entity_table, parent_name, fan_name, fan, fan_index + 1)
if fan_name not in self.fan_status_dict:
self.fan_status_dict[fan_name] = FanStatus(fan, is_psu_fan)
fan_status = self.fan_status_dict[fan_name]
speed = NOT_AVAILABLE
speed_tolerance = NOT_AVAILABLE
speed_target = NOT_AVAILABLE
fan_fault_status = NOT_AVAILABLE
fan_direction = NOT_AVAILABLE
is_replaceable = try_get(fan.is_replaceable, False)
presence = try_get(fan.get_presence, False)
if presence:
speed = try_get(fan.get_speed)
speed_tolerance = try_get(fan.get_speed_tolerance)
speed_target = try_get(fan.get_target_speed)
fan_fault_status = try_get(fan.get_status, False)
fan_direction = try_get(fan.get_direction)
set_led = not fan_status.led_initialized
if fan_status.set_presence(presence):
set_led = True
self._log_on_status_changed(fan_status.presence,
'Fan removed warning cleared: {} was inserted'.format(fan_name),
'Fan removed warning: {} was removed from '
'the system, potential overheat hazard'.format(fan_name)
)
if presence and fan_status.set_fault_status(fan_fault_status):
set_led = True
self._log_on_status_changed(fan_status.status,
'Fan fault warning cleared: {} is back to normal'.format(fan_name),
'Fan fault warning: {} is broken'.format(fan_name)
)
if presence and fan_status.set_under_speed(speed, speed_target, speed_tolerance):
set_led = True
self._log_on_status_changed(not fan_status.under_speed,
'Fan low speed warning cleared: {} speed is back to normal'.format(fan_name),
'Fan low speed warning: {} current speed={}, target speed={}, tolerance={}'.
format(fan_name, speed, speed_target, speed_tolerance)
)
if presence and fan_status.set_over_speed(speed, speed_target, speed_tolerance):
set_led = True
self._log_on_status_changed(not fan_status.over_speed,
'Fan high speed warning cleared: {} speed is back to normal'.format(fan_name),
'Fan high speed warning: {} target speed={}, current speed={}, tolerance={}'.
format(fan_name, speed_target, speed, speed_tolerance)
)
# TODO: handle invalid fan direction
# We don't set PSU led here, PSU led will be handled in psud
if set_led:
if not is_psu_fan:
self._set_fan_led(parent, fan, fan_name, fan_status)
if fan_fault_status != NOT_AVAILABLE:
fan_fault_status = fan_status.is_ok()
fvs = swsscommon.FieldValuePairs(
[('presence', str(presence)),
('drawer_name', drawer_name),
('model', str(try_get(fan.get_model))),
('serial', str(try_get(fan.get_serial))),
('status', str(fan_fault_status)),
('direction', str(fan_direction)),
('speed', str(speed)),
('speed_tolerance', str(speed_tolerance)),
('speed_target', str(speed_target)),
('is_replaceable', str(is_replaceable)),
('timestamp', datetime.now().strftime('%Y%m%d %H:%M:%S'))
])
self.table.set(fan_name, fvs)
def _set_fan_led(self, fan_drawer, fan, fan_name, fan_status):
"""
Set fan led according to current status
:param fan_drawer: Object representing a platform Fan drawer or PSU
:param fan: Object representing a platform Fan
:param fan_name: Name of the Fan object in case any vendor not implement Fan.get_name
:param fan_status: Object representing the FanStatus
:return:
"""
try:
led_color = fan.STATUS_LED_COLOR_GREEN if fan_status.is_ok() else fan.STATUS_LED_COLOR_RED
fan.set_status_led(led_color)
fan_drawer.set_status_led(led_color)
except NotImplementedError as e:
self.log_warning('Failed to set status LED for fan {}, set_status_led not implemented'.format(fan_name))
# Set led_initialized to True even if there is NotImplementedError as it is not neccessary to
# print the warning log again and again. But if there is other exception, we could not
# reach this line, and it will retry setting led color in the next run.
fan_status.led_initialized = True
def _update_led_color(self):
for fan_name, fan_status in self.fan_status_dict.items():
if self.task_stopping_event.is_set():
return
try:
fvs = swsscommon.FieldValuePairs([
('led_status', str(try_get(fan_status.fan.get_status_led)))
])
except Exception as e:
self.log_warning('Failed to get status LED state for fan {} - {}'.format(fan_name, e))
fvs = swsscommon.FieldValuePairs([
('led_status', NOT_AVAILABLE)
])
self.table.set(fan_name, fvs)
for drawer in self.chassis.get_all_fan_drawers():
if self.task_stopping_event.is_set():
return
drawer_name = try_get(drawer.get_name)
if drawer_name == NOT_AVAILABLE:
continue
try:
fvs = swsscommon.FieldValuePairs([
('led_status', str(try_get(drawer.get_status_led)))
])
except Exception as e:
self.log_warning('Failed to get status LED state for fan drawer')
fvs = swsscommon.FieldValuePairs([
('led_status', NOT_AVAILABLE)
])
self.drawer_table.set(drawer_name, fvs)
class TemperatureStatus(logger.Logger):
TEMPERATURE_DIFF_THRESHOLD = 10
def __init__(self):
super(TemperatureStatus, self).__init__(SYSLOG_IDENTIFIER)
self.temperature = None
self.over_temperature = False
self.under_temperature = False
def set_temperature(self, name, temperature):
"""
Record temperature changes, if it changed too fast, raise a warning.
:param name: Name of the thermal.
:param temperature: New temperature value.
:return:
"""
if temperature == NOT_AVAILABLE:
if self.temperature is not None:
self.log_warning('Temperature of {} became unavailable'.format(name))
self.temperature = None
return
if self.temperature is None:
self.temperature = temperature
else:
diff = abs(temperature - self.temperature)
if diff > TemperatureStatus.TEMPERATURE_DIFF_THRESHOLD:
self.log_warning(
'Temperature of {} changed too fast, from {} to {}, please check your hardware'.format(
name, self.temperature, temperature))
self.temperature = temperature
def _check_temperature_value_available(self, temperature, threshold, current_status):
if temperature == NOT_AVAILABLE or threshold == NOT_AVAILABLE:
if current_status is True:
self.log_warning('Thermal temperature or threshold became unavailable, '
'temperature={}, threshold={}'.format(temperature, threshold))
return False
return True
def set_over_temperature(self, temperature, threshold):
"""
Set over temperature status
:param temperature: Temperature
:param threshold: High threshold
:return: True if over temperature status changed else False
"""
if not self._check_temperature_value_available(temperature, threshold, self.over_temperature):
old_status = self.over_temperature
self.over_temperature = False
return old_status != self.over_temperature
status = temperature > threshold
if status == self.over_temperature:
return False
self.over_temperature = status
return True
def set_under_temperature(self, temperature, threshold):
"""
Set over temperature status
:param temperature: Temperature
:param threshold: Low threshold
:return: True if under temperature status changed else False
"""
if not self._check_temperature_value_available(temperature, threshold, self.under_temperature):
old_status = self.under_temperature
self.under_temperature = False
return old_status != self.under_temperature
status = temperature < threshold
if status == self.under_temperature:
return False
self.under_temperature = status
return True
#
# TemperatureUpdater ======================================================================
#
class TemperatureUpdater(logger.Logger):
# Temperature information table name in database
TEMPER_INFO_TABLE_NAME = 'TEMPERATURE_INFO'
def __init__(self, chassis, task_stopping_event):
"""
Initializer of TemperatureUpdater
:param chassis: Object representing a platform chassis
"""
super(TemperatureUpdater, self).__init__(SYSLOG_IDENTIFIER)
self.chassis = chassis
self.task_stopping_event = task_stopping_event
self.temperature_status_dict = {}
state_db = daemon_base.db_connect("STATE_DB")
self.table = swsscommon.Table(state_db, TemperatureUpdater.TEMPER_INFO_TABLE_NAME)
self.phy_entity_table = swsscommon.Table(state_db, PHYSICAL_ENTITY_INFO_TABLE)
self.chassis_table = None
self.is_chassis_system = chassis.is_modular_chassis()
if self.is_chassis_system:
my_slot = try_get(chassis.get_my_slot, INVALID_SLOT)
if my_slot != INVALID_SLOT:
table_name = TemperatureUpdater.TEMPER_INFO_TABLE_NAME+'_'+str(my_slot)
chassis_state_db = daemon_base.db_connect("CHASSIS_STATE_DB")
self.chassis_table = swsscommon.Table(chassis_state_db, table_name)
def deinit(self):
"""
Deinitializer of TemperatureUpdater
:return:
"""
for name in self.temperature_status_dict.keys():
self.table._del(name)
if self.is_chassis_system and self.chassis_table is not None:
self.chassis_table._del(name)
def _log_on_status_changed(self, normal_status, normal_log, abnormal_log):
"""
Log when any status changed
:param normal_status: Expected status.
:param normal_log: Log string for expected status.
:param abnormal_log: Log string for unexpected status
:return:
"""
if normal_status:
self.log_notice(normal_log)
else:
self.log_warning(abnormal_log)
def update(self):
"""
Update all temperature information to database
:return:
"""
self.log_debug("Start temperature updating")
for index, thermal in enumerate(self.chassis.get_all_thermals()):
if self.task_stopping_event.is_set():
return
try:
self._refresh_temperature_status(CHASSIS_INFO_KEY, thermal, index)
except Exception as e:
self.log_warning('Failed to update thermal status - {}'.format(repr(e)))
for psu_index, psu in enumerate(self.chassis.get_all_psus()):
parent_name = 'PSU {}'.format(psu_index + 1)
for thermal_index, thermal in enumerate(psu.get_all_thermals()):
if self.task_stopping_event.is_set():
return
try:
self._refresh_temperature_status(parent_name, thermal, thermal_index)
except Exception as e:
self.log_warning('Failed to update thermal status - {}'.format(repr(e)))
for sfp_index, sfp in enumerate(self.chassis.get_all_sfps()):
parent_name = 'SFP {}'.format(sfp_index + 1)
for thermal_index, thermal in enumerate(sfp.get_all_thermals()):
if self.task_stopping_event.is_set():
return
try:
self._refresh_temperature_status(parent_name, thermal, thermal_index)
except Exception as e:
self.log_warning('Failed to update thermal status - {}'.format(repr(e)))
self.log_debug("End temperature updating")
def _refresh_temperature_status(self, parent_name, thermal, thermal_index):
"""
Get temperature status by platform API and write to database
:param parent_name: Name of parent device of the thermal object
:param thermal: Object representing a platform thermal zone
:param thermal_index: Index of the thermal object in platform chassis
:return:
"""
name = try_get(thermal.get_name, '{} Thermal {}'.format(parent_name, thermal_index + 1))
# Only save entity info for thermals that belong to chassis and PSU
# for SFP thermal, they don't need save entity info because snmp can deduce the relation from TRANSCEIVER_DOM_SENSOR
# and as we save logical port in TRANSCEIVER_INFO table, for split cable, a SFP thermal might have multiple parent
# logical port
if 'SFP' not in parent_name:
update_entity_info(self.phy_entity_table, parent_name, name, thermal, thermal_index + 1)
if name not in self.temperature_status_dict:
self.temperature_status_dict[name] = TemperatureStatus()
temperature_status = self.temperature_status_dict[name]
high_threshold = NOT_AVAILABLE
low_threshold = NOT_AVAILABLE
high_critical_threshold = NOT_AVAILABLE
low_critical_threshold = NOT_AVAILABLE
maximum_temperature = NOT_AVAILABLE
minimum_temperature = NOT_AVAILABLE
temperature = try_get(thermal.get_temperature)
is_replaceable = try_get(thermal.is_replaceable, False)
if temperature != NOT_AVAILABLE:
temperature_status.set_temperature(name, temperature)
minimum_temperature = try_get(thermal.get_minimum_recorded)
maximum_temperature = try_get(thermal.get_maximum_recorded)
high_threshold = try_get(thermal.get_high_threshold)
low_threshold = try_get(thermal.get_low_threshold)
high_critical_threshold = try_get(thermal.get_high_critical_threshold)
low_critical_threshold = try_get(thermal.get_low_critical_threshold)
warning = False
if temperature != NOT_AVAILABLE and temperature_status.set_over_temperature(temperature, high_threshold):
self._log_on_status_changed(not temperature_status.over_temperature,
'High temperature warning cleared: {} temperature restored to {}C, high threshold {}C'.
format(name, temperature, high_threshold),
'High temperature warning: {} current temperature {}C, high threshold {}C'.
format(name, temperature, high_threshold)
)
warning = warning | temperature_status.over_temperature
if temperature != NOT_AVAILABLE and temperature_status.set_under_temperature(temperature, low_threshold):
self._log_on_status_changed(not temperature_status.under_temperature,
'Low temperature warning cleared: {} temperature restored to {}C, low threshold {}C'.
format(name, temperature, low_threshold),
'Low temperature warning: {} current temperature {}C, low threshold {}C'.
format(name, temperature, low_threshold)
)
warning = warning | temperature_status.under_temperature
fvs = swsscommon.FieldValuePairs(
[('temperature', str(temperature)),
('minimum_temperature', str(minimum_temperature)),
('maximum_temperature', str(maximum_temperature)),
('high_threshold', str(high_threshold)),
('low_threshold', str(low_threshold)),
('warning_status', str(warning)),
('critical_high_threshold', str(high_critical_threshold)),
('critical_low_threshold', str(low_critical_threshold)),
('is_replaceable', str(is_replaceable)),
('timestamp', datetime.now().strftime('%Y%m%d %H:%M:%S'))
])
self.table.set(name, fvs)
if self.is_chassis_system and self.chassis_table is not None:
self.chassis_table.set(name, fvs)
class ThermalMonitor(ProcessTaskBase):
# Initial update interval
INITIAL_INTERVAL = 5
# Update interval value
UPDATE_INTERVAL = 60
# Update elapse threshold. If update used time is larger than the value, generate a warning log.
UPDATE_ELAPSED_THRESHOLD = 30
def __init__(self, chassis):
"""
Initializer for ThermalMonitor
:param chassis: Object representing a platform chassis
"""
super(ThermalMonitor, self).__init__()
self.wait_time = self.INITIAL_INTERVAL
# TODO: Refactor to eliminate the need for this Logger instance
self.logger = logger.Logger(SYSLOG_IDENTIFIER)
# Set minimum logging level to INFO
self.logger.set_min_log_priority_info()
self.fan_updater = FanUpdater(chassis, self.task_stopping_event)
self.temperature_updater = TemperatureUpdater(chassis, self.task_stopping_event)
def main(self):
begin = time.time()
self.fan_updater.update()
self.temperature_updater.update()
elapsed = time.time() - begin
if elapsed < self.UPDATE_INTERVAL:
self.wait_time = self.UPDATE_INTERVAL - elapsed
else:
self.wait_time = self.INITIAL_INTERVAL
if elapsed > self.UPDATE_ELAPSED_THRESHOLD:
self.logger.log_warning('Update fan and temperature status took {} seconds, '
'there might be performance risk'.format(elapsed))
def task_worker(self):
"""
Thread function to handle Fan status update and temperature status update
:return:
"""
self.logger.log_info("Start thermal monitoring loop")
# Start loop to update fan, temperature info in DB periodically
while not self.task_stopping_event.wait(self.wait_time):
self.main()
self.fan_updater.deinit()
self.temperature_updater.deinit()
self.logger.log_info("Stop thermal monitoring loop")
#
# Daemon =======================================================================
#
class ThermalControlDaemon(daemon_base.DaemonBase):
# Interval to run thermal control logic
INTERVAL = 60
RUN_POLICY_WARN_THRESHOLD_SECS = 30
FAST_START_INTERVAL = 15
POLICY_FILE = '/usr/share/sonic/platform/thermal_policy.json'
def __init__(self):
"""
Initializer of ThermalControlDaemon
"""
super(ThermalControlDaemon, self).__init__(SYSLOG_IDENTIFIER)
# Set minimum logging level to INFO
self.set_min_log_priority_info()
self.stop_event = threading.Event()
self.wait_time = self.INTERVAL
self.chassis = sonic_platform.platform.Platform().get_chassis()
self.thermal_monitor = ThermalMonitor(self.chassis)
self.thermal_monitor.task_run()
self.thermal_manager = None
try:
self.thermal_manager = self.chassis.get_thermal_manager()
if self.thermal_manager:
self.thermal_manager.initialize()
self.thermal_manager.load(ThermalControlDaemon.POLICY_FILE)
self.thermal_manager.init_thermal_algorithm(self.chassis)
except NotImplementedError:
self.log_warning('Thermal manager is not supported on this platform')
except Exception as e:
self.log_error('Caught exception while initializing thermal manager - {}'.format(repr(e)))
def deinit(self):
"""
Deinitializer of ThermalControlDaemon
"""
try:
if self.thermal_manager:
self.thermal_manager.deinitialize()
except Exception as e:
self.log_error('Caught exception while destroying thermal manager - {}'.format(repr(e)))
self.thermal_monitor.task_stop()
# Override signal handler from DaemonBase
def signal_handler(self, sig, frame):
"""
Signal handler
:param sig: Signal number
:param frame: not used
:return:
"""
FATAL_SIGNALS = [signal.SIGINT, signal.SIGTERM]
NONFATAL_SIGNALS = [signal.SIGHUP]
global exit_code
if sig in FATAL_SIGNALS:
self.log_info("Caught signal '{}' - exiting...".format(SIGNALS_TO_NAMES_DICT[sig]))
exit_code = 128 + sig # Make sure we exit with a non-zero code so that supervisor will try to restart us
self.thermal_monitor.task_stop()
self.thermal_manager.stop()
self.stop_event.set()
elif sig in NONFATAL_SIGNALS:
self.log_info("Caught signal '{}' - ignoring...".format(SIGNALS_TO_NAMES_DICT[sig]))
else:
self.log_warning("Caught unhandled signal '{}' - ignoring...".format(SIGNALS_TO_NAMES_DICT[sig]))
# Main daemon logic
def run(self):
"""
Run main logical of this daemon
:return:
"""
if self.stop_event.wait(self.wait_time):
# We received a fatal signal
return False
begin = time.time()
try:
if self.thermal_manager:
self.thermal_manager.run_policy(self.chassis)
except Exception as e:
self.log_error('Caught exception while running thermal policy - {}'.format(repr(e)))
elapsed = time.time() - begin
if elapsed < self.INTERVAL:
self.wait_time = self.INTERVAL - elapsed
else:
self.wait_time = self.FAST_START_INTERVAL
if elapsed > self.RUN_POLICY_WARN_THRESHOLD_SECS:
self.log_warning('Thermal policy execution took {} seconds, '
'there might be performance risk'.format(elapsed))
return True
#
# Main =========================================================================
#
def main():
thermal_control = ThermalControlDaemon()
thermal_control.log_info("Starting up...")
while thermal_control.run():
pass
thermal_control.log_info("Shutting down with exit code {}...".format(exit_code))
thermal_control.deinit()
return exit_code
if __name__ == '__main__':
sys.exit(main())