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" 20f80a87f2SEd Tanous #include "filter_expr_executor.hpp" 21539d8c6bSEd Tanous #include "generated/enums/event.hpp" 22539d8c6bSEd Tanous #include "generated/enums/log_entry.hpp" 233ccb3adbSEd Tanous #include "http_client.hpp" 24c0353249SWludzik, Jozef #include "metric_report.hpp" 252c6ffdb0SEd Tanous #include "ossl_random.hpp" 263ccb3adbSEd Tanous #include "persistent_data.hpp" 277f4eb588SAppaRao Puli #include "registries.hpp" 288dab0f58SEd Tanous #include "registries_selector.hpp" 2950ebd4afSEd Tanous #include "str_utility.hpp" 3077665bdaSNan Zhou #include "utility.hpp" 313ccb3adbSEd Tanous #include "utils/json_utils.hpp" 325b90429aSEd Tanous #include "utils/time_utils.hpp" 337f4eb588SAppaRao Puli 347f4eb588SAppaRao Puli #include <sys/inotify.h> 35b52664e2SAppaRao Puli 36fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 37f80a87f2SEd Tanous #include <boost/circular_buffer.hpp> 38b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 39ef4c65b7SEd Tanous #include <boost/url/format.hpp> 404a7fbefdSEd Tanous #include <boost/url/url_view_base.hpp> 41b5b40605Snitroglycerine #include <sdbusplus/bus/match.hpp> 421214b7e7SGunnar Mills 435e44e3d8SAppaRao Puli #include <algorithm> 44b52664e2SAppaRao Puli #include <cstdlib> 45b52664e2SAppaRao Puli #include <ctime> 461bf712bcSAyushi Smriti #include <fstream> 47b52664e2SAppaRao Puli #include <memory> 483544d2a7SEd Tanous #include <ranges> 4926702d01SEd Tanous #include <span> 50b52664e2SAppaRao Puli 51b52664e2SAppaRao Puli namespace redfish 52b52664e2SAppaRao Puli { 53156d6b00SAppaRao Puli 54156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 55156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 56156d6b00SAppaRao Puli 575e44e3d8SAppaRao Puli static constexpr const char* subscriptionTypeSSE = "SSE"; 581bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 591bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 601bf712bcSAyushi Smriti 615e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSubscriptions = 20; 625e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSSESubscriptions = 10; 635e44e3d8SAppaRao Puli 64cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 654642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 664642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 674642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 684642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 69cf9e417dSEd Tanous 70cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 714642bf8fSGeorge Liu static int inotifyFd = -1; 72cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 734642bf8fSGeorge Liu static int dirWatchDesc = -1; 74cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 754642bf8fSGeorge Liu static int fileWatchDesc = -1; 76f80a87f2SEd Tanous struct EventLogObjectsType 77f80a87f2SEd Tanous { 78f80a87f2SEd Tanous std::string id; 79f80a87f2SEd Tanous std::string timestamp; 80f80a87f2SEd Tanous std::string messageId; 81f80a87f2SEd Tanous std::vector<std::string> messageArgs; 82f80a87f2SEd Tanous }; 834642bf8fSGeorge Liu 84fffb8c1fSEd Tanous namespace registries 854642bf8fSGeorge Liu { 867f4eb588SAppaRao Puli static const Message* 877f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 8826702d01SEd Tanous const std::span<const MessageEntry>& registry) 897f4eb588SAppaRao Puli { 903544d2a7SEd Tanous std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if( 913544d2a7SEd Tanous registry, [&messageKey](const MessageEntry& messageEntry) { 9255f79e6fSEd Tanous return messageKey == messageEntry.first; 937f4eb588SAppaRao Puli }); 9426702d01SEd Tanous if (messageIt != registry.end()) 957f4eb588SAppaRao Puli { 967f4eb588SAppaRao Puli return &messageIt->second; 977f4eb588SAppaRao Puli } 987f4eb588SAppaRao Puli 997f4eb588SAppaRao Puli return nullptr; 1007f4eb588SAppaRao Puli } 1017f4eb588SAppaRao Puli 10226ccae32SEd Tanous static const Message* formatMessage(std::string_view messageID) 1037f4eb588SAppaRao Puli { 1047f4eb588SAppaRao Puli // Redfish MessageIds are in the form 1057f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1067f4eb588SAppaRao Puli // the right Message 1077f4eb588SAppaRao Puli std::vector<std::string> fields; 1087f4eb588SAppaRao Puli fields.reserve(4); 10950ebd4afSEd Tanous 11050ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 1117f4eb588SAppaRao Puli if (fields.size() != 4) 1127f4eb588SAppaRao Puli { 1137f4eb588SAppaRao Puli return nullptr; 1147f4eb588SAppaRao Puli } 11502cad96eSEd Tanous const std::string& registryName = fields[0]; 11602cad96eSEd Tanous const std::string& messageKey = fields[3]; 1177f4eb588SAppaRao Puli 1187f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 119b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1207f4eb588SAppaRao Puli } 121fffb8c1fSEd Tanous } // namespace registries 1227f4eb588SAppaRao Puli 1237f4eb588SAppaRao Puli namespace event_log 1247f4eb588SAppaRao Puli { 1252558979cSP Dheeraj Srujan Kumar inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 1267f4eb588SAppaRao Puli { 1277f4eb588SAppaRao Puli static time_t prevTs = 0; 1287f4eb588SAppaRao Puli static int index = 0; 1297f4eb588SAppaRao Puli 1307f4eb588SAppaRao Puli // Get the entry timestamp 1317f4eb588SAppaRao Puli std::time_t curTs = 0; 1327f4eb588SAppaRao Puli std::tm timeStruct = {}; 1337f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1347f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1357f4eb588SAppaRao Puli { 1367f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1377f4eb588SAppaRao Puli if (curTs == -1) 1387f4eb588SAppaRao Puli { 1397f4eb588SAppaRao Puli return false; 1407f4eb588SAppaRao Puli } 1417f4eb588SAppaRao Puli } 1427f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1437f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1447f4eb588SAppaRao Puli 1457f4eb588SAppaRao Puli // Save the timestamp 1467f4eb588SAppaRao Puli prevTs = curTs; 1477f4eb588SAppaRao Puli 1487f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1497f4eb588SAppaRao Puli if (index > 0) 1507f4eb588SAppaRao Puli { 1517f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1527f4eb588SAppaRao Puli } 1537f4eb588SAppaRao Puli return true; 1547f4eb588SAppaRao Puli } 1557f4eb588SAppaRao Puli 15623a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 15723a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1585e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1597f4eb588SAppaRao Puli { 1607f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1617f4eb588SAppaRao Puli // First get the Timestamp 162f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1637f4eb588SAppaRao Puli if (space == std::string::npos) 1647f4eb588SAppaRao Puli { 165*03d4d37cSAlexander Hansen BMCWEB_LOG_ERROR("EventLog Params: could not find first space: {}", 166*03d4d37cSAlexander Hansen logEntry); 1677f4eb588SAppaRao Puli return -EINVAL; 1687f4eb588SAppaRao Puli } 1697f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1707f4eb588SAppaRao Puli // Then get the log contents 171f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1727f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1737f4eb588SAppaRao Puli { 174*03d4d37cSAlexander Hansen BMCWEB_LOG_ERROR("EventLog Params: could not find log contents: {}", 175*03d4d37cSAlexander Hansen logEntry); 1767f4eb588SAppaRao Puli return -EINVAL; 1777f4eb588SAppaRao Puli } 1787f4eb588SAppaRao Puli std::string_view entry(logEntry); 1797f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1807f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1817f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 18250ebd4afSEd Tanous bmcweb::split(logEntryFields, entry, ','); 1837f4eb588SAppaRao Puli // We need at least a MessageId to be valid 18426f6976fSEd Tanous if (logEntryFields.empty()) 1857f4eb588SAppaRao Puli { 186*03d4d37cSAlexander Hansen BMCWEB_LOG_ERROR("EventLog Params: could not find entry fields: {}", 187*03d4d37cSAlexander Hansen logEntry); 1887f4eb588SAppaRao Puli return -EINVAL; 1897f4eb588SAppaRao Puli } 1907f4eb588SAppaRao Puli messageID = logEntryFields[0]; 1917f4eb588SAppaRao Puli 1927f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 1937f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 1947f4eb588SAppaRao Puli { 19502cad96eSEd Tanous const std::string& messageArgsStart = logEntryFields[1]; 1967f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 1977f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 1987f4eb588SAppaRao Puli { 1995e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 2005e715de6SAppaRao Puli logEntryFields.end()); 2017f4eb588SAppaRao Puli } 2027f4eb588SAppaRao Puli } 2037f4eb588SAppaRao Puli 2047f4eb588SAppaRao Puli return 0; 2057f4eb588SAppaRao Puli } 2067f4eb588SAppaRao Puli 20723a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 2087f4eb588SAppaRao Puli std::string& registryName, 2097f4eb588SAppaRao Puli std::string& messageKey) 2107f4eb588SAppaRao Puli { 2117f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2127f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2137f4eb588SAppaRao Puli // the right Message 2147f4eb588SAppaRao Puli std::vector<std::string> fields; 2157f4eb588SAppaRao Puli fields.reserve(4); 21650ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 2177f4eb588SAppaRao Puli if (fields.size() == 4) 2187f4eb588SAppaRao Puli { 2197f4eb588SAppaRao Puli registryName = fields[0]; 2207f4eb588SAppaRao Puli messageKey = fields[3]; 2217f4eb588SAppaRao Puli } 2227f4eb588SAppaRao Puli } 2237f4eb588SAppaRao Puli 224bd79bce8SPatrick Williams inline int formatEventLogEntry( 225bd79bce8SPatrick Williams const std::string& logEntryID, const std::string& messageID, 226bd79bce8SPatrick Williams const std::span<std::string_view> messageArgs, std::string timestamp, 227bd79bce8SPatrick Williams const std::string& customText, nlohmann::json::object_t& logEntryJson) 2287f4eb588SAppaRao Puli { 2297f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 230fffb8c1fSEd Tanous const registries::Message* message = registries::formatMessage(messageID); 2317f4eb588SAppaRao Puli 23280f595e7SEd Tanous if (message == nullptr) 2337f4eb588SAppaRao Puli { 23480f595e7SEd Tanous return -1; 2357f4eb588SAppaRao Puli } 2367f4eb588SAppaRao Puli 237bd79bce8SPatrick Williams std::string msg = 238bd79bce8SPatrick Williams redfish::registries::fillMessageArgs(messageArgs, message->message); 23980f595e7SEd Tanous if (msg.empty()) 24080f595e7SEd Tanous { 24180f595e7SEd Tanous return -1; 24280f595e7SEd Tanous } 2437f4eb588SAppaRao Puli 2447f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2457f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2467f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 247f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 248b2f7609bSEd Tanous std::size_t plus = timestamp.find_first_of('+', dot); 2497f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2507f4eb588SAppaRao Puli { 2517f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2527f4eb588SAppaRao Puli } 2537f4eb588SAppaRao Puli 2547f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2551476687dSEd Tanous logEntryJson["EventId"] = logEntryID; 256539d8c6bSEd Tanous 25780f595e7SEd Tanous logEntryJson["Severity"] = message->messageSeverity; 2581476687dSEd Tanous logEntryJson["Message"] = std::move(msg); 2591476687dSEd Tanous logEntryJson["MessageId"] = messageID; 2601476687dSEd Tanous logEntryJson["MessageArgs"] = messageArgs; 2611476687dSEd Tanous logEntryJson["EventTimestamp"] = std::move(timestamp); 2621476687dSEd Tanous logEntryJson["Context"] = customText; 2637f4eb588SAppaRao Puli return 0; 2647f4eb588SAppaRao Puli } 2657f4eb588SAppaRao Puli 2667f4eb588SAppaRao Puli } // namespace event_log 2677f4eb588SAppaRao Puli 26828afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 269b52664e2SAppaRao Puli { 270b52664e2SAppaRao Puli public: 271b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 272b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 273b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 274b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 275b52664e2SAppaRao Puli 2764a7fbefdSEd Tanous Subscription(const boost::urls::url_view_base& url, 2774a7fbefdSEd Tanous boost::asio::io_context& ioc) : 278a716aa74SEd Tanous policy(std::make_shared<crow::ConnectionPolicy>()) 279b52664e2SAppaRao Puli { 280a716aa74SEd Tanous destinationUrl = url; 2815e44e3d8SAppaRao Puli client.emplace(ioc, policy); 2827adb85acSSunitha Harish // Subscription constructor 283d14a48ffSCarson Labrado policy->invalidResp = retryRespHandler; 284b52664e2SAppaRao Puli } 2854bbf237fSAppaRao Puli 2865e44e3d8SAppaRao Puli explicit Subscription(crow::sse_socket::Connection& connIn) : 2875e44e3d8SAppaRao Puli sseConn(&connIn) 2885e44e3d8SAppaRao Puli {} 2895e44e3d8SAppaRao Puli 2909f616dd1SEd Tanous ~Subscription() = default; 291b52664e2SAppaRao Puli 2925e44e3d8SAppaRao Puli bool sendEvent(std::string&& msg) 293b52664e2SAppaRao Puli { 2946ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 2956ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 2966ba8c82eSsunharis_in .getEventServiceConfig(); 2976ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 2986ba8c82eSsunharis_in { 2996ba8c82eSsunharis_in return false; 3006ba8c82eSsunharis_in } 3016ba8c82eSsunharis_in 3025e44e3d8SAppaRao Puli if (client) 3035e44e3d8SAppaRao Puli { 30419bb362bSEd Tanous client->sendData( 30519bb362bSEd Tanous std::move(msg), destinationUrl, 30619bb362bSEd Tanous static_cast<ensuressl::VerifyCertificate>(verifyCertificate), 30719bb362bSEd Tanous httpHeaders, boost::beast::http::verb::post); 3085e44e3d8SAppaRao Puli return true; 3095e44e3d8SAppaRao Puli } 3107adb85acSSunitha Harish 3114bbf237fSAppaRao Puli if (sseConn != nullptr) 3124bbf237fSAppaRao Puli { 3135e44e3d8SAppaRao Puli eventSeqNum++; 3145e44e3d8SAppaRao Puli sseConn->sendEvent(std::to_string(eventSeqNum), msg); 3154bbf237fSAppaRao Puli } 3166ba8c82eSsunharis_in return true; 3174bbf237fSAppaRao Puli } 3184bbf237fSAppaRao Puli 319f80a87f2SEd Tanous bool eventMatchesFilter(const nlohmann::json::object_t& eventMessage, 320f80a87f2SEd Tanous std::string_view resType) 321f80a87f2SEd Tanous { 322f80a87f2SEd Tanous // If resourceTypes list is empty, assume all 323f80a87f2SEd Tanous if (!resourceTypes.empty()) 324f80a87f2SEd Tanous { 325f80a87f2SEd Tanous // Search the resourceTypes list for the subscription. 326f80a87f2SEd Tanous auto resourceTypeIndex = std::ranges::find_if( 327f80a87f2SEd Tanous resourceTypes, [resType](const std::string& rtEntry) { 328f80a87f2SEd Tanous return rtEntry == resType; 329f80a87f2SEd Tanous }); 330f80a87f2SEd Tanous if (resourceTypeIndex == resourceTypes.end()) 331f80a87f2SEd Tanous { 332f80a87f2SEd Tanous BMCWEB_LOG_DEBUG("Not subscribed to this resource"); 333f80a87f2SEd Tanous return false; 334f80a87f2SEd Tanous } 335f80a87f2SEd Tanous BMCWEB_LOG_DEBUG("ResourceType {} found in the subscribed list", 336f80a87f2SEd Tanous resType); 337f80a87f2SEd Tanous } 338f80a87f2SEd Tanous 339f80a87f2SEd Tanous // If registryMsgIds list is empty, assume all 340f80a87f2SEd Tanous if (!registryMsgIds.empty()) 341f80a87f2SEd Tanous { 342f80a87f2SEd Tanous auto eventJson = eventMessage.find("MessageId"); 343f80a87f2SEd Tanous if (eventJson == eventMessage.end()) 344f80a87f2SEd Tanous { 345*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("'MessageId' not present"); 346f80a87f2SEd Tanous return false; 347f80a87f2SEd Tanous } 348f80a87f2SEd Tanous 349f80a87f2SEd Tanous const std::string* messageId = 350f80a87f2SEd Tanous eventJson->second.get_ptr<const std::string*>(); 351f80a87f2SEd Tanous if (messageId == nullptr) 352f80a87f2SEd Tanous { 353f80a87f2SEd Tanous BMCWEB_LOG_ERROR("EventType wasn't a string???"); 354f80a87f2SEd Tanous return false; 355f80a87f2SEd Tanous } 356f80a87f2SEd Tanous 357f80a87f2SEd Tanous std::string registry; 358f80a87f2SEd Tanous std::string messageKey; 359f80a87f2SEd Tanous event_log::getRegistryAndMessageKey(*messageId, registry, 360f80a87f2SEd Tanous messageKey); 361f80a87f2SEd Tanous 362*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("extracted registry {}", registry); 363*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("extracted message key {}", messageKey); 364*03d4d37cSAlexander Hansen 365f80a87f2SEd Tanous auto obj = std::ranges::find(registryMsgIds, registry); 366f80a87f2SEd Tanous if (obj == registryMsgIds.end()) 367f80a87f2SEd Tanous { 368*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("did not find registry {} in registryMsgIds", 369*03d4d37cSAlexander Hansen registry); 370*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("registryMsgIds has {} entries", 371*03d4d37cSAlexander Hansen registryMsgIds.size()); 372f80a87f2SEd Tanous return false; 373f80a87f2SEd Tanous } 374f80a87f2SEd Tanous } 375f80a87f2SEd Tanous 376f80a87f2SEd Tanous if (filter) 377f80a87f2SEd Tanous { 378f80a87f2SEd Tanous if (!memberMatches(eventMessage, *filter)) 379f80a87f2SEd Tanous { 380f80a87f2SEd Tanous BMCWEB_LOG_DEBUG("Filter didn't match"); 381f80a87f2SEd Tanous return false; 382f80a87f2SEd Tanous } 383f80a87f2SEd Tanous } 384f80a87f2SEd Tanous 385f80a87f2SEd Tanous return true; 386f80a87f2SEd Tanous } 387f80a87f2SEd Tanous 3886ba8c82eSsunharis_in bool sendTestEventLog() 3890b4bdd93SAppaRao Puli { 390f80a87f2SEd Tanous nlohmann::json::array_t logEntryArray; 391f80a87f2SEd Tanous nlohmann::json& logEntryJson = logEntryArray.emplace_back(); 3920b4bdd93SAppaRao Puli 393613dabeaSEd Tanous logEntryJson["EventId"] = "TestID"; 394539d8c6bSEd Tanous logEntryJson["Severity"] = log_entry::EventSeverity::OK; 395613dabeaSEd Tanous logEntryJson["Message"] = "Generated test event"; 396613dabeaSEd Tanous logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog"; 397d2cdd478SChandra Harkude // MemberId is 0 : since we are sending one event record. 398d2cdd478SChandra Harkude logEntryJson["MemberId"] = 0; 399613dabeaSEd Tanous logEntryJson["MessageArgs"] = nlohmann::json::array(); 400613dabeaSEd Tanous logEntryJson["EventTimestamp"] = 401613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 402613dabeaSEd Tanous logEntryJson["Context"] = customText; 4030b4bdd93SAppaRao Puli 4041476687dSEd Tanous nlohmann::json msg; 4051476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4061476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4071476687dSEd Tanous msg["Name"] = "Event Log"; 4081476687dSEd Tanous msg["Events"] = logEntryArray; 4090b4bdd93SAppaRao Puli 410bd79bce8SPatrick Williams std::string strMsg = 411bd79bce8SPatrick Williams msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 4125e44e3d8SAppaRao Puli return sendEvent(std::move(strMsg)); 4130b4bdd93SAppaRao Puli } 4140b4bdd93SAppaRao Puli 4157f4eb588SAppaRao Puli void filterAndSendEventLogs( 4167f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4177f4eb588SAppaRao Puli { 418f80a87f2SEd Tanous nlohmann::json::array_t logEntryArray; 4197f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4207f4eb588SAppaRao Puli { 421f80a87f2SEd Tanous std::vector<std::string_view> messageArgsView( 422f80a87f2SEd Tanous logEntry.messageArgs.begin(), logEntry.messageArgs.end()); 4237f4eb588SAppaRao Puli 424f80a87f2SEd Tanous nlohmann::json::object_t bmcLogEntry; 425f80a87f2SEd Tanous if (event_log::formatEventLogEntry( 426f80a87f2SEd Tanous logEntry.id, logEntry.messageId, messageArgsView, 427f80a87f2SEd Tanous logEntry.timestamp, customText, bmcLogEntry) != 0) 4287f4eb588SAppaRao Puli { 42962598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry failed"); 4307f4eb588SAppaRao Puli continue; 4317f4eb588SAppaRao Puli } 432f80a87f2SEd Tanous 433f80a87f2SEd Tanous if (!eventMatchesFilter(bmcLogEntry, "")) 434f80a87f2SEd Tanous { 435*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("Event {} did not match the filter", 436*03d4d37cSAlexander Hansen nlohmann::json(bmcLogEntry).dump()); 437f80a87f2SEd Tanous continue; 438f80a87f2SEd Tanous } 439f80a87f2SEd Tanous 440f80a87f2SEd Tanous logEntryArray.emplace_back(std::move(bmcLogEntry)); 4417f4eb588SAppaRao Puli } 4427f4eb588SAppaRao Puli 44326f6976fSEd Tanous if (logEntryArray.empty()) 4447f4eb588SAppaRao Puli { 44562598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 4467f4eb588SAppaRao Puli return; 4477f4eb588SAppaRao Puli } 4487f4eb588SAppaRao Puli 4491476687dSEd Tanous nlohmann::json msg; 4501476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4511476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4521476687dSEd Tanous msg["Name"] = "Event Log"; 453f80a87f2SEd Tanous msg["Events"] = std::move(logEntryArray); 454bd79bce8SPatrick Williams std::string strMsg = 455bd79bce8SPatrick Williams msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 4565e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 4575e44e3d8SAppaRao Puli eventSeqNum++; 4587f4eb588SAppaRao Puli } 4597f4eb588SAppaRao Puli 460248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 4611e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 462156d6b00SAppaRao Puli { 463ef4c65b7SEd Tanous boost::urls::url mrdUri = boost::urls::format( 464ef4c65b7SEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", 465ef4c65b7SEd Tanous reportId); 466156d6b00SAppaRao Puli 467156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 46826f6976fSEd Tanous if (!metricReportDefinitions.empty()) 469156d6b00SAppaRao Puli { 4703544d2a7SEd Tanous if (std::ranges::find(metricReportDefinitions, mrdUri.buffer()) == 4713544d2a7SEd Tanous metricReportDefinitions.end()) 472156d6b00SAppaRao Puli { 473156d6b00SAppaRao Puli return; 474156d6b00SAppaRao Puli } 475156d6b00SAppaRao Puli } 476156d6b00SAppaRao Puli 477c0353249SWludzik, Jozef nlohmann::json msg; 478248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 479156d6b00SAppaRao Puli { 48062598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to fill the MetricReport for DBus " 48162598e31SEd Tanous "Report with id {}", 48262598e31SEd Tanous reportId); 483c0353249SWludzik, Jozef return; 484156d6b00SAppaRao Puli } 485156d6b00SAppaRao Puli 48622daffd7SAppaRao Puli // Context is set by user during Event subscription and it must be 48722daffd7SAppaRao Puli // set for MetricReport response. 48822daffd7SAppaRao Puli if (!customText.empty()) 48922daffd7SAppaRao Puli { 49022daffd7SAppaRao Puli msg["Context"] = customText; 49122daffd7SAppaRao Puli } 49222daffd7SAppaRao Puli 493bd79bce8SPatrick Williams std::string strMsg = 494bd79bce8SPatrick Williams msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 4955e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 496156d6b00SAppaRao Puli } 497156d6b00SAppaRao Puli 498d14a48ffSCarson Labrado void updateRetryConfig(uint32_t retryAttempts, 499d14a48ffSCarson Labrado uint32_t retryTimeoutInterval) 500fe44eb0bSAyushi Smriti { 50193cf0ac2SEd Tanous if (policy == nullptr) 50293cf0ac2SEd Tanous { 50393cf0ac2SEd Tanous BMCWEB_LOG_DEBUG("Retry policy was nullptr, ignoring set"); 50493cf0ac2SEd Tanous return; 50593cf0ac2SEd Tanous } 506d14a48ffSCarson Labrado policy->maxRetryAttempts = retryAttempts; 507d14a48ffSCarson Labrado policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval); 50862de0c68SAppaRao Puli } 509fe44eb0bSAyushi Smriti 5109eb808c1SEd Tanous uint64_t getEventSeqNum() const 51196330b99SSunitha Harish { 51296330b99SSunitha Harish return eventSeqNum; 51396330b99SSunitha Harish } 51496330b99SSunitha Harish 5155e44e3d8SAppaRao Puli void setSubscriptionId(const std::string& id2) 5165e44e3d8SAppaRao Puli { 51762598e31SEd Tanous BMCWEB_LOG_DEBUG("Subscription ID: {}", id2); 5185e44e3d8SAppaRao Puli subId = id2; 5195e44e3d8SAppaRao Puli } 5205e44e3d8SAppaRao Puli 5215e44e3d8SAppaRao Puli std::string getSubscriptionId() 5225e44e3d8SAppaRao Puli { 5235e44e3d8SAppaRao Puli return subId; 5245e44e3d8SAppaRao Puli } 5255e44e3d8SAppaRao Puli 5265e44e3d8SAppaRao Puli bool matchSseId(const crow::sse_socket::Connection& thisConn) 5275e44e3d8SAppaRao Puli { 5285e44e3d8SAppaRao Puli return &thisConn == sseConn; 5295e44e3d8SAppaRao Puli } 5305e44e3d8SAppaRao Puli 531a7a80296SCarson Labrado // Check used to indicate what response codes are valid as part of our retry 532a7a80296SCarson Labrado // policy. 2XX is considered acceptable 533a7a80296SCarson Labrado static boost::system::error_code retryRespHandler(unsigned int respCode) 534a7a80296SCarson Labrado { 53562598e31SEd Tanous BMCWEB_LOG_DEBUG( 53662598e31SEd Tanous "Checking response code validity for SubscriptionEvent"); 537a7a80296SCarson Labrado if ((respCode < 200) || (respCode >= 300)) 538a7a80296SCarson Labrado { 539a7a80296SCarson Labrado return boost::system::errc::make_error_code( 540a7a80296SCarson Labrado boost::system::errc::result_out_of_range); 541a7a80296SCarson Labrado } 542a7a80296SCarson Labrado 543a7a80296SCarson Labrado // Return 0 if the response code is valid 544a7a80296SCarson Labrado return boost::system::errc::make_error_code( 545a7a80296SCarson Labrado boost::system::errc::success); 5469fa6d147SNan Zhou } 547f80a87f2SEd Tanous 548f80a87f2SEd Tanous private: 549f80a87f2SEd Tanous std::string subId; 550f80a87f2SEd Tanous uint64_t eventSeqNum = 1; 551f80a87f2SEd Tanous boost::urls::url host; 552f80a87f2SEd Tanous std::shared_ptr<crow::ConnectionPolicy> policy; 553f80a87f2SEd Tanous crow::sse_socket::Connection* sseConn = nullptr; 554f80a87f2SEd Tanous 555f80a87f2SEd Tanous std::optional<crow::HttpClient> client; 556f80a87f2SEd Tanous 557f80a87f2SEd Tanous public: 558f80a87f2SEd Tanous std::optional<filter_ast::LogicalAnd> filter; 559b52664e2SAppaRao Puli }; 560b52664e2SAppaRao Puli 561b52664e2SAppaRao Puli class EventServiceManager 562b52664e2SAppaRao Puli { 563b52664e2SAppaRao Puli private: 564d3a9e084SEd Tanous bool serviceEnabled = false; 565d3a9e084SEd Tanous uint32_t retryAttempts = 0; 566d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 5677d1cc387SAppaRao Puli 5682558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 5699f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 5709f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 57159d494eeSPatrick Williams std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor; 572b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 573b52664e2SAppaRao Puli subscriptionsMap; 574b52664e2SAppaRao Puli 5759f616dd1SEd Tanous uint64_t eventId{1}; 57696330b99SSunitha Harish 577f80a87f2SEd Tanous struct Event 578f80a87f2SEd Tanous { 579f80a87f2SEd Tanous std::string id; 580f80a87f2SEd Tanous nlohmann::json message; 581f80a87f2SEd Tanous }; 582f80a87f2SEd Tanous 583f80a87f2SEd Tanous constexpr static size_t maxMessages = 200; 584f80a87f2SEd Tanous boost::circular_buffer<Event> messages{maxMessages}; 585f80a87f2SEd Tanous 586f8ca6d79SEd Tanous boost::asio::io_context& ioc; 587f8ca6d79SEd Tanous 588b52664e2SAppaRao Puli public: 5899f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 5909f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 5919f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 5929f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 593ecd6a3a2SEd Tanous ~EventServiceManager() = default; 5949f616dd1SEd Tanous 595f8ca6d79SEd Tanous explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn) 596b52664e2SAppaRao Puli { 597f8ca6d79SEd Tanous // Load config from persist store. 598f8ca6d79SEd Tanous initConfig(); 599f8ca6d79SEd Tanous } 600f8ca6d79SEd Tanous 601f8ca6d79SEd Tanous static EventServiceManager& 602f8ca6d79SEd Tanous getInstance(boost::asio::io_context* ioc = nullptr) 603f8ca6d79SEd Tanous { 604f8ca6d79SEd Tanous static EventServiceManager handler(*ioc); 605b52664e2SAppaRao Puli return handler; 606b52664e2SAppaRao Puli } 607b52664e2SAppaRao Puli 6081bf712bcSAyushi Smriti void initConfig() 6091bf712bcSAyushi Smriti { 61028afb49cSJunLin Chen loadOldBehavior(); 6111bf712bcSAyushi Smriti 61228afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 61328afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 61428afb49cSJunLin Chen .getEventServiceConfig(); 6151bf712bcSAyushi Smriti 61628afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 61728afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 61828afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6191bf712bcSAyushi Smriti 62028afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 62128afb49cSJunLin Chen .subscriptionsConfigMap) 6221bf712bcSAyushi Smriti { 62328afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 62428afb49cSJunLin Chen it.second; 6254bbf237fSAppaRao Puli 6266fd29553SEd Tanous boost::system::result<boost::urls::url> url = 627a716aa74SEd Tanous boost::urls::parse_absolute_uri(newSub->destinationUrl); 6281bf712bcSAyushi Smriti 629a716aa74SEd Tanous if (!url) 6301bf712bcSAyushi Smriti { 63162598e31SEd Tanous BMCWEB_LOG_ERROR( 63262598e31SEd Tanous "Failed to validate and split destination url"); 6331bf712bcSAyushi Smriti continue; 6341bf712bcSAyushi Smriti } 6351bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 636a716aa74SEd Tanous std::make_shared<Subscription>(*url, ioc); 6371bf712bcSAyushi Smriti 63828afb49cSJunLin Chen subValue->id = newSub->id; 63928afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 64028afb49cSJunLin Chen subValue->protocol = newSub->protocol; 64119bb362bSEd Tanous subValue->verifyCertificate = newSub->verifyCertificate; 64228afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 64328afb49cSJunLin Chen subValue->customText = newSub->customText; 64428afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 64528afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 64628afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 64728afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 64828afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 64928afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 65028afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 6511bf712bcSAyushi Smriti 65228afb49cSJunLin Chen if (subValue->id.empty()) 6531bf712bcSAyushi Smriti { 65462598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to add subscription"); 6551bf712bcSAyushi Smriti } 65628afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 65728afb49cSJunLin Chen 65828afb49cSJunLin Chen updateNoOfSubscribersCount(); 65928afb49cSJunLin Chen 66083328316SEd Tanous if constexpr (!BMCWEB_REDFISH_DBUS_LOG) 66183328316SEd Tanous { 6622558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 66383328316SEd Tanous } 6642558979cSP Dheeraj Srujan Kumar 66528afb49cSJunLin Chen // Update retry configuration. 66628afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 6671bf712bcSAyushi Smriti } 6681bf712bcSAyushi Smriti } 6691bf712bcSAyushi Smriti 67056d2396dSEd Tanous static void loadOldBehavior() 671b52664e2SAppaRao Puli { 67228afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 67328afb49cSJunLin Chen if (!eventConfigFile.good()) 6741bf712bcSAyushi Smriti { 67562598e31SEd Tanous BMCWEB_LOG_DEBUG("Old eventService config not exist"); 67628afb49cSJunLin Chen return; 67728afb49cSJunLin Chen } 67828afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 67928afb49cSJunLin Chen if (jsonData.is_discarded()) 6804bbf237fSAppaRao Puli { 68162598e31SEd Tanous BMCWEB_LOG_ERROR("Old eventService config parse error."); 68228afb49cSJunLin Chen return; 68328afb49cSJunLin Chen } 68428afb49cSJunLin Chen 6850bdda665SEd Tanous const nlohmann::json::object_t* obj = 6860bdda665SEd Tanous jsonData.get_ptr<const nlohmann::json::object_t*>(); 6870bdda665SEd Tanous for (const auto& item : *obj) 68828afb49cSJunLin Chen { 6890bdda665SEd Tanous if (item.first == "Configuration") 69028afb49cSJunLin Chen { 69128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 69228afb49cSJunLin Chen .getEventServiceConfig() 6930bdda665SEd Tanous .fromJson(item.second); 69428afb49cSJunLin Chen } 6950bdda665SEd Tanous else if (item.first == "Subscriptions") 69628afb49cSJunLin Chen { 6970bdda665SEd Tanous for (const auto& elem : item.second) 69828afb49cSJunLin Chen { 69928afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 70028afb49cSJunLin Chen newSubscription = 70128afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 70228afb49cSJunLin Chen true); 70328afb49cSJunLin Chen if (newSubscription == nullptr) 70428afb49cSJunLin Chen { 70562598e31SEd Tanous BMCWEB_LOG_ERROR("Problem reading subscription " 70662598e31SEd Tanous "from old persistent store"); 7074bbf237fSAppaRao Puli continue; 7084bbf237fSAppaRao Puli } 7091bf712bcSAyushi Smriti 71028afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 71128afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 7121bf712bcSAyushi Smriti 71328afb49cSJunLin Chen std::string id; 7141bf712bcSAyushi Smriti 71528afb49cSJunLin Chen int retry = 3; 716e662eae8SEd Tanous while (retry != 0) 7171bf712bcSAyushi Smriti { 71828afb49cSJunLin Chen id = std::to_string(dist(gen)); 71928afb49cSJunLin Chen if (gen.error()) 7207d1cc387SAppaRao Puli { 72128afb49cSJunLin Chen retry = 0; 72228afb49cSJunLin Chen break; 72328afb49cSJunLin Chen } 72428afb49cSJunLin Chen newSubscription->id = id; 72528afb49cSJunLin Chen auto inserted = 72628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 72728afb49cSJunLin Chen .subscriptionsConfigMap.insert( 72828afb49cSJunLin Chen std::pair(id, newSubscription)); 72928afb49cSJunLin Chen if (inserted.second) 73028afb49cSJunLin Chen { 73128afb49cSJunLin Chen break; 73228afb49cSJunLin Chen } 73328afb49cSJunLin Chen --retry; 7347d1cc387SAppaRao Puli } 7357d1cc387SAppaRao Puli 73628afb49cSJunLin Chen if (retry <= 0) 73728afb49cSJunLin Chen { 73862598e31SEd Tanous BMCWEB_LOG_ERROR( 73962598e31SEd Tanous "Failed to generate random number from old " 74062598e31SEd Tanous "persistent store"); 74128afb49cSJunLin Chen continue; 74228afb49cSJunLin Chen } 74328afb49cSJunLin Chen } 74428afb49cSJunLin Chen } 74528afb49cSJunLin Chen 74628afb49cSJunLin Chen persistent_data::getConfig().writeData(); 7474c521c3cSEd Tanous std::error_code ec; 7484c521c3cSEd Tanous std::filesystem::remove(eventServiceFile, ec); 7494c521c3cSEd Tanous if (ec) 7504c521c3cSEd Tanous { 7514c521c3cSEd Tanous BMCWEB_LOG_DEBUG( 7524c521c3cSEd Tanous "Failed to remove old event service file. Ignoring"); 7534c521c3cSEd Tanous } 7544c521c3cSEd Tanous else 7554c521c3cSEd Tanous { 75662598e31SEd Tanous BMCWEB_LOG_DEBUG("Remove old eventservice config"); 75728afb49cSJunLin Chen } 75828afb49cSJunLin Chen } 7594c521c3cSEd Tanous } 76028afb49cSJunLin Chen 7619eb808c1SEd Tanous void updateSubscriptionData() const 76228afb49cSJunLin Chen { 76328afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 76428afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 76528afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 76628afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 76728afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 76828afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 76928afb49cSJunLin Chen 77028afb49cSJunLin Chen persistent_data::getConfig().writeData(); 77128afb49cSJunLin Chen } 77228afb49cSJunLin Chen 77328afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7747d1cc387SAppaRao Puli { 7757d1cc387SAppaRao Puli bool updateConfig = false; 776fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 7777d1cc387SAppaRao Puli 77828afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 7797d1cc387SAppaRao Puli { 78028afb49cSJunLin Chen serviceEnabled = cfg.enabled; 781e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 7827d1cc387SAppaRao Puli { 7837d1cc387SAppaRao Puli registerMetricReportSignal(); 7847d1cc387SAppaRao Puli } 7857d1cc387SAppaRao Puli else 7867d1cc387SAppaRao Puli { 7877d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7887d1cc387SAppaRao Puli } 7897d1cc387SAppaRao Puli updateConfig = true; 7907d1cc387SAppaRao Puli } 7917d1cc387SAppaRao Puli 79228afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 7937d1cc387SAppaRao Puli { 79428afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 7957d1cc387SAppaRao Puli updateConfig = true; 796fe44eb0bSAyushi Smriti updateRetryCfg = true; 7977d1cc387SAppaRao Puli } 7987d1cc387SAppaRao Puli 79928afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 8007d1cc387SAppaRao Puli { 80128afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 8027d1cc387SAppaRao Puli updateConfig = true; 803fe44eb0bSAyushi Smriti updateRetryCfg = true; 8047d1cc387SAppaRao Puli } 8057d1cc387SAppaRao Puli 8067d1cc387SAppaRao Puli if (updateConfig) 8077d1cc387SAppaRao Puli { 8087d1cc387SAppaRao Puli updateSubscriptionData(); 8097d1cc387SAppaRao Puli } 810fe44eb0bSAyushi Smriti 811fe44eb0bSAyushi Smriti if (updateRetryCfg) 812fe44eb0bSAyushi Smriti { 813fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 814fe44eb0bSAyushi Smriti for (const auto& it : 815fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 816fe44eb0bSAyushi Smriti { 8175e44e3d8SAppaRao Puli Subscription& entry = *it.second; 8185e44e3d8SAppaRao Puli entry.updateRetryConfig(retryAttempts, retryTimeoutInterval); 819fe44eb0bSAyushi Smriti } 820fe44eb0bSAyushi Smriti } 8217d1cc387SAppaRao Puli } 8227d1cc387SAppaRao Puli 8237d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8247d1cc387SAppaRao Puli { 8257d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8267d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8277d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8287d1cc387SAppaRao Puli { 8297d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8307d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8317d1cc387SAppaRao Puli { 8327d1cc387SAppaRao Puli eventLogSubCount++; 8337d1cc387SAppaRao Puli } 8347d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8357d1cc387SAppaRao Puli { 8367d1cc387SAppaRao Puli metricReportSubCount++; 8377d1cc387SAppaRao Puli } 8387d1cc387SAppaRao Puli } 8397d1cc387SAppaRao Puli 8407d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8417d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8427d1cc387SAppaRao Puli { 8437d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 844e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 8457d1cc387SAppaRao Puli { 8467d1cc387SAppaRao Puli registerMetricReportSignal(); 8477d1cc387SAppaRao Puli } 8487d1cc387SAppaRao Puli else 8497d1cc387SAppaRao Puli { 8507d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8517d1cc387SAppaRao Puli } 8527d1cc387SAppaRao Puli } 8537d1cc387SAppaRao Puli } 8547d1cc387SAppaRao Puli 855b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 856b52664e2SAppaRao Puli { 857b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 858b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 859b52664e2SAppaRao Puli { 86062598e31SEd Tanous BMCWEB_LOG_ERROR("No subscription exist with ID:{}", id); 861b52664e2SAppaRao Puli return nullptr; 862b52664e2SAppaRao Puli } 863b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 864b52664e2SAppaRao Puli return subValue; 865b52664e2SAppaRao Puli } 866b52664e2SAppaRao Puli 867f80a87f2SEd Tanous std::string 868f80a87f2SEd Tanous addSubscriptionInternal(const std::shared_ptr<Subscription>& subValue) 869b52664e2SAppaRao Puli { 870fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 871fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 872fc76b8acSEd Tanous 873b52664e2SAppaRao Puli std::string id; 874b52664e2SAppaRao Puli 875b52664e2SAppaRao Puli int retry = 3; 876e662eae8SEd Tanous while (retry != 0) 877b52664e2SAppaRao Puli { 878fc76b8acSEd Tanous id = std::to_string(dist(gen)); 879fc76b8acSEd Tanous if (gen.error()) 880fc76b8acSEd Tanous { 881fc76b8acSEd Tanous retry = 0; 882fc76b8acSEd Tanous break; 883fc76b8acSEd Tanous } 884b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 885b52664e2SAppaRao Puli if (inserted.second) 886b52664e2SAppaRao Puli { 887b52664e2SAppaRao Puli break; 888b52664e2SAppaRao Puli } 889b52664e2SAppaRao Puli --retry; 89023a21a1cSEd Tanous } 891b52664e2SAppaRao Puli 892b52664e2SAppaRao Puli if (retry <= 0) 893b52664e2SAppaRao Puli { 89462598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to generate random number"); 895abb93cddSEd Tanous return ""; 896b52664e2SAppaRao Puli } 897b52664e2SAppaRao Puli 89828afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 89928afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 90028afb49cSJunLin Chen newSub->id = id; 90128afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 90228afb49cSJunLin Chen newSub->protocol = subValue->protocol; 90328afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 90428afb49cSJunLin Chen newSub->customText = subValue->customText; 90528afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 90628afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 90728afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 90828afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 90928afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 91028afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 91128afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 91228afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 91328afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 91428afb49cSJunLin Chen 9157d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 9161bf712bcSAyushi Smriti 91783328316SEd Tanous if constexpr (!BMCWEB_REDFISH_DBUS_LOG) 91883328316SEd Tanous { 9192558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 9207f4eb588SAppaRao Puli { 9212558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 9227f4eb588SAppaRao Puli } 92383328316SEd Tanous } 924fe44eb0bSAyushi Smriti // Update retry configuration. 925fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 926fe44eb0bSAyushi Smriti 9275e44e3d8SAppaRao Puli // Set Subscription ID for back trace 9285e44e3d8SAppaRao Puli subValue->setSubscriptionId(id); 929f80a87f2SEd Tanous 930f80a87f2SEd Tanous return id; 931f80a87f2SEd Tanous } 932f80a87f2SEd Tanous 933f80a87f2SEd Tanous std::string 934f80a87f2SEd Tanous addSSESubscription(const std::shared_ptr<Subscription>& subValue, 935f80a87f2SEd Tanous std::string_view lastEventId) 936f80a87f2SEd Tanous { 937f80a87f2SEd Tanous std::string id = addSubscriptionInternal(subValue); 938f80a87f2SEd Tanous 939f80a87f2SEd Tanous if (!lastEventId.empty()) 940f80a87f2SEd Tanous { 941f80a87f2SEd Tanous BMCWEB_LOG_INFO("Attempting to find message for last id {}", 942f80a87f2SEd Tanous lastEventId); 943f80a87f2SEd Tanous boost::circular_buffer<Event>::iterator lastEvent = 944f80a87f2SEd Tanous std::find_if(messages.begin(), messages.end(), 945f80a87f2SEd Tanous [&lastEventId](const Event& event) { 946f80a87f2SEd Tanous return event.id == lastEventId; 947f80a87f2SEd Tanous }); 948f80a87f2SEd Tanous // Can't find a matching ID 949f80a87f2SEd Tanous if (lastEvent == messages.end()) 950f80a87f2SEd Tanous { 951f80a87f2SEd Tanous nlohmann::json msg = messages::eventBufferExceeded(); 952f80a87f2SEd Tanous // If the buffer overloaded, send all messages. 953f80a87f2SEd Tanous subValue->sendEvent(msg); 954f80a87f2SEd Tanous lastEvent = messages.begin(); 955f80a87f2SEd Tanous } 956f80a87f2SEd Tanous else 957f80a87f2SEd Tanous { 958f80a87f2SEd Tanous // Skip the last event the user already has 959f80a87f2SEd Tanous lastEvent++; 960f80a87f2SEd Tanous } 961f80a87f2SEd Tanous 962f80a87f2SEd Tanous for (boost::circular_buffer<Event>::const_iterator event = 963f80a87f2SEd Tanous lastEvent; 964f80a87f2SEd Tanous lastEvent != messages.end(); lastEvent++) 965f80a87f2SEd Tanous { 966f80a87f2SEd Tanous subValue->sendEvent(event->message); 967f80a87f2SEd Tanous } 968f80a87f2SEd Tanous } 969f80a87f2SEd Tanous return id; 970f80a87f2SEd Tanous } 971f80a87f2SEd Tanous 972f80a87f2SEd Tanous std::string 973f80a87f2SEd Tanous addPushSubscription(const std::shared_ptr<Subscription>& subValue) 974f80a87f2SEd Tanous { 975f80a87f2SEd Tanous std::string id = addSubscriptionInternal(subValue); 976f80a87f2SEd Tanous 977f80a87f2SEd Tanous updateSubscriptionData(); 978b52664e2SAppaRao Puli return id; 979b52664e2SAppaRao Puli } 980b52664e2SAppaRao Puli 981b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 982b52664e2SAppaRao Puli { 983b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 98455f79e6fSEd Tanous return obj != subscriptionsMap.end(); 985b52664e2SAppaRao Puli } 986b52664e2SAppaRao Puli 987b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 988b52664e2SAppaRao Puli { 989b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 990b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 991b52664e2SAppaRao Puli { 992b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 99328afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 99428afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 99528afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 99628afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9977d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 998b52664e2SAppaRao Puli updateSubscriptionData(); 999b52664e2SAppaRao Puli } 1000b52664e2SAppaRao Puli } 1001b52664e2SAppaRao Puli 10025e44e3d8SAppaRao Puli void deleteSseSubscription(const crow::sse_socket::Connection& thisConn) 10035e44e3d8SAppaRao Puli { 1004bdbfae2aSEd Tanous for (auto it = subscriptionsMap.begin(); it != subscriptionsMap.end();) 10055e44e3d8SAppaRao Puli { 1006bdbfae2aSEd Tanous std::shared_ptr<Subscription> entry = it->second; 10075e44e3d8SAppaRao Puli bool entryIsThisConn = entry->matchSseId(thisConn); 10085e44e3d8SAppaRao Puli if (entryIsThisConn) 10095e44e3d8SAppaRao Puli { 10105e44e3d8SAppaRao Puli persistent_data::EventServiceStore::getInstance() 10115e44e3d8SAppaRao Puli .subscriptionsConfigMap.erase( 1012bdbfae2aSEd Tanous it->second->getSubscriptionId()); 1013bdbfae2aSEd Tanous it = subscriptionsMap.erase(it); 10145e44e3d8SAppaRao Puli return; 10155e44e3d8SAppaRao Puli } 1016bdbfae2aSEd Tanous it++; 10175e44e3d8SAppaRao Puli } 10185e44e3d8SAppaRao Puli } 10195e44e3d8SAppaRao Puli 10205e44e3d8SAppaRao Puli size_t getNumberOfSubscriptions() const 1021b52664e2SAppaRao Puli { 1022b52664e2SAppaRao Puli return subscriptionsMap.size(); 1023b52664e2SAppaRao Puli } 1024b52664e2SAppaRao Puli 10255e44e3d8SAppaRao Puli size_t getNumberOfSSESubscriptions() const 10265e44e3d8SAppaRao Puli { 10273544d2a7SEd Tanous auto size = std::ranges::count_if( 10283544d2a7SEd Tanous subscriptionsMap, 10295e44e3d8SAppaRao Puli [](const std::pair<std::string, std::shared_ptr<Subscription>>& 10305e44e3d8SAppaRao Puli entry) { 10315e44e3d8SAppaRao Puli return (entry.second->subscriptionType == subscriptionTypeSSE); 10325e44e3d8SAppaRao Puli }); 10335e44e3d8SAppaRao Puli return static_cast<size_t>(size); 10345e44e3d8SAppaRao Puli } 10355e44e3d8SAppaRao Puli 1036b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 1037b52664e2SAppaRao Puli { 1038b52664e2SAppaRao Puli std::vector<std::string> idList; 1039b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 1040b52664e2SAppaRao Puli { 1041b52664e2SAppaRao Puli idList.emplace_back(it.first); 1042b52664e2SAppaRao Puli } 1043b52664e2SAppaRao Puli return idList; 1044b52664e2SAppaRao Puli } 1045b52664e2SAppaRao Puli 10466ba8c82eSsunharis_in bool sendTestEventLog() 10470b4bdd93SAppaRao Puli { 10485e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 10490b4bdd93SAppaRao Puli { 10500b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 10516ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 10526ba8c82eSsunharis_in { 10536ba8c82eSsunharis_in return false; 10540b4bdd93SAppaRao Puli } 10550b4bdd93SAppaRao Puli } 10566ba8c82eSsunharis_in return true; 10576ba8c82eSsunharis_in } 1058e9a14131SAppaRao Puli 1059f80a87f2SEd Tanous void sendEvent(nlohmann::json::object_t eventMessage, 1060f80a87f2SEd Tanous std::string_view origin, std::string_view resourceType) 106196330b99SSunitha Harish { 1062613dabeaSEd Tanous eventMessage["EventId"] = eventId; 1063f80a87f2SEd Tanous 1064613dabeaSEd Tanous eventMessage["EventTimestamp"] = 1065613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1066613dabeaSEd Tanous eventMessage["OriginOfCondition"] = origin; 1067613dabeaSEd Tanous 1068f80a87f2SEd Tanous // MemberId is 0 : since we are sending one event record. 1069f80a87f2SEd Tanous eventMessage["MemberId"] = 0; 107096330b99SSunitha Harish 1071f80a87f2SEd Tanous messages.push_back(Event(std::to_string(eventId), eventMessage)); 1072f80a87f2SEd Tanous 1073f80a87f2SEd Tanous for (auto& it : subscriptionsMap) 107496330b99SSunitha Harish { 1075f80a87f2SEd Tanous std::shared_ptr<Subscription>& entry = it.second; 1076f80a87f2SEd Tanous if (!entry->eventMatchesFilter(eventMessage, resourceType)) 107796330b99SSunitha Harish { 1078f80a87f2SEd Tanous BMCWEB_LOG_DEBUG("Filter didn't match"); 1079f80a87f2SEd Tanous continue; 108096330b99SSunitha Harish } 1081f80a87f2SEd Tanous 1082f80a87f2SEd Tanous nlohmann::json::array_t eventRecord; 1083f80a87f2SEd Tanous eventRecord.emplace_back(eventMessage); 1084f80a87f2SEd Tanous 1085613dabeaSEd Tanous nlohmann::json msgJson; 1086613dabeaSEd Tanous 1087613dabeaSEd Tanous msgJson["@odata.type"] = "#Event.v1_4_0.Event"; 1088613dabeaSEd Tanous msgJson["Name"] = "Event Log"; 1089613dabeaSEd Tanous msgJson["Id"] = eventId; 1090f80a87f2SEd Tanous msgJson["Events"] = std::move(eventRecord); 1091f52c03c1SCarson Labrado 1092f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1093f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 10945e44e3d8SAppaRao Puli entry->sendEvent(std::move(strMsg)); 10958ece0e45SEd Tanous eventId++; // increment the eventId 109696330b99SSunitha Harish } 109796330b99SSunitha Harish } 109896330b99SSunitha Harish 10992558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 11007f4eb588SAppaRao Puli { 11012558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 11022558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 11032558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 11042558979cSP Dheeraj Srujan Kumar } 11052558979cSP Dheeraj Srujan Kumar 11062558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 11072558979cSP Dheeraj Srujan Kumar { 11082558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 11092558979cSP Dheeraj Srujan Kumar 11107f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11117f4eb588SAppaRao Puli if (!logStream.good()) 11127f4eb588SAppaRao Puli { 111362598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed "); 11147f4eb588SAppaRao Puli return; 11157f4eb588SAppaRao Puli } 11167f4eb588SAppaRao Puli std::string logEntry; 11177f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11187f4eb588SAppaRao Puli { 11192558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11207f4eb588SAppaRao Puli } 11217f4eb588SAppaRao Puli } 11227f4eb588SAppaRao Puli 11237f4eb588SAppaRao Puli void readEventLogsFromFile() 11247f4eb588SAppaRao Puli { 11257f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11267f4eb588SAppaRao Puli if (!logStream.good()) 11277f4eb588SAppaRao Puli { 112862598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed"); 11297f4eb588SAppaRao Puli return; 11307f4eb588SAppaRao Puli } 11317f4eb588SAppaRao Puli 11327f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11337f4eb588SAppaRao Puli 11347f4eb588SAppaRao Puli std::string logEntry; 11352558979cSP Dheeraj Srujan Kumar 1136*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("Redfish log file: seek to {}", 1137*03d4d37cSAlexander Hansen static_cast<int>(redfishLogFilePosition)); 1138*03d4d37cSAlexander Hansen 11392558979cSP Dheeraj Srujan Kumar // Get the read pointer to the next log to be read. 11402558979cSP Dheeraj Srujan Kumar logStream.seekg(redfishLogFilePosition); 11412558979cSP Dheeraj Srujan Kumar 11427f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11437f4eb588SAppaRao Puli { 1144*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("Redfish log file: found new event log entry"); 11452558979cSP Dheeraj Srujan Kumar // Update Pointer position 11462558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11472558979cSP Dheeraj Srujan Kumar 11482558979cSP Dheeraj Srujan Kumar std::string idStr; 11492558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 11507f4eb588SAppaRao Puli { 1151*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG( 1152*03d4d37cSAlexander Hansen "Redfish log file: could not get unique entry id for {}", 1153*03d4d37cSAlexander Hansen logEntry); 11547f4eb588SAppaRao Puli continue; 11557f4eb588SAppaRao Puli } 11567f4eb588SAppaRao Puli 1157e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11587f4eb588SAppaRao Puli { 11592558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 11602558979cSP Dheeraj Srujan Kumar // the remaining items below. 11612558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 1162*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG( 1163*03d4d37cSAlexander Hansen "Redfish log file: no subscribers / event service not enabled"); 11647f4eb588SAppaRao Puli continue; 11657f4eb588SAppaRao Puli } 11667f4eb588SAppaRao Puli 11677f4eb588SAppaRao Puli std::string timestamp; 11687f4eb588SAppaRao Puli std::string messageID; 11695e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11707f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11717f4eb588SAppaRao Puli messageArgs) != 0) 11727f4eb588SAppaRao Puli { 1173*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("Read eventLog entry params failed for {}", 1174*03d4d37cSAlexander Hansen logEntry); 11757f4eb588SAppaRao Puli continue; 11767f4eb588SAppaRao Puli } 11777f4eb588SAppaRao Puli 1178f80a87f2SEd Tanous eventRecords.emplace_back(idStr, timestamp, messageID, messageArgs); 11797f4eb588SAppaRao Puli } 11807f4eb588SAppaRao Puli 1181e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11822558979cSP Dheeraj Srujan Kumar { 118362598e31SEd Tanous BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions."); 11842558979cSP Dheeraj Srujan Kumar return; 11852558979cSP Dheeraj Srujan Kumar } 11862558979cSP Dheeraj Srujan Kumar 11872558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 11882558979cSP Dheeraj Srujan Kumar { 11892558979cSP Dheeraj Srujan Kumar // No Records to send 119062598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 11912558979cSP Dheeraj Srujan Kumar return; 11922558979cSP Dheeraj Srujan Kumar } 11932558979cSP Dheeraj Srujan Kumar 11945e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 11957f4eb588SAppaRao Puli { 11967f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 11977f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 11987f4eb588SAppaRao Puli { 11997f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 12007f4eb588SAppaRao Puli } 12017f4eb588SAppaRao Puli } 12027f4eb588SAppaRao Puli } 12037f4eb588SAppaRao Puli 12047f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 12057f4eb588SAppaRao Puli { 12066a9f85f9SAppaRao Puli if (!inotifyConn) 12077f4eb588SAppaRao Puli { 1208*03d4d37cSAlexander Hansen BMCWEB_LOG_ERROR("inotify Connection is not present"); 12097f4eb588SAppaRao Puli return; 12107f4eb588SAppaRao Puli } 12117f4eb588SAppaRao Puli 12127f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 12137f4eb588SAppaRao Puli 1214bd79bce8SPatrick Williams inotifyConn->async_read_some( 1215bd79bce8SPatrick Williams boost::asio::buffer(readBuffer), 12167f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 12177f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 12189ed3f90aSEd Tanous if (ec == boost::asio::error::operation_aborted) 12199ed3f90aSEd Tanous { 12209ed3f90aSEd Tanous BMCWEB_LOG_DEBUG("Inotify was canceled (shutdown?)"); 12219ed3f90aSEd Tanous return; 12229ed3f90aSEd Tanous } 12237f4eb588SAppaRao Puli if (ec) 12247f4eb588SAppaRao Puli { 122562598e31SEd Tanous BMCWEB_LOG_ERROR("Callback Error: {}", ec.message()); 12267f4eb588SAppaRao Puli return; 12277f4eb588SAppaRao Puli } 1228*03d4d37cSAlexander Hansen 1229*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("reading {} via inotify", bytesTransferred); 1230*03d4d37cSAlexander Hansen 12317f4eb588SAppaRao Puli std::size_t index = 0; 1232b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 12337f4eb588SAppaRao Puli { 1234d3a9e084SEd Tanous struct inotify_event event 1235d3a9e084SEd Tanous {}; 1236b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1237b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1238b792cc56SAppaRao Puli { 1239b792cc56SAppaRao Puli if ((event.len == 0) || 1240b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1241b792cc56SAppaRao Puli { 1242b792cc56SAppaRao Puli index += (iEventSize + event.len); 1243b792cc56SAppaRao Puli continue; 1244b792cc56SAppaRao Puli } 1245b792cc56SAppaRao Puli 12464f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 12474f568f74SJiaqing Zhao if (fileName != "redfish") 1248b792cc56SAppaRao Puli { 1249b792cc56SAppaRao Puli index += (iEventSize + event.len); 1250b792cc56SAppaRao Puli continue; 1251b792cc56SAppaRao Puli } 1252b792cc56SAppaRao Puli 125362598e31SEd Tanous BMCWEB_LOG_DEBUG( 125462598e31SEd Tanous "Redfish log file created/deleted. event.name: {}", 125562598e31SEd Tanous fileName); 1256b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1257b792cc56SAppaRao Puli { 1258b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1259b792cc56SAppaRao Puli { 126062598e31SEd Tanous BMCWEB_LOG_DEBUG( 126162598e31SEd Tanous "Remove and Add inotify watcher on " 126262598e31SEd Tanous "redfish event log file"); 1263016761afSAppaRao Puli // Remove existing inotify watcher and add 1264016761afSAppaRao Puli // with new redfish event log file. 1265016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1266016761afSAppaRao Puli fileWatchDesc = -1; 1267b792cc56SAppaRao Puli } 1268b792cc56SAppaRao Puli 1269b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1270b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1271b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1272b792cc56SAppaRao Puli { 127362598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for " 127462598e31SEd Tanous "redfish log file."); 1275b792cc56SAppaRao Puli return; 1276b792cc56SAppaRao Puli } 1277b792cc56SAppaRao Puli 1278b792cc56SAppaRao Puli EventServiceManager::getInstance() 12792558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1280b792cc56SAppaRao Puli EventServiceManager::getInstance() 1281b792cc56SAppaRao Puli .readEventLogsFromFile(); 1282b792cc56SAppaRao Puli } 1283b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1284b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1285b792cc56SAppaRao Puli { 1286b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1287b792cc56SAppaRao Puli { 1288b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1289b792cc56SAppaRao Puli fileWatchDesc = -1; 1290b792cc56SAppaRao Puli } 1291b792cc56SAppaRao Puli } 1292b792cc56SAppaRao Puli } 1293b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1294b792cc56SAppaRao Puli { 1295b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12967f4eb588SAppaRao Puli { 12977f4eb588SAppaRao Puli EventServiceManager::getInstance() 12987f4eb588SAppaRao Puli .readEventLogsFromFile(); 12997f4eb588SAppaRao Puli } 1300b792cc56SAppaRao Puli } 1301b792cc56SAppaRao Puli index += (iEventSize + event.len); 13027f4eb588SAppaRao Puli } 13037f4eb588SAppaRao Puli 13047f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13057f4eb588SAppaRao Puli }); 13067f4eb588SAppaRao Puli } 13077f4eb588SAppaRao Puli 13087f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 13097f4eb588SAppaRao Puli { 1310*03d4d37cSAlexander Hansen BMCWEB_LOG_DEBUG("starting Event Log Monitor"); 1311*03d4d37cSAlexander Hansen 131223a21a1cSEd Tanous inotifyConn.emplace(ioc); 1313b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1314b792cc56SAppaRao Puli if (inotifyFd == -1) 13157f4eb588SAppaRao Puli { 131662598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_init1 failed."); 13177f4eb588SAppaRao Puli return -1; 13187f4eb588SAppaRao Puli } 1319b792cc56SAppaRao Puli 1320b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1321b792cc56SAppaRao Puli // create/delete. 1322b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1323b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1324b792cc56SAppaRao Puli if (dirWatchDesc == -1) 13257f4eb588SAppaRao Puli { 132662598e31SEd Tanous BMCWEB_LOG_ERROR( 132762598e31SEd Tanous "inotify_add_watch failed for event log directory."); 13287f4eb588SAppaRao Puli return -1; 13297f4eb588SAppaRao Puli } 13307f4eb588SAppaRao Puli 1331b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 1332bd79bce8SPatrick Williams fileWatchDesc = 1333bd79bce8SPatrick Williams inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY); 1334b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1335b792cc56SAppaRao Puli { 133662598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for redfish log file."); 1337b792cc56SAppaRao Puli // Don't return error if file not exist. 1338b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1339b792cc56SAppaRao Puli } 1340b792cc56SAppaRao Puli 13417f4eb588SAppaRao Puli // monitor redfish event log file 1342b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13437f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13447f4eb588SAppaRao Puli 13457f4eb588SAppaRao Puli return 0; 13467f4eb588SAppaRao Puli } 13477f4eb588SAppaRao Puli 13489ed3f90aSEd Tanous static void stopEventLogMonitor() 13499ed3f90aSEd Tanous { 13509ed3f90aSEd Tanous inotifyConn.reset(); 13519ed3f90aSEd Tanous } 13529ed3f90aSEd Tanous 135359d494eeSPatrick Williams static void getReadingsForReport(sdbusplus::message_t& msg) 1354156d6b00SAppaRao Puli { 135556d2396dSEd Tanous if (msg.is_method_error()) 135656d2396dSEd Tanous { 135762598e31SEd Tanous BMCWEB_LOG_ERROR("TelemetryMonitor Signal error"); 135856d2396dSEd Tanous return; 135956d2396dSEd Tanous } 136056d2396dSEd Tanous 1361c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1362c0353249SWludzik, Jozef std::string id = path.filename(); 1363c0353249SWludzik, Jozef if (id.empty()) 1364156d6b00SAppaRao Puli { 136562598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get Id from path"); 1366156d6b00SAppaRao Puli return; 1367156d6b00SAppaRao Puli } 1368156d6b00SAppaRao Puli 1369c0353249SWludzik, Jozef std::string interface; 1370b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1371c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1372c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1373c0353249SWludzik, Jozef 1374bd79bce8SPatrick Williams auto found = std::ranges::find_if(props, [](const auto& x) { 1375bd79bce8SPatrick Williams return x.first == "Readings"; 1376bd79bce8SPatrick Williams }); 1377c0353249SWludzik, Jozef if (found == props.end()) 1378156d6b00SAppaRao Puli { 137962598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 1380156d6b00SAppaRao Puli return; 1381156d6b00SAppaRao Puli } 1382156d6b00SAppaRao Puli 13831e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 13841e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1385e662eae8SEd Tanous if (readings == nullptr) 13861e1e598dSJonathan Doman { 138762598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 13881e1e598dSJonathan Doman return; 13891e1e598dSJonathan Doman } 13901e1e598dSJonathan Doman 1391156d6b00SAppaRao Puli for (const auto& it : 1392156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1393156d6b00SAppaRao Puli { 1394e05aec50SEd Tanous Subscription& entry = *it.second; 1395c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1396156d6b00SAppaRao Puli { 13971e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1398156d6b00SAppaRao Puli } 1399156d6b00SAppaRao Puli } 1400156d6b00SAppaRao Puli } 1401156d6b00SAppaRao Puli 1402156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1403156d6b00SAppaRao Puli { 14047d1cc387SAppaRao Puli if (matchTelemetryMonitor) 14057d1cc387SAppaRao Puli { 140662598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Unregister"); 1407156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1408156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1409156d6b00SAppaRao Puli } 14107d1cc387SAppaRao Puli } 1411156d6b00SAppaRao Puli 1412156d6b00SAppaRao Puli void registerMetricReportSignal() 1413156d6b00SAppaRao Puli { 14147d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1415156d6b00SAppaRao Puli { 141662598e31SEd Tanous BMCWEB_LOG_DEBUG("Not registering metric report signal."); 1417156d6b00SAppaRao Puli return; 1418156d6b00SAppaRao Puli } 1419156d6b00SAppaRao Puli 142062598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Register"); 1421c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1422c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1423c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1424156d6b00SAppaRao Puli 142559d494eeSPatrick Williams matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>( 142656d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1427156d6b00SAppaRao Puli } 142823a21a1cSEd Tanous }; 1429b52664e2SAppaRao Puli 1430b52664e2SAppaRao Puli } // namespace redfish 1431