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" 20*f80a87f2SEd Tanous #include "filter_expr_executor.hpp" 213ccb3adbSEd Tanous #include "http_client.hpp" 22c0353249SWludzik, Jozef #include "metric_report.hpp" 232c6ffdb0SEd Tanous #include "ossl_random.hpp" 243ccb3adbSEd Tanous #include "persistent_data.hpp" 257f4eb588SAppaRao Puli #include "registries.hpp" 268dab0f58SEd Tanous #include "registries_selector.hpp" 2750ebd4afSEd Tanous #include "str_utility.hpp" 2877665bdaSNan Zhou #include "utility.hpp" 293ccb3adbSEd Tanous #include "utils/json_utils.hpp" 305b90429aSEd Tanous #include "utils/time_utils.hpp" 317f4eb588SAppaRao Puli 327f4eb588SAppaRao Puli #include <sys/inotify.h> 33b52664e2SAppaRao Puli 34fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 35*f80a87f2SEd Tanous #include <boost/circular_buffer.hpp> 36b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 37ef4c65b7SEd Tanous #include <boost/url/format.hpp> 384a7fbefdSEd Tanous #include <boost/url/url_view_base.hpp> 39b5b40605Snitroglycerine #include <sdbusplus/bus/match.hpp> 401214b7e7SGunnar Mills 415e44e3d8SAppaRao Puli #include <algorithm> 42b52664e2SAppaRao Puli #include <cstdlib> 43b52664e2SAppaRao Puli #include <ctime> 441bf712bcSAyushi Smriti #include <fstream> 45b52664e2SAppaRao Puli #include <memory> 463544d2a7SEd Tanous #include <ranges> 4726702d01SEd Tanous #include <span> 48b52664e2SAppaRao Puli 49b52664e2SAppaRao Puli namespace redfish 50b52664e2SAppaRao Puli { 51156d6b00SAppaRao Puli 52156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 53156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 54156d6b00SAppaRao Puli 555e44e3d8SAppaRao Puli static constexpr const char* subscriptionTypeSSE = "SSE"; 561bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 571bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 581bf712bcSAyushi Smriti 595e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSubscriptions = 20; 605e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSSESubscriptions = 10; 615e44e3d8SAppaRao Puli 62cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 634642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 644642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 654642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 664642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 67cf9e417dSEd Tanous 68cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 694642bf8fSGeorge Liu static int inotifyFd = -1; 70cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 714642bf8fSGeorge Liu static int dirWatchDesc = -1; 72cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 734642bf8fSGeorge Liu static int fileWatchDesc = -1; 74*f80a87f2SEd Tanous struct EventLogObjectsType 75*f80a87f2SEd Tanous { 76*f80a87f2SEd Tanous std::string id; 77*f80a87f2SEd Tanous std::string timestamp; 78*f80a87f2SEd Tanous std::string messageId; 79*f80a87f2SEd Tanous std::vector<std::string> messageArgs; 80*f80a87f2SEd Tanous }; 814642bf8fSGeorge Liu 82fffb8c1fSEd Tanous namespace registries 834642bf8fSGeorge Liu { 847f4eb588SAppaRao Puli static const Message* 857f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 8626702d01SEd Tanous const std::span<const MessageEntry>& registry) 877f4eb588SAppaRao Puli { 883544d2a7SEd Tanous std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if( 893544d2a7SEd Tanous registry, [&messageKey](const MessageEntry& messageEntry) { 9055f79e6fSEd Tanous return messageKey == messageEntry.first; 917f4eb588SAppaRao Puli }); 9226702d01SEd Tanous if (messageIt != registry.end()) 937f4eb588SAppaRao Puli { 947f4eb588SAppaRao Puli return &messageIt->second; 957f4eb588SAppaRao Puli } 967f4eb588SAppaRao Puli 977f4eb588SAppaRao Puli return nullptr; 987f4eb588SAppaRao Puli } 997f4eb588SAppaRao Puli 10026ccae32SEd Tanous static const Message* formatMessage(std::string_view messageID) 1017f4eb588SAppaRao Puli { 1027f4eb588SAppaRao Puli // Redfish MessageIds are in the form 1037f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1047f4eb588SAppaRao Puli // the right Message 1057f4eb588SAppaRao Puli std::vector<std::string> fields; 1067f4eb588SAppaRao Puli fields.reserve(4); 10750ebd4afSEd Tanous 10850ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 1097f4eb588SAppaRao Puli if (fields.size() != 4) 1107f4eb588SAppaRao Puli { 1117f4eb588SAppaRao Puli return nullptr; 1127f4eb588SAppaRao Puli } 11302cad96eSEd Tanous const std::string& registryName = fields[0]; 11402cad96eSEd Tanous const std::string& messageKey = fields[3]; 1157f4eb588SAppaRao Puli 1167f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 117b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1187f4eb588SAppaRao Puli } 119fffb8c1fSEd Tanous } // namespace registries 1207f4eb588SAppaRao Puli 1217f4eb588SAppaRao Puli namespace event_log 1227f4eb588SAppaRao Puli { 1232558979cSP Dheeraj Srujan Kumar inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 1247f4eb588SAppaRao Puli { 1257f4eb588SAppaRao Puli static time_t prevTs = 0; 1267f4eb588SAppaRao Puli static int index = 0; 1277f4eb588SAppaRao Puli 1287f4eb588SAppaRao Puli // Get the entry timestamp 1297f4eb588SAppaRao Puli std::time_t curTs = 0; 1307f4eb588SAppaRao Puli std::tm timeStruct = {}; 1317f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1327f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1337f4eb588SAppaRao Puli { 1347f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1357f4eb588SAppaRao Puli if (curTs == -1) 1367f4eb588SAppaRao Puli { 1377f4eb588SAppaRao Puli return false; 1387f4eb588SAppaRao Puli } 1397f4eb588SAppaRao Puli } 1407f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1417f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1427f4eb588SAppaRao Puli 1437f4eb588SAppaRao Puli // Save the timestamp 1447f4eb588SAppaRao Puli prevTs = curTs; 1457f4eb588SAppaRao Puli 1467f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1477f4eb588SAppaRao Puli if (index > 0) 1487f4eb588SAppaRao Puli { 1497f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1507f4eb588SAppaRao Puli } 1517f4eb588SAppaRao Puli return true; 1527f4eb588SAppaRao Puli } 1537f4eb588SAppaRao Puli 15423a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 15523a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1565e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1577f4eb588SAppaRao Puli { 1587f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1597f4eb588SAppaRao Puli // First get the Timestamp 160f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1617f4eb588SAppaRao Puli if (space == std::string::npos) 1627f4eb588SAppaRao Puli { 1637f4eb588SAppaRao Puli return -EINVAL; 1647f4eb588SAppaRao Puli } 1657f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1667f4eb588SAppaRao Puli // Then get the log contents 167f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1687f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1697f4eb588SAppaRao Puli { 1707f4eb588SAppaRao Puli return -EINVAL; 1717f4eb588SAppaRao Puli } 1727f4eb588SAppaRao Puli std::string_view entry(logEntry); 1737f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1747f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1757f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 17650ebd4afSEd Tanous bmcweb::split(logEntryFields, entry, ','); 1777f4eb588SAppaRao Puli // We need at least a MessageId to be valid 17826f6976fSEd Tanous if (logEntryFields.empty()) 1797f4eb588SAppaRao Puli { 1807f4eb588SAppaRao Puli return -EINVAL; 1817f4eb588SAppaRao Puli } 1827f4eb588SAppaRao Puli messageID = logEntryFields[0]; 1837f4eb588SAppaRao Puli 1847f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 1857f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 1867f4eb588SAppaRao Puli { 18702cad96eSEd Tanous const std::string& messageArgsStart = logEntryFields[1]; 1887f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 1897f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 1907f4eb588SAppaRao Puli { 1915e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 1925e715de6SAppaRao Puli logEntryFields.end()); 1937f4eb588SAppaRao Puli } 1947f4eb588SAppaRao Puli } 1957f4eb588SAppaRao Puli 1967f4eb588SAppaRao Puli return 0; 1977f4eb588SAppaRao Puli } 1987f4eb588SAppaRao Puli 19923a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 2007f4eb588SAppaRao Puli std::string& registryName, 2017f4eb588SAppaRao Puli std::string& messageKey) 2027f4eb588SAppaRao Puli { 2037f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2047f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2057f4eb588SAppaRao Puli // the right Message 2067f4eb588SAppaRao Puli std::vector<std::string> fields; 2077f4eb588SAppaRao Puli fields.reserve(4); 20850ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 2097f4eb588SAppaRao Puli if (fields.size() == 4) 2107f4eb588SAppaRao Puli { 2117f4eb588SAppaRao Puli registryName = fields[0]; 2127f4eb588SAppaRao Puli messageKey = fields[3]; 2137f4eb588SAppaRao Puli } 2147f4eb588SAppaRao Puli } 2157f4eb588SAppaRao Puli 21623a21a1cSEd Tanous inline int formatEventLogEntry(const std::string& logEntryID, 2177f4eb588SAppaRao Puli const std::string& messageID, 218c5ba4c27SEd Tanous const std::span<std::string_view> messageArgs, 21923a21a1cSEd Tanous std::string timestamp, 220b5a76932SEd Tanous const std::string& customText, 221*f80a87f2SEd Tanous nlohmann::json::object_t& logEntryJson) 2227f4eb588SAppaRao Puli { 2237f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 224fffb8c1fSEd Tanous const registries::Message* message = registries::formatMessage(messageID); 2257f4eb588SAppaRao Puli 22680f595e7SEd Tanous if (message == nullptr) 2277f4eb588SAppaRao Puli { 22880f595e7SEd Tanous return -1; 2297f4eb588SAppaRao Puli } 2307f4eb588SAppaRao Puli 23189492a15SPatrick Williams std::string msg = redfish::registries::fillMessageArgs(messageArgs, 23289492a15SPatrick Williams message->message); 23380f595e7SEd Tanous if (msg.empty()) 23480f595e7SEd Tanous { 23580f595e7SEd Tanous return -1; 23680f595e7SEd Tanous } 2377f4eb588SAppaRao Puli 2387f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2397f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2407f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 241f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 242b2f7609bSEd Tanous std::size_t plus = timestamp.find_first_of('+', dot); 2437f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2447f4eb588SAppaRao Puli { 2457f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2467f4eb588SAppaRao Puli } 2477f4eb588SAppaRao Puli 2487f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2491476687dSEd Tanous logEntryJson["EventId"] = logEntryID; 2501476687dSEd Tanous logEntryJson["EventType"] = "Event"; 25180f595e7SEd Tanous logEntryJson["Severity"] = message->messageSeverity; 2521476687dSEd Tanous logEntryJson["Message"] = std::move(msg); 2531476687dSEd Tanous logEntryJson["MessageId"] = messageID; 2541476687dSEd Tanous logEntryJson["MessageArgs"] = messageArgs; 2551476687dSEd Tanous logEntryJson["EventTimestamp"] = std::move(timestamp); 2561476687dSEd Tanous logEntryJson["Context"] = customText; 2577f4eb588SAppaRao Puli return 0; 2587f4eb588SAppaRao Puli } 2597f4eb588SAppaRao Puli 2607f4eb588SAppaRao Puli } // namespace event_log 2617f4eb588SAppaRao Puli 26228afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 263b52664e2SAppaRao Puli { 264b52664e2SAppaRao Puli public: 265b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 266b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 267b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 268b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 269b52664e2SAppaRao Puli 2704a7fbefdSEd Tanous Subscription(const boost::urls::url_view_base& url, 2714a7fbefdSEd Tanous boost::asio::io_context& ioc) : 272a716aa74SEd Tanous policy(std::make_shared<crow::ConnectionPolicy>()) 273b52664e2SAppaRao Puli { 274a716aa74SEd Tanous destinationUrl = url; 2755e44e3d8SAppaRao Puli client.emplace(ioc, policy); 2767adb85acSSunitha Harish // Subscription constructor 277d14a48ffSCarson Labrado policy->invalidResp = retryRespHandler; 278b52664e2SAppaRao Puli } 2794bbf237fSAppaRao Puli 2805e44e3d8SAppaRao Puli explicit Subscription(crow::sse_socket::Connection& connIn) : 2815e44e3d8SAppaRao Puli sseConn(&connIn) 2825e44e3d8SAppaRao Puli {} 2835e44e3d8SAppaRao Puli 2849f616dd1SEd Tanous ~Subscription() = default; 285b52664e2SAppaRao Puli 2865e44e3d8SAppaRao Puli bool sendEvent(std::string&& msg) 287b52664e2SAppaRao Puli { 2886ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 2896ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 2906ba8c82eSsunharis_in .getEventServiceConfig(); 2916ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 2926ba8c82eSsunharis_in { 2936ba8c82eSsunharis_in return false; 2946ba8c82eSsunharis_in } 2956ba8c82eSsunharis_in 2965e44e3d8SAppaRao Puli if (client) 2975e44e3d8SAppaRao Puli { 29819bb362bSEd Tanous client->sendData( 29919bb362bSEd Tanous std::move(msg), destinationUrl, 30019bb362bSEd Tanous static_cast<ensuressl::VerifyCertificate>(verifyCertificate), 30119bb362bSEd Tanous httpHeaders, boost::beast::http::verb::post); 3025e44e3d8SAppaRao Puli return true; 3035e44e3d8SAppaRao Puli } 3047adb85acSSunitha Harish 3054bbf237fSAppaRao Puli if (sseConn != nullptr) 3064bbf237fSAppaRao Puli { 3075e44e3d8SAppaRao Puli eventSeqNum++; 3085e44e3d8SAppaRao Puli sseConn->sendEvent(std::to_string(eventSeqNum), msg); 3094bbf237fSAppaRao Puli } 3106ba8c82eSsunharis_in return true; 3114bbf237fSAppaRao Puli } 3124bbf237fSAppaRao Puli 313*f80a87f2SEd Tanous bool eventMatchesFilter(const nlohmann::json::object_t& eventMessage, 314*f80a87f2SEd Tanous std::string_view resType) 315*f80a87f2SEd Tanous { 316*f80a87f2SEd Tanous // If resourceTypes list is empty, assume all 317*f80a87f2SEd Tanous if (!resourceTypes.empty()) 318*f80a87f2SEd Tanous { 319*f80a87f2SEd Tanous // Search the resourceTypes list for the subscription. 320*f80a87f2SEd Tanous auto resourceTypeIndex = std::ranges::find_if( 321*f80a87f2SEd Tanous resourceTypes, [resType](const std::string& rtEntry) { 322*f80a87f2SEd Tanous return rtEntry == resType; 323*f80a87f2SEd Tanous }); 324*f80a87f2SEd Tanous if (resourceTypeIndex == resourceTypes.end()) 325*f80a87f2SEd Tanous { 326*f80a87f2SEd Tanous BMCWEB_LOG_DEBUG("Not subscribed to this resource"); 327*f80a87f2SEd Tanous return false; 328*f80a87f2SEd Tanous } 329*f80a87f2SEd Tanous BMCWEB_LOG_DEBUG("ResourceType {} found in the subscribed list", 330*f80a87f2SEd Tanous resType); 331*f80a87f2SEd Tanous } 332*f80a87f2SEd Tanous 333*f80a87f2SEd Tanous // If registryMsgIds list is empty, assume all 334*f80a87f2SEd Tanous if (!registryMsgIds.empty()) 335*f80a87f2SEd Tanous { 336*f80a87f2SEd Tanous auto eventJson = eventMessage.find("MessageId"); 337*f80a87f2SEd Tanous if (eventJson == eventMessage.end()) 338*f80a87f2SEd Tanous { 339*f80a87f2SEd Tanous return false; 340*f80a87f2SEd Tanous } 341*f80a87f2SEd Tanous 342*f80a87f2SEd Tanous const std::string* messageId = 343*f80a87f2SEd Tanous eventJson->second.get_ptr<const std::string*>(); 344*f80a87f2SEd Tanous if (messageId == nullptr) 345*f80a87f2SEd Tanous { 346*f80a87f2SEd Tanous BMCWEB_LOG_ERROR("EventType wasn't a string???"); 347*f80a87f2SEd Tanous return false; 348*f80a87f2SEd Tanous } 349*f80a87f2SEd Tanous 350*f80a87f2SEd Tanous std::string registry; 351*f80a87f2SEd Tanous std::string messageKey; 352*f80a87f2SEd Tanous event_log::getRegistryAndMessageKey(*messageId, registry, 353*f80a87f2SEd Tanous messageKey); 354*f80a87f2SEd Tanous 355*f80a87f2SEd Tanous auto obj = std::ranges::find(registryMsgIds, registry); 356*f80a87f2SEd Tanous if (obj == registryMsgIds.end()) 357*f80a87f2SEd Tanous { 358*f80a87f2SEd Tanous return false; 359*f80a87f2SEd Tanous } 360*f80a87f2SEd Tanous } 361*f80a87f2SEd Tanous 362*f80a87f2SEd Tanous if (filter) 363*f80a87f2SEd Tanous { 364*f80a87f2SEd Tanous if (!memberMatches(eventMessage, *filter)) 365*f80a87f2SEd Tanous { 366*f80a87f2SEd Tanous BMCWEB_LOG_DEBUG("Filter didn't match"); 367*f80a87f2SEd Tanous return false; 368*f80a87f2SEd Tanous } 369*f80a87f2SEd Tanous } 370*f80a87f2SEd Tanous 371*f80a87f2SEd Tanous return true; 372*f80a87f2SEd Tanous } 373*f80a87f2SEd Tanous 3746ba8c82eSsunharis_in bool sendTestEventLog() 3750b4bdd93SAppaRao Puli { 376*f80a87f2SEd Tanous nlohmann::json::array_t logEntryArray; 377*f80a87f2SEd Tanous nlohmann::json& logEntryJson = logEntryArray.emplace_back(); 3780b4bdd93SAppaRao Puli 379613dabeaSEd Tanous logEntryJson["EventId"] = "TestID"; 380613dabeaSEd Tanous logEntryJson["EventType"] = "Event"; 381613dabeaSEd Tanous logEntryJson["Severity"] = "OK"; 382613dabeaSEd Tanous logEntryJson["Message"] = "Generated test event"; 383613dabeaSEd Tanous logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog"; 384613dabeaSEd Tanous logEntryJson["MessageArgs"] = nlohmann::json::array(); 385613dabeaSEd Tanous logEntryJson["EventTimestamp"] = 386613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 387613dabeaSEd Tanous logEntryJson["Context"] = customText; 3880b4bdd93SAppaRao Puli 3891476687dSEd Tanous nlohmann::json msg; 3901476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 3911476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 3921476687dSEd Tanous msg["Name"] = "Event Log"; 3931476687dSEd Tanous msg["Events"] = logEntryArray; 3940b4bdd93SAppaRao Puli 39589492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 39689492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 3975e44e3d8SAppaRao Puli return sendEvent(std::move(strMsg)); 3980b4bdd93SAppaRao Puli } 3990b4bdd93SAppaRao Puli 4007f4eb588SAppaRao Puli void filterAndSendEventLogs( 4017f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4027f4eb588SAppaRao Puli { 403*f80a87f2SEd Tanous nlohmann::json::array_t logEntryArray; 4047f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4057f4eb588SAppaRao Puli { 406*f80a87f2SEd Tanous std::vector<std::string_view> messageArgsView( 407*f80a87f2SEd Tanous logEntry.messageArgs.begin(), logEntry.messageArgs.end()); 4087f4eb588SAppaRao Puli 409*f80a87f2SEd Tanous nlohmann::json::object_t bmcLogEntry; 410*f80a87f2SEd Tanous if (event_log::formatEventLogEntry( 411*f80a87f2SEd Tanous logEntry.id, logEntry.messageId, messageArgsView, 412*f80a87f2SEd Tanous logEntry.timestamp, customText, bmcLogEntry) != 0) 4137f4eb588SAppaRao Puli { 41462598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry failed"); 4157f4eb588SAppaRao Puli continue; 4167f4eb588SAppaRao Puli } 417*f80a87f2SEd Tanous 418*f80a87f2SEd Tanous if (!eventMatchesFilter(bmcLogEntry, "")) 419*f80a87f2SEd Tanous { 420*f80a87f2SEd Tanous continue; 421*f80a87f2SEd Tanous } 422*f80a87f2SEd Tanous 423*f80a87f2SEd Tanous logEntryArray.emplace_back(std::move(bmcLogEntry)); 4247f4eb588SAppaRao Puli } 4257f4eb588SAppaRao Puli 42626f6976fSEd Tanous if (logEntryArray.empty()) 4277f4eb588SAppaRao Puli { 42862598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 4297f4eb588SAppaRao Puli return; 4307f4eb588SAppaRao Puli } 4317f4eb588SAppaRao Puli 4321476687dSEd Tanous nlohmann::json msg; 4331476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4341476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4351476687dSEd Tanous msg["Name"] = "Event Log"; 436*f80a87f2SEd Tanous msg["Events"] = std::move(logEntryArray); 43789492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 43889492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 4395e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 4405e44e3d8SAppaRao Puli eventSeqNum++; 4417f4eb588SAppaRao Puli } 4427f4eb588SAppaRao Puli 443248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 4441e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 445156d6b00SAppaRao Puli { 446ef4c65b7SEd Tanous boost::urls::url mrdUri = boost::urls::format( 447ef4c65b7SEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", 448ef4c65b7SEd Tanous reportId); 449156d6b00SAppaRao Puli 450156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 45126f6976fSEd Tanous if (!metricReportDefinitions.empty()) 452156d6b00SAppaRao Puli { 4533544d2a7SEd Tanous if (std::ranges::find(metricReportDefinitions, mrdUri.buffer()) == 4543544d2a7SEd Tanous metricReportDefinitions.end()) 455156d6b00SAppaRao Puli { 456156d6b00SAppaRao Puli return; 457156d6b00SAppaRao Puli } 458156d6b00SAppaRao Puli } 459156d6b00SAppaRao Puli 460c0353249SWludzik, Jozef nlohmann::json msg; 461248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 462156d6b00SAppaRao Puli { 46362598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to fill the MetricReport for DBus " 46462598e31SEd Tanous "Report with id {}", 46562598e31SEd Tanous reportId); 466c0353249SWludzik, Jozef return; 467156d6b00SAppaRao Puli } 468156d6b00SAppaRao Puli 46922daffd7SAppaRao Puli // Context is set by user during Event subscription and it must be 47022daffd7SAppaRao Puli // set for MetricReport response. 47122daffd7SAppaRao Puli if (!customText.empty()) 47222daffd7SAppaRao Puli { 47322daffd7SAppaRao Puli msg["Context"] = customText; 47422daffd7SAppaRao Puli } 47522daffd7SAppaRao Puli 47689492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 47789492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 4785e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 479156d6b00SAppaRao Puli } 480156d6b00SAppaRao Puli 481d14a48ffSCarson Labrado void updateRetryConfig(uint32_t retryAttempts, 482d14a48ffSCarson Labrado uint32_t retryTimeoutInterval) 483fe44eb0bSAyushi Smriti { 48493cf0ac2SEd Tanous if (policy == nullptr) 48593cf0ac2SEd Tanous { 48693cf0ac2SEd Tanous BMCWEB_LOG_DEBUG("Retry policy was nullptr, ignoring set"); 48793cf0ac2SEd Tanous return; 48893cf0ac2SEd Tanous } 489d14a48ffSCarson Labrado policy->maxRetryAttempts = retryAttempts; 490d14a48ffSCarson Labrado policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval); 49162de0c68SAppaRao Puli } 492fe44eb0bSAyushi Smriti 4939eb808c1SEd Tanous uint64_t getEventSeqNum() const 49496330b99SSunitha Harish { 49596330b99SSunitha Harish return eventSeqNum; 49696330b99SSunitha Harish } 49796330b99SSunitha Harish 4985e44e3d8SAppaRao Puli void setSubscriptionId(const std::string& id2) 4995e44e3d8SAppaRao Puli { 50062598e31SEd Tanous BMCWEB_LOG_DEBUG("Subscription ID: {}", id2); 5015e44e3d8SAppaRao Puli subId = id2; 5025e44e3d8SAppaRao Puli } 5035e44e3d8SAppaRao Puli 5045e44e3d8SAppaRao Puli std::string getSubscriptionId() 5055e44e3d8SAppaRao Puli { 5065e44e3d8SAppaRao Puli return subId; 5075e44e3d8SAppaRao Puli } 5085e44e3d8SAppaRao Puli 5095e44e3d8SAppaRao Puli bool matchSseId(const crow::sse_socket::Connection& thisConn) 5105e44e3d8SAppaRao Puli { 5115e44e3d8SAppaRao Puli return &thisConn == sseConn; 5125e44e3d8SAppaRao Puli } 5135e44e3d8SAppaRao Puli 514a7a80296SCarson Labrado // Check used to indicate what response codes are valid as part of our retry 515a7a80296SCarson Labrado // policy. 2XX is considered acceptable 516a7a80296SCarson Labrado static boost::system::error_code retryRespHandler(unsigned int respCode) 517a7a80296SCarson Labrado { 51862598e31SEd Tanous BMCWEB_LOG_DEBUG( 51962598e31SEd Tanous "Checking response code validity for SubscriptionEvent"); 520a7a80296SCarson Labrado if ((respCode < 200) || (respCode >= 300)) 521a7a80296SCarson Labrado { 522a7a80296SCarson Labrado return boost::system::errc::make_error_code( 523a7a80296SCarson Labrado boost::system::errc::result_out_of_range); 524a7a80296SCarson Labrado } 525a7a80296SCarson Labrado 526a7a80296SCarson Labrado // Return 0 if the response code is valid 527a7a80296SCarson Labrado return boost::system::errc::make_error_code( 528a7a80296SCarson Labrado boost::system::errc::success); 5299fa6d147SNan Zhou } 530*f80a87f2SEd Tanous 531*f80a87f2SEd Tanous private: 532*f80a87f2SEd Tanous std::string subId; 533*f80a87f2SEd Tanous uint64_t eventSeqNum = 1; 534*f80a87f2SEd Tanous boost::urls::url host; 535*f80a87f2SEd Tanous std::shared_ptr<crow::ConnectionPolicy> policy; 536*f80a87f2SEd Tanous crow::sse_socket::Connection* sseConn = nullptr; 537*f80a87f2SEd Tanous 538*f80a87f2SEd Tanous std::optional<crow::HttpClient> client; 539*f80a87f2SEd Tanous 540*f80a87f2SEd Tanous public: 541*f80a87f2SEd Tanous std::optional<filter_ast::LogicalAnd> filter; 542b52664e2SAppaRao Puli }; 543b52664e2SAppaRao Puli 544b52664e2SAppaRao Puli class EventServiceManager 545b52664e2SAppaRao Puli { 546b52664e2SAppaRao Puli private: 547d3a9e084SEd Tanous bool serviceEnabled = false; 548d3a9e084SEd Tanous uint32_t retryAttempts = 0; 549d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 5507d1cc387SAppaRao Puli 5512558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 5529f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 5539f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 55459d494eeSPatrick Williams std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor; 555b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 556b52664e2SAppaRao Puli subscriptionsMap; 557b52664e2SAppaRao Puli 5589f616dd1SEd Tanous uint64_t eventId{1}; 55996330b99SSunitha Harish 560*f80a87f2SEd Tanous struct Event 561*f80a87f2SEd Tanous { 562*f80a87f2SEd Tanous std::string id; 563*f80a87f2SEd Tanous nlohmann::json message; 564*f80a87f2SEd Tanous }; 565*f80a87f2SEd Tanous 566*f80a87f2SEd Tanous constexpr static size_t maxMessages = 200; 567*f80a87f2SEd Tanous boost::circular_buffer<Event> messages{maxMessages}; 568*f80a87f2SEd Tanous 569f8ca6d79SEd Tanous boost::asio::io_context& ioc; 570f8ca6d79SEd Tanous 571b52664e2SAppaRao Puli public: 5729f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 5739f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 5749f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 5759f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 576ecd6a3a2SEd Tanous ~EventServiceManager() = default; 5779f616dd1SEd Tanous 578f8ca6d79SEd Tanous explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn) 579b52664e2SAppaRao Puli { 580f8ca6d79SEd Tanous // Load config from persist store. 581f8ca6d79SEd Tanous initConfig(); 582f8ca6d79SEd Tanous } 583f8ca6d79SEd Tanous 584f8ca6d79SEd Tanous static EventServiceManager& 585f8ca6d79SEd Tanous getInstance(boost::asio::io_context* ioc = nullptr) 586f8ca6d79SEd Tanous { 587f8ca6d79SEd Tanous static EventServiceManager handler(*ioc); 588b52664e2SAppaRao Puli return handler; 589b52664e2SAppaRao Puli } 590b52664e2SAppaRao Puli 5911bf712bcSAyushi Smriti void initConfig() 5921bf712bcSAyushi Smriti { 59328afb49cSJunLin Chen loadOldBehavior(); 5941bf712bcSAyushi Smriti 59528afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 59628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 59728afb49cSJunLin Chen .getEventServiceConfig(); 5981bf712bcSAyushi Smriti 59928afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 60028afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 60128afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6021bf712bcSAyushi Smriti 60328afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 60428afb49cSJunLin Chen .subscriptionsConfigMap) 6051bf712bcSAyushi Smriti { 60628afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 60728afb49cSJunLin Chen it.second; 6084bbf237fSAppaRao Puli 6096fd29553SEd Tanous boost::system::result<boost::urls::url> url = 610a716aa74SEd Tanous boost::urls::parse_absolute_uri(newSub->destinationUrl); 6111bf712bcSAyushi Smriti 612a716aa74SEd Tanous if (!url) 6131bf712bcSAyushi Smriti { 61462598e31SEd Tanous BMCWEB_LOG_ERROR( 61562598e31SEd Tanous "Failed to validate and split destination url"); 6161bf712bcSAyushi Smriti continue; 6171bf712bcSAyushi Smriti } 6181bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 619a716aa74SEd Tanous std::make_shared<Subscription>(*url, ioc); 6201bf712bcSAyushi Smriti 62128afb49cSJunLin Chen subValue->id = newSub->id; 62228afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 62328afb49cSJunLin Chen subValue->protocol = newSub->protocol; 62419bb362bSEd Tanous subValue->verifyCertificate = newSub->verifyCertificate; 62528afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 62628afb49cSJunLin Chen subValue->customText = newSub->customText; 62728afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 62828afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 62928afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 63028afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 63128afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 63228afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 63328afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 6341bf712bcSAyushi Smriti 63528afb49cSJunLin Chen if (subValue->id.empty()) 6361bf712bcSAyushi Smriti { 63762598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to add subscription"); 6381bf712bcSAyushi Smriti } 63928afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 64028afb49cSJunLin Chen 64128afb49cSJunLin Chen updateNoOfSubscribersCount(); 64228afb49cSJunLin Chen 64383328316SEd Tanous if constexpr (!BMCWEB_REDFISH_DBUS_LOG) 64483328316SEd Tanous { 6452558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 64683328316SEd Tanous } 6472558979cSP Dheeraj Srujan Kumar 64828afb49cSJunLin Chen // Update retry configuration. 64928afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 6501bf712bcSAyushi Smriti } 6511bf712bcSAyushi Smriti } 6521bf712bcSAyushi Smriti 65356d2396dSEd Tanous static void loadOldBehavior() 654b52664e2SAppaRao Puli { 65528afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 65628afb49cSJunLin Chen if (!eventConfigFile.good()) 6571bf712bcSAyushi Smriti { 65862598e31SEd Tanous BMCWEB_LOG_DEBUG("Old eventService config not exist"); 65928afb49cSJunLin Chen return; 66028afb49cSJunLin Chen } 66128afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 66228afb49cSJunLin Chen if (jsonData.is_discarded()) 6634bbf237fSAppaRao Puli { 66462598e31SEd Tanous BMCWEB_LOG_ERROR("Old eventService config parse error."); 66528afb49cSJunLin Chen return; 66628afb49cSJunLin Chen } 66728afb49cSJunLin Chen 6680bdda665SEd Tanous const nlohmann::json::object_t* obj = 6690bdda665SEd Tanous jsonData.get_ptr<const nlohmann::json::object_t*>(); 6700bdda665SEd Tanous for (const auto& item : *obj) 67128afb49cSJunLin Chen { 6720bdda665SEd Tanous if (item.first == "Configuration") 67328afb49cSJunLin Chen { 67428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 67528afb49cSJunLin Chen .getEventServiceConfig() 6760bdda665SEd Tanous .fromJson(item.second); 67728afb49cSJunLin Chen } 6780bdda665SEd Tanous else if (item.first == "Subscriptions") 67928afb49cSJunLin Chen { 6800bdda665SEd Tanous for (const auto& elem : item.second) 68128afb49cSJunLin Chen { 68228afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 68328afb49cSJunLin Chen newSubscription = 68428afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 68528afb49cSJunLin Chen true); 68628afb49cSJunLin Chen if (newSubscription == nullptr) 68728afb49cSJunLin Chen { 68862598e31SEd Tanous BMCWEB_LOG_ERROR("Problem reading subscription " 68962598e31SEd Tanous "from old persistent store"); 6904bbf237fSAppaRao Puli continue; 6914bbf237fSAppaRao Puli } 6921bf712bcSAyushi Smriti 69328afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 69428afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 6951bf712bcSAyushi Smriti 69628afb49cSJunLin Chen std::string id; 6971bf712bcSAyushi Smriti 69828afb49cSJunLin Chen int retry = 3; 699e662eae8SEd Tanous while (retry != 0) 7001bf712bcSAyushi Smriti { 70128afb49cSJunLin Chen id = std::to_string(dist(gen)); 70228afb49cSJunLin Chen if (gen.error()) 7037d1cc387SAppaRao Puli { 70428afb49cSJunLin Chen retry = 0; 70528afb49cSJunLin Chen break; 70628afb49cSJunLin Chen } 70728afb49cSJunLin Chen newSubscription->id = id; 70828afb49cSJunLin Chen auto inserted = 70928afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 71028afb49cSJunLin Chen .subscriptionsConfigMap.insert( 71128afb49cSJunLin Chen std::pair(id, newSubscription)); 71228afb49cSJunLin Chen if (inserted.second) 71328afb49cSJunLin Chen { 71428afb49cSJunLin Chen break; 71528afb49cSJunLin Chen } 71628afb49cSJunLin Chen --retry; 7177d1cc387SAppaRao Puli } 7187d1cc387SAppaRao Puli 71928afb49cSJunLin Chen if (retry <= 0) 72028afb49cSJunLin Chen { 72162598e31SEd Tanous BMCWEB_LOG_ERROR( 72262598e31SEd Tanous "Failed to generate random number from old " 72362598e31SEd Tanous "persistent store"); 72428afb49cSJunLin Chen continue; 72528afb49cSJunLin Chen } 72628afb49cSJunLin Chen } 72728afb49cSJunLin Chen } 72828afb49cSJunLin Chen 72928afb49cSJunLin Chen persistent_data::getConfig().writeData(); 7304c521c3cSEd Tanous std::error_code ec; 7314c521c3cSEd Tanous std::filesystem::remove(eventServiceFile, ec); 7324c521c3cSEd Tanous if (ec) 7334c521c3cSEd Tanous { 7344c521c3cSEd Tanous BMCWEB_LOG_DEBUG( 7354c521c3cSEd Tanous "Failed to remove old event service file. Ignoring"); 7364c521c3cSEd Tanous } 7374c521c3cSEd Tanous else 7384c521c3cSEd Tanous { 73962598e31SEd Tanous BMCWEB_LOG_DEBUG("Remove old eventservice config"); 74028afb49cSJunLin Chen } 74128afb49cSJunLin Chen } 7424c521c3cSEd Tanous } 74328afb49cSJunLin Chen 7449eb808c1SEd Tanous void updateSubscriptionData() const 74528afb49cSJunLin Chen { 74628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 74728afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 74828afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 74928afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 75028afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 75128afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 75228afb49cSJunLin Chen 75328afb49cSJunLin Chen persistent_data::getConfig().writeData(); 75428afb49cSJunLin Chen } 75528afb49cSJunLin Chen 75628afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7577d1cc387SAppaRao Puli { 7587d1cc387SAppaRao Puli bool updateConfig = false; 759fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 7607d1cc387SAppaRao Puli 76128afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 7627d1cc387SAppaRao Puli { 76328afb49cSJunLin Chen serviceEnabled = cfg.enabled; 764e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 7657d1cc387SAppaRao Puli { 7667d1cc387SAppaRao Puli registerMetricReportSignal(); 7677d1cc387SAppaRao Puli } 7687d1cc387SAppaRao Puli else 7697d1cc387SAppaRao Puli { 7707d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7717d1cc387SAppaRao Puli } 7727d1cc387SAppaRao Puli updateConfig = true; 7737d1cc387SAppaRao Puli } 7747d1cc387SAppaRao Puli 77528afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 7767d1cc387SAppaRao Puli { 77728afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 7787d1cc387SAppaRao Puli updateConfig = true; 779fe44eb0bSAyushi Smriti updateRetryCfg = true; 7807d1cc387SAppaRao Puli } 7817d1cc387SAppaRao Puli 78228afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 7837d1cc387SAppaRao Puli { 78428afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 7857d1cc387SAppaRao Puli updateConfig = true; 786fe44eb0bSAyushi Smriti updateRetryCfg = true; 7877d1cc387SAppaRao Puli } 7887d1cc387SAppaRao Puli 7897d1cc387SAppaRao Puli if (updateConfig) 7907d1cc387SAppaRao Puli { 7917d1cc387SAppaRao Puli updateSubscriptionData(); 7927d1cc387SAppaRao Puli } 793fe44eb0bSAyushi Smriti 794fe44eb0bSAyushi Smriti if (updateRetryCfg) 795fe44eb0bSAyushi Smriti { 796fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 797fe44eb0bSAyushi Smriti for (const auto& it : 798fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 799fe44eb0bSAyushi Smriti { 8005e44e3d8SAppaRao Puli Subscription& entry = *it.second; 8015e44e3d8SAppaRao Puli entry.updateRetryConfig(retryAttempts, retryTimeoutInterval); 802fe44eb0bSAyushi Smriti } 803fe44eb0bSAyushi Smriti } 8047d1cc387SAppaRao Puli } 8057d1cc387SAppaRao Puli 8067d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8077d1cc387SAppaRao Puli { 8087d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8097d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8107d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8117d1cc387SAppaRao Puli { 8127d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8137d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8147d1cc387SAppaRao Puli { 8157d1cc387SAppaRao Puli eventLogSubCount++; 8167d1cc387SAppaRao Puli } 8177d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8187d1cc387SAppaRao Puli { 8197d1cc387SAppaRao Puli metricReportSubCount++; 8207d1cc387SAppaRao Puli } 8217d1cc387SAppaRao Puli } 8227d1cc387SAppaRao Puli 8237d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8247d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8257d1cc387SAppaRao Puli { 8267d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 827e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 8287d1cc387SAppaRao Puli { 8297d1cc387SAppaRao Puli registerMetricReportSignal(); 8307d1cc387SAppaRao Puli } 8317d1cc387SAppaRao Puli else 8327d1cc387SAppaRao Puli { 8337d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8347d1cc387SAppaRao Puli } 8357d1cc387SAppaRao Puli } 8367d1cc387SAppaRao Puli } 8377d1cc387SAppaRao Puli 838b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 839b52664e2SAppaRao Puli { 840b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 841b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 842b52664e2SAppaRao Puli { 84362598e31SEd Tanous BMCWEB_LOG_ERROR("No subscription exist with ID:{}", id); 844b52664e2SAppaRao Puli return nullptr; 845b52664e2SAppaRao Puli } 846b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 847b52664e2SAppaRao Puli return subValue; 848b52664e2SAppaRao Puli } 849b52664e2SAppaRao Puli 850*f80a87f2SEd Tanous std::string 851*f80a87f2SEd Tanous addSubscriptionInternal(const std::shared_ptr<Subscription>& subValue) 852b52664e2SAppaRao Puli { 853fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 854fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 855fc76b8acSEd Tanous 856b52664e2SAppaRao Puli std::string id; 857b52664e2SAppaRao Puli 858b52664e2SAppaRao Puli int retry = 3; 859e662eae8SEd Tanous while (retry != 0) 860b52664e2SAppaRao Puli { 861fc76b8acSEd Tanous id = std::to_string(dist(gen)); 862fc76b8acSEd Tanous if (gen.error()) 863fc76b8acSEd Tanous { 864fc76b8acSEd Tanous retry = 0; 865fc76b8acSEd Tanous break; 866fc76b8acSEd Tanous } 867b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 868b52664e2SAppaRao Puli if (inserted.second) 869b52664e2SAppaRao Puli { 870b52664e2SAppaRao Puli break; 871b52664e2SAppaRao Puli } 872b52664e2SAppaRao Puli --retry; 87323a21a1cSEd Tanous } 874b52664e2SAppaRao Puli 875b52664e2SAppaRao Puli if (retry <= 0) 876b52664e2SAppaRao Puli { 87762598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to generate random number"); 878abb93cddSEd Tanous return ""; 879b52664e2SAppaRao Puli } 880b52664e2SAppaRao Puli 88128afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 88228afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 88328afb49cSJunLin Chen newSub->id = id; 88428afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 88528afb49cSJunLin Chen newSub->protocol = subValue->protocol; 88628afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 88728afb49cSJunLin Chen newSub->customText = subValue->customText; 88828afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 88928afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 89028afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 89128afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 89228afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 89328afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 89428afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 89528afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 89628afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 89728afb49cSJunLin Chen 8987d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 8991bf712bcSAyushi Smriti 90083328316SEd Tanous if constexpr (!BMCWEB_REDFISH_DBUS_LOG) 90183328316SEd Tanous { 9022558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 9037f4eb588SAppaRao Puli { 9042558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 9057f4eb588SAppaRao Puli } 90683328316SEd Tanous } 907fe44eb0bSAyushi Smriti // Update retry configuration. 908fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 909fe44eb0bSAyushi Smriti 9105e44e3d8SAppaRao Puli // Set Subscription ID for back trace 9115e44e3d8SAppaRao Puli subValue->setSubscriptionId(id); 912*f80a87f2SEd Tanous 913*f80a87f2SEd Tanous return id; 914*f80a87f2SEd Tanous } 915*f80a87f2SEd Tanous 916*f80a87f2SEd Tanous std::string 917*f80a87f2SEd Tanous addSSESubscription(const std::shared_ptr<Subscription>& subValue, 918*f80a87f2SEd Tanous std::string_view lastEventId) 919*f80a87f2SEd Tanous { 920*f80a87f2SEd Tanous std::string id = addSubscriptionInternal(subValue); 921*f80a87f2SEd Tanous 922*f80a87f2SEd Tanous if (!lastEventId.empty()) 923*f80a87f2SEd Tanous { 924*f80a87f2SEd Tanous BMCWEB_LOG_INFO("Attempting to find message for last id {}", 925*f80a87f2SEd Tanous lastEventId); 926*f80a87f2SEd Tanous boost::circular_buffer<Event>::iterator lastEvent = 927*f80a87f2SEd Tanous std::find_if(messages.begin(), messages.end(), 928*f80a87f2SEd Tanous [&lastEventId](const Event& event) { 929*f80a87f2SEd Tanous return event.id == lastEventId; 930*f80a87f2SEd Tanous }); 931*f80a87f2SEd Tanous // Can't find a matching ID 932*f80a87f2SEd Tanous if (lastEvent == messages.end()) 933*f80a87f2SEd Tanous { 934*f80a87f2SEd Tanous nlohmann::json msg = messages::eventBufferExceeded(); 935*f80a87f2SEd Tanous // If the buffer overloaded, send all messages. 936*f80a87f2SEd Tanous subValue->sendEvent(msg); 937*f80a87f2SEd Tanous lastEvent = messages.begin(); 938*f80a87f2SEd Tanous } 939*f80a87f2SEd Tanous else 940*f80a87f2SEd Tanous { 941*f80a87f2SEd Tanous // Skip the last event the user already has 942*f80a87f2SEd Tanous lastEvent++; 943*f80a87f2SEd Tanous } 944*f80a87f2SEd Tanous 945*f80a87f2SEd Tanous for (boost::circular_buffer<Event>::const_iterator event = 946*f80a87f2SEd Tanous lastEvent; 947*f80a87f2SEd Tanous lastEvent != messages.end(); lastEvent++) 948*f80a87f2SEd Tanous { 949*f80a87f2SEd Tanous subValue->sendEvent(event->message); 950*f80a87f2SEd Tanous } 951*f80a87f2SEd Tanous } 952*f80a87f2SEd Tanous return id; 953*f80a87f2SEd Tanous } 954*f80a87f2SEd Tanous 955*f80a87f2SEd Tanous std::string 956*f80a87f2SEd Tanous addPushSubscription(const std::shared_ptr<Subscription>& subValue) 957*f80a87f2SEd Tanous { 958*f80a87f2SEd Tanous std::string id = addSubscriptionInternal(subValue); 959*f80a87f2SEd Tanous 960*f80a87f2SEd Tanous updateSubscriptionData(); 961b52664e2SAppaRao Puli return id; 962b52664e2SAppaRao Puli } 963b52664e2SAppaRao Puli 964b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 965b52664e2SAppaRao Puli { 966b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 96755f79e6fSEd Tanous return obj != subscriptionsMap.end(); 968b52664e2SAppaRao Puli } 969b52664e2SAppaRao Puli 970b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 971b52664e2SAppaRao Puli { 972b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 973b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 974b52664e2SAppaRao Puli { 975b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 97628afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 97728afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 97828afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 97928afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9807d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 981b52664e2SAppaRao Puli updateSubscriptionData(); 982b52664e2SAppaRao Puli } 983b52664e2SAppaRao Puli } 984b52664e2SAppaRao Puli 9855e44e3d8SAppaRao Puli void deleteSseSubscription(const crow::sse_socket::Connection& thisConn) 9865e44e3d8SAppaRao Puli { 987bdbfae2aSEd Tanous for (auto it = subscriptionsMap.begin(); it != subscriptionsMap.end();) 9885e44e3d8SAppaRao Puli { 989bdbfae2aSEd Tanous std::shared_ptr<Subscription> entry = it->second; 9905e44e3d8SAppaRao Puli bool entryIsThisConn = entry->matchSseId(thisConn); 9915e44e3d8SAppaRao Puli if (entryIsThisConn) 9925e44e3d8SAppaRao Puli { 9935e44e3d8SAppaRao Puli persistent_data::EventServiceStore::getInstance() 9945e44e3d8SAppaRao Puli .subscriptionsConfigMap.erase( 995bdbfae2aSEd Tanous it->second->getSubscriptionId()); 996bdbfae2aSEd Tanous it = subscriptionsMap.erase(it); 9975e44e3d8SAppaRao Puli return; 9985e44e3d8SAppaRao Puli } 999bdbfae2aSEd Tanous it++; 10005e44e3d8SAppaRao Puli } 10015e44e3d8SAppaRao Puli } 10025e44e3d8SAppaRao Puli 10035e44e3d8SAppaRao Puli size_t getNumberOfSubscriptions() const 1004b52664e2SAppaRao Puli { 1005b52664e2SAppaRao Puli return subscriptionsMap.size(); 1006b52664e2SAppaRao Puli } 1007b52664e2SAppaRao Puli 10085e44e3d8SAppaRao Puli size_t getNumberOfSSESubscriptions() const 10095e44e3d8SAppaRao Puli { 10103544d2a7SEd Tanous auto size = std::ranges::count_if( 10113544d2a7SEd Tanous subscriptionsMap, 10125e44e3d8SAppaRao Puli [](const std::pair<std::string, std::shared_ptr<Subscription>>& 10135e44e3d8SAppaRao Puli entry) { 10145e44e3d8SAppaRao Puli return (entry.second->subscriptionType == subscriptionTypeSSE); 10155e44e3d8SAppaRao Puli }); 10165e44e3d8SAppaRao Puli return static_cast<size_t>(size); 10175e44e3d8SAppaRao Puli } 10185e44e3d8SAppaRao Puli 1019b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 1020b52664e2SAppaRao Puli { 1021b52664e2SAppaRao Puli std::vector<std::string> idList; 1022b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 1023b52664e2SAppaRao Puli { 1024b52664e2SAppaRao Puli idList.emplace_back(it.first); 1025b52664e2SAppaRao Puli } 1026b52664e2SAppaRao Puli return idList; 1027b52664e2SAppaRao Puli } 1028b52664e2SAppaRao Puli 10296ba8c82eSsunharis_in bool sendTestEventLog() 10300b4bdd93SAppaRao Puli { 10315e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 10320b4bdd93SAppaRao Puli { 10330b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 10346ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 10356ba8c82eSsunharis_in { 10366ba8c82eSsunharis_in return false; 10370b4bdd93SAppaRao Puli } 10380b4bdd93SAppaRao Puli } 10396ba8c82eSsunharis_in return true; 10406ba8c82eSsunharis_in } 1041e9a14131SAppaRao Puli 1042*f80a87f2SEd Tanous void sendEvent(nlohmann::json::object_t eventMessage, 1043*f80a87f2SEd Tanous std::string_view origin, std::string_view resourceType) 104496330b99SSunitha Harish { 1045613dabeaSEd Tanous eventMessage["EventId"] = eventId; 1046*f80a87f2SEd Tanous 1047613dabeaSEd Tanous eventMessage["EventTimestamp"] = 1048613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1049613dabeaSEd Tanous eventMessage["OriginOfCondition"] = origin; 1050613dabeaSEd Tanous 1051*f80a87f2SEd Tanous // MemberId is 0 : since we are sending one event record. 1052*f80a87f2SEd Tanous eventMessage["MemberId"] = 0; 105396330b99SSunitha Harish 1054*f80a87f2SEd Tanous messages.push_back(Event(std::to_string(eventId), eventMessage)); 1055*f80a87f2SEd Tanous 1056*f80a87f2SEd Tanous for (auto& it : subscriptionsMap) 105796330b99SSunitha Harish { 1058*f80a87f2SEd Tanous std::shared_ptr<Subscription>& entry = it.second; 1059*f80a87f2SEd Tanous if (!entry->eventMatchesFilter(eventMessage, resourceType)) 106096330b99SSunitha Harish { 1061*f80a87f2SEd Tanous BMCWEB_LOG_DEBUG("Filter didn't match"); 1062*f80a87f2SEd Tanous continue; 106396330b99SSunitha Harish } 1064*f80a87f2SEd Tanous 1065*f80a87f2SEd Tanous nlohmann::json::array_t eventRecord; 1066*f80a87f2SEd Tanous eventRecord.emplace_back(eventMessage); 1067*f80a87f2SEd Tanous 1068613dabeaSEd Tanous nlohmann::json msgJson; 1069613dabeaSEd Tanous 1070613dabeaSEd Tanous msgJson["@odata.type"] = "#Event.v1_4_0.Event"; 1071613dabeaSEd Tanous msgJson["Name"] = "Event Log"; 1072613dabeaSEd Tanous msgJson["Id"] = eventId; 1073*f80a87f2SEd Tanous msgJson["Events"] = std::move(eventRecord); 1074f52c03c1SCarson Labrado 1075f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1076f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 10775e44e3d8SAppaRao Puli entry->sendEvent(std::move(strMsg)); 10788ece0e45SEd Tanous eventId++; // increment the eventId 107996330b99SSunitha Harish } 108096330b99SSunitha Harish } 108196330b99SSunitha Harish 10822558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 10837f4eb588SAppaRao Puli { 10842558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 10852558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 10862558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 10872558979cSP Dheeraj Srujan Kumar } 10882558979cSP Dheeraj Srujan Kumar 10892558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 10902558979cSP Dheeraj Srujan Kumar { 10912558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 10922558979cSP Dheeraj Srujan Kumar 10937f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 10947f4eb588SAppaRao Puli if (!logStream.good()) 10957f4eb588SAppaRao Puli { 109662598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed "); 10977f4eb588SAppaRao Puli return; 10987f4eb588SAppaRao Puli } 10997f4eb588SAppaRao Puli std::string logEntry; 11007f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11017f4eb588SAppaRao Puli { 11022558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11037f4eb588SAppaRao Puli } 11047f4eb588SAppaRao Puli } 11057f4eb588SAppaRao Puli 11067f4eb588SAppaRao Puli void readEventLogsFromFile() 11077f4eb588SAppaRao Puli { 11087f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11097f4eb588SAppaRao Puli if (!logStream.good()) 11107f4eb588SAppaRao Puli { 111162598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed"); 11127f4eb588SAppaRao Puli return; 11137f4eb588SAppaRao Puli } 11147f4eb588SAppaRao Puli 11157f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11167f4eb588SAppaRao Puli 11177f4eb588SAppaRao Puli std::string logEntry; 11182558979cSP Dheeraj Srujan Kumar 11192558979cSP Dheeraj Srujan Kumar // Get the read pointer to the next log to be read. 11202558979cSP Dheeraj Srujan Kumar logStream.seekg(redfishLogFilePosition); 11212558979cSP Dheeraj Srujan Kumar 11227f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11237f4eb588SAppaRao Puli { 11242558979cSP Dheeraj Srujan Kumar // Update Pointer position 11252558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11262558979cSP Dheeraj Srujan Kumar 11272558979cSP Dheeraj Srujan Kumar std::string idStr; 11282558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 11297f4eb588SAppaRao Puli { 11307f4eb588SAppaRao Puli continue; 11317f4eb588SAppaRao Puli } 11327f4eb588SAppaRao Puli 1133e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11347f4eb588SAppaRao Puli { 11352558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 11362558979cSP Dheeraj Srujan Kumar // the remaining items below. 11372558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 11387f4eb588SAppaRao Puli continue; 11397f4eb588SAppaRao Puli } 11407f4eb588SAppaRao Puli 11417f4eb588SAppaRao Puli std::string timestamp; 11427f4eb588SAppaRao Puli std::string messageID; 11435e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11447f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11457f4eb588SAppaRao Puli messageArgs) != 0) 11467f4eb588SAppaRao Puli { 114762598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry params failed"); 11487f4eb588SAppaRao Puli continue; 11497f4eb588SAppaRao Puli } 11507f4eb588SAppaRao Puli 1151*f80a87f2SEd Tanous eventRecords.emplace_back(idStr, timestamp, messageID, messageArgs); 11527f4eb588SAppaRao Puli } 11537f4eb588SAppaRao Puli 1154e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11552558979cSP Dheeraj Srujan Kumar { 115662598e31SEd Tanous BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions."); 11572558979cSP Dheeraj Srujan Kumar return; 11582558979cSP Dheeraj Srujan Kumar } 11592558979cSP Dheeraj Srujan Kumar 11602558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 11612558979cSP Dheeraj Srujan Kumar { 11622558979cSP Dheeraj Srujan Kumar // No Records to send 116362598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 11642558979cSP Dheeraj Srujan Kumar return; 11652558979cSP Dheeraj Srujan Kumar } 11662558979cSP Dheeraj Srujan Kumar 11675e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 11687f4eb588SAppaRao Puli { 11697f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 11707f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 11717f4eb588SAppaRao Puli { 11727f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 11737f4eb588SAppaRao Puli } 11747f4eb588SAppaRao Puli } 11757f4eb588SAppaRao Puli } 11767f4eb588SAppaRao Puli 11777f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 11787f4eb588SAppaRao Puli { 11796a9f85f9SAppaRao Puli if (!inotifyConn) 11807f4eb588SAppaRao Puli { 11817f4eb588SAppaRao Puli return; 11827f4eb588SAppaRao Puli } 11837f4eb588SAppaRao Puli 11847f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 11857f4eb588SAppaRao Puli 1186002d39b4SEd Tanous inotifyConn->async_read_some(boost::asio::buffer(readBuffer), 11877f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 11887f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 11899ed3f90aSEd Tanous if (ec == boost::asio::error::operation_aborted) 11909ed3f90aSEd Tanous { 11919ed3f90aSEd Tanous BMCWEB_LOG_DEBUG("Inotify was canceled (shutdown?)"); 11929ed3f90aSEd Tanous return; 11939ed3f90aSEd Tanous } 11947f4eb588SAppaRao Puli if (ec) 11957f4eb588SAppaRao Puli { 119662598e31SEd Tanous BMCWEB_LOG_ERROR("Callback Error: {}", ec.message()); 11977f4eb588SAppaRao Puli return; 11987f4eb588SAppaRao Puli } 11997f4eb588SAppaRao Puli std::size_t index = 0; 1200b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 12017f4eb588SAppaRao Puli { 1202d3a9e084SEd Tanous struct inotify_event event 1203d3a9e084SEd Tanous {}; 1204b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1205b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1206b792cc56SAppaRao Puli { 1207b792cc56SAppaRao Puli if ((event.len == 0) || 1208b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1209b792cc56SAppaRao Puli { 1210b792cc56SAppaRao Puli index += (iEventSize + event.len); 1211b792cc56SAppaRao Puli continue; 1212b792cc56SAppaRao Puli } 1213b792cc56SAppaRao Puli 12144f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 12154f568f74SJiaqing Zhao if (fileName != "redfish") 1216b792cc56SAppaRao Puli { 1217b792cc56SAppaRao Puli index += (iEventSize + event.len); 1218b792cc56SAppaRao Puli continue; 1219b792cc56SAppaRao Puli } 1220b792cc56SAppaRao Puli 122162598e31SEd Tanous BMCWEB_LOG_DEBUG( 122262598e31SEd Tanous "Redfish log file created/deleted. event.name: {}", 122362598e31SEd Tanous fileName); 1224b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1225b792cc56SAppaRao Puli { 1226b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1227b792cc56SAppaRao Puli { 122862598e31SEd Tanous BMCWEB_LOG_DEBUG( 122962598e31SEd Tanous "Remove and Add inotify watcher on " 123062598e31SEd Tanous "redfish event log file"); 1231016761afSAppaRao Puli // Remove existing inotify watcher and add 1232016761afSAppaRao Puli // with new redfish event log file. 1233016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1234016761afSAppaRao Puli fileWatchDesc = -1; 1235b792cc56SAppaRao Puli } 1236b792cc56SAppaRao Puli 1237b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1238b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1239b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1240b792cc56SAppaRao Puli { 124162598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for " 124262598e31SEd Tanous "redfish log file."); 1243b792cc56SAppaRao Puli return; 1244b792cc56SAppaRao Puli } 1245b792cc56SAppaRao Puli 1246b792cc56SAppaRao Puli EventServiceManager::getInstance() 12472558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1248b792cc56SAppaRao Puli EventServiceManager::getInstance() 1249b792cc56SAppaRao Puli .readEventLogsFromFile(); 1250b792cc56SAppaRao Puli } 1251b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1252b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1253b792cc56SAppaRao Puli { 1254b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1255b792cc56SAppaRao Puli { 1256b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1257b792cc56SAppaRao Puli fileWatchDesc = -1; 1258b792cc56SAppaRao Puli } 1259b792cc56SAppaRao Puli } 1260b792cc56SAppaRao Puli } 1261b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1262b792cc56SAppaRao Puli { 1263b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12647f4eb588SAppaRao Puli { 12657f4eb588SAppaRao Puli EventServiceManager::getInstance() 12667f4eb588SAppaRao Puli .readEventLogsFromFile(); 12677f4eb588SAppaRao Puli } 1268b792cc56SAppaRao Puli } 1269b792cc56SAppaRao Puli index += (iEventSize + event.len); 12707f4eb588SAppaRao Puli } 12717f4eb588SAppaRao Puli 12727f4eb588SAppaRao Puli watchRedfishEventLogFile(); 12737f4eb588SAppaRao Puli }); 12747f4eb588SAppaRao Puli } 12757f4eb588SAppaRao Puli 12767f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 12777f4eb588SAppaRao Puli { 127823a21a1cSEd Tanous inotifyConn.emplace(ioc); 1279b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1280b792cc56SAppaRao Puli if (inotifyFd == -1) 12817f4eb588SAppaRao Puli { 128262598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_init1 failed."); 12837f4eb588SAppaRao Puli return -1; 12847f4eb588SAppaRao Puli } 1285b792cc56SAppaRao Puli 1286b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1287b792cc56SAppaRao Puli // create/delete. 1288b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1289b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1290b792cc56SAppaRao Puli if (dirWatchDesc == -1) 12917f4eb588SAppaRao Puli { 129262598e31SEd Tanous BMCWEB_LOG_ERROR( 129362598e31SEd Tanous "inotify_add_watch failed for event log directory."); 12947f4eb588SAppaRao Puli return -1; 12957f4eb588SAppaRao Puli } 12967f4eb588SAppaRao Puli 1297b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 129889492a15SPatrick Williams fileWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogFile, 129989492a15SPatrick Williams IN_MODIFY); 1300b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1301b792cc56SAppaRao Puli { 130262598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for redfish log file."); 1303b792cc56SAppaRao Puli // Don't return error if file not exist. 1304b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1305b792cc56SAppaRao Puli } 1306b792cc56SAppaRao Puli 13077f4eb588SAppaRao Puli // monitor redfish event log file 1308b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13097f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13107f4eb588SAppaRao Puli 13117f4eb588SAppaRao Puli return 0; 13127f4eb588SAppaRao Puli } 13137f4eb588SAppaRao Puli 13149ed3f90aSEd Tanous static void stopEventLogMonitor() 13159ed3f90aSEd Tanous { 13169ed3f90aSEd Tanous inotifyConn.reset(); 13179ed3f90aSEd Tanous } 13189ed3f90aSEd Tanous 131959d494eeSPatrick Williams static void getReadingsForReport(sdbusplus::message_t& msg) 1320156d6b00SAppaRao Puli { 132156d2396dSEd Tanous if (msg.is_method_error()) 132256d2396dSEd Tanous { 132362598e31SEd Tanous BMCWEB_LOG_ERROR("TelemetryMonitor Signal error"); 132456d2396dSEd Tanous return; 132556d2396dSEd Tanous } 132656d2396dSEd Tanous 1327c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1328c0353249SWludzik, Jozef std::string id = path.filename(); 1329c0353249SWludzik, Jozef if (id.empty()) 1330156d6b00SAppaRao Puli { 133162598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get Id from path"); 1332156d6b00SAppaRao Puli return; 1333156d6b00SAppaRao Puli } 1334156d6b00SAppaRao Puli 1335c0353249SWludzik, Jozef std::string interface; 1336b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1337c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1338c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1339c0353249SWludzik, Jozef 13403544d2a7SEd Tanous auto found = std::ranges::find_if( 13413544d2a7SEd Tanous props, [](const auto& x) { return x.first == "Readings"; }); 1342c0353249SWludzik, Jozef if (found == props.end()) 1343156d6b00SAppaRao Puli { 134462598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 1345156d6b00SAppaRao Puli return; 1346156d6b00SAppaRao Puli } 1347156d6b00SAppaRao Puli 13481e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 13491e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1350e662eae8SEd Tanous if (readings == nullptr) 13511e1e598dSJonathan Doman { 135262598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 13531e1e598dSJonathan Doman return; 13541e1e598dSJonathan Doman } 13551e1e598dSJonathan Doman 1356156d6b00SAppaRao Puli for (const auto& it : 1357156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1358156d6b00SAppaRao Puli { 1359e05aec50SEd Tanous Subscription& entry = *it.second; 1360c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1361156d6b00SAppaRao Puli { 13621e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1363156d6b00SAppaRao Puli } 1364156d6b00SAppaRao Puli } 1365156d6b00SAppaRao Puli } 1366156d6b00SAppaRao Puli 1367156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1368156d6b00SAppaRao Puli { 13697d1cc387SAppaRao Puli if (matchTelemetryMonitor) 13707d1cc387SAppaRao Puli { 137162598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Unregister"); 1372156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1373156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1374156d6b00SAppaRao Puli } 13757d1cc387SAppaRao Puli } 1376156d6b00SAppaRao Puli 1377156d6b00SAppaRao Puli void registerMetricReportSignal() 1378156d6b00SAppaRao Puli { 13797d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1380156d6b00SAppaRao Puli { 138162598e31SEd Tanous BMCWEB_LOG_DEBUG("Not registering metric report signal."); 1382156d6b00SAppaRao Puli return; 1383156d6b00SAppaRao Puli } 1384156d6b00SAppaRao Puli 138562598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Register"); 1386c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1387c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1388c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1389156d6b00SAppaRao Puli 139059d494eeSPatrick Williams matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>( 139156d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1392156d6b00SAppaRao Puli } 139323a21a1cSEd Tanous }; 1394b52664e2SAppaRao Puli 1395b52664e2SAppaRao Puli } // namespace redfish 1396