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> 38b5b40605Snitroglycerine #include <sdbusplus/bus/match.hpp> 391214b7e7SGunnar Mills 40b52664e2SAppaRao Puli #include <cstdlib> 41b52664e2SAppaRao Puli #include <ctime> 421bf712bcSAyushi Smriti #include <fstream> 43b52664e2SAppaRao Puli #include <memory> 4426702d01SEd Tanous #include <span> 45b52664e2SAppaRao Puli 46b52664e2SAppaRao Puli namespace redfish 47b52664e2SAppaRao Puli { 48156d6b00SAppaRao Puli 49156d6b00SAppaRao Puli using ReadingsObjType = 5093f5d7c7SWludzik, Jozef std::vector<std::tuple<std::string, std::string, double, int32_t>>; 51156d6b00SAppaRao Puli 52156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 53156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 54156d6b00SAppaRao Puli 551bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 561bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 571bf712bcSAyushi Smriti 58fffb8c1fSEd Tanous namespace registries 597f4eb588SAppaRao Puli { 6026702d01SEd Tanous inline std::span<const MessageEntry> 61b304bd79SP Dheeraj Srujan Kumar getRegistryFromPrefix(const std::string& registryName) 62b304bd79SP Dheeraj Srujan Kumar { 63b304bd79SP Dheeraj Srujan Kumar if (task_event::header.registryPrefix == registryName) 64b304bd79SP Dheeraj Srujan Kumar { 6526702d01SEd Tanous return {task_event::registry}; 66b304bd79SP Dheeraj Srujan Kumar } 67b304bd79SP Dheeraj Srujan Kumar if (openbmc::header.registryPrefix == registryName) 68b304bd79SP Dheeraj Srujan Kumar { 6926702d01SEd Tanous return {openbmc::registry}; 70b304bd79SP Dheeraj Srujan Kumar } 71b304bd79SP Dheeraj Srujan Kumar if (base::header.registryPrefix == registryName) 72b304bd79SP Dheeraj Srujan Kumar { 7326702d01SEd Tanous return {base::registry}; 74b304bd79SP Dheeraj Srujan Kumar } 7526702d01SEd Tanous return {openbmc::registry}; 76b304bd79SP Dheeraj Srujan Kumar } 77fffb8c1fSEd Tanous } // namespace registries 78b304bd79SP Dheeraj Srujan Kumar 794642bf8fSGeorge Liu #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 80cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 814642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 824642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 834642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 844642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 85cf9e417dSEd Tanous 86cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 874642bf8fSGeorge Liu static int inotifyFd = -1; 88cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 894642bf8fSGeorge Liu static int dirWatchDesc = -1; 90cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 914642bf8fSGeorge Liu static int fileWatchDesc = -1; 924642bf8fSGeorge Liu 934642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 944642bf8fSGeorge Liu using EventLogObjectsType = 954642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 964642bf8fSGeorge Liu std::vector<std::string>>; 974642bf8fSGeorge Liu 98fffb8c1fSEd Tanous namespace registries 994642bf8fSGeorge Liu { 1007f4eb588SAppaRao Puli static const Message* 1017f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 10226702d01SEd Tanous const std::span<const MessageEntry>& registry) 1037f4eb588SAppaRao Puli { 10426702d01SEd Tanous std::span<const MessageEntry>::iterator messageIt = 10526702d01SEd Tanous std::find_if(registry.begin(), registry.end(), 1067f4eb588SAppaRao Puli [&messageKey](const MessageEntry& messageEntry) { 10755f79e6fSEd Tanous return messageKey == messageEntry.first; 1087f4eb588SAppaRao Puli }); 10926702d01SEd Tanous if (messageIt != registry.end()) 1107f4eb588SAppaRao Puli { 1117f4eb588SAppaRao Puli return &messageIt->second; 1127f4eb588SAppaRao Puli } 1137f4eb588SAppaRao Puli 1147f4eb588SAppaRao Puli return nullptr; 1157f4eb588SAppaRao Puli } 1167f4eb588SAppaRao Puli 117*26ccae32SEd Tanous static const Message* formatMessage(std::string_view messageID) 1187f4eb588SAppaRao Puli { 1197f4eb588SAppaRao Puli // Redfish MessageIds are in the form 1207f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1217f4eb588SAppaRao Puli // the right Message 1227f4eb588SAppaRao Puli std::vector<std::string> fields; 1237f4eb588SAppaRao Puli fields.reserve(4); 12450ebd4afSEd Tanous 12550ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 1267f4eb588SAppaRao Puli if (fields.size() != 4) 1277f4eb588SAppaRao Puli { 1287f4eb588SAppaRao Puli return nullptr; 1297f4eb588SAppaRao Puli } 13002cad96eSEd Tanous const std::string& registryName = fields[0]; 13102cad96eSEd Tanous const std::string& messageKey = fields[3]; 1327f4eb588SAppaRao Puli 1337f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 134b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1357f4eb588SAppaRao Puli } 136fffb8c1fSEd Tanous } // namespace registries 1377f4eb588SAppaRao Puli 1387f4eb588SAppaRao Puli namespace event_log 1397f4eb588SAppaRao Puli { 1402558979cSP Dheeraj Srujan Kumar inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 1417f4eb588SAppaRao Puli { 1427f4eb588SAppaRao Puli static time_t prevTs = 0; 1437f4eb588SAppaRao Puli static int index = 0; 1447f4eb588SAppaRao Puli 1457f4eb588SAppaRao Puli // Get the entry timestamp 1467f4eb588SAppaRao Puli std::time_t curTs = 0; 1477f4eb588SAppaRao Puli std::tm timeStruct = {}; 1487f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1497f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1507f4eb588SAppaRao Puli { 1517f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1527f4eb588SAppaRao Puli if (curTs == -1) 1537f4eb588SAppaRao Puli { 1547f4eb588SAppaRao Puli return false; 1557f4eb588SAppaRao Puli } 1567f4eb588SAppaRao Puli } 1577f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1587f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1597f4eb588SAppaRao Puli 1607f4eb588SAppaRao Puli // Save the timestamp 1617f4eb588SAppaRao Puli prevTs = curTs; 1627f4eb588SAppaRao Puli 1637f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1647f4eb588SAppaRao Puli if (index > 0) 1657f4eb588SAppaRao Puli { 1667f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1677f4eb588SAppaRao Puli } 1687f4eb588SAppaRao Puli return true; 1697f4eb588SAppaRao Puli } 1707f4eb588SAppaRao Puli 17123a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 17223a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1735e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1747f4eb588SAppaRao Puli { 1757f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1767f4eb588SAppaRao Puli // First get the Timestamp 177f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1787f4eb588SAppaRao Puli if (space == std::string::npos) 1797f4eb588SAppaRao Puli { 1807f4eb588SAppaRao Puli return -EINVAL; 1817f4eb588SAppaRao Puli } 1827f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1837f4eb588SAppaRao Puli // Then get the log contents 184f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1857f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1867f4eb588SAppaRao Puli { 1877f4eb588SAppaRao Puli return -EINVAL; 1887f4eb588SAppaRao Puli } 1897f4eb588SAppaRao Puli std::string_view entry(logEntry); 1907f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1917f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1927f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 19350ebd4afSEd Tanous bmcweb::split(logEntryFields, entry, ','); 1947f4eb588SAppaRao Puli // We need at least a MessageId to be valid 19526f6976fSEd Tanous if (logEntryFields.empty()) 1967f4eb588SAppaRao Puli { 1977f4eb588SAppaRao Puli return -EINVAL; 1987f4eb588SAppaRao Puli } 1997f4eb588SAppaRao Puli messageID = logEntryFields[0]; 2007f4eb588SAppaRao Puli 2017f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 2027f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 2037f4eb588SAppaRao Puli { 20402cad96eSEd Tanous const std::string& messageArgsStart = logEntryFields[1]; 2057f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 2067f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 2077f4eb588SAppaRao Puli { 2085e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 2095e715de6SAppaRao Puli logEntryFields.end()); 2107f4eb588SAppaRao Puli } 2117f4eb588SAppaRao Puli } 2127f4eb588SAppaRao Puli 2137f4eb588SAppaRao Puli return 0; 2147f4eb588SAppaRao Puli } 2157f4eb588SAppaRao Puli 21623a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 2177f4eb588SAppaRao Puli std::string& registryName, 2187f4eb588SAppaRao Puli std::string& messageKey) 2197f4eb588SAppaRao Puli { 2207f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2217f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2227f4eb588SAppaRao Puli // the right Message 2237f4eb588SAppaRao Puli std::vector<std::string> fields; 2247f4eb588SAppaRao Puli fields.reserve(4); 22550ebd4afSEd Tanous bmcweb::split(fields, messageID, '.'); 2267f4eb588SAppaRao Puli if (fields.size() == 4) 2277f4eb588SAppaRao Puli { 2287f4eb588SAppaRao Puli registryName = fields[0]; 2297f4eb588SAppaRao Puli messageKey = fields[3]; 2307f4eb588SAppaRao Puli } 2317f4eb588SAppaRao Puli } 2327f4eb588SAppaRao Puli 23323a21a1cSEd Tanous inline int formatEventLogEntry(const std::string& logEntryID, 2347f4eb588SAppaRao Puli const std::string& messageID, 235c5ba4c27SEd Tanous const std::span<std::string_view> messageArgs, 23623a21a1cSEd Tanous std::string timestamp, 237b5a76932SEd Tanous const std::string& customText, 2387f4eb588SAppaRao Puli nlohmann::json& logEntryJson) 2397f4eb588SAppaRao Puli { 2407f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 241fffb8c1fSEd Tanous const registries::Message* message = registries::formatMessage(messageID); 2427f4eb588SAppaRao Puli 24380f595e7SEd Tanous if (message == nullptr) 2447f4eb588SAppaRao Puli { 24580f595e7SEd Tanous return -1; 2467f4eb588SAppaRao Puli } 2477f4eb588SAppaRao Puli 24880f595e7SEd Tanous std::string msg = 24980f595e7SEd Tanous redfish::registries::fillMessageArgs(messageArgs, message->message); 25080f595e7SEd Tanous if (msg.empty()) 25180f595e7SEd Tanous { 25280f595e7SEd Tanous return -1; 25380f595e7SEd Tanous } 2547f4eb588SAppaRao Puli 2557f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2567f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2577f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 258f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 259b2f7609bSEd Tanous std::size_t plus = timestamp.find_first_of('+', dot); 2607f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2617f4eb588SAppaRao Puli { 2627f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2637f4eb588SAppaRao Puli } 2647f4eb588SAppaRao Puli 2657f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2661476687dSEd Tanous logEntryJson["EventId"] = logEntryID; 2671476687dSEd Tanous logEntryJson["EventType"] = "Event"; 26880f595e7SEd Tanous logEntryJson["Severity"] = message->messageSeverity; 2691476687dSEd Tanous logEntryJson["Message"] = std::move(msg); 2701476687dSEd Tanous logEntryJson["MessageId"] = messageID; 2711476687dSEd Tanous logEntryJson["MessageArgs"] = messageArgs; 2721476687dSEd Tanous logEntryJson["EventTimestamp"] = std::move(timestamp); 2731476687dSEd Tanous logEntryJson["Context"] = customText; 2747f4eb588SAppaRao Puli return 0; 2757f4eb588SAppaRao Puli } 2767f4eb588SAppaRao Puli 2777f4eb588SAppaRao Puli } // namespace event_log 2787f4eb588SAppaRao Puli #endif 2797f4eb588SAppaRao Puli 28023a21a1cSEd Tanous inline bool isFilterQuerySpecialChar(char c) 28107941a88SAyushi Smriti { 28207941a88SAyushi Smriti switch (c) 28307941a88SAyushi Smriti { 28407941a88SAyushi Smriti case '(': 28507941a88SAyushi Smriti case ')': 28607941a88SAyushi Smriti case '\'': 28707941a88SAyushi Smriti return true; 28807941a88SAyushi Smriti default: 28907941a88SAyushi Smriti return false; 29007941a88SAyushi Smriti } 29107941a88SAyushi Smriti } 29207941a88SAyushi Smriti 29323a21a1cSEd Tanous inline bool 29423a21a1cSEd Tanous readSSEQueryParams(std::string sseFilter, std::string& formatType, 29507941a88SAyushi Smriti std::vector<std::string>& messageIds, 29607941a88SAyushi Smriti std::vector<std::string>& registryPrefixes, 297144b6318SAppaRao Puli std::vector<std::string>& metricReportDefinitions) 29807941a88SAyushi Smriti { 29907941a88SAyushi Smriti sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(), 30007941a88SAyushi Smriti isFilterQuerySpecialChar), 30107941a88SAyushi Smriti sseFilter.end()); 30207941a88SAyushi Smriti 30307941a88SAyushi Smriti std::vector<std::string> result; 30450ebd4afSEd Tanous 30550ebd4afSEd Tanous // NOLINTNEXTLINE 30650ebd4afSEd Tanous bmcweb::split(result, sseFilter, ' '); 30707941a88SAyushi Smriti 30807941a88SAyushi Smriti BMCWEB_LOG_DEBUG << "No of tokens in SEE query: " << result.size(); 30907941a88SAyushi Smriti 31007941a88SAyushi Smriti constexpr uint8_t divisor = 4; 31107941a88SAyushi Smriti constexpr uint8_t minTokenSize = 3; 31207941a88SAyushi Smriti if (result.size() % divisor != minTokenSize) 31307941a88SAyushi Smriti { 31407941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid SSE filter specified."; 31507941a88SAyushi Smriti return false; 31607941a88SAyushi Smriti } 31707941a88SAyushi Smriti 31807941a88SAyushi Smriti for (std::size_t i = 0; i < result.size(); i += divisor) 31907941a88SAyushi Smriti { 32002cad96eSEd Tanous const std::string& key = result[i]; 32102cad96eSEd Tanous const std::string& op = result[i + 1]; 32202cad96eSEd Tanous const std::string& value = result[i + 2]; 32307941a88SAyushi Smriti 32407941a88SAyushi Smriti if ((i + minTokenSize) < result.size()) 32507941a88SAyushi Smriti { 32602cad96eSEd Tanous const std::string& separator = result[i + minTokenSize]; 32707941a88SAyushi Smriti // SSE supports only "or" and "and" in query params. 3284e0453b1SGunnar Mills if ((separator != "or") && (separator != "and")) 32907941a88SAyushi Smriti { 33007941a88SAyushi Smriti BMCWEB_LOG_ERROR 33107941a88SAyushi Smriti << "Invalid group operator in SSE query parameters"; 33207941a88SAyushi Smriti return false; 33307941a88SAyushi Smriti } 33407941a88SAyushi Smriti } 33507941a88SAyushi Smriti 33607941a88SAyushi Smriti // SSE supports only "eq" as per spec. 33707941a88SAyushi Smriti if (op != "eq") 33807941a88SAyushi Smriti { 33907941a88SAyushi Smriti BMCWEB_LOG_ERROR 34007941a88SAyushi Smriti << "Invalid assignment operator in SSE query parameters"; 34107941a88SAyushi Smriti return false; 34207941a88SAyushi Smriti } 34307941a88SAyushi Smriti 34407941a88SAyushi Smriti BMCWEB_LOG_DEBUG << key << " : " << value; 34507941a88SAyushi Smriti if (key == "EventFormatType") 34607941a88SAyushi Smriti { 34707941a88SAyushi Smriti formatType = value; 34807941a88SAyushi Smriti } 34907941a88SAyushi Smriti else if (key == "MessageId") 35007941a88SAyushi Smriti { 35107941a88SAyushi Smriti messageIds.push_back(value); 35207941a88SAyushi Smriti } 35307941a88SAyushi Smriti else if (key == "RegistryPrefix") 35407941a88SAyushi Smriti { 35507941a88SAyushi Smriti registryPrefixes.push_back(value); 35607941a88SAyushi Smriti } 35707941a88SAyushi Smriti else if (key == "MetricReportDefinition") 35807941a88SAyushi Smriti { 35907941a88SAyushi Smriti metricReportDefinitions.push_back(value); 36007941a88SAyushi Smriti } 36107941a88SAyushi Smriti else 36207941a88SAyushi Smriti { 36307941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid property(" << key 36407941a88SAyushi Smriti << ")in SSE filter query."; 36507941a88SAyushi Smriti return false; 36607941a88SAyushi Smriti } 36707941a88SAyushi Smriti } 36807941a88SAyushi Smriti return true; 36907941a88SAyushi Smriti } 37007941a88SAyushi Smriti 37128afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 372b52664e2SAppaRao Puli { 373b52664e2SAppaRao Puli public: 374b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 375b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 376b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 377b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 378b52664e2SAppaRao Puli 379eb1c47d3SEd Tanous Subscription(const std::string& inHost, uint16_t inPort, 380b52664e2SAppaRao Puli const std::string& inPath, const std::string& inUriProto) : 3810b4bdd93SAppaRao Puli eventSeqNum(1), 3820b4bdd93SAppaRao Puli host(inHost), port(inPort), path(inPath), uriProto(inUriProto) 383b52664e2SAppaRao Puli { 3847adb85acSSunitha Harish // Subscription constructor 385b52664e2SAppaRao Puli } 3864bbf237fSAppaRao Puli 3874e23a444SEd Tanous explicit Subscription( 3880d5f5cf4SEd Tanous const std::shared_ptr<boost::asio::ip::tcp::socket>& adaptor) : 389b31cef67SEd Tanous eventSeqNum(1), 390b31cef67SEd Tanous sseConn(std::make_shared<crow::ServerSentEvents>(adaptor)) 391b31cef67SEd Tanous {} 3924bbf237fSAppaRao Puli 3939f616dd1SEd Tanous ~Subscription() = default; 394b52664e2SAppaRao Puli 395f52c03c1SCarson Labrado bool sendEvent(std::string& msg) 396b52664e2SAppaRao Puli { 3976ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 3986ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 3996ba8c82eSsunharis_in .getEventServiceConfig(); 4006ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 4016ba8c82eSsunharis_in { 4026ba8c82eSsunharis_in return false; 4036ba8c82eSsunharis_in } 4046ba8c82eSsunharis_in 405e38778a5SAppaRao Puli bool useSSL = (uriProto == "https"); 406f52c03c1SCarson Labrado // A connection pool will be created if one does not already exist 407244256ccSCarson Labrado crow::HttpClient::getInstance().sendData( 408e38778a5SAppaRao Puli msg, id, host, port, path, useSSL, httpHeaders, 409244256ccSCarson Labrado boost::beast::http::verb::post, retryPolicyName); 4107adb85acSSunitha Harish eventSeqNum++; 4117adb85acSSunitha Harish 4124bbf237fSAppaRao Puli if (sseConn != nullptr) 4134bbf237fSAppaRao Puli { 4144bbf237fSAppaRao Puli sseConn->sendData(eventSeqNum, msg); 4154bbf237fSAppaRao Puli } 4166ba8c82eSsunharis_in return true; 4174bbf237fSAppaRao Puli } 4184bbf237fSAppaRao Puli 4196ba8c82eSsunharis_in bool sendTestEventLog() 4200b4bdd93SAppaRao Puli { 4210b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 4220b4bdd93SAppaRao Puli logEntryArray.push_back({}); 4230b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 4240b4bdd93SAppaRao Puli 425613dabeaSEd Tanous logEntryJson["EventId"] = "TestID"; 426613dabeaSEd Tanous logEntryJson["EventType"] = "Event"; 427613dabeaSEd Tanous logEntryJson["Severity"] = "OK"; 428613dabeaSEd Tanous logEntryJson["Message"] = "Generated test event"; 429613dabeaSEd Tanous logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog"; 430613dabeaSEd Tanous logEntryJson["MessageArgs"] = nlohmann::json::array(); 431613dabeaSEd Tanous logEntryJson["EventTimestamp"] = 432613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 433613dabeaSEd Tanous logEntryJson["Context"] = customText; 4340b4bdd93SAppaRao Puli 4351476687dSEd Tanous nlohmann::json msg; 4361476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 4371476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 4381476687dSEd Tanous msg["Name"] = "Event Log"; 4391476687dSEd Tanous msg["Events"] = logEntryArray; 4400b4bdd93SAppaRao Puli 441f52c03c1SCarson Labrado std::string strMsg = 442f52c03c1SCarson Labrado msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 443f52c03c1SCarson Labrado return this->sendEvent(strMsg); 4440b4bdd93SAppaRao Puli } 4450b4bdd93SAppaRao Puli 4467f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 4477f4eb588SAppaRao Puli void filterAndSendEventLogs( 4487f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4497f4eb588SAppaRao Puli { 4507f4eb588SAppaRao Puli nlohmann::json logEntryArray; 4517f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4527f4eb588SAppaRao Puli { 4537f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 4547f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 4557f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 4567f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 4577f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 4585e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 4597f4eb588SAppaRao Puli 4607f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 4617f4eb588SAppaRao Puli // send everything. 46226f6976fSEd Tanous if (!registryPrefixes.empty()) 4637f4eb588SAppaRao Puli { 4647f4eb588SAppaRao Puli auto obj = std::find(registryPrefixes.begin(), 4657f4eb588SAppaRao Puli registryPrefixes.end(), registryName); 4667f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 4677f4eb588SAppaRao Puli { 4687f4eb588SAppaRao Puli continue; 4697f4eb588SAppaRao Puli } 4707f4eb588SAppaRao Puli } 4717f4eb588SAppaRao Puli 4727f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 4737f4eb588SAppaRao Puli // send everything. 47426f6976fSEd Tanous if (!registryMsgIds.empty()) 4757f4eb588SAppaRao Puli { 4767f4eb588SAppaRao Puli auto obj = std::find(registryMsgIds.begin(), 4777f4eb588SAppaRao Puli registryMsgIds.end(), messageKey); 4787f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 4797f4eb588SAppaRao Puli { 4807f4eb588SAppaRao Puli continue; 4817f4eb588SAppaRao Puli } 4827f4eb588SAppaRao Puli } 4837f4eb588SAppaRao Puli 484c5ba4c27SEd Tanous std::vector<std::string_view> messageArgsView(messageArgs.begin(), 485c5ba4c27SEd Tanous messageArgs.end()); 486c5ba4c27SEd Tanous 4877f4eb588SAppaRao Puli logEntryArray.push_back({}); 4887f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 489c5ba4c27SEd Tanous if (event_log::formatEventLogEntry(idStr, messageID, 490c5ba4c27SEd Tanous messageArgsView, timestamp, 491c5ba4c27SEd Tanous customText, bmcLogEntry) != 0) 4927f4eb588SAppaRao Puli { 4937f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 4947f4eb588SAppaRao Puli continue; 4957f4eb588SAppaRao Puli } 4967f4eb588SAppaRao Puli } 4977f4eb588SAppaRao Puli 49826f6976fSEd Tanous if (logEntryArray.empty()) 4997f4eb588SAppaRao Puli { 5007f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 5017f4eb588SAppaRao Puli return; 5027f4eb588SAppaRao Puli } 5037f4eb588SAppaRao Puli 5041476687dSEd Tanous nlohmann::json msg; 5051476687dSEd Tanous msg["@odata.type"] = "#Event.v1_4_0.Event"; 5061476687dSEd Tanous msg["Id"] = std::to_string(eventSeqNum); 5071476687dSEd Tanous msg["Name"] = "Event Log"; 5081476687dSEd Tanous msg["Events"] = logEntryArray; 5097f4eb588SAppaRao Puli 510f52c03c1SCarson Labrado std::string strMsg = 511f52c03c1SCarson Labrado msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 512f52c03c1SCarson Labrado this->sendEvent(strMsg); 5137f4eb588SAppaRao Puli } 5147f4eb588SAppaRao Puli #endif 5157f4eb588SAppaRao Puli 516248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 5171e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 518156d6b00SAppaRao Puli { 519456cd875SSzymon Dompke boost::urls::url mrdUri = 520456cd875SSzymon Dompke crow::utility::urlFromPieces("redfish", "v1", "TelemetryService", 521456cd875SSzymon Dompke "MetricReportDefinitions", reportId); 522156d6b00SAppaRao Puli 523156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 52426f6976fSEd Tanous if (!metricReportDefinitions.empty()) 525156d6b00SAppaRao Puli { 526156d6b00SAppaRao Puli if (std::find(metricReportDefinitions.begin(), 527156d6b00SAppaRao Puli metricReportDefinitions.end(), 528079360aeSEd Tanous mrdUri.buffer()) == metricReportDefinitions.end()) 529156d6b00SAppaRao Puli { 530156d6b00SAppaRao Puli return; 531156d6b00SAppaRao Puli } 532156d6b00SAppaRao Puli } 533156d6b00SAppaRao Puli 534c0353249SWludzik, Jozef nlohmann::json msg; 535248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 536156d6b00SAppaRao Puli { 537c0353249SWludzik, Jozef BMCWEB_LOG_ERROR << "Failed to fill the MetricReport for DBus " 538c0353249SWludzik, Jozef "Report with id " 539248d0230SEd Tanous << reportId; 540c0353249SWludzik, Jozef return; 541156d6b00SAppaRao Puli } 542156d6b00SAppaRao Puli 54322daffd7SAppaRao Puli // Context is set by user during Event subscription and it must be 54422daffd7SAppaRao Puli // set for MetricReport response. 54522daffd7SAppaRao Puli if (!customText.empty()) 54622daffd7SAppaRao Puli { 54722daffd7SAppaRao Puli msg["Context"] = customText; 54822daffd7SAppaRao Puli } 54922daffd7SAppaRao Puli 550f52c03c1SCarson Labrado std::string strMsg = 551f52c03c1SCarson Labrado msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 552f52c03c1SCarson Labrado this->sendEvent(strMsg); 553156d6b00SAppaRao Puli } 554156d6b00SAppaRao Puli 555fe44eb0bSAyushi Smriti void updateRetryConfig(const uint32_t retryAttempts, 556fe44eb0bSAyushi Smriti const uint32_t retryTimeoutInterval) 557fe44eb0bSAyushi Smriti { 558f52c03c1SCarson Labrado crow::HttpClient::getInstance().setRetryConfig( 559a7a80296SCarson Labrado retryAttempts, retryTimeoutInterval, retryRespHandler, 560a7a80296SCarson Labrado retryPolicyName); 56162de0c68SAppaRao Puli } 562fe44eb0bSAyushi Smriti 563fe44eb0bSAyushi Smriti void updateRetryPolicy() 564fe44eb0bSAyushi Smriti { 565f52c03c1SCarson Labrado crow::HttpClient::getInstance().setRetryPolicy(retryPolicy, 566f52c03c1SCarson Labrado retryPolicyName); 56762de0c68SAppaRao Puli } 568fe44eb0bSAyushi Smriti 5699eb808c1SEd Tanous uint64_t getEventSeqNum() const 57096330b99SSunitha Harish { 57196330b99SSunitha Harish return eventSeqNum; 57296330b99SSunitha Harish } 57396330b99SSunitha Harish 574b52664e2SAppaRao Puli private: 5750b4bdd93SAppaRao Puli uint64_t eventSeqNum; 576b52664e2SAppaRao Puli std::string host; 577eb1c47d3SEd Tanous uint16_t port = 0; 578b52664e2SAppaRao Puli std::string path; 579b52664e2SAppaRao Puli std::string uriProto; 5804bbf237fSAppaRao Puli std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr; 581f52c03c1SCarson Labrado std::string retryPolicyName = "SubscriptionEvent"; 582a7a80296SCarson Labrado 583a7a80296SCarson Labrado // Check used to indicate what response codes are valid as part of our retry 584a7a80296SCarson Labrado // policy. 2XX is considered acceptable 585a7a80296SCarson Labrado static boost::system::error_code retryRespHandler(unsigned int respCode) 586a7a80296SCarson Labrado { 587a7a80296SCarson Labrado BMCWEB_LOG_DEBUG 588a7a80296SCarson Labrado << "Checking response code validity for SubscriptionEvent"; 589a7a80296SCarson Labrado if ((respCode < 200) || (respCode >= 300)) 590a7a80296SCarson Labrado { 591a7a80296SCarson Labrado return boost::system::errc::make_error_code( 592a7a80296SCarson Labrado boost::system::errc::result_out_of_range); 593a7a80296SCarson Labrado } 594a7a80296SCarson Labrado 595a7a80296SCarson Labrado // Return 0 if the response code is valid 596a7a80296SCarson Labrado return boost::system::errc::make_error_code( 597a7a80296SCarson Labrado boost::system::errc::success); 5989fa6d147SNan Zhou } 599b52664e2SAppaRao Puli }; 600b52664e2SAppaRao Puli 601b52664e2SAppaRao Puli class EventServiceManager 602b52664e2SAppaRao Puli { 603b52664e2SAppaRao Puli private: 604d3a9e084SEd Tanous bool serviceEnabled = false; 605d3a9e084SEd Tanous uint32_t retryAttempts = 0; 606d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 6077d1cc387SAppaRao Puli 6089f616dd1SEd Tanous EventServiceManager() 609b52664e2SAppaRao Puli { 6101bf712bcSAyushi Smriti // Load config from persist store. 6111bf712bcSAyushi Smriti initConfig(); 612b52664e2SAppaRao Puli } 613b52664e2SAppaRao Puli 6142558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 6159f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 6169f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 61759d494eeSPatrick Williams std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor; 618b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 619b52664e2SAppaRao Puli subscriptionsMap; 620b52664e2SAppaRao Puli 6219f616dd1SEd Tanous uint64_t eventId{1}; 62296330b99SSunitha Harish 623b52664e2SAppaRao Puli public: 6249f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 6259f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 6269f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 6279f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 628ecd6a3a2SEd Tanous ~EventServiceManager() = default; 6299f616dd1SEd Tanous 630b52664e2SAppaRao Puli static EventServiceManager& getInstance() 631b52664e2SAppaRao Puli { 632b52664e2SAppaRao Puli static EventServiceManager handler; 633b52664e2SAppaRao Puli return handler; 634b52664e2SAppaRao Puli } 635b52664e2SAppaRao Puli 6361bf712bcSAyushi Smriti void initConfig() 6371bf712bcSAyushi Smriti { 63828afb49cSJunLin Chen loadOldBehavior(); 6391bf712bcSAyushi Smriti 64028afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 64128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 64228afb49cSJunLin Chen .getEventServiceConfig(); 6431bf712bcSAyushi Smriti 64428afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 64528afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 64628afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6471bf712bcSAyushi Smriti 64828afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 64928afb49cSJunLin Chen .subscriptionsConfigMap) 6501bf712bcSAyushi Smriti { 65128afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 65228afb49cSJunLin Chen it.second; 6534bbf237fSAppaRao Puli 6541bf712bcSAyushi Smriti std::string host; 6551bf712bcSAyushi Smriti std::string urlProto; 656eb1c47d3SEd Tanous uint16_t port = 0; 6571bf712bcSAyushi Smriti std::string path; 65811baefe4SEd Tanous bool status = crow::utility::validateAndSplitUrl( 65911baefe4SEd Tanous newSub->destinationUrl, urlProto, host, port, path); 6601bf712bcSAyushi Smriti 6611bf712bcSAyushi Smriti if (!status) 6621bf712bcSAyushi Smriti { 6631bf712bcSAyushi Smriti BMCWEB_LOG_ERROR 6641bf712bcSAyushi Smriti << "Failed to validate and split destination url"; 6651bf712bcSAyushi Smriti continue; 6661bf712bcSAyushi Smriti } 6671bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 6681bf712bcSAyushi Smriti std::make_shared<Subscription>(host, port, path, urlProto); 6691bf712bcSAyushi Smriti 67028afb49cSJunLin Chen subValue->id = newSub->id; 67128afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 67228afb49cSJunLin Chen subValue->protocol = newSub->protocol; 67328afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 67428afb49cSJunLin Chen subValue->customText = newSub->customText; 67528afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 67628afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 67728afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 67828afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 67928afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 68028afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 68128afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 6821bf712bcSAyushi Smriti 68328afb49cSJunLin Chen if (subValue->id.empty()) 6841bf712bcSAyushi Smriti { 6851bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Failed to add subscription"; 6861bf712bcSAyushi Smriti } 68728afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 68828afb49cSJunLin Chen 68928afb49cSJunLin Chen updateNoOfSubscribersCount(); 69028afb49cSJunLin Chen 69128afb49cSJunLin Chen #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 6922558979cSP Dheeraj Srujan Kumar 6932558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 6942558979cSP Dheeraj Srujan Kumar 69528afb49cSJunLin Chen #endif 69628afb49cSJunLin Chen // Update retry configuration. 69728afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 69828afb49cSJunLin Chen subValue->updateRetryPolicy(); 6991bf712bcSAyushi Smriti } 7001bf712bcSAyushi Smriti } 7011bf712bcSAyushi Smriti 70256d2396dSEd Tanous static void loadOldBehavior() 703b52664e2SAppaRao Puli { 70428afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 70528afb49cSJunLin Chen if (!eventConfigFile.good()) 7061bf712bcSAyushi Smriti { 70728afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Old eventService config not exist"; 70828afb49cSJunLin Chen return; 70928afb49cSJunLin Chen } 71028afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 71128afb49cSJunLin Chen if (jsonData.is_discarded()) 7124bbf237fSAppaRao Puli { 71328afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Old eventService config parse error."; 71428afb49cSJunLin Chen return; 71528afb49cSJunLin Chen } 71628afb49cSJunLin Chen 71728afb49cSJunLin Chen for (const auto& item : jsonData.items()) 71828afb49cSJunLin Chen { 71928afb49cSJunLin Chen if (item.key() == "Configuration") 72028afb49cSJunLin Chen { 72128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 72228afb49cSJunLin Chen .getEventServiceConfig() 72328afb49cSJunLin Chen .fromJson(item.value()); 72428afb49cSJunLin Chen } 72528afb49cSJunLin Chen else if (item.key() == "Subscriptions") 72628afb49cSJunLin Chen { 72728afb49cSJunLin Chen for (const auto& elem : item.value()) 72828afb49cSJunLin Chen { 72928afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 73028afb49cSJunLin Chen newSubscription = 73128afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 73228afb49cSJunLin Chen true); 73328afb49cSJunLin Chen if (newSubscription == nullptr) 73428afb49cSJunLin Chen { 73528afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Problem reading subscription " 73628afb49cSJunLin Chen "from old persistent store"; 7374bbf237fSAppaRao Puli continue; 7384bbf237fSAppaRao Puli } 7391bf712bcSAyushi Smriti 74028afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 74128afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 7421bf712bcSAyushi Smriti 74328afb49cSJunLin Chen std::string id; 7441bf712bcSAyushi Smriti 74528afb49cSJunLin Chen int retry = 3; 746e662eae8SEd Tanous while (retry != 0) 7471bf712bcSAyushi Smriti { 74828afb49cSJunLin Chen id = std::to_string(dist(gen)); 74928afb49cSJunLin Chen if (gen.error()) 7507d1cc387SAppaRao Puli { 75128afb49cSJunLin Chen retry = 0; 75228afb49cSJunLin Chen break; 75328afb49cSJunLin Chen } 75428afb49cSJunLin Chen newSubscription->id = id; 75528afb49cSJunLin Chen auto inserted = 75628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 75728afb49cSJunLin Chen .subscriptionsConfigMap.insert( 75828afb49cSJunLin Chen std::pair(id, newSubscription)); 75928afb49cSJunLin Chen if (inserted.second) 76028afb49cSJunLin Chen { 76128afb49cSJunLin Chen break; 76228afb49cSJunLin Chen } 76328afb49cSJunLin Chen --retry; 7647d1cc387SAppaRao Puli } 7657d1cc387SAppaRao Puli 76628afb49cSJunLin Chen if (retry <= 0) 76728afb49cSJunLin Chen { 76828afb49cSJunLin Chen BMCWEB_LOG_ERROR 76928afb49cSJunLin Chen << "Failed to generate random number from old " 77028afb49cSJunLin Chen "persistent store"; 77128afb49cSJunLin Chen continue; 77228afb49cSJunLin Chen } 77328afb49cSJunLin Chen } 77428afb49cSJunLin Chen } 77528afb49cSJunLin Chen 77628afb49cSJunLin Chen persistent_data::getConfig().writeData(); 77728afb49cSJunLin Chen std::remove(eventServiceFile); 77828afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Remove old eventservice config"; 77928afb49cSJunLin Chen } 78028afb49cSJunLin Chen } 78128afb49cSJunLin Chen 7829eb808c1SEd Tanous void updateSubscriptionData() const 78328afb49cSJunLin Chen { 78428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 78528afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 78628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 78728afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 78828afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 78928afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 79028afb49cSJunLin Chen 79128afb49cSJunLin Chen persistent_data::getConfig().writeData(); 79228afb49cSJunLin Chen } 79328afb49cSJunLin Chen 79428afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7957d1cc387SAppaRao Puli { 7967d1cc387SAppaRao Puli bool updateConfig = false; 797fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 7987d1cc387SAppaRao Puli 79928afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 8007d1cc387SAppaRao Puli { 80128afb49cSJunLin Chen serviceEnabled = cfg.enabled; 802e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 8037d1cc387SAppaRao Puli { 8047d1cc387SAppaRao Puli registerMetricReportSignal(); 8057d1cc387SAppaRao Puli } 8067d1cc387SAppaRao Puli else 8077d1cc387SAppaRao Puli { 8087d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8097d1cc387SAppaRao Puli } 8107d1cc387SAppaRao Puli updateConfig = true; 8117d1cc387SAppaRao Puli } 8127d1cc387SAppaRao Puli 81328afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 8147d1cc387SAppaRao Puli { 81528afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 8167d1cc387SAppaRao Puli updateConfig = true; 817fe44eb0bSAyushi Smriti updateRetryCfg = true; 8187d1cc387SAppaRao Puli } 8197d1cc387SAppaRao Puli 82028afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 8217d1cc387SAppaRao Puli { 82228afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 8237d1cc387SAppaRao Puli updateConfig = true; 824fe44eb0bSAyushi Smriti updateRetryCfg = true; 8257d1cc387SAppaRao Puli } 8267d1cc387SAppaRao Puli 8277d1cc387SAppaRao Puli if (updateConfig) 8287d1cc387SAppaRao Puli { 8297d1cc387SAppaRao Puli updateSubscriptionData(); 8307d1cc387SAppaRao Puli } 831fe44eb0bSAyushi Smriti 832fe44eb0bSAyushi Smriti if (updateRetryCfg) 833fe44eb0bSAyushi Smriti { 834fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 835fe44eb0bSAyushi Smriti for (const auto& it : 836fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 837fe44eb0bSAyushi Smriti { 838fe44eb0bSAyushi Smriti std::shared_ptr<Subscription> entry = it.second; 839fe44eb0bSAyushi Smriti entry->updateRetryConfig(retryAttempts, retryTimeoutInterval); 840fe44eb0bSAyushi Smriti } 841fe44eb0bSAyushi Smriti } 8427d1cc387SAppaRao Puli } 8437d1cc387SAppaRao Puli 8447d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8457d1cc387SAppaRao Puli { 8467d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8477d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8487d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8497d1cc387SAppaRao Puli { 8507d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8517d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8527d1cc387SAppaRao Puli { 8537d1cc387SAppaRao Puli eventLogSubCount++; 8547d1cc387SAppaRao Puli } 8557d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8567d1cc387SAppaRao Puli { 8577d1cc387SAppaRao Puli metricReportSubCount++; 8587d1cc387SAppaRao Puli } 8597d1cc387SAppaRao Puli } 8607d1cc387SAppaRao Puli 8617d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8627d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8637d1cc387SAppaRao Puli { 8647d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 865e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 8667d1cc387SAppaRao Puli { 8677d1cc387SAppaRao Puli registerMetricReportSignal(); 8687d1cc387SAppaRao Puli } 8697d1cc387SAppaRao Puli else 8707d1cc387SAppaRao Puli { 8717d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8727d1cc387SAppaRao Puli } 8737d1cc387SAppaRao Puli } 8747d1cc387SAppaRao Puli } 8757d1cc387SAppaRao Puli 876b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 877b52664e2SAppaRao Puli { 878b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 879b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 880b52664e2SAppaRao Puli { 881b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 882b52664e2SAppaRao Puli return nullptr; 883b52664e2SAppaRao Puli } 884b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 885b52664e2SAppaRao Puli return subValue; 886b52664e2SAppaRao Puli } 887b52664e2SAppaRao Puli 888b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 8891bf712bcSAyushi Smriti const bool updateFile = true) 890b52664e2SAppaRao Puli { 891fc76b8acSEd Tanous 892fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 893fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 894fc76b8acSEd Tanous 895b52664e2SAppaRao Puli std::string id; 896b52664e2SAppaRao Puli 897b52664e2SAppaRao Puli int retry = 3; 898e662eae8SEd Tanous while (retry != 0) 899b52664e2SAppaRao Puli { 900fc76b8acSEd Tanous id = std::to_string(dist(gen)); 901fc76b8acSEd Tanous if (gen.error()) 902fc76b8acSEd Tanous { 903fc76b8acSEd Tanous retry = 0; 904fc76b8acSEd Tanous break; 905fc76b8acSEd Tanous } 906b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 907b52664e2SAppaRao Puli if (inserted.second) 908b52664e2SAppaRao Puli { 909b52664e2SAppaRao Puli break; 910b52664e2SAppaRao Puli } 911b52664e2SAppaRao Puli --retry; 91223a21a1cSEd Tanous } 913b52664e2SAppaRao Puli 914b52664e2SAppaRao Puli if (retry <= 0) 915b52664e2SAppaRao Puli { 916b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Failed to generate random number"; 917abb93cddSEd Tanous return ""; 918b52664e2SAppaRao Puli } 919b52664e2SAppaRao Puli 92028afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 92128afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 92228afb49cSJunLin Chen newSub->id = id; 92328afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 92428afb49cSJunLin Chen newSub->protocol = subValue->protocol; 92528afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 92628afb49cSJunLin Chen newSub->customText = subValue->customText; 92728afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 92828afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 92928afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 93028afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 93128afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 93228afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 93328afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 93428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 93528afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 93628afb49cSJunLin Chen 9377d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 9381bf712bcSAyushi Smriti 9391bf712bcSAyushi Smriti if (updateFile) 9401bf712bcSAyushi Smriti { 941b52664e2SAppaRao Puli updateSubscriptionData(); 9421bf712bcSAyushi Smriti } 9437f4eb588SAppaRao Puli 9447f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 9452558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 9467f4eb588SAppaRao Puli { 9472558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 9487f4eb588SAppaRao Puli } 9497f4eb588SAppaRao Puli #endif 950fe44eb0bSAyushi Smriti // Update retry configuration. 951fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 952fe44eb0bSAyushi Smriti subValue->updateRetryPolicy(); 953fe44eb0bSAyushi Smriti 954b52664e2SAppaRao Puli return id; 955b52664e2SAppaRao Puli } 956b52664e2SAppaRao Puli 957b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 958b52664e2SAppaRao Puli { 959b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 96055f79e6fSEd Tanous return obj != subscriptionsMap.end(); 961b52664e2SAppaRao Puli } 962b52664e2SAppaRao Puli 963b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 964b52664e2SAppaRao Puli { 965b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 966b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 967b52664e2SAppaRao Puli { 968b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 96928afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 97028afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 97128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 97228afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9737d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 974b52664e2SAppaRao Puli updateSubscriptionData(); 975b52664e2SAppaRao Puli } 976b52664e2SAppaRao Puli } 977b52664e2SAppaRao Puli 978b52664e2SAppaRao Puli size_t getNumberOfSubscriptions() 979b52664e2SAppaRao Puli { 980b52664e2SAppaRao Puli return subscriptionsMap.size(); 981b52664e2SAppaRao Puli } 982b52664e2SAppaRao Puli 983b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 984b52664e2SAppaRao Puli { 985b52664e2SAppaRao Puli std::vector<std::string> idList; 986b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 987b52664e2SAppaRao Puli { 988b52664e2SAppaRao Puli idList.emplace_back(it.first); 989b52664e2SAppaRao Puli } 990b52664e2SAppaRao Puli return idList; 991b52664e2SAppaRao Puli } 992b52664e2SAppaRao Puli 993b52664e2SAppaRao Puli bool isDestinationExist(const std::string& destUrl) 994b52664e2SAppaRao Puli { 995b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 996b52664e2SAppaRao Puli { 997b52664e2SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 998b52664e2SAppaRao Puli if (entry->destinationUrl == destUrl) 999b52664e2SAppaRao Puli { 1000b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 1001b52664e2SAppaRao Puli return true; 1002b52664e2SAppaRao Puli } 1003b52664e2SAppaRao Puli } 1004b52664e2SAppaRao Puli return false; 1005b52664e2SAppaRao Puli } 10060b4bdd93SAppaRao Puli 10076ba8c82eSsunharis_in bool sendTestEventLog() 10080b4bdd93SAppaRao Puli { 10090b4bdd93SAppaRao Puli for (const auto& it : this->subscriptionsMap) 10100b4bdd93SAppaRao Puli { 10110b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 10126ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 10136ba8c82eSsunharis_in { 10146ba8c82eSsunharis_in return false; 10150b4bdd93SAppaRao Puli } 10160b4bdd93SAppaRao Puli } 10176ba8c82eSsunharis_in return true; 10186ba8c82eSsunharis_in } 1019e9a14131SAppaRao Puli 1020613dabeaSEd Tanous void sendEvent(nlohmann::json eventMessage, const std::string& origin, 1021613dabeaSEd Tanous const std::string& resType) 102296330b99SSunitha Harish { 1023dbb59d4dSEd Tanous if (!serviceEnabled || (noOfEventLogSubscribers == 0U)) 10246ba8c82eSsunharis_in { 10256ba8c82eSsunharis_in BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 10266ba8c82eSsunharis_in return; 10276ba8c82eSsunharis_in } 102896330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 102996330b99SSunitha Harish 1030613dabeaSEd Tanous eventMessage["EventId"] = eventId; 1031613dabeaSEd Tanous // MemberId is 0 : since we are sending one event record. 1032613dabeaSEd Tanous eventMessage["MemberId"] = 0; 1033613dabeaSEd Tanous eventMessage["EventTimestamp"] = 1034613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1035613dabeaSEd Tanous eventMessage["OriginOfCondition"] = origin; 1036613dabeaSEd Tanous 1037613dabeaSEd Tanous eventRecord.emplace_back(std::move(eventMessage)); 103896330b99SSunitha Harish 103996330b99SSunitha Harish for (const auto& it : this->subscriptionsMap) 104096330b99SSunitha Harish { 104196330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 104296330b99SSunitha Harish bool isSubscribed = false; 104396330b99SSunitha Harish // Search the resourceTypes list for the subscription. 104496330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 104596330b99SSunitha Harish // send everything. 104626f6976fSEd Tanous if (!entry->resourceTypes.empty()) 104796330b99SSunitha Harish { 10483174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 104996330b99SSunitha Harish { 105096330b99SSunitha Harish if (resType == resource) 105196330b99SSunitha Harish { 105296330b99SSunitha Harish BMCWEB_LOG_INFO << "ResourceType " << resource 105396330b99SSunitha Harish << " found in the subscribed list"; 105496330b99SSunitha Harish isSubscribed = true; 105596330b99SSunitha Harish break; 105696330b99SSunitha Harish } 105796330b99SSunitha Harish } 105896330b99SSunitha Harish } 105996330b99SSunitha Harish else // resourceTypes list is empty. 106096330b99SSunitha Harish { 106196330b99SSunitha Harish isSubscribed = true; 106296330b99SSunitha Harish } 106396330b99SSunitha Harish if (isSubscribed) 106496330b99SSunitha Harish { 1065613dabeaSEd Tanous nlohmann::json msgJson; 1066613dabeaSEd Tanous 1067613dabeaSEd Tanous msgJson["@odata.type"] = "#Event.v1_4_0.Event"; 1068613dabeaSEd Tanous msgJson["Name"] = "Event Log"; 1069613dabeaSEd Tanous msgJson["Id"] = eventId; 1070613dabeaSEd Tanous msgJson["Events"] = eventRecord; 1071f52c03c1SCarson Labrado 1072f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1073f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 1074f52c03c1SCarson Labrado entry->sendEvent(strMsg); 107596330b99SSunitha Harish eventId++; // increament the eventId 107696330b99SSunitha Harish } 107796330b99SSunitha Harish else 107896330b99SSunitha Harish { 107996330b99SSunitha Harish BMCWEB_LOG_INFO << "Not subscribed to this resource"; 108096330b99SSunitha Harish } 108196330b99SSunitha Harish } 108296330b99SSunitha Harish } 10835738de59SAsmitha Karunanithi void sendBroadcastMsg(const std::string& broadcastMsg) 10845738de59SAsmitha Karunanithi { 10855738de59SAsmitha Karunanithi for (const auto& it : this->subscriptionsMap) 10865738de59SAsmitha Karunanithi { 10875738de59SAsmitha Karunanithi std::shared_ptr<Subscription> entry = it.second; 1088613dabeaSEd Tanous nlohmann::json msgJson; 1089613dabeaSEd Tanous msgJson["Timestamp"] = 1090613dabeaSEd Tanous redfish::time_utils::getDateTimeOffsetNow().first; 1091613dabeaSEd Tanous msgJson["OriginOfCondition"] = "/ibm/v1/HMC/BroadcastService"; 1092613dabeaSEd Tanous msgJson["Name"] = "Broadcast Message"; 1093613dabeaSEd Tanous msgJson["Message"] = broadcastMsg; 1094f52c03c1SCarson Labrado 1095f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1096f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 1097f52c03c1SCarson Labrado entry->sendEvent(strMsg); 10985738de59SAsmitha Karunanithi } 10995738de59SAsmitha Karunanithi } 110096330b99SSunitha Harish 11017f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 11022558979cSP Dheeraj Srujan Kumar 11032558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 11047f4eb588SAppaRao Puli { 11052558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 11062558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 11072558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 11082558979cSP Dheeraj Srujan Kumar } 11092558979cSP Dheeraj Srujan Kumar 11102558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 11112558979cSP Dheeraj Srujan Kumar { 11122558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 11132558979cSP Dheeraj Srujan Kumar 11147f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11157f4eb588SAppaRao Puli if (!logStream.good()) 11167f4eb588SAppaRao Puli { 11177f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 11187f4eb588SAppaRao Puli return; 11197f4eb588SAppaRao Puli } 11207f4eb588SAppaRao Puli std::string logEntry; 11217f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11227f4eb588SAppaRao Puli { 11232558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11247f4eb588SAppaRao Puli } 11252558979cSP Dheeraj Srujan Kumar 11262558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "Next Log Position : " << redfishLogFilePosition; 11277f4eb588SAppaRao Puli } 11287f4eb588SAppaRao Puli 11297f4eb588SAppaRao Puli void readEventLogsFromFile() 11307f4eb588SAppaRao Puli { 11317f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11327f4eb588SAppaRao Puli if (!logStream.good()) 11337f4eb588SAppaRao Puli { 11347f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed"; 11357f4eb588SAppaRao Puli return; 11367f4eb588SAppaRao Puli } 11377f4eb588SAppaRao Puli 11387f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11397f4eb588SAppaRao Puli 11407f4eb588SAppaRao Puli std::string logEntry; 11412558979cSP Dheeraj Srujan Kumar 11422558979cSP Dheeraj Srujan Kumar // Get the read pointer to the next log to be read. 11432558979cSP Dheeraj Srujan Kumar logStream.seekg(redfishLogFilePosition); 11442558979cSP Dheeraj Srujan Kumar 11457f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11467f4eb588SAppaRao Puli { 11472558979cSP Dheeraj Srujan Kumar // Update Pointer position 11482558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11492558979cSP Dheeraj Srujan Kumar 11502558979cSP Dheeraj Srujan Kumar std::string idStr; 11512558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 11527f4eb588SAppaRao Puli { 11537f4eb588SAppaRao Puli continue; 11547f4eb588SAppaRao Puli } 11557f4eb588SAppaRao Puli 1156e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11577f4eb588SAppaRao Puli { 11582558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 11592558979cSP Dheeraj Srujan Kumar // the remaining items below. 11602558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 11617f4eb588SAppaRao Puli continue; 11627f4eb588SAppaRao Puli } 11637f4eb588SAppaRao Puli 11647f4eb588SAppaRao Puli std::string timestamp; 11657f4eb588SAppaRao Puli std::string messageID; 11665e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11677f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11687f4eb588SAppaRao Puli messageArgs) != 0) 11697f4eb588SAppaRao Puli { 11707f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 11717f4eb588SAppaRao Puli continue; 11727f4eb588SAppaRao Puli } 11737f4eb588SAppaRao Puli 11747f4eb588SAppaRao Puli std::string registryName; 11757f4eb588SAppaRao Puli std::string messageKey; 11767f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 11777f4eb588SAppaRao Puli messageKey); 11787f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 11797f4eb588SAppaRao Puli { 11807f4eb588SAppaRao Puli continue; 11817f4eb588SAppaRao Puli } 11827f4eb588SAppaRao Puli 11837f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 11847f4eb588SAppaRao Puli messageKey, messageArgs); 11857f4eb588SAppaRao Puli } 11867f4eb588SAppaRao Puli 1187e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11882558979cSP Dheeraj Srujan Kumar { 11892558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 11902558979cSP Dheeraj Srujan Kumar return; 11912558979cSP Dheeraj Srujan Kumar } 11922558979cSP Dheeraj Srujan Kumar 11932558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 11942558979cSP Dheeraj Srujan Kumar { 11952558979cSP Dheeraj Srujan Kumar // No Records to send 11962558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 11972558979cSP Dheeraj Srujan Kumar return; 11982558979cSP Dheeraj Srujan Kumar } 11992558979cSP Dheeraj Srujan Kumar 12007f4eb588SAppaRao Puli for (const auto& it : this->subscriptionsMap) 12017f4eb588SAppaRao Puli { 12027f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 12037f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 12047f4eb588SAppaRao Puli { 12057f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 12067f4eb588SAppaRao Puli } 12077f4eb588SAppaRao Puli } 12087f4eb588SAppaRao Puli } 12097f4eb588SAppaRao Puli 12107f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 12117f4eb588SAppaRao Puli { 12126a9f85f9SAppaRao Puli if (!inotifyConn) 12137f4eb588SAppaRao Puli { 12147f4eb588SAppaRao Puli return; 12157f4eb588SAppaRao Puli } 12167f4eb588SAppaRao Puli 12177f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 12187f4eb588SAppaRao Puli 1219002d39b4SEd Tanous inotifyConn->async_read_some(boost::asio::buffer(readBuffer), 12207f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 12217f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 12227f4eb588SAppaRao Puli if (ec) 12237f4eb588SAppaRao Puli { 12247f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 12257f4eb588SAppaRao Puli return; 12267f4eb588SAppaRao Puli } 12277f4eb588SAppaRao Puli std::size_t index = 0; 1228b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 12297f4eb588SAppaRao Puli { 1230d3a9e084SEd Tanous struct inotify_event event 1231d3a9e084SEd Tanous {}; 1232b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1233b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1234b792cc56SAppaRao Puli { 1235b792cc56SAppaRao Puli if ((event.len == 0) || 1236b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1237b792cc56SAppaRao Puli { 1238b792cc56SAppaRao Puli index += (iEventSize + event.len); 1239b792cc56SAppaRao Puli continue; 1240b792cc56SAppaRao Puli } 1241b792cc56SAppaRao Puli 12424f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 12434f568f74SJiaqing Zhao if (fileName != "redfish") 1244b792cc56SAppaRao Puli { 1245b792cc56SAppaRao Puli index += (iEventSize + event.len); 1246b792cc56SAppaRao Puli continue; 1247b792cc56SAppaRao Puli } 1248b792cc56SAppaRao Puli 1249b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1250b792cc56SAppaRao Puli << "Redfish log file created/deleted. event.name: " 1251b792cc56SAppaRao Puli << fileName; 1252b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1253b792cc56SAppaRao Puli { 1254b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1255b792cc56SAppaRao Puli { 1256b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1257016761afSAppaRao Puli << "Remove and Add inotify watcher on " 1258016761afSAppaRao Puli "redfish event log file"; 1259016761afSAppaRao Puli // Remove existing inotify watcher and add 1260016761afSAppaRao Puli // with new redfish event log file. 1261016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1262016761afSAppaRao Puli fileWatchDesc = -1; 1263b792cc56SAppaRao Puli } 1264b792cc56SAppaRao Puli 1265b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1266b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1267b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1268b792cc56SAppaRao Puli { 1269002d39b4SEd Tanous BMCWEB_LOG_ERROR << "inotify_add_watch failed for " 1270b792cc56SAppaRao Puli "redfish log file."; 1271b792cc56SAppaRao Puli return; 1272b792cc56SAppaRao Puli } 1273b792cc56SAppaRao Puli 1274b792cc56SAppaRao Puli EventServiceManager::getInstance() 12752558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1276b792cc56SAppaRao Puli EventServiceManager::getInstance() 1277b792cc56SAppaRao Puli .readEventLogsFromFile(); 1278b792cc56SAppaRao Puli } 1279b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1280b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1281b792cc56SAppaRao Puli { 1282b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1283b792cc56SAppaRao Puli { 1284b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1285b792cc56SAppaRao Puli fileWatchDesc = -1; 1286b792cc56SAppaRao Puli } 1287b792cc56SAppaRao Puli } 1288b792cc56SAppaRao Puli } 1289b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1290b792cc56SAppaRao Puli { 1291b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12927f4eb588SAppaRao Puli { 12937f4eb588SAppaRao Puli EventServiceManager::getInstance() 12947f4eb588SAppaRao Puli .readEventLogsFromFile(); 12957f4eb588SAppaRao Puli } 1296b792cc56SAppaRao Puli } 1297b792cc56SAppaRao Puli index += (iEventSize + event.len); 12987f4eb588SAppaRao Puli } 12997f4eb588SAppaRao Puli 13007f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13017f4eb588SAppaRao Puli }); 13027f4eb588SAppaRao Puli } 13037f4eb588SAppaRao Puli 13047f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 13057f4eb588SAppaRao Puli { 130623a21a1cSEd Tanous inotifyConn.emplace(ioc); 1307b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1308b792cc56SAppaRao Puli if (inotifyFd == -1) 13097f4eb588SAppaRao Puli { 13107f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "inotify_init1 failed."; 13117f4eb588SAppaRao Puli return -1; 13127f4eb588SAppaRao Puli } 1313b792cc56SAppaRao Puli 1314b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1315b792cc56SAppaRao Puli // create/delete. 1316b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1317b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1318b792cc56SAppaRao Puli if (dirWatchDesc == -1) 13197f4eb588SAppaRao Puli { 13207f4eb588SAppaRao Puli BMCWEB_LOG_ERROR 1321b792cc56SAppaRao Puli << "inotify_add_watch failed for event log directory."; 13227f4eb588SAppaRao Puli return -1; 13237f4eb588SAppaRao Puli } 13247f4eb588SAppaRao Puli 1325b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 1326b792cc56SAppaRao Puli fileWatchDesc = 1327b792cc56SAppaRao Puli inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY); 1328b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1329b792cc56SAppaRao Puli { 1330b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1331b792cc56SAppaRao Puli << "inotify_add_watch failed for redfish log file."; 1332b792cc56SAppaRao Puli // Don't return error if file not exist. 1333b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1334b792cc56SAppaRao Puli } 1335b792cc56SAppaRao Puli 13367f4eb588SAppaRao Puli // monitor redfish event log file 1337b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13387f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13397f4eb588SAppaRao Puli 13407f4eb588SAppaRao Puli return 0; 13417f4eb588SAppaRao Puli } 13427f4eb588SAppaRao Puli 13437f4eb588SAppaRao Puli #endif 134459d494eeSPatrick Williams static void getReadingsForReport(sdbusplus::message_t& msg) 1345156d6b00SAppaRao Puli { 134656d2396dSEd Tanous if (msg.is_method_error()) 134756d2396dSEd Tanous { 134856d2396dSEd Tanous BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 134956d2396dSEd Tanous return; 135056d2396dSEd Tanous } 135156d2396dSEd Tanous 1352c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1353c0353249SWludzik, Jozef std::string id = path.filename(); 1354c0353249SWludzik, Jozef if (id.empty()) 1355156d6b00SAppaRao Puli { 1356c0353249SWludzik, Jozef BMCWEB_LOG_ERROR << "Failed to get Id from path"; 1357156d6b00SAppaRao Puli return; 1358156d6b00SAppaRao Puli } 1359156d6b00SAppaRao Puli 1360c0353249SWludzik, Jozef std::string interface; 1361b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1362c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1363c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1364c0353249SWludzik, Jozef 1365c0353249SWludzik, Jozef auto found = 1366c0353249SWludzik, Jozef std::find_if(props.begin(), props.end(), 1367c0353249SWludzik, Jozef [](const auto& x) { return x.first == "Readings"; }); 1368c0353249SWludzik, Jozef if (found == props.end()) 1369156d6b00SAppaRao Puli { 1370c0353249SWludzik, Jozef BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 1371156d6b00SAppaRao Puli return; 1372156d6b00SAppaRao Puli } 1373156d6b00SAppaRao Puli 13741e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 13751e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1376e662eae8SEd Tanous if (readings == nullptr) 13771e1e598dSJonathan Doman { 13781e1e598dSJonathan Doman BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 13791e1e598dSJonathan Doman return; 13801e1e598dSJonathan Doman } 13811e1e598dSJonathan Doman 1382156d6b00SAppaRao Puli for (const auto& it : 1383156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1384156d6b00SAppaRao Puli { 1385e05aec50SEd Tanous Subscription& entry = *it.second; 1386c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1387156d6b00SAppaRao Puli { 13881e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1389156d6b00SAppaRao Puli } 1390156d6b00SAppaRao Puli } 1391156d6b00SAppaRao Puli } 1392156d6b00SAppaRao Puli 1393156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1394156d6b00SAppaRao Puli { 13957d1cc387SAppaRao Puli if (matchTelemetryMonitor) 13967d1cc387SAppaRao Puli { 1397156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1398156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1399156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1400156d6b00SAppaRao Puli } 14017d1cc387SAppaRao Puli } 1402156d6b00SAppaRao Puli 1403156d6b00SAppaRao Puli void registerMetricReportSignal() 1404156d6b00SAppaRao Puli { 14057d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1406156d6b00SAppaRao Puli { 14077d1cc387SAppaRao Puli BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1408156d6b00SAppaRao Puli return; 1409156d6b00SAppaRao Puli } 1410156d6b00SAppaRao Puli 1411156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1412c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1413c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1414c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1415156d6b00SAppaRao Puli 141659d494eeSPatrick Williams matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>( 141756d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1418156d6b00SAppaRao Puli } 141923a21a1cSEd Tanous }; 1420b52664e2SAppaRao Puli 1421b52664e2SAppaRao Puli } // namespace redfish 1422