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" 297f4eb588SAppaRao Puli 307f4eb588SAppaRao Puli #include <sys/inotify.h> 31b52664e2SAppaRao Puli 3211ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 33fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 34b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 35ef4c65b7SEd Tanous #include <boost/url/format.hpp> 36b5b40605Snitroglycerine #include <sdbusplus/bus/match.hpp> 371214b7e7SGunnar Mills 385e44e3d8SAppaRao Puli #include <algorithm> 39b52664e2SAppaRao Puli #include <cstdlib> 40b52664e2SAppaRao Puli #include <ctime> 411bf712bcSAyushi Smriti #include <fstream> 42b52664e2SAppaRao Puli #include <memory> 433544d2a7SEd Tanous #include <ranges> 4426702d01SEd Tanous #include <span> 45b52664e2SAppaRao Puli 46b52664e2SAppaRao Puli namespace redfish 47b52664e2SAppaRao Puli { 48156d6b00SAppaRao Puli 49156d6b00SAppaRao Puli using ReadingsObjType = 5093f5d7c7SWludzik, Jozef std::vector<std::tuple<std::string, std::string, double, int32_t>>; 51156d6b00SAppaRao Puli 52156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 53156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 54156d6b00SAppaRao Puli 555e44e3d8SAppaRao Puli static constexpr const char* subscriptionTypeSSE = "SSE"; 561bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 571bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 581bf712bcSAyushi Smriti 595e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSubscriptions = 20; 605e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSSESubscriptions = 10; 615e44e3d8SAppaRao Puli 624642bf8fSGeorge Liu #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 63cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 644642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 654642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 664642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 674642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 68cf9e417dSEd Tanous 69cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 704642bf8fSGeorge Liu static int inotifyFd = -1; 71cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 724642bf8fSGeorge Liu static int dirWatchDesc = -1; 73cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 744642bf8fSGeorge Liu static int fileWatchDesc = -1; 754642bf8fSGeorge Liu 764642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 774642bf8fSGeorge Liu using EventLogObjectsType = 784642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 794642bf8fSGeorge Liu std::vector<std::string>>; 804642bf8fSGeorge Liu 81fffb8c1fSEd Tanous namespace registries 824642bf8fSGeorge Liu { 837f4eb588SAppaRao Puli static const Message* 847f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 8526702d01SEd Tanous const std::span<const MessageEntry>& registry) 867f4eb588SAppaRao Puli { 873544d2a7SEd Tanous std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if( 883544d2a7SEd Tanous registry, [&messageKey](const MessageEntry& messageEntry) { 8955f79e6fSEd Tanous return messageKey == messageEntry.first; 907f4eb588SAppaRao Puli }); 9126702d01SEd Tanous if (messageIt != registry.end()) 927f4eb588SAppaRao Puli { 937f4eb588SAppaRao Puli return &messageIt->second; 947f4eb588SAppaRao Puli } 957f4eb588SAppaRao Puli 967f4eb588SAppaRao Puli return nullptr; 977f4eb588SAppaRao Puli } 987f4eb588SAppaRao Puli 9926ccae32SEd Tanous static const Message* formatMessage(std::string_view messageID) 1007f4eb588SAppaRao Puli { 1017f4eb588SAppaRao Puli // Redfish MessageIds are in the form 1027f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1037f4eb588SAppaRao Puli // the right Message 1047f4eb588SAppaRao Puli std::vector<std::string> fields; 1057f4eb588SAppaRao Puli fields.reserve(4); 10650ebd4afSEd Tanous 10750ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 1087f4eb588SAppaRao Puli if (fields.size() != 4) 1097f4eb588SAppaRao Puli { 1107f4eb588SAppaRao Puli return nullptr; 1117f4eb588SAppaRao Puli } 11202cad96eSEd Tanous const std::string& registryName = fields[0]; 11302cad96eSEd Tanous const std::string& messageKey = fields[3]; 1147f4eb588SAppaRao Puli 1157f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 116b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1177f4eb588SAppaRao Puli } 118fffb8c1fSEd Tanous } // namespace registries 1197f4eb588SAppaRao Puli 1207f4eb588SAppaRao Puli namespace event_log 1217f4eb588SAppaRao Puli { 1222558979cSP Dheeraj Srujan Kumar inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 1237f4eb588SAppaRao Puli { 1247f4eb588SAppaRao Puli static time_t prevTs = 0; 1257f4eb588SAppaRao Puli static int index = 0; 1267f4eb588SAppaRao Puli 1277f4eb588SAppaRao Puli // Get the entry timestamp 1287f4eb588SAppaRao Puli std::time_t curTs = 0; 1297f4eb588SAppaRao Puli std::tm timeStruct = {}; 1307f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1317f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1327f4eb588SAppaRao Puli { 1337f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1347f4eb588SAppaRao Puli if (curTs == -1) 1357f4eb588SAppaRao Puli { 1367f4eb588SAppaRao Puli return false; 1377f4eb588SAppaRao Puli } 1387f4eb588SAppaRao Puli } 1397f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1407f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1417f4eb588SAppaRao Puli 1427f4eb588SAppaRao Puli // Save the timestamp 1437f4eb588SAppaRao Puli prevTs = curTs; 1447f4eb588SAppaRao Puli 1457f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1467f4eb588SAppaRao Puli if (index > 0) 1477f4eb588SAppaRao Puli { 1487f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1497f4eb588SAppaRao Puli } 1507f4eb588SAppaRao Puli return true; 1517f4eb588SAppaRao Puli } 1527f4eb588SAppaRao Puli 15323a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 15423a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1555e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1567f4eb588SAppaRao Puli { 1577f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1587f4eb588SAppaRao Puli // First get the Timestamp 159f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1607f4eb588SAppaRao Puli if (space == std::string::npos) 1617f4eb588SAppaRao Puli { 1627f4eb588SAppaRao Puli return -EINVAL; 1637f4eb588SAppaRao Puli } 1647f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1657f4eb588SAppaRao Puli // Then get the log contents 166f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1677f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1687f4eb588SAppaRao Puli { 1697f4eb588SAppaRao Puli return -EINVAL; 1707f4eb588SAppaRao Puli } 1717f4eb588SAppaRao Puli std::string_view entry(logEntry); 1727f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1737f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1747f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 17550ebd4afSEd Tanous bmcweb::split(logEntryFields, entry, ','); 1767f4eb588SAppaRao Puli // We need at least a MessageId to be valid 17726f6976fSEd Tanous if (logEntryFields.empty()) 1787f4eb588SAppaRao Puli { 1797f4eb588SAppaRao Puli return -EINVAL; 1807f4eb588SAppaRao Puli } 1817f4eb588SAppaRao Puli messageID = logEntryFields[0]; 1827f4eb588SAppaRao Puli 1837f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 1847f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 1857f4eb588SAppaRao Puli { 18602cad96eSEd Tanous const std::string& messageArgsStart = logEntryFields[1]; 1877f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 1887f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 1897f4eb588SAppaRao Puli { 1905e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 1915e715de6SAppaRao Puli logEntryFields.end()); 1927f4eb588SAppaRao Puli } 1937f4eb588SAppaRao Puli } 1947f4eb588SAppaRao Puli 1957f4eb588SAppaRao Puli return 0; 1967f4eb588SAppaRao Puli } 1977f4eb588SAppaRao Puli 19823a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 1997f4eb588SAppaRao Puli std::string& registryName, 2007f4eb588SAppaRao Puli std::string& messageKey) 2017f4eb588SAppaRao Puli { 2027f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2037f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2047f4eb588SAppaRao Puli // the right Message 2057f4eb588SAppaRao Puli std::vector<std::string> fields; 2067f4eb588SAppaRao Puli fields.reserve(4); 20750ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 2087f4eb588SAppaRao Puli if (fields.size() == 4) 2097f4eb588SAppaRao Puli { 2107f4eb588SAppaRao Puli registryName = fields[0]; 2117f4eb588SAppaRao Puli messageKey = fields[3]; 2127f4eb588SAppaRao Puli } 2137f4eb588SAppaRao Puli } 2147f4eb588SAppaRao Puli 21523a21a1cSEd Tanous inline int formatEventLogEntry(const std::string& logEntryID, 2167f4eb588SAppaRao Puli const std::string& messageID, 217c5ba4c27SEd Tanous const std::span<std::string_view> messageArgs, 21823a21a1cSEd Tanous std::string timestamp, 219b5a76932SEd Tanous const std::string& customText, 2207f4eb588SAppaRao Puli nlohmann::json& logEntryJson) 2217f4eb588SAppaRao Puli { 2227f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 223fffb8c1fSEd Tanous const registries::Message* message = registries::formatMessage(messageID); 2247f4eb588SAppaRao Puli 22580f595e7SEd Tanous if (message == nullptr) 2267f4eb588SAppaRao Puli { 22780f595e7SEd Tanous return -1; 2287f4eb588SAppaRao Puli } 2297f4eb588SAppaRao Puli 23089492a15SPatrick Williams std::string msg = redfish::registries::fillMessageArgs(messageArgs, 23189492a15SPatrick Williams message->message); 23280f595e7SEd Tanous if (msg.empty()) 23380f595e7SEd Tanous { 23480f595e7SEd Tanous return -1; 23580f595e7SEd Tanous } 2367f4eb588SAppaRao Puli 2377f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2387f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2397f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 240f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 241b2f7609bSEd Tanous std::size_t plus = timestamp.find_first_of('+', dot); 2427f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2437f4eb588SAppaRao Puli { 2447f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2457f4eb588SAppaRao Puli } 2467f4eb588SAppaRao Puli 2477f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2481476687dSEd Tanous logEntryJson["EventId"] = logEntryID; 2491476687dSEd Tanous logEntryJson["EventType"] = "Event"; 25080f595e7SEd Tanous logEntryJson["Severity"] = message->messageSeverity; 2511476687dSEd Tanous logEntryJson["Message"] = std::move(msg); 2521476687dSEd Tanous logEntryJson["MessageId"] = messageID; 2531476687dSEd Tanous logEntryJson["MessageArgs"] = messageArgs; 2541476687dSEd Tanous logEntryJson["EventTimestamp"] = std::move(timestamp); 2551476687dSEd Tanous logEntryJson["Context"] = customText; 2567f4eb588SAppaRao Puli return 0; 2577f4eb588SAppaRao Puli } 2587f4eb588SAppaRao Puli 2597f4eb588SAppaRao Puli } // namespace event_log 2607f4eb588SAppaRao Puli #endif 2617f4eb588SAppaRao Puli 26223a21a1cSEd Tanous inline bool isFilterQuerySpecialChar(char c) 26307941a88SAyushi Smriti { 26407941a88SAyushi Smriti switch (c) 26507941a88SAyushi Smriti { 26607941a88SAyushi Smriti case '(': 26707941a88SAyushi Smriti case ')': 26807941a88SAyushi Smriti case '\'': 26907941a88SAyushi Smriti return true; 27007941a88SAyushi Smriti default: 27107941a88SAyushi Smriti return false; 27207941a88SAyushi Smriti } 27307941a88SAyushi Smriti } 27407941a88SAyushi Smriti 27523a21a1cSEd Tanous inline bool 27623a21a1cSEd Tanous readSSEQueryParams(std::string sseFilter, std::string& formatType, 27707941a88SAyushi Smriti std::vector<std::string>& messageIds, 27807941a88SAyushi Smriti std::vector<std::string>& registryPrefixes, 279144b6318SAppaRao Puli std::vector<std::string>& metricReportDefinitions) 28007941a88SAyushi Smriti { 2813544d2a7SEd Tanous auto remove = std::ranges::remove_if(sseFilter, isFilterQuerySpecialChar); 2823544d2a7SEd Tanous sseFilter.erase(std::ranges::begin(remove), sseFilter.end()); 28307941a88SAyushi Smriti 28407941a88SAyushi Smriti std::vector<std::string> result; 28550ebd4afSEd Tanous 28650ebd4afSEd Tanous // NOLINTNEXTLINE 28750ebd4afSEd Tanous bmcweb::split(result, sseFilter, ' '); 28807941a88SAyushi Smriti 28962598e31SEd Tanous BMCWEB_LOG_DEBUG("No of tokens in SEE query: {}", result.size()); 29007941a88SAyushi Smriti 29107941a88SAyushi Smriti constexpr uint8_t divisor = 4; 29207941a88SAyushi Smriti constexpr uint8_t minTokenSize = 3; 29307941a88SAyushi Smriti if (result.size() % divisor != minTokenSize) 29407941a88SAyushi Smriti { 29562598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid SSE filter specified."); 29607941a88SAyushi Smriti return false; 29707941a88SAyushi Smriti } 29807941a88SAyushi Smriti 29907941a88SAyushi Smriti for (std::size_t i = 0; i < result.size(); i += divisor) 30007941a88SAyushi Smriti { 30102cad96eSEd Tanous const std::string& key = result[i]; 30202cad96eSEd Tanous const std::string& op = result[i + 1]; 30302cad96eSEd Tanous const std::string& value = result[i + 2]; 30407941a88SAyushi Smriti 30507941a88SAyushi Smriti if ((i + minTokenSize) < result.size()) 30607941a88SAyushi Smriti { 30702cad96eSEd Tanous const std::string& separator = result[i + minTokenSize]; 30807941a88SAyushi Smriti // SSE supports only "or" and "and" in query params. 3094e0453b1SGunnar Mills if ((separator != "or") && (separator != "and")) 31007941a88SAyushi Smriti { 31162598e31SEd Tanous BMCWEB_LOG_ERROR( 31262598e31SEd Tanous "Invalid group operator in SSE query parameters"); 31307941a88SAyushi Smriti return false; 31407941a88SAyushi Smriti } 31507941a88SAyushi Smriti } 31607941a88SAyushi Smriti 31707941a88SAyushi Smriti // SSE supports only "eq" as per spec. 31807941a88SAyushi Smriti if (op != "eq") 31907941a88SAyushi Smriti { 32062598e31SEd Tanous BMCWEB_LOG_ERROR( 32162598e31SEd Tanous "Invalid assignment operator in SSE query parameters"); 32207941a88SAyushi Smriti return false; 32307941a88SAyushi Smriti } 32407941a88SAyushi Smriti 32562598e31SEd Tanous BMCWEB_LOG_DEBUG("{} : {}", key, value); 32607941a88SAyushi Smriti if (key == "EventFormatType") 32707941a88SAyushi Smriti { 32807941a88SAyushi Smriti formatType = value; 32907941a88SAyushi Smriti } 33007941a88SAyushi Smriti else if (key == "MessageId") 33107941a88SAyushi Smriti { 33207941a88SAyushi Smriti messageIds.push_back(value); 33307941a88SAyushi Smriti } 33407941a88SAyushi Smriti else if (key == "RegistryPrefix") 33507941a88SAyushi Smriti { 33607941a88SAyushi Smriti registryPrefixes.push_back(value); 33707941a88SAyushi Smriti } 33807941a88SAyushi Smriti else if (key == "MetricReportDefinition") 33907941a88SAyushi Smriti { 34007941a88SAyushi Smriti metricReportDefinitions.push_back(value); 34107941a88SAyushi Smriti } 34207941a88SAyushi Smriti else 34307941a88SAyushi Smriti { 34462598e31SEd Tanous BMCWEB_LOG_ERROR("Invalid property({})in SSE filter query.", key); 34507941a88SAyushi Smriti return false; 34607941a88SAyushi Smriti } 34707941a88SAyushi Smriti } 34807941a88SAyushi Smriti return true; 34907941a88SAyushi Smriti } 35007941a88SAyushi Smriti 35128afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 352b52664e2SAppaRao Puli { 353b52664e2SAppaRao Puli public: 354b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 355b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 356b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 357b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 358b52664e2SAppaRao Puli 359a716aa74SEd Tanous Subscription(boost::urls::url_view url, boost::asio::io_context& ioc) : 360a716aa74SEd Tanous policy(std::make_shared<crow::ConnectionPolicy>()) 361b52664e2SAppaRao Puli { 362a716aa74SEd Tanous destinationUrl = url; 3635e44e3d8SAppaRao Puli client.emplace(ioc, policy); 3647adb85acSSunitha Harish // Subscription constructor 365d14a48ffSCarson Labrado policy->invalidResp = retryRespHandler; 366b52664e2SAppaRao Puli } 3674bbf237fSAppaRao Puli 3685e44e3d8SAppaRao Puli explicit Subscription(crow::sse_socket::Connection& connIn) : 3695e44e3d8SAppaRao Puli sseConn(&connIn) 3705e44e3d8SAppaRao Puli {} 3715e44e3d8SAppaRao Puli 3729f616dd1SEd Tanous ~Subscription() = default; 373b52664e2SAppaRao Puli 3745e44e3d8SAppaRao Puli bool sendEvent(std::string&& msg) 375b52664e2SAppaRao Puli { 3766ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 3776ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 3786ba8c82eSsunharis_in .getEventServiceConfig(); 3796ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 3806ba8c82eSsunharis_in { 3816ba8c82eSsunharis_in return false; 3826ba8c82eSsunharis_in } 3836ba8c82eSsunharis_in 384f52c03c1SCarson Labrado // A connection pool will be created if one does not already exist 3855e44e3d8SAppaRao Puli if (client) 3865e44e3d8SAppaRao Puli { 387a716aa74SEd Tanous client->sendData(std::move(msg), destinationUrl, httpHeaders, 388a716aa74SEd Tanous boost::beast::http::verb::post); 3895e44e3d8SAppaRao Puli return true; 3905e44e3d8SAppaRao Puli } 3917adb85acSSunitha Harish 3924bbf237fSAppaRao Puli if (sseConn != nullptr) 3934bbf237fSAppaRao Puli { 3945e44e3d8SAppaRao Puli eventSeqNum++; 3955e44e3d8SAppaRao Puli sseConn->sendEvent(std::to_string(eventSeqNum), msg); 3964bbf237fSAppaRao Puli } 3976ba8c82eSsunharis_in return true; 3984bbf237fSAppaRao Puli } 3994bbf237fSAppaRao Puli 4006ba8c82eSsunharis_in bool sendTestEventLog() 4010b4bdd93SAppaRao Puli { 4020b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 4030b4bdd93SAppaRao Puli logEntryArray.push_back({}); 4040b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 4050b4bdd93SAppaRao Puli 406613dabeaSEd Tanous logEntryJson["EventId"] = "TestID"; 407613dabeaSEd Tanous logEntryJson["EventType"] = "Event"; 408613dabeaSEd Tanous logEntryJson["Severity"] = "OK"; 409613dabeaSEd Tanous logEntryJson["Message"] = "Generated test event"; 410613dabeaSEd Tanous logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog"; 411613dabeaSEd Tanous logEntryJson["MessageArgs"] = nlohmann::json::array(); 412613dabeaSEd Tanous logEntryJson["EventTimestamp"] = 413613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 414613dabeaSEd Tanous logEntryJson["Context"] = customText; 4150b4bdd93SAppaRao Puli 4161476687dSEd Tanous nlohmann::json msg; 4171476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4181476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4191476687dSEd Tanous msg["Name"] = "Event Log"; 4201476687dSEd Tanous msg["Events"] = logEntryArray; 4210b4bdd93SAppaRao Puli 42289492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 42389492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 4245e44e3d8SAppaRao Puli return sendEvent(std::move(strMsg)); 4250b4bdd93SAppaRao Puli } 4260b4bdd93SAppaRao Puli 4277f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 4287f4eb588SAppaRao Puli void filterAndSendEventLogs( 4297f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4307f4eb588SAppaRao Puli { 4317f4eb588SAppaRao Puli nlohmann::json logEntryArray; 4327f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4337f4eb588SAppaRao Puli { 4347f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 4357f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 4367f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 4377f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 4387f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 4395e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 4407f4eb588SAppaRao Puli 4417f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 4427f4eb588SAppaRao Puli // send everything. 44326f6976fSEd Tanous if (!registryPrefixes.empty()) 4447f4eb588SAppaRao Puli { 4453544d2a7SEd Tanous auto obj = std::ranges::find(registryPrefixes, registryName); 4467f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 4477f4eb588SAppaRao Puli { 4487f4eb588SAppaRao Puli continue; 4497f4eb588SAppaRao Puli } 4507f4eb588SAppaRao Puli } 4517f4eb588SAppaRao Puli 4527f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 4537f4eb588SAppaRao Puli // send everything. 45426f6976fSEd Tanous if (!registryMsgIds.empty()) 4557f4eb588SAppaRao Puli { 4563544d2a7SEd Tanous auto obj = std::ranges::find(registryMsgIds, messageKey); 4577f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 4587f4eb588SAppaRao Puli { 4597f4eb588SAppaRao Puli continue; 4607f4eb588SAppaRao Puli } 4617f4eb588SAppaRao Puli } 4627f4eb588SAppaRao Puli 463c5ba4c27SEd Tanous std::vector<std::string_view> messageArgsView(messageArgs.begin(), 464c5ba4c27SEd Tanous messageArgs.end()); 465c5ba4c27SEd Tanous 4667f4eb588SAppaRao Puli logEntryArray.push_back({}); 4677f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 468c5ba4c27SEd Tanous if (event_log::formatEventLogEntry(idStr, messageID, 469c5ba4c27SEd Tanous messageArgsView, timestamp, 470c5ba4c27SEd Tanous customText, bmcLogEntry) != 0) 4717f4eb588SAppaRao Puli { 47262598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry failed"); 4737f4eb588SAppaRao Puli continue; 4747f4eb588SAppaRao Puli } 4757f4eb588SAppaRao Puli } 4767f4eb588SAppaRao Puli 47726f6976fSEd Tanous if (logEntryArray.empty()) 4787f4eb588SAppaRao Puli { 47962598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 4807f4eb588SAppaRao Puli return; 4817f4eb588SAppaRao Puli } 4827f4eb588SAppaRao Puli 4831476687dSEd Tanous nlohmann::json msg; 4841476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4851476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4861476687dSEd Tanous msg["Name"] = "Event Log"; 4871476687dSEd Tanous msg["Events"] = logEntryArray; 48889492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 48989492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 4905e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 4915e44e3d8SAppaRao Puli eventSeqNum++; 4927f4eb588SAppaRao Puli } 4937f4eb588SAppaRao Puli #endif 4947f4eb588SAppaRao Puli 495248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 4961e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 497156d6b00SAppaRao Puli { 498ef4c65b7SEd Tanous boost::urls::url mrdUri = boost::urls::format( 499ef4c65b7SEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", 500ef4c65b7SEd Tanous reportId); 501156d6b00SAppaRao Puli 502156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 50326f6976fSEd Tanous if (!metricReportDefinitions.empty()) 504156d6b00SAppaRao Puli { 5053544d2a7SEd Tanous if (std::ranges::find(metricReportDefinitions, mrdUri.buffer()) == 5063544d2a7SEd Tanous metricReportDefinitions.end()) 507156d6b00SAppaRao Puli { 508156d6b00SAppaRao Puli return; 509156d6b00SAppaRao Puli } 510156d6b00SAppaRao Puli } 511156d6b00SAppaRao Puli 512c0353249SWludzik, Jozef nlohmann::json msg; 513248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 514156d6b00SAppaRao Puli { 51562598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to fill the MetricReport for DBus " 51662598e31SEd Tanous "Report with id {}", 51762598e31SEd Tanous reportId); 518c0353249SWludzik, Jozef return; 519156d6b00SAppaRao Puli } 520156d6b00SAppaRao Puli 52122daffd7SAppaRao Puli // Context is set by user during Event subscription and it must be 52222daffd7SAppaRao Puli // set for MetricReport response. 52322daffd7SAppaRao Puli if (!customText.empty()) 52422daffd7SAppaRao Puli { 52522daffd7SAppaRao Puli msg["Context"] = customText; 52622daffd7SAppaRao Puli } 52722daffd7SAppaRao Puli 52889492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 52989492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 5305e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 531156d6b00SAppaRao Puli } 532156d6b00SAppaRao Puli 533d14a48ffSCarson Labrado void updateRetryConfig(uint32_t retryAttempts, 534d14a48ffSCarson Labrado uint32_t retryTimeoutInterval) 535fe44eb0bSAyushi Smriti { 536d14a48ffSCarson Labrado policy->maxRetryAttempts = retryAttempts; 537d14a48ffSCarson Labrado policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval); 53862de0c68SAppaRao Puli } 539fe44eb0bSAyushi Smriti 5409eb808c1SEd Tanous uint64_t getEventSeqNum() const 54196330b99SSunitha Harish { 54296330b99SSunitha Harish return eventSeqNum; 54396330b99SSunitha Harish } 54496330b99SSunitha Harish 5455e44e3d8SAppaRao Puli void setSubscriptionId(const std::string& id2) 5465e44e3d8SAppaRao Puli { 54762598e31SEd Tanous BMCWEB_LOG_DEBUG("Subscription ID: {}", id2); 5485e44e3d8SAppaRao Puli subId = id2; 5495e44e3d8SAppaRao Puli } 5505e44e3d8SAppaRao Puli 5515e44e3d8SAppaRao Puli std::string getSubscriptionId() 5525e44e3d8SAppaRao Puli { 5535e44e3d8SAppaRao Puli return subId; 5545e44e3d8SAppaRao Puli } 5555e44e3d8SAppaRao Puli 5565e44e3d8SAppaRao Puli bool matchSseId(const crow::sse_socket::Connection& thisConn) 5575e44e3d8SAppaRao Puli { 5585e44e3d8SAppaRao Puli return &thisConn == sseConn; 5595e44e3d8SAppaRao Puli } 5605e44e3d8SAppaRao Puli 561b52664e2SAppaRao Puli private: 5625e44e3d8SAppaRao Puli std::string subId; 563d14a48ffSCarson Labrado uint64_t eventSeqNum = 1; 564a716aa74SEd Tanous boost::urls::url host; 565d14a48ffSCarson Labrado std::shared_ptr<crow::ConnectionPolicy> policy; 5665e44e3d8SAppaRao Puli crow::sse_socket::Connection* sseConn = nullptr; 5675e44e3d8SAppaRao Puli std::optional<crow::HttpClient> client; 568b52664e2SAppaRao Puli std::string path; 569b52664e2SAppaRao Puli std::string uriProto; 570a7a80296SCarson Labrado 571a7a80296SCarson Labrado // Check used to indicate what response codes are valid as part of our retry 572a7a80296SCarson Labrado // policy. 2XX is considered acceptable 573a7a80296SCarson Labrado static boost::system::error_code retryRespHandler(unsigned int respCode) 574a7a80296SCarson Labrado { 57562598e31SEd Tanous BMCWEB_LOG_DEBUG( 57662598e31SEd Tanous "Checking response code validity for SubscriptionEvent"); 577a7a80296SCarson Labrado if ((respCode < 200) || (respCode >= 300)) 578a7a80296SCarson Labrado { 579a7a80296SCarson Labrado return boost::system::errc::make_error_code( 580a7a80296SCarson Labrado boost::system::errc::result_out_of_range); 581a7a80296SCarson Labrado } 582a7a80296SCarson Labrado 583a7a80296SCarson Labrado // Return 0 if the response code is valid 584a7a80296SCarson Labrado return boost::system::errc::make_error_code( 585a7a80296SCarson Labrado boost::system::errc::success); 5869fa6d147SNan Zhou } 587b52664e2SAppaRao Puli }; 588b52664e2SAppaRao Puli 589b52664e2SAppaRao Puli class EventServiceManager 590b52664e2SAppaRao Puli { 591b52664e2SAppaRao Puli private: 592d3a9e084SEd Tanous bool serviceEnabled = false; 593d3a9e084SEd Tanous uint32_t retryAttempts = 0; 594d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 5957d1cc387SAppaRao Puli 5962558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 5979f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 5989f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 59959d494eeSPatrick Williams std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor; 600b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 601b52664e2SAppaRao Puli subscriptionsMap; 602b52664e2SAppaRao Puli 6039f616dd1SEd Tanous uint64_t eventId{1}; 60496330b99SSunitha Harish 605f8ca6d79SEd Tanous boost::asio::io_context& ioc; 606f8ca6d79SEd Tanous 607b52664e2SAppaRao Puli public: 6089f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 6099f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 6109f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 6119f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 612ecd6a3a2SEd Tanous ~EventServiceManager() = default; 6139f616dd1SEd Tanous 614f8ca6d79SEd Tanous explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn) 615b52664e2SAppaRao Puli { 616f8ca6d79SEd Tanous // Load config from persist store. 617f8ca6d79SEd Tanous initConfig(); 618f8ca6d79SEd Tanous } 619f8ca6d79SEd Tanous 620f8ca6d79SEd Tanous static EventServiceManager& 621f8ca6d79SEd Tanous getInstance(boost::asio::io_context* ioc = nullptr) 622f8ca6d79SEd Tanous { 623f8ca6d79SEd Tanous static EventServiceManager handler(*ioc); 624b52664e2SAppaRao Puli return handler; 625b52664e2SAppaRao Puli } 626b52664e2SAppaRao Puli 6271bf712bcSAyushi Smriti void initConfig() 6281bf712bcSAyushi Smriti { 62928afb49cSJunLin Chen loadOldBehavior(); 6301bf712bcSAyushi Smriti 63128afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 63228afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 63328afb49cSJunLin Chen .getEventServiceConfig(); 6341bf712bcSAyushi Smriti 63528afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 63628afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 63728afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6381bf712bcSAyushi Smriti 63928afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 64028afb49cSJunLin Chen .subscriptionsConfigMap) 6411bf712bcSAyushi Smriti { 64228afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 64328afb49cSJunLin Chen it.second; 6444bbf237fSAppaRao Puli 645*6fd29553SEd Tanous boost::system::result<boost::urls::url> url = 646a716aa74SEd Tanous boost::urls::parse_absolute_uri(newSub->destinationUrl); 6471bf712bcSAyushi Smriti 648a716aa74SEd Tanous if (!url) 6491bf712bcSAyushi Smriti { 65062598e31SEd Tanous BMCWEB_LOG_ERROR( 65162598e31SEd Tanous "Failed to validate and split destination url"); 6521bf712bcSAyushi Smriti continue; 6531bf712bcSAyushi Smriti } 6541bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 655a716aa74SEd Tanous std::make_shared<Subscription>(*url, ioc); 6561bf712bcSAyushi Smriti 65728afb49cSJunLin Chen subValue->id = newSub->id; 65828afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 65928afb49cSJunLin Chen subValue->protocol = newSub->protocol; 66028afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 66128afb49cSJunLin Chen subValue->customText = newSub->customText; 66228afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 66328afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 66428afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 66528afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 66628afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 66728afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 66828afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 6691bf712bcSAyushi Smriti 67028afb49cSJunLin Chen if (subValue->id.empty()) 6711bf712bcSAyushi Smriti { 67262598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to add subscription"); 6731bf712bcSAyushi Smriti } 67428afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 67528afb49cSJunLin Chen 67628afb49cSJunLin Chen updateNoOfSubscribersCount(); 67728afb49cSJunLin Chen 67828afb49cSJunLin Chen #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 6792558979cSP Dheeraj Srujan Kumar 6802558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 6812558979cSP Dheeraj Srujan Kumar 68228afb49cSJunLin Chen #endif 68328afb49cSJunLin Chen // Update retry configuration. 68428afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 6851bf712bcSAyushi Smriti } 6861bf712bcSAyushi Smriti } 6871bf712bcSAyushi Smriti 68856d2396dSEd Tanous static void loadOldBehavior() 689b52664e2SAppaRao Puli { 69028afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 69128afb49cSJunLin Chen if (!eventConfigFile.good()) 6921bf712bcSAyushi Smriti { 69362598e31SEd Tanous BMCWEB_LOG_DEBUG("Old eventService config not exist"); 69428afb49cSJunLin Chen return; 69528afb49cSJunLin Chen } 69628afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 69728afb49cSJunLin Chen if (jsonData.is_discarded()) 6984bbf237fSAppaRao Puli { 69962598e31SEd Tanous BMCWEB_LOG_ERROR("Old eventService config parse error."); 70028afb49cSJunLin Chen return; 70128afb49cSJunLin Chen } 70228afb49cSJunLin Chen 70328afb49cSJunLin Chen for (const auto& item : jsonData.items()) 70428afb49cSJunLin Chen { 70528afb49cSJunLin Chen if (item.key() == "Configuration") 70628afb49cSJunLin Chen { 70728afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 70828afb49cSJunLin Chen .getEventServiceConfig() 70928afb49cSJunLin Chen .fromJson(item.value()); 71028afb49cSJunLin Chen } 71128afb49cSJunLin Chen else if (item.key() == "Subscriptions") 71228afb49cSJunLin Chen { 71328afb49cSJunLin Chen for (const auto& elem : item.value()) 71428afb49cSJunLin Chen { 71528afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 71628afb49cSJunLin Chen newSubscription = 71728afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 71828afb49cSJunLin Chen true); 71928afb49cSJunLin Chen if (newSubscription == nullptr) 72028afb49cSJunLin Chen { 72162598e31SEd Tanous BMCWEB_LOG_ERROR("Problem reading subscription " 72262598e31SEd Tanous "from old persistent store"); 7234bbf237fSAppaRao Puli continue; 7244bbf237fSAppaRao Puli } 7251bf712bcSAyushi Smriti 72628afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 72728afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 7281bf712bcSAyushi Smriti 72928afb49cSJunLin Chen std::string id; 7301bf712bcSAyushi Smriti 73128afb49cSJunLin Chen int retry = 3; 732e662eae8SEd Tanous while (retry != 0) 7331bf712bcSAyushi Smriti { 73428afb49cSJunLin Chen id = std::to_string(dist(gen)); 73528afb49cSJunLin Chen if (gen.error()) 7367d1cc387SAppaRao Puli { 73728afb49cSJunLin Chen retry = 0; 73828afb49cSJunLin Chen break; 73928afb49cSJunLin Chen } 74028afb49cSJunLin Chen newSubscription->id = id; 74128afb49cSJunLin Chen auto inserted = 74228afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 74328afb49cSJunLin Chen .subscriptionsConfigMap.insert( 74428afb49cSJunLin Chen std::pair(id, newSubscription)); 74528afb49cSJunLin Chen if (inserted.second) 74628afb49cSJunLin Chen { 74728afb49cSJunLin Chen break; 74828afb49cSJunLin Chen } 74928afb49cSJunLin Chen --retry; 7507d1cc387SAppaRao Puli } 7517d1cc387SAppaRao Puli 75228afb49cSJunLin Chen if (retry <= 0) 75328afb49cSJunLin Chen { 75462598e31SEd Tanous BMCWEB_LOG_ERROR( 75562598e31SEd Tanous "Failed to generate random number from old " 75662598e31SEd Tanous "persistent store"); 75728afb49cSJunLin Chen continue; 75828afb49cSJunLin Chen } 75928afb49cSJunLin Chen } 76028afb49cSJunLin Chen } 76128afb49cSJunLin Chen 76228afb49cSJunLin Chen persistent_data::getConfig().writeData(); 76328afb49cSJunLin Chen std::remove(eventServiceFile); 76462598e31SEd Tanous BMCWEB_LOG_DEBUG("Remove old eventservice config"); 76528afb49cSJunLin Chen } 76628afb49cSJunLin Chen } 76728afb49cSJunLin Chen 7689eb808c1SEd Tanous void updateSubscriptionData() const 76928afb49cSJunLin Chen { 77028afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 77128afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 77228afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 77328afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 77428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 77528afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 77628afb49cSJunLin Chen 77728afb49cSJunLin Chen persistent_data::getConfig().writeData(); 77828afb49cSJunLin Chen } 77928afb49cSJunLin Chen 78028afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7817d1cc387SAppaRao Puli { 7827d1cc387SAppaRao Puli bool updateConfig = false; 783fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 7847d1cc387SAppaRao Puli 78528afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 7867d1cc387SAppaRao Puli { 78728afb49cSJunLin Chen serviceEnabled = cfg.enabled; 788e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 7897d1cc387SAppaRao Puli { 7907d1cc387SAppaRao Puli registerMetricReportSignal(); 7917d1cc387SAppaRao Puli } 7927d1cc387SAppaRao Puli else 7937d1cc387SAppaRao Puli { 7947d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7957d1cc387SAppaRao Puli } 7967d1cc387SAppaRao Puli updateConfig = true; 7977d1cc387SAppaRao Puli } 7987d1cc387SAppaRao Puli 79928afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 8007d1cc387SAppaRao Puli { 80128afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 8027d1cc387SAppaRao Puli updateConfig = true; 803fe44eb0bSAyushi Smriti updateRetryCfg = true; 8047d1cc387SAppaRao Puli } 8057d1cc387SAppaRao Puli 80628afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 8077d1cc387SAppaRao Puli { 80828afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 8097d1cc387SAppaRao Puli updateConfig = true; 810fe44eb0bSAyushi Smriti updateRetryCfg = true; 8117d1cc387SAppaRao Puli } 8127d1cc387SAppaRao Puli 8137d1cc387SAppaRao Puli if (updateConfig) 8147d1cc387SAppaRao Puli { 8157d1cc387SAppaRao Puli updateSubscriptionData(); 8167d1cc387SAppaRao Puli } 817fe44eb0bSAyushi Smriti 818fe44eb0bSAyushi Smriti if (updateRetryCfg) 819fe44eb0bSAyushi Smriti { 820fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 821fe44eb0bSAyushi Smriti for (const auto& it : 822fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 823fe44eb0bSAyushi Smriti { 8245e44e3d8SAppaRao Puli Subscription& entry = *it.second; 8255e44e3d8SAppaRao Puli entry.updateRetryConfig(retryAttempts, retryTimeoutInterval); 826fe44eb0bSAyushi Smriti } 827fe44eb0bSAyushi Smriti } 8287d1cc387SAppaRao Puli } 8297d1cc387SAppaRao Puli 8307d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8317d1cc387SAppaRao Puli { 8327d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8337d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8347d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8357d1cc387SAppaRao Puli { 8367d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8377d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8387d1cc387SAppaRao Puli { 8397d1cc387SAppaRao Puli eventLogSubCount++; 8407d1cc387SAppaRao Puli } 8417d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8427d1cc387SAppaRao Puli { 8437d1cc387SAppaRao Puli metricReportSubCount++; 8447d1cc387SAppaRao Puli } 8457d1cc387SAppaRao Puli } 8467d1cc387SAppaRao Puli 8477d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8487d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8497d1cc387SAppaRao Puli { 8507d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 851e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 8527d1cc387SAppaRao Puli { 8537d1cc387SAppaRao Puli registerMetricReportSignal(); 8547d1cc387SAppaRao Puli } 8557d1cc387SAppaRao Puli else 8567d1cc387SAppaRao Puli { 8577d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8587d1cc387SAppaRao Puli } 8597d1cc387SAppaRao Puli } 8607d1cc387SAppaRao Puli } 8617d1cc387SAppaRao Puli 862b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 863b52664e2SAppaRao Puli { 864b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 865b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 866b52664e2SAppaRao Puli { 86762598e31SEd Tanous BMCWEB_LOG_ERROR("No subscription exist with ID:{}", id); 868b52664e2SAppaRao Puli return nullptr; 869b52664e2SAppaRao Puli } 870b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 871b52664e2SAppaRao Puli return subValue; 872b52664e2SAppaRao Puli } 873b52664e2SAppaRao Puli 874b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 8751bf712bcSAyushi Smriti const bool updateFile = true) 876b52664e2SAppaRao Puli { 877fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 878fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 879fc76b8acSEd Tanous 880b52664e2SAppaRao Puli std::string id; 881b52664e2SAppaRao Puli 882b52664e2SAppaRao Puli int retry = 3; 883e662eae8SEd Tanous while (retry != 0) 884b52664e2SAppaRao Puli { 885fc76b8acSEd Tanous id = std::to_string(dist(gen)); 886fc76b8acSEd Tanous if (gen.error()) 887fc76b8acSEd Tanous { 888fc76b8acSEd Tanous retry = 0; 889fc76b8acSEd Tanous break; 890fc76b8acSEd Tanous } 891b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 892b52664e2SAppaRao Puli if (inserted.second) 893b52664e2SAppaRao Puli { 894b52664e2SAppaRao Puli break; 895b52664e2SAppaRao Puli } 896b52664e2SAppaRao Puli --retry; 89723a21a1cSEd Tanous } 898b52664e2SAppaRao Puli 899b52664e2SAppaRao Puli if (retry <= 0) 900b52664e2SAppaRao Puli { 90162598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to generate random number"); 902abb93cddSEd Tanous return ""; 903b52664e2SAppaRao Puli } 904b52664e2SAppaRao Puli 90528afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 90628afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 90728afb49cSJunLin Chen newSub->id = id; 90828afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 90928afb49cSJunLin Chen newSub->protocol = subValue->protocol; 91028afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 91128afb49cSJunLin Chen newSub->customText = subValue->customText; 91228afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 91328afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 91428afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 91528afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 91628afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 91728afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 91828afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 91928afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 92028afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 92128afb49cSJunLin Chen 9227d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 9231bf712bcSAyushi Smriti 9241bf712bcSAyushi Smriti if (updateFile) 9251bf712bcSAyushi Smriti { 926b52664e2SAppaRao Puli updateSubscriptionData(); 9271bf712bcSAyushi Smriti } 9287f4eb588SAppaRao Puli 9297f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 9302558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 9317f4eb588SAppaRao Puli { 9322558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 9337f4eb588SAppaRao Puli } 9347f4eb588SAppaRao Puli #endif 935fe44eb0bSAyushi Smriti // Update retry configuration. 936fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 937fe44eb0bSAyushi Smriti 9385e44e3d8SAppaRao Puli // Set Subscription ID for back trace 9395e44e3d8SAppaRao Puli subValue->setSubscriptionId(id); 940b52664e2SAppaRao Puli return id; 941b52664e2SAppaRao Puli } 942b52664e2SAppaRao Puli 943b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 944b52664e2SAppaRao Puli { 945b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 94655f79e6fSEd Tanous return obj != subscriptionsMap.end(); 947b52664e2SAppaRao Puli } 948b52664e2SAppaRao Puli 949b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 950b52664e2SAppaRao Puli { 951b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 952b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 953b52664e2SAppaRao Puli { 954b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 95528afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 95628afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 95728afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 95828afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9597d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 960b52664e2SAppaRao Puli updateSubscriptionData(); 961b52664e2SAppaRao Puli } 962b52664e2SAppaRao Puli } 963b52664e2SAppaRao Puli 9645e44e3d8SAppaRao Puli void deleteSseSubscription(const crow::sse_socket::Connection& thisConn) 9655e44e3d8SAppaRao Puli { 9665e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 9675e44e3d8SAppaRao Puli { 9685e44e3d8SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 9695e44e3d8SAppaRao Puli bool entryIsThisConn = entry->matchSseId(thisConn); 9705e44e3d8SAppaRao Puli if (entryIsThisConn) 9715e44e3d8SAppaRao Puli { 9725e44e3d8SAppaRao Puli persistent_data::EventServiceStore::getInstance() 9735e44e3d8SAppaRao Puli .subscriptionsConfigMap.erase( 9745e44e3d8SAppaRao Puli it.second->getSubscriptionId()); 9755e44e3d8SAppaRao Puli return; 9765e44e3d8SAppaRao Puli } 9775e44e3d8SAppaRao Puli } 9785e44e3d8SAppaRao Puli } 9795e44e3d8SAppaRao Puli 9805e44e3d8SAppaRao Puli size_t getNumberOfSubscriptions() const 981b52664e2SAppaRao Puli { 982b52664e2SAppaRao Puli return subscriptionsMap.size(); 983b52664e2SAppaRao Puli } 984b52664e2SAppaRao Puli 9855e44e3d8SAppaRao Puli size_t getNumberOfSSESubscriptions() const 9865e44e3d8SAppaRao Puli { 9873544d2a7SEd Tanous auto size = std::ranges::count_if( 9883544d2a7SEd Tanous subscriptionsMap, 9895e44e3d8SAppaRao Puli [](const std::pair<std::string, std::shared_ptr<Subscription>>& 9905e44e3d8SAppaRao Puli entry) { 9915e44e3d8SAppaRao Puli return (entry.second->subscriptionType == subscriptionTypeSSE); 9925e44e3d8SAppaRao Puli }); 9935e44e3d8SAppaRao Puli return static_cast<size_t>(size); 9945e44e3d8SAppaRao Puli } 9955e44e3d8SAppaRao Puli 996b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 997b52664e2SAppaRao Puli { 998b52664e2SAppaRao Puli std::vector<std::string> idList; 999b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 1000b52664e2SAppaRao Puli { 1001b52664e2SAppaRao Puli idList.emplace_back(it.first); 1002b52664e2SAppaRao Puli } 1003b52664e2SAppaRao Puli return idList; 1004b52664e2SAppaRao Puli } 1005b52664e2SAppaRao Puli 10066ba8c82eSsunharis_in bool sendTestEventLog() 10070b4bdd93SAppaRao Puli { 10085e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 10090b4bdd93SAppaRao Puli { 10100b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 10116ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 10126ba8c82eSsunharis_in { 10136ba8c82eSsunharis_in return false; 10140b4bdd93SAppaRao Puli } 10150b4bdd93SAppaRao Puli } 10166ba8c82eSsunharis_in return true; 10176ba8c82eSsunharis_in } 1018e9a14131SAppaRao Puli 1019613dabeaSEd Tanous void sendEvent(nlohmann::json eventMessage, const std::string& origin, 1020613dabeaSEd Tanous const std::string& resType) 102196330b99SSunitha Harish { 1022dbb59d4dSEd Tanous if (!serviceEnabled || (noOfEventLogSubscribers == 0U)) 10236ba8c82eSsunharis_in { 102462598e31SEd Tanous BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions."); 10256ba8c82eSsunharis_in return; 10266ba8c82eSsunharis_in } 102796330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 102896330b99SSunitha Harish 1029613dabeaSEd Tanous eventMessage["EventId"] = eventId; 1030613dabeaSEd Tanous // MemberId is 0 : since we are sending one event record. 1031613dabeaSEd Tanous eventMessage["MemberId"] = 0; 1032613dabeaSEd Tanous eventMessage["EventTimestamp"] = 1033613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1034613dabeaSEd Tanous eventMessage["OriginOfCondition"] = origin; 1035613dabeaSEd Tanous 1036613dabeaSEd Tanous eventRecord.emplace_back(std::move(eventMessage)); 103796330b99SSunitha Harish 10385e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 103996330b99SSunitha Harish { 104096330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 104196330b99SSunitha Harish bool isSubscribed = false; 104296330b99SSunitha Harish // Search the resourceTypes list for the subscription. 104396330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 104496330b99SSunitha Harish // send everything. 104526f6976fSEd Tanous if (!entry->resourceTypes.empty()) 104696330b99SSunitha Harish { 10473174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 104896330b99SSunitha Harish { 104996330b99SSunitha Harish if (resType == resource) 105096330b99SSunitha Harish { 105162598e31SEd Tanous BMCWEB_LOG_INFO( 105262598e31SEd Tanous "ResourceType {} found in the subscribed list", 105362598e31SEd Tanous resource); 105496330b99SSunitha Harish isSubscribed = true; 105596330b99SSunitha Harish break; 105696330b99SSunitha Harish } 105796330b99SSunitha Harish } 105896330b99SSunitha Harish } 105996330b99SSunitha Harish else // resourceTypes list is empty. 106096330b99SSunitha Harish { 106196330b99SSunitha Harish isSubscribed = true; 106296330b99SSunitha Harish } 106396330b99SSunitha Harish if (isSubscribed) 106496330b99SSunitha Harish { 1065613dabeaSEd Tanous nlohmann::json msgJson; 1066613dabeaSEd Tanous 1067613dabeaSEd Tanous msgJson["@odata.type"] = "#Event.v1_4_0.Event"; 1068613dabeaSEd Tanous msgJson["Name"] = "Event Log"; 1069613dabeaSEd Tanous msgJson["Id"] = eventId; 1070613dabeaSEd Tanous msgJson["Events"] = eventRecord; 1071f52c03c1SCarson Labrado 1072f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1073f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 10745e44e3d8SAppaRao Puli entry->sendEvent(std::move(strMsg)); 107596330b99SSunitha Harish eventId++; // increament the eventId 107696330b99SSunitha Harish } 107796330b99SSunitha Harish else 107896330b99SSunitha Harish { 107962598e31SEd Tanous BMCWEB_LOG_INFO("Not subscribed to this resource"); 108096330b99SSunitha Harish } 108196330b99SSunitha Harish } 108296330b99SSunitha Harish } 108396330b99SSunitha Harish 10847f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 10852558979cSP Dheeraj Srujan Kumar 10862558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 10877f4eb588SAppaRao Puli { 10882558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 10892558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 10902558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 10912558979cSP Dheeraj Srujan Kumar } 10922558979cSP Dheeraj Srujan Kumar 10932558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 10942558979cSP Dheeraj Srujan Kumar { 10952558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 10962558979cSP Dheeraj Srujan Kumar 10977f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 10987f4eb588SAppaRao Puli if (!logStream.good()) 10997f4eb588SAppaRao Puli { 110062598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed "); 11017f4eb588SAppaRao Puli return; 11027f4eb588SAppaRao Puli } 11037f4eb588SAppaRao Puli std::string logEntry; 11047f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11057f4eb588SAppaRao Puli { 11062558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11077f4eb588SAppaRao Puli } 11087f4eb588SAppaRao Puli } 11097f4eb588SAppaRao Puli 11107f4eb588SAppaRao Puli void readEventLogsFromFile() 11117f4eb588SAppaRao Puli { 11127f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11137f4eb588SAppaRao Puli if (!logStream.good()) 11147f4eb588SAppaRao Puli { 111562598e31SEd Tanous BMCWEB_LOG_ERROR(" Redfish log file open failed"); 11167f4eb588SAppaRao Puli return; 11177f4eb588SAppaRao Puli } 11187f4eb588SAppaRao Puli 11197f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11207f4eb588SAppaRao Puli 11217f4eb588SAppaRao Puli std::string logEntry; 11222558979cSP Dheeraj Srujan Kumar 11232558979cSP Dheeraj Srujan Kumar // Get the read pointer to the next log to be read. 11242558979cSP Dheeraj Srujan Kumar logStream.seekg(redfishLogFilePosition); 11252558979cSP Dheeraj Srujan Kumar 11267f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11277f4eb588SAppaRao Puli { 11282558979cSP Dheeraj Srujan Kumar // Update Pointer position 11292558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11302558979cSP Dheeraj Srujan Kumar 11312558979cSP Dheeraj Srujan Kumar std::string idStr; 11322558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 11337f4eb588SAppaRao Puli { 11347f4eb588SAppaRao Puli continue; 11357f4eb588SAppaRao Puli } 11367f4eb588SAppaRao Puli 1137e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11387f4eb588SAppaRao Puli { 11392558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 11402558979cSP Dheeraj Srujan Kumar // the remaining items below. 11412558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 11427f4eb588SAppaRao Puli continue; 11437f4eb588SAppaRao Puli } 11447f4eb588SAppaRao Puli 11457f4eb588SAppaRao Puli std::string timestamp; 11467f4eb588SAppaRao Puli std::string messageID; 11475e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11487f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11497f4eb588SAppaRao Puli messageArgs) != 0) 11507f4eb588SAppaRao Puli { 115162598e31SEd Tanous BMCWEB_LOG_DEBUG("Read eventLog entry params failed"); 11527f4eb588SAppaRao Puli continue; 11537f4eb588SAppaRao Puli } 11547f4eb588SAppaRao Puli 11557f4eb588SAppaRao Puli std::string registryName; 11567f4eb588SAppaRao Puli std::string messageKey; 11577f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 11587f4eb588SAppaRao Puli messageKey); 11597f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 11607f4eb588SAppaRao Puli { 11617f4eb588SAppaRao Puli continue; 11627f4eb588SAppaRao Puli } 11637f4eb588SAppaRao Puli 11647f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 11657f4eb588SAppaRao Puli messageKey, messageArgs); 11667f4eb588SAppaRao Puli } 11677f4eb588SAppaRao Puli 1168e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11692558979cSP Dheeraj Srujan Kumar { 117062598e31SEd Tanous BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions."); 11712558979cSP Dheeraj Srujan Kumar return; 11722558979cSP Dheeraj Srujan Kumar } 11732558979cSP Dheeraj Srujan Kumar 11742558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 11752558979cSP Dheeraj Srujan Kumar { 11762558979cSP Dheeraj Srujan Kumar // No Records to send 117762598e31SEd Tanous BMCWEB_LOG_DEBUG("No log entries available to be transferred."); 11782558979cSP Dheeraj Srujan Kumar return; 11792558979cSP Dheeraj Srujan Kumar } 11802558979cSP Dheeraj Srujan Kumar 11815e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 11827f4eb588SAppaRao Puli { 11837f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 11847f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 11857f4eb588SAppaRao Puli { 11867f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 11877f4eb588SAppaRao Puli } 11887f4eb588SAppaRao Puli } 11897f4eb588SAppaRao Puli } 11907f4eb588SAppaRao Puli 11917f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 11927f4eb588SAppaRao Puli { 11936a9f85f9SAppaRao Puli if (!inotifyConn) 11947f4eb588SAppaRao Puli { 11957f4eb588SAppaRao Puli return; 11967f4eb588SAppaRao Puli } 11977f4eb588SAppaRao Puli 11987f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 11997f4eb588SAppaRao Puli 1200002d39b4SEd Tanous inotifyConn->async_read_some(boost::asio::buffer(readBuffer), 12017f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 12027f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 12037f4eb588SAppaRao Puli if (ec) 12047f4eb588SAppaRao Puli { 120562598e31SEd Tanous BMCWEB_LOG_ERROR("Callback Error: {}", ec.message()); 12067f4eb588SAppaRao Puli return; 12077f4eb588SAppaRao Puli } 12087f4eb588SAppaRao Puli std::size_t index = 0; 1209b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 12107f4eb588SAppaRao Puli { 1211d3a9e084SEd Tanous struct inotify_event event 1212d3a9e084SEd Tanous {}; 1213b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1214b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1215b792cc56SAppaRao Puli { 1216b792cc56SAppaRao Puli if ((event.len == 0) || 1217b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1218b792cc56SAppaRao Puli { 1219b792cc56SAppaRao Puli index += (iEventSize + event.len); 1220b792cc56SAppaRao Puli continue; 1221b792cc56SAppaRao Puli } 1222b792cc56SAppaRao Puli 12234f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 12244f568f74SJiaqing Zhao if (fileName != "redfish") 1225b792cc56SAppaRao Puli { 1226b792cc56SAppaRao Puli index += (iEventSize + event.len); 1227b792cc56SAppaRao Puli continue; 1228b792cc56SAppaRao Puli } 1229b792cc56SAppaRao Puli 123062598e31SEd Tanous BMCWEB_LOG_DEBUG( 123162598e31SEd Tanous "Redfish log file created/deleted. event.name: {}", 123262598e31SEd Tanous fileName); 1233b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1234b792cc56SAppaRao Puli { 1235b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1236b792cc56SAppaRao Puli { 123762598e31SEd Tanous BMCWEB_LOG_DEBUG( 123862598e31SEd Tanous "Remove and Add inotify watcher on " 123962598e31SEd Tanous "redfish event log file"); 1240016761afSAppaRao Puli // Remove existing inotify watcher and add 1241016761afSAppaRao Puli // with new redfish event log file. 1242016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1243016761afSAppaRao Puli fileWatchDesc = -1; 1244b792cc56SAppaRao Puli } 1245b792cc56SAppaRao Puli 1246b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1247b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1248b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1249b792cc56SAppaRao Puli { 125062598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for " 125162598e31SEd Tanous "redfish log file."); 1252b792cc56SAppaRao Puli return; 1253b792cc56SAppaRao Puli } 1254b792cc56SAppaRao Puli 1255b792cc56SAppaRao Puli EventServiceManager::getInstance() 12562558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1257b792cc56SAppaRao Puli EventServiceManager::getInstance() 1258b792cc56SAppaRao Puli .readEventLogsFromFile(); 1259b792cc56SAppaRao Puli } 1260b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1261b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1262b792cc56SAppaRao Puli { 1263b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1264b792cc56SAppaRao Puli { 1265b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1266b792cc56SAppaRao Puli fileWatchDesc = -1; 1267b792cc56SAppaRao Puli } 1268b792cc56SAppaRao Puli } 1269b792cc56SAppaRao Puli } 1270b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1271b792cc56SAppaRao Puli { 1272b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12737f4eb588SAppaRao Puli { 12747f4eb588SAppaRao Puli EventServiceManager::getInstance() 12757f4eb588SAppaRao Puli .readEventLogsFromFile(); 12767f4eb588SAppaRao Puli } 1277b792cc56SAppaRao Puli } 1278b792cc56SAppaRao Puli index += (iEventSize + event.len); 12797f4eb588SAppaRao Puli } 12807f4eb588SAppaRao Puli 12817f4eb588SAppaRao Puli watchRedfishEventLogFile(); 12827f4eb588SAppaRao Puli }); 12837f4eb588SAppaRao Puli } 12847f4eb588SAppaRao Puli 12857f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 12867f4eb588SAppaRao Puli { 128723a21a1cSEd Tanous inotifyConn.emplace(ioc); 1288b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1289b792cc56SAppaRao Puli if (inotifyFd == -1) 12907f4eb588SAppaRao Puli { 129162598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_init1 failed."); 12927f4eb588SAppaRao Puli return -1; 12937f4eb588SAppaRao Puli } 1294b792cc56SAppaRao Puli 1295b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1296b792cc56SAppaRao Puli // create/delete. 1297b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1298b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1299b792cc56SAppaRao Puli if (dirWatchDesc == -1) 13007f4eb588SAppaRao Puli { 130162598e31SEd Tanous BMCWEB_LOG_ERROR( 130262598e31SEd Tanous "inotify_add_watch failed for event log directory."); 13037f4eb588SAppaRao Puli return -1; 13047f4eb588SAppaRao Puli } 13057f4eb588SAppaRao Puli 1306b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 130789492a15SPatrick Williams fileWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogFile, 130889492a15SPatrick Williams IN_MODIFY); 1309b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1310b792cc56SAppaRao Puli { 131162598e31SEd Tanous BMCWEB_LOG_ERROR("inotify_add_watch failed for redfish log file."); 1312b792cc56SAppaRao Puli // Don't return error if file not exist. 1313b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1314b792cc56SAppaRao Puli } 1315b792cc56SAppaRao Puli 13167f4eb588SAppaRao Puli // monitor redfish event log file 1317b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13187f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13197f4eb588SAppaRao Puli 13207f4eb588SAppaRao Puli return 0; 13217f4eb588SAppaRao Puli } 13227f4eb588SAppaRao Puli 13237f4eb588SAppaRao Puli #endif 132459d494eeSPatrick Williams static void getReadingsForReport(sdbusplus::message_t& msg) 1325156d6b00SAppaRao Puli { 132656d2396dSEd Tanous if (msg.is_method_error()) 132756d2396dSEd Tanous { 132862598e31SEd Tanous BMCWEB_LOG_ERROR("TelemetryMonitor Signal error"); 132956d2396dSEd Tanous return; 133056d2396dSEd Tanous } 133156d2396dSEd Tanous 1332c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1333c0353249SWludzik, Jozef std::string id = path.filename(); 1334c0353249SWludzik, Jozef if (id.empty()) 1335156d6b00SAppaRao Puli { 133662598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to get Id from path"); 1337156d6b00SAppaRao Puli return; 1338156d6b00SAppaRao Puli } 1339156d6b00SAppaRao Puli 1340c0353249SWludzik, Jozef std::string interface; 1341b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1342c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1343c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1344c0353249SWludzik, Jozef 13453544d2a7SEd Tanous auto found = std::ranges::find_if( 13463544d2a7SEd Tanous props, [](const auto& x) { return x.first == "Readings"; }); 1347c0353249SWludzik, Jozef if (found == props.end()) 1348156d6b00SAppaRao Puli { 134962598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 1350156d6b00SAppaRao Puli return; 1351156d6b00SAppaRao Puli } 1352156d6b00SAppaRao Puli 13531e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 13541e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1355e662eae8SEd Tanous if (readings == nullptr) 13561e1e598dSJonathan Doman { 135762598e31SEd Tanous BMCWEB_LOG_INFO("Failed to get Readings from Report properties"); 13581e1e598dSJonathan Doman return; 13591e1e598dSJonathan Doman } 13601e1e598dSJonathan Doman 1361156d6b00SAppaRao Puli for (const auto& it : 1362156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1363156d6b00SAppaRao Puli { 1364e05aec50SEd Tanous Subscription& entry = *it.second; 1365c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1366156d6b00SAppaRao Puli { 13671e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1368156d6b00SAppaRao Puli } 1369156d6b00SAppaRao Puli } 1370156d6b00SAppaRao Puli } 1371156d6b00SAppaRao Puli 1372156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1373156d6b00SAppaRao Puli { 13747d1cc387SAppaRao Puli if (matchTelemetryMonitor) 13757d1cc387SAppaRao Puli { 137662598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Unregister"); 1377156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1378156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1379156d6b00SAppaRao Puli } 13807d1cc387SAppaRao Puli } 1381156d6b00SAppaRao Puli 1382156d6b00SAppaRao Puli void registerMetricReportSignal() 1383156d6b00SAppaRao Puli { 13847d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1385156d6b00SAppaRao Puli { 138662598e31SEd Tanous BMCWEB_LOG_DEBUG("Not registering metric report signal."); 1387156d6b00SAppaRao Puli return; 1388156d6b00SAppaRao Puli } 1389156d6b00SAppaRao Puli 139062598e31SEd Tanous BMCWEB_LOG_DEBUG("Metrics report signal - Register"); 1391c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1392c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1393c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1394156d6b00SAppaRao Puli 139559d494eeSPatrick Williams matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>( 139656d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1397156d6b00SAppaRao Puli } 139823a21a1cSEd Tanous }; 1399b52664e2SAppaRao Puli 1400b52664e2SAppaRao Puli } // namespace redfish 1401