def getSensorTemp()

in platform/broadcom/sonic-platform-modules-juniper/qfx5210/utils/juniper_qfx5210_monitor.py [0:0]


    def getSensorTemp(self):
        sum = 0
        global isPlatformAFI
        global is80PerFlag
        global is60PerFlag
        global isFireThresholdReached
        global FireThresholdSecsRemaining
        global isFireThresholdPrint 
        global PrevASICValue 
        #AFI
        if (isPlatformAFI == True):
            temp_policy = temp_policy_AFI
        else:
        #AFO
            temp_policy = temp_policy_AFO

        """ Dictionary where 
            key = thermal id index starting from 0. 0 is the sensor 1 ...
            value = Different temp ranges """
        SensorFlag = {
            0: [0,0,0,0,0,0,0,0],
            1: [0,0,0,0,0,0,0,0],
            2: [0,0,0,0,0,0,0,0],
            3: [0,0,0,0,0,0,0,0],
            4: [0,0,0,0,0,0,0,0],
            5: [0,0,0,0,0,0,0,0],
            6: [0,0,0,0,0,0,0,0],
            7: [0,0,0,0,0,0,0,0],
        }    
        # if the Firethreshold Flag is set and 120 seconds have elapsed, invoking the "poweroff" to shutdown the box
        if (isFireThresholdReached == True):
            firethr = FireThresholdSecsRemaining - 20
            if firethr == 0:
                logging.critical('CRITICAL: Fire Threshold reached: System is going to shutdown now')
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: Fire Threshold reached: System is going to shutdown now\n')
            else:
                logging.critical('CRITICAL: Fire Threshold reached: System is going to shutdown in %s seconds', firethr)
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: Fire Threshold reached: System is going to shutdown in %s seconds\n' % firethr)

            FireThresholdSecsRemaining = FireThresholdSecsRemaining - 20
            logging.critical('CRITICAL: Value of FireThresholdSecsRemaining %s seconds', FireThresholdSecsRemaining)

            if (FireThresholdSecsRemaining == 0):
                isFireThresholdReached == False
                time.sleep(20)
                cmd = ["poweroff"]
                subprocess.call(cmd)

        for x in range(self.SENSOR_CORETEMP_NUM_ON_MAIN_BOARD):
            if x < self.SENSOR_NUM_ON_MAIN_BOARD:
                value = self._get_sensor_node_val(x+1)
                logging.debug('Sensor value %d : %s', x, value)
            elif x == self.CORETEMP_INDEX_ON_MAIN_BOARD:
                value = self.get_coretempValue()
                logging.debug('Main Board CORE temp: %s', value)
            else:
                logging.debug('Reading ASIC Temp value using bcmcmd')
                proc = subprocess.Popen("bcmcmd \"show temp\" | grep \"maximum peak temperature\" | awk '{ print $5 }' > /var/log/asic_value 2>&1 & ",shell=True)
                time.sleep(2)
                cmd = ["kill", "-9", proc.pid]
                getstatusoutput_noshell(cmd)
                
                if os.stat("/var/log/asic_value").st_size == 0:
                    value = PrevASICValue
                    logging.debug('No ASIC Temp file, Prev ASIC Temp Value: %s', PrevASICValue)
                else:
                    with open('/var/log/asic_value', 'r') as f:
                        value1 = f.readline()
                    value2 = float(value1)
                    value1 = value2 * 1000
                    value = int(value1)
                    PrevASICValue = value
                    logging.debug('Reading from ASIC Temp file: %s', value)
                    logging.debug('Reading from Prev ASIC Temp Value: %s', PrevASICValue)
                
                subprocess.call(['rm', '/var/log/asic_value'])

            # 60% Duty Cycle for AFO and 70% Duty Cycle for AFI
            if value > temp_policy[x][0][1] and value <= temp_policy[x][0][2]:
                SensorFlag[x][0] = True

            # 60% Prev Duty Cycle for AFO and 70% Prev Duty Cycle for AFI
            elif value > temp_policy[x][1][1] and value < temp_policy[x][1][2]:
                SensorFlag[x][1] = True

            # 80% Duty Cycle
            elif value == temp_policy[x][2][1]:
                SensorFlag[x][2] = True

            #80% Prev Duty Cycle
            elif value > temp_policy[x][3][1] and value < temp_policy[x][3][2]:
                SensorFlag[x][3] = True

            #100% Duty Cycle
            elif value >= temp_policy[x][4][1]:
                SensorFlag[x][4] = True
            
            else:
                pass

            # Yellow Alarm
            if value >= temp_policy[x][5][1] and value < temp_policy[x][5][2]:
                SensorFlag[x][5] = True

            # Red Alarm
            elif value >= temp_policy[x][6][1] and value < temp_policy[x][6][2]:
                SensorFlag[x][6] = True

            # Fire Shut down    
            elif value >= temp_policy[x][7][1]:
                SensorFlag[x][7] = True
         
        fan = QFX5210_FanUtil()
        # CHECK IF ANY TEMPERATURE SENSORS HAS SET FIRE SHUTDOWN FLAG
        if SensorFlag[0][7] or SensorFlag[1][7] or SensorFlag[2][7] or SensorFlag[3][7] or SensorFlag[4][7] or SensorFlag[5][7] or SensorFlag[6][7] or SensorFlag[7][7]:
            isFireThresholdReached = True
            if (isFireThresholdPrint == True):
                logging.critical('CRITICAL: Fire Threshold reached: System is going to shutdown in 120 seconds')
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: Fire Threshold reached: System is going to shutdown in 120 seconds')

                isFireThresholdPrint = False

            logging.debug('Temp Sensor is set to FIRE SHUTDOWN Flag')
            fan.set_fan_duty_cycle(100)
            self.set_alarm_led_brightness(2)

        # CHECK IF ANY TEMPERATURE SENSORS HAS SET 'RED' ALARM FLAG, IF YES, SET THE ALARM LED TO 'RED'
        elif SensorFlag[0][6] or SensorFlag[1][6] or SensorFlag[2][6] or SensorFlag[3][6] or SensorFlag[4][6] or SensorFlag[5][6] or SensorFlag[6][6] or SensorFlag[7][6]:
            fan.set_fan_duty_cycle(100)
            self.set_alarm_led_brightness(2)
            logging.debug('Temp Sensor is set to Red Alarm Flag')
            if (isFireThresholdReached == True):
                logging.critical('CRITICAL: System Stabilized, not shutting down')
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: System Stabilized, not shutting down\n')
                FireThresholdSecsRemaining = 120
                isFireThresholdReached = False

        # CHECK IF ANY TEMPERATURE SENSORS HAS SET 'YELLOW' ALARM FLAG, IF YES, SET THE ALARM LED TO 'YELLOW'
        elif SensorFlag[0][5] or SensorFlag[1][5] or SensorFlag[2][5] or SensorFlag[3][5] or SensorFlag[4][5] or SensorFlag[5][5] or SensorFlag[6][5] or SensorFlag[7][5]:
            fan.set_fan_duty_cycle(100)
            self.set_alarm_led_brightness(1)
            logging.debug('Temp Sensor is set to Yellow Alarm Flag')
            if (isFireThresholdReached == True):
                logging.critical('CRITICAL: System Stabilized, not shutting down')
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: System Stabilized, not shutting down\n')
                FireThresholdSecsRemaining = 120
                isFireThresholdReached = False
   
        # CHECK IF ANY TEMPERATURE SENSORS HAS SET 100% DUTY CYCLE FLAG, IF YES, SET THE FAN DUTY CYCLE TO 100%
        elif SensorFlag[0][4] or SensorFlag[1][4] or SensorFlag[2][4] or SensorFlag[3][4] or SensorFlag[4][4] or SensorFlag[5][4] or SensorFlag[6][4] or SensorFlag[7][4]:
            fan.set_fan_duty_cycle(100)
            value = self.get_alarm_led_brightness()
            if ( value > 0):
                self.set_alarm_led_brightness(0)
            is80PerFlag = False
            is60PerFlag = False
            logging.debug('Temp Sensor is set to 100% Duty Cycle Flag')

        # CHECK IF ANY TEMPERATURE SENSORS HAS SET 80% DUTY CYCLE PREV FLAG, IF YES, SET THE FAN DUTY CYCLE TO 80%
        elif SensorFlag[0][3] or SensorFlag[1][3] or SensorFlag[2][3] or SensorFlag[3][3] or SensorFlag[4][3] or SensorFlag[5][3] or SensorFlag[6][3] or SensorFlag[7][3]:
            if (is80PerFlag == True):
                fan.set_fan_duty_cycle(80)
                is80PerFlag = False
            else:
                pass

            value = self.get_alarm_led_brightness()
            if ( value > 0):
                self.set_alarm_led_brightness(0)

            if (isFireThresholdReached == True):
                logging.critical('CRITICAL: System Stabilized, not shutting down')
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: System Stabilized, not shutting down')
                FireThresholdSecsRemaining = 120
                isFireThresholdReached = False
            logging.debug('Temp Sensor is set to 80% Prev Duty Cycle Flag')

        # CHECK IF ANY TEMPERATURE SENSORS HAS SET 80% DUTY CYCLE FLAG, IF YES, SET THE FAN DUTY CYCLE TO 80%
        elif SensorFlag[0][2] or SensorFlag[1][2] or SensorFlag[2][2] or SensorFlag[3][2] or SensorFlag[4][2] or SensorFlag[5][2] or SensorFlag[6][2] or SensorFlag[7][2]:
            fan.set_fan_duty_cycle(80)
            value = self.get_alarm_led_brightness()
            if ( value > 0):
                self.set_alarm_led_brightness(0)
            is80PerFlag = True

            if (isFireThresholdReached == True):
                logging.critical('CRITICAL: System Stabilized, not shutting down')
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: System Stabilized, not shutting down\n')
                FireThresholdSecsRemaining = 120
                isFireThresholdReached = False

            logging.debug('Temp Sensor is set to 80% Duty Cycle Flag')

        # FOR "AFO" Platform CHECK IF ANY TEMPERATURE SENSORS HAS SET 60% DUTY CYCLE PREV FLAG, IF YES, SET THE FAN DUTY CYCLE TO 60%
        # FOR "AFI" Platform CHECK IF ANY TEMPERATURE SENSORS HAS SET 70% DUTY CYCLE PREV FLAG, IF YES, SET THE FAN DUTY CYCLE TO 70%
        elif SensorFlag[0][1] or SensorFlag[1][1] or SensorFlag[2][1] or SensorFlag[3][1] or SensorFlag[4][1] or SensorFlag[5][1] or SensorFlag[6][1] or SensorFlag[7][1]:
            if (is60PerFlag == True):
                if (isPlatformAFI == True):
                    fan.set_fan_duty_cycle(70)
                else:
                    fan.set_fan_duty_cycle(60)

                is60PerFlag = False
                is80PerFlag = True
            else:
                pass
 
            value = self.get_alarm_led_brightness()
            if ( value > 0):
                self.set_alarm_led_brightness(0)

            if (isFireThresholdReached == True):
                logging.critical('CRITICAL: System Stabilized, not shutting down')
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: System Stabilized, not shutting down\n')
                FireThresholdSecsRemaining = 120
                isFireThresholdReached = False

            logging.debug('Temp Sensor is set to 60% Prev Duty Cycle Flag')

        # FOR "AFO" Platform CHECK IF ANY TEMPERATURE SENSORS HAS SET 60% DUTY CYCLE FLAG, IF YES, SET THE FAN DUTY CYCLE TO 60%
        # FOR "AFI" Platform CHECK IF ANY TEMPERATURE SENSORS HAS SET 70% DUTY CYCLE FLAG, IF YES, SET THE FAN DUTY CYCLE TO 70%
        elif SensorFlag[0][0] or SensorFlag[1][0] or SensorFlag[2][0] or SensorFlag[3][0] or SensorFlag[4][0] or SensorFlag[5][0] or SensorFlag[6][0] or SensorFlag[7][0]:
            if (isPlatformAFI == True):
                fan.set_fan_duty_cycle(70)
            else:
                fan.set_fan_duty_cycle(60)
            value = self.get_alarm_led_brightness()
            if ( value > 0):
                self.set_alarm_led_brightness(0)
            is60PerFlag = True
            is80PerFlag = True

            if (isFireThresholdReached == True):
                logging.critical('CRITICAL: System Stabilized, not shutting down')
                with open("/dev/console", 'w') as f:
                    f.write('CRITICAL: System Stabilized, not shutting down\n')
                FireThresholdSecsRemaining = 120
                isFireThresholdReached = False
            logging.debug('Temp Sensor is set to 60% Duty Cycle Flag')

        else:
            pass
            

        # RESET ALL THE SENSOR FLAGS
        for x in range(self.SENSOR_CORETEMP_NUM_ON_MAIN_BOARD):
            for y in range(self.THERMAL_NUM_RANGE):
                SensorFlag[x][y] = 0