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" 223ccb3adbSEd Tanous #include "persistent_data.hpp" 233ccb3adbSEd Tanous #include "random.hpp" 247f4eb588SAppaRao Puli #include "registries.hpp" 257f4eb588SAppaRao Puli #include "registries/base_message_registry.hpp" 267f4eb588SAppaRao Puli #include "registries/openbmc_message_registry.hpp" 27b304bd79SP Dheeraj Srujan Kumar #include "registries/task_event_message_registry.hpp" 283ccb3adbSEd Tanous #include "server_sent_events.hpp" 2950ebd4afSEd Tanous #include "str_utility.hpp" 3077665bdaSNan Zhou #include "utility.hpp" 313ccb3adbSEd Tanous #include "utils/json_utils.hpp" 327f4eb588SAppaRao Puli 337f4eb588SAppaRao Puli #include <sys/inotify.h> 34b52664e2SAppaRao Puli 3511ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 36fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 37b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 38ef4c65b7SEd Tanous #include <boost/url/format.hpp> 39b5b40605Snitroglycerine #include <sdbusplus/bus/match.hpp> 401214b7e7SGunnar Mills 41b52664e2SAppaRao Puli #include <cstdlib> 42b52664e2SAppaRao Puli #include <ctime> 431bf712bcSAyushi Smriti #include <fstream> 44b52664e2SAppaRao Puli #include <memory> 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 561bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 571bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 581bf712bcSAyushi Smriti 59fffb8c1fSEd Tanous namespace registries 607f4eb588SAppaRao Puli { 6126702d01SEd Tanous inline std::span<const MessageEntry> 62b304bd79SP Dheeraj Srujan Kumar getRegistryFromPrefix(const std::string& registryName) 63b304bd79SP Dheeraj Srujan Kumar { 64b304bd79SP Dheeraj Srujan Kumar if (task_event::header.registryPrefix == registryName) 65b304bd79SP Dheeraj Srujan Kumar { 6626702d01SEd Tanous return {task_event::registry}; 67b304bd79SP Dheeraj Srujan Kumar } 68b304bd79SP Dheeraj Srujan Kumar if (openbmc::header.registryPrefix == registryName) 69b304bd79SP Dheeraj Srujan Kumar { 7026702d01SEd Tanous return {openbmc::registry}; 71b304bd79SP Dheeraj Srujan Kumar } 72b304bd79SP Dheeraj Srujan Kumar if (base::header.registryPrefix == registryName) 73b304bd79SP Dheeraj Srujan Kumar { 7426702d01SEd Tanous return {base::registry}; 75b304bd79SP Dheeraj Srujan Kumar } 7626702d01SEd Tanous return {openbmc::registry}; 77b304bd79SP Dheeraj Srujan Kumar } 78fffb8c1fSEd Tanous } // namespace registries 79b304bd79SP Dheeraj Srujan Kumar 804642bf8fSGeorge Liu #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 81cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 824642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 834642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 844642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 854642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 86cf9e417dSEd Tanous 87cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 884642bf8fSGeorge Liu static int inotifyFd = -1; 89cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 904642bf8fSGeorge Liu static int dirWatchDesc = -1; 91cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 924642bf8fSGeorge Liu static int fileWatchDesc = -1; 934642bf8fSGeorge Liu 944642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 954642bf8fSGeorge Liu using EventLogObjectsType = 964642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 974642bf8fSGeorge Liu std::vector<std::string>>; 984642bf8fSGeorge Liu 99fffb8c1fSEd Tanous namespace registries 1004642bf8fSGeorge Liu { 1017f4eb588SAppaRao Puli static const Message* 1027f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 10326702d01SEd Tanous const std::span<const MessageEntry>& registry) 1047f4eb588SAppaRao Puli { 10526702d01SEd Tanous std::span<const MessageEntry>::iterator messageIt = 10626702d01SEd Tanous std::find_if(registry.begin(), registry.end(), 1077f4eb588SAppaRao Puli [&messageKey](const MessageEntry& messageEntry) { 10855f79e6fSEd Tanous return messageKey == messageEntry.first; 1097f4eb588SAppaRao Puli }); 11026702d01SEd Tanous if (messageIt != registry.end()) 1117f4eb588SAppaRao Puli { 1127f4eb588SAppaRao Puli return &messageIt->second; 1137f4eb588SAppaRao Puli } 1147f4eb588SAppaRao Puli 1157f4eb588SAppaRao Puli return nullptr; 1167f4eb588SAppaRao Puli } 1177f4eb588SAppaRao Puli 11826ccae32SEd Tanous static const Message* formatMessage(std::string_view messageID) 1197f4eb588SAppaRao Puli { 1207f4eb588SAppaRao Puli // Redfish MessageIds are in the form 1217f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1227f4eb588SAppaRao Puli // the right Message 1237f4eb588SAppaRao Puli std::vector<std::string> fields; 1247f4eb588SAppaRao Puli fields.reserve(4); 12550ebd4afSEd Tanous 12650ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 1277f4eb588SAppaRao Puli if (fields.size() != 4) 1287f4eb588SAppaRao Puli { 1297f4eb588SAppaRao Puli return nullptr; 1307f4eb588SAppaRao Puli } 13102cad96eSEd Tanous const std::string& registryName = fields[0]; 13202cad96eSEd Tanous const std::string& messageKey = fields[3]; 1337f4eb588SAppaRao Puli 1347f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 135b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1367f4eb588SAppaRao Puli } 137fffb8c1fSEd Tanous } // namespace registries 1387f4eb588SAppaRao Puli 1397f4eb588SAppaRao Puli namespace event_log 1407f4eb588SAppaRao Puli { 1412558979cSP Dheeraj Srujan Kumar inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 1427f4eb588SAppaRao Puli { 1437f4eb588SAppaRao Puli static time_t prevTs = 0; 1447f4eb588SAppaRao Puli static int index = 0; 1457f4eb588SAppaRao Puli 1467f4eb588SAppaRao Puli // Get the entry timestamp 1477f4eb588SAppaRao Puli std::time_t curTs = 0; 1487f4eb588SAppaRao Puli std::tm timeStruct = {}; 1497f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1507f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1517f4eb588SAppaRao Puli { 1527f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1537f4eb588SAppaRao Puli if (curTs == -1) 1547f4eb588SAppaRao Puli { 1557f4eb588SAppaRao Puli return false; 1567f4eb588SAppaRao Puli } 1577f4eb588SAppaRao Puli } 1587f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1597f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1607f4eb588SAppaRao Puli 1617f4eb588SAppaRao Puli // Save the timestamp 1627f4eb588SAppaRao Puli prevTs = curTs; 1637f4eb588SAppaRao Puli 1647f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1657f4eb588SAppaRao Puli if (index > 0) 1667f4eb588SAppaRao Puli { 1677f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1687f4eb588SAppaRao Puli } 1697f4eb588SAppaRao Puli return true; 1707f4eb588SAppaRao Puli } 1717f4eb588SAppaRao Puli 17223a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 17323a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1745e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1757f4eb588SAppaRao Puli { 1767f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1777f4eb588SAppaRao Puli // First get the Timestamp 178f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1797f4eb588SAppaRao Puli if (space == std::string::npos) 1807f4eb588SAppaRao Puli { 1817f4eb588SAppaRao Puli return -EINVAL; 1827f4eb588SAppaRao Puli } 1837f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1847f4eb588SAppaRao Puli // Then get the log contents 185f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1867f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1877f4eb588SAppaRao Puli { 1887f4eb588SAppaRao Puli return -EINVAL; 1897f4eb588SAppaRao Puli } 1907f4eb588SAppaRao Puli std::string_view entry(logEntry); 1917f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1927f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1937f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 19450ebd4afSEd Tanous bmcweb::split(logEntryFields, entry, ','); 1957f4eb588SAppaRao Puli // We need at least a MessageId to be valid 19626f6976fSEd Tanous if (logEntryFields.empty()) 1977f4eb588SAppaRao Puli { 1987f4eb588SAppaRao Puli return -EINVAL; 1997f4eb588SAppaRao Puli } 2007f4eb588SAppaRao Puli messageID = logEntryFields[0]; 2017f4eb588SAppaRao Puli 2027f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 2037f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 2047f4eb588SAppaRao Puli { 20502cad96eSEd Tanous const std::string& messageArgsStart = logEntryFields[1]; 2067f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 2077f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 2087f4eb588SAppaRao Puli { 2095e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 2105e715de6SAppaRao Puli logEntryFields.end()); 2117f4eb588SAppaRao Puli } 2127f4eb588SAppaRao Puli } 2137f4eb588SAppaRao Puli 2147f4eb588SAppaRao Puli return 0; 2157f4eb588SAppaRao Puli } 2167f4eb588SAppaRao Puli 21723a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 2187f4eb588SAppaRao Puli std::string& registryName, 2197f4eb588SAppaRao Puli std::string& messageKey) 2207f4eb588SAppaRao Puli { 2217f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2227f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2237f4eb588SAppaRao Puli // the right Message 2247f4eb588SAppaRao Puli std::vector<std::string> fields; 2257f4eb588SAppaRao Puli fields.reserve(4); 22650ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 2277f4eb588SAppaRao Puli if (fields.size() == 4) 2287f4eb588SAppaRao Puli { 2297f4eb588SAppaRao Puli registryName = fields[0]; 2307f4eb588SAppaRao Puli messageKey = fields[3]; 2317f4eb588SAppaRao Puli } 2327f4eb588SAppaRao Puli } 2337f4eb588SAppaRao Puli 23423a21a1cSEd Tanous inline int formatEventLogEntry(const std::string& logEntryID, 2357f4eb588SAppaRao Puli const std::string& messageID, 236c5ba4c27SEd Tanous const std::span<std::string_view> messageArgs, 23723a21a1cSEd Tanous std::string timestamp, 238b5a76932SEd Tanous const std::string& customText, 2397f4eb588SAppaRao Puli nlohmann::json& logEntryJson) 2407f4eb588SAppaRao Puli { 2417f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 242fffb8c1fSEd Tanous const registries::Message* message = registries::formatMessage(messageID); 2437f4eb588SAppaRao Puli 24480f595e7SEd Tanous if (message == nullptr) 2457f4eb588SAppaRao Puli { 24680f595e7SEd Tanous return -1; 2477f4eb588SAppaRao Puli } 2487f4eb588SAppaRao Puli 24989492a15SPatrick Williams std::string msg = redfish::registries::fillMessageArgs(messageArgs, 25089492a15SPatrick Williams message->message); 25180f595e7SEd Tanous if (msg.empty()) 25280f595e7SEd Tanous { 25380f595e7SEd Tanous return -1; 25480f595e7SEd Tanous } 2557f4eb588SAppaRao Puli 2567f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2577f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2587f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 259f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 260b2f7609bSEd Tanous std::size_t plus = timestamp.find_first_of('+', dot); 2617f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2627f4eb588SAppaRao Puli { 2637f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2647f4eb588SAppaRao Puli } 2657f4eb588SAppaRao Puli 2667f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2671476687dSEd Tanous logEntryJson["EventId"] = logEntryID; 2681476687dSEd Tanous logEntryJson["EventType"] = "Event"; 26980f595e7SEd Tanous logEntryJson["Severity"] = message->messageSeverity; 2701476687dSEd Tanous logEntryJson["Message"] = std::move(msg); 2711476687dSEd Tanous logEntryJson["MessageId"] = messageID; 2721476687dSEd Tanous logEntryJson["MessageArgs"] = messageArgs; 2731476687dSEd Tanous logEntryJson["EventTimestamp"] = std::move(timestamp); 2741476687dSEd Tanous logEntryJson["Context"] = customText; 2757f4eb588SAppaRao Puli return 0; 2767f4eb588SAppaRao Puli } 2777f4eb588SAppaRao Puli 2787f4eb588SAppaRao Puli } // namespace event_log 2797f4eb588SAppaRao Puli #endif 2807f4eb588SAppaRao Puli 28123a21a1cSEd Tanous inline bool isFilterQuerySpecialChar(char c) 28207941a88SAyushi Smriti { 28307941a88SAyushi Smriti switch (c) 28407941a88SAyushi Smriti { 28507941a88SAyushi Smriti case '(': 28607941a88SAyushi Smriti case ')': 28707941a88SAyushi Smriti case '\'': 28807941a88SAyushi Smriti return true; 28907941a88SAyushi Smriti default: 29007941a88SAyushi Smriti return false; 29107941a88SAyushi Smriti } 29207941a88SAyushi Smriti } 29307941a88SAyushi Smriti 29423a21a1cSEd Tanous inline bool 29523a21a1cSEd Tanous readSSEQueryParams(std::string sseFilter, std::string& formatType, 29607941a88SAyushi Smriti std::vector<std::string>& messageIds, 29707941a88SAyushi Smriti std::vector<std::string>& registryPrefixes, 298144b6318SAppaRao Puli std::vector<std::string>& metricReportDefinitions) 29907941a88SAyushi Smriti { 30007941a88SAyushi Smriti sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(), 30107941a88SAyushi Smriti isFilterQuerySpecialChar), 30207941a88SAyushi Smriti sseFilter.end()); 30307941a88SAyushi Smriti 30407941a88SAyushi Smriti std::vector<std::string> result; 30550ebd4afSEd Tanous 30650ebd4afSEd Tanous // NOLINTNEXTLINE 30750ebd4afSEd Tanous bmcweb::split(result, sseFilter, ' '); 30807941a88SAyushi Smriti 30907941a88SAyushi Smriti BMCWEB_LOG_DEBUG << "No of tokens in SEE query: " << result.size(); 31007941a88SAyushi Smriti 31107941a88SAyushi Smriti constexpr uint8_t divisor = 4; 31207941a88SAyushi Smriti constexpr uint8_t minTokenSize = 3; 31307941a88SAyushi Smriti if (result.size() % divisor != minTokenSize) 31407941a88SAyushi Smriti { 31507941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid SSE filter specified."; 31607941a88SAyushi Smriti return false; 31707941a88SAyushi Smriti } 31807941a88SAyushi Smriti 31907941a88SAyushi Smriti for (std::size_t i = 0; i < result.size(); i += divisor) 32007941a88SAyushi Smriti { 32102cad96eSEd Tanous const std::string& key = result[i]; 32202cad96eSEd Tanous const std::string& op = result[i + 1]; 32302cad96eSEd Tanous const std::string& value = result[i + 2]; 32407941a88SAyushi Smriti 32507941a88SAyushi Smriti if ((i + minTokenSize) < result.size()) 32607941a88SAyushi Smriti { 32702cad96eSEd Tanous const std::string& separator = result[i + minTokenSize]; 32807941a88SAyushi Smriti // SSE supports only "or" and "and" in query params. 3294e0453b1SGunnar Mills if ((separator != "or") && (separator != "and")) 33007941a88SAyushi Smriti { 33107941a88SAyushi Smriti BMCWEB_LOG_ERROR 33207941a88SAyushi Smriti << "Invalid group operator in SSE query parameters"; 33307941a88SAyushi Smriti return false; 33407941a88SAyushi Smriti } 33507941a88SAyushi Smriti } 33607941a88SAyushi Smriti 33707941a88SAyushi Smriti // SSE supports only "eq" as per spec. 33807941a88SAyushi Smriti if (op != "eq") 33907941a88SAyushi Smriti { 34007941a88SAyushi Smriti BMCWEB_LOG_ERROR 34107941a88SAyushi Smriti << "Invalid assignment operator in SSE query parameters"; 34207941a88SAyushi Smriti return false; 34307941a88SAyushi Smriti } 34407941a88SAyushi Smriti 34507941a88SAyushi Smriti BMCWEB_LOG_DEBUG << key << " : " << value; 34607941a88SAyushi Smriti if (key == "EventFormatType") 34707941a88SAyushi Smriti { 34807941a88SAyushi Smriti formatType = value; 34907941a88SAyushi Smriti } 35007941a88SAyushi Smriti else if (key == "MessageId") 35107941a88SAyushi Smriti { 35207941a88SAyushi Smriti messageIds.push_back(value); 35307941a88SAyushi Smriti } 35407941a88SAyushi Smriti else if (key == "RegistryPrefix") 35507941a88SAyushi Smriti { 35607941a88SAyushi Smriti registryPrefixes.push_back(value); 35707941a88SAyushi Smriti } 35807941a88SAyushi Smriti else if (key == "MetricReportDefinition") 35907941a88SAyushi Smriti { 36007941a88SAyushi Smriti metricReportDefinitions.push_back(value); 36107941a88SAyushi Smriti } 36207941a88SAyushi Smriti else 36307941a88SAyushi Smriti { 36407941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid property(" << key 36507941a88SAyushi Smriti << ")in SSE filter query."; 36607941a88SAyushi Smriti return false; 36707941a88SAyushi Smriti } 36807941a88SAyushi Smriti } 36907941a88SAyushi Smriti return true; 37007941a88SAyushi Smriti } 37107941a88SAyushi Smriti 37228afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 373b52664e2SAppaRao Puli { 374b52664e2SAppaRao Puli public: 375b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 376b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 377b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 378b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 379b52664e2SAppaRao Puli 380eb1c47d3SEd Tanous Subscription(const std::string& inHost, uint16_t inPort, 381*f8ca6d79SEd Tanous const std::string& inPath, const std::string& inUriProto, 382*f8ca6d79SEd Tanous boost::asio::io_context& ioc) : 383d14a48ffSCarson Labrado host(inHost), 384d14a48ffSCarson Labrado port(inPort), policy(std::make_shared<crow::ConnectionPolicy>()), 385*f8ca6d79SEd Tanous client(ioc, policy), path(inPath), uriProto(inUriProto) 386b52664e2SAppaRao Puli { 3877adb85acSSunitha Harish // Subscription constructor 388d14a48ffSCarson Labrado policy->invalidResp = retryRespHandler; 389b52664e2SAppaRao Puli } 3904bbf237fSAppaRao Puli 3919f616dd1SEd Tanous ~Subscription() = default; 392b52664e2SAppaRao Puli 393f52c03c1SCarson Labrado bool sendEvent(std::string& msg) 394b52664e2SAppaRao Puli { 3956ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 3966ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 3976ba8c82eSsunharis_in .getEventServiceConfig(); 3986ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 3996ba8c82eSsunharis_in { 4006ba8c82eSsunharis_in return false; 4016ba8c82eSsunharis_in } 4026ba8c82eSsunharis_in 403e38778a5SAppaRao Puli bool useSSL = (uriProto == "https"); 404f52c03c1SCarson Labrado // A connection pool will be created if one does not already exist 405d14a48ffSCarson Labrado client.sendData(msg, host, port, path, useSSL, httpHeaders, 406d14a48ffSCarson Labrado boost::beast::http::verb::post); 4077adb85acSSunitha Harish eventSeqNum++; 4087adb85acSSunitha Harish 4094bbf237fSAppaRao Puli if (sseConn != nullptr) 4104bbf237fSAppaRao Puli { 4114bbf237fSAppaRao Puli sseConn->sendData(eventSeqNum, msg); 4124bbf237fSAppaRao Puli } 4136ba8c82eSsunharis_in return true; 4144bbf237fSAppaRao Puli } 4154bbf237fSAppaRao Puli 4166ba8c82eSsunharis_in bool sendTestEventLog() 4170b4bdd93SAppaRao Puli { 4180b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 4190b4bdd93SAppaRao Puli logEntryArray.push_back({}); 4200b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 4210b4bdd93SAppaRao Puli 422613dabeaSEd Tanous logEntryJson["EventId"] = "TestID"; 423613dabeaSEd Tanous logEntryJson["EventType"] = "Event"; 424613dabeaSEd Tanous logEntryJson["Severity"] = "OK"; 425613dabeaSEd Tanous logEntryJson["Message"] = "Generated test event"; 426613dabeaSEd Tanous logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog"; 427613dabeaSEd Tanous logEntryJson["MessageArgs"] = nlohmann::json::array(); 428613dabeaSEd Tanous logEntryJson["EventTimestamp"] = 429613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 430613dabeaSEd Tanous logEntryJson["Context"] = customText; 4310b4bdd93SAppaRao Puli 4321476687dSEd Tanous nlohmann::json msg; 4331476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4341476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4351476687dSEd Tanous msg["Name"] = "Event Log"; 4361476687dSEd Tanous msg["Events"] = logEntryArray; 4370b4bdd93SAppaRao Puli 43889492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 43989492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 440f52c03c1SCarson Labrado return this->sendEvent(strMsg); 4410b4bdd93SAppaRao Puli } 4420b4bdd93SAppaRao Puli 4437f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 4447f4eb588SAppaRao Puli void filterAndSendEventLogs( 4457f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4467f4eb588SAppaRao Puli { 4477f4eb588SAppaRao Puli nlohmann::json logEntryArray; 4487f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4497f4eb588SAppaRao Puli { 4507f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 4517f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 4527f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 4537f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 4547f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 4555e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 4567f4eb588SAppaRao Puli 4577f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 4587f4eb588SAppaRao Puli // send everything. 45926f6976fSEd Tanous if (!registryPrefixes.empty()) 4607f4eb588SAppaRao Puli { 4617f4eb588SAppaRao Puli auto obj = std::find(registryPrefixes.begin(), 4627f4eb588SAppaRao Puli registryPrefixes.end(), registryName); 4637f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 4647f4eb588SAppaRao Puli { 4657f4eb588SAppaRao Puli continue; 4667f4eb588SAppaRao Puli } 4677f4eb588SAppaRao Puli } 4687f4eb588SAppaRao Puli 4697f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 4707f4eb588SAppaRao Puli // send everything. 47126f6976fSEd Tanous if (!registryMsgIds.empty()) 4727f4eb588SAppaRao Puli { 4737f4eb588SAppaRao Puli auto obj = std::find(registryMsgIds.begin(), 4747f4eb588SAppaRao Puli registryMsgIds.end(), messageKey); 4757f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 4767f4eb588SAppaRao Puli { 4777f4eb588SAppaRao Puli continue; 4787f4eb588SAppaRao Puli } 4797f4eb588SAppaRao Puli } 4807f4eb588SAppaRao Puli 481c5ba4c27SEd Tanous std::vector<std::string_view> messageArgsView(messageArgs.begin(), 482c5ba4c27SEd Tanous messageArgs.end()); 483c5ba4c27SEd Tanous 4847f4eb588SAppaRao Puli logEntryArray.push_back({}); 4857f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 486c5ba4c27SEd Tanous if (event_log::formatEventLogEntry(idStr, messageID, 487c5ba4c27SEd Tanous messageArgsView, timestamp, 488c5ba4c27SEd Tanous customText, bmcLogEntry) != 0) 4897f4eb588SAppaRao Puli { 4907f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 4917f4eb588SAppaRao Puli continue; 4927f4eb588SAppaRao Puli } 4937f4eb588SAppaRao Puli } 4947f4eb588SAppaRao Puli 49526f6976fSEd Tanous if (logEntryArray.empty()) 4967f4eb588SAppaRao Puli { 4977f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 4987f4eb588SAppaRao Puli return; 4997f4eb588SAppaRao Puli } 5007f4eb588SAppaRao Puli 5011476687dSEd Tanous nlohmann::json msg; 5021476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 5031476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 5041476687dSEd Tanous msg["Name"] = "Event Log"; 5051476687dSEd Tanous msg["Events"] = logEntryArray; 5067f4eb588SAppaRao Puli 50789492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 50889492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 509f52c03c1SCarson Labrado this->sendEvent(strMsg); 5107f4eb588SAppaRao Puli } 5117f4eb588SAppaRao Puli #endif 5127f4eb588SAppaRao Puli 513248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 5141e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 515156d6b00SAppaRao Puli { 516ef4c65b7SEd Tanous boost::urls::url mrdUri = boost::urls::format( 517ef4c65b7SEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", 518ef4c65b7SEd Tanous reportId); 519156d6b00SAppaRao Puli 520156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 52126f6976fSEd Tanous if (!metricReportDefinitions.empty()) 522156d6b00SAppaRao Puli { 523156d6b00SAppaRao Puli if (std::find(metricReportDefinitions.begin(), 524156d6b00SAppaRao Puli metricReportDefinitions.end(), 525079360aeSEd Tanous mrdUri.buffer()) == metricReportDefinitions.end()) 526156d6b00SAppaRao Puli { 527156d6b00SAppaRao Puli return; 528156d6b00SAppaRao Puli } 529156d6b00SAppaRao Puli } 530156d6b00SAppaRao Puli 531c0353249SWludzik, Jozef nlohmann::json msg; 532248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 533156d6b00SAppaRao Puli { 534c0353249SWludzik, Jozef BMCWEB_LOG_ERROR << "Failed to fill the MetricReport for DBus " 535c0353249SWludzik, Jozef "Report with id " 536248d0230SEd Tanous << reportId; 537c0353249SWludzik, Jozef return; 538156d6b00SAppaRao Puli } 539156d6b00SAppaRao Puli 54022daffd7SAppaRao Puli // Context is set by user during Event subscription and it must be 54122daffd7SAppaRao Puli // set for MetricReport response. 54222daffd7SAppaRao Puli if (!customText.empty()) 54322daffd7SAppaRao Puli { 54422daffd7SAppaRao Puli msg["Context"] = customText; 54522daffd7SAppaRao Puli } 54622daffd7SAppaRao Puli 54789492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 54889492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 549f52c03c1SCarson Labrado this->sendEvent(strMsg); 550156d6b00SAppaRao Puli } 551156d6b00SAppaRao Puli 552d14a48ffSCarson Labrado void updateRetryConfig(uint32_t retryAttempts, 553d14a48ffSCarson Labrado uint32_t retryTimeoutInterval) 554fe44eb0bSAyushi Smriti { 555d14a48ffSCarson Labrado policy->maxRetryAttempts = retryAttempts; 556d14a48ffSCarson Labrado policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval); 55762de0c68SAppaRao Puli } 558fe44eb0bSAyushi Smriti 5599eb808c1SEd Tanous uint64_t getEventSeqNum() const 56096330b99SSunitha Harish { 56196330b99SSunitha Harish return eventSeqNum; 56296330b99SSunitha Harish } 56396330b99SSunitha Harish 564b52664e2SAppaRao Puli private: 565d14a48ffSCarson Labrado uint64_t eventSeqNum = 1; 566b52664e2SAppaRao Puli std::string host; 567eb1c47d3SEd Tanous uint16_t port = 0; 568d14a48ffSCarson Labrado std::shared_ptr<crow::ConnectionPolicy> policy; 569d14a48ffSCarson Labrado crow::HttpClient client; 570b52664e2SAppaRao Puli std::string path; 571b52664e2SAppaRao Puli std::string uriProto; 5724bbf237fSAppaRao Puli std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr; 573a7a80296SCarson Labrado 574a7a80296SCarson Labrado // Check used to indicate what response codes are valid as part of our retry 575a7a80296SCarson Labrado // policy. 2XX is considered acceptable 576a7a80296SCarson Labrado static boost::system::error_code retryRespHandler(unsigned int respCode) 577a7a80296SCarson Labrado { 578a7a80296SCarson Labrado BMCWEB_LOG_DEBUG 579a7a80296SCarson Labrado << "Checking response code validity for SubscriptionEvent"; 580a7a80296SCarson Labrado if ((respCode < 200) || (respCode >= 300)) 581a7a80296SCarson Labrado { 582a7a80296SCarson Labrado return boost::system::errc::make_error_code( 583a7a80296SCarson Labrado boost::system::errc::result_out_of_range); 584a7a80296SCarson Labrado } 585a7a80296SCarson Labrado 586a7a80296SCarson Labrado // Return 0 if the response code is valid 587a7a80296SCarson Labrado return boost::system::errc::make_error_code( 588a7a80296SCarson Labrado boost::system::errc::success); 5899fa6d147SNan Zhou } 590b52664e2SAppaRao Puli }; 591b52664e2SAppaRao Puli 592b52664e2SAppaRao Puli class EventServiceManager 593b52664e2SAppaRao Puli { 594b52664e2SAppaRao Puli private: 595d3a9e084SEd Tanous bool serviceEnabled = false; 596d3a9e084SEd Tanous uint32_t retryAttempts = 0; 597d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 5987d1cc387SAppaRao Puli 5992558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 6009f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 6019f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 60259d494eeSPatrick Williams std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor; 603b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 604b52664e2SAppaRao Puli subscriptionsMap; 605b52664e2SAppaRao Puli 6069f616dd1SEd Tanous uint64_t eventId{1}; 60796330b99SSunitha Harish 608*f8ca6d79SEd Tanous boost::asio::io_context& ioc; 609*f8ca6d79SEd Tanous 610b52664e2SAppaRao Puli public: 6119f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 6129f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 6139f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 6149f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 615ecd6a3a2SEd Tanous ~EventServiceManager() = default; 6169f616dd1SEd Tanous 617*f8ca6d79SEd Tanous explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn) 618b52664e2SAppaRao Puli { 619*f8ca6d79SEd Tanous // Load config from persist store. 620*f8ca6d79SEd Tanous initConfig(); 621*f8ca6d79SEd Tanous } 622*f8ca6d79SEd Tanous 623*f8ca6d79SEd Tanous static EventServiceManager& 624*f8ca6d79SEd Tanous getInstance(boost::asio::io_context* ioc = nullptr) 625*f8ca6d79SEd Tanous { 626*f8ca6d79SEd Tanous static EventServiceManager handler(*ioc); 627b52664e2SAppaRao Puli return handler; 628b52664e2SAppaRao Puli } 629b52664e2SAppaRao Puli 6301bf712bcSAyushi Smriti void initConfig() 6311bf712bcSAyushi Smriti { 63228afb49cSJunLin Chen loadOldBehavior(); 6331bf712bcSAyushi Smriti 63428afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 63528afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 63628afb49cSJunLin Chen .getEventServiceConfig(); 6371bf712bcSAyushi Smriti 63828afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 63928afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 64028afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6411bf712bcSAyushi Smriti 64228afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 64328afb49cSJunLin Chen .subscriptionsConfigMap) 6441bf712bcSAyushi Smriti { 64528afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 64628afb49cSJunLin Chen it.second; 6474bbf237fSAppaRao Puli 6481bf712bcSAyushi Smriti std::string host; 6491bf712bcSAyushi Smriti std::string urlProto; 650eb1c47d3SEd Tanous uint16_t port = 0; 6511bf712bcSAyushi Smriti std::string path; 65211baefe4SEd Tanous bool status = crow::utility::validateAndSplitUrl( 65311baefe4SEd Tanous newSub->destinationUrl, urlProto, host, port, path); 6541bf712bcSAyushi Smriti 6551bf712bcSAyushi Smriti if (!status) 6561bf712bcSAyushi Smriti { 6571bf712bcSAyushi Smriti BMCWEB_LOG_ERROR 6581bf712bcSAyushi Smriti << "Failed to validate and split destination url"; 6591bf712bcSAyushi Smriti continue; 6601bf712bcSAyushi Smriti } 6611bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 662*f8ca6d79SEd Tanous std::make_shared<Subscription>(host, port, path, urlProto, 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 { 6791bf712bcSAyushi Smriti 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 { 70028afb49cSJunLin Chen 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 { 70628afb49cSJunLin Chen 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 { 72828afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Problem reading subscription " 72928afb49cSJunLin Chen "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 { 76128afb49cSJunLin Chen BMCWEB_LOG_ERROR 76228afb49cSJunLin Chen << "Failed to generate random number from old " 76328afb49cSJunLin Chen "persistent store"; 76428afb49cSJunLin Chen continue; 76528afb49cSJunLin Chen } 76628afb49cSJunLin Chen } 76728afb49cSJunLin Chen } 76828afb49cSJunLin Chen 76928afb49cSJunLin Chen persistent_data::getConfig().writeData(); 77028afb49cSJunLin Chen std::remove(eventServiceFile); 77128afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Remove old eventservice config"; 77228afb49cSJunLin Chen } 77328afb49cSJunLin Chen } 77428afb49cSJunLin Chen 7759eb808c1SEd Tanous void updateSubscriptionData() const 77628afb49cSJunLin Chen { 77728afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 77828afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 77928afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 78028afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 78128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 78228afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 78328afb49cSJunLin Chen 78428afb49cSJunLin Chen persistent_data::getConfig().writeData(); 78528afb49cSJunLin Chen } 78628afb49cSJunLin Chen 78728afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7887d1cc387SAppaRao Puli { 7897d1cc387SAppaRao Puli bool updateConfig = false; 790fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 7917d1cc387SAppaRao Puli 79228afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 7937d1cc387SAppaRao Puli { 79428afb49cSJunLin Chen serviceEnabled = cfg.enabled; 795e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 7967d1cc387SAppaRao Puli { 7977d1cc387SAppaRao Puli registerMetricReportSignal(); 7987d1cc387SAppaRao Puli } 7997d1cc387SAppaRao Puli else 8007d1cc387SAppaRao Puli { 8017d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8027d1cc387SAppaRao Puli } 8037d1cc387SAppaRao Puli updateConfig = true; 8047d1cc387SAppaRao Puli } 8057d1cc387SAppaRao Puli 80628afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 8077d1cc387SAppaRao Puli { 80828afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 8097d1cc387SAppaRao Puli updateConfig = true; 810fe44eb0bSAyushi Smriti updateRetryCfg = true; 8117d1cc387SAppaRao Puli } 8127d1cc387SAppaRao Puli 81328afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 8147d1cc387SAppaRao Puli { 81528afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 8167d1cc387SAppaRao Puli updateConfig = true; 817fe44eb0bSAyushi Smriti updateRetryCfg = true; 8187d1cc387SAppaRao Puli } 8197d1cc387SAppaRao Puli 8207d1cc387SAppaRao Puli if (updateConfig) 8217d1cc387SAppaRao Puli { 8227d1cc387SAppaRao Puli updateSubscriptionData(); 8237d1cc387SAppaRao Puli } 824fe44eb0bSAyushi Smriti 825fe44eb0bSAyushi Smriti if (updateRetryCfg) 826fe44eb0bSAyushi Smriti { 827fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 828fe44eb0bSAyushi Smriti for (const auto& it : 829fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 830fe44eb0bSAyushi Smriti { 831fe44eb0bSAyushi Smriti std::shared_ptr<Subscription> entry = it.second; 832fe44eb0bSAyushi Smriti entry->updateRetryConfig(retryAttempts, retryTimeoutInterval); 833fe44eb0bSAyushi Smriti } 834fe44eb0bSAyushi Smriti } 8357d1cc387SAppaRao Puli } 8367d1cc387SAppaRao Puli 8377d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8387d1cc387SAppaRao Puli { 8397d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8407d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8417d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8427d1cc387SAppaRao Puli { 8437d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8447d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8457d1cc387SAppaRao Puli { 8467d1cc387SAppaRao Puli eventLogSubCount++; 8477d1cc387SAppaRao Puli } 8487d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8497d1cc387SAppaRao Puli { 8507d1cc387SAppaRao Puli metricReportSubCount++; 8517d1cc387SAppaRao Puli } 8527d1cc387SAppaRao Puli } 8537d1cc387SAppaRao Puli 8547d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8557d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8567d1cc387SAppaRao Puli { 8577d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 858e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 8597d1cc387SAppaRao Puli { 8607d1cc387SAppaRao Puli registerMetricReportSignal(); 8617d1cc387SAppaRao Puli } 8627d1cc387SAppaRao Puli else 8637d1cc387SAppaRao Puli { 8647d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8657d1cc387SAppaRao Puli } 8667d1cc387SAppaRao Puli } 8677d1cc387SAppaRao Puli } 8687d1cc387SAppaRao Puli 869b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 870b52664e2SAppaRao Puli { 871b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 872b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 873b52664e2SAppaRao Puli { 874b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 875b52664e2SAppaRao Puli return nullptr; 876b52664e2SAppaRao Puli } 877b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 878b52664e2SAppaRao Puli return subValue; 879b52664e2SAppaRao Puli } 880b52664e2SAppaRao Puli 881b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 8821bf712bcSAyushi Smriti const bool updateFile = true) 883b52664e2SAppaRao Puli { 884fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 885fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 886fc76b8acSEd Tanous 887b52664e2SAppaRao Puli std::string id; 888b52664e2SAppaRao Puli 889b52664e2SAppaRao Puli int retry = 3; 890e662eae8SEd Tanous while (retry != 0) 891b52664e2SAppaRao Puli { 892fc76b8acSEd Tanous id = std::to_string(dist(gen)); 893fc76b8acSEd Tanous if (gen.error()) 894fc76b8acSEd Tanous { 895fc76b8acSEd Tanous retry = 0; 896fc76b8acSEd Tanous break; 897fc76b8acSEd Tanous } 898b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 899b52664e2SAppaRao Puli if (inserted.second) 900b52664e2SAppaRao Puli { 901b52664e2SAppaRao Puli break; 902b52664e2SAppaRao Puli } 903b52664e2SAppaRao Puli --retry; 90423a21a1cSEd Tanous } 905b52664e2SAppaRao Puli 906b52664e2SAppaRao Puli if (retry <= 0) 907b52664e2SAppaRao Puli { 908b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Failed to generate random number"; 909abb93cddSEd Tanous return ""; 910b52664e2SAppaRao Puli } 911b52664e2SAppaRao Puli 91228afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 91328afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 91428afb49cSJunLin Chen newSub->id = id; 91528afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 91628afb49cSJunLin Chen newSub->protocol = subValue->protocol; 91728afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 91828afb49cSJunLin Chen newSub->customText = subValue->customText; 91928afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 92028afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 92128afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 92228afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 92328afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 92428afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 92528afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 92628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 92728afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 92828afb49cSJunLin Chen 9297d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 9301bf712bcSAyushi Smriti 9311bf712bcSAyushi Smriti if (updateFile) 9321bf712bcSAyushi Smriti { 933b52664e2SAppaRao Puli updateSubscriptionData(); 9341bf712bcSAyushi Smriti } 9357f4eb588SAppaRao Puli 9367f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 9372558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 9387f4eb588SAppaRao Puli { 9392558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 9407f4eb588SAppaRao Puli } 9417f4eb588SAppaRao Puli #endif 942fe44eb0bSAyushi Smriti // Update retry configuration. 943fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 944fe44eb0bSAyushi Smriti 945b52664e2SAppaRao Puli return id; 946b52664e2SAppaRao Puli } 947b52664e2SAppaRao Puli 948b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 949b52664e2SAppaRao Puli { 950b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 95155f79e6fSEd Tanous return obj != subscriptionsMap.end(); 952b52664e2SAppaRao Puli } 953b52664e2SAppaRao Puli 954b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 955b52664e2SAppaRao Puli { 956b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 957b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 958b52664e2SAppaRao Puli { 959b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 96028afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 96128afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 96228afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 96328afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9647d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 965b52664e2SAppaRao Puli updateSubscriptionData(); 966b52664e2SAppaRao Puli } 967b52664e2SAppaRao Puli } 968b52664e2SAppaRao Puli 969b52664e2SAppaRao Puli size_t getNumberOfSubscriptions() 970b52664e2SAppaRao Puli { 971b52664e2SAppaRao Puli return subscriptionsMap.size(); 972b52664e2SAppaRao Puli } 973b52664e2SAppaRao Puli 974b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 975b52664e2SAppaRao Puli { 976b52664e2SAppaRao Puli std::vector<std::string> idList; 977b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 978b52664e2SAppaRao Puli { 979b52664e2SAppaRao Puli idList.emplace_back(it.first); 980b52664e2SAppaRao Puli } 981b52664e2SAppaRao Puli return idList; 982b52664e2SAppaRao Puli } 983b52664e2SAppaRao Puli 984b52664e2SAppaRao Puli bool isDestinationExist(const std::string& destUrl) 985b52664e2SAppaRao Puli { 986b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 987b52664e2SAppaRao Puli { 988b52664e2SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 989b52664e2SAppaRao Puli if (entry->destinationUrl == destUrl) 990b52664e2SAppaRao Puli { 991b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 992b52664e2SAppaRao Puli return true; 993b52664e2SAppaRao Puli } 994b52664e2SAppaRao Puli } 995b52664e2SAppaRao Puli return false; 996b52664e2SAppaRao Puli } 9970b4bdd93SAppaRao Puli 9986ba8c82eSsunharis_in bool sendTestEventLog() 9990b4bdd93SAppaRao Puli { 10000b4bdd93SAppaRao Puli for (const auto& it : this->subscriptionsMap) 10010b4bdd93SAppaRao Puli { 10020b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 10036ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 10046ba8c82eSsunharis_in { 10056ba8c82eSsunharis_in return false; 10060b4bdd93SAppaRao Puli } 10070b4bdd93SAppaRao Puli } 10086ba8c82eSsunharis_in return true; 10096ba8c82eSsunharis_in } 1010e9a14131SAppaRao Puli 1011613dabeaSEd Tanous void sendEvent(nlohmann::json eventMessage, const std::string& origin, 1012613dabeaSEd Tanous const std::string& resType) 101396330b99SSunitha Harish { 1014dbb59d4dSEd Tanous if (!serviceEnabled || (noOfEventLogSubscribers == 0U)) 10156ba8c82eSsunharis_in { 10166ba8c82eSsunharis_in BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 10176ba8c82eSsunharis_in return; 10186ba8c82eSsunharis_in } 101996330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 102096330b99SSunitha Harish 1021613dabeaSEd Tanous eventMessage["EventId"] = eventId; 1022613dabeaSEd Tanous // MemberId is 0 : since we are sending one event record. 1023613dabeaSEd Tanous eventMessage["MemberId"] = 0; 1024613dabeaSEd Tanous eventMessage["EventTimestamp"] = 1025613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1026613dabeaSEd Tanous eventMessage["OriginOfCondition"] = origin; 1027613dabeaSEd Tanous 1028613dabeaSEd Tanous eventRecord.emplace_back(std::move(eventMessage)); 102996330b99SSunitha Harish 103096330b99SSunitha Harish for (const auto& it : this->subscriptionsMap) 103196330b99SSunitha Harish { 103296330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 103396330b99SSunitha Harish bool isSubscribed = false; 103496330b99SSunitha Harish // Search the resourceTypes list for the subscription. 103596330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 103696330b99SSunitha Harish // send everything. 103726f6976fSEd Tanous if (!entry->resourceTypes.empty()) 103896330b99SSunitha Harish { 10393174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 104096330b99SSunitha Harish { 104196330b99SSunitha Harish if (resType == resource) 104296330b99SSunitha Harish { 104396330b99SSunitha Harish BMCWEB_LOG_INFO << "ResourceType " << resource 104496330b99SSunitha Harish << " found in the subscribed list"; 104596330b99SSunitha Harish isSubscribed = true; 104696330b99SSunitha Harish break; 104796330b99SSunitha Harish } 104896330b99SSunitha Harish } 104996330b99SSunitha Harish } 105096330b99SSunitha Harish else // resourceTypes list is empty. 105196330b99SSunitha Harish { 105296330b99SSunitha Harish isSubscribed = true; 105396330b99SSunitha Harish } 105496330b99SSunitha Harish if (isSubscribed) 105596330b99SSunitha Harish { 1056613dabeaSEd Tanous nlohmann::json msgJson; 1057613dabeaSEd Tanous 1058613dabeaSEd Tanous msgJson["@odata.type"] = "#Event.v1_4_0.Event"; 1059613dabeaSEd Tanous msgJson["Name"] = "Event Log"; 1060613dabeaSEd Tanous msgJson["Id"] = eventId; 1061613dabeaSEd Tanous msgJson["Events"] = eventRecord; 1062f52c03c1SCarson Labrado 1063f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1064f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 1065f52c03c1SCarson Labrado entry->sendEvent(strMsg); 106696330b99SSunitha Harish eventId++; // increament the eventId 106796330b99SSunitha Harish } 106896330b99SSunitha Harish else 106996330b99SSunitha Harish { 107096330b99SSunitha Harish BMCWEB_LOG_INFO << "Not subscribed to this resource"; 107196330b99SSunitha Harish } 107296330b99SSunitha Harish } 107396330b99SSunitha Harish } 10745738de59SAsmitha Karunanithi void sendBroadcastMsg(const std::string& broadcastMsg) 10755738de59SAsmitha Karunanithi { 10765738de59SAsmitha Karunanithi for (const auto& it : this->subscriptionsMap) 10775738de59SAsmitha Karunanithi { 10785738de59SAsmitha Karunanithi std::shared_ptr<Subscription> entry = it.second; 1079613dabeaSEd Tanous nlohmann::json msgJson; 1080613dabeaSEd Tanous msgJson["Timestamp"] = 1081613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1082613dabeaSEd Tanous msgJson["OriginOfCondition"] = "/ibm/v1/HMC/BroadcastService"; 1083613dabeaSEd Tanous msgJson["Name"] = "Broadcast Message"; 1084613dabeaSEd Tanous msgJson["Message"] = broadcastMsg; 1085f52c03c1SCarson Labrado 1086f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1087f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 1088f52c03c1SCarson Labrado entry->sendEvent(strMsg); 10895738de59SAsmitha Karunanithi } 10905738de59SAsmitha Karunanithi } 109196330b99SSunitha Harish 10927f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 10932558979cSP Dheeraj Srujan Kumar 10942558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 10957f4eb588SAppaRao Puli { 10962558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 10972558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 10982558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 10992558979cSP Dheeraj Srujan Kumar } 11002558979cSP Dheeraj Srujan Kumar 11012558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 11022558979cSP Dheeraj Srujan Kumar { 11032558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 11042558979cSP Dheeraj Srujan Kumar 11057f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11067f4eb588SAppaRao Puli if (!logStream.good()) 11077f4eb588SAppaRao Puli { 11087f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 11097f4eb588SAppaRao Puli return; 11107f4eb588SAppaRao Puli } 11117f4eb588SAppaRao Puli std::string logEntry; 11127f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11137f4eb588SAppaRao Puli { 11142558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11157f4eb588SAppaRao Puli } 11162558979cSP Dheeraj Srujan Kumar 11172558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "Next Log Position : " << redfishLogFilePosition; 11187f4eb588SAppaRao Puli } 11197f4eb588SAppaRao Puli 11207f4eb588SAppaRao Puli void readEventLogsFromFile() 11217f4eb588SAppaRao Puli { 11227f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11237f4eb588SAppaRao Puli if (!logStream.good()) 11247f4eb588SAppaRao Puli { 11257f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed"; 11267f4eb588SAppaRao Puli return; 11277f4eb588SAppaRao Puli } 11287f4eb588SAppaRao Puli 11297f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11307f4eb588SAppaRao Puli 11317f4eb588SAppaRao Puli std::string logEntry; 11322558979cSP Dheeraj Srujan Kumar 11332558979cSP Dheeraj Srujan Kumar // Get the read pointer to the next log to be read. 11342558979cSP Dheeraj Srujan Kumar logStream.seekg(redfishLogFilePosition); 11352558979cSP Dheeraj Srujan Kumar 11367f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11377f4eb588SAppaRao Puli { 11382558979cSP Dheeraj Srujan Kumar // Update Pointer position 11392558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11402558979cSP Dheeraj Srujan Kumar 11412558979cSP Dheeraj Srujan Kumar std::string idStr; 11422558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 11437f4eb588SAppaRao Puli { 11447f4eb588SAppaRao Puli continue; 11457f4eb588SAppaRao Puli } 11467f4eb588SAppaRao Puli 1147e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11487f4eb588SAppaRao Puli { 11492558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 11502558979cSP Dheeraj Srujan Kumar // the remaining items below. 11512558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 11527f4eb588SAppaRao Puli continue; 11537f4eb588SAppaRao Puli } 11547f4eb588SAppaRao Puli 11557f4eb588SAppaRao Puli std::string timestamp; 11567f4eb588SAppaRao Puli std::string messageID; 11575e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11587f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11597f4eb588SAppaRao Puli messageArgs) != 0) 11607f4eb588SAppaRao Puli { 11617f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 11627f4eb588SAppaRao Puli continue; 11637f4eb588SAppaRao Puli } 11647f4eb588SAppaRao Puli 11657f4eb588SAppaRao Puli std::string registryName; 11667f4eb588SAppaRao Puli std::string messageKey; 11677f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 11687f4eb588SAppaRao Puli messageKey); 11697f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 11707f4eb588SAppaRao Puli { 11717f4eb588SAppaRao Puli continue; 11727f4eb588SAppaRao Puli } 11737f4eb588SAppaRao Puli 11747f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 11757f4eb588SAppaRao Puli messageKey, messageArgs); 11767f4eb588SAppaRao Puli } 11777f4eb588SAppaRao Puli 1178e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11792558979cSP Dheeraj Srujan Kumar { 11802558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 11812558979cSP Dheeraj Srujan Kumar return; 11822558979cSP Dheeraj Srujan Kumar } 11832558979cSP Dheeraj Srujan Kumar 11842558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 11852558979cSP Dheeraj Srujan Kumar { 11862558979cSP Dheeraj Srujan Kumar // No Records to send 11872558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 11882558979cSP Dheeraj Srujan Kumar return; 11892558979cSP Dheeraj Srujan Kumar } 11902558979cSP Dheeraj Srujan Kumar 11917f4eb588SAppaRao Puli for (const auto& it : this->subscriptionsMap) 11927f4eb588SAppaRao Puli { 11937f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 11947f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 11957f4eb588SAppaRao Puli { 11967f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 11977f4eb588SAppaRao Puli } 11987f4eb588SAppaRao Puli } 11997f4eb588SAppaRao Puli } 12007f4eb588SAppaRao Puli 12017f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 12027f4eb588SAppaRao Puli { 12036a9f85f9SAppaRao Puli if (!inotifyConn) 12047f4eb588SAppaRao Puli { 12057f4eb588SAppaRao Puli return; 12067f4eb588SAppaRao Puli } 12077f4eb588SAppaRao Puli 12087f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 12097f4eb588SAppaRao Puli 1210002d39b4SEd Tanous inotifyConn->async_read_some(boost::asio::buffer(readBuffer), 12117f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 12127f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 12137f4eb588SAppaRao Puli if (ec) 12147f4eb588SAppaRao Puli { 12157f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 12167f4eb588SAppaRao Puli return; 12177f4eb588SAppaRao Puli } 12187f4eb588SAppaRao Puli std::size_t index = 0; 1219b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 12207f4eb588SAppaRao Puli { 1221d3a9e084SEd Tanous struct inotify_event event 1222d3a9e084SEd Tanous {}; 1223b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1224b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1225b792cc56SAppaRao Puli { 1226b792cc56SAppaRao Puli if ((event.len == 0) || 1227b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1228b792cc56SAppaRao Puli { 1229b792cc56SAppaRao Puli index += (iEventSize + event.len); 1230b792cc56SAppaRao Puli continue; 1231b792cc56SAppaRao Puli } 1232b792cc56SAppaRao Puli 12334f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 12344f568f74SJiaqing Zhao if (fileName != "redfish") 1235b792cc56SAppaRao Puli { 1236b792cc56SAppaRao Puli index += (iEventSize + event.len); 1237b792cc56SAppaRao Puli continue; 1238b792cc56SAppaRao Puli } 1239b792cc56SAppaRao Puli 1240b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1241b792cc56SAppaRao Puli << "Redfish log file created/deleted. event.name: " 1242b792cc56SAppaRao Puli << fileName; 1243b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1244b792cc56SAppaRao Puli { 1245b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1246b792cc56SAppaRao Puli { 1247b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1248016761afSAppaRao Puli << "Remove and Add inotify watcher on " 1249016761afSAppaRao Puli "redfish event log file"; 1250016761afSAppaRao Puli // Remove existing inotify watcher and add 1251016761afSAppaRao Puli // with new redfish event log file. 1252016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1253016761afSAppaRao Puli fileWatchDesc = -1; 1254b792cc56SAppaRao Puli } 1255b792cc56SAppaRao Puli 1256b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1257b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1258b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1259b792cc56SAppaRao Puli { 1260002d39b4SEd Tanous BMCWEB_LOG_ERROR << "inotify_add_watch failed for " 1261b792cc56SAppaRao Puli "redfish log file."; 1262b792cc56SAppaRao Puli return; 1263b792cc56SAppaRao Puli } 1264b792cc56SAppaRao Puli 1265b792cc56SAppaRao Puli EventServiceManager::getInstance() 12662558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1267b792cc56SAppaRao Puli EventServiceManager::getInstance() 1268b792cc56SAppaRao Puli .readEventLogsFromFile(); 1269b792cc56SAppaRao Puli } 1270b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1271b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1272b792cc56SAppaRao Puli { 1273b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1274b792cc56SAppaRao Puli { 1275b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1276b792cc56SAppaRao Puli fileWatchDesc = -1; 1277b792cc56SAppaRao Puli } 1278b792cc56SAppaRao Puli } 1279b792cc56SAppaRao Puli } 1280b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1281b792cc56SAppaRao Puli { 1282b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12837f4eb588SAppaRao Puli { 12847f4eb588SAppaRao Puli EventServiceManager::getInstance() 12857f4eb588SAppaRao Puli .readEventLogsFromFile(); 12867f4eb588SAppaRao Puli } 1287b792cc56SAppaRao Puli } 1288b792cc56SAppaRao Puli index += (iEventSize + event.len); 12897f4eb588SAppaRao Puli } 12907f4eb588SAppaRao Puli 12917f4eb588SAppaRao Puli watchRedfishEventLogFile(); 12927f4eb588SAppaRao Puli }); 12937f4eb588SAppaRao Puli } 12947f4eb588SAppaRao Puli 12957f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 12967f4eb588SAppaRao Puli { 129723a21a1cSEd Tanous inotifyConn.emplace(ioc); 1298b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1299b792cc56SAppaRao Puli if (inotifyFd == -1) 13007f4eb588SAppaRao Puli { 13017f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "inotify_init1 failed."; 13027f4eb588SAppaRao Puli return -1; 13037f4eb588SAppaRao Puli } 1304b792cc56SAppaRao Puli 1305b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1306b792cc56SAppaRao Puli // create/delete. 1307b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1308b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1309b792cc56SAppaRao Puli if (dirWatchDesc == -1) 13107f4eb588SAppaRao Puli { 13117f4eb588SAppaRao Puli BMCWEB_LOG_ERROR 1312b792cc56SAppaRao Puli << "inotify_add_watch failed for event log directory."; 13137f4eb588SAppaRao Puli return -1; 13147f4eb588SAppaRao Puli } 13157f4eb588SAppaRao Puli 1316b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 131789492a15SPatrick Williams fileWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogFile, 131889492a15SPatrick Williams IN_MODIFY); 1319b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1320b792cc56SAppaRao Puli { 1321b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1322b792cc56SAppaRao Puli << "inotify_add_watch failed for redfish log file."; 1323b792cc56SAppaRao Puli // Don't return error if file not exist. 1324b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1325b792cc56SAppaRao Puli } 1326b792cc56SAppaRao Puli 13277f4eb588SAppaRao Puli // monitor redfish event log file 1328b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13297f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13307f4eb588SAppaRao Puli 13317f4eb588SAppaRao Puli return 0; 13327f4eb588SAppaRao Puli } 13337f4eb588SAppaRao Puli 13347f4eb588SAppaRao Puli #endif 133559d494eeSPatrick Williams static void getReadingsForReport(sdbusplus::message_t& msg) 1336156d6b00SAppaRao Puli { 133756d2396dSEd Tanous if (msg.is_method_error()) 133856d2396dSEd Tanous { 133956d2396dSEd Tanous BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 134056d2396dSEd Tanous return; 134156d2396dSEd Tanous } 134256d2396dSEd Tanous 1343c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1344c0353249SWludzik, Jozef std::string id = path.filename(); 1345c0353249SWludzik, Jozef if (id.empty()) 1346156d6b00SAppaRao Puli { 1347c0353249SWludzik, Jozef BMCWEB_LOG_ERROR << "Failed to get Id from path"; 1348156d6b00SAppaRao Puli return; 1349156d6b00SAppaRao Puli } 1350156d6b00SAppaRao Puli 1351c0353249SWludzik, Jozef std::string interface; 1352b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1353c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1354c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1355c0353249SWludzik, Jozef 1356c0353249SWludzik, Jozef auto found = 1357c0353249SWludzik, Jozef std::find_if(props.begin(), props.end(), 1358c0353249SWludzik, Jozef [](const auto& x) { return x.first == "Readings"; }); 1359c0353249SWludzik, Jozef if (found == props.end()) 1360156d6b00SAppaRao Puli { 1361c0353249SWludzik, Jozef BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 1362156d6b00SAppaRao Puli return; 1363156d6b00SAppaRao Puli } 1364156d6b00SAppaRao Puli 13651e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 13661e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1367e662eae8SEd Tanous if (readings == nullptr) 13681e1e598dSJonathan Doman { 13691e1e598dSJonathan Doman BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 13701e1e598dSJonathan Doman return; 13711e1e598dSJonathan Doman } 13721e1e598dSJonathan Doman 1373156d6b00SAppaRao Puli for (const auto& it : 1374156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1375156d6b00SAppaRao Puli { 1376e05aec50SEd Tanous Subscription& entry = *it.second; 1377c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1378156d6b00SAppaRao Puli { 13791e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1380156d6b00SAppaRao Puli } 1381156d6b00SAppaRao Puli } 1382156d6b00SAppaRao Puli } 1383156d6b00SAppaRao Puli 1384156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1385156d6b00SAppaRao Puli { 13867d1cc387SAppaRao Puli if (matchTelemetryMonitor) 13877d1cc387SAppaRao Puli { 1388156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1389156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1390156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1391156d6b00SAppaRao Puli } 13927d1cc387SAppaRao Puli } 1393156d6b00SAppaRao Puli 1394156d6b00SAppaRao Puli void registerMetricReportSignal() 1395156d6b00SAppaRao Puli { 13967d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1397156d6b00SAppaRao Puli { 13987d1cc387SAppaRao Puli BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1399156d6b00SAppaRao Puli return; 1400156d6b00SAppaRao Puli } 1401156d6b00SAppaRao Puli 1402156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1403c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1404c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1405c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1406156d6b00SAppaRao Puli 140759d494eeSPatrick Williams matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>( 140856d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1409156d6b00SAppaRao Puli } 141023a21a1cSEd Tanous }; 1411b52664e2SAppaRao Puli 1412b52664e2SAppaRao Puli } // namespace redfish 1413