1b52664e2SAppaRao Puli /* 2b52664e2SAppaRao Puli // Copyright (c) 2020 Intel Corporation 3b52664e2SAppaRao Puli // 4b52664e2SAppaRao Puli // Licensed under the Apache License, Version 2.0 (the "License"); 5b52664e2SAppaRao Puli // you may not use this file except in compliance with the License. 6b52664e2SAppaRao Puli // You may obtain a copy of the License at 7b52664e2SAppaRao Puli // 8b52664e2SAppaRao Puli // http://www.apache.org/licenses/LICENSE-2.0 9b52664e2SAppaRao Puli // 10b52664e2SAppaRao Puli // Unless required by applicable law or agreed to in writing, software 11b52664e2SAppaRao Puli // distributed under the License is distributed on an "AS IS" BASIS, 12b52664e2SAppaRao Puli // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b52664e2SAppaRao Puli // See the License for the specific language governing permissions and 14b52664e2SAppaRao Puli // limitations under the License. 15b52664e2SAppaRao Puli */ 16b52664e2SAppaRao Puli #pragma once 173ccb3adbSEd Tanous #include "dbus_utility.hpp" 183ccb3adbSEd Tanous #include "error_messages.hpp" 193ccb3adbSEd Tanous #include "event_service_store.hpp" 203ccb3adbSEd Tanous #include "http_client.hpp" 21c0353249SWludzik, Jozef #include "metric_report.hpp" 222c6ffdb0SEd Tanous #include "ossl_random.hpp" 233ccb3adbSEd Tanous #include "persistent_data.hpp" 247f4eb588SAppaRao Puli #include "registries.hpp" 258dab0f58SEd Tanous #include "registries_selector.hpp" 2650ebd4afSEd Tanous #include "str_utility.hpp" 2777665bdaSNan Zhou #include "utility.hpp" 283ccb3adbSEd Tanous #include "utils/json_utils.hpp" 295b90429aSEd Tanous #include "utils/time_utils.hpp" 307f4eb588SAppaRao Puli 317f4eb588SAppaRao Puli #include <sys/inotify.h> 32b52664e2SAppaRao Puli 33fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 34b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 35ef4c65b7SEd Tanous #include <boost/url/format.hpp> 364a7fbefdSEd Tanous #include <boost/url/url_view_base.hpp> 37b5b40605Snitroglycerine #include <sdbusplus/bus/match.hpp> 381214b7e7SGunnar Mills 395e44e3d8SAppaRao Puli #include <algorithm> 40b52664e2SAppaRao Puli #include <cstdlib> 41b52664e2SAppaRao Puli #include <ctime> 421bf712bcSAyushi Smriti #include <fstream> 43b52664e2SAppaRao Puli #include <memory> 443544d2a7SEd Tanous #include <ranges> 4526702d01SEd Tanous #include <span> 46b52664e2SAppaRao Puli 47b52664e2SAppaRao Puli namespace redfish 48b52664e2SAppaRao Puli { 49156d6b00SAppaRao Puli 50156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 51156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 52156d6b00SAppaRao Puli 535e44e3d8SAppaRao Puli static constexpr const char* subscriptionTypeSSE = "SSE"; 541bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 551bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 561bf712bcSAyushi Smriti 575e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSubscriptions = 20; 585e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSSESubscriptions = 10; 595e44e3d8SAppaRao Puli 60cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 614642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 624642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 634642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 644642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 65cf9e417dSEd Tanous 66cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 674642bf8fSGeorge Liu static int inotifyFd = -1; 68cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 694642bf8fSGeorge Liu static int dirWatchDesc = -1; 70cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 714642bf8fSGeorge Liu static int fileWatchDesc = -1; 724642bf8fSGeorge Liu 734642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 744642bf8fSGeorge Liu using EventLogObjectsType = 754642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 764642bf8fSGeorge Liu std::vector<std::string>>; 774642bf8fSGeorge Liu 78fffb8c1fSEd Tanous namespace registries 794642bf8fSGeorge Liu { 807f4eb588SAppaRao Puli static const Message* 817f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 8226702d01SEd Tanous const std::span<const MessageEntry>& registry) 837f4eb588SAppaRao Puli { 843544d2a7SEd Tanous std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if( 853544d2a7SEd Tanous registry, [&messageKey](const MessageEntry& messageEntry) { 8655f79e6fSEd Tanous return messageKey == messageEntry.first; 877f4eb588SAppaRao Puli }); 8826702d01SEd Tanous if (messageIt != registry.end()) 897f4eb588SAppaRao Puli { 907f4eb588SAppaRao Puli return &messageIt->second; 917f4eb588SAppaRao Puli } 927f4eb588SAppaRao Puli 937f4eb588SAppaRao Puli return nullptr; 947f4eb588SAppaRao Puli } 957f4eb588SAppaRao Puli 9626ccae32SEd Tanous static const Message* formatMessage(std::string_view messageID) 977f4eb588SAppaRao Puli { 987f4eb588SAppaRao Puli // Redfish MessageIds are in the form 997f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1007f4eb588SAppaRao Puli // the right Message 1017f4eb588SAppaRao Puli std::vector<std::string> fields; 1027f4eb588SAppaRao Puli fields.reserve(4); 10350ebd4afSEd Tanous 10450ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 1057f4eb588SAppaRao Puli if (fields.size() != 4) 1067f4eb588SAppaRao Puli { 1077f4eb588SAppaRao Puli return nullptr; 1087f4eb588SAppaRao Puli } 10902cad96eSEd Tanous const std::string& registryName = fields[0]; 11002cad96eSEd Tanous const std::string& messageKey = fields[3]; 1117f4eb588SAppaRao Puli 1127f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 113b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1147f4eb588SAppaRao Puli } 115fffb8c1fSEd Tanous } // namespace registries 1167f4eb588SAppaRao Puli 1177f4eb588SAppaRao Puli namespace event_log 1187f4eb588SAppaRao Puli { 1192558979cSP Dheeraj Srujan Kumar inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 1207f4eb588SAppaRao Puli { 1217f4eb588SAppaRao Puli static time_t prevTs = 0; 1227f4eb588SAppaRao Puli static int index = 0; 1237f4eb588SAppaRao Puli 1247f4eb588SAppaRao Puli // Get the entry timestamp 1257f4eb588SAppaRao Puli std::time_t curTs = 0; 1267f4eb588SAppaRao Puli std::tm timeStruct = {}; 1277f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1287f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1297f4eb588SAppaRao Puli { 1307f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1317f4eb588SAppaRao Puli if (curTs == -1) 1327f4eb588SAppaRao Puli { 1337f4eb588SAppaRao Puli return false; 1347f4eb588SAppaRao Puli } 1357f4eb588SAppaRao Puli } 1367f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1377f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1387f4eb588SAppaRao Puli 1397f4eb588SAppaRao Puli // Save the timestamp 1407f4eb588SAppaRao Puli prevTs = curTs; 1417f4eb588SAppaRao Puli 1427f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1437f4eb588SAppaRao Puli if (index > 0) 1447f4eb588SAppaRao Puli { 1457f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1467f4eb588SAppaRao Puli } 1477f4eb588SAppaRao Puli return true; 1487f4eb588SAppaRao Puli } 1497f4eb588SAppaRao Puli 15023a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 15123a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1525e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1537f4eb588SAppaRao Puli { 1547f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1557f4eb588SAppaRao Puli // First get the Timestamp 156f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1577f4eb588SAppaRao Puli if (space == std::string::npos) 1587f4eb588SAppaRao Puli { 1597f4eb588SAppaRao Puli return -EINVAL; 1607f4eb588SAppaRao Puli } 1617f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1627f4eb588SAppaRao Puli // Then get the log contents 163f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1647f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1657f4eb588SAppaRao Puli { 1667f4eb588SAppaRao Puli return -EINVAL; 1677f4eb588SAppaRao Puli } 1687f4eb588SAppaRao Puli std::string_view entry(logEntry); 1697f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1707f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1717f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 17250ebd4afSEd Tanous bmcweb::split(logEntryFields, entry, ','); 1737f4eb588SAppaRao Puli // We need at least a MessageId to be valid 17426f6976fSEd Tanous if (logEntryFields.empty()) 1757f4eb588SAppaRao Puli { 1767f4eb588SAppaRao Puli return -EINVAL; 1777f4eb588SAppaRao Puli } 1787f4eb588SAppaRao Puli messageID = logEntryFields[0]; 1797f4eb588SAppaRao Puli 1807f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 1817f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 1827f4eb588SAppaRao Puli { 18302cad96eSEd Tanous const std::string& messageArgsStart = logEntryFields[1]; 1847f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 1857f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 1867f4eb588SAppaRao Puli { 1875e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 1885e715de6SAppaRao Puli logEntryFields.end()); 1897f4eb588SAppaRao Puli } 1907f4eb588SAppaRao Puli } 1917f4eb588SAppaRao Puli 1927f4eb588SAppaRao Puli return 0; 1937f4eb588SAppaRao Puli } 1947f4eb588SAppaRao Puli 19523a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 1967f4eb588SAppaRao Puli std::string& registryName, 1977f4eb588SAppaRao Puli std::string& messageKey) 1987f4eb588SAppaRao Puli { 1997f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2007f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2017f4eb588SAppaRao Puli // the right Message 2027f4eb588SAppaRao Puli std::vector<std::string> fields; 2037f4eb588SAppaRao Puli fields.reserve(4); 20450ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 2057f4eb588SAppaRao Puli if (fields.size() == 4) 2067f4eb588SAppaRao Puli { 2077f4eb588SAppaRao Puli registryName = fields[0]; 2087f4eb588SAppaRao Puli messageKey = fields[3]; 2097f4eb588SAppaRao Puli } 2107f4eb588SAppaRao Puli } 2117f4eb588SAppaRao Puli 21223a21a1cSEd Tanous inline int formatEventLogEntry(const std::string& logEntryID, 2137f4eb588SAppaRao Puli const std::string& messageID, 214c5ba4c27SEd Tanous const std::span<std::string_view> messageArgs, 21523a21a1cSEd Tanous std::string timestamp, 216b5a76932SEd Tanous const std::string& customText, 2177f4eb588SAppaRao Puli nlohmann::json& logEntryJson) 2187f4eb588SAppaRao Puli { 2197f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 220fffb8c1fSEd Tanous const registries::Message* message = registries::formatMessage(messageID); 2217f4eb588SAppaRao Puli 22280f595e7SEd Tanous if (message == nullptr) 2237f4eb588SAppaRao Puli { 22480f595e7SEd Tanous return -1; 2257f4eb588SAppaRao Puli } 2267f4eb588SAppaRao Puli 22789492a15SPatrick Williams std::string msg = redfish::registries::fillMessageArgs(messageArgs, 22889492a15SPatrick Williams message->message); 22980f595e7SEd Tanous if (msg.empty()) 23080f595e7SEd Tanous { 23180f595e7SEd Tanous return -1; 23280f595e7SEd Tanous } 2337f4eb588SAppaRao Puli 2347f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2357f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2367f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 237f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 238b2f7609bSEd Tanous std::size_t plus = timestamp.find_first_of('+', dot); 2397f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2407f4eb588SAppaRao Puli { 2417f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2427f4eb588SAppaRao Puli } 2437f4eb588SAppaRao Puli 2447f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2451476687dSEd Tanous logEntryJson["EventId"] = logEntryID; 2461476687dSEd Tanous logEntryJson["EventType"] = "Event"; 24780f595e7SEd Tanous logEntryJson["Severity"] = message->messageSeverity; 2481476687dSEd Tanous logEntryJson["Message"] = std::move(msg); 2491476687dSEd Tanous logEntryJson["MessageId"] = messageID; 2501476687dSEd Tanous logEntryJson["MessageArgs"] = messageArgs; 2511476687dSEd Tanous logEntryJson["EventTimestamp"] = std::move(timestamp); 2521476687dSEd Tanous logEntryJson["Context"] = customText; 2537f4eb588SAppaRao Puli return 0; 2547f4eb588SAppaRao Puli } 2557f4eb588SAppaRao Puli 2567f4eb588SAppaRao Puli } // namespace event_log 2577f4eb588SAppaRao Puli 25828afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 259b52664e2SAppaRao Puli { 260b52664e2SAppaRao Puli public: 261b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 262b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 263b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 264b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 265b52664e2SAppaRao Puli 2664a7fbefdSEd Tanous Subscription(const boost::urls::url_view_base& url, 2674a7fbefdSEd Tanous boost::asio::io_context& ioc) : 268a716aa74SEd Tanous policy(std::make_shared<crow::ConnectionPolicy>()) 269b52664e2SAppaRao Puli { 270a716aa74SEd Tanous destinationUrl = url; 2715e44e3d8SAppaRao Puli client.emplace(ioc, policy); 2727adb85acSSunitha Harish // Subscription constructor 273d14a48ffSCarson Labrado policy->invalidResp = retryRespHandler; 274b52664e2SAppaRao Puli } 2754bbf237fSAppaRao Puli 2765e44e3d8SAppaRao Puli explicit Subscription(crow::sse_socket::Connection& connIn) : 2775e44e3d8SAppaRao Puli sseConn(&connIn) 2785e44e3d8SAppaRao Puli {} 2795e44e3d8SAppaRao Puli 2809f616dd1SEd Tanous ~Subscription() = default; 281b52664e2SAppaRao Puli 2825e44e3d8SAppaRao Puli bool sendEvent(std::string&& msg) 283b52664e2SAppaRao Puli { 2846ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 2856ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 2866ba8c82eSsunharis_in .getEventServiceConfig(); 2876ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 2886ba8c82eSsunharis_in { 2896ba8c82eSsunharis_in return false; 2906ba8c82eSsunharis_in } 2916ba8c82eSsunharis_in 292f52c03c1SCarson Labrado // A connection pool will be created if one does not already exist 2935e44e3d8SAppaRao Puli if (client) 2945e44e3d8SAppaRao Puli { 295a716aa74SEd Tanous client->sendData(std::move(msg), destinationUrl, httpHeaders, 296a716aa74SEd Tanous boost::beast::http::verb::post); 2975e44e3d8SAppaRao Puli return true; 2985e44e3d8SAppaRao Puli } 2997adb85acSSunitha Harish 3004bbf237fSAppaRao Puli if (sseConn != nullptr) 3014bbf237fSAppaRao Puli { 3025e44e3d8SAppaRao Puli eventSeqNum++; 3035e44e3d8SAppaRao Puli sseConn->sendEvent(std::to_string(eventSeqNum), msg); 3044bbf237fSAppaRao Puli } 3056ba8c82eSsunharis_in return true; 3064bbf237fSAppaRao Puli } 3074bbf237fSAppaRao Puli 3086ba8c82eSsunharis_in bool sendTestEventLog() 3090b4bdd93SAppaRao Puli { 3100b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 3110b4bdd93SAppaRao Puli logEntryArray.push_back({}); 3120b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 3130b4bdd93SAppaRao Puli 314613dabeaSEd Tanous logEntryJson["EventId"] = "TestID"; 315613dabeaSEd Tanous logEntryJson["EventType"] = "Event"; 316613dabeaSEd Tanous logEntryJson["Severity"] = "OK"; 317613dabeaSEd Tanous logEntryJson["Message"] = "Generated test event"; 318613dabeaSEd Tanous logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog"; 319613dabeaSEd Tanous logEntryJson["MessageArgs"] = nlohmann::json::array(); 320613dabeaSEd Tanous logEntryJson["EventTimestamp"] = 321613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 322613dabeaSEd Tanous logEntryJson["Context"] = customText; 3230b4bdd93SAppaRao Puli 3241476687dSEd Tanous nlohmann::json msg; 3251476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 3261476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 3271476687dSEd Tanous msg["Name"] = "Event Log"; 3281476687dSEd Tanous msg["Events"] = logEntryArray; 3290b4bdd93SAppaRao Puli 33089492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 33189492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 3325e44e3d8SAppaRao Puli return sendEvent(std::move(strMsg)); 3330b4bdd93SAppaRao Puli } 3340b4bdd93SAppaRao Puli 3357f4eb588SAppaRao Puli void filterAndSendEventLogs( 3367f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 3377f4eb588SAppaRao Puli { 3387f4eb588SAppaRao Puli nlohmann::json logEntryArray; 3397f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 3407f4eb588SAppaRao Puli { 3417f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 3427f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 3437f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 3447f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 3457f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 3465e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 3477f4eb588SAppaRao Puli 3487f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 3497f4eb588SAppaRao Puli // send everything. 35026f6976fSEd Tanous if (!registryPrefixes.empty()) 3517f4eb588SAppaRao Puli { 3523544d2a7SEd Tanous auto obj = std::ranges::find(registryPrefixes, registryName); 3537f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 3547f4eb588SAppaRao Puli { 3557f4eb588SAppaRao Puli continue; 3567f4eb588SAppaRao Puli } 3577f4eb588SAppaRao Puli } 3587f4eb588SAppaRao Puli 3597f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 3607f4eb588SAppaRao Puli // send everything. 36126f6976fSEd Tanous if (!registryMsgIds.empty()) 3627f4eb588SAppaRao Puli { 3633544d2a7SEd Tanous auto obj = std::ranges::find(registryMsgIds, messageKey); 3647f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 3657f4eb588SAppaRao Puli { 3667f4eb588SAppaRao Puli continue; 3677f4eb588SAppaRao Puli } 3687f4eb588SAppaRao Puli } 3697f4eb588SAppaRao Puli 370c5ba4c27SEd Tanous std::vector<std::string_view> messageArgsView(messageArgs.begin(), 371c5ba4c27SEd Tanous messageArgs.end()); 372c5ba4c27SEd Tanous 3737f4eb588SAppaRao Puli logEntryArray.push_back({}); 3747f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 375c5ba4c27SEd Tanous if (event_log::formatEventLogEntry(idStr, messageID, 376c5ba4c27SEd Tanous messageArgsView, timestamp, 377c5ba4c27SEd Tanous customText, bmcLogEntry) != 0) 3787f4eb588SAppaRao Puli { 37962598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry failed"); 3807f4eb588SAppaRao Puli continue; 3817f4eb588SAppaRao Puli } 3827f4eb588SAppaRao Puli } 3837f4eb588SAppaRao Puli 38426f6976fSEd Tanous if (logEntryArray.empty()) 3857f4eb588SAppaRao Puli { 38662598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 3877f4eb588SAppaRao Puli return; 3887f4eb588SAppaRao Puli } 3897f4eb588SAppaRao Puli 3901476687dSEd Tanous nlohmann::json msg; 3911476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 3921476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 3931476687dSEd Tanous msg["Name"] = "Event Log"; 3941476687dSEd Tanous msg["Events"] = logEntryArray; 39589492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 39689492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 3975e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 3985e44e3d8SAppaRao Puli eventSeqNum++; 3997f4eb588SAppaRao Puli } 4007f4eb588SAppaRao Puli 401248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 4021e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 403156d6b00SAppaRao Puli { 404ef4c65b7SEd Tanous boost::urls::url mrdUri = boost::urls::format( 405ef4c65b7SEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", 406ef4c65b7SEd Tanous reportId); 407156d6b00SAppaRao Puli 408156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 40926f6976fSEd Tanous if (!metricReportDefinitions.empty()) 410156d6b00SAppaRao Puli { 4113544d2a7SEd Tanous if (std::ranges::find(metricReportDefinitions, mrdUri.buffer()) == 4123544d2a7SEd Tanous metricReportDefinitions.end()) 413156d6b00SAppaRao Puli { 414156d6b00SAppaRao Puli return; 415156d6b00SAppaRao Puli } 416156d6b00SAppaRao Puli } 417156d6b00SAppaRao Puli 418c0353249SWludzik, Jozef nlohmann::json msg; 419248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 420156d6b00SAppaRao Puli { 42162598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to fill the MetricReport for DBus " 42262598e31SEd Tanous "Report with id {}", 42362598e31SEd Tanous reportId); 424c0353249SWludzik, Jozef return; 425156d6b00SAppaRao Puli } 426156d6b00SAppaRao Puli 42722daffd7SAppaRao Puli // Context is set by user during Event subscription and it must be 42822daffd7SAppaRao Puli // set for MetricReport response. 42922daffd7SAppaRao Puli if (!customText.empty()) 43022daffd7SAppaRao Puli { 43122daffd7SAppaRao Puli msg["Context"] = customText; 43222daffd7SAppaRao Puli } 43322daffd7SAppaRao Puli 43489492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 43589492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 4365e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 437156d6b00SAppaRao Puli } 438156d6b00SAppaRao Puli 439d14a48ffSCarson Labrado void updateRetryConfig(uint32_t retryAttempts, 440d14a48ffSCarson Labrado uint32_t retryTimeoutInterval) 441fe44eb0bSAyushi Smriti { 44293cf0ac2SEd Tanous if (policy == nullptr) 44393cf0ac2SEd Tanous { 44493cf0ac2SEd Tanous BMCWEB_LOG_DEBUG("Retry policy was nullptr, ignoring set"); 44593cf0ac2SEd Tanous return; 44693cf0ac2SEd Tanous } 447d14a48ffSCarson Labrado policy->maxRetryAttempts = retryAttempts; 448d14a48ffSCarson Labrado policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval); 44962de0c68SAppaRao Puli } 450fe44eb0bSAyushi Smriti 4519eb808c1SEd Tanous uint64_t getEventSeqNum() const 45296330b99SSunitha Harish { 45396330b99SSunitha Harish return eventSeqNum; 45496330b99SSunitha Harish } 45596330b99SSunitha Harish 4565e44e3d8SAppaRao Puli void setSubscriptionId(const std::string& id2) 4575e44e3d8SAppaRao Puli { 45862598e31SEd Tanous BMCWEB_LOG_DEBUG("Subscription ID: {}", id2); 4595e44e3d8SAppaRao Puli subId = id2; 4605e44e3d8SAppaRao Puli } 4615e44e3d8SAppaRao Puli 4625e44e3d8SAppaRao Puli std::string getSubscriptionId() 4635e44e3d8SAppaRao Puli { 4645e44e3d8SAppaRao Puli return subId; 4655e44e3d8SAppaRao Puli } 4665e44e3d8SAppaRao Puli 4675e44e3d8SAppaRao Puli bool matchSseId(const crow::sse_socket::Connection& thisConn) 4685e44e3d8SAppaRao Puli { 4695e44e3d8SAppaRao Puli return &thisConn == sseConn; 4705e44e3d8SAppaRao Puli } 4715e44e3d8SAppaRao Puli 472b52664e2SAppaRao Puli private: 4735e44e3d8SAppaRao Puli std::string subId; 474d14a48ffSCarson Labrado uint64_t eventSeqNum = 1; 475a716aa74SEd Tanous boost::urls::url host; 476d14a48ffSCarson Labrado std::shared_ptr<crow::ConnectionPolicy> policy; 4775e44e3d8SAppaRao Puli crow::sse_socket::Connection* sseConn = nullptr; 4785e44e3d8SAppaRao Puli std::optional<crow::HttpClient> client; 479b52664e2SAppaRao Puli std::string path; 480b52664e2SAppaRao Puli std::string uriProto; 481a7a80296SCarson Labrado 482a7a80296SCarson Labrado // Check used to indicate what response codes are valid as part of our retry 483a7a80296SCarson Labrado // policy. 2XX is considered acceptable 484a7a80296SCarson Labrado static boost::system::error_code retryRespHandler(unsigned int respCode) 485a7a80296SCarson Labrado { 48662598e31SEd Tanous BMCWEB_LOG_DEBUG( 48762598e31SEd Tanous "Checking response code validity for SubscriptionEvent"); 488a7a80296SCarson Labrado if ((respCode < 200) || (respCode >= 300)) 489a7a80296SCarson Labrado { 490a7a80296SCarson Labrado return boost::system::errc::make_error_code( 491a7a80296SCarson Labrado boost::system::errc::result_out_of_range); 492a7a80296SCarson Labrado } 493a7a80296SCarson Labrado 494a7a80296SCarson Labrado // Return 0 if the response code is valid 495a7a80296SCarson Labrado return boost::system::errc::make_error_code( 496a7a80296SCarson Labrado boost::system::errc::success); 4979fa6d147SNan Zhou } 498b52664e2SAppaRao Puli }; 499b52664e2SAppaRao Puli 500b52664e2SAppaRao Puli class EventServiceManager 501b52664e2SAppaRao Puli { 502b52664e2SAppaRao Puli private: 503d3a9e084SEd Tanous bool serviceEnabled = false; 504d3a9e084SEd Tanous uint32_t retryAttempts = 0; 505d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 5067d1cc387SAppaRao Puli 5072558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 5089f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 5099f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 51059d494eeSPatrick Williams std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor; 511b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 512b52664e2SAppaRao Puli subscriptionsMap; 513b52664e2SAppaRao Puli 5149f616dd1SEd Tanous uint64_t eventId{1}; 51596330b99SSunitha Harish 516f8ca6d79SEd Tanous boost::asio::io_context& ioc; 517f8ca6d79SEd Tanous 518b52664e2SAppaRao Puli public: 5199f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 5209f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 5219f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 5229f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 523ecd6a3a2SEd Tanous ~EventServiceManager() = default; 5249f616dd1SEd Tanous 525f8ca6d79SEd Tanous explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn) 526b52664e2SAppaRao Puli { 527f8ca6d79SEd Tanous // Load config from persist store. 528f8ca6d79SEd Tanous initConfig(); 529f8ca6d79SEd Tanous } 530f8ca6d79SEd Tanous 531f8ca6d79SEd Tanous static EventServiceManager& 532f8ca6d79SEd Tanous getInstance(boost::asio::io_context* ioc = nullptr) 533f8ca6d79SEd Tanous { 534f8ca6d79SEd Tanous static EventServiceManager handler(*ioc); 535b52664e2SAppaRao Puli return handler; 536b52664e2SAppaRao Puli } 537b52664e2SAppaRao Puli 5381bf712bcSAyushi Smriti void initConfig() 5391bf712bcSAyushi Smriti { 54028afb49cSJunLin Chen loadOldBehavior(); 5411bf712bcSAyushi Smriti 54228afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 54328afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 54428afb49cSJunLin Chen .getEventServiceConfig(); 5451bf712bcSAyushi Smriti 54628afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 54728afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 54828afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 5491bf712bcSAyushi Smriti 55028afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 55128afb49cSJunLin Chen .subscriptionsConfigMap) 5521bf712bcSAyushi Smriti { 55328afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 55428afb49cSJunLin Chen it.second; 5554bbf237fSAppaRao Puli 5566fd29553SEd Tanous boost::system::result<boost::urls::url> url = 557a716aa74SEd Tanous boost::urls::parse_absolute_uri(newSub->destinationUrl); 5581bf712bcSAyushi Smriti 559a716aa74SEd Tanous if (!url) 5601bf712bcSAyushi Smriti { 56162598e31SEd Tanous BMCWEB_LOG_ERROR( 56262598e31SEd Tanous "Failed to validate and split destination url"); 5631bf712bcSAyushi Smriti continue; 5641bf712bcSAyushi Smriti } 5651bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 566a716aa74SEd Tanous std::make_shared<Subscription>(*url, ioc); 5671bf712bcSAyushi Smriti 56828afb49cSJunLin Chen subValue->id = newSub->id; 56928afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 57028afb49cSJunLin Chen subValue->protocol = newSub->protocol; 57128afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 57228afb49cSJunLin Chen subValue->customText = newSub->customText; 57328afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 57428afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 57528afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 57628afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 57728afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 57828afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 57928afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 5801bf712bcSAyushi Smriti 58128afb49cSJunLin Chen if (subValue->id.empty()) 5821bf712bcSAyushi Smriti { 58362598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to add subscription"); 5841bf712bcSAyushi Smriti } 58528afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 58628afb49cSJunLin Chen 58728afb49cSJunLin Chen updateNoOfSubscribersCount(); 58828afb49cSJunLin Chen 58983328316SEd Tanous if constexpr (!BMCWEB_REDFISH_DBUS_LOG) 59083328316SEd Tanous { 5912558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 59283328316SEd Tanous } 5932558979cSP Dheeraj Srujan Kumar 59428afb49cSJunLin Chen // Update retry configuration. 59528afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 5961bf712bcSAyushi Smriti } 5971bf712bcSAyushi Smriti } 5981bf712bcSAyushi Smriti 59956d2396dSEd Tanous static void loadOldBehavior() 600b52664e2SAppaRao Puli { 60128afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 60228afb49cSJunLin Chen if (!eventConfigFile.good()) 6031bf712bcSAyushi Smriti { 60462598e31SEd Tanous BMCWEB_LOG_DEBUG("Old eventService config not exist"); 60528afb49cSJunLin Chen return; 60628afb49cSJunLin Chen } 60728afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 60828afb49cSJunLin Chen if (jsonData.is_discarded()) 6094bbf237fSAppaRao Puli { 61062598e31SEd Tanous BMCWEB_LOG_ERROR("Old eventService config parse error."); 61128afb49cSJunLin Chen return; 61228afb49cSJunLin Chen } 61328afb49cSJunLin Chen 6140bdda665SEd Tanous const nlohmann::json::object_t* obj = 6150bdda665SEd Tanous jsonData.get_ptr<const nlohmann::json::object_t*>(); 6160bdda665SEd Tanous for (const auto& item : *obj) 61728afb49cSJunLin Chen { 6180bdda665SEd Tanous if (item.first == "Configuration") 61928afb49cSJunLin Chen { 62028afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 62128afb49cSJunLin Chen .getEventServiceConfig() 6220bdda665SEd Tanous .fromJson(item.second); 62328afb49cSJunLin Chen } 6240bdda665SEd Tanous else if (item.first == "Subscriptions") 62528afb49cSJunLin Chen { 6260bdda665SEd Tanous for (const auto& elem : item.second) 62728afb49cSJunLin Chen { 62828afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 62928afb49cSJunLin Chen newSubscription = 63028afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 63128afb49cSJunLin Chen true); 63228afb49cSJunLin Chen if (newSubscription == nullptr) 63328afb49cSJunLin Chen { 63462598e31SEd Tanous BMCWEB_LOG_ERROR("Problem reading subscription " 63562598e31SEd Tanous "from old persistent store"); 6364bbf237fSAppaRao Puli continue; 6374bbf237fSAppaRao Puli } 6381bf712bcSAyushi Smriti 63928afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 64028afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 6411bf712bcSAyushi Smriti 64228afb49cSJunLin Chen std::string id; 6431bf712bcSAyushi Smriti 64428afb49cSJunLin Chen int retry = 3; 645e662eae8SEd Tanous while (retry != 0) 6461bf712bcSAyushi Smriti { 64728afb49cSJunLin Chen id = std::to_string(dist(gen)); 64828afb49cSJunLin Chen if (gen.error()) 6497d1cc387SAppaRao Puli { 65028afb49cSJunLin Chen retry = 0; 65128afb49cSJunLin Chen break; 65228afb49cSJunLin Chen } 65328afb49cSJunLin Chen newSubscription->id = id; 65428afb49cSJunLin Chen auto inserted = 65528afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 65628afb49cSJunLin Chen .subscriptionsConfigMap.insert( 65728afb49cSJunLin Chen std::pair(id, newSubscription)); 65828afb49cSJunLin Chen if (inserted.second) 65928afb49cSJunLin Chen { 66028afb49cSJunLin Chen break; 66128afb49cSJunLin Chen } 66228afb49cSJunLin Chen --retry; 6637d1cc387SAppaRao Puli } 6647d1cc387SAppaRao Puli 66528afb49cSJunLin Chen if (retry <= 0) 66628afb49cSJunLin Chen { 66762598e31SEd Tanous BMCWEB_LOG_ERROR( 66862598e31SEd Tanous "Failed to generate random number from old " 66962598e31SEd Tanous "persistent store"); 67028afb49cSJunLin Chen continue; 67128afb49cSJunLin Chen } 67228afb49cSJunLin Chen } 67328afb49cSJunLin Chen } 67428afb49cSJunLin Chen 67528afb49cSJunLin Chen persistent_data::getConfig().writeData(); 6764c521c3cSEd Tanous std::error_code ec; 6774c521c3cSEd Tanous std::filesystem::remove(eventServiceFile, ec); 6784c521c3cSEd Tanous if (ec) 6794c521c3cSEd Tanous { 6804c521c3cSEd Tanous BMCWEB_LOG_DEBUG( 6814c521c3cSEd Tanous "Failed to remove old event service file. Ignoring"); 6824c521c3cSEd Tanous } 6834c521c3cSEd Tanous else 6844c521c3cSEd Tanous { 68562598e31SEd Tanous BMCWEB_LOG_DEBUG("Remove old eventservice config"); 68628afb49cSJunLin Chen } 68728afb49cSJunLin Chen } 6884c521c3cSEd Tanous } 68928afb49cSJunLin Chen 6909eb808c1SEd Tanous void updateSubscriptionData() const 69128afb49cSJunLin Chen { 69228afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 69328afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 69428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 69528afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 69628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 69728afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 69828afb49cSJunLin Chen 69928afb49cSJunLin Chen persistent_data::getConfig().writeData(); 70028afb49cSJunLin Chen } 70128afb49cSJunLin Chen 70228afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7037d1cc387SAppaRao Puli { 7047d1cc387SAppaRao Puli bool updateConfig = false; 705fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 7067d1cc387SAppaRao Puli 70728afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 7087d1cc387SAppaRao Puli { 70928afb49cSJunLin Chen serviceEnabled = cfg.enabled; 710e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 7117d1cc387SAppaRao Puli { 7127d1cc387SAppaRao Puli registerMetricReportSignal(); 7137d1cc387SAppaRao Puli } 7147d1cc387SAppaRao Puli else 7157d1cc387SAppaRao Puli { 7167d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7177d1cc387SAppaRao Puli } 7187d1cc387SAppaRao Puli updateConfig = true; 7197d1cc387SAppaRao Puli } 7207d1cc387SAppaRao Puli 72128afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 7227d1cc387SAppaRao Puli { 72328afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 7247d1cc387SAppaRao Puli updateConfig = true; 725fe44eb0bSAyushi Smriti updateRetryCfg = true; 7267d1cc387SAppaRao Puli } 7277d1cc387SAppaRao Puli 72828afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 7297d1cc387SAppaRao Puli { 73028afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 7317d1cc387SAppaRao Puli updateConfig = true; 732fe44eb0bSAyushi Smriti updateRetryCfg = true; 7337d1cc387SAppaRao Puli } 7347d1cc387SAppaRao Puli 7357d1cc387SAppaRao Puli if (updateConfig) 7367d1cc387SAppaRao Puli { 7377d1cc387SAppaRao Puli updateSubscriptionData(); 7387d1cc387SAppaRao Puli } 739fe44eb0bSAyushi Smriti 740fe44eb0bSAyushi Smriti if (updateRetryCfg) 741fe44eb0bSAyushi Smriti { 742fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 743fe44eb0bSAyushi Smriti for (const auto& it : 744fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 745fe44eb0bSAyushi Smriti { 7465e44e3d8SAppaRao Puli Subscription& entry = *it.second; 7475e44e3d8SAppaRao Puli entry.updateRetryConfig(retryAttempts, retryTimeoutInterval); 748fe44eb0bSAyushi Smriti } 749fe44eb0bSAyushi Smriti } 7507d1cc387SAppaRao Puli } 7517d1cc387SAppaRao Puli 7527d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 7537d1cc387SAppaRao Puli { 7547d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 7557d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 7567d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 7577d1cc387SAppaRao Puli { 7587d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 7597d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 7607d1cc387SAppaRao Puli { 7617d1cc387SAppaRao Puli eventLogSubCount++; 7627d1cc387SAppaRao Puli } 7637d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 7647d1cc387SAppaRao Puli { 7657d1cc387SAppaRao Puli metricReportSubCount++; 7667d1cc387SAppaRao Puli } 7677d1cc387SAppaRao Puli } 7687d1cc387SAppaRao Puli 7697d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 7707d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 7717d1cc387SAppaRao Puli { 7727d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 773e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 7747d1cc387SAppaRao Puli { 7757d1cc387SAppaRao Puli registerMetricReportSignal(); 7767d1cc387SAppaRao Puli } 7777d1cc387SAppaRao Puli else 7787d1cc387SAppaRao Puli { 7797d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7807d1cc387SAppaRao Puli } 7817d1cc387SAppaRao Puli } 7827d1cc387SAppaRao Puli } 7837d1cc387SAppaRao Puli 784b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 785b52664e2SAppaRao Puli { 786b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 787b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 788b52664e2SAppaRao Puli { 78962598e31SEd Tanous BMCWEB_LOG_ERROR("No subscription exist with ID:{}", id); 790b52664e2SAppaRao Puli return nullptr; 791b52664e2SAppaRao Puli } 792b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 793b52664e2SAppaRao Puli return subValue; 794b52664e2SAppaRao Puli } 795b52664e2SAppaRao Puli 796b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 7971bf712bcSAyushi Smriti const bool updateFile = true) 798b52664e2SAppaRao Puli { 799fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 800fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 801fc76b8acSEd Tanous 802b52664e2SAppaRao Puli std::string id; 803b52664e2SAppaRao Puli 804b52664e2SAppaRao Puli int retry = 3; 805e662eae8SEd Tanous while (retry != 0) 806b52664e2SAppaRao Puli { 807fc76b8acSEd Tanous id = std::to_string(dist(gen)); 808fc76b8acSEd Tanous if (gen.error()) 809fc76b8acSEd Tanous { 810fc76b8acSEd Tanous retry = 0; 811fc76b8acSEd Tanous break; 812fc76b8acSEd Tanous } 813b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 814b52664e2SAppaRao Puli if (inserted.second) 815b52664e2SAppaRao Puli { 816b52664e2SAppaRao Puli break; 817b52664e2SAppaRao Puli } 818b52664e2SAppaRao Puli --retry; 81923a21a1cSEd Tanous } 820b52664e2SAppaRao Puli 821b52664e2SAppaRao Puli if (retry <= 0) 822b52664e2SAppaRao Puli { 82362598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to generate random number"); 824abb93cddSEd Tanous return ""; 825b52664e2SAppaRao Puli } 826b52664e2SAppaRao Puli 82728afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 82828afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 82928afb49cSJunLin Chen newSub->id = id; 83028afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 83128afb49cSJunLin Chen newSub->protocol = subValue->protocol; 83228afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 83328afb49cSJunLin Chen newSub->customText = subValue->customText; 83428afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 83528afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 83628afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 83728afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 83828afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 83928afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 84028afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 84128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 84228afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 84328afb49cSJunLin Chen 8447d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 8451bf712bcSAyushi Smriti 8461bf712bcSAyushi Smriti if (updateFile) 8471bf712bcSAyushi Smriti { 848b52664e2SAppaRao Puli updateSubscriptionData(); 8491bf712bcSAyushi Smriti } 8507f4eb588SAppaRao Puli 85183328316SEd Tanous if constexpr (!BMCWEB_REDFISH_DBUS_LOG) 85283328316SEd Tanous { 8532558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 8547f4eb588SAppaRao Puli { 8552558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 8567f4eb588SAppaRao Puli } 85783328316SEd Tanous } 858fe44eb0bSAyushi Smriti // Update retry configuration. 859fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 860fe44eb0bSAyushi Smriti 8615e44e3d8SAppaRao Puli // Set Subscription ID for back trace 8625e44e3d8SAppaRao Puli subValue->setSubscriptionId(id); 863b52664e2SAppaRao Puli return id; 864b52664e2SAppaRao Puli } 865b52664e2SAppaRao Puli 866b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 867b52664e2SAppaRao Puli { 868b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 86955f79e6fSEd Tanous return obj != subscriptionsMap.end(); 870b52664e2SAppaRao Puli } 871b52664e2SAppaRao Puli 872b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 873b52664e2SAppaRao Puli { 874b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 875b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 876b52664e2SAppaRao Puli { 877b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 87828afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 87928afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 88028afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 88128afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 8827d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 883b52664e2SAppaRao Puli updateSubscriptionData(); 884b52664e2SAppaRao Puli } 885b52664e2SAppaRao Puli } 886b52664e2SAppaRao Puli 8875e44e3d8SAppaRao Puli void deleteSseSubscription(const crow::sse_socket::Connection& thisConn) 8885e44e3d8SAppaRao Puli { 889bdbfae2aSEd Tanous for (auto it = subscriptionsMap.begin(); it != subscriptionsMap.end();) 8905e44e3d8SAppaRao Puli { 891bdbfae2aSEd Tanous std::shared_ptr<Subscription> entry = it->second; 8925e44e3d8SAppaRao Puli bool entryIsThisConn = entry->matchSseId(thisConn); 8935e44e3d8SAppaRao Puli if (entryIsThisConn) 8945e44e3d8SAppaRao Puli { 8955e44e3d8SAppaRao Puli persistent_data::EventServiceStore::getInstance() 8965e44e3d8SAppaRao Puli .subscriptionsConfigMap.erase( 897bdbfae2aSEd Tanous it->second->getSubscriptionId()); 898bdbfae2aSEd Tanous it = subscriptionsMap.erase(it); 8995e44e3d8SAppaRao Puli return; 9005e44e3d8SAppaRao Puli } 901bdbfae2aSEd Tanous it++; 9025e44e3d8SAppaRao Puli } 9035e44e3d8SAppaRao Puli } 9045e44e3d8SAppaRao Puli 9055e44e3d8SAppaRao Puli size_t getNumberOfSubscriptions() const 906b52664e2SAppaRao Puli { 907b52664e2SAppaRao Puli return subscriptionsMap.size(); 908b52664e2SAppaRao Puli } 909b52664e2SAppaRao Puli 9105e44e3d8SAppaRao Puli size_t getNumberOfSSESubscriptions() const 9115e44e3d8SAppaRao Puli { 9123544d2a7SEd Tanous auto size = std::ranges::count_if( 9133544d2a7SEd Tanous subscriptionsMap, 9145e44e3d8SAppaRao Puli [](const std::pair<std::string, std::shared_ptr<Subscription>>& 9155e44e3d8SAppaRao Puli entry) { 9165e44e3d8SAppaRao Puli return (entry.second->subscriptionType == subscriptionTypeSSE); 9175e44e3d8SAppaRao Puli }); 9185e44e3d8SAppaRao Puli return static_cast<size_t>(size); 9195e44e3d8SAppaRao Puli } 9205e44e3d8SAppaRao Puli 921b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 922b52664e2SAppaRao Puli { 923b52664e2SAppaRao Puli std::vector<std::string> idList; 924b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 925b52664e2SAppaRao Puli { 926b52664e2SAppaRao Puli idList.emplace_back(it.first); 927b52664e2SAppaRao Puli } 928b52664e2SAppaRao Puli return idList; 929b52664e2SAppaRao Puli } 930b52664e2SAppaRao Puli 9316ba8c82eSsunharis_in bool sendTestEventLog() 9320b4bdd93SAppaRao Puli { 9335e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 9340b4bdd93SAppaRao Puli { 9350b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 9366ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 9376ba8c82eSsunharis_in { 9386ba8c82eSsunharis_in return false; 9390b4bdd93SAppaRao Puli } 9400b4bdd93SAppaRao Puli } 9416ba8c82eSsunharis_in return true; 9426ba8c82eSsunharis_in } 943e9a14131SAppaRao Puli 944613dabeaSEd Tanous void sendEvent(nlohmann::json eventMessage, const std::string& origin, 945613dabeaSEd Tanous const std::string& resType) 94696330b99SSunitha Harish { 947dbb59d4dSEd Tanous if (!serviceEnabled || (noOfEventLogSubscribers == 0U)) 9486ba8c82eSsunharis_in { 94962598e31SEd Tanous BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions."); 9506ba8c82eSsunharis_in return; 9516ba8c82eSsunharis_in } 95296330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 95396330b99SSunitha Harish 954613dabeaSEd Tanous eventMessage["EventId"] = eventId; 955613dabeaSEd Tanous // MemberId is 0 : since we are sending one event record. 956613dabeaSEd Tanous eventMessage["MemberId"] = 0; 957613dabeaSEd Tanous eventMessage["EventTimestamp"] = 958613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 959613dabeaSEd Tanous eventMessage["OriginOfCondition"] = origin; 960613dabeaSEd Tanous 961613dabeaSEd Tanous eventRecord.emplace_back(std::move(eventMessage)); 96296330b99SSunitha Harish 9635e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 96496330b99SSunitha Harish { 96596330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 96696330b99SSunitha Harish bool isSubscribed = false; 96796330b99SSunitha Harish // Search the resourceTypes list for the subscription. 96896330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 96996330b99SSunitha Harish // send everything. 97026f6976fSEd Tanous if (!entry->resourceTypes.empty()) 97196330b99SSunitha Harish { 9723174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 97396330b99SSunitha Harish { 97496330b99SSunitha Harish if (resType == resource) 97596330b99SSunitha Harish { 97662598e31SEd Tanous BMCWEB_LOG_INFO( 97762598e31SEd Tanous "ResourceType {} found in the subscribed list", 97862598e31SEd Tanous resource); 97996330b99SSunitha Harish isSubscribed = true; 98096330b99SSunitha Harish break; 98196330b99SSunitha Harish } 98296330b99SSunitha Harish } 98396330b99SSunitha Harish } 98496330b99SSunitha Harish else // resourceTypes list is empty. 98596330b99SSunitha Harish { 98696330b99SSunitha Harish isSubscribed = true; 98796330b99SSunitha Harish } 98896330b99SSunitha Harish if (isSubscribed) 98996330b99SSunitha Harish { 990613dabeaSEd Tanous nlohmann::json msgJson; 991613dabeaSEd Tanous 992613dabeaSEd Tanous msgJson["@odata.type"] = "#Event.v1_4_0.Event"; 993613dabeaSEd Tanous msgJson["Name"] = "Event Log"; 994613dabeaSEd Tanous msgJson["Id"] = eventId; 995613dabeaSEd Tanous msgJson["Events"] = eventRecord; 996f52c03c1SCarson Labrado 997f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 998f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 9995e44e3d8SAppaRao Puli entry->sendEvent(std::move(strMsg)); 10008ece0e45SEd Tanous eventId++; // increment the eventId 100196330b99SSunitha Harish } 100296330b99SSunitha Harish else 100396330b99SSunitha Harish { 100462598e31SEd Tanous BMCWEB_LOG_INFO("Not subscribed to this resource"); 100596330b99SSunitha Harish } 100696330b99SSunitha Harish } 100796330b99SSunitha Harish } 100896330b99SSunitha Harish 10092558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 10107f4eb588SAppaRao Puli { 10112558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 10122558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 10132558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 10142558979cSP Dheeraj Srujan Kumar } 10152558979cSP Dheeraj Srujan Kumar 10162558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 10172558979cSP Dheeraj Srujan Kumar { 10182558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 10192558979cSP Dheeraj Srujan Kumar 10207f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 10217f4eb588SAppaRao Puli if (!logStream.good()) 10227f4eb588SAppaRao Puli { 102362598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed "); 10247f4eb588SAppaRao Puli return; 10257f4eb588SAppaRao Puli } 10267f4eb588SAppaRao Puli std::string logEntry; 10277f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 10287f4eb588SAppaRao Puli { 10292558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 10307f4eb588SAppaRao Puli } 10317f4eb588SAppaRao Puli } 10327f4eb588SAppaRao Puli 10337f4eb588SAppaRao Puli void readEventLogsFromFile() 10347f4eb588SAppaRao Puli { 10357f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 10367f4eb588SAppaRao Puli if (!logStream.good()) 10377f4eb588SAppaRao Puli { 103862598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed"); 10397f4eb588SAppaRao Puli return; 10407f4eb588SAppaRao Puli } 10417f4eb588SAppaRao Puli 10427f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 10437f4eb588SAppaRao Puli 10447f4eb588SAppaRao Puli std::string logEntry; 10452558979cSP Dheeraj Srujan Kumar 10462558979cSP Dheeraj Srujan Kumar // Get the read pointer to the next log to be read. 10472558979cSP Dheeraj Srujan Kumar logStream.seekg(redfishLogFilePosition); 10482558979cSP Dheeraj Srujan Kumar 10497f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 10507f4eb588SAppaRao Puli { 10512558979cSP Dheeraj Srujan Kumar // Update Pointer position 10522558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 10532558979cSP Dheeraj Srujan Kumar 10542558979cSP Dheeraj Srujan Kumar std::string idStr; 10552558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 10567f4eb588SAppaRao Puli { 10577f4eb588SAppaRao Puli continue; 10587f4eb588SAppaRao Puli } 10597f4eb588SAppaRao Puli 1060e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 10617f4eb588SAppaRao Puli { 10622558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 10632558979cSP Dheeraj Srujan Kumar // the remaining items below. 10642558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 10657f4eb588SAppaRao Puli continue; 10667f4eb588SAppaRao Puli } 10677f4eb588SAppaRao Puli 10687f4eb588SAppaRao Puli std::string timestamp; 10697f4eb588SAppaRao Puli std::string messageID; 10705e715de6SAppaRao Puli std::vector<std::string> messageArgs; 10717f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 10727f4eb588SAppaRao Puli messageArgs) != 0) 10737f4eb588SAppaRao Puli { 107462598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry params failed"); 10757f4eb588SAppaRao Puli continue; 10767f4eb588SAppaRao Puli } 10777f4eb588SAppaRao Puli 10787f4eb588SAppaRao Puli std::string registryName; 10797f4eb588SAppaRao Puli std::string messageKey; 10807f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 10817f4eb588SAppaRao Puli messageKey); 10827f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 10837f4eb588SAppaRao Puli { 10847f4eb588SAppaRao Puli continue; 10857f4eb588SAppaRao Puli } 10867f4eb588SAppaRao Puli 10877f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 10887f4eb588SAppaRao Puli messageKey, messageArgs); 10897f4eb588SAppaRao Puli } 10907f4eb588SAppaRao Puli 1091e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 10922558979cSP Dheeraj Srujan Kumar { 109362598e31SEd Tanous BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions."); 10942558979cSP Dheeraj Srujan Kumar return; 10952558979cSP Dheeraj Srujan Kumar } 10962558979cSP Dheeraj Srujan Kumar 10972558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 10982558979cSP Dheeraj Srujan Kumar { 10992558979cSP Dheeraj Srujan Kumar // No Records to send 110062598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 11012558979cSP Dheeraj Srujan Kumar return; 11022558979cSP Dheeraj Srujan Kumar } 11032558979cSP Dheeraj Srujan Kumar 11045e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 11057f4eb588SAppaRao Puli { 11067f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 11077f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 11087f4eb588SAppaRao Puli { 11097f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 11107f4eb588SAppaRao Puli } 11117f4eb588SAppaRao Puli } 11127f4eb588SAppaRao Puli } 11137f4eb588SAppaRao Puli 11147f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 11157f4eb588SAppaRao Puli { 11166a9f85f9SAppaRao Puli if (!inotifyConn) 11177f4eb588SAppaRao Puli { 11187f4eb588SAppaRao Puli return; 11197f4eb588SAppaRao Puli } 11207f4eb588SAppaRao Puli 11217f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 11227f4eb588SAppaRao Puli 1123002d39b4SEd Tanous inotifyConn->async_read_some(boost::asio::buffer(readBuffer), 11247f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 11257f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 1126*9ed3f90aSEd Tanous if (ec == boost::asio::error::operation_aborted) 1127*9ed3f90aSEd Tanous { 1128*9ed3f90aSEd Tanous BMCWEB_LOG_DEBUG("Inotify was canceled (shutdown?)"); 1129*9ed3f90aSEd Tanous return; 1130*9ed3f90aSEd Tanous } 11317f4eb588SAppaRao Puli if (ec) 11327f4eb588SAppaRao Puli { 113362598e31SEd Tanous BMCWEB_LOG_ERROR("Callback Error: {}", ec.message()); 11347f4eb588SAppaRao Puli return; 11357f4eb588SAppaRao Puli } 11367f4eb588SAppaRao Puli std::size_t index = 0; 1137b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 11387f4eb588SAppaRao Puli { 1139d3a9e084SEd Tanous struct inotify_event event 1140d3a9e084SEd Tanous {}; 1141b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1142b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1143b792cc56SAppaRao Puli { 1144b792cc56SAppaRao Puli if ((event.len == 0) || 1145b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1146b792cc56SAppaRao Puli { 1147b792cc56SAppaRao Puli index += (iEventSize + event.len); 1148b792cc56SAppaRao Puli continue; 1149b792cc56SAppaRao Puli } 1150b792cc56SAppaRao Puli 11514f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 11524f568f74SJiaqing Zhao if (fileName != "redfish") 1153b792cc56SAppaRao Puli { 1154b792cc56SAppaRao Puli index += (iEventSize + event.len); 1155b792cc56SAppaRao Puli continue; 1156b792cc56SAppaRao Puli } 1157b792cc56SAppaRao Puli 115862598e31SEd Tanous BMCWEB_LOG_DEBUG( 115962598e31SEd Tanous "Redfish log file created/deleted. event.name: {}", 116062598e31SEd Tanous fileName); 1161b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1162b792cc56SAppaRao Puli { 1163b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1164b792cc56SAppaRao Puli { 116562598e31SEd Tanous BMCWEB_LOG_DEBUG( 116662598e31SEd Tanous "Remove and Add inotify watcher on " 116762598e31SEd Tanous "redfish event log file"); 1168016761afSAppaRao Puli // Remove existing inotify watcher and add 1169016761afSAppaRao Puli // with new redfish event log file. 1170016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1171016761afSAppaRao Puli fileWatchDesc = -1; 1172b792cc56SAppaRao Puli } 1173b792cc56SAppaRao Puli 1174b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1175b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1176b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1177b792cc56SAppaRao Puli { 117862598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for " 117962598e31SEd Tanous "redfish log file."); 1180b792cc56SAppaRao Puli return; 1181b792cc56SAppaRao Puli } 1182b792cc56SAppaRao Puli 1183b792cc56SAppaRao Puli EventServiceManager::getInstance() 11842558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1185b792cc56SAppaRao Puli EventServiceManager::getInstance() 1186b792cc56SAppaRao Puli .readEventLogsFromFile(); 1187b792cc56SAppaRao Puli } 1188b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1189b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1190b792cc56SAppaRao Puli { 1191b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1192b792cc56SAppaRao Puli { 1193b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1194b792cc56SAppaRao Puli fileWatchDesc = -1; 1195b792cc56SAppaRao Puli } 1196b792cc56SAppaRao Puli } 1197b792cc56SAppaRao Puli } 1198b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1199b792cc56SAppaRao Puli { 1200b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12017f4eb588SAppaRao Puli { 12027f4eb588SAppaRao Puli EventServiceManager::getInstance() 12037f4eb588SAppaRao Puli .readEventLogsFromFile(); 12047f4eb588SAppaRao Puli } 1205b792cc56SAppaRao Puli } 1206b792cc56SAppaRao Puli index += (iEventSize + event.len); 12077f4eb588SAppaRao Puli } 12087f4eb588SAppaRao Puli 12097f4eb588SAppaRao Puli watchRedfishEventLogFile(); 12107f4eb588SAppaRao Puli }); 12117f4eb588SAppaRao Puli } 12127f4eb588SAppaRao Puli 12137f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 12147f4eb588SAppaRao Puli { 121523a21a1cSEd Tanous inotifyConn.emplace(ioc); 1216b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1217b792cc56SAppaRao Puli if (inotifyFd == -1) 12187f4eb588SAppaRao Puli { 121962598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_init1 failed."); 12207f4eb588SAppaRao Puli return -1; 12217f4eb588SAppaRao Puli } 1222b792cc56SAppaRao Puli 1223b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1224b792cc56SAppaRao Puli // create/delete. 1225b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1226b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1227b792cc56SAppaRao Puli if (dirWatchDesc == -1) 12287f4eb588SAppaRao Puli { 122962598e31SEd Tanous BMCWEB_LOG_ERROR( 123062598e31SEd Tanous "inotify_add_watch failed for event log directory."); 12317f4eb588SAppaRao Puli return -1; 12327f4eb588SAppaRao Puli } 12337f4eb588SAppaRao Puli 1234b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 123589492a15SPatrick Williams fileWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogFile, 123689492a15SPatrick Williams IN_MODIFY); 1237b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1238b792cc56SAppaRao Puli { 123962598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for redfish log file."); 1240b792cc56SAppaRao Puli // Don't return error if file not exist. 1241b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1242b792cc56SAppaRao Puli } 1243b792cc56SAppaRao Puli 12447f4eb588SAppaRao Puli // monitor redfish event log file 1245b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 12467f4eb588SAppaRao Puli watchRedfishEventLogFile(); 12477f4eb588SAppaRao Puli 12487f4eb588SAppaRao Puli return 0; 12497f4eb588SAppaRao Puli } 12507f4eb588SAppaRao Puli 1251*9ed3f90aSEd Tanous static void stopEventLogMonitor() 1252*9ed3f90aSEd Tanous { 1253*9ed3f90aSEd Tanous inotifyConn.reset(); 1254*9ed3f90aSEd Tanous } 1255*9ed3f90aSEd Tanous 125659d494eeSPatrick Williams static void getReadingsForReport(sdbusplus::message_t& msg) 1257156d6b00SAppaRao Puli { 125856d2396dSEd Tanous if (msg.is_method_error()) 125956d2396dSEd Tanous { 126062598e31SEd Tanous BMCWEB_LOG_ERROR("TelemetryMonitor Signal error"); 126156d2396dSEd Tanous return; 126256d2396dSEd Tanous } 126356d2396dSEd Tanous 1264c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1265c0353249SWludzik, Jozef std::string id = path.filename(); 1266c0353249SWludzik, Jozef if (id.empty()) 1267156d6b00SAppaRao Puli { 126862598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get Id from path"); 1269156d6b00SAppaRao Puli return; 1270156d6b00SAppaRao Puli } 1271156d6b00SAppaRao Puli 1272c0353249SWludzik, Jozef std::string interface; 1273b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1274c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1275c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1276c0353249SWludzik, Jozef 12773544d2a7SEd Tanous auto found = std::ranges::find_if( 12783544d2a7SEd Tanous props, [](const auto& x) { return x.first == "Readings"; }); 1279c0353249SWludzik, Jozef if (found == props.end()) 1280156d6b00SAppaRao Puli { 128162598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 1282156d6b00SAppaRao Puli return; 1283156d6b00SAppaRao Puli } 1284156d6b00SAppaRao Puli 12851e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 12861e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1287e662eae8SEd Tanous if (readings == nullptr) 12881e1e598dSJonathan Doman { 128962598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 12901e1e598dSJonathan Doman return; 12911e1e598dSJonathan Doman } 12921e1e598dSJonathan Doman 1293156d6b00SAppaRao Puli for (const auto& it : 1294156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1295156d6b00SAppaRao Puli { 1296e05aec50SEd Tanous Subscription& entry = *it.second; 1297c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1298156d6b00SAppaRao Puli { 12991e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1300156d6b00SAppaRao Puli } 1301156d6b00SAppaRao Puli } 1302156d6b00SAppaRao Puli } 1303156d6b00SAppaRao Puli 1304156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1305156d6b00SAppaRao Puli { 13067d1cc387SAppaRao Puli if (matchTelemetryMonitor) 13077d1cc387SAppaRao Puli { 130862598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Unregister"); 1309156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1310156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1311156d6b00SAppaRao Puli } 13127d1cc387SAppaRao Puli } 1313156d6b00SAppaRao Puli 1314156d6b00SAppaRao Puli void registerMetricReportSignal() 1315156d6b00SAppaRao Puli { 13167d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1317156d6b00SAppaRao Puli { 131862598e31SEd Tanous BMCWEB_LOG_DEBUG("Not registering metric report signal."); 1319156d6b00SAppaRao Puli return; 1320156d6b00SAppaRao Puli } 1321156d6b00SAppaRao Puli 132262598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Register"); 1323c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1324c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1325c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1326156d6b00SAppaRao Puli 132759d494eeSPatrick Williams matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>( 132856d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1329156d6b00SAppaRao Puli } 133023a21a1cSEd Tanous }; 1331b52664e2SAppaRao Puli 1332b52664e2SAppaRao Puli } // namespace redfish 1333