1b52664e2SAppaRao Puli /* 2b52664e2SAppaRao Puli // Copyright (c) 2020 Intel Corporation 3b52664e2SAppaRao Puli // 4b52664e2SAppaRao Puli // Licensed under the Apache License, Version 2.0 (the "License"); 5b52664e2SAppaRao Puli // you may not use this file except in compliance with the License. 6b52664e2SAppaRao Puli // You may obtain a copy of the License at 7b52664e2SAppaRao Puli // 8b52664e2SAppaRao Puli // http://www.apache.org/licenses/LICENSE-2.0 9b52664e2SAppaRao Puli // 10b52664e2SAppaRao Puli // Unless required by applicable law or agreed to in writing, software 11b52664e2SAppaRao Puli // distributed under the License is distributed on an "AS IS" BASIS, 12b52664e2SAppaRao Puli // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b52664e2SAppaRao Puli // See the License for the specific language governing permissions and 14b52664e2SAppaRao Puli // limitations under the License. 15b52664e2SAppaRao Puli */ 16b52664e2SAppaRao Puli #pragma once 173ccb3adbSEd Tanous #include "dbus_utility.hpp" 183ccb3adbSEd Tanous #include "error_messages.hpp" 193ccb3adbSEd Tanous #include "event_service_store.hpp" 203ccb3adbSEd Tanous #include "http_client.hpp" 21c0353249SWludzik, Jozef #include "metric_report.hpp" 222c6ffdb0SEd Tanous #include "ossl_random.hpp" 233ccb3adbSEd Tanous #include "persistent_data.hpp" 247f4eb588SAppaRao Puli #include "registries.hpp" 258dab0f58SEd Tanous #include "registries_selector.hpp" 2650ebd4afSEd Tanous #include "str_utility.hpp" 2777665bdaSNan Zhou #include "utility.hpp" 283ccb3adbSEd Tanous #include "utils/json_utils.hpp" 29*5b90429aSEd Tanous #include "utils/time_utils.hpp" 307f4eb588SAppaRao Puli 317f4eb588SAppaRao Puli #include <sys/inotify.h> 32b52664e2SAppaRao Puli 33fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 34b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 35ef4c65b7SEd Tanous #include <boost/url/format.hpp> 364a7fbefdSEd Tanous #include <boost/url/url_view_base.hpp> 37b5b40605Snitroglycerine #include <sdbusplus/bus/match.hpp> 381214b7e7SGunnar Mills 395e44e3d8SAppaRao Puli #include <algorithm> 40b52664e2SAppaRao Puli #include <cstdlib> 41b52664e2SAppaRao Puli #include <ctime> 421bf712bcSAyushi Smriti #include <fstream> 43b52664e2SAppaRao Puli #include <memory> 443544d2a7SEd Tanous #include <ranges> 4526702d01SEd Tanous #include <span> 46b52664e2SAppaRao Puli 47b52664e2SAppaRao Puli namespace redfish 48b52664e2SAppaRao Puli { 49156d6b00SAppaRao Puli 50156d6b00SAppaRao Puli using ReadingsObjType = 5193f5d7c7SWludzik, Jozef std::vector<std::tuple<std::string, std::string, double, int32_t>>; 52156d6b00SAppaRao Puli 53156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 54156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 55156d6b00SAppaRao Puli 565e44e3d8SAppaRao Puli static constexpr const char* subscriptionTypeSSE = "SSE"; 571bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 581bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 591bf712bcSAyushi Smriti 605e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSubscriptions = 20; 615e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSSESubscriptions = 10; 625e44e3d8SAppaRao Puli 634642bf8fSGeorge Liu #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 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; 764642bf8fSGeorge Liu 774642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 784642bf8fSGeorge Liu using EventLogObjectsType = 794642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 804642bf8fSGeorge Liu std::vector<std::string>>; 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, 2217f4eb588SAppaRao Puli nlohmann::json& 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 #endif 2627f4eb588SAppaRao Puli 26323a21a1cSEd Tanous inline bool isFilterQuerySpecialChar(char c) 26407941a88SAyushi Smriti { 26507941a88SAyushi Smriti switch (c) 26607941a88SAyushi Smriti { 26707941a88SAyushi Smriti case '(': 26807941a88SAyushi Smriti case ')': 26907941a88SAyushi Smriti case '\'': 27007941a88SAyushi Smriti return true; 27107941a88SAyushi Smriti default: 27207941a88SAyushi Smriti return false; 27307941a88SAyushi Smriti } 27407941a88SAyushi Smriti } 27507941a88SAyushi Smriti 27623a21a1cSEd Tanous inline bool 27723a21a1cSEd Tanous readSSEQueryParams(std::string sseFilter, std::string& formatType, 27807941a88SAyushi Smriti std::vector<std::string>& messageIds, 27907941a88SAyushi Smriti std::vector<std::string>& registryPrefixes, 280144b6318SAppaRao Puli std::vector<std::string>& metricReportDefinitions) 28107941a88SAyushi Smriti { 2823544d2a7SEd Tanous auto remove = std::ranges::remove_if(sseFilter, isFilterQuerySpecialChar); 2833544d2a7SEd Tanous sseFilter.erase(std::ranges::begin(remove), sseFilter.end()); 28407941a88SAyushi Smriti 28507941a88SAyushi Smriti std::vector<std::string> result; 28650ebd4afSEd Tanous 28750ebd4afSEd Tanous // NOLINTNEXTLINE 28850ebd4afSEd Tanous bmcweb::split(result, sseFilter, ' '); 28907941a88SAyushi Smriti 29062598e31SEd Tanous BMCWEB_LOG_DEBUG("No of tokens in SEE query: {}", result.size()); 29107941a88SAyushi Smriti 29207941a88SAyushi Smriti constexpr uint8_t divisor = 4; 29307941a88SAyushi Smriti constexpr uint8_t minTokenSize = 3; 29407941a88SAyushi Smriti if (result.size() % divisor != minTokenSize) 29507941a88SAyushi Smriti { 29662598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid SSE filter specified."); 29707941a88SAyushi Smriti return false; 29807941a88SAyushi Smriti } 29907941a88SAyushi Smriti 30007941a88SAyushi Smriti for (std::size_t i = 0; i < result.size(); i += divisor) 30107941a88SAyushi Smriti { 30202cad96eSEd Tanous const std::string& key = result[i]; 30302cad96eSEd Tanous const std::string& op = result[i + 1]; 30402cad96eSEd Tanous const std::string& value = result[i + 2]; 30507941a88SAyushi Smriti 30607941a88SAyushi Smriti if ((i + minTokenSize) < result.size()) 30707941a88SAyushi Smriti { 30802cad96eSEd Tanous const std::string& separator = result[i + minTokenSize]; 30907941a88SAyushi Smriti // SSE supports only "or" and "and" in query params. 3104e0453b1SGunnar Mills if ((separator != "or") && (separator != "and")) 31107941a88SAyushi Smriti { 31262598e31SEd Tanous BMCWEB_LOG_ERROR( 31362598e31SEd Tanous "Invalid group operator in SSE query parameters"); 31407941a88SAyushi Smriti return false; 31507941a88SAyushi Smriti } 31607941a88SAyushi Smriti } 31707941a88SAyushi Smriti 31807941a88SAyushi Smriti // SSE supports only "eq" as per spec. 31907941a88SAyushi Smriti if (op != "eq") 32007941a88SAyushi Smriti { 32162598e31SEd Tanous BMCWEB_LOG_ERROR( 32262598e31SEd Tanous "Invalid assignment operator in SSE query parameters"); 32307941a88SAyushi Smriti return false; 32407941a88SAyushi Smriti } 32507941a88SAyushi Smriti 32662598e31SEd Tanous BMCWEB_LOG_DEBUG("{} : {}", key, value); 32707941a88SAyushi Smriti if (key == "EventFormatType") 32807941a88SAyushi Smriti { 32907941a88SAyushi Smriti formatType = value; 33007941a88SAyushi Smriti } 33107941a88SAyushi Smriti else if (key == "MessageId") 33207941a88SAyushi Smriti { 33307941a88SAyushi Smriti messageIds.push_back(value); 33407941a88SAyushi Smriti } 33507941a88SAyushi Smriti else if (key == "RegistryPrefix") 33607941a88SAyushi Smriti { 33707941a88SAyushi Smriti registryPrefixes.push_back(value); 33807941a88SAyushi Smriti } 33907941a88SAyushi Smriti else if (key == "MetricReportDefinition") 34007941a88SAyushi Smriti { 34107941a88SAyushi Smriti metricReportDefinitions.push_back(value); 34207941a88SAyushi Smriti } 34307941a88SAyushi Smriti else 34407941a88SAyushi Smriti { 34562598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid property({})in SSE filter query.", key); 34607941a88SAyushi Smriti return false; 34707941a88SAyushi Smriti } 34807941a88SAyushi Smriti } 34907941a88SAyushi Smriti return true; 35007941a88SAyushi Smriti } 35107941a88SAyushi Smriti 35228afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 353b52664e2SAppaRao Puli { 354b52664e2SAppaRao Puli public: 355b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 356b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 357b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 358b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 359b52664e2SAppaRao Puli 3604a7fbefdSEd Tanous Subscription(const boost::urls::url_view_base& url, 3614a7fbefdSEd Tanous boost::asio::io_context& ioc) : 362a716aa74SEd Tanous policy(std::make_shared<crow::ConnectionPolicy>()) 363b52664e2SAppaRao Puli { 364a716aa74SEd Tanous destinationUrl = url; 3655e44e3d8SAppaRao Puli client.emplace(ioc, policy); 3667adb85acSSunitha Harish // Subscription constructor 367d14a48ffSCarson Labrado policy->invalidResp = retryRespHandler; 368b52664e2SAppaRao Puli } 3694bbf237fSAppaRao Puli 3705e44e3d8SAppaRao Puli explicit Subscription(crow::sse_socket::Connection& connIn) : 3715e44e3d8SAppaRao Puli sseConn(&connIn) 3725e44e3d8SAppaRao Puli {} 3735e44e3d8SAppaRao Puli 3749f616dd1SEd Tanous ~Subscription() = default; 375b52664e2SAppaRao Puli 3765e44e3d8SAppaRao Puli bool sendEvent(std::string&& msg) 377b52664e2SAppaRao Puli { 3786ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 3796ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 3806ba8c82eSsunharis_in .getEventServiceConfig(); 3816ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 3826ba8c82eSsunharis_in { 3836ba8c82eSsunharis_in return false; 3846ba8c82eSsunharis_in } 3856ba8c82eSsunharis_in 386f52c03c1SCarson Labrado // A connection pool will be created if one does not already exist 3875e44e3d8SAppaRao Puli if (client) 3885e44e3d8SAppaRao Puli { 389a716aa74SEd Tanous client->sendData(std::move(msg), destinationUrl, httpHeaders, 390a716aa74SEd Tanous boost::beast::http::verb::post); 3915e44e3d8SAppaRao Puli return true; 3925e44e3d8SAppaRao Puli } 3937adb85acSSunitha Harish 3944bbf237fSAppaRao Puli if (sseConn != nullptr) 3954bbf237fSAppaRao Puli { 3965e44e3d8SAppaRao Puli eventSeqNum++; 3975e44e3d8SAppaRao Puli sseConn->sendEvent(std::to_string(eventSeqNum), msg); 3984bbf237fSAppaRao Puli } 3996ba8c82eSsunharis_in return true; 4004bbf237fSAppaRao Puli } 4014bbf237fSAppaRao Puli 4026ba8c82eSsunharis_in bool sendTestEventLog() 4030b4bdd93SAppaRao Puli { 4040b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 4050b4bdd93SAppaRao Puli logEntryArray.push_back({}); 4060b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 4070b4bdd93SAppaRao Puli 408613dabeaSEd Tanous logEntryJson["EventId"] = "TestID"; 409613dabeaSEd Tanous logEntryJson["EventType"] = "Event"; 410613dabeaSEd Tanous logEntryJson["Severity"] = "OK"; 411613dabeaSEd Tanous logEntryJson["Message"] = "Generated test event"; 412613dabeaSEd Tanous logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog"; 413613dabeaSEd Tanous logEntryJson["MessageArgs"] = nlohmann::json::array(); 414613dabeaSEd Tanous logEntryJson["EventTimestamp"] = 415613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 416613dabeaSEd Tanous logEntryJson["Context"] = customText; 4170b4bdd93SAppaRao Puli 4181476687dSEd Tanous nlohmann::json msg; 4191476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4201476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4211476687dSEd Tanous msg["Name"] = "Event Log"; 4221476687dSEd Tanous msg["Events"] = logEntryArray; 4230b4bdd93SAppaRao Puli 42489492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 42589492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 4265e44e3d8SAppaRao Puli return sendEvent(std::move(strMsg)); 4270b4bdd93SAppaRao Puli } 4280b4bdd93SAppaRao Puli 4297f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 4307f4eb588SAppaRao Puli void filterAndSendEventLogs( 4317f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4327f4eb588SAppaRao Puli { 4337f4eb588SAppaRao Puli nlohmann::json logEntryArray; 4347f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4357f4eb588SAppaRao Puli { 4367f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 4377f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 4387f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 4397f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 4407f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 4415e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 4427f4eb588SAppaRao Puli 4437f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 4447f4eb588SAppaRao Puli // send everything. 44526f6976fSEd Tanous if (!registryPrefixes.empty()) 4467f4eb588SAppaRao Puli { 4473544d2a7SEd Tanous auto obj = std::ranges::find(registryPrefixes, registryName); 4487f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 4497f4eb588SAppaRao Puli { 4507f4eb588SAppaRao Puli continue; 4517f4eb588SAppaRao Puli } 4527f4eb588SAppaRao Puli } 4537f4eb588SAppaRao Puli 4547f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 4557f4eb588SAppaRao Puli // send everything. 45626f6976fSEd Tanous if (!registryMsgIds.empty()) 4577f4eb588SAppaRao Puli { 4583544d2a7SEd Tanous auto obj = std::ranges::find(registryMsgIds, messageKey); 4597f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 4607f4eb588SAppaRao Puli { 4617f4eb588SAppaRao Puli continue; 4627f4eb588SAppaRao Puli } 4637f4eb588SAppaRao Puli } 4647f4eb588SAppaRao Puli 465c5ba4c27SEd Tanous std::vector<std::string_view> messageArgsView(messageArgs.begin(), 466c5ba4c27SEd Tanous messageArgs.end()); 467c5ba4c27SEd Tanous 4687f4eb588SAppaRao Puli logEntryArray.push_back({}); 4697f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 470c5ba4c27SEd Tanous if (event_log::formatEventLogEntry(idStr, messageID, 471c5ba4c27SEd Tanous messageArgsView, timestamp, 472c5ba4c27SEd Tanous customText, bmcLogEntry) != 0) 4737f4eb588SAppaRao Puli { 47462598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry failed"); 4757f4eb588SAppaRao Puli continue; 4767f4eb588SAppaRao Puli } 4777f4eb588SAppaRao Puli } 4787f4eb588SAppaRao Puli 47926f6976fSEd Tanous if (logEntryArray.empty()) 4807f4eb588SAppaRao Puli { 48162598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 4827f4eb588SAppaRao Puli return; 4837f4eb588SAppaRao Puli } 4847f4eb588SAppaRao Puli 4851476687dSEd Tanous nlohmann::json msg; 4861476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4871476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4881476687dSEd Tanous msg["Name"] = "Event Log"; 4891476687dSEd Tanous msg["Events"] = logEntryArray; 49089492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 49189492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 4925e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 4935e44e3d8SAppaRao Puli eventSeqNum++; 4947f4eb588SAppaRao Puli } 4957f4eb588SAppaRao Puli #endif 4967f4eb588SAppaRao Puli 497248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 4981e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 499156d6b00SAppaRao Puli { 500ef4c65b7SEd Tanous boost::urls::url mrdUri = boost::urls::format( 501ef4c65b7SEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", 502ef4c65b7SEd Tanous reportId); 503156d6b00SAppaRao Puli 504156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 50526f6976fSEd Tanous if (!metricReportDefinitions.empty()) 506156d6b00SAppaRao Puli { 5073544d2a7SEd Tanous if (std::ranges::find(metricReportDefinitions, mrdUri.buffer()) == 5083544d2a7SEd Tanous metricReportDefinitions.end()) 509156d6b00SAppaRao Puli { 510156d6b00SAppaRao Puli return; 511156d6b00SAppaRao Puli } 512156d6b00SAppaRao Puli } 513156d6b00SAppaRao Puli 514c0353249SWludzik, Jozef nlohmann::json msg; 515248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 516156d6b00SAppaRao Puli { 51762598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to fill the MetricReport for DBus " 51862598e31SEd Tanous "Report with id {}", 51962598e31SEd Tanous reportId); 520c0353249SWludzik, Jozef return; 521156d6b00SAppaRao Puli } 522156d6b00SAppaRao Puli 52322daffd7SAppaRao Puli // Context is set by user during Event subscription and it must be 52422daffd7SAppaRao Puli // set for MetricReport response. 52522daffd7SAppaRao Puli if (!customText.empty()) 52622daffd7SAppaRao Puli { 52722daffd7SAppaRao Puli msg["Context"] = customText; 52822daffd7SAppaRao Puli } 52922daffd7SAppaRao Puli 53089492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 53189492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 5325e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 533156d6b00SAppaRao Puli } 534156d6b00SAppaRao Puli 535d14a48ffSCarson Labrado void updateRetryConfig(uint32_t retryAttempts, 536d14a48ffSCarson Labrado uint32_t retryTimeoutInterval) 537fe44eb0bSAyushi Smriti { 53893cf0ac2SEd Tanous if (policy == nullptr) 53993cf0ac2SEd Tanous { 54093cf0ac2SEd Tanous BMCWEB_LOG_DEBUG("Retry policy was nullptr, ignoring set"); 54193cf0ac2SEd Tanous return; 54293cf0ac2SEd Tanous } 543d14a48ffSCarson Labrado policy->maxRetryAttempts = retryAttempts; 544d14a48ffSCarson Labrado policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval); 54562de0c68SAppaRao Puli } 546fe44eb0bSAyushi Smriti 5479eb808c1SEd Tanous uint64_t getEventSeqNum() const 54896330b99SSunitha Harish { 54996330b99SSunitha Harish return eventSeqNum; 55096330b99SSunitha Harish } 55196330b99SSunitha Harish 5525e44e3d8SAppaRao Puli void setSubscriptionId(const std::string& id2) 5535e44e3d8SAppaRao Puli { 55462598e31SEd Tanous BMCWEB_LOG_DEBUG("Subscription ID: {}", id2); 5555e44e3d8SAppaRao Puli subId = id2; 5565e44e3d8SAppaRao Puli } 5575e44e3d8SAppaRao Puli 5585e44e3d8SAppaRao Puli std::string getSubscriptionId() 5595e44e3d8SAppaRao Puli { 5605e44e3d8SAppaRao Puli return subId; 5615e44e3d8SAppaRao Puli } 5625e44e3d8SAppaRao Puli 5635e44e3d8SAppaRao Puli bool matchSseId(const crow::sse_socket::Connection& thisConn) 5645e44e3d8SAppaRao Puli { 5655e44e3d8SAppaRao Puli return &thisConn == sseConn; 5665e44e3d8SAppaRao Puli } 5675e44e3d8SAppaRao Puli 568b52664e2SAppaRao Puli private: 5695e44e3d8SAppaRao Puli std::string subId; 570d14a48ffSCarson Labrado uint64_t eventSeqNum = 1; 571a716aa74SEd Tanous boost::urls::url host; 572d14a48ffSCarson Labrado std::shared_ptr<crow::ConnectionPolicy> policy; 5735e44e3d8SAppaRao Puli crow::sse_socket::Connection* sseConn = nullptr; 5745e44e3d8SAppaRao Puli std::optional<crow::HttpClient> client; 575b52664e2SAppaRao Puli std::string path; 576b52664e2SAppaRao Puli std::string uriProto; 577a7a80296SCarson Labrado 578a7a80296SCarson Labrado // Check used to indicate what response codes are valid as part of our retry 579a7a80296SCarson Labrado // policy. 2XX is considered acceptable 580a7a80296SCarson Labrado static boost::system::error_code retryRespHandler(unsigned int respCode) 581a7a80296SCarson Labrado { 58262598e31SEd Tanous BMCWEB_LOG_DEBUG( 58362598e31SEd Tanous "Checking response code validity for SubscriptionEvent"); 584a7a80296SCarson Labrado if ((respCode < 200) || (respCode >= 300)) 585a7a80296SCarson Labrado { 586a7a80296SCarson Labrado return boost::system::errc::make_error_code( 587a7a80296SCarson Labrado boost::system::errc::result_out_of_range); 588a7a80296SCarson Labrado } 589a7a80296SCarson Labrado 590a7a80296SCarson Labrado // Return 0 if the response code is valid 591a7a80296SCarson Labrado return boost::system::errc::make_error_code( 592a7a80296SCarson Labrado boost::system::errc::success); 5939fa6d147SNan Zhou } 594b52664e2SAppaRao Puli }; 595b52664e2SAppaRao Puli 596b52664e2SAppaRao Puli class EventServiceManager 597b52664e2SAppaRao Puli { 598b52664e2SAppaRao Puli private: 599d3a9e084SEd Tanous bool serviceEnabled = false; 600d3a9e084SEd Tanous uint32_t retryAttempts = 0; 601d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 6027d1cc387SAppaRao Puli 6032558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 6049f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 6059f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 60659d494eeSPatrick Williams std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor; 607b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 608b52664e2SAppaRao Puli subscriptionsMap; 609b52664e2SAppaRao Puli 6109f616dd1SEd Tanous uint64_t eventId{1}; 61196330b99SSunitha Harish 612f8ca6d79SEd Tanous boost::asio::io_context& ioc; 613f8ca6d79SEd Tanous 614b52664e2SAppaRao Puli public: 6159f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 6169f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 6179f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 6189f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 619ecd6a3a2SEd Tanous ~EventServiceManager() = default; 6209f616dd1SEd Tanous 621f8ca6d79SEd Tanous explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn) 622b52664e2SAppaRao Puli { 623f8ca6d79SEd Tanous // Load config from persist store. 624f8ca6d79SEd Tanous initConfig(); 625f8ca6d79SEd Tanous } 626f8ca6d79SEd Tanous 627f8ca6d79SEd Tanous static EventServiceManager& 628f8ca6d79SEd Tanous getInstance(boost::asio::io_context* ioc = nullptr) 629f8ca6d79SEd Tanous { 630f8ca6d79SEd Tanous static EventServiceManager handler(*ioc); 631b52664e2SAppaRao Puli return handler; 632b52664e2SAppaRao Puli } 633b52664e2SAppaRao Puli 6341bf712bcSAyushi Smriti void initConfig() 6351bf712bcSAyushi Smriti { 63628afb49cSJunLin Chen loadOldBehavior(); 6371bf712bcSAyushi Smriti 63828afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 63928afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 64028afb49cSJunLin Chen .getEventServiceConfig(); 6411bf712bcSAyushi Smriti 64228afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 64328afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 64428afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6451bf712bcSAyushi Smriti 64628afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 64728afb49cSJunLin Chen .subscriptionsConfigMap) 6481bf712bcSAyushi Smriti { 64928afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 65028afb49cSJunLin Chen it.second; 6514bbf237fSAppaRao Puli 6526fd29553SEd Tanous boost::system::result<boost::urls::url> url = 653a716aa74SEd Tanous boost::urls::parse_absolute_uri(newSub->destinationUrl); 6541bf712bcSAyushi Smriti 655a716aa74SEd Tanous if (!url) 6561bf712bcSAyushi Smriti { 65762598e31SEd Tanous BMCWEB_LOG_ERROR( 65862598e31SEd Tanous "Failed to validate and split destination url"); 6591bf712bcSAyushi Smriti continue; 6601bf712bcSAyushi Smriti } 6611bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 662a716aa74SEd Tanous std::make_shared<Subscription>(*url, ioc); 6631bf712bcSAyushi Smriti 66428afb49cSJunLin Chen subValue->id = newSub->id; 66528afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 66628afb49cSJunLin Chen subValue->protocol = newSub->protocol; 66728afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 66828afb49cSJunLin Chen subValue->customText = newSub->customText; 66928afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 67028afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 67128afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 67228afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 67328afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 67428afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 67528afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 6761bf712bcSAyushi Smriti 67728afb49cSJunLin Chen if (subValue->id.empty()) 6781bf712bcSAyushi Smriti { 67962598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to add subscription"); 6801bf712bcSAyushi Smriti } 68128afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 68228afb49cSJunLin Chen 68328afb49cSJunLin Chen updateNoOfSubscribersCount(); 68428afb49cSJunLin Chen 68528afb49cSJunLin Chen #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 6862558979cSP Dheeraj Srujan Kumar 6872558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 6882558979cSP Dheeraj Srujan Kumar 68928afb49cSJunLin Chen #endif 69028afb49cSJunLin Chen // Update retry configuration. 69128afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 6921bf712bcSAyushi Smriti } 6931bf712bcSAyushi Smriti } 6941bf712bcSAyushi Smriti 69556d2396dSEd Tanous static void loadOldBehavior() 696b52664e2SAppaRao Puli { 69728afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 69828afb49cSJunLin Chen if (!eventConfigFile.good()) 6991bf712bcSAyushi Smriti { 70062598e31SEd Tanous BMCWEB_LOG_DEBUG("Old eventService config not exist"); 70128afb49cSJunLin Chen return; 70228afb49cSJunLin Chen } 70328afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 70428afb49cSJunLin Chen if (jsonData.is_discarded()) 7054bbf237fSAppaRao Puli { 70662598e31SEd Tanous BMCWEB_LOG_ERROR("Old eventService config parse error."); 70728afb49cSJunLin Chen return; 70828afb49cSJunLin Chen } 70928afb49cSJunLin Chen 71028afb49cSJunLin Chen for (const auto& item : jsonData.items()) 71128afb49cSJunLin Chen { 71228afb49cSJunLin Chen if (item.key() == "Configuration") 71328afb49cSJunLin Chen { 71428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 71528afb49cSJunLin Chen .getEventServiceConfig() 71628afb49cSJunLin Chen .fromJson(item.value()); 71728afb49cSJunLin Chen } 71828afb49cSJunLin Chen else if (item.key() == "Subscriptions") 71928afb49cSJunLin Chen { 72028afb49cSJunLin Chen for (const auto& elem : item.value()) 72128afb49cSJunLin Chen { 72228afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 72328afb49cSJunLin Chen newSubscription = 72428afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 72528afb49cSJunLin Chen true); 72628afb49cSJunLin Chen if (newSubscription == nullptr) 72728afb49cSJunLin Chen { 72862598e31SEd Tanous BMCWEB_LOG_ERROR("Problem reading subscription " 72962598e31SEd Tanous "from old persistent store"); 7304bbf237fSAppaRao Puli continue; 7314bbf237fSAppaRao Puli } 7321bf712bcSAyushi Smriti 73328afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 73428afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 7351bf712bcSAyushi Smriti 73628afb49cSJunLin Chen std::string id; 7371bf712bcSAyushi Smriti 73828afb49cSJunLin Chen int retry = 3; 739e662eae8SEd Tanous while (retry != 0) 7401bf712bcSAyushi Smriti { 74128afb49cSJunLin Chen id = std::to_string(dist(gen)); 74228afb49cSJunLin Chen if (gen.error()) 7437d1cc387SAppaRao Puli { 74428afb49cSJunLin Chen retry = 0; 74528afb49cSJunLin Chen break; 74628afb49cSJunLin Chen } 74728afb49cSJunLin Chen newSubscription->id = id; 74828afb49cSJunLin Chen auto inserted = 74928afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 75028afb49cSJunLin Chen .subscriptionsConfigMap.insert( 75128afb49cSJunLin Chen std::pair(id, newSubscription)); 75228afb49cSJunLin Chen if (inserted.second) 75328afb49cSJunLin Chen { 75428afb49cSJunLin Chen break; 75528afb49cSJunLin Chen } 75628afb49cSJunLin Chen --retry; 7577d1cc387SAppaRao Puli } 7587d1cc387SAppaRao Puli 75928afb49cSJunLin Chen if (retry <= 0) 76028afb49cSJunLin Chen { 76162598e31SEd Tanous BMCWEB_LOG_ERROR( 76262598e31SEd Tanous "Failed to generate random number from old " 76362598e31SEd Tanous "persistent store"); 76428afb49cSJunLin Chen continue; 76528afb49cSJunLin Chen } 76628afb49cSJunLin Chen } 76728afb49cSJunLin Chen } 76828afb49cSJunLin Chen 76928afb49cSJunLin Chen persistent_data::getConfig().writeData(); 7704c521c3cSEd Tanous std::error_code ec; 7714c521c3cSEd Tanous std::filesystem::remove(eventServiceFile, ec); 7724c521c3cSEd Tanous if (ec) 7734c521c3cSEd Tanous { 7744c521c3cSEd Tanous BMCWEB_LOG_DEBUG( 7754c521c3cSEd Tanous "Failed to remove old event service file. Ignoring"); 7764c521c3cSEd Tanous } 7774c521c3cSEd Tanous else 7784c521c3cSEd Tanous { 77962598e31SEd Tanous BMCWEB_LOG_DEBUG("Remove old eventservice config"); 78028afb49cSJunLin Chen } 78128afb49cSJunLin Chen } 7824c521c3cSEd Tanous } 78328afb49cSJunLin Chen 7849eb808c1SEd Tanous void updateSubscriptionData() const 78528afb49cSJunLin Chen { 78628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 78728afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 78828afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 78928afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 79028afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 79128afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 79228afb49cSJunLin Chen 79328afb49cSJunLin Chen persistent_data::getConfig().writeData(); 79428afb49cSJunLin Chen } 79528afb49cSJunLin Chen 79628afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7977d1cc387SAppaRao Puli { 7987d1cc387SAppaRao Puli bool updateConfig = false; 799fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 8007d1cc387SAppaRao Puli 80128afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 8027d1cc387SAppaRao Puli { 80328afb49cSJunLin Chen serviceEnabled = cfg.enabled; 804e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 8057d1cc387SAppaRao Puli { 8067d1cc387SAppaRao Puli registerMetricReportSignal(); 8077d1cc387SAppaRao Puli } 8087d1cc387SAppaRao Puli else 8097d1cc387SAppaRao Puli { 8107d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8117d1cc387SAppaRao Puli } 8127d1cc387SAppaRao Puli updateConfig = true; 8137d1cc387SAppaRao Puli } 8147d1cc387SAppaRao Puli 81528afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 8167d1cc387SAppaRao Puli { 81728afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 8187d1cc387SAppaRao Puli updateConfig = true; 819fe44eb0bSAyushi Smriti updateRetryCfg = true; 8207d1cc387SAppaRao Puli } 8217d1cc387SAppaRao Puli 82228afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 8237d1cc387SAppaRao Puli { 82428afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 8257d1cc387SAppaRao Puli updateConfig = true; 826fe44eb0bSAyushi Smriti updateRetryCfg = true; 8277d1cc387SAppaRao Puli } 8287d1cc387SAppaRao Puli 8297d1cc387SAppaRao Puli if (updateConfig) 8307d1cc387SAppaRao Puli { 8317d1cc387SAppaRao Puli updateSubscriptionData(); 8327d1cc387SAppaRao Puli } 833fe44eb0bSAyushi Smriti 834fe44eb0bSAyushi Smriti if (updateRetryCfg) 835fe44eb0bSAyushi Smriti { 836fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 837fe44eb0bSAyushi Smriti for (const auto& it : 838fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 839fe44eb0bSAyushi Smriti { 8405e44e3d8SAppaRao Puli Subscription& entry = *it.second; 8415e44e3d8SAppaRao Puli entry.updateRetryConfig(retryAttempts, retryTimeoutInterval); 842fe44eb0bSAyushi Smriti } 843fe44eb0bSAyushi Smriti } 8447d1cc387SAppaRao Puli } 8457d1cc387SAppaRao Puli 8467d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8477d1cc387SAppaRao Puli { 8487d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8497d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8507d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8517d1cc387SAppaRao Puli { 8527d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8537d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8547d1cc387SAppaRao Puli { 8557d1cc387SAppaRao Puli eventLogSubCount++; 8567d1cc387SAppaRao Puli } 8577d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8587d1cc387SAppaRao Puli { 8597d1cc387SAppaRao Puli metricReportSubCount++; 8607d1cc387SAppaRao Puli } 8617d1cc387SAppaRao Puli } 8627d1cc387SAppaRao Puli 8637d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8647d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8657d1cc387SAppaRao Puli { 8667d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 867e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 8687d1cc387SAppaRao Puli { 8697d1cc387SAppaRao Puli registerMetricReportSignal(); 8707d1cc387SAppaRao Puli } 8717d1cc387SAppaRao Puli else 8727d1cc387SAppaRao Puli { 8737d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8747d1cc387SAppaRao Puli } 8757d1cc387SAppaRao Puli } 8767d1cc387SAppaRao Puli } 8777d1cc387SAppaRao Puli 878b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 879b52664e2SAppaRao Puli { 880b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 881b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 882b52664e2SAppaRao Puli { 88362598e31SEd Tanous BMCWEB_LOG_ERROR("No subscription exist with ID:{}", id); 884b52664e2SAppaRao Puli return nullptr; 885b52664e2SAppaRao Puli } 886b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 887b52664e2SAppaRao Puli return subValue; 888b52664e2SAppaRao Puli } 889b52664e2SAppaRao Puli 890b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 8911bf712bcSAyushi Smriti const bool updateFile = true) 892b52664e2SAppaRao Puli { 893fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 894fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 895fc76b8acSEd Tanous 896b52664e2SAppaRao Puli std::string id; 897b52664e2SAppaRao Puli 898b52664e2SAppaRao Puli int retry = 3; 899e662eae8SEd Tanous while (retry != 0) 900b52664e2SAppaRao Puli { 901fc76b8acSEd Tanous id = std::to_string(dist(gen)); 902fc76b8acSEd Tanous if (gen.error()) 903fc76b8acSEd Tanous { 904fc76b8acSEd Tanous retry = 0; 905fc76b8acSEd Tanous break; 906fc76b8acSEd Tanous } 907b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 908b52664e2SAppaRao Puli if (inserted.second) 909b52664e2SAppaRao Puli { 910b52664e2SAppaRao Puli break; 911b52664e2SAppaRao Puli } 912b52664e2SAppaRao Puli --retry; 91323a21a1cSEd Tanous } 914b52664e2SAppaRao Puli 915b52664e2SAppaRao Puli if (retry <= 0) 916b52664e2SAppaRao Puli { 91762598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to generate random number"); 918abb93cddSEd Tanous return ""; 919b52664e2SAppaRao Puli } 920b52664e2SAppaRao Puli 92128afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 92228afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 92328afb49cSJunLin Chen newSub->id = id; 92428afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 92528afb49cSJunLin Chen newSub->protocol = subValue->protocol; 92628afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 92728afb49cSJunLin Chen newSub->customText = subValue->customText; 92828afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 92928afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 93028afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 93128afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 93228afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 93328afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 93428afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 93528afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 93628afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 93728afb49cSJunLin Chen 9387d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 9391bf712bcSAyushi Smriti 9401bf712bcSAyushi Smriti if (updateFile) 9411bf712bcSAyushi Smriti { 942b52664e2SAppaRao Puli updateSubscriptionData(); 9431bf712bcSAyushi Smriti } 9447f4eb588SAppaRao Puli 9457f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 9462558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 9477f4eb588SAppaRao Puli { 9482558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 9497f4eb588SAppaRao Puli } 9507f4eb588SAppaRao Puli #endif 951fe44eb0bSAyushi Smriti // Update retry configuration. 952fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 953fe44eb0bSAyushi Smriti 9545e44e3d8SAppaRao Puli // Set Subscription ID for back trace 9555e44e3d8SAppaRao Puli subValue->setSubscriptionId(id); 956b52664e2SAppaRao Puli return id; 957b52664e2SAppaRao Puli } 958b52664e2SAppaRao Puli 959b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 960b52664e2SAppaRao Puli { 961b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 96255f79e6fSEd Tanous return obj != subscriptionsMap.end(); 963b52664e2SAppaRao Puli } 964b52664e2SAppaRao Puli 965b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 966b52664e2SAppaRao Puli { 967b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 968b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 969b52664e2SAppaRao Puli { 970b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 97128afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 97228afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 97328afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 97428afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9757d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 976b52664e2SAppaRao Puli updateSubscriptionData(); 977b52664e2SAppaRao Puli } 978b52664e2SAppaRao Puli } 979b52664e2SAppaRao Puli 9805e44e3d8SAppaRao Puli void deleteSseSubscription(const crow::sse_socket::Connection& thisConn) 9815e44e3d8SAppaRao Puli { 9825e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 9835e44e3d8SAppaRao Puli { 9845e44e3d8SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 9855e44e3d8SAppaRao Puli bool entryIsThisConn = entry->matchSseId(thisConn); 9865e44e3d8SAppaRao Puli if (entryIsThisConn) 9875e44e3d8SAppaRao Puli { 9885e44e3d8SAppaRao Puli persistent_data::EventServiceStore::getInstance() 9895e44e3d8SAppaRao Puli .subscriptionsConfigMap.erase( 9905e44e3d8SAppaRao Puli it.second->getSubscriptionId()); 9915e44e3d8SAppaRao Puli return; 9925e44e3d8SAppaRao Puli } 9935e44e3d8SAppaRao Puli } 9945e44e3d8SAppaRao Puli } 9955e44e3d8SAppaRao Puli 9965e44e3d8SAppaRao Puli size_t getNumberOfSubscriptions() const 997b52664e2SAppaRao Puli { 998b52664e2SAppaRao Puli return subscriptionsMap.size(); 999b52664e2SAppaRao Puli } 1000b52664e2SAppaRao Puli 10015e44e3d8SAppaRao Puli size_t getNumberOfSSESubscriptions() const 10025e44e3d8SAppaRao Puli { 10033544d2a7SEd Tanous auto size = std::ranges::count_if( 10043544d2a7SEd Tanous subscriptionsMap, 10055e44e3d8SAppaRao Puli [](const std::pair<std::string, std::shared_ptr<Subscription>>& 10065e44e3d8SAppaRao Puli entry) { 10075e44e3d8SAppaRao Puli return (entry.second->subscriptionType == subscriptionTypeSSE); 10085e44e3d8SAppaRao Puli }); 10095e44e3d8SAppaRao Puli return static_cast<size_t>(size); 10105e44e3d8SAppaRao Puli } 10115e44e3d8SAppaRao Puli 1012b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 1013b52664e2SAppaRao Puli { 1014b52664e2SAppaRao Puli std::vector<std::string> idList; 1015b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 1016b52664e2SAppaRao Puli { 1017b52664e2SAppaRao Puli idList.emplace_back(it.first); 1018b52664e2SAppaRao Puli } 1019b52664e2SAppaRao Puli return idList; 1020b52664e2SAppaRao Puli } 1021b52664e2SAppaRao Puli 10226ba8c82eSsunharis_in bool sendTestEventLog() 10230b4bdd93SAppaRao Puli { 10245e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 10250b4bdd93SAppaRao Puli { 10260b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 10276ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 10286ba8c82eSsunharis_in { 10296ba8c82eSsunharis_in return false; 10300b4bdd93SAppaRao Puli } 10310b4bdd93SAppaRao Puli } 10326ba8c82eSsunharis_in return true; 10336ba8c82eSsunharis_in } 1034e9a14131SAppaRao Puli 1035613dabeaSEd Tanous void sendEvent(nlohmann::json eventMessage, const std::string& origin, 1036613dabeaSEd Tanous const std::string& resType) 103796330b99SSunitha Harish { 1038dbb59d4dSEd Tanous if (!serviceEnabled || (noOfEventLogSubscribers == 0U)) 10396ba8c82eSsunharis_in { 104062598e31SEd Tanous BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions."); 10416ba8c82eSsunharis_in return; 10426ba8c82eSsunharis_in } 104396330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 104496330b99SSunitha Harish 1045613dabeaSEd Tanous eventMessage["EventId"] = eventId; 1046613dabeaSEd Tanous // MemberId is 0 : since we are sending one event record. 1047613dabeaSEd Tanous eventMessage["MemberId"] = 0; 1048613dabeaSEd Tanous eventMessage["EventTimestamp"] = 1049613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1050613dabeaSEd Tanous eventMessage["OriginOfCondition"] = origin; 1051613dabeaSEd Tanous 1052613dabeaSEd Tanous eventRecord.emplace_back(std::move(eventMessage)); 105396330b99SSunitha Harish 10545e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 105596330b99SSunitha Harish { 105696330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 105796330b99SSunitha Harish bool isSubscribed = false; 105896330b99SSunitha Harish // Search the resourceTypes list for the subscription. 105996330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 106096330b99SSunitha Harish // send everything. 106126f6976fSEd Tanous if (!entry->resourceTypes.empty()) 106296330b99SSunitha Harish { 10633174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 106496330b99SSunitha Harish { 106596330b99SSunitha Harish if (resType == resource) 106696330b99SSunitha Harish { 106762598e31SEd Tanous BMCWEB_LOG_INFO( 106862598e31SEd Tanous "ResourceType {} found in the subscribed list", 106962598e31SEd Tanous resource); 107096330b99SSunitha Harish isSubscribed = true; 107196330b99SSunitha Harish break; 107296330b99SSunitha Harish } 107396330b99SSunitha Harish } 107496330b99SSunitha Harish } 107596330b99SSunitha Harish else // resourceTypes list is empty. 107696330b99SSunitha Harish { 107796330b99SSunitha Harish isSubscribed = true; 107896330b99SSunitha Harish } 107996330b99SSunitha Harish if (isSubscribed) 108096330b99SSunitha Harish { 1081613dabeaSEd Tanous nlohmann::json msgJson; 1082613dabeaSEd Tanous 1083613dabeaSEd Tanous msgJson["@odata.type"] = "#Event.v1_4_0.Event"; 1084613dabeaSEd Tanous msgJson["Name"] = "Event Log"; 1085613dabeaSEd Tanous msgJson["Id"] = eventId; 1086613dabeaSEd Tanous msgJson["Events"] = eventRecord; 1087f52c03c1SCarson Labrado 1088f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1089f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 10905e44e3d8SAppaRao Puli entry->sendEvent(std::move(strMsg)); 10918ece0e45SEd Tanous eventId++; // increment the eventId 109296330b99SSunitha Harish } 109396330b99SSunitha Harish else 109496330b99SSunitha Harish { 109562598e31SEd Tanous BMCWEB_LOG_INFO("Not subscribed to this resource"); 109696330b99SSunitha Harish } 109796330b99SSunitha Harish } 109896330b99SSunitha Harish } 109996330b99SSunitha Harish 11007f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 11012558979cSP Dheeraj Srujan Kumar 11022558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 11037f4eb588SAppaRao Puli { 11042558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 11052558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 11062558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 11072558979cSP Dheeraj Srujan Kumar } 11082558979cSP Dheeraj Srujan Kumar 11092558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 11102558979cSP Dheeraj Srujan Kumar { 11112558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 11122558979cSP Dheeraj Srujan Kumar 11137f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11147f4eb588SAppaRao Puli if (!logStream.good()) 11157f4eb588SAppaRao Puli { 111662598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed "); 11177f4eb588SAppaRao Puli return; 11187f4eb588SAppaRao Puli } 11197f4eb588SAppaRao Puli std::string logEntry; 11207f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11217f4eb588SAppaRao Puli { 11222558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11237f4eb588SAppaRao Puli } 11247f4eb588SAppaRao Puli } 11257f4eb588SAppaRao Puli 11267f4eb588SAppaRao Puli void readEventLogsFromFile() 11277f4eb588SAppaRao Puli { 11287f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11297f4eb588SAppaRao Puli if (!logStream.good()) 11307f4eb588SAppaRao Puli { 113162598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed"); 11327f4eb588SAppaRao Puli return; 11337f4eb588SAppaRao Puli } 11347f4eb588SAppaRao Puli 11357f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11367f4eb588SAppaRao Puli 11377f4eb588SAppaRao Puli std::string logEntry; 11382558979cSP Dheeraj Srujan Kumar 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 { 11442558979cSP Dheeraj Srujan Kumar // Update Pointer position 11452558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11462558979cSP Dheeraj Srujan Kumar 11472558979cSP Dheeraj Srujan Kumar std::string idStr; 11482558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 11497f4eb588SAppaRao Puli { 11507f4eb588SAppaRao Puli continue; 11517f4eb588SAppaRao Puli } 11527f4eb588SAppaRao Puli 1153e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11547f4eb588SAppaRao Puli { 11552558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 11562558979cSP Dheeraj Srujan Kumar // the remaining items below. 11572558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 11587f4eb588SAppaRao Puli continue; 11597f4eb588SAppaRao Puli } 11607f4eb588SAppaRao Puli 11617f4eb588SAppaRao Puli std::string timestamp; 11627f4eb588SAppaRao Puli std::string messageID; 11635e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11647f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11657f4eb588SAppaRao Puli messageArgs) != 0) 11667f4eb588SAppaRao Puli { 116762598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry params failed"); 11687f4eb588SAppaRao Puli continue; 11697f4eb588SAppaRao Puli } 11707f4eb588SAppaRao Puli 11717f4eb588SAppaRao Puli std::string registryName; 11727f4eb588SAppaRao Puli std::string messageKey; 11737f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 11747f4eb588SAppaRao Puli messageKey); 11757f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 11767f4eb588SAppaRao Puli { 11777f4eb588SAppaRao Puli continue; 11787f4eb588SAppaRao Puli } 11797f4eb588SAppaRao Puli 11807f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 11817f4eb588SAppaRao Puli messageKey, messageArgs); 11827f4eb588SAppaRao Puli } 11837f4eb588SAppaRao Puli 1184e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11852558979cSP Dheeraj Srujan Kumar { 118662598e31SEd Tanous BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions."); 11872558979cSP Dheeraj Srujan Kumar return; 11882558979cSP Dheeraj Srujan Kumar } 11892558979cSP Dheeraj Srujan Kumar 11902558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 11912558979cSP Dheeraj Srujan Kumar { 11922558979cSP Dheeraj Srujan Kumar // No Records to send 119362598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 11942558979cSP Dheeraj Srujan Kumar return; 11952558979cSP Dheeraj Srujan Kumar } 11962558979cSP Dheeraj Srujan Kumar 11975e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 11987f4eb588SAppaRao Puli { 11997f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 12007f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 12017f4eb588SAppaRao Puli { 12027f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 12037f4eb588SAppaRao Puli } 12047f4eb588SAppaRao Puli } 12057f4eb588SAppaRao Puli } 12067f4eb588SAppaRao Puli 12077f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 12087f4eb588SAppaRao Puli { 12096a9f85f9SAppaRao Puli if (!inotifyConn) 12107f4eb588SAppaRao Puli { 12117f4eb588SAppaRao Puli return; 12127f4eb588SAppaRao Puli } 12137f4eb588SAppaRao Puli 12147f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 12157f4eb588SAppaRao Puli 1216002d39b4SEd Tanous inotifyConn->async_read_some(boost::asio::buffer(readBuffer), 12177f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 12187f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 12197f4eb588SAppaRao Puli if (ec) 12207f4eb588SAppaRao Puli { 122162598e31SEd Tanous BMCWEB_LOG_ERROR("Callback Error: {}", ec.message()); 12227f4eb588SAppaRao Puli return; 12237f4eb588SAppaRao Puli } 12247f4eb588SAppaRao Puli std::size_t index = 0; 1225b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 12267f4eb588SAppaRao Puli { 1227d3a9e084SEd Tanous struct inotify_event event 1228d3a9e084SEd Tanous {}; 1229b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1230b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1231b792cc56SAppaRao Puli { 1232b792cc56SAppaRao Puli if ((event.len == 0) || 1233b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1234b792cc56SAppaRao Puli { 1235b792cc56SAppaRao Puli index += (iEventSize + event.len); 1236b792cc56SAppaRao Puli continue; 1237b792cc56SAppaRao Puli } 1238b792cc56SAppaRao Puli 12394f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 12404f568f74SJiaqing Zhao if (fileName != "redfish") 1241b792cc56SAppaRao Puli { 1242b792cc56SAppaRao Puli index += (iEventSize + event.len); 1243b792cc56SAppaRao Puli continue; 1244b792cc56SAppaRao Puli } 1245b792cc56SAppaRao Puli 124662598e31SEd Tanous BMCWEB_LOG_DEBUG( 124762598e31SEd Tanous "Redfish log file created/deleted. event.name: {}", 124862598e31SEd Tanous fileName); 1249b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1250b792cc56SAppaRao Puli { 1251b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1252b792cc56SAppaRao Puli { 125362598e31SEd Tanous BMCWEB_LOG_DEBUG( 125462598e31SEd Tanous "Remove and Add inotify watcher on " 125562598e31SEd Tanous "redfish event log file"); 1256016761afSAppaRao Puli // Remove existing inotify watcher and add 1257016761afSAppaRao Puli // with new redfish event log file. 1258016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1259016761afSAppaRao Puli fileWatchDesc = -1; 1260b792cc56SAppaRao Puli } 1261b792cc56SAppaRao Puli 1262b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1263b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1264b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1265b792cc56SAppaRao Puli { 126662598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for " 126762598e31SEd Tanous "redfish log file."); 1268b792cc56SAppaRao Puli return; 1269b792cc56SAppaRao Puli } 1270b792cc56SAppaRao Puli 1271b792cc56SAppaRao Puli EventServiceManager::getInstance() 12722558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1273b792cc56SAppaRao Puli EventServiceManager::getInstance() 1274b792cc56SAppaRao Puli .readEventLogsFromFile(); 1275b792cc56SAppaRao Puli } 1276b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1277b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1278b792cc56SAppaRao Puli { 1279b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1280b792cc56SAppaRao Puli { 1281b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1282b792cc56SAppaRao Puli fileWatchDesc = -1; 1283b792cc56SAppaRao Puli } 1284b792cc56SAppaRao Puli } 1285b792cc56SAppaRao Puli } 1286b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1287b792cc56SAppaRao Puli { 1288b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12897f4eb588SAppaRao Puli { 12907f4eb588SAppaRao Puli EventServiceManager::getInstance() 12917f4eb588SAppaRao Puli .readEventLogsFromFile(); 12927f4eb588SAppaRao Puli } 1293b792cc56SAppaRao Puli } 1294b792cc56SAppaRao Puli index += (iEventSize + event.len); 12957f4eb588SAppaRao Puli } 12967f4eb588SAppaRao Puli 12977f4eb588SAppaRao Puli watchRedfishEventLogFile(); 12987f4eb588SAppaRao Puli }); 12997f4eb588SAppaRao Puli } 13007f4eb588SAppaRao Puli 13017f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 13027f4eb588SAppaRao Puli { 130323a21a1cSEd Tanous inotifyConn.emplace(ioc); 1304b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1305b792cc56SAppaRao Puli if (inotifyFd == -1) 13067f4eb588SAppaRao Puli { 130762598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_init1 failed."); 13087f4eb588SAppaRao Puli return -1; 13097f4eb588SAppaRao Puli } 1310b792cc56SAppaRao Puli 1311b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1312b792cc56SAppaRao Puli // create/delete. 1313b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1314b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1315b792cc56SAppaRao Puli if (dirWatchDesc == -1) 13167f4eb588SAppaRao Puli { 131762598e31SEd Tanous BMCWEB_LOG_ERROR( 131862598e31SEd Tanous "inotify_add_watch failed for event log directory."); 13197f4eb588SAppaRao Puli return -1; 13207f4eb588SAppaRao Puli } 13217f4eb588SAppaRao Puli 1322b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 132389492a15SPatrick Williams fileWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogFile, 132489492a15SPatrick Williams IN_MODIFY); 1325b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1326b792cc56SAppaRao Puli { 132762598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for redfish log file."); 1328b792cc56SAppaRao Puli // Don't return error if file not exist. 1329b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1330b792cc56SAppaRao Puli } 1331b792cc56SAppaRao Puli 13327f4eb588SAppaRao Puli // monitor redfish event log file 1333b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13347f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13357f4eb588SAppaRao Puli 13367f4eb588SAppaRao Puli return 0; 13377f4eb588SAppaRao Puli } 13387f4eb588SAppaRao Puli 13397f4eb588SAppaRao Puli #endif 134059d494eeSPatrick Williams static void getReadingsForReport(sdbusplus::message_t& msg) 1341156d6b00SAppaRao Puli { 134256d2396dSEd Tanous if (msg.is_method_error()) 134356d2396dSEd Tanous { 134462598e31SEd Tanous BMCWEB_LOG_ERROR("TelemetryMonitor Signal error"); 134556d2396dSEd Tanous return; 134656d2396dSEd Tanous } 134756d2396dSEd Tanous 1348c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1349c0353249SWludzik, Jozef std::string id = path.filename(); 1350c0353249SWludzik, Jozef if (id.empty()) 1351156d6b00SAppaRao Puli { 135262598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get Id from path"); 1353156d6b00SAppaRao Puli return; 1354156d6b00SAppaRao Puli } 1355156d6b00SAppaRao Puli 1356c0353249SWludzik, Jozef std::string interface; 1357b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1358c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1359c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1360c0353249SWludzik, Jozef 13613544d2a7SEd Tanous auto found = std::ranges::find_if( 13623544d2a7SEd Tanous props, [](const auto& x) { return x.first == "Readings"; }); 1363c0353249SWludzik, Jozef if (found == props.end()) 1364156d6b00SAppaRao Puli { 136562598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 1366156d6b00SAppaRao Puli return; 1367156d6b00SAppaRao Puli } 1368156d6b00SAppaRao Puli 13691e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 13701e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1371e662eae8SEd Tanous if (readings == nullptr) 13721e1e598dSJonathan Doman { 137362598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 13741e1e598dSJonathan Doman return; 13751e1e598dSJonathan Doman } 13761e1e598dSJonathan Doman 1377156d6b00SAppaRao Puli for (const auto& it : 1378156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1379156d6b00SAppaRao Puli { 1380e05aec50SEd Tanous Subscription& entry = *it.second; 1381c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1382156d6b00SAppaRao Puli { 13831e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1384156d6b00SAppaRao Puli } 1385156d6b00SAppaRao Puli } 1386156d6b00SAppaRao Puli } 1387156d6b00SAppaRao Puli 1388156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1389156d6b00SAppaRao Puli { 13907d1cc387SAppaRao Puli if (matchTelemetryMonitor) 13917d1cc387SAppaRao Puli { 139262598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Unregister"); 1393156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1394156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1395156d6b00SAppaRao Puli } 13967d1cc387SAppaRao Puli } 1397156d6b00SAppaRao Puli 1398156d6b00SAppaRao Puli void registerMetricReportSignal() 1399156d6b00SAppaRao Puli { 14007d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1401156d6b00SAppaRao Puli { 140262598e31SEd Tanous BMCWEB_LOG_DEBUG("Not registering metric report signal."); 1403156d6b00SAppaRao Puli return; 1404156d6b00SAppaRao Puli } 1405156d6b00SAppaRao Puli 140662598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Register"); 1407c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1408c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1409c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1410156d6b00SAppaRao Puli 141159d494eeSPatrick Williams matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>( 141256d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1413156d6b00SAppaRao Puli } 141423a21a1cSEd Tanous }; 1415b52664e2SAppaRao Puli 1416b52664e2SAppaRao Puli } // namespace redfish 1417