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" 27*5e44e3d8SAppaRao Puli #include "registries/privilege_registry.hpp" 28b304bd79SP Dheeraj Srujan Kumar #include "registries/task_event_message_registry.hpp" 293ccb3adbSEd Tanous #include "server_sent_events.hpp" 3050ebd4afSEd Tanous #include "str_utility.hpp" 3177665bdaSNan Zhou #include "utility.hpp" 323ccb3adbSEd Tanous #include "utils/json_utils.hpp" 337f4eb588SAppaRao Puli 347f4eb588SAppaRao Puli #include <sys/inotify.h> 35b52664e2SAppaRao Puli 3611ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 37fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 38b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 39ef4c65b7SEd Tanous #include <boost/url/format.hpp> 40b5b40605Snitroglycerine #include <sdbusplus/bus/match.hpp> 411214b7e7SGunnar Mills 42*5e44e3d8SAppaRao Puli #include <algorithm> 43b52664e2SAppaRao Puli #include <cstdlib> 44b52664e2SAppaRao Puli #include <ctime> 451bf712bcSAyushi Smriti #include <fstream> 46b52664e2SAppaRao Puli #include <memory> 4726702d01SEd Tanous #include <span> 48b52664e2SAppaRao Puli 49b52664e2SAppaRao Puli namespace redfish 50b52664e2SAppaRao Puli { 51156d6b00SAppaRao Puli 52156d6b00SAppaRao Puli using ReadingsObjType = 5393f5d7c7SWludzik, Jozef std::vector<std::tuple<std::string, std::string, double, int32_t>>; 54156d6b00SAppaRao Puli 55156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 56156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 57156d6b00SAppaRao Puli 58*5e44e3d8SAppaRao Puli static constexpr const char* subscriptionTypeSSE = "SSE"; 591bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 601bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 611bf712bcSAyushi Smriti 62*5e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSubscriptions = 20; 63*5e44e3d8SAppaRao Puli static constexpr const uint8_t maxNoOfSSESubscriptions = 10; 64*5e44e3d8SAppaRao Puli 65fffb8c1fSEd Tanous namespace registries 667f4eb588SAppaRao Puli { 6726702d01SEd Tanous inline std::span<const MessageEntry> 68b304bd79SP Dheeraj Srujan Kumar getRegistryFromPrefix(const std::string& registryName) 69b304bd79SP Dheeraj Srujan Kumar { 70b304bd79SP Dheeraj Srujan Kumar if (task_event::header.registryPrefix == registryName) 71b304bd79SP Dheeraj Srujan Kumar { 7226702d01SEd Tanous return {task_event::registry}; 73b304bd79SP Dheeraj Srujan Kumar } 74b304bd79SP Dheeraj Srujan Kumar if (openbmc::header.registryPrefix == registryName) 75b304bd79SP Dheeraj Srujan Kumar { 7626702d01SEd Tanous return {openbmc::registry}; 77b304bd79SP Dheeraj Srujan Kumar } 78b304bd79SP Dheeraj Srujan Kumar if (base::header.registryPrefix == registryName) 79b304bd79SP Dheeraj Srujan Kumar { 8026702d01SEd Tanous return {base::registry}; 81b304bd79SP Dheeraj Srujan Kumar } 8226702d01SEd Tanous return {openbmc::registry}; 83b304bd79SP Dheeraj Srujan Kumar } 84fffb8c1fSEd Tanous } // namespace registries 85b304bd79SP Dheeraj Srujan Kumar 864642bf8fSGeorge Liu #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 87cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 884642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 894642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 904642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 914642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 92cf9e417dSEd Tanous 93cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 944642bf8fSGeorge Liu static int inotifyFd = -1; 95cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 964642bf8fSGeorge Liu static int dirWatchDesc = -1; 97cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 984642bf8fSGeorge Liu static int fileWatchDesc = -1; 994642bf8fSGeorge Liu 1004642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 1014642bf8fSGeorge Liu using EventLogObjectsType = 1024642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 1034642bf8fSGeorge Liu std::vector<std::string>>; 1044642bf8fSGeorge Liu 105fffb8c1fSEd Tanous namespace registries 1064642bf8fSGeorge Liu { 1077f4eb588SAppaRao Puli static const Message* 1087f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 10926702d01SEd Tanous const std::span<const MessageEntry>& registry) 1107f4eb588SAppaRao Puli { 11126702d01SEd Tanous std::span<const MessageEntry>::iterator messageIt = 11226702d01SEd Tanous std::find_if(registry.begin(), registry.end(), 1137f4eb588SAppaRao Puli [&messageKey](const MessageEntry& messageEntry) { 11455f79e6fSEd Tanous return messageKey == messageEntry.first; 1157f4eb588SAppaRao Puli }); 11626702d01SEd Tanous if (messageIt != registry.end()) 1177f4eb588SAppaRao Puli { 1187f4eb588SAppaRao Puli return &messageIt->second; 1197f4eb588SAppaRao Puli } 1207f4eb588SAppaRao Puli 1217f4eb588SAppaRao Puli return nullptr; 1227f4eb588SAppaRao Puli } 1237f4eb588SAppaRao Puli 12426ccae32SEd Tanous static const Message* formatMessage(std::string_view messageID) 1257f4eb588SAppaRao Puli { 1267f4eb588SAppaRao Puli // Redfish MessageIds are in the form 1277f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1287f4eb588SAppaRao Puli // the right Message 1297f4eb588SAppaRao Puli std::vector<std::string> fields; 1307f4eb588SAppaRao Puli fields.reserve(4); 13150ebd4afSEd Tanous 13250ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 1337f4eb588SAppaRao Puli if (fields.size() != 4) 1347f4eb588SAppaRao Puli { 1357f4eb588SAppaRao Puli return nullptr; 1367f4eb588SAppaRao Puli } 13702cad96eSEd Tanous const std::string& registryName = fields[0]; 13802cad96eSEd Tanous const std::string& messageKey = fields[3]; 1397f4eb588SAppaRao Puli 1407f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 141b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1427f4eb588SAppaRao Puli } 143fffb8c1fSEd Tanous } // namespace registries 1447f4eb588SAppaRao Puli 1457f4eb588SAppaRao Puli namespace event_log 1467f4eb588SAppaRao Puli { 1472558979cSP Dheeraj Srujan Kumar inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 1487f4eb588SAppaRao Puli { 1497f4eb588SAppaRao Puli static time_t prevTs = 0; 1507f4eb588SAppaRao Puli static int index = 0; 1517f4eb588SAppaRao Puli 1527f4eb588SAppaRao Puli // Get the entry timestamp 1537f4eb588SAppaRao Puli std::time_t curTs = 0; 1547f4eb588SAppaRao Puli std::tm timeStruct = {}; 1557f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1567f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1577f4eb588SAppaRao Puli { 1587f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1597f4eb588SAppaRao Puli if (curTs == -1) 1607f4eb588SAppaRao Puli { 1617f4eb588SAppaRao Puli return false; 1627f4eb588SAppaRao Puli } 1637f4eb588SAppaRao Puli } 1647f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1657f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1667f4eb588SAppaRao Puli 1677f4eb588SAppaRao Puli // Save the timestamp 1687f4eb588SAppaRao Puli prevTs = curTs; 1697f4eb588SAppaRao Puli 1707f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1717f4eb588SAppaRao Puli if (index > 0) 1727f4eb588SAppaRao Puli { 1737f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1747f4eb588SAppaRao Puli } 1757f4eb588SAppaRao Puli return true; 1767f4eb588SAppaRao Puli } 1777f4eb588SAppaRao Puli 17823a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 17923a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1805e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1817f4eb588SAppaRao Puli { 1827f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1837f4eb588SAppaRao Puli // First get the Timestamp 184f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1857f4eb588SAppaRao Puli if (space == std::string::npos) 1867f4eb588SAppaRao Puli { 1877f4eb588SAppaRao Puli return -EINVAL; 1887f4eb588SAppaRao Puli } 1897f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1907f4eb588SAppaRao Puli // Then get the log contents 191f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1927f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1937f4eb588SAppaRao Puli { 1947f4eb588SAppaRao Puli return -EINVAL; 1957f4eb588SAppaRao Puli } 1967f4eb588SAppaRao Puli std::string_view entry(logEntry); 1977f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1987f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1997f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 20050ebd4afSEd Tanous bmcweb::split(logEntryFields, entry, ','); 2017f4eb588SAppaRao Puli // We need at least a MessageId to be valid 20226f6976fSEd Tanous if (logEntryFields.empty()) 2037f4eb588SAppaRao Puli { 2047f4eb588SAppaRao Puli return -EINVAL; 2057f4eb588SAppaRao Puli } 2067f4eb588SAppaRao Puli messageID = logEntryFields[0]; 2077f4eb588SAppaRao Puli 2087f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 2097f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 2107f4eb588SAppaRao Puli { 21102cad96eSEd Tanous const std::string& messageArgsStart = logEntryFields[1]; 2127f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 2137f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 2147f4eb588SAppaRao Puli { 2155e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 2165e715de6SAppaRao Puli logEntryFields.end()); 2177f4eb588SAppaRao Puli } 2187f4eb588SAppaRao Puli } 2197f4eb588SAppaRao Puli 2207f4eb588SAppaRao Puli return 0; 2217f4eb588SAppaRao Puli } 2227f4eb588SAppaRao Puli 22323a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 2247f4eb588SAppaRao Puli std::string& registryName, 2257f4eb588SAppaRao Puli std::string& messageKey) 2267f4eb588SAppaRao Puli { 2277f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2287f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2297f4eb588SAppaRao Puli // the right Message 2307f4eb588SAppaRao Puli std::vector<std::string> fields; 2317f4eb588SAppaRao Puli fields.reserve(4); 23250ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 2337f4eb588SAppaRao Puli if (fields.size() == 4) 2347f4eb588SAppaRao Puli { 2357f4eb588SAppaRao Puli registryName = fields[0]; 2367f4eb588SAppaRao Puli messageKey = fields[3]; 2377f4eb588SAppaRao Puli } 2387f4eb588SAppaRao Puli } 2397f4eb588SAppaRao Puli 24023a21a1cSEd Tanous inline int formatEventLogEntry(const std::string& logEntryID, 2417f4eb588SAppaRao Puli const std::string& messageID, 242c5ba4c27SEd Tanous const std::span<std::string_view> messageArgs, 24323a21a1cSEd Tanous std::string timestamp, 244b5a76932SEd Tanous const std::string& customText, 2457f4eb588SAppaRao Puli nlohmann::json& logEntryJson) 2467f4eb588SAppaRao Puli { 2477f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 248fffb8c1fSEd Tanous const registries::Message* message = registries::formatMessage(messageID); 2497f4eb588SAppaRao Puli 25080f595e7SEd Tanous if (message == nullptr) 2517f4eb588SAppaRao Puli { 25280f595e7SEd Tanous return -1; 2537f4eb588SAppaRao Puli } 2547f4eb588SAppaRao Puli 25589492a15SPatrick Williams std::string msg = redfish::registries::fillMessageArgs(messageArgs, 25689492a15SPatrick Williams message->message); 25780f595e7SEd Tanous if (msg.empty()) 25880f595e7SEd Tanous { 25980f595e7SEd Tanous return -1; 26080f595e7SEd Tanous } 2617f4eb588SAppaRao Puli 2627f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2637f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2647f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 265f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 266b2f7609bSEd Tanous std::size_t plus = timestamp.find_first_of('+', dot); 2677f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2687f4eb588SAppaRao Puli { 2697f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2707f4eb588SAppaRao Puli } 2717f4eb588SAppaRao Puli 2727f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2731476687dSEd Tanous logEntryJson["EventId"] = logEntryID; 2741476687dSEd Tanous logEntryJson["EventType"] = "Event"; 27580f595e7SEd Tanous logEntryJson["Severity"] = message->messageSeverity; 2761476687dSEd Tanous logEntryJson["Message"] = std::move(msg); 2771476687dSEd Tanous logEntryJson["MessageId"] = messageID; 2781476687dSEd Tanous logEntryJson["MessageArgs"] = messageArgs; 2791476687dSEd Tanous logEntryJson["EventTimestamp"] = std::move(timestamp); 2801476687dSEd Tanous logEntryJson["Context"] = customText; 2817f4eb588SAppaRao Puli return 0; 2827f4eb588SAppaRao Puli } 2837f4eb588SAppaRao Puli 2847f4eb588SAppaRao Puli } // namespace event_log 2857f4eb588SAppaRao Puli #endif 2867f4eb588SAppaRao Puli 28723a21a1cSEd Tanous inline bool isFilterQuerySpecialChar(char c) 28807941a88SAyushi Smriti { 28907941a88SAyushi Smriti switch (c) 29007941a88SAyushi Smriti { 29107941a88SAyushi Smriti case '(': 29207941a88SAyushi Smriti case ')': 29307941a88SAyushi Smriti case '\'': 29407941a88SAyushi Smriti return true; 29507941a88SAyushi Smriti default: 29607941a88SAyushi Smriti return false; 29707941a88SAyushi Smriti } 29807941a88SAyushi Smriti } 29907941a88SAyushi Smriti 30023a21a1cSEd Tanous inline bool 30123a21a1cSEd Tanous readSSEQueryParams(std::string sseFilter, std::string& formatType, 30207941a88SAyushi Smriti std::vector<std::string>& messageIds, 30307941a88SAyushi Smriti std::vector<std::string>& registryPrefixes, 304144b6318SAppaRao Puli std::vector<std::string>& metricReportDefinitions) 30507941a88SAyushi Smriti { 30607941a88SAyushi Smriti sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(), 30707941a88SAyushi Smriti isFilterQuerySpecialChar), 30807941a88SAyushi Smriti sseFilter.end()); 30907941a88SAyushi Smriti 31007941a88SAyushi Smriti std::vector<std::string> result; 31150ebd4afSEd Tanous 31250ebd4afSEd Tanous // NOLINTNEXTLINE 31350ebd4afSEd Tanous bmcweb::split(result, sseFilter, ' '); 31407941a88SAyushi Smriti 31507941a88SAyushi Smriti BMCWEB_LOG_DEBUG << "No of tokens in SEE query: " << result.size(); 31607941a88SAyushi Smriti 31707941a88SAyushi Smriti constexpr uint8_t divisor = 4; 31807941a88SAyushi Smriti constexpr uint8_t minTokenSize = 3; 31907941a88SAyushi Smriti if (result.size() % divisor != minTokenSize) 32007941a88SAyushi Smriti { 32107941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid SSE filter specified."; 32207941a88SAyushi Smriti return false; 32307941a88SAyushi Smriti } 32407941a88SAyushi Smriti 32507941a88SAyushi Smriti for (std::size_t i = 0; i < result.size(); i += divisor) 32607941a88SAyushi Smriti { 32702cad96eSEd Tanous const std::string& key = result[i]; 32802cad96eSEd Tanous const std::string& op = result[i + 1]; 32902cad96eSEd Tanous const std::string& value = result[i + 2]; 33007941a88SAyushi Smriti 33107941a88SAyushi Smriti if ((i + minTokenSize) < result.size()) 33207941a88SAyushi Smriti { 33302cad96eSEd Tanous const std::string& separator = result[i + minTokenSize]; 33407941a88SAyushi Smriti // SSE supports only "or" and "and" in query params. 3354e0453b1SGunnar Mills if ((separator != "or") && (separator != "and")) 33607941a88SAyushi Smriti { 33707941a88SAyushi Smriti BMCWEB_LOG_ERROR 33807941a88SAyushi Smriti << "Invalid group operator in SSE query parameters"; 33907941a88SAyushi Smriti return false; 34007941a88SAyushi Smriti } 34107941a88SAyushi Smriti } 34207941a88SAyushi Smriti 34307941a88SAyushi Smriti // SSE supports only "eq" as per spec. 34407941a88SAyushi Smriti if (op != "eq") 34507941a88SAyushi Smriti { 34607941a88SAyushi Smriti BMCWEB_LOG_ERROR 34707941a88SAyushi Smriti << "Invalid assignment operator in SSE query parameters"; 34807941a88SAyushi Smriti return false; 34907941a88SAyushi Smriti } 35007941a88SAyushi Smriti 35107941a88SAyushi Smriti BMCWEB_LOG_DEBUG << key << " : " << value; 35207941a88SAyushi Smriti if (key == "EventFormatType") 35307941a88SAyushi Smriti { 35407941a88SAyushi Smriti formatType = value; 35507941a88SAyushi Smriti } 35607941a88SAyushi Smriti else if (key == "MessageId") 35707941a88SAyushi Smriti { 35807941a88SAyushi Smriti messageIds.push_back(value); 35907941a88SAyushi Smriti } 36007941a88SAyushi Smriti else if (key == "RegistryPrefix") 36107941a88SAyushi Smriti { 36207941a88SAyushi Smriti registryPrefixes.push_back(value); 36307941a88SAyushi Smriti } 36407941a88SAyushi Smriti else if (key == "MetricReportDefinition") 36507941a88SAyushi Smriti { 36607941a88SAyushi Smriti metricReportDefinitions.push_back(value); 36707941a88SAyushi Smriti } 36807941a88SAyushi Smriti else 36907941a88SAyushi Smriti { 37007941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid property(" << key 37107941a88SAyushi Smriti << ")in SSE filter query."; 37207941a88SAyushi Smriti return false; 37307941a88SAyushi Smriti } 37407941a88SAyushi Smriti } 37507941a88SAyushi Smriti return true; 37607941a88SAyushi Smriti } 37707941a88SAyushi Smriti 37828afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 379b52664e2SAppaRao Puli { 380b52664e2SAppaRao Puli public: 381b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 382b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 383b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 384b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 385b52664e2SAppaRao Puli 386eb1c47d3SEd Tanous Subscription(const std::string& inHost, uint16_t inPort, 387f8ca6d79SEd Tanous const std::string& inPath, const std::string& inUriProto, 388f8ca6d79SEd Tanous boost::asio::io_context& ioc) : 389d14a48ffSCarson Labrado host(inHost), 390d14a48ffSCarson Labrado port(inPort), policy(std::make_shared<crow::ConnectionPolicy>()), 391*5e44e3d8SAppaRao Puli path(inPath), uriProto(inUriProto) 392b52664e2SAppaRao Puli { 393*5e44e3d8SAppaRao Puli client.emplace(ioc, policy); 3947adb85acSSunitha Harish // Subscription constructor 395d14a48ffSCarson Labrado policy->invalidResp = retryRespHandler; 396b52664e2SAppaRao Puli } 3974bbf237fSAppaRao Puli 398*5e44e3d8SAppaRao Puli explicit Subscription(crow::sse_socket::Connection& connIn) : 399*5e44e3d8SAppaRao Puli sseConn(&connIn) 400*5e44e3d8SAppaRao Puli {} 401*5e44e3d8SAppaRao Puli 4029f616dd1SEd Tanous ~Subscription() = default; 403b52664e2SAppaRao Puli 404*5e44e3d8SAppaRao Puli bool sendEvent(std::string&& msg) 405b52664e2SAppaRao Puli { 4066ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 4076ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 4086ba8c82eSsunharis_in .getEventServiceConfig(); 4096ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 4106ba8c82eSsunharis_in { 4116ba8c82eSsunharis_in return false; 4126ba8c82eSsunharis_in } 4136ba8c82eSsunharis_in 414e38778a5SAppaRao Puli bool useSSL = (uriProto == "https"); 415f52c03c1SCarson Labrado // A connection pool will be created if one does not already exist 416*5e44e3d8SAppaRao Puli if (client) 417*5e44e3d8SAppaRao Puli { 418*5e44e3d8SAppaRao Puli client->sendData(std::move(msg), host, port, path, useSSL, 419*5e44e3d8SAppaRao Puli httpHeaders, boost::beast::http::verb::post); 420*5e44e3d8SAppaRao Puli return true; 421*5e44e3d8SAppaRao Puli } 4227adb85acSSunitha Harish 4234bbf237fSAppaRao Puli if (sseConn != nullptr) 4244bbf237fSAppaRao Puli { 425*5e44e3d8SAppaRao Puli eventSeqNum++; 426*5e44e3d8SAppaRao Puli sseConn->sendEvent(std::to_string(eventSeqNum), msg); 4274bbf237fSAppaRao Puli } 4286ba8c82eSsunharis_in return true; 4294bbf237fSAppaRao Puli } 4304bbf237fSAppaRao Puli 4316ba8c82eSsunharis_in bool sendTestEventLog() 4320b4bdd93SAppaRao Puli { 4330b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 4340b4bdd93SAppaRao Puli logEntryArray.push_back({}); 4350b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 4360b4bdd93SAppaRao Puli 437613dabeaSEd Tanous logEntryJson["EventId"] = "TestID"; 438613dabeaSEd Tanous logEntryJson["EventType"] = "Event"; 439613dabeaSEd Tanous logEntryJson["Severity"] = "OK"; 440613dabeaSEd Tanous logEntryJson["Message"] = "Generated test event"; 441613dabeaSEd Tanous logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog"; 442613dabeaSEd Tanous logEntryJson["MessageArgs"] = nlohmann::json::array(); 443613dabeaSEd Tanous logEntryJson["EventTimestamp"] = 444613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 445613dabeaSEd Tanous logEntryJson["Context"] = customText; 4460b4bdd93SAppaRao Puli 4471476687dSEd Tanous nlohmann::json msg; 4481476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4491476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4501476687dSEd Tanous msg["Name"] = "Event Log"; 4511476687dSEd Tanous msg["Events"] = logEntryArray; 4520b4bdd93SAppaRao Puli 45389492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 45489492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 455*5e44e3d8SAppaRao Puli return sendEvent(std::move(strMsg)); 4560b4bdd93SAppaRao Puli } 4570b4bdd93SAppaRao Puli 4587f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 4597f4eb588SAppaRao Puli void filterAndSendEventLogs( 4607f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4617f4eb588SAppaRao Puli { 4627f4eb588SAppaRao Puli nlohmann::json logEntryArray; 4637f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4647f4eb588SAppaRao Puli { 4657f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 4667f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 4677f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 4687f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 4697f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 4705e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 4717f4eb588SAppaRao Puli 4727f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 4737f4eb588SAppaRao Puli // send everything. 47426f6976fSEd Tanous if (!registryPrefixes.empty()) 4757f4eb588SAppaRao Puli { 4767f4eb588SAppaRao Puli auto obj = std::find(registryPrefixes.begin(), 4777f4eb588SAppaRao Puli registryPrefixes.end(), registryName); 4787f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 4797f4eb588SAppaRao Puli { 4807f4eb588SAppaRao Puli continue; 4817f4eb588SAppaRao Puli } 4827f4eb588SAppaRao Puli } 4837f4eb588SAppaRao Puli 4847f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 4857f4eb588SAppaRao Puli // send everything. 48626f6976fSEd Tanous if (!registryMsgIds.empty()) 4877f4eb588SAppaRao Puli { 4887f4eb588SAppaRao Puli auto obj = std::find(registryMsgIds.begin(), 4897f4eb588SAppaRao Puli registryMsgIds.end(), messageKey); 4907f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 4917f4eb588SAppaRao Puli { 4927f4eb588SAppaRao Puli continue; 4937f4eb588SAppaRao Puli } 4947f4eb588SAppaRao Puli } 4957f4eb588SAppaRao Puli 496c5ba4c27SEd Tanous std::vector<std::string_view> messageArgsView(messageArgs.begin(), 497c5ba4c27SEd Tanous messageArgs.end()); 498c5ba4c27SEd Tanous 4997f4eb588SAppaRao Puli logEntryArray.push_back({}); 5007f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 501c5ba4c27SEd Tanous if (event_log::formatEventLogEntry(idStr, messageID, 502c5ba4c27SEd Tanous messageArgsView, timestamp, 503c5ba4c27SEd Tanous customText, bmcLogEntry) != 0) 5047f4eb588SAppaRao Puli { 5057f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 5067f4eb588SAppaRao Puli continue; 5077f4eb588SAppaRao Puli } 5087f4eb588SAppaRao Puli } 5097f4eb588SAppaRao Puli 51026f6976fSEd Tanous if (logEntryArray.empty()) 5117f4eb588SAppaRao Puli { 5127f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 5137f4eb588SAppaRao Puli return; 5147f4eb588SAppaRao Puli } 5157f4eb588SAppaRao Puli 5161476687dSEd Tanous nlohmann::json msg; 5171476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 5181476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 5191476687dSEd Tanous msg["Name"] = "Event Log"; 5201476687dSEd Tanous msg["Events"] = logEntryArray; 52189492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 52289492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 523*5e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 524*5e44e3d8SAppaRao Puli eventSeqNum++; 5257f4eb588SAppaRao Puli } 5267f4eb588SAppaRao Puli #endif 5277f4eb588SAppaRao Puli 528248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 5291e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 530156d6b00SAppaRao Puli { 531ef4c65b7SEd Tanous boost::urls::url mrdUri = boost::urls::format( 532ef4c65b7SEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", 533ef4c65b7SEd Tanous reportId); 534156d6b00SAppaRao Puli 535156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 53626f6976fSEd Tanous if (!metricReportDefinitions.empty()) 537156d6b00SAppaRao Puli { 538156d6b00SAppaRao Puli if (std::find(metricReportDefinitions.begin(), 539156d6b00SAppaRao Puli metricReportDefinitions.end(), 540079360aeSEd Tanous mrdUri.buffer()) == metricReportDefinitions.end()) 541156d6b00SAppaRao Puli { 542156d6b00SAppaRao Puli return; 543156d6b00SAppaRao Puli } 544156d6b00SAppaRao Puli } 545156d6b00SAppaRao Puli 546c0353249SWludzik, Jozef nlohmann::json msg; 547248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 548156d6b00SAppaRao Puli { 549c0353249SWludzik, Jozef BMCWEB_LOG_ERROR << "Failed to fill the MetricReport for DBus " 550c0353249SWludzik, Jozef "Report with id " 551248d0230SEd Tanous << reportId; 552c0353249SWludzik, Jozef return; 553156d6b00SAppaRao Puli } 554156d6b00SAppaRao Puli 55522daffd7SAppaRao Puli // Context is set by user during Event subscription and it must be 55622daffd7SAppaRao Puli // set for MetricReport response. 55722daffd7SAppaRao Puli if (!customText.empty()) 55822daffd7SAppaRao Puli { 55922daffd7SAppaRao Puli msg["Context"] = customText; 56022daffd7SAppaRao Puli } 56122daffd7SAppaRao Puli 56289492a15SPatrick Williams std::string strMsg = msg.dump(2, ' ', true, 56389492a15SPatrick Williams nlohmann::json::error_handler_t::replace); 564*5e44e3d8SAppaRao Puli sendEvent(std::move(strMsg)); 565156d6b00SAppaRao Puli } 566156d6b00SAppaRao Puli 567d14a48ffSCarson Labrado void updateRetryConfig(uint32_t retryAttempts, 568d14a48ffSCarson Labrado uint32_t retryTimeoutInterval) 569fe44eb0bSAyushi Smriti { 570d14a48ffSCarson Labrado policy->maxRetryAttempts = retryAttempts; 571d14a48ffSCarson Labrado policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval); 57262de0c68SAppaRao Puli } 573fe44eb0bSAyushi Smriti 5749eb808c1SEd Tanous uint64_t getEventSeqNum() const 57596330b99SSunitha Harish { 57696330b99SSunitha Harish return eventSeqNum; 57796330b99SSunitha Harish } 57896330b99SSunitha Harish 579*5e44e3d8SAppaRao Puli void setSubscriptionId(const std::string& id2) 580*5e44e3d8SAppaRao Puli { 581*5e44e3d8SAppaRao Puli BMCWEB_LOG_DEBUG << "Subscription ID: " << id2; 582*5e44e3d8SAppaRao Puli subId = id2; 583*5e44e3d8SAppaRao Puli } 584*5e44e3d8SAppaRao Puli 585*5e44e3d8SAppaRao Puli std::string getSubscriptionId() 586*5e44e3d8SAppaRao Puli { 587*5e44e3d8SAppaRao Puli return subId; 588*5e44e3d8SAppaRao Puli } 589*5e44e3d8SAppaRao Puli 590*5e44e3d8SAppaRao Puli bool matchSseId(const crow::sse_socket::Connection& thisConn) 591*5e44e3d8SAppaRao Puli { 592*5e44e3d8SAppaRao Puli return &thisConn == sseConn; 593*5e44e3d8SAppaRao Puli } 594*5e44e3d8SAppaRao Puli 595b52664e2SAppaRao Puli private: 596*5e44e3d8SAppaRao Puli std::string subId; 597d14a48ffSCarson Labrado uint64_t eventSeqNum = 1; 598b52664e2SAppaRao Puli std::string host; 599eb1c47d3SEd Tanous uint16_t port = 0; 600d14a48ffSCarson Labrado std::shared_ptr<crow::ConnectionPolicy> policy; 601*5e44e3d8SAppaRao Puli crow::sse_socket::Connection* sseConn = nullptr; 602*5e44e3d8SAppaRao Puli std::optional<crow::HttpClient> client; 603b52664e2SAppaRao Puli std::string path; 604b52664e2SAppaRao Puli std::string uriProto; 605a7a80296SCarson Labrado 606a7a80296SCarson Labrado // Check used to indicate what response codes are valid as part of our retry 607a7a80296SCarson Labrado // policy. 2XX is considered acceptable 608a7a80296SCarson Labrado static boost::system::error_code retryRespHandler(unsigned int respCode) 609a7a80296SCarson Labrado { 610a7a80296SCarson Labrado BMCWEB_LOG_DEBUG 611a7a80296SCarson Labrado << "Checking response code validity for SubscriptionEvent"; 612a7a80296SCarson Labrado if ((respCode < 200) || (respCode >= 300)) 613a7a80296SCarson Labrado { 614a7a80296SCarson Labrado return boost::system::errc::make_error_code( 615a7a80296SCarson Labrado boost::system::errc::result_out_of_range); 616a7a80296SCarson Labrado } 617a7a80296SCarson Labrado 618a7a80296SCarson Labrado // Return 0 if the response code is valid 619a7a80296SCarson Labrado return boost::system::errc::make_error_code( 620a7a80296SCarson Labrado boost::system::errc::success); 6219fa6d147SNan Zhou } 622b52664e2SAppaRao Puli }; 623b52664e2SAppaRao Puli 624b52664e2SAppaRao Puli class EventServiceManager 625b52664e2SAppaRao Puli { 626b52664e2SAppaRao Puli private: 627d3a9e084SEd Tanous bool serviceEnabled = false; 628d3a9e084SEd Tanous uint32_t retryAttempts = 0; 629d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 6307d1cc387SAppaRao Puli 6312558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 6329f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 6339f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 63459d494eeSPatrick Williams std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor; 635b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 636b52664e2SAppaRao Puli subscriptionsMap; 637b52664e2SAppaRao Puli 6389f616dd1SEd Tanous uint64_t eventId{1}; 63996330b99SSunitha Harish 640f8ca6d79SEd Tanous boost::asio::io_context& ioc; 641f8ca6d79SEd Tanous 642b52664e2SAppaRao Puli public: 6439f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 6449f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 6459f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 6469f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 647ecd6a3a2SEd Tanous ~EventServiceManager() = default; 6489f616dd1SEd Tanous 649f8ca6d79SEd Tanous explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn) 650b52664e2SAppaRao Puli { 651f8ca6d79SEd Tanous // Load config from persist store. 652f8ca6d79SEd Tanous initConfig(); 653f8ca6d79SEd Tanous } 654f8ca6d79SEd Tanous 655f8ca6d79SEd Tanous static EventServiceManager& 656f8ca6d79SEd Tanous getInstance(boost::asio::io_context* ioc = nullptr) 657f8ca6d79SEd Tanous { 658f8ca6d79SEd Tanous static EventServiceManager handler(*ioc); 659b52664e2SAppaRao Puli return handler; 660b52664e2SAppaRao Puli } 661b52664e2SAppaRao Puli 6621bf712bcSAyushi Smriti void initConfig() 6631bf712bcSAyushi Smriti { 66428afb49cSJunLin Chen loadOldBehavior(); 6651bf712bcSAyushi Smriti 66628afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 66728afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 66828afb49cSJunLin Chen .getEventServiceConfig(); 6691bf712bcSAyushi Smriti 67028afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 67128afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 67228afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6731bf712bcSAyushi Smriti 67428afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 67528afb49cSJunLin Chen .subscriptionsConfigMap) 6761bf712bcSAyushi Smriti { 67728afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 67828afb49cSJunLin Chen it.second; 6794bbf237fSAppaRao Puli 6801bf712bcSAyushi Smriti std::string host; 6811bf712bcSAyushi Smriti std::string urlProto; 682eb1c47d3SEd Tanous uint16_t port = 0; 6831bf712bcSAyushi Smriti std::string path; 68411baefe4SEd Tanous bool status = crow::utility::validateAndSplitUrl( 68511baefe4SEd Tanous newSub->destinationUrl, urlProto, host, port, path); 6861bf712bcSAyushi Smriti 6871bf712bcSAyushi Smriti if (!status) 6881bf712bcSAyushi Smriti { 6891bf712bcSAyushi Smriti BMCWEB_LOG_ERROR 6901bf712bcSAyushi Smriti << "Failed to validate and split destination url"; 6911bf712bcSAyushi Smriti continue; 6921bf712bcSAyushi Smriti } 6931bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 694f8ca6d79SEd Tanous std::make_shared<Subscription>(host, port, path, urlProto, ioc); 6951bf712bcSAyushi Smriti 69628afb49cSJunLin Chen subValue->id = newSub->id; 69728afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 69828afb49cSJunLin Chen subValue->protocol = newSub->protocol; 69928afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 70028afb49cSJunLin Chen subValue->customText = newSub->customText; 70128afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 70228afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 70328afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 70428afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 70528afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 70628afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 70728afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 7081bf712bcSAyushi Smriti 70928afb49cSJunLin Chen if (subValue->id.empty()) 7101bf712bcSAyushi Smriti { 7111bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Failed to add subscription"; 7121bf712bcSAyushi Smriti } 71328afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 71428afb49cSJunLin Chen 71528afb49cSJunLin Chen updateNoOfSubscribersCount(); 71628afb49cSJunLin Chen 71728afb49cSJunLin Chen #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 7182558979cSP Dheeraj Srujan Kumar 7192558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 7202558979cSP Dheeraj Srujan Kumar 72128afb49cSJunLin Chen #endif 72228afb49cSJunLin Chen // Update retry configuration. 72328afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 7241bf712bcSAyushi Smriti } 7251bf712bcSAyushi Smriti } 7261bf712bcSAyushi Smriti 72756d2396dSEd Tanous static void loadOldBehavior() 728b52664e2SAppaRao Puli { 72928afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 73028afb49cSJunLin Chen if (!eventConfigFile.good()) 7311bf712bcSAyushi Smriti { 73228afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Old eventService config not exist"; 73328afb49cSJunLin Chen return; 73428afb49cSJunLin Chen } 73528afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 73628afb49cSJunLin Chen if (jsonData.is_discarded()) 7374bbf237fSAppaRao Puli { 73828afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Old eventService config parse error."; 73928afb49cSJunLin Chen return; 74028afb49cSJunLin Chen } 74128afb49cSJunLin Chen 74228afb49cSJunLin Chen for (const auto& item : jsonData.items()) 74328afb49cSJunLin Chen { 74428afb49cSJunLin Chen if (item.key() == "Configuration") 74528afb49cSJunLin Chen { 74628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 74728afb49cSJunLin Chen .getEventServiceConfig() 74828afb49cSJunLin Chen .fromJson(item.value()); 74928afb49cSJunLin Chen } 75028afb49cSJunLin Chen else if (item.key() == "Subscriptions") 75128afb49cSJunLin Chen { 75228afb49cSJunLin Chen for (const auto& elem : item.value()) 75328afb49cSJunLin Chen { 75428afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 75528afb49cSJunLin Chen newSubscription = 75628afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 75728afb49cSJunLin Chen true); 75828afb49cSJunLin Chen if (newSubscription == nullptr) 75928afb49cSJunLin Chen { 76028afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Problem reading subscription " 76128afb49cSJunLin Chen "from old persistent store"; 7624bbf237fSAppaRao Puli continue; 7634bbf237fSAppaRao Puli } 7641bf712bcSAyushi Smriti 76528afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 76628afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 7671bf712bcSAyushi Smriti 76828afb49cSJunLin Chen std::string id; 7691bf712bcSAyushi Smriti 77028afb49cSJunLin Chen int retry = 3; 771e662eae8SEd Tanous while (retry != 0) 7721bf712bcSAyushi Smriti { 77328afb49cSJunLin Chen id = std::to_string(dist(gen)); 77428afb49cSJunLin Chen if (gen.error()) 7757d1cc387SAppaRao Puli { 77628afb49cSJunLin Chen retry = 0; 77728afb49cSJunLin Chen break; 77828afb49cSJunLin Chen } 77928afb49cSJunLin Chen newSubscription->id = id; 78028afb49cSJunLin Chen auto inserted = 78128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 78228afb49cSJunLin Chen .subscriptionsConfigMap.insert( 78328afb49cSJunLin Chen std::pair(id, newSubscription)); 78428afb49cSJunLin Chen if (inserted.second) 78528afb49cSJunLin Chen { 78628afb49cSJunLin Chen break; 78728afb49cSJunLin Chen } 78828afb49cSJunLin Chen --retry; 7897d1cc387SAppaRao Puli } 7907d1cc387SAppaRao Puli 79128afb49cSJunLin Chen if (retry <= 0) 79228afb49cSJunLin Chen { 79328afb49cSJunLin Chen BMCWEB_LOG_ERROR 79428afb49cSJunLin Chen << "Failed to generate random number from old " 79528afb49cSJunLin Chen "persistent store"; 79628afb49cSJunLin Chen continue; 79728afb49cSJunLin Chen } 79828afb49cSJunLin Chen } 79928afb49cSJunLin Chen } 80028afb49cSJunLin Chen 80128afb49cSJunLin Chen persistent_data::getConfig().writeData(); 80228afb49cSJunLin Chen std::remove(eventServiceFile); 80328afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Remove old eventservice config"; 80428afb49cSJunLin Chen } 80528afb49cSJunLin Chen } 80628afb49cSJunLin Chen 8079eb808c1SEd Tanous void updateSubscriptionData() const 80828afb49cSJunLin Chen { 80928afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 81028afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 81128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 81228afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 81328afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 81428afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 81528afb49cSJunLin Chen 81628afb49cSJunLin Chen persistent_data::getConfig().writeData(); 81728afb49cSJunLin Chen } 81828afb49cSJunLin Chen 81928afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 8207d1cc387SAppaRao Puli { 8217d1cc387SAppaRao Puli bool updateConfig = false; 822fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 8237d1cc387SAppaRao Puli 82428afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 8257d1cc387SAppaRao Puli { 82628afb49cSJunLin Chen serviceEnabled = cfg.enabled; 827e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 8287d1cc387SAppaRao Puli { 8297d1cc387SAppaRao Puli registerMetricReportSignal(); 8307d1cc387SAppaRao Puli } 8317d1cc387SAppaRao Puli else 8327d1cc387SAppaRao Puli { 8337d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8347d1cc387SAppaRao Puli } 8357d1cc387SAppaRao Puli updateConfig = true; 8367d1cc387SAppaRao Puli } 8377d1cc387SAppaRao Puli 83828afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 8397d1cc387SAppaRao Puli { 84028afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 8417d1cc387SAppaRao Puli updateConfig = true; 842fe44eb0bSAyushi Smriti updateRetryCfg = true; 8437d1cc387SAppaRao Puli } 8447d1cc387SAppaRao Puli 84528afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 8467d1cc387SAppaRao Puli { 84728afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 8487d1cc387SAppaRao Puli updateConfig = true; 849fe44eb0bSAyushi Smriti updateRetryCfg = true; 8507d1cc387SAppaRao Puli } 8517d1cc387SAppaRao Puli 8527d1cc387SAppaRao Puli if (updateConfig) 8537d1cc387SAppaRao Puli { 8547d1cc387SAppaRao Puli updateSubscriptionData(); 8557d1cc387SAppaRao Puli } 856fe44eb0bSAyushi Smriti 857fe44eb0bSAyushi Smriti if (updateRetryCfg) 858fe44eb0bSAyushi Smriti { 859fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 860fe44eb0bSAyushi Smriti for (const auto& it : 861fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 862fe44eb0bSAyushi Smriti { 863*5e44e3d8SAppaRao Puli Subscription& entry = *it.second; 864*5e44e3d8SAppaRao Puli entry.updateRetryConfig(retryAttempts, retryTimeoutInterval); 865fe44eb0bSAyushi Smriti } 866fe44eb0bSAyushi Smriti } 8677d1cc387SAppaRao Puli } 8687d1cc387SAppaRao Puli 8697d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8707d1cc387SAppaRao Puli { 8717d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8727d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8737d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8747d1cc387SAppaRao Puli { 8757d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8767d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8777d1cc387SAppaRao Puli { 8787d1cc387SAppaRao Puli eventLogSubCount++; 8797d1cc387SAppaRao Puli } 8807d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8817d1cc387SAppaRao Puli { 8827d1cc387SAppaRao Puli metricReportSubCount++; 8837d1cc387SAppaRao Puli } 8847d1cc387SAppaRao Puli } 8857d1cc387SAppaRao Puli 8867d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8877d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8887d1cc387SAppaRao Puli { 8897d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 890e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 8917d1cc387SAppaRao Puli { 8927d1cc387SAppaRao Puli registerMetricReportSignal(); 8937d1cc387SAppaRao Puli } 8947d1cc387SAppaRao Puli else 8957d1cc387SAppaRao Puli { 8967d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8977d1cc387SAppaRao Puli } 8987d1cc387SAppaRao Puli } 8997d1cc387SAppaRao Puli } 9007d1cc387SAppaRao Puli 901b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 902b52664e2SAppaRao Puli { 903b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 904b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 905b52664e2SAppaRao Puli { 906b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 907b52664e2SAppaRao Puli return nullptr; 908b52664e2SAppaRao Puli } 909b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 910b52664e2SAppaRao Puli return subValue; 911b52664e2SAppaRao Puli } 912b52664e2SAppaRao Puli 913b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 9141bf712bcSAyushi Smriti const bool updateFile = true) 915b52664e2SAppaRao Puli { 916fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 917fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 918fc76b8acSEd Tanous 919b52664e2SAppaRao Puli std::string id; 920b52664e2SAppaRao Puli 921b52664e2SAppaRao Puli int retry = 3; 922e662eae8SEd Tanous while (retry != 0) 923b52664e2SAppaRao Puli { 924fc76b8acSEd Tanous id = std::to_string(dist(gen)); 925fc76b8acSEd Tanous if (gen.error()) 926fc76b8acSEd Tanous { 927fc76b8acSEd Tanous retry = 0; 928fc76b8acSEd Tanous break; 929fc76b8acSEd Tanous } 930b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 931b52664e2SAppaRao Puli if (inserted.second) 932b52664e2SAppaRao Puli { 933b52664e2SAppaRao Puli break; 934b52664e2SAppaRao Puli } 935b52664e2SAppaRao Puli --retry; 93623a21a1cSEd Tanous } 937b52664e2SAppaRao Puli 938b52664e2SAppaRao Puli if (retry <= 0) 939b52664e2SAppaRao Puli { 940b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Failed to generate random number"; 941abb93cddSEd Tanous return ""; 942b52664e2SAppaRao Puli } 943b52664e2SAppaRao Puli 94428afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 94528afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 94628afb49cSJunLin Chen newSub->id = id; 94728afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 94828afb49cSJunLin Chen newSub->protocol = subValue->protocol; 94928afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 95028afb49cSJunLin Chen newSub->customText = subValue->customText; 95128afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 95228afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 95328afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 95428afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 95528afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 95628afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 95728afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 95828afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 95928afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 96028afb49cSJunLin Chen 9617d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 9621bf712bcSAyushi Smriti 9631bf712bcSAyushi Smriti if (updateFile) 9641bf712bcSAyushi Smriti { 965b52664e2SAppaRao Puli updateSubscriptionData(); 9661bf712bcSAyushi Smriti } 9677f4eb588SAppaRao Puli 9687f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 9692558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 9707f4eb588SAppaRao Puli { 9712558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 9727f4eb588SAppaRao Puli } 9737f4eb588SAppaRao Puli #endif 974fe44eb0bSAyushi Smriti // Update retry configuration. 975fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 976fe44eb0bSAyushi Smriti 977*5e44e3d8SAppaRao Puli // Set Subscription ID for back trace 978*5e44e3d8SAppaRao Puli subValue->setSubscriptionId(id); 979b52664e2SAppaRao Puli return id; 980b52664e2SAppaRao Puli } 981b52664e2SAppaRao Puli 982b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 983b52664e2SAppaRao Puli { 984b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 98555f79e6fSEd Tanous return obj != subscriptionsMap.end(); 986b52664e2SAppaRao Puli } 987b52664e2SAppaRao Puli 988b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 989b52664e2SAppaRao Puli { 990b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 991b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 992b52664e2SAppaRao Puli { 993b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 99428afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 99528afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 99628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 99728afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9987d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 999b52664e2SAppaRao Puli updateSubscriptionData(); 1000b52664e2SAppaRao Puli } 1001b52664e2SAppaRao Puli } 1002b52664e2SAppaRao Puli 1003*5e44e3d8SAppaRao Puli void deleteSseSubscription(const crow::sse_socket::Connection& thisConn) 1004*5e44e3d8SAppaRao Puli { 1005*5e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 1006*5e44e3d8SAppaRao Puli { 1007*5e44e3d8SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 1008*5e44e3d8SAppaRao Puli bool entryIsThisConn = entry->matchSseId(thisConn); 1009*5e44e3d8SAppaRao Puli if (entryIsThisConn) 1010*5e44e3d8SAppaRao Puli { 1011*5e44e3d8SAppaRao Puli persistent_data::EventServiceStore::getInstance() 1012*5e44e3d8SAppaRao Puli .subscriptionsConfigMap.erase( 1013*5e44e3d8SAppaRao Puli it.second->getSubscriptionId()); 1014*5e44e3d8SAppaRao Puli return; 1015*5e44e3d8SAppaRao Puli } 1016*5e44e3d8SAppaRao Puli } 1017*5e44e3d8SAppaRao Puli } 1018*5e44e3d8SAppaRao Puli 1019*5e44e3d8SAppaRao Puli size_t getNumberOfSubscriptions() const 1020b52664e2SAppaRao Puli { 1021b52664e2SAppaRao Puli return subscriptionsMap.size(); 1022b52664e2SAppaRao Puli } 1023b52664e2SAppaRao Puli 1024*5e44e3d8SAppaRao Puli size_t getNumberOfSSESubscriptions() const 1025*5e44e3d8SAppaRao Puli { 1026*5e44e3d8SAppaRao Puli auto size = std::count_if( 1027*5e44e3d8SAppaRao Puli subscriptionsMap.begin(), subscriptionsMap.end(), 1028*5e44e3d8SAppaRao Puli [](const std::pair<std::string, std::shared_ptr<Subscription>>& 1029*5e44e3d8SAppaRao Puli entry) { 1030*5e44e3d8SAppaRao Puli return (entry.second->subscriptionType == subscriptionTypeSSE); 1031*5e44e3d8SAppaRao Puli }); 1032*5e44e3d8SAppaRao Puli return static_cast<size_t>(size); 1033*5e44e3d8SAppaRao Puli } 1034*5e44e3d8SAppaRao Puli 1035b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 1036b52664e2SAppaRao Puli { 1037b52664e2SAppaRao Puli std::vector<std::string> idList; 1038b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 1039b52664e2SAppaRao Puli { 1040b52664e2SAppaRao Puli idList.emplace_back(it.first); 1041b52664e2SAppaRao Puli } 1042b52664e2SAppaRao Puli return idList; 1043b52664e2SAppaRao Puli } 1044b52664e2SAppaRao Puli 1045*5e44e3d8SAppaRao Puli bool isDestinationExist(const std::string& destUrl) const 1046b52664e2SAppaRao Puli { 1047b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 1048b52664e2SAppaRao Puli { 1049b52664e2SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 1050b52664e2SAppaRao Puli if (entry->destinationUrl == destUrl) 1051b52664e2SAppaRao Puli { 1052b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 1053b52664e2SAppaRao Puli return true; 1054b52664e2SAppaRao Puli } 1055b52664e2SAppaRao Puli } 1056b52664e2SAppaRao Puli return false; 1057b52664e2SAppaRao Puli } 10580b4bdd93SAppaRao Puli 10596ba8c82eSsunharis_in bool sendTestEventLog() 10600b4bdd93SAppaRao Puli { 1061*5e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 10620b4bdd93SAppaRao Puli { 10630b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 10646ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 10656ba8c82eSsunharis_in { 10666ba8c82eSsunharis_in return false; 10670b4bdd93SAppaRao Puli } 10680b4bdd93SAppaRao Puli } 10696ba8c82eSsunharis_in return true; 10706ba8c82eSsunharis_in } 1071e9a14131SAppaRao Puli 1072613dabeaSEd Tanous void sendEvent(nlohmann::json eventMessage, const std::string& origin, 1073613dabeaSEd Tanous const std::string& resType) 107496330b99SSunitha Harish { 1075dbb59d4dSEd Tanous if (!serviceEnabled || (noOfEventLogSubscribers == 0U)) 10766ba8c82eSsunharis_in { 10776ba8c82eSsunharis_in BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 10786ba8c82eSsunharis_in return; 10796ba8c82eSsunharis_in } 108096330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 108196330b99SSunitha Harish 1082613dabeaSEd Tanous eventMessage["EventId"] = eventId; 1083613dabeaSEd Tanous // MemberId is 0 : since we are sending one event record. 1084613dabeaSEd Tanous eventMessage["MemberId"] = 0; 1085613dabeaSEd Tanous eventMessage["EventTimestamp"] = 1086613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1087613dabeaSEd Tanous eventMessage["OriginOfCondition"] = origin; 1088613dabeaSEd Tanous 1089613dabeaSEd Tanous eventRecord.emplace_back(std::move(eventMessage)); 109096330b99SSunitha Harish 1091*5e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 109296330b99SSunitha Harish { 109396330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 109496330b99SSunitha Harish bool isSubscribed = false; 109596330b99SSunitha Harish // Search the resourceTypes list for the subscription. 109696330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 109796330b99SSunitha Harish // send everything. 109826f6976fSEd Tanous if (!entry->resourceTypes.empty()) 109996330b99SSunitha Harish { 11003174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 110196330b99SSunitha Harish { 110296330b99SSunitha Harish if (resType == resource) 110396330b99SSunitha Harish { 110496330b99SSunitha Harish BMCWEB_LOG_INFO << "ResourceType " << resource 110596330b99SSunitha Harish << " found in the subscribed list"; 110696330b99SSunitha Harish isSubscribed = true; 110796330b99SSunitha Harish break; 110896330b99SSunitha Harish } 110996330b99SSunitha Harish } 111096330b99SSunitha Harish } 111196330b99SSunitha Harish else // resourceTypes list is empty. 111296330b99SSunitha Harish { 111396330b99SSunitha Harish isSubscribed = true; 111496330b99SSunitha Harish } 111596330b99SSunitha Harish if (isSubscribed) 111696330b99SSunitha Harish { 1117613dabeaSEd Tanous nlohmann::json msgJson; 1118613dabeaSEd Tanous 1119613dabeaSEd Tanous msgJson["@odata.type"] = "#Event.v1_4_0.Event"; 1120613dabeaSEd Tanous msgJson["Name"] = "Event Log"; 1121613dabeaSEd Tanous msgJson["Id"] = eventId; 1122613dabeaSEd Tanous msgJson["Events"] = eventRecord; 1123f52c03c1SCarson Labrado 1124f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1125f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 1126*5e44e3d8SAppaRao Puli entry->sendEvent(std::move(strMsg)); 112796330b99SSunitha Harish eventId++; // increament the eventId 112896330b99SSunitha Harish } 112996330b99SSunitha Harish else 113096330b99SSunitha Harish { 113196330b99SSunitha Harish BMCWEB_LOG_INFO << "Not subscribed to this resource"; 113296330b99SSunitha Harish } 113396330b99SSunitha Harish } 113496330b99SSunitha Harish } 11355738de59SAsmitha Karunanithi void sendBroadcastMsg(const std::string& broadcastMsg) 11365738de59SAsmitha Karunanithi { 1137*5e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 11385738de59SAsmitha Karunanithi { 11395738de59SAsmitha Karunanithi std::shared_ptr<Subscription> entry = it.second; 1140613dabeaSEd Tanous nlohmann::json msgJson; 1141613dabeaSEd Tanous msgJson["Timestamp"] = 1142613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1143613dabeaSEd Tanous msgJson["OriginOfCondition"] = "/ibm/v1/HMC/BroadcastService"; 1144613dabeaSEd Tanous msgJson["Name"] = "Broadcast Message"; 1145613dabeaSEd Tanous msgJson["Message"] = broadcastMsg; 1146f52c03c1SCarson Labrado 1147f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1148f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 1149*5e44e3d8SAppaRao Puli entry->sendEvent(std::move(strMsg)); 11505738de59SAsmitha Karunanithi } 11515738de59SAsmitha Karunanithi } 115296330b99SSunitha Harish 11537f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 11542558979cSP Dheeraj Srujan Kumar 11552558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 11567f4eb588SAppaRao Puli { 11572558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 11582558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 11592558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 11602558979cSP Dheeraj Srujan Kumar } 11612558979cSP Dheeraj Srujan Kumar 11622558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 11632558979cSP Dheeraj Srujan Kumar { 11642558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 11652558979cSP Dheeraj Srujan Kumar 11667f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11677f4eb588SAppaRao Puli if (!logStream.good()) 11687f4eb588SAppaRao Puli { 11697f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 11707f4eb588SAppaRao Puli return; 11717f4eb588SAppaRao Puli } 11727f4eb588SAppaRao Puli std::string logEntry; 11737f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11747f4eb588SAppaRao Puli { 11752558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11767f4eb588SAppaRao Puli } 11772558979cSP Dheeraj Srujan Kumar 11782558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "Next Log Position : " << redfishLogFilePosition; 11797f4eb588SAppaRao Puli } 11807f4eb588SAppaRao Puli 11817f4eb588SAppaRao Puli void readEventLogsFromFile() 11827f4eb588SAppaRao Puli { 11837f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11847f4eb588SAppaRao Puli if (!logStream.good()) 11857f4eb588SAppaRao Puli { 11867f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed"; 11877f4eb588SAppaRao Puli return; 11887f4eb588SAppaRao Puli } 11897f4eb588SAppaRao Puli 11907f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11917f4eb588SAppaRao Puli 11927f4eb588SAppaRao Puli std::string logEntry; 11932558979cSP Dheeraj Srujan Kumar 11942558979cSP Dheeraj Srujan Kumar // Get the read pointer to the next log to be read. 11952558979cSP Dheeraj Srujan Kumar logStream.seekg(redfishLogFilePosition); 11962558979cSP Dheeraj Srujan Kumar 11977f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11987f4eb588SAppaRao Puli { 11992558979cSP Dheeraj Srujan Kumar // Update Pointer position 12002558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 12012558979cSP Dheeraj Srujan Kumar 12022558979cSP Dheeraj Srujan Kumar std::string idStr; 12032558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 12047f4eb588SAppaRao Puli { 12057f4eb588SAppaRao Puli continue; 12067f4eb588SAppaRao Puli } 12077f4eb588SAppaRao Puli 1208e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 12097f4eb588SAppaRao Puli { 12102558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 12112558979cSP Dheeraj Srujan Kumar // the remaining items below. 12122558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 12137f4eb588SAppaRao Puli continue; 12147f4eb588SAppaRao Puli } 12157f4eb588SAppaRao Puli 12167f4eb588SAppaRao Puli std::string timestamp; 12177f4eb588SAppaRao Puli std::string messageID; 12185e715de6SAppaRao Puli std::vector<std::string> messageArgs; 12197f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 12207f4eb588SAppaRao Puli messageArgs) != 0) 12217f4eb588SAppaRao Puli { 12227f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 12237f4eb588SAppaRao Puli continue; 12247f4eb588SAppaRao Puli } 12257f4eb588SAppaRao Puli 12267f4eb588SAppaRao Puli std::string registryName; 12277f4eb588SAppaRao Puli std::string messageKey; 12287f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 12297f4eb588SAppaRao Puli messageKey); 12307f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 12317f4eb588SAppaRao Puli { 12327f4eb588SAppaRao Puli continue; 12337f4eb588SAppaRao Puli } 12347f4eb588SAppaRao Puli 12357f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 12367f4eb588SAppaRao Puli messageKey, messageArgs); 12377f4eb588SAppaRao Puli } 12387f4eb588SAppaRao Puli 1239e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 12402558979cSP Dheeraj Srujan Kumar { 12412558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 12422558979cSP Dheeraj Srujan Kumar return; 12432558979cSP Dheeraj Srujan Kumar } 12442558979cSP Dheeraj Srujan Kumar 12452558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 12462558979cSP Dheeraj Srujan Kumar { 12472558979cSP Dheeraj Srujan Kumar // No Records to send 12482558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 12492558979cSP Dheeraj Srujan Kumar return; 12502558979cSP Dheeraj Srujan Kumar } 12512558979cSP Dheeraj Srujan Kumar 1252*5e44e3d8SAppaRao Puli for (const auto& it : subscriptionsMap) 12537f4eb588SAppaRao Puli { 12547f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 12557f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 12567f4eb588SAppaRao Puli { 12577f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 12587f4eb588SAppaRao Puli } 12597f4eb588SAppaRao Puli } 12607f4eb588SAppaRao Puli } 12617f4eb588SAppaRao Puli 12627f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 12637f4eb588SAppaRao Puli { 12646a9f85f9SAppaRao Puli if (!inotifyConn) 12657f4eb588SAppaRao Puli { 12667f4eb588SAppaRao Puli return; 12677f4eb588SAppaRao Puli } 12687f4eb588SAppaRao Puli 12697f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 12707f4eb588SAppaRao Puli 1271002d39b4SEd Tanous inotifyConn->async_read_some(boost::asio::buffer(readBuffer), 12727f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 12737f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 12747f4eb588SAppaRao Puli if (ec) 12757f4eb588SAppaRao Puli { 12767f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 12777f4eb588SAppaRao Puli return; 12787f4eb588SAppaRao Puli } 12797f4eb588SAppaRao Puli std::size_t index = 0; 1280b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 12817f4eb588SAppaRao Puli { 1282d3a9e084SEd Tanous struct inotify_event event 1283d3a9e084SEd Tanous {}; 1284b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1285b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1286b792cc56SAppaRao Puli { 1287b792cc56SAppaRao Puli if ((event.len == 0) || 1288b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1289b792cc56SAppaRao Puli { 1290b792cc56SAppaRao Puli index += (iEventSize + event.len); 1291b792cc56SAppaRao Puli continue; 1292b792cc56SAppaRao Puli } 1293b792cc56SAppaRao Puli 12944f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 12954f568f74SJiaqing Zhao if (fileName != "redfish") 1296b792cc56SAppaRao Puli { 1297b792cc56SAppaRao Puli index += (iEventSize + event.len); 1298b792cc56SAppaRao Puli continue; 1299b792cc56SAppaRao Puli } 1300b792cc56SAppaRao Puli 1301b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1302b792cc56SAppaRao Puli << "Redfish log file created/deleted. event.name: " 1303b792cc56SAppaRao Puli << fileName; 1304b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1305b792cc56SAppaRao Puli { 1306b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1307b792cc56SAppaRao Puli { 1308b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1309016761afSAppaRao Puli << "Remove and Add inotify watcher on " 1310016761afSAppaRao Puli "redfish event log file"; 1311016761afSAppaRao Puli // Remove existing inotify watcher and add 1312016761afSAppaRao Puli // with new redfish event log file. 1313016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1314016761afSAppaRao Puli fileWatchDesc = -1; 1315b792cc56SAppaRao Puli } 1316b792cc56SAppaRao Puli 1317b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1318b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1319b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1320b792cc56SAppaRao Puli { 1321002d39b4SEd Tanous BMCWEB_LOG_ERROR << "inotify_add_watch failed for " 1322b792cc56SAppaRao Puli "redfish log file."; 1323b792cc56SAppaRao Puli return; 1324b792cc56SAppaRao Puli } 1325b792cc56SAppaRao Puli 1326b792cc56SAppaRao Puli EventServiceManager::getInstance() 13272558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1328b792cc56SAppaRao Puli EventServiceManager::getInstance() 1329b792cc56SAppaRao Puli .readEventLogsFromFile(); 1330b792cc56SAppaRao Puli } 1331b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1332b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1333b792cc56SAppaRao Puli { 1334b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1335b792cc56SAppaRao Puli { 1336b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1337b792cc56SAppaRao Puli fileWatchDesc = -1; 1338b792cc56SAppaRao Puli } 1339b792cc56SAppaRao Puli } 1340b792cc56SAppaRao Puli } 1341b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1342b792cc56SAppaRao Puli { 1343b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 13447f4eb588SAppaRao Puli { 13457f4eb588SAppaRao Puli EventServiceManager::getInstance() 13467f4eb588SAppaRao Puli .readEventLogsFromFile(); 13477f4eb588SAppaRao Puli } 1348b792cc56SAppaRao Puli } 1349b792cc56SAppaRao Puli index += (iEventSize + event.len); 13507f4eb588SAppaRao Puli } 13517f4eb588SAppaRao Puli 13527f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13537f4eb588SAppaRao Puli }); 13547f4eb588SAppaRao Puli } 13557f4eb588SAppaRao Puli 13567f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 13577f4eb588SAppaRao Puli { 135823a21a1cSEd Tanous inotifyConn.emplace(ioc); 1359b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1360b792cc56SAppaRao Puli if (inotifyFd == -1) 13617f4eb588SAppaRao Puli { 13627f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "inotify_init1 failed."; 13637f4eb588SAppaRao Puli return -1; 13647f4eb588SAppaRao Puli } 1365b792cc56SAppaRao Puli 1366b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1367b792cc56SAppaRao Puli // create/delete. 1368b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1369b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1370b792cc56SAppaRao Puli if (dirWatchDesc == -1) 13717f4eb588SAppaRao Puli { 13727f4eb588SAppaRao Puli BMCWEB_LOG_ERROR 1373b792cc56SAppaRao Puli << "inotify_add_watch failed for event log directory."; 13747f4eb588SAppaRao Puli return -1; 13757f4eb588SAppaRao Puli } 13767f4eb588SAppaRao Puli 1377b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 137889492a15SPatrick Williams fileWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogFile, 137989492a15SPatrick Williams IN_MODIFY); 1380b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1381b792cc56SAppaRao Puli { 1382b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1383b792cc56SAppaRao Puli << "inotify_add_watch failed for redfish log file."; 1384b792cc56SAppaRao Puli // Don't return error if file not exist. 1385b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1386b792cc56SAppaRao Puli } 1387b792cc56SAppaRao Puli 13887f4eb588SAppaRao Puli // monitor redfish event log file 1389b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13907f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13917f4eb588SAppaRao Puli 13927f4eb588SAppaRao Puli return 0; 13937f4eb588SAppaRao Puli } 13947f4eb588SAppaRao Puli 13957f4eb588SAppaRao Puli #endif 139659d494eeSPatrick Williams static void getReadingsForReport(sdbusplus::message_t& msg) 1397156d6b00SAppaRao Puli { 139856d2396dSEd Tanous if (msg.is_method_error()) 139956d2396dSEd Tanous { 140056d2396dSEd Tanous BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 140156d2396dSEd Tanous return; 140256d2396dSEd Tanous } 140356d2396dSEd Tanous 1404c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1405c0353249SWludzik, Jozef std::string id = path.filename(); 1406c0353249SWludzik, Jozef if (id.empty()) 1407156d6b00SAppaRao Puli { 1408c0353249SWludzik, Jozef BMCWEB_LOG_ERROR << "Failed to get Id from path"; 1409156d6b00SAppaRao Puli return; 1410156d6b00SAppaRao Puli } 1411156d6b00SAppaRao Puli 1412c0353249SWludzik, Jozef std::string interface; 1413b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1414c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1415c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1416c0353249SWludzik, Jozef 1417c0353249SWludzik, Jozef auto found = 1418c0353249SWludzik, Jozef std::find_if(props.begin(), props.end(), 1419c0353249SWludzik, Jozef [](const auto& x) { return x.first == "Readings"; }); 1420c0353249SWludzik, Jozef if (found == props.end()) 1421156d6b00SAppaRao Puli { 1422c0353249SWludzik, Jozef BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 1423156d6b00SAppaRao Puli return; 1424156d6b00SAppaRao Puli } 1425156d6b00SAppaRao Puli 14261e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 14271e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1428e662eae8SEd Tanous if (readings == nullptr) 14291e1e598dSJonathan Doman { 14301e1e598dSJonathan Doman BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 14311e1e598dSJonathan Doman return; 14321e1e598dSJonathan Doman } 14331e1e598dSJonathan Doman 1434156d6b00SAppaRao Puli for (const auto& it : 1435156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1436156d6b00SAppaRao Puli { 1437e05aec50SEd Tanous Subscription& entry = *it.second; 1438c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1439156d6b00SAppaRao Puli { 14401e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1441156d6b00SAppaRao Puli } 1442156d6b00SAppaRao Puli } 1443156d6b00SAppaRao Puli } 1444156d6b00SAppaRao Puli 1445156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1446156d6b00SAppaRao Puli { 14477d1cc387SAppaRao Puli if (matchTelemetryMonitor) 14487d1cc387SAppaRao Puli { 1449156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1450156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1451156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1452156d6b00SAppaRao Puli } 14537d1cc387SAppaRao Puli } 1454156d6b00SAppaRao Puli 1455156d6b00SAppaRao Puli void registerMetricReportSignal() 1456156d6b00SAppaRao Puli { 14577d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1458156d6b00SAppaRao Puli { 14597d1cc387SAppaRao Puli BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1460156d6b00SAppaRao Puli return; 1461156d6b00SAppaRao Puli } 1462156d6b00SAppaRao Puli 1463156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1464c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1465c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1466c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1467156d6b00SAppaRao Puli 146859d494eeSPatrick Williams matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>( 146956d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1470156d6b00SAppaRao Puli } 147123a21a1cSEd Tanous }; 1472b52664e2SAppaRao Puli 1473b52664e2SAppaRao Puli } // namespace redfish 1474