Skip to content

Commit

Permalink
Added support for file system based sensors implementation. (#438)
Browse files Browse the repository at this point in the history
* Added support for file system based sensors implementation.

* Fixed UT

* Fixed UT coverage
  • Loading branch information
bmridul authored Mar 20, 2024
1 parent 04b0f88 commit d513eca
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 deletions.
44 changes: 36 additions & 8 deletions sonic-sensormond/scripts/sensormond
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import signal
import sys
import threading
import time
import yaml

import sonic_platform
from sonic_py_common import daemon_base, logger
from sonic_py_common import daemon_base, logger, device_info
from swsscommon import swsscommon
from sonic_platform_base.sensor_fs import VoltageSensorFs, CurrentSensorFs


SYSLOG_IDENTIFIER = 'sensormond'
Expand Down Expand Up @@ -175,7 +177,7 @@ class VoltageUpdater(SensorUpdater):
# Voltage information table name in database
VOLTAGE_INFO_TABLE_NAME = 'VOLTAGE_INFO'

def __init__(self, chassis):
def __init__(self, chassis, fs_sensors):
'''
Initializer of VoltageUpdater
:param chassis: Object representing a platform chassis
Expand All @@ -187,14 +189,18 @@ class VoltageUpdater(SensorUpdater):
if self.is_chassis_system:
self.module_voltage_sensors = set()

self.fs_sensors = fs_sensors

def update(self):
'''
Update all voltage information to database
:return:
'''
self.log_debug("Start voltage update")
for index, voltage_sensor in enumerate(self.chassis.get_all_voltage_sensors()):

sensor_list = self.fs_sensors + self.chassis.get_all_voltage_sensors()

for index, voltage_sensor in enumerate(sensor_list):
self._refresh_voltage_status(CHASSIS_INFO_KEY, voltage_sensor, index)

if self.is_chassis_system:
Expand Down Expand Up @@ -303,7 +309,7 @@ class CurrentUpdater(SensorUpdater):
# Current information table name in database
CURRENT_INFO_TABLE_NAME = 'CURRENT_INFO'

def __init__(self, chassis):
def __init__(self, chassis, fs_sensors):
'''
Initializer of CurrentUpdater
:param chassis: Object representing a platform chassis
Expand All @@ -314,14 +320,18 @@ class CurrentUpdater(SensorUpdater):
if self.is_chassis_system:
self.module_current_sensors = set()

self.fs_sensors = fs_sensors

def update(self):
'''
Update all current information to database
:return:
'''
self.log_debug("Start current updating")
for index, current_sensor in enumerate(self.chassis.get_all_current_sensors()):

sensor_list = self.fs_sensors + self.chassis.get_all_current_sensors()

for index, current_sensor in enumerate(sensor_list):
self._refresh_current_status(CHASSIS_INFO_KEY, current_sensor, index)

if self.is_chassis_system:
Expand Down Expand Up @@ -450,14 +460,32 @@ class SensorMonitorDaemon(daemon_base.DaemonBase):

self.interval = self.UPDATE_INTERVAL

self._voltage_sensor_fs = []
self._current_sensor_fs = []

try:
self.chassis = sonic_platform.platform.Platform().get_chassis()
except Exception as e:
self.log_error("Failed to get chassis info, err: {}".format(repr(e)))

self.voltage_updater = VoltageUpdater(self.chassis)
# Initialize voltage and current sensors lists from data file if available
try:
(platform_path, _) = device_info.get_paths_to_platform_and_hwsku_dirs()
self.sensors_yaml_file = platform_path + "/sensors.yaml"

with open(self.sensors_yaml_file, 'r') as f:
sensors_data = yaml.safe_load(f)
if 'voltage_sensors' in sensors_data:
self._voltage_sensor_fs = VoltageSensorFs.factory(VoltageSensorFs, sensors_data['voltage_sensors'])
if 'current_sensors' in sensors_data:
self._current_sensor_fs = CurrentSensorFs.factory(CurrentSensorFs, sensors_data['current_sensors'])
except:
# Sensors yaml file is not available
pass

self.voltage_updater = VoltageUpdater(self.chassis, self._voltage_sensor_fs)

self.current_updater = CurrentUpdater(self.chassis)
self.current_updater = CurrentUpdater(self.chassis, self._current_sensor_fs)


# Override signal handler from DaemonBase
Expand Down Expand Up @@ -504,7 +532,7 @@ class SensorMonitorDaemon(daemon_base.DaemonBase):
self.wait_time = self.INITIAL_INTERVAL

if elapsed > self.UPDATE_ELAPSED_THRESHOLD:
self.logger.log_warning('Sensors update took a long time : '
self.log_warning('Sensors update took a long time : '
'{} seconds'.format(elapsed))

return True
Expand Down
18 changes: 18 additions & 0 deletions sonic-sensormond/tests/sensors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
voltage_sensors:
- name : VSENSOR1
sensor: 'sensor_data/VSENSOR1'
high_thresholds: [ 1000, 1050, 1080 ]
low_thresholds: [ 800, 850, 890 ]
- name : VSENSOR2
sensor: 'sensor_data/VSENSOR2'
high_thresholds: [ 800, 850, 870 ]
low_thresholds: [ 600, 620, 750 ]
current_sensors:
- name : CSENSOR1
sensor: 'sensor_data/CSENSOR1'
high_thresholds: [ 1000, 1050, 1080 ]
low_thresholds: [ 800, 850, 890 ]
- name : CSENSOR2
sensor: 'sensor_data/CSENSOR2'
high_thresholds: [ 800, 850, 870 ]
low_thresholds: [ 600, 620, 750 ]
43 changes: 22 additions & 21 deletions sonic-sensormond/tests/test_sensormond.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import yaml
import multiprocessing
from imp import load_source
from unittest import mock
Expand Down Expand Up @@ -106,7 +107,7 @@ class TestVoltageUpdater(object):
"""
def test_deinit(self):
chassis = MockChassis()
voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])
voltage_updater.voltage_status_dict = {'key1': 'value1', 'key2': 'value2'}
voltage_updater.table = Table("STATE_DB", "xtable")
voltage_updater.table._del = mock.MagicMock()
Expand All @@ -127,7 +128,7 @@ def test_deinit(self):
def test_over_voltage(self):
chassis = MockChassis()
chassis.make_over_threshold_voltage_sensor()
voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])
voltage_updater.update()
voltage_sensor_list = chassis.get_all_voltage_sensors()
assert voltage_updater.log_warning.call_count == 1
Expand All @@ -141,7 +142,7 @@ def test_over_voltage(self):
def test_under_voltage(self):
chassis = MockChassis()
chassis.make_under_threshold_voltage_sensor()
voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])
voltage_updater.update()
voltage_sensor_list = chassis.get_all_voltage_sensors()
assert voltage_updater.log_warning.call_count == 1
Expand All @@ -159,7 +160,7 @@ def test_update_voltage_sensor_with_exception(self):
voltage_sensor.make_over_threshold()
chassis.get_all_voltage_sensors().append(voltage_sensor)

voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])
voltage_updater.update()
assert voltage_updater.log_warning.call_count == 2

Expand All @@ -179,7 +180,7 @@ def test_update_module_voltage_sensors(self):
chassis = MockChassis()
chassis.make_module_voltage_sensor()
chassis.set_modular_chassis(True)
voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])
voltage_updater.update()
assert len(voltage_updater.module_voltage_sensors) == 1

Expand All @@ -194,7 +195,7 @@ class TestCurrentUpdater(object):
"""
def test_deinit(self):
chassis = MockChassis()
current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])
current_updater.current_status_dict = {'key1': 'value1', 'key2': 'value2'}
current_updater.table = Table("STATE_DB", "xtable")
current_updater.table._del = mock.MagicMock()
Expand All @@ -215,7 +216,7 @@ def test_deinit(self):
def test_over_current(self):
chassis = MockChassis()
chassis.make_over_threshold_current_sensor()
current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])
current_updater.update()
current_sensor_list = chassis.get_all_current_sensors()
assert current_updater.log_warning.call_count == 1
Expand All @@ -229,7 +230,7 @@ def test_over_current(self):
def test_under_current(self):
chassis = MockChassis()
chassis.make_under_threshold_current_sensor()
current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])
current_updater.update()
current_sensor_list = chassis.get_all_current_sensors()
assert current_updater.log_warning.call_count == 1
Expand All @@ -247,7 +248,7 @@ def test_update_current_sensor_with_exception(self):
current_sensor.make_over_threshold()
chassis.get_all_current_sensors().append(current_sensor)

current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])
current_updater.update()
assert current_updater.log_warning.call_count == 2

Expand All @@ -267,7 +268,7 @@ def test_update_module_current_sensors(self):
chassis = MockChassis()
chassis.make_module_current_sensor()
chassis.set_modular_chassis(True)
current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])
current_updater.update()
assert len(current_updater.module_current_sensors) == 1

Expand All @@ -282,17 +283,17 @@ def test_updater_voltage_sensor_check_modular_chassis():
chassis = MockChassis()
assert chassis.is_modular_chassis() == False

voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])
assert voltage_updater.chassis_table == None

chassis.set_modular_chassis(True)
chassis.set_my_slot(-1)
voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])
assert voltage_updater.chassis_table == None

my_slot = 1
chassis.set_my_slot(my_slot)
voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])
assert voltage_updater.chassis_table != None
assert voltage_updater.chassis_table.table_name == '{}_{}'.format(VOLTAGE_INFO_TABLE_NAME, str(my_slot))

Expand All @@ -305,7 +306,7 @@ def test_updater_voltage_sensor_check_chassis_table():

chassis.set_modular_chassis(True)
chassis.set_my_slot(1)
voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])

voltage_updater.update()
assert voltage_updater.chassis_table.get_size() == chassis.get_num_voltage_sensors()
Expand All @@ -323,7 +324,7 @@ def test_updater_voltage_sensor_check_min_max():

chassis.set_modular_chassis(True)
chassis.set_my_slot(1)
voltage_updater = sensormond.VoltageUpdater(chassis)
voltage_updater = sensormond.VoltageUpdater(chassis, [])

voltage_updater.update()
slot_dict = voltage_updater.chassis_table.get(voltage_sensor.get_name())
Expand All @@ -335,17 +336,17 @@ def test_updater_current_sensor_check_modular_chassis():
chassis = MockChassis()
assert chassis.is_modular_chassis() == False

current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])
assert current_updater.chassis_table == None

chassis.set_modular_chassis(True)
chassis.set_my_slot(-1)
current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])
assert current_updater.chassis_table == None

my_slot = 1
chassis.set_my_slot(my_slot)
current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])
assert current_updater.chassis_table != None
assert current_updater.chassis_table.table_name == '{}_{}'.format(CURRENT_INFO_TABLE_NAME, str(my_slot))

Expand All @@ -358,7 +359,7 @@ def test_updater_current_sensor_check_chassis_table():

chassis.set_modular_chassis(True)
chassis.set_my_slot(1)
current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])

current_updater.update()
assert current_updater.chassis_table.get_size() == chassis.get_num_current_sensors()
Expand All @@ -377,7 +378,7 @@ def test_updater_current_sensor_check_min_max():

chassis.set_modular_chassis(True)
chassis.set_my_slot(1)
current_updater = sensormond.CurrentUpdater(chassis)
current_updater = sensormond.CurrentUpdater(chassis, [])

current_updater.update()
slot_dict = current_updater.chassis_table.get(current_sensor.get_name())
Expand Down Expand Up @@ -437,7 +438,7 @@ def test_signal_handler():
assert daemon_sensormond.stop_event.set.call_count == 0
assert sensormond.exit_code == 1


@mock.patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', mock.MagicMock(return_value=(tests_path, '')))
def test_daemon_run():

import sonic_platform.platform
Expand Down

0 comments on commit d513eca

Please sign in to comment.