diff --git a/orchagent/flexcounterorch.cpp b/orchagent/flexcounterorch.cpp index 5aaec6b986b..b41d709e871 100644 --- a/orchagent/flexcounterorch.cpp +++ b/orchagent/flexcounterorch.cpp @@ -1,6 +1,7 @@ #include #include "flexcounterorch.h" #include "portsorch.h" +#include "switchorch.h" #include "select.h" #include "notifier.h" #include "redisclient.h" @@ -16,6 +17,7 @@ unordered_map flexCounterGroupMap = {"PORT", PORT_STAT_COUNTER_FLEX_COUNTER_GROUP}, {"QUEUE", QUEUE_STAT_COUNTER_FLEX_COUNTER_GROUP}, {"PFCWD", PFC_WD_FLEX_COUNTER_GROUP}, + {"SENSORS", SWITCH_SENSORS_FLEX_GROUP}, }; diff --git a/orchagent/switchorch.cpp b/orchagent/switchorch.cpp index 7b7d63a3f1b..3282dabc1b5 100644 --- a/orchagent/switchorch.cpp +++ b/orchagent/switchorch.cpp @@ -1,7 +1,10 @@ #include +#include #include "switchorch.h" +#include "schema.h" #include "converter.h" +#include "sai_serialize.h" #include "notifier.h" #include "notificationproducer.h" @@ -28,6 +31,15 @@ const map packet_action_map = {"trap", SAI_PACKET_ACTION_TRAP} }; +#define SWITCH_SENSORS_FLEX_POLL_MSECS "15000" + +static const vector SwitchSensorIds = +{ + SAI_SWITCH_ATTR_MAX_TEMP, + SAI_SWITCH_ATTR_AVERAGE_TEMP, + SAI_SWITCH_ATTR_TEMP_LIST +}; + SwitchOrch::SwitchOrch(DBConnector *db, string tableName) : Orch(db, tableName), m_db(db) @@ -35,12 +47,60 @@ SwitchOrch::SwitchOrch(DBConnector *db, string tableName) : m_restartCheckNotificationConsumer = new NotificationConsumer(db, "RESTARTCHECK"); auto restartCheckNotifier = new Notifier(m_restartCheckNotificationConsumer, this, "RESTARTCHECK"); Orch::addExecutor(restartCheckNotifier); + + m_flex_db = shared_ptr(new DBConnector(FLEX_COUNTER_DB, DBConnector::DEFAULT_UNIXSOCKET, 0)); + m_flexCounterTable = shared_ptr(new ProducerTable(m_flex_db.get(), FLEX_COUNTER_TABLE)); + m_flexCounterGroupTable = shared_ptr(new ProducerTable(m_flex_db.get(), FLEX_COUNTER_GROUP_TABLE)); +} + +string SwitchOrch::getSwitchSensorsFlexCounterTableKey(string key) +{ + return string(SWITCH_SENSORS_FLEX_GROUP) + ":" + key; +} + +void SwitchOrch::addSwitchSensorsToFlexCounters() +{ + SWSS_LOG_ENTER(); + + try + { + vector fieldValues; + fieldValues.emplace_back(POLL_INTERVAL_FIELD, SWITCH_SENSORS_FLEX_POLL_MSECS); + fieldValues.emplace_back(STATS_MODE_FIELD, STATS_MODE_READ); + m_flexCounterGroupTable->set(SWITCH_SENSORS_FLEX_GROUP, fieldValues); + } + + catch (...) + { + SWSS_LOG_WARN("SWITCH Sensors flex counter groups was not set"); + } + + /* Add switch to flex_counter for updating sensors */ + const auto id = sai_serialize_object_id(gSwitchId); + string key = getSwitchSensorsFlexCounterTableKey(id); + std::string delimiter = ""; + std::ostringstream counters_stream; + for (const auto &id: SwitchSensorIds) + { + counters_stream << delimiter << sai_serialize_switch_attr(id); + delimiter = ","; + } + + vector fields; + fields.clear(); + fields.emplace_back(SWITCH_SENSOR_ID_LIST, counters_stream.str()); + + m_flexCounterTable->set(key, fields); + + SWSS_LOG_NOTICE("Added SWITCH Sensors to Flex Counters"); } void SwitchOrch::doTask(Consumer &consumer) { SWSS_LOG_ENTER(); + addSwitchSensorsToFlexCounters(); + auto it = consumer.m_toSync.begin(); while (it != consumer.m_toSync.end()) { diff --git a/orchagent/switchorch.h b/orchagent/switchorch.h index e30def48268..e1cbfa5aa8a 100644 --- a/orchagent/switchorch.h +++ b/orchagent/switchorch.h @@ -1,6 +1,9 @@ #pragma once #include "orch.h" +#include "producertable.h" + +#define SWITCH_SENSORS_FLEX_GROUP "SWITCH_SENSORS" struct WarmRestartCheck { @@ -22,11 +25,17 @@ class SwitchOrch : public Orch bool setAgingFDB(uint32_t sec); private: void doTask(Consumer &consumer); + std::string getSwitchSensorsFlexCounterTableKey(std::string); + void addSwitchSensorsToFlexCounters(); NotificationConsumer* m_restartCheckNotificationConsumer; void doTask(NotificationConsumer& consumer); DBConnector *m_db; + shared_ptr m_flex_db = nullptr; + shared_ptr m_flexCounterTable = nullptr; + shared_ptr m_flexCounterGroupTable = nullptr; + // Information contained in the request from // external program for orchagent pre-shutdown state check WarmRestartCheck m_warmRestartCheck = {false, false, false};