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 17c0353249SWludzik, Jozef #include "metric_report.hpp" 187f4eb588SAppaRao Puli #include "registries.hpp" 197f4eb588SAppaRao Puli #include "registries/base_message_registry.hpp" 207f4eb588SAppaRao Puli #include "registries/openbmc_message_registry.hpp" 21b304bd79SP Dheeraj Srujan Kumar #include "registries/task_event_message_registry.hpp" 227f4eb588SAppaRao Puli 237f4eb588SAppaRao Puli #include <sys/inotify.h> 24b52664e2SAppaRao Puli 25fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 26b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 27168e20c1SEd Tanous #include <dbus_utility.hpp> 281214b7e7SGunnar Mills #include <error_messages.hpp> 2928afb49cSJunLin Chen #include <event_service_store.hpp> 30eb1c47d3SEd Tanous #include <http/utility.hpp> 311214b7e7SGunnar Mills #include <http_client.hpp> 3228afb49cSJunLin Chen #include <persistent_data.hpp> 33fc76b8acSEd Tanous #include <random.hpp> 344bbf237fSAppaRao Puli #include <server_sent_events.hpp> 351214b7e7SGunnar Mills #include <utils/json_utils.hpp> 361214b7e7SGunnar Mills 37b52664e2SAppaRao Puli #include <cstdlib> 38b52664e2SAppaRao Puli #include <ctime> 391bf712bcSAyushi Smriti #include <fstream> 40b52664e2SAppaRao Puli #include <memory> 4126702d01SEd Tanous #include <span> 42b52664e2SAppaRao Puli 43b52664e2SAppaRao Puli namespace redfish 44b52664e2SAppaRao Puli { 45156d6b00SAppaRao Puli 46156d6b00SAppaRao Puli using ReadingsObjType = 4793f5d7c7SWludzik, Jozef std::vector<std::tuple<std::string, std::string, double, int32_t>>; 48156d6b00SAppaRao Puli 49156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 50156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 51156d6b00SAppaRao Puli 521bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 531bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 541bf712bcSAyushi Smriti 55fffb8c1fSEd Tanous namespace registries 567f4eb588SAppaRao Puli { 5726702d01SEd Tanous inline std::span<const MessageEntry> 58b304bd79SP Dheeraj Srujan Kumar getRegistryFromPrefix(const std::string& registryName) 59b304bd79SP Dheeraj Srujan Kumar { 60b304bd79SP Dheeraj Srujan Kumar if (task_event::header.registryPrefix == registryName) 61b304bd79SP Dheeraj Srujan Kumar { 6226702d01SEd Tanous return {task_event::registry}; 63b304bd79SP Dheeraj Srujan Kumar } 64b304bd79SP Dheeraj Srujan Kumar if (openbmc::header.registryPrefix == registryName) 65b304bd79SP Dheeraj Srujan Kumar { 6626702d01SEd Tanous return {openbmc::registry}; 67b304bd79SP Dheeraj Srujan Kumar } 68b304bd79SP Dheeraj Srujan Kumar if (base::header.registryPrefix == registryName) 69b304bd79SP Dheeraj Srujan Kumar { 7026702d01SEd Tanous return {base::registry}; 71b304bd79SP Dheeraj Srujan Kumar } 7226702d01SEd Tanous return {openbmc::registry}; 73b304bd79SP Dheeraj Srujan Kumar } 74fffb8c1fSEd Tanous } // namespace registries 75b304bd79SP Dheeraj Srujan Kumar 764642bf8fSGeorge Liu #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 774642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 784642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 794642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 804642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 814642bf8fSGeorge Liu static int inotifyFd = -1; 824642bf8fSGeorge Liu static int dirWatchDesc = -1; 834642bf8fSGeorge Liu static int fileWatchDesc = -1; 844642bf8fSGeorge Liu 854642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 864642bf8fSGeorge Liu using EventLogObjectsType = 874642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 884642bf8fSGeorge Liu std::vector<std::string>>; 894642bf8fSGeorge Liu 90fffb8c1fSEd Tanous namespace registries 914642bf8fSGeorge Liu { 927f4eb588SAppaRao Puli static const Message* 937f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 9426702d01SEd Tanous const std::span<const MessageEntry>& registry) 957f4eb588SAppaRao Puli { 9626702d01SEd Tanous std::span<const MessageEntry>::iterator messageIt = 9726702d01SEd Tanous std::find_if(registry.begin(), registry.end(), 987f4eb588SAppaRao Puli [&messageKey](const MessageEntry& messageEntry) { 9955f79e6fSEd Tanous return messageKey == messageEntry.first; 1007f4eb588SAppaRao Puli }); 10126702d01SEd Tanous if (messageIt != registry.end()) 1027f4eb588SAppaRao Puli { 1037f4eb588SAppaRao Puli return &messageIt->second; 1047f4eb588SAppaRao Puli } 1057f4eb588SAppaRao Puli 1067f4eb588SAppaRao Puli return nullptr; 1077f4eb588SAppaRao Puli } 1087f4eb588SAppaRao Puli 1097f4eb588SAppaRao Puli static const Message* formatMessage(const std::string_view& messageID) 1107f4eb588SAppaRao Puli { 1117f4eb588SAppaRao Puli // Redfish MessageIds are in the form 1127f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1137f4eb588SAppaRao Puli // the right Message 1147f4eb588SAppaRao Puli std::vector<std::string> fields; 1157f4eb588SAppaRao Puli fields.reserve(4); 1167f4eb588SAppaRao Puli boost::split(fields, messageID, boost::is_any_of(".")); 1177f4eb588SAppaRao Puli if (fields.size() != 4) 1187f4eb588SAppaRao Puli { 1197f4eb588SAppaRao Puli return nullptr; 1207f4eb588SAppaRao Puli } 1217f4eb588SAppaRao Puli std::string& registryName = fields[0]; 1227f4eb588SAppaRao Puli std::string& messageKey = fields[3]; 1237f4eb588SAppaRao Puli 1247f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 125b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1267f4eb588SAppaRao Puli } 127fffb8c1fSEd Tanous } // namespace registries 1287f4eb588SAppaRao Puli 1297f4eb588SAppaRao Puli namespace event_log 1307f4eb588SAppaRao Puli { 1312558979cSP Dheeraj Srujan Kumar inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID) 1327f4eb588SAppaRao Puli { 1337f4eb588SAppaRao Puli static time_t prevTs = 0; 1347f4eb588SAppaRao Puli static int index = 0; 1357f4eb588SAppaRao Puli 1367f4eb588SAppaRao Puli // Get the entry timestamp 1377f4eb588SAppaRao Puli std::time_t curTs = 0; 1387f4eb588SAppaRao Puli std::tm timeStruct = {}; 1397f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1407f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1417f4eb588SAppaRao Puli { 1427f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1437f4eb588SAppaRao Puli if (curTs == -1) 1447f4eb588SAppaRao Puli { 1457f4eb588SAppaRao Puli return false; 1467f4eb588SAppaRao Puli } 1477f4eb588SAppaRao Puli } 1487f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1497f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1507f4eb588SAppaRao Puli 1517f4eb588SAppaRao Puli // Save the timestamp 1527f4eb588SAppaRao Puli prevTs = curTs; 1537f4eb588SAppaRao Puli 1547f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1557f4eb588SAppaRao Puli if (index > 0) 1567f4eb588SAppaRao Puli { 1577f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1587f4eb588SAppaRao Puli } 1597f4eb588SAppaRao Puli return true; 1607f4eb588SAppaRao Puli } 1617f4eb588SAppaRao Puli 16223a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 16323a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1645e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1657f4eb588SAppaRao Puli { 1667f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1677f4eb588SAppaRao Puli // First get the Timestamp 168f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1697f4eb588SAppaRao Puli if (space == std::string::npos) 1707f4eb588SAppaRao Puli { 1717f4eb588SAppaRao Puli return -EINVAL; 1727f4eb588SAppaRao Puli } 1737f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1747f4eb588SAppaRao Puli // Then get the log contents 175f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1767f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1777f4eb588SAppaRao Puli { 1787f4eb588SAppaRao Puli return -EINVAL; 1797f4eb588SAppaRao Puli } 1807f4eb588SAppaRao Puli std::string_view entry(logEntry); 1817f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1827f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1837f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 1847f4eb588SAppaRao Puli boost::split(logEntryFields, entry, boost::is_any_of(","), 1857f4eb588SAppaRao Puli boost::token_compress_on); 1867f4eb588SAppaRao Puli // We need at least a MessageId to be valid 18726f6976fSEd Tanous if (logEntryFields.empty()) 1887f4eb588SAppaRao Puli { 1897f4eb588SAppaRao Puli return -EINVAL; 1907f4eb588SAppaRao Puli } 1917f4eb588SAppaRao Puli messageID = logEntryFields[0]; 1927f4eb588SAppaRao Puli 1937f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 1947f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 1957f4eb588SAppaRao Puli { 1967f4eb588SAppaRao Puli std::string& messageArgsStart = logEntryFields[1]; 1977f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 1987f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 1997f4eb588SAppaRao Puli { 2005e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 2015e715de6SAppaRao Puli logEntryFields.end()); 2027f4eb588SAppaRao Puli } 2037f4eb588SAppaRao Puli } 2047f4eb588SAppaRao Puli 2057f4eb588SAppaRao Puli return 0; 2067f4eb588SAppaRao Puli } 2077f4eb588SAppaRao Puli 20823a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 2097f4eb588SAppaRao Puli std::string& registryName, 2107f4eb588SAppaRao Puli std::string& messageKey) 2117f4eb588SAppaRao Puli { 2127f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2137f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2147f4eb588SAppaRao Puli // the right Message 2157f4eb588SAppaRao Puli std::vector<std::string> fields; 2167f4eb588SAppaRao Puli fields.reserve(4); 2177f4eb588SAppaRao Puli boost::split(fields, messageID, boost::is_any_of(".")); 2187f4eb588SAppaRao Puli if (fields.size() == 4) 2197f4eb588SAppaRao Puli { 2207f4eb588SAppaRao Puli registryName = fields[0]; 2217f4eb588SAppaRao Puli messageKey = fields[3]; 2227f4eb588SAppaRao Puli } 2237f4eb588SAppaRao Puli } 2247f4eb588SAppaRao Puli 22523a21a1cSEd Tanous inline int formatEventLogEntry(const std::string& logEntryID, 2267f4eb588SAppaRao Puli const std::string& messageID, 227c5ba4c27SEd Tanous const std::span<std::string_view> messageArgs, 22823a21a1cSEd Tanous std::string timestamp, 229b5a76932SEd Tanous const std::string& customText, 2307f4eb588SAppaRao Puli nlohmann::json& logEntryJson) 2317f4eb588SAppaRao Puli { 2327f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 233fffb8c1fSEd Tanous const registries::Message* message = registries::formatMessage(messageID); 2347f4eb588SAppaRao Puli 2357f4eb588SAppaRao Puli std::string msg; 2367f4eb588SAppaRao Puli std::string severity; 2377f4eb588SAppaRao Puli if (message != nullptr) 2387f4eb588SAppaRao Puli { 2397f4eb588SAppaRao Puli msg = message->message; 2405f2b84eeSEd Tanous severity = message->messageSeverity; 2417f4eb588SAppaRao Puli } 2427f4eb588SAppaRao Puli 243fffb8c1fSEd Tanous redfish::registries::fillMessageArgs(messageArgs, msg); 2447f4eb588SAppaRao Puli 2457f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2467f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2477f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 248f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 249f23b7296SEd Tanous std::size_t plus = timestamp.find_first_of('+'); 2507f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2517f4eb588SAppaRao Puli { 2527f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2537f4eb588SAppaRao Puli } 2547f4eb588SAppaRao Puli 2557f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2567f4eb588SAppaRao Puli logEntryJson = {{"EventId", logEntryID}, 2577f4eb588SAppaRao Puli {"EventType", "Event"}, 2587f4eb588SAppaRao Puli {"Severity", std::move(severity)}, 2597f4eb588SAppaRao Puli {"Message", std::move(msg)}, 260f23b7296SEd Tanous {"MessageId", messageID}, 261f23b7296SEd Tanous {"MessageArgs", messageArgs}, 2627f4eb588SAppaRao Puli {"EventTimestamp", std::move(timestamp)}, 2637f4eb588SAppaRao Puli {"Context", customText}}; 2647f4eb588SAppaRao Puli return 0; 2657f4eb588SAppaRao Puli } 2667f4eb588SAppaRao Puli 2677f4eb588SAppaRao Puli } // namespace event_log 2687f4eb588SAppaRao Puli #endif 2697f4eb588SAppaRao Puli 27023a21a1cSEd Tanous inline bool isFilterQuerySpecialChar(char c) 27107941a88SAyushi Smriti { 27207941a88SAyushi Smriti switch (c) 27307941a88SAyushi Smriti { 27407941a88SAyushi Smriti case '(': 27507941a88SAyushi Smriti case ')': 27607941a88SAyushi Smriti case '\'': 27707941a88SAyushi Smriti return true; 27807941a88SAyushi Smriti default: 27907941a88SAyushi Smriti return false; 28007941a88SAyushi Smriti } 28107941a88SAyushi Smriti } 28207941a88SAyushi Smriti 28323a21a1cSEd Tanous inline bool 28423a21a1cSEd Tanous readSSEQueryParams(std::string sseFilter, std::string& formatType, 28507941a88SAyushi Smriti std::vector<std::string>& messageIds, 28607941a88SAyushi Smriti std::vector<std::string>& registryPrefixes, 287144b6318SAppaRao Puli std::vector<std::string>& metricReportDefinitions) 28807941a88SAyushi Smriti { 28907941a88SAyushi Smriti sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(), 29007941a88SAyushi Smriti isFilterQuerySpecialChar), 29107941a88SAyushi Smriti sseFilter.end()); 29207941a88SAyushi Smriti 29307941a88SAyushi Smriti std::vector<std::string> result; 29407941a88SAyushi Smriti boost::split(result, sseFilter, boost::is_any_of(" "), 29507941a88SAyushi Smriti boost::token_compress_on); 29607941a88SAyushi Smriti 29707941a88SAyushi Smriti BMCWEB_LOG_DEBUG << "No of tokens in SEE query: " << result.size(); 29807941a88SAyushi Smriti 29907941a88SAyushi Smriti constexpr uint8_t divisor = 4; 30007941a88SAyushi Smriti constexpr uint8_t minTokenSize = 3; 30107941a88SAyushi Smriti if (result.size() % divisor != minTokenSize) 30207941a88SAyushi Smriti { 30307941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid SSE filter specified."; 30407941a88SAyushi Smriti return false; 30507941a88SAyushi Smriti } 30607941a88SAyushi Smriti 30707941a88SAyushi Smriti for (std::size_t i = 0; i < result.size(); i += divisor) 30807941a88SAyushi Smriti { 30907941a88SAyushi Smriti std::string& key = result[i]; 31007941a88SAyushi Smriti std::string& op = result[i + 1]; 31107941a88SAyushi Smriti std::string& value = result[i + 2]; 31207941a88SAyushi Smriti 31307941a88SAyushi Smriti if ((i + minTokenSize) < result.size()) 31407941a88SAyushi Smriti { 3154e0453b1SGunnar Mills std::string& separator = result[i + minTokenSize]; 31607941a88SAyushi Smriti // SSE supports only "or" and "and" in query params. 3174e0453b1SGunnar Mills if ((separator != "or") && (separator != "and")) 31807941a88SAyushi Smriti { 31907941a88SAyushi Smriti BMCWEB_LOG_ERROR 32007941a88SAyushi Smriti << "Invalid group operator in SSE query parameters"; 32107941a88SAyushi Smriti return false; 32207941a88SAyushi Smriti } 32307941a88SAyushi Smriti } 32407941a88SAyushi Smriti 32507941a88SAyushi Smriti // SSE supports only "eq" as per spec. 32607941a88SAyushi Smriti if (op != "eq") 32707941a88SAyushi Smriti { 32807941a88SAyushi Smriti BMCWEB_LOG_ERROR 32907941a88SAyushi Smriti << "Invalid assignment operator in SSE query parameters"; 33007941a88SAyushi Smriti return false; 33107941a88SAyushi Smriti } 33207941a88SAyushi Smriti 33307941a88SAyushi Smriti BMCWEB_LOG_DEBUG << key << " : " << value; 33407941a88SAyushi Smriti if (key == "EventFormatType") 33507941a88SAyushi Smriti { 33607941a88SAyushi Smriti formatType = value; 33707941a88SAyushi Smriti } 33807941a88SAyushi Smriti else if (key == "MessageId") 33907941a88SAyushi Smriti { 34007941a88SAyushi Smriti messageIds.push_back(value); 34107941a88SAyushi Smriti } 34207941a88SAyushi Smriti else if (key == "RegistryPrefix") 34307941a88SAyushi Smriti { 34407941a88SAyushi Smriti registryPrefixes.push_back(value); 34507941a88SAyushi Smriti } 34607941a88SAyushi Smriti else if (key == "MetricReportDefinition") 34707941a88SAyushi Smriti { 34807941a88SAyushi Smriti metricReportDefinitions.push_back(value); 34907941a88SAyushi Smriti } 35007941a88SAyushi Smriti else 35107941a88SAyushi Smriti { 35207941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid property(" << key 35307941a88SAyushi Smriti << ")in SSE filter query."; 35407941a88SAyushi Smriti return false; 35507941a88SAyushi Smriti } 35607941a88SAyushi Smriti } 35707941a88SAyushi Smriti return true; 35807941a88SAyushi Smriti } 35907941a88SAyushi Smriti 36028afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 361b52664e2SAppaRao Puli { 362b52664e2SAppaRao Puli public: 363b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 364b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 365b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 366b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 367b52664e2SAppaRao Puli 368eb1c47d3SEd Tanous Subscription(const std::string& inHost, uint16_t inPort, 369b52664e2SAppaRao Puli const std::string& inPath, const std::string& inUriProto) : 3700b4bdd93SAppaRao Puli eventSeqNum(1), 3710b4bdd93SAppaRao Puli host(inHost), port(inPort), path(inPath), uriProto(inUriProto) 372b52664e2SAppaRao Puli { 3737adb85acSSunitha Harish // Subscription constructor 374b52664e2SAppaRao Puli } 3754bbf237fSAppaRao Puli 37623e64207SEd Tanous Subscription(const std::shared_ptr<boost::beast::tcp_stream>& adaptor) : 3774bbf237fSAppaRao Puli eventSeqNum(1) 3784bbf237fSAppaRao Puli { 3794bbf237fSAppaRao Puli sseConn = std::make_shared<crow::ServerSentEvents>(adaptor); 3804bbf237fSAppaRao Puli } 3814bbf237fSAppaRao Puli 3829f616dd1SEd Tanous ~Subscription() = default; 383b52664e2SAppaRao Puli 384*f52c03c1SCarson Labrado bool sendEvent(std::string& msg) 385b52664e2SAppaRao Puli { 3866ba8c82eSsunharis_in persistent_data::EventServiceConfig eventServiceConfig = 3876ba8c82eSsunharis_in persistent_data::EventServiceStore::getInstance() 3886ba8c82eSsunharis_in .getEventServiceConfig(); 3896ba8c82eSsunharis_in if (!eventServiceConfig.enabled) 3906ba8c82eSsunharis_in { 3916ba8c82eSsunharis_in return false; 3926ba8c82eSsunharis_in } 3936ba8c82eSsunharis_in 394*f52c03c1SCarson Labrado // A connection pool will be created if one does not already exist 395*f52c03c1SCarson Labrado crow::HttpClient::getInstance().sendData(msg, id, host, port, path, 396*f52c03c1SCarson Labrado httpHeaders, retryPolicyName); 3977adb85acSSunitha Harish eventSeqNum++; 3987adb85acSSunitha Harish 3994bbf237fSAppaRao Puli if (sseConn != nullptr) 4004bbf237fSAppaRao Puli { 4014bbf237fSAppaRao Puli sseConn->sendData(eventSeqNum, msg); 4024bbf237fSAppaRao Puli } 4036ba8c82eSsunharis_in return true; 4044bbf237fSAppaRao Puli } 4054bbf237fSAppaRao Puli 4066ba8c82eSsunharis_in bool sendTestEventLog() 4070b4bdd93SAppaRao Puli { 4080b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 4090b4bdd93SAppaRao Puli logEntryArray.push_back({}); 4100b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 4110b4bdd93SAppaRao Puli 4127c8c4058STejas Patil logEntryJson = { 4137c8c4058STejas Patil {"EventId", "TestID"}, 4140b4bdd93SAppaRao Puli {"EventType", "Event"}, 4150b4bdd93SAppaRao Puli {"Severity", "OK"}, 4160b4bdd93SAppaRao Puli {"Message", "Generated test event"}, 4174a0bf539SManojkiran Eda {"MessageId", "OpenBMC.0.2.TestEventLog"}, 4180b4bdd93SAppaRao Puli {"MessageArgs", nlohmann::json::array()}, 4197c8c4058STejas Patil {"EventTimestamp", crow::utility::getDateTimeOffsetNow().first}, 4200b4bdd93SAppaRao Puli {"Context", customText}}; 4210b4bdd93SAppaRao Puli 4220b4bdd93SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 4230b4bdd93SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 4240b4bdd93SAppaRao Puli {"Name", "Event Log"}, 4250b4bdd93SAppaRao Puli {"Events", logEntryArray}}; 4260b4bdd93SAppaRao Puli 427*f52c03c1SCarson Labrado std::string strMsg = 428*f52c03c1SCarson Labrado msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 429*f52c03c1SCarson Labrado return this->sendEvent(strMsg); 4300b4bdd93SAppaRao Puli } 4310b4bdd93SAppaRao Puli 4327f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 4337f4eb588SAppaRao Puli void filterAndSendEventLogs( 4347f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4357f4eb588SAppaRao Puli { 4367f4eb588SAppaRao Puli nlohmann::json logEntryArray; 4377f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4387f4eb588SAppaRao Puli { 4397f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 4407f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 4417f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 4427f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 4437f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 4445e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 4457f4eb588SAppaRao Puli 4467f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 4477f4eb588SAppaRao Puli // send everything. 44826f6976fSEd Tanous if (!registryPrefixes.empty()) 4497f4eb588SAppaRao Puli { 4507f4eb588SAppaRao Puli auto obj = std::find(registryPrefixes.begin(), 4517f4eb588SAppaRao Puli registryPrefixes.end(), registryName); 4527f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 4537f4eb588SAppaRao Puli { 4547f4eb588SAppaRao Puli continue; 4557f4eb588SAppaRao Puli } 4567f4eb588SAppaRao Puli } 4577f4eb588SAppaRao Puli 4587f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 4597f4eb588SAppaRao Puli // send everything. 46026f6976fSEd Tanous if (!registryMsgIds.empty()) 4617f4eb588SAppaRao Puli { 4627f4eb588SAppaRao Puli auto obj = std::find(registryMsgIds.begin(), 4637f4eb588SAppaRao Puli registryMsgIds.end(), messageKey); 4647f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 4657f4eb588SAppaRao Puli { 4667f4eb588SAppaRao Puli continue; 4677f4eb588SAppaRao Puli } 4687f4eb588SAppaRao Puli } 4697f4eb588SAppaRao Puli 470c5ba4c27SEd Tanous std::vector<std::string_view> messageArgsView(messageArgs.begin(), 471c5ba4c27SEd Tanous messageArgs.end()); 472c5ba4c27SEd Tanous 4737f4eb588SAppaRao Puli logEntryArray.push_back({}); 4747f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 475c5ba4c27SEd Tanous if (event_log::formatEventLogEntry(idStr, messageID, 476c5ba4c27SEd Tanous messageArgsView, timestamp, 477c5ba4c27SEd Tanous customText, bmcLogEntry) != 0) 4787f4eb588SAppaRao Puli { 4797f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 4807f4eb588SAppaRao Puli continue; 4817f4eb588SAppaRao Puli } 4827f4eb588SAppaRao Puli } 4837f4eb588SAppaRao Puli 48426f6976fSEd Tanous if (logEntryArray.empty()) 4857f4eb588SAppaRao Puli { 4867f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 4877f4eb588SAppaRao Puli return; 4887f4eb588SAppaRao Puli } 4897f4eb588SAppaRao Puli 4907f4eb588SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 4917f4eb588SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 4927f4eb588SAppaRao Puli {"Name", "Event Log"}, 4937f4eb588SAppaRao Puli {"Events", logEntryArray}}; 4947f4eb588SAppaRao Puli 495*f52c03c1SCarson Labrado std::string strMsg = 496*f52c03c1SCarson Labrado msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 497*f52c03c1SCarson Labrado this->sendEvent(strMsg); 4987f4eb588SAppaRao Puli } 4997f4eb588SAppaRao Puli #endif 5007f4eb588SAppaRao Puli 501248d0230SEd Tanous void filterAndSendReports(const std::string& reportId, 5021e1e598dSJonathan Doman const telemetry::TimestampReadings& var) 503156d6b00SAppaRao Puli { 504456cd875SSzymon Dompke boost::urls::url mrdUri = 505456cd875SSzymon Dompke crow::utility::urlFromPieces("redfish", "v1", "TelemetryService", 506456cd875SSzymon Dompke "MetricReportDefinitions", reportId); 507156d6b00SAppaRao Puli 508156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 50926f6976fSEd Tanous if (!metricReportDefinitions.empty()) 510156d6b00SAppaRao Puli { 511156d6b00SAppaRao Puli if (std::find(metricReportDefinitions.begin(), 512156d6b00SAppaRao Puli metricReportDefinitions.end(), 513456cd875SSzymon Dompke mrdUri.string()) == metricReportDefinitions.end()) 514156d6b00SAppaRao Puli { 515156d6b00SAppaRao Puli return; 516156d6b00SAppaRao Puli } 517156d6b00SAppaRao Puli } 518156d6b00SAppaRao Puli 519c0353249SWludzik, Jozef nlohmann::json msg; 520248d0230SEd Tanous if (!telemetry::fillReport(msg, reportId, var)) 521156d6b00SAppaRao Puli { 522c0353249SWludzik, Jozef BMCWEB_LOG_ERROR << "Failed to fill the MetricReport for DBus " 523c0353249SWludzik, Jozef "Report with id " 524248d0230SEd Tanous << reportId; 525c0353249SWludzik, Jozef return; 526156d6b00SAppaRao Puli } 527156d6b00SAppaRao Puli 528*f52c03c1SCarson Labrado std::string strMsg = 529*f52c03c1SCarson Labrado msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); 530*f52c03c1SCarson Labrado this->sendEvent(strMsg); 531156d6b00SAppaRao Puli } 532156d6b00SAppaRao Puli 533fe44eb0bSAyushi Smriti void updateRetryConfig(const uint32_t retryAttempts, 534fe44eb0bSAyushi Smriti const uint32_t retryTimeoutInterval) 535fe44eb0bSAyushi Smriti { 536*f52c03c1SCarson Labrado crow::HttpClient::getInstance().setRetryConfig( 537*f52c03c1SCarson Labrado retryAttempts, retryTimeoutInterval, retryPolicyName); 53862de0c68SAppaRao Puli } 539fe44eb0bSAyushi Smriti 540fe44eb0bSAyushi Smriti void updateRetryPolicy() 541fe44eb0bSAyushi Smriti { 542*f52c03c1SCarson Labrado crow::HttpClient::getInstance().setRetryPolicy(retryPolicy, 543*f52c03c1SCarson Labrado retryPolicyName); 54462de0c68SAppaRao Puli } 545fe44eb0bSAyushi Smriti 5469eb808c1SEd Tanous uint64_t getEventSeqNum() const 54796330b99SSunitha Harish { 54896330b99SSunitha Harish return eventSeqNum; 54996330b99SSunitha Harish } 55096330b99SSunitha Harish 551b52664e2SAppaRao Puli private: 5520b4bdd93SAppaRao Puli uint64_t eventSeqNum; 553b52664e2SAppaRao Puli std::string host; 554eb1c47d3SEd Tanous uint16_t port = 0; 555b52664e2SAppaRao Puli std::string path; 556b52664e2SAppaRao Puli std::string uriProto; 5574bbf237fSAppaRao Puli std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr; 558*f52c03c1SCarson Labrado std::string retryPolicyName = "SubscriptionEvent"; 559b52664e2SAppaRao Puli }; 560b52664e2SAppaRao Puli 561b52664e2SAppaRao Puli class EventServiceManager 562b52664e2SAppaRao Puli { 563b52664e2SAppaRao Puli private: 564d3a9e084SEd Tanous bool serviceEnabled = false; 565d3a9e084SEd Tanous uint32_t retryAttempts = 0; 566d3a9e084SEd Tanous uint32_t retryTimeoutInterval = 0; 5677d1cc387SAppaRao Puli 5689f616dd1SEd Tanous EventServiceManager() 569b52664e2SAppaRao Puli { 5701bf712bcSAyushi Smriti // Load config from persist store. 5711bf712bcSAyushi Smriti initConfig(); 572b52664e2SAppaRao Puli } 573b52664e2SAppaRao Puli 5742558979cSP Dheeraj Srujan Kumar std::streampos redfishLogFilePosition{0}; 5759f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 5769f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 577156d6b00SAppaRao Puli std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor; 578b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 579b52664e2SAppaRao Puli subscriptionsMap; 580b52664e2SAppaRao Puli 5819f616dd1SEd Tanous uint64_t eventId{1}; 58296330b99SSunitha Harish 583b52664e2SAppaRao Puli public: 5849f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 5859f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 5869f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 5879f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 588ecd6a3a2SEd Tanous ~EventServiceManager() = default; 5899f616dd1SEd Tanous 590b52664e2SAppaRao Puli static EventServiceManager& getInstance() 591b52664e2SAppaRao Puli { 592b52664e2SAppaRao Puli static EventServiceManager handler; 593b52664e2SAppaRao Puli return handler; 594b52664e2SAppaRao Puli } 595b52664e2SAppaRao Puli 5961bf712bcSAyushi Smriti void initConfig() 5971bf712bcSAyushi Smriti { 59828afb49cSJunLin Chen loadOldBehavior(); 5991bf712bcSAyushi Smriti 60028afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 60128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 60228afb49cSJunLin Chen .getEventServiceConfig(); 6031bf712bcSAyushi Smriti 60428afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 60528afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 60628afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6071bf712bcSAyushi Smriti 60828afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 60928afb49cSJunLin Chen .subscriptionsConfigMap) 6101bf712bcSAyushi Smriti { 61128afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 61228afb49cSJunLin Chen it.second; 6134bbf237fSAppaRao Puli 6141bf712bcSAyushi Smriti std::string host; 6151bf712bcSAyushi Smriti std::string urlProto; 616eb1c47d3SEd Tanous uint16_t port = 0; 6171bf712bcSAyushi Smriti std::string path; 61811baefe4SEd Tanous bool status = crow::utility::validateAndSplitUrl( 61911baefe4SEd Tanous newSub->destinationUrl, urlProto, host, port, path); 6201bf712bcSAyushi Smriti 6211bf712bcSAyushi Smriti if (!status) 6221bf712bcSAyushi Smriti { 6231bf712bcSAyushi Smriti BMCWEB_LOG_ERROR 6241bf712bcSAyushi Smriti << "Failed to validate and split destination url"; 6251bf712bcSAyushi Smriti continue; 6261bf712bcSAyushi Smriti } 6271bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 6281bf712bcSAyushi Smriti std::make_shared<Subscription>(host, port, path, urlProto); 6291bf712bcSAyushi Smriti 63028afb49cSJunLin Chen subValue->id = newSub->id; 63128afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 63228afb49cSJunLin Chen subValue->protocol = newSub->protocol; 63328afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 63428afb49cSJunLin Chen subValue->customText = newSub->customText; 63528afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 63628afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 63728afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 63828afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 63928afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 64028afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 64128afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 6421bf712bcSAyushi Smriti 64328afb49cSJunLin Chen if (subValue->id.empty()) 6441bf712bcSAyushi Smriti { 6451bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Failed to add subscription"; 6461bf712bcSAyushi Smriti } 64728afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 64828afb49cSJunLin Chen 64928afb49cSJunLin Chen updateNoOfSubscribersCount(); 65028afb49cSJunLin Chen 65128afb49cSJunLin Chen #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 6522558979cSP Dheeraj Srujan Kumar 6532558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 6542558979cSP Dheeraj Srujan Kumar 65528afb49cSJunLin Chen #endif 65628afb49cSJunLin Chen // Update retry configuration. 65728afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 65828afb49cSJunLin Chen subValue->updateRetryPolicy(); 6591bf712bcSAyushi Smriti } 6601bf712bcSAyushi Smriti } 6611bf712bcSAyushi Smriti 66256d2396dSEd Tanous static void loadOldBehavior() 663b52664e2SAppaRao Puli { 66428afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 66528afb49cSJunLin Chen if (!eventConfigFile.good()) 6661bf712bcSAyushi Smriti { 66728afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Old eventService config not exist"; 66828afb49cSJunLin Chen return; 66928afb49cSJunLin Chen } 67028afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 67128afb49cSJunLin Chen if (jsonData.is_discarded()) 6724bbf237fSAppaRao Puli { 67328afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Old eventService config parse error."; 67428afb49cSJunLin Chen return; 67528afb49cSJunLin Chen } 67628afb49cSJunLin Chen 67728afb49cSJunLin Chen for (const auto& item : jsonData.items()) 67828afb49cSJunLin Chen { 67928afb49cSJunLin Chen if (item.key() == "Configuration") 68028afb49cSJunLin Chen { 68128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 68228afb49cSJunLin Chen .getEventServiceConfig() 68328afb49cSJunLin Chen .fromJson(item.value()); 68428afb49cSJunLin Chen } 68528afb49cSJunLin Chen else if (item.key() == "Subscriptions") 68628afb49cSJunLin Chen { 68728afb49cSJunLin Chen for (const auto& elem : item.value()) 68828afb49cSJunLin Chen { 68928afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 69028afb49cSJunLin Chen newSubscription = 69128afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 69228afb49cSJunLin Chen true); 69328afb49cSJunLin Chen if (newSubscription == nullptr) 69428afb49cSJunLin Chen { 69528afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Problem reading subscription " 69628afb49cSJunLin Chen "from old persistent store"; 6974bbf237fSAppaRao Puli continue; 6984bbf237fSAppaRao Puli } 6991bf712bcSAyushi Smriti 70028afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 70128afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 7021bf712bcSAyushi Smriti 70328afb49cSJunLin Chen std::string id; 7041bf712bcSAyushi Smriti 70528afb49cSJunLin Chen int retry = 3; 706e662eae8SEd Tanous while (retry != 0) 7071bf712bcSAyushi Smriti { 70828afb49cSJunLin Chen id = std::to_string(dist(gen)); 70928afb49cSJunLin Chen if (gen.error()) 7107d1cc387SAppaRao Puli { 71128afb49cSJunLin Chen retry = 0; 71228afb49cSJunLin Chen break; 71328afb49cSJunLin Chen } 71428afb49cSJunLin Chen newSubscription->id = id; 71528afb49cSJunLin Chen auto inserted = 71628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 71728afb49cSJunLin Chen .subscriptionsConfigMap.insert( 71828afb49cSJunLin Chen std::pair(id, newSubscription)); 71928afb49cSJunLin Chen if (inserted.second) 72028afb49cSJunLin Chen { 72128afb49cSJunLin Chen break; 72228afb49cSJunLin Chen } 72328afb49cSJunLin Chen --retry; 7247d1cc387SAppaRao Puli } 7257d1cc387SAppaRao Puli 72628afb49cSJunLin Chen if (retry <= 0) 72728afb49cSJunLin Chen { 72828afb49cSJunLin Chen BMCWEB_LOG_ERROR 72928afb49cSJunLin Chen << "Failed to generate random number from old " 73028afb49cSJunLin Chen "persistent store"; 73128afb49cSJunLin Chen continue; 73228afb49cSJunLin Chen } 73328afb49cSJunLin Chen } 73428afb49cSJunLin Chen } 73528afb49cSJunLin Chen 73628afb49cSJunLin Chen persistent_data::getConfig().writeData(); 73728afb49cSJunLin Chen std::remove(eventServiceFile); 73828afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Remove old eventservice config"; 73928afb49cSJunLin Chen } 74028afb49cSJunLin Chen } 74128afb49cSJunLin Chen 7429eb808c1SEd Tanous void updateSubscriptionData() const 74328afb49cSJunLin Chen { 74428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 74528afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 74628afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 74728afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 74828afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 74928afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 75028afb49cSJunLin Chen 75128afb49cSJunLin Chen persistent_data::getConfig().writeData(); 75228afb49cSJunLin Chen } 75328afb49cSJunLin Chen 75428afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7557d1cc387SAppaRao Puli { 7567d1cc387SAppaRao Puli bool updateConfig = false; 757fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 7587d1cc387SAppaRao Puli 75928afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 7607d1cc387SAppaRao Puli { 76128afb49cSJunLin Chen serviceEnabled = cfg.enabled; 762e662eae8SEd Tanous if (serviceEnabled && noOfMetricReportSubscribers != 0U) 7637d1cc387SAppaRao Puli { 7647d1cc387SAppaRao Puli registerMetricReportSignal(); 7657d1cc387SAppaRao Puli } 7667d1cc387SAppaRao Puli else 7677d1cc387SAppaRao Puli { 7687d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7697d1cc387SAppaRao Puli } 7707d1cc387SAppaRao Puli updateConfig = true; 7717d1cc387SAppaRao Puli } 7727d1cc387SAppaRao Puli 77328afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 7747d1cc387SAppaRao Puli { 77528afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 7767d1cc387SAppaRao Puli updateConfig = true; 777fe44eb0bSAyushi Smriti updateRetryCfg = true; 7787d1cc387SAppaRao Puli } 7797d1cc387SAppaRao Puli 78028afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 7817d1cc387SAppaRao Puli { 78228afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 7837d1cc387SAppaRao Puli updateConfig = true; 784fe44eb0bSAyushi Smriti updateRetryCfg = true; 7857d1cc387SAppaRao Puli } 7867d1cc387SAppaRao Puli 7877d1cc387SAppaRao Puli if (updateConfig) 7887d1cc387SAppaRao Puli { 7897d1cc387SAppaRao Puli updateSubscriptionData(); 7907d1cc387SAppaRao Puli } 791fe44eb0bSAyushi Smriti 792fe44eb0bSAyushi Smriti if (updateRetryCfg) 793fe44eb0bSAyushi Smriti { 794fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 795fe44eb0bSAyushi Smriti for (const auto& it : 796fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 797fe44eb0bSAyushi Smriti { 798fe44eb0bSAyushi Smriti std::shared_ptr<Subscription> entry = it.second; 799fe44eb0bSAyushi Smriti entry->updateRetryConfig(retryAttempts, retryTimeoutInterval); 800fe44eb0bSAyushi Smriti } 801fe44eb0bSAyushi Smriti } 8027d1cc387SAppaRao Puli } 8037d1cc387SAppaRao Puli 8047d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8057d1cc387SAppaRao Puli { 8067d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8077d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8087d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8097d1cc387SAppaRao Puli { 8107d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8117d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8127d1cc387SAppaRao Puli { 8137d1cc387SAppaRao Puli eventLogSubCount++; 8147d1cc387SAppaRao Puli } 8157d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8167d1cc387SAppaRao Puli { 8177d1cc387SAppaRao Puli metricReportSubCount++; 8187d1cc387SAppaRao Puli } 8197d1cc387SAppaRao Puli } 8207d1cc387SAppaRao Puli 8217d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8227d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8237d1cc387SAppaRao Puli { 8247d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 825e662eae8SEd Tanous if (noOfMetricReportSubscribers != 0U) 8267d1cc387SAppaRao Puli { 8277d1cc387SAppaRao Puli registerMetricReportSignal(); 8287d1cc387SAppaRao Puli } 8297d1cc387SAppaRao Puli else 8307d1cc387SAppaRao Puli { 8317d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8327d1cc387SAppaRao Puli } 8337d1cc387SAppaRao Puli } 8347d1cc387SAppaRao Puli } 8357d1cc387SAppaRao Puli 836b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 837b52664e2SAppaRao Puli { 838b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 839b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 840b52664e2SAppaRao Puli { 841b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 842b52664e2SAppaRao Puli return nullptr; 843b52664e2SAppaRao Puli } 844b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 845b52664e2SAppaRao Puli return subValue; 846b52664e2SAppaRao Puli } 847b52664e2SAppaRao Puli 848b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 8491bf712bcSAyushi Smriti const bool updateFile = true) 850b52664e2SAppaRao Puli { 851fc76b8acSEd Tanous 852fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 853fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 854fc76b8acSEd Tanous 855b52664e2SAppaRao Puli std::string id; 856b52664e2SAppaRao Puli 857b52664e2SAppaRao Puli int retry = 3; 858e662eae8SEd Tanous while (retry != 0) 859b52664e2SAppaRao Puli { 860fc76b8acSEd Tanous id = std::to_string(dist(gen)); 861fc76b8acSEd Tanous if (gen.error()) 862fc76b8acSEd Tanous { 863fc76b8acSEd Tanous retry = 0; 864fc76b8acSEd Tanous break; 865fc76b8acSEd Tanous } 866b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 867b52664e2SAppaRao Puli if (inserted.second) 868b52664e2SAppaRao Puli { 869b52664e2SAppaRao Puli break; 870b52664e2SAppaRao Puli } 871b52664e2SAppaRao Puli --retry; 87223a21a1cSEd Tanous } 873b52664e2SAppaRao Puli 874b52664e2SAppaRao Puli if (retry <= 0) 875b52664e2SAppaRao Puli { 876b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Failed to generate random number"; 877abb93cddSEd Tanous return ""; 878b52664e2SAppaRao Puli } 879b52664e2SAppaRao Puli 88028afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 88128afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 88228afb49cSJunLin Chen newSub->id = id; 88328afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 88428afb49cSJunLin Chen newSub->protocol = subValue->protocol; 88528afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 88628afb49cSJunLin Chen newSub->customText = subValue->customText; 88728afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 88828afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 88928afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 89028afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 89128afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 89228afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 89328afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 89428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 89528afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 89628afb49cSJunLin Chen 8977d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 8981bf712bcSAyushi Smriti 8991bf712bcSAyushi Smriti if (updateFile) 9001bf712bcSAyushi Smriti { 901b52664e2SAppaRao Puli updateSubscriptionData(); 9021bf712bcSAyushi Smriti } 9037f4eb588SAppaRao Puli 9047f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 9052558979cSP Dheeraj Srujan Kumar if (redfishLogFilePosition != 0) 9067f4eb588SAppaRao Puli { 9072558979cSP Dheeraj Srujan Kumar cacheRedfishLogFile(); 9087f4eb588SAppaRao Puli } 9097f4eb588SAppaRao Puli #endif 910fe44eb0bSAyushi Smriti // Update retry configuration. 911fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 912fe44eb0bSAyushi Smriti subValue->updateRetryPolicy(); 913fe44eb0bSAyushi Smriti 914b52664e2SAppaRao Puli return id; 915b52664e2SAppaRao Puli } 916b52664e2SAppaRao Puli 917b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 918b52664e2SAppaRao Puli { 919b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 92055f79e6fSEd Tanous return obj != subscriptionsMap.end(); 921b52664e2SAppaRao Puli } 922b52664e2SAppaRao Puli 923b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 924b52664e2SAppaRao Puli { 925b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 926b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 927b52664e2SAppaRao Puli { 928b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 92928afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 93028afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 93128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 93228afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9337d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 934b52664e2SAppaRao Puli updateSubscriptionData(); 935b52664e2SAppaRao Puli } 936b52664e2SAppaRao Puli } 937b52664e2SAppaRao Puli 938b52664e2SAppaRao Puli size_t getNumberOfSubscriptions() 939b52664e2SAppaRao Puli { 940b52664e2SAppaRao Puli return subscriptionsMap.size(); 941b52664e2SAppaRao Puli } 942b52664e2SAppaRao Puli 943b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 944b52664e2SAppaRao Puli { 945b52664e2SAppaRao Puli std::vector<std::string> idList; 946b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 947b52664e2SAppaRao Puli { 948b52664e2SAppaRao Puli idList.emplace_back(it.first); 949b52664e2SAppaRao Puli } 950b52664e2SAppaRao Puli return idList; 951b52664e2SAppaRao Puli } 952b52664e2SAppaRao Puli 953b52664e2SAppaRao Puli bool isDestinationExist(const std::string& destUrl) 954b52664e2SAppaRao Puli { 955b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 956b52664e2SAppaRao Puli { 957b52664e2SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 958b52664e2SAppaRao Puli if (entry->destinationUrl == destUrl) 959b52664e2SAppaRao Puli { 960b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 961b52664e2SAppaRao Puli return true; 962b52664e2SAppaRao Puli } 963b52664e2SAppaRao Puli } 964b52664e2SAppaRao Puli return false; 965b52664e2SAppaRao Puli } 9660b4bdd93SAppaRao Puli 9676ba8c82eSsunharis_in bool sendTestEventLog() 9680b4bdd93SAppaRao Puli { 9690b4bdd93SAppaRao Puli for (const auto& it : this->subscriptionsMap) 9700b4bdd93SAppaRao Puli { 9710b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 9726ba8c82eSsunharis_in if (!entry->sendTestEventLog()) 9736ba8c82eSsunharis_in { 9746ba8c82eSsunharis_in return false; 9750b4bdd93SAppaRao Puli } 9760b4bdd93SAppaRao Puli } 9776ba8c82eSsunharis_in return true; 9786ba8c82eSsunharis_in } 979e9a14131SAppaRao Puli 98096330b99SSunitha Harish void sendEvent(const nlohmann::json& eventMessageIn, 98196330b99SSunitha Harish const std::string& origin, const std::string& resType) 98296330b99SSunitha Harish { 983dbb59d4dSEd Tanous if (!serviceEnabled || (noOfEventLogSubscribers == 0U)) 9846ba8c82eSsunharis_in { 9856ba8c82eSsunharis_in BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 9866ba8c82eSsunharis_in return; 9876ba8c82eSsunharis_in } 98896330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 98996330b99SSunitha Harish nlohmann::json eventMessage = eventMessageIn; 99096330b99SSunitha Harish // MemberId is 0 : since we are sending one event record. 99196330b99SSunitha Harish uint64_t memberId = 0; 99296330b99SSunitha Harish 99396330b99SSunitha Harish nlohmann::json event = { 99496330b99SSunitha Harish {"EventId", eventId}, 99596330b99SSunitha Harish {"MemberId", memberId}, 9967c8c4058STejas Patil {"EventTimestamp", crow::utility::getDateTimeOffsetNow().first}, 99796330b99SSunitha Harish {"OriginOfCondition", origin}}; 99896330b99SSunitha Harish for (nlohmann::json::iterator it = event.begin(); it != event.end(); 99996330b99SSunitha Harish ++it) 100096330b99SSunitha Harish { 100196330b99SSunitha Harish eventMessage[it.key()] = it.value(); 100296330b99SSunitha Harish } 100396330b99SSunitha Harish eventRecord.push_back(eventMessage); 100496330b99SSunitha Harish 100596330b99SSunitha Harish for (const auto& it : this->subscriptionsMap) 100696330b99SSunitha Harish { 100796330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 100896330b99SSunitha Harish bool isSubscribed = false; 100996330b99SSunitha Harish // Search the resourceTypes list for the subscription. 101096330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 101196330b99SSunitha Harish // send everything. 101226f6976fSEd Tanous if (!entry->resourceTypes.empty()) 101396330b99SSunitha Harish { 10143174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 101596330b99SSunitha Harish { 101696330b99SSunitha Harish if (resType == resource) 101796330b99SSunitha Harish { 101896330b99SSunitha Harish BMCWEB_LOG_INFO << "ResourceType " << resource 101996330b99SSunitha Harish << " found in the subscribed list"; 102096330b99SSunitha Harish isSubscribed = true; 102196330b99SSunitha Harish break; 102296330b99SSunitha Harish } 102396330b99SSunitha Harish } 102496330b99SSunitha Harish } 102596330b99SSunitha Harish else // resourceTypes list is empty. 102696330b99SSunitha Harish { 102796330b99SSunitha Harish isSubscribed = true; 102896330b99SSunitha Harish } 102996330b99SSunitha Harish if (isSubscribed) 103096330b99SSunitha Harish { 103196330b99SSunitha Harish nlohmann::json msgJson = { 103296330b99SSunitha Harish {"@odata.type", "#Event.v1_4_0.Event"}, 103396330b99SSunitha Harish {"Name", "Event Log"}, 103496330b99SSunitha Harish {"Id", eventId}, 103596330b99SSunitha Harish {"Events", eventRecord}}; 1036*f52c03c1SCarson Labrado 1037*f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1038*f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 1039*f52c03c1SCarson Labrado entry->sendEvent(strMsg); 104096330b99SSunitha Harish eventId++; // increament the eventId 104196330b99SSunitha Harish } 104296330b99SSunitha Harish else 104396330b99SSunitha Harish { 104496330b99SSunitha Harish BMCWEB_LOG_INFO << "Not subscribed to this resource"; 104596330b99SSunitha Harish } 104696330b99SSunitha Harish } 104796330b99SSunitha Harish } 10485738de59SAsmitha Karunanithi void sendBroadcastMsg(const std::string& broadcastMsg) 10495738de59SAsmitha Karunanithi { 10505738de59SAsmitha Karunanithi for (const auto& it : this->subscriptionsMap) 10515738de59SAsmitha Karunanithi { 10525738de59SAsmitha Karunanithi std::shared_ptr<Subscription> entry = it.second; 10535738de59SAsmitha Karunanithi nlohmann::json msgJson = { 10547c8c4058STejas Patil {"Timestamp", crow::utility::getDateTimeOffsetNow().first}, 10555738de59SAsmitha Karunanithi {"OriginOfCondition", "/ibm/v1/HMC/BroadcastService"}, 10565738de59SAsmitha Karunanithi {"Name", "Broadcast Message"}, 10575738de59SAsmitha Karunanithi {"Message", broadcastMsg}}; 1058*f52c03c1SCarson Labrado 1059*f52c03c1SCarson Labrado std::string strMsg = msgJson.dump( 1060*f52c03c1SCarson Labrado 2, ' ', true, nlohmann::json::error_handler_t::replace); 1061*f52c03c1SCarson Labrado entry->sendEvent(strMsg); 10625738de59SAsmitha Karunanithi } 10635738de59SAsmitha Karunanithi } 106496330b99SSunitha Harish 10657f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 10662558979cSP Dheeraj Srujan Kumar 10672558979cSP Dheeraj Srujan Kumar void resetRedfishFilePosition() 10687f4eb588SAppaRao Puli { 10692558979cSP Dheeraj Srujan Kumar // Control would be here when Redfish file is created. 10702558979cSP Dheeraj Srujan Kumar // Reset File Position as new file is created 10712558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = 0; 10722558979cSP Dheeraj Srujan Kumar } 10732558979cSP Dheeraj Srujan Kumar 10742558979cSP Dheeraj Srujan Kumar void cacheRedfishLogFile() 10752558979cSP Dheeraj Srujan Kumar { 10762558979cSP Dheeraj Srujan Kumar // Open the redfish file and read till the last record. 10772558979cSP Dheeraj Srujan Kumar 10787f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 10797f4eb588SAppaRao Puli if (!logStream.good()) 10807f4eb588SAppaRao Puli { 10817f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 10827f4eb588SAppaRao Puli return; 10837f4eb588SAppaRao Puli } 10847f4eb588SAppaRao Puli std::string logEntry; 10857f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 10867f4eb588SAppaRao Puli { 10872558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 10887f4eb588SAppaRao Puli } 10892558979cSP Dheeraj Srujan Kumar 10902558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "Next Log Position : " << redfishLogFilePosition; 10917f4eb588SAppaRao Puli } 10927f4eb588SAppaRao Puli 10937f4eb588SAppaRao Puli void readEventLogsFromFile() 10947f4eb588SAppaRao Puli { 10957f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 10967f4eb588SAppaRao Puli if (!logStream.good()) 10977f4eb588SAppaRao Puli { 10987f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed"; 10997f4eb588SAppaRao Puli return; 11007f4eb588SAppaRao Puli } 11017f4eb588SAppaRao Puli 11027f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11037f4eb588SAppaRao Puli 11047f4eb588SAppaRao Puli std::string logEntry; 11052558979cSP Dheeraj Srujan Kumar 11062558979cSP Dheeraj Srujan Kumar // Get the read pointer to the next log to be read. 11072558979cSP Dheeraj Srujan Kumar logStream.seekg(redfishLogFilePosition); 11082558979cSP Dheeraj Srujan Kumar 11097f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11107f4eb588SAppaRao Puli { 11112558979cSP Dheeraj Srujan Kumar // Update Pointer position 11122558979cSP Dheeraj Srujan Kumar redfishLogFilePosition = logStream.tellg(); 11132558979cSP Dheeraj Srujan Kumar 11142558979cSP Dheeraj Srujan Kumar std::string idStr; 11152558979cSP Dheeraj Srujan Kumar if (!event_log::getUniqueEntryID(logEntry, idStr)) 11167f4eb588SAppaRao Puli { 11177f4eb588SAppaRao Puli continue; 11187f4eb588SAppaRao Puli } 11197f4eb588SAppaRao Puli 1120e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11217f4eb588SAppaRao Puli { 11222558979cSP Dheeraj Srujan Kumar // If Service is not enabled, no need to compute 11232558979cSP Dheeraj Srujan Kumar // the remaining items below. 11242558979cSP Dheeraj Srujan Kumar // But, Loop must continue to keep track of Timestamp 11257f4eb588SAppaRao Puli continue; 11267f4eb588SAppaRao Puli } 11277f4eb588SAppaRao Puli 11287f4eb588SAppaRao Puli std::string timestamp; 11297f4eb588SAppaRao Puli std::string messageID; 11305e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11317f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11327f4eb588SAppaRao Puli messageArgs) != 0) 11337f4eb588SAppaRao Puli { 11347f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 11357f4eb588SAppaRao Puli continue; 11367f4eb588SAppaRao Puli } 11377f4eb588SAppaRao Puli 11387f4eb588SAppaRao Puli std::string registryName; 11397f4eb588SAppaRao Puli std::string messageKey; 11407f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 11417f4eb588SAppaRao Puli messageKey); 11427f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 11437f4eb588SAppaRao Puli { 11447f4eb588SAppaRao Puli continue; 11457f4eb588SAppaRao Puli } 11467f4eb588SAppaRao Puli 11477f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 11487f4eb588SAppaRao Puli messageKey, messageArgs); 11497f4eb588SAppaRao Puli } 11507f4eb588SAppaRao Puli 1151e662eae8SEd Tanous if (!serviceEnabled || noOfEventLogSubscribers == 0) 11522558979cSP Dheeraj Srujan Kumar { 11532558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 11542558979cSP Dheeraj Srujan Kumar return; 11552558979cSP Dheeraj Srujan Kumar } 11562558979cSP Dheeraj Srujan Kumar 11572558979cSP Dheeraj Srujan Kumar if (eventRecords.empty()) 11582558979cSP Dheeraj Srujan Kumar { 11592558979cSP Dheeraj Srujan Kumar // No Records to send 11602558979cSP Dheeraj Srujan Kumar BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 11612558979cSP Dheeraj Srujan Kumar return; 11622558979cSP Dheeraj Srujan Kumar } 11632558979cSP Dheeraj Srujan Kumar 11647f4eb588SAppaRao Puli for (const auto& it : this->subscriptionsMap) 11657f4eb588SAppaRao Puli { 11667f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 11677f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 11687f4eb588SAppaRao Puli { 11697f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 11707f4eb588SAppaRao Puli } 11717f4eb588SAppaRao Puli } 11727f4eb588SAppaRao Puli } 11737f4eb588SAppaRao Puli 11747f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 11757f4eb588SAppaRao Puli { 11766a9f85f9SAppaRao Puli if (!inotifyConn) 11777f4eb588SAppaRao Puli { 11787f4eb588SAppaRao Puli return; 11797f4eb588SAppaRao Puli } 11807f4eb588SAppaRao Puli 11817f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 11827f4eb588SAppaRao Puli 11837f4eb588SAppaRao Puli inotifyConn->async_read_some( 11847f4eb588SAppaRao Puli boost::asio::buffer(readBuffer), 11857f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 11867f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 11877f4eb588SAppaRao Puli if (ec) 11887f4eb588SAppaRao Puli { 11897f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 11907f4eb588SAppaRao Puli return; 11917f4eb588SAppaRao Puli } 11927f4eb588SAppaRao Puli std::size_t index = 0; 1193b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 11947f4eb588SAppaRao Puli { 1195d3a9e084SEd Tanous struct inotify_event event 1196d3a9e084SEd Tanous {}; 1197b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1198b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1199b792cc56SAppaRao Puli { 1200b792cc56SAppaRao Puli if ((event.len == 0) || 1201b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1202b792cc56SAppaRao Puli { 1203b792cc56SAppaRao Puli index += (iEventSize + event.len); 1204b792cc56SAppaRao Puli continue; 1205b792cc56SAppaRao Puli } 1206b792cc56SAppaRao Puli 12074f568f74SJiaqing Zhao std::string fileName(&readBuffer[index + iEventSize]); 12084f568f74SJiaqing Zhao if (fileName != "redfish") 1209b792cc56SAppaRao Puli { 1210b792cc56SAppaRao Puli index += (iEventSize + event.len); 1211b792cc56SAppaRao Puli continue; 1212b792cc56SAppaRao Puli } 1213b792cc56SAppaRao Puli 1214b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1215b792cc56SAppaRao Puli << "Redfish log file created/deleted. event.name: " 1216b792cc56SAppaRao Puli << fileName; 1217b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1218b792cc56SAppaRao Puli { 1219b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1220b792cc56SAppaRao Puli { 1221b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1222016761afSAppaRao Puli << "Remove and Add inotify watcher on " 1223016761afSAppaRao Puli "redfish event log file"; 1224016761afSAppaRao Puli // Remove existing inotify watcher and add 1225016761afSAppaRao Puli // with new redfish event log file. 1226016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1227016761afSAppaRao Puli fileWatchDesc = -1; 1228b792cc56SAppaRao Puli } 1229b792cc56SAppaRao Puli 1230b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1231b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1232b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1233b792cc56SAppaRao Puli { 1234b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1235b792cc56SAppaRao Puli << "inotify_add_watch failed for " 1236b792cc56SAppaRao Puli "redfish log file."; 1237b792cc56SAppaRao Puli return; 1238b792cc56SAppaRao Puli } 1239b792cc56SAppaRao Puli 1240b792cc56SAppaRao Puli EventServiceManager::getInstance() 12412558979cSP Dheeraj Srujan Kumar .resetRedfishFilePosition(); 1242b792cc56SAppaRao Puli EventServiceManager::getInstance() 1243b792cc56SAppaRao Puli .readEventLogsFromFile(); 1244b792cc56SAppaRao Puli } 1245b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1246b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1247b792cc56SAppaRao Puli { 1248b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1249b792cc56SAppaRao Puli { 1250b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1251b792cc56SAppaRao Puli fileWatchDesc = -1; 1252b792cc56SAppaRao Puli } 1253b792cc56SAppaRao Puli } 1254b792cc56SAppaRao Puli } 1255b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1256b792cc56SAppaRao Puli { 1257b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12587f4eb588SAppaRao Puli { 12597f4eb588SAppaRao Puli EventServiceManager::getInstance() 12607f4eb588SAppaRao Puli .readEventLogsFromFile(); 12617f4eb588SAppaRao Puli } 1262b792cc56SAppaRao Puli } 1263b792cc56SAppaRao Puli index += (iEventSize + event.len); 12647f4eb588SAppaRao Puli } 12657f4eb588SAppaRao Puli 12667f4eb588SAppaRao Puli watchRedfishEventLogFile(); 12677f4eb588SAppaRao Puli }); 12687f4eb588SAppaRao Puli } 12697f4eb588SAppaRao Puli 12707f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 12717f4eb588SAppaRao Puli { 127223a21a1cSEd Tanous inotifyConn.emplace(ioc); 1273b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1274b792cc56SAppaRao Puli if (inotifyFd == -1) 12757f4eb588SAppaRao Puli { 12767f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "inotify_init1 failed."; 12777f4eb588SAppaRao Puli return -1; 12787f4eb588SAppaRao Puli } 1279b792cc56SAppaRao Puli 1280b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1281b792cc56SAppaRao Puli // create/delete. 1282b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1283b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1284b792cc56SAppaRao Puli if (dirWatchDesc == -1) 12857f4eb588SAppaRao Puli { 12867f4eb588SAppaRao Puli BMCWEB_LOG_ERROR 1287b792cc56SAppaRao Puli << "inotify_add_watch failed for event log directory."; 12887f4eb588SAppaRao Puli return -1; 12897f4eb588SAppaRao Puli } 12907f4eb588SAppaRao Puli 1291b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 1292b792cc56SAppaRao Puli fileWatchDesc = 1293b792cc56SAppaRao Puli inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY); 1294b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1295b792cc56SAppaRao Puli { 1296b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1297b792cc56SAppaRao Puli << "inotify_add_watch failed for redfish log file."; 1298b792cc56SAppaRao Puli // Don't return error if file not exist. 1299b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1300b792cc56SAppaRao Puli } 1301b792cc56SAppaRao Puli 13027f4eb588SAppaRao Puli // monitor redfish event log file 1303b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13047f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13057f4eb588SAppaRao Puli 13067f4eb588SAppaRao Puli return 0; 13077f4eb588SAppaRao Puli } 13087f4eb588SAppaRao Puli 13097f4eb588SAppaRao Puli #endif 131056d2396dSEd Tanous static void getReadingsForReport(sdbusplus::message::message& msg) 1311156d6b00SAppaRao Puli { 131256d2396dSEd Tanous if (msg.is_method_error()) 131356d2396dSEd Tanous { 131456d2396dSEd Tanous BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 131556d2396dSEd Tanous return; 131656d2396dSEd Tanous } 131756d2396dSEd Tanous 1318c0353249SWludzik, Jozef sdbusplus::message::object_path path(msg.get_path()); 1319c0353249SWludzik, Jozef std::string id = path.filename(); 1320c0353249SWludzik, Jozef if (id.empty()) 1321156d6b00SAppaRao Puli { 1322c0353249SWludzik, Jozef BMCWEB_LOG_ERROR << "Failed to get Id from path"; 1323156d6b00SAppaRao Puli return; 1324156d6b00SAppaRao Puli } 1325156d6b00SAppaRao Puli 1326c0353249SWludzik, Jozef std::string interface; 1327b9d36b47SEd Tanous dbus::utility::DBusPropertiesMap props; 1328c0353249SWludzik, Jozef std::vector<std::string> invalidProps; 1329c0353249SWludzik, Jozef msg.read(interface, props, invalidProps); 1330c0353249SWludzik, Jozef 1331c0353249SWludzik, Jozef auto found = 1332c0353249SWludzik, Jozef std::find_if(props.begin(), props.end(), 1333c0353249SWludzik, Jozef [](const auto& x) { return x.first == "Readings"; }); 1334c0353249SWludzik, Jozef if (found == props.end()) 1335156d6b00SAppaRao Puli { 1336c0353249SWludzik, Jozef BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 1337156d6b00SAppaRao Puli return; 1338156d6b00SAppaRao Puli } 1339156d6b00SAppaRao Puli 13401e1e598dSJonathan Doman const telemetry::TimestampReadings* readings = 13411e1e598dSJonathan Doman std::get_if<telemetry::TimestampReadings>(&found->second); 1342e662eae8SEd Tanous if (readings == nullptr) 13431e1e598dSJonathan Doman { 13441e1e598dSJonathan Doman BMCWEB_LOG_INFO << "Failed to get Readings from Report properties"; 13451e1e598dSJonathan Doman return; 13461e1e598dSJonathan Doman } 13471e1e598dSJonathan Doman 1348156d6b00SAppaRao Puli for (const auto& it : 1349156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1350156d6b00SAppaRao Puli { 1351e05aec50SEd Tanous Subscription& entry = *it.second; 1352c0353249SWludzik, Jozef if (entry.eventFormatType == metricReportFormatType) 1353156d6b00SAppaRao Puli { 13541e1e598dSJonathan Doman entry.filterAndSendReports(id, *readings); 1355156d6b00SAppaRao Puli } 1356156d6b00SAppaRao Puli } 1357156d6b00SAppaRao Puli } 1358156d6b00SAppaRao Puli 1359156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1360156d6b00SAppaRao Puli { 13617d1cc387SAppaRao Puli if (matchTelemetryMonitor) 13627d1cc387SAppaRao Puli { 1363156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1364156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1365156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1366156d6b00SAppaRao Puli } 13677d1cc387SAppaRao Puli } 1368156d6b00SAppaRao Puli 1369156d6b00SAppaRao Puli void registerMetricReportSignal() 1370156d6b00SAppaRao Puli { 13717d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1372156d6b00SAppaRao Puli { 13737d1cc387SAppaRao Puli BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1374156d6b00SAppaRao Puli return; 1375156d6b00SAppaRao Puli } 1376156d6b00SAppaRao Puli 1377156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1378c0353249SWludzik, Jozef std::string matchStr = "type='signal',member='PropertiesChanged'," 1379c0353249SWludzik, Jozef "interface='org.freedesktop.DBus.Properties'," 1380c0353249SWludzik, Jozef "arg0=xyz.openbmc_project.Telemetry.Report"; 1381156d6b00SAppaRao Puli 1382156d6b00SAppaRao Puli matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>( 138356d2396dSEd Tanous *crow::connections::systemBus, matchStr, getReadingsForReport); 1384156d6b00SAppaRao Puli } 138523a21a1cSEd Tanous }; 1386b52664e2SAppaRao Puli 1387b52664e2SAppaRao Puli } // namespace redfish 1388