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 17b52664e2SAppaRao Puli #include "node.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> 271214b7e7SGunnar Mills #include <error_messages.hpp> 281214b7e7SGunnar Mills #include <http_client.hpp> 29fc76b8acSEd Tanous #include <random.hpp> 304bbf237fSAppaRao Puli #include <server_sent_events.hpp> 311214b7e7SGunnar Mills #include <utils/json_utils.hpp> 321214b7e7SGunnar Mills 33b52664e2SAppaRao Puli #include <cstdlib> 34b52664e2SAppaRao Puli #include <ctime> 351bf712bcSAyushi Smriti #include <fstream> 36b52664e2SAppaRao Puli #include <memory> 37b52664e2SAppaRao Puli #include <variant> 38b52664e2SAppaRao Puli 39b52664e2SAppaRao Puli namespace redfish 40b52664e2SAppaRao Puli { 41156d6b00SAppaRao Puli 42156d6b00SAppaRao Puli using ReadingsObjType = 4393f5d7c7SWludzik, Jozef std::vector<std::tuple<std::string, std::string, double, int32_t>>; 447d1cc387SAppaRao Puli using EventServiceConfig = std::tuple<bool, uint32_t, uint32_t>; 45156d6b00SAppaRao Puli 46156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 47156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 48156d6b00SAppaRao Puli 491bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 501bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 511bf712bcSAyushi Smriti 527f4eb588SAppaRao Puli namespace message_registries 537f4eb588SAppaRao Puli { 54b304bd79SP Dheeraj Srujan Kumar inline boost::beast::span<const MessageEntry> 55b304bd79SP Dheeraj Srujan Kumar getRegistryFromPrefix(const std::string& registryName) 56b304bd79SP Dheeraj Srujan Kumar { 57b304bd79SP Dheeraj Srujan Kumar if (task_event::header.registryPrefix == registryName) 58b304bd79SP Dheeraj Srujan Kumar { 59b304bd79SP Dheeraj Srujan Kumar return boost::beast::span<const MessageEntry>(task_event::registry); 60b304bd79SP Dheeraj Srujan Kumar } 61b304bd79SP Dheeraj Srujan Kumar if (openbmc::header.registryPrefix == registryName) 62b304bd79SP Dheeraj Srujan Kumar { 63b304bd79SP Dheeraj Srujan Kumar return boost::beast::span<const MessageEntry>(openbmc::registry); 64b304bd79SP Dheeraj Srujan Kumar } 65b304bd79SP Dheeraj Srujan Kumar if (base::header.registryPrefix == registryName) 66b304bd79SP Dheeraj Srujan Kumar { 67b304bd79SP Dheeraj Srujan Kumar return boost::beast::span<const MessageEntry>(base::registry); 68b304bd79SP Dheeraj Srujan Kumar } 69b304bd79SP Dheeraj Srujan Kumar return boost::beast::span<const MessageEntry>(openbmc::registry); 70b304bd79SP Dheeraj Srujan Kumar } 71*4642bf8fSGeorge Liu } // namespace message_registries 72b304bd79SP Dheeraj Srujan Kumar 73*4642bf8fSGeorge Liu #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 74*4642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 75*4642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 76*4642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 77*4642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 78*4642bf8fSGeorge Liu static int inotifyFd = -1; 79*4642bf8fSGeorge Liu static int dirWatchDesc = -1; 80*4642bf8fSGeorge Liu static int fileWatchDesc = -1; 81*4642bf8fSGeorge Liu 82*4642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 83*4642bf8fSGeorge Liu using EventLogObjectsType = 84*4642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 85*4642bf8fSGeorge Liu std::vector<std::string>>; 86*4642bf8fSGeorge Liu 87*4642bf8fSGeorge Liu namespace message_registries 88*4642bf8fSGeorge Liu { 897f4eb588SAppaRao Puli static const Message* 907f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 917f4eb588SAppaRao Puli const boost::beast::span<const MessageEntry>& registry) 927f4eb588SAppaRao Puli { 937f4eb588SAppaRao Puli boost::beast::span<const MessageEntry>::const_iterator messageIt = 947f4eb588SAppaRao Puli std::find_if(registry.cbegin(), registry.cend(), 957f4eb588SAppaRao Puli [&messageKey](const MessageEntry& messageEntry) { 967f4eb588SAppaRao Puli return !messageKey.compare(messageEntry.first); 977f4eb588SAppaRao Puli }); 987f4eb588SAppaRao Puli if (messageIt != registry.cend()) 997f4eb588SAppaRao Puli { 1007f4eb588SAppaRao Puli return &messageIt->second; 1017f4eb588SAppaRao Puli } 1027f4eb588SAppaRao Puli 1037f4eb588SAppaRao Puli return nullptr; 1047f4eb588SAppaRao Puli } 1057f4eb588SAppaRao Puli 1067f4eb588SAppaRao Puli static const Message* formatMessage(const std::string_view& messageID) 1077f4eb588SAppaRao Puli { 1087f4eb588SAppaRao Puli // Redfish MessageIds are in the form 1097f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 1107f4eb588SAppaRao Puli // the right Message 1117f4eb588SAppaRao Puli std::vector<std::string> fields; 1127f4eb588SAppaRao Puli fields.reserve(4); 1137f4eb588SAppaRao Puli boost::split(fields, messageID, boost::is_any_of(".")); 1147f4eb588SAppaRao Puli if (fields.size() != 4) 1157f4eb588SAppaRao Puli { 1167f4eb588SAppaRao Puli return nullptr; 1177f4eb588SAppaRao Puli } 1187f4eb588SAppaRao Puli std::string& registryName = fields[0]; 1197f4eb588SAppaRao Puli std::string& messageKey = fields[3]; 1207f4eb588SAppaRao Puli 1217f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 122b304bd79SP Dheeraj Srujan Kumar return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName)); 1237f4eb588SAppaRao Puli } 1247f4eb588SAppaRao Puli } // namespace message_registries 1257f4eb588SAppaRao Puli 1267f4eb588SAppaRao Puli namespace event_log 1277f4eb588SAppaRao Puli { 12823a21a1cSEd Tanous inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID, 1297f4eb588SAppaRao Puli const bool firstEntry = true) 1307f4eb588SAppaRao Puli { 1317f4eb588SAppaRao Puli static time_t prevTs = 0; 1327f4eb588SAppaRao Puli static int index = 0; 1337f4eb588SAppaRao Puli if (firstEntry) 1347f4eb588SAppaRao Puli { 1357f4eb588SAppaRao Puli prevTs = 0; 1367f4eb588SAppaRao Puli } 1377f4eb588SAppaRao Puli 1387f4eb588SAppaRao Puli // Get the entry timestamp 1397f4eb588SAppaRao Puli std::time_t curTs = 0; 1407f4eb588SAppaRao Puli std::tm timeStruct = {}; 1417f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1427f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1437f4eb588SAppaRao Puli { 1447f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1457f4eb588SAppaRao Puli if (curTs == -1) 1467f4eb588SAppaRao Puli { 1477f4eb588SAppaRao Puli return false; 1487f4eb588SAppaRao Puli } 1497f4eb588SAppaRao Puli } 1507f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1517f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1527f4eb588SAppaRao Puli 1537f4eb588SAppaRao Puli // Save the timestamp 1547f4eb588SAppaRao Puli prevTs = curTs; 1557f4eb588SAppaRao Puli 1567f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1577f4eb588SAppaRao Puli if (index > 0) 1587f4eb588SAppaRao Puli { 1597f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1607f4eb588SAppaRao Puli } 1617f4eb588SAppaRao Puli return true; 1627f4eb588SAppaRao Puli } 1637f4eb588SAppaRao Puli 16423a21a1cSEd Tanous inline int getEventLogParams(const std::string& logEntry, 16523a21a1cSEd Tanous std::string& timestamp, std::string& messageID, 1665e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1677f4eb588SAppaRao Puli { 1687f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1697f4eb588SAppaRao Puli // First get the Timestamp 170f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1717f4eb588SAppaRao Puli if (space == std::string::npos) 1727f4eb588SAppaRao Puli { 1737f4eb588SAppaRao Puli return -EINVAL; 1747f4eb588SAppaRao Puli } 1757f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1767f4eb588SAppaRao Puli // Then get the log contents 177f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1787f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1797f4eb588SAppaRao Puli { 1807f4eb588SAppaRao Puli return -EINVAL; 1817f4eb588SAppaRao Puli } 1827f4eb588SAppaRao Puli std::string_view entry(logEntry); 1837f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1847f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1857f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 1867f4eb588SAppaRao Puli boost::split(logEntryFields, entry, boost::is_any_of(","), 1877f4eb588SAppaRao Puli boost::token_compress_on); 1887f4eb588SAppaRao Puli // We need at least a MessageId to be valid 1897f4eb588SAppaRao Puli if (logEntryFields.size() < 1) 1907f4eb588SAppaRao Puli { 1917f4eb588SAppaRao Puli return -EINVAL; 1927f4eb588SAppaRao Puli } 1937f4eb588SAppaRao Puli messageID = logEntryFields[0]; 1947f4eb588SAppaRao Puli 1957f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 1967f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 1977f4eb588SAppaRao Puli { 1987f4eb588SAppaRao Puli std::string& messageArgsStart = logEntryFields[1]; 1997f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 2007f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 2017f4eb588SAppaRao Puli { 2025e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 2035e715de6SAppaRao Puli logEntryFields.end()); 2047f4eb588SAppaRao Puli } 2057f4eb588SAppaRao Puli } 2067f4eb588SAppaRao Puli 2077f4eb588SAppaRao Puli return 0; 2087f4eb588SAppaRao Puli } 2097f4eb588SAppaRao Puli 21023a21a1cSEd Tanous inline void getRegistryAndMessageKey(const std::string& messageID, 2117f4eb588SAppaRao Puli std::string& registryName, 2127f4eb588SAppaRao Puli std::string& messageKey) 2137f4eb588SAppaRao Puli { 2147f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2157f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2167f4eb588SAppaRao Puli // the right Message 2177f4eb588SAppaRao Puli std::vector<std::string> fields; 2187f4eb588SAppaRao Puli fields.reserve(4); 2197f4eb588SAppaRao Puli boost::split(fields, messageID, boost::is_any_of(".")); 2207f4eb588SAppaRao Puli if (fields.size() == 4) 2217f4eb588SAppaRao Puli { 2227f4eb588SAppaRao Puli registryName = fields[0]; 2237f4eb588SAppaRao Puli messageKey = fields[3]; 2247f4eb588SAppaRao Puli } 2257f4eb588SAppaRao Puli } 2267f4eb588SAppaRao Puli 22723a21a1cSEd Tanous inline int formatEventLogEntry(const std::string& logEntryID, 2287f4eb588SAppaRao Puli const std::string& messageID, 2295e715de6SAppaRao Puli const std::vector<std::string>& messageArgs, 23023a21a1cSEd Tanous std::string timestamp, 231b5a76932SEd Tanous const std::string& customText, 2327f4eb588SAppaRao Puli nlohmann::json& logEntryJson) 2337f4eb588SAppaRao Puli { 2347f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 2357f4eb588SAppaRao Puli const message_registries::Message* message = 2367f4eb588SAppaRao Puli message_registries::formatMessage(messageID); 2377f4eb588SAppaRao Puli 2387f4eb588SAppaRao Puli std::string msg; 2397f4eb588SAppaRao Puli std::string severity; 2407f4eb588SAppaRao Puli if (message != nullptr) 2417f4eb588SAppaRao Puli { 2427f4eb588SAppaRao Puli msg = message->message; 2437f4eb588SAppaRao Puli severity = message->severity; 2447f4eb588SAppaRao Puli } 2457f4eb588SAppaRao Puli 2467f4eb588SAppaRao Puli // Fill the MessageArgs into the Message 2477f4eb588SAppaRao Puli int i = 0; 2487f4eb588SAppaRao Puli for (const std::string& messageArg : messageArgs) 2497f4eb588SAppaRao Puli { 2507f4eb588SAppaRao Puli std::string argStr = "%" + std::to_string(++i); 2517f4eb588SAppaRao Puli size_t argPos = msg.find(argStr); 2527f4eb588SAppaRao Puli if (argPos != std::string::npos) 2537f4eb588SAppaRao Puli { 2547f4eb588SAppaRao Puli msg.replace(argPos, argStr.length(), messageArg); 2557f4eb588SAppaRao Puli } 2567f4eb588SAppaRao Puli } 2577f4eb588SAppaRao Puli 2587f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2597f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2607f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 261f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 262f23b7296SEd Tanous std::size_t plus = timestamp.find_first_of('+'); 2637f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2647f4eb588SAppaRao Puli { 2657f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2667f4eb588SAppaRao Puli } 2677f4eb588SAppaRao Puli 2687f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2697f4eb588SAppaRao Puli logEntryJson = {{"EventId", logEntryID}, 2707f4eb588SAppaRao Puli {"EventType", "Event"}, 2717f4eb588SAppaRao Puli {"Severity", std::move(severity)}, 2727f4eb588SAppaRao Puli {"Message", std::move(msg)}, 273f23b7296SEd Tanous {"MessageId", messageID}, 274f23b7296SEd Tanous {"MessageArgs", messageArgs}, 2757f4eb588SAppaRao Puli {"EventTimestamp", std::move(timestamp)}, 2767f4eb588SAppaRao Puli {"Context", customText}}; 2777f4eb588SAppaRao Puli return 0; 2787f4eb588SAppaRao Puli } 2797f4eb588SAppaRao Puli 2807f4eb588SAppaRao Puli } // namespace event_log 2817f4eb588SAppaRao Puli #endif 2827f4eb588SAppaRao Puli 28323a21a1cSEd Tanous inline bool isFilterQuerySpecialChar(char c) 28407941a88SAyushi Smriti { 28507941a88SAyushi Smriti switch (c) 28607941a88SAyushi Smriti { 28707941a88SAyushi Smriti case '(': 28807941a88SAyushi Smriti case ')': 28907941a88SAyushi Smriti case '\'': 29007941a88SAyushi Smriti return true; 29107941a88SAyushi Smriti default: 29207941a88SAyushi Smriti return false; 29307941a88SAyushi Smriti } 29407941a88SAyushi Smriti } 29507941a88SAyushi Smriti 29623a21a1cSEd Tanous inline bool 29723a21a1cSEd Tanous readSSEQueryParams(std::string sseFilter, std::string& formatType, 29807941a88SAyushi Smriti std::vector<std::string>& messageIds, 29907941a88SAyushi Smriti std::vector<std::string>& registryPrefixes, 300144b6318SAppaRao Puli std::vector<std::string>& metricReportDefinitions) 30107941a88SAyushi Smriti { 30207941a88SAyushi Smriti sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(), 30307941a88SAyushi Smriti isFilterQuerySpecialChar), 30407941a88SAyushi Smriti sseFilter.end()); 30507941a88SAyushi Smriti 30607941a88SAyushi Smriti std::vector<std::string> result; 30707941a88SAyushi Smriti boost::split(result, sseFilter, boost::is_any_of(" "), 30807941a88SAyushi Smriti boost::token_compress_on); 30907941a88SAyushi Smriti 31007941a88SAyushi Smriti BMCWEB_LOG_DEBUG << "No of tokens in SEE query: " << result.size(); 31107941a88SAyushi Smriti 31207941a88SAyushi Smriti constexpr uint8_t divisor = 4; 31307941a88SAyushi Smriti constexpr uint8_t minTokenSize = 3; 31407941a88SAyushi Smriti if (result.size() % divisor != minTokenSize) 31507941a88SAyushi Smriti { 31607941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid SSE filter specified."; 31707941a88SAyushi Smriti return false; 31807941a88SAyushi Smriti } 31907941a88SAyushi Smriti 32007941a88SAyushi Smriti for (std::size_t i = 0; i < result.size(); i += divisor) 32107941a88SAyushi Smriti { 32207941a88SAyushi Smriti std::string& key = result[i]; 32307941a88SAyushi Smriti std::string& op = result[i + 1]; 32407941a88SAyushi Smriti std::string& value = result[i + 2]; 32507941a88SAyushi Smriti 32607941a88SAyushi Smriti if ((i + minTokenSize) < result.size()) 32707941a88SAyushi Smriti { 3284e0453b1SGunnar Mills std::string& separator = result[i + minTokenSize]; 32907941a88SAyushi Smriti // SSE supports only "or" and "and" in query params. 3304e0453b1SGunnar Mills if ((separator != "or") && (separator != "and")) 33107941a88SAyushi Smriti { 33207941a88SAyushi Smriti BMCWEB_LOG_ERROR 33307941a88SAyushi Smriti << "Invalid group operator in SSE query parameters"; 33407941a88SAyushi Smriti return false; 33507941a88SAyushi Smriti } 33607941a88SAyushi Smriti } 33707941a88SAyushi Smriti 33807941a88SAyushi Smriti // SSE supports only "eq" as per spec. 33907941a88SAyushi Smriti if (op != "eq") 34007941a88SAyushi Smriti { 34107941a88SAyushi Smriti BMCWEB_LOG_ERROR 34207941a88SAyushi Smriti << "Invalid assignment operator in SSE query parameters"; 34307941a88SAyushi Smriti return false; 34407941a88SAyushi Smriti } 34507941a88SAyushi Smriti 34607941a88SAyushi Smriti BMCWEB_LOG_DEBUG << key << " : " << value; 34707941a88SAyushi Smriti if (key == "EventFormatType") 34807941a88SAyushi Smriti { 34907941a88SAyushi Smriti formatType = value; 35007941a88SAyushi Smriti } 35107941a88SAyushi Smriti else if (key == "MessageId") 35207941a88SAyushi Smriti { 35307941a88SAyushi Smriti messageIds.push_back(value); 35407941a88SAyushi Smriti } 35507941a88SAyushi Smriti else if (key == "RegistryPrefix") 35607941a88SAyushi Smriti { 35707941a88SAyushi Smriti registryPrefixes.push_back(value); 35807941a88SAyushi Smriti } 35907941a88SAyushi Smriti else if (key == "MetricReportDefinition") 36007941a88SAyushi Smriti { 36107941a88SAyushi Smriti metricReportDefinitions.push_back(value); 36207941a88SAyushi Smriti } 36307941a88SAyushi Smriti else 36407941a88SAyushi Smriti { 36507941a88SAyushi Smriti BMCWEB_LOG_ERROR << "Invalid property(" << key 36607941a88SAyushi Smriti << ")in SSE filter query."; 36707941a88SAyushi Smriti return false; 36807941a88SAyushi Smriti } 36907941a88SAyushi Smriti } 37007941a88SAyushi Smriti return true; 37107941a88SAyushi Smriti } 37207941a88SAyushi Smriti 373b52664e2SAppaRao Puli class Subscription 374b52664e2SAppaRao Puli { 375b52664e2SAppaRao Puli public: 376b52664e2SAppaRao Puli std::string id; 377b52664e2SAppaRao Puli std::string destinationUrl; 378b52664e2SAppaRao Puli std::string protocol; 379b52664e2SAppaRao Puli std::string retryPolicy; 380b52664e2SAppaRao Puli std::string customText; 381b52664e2SAppaRao Puli std::string eventFormatType; 382b52664e2SAppaRao Puli std::string subscriptionType; 383b52664e2SAppaRao Puli std::vector<std::string> registryMsgIds; 384b52664e2SAppaRao Puli std::vector<std::string> registryPrefixes; 385e56f254cSSunitha Harish std::vector<std::string> resourceTypes; 386b52664e2SAppaRao Puli std::vector<nlohmann::json> httpHeaders; // key-value pair 387144b6318SAppaRao Puli std::vector<std::string> metricReportDefinitions; 388b52664e2SAppaRao Puli 389b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 390b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 391b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 392b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 393b52664e2SAppaRao Puli 394b52664e2SAppaRao Puli Subscription(const std::string& inHost, const std::string& inPort, 395b52664e2SAppaRao Puli const std::string& inPath, const std::string& inUriProto) : 3960b4bdd93SAppaRao Puli eventSeqNum(1), 3970b4bdd93SAppaRao Puli host(inHost), port(inPort), path(inPath), uriProto(inUriProto) 398b52664e2SAppaRao Puli { 399b52664e2SAppaRao Puli conn = std::make_shared<crow::HttpClient>( 400fe44eb0bSAyushi Smriti crow::connections::systemBus->get_io_context(), id, host, port, 401fe44eb0bSAyushi Smriti path); 402b52664e2SAppaRao Puli } 4034bbf237fSAppaRao Puli 40423e64207SEd Tanous Subscription(const std::shared_ptr<boost::beast::tcp_stream>& adaptor) : 4054bbf237fSAppaRao Puli eventSeqNum(1) 4064bbf237fSAppaRao Puli { 4074bbf237fSAppaRao Puli sseConn = std::make_shared<crow::ServerSentEvents>(adaptor); 4084bbf237fSAppaRao Puli } 4094bbf237fSAppaRao Puli 4109f616dd1SEd Tanous ~Subscription() = default; 411b52664e2SAppaRao Puli 412b52664e2SAppaRao Puli void sendEvent(const std::string& msg) 413b52664e2SAppaRao Puli { 4144bbf237fSAppaRao Puli if (conn != nullptr) 4154bbf237fSAppaRao Puli { 416b52664e2SAppaRao Puli std::vector<std::pair<std::string, std::string>> reqHeaders; 417b52664e2SAppaRao Puli for (const auto& header : httpHeaders) 418b52664e2SAppaRao Puli { 419b52664e2SAppaRao Puli for (const auto& item : header.items()) 420b52664e2SAppaRao Puli { 421b52664e2SAppaRao Puli std::string key = item.key(); 422b52664e2SAppaRao Puli std::string val = item.value(); 423b52664e2SAppaRao Puli reqHeaders.emplace_back(std::pair(key, val)); 424b52664e2SAppaRao Puli } 425b52664e2SAppaRao Puli } 426b52664e2SAppaRao Puli conn->setHeaders(reqHeaders); 4272a5689a7SAppaRao Puli conn->sendData(msg); 42896330b99SSunitha Harish this->eventSeqNum++; 429b52664e2SAppaRao Puli } 430b52664e2SAppaRao Puli 4314bbf237fSAppaRao Puli if (sseConn != nullptr) 4324bbf237fSAppaRao Puli { 4334bbf237fSAppaRao Puli sseConn->sendData(eventSeqNum, msg); 4344bbf237fSAppaRao Puli } 4354bbf237fSAppaRao Puli } 4364bbf237fSAppaRao Puli 4370b4bdd93SAppaRao Puli void sendTestEventLog() 4380b4bdd93SAppaRao Puli { 4390b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 4400b4bdd93SAppaRao Puli logEntryArray.push_back({}); 4410b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 4420b4bdd93SAppaRao Puli 4430b4bdd93SAppaRao Puli logEntryJson = {{"EventId", "TestID"}, 4440b4bdd93SAppaRao Puli {"EventType", "Event"}, 4450b4bdd93SAppaRao Puli {"Severity", "OK"}, 4460b4bdd93SAppaRao Puli {"Message", "Generated test event"}, 4470b4bdd93SAppaRao Puli {"MessageId", "OpenBMC.0.1.TestEventLog"}, 4480b4bdd93SAppaRao Puli {"MessageArgs", nlohmann::json::array()}, 4490b4bdd93SAppaRao Puli {"EventTimestamp", crow::utility::dateTimeNow()}, 4500b4bdd93SAppaRao Puli {"Context", customText}}; 4510b4bdd93SAppaRao Puli 4520b4bdd93SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 4530b4bdd93SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 4540b4bdd93SAppaRao Puli {"Name", "Event Log"}, 4550b4bdd93SAppaRao Puli {"Events", logEntryArray}}; 4560b4bdd93SAppaRao Puli 45771f52d96SEd Tanous this->sendEvent( 45871f52d96SEd Tanous msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 4590b4bdd93SAppaRao Puli } 4600b4bdd93SAppaRao Puli 4617f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 4627f4eb588SAppaRao Puli void filterAndSendEventLogs( 4637f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4647f4eb588SAppaRao Puli { 4657f4eb588SAppaRao Puli nlohmann::json logEntryArray; 4667f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4677f4eb588SAppaRao Puli { 4687f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 4697f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 4707f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 4717f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 4727f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 4735e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 4747f4eb588SAppaRao Puli 4757f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 4767f4eb588SAppaRao Puli // send everything. 4777f4eb588SAppaRao Puli if (registryPrefixes.size()) 4787f4eb588SAppaRao Puli { 4797f4eb588SAppaRao Puli auto obj = std::find(registryPrefixes.begin(), 4807f4eb588SAppaRao Puli registryPrefixes.end(), registryName); 4817f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 4827f4eb588SAppaRao Puli { 4837f4eb588SAppaRao Puli continue; 4847f4eb588SAppaRao Puli } 4857f4eb588SAppaRao Puli } 4867f4eb588SAppaRao Puli 4877f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 4887f4eb588SAppaRao Puli // send everything. 4897f4eb588SAppaRao Puli if (registryMsgIds.size()) 4907f4eb588SAppaRao Puli { 4917f4eb588SAppaRao Puli auto obj = std::find(registryMsgIds.begin(), 4927f4eb588SAppaRao Puli registryMsgIds.end(), messageKey); 4937f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 4947f4eb588SAppaRao Puli { 4957f4eb588SAppaRao Puli continue; 4967f4eb588SAppaRao Puli } 4977f4eb588SAppaRao Puli } 4987f4eb588SAppaRao Puli 4997f4eb588SAppaRao Puli logEntryArray.push_back({}); 5007f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 5017f4eb588SAppaRao Puli if (event_log::formatEventLogEntry(idStr, messageID, messageArgs, 5027f4eb588SAppaRao Puli timestamp, customText, 5037f4eb588SAppaRao Puli bmcLogEntry) != 0) 5047f4eb588SAppaRao Puli { 5057f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 5067f4eb588SAppaRao Puli continue; 5077f4eb588SAppaRao Puli } 5087f4eb588SAppaRao Puli } 5097f4eb588SAppaRao Puli 5107f4eb588SAppaRao Puli if (logEntryArray.size() < 1) 5117f4eb588SAppaRao Puli { 5127f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 5137f4eb588SAppaRao Puli return; 5147f4eb588SAppaRao Puli } 5157f4eb588SAppaRao Puli 5167f4eb588SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 5177f4eb588SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 5187f4eb588SAppaRao Puli {"Name", "Event Log"}, 5197f4eb588SAppaRao Puli {"Events", logEntryArray}}; 5207f4eb588SAppaRao Puli 52171f52d96SEd Tanous this->sendEvent( 52271f52d96SEd Tanous msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 5237f4eb588SAppaRao Puli } 5247f4eb588SAppaRao Puli #endif 5257f4eb588SAppaRao Puli 52623a21a1cSEd Tanous void filterAndSendReports(const std::string& id2, 527156d6b00SAppaRao Puli const std::string& readingsTs, 528156d6b00SAppaRao Puli const ReadingsObjType& readings) 529156d6b00SAppaRao Puli { 530156d6b00SAppaRao Puli std::string metricReportDef = 53123a21a1cSEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/" + id2; 532156d6b00SAppaRao Puli 533156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 534156d6b00SAppaRao Puli if (metricReportDefinitions.size()) 535156d6b00SAppaRao Puli { 536156d6b00SAppaRao Puli if (std::find(metricReportDefinitions.begin(), 537156d6b00SAppaRao Puli metricReportDefinitions.end(), 538156d6b00SAppaRao Puli metricReportDef) == metricReportDefinitions.end()) 539156d6b00SAppaRao Puli { 540156d6b00SAppaRao Puli return; 541156d6b00SAppaRao Puli } 542156d6b00SAppaRao Puli } 543156d6b00SAppaRao Puli 544156d6b00SAppaRao Puli nlohmann::json metricValuesArray = nlohmann::json::array(); 545156d6b00SAppaRao Puli for (const auto& it : readings) 546156d6b00SAppaRao Puli { 547156d6b00SAppaRao Puli metricValuesArray.push_back({}); 548156d6b00SAppaRao Puli nlohmann::json& entry = metricValuesArray.back(); 549156d6b00SAppaRao Puli 55093f5d7c7SWludzik, Jozef auto& [id, property, value, timestamp] = it; 55193f5d7c7SWludzik, Jozef 55293f5d7c7SWludzik, Jozef entry = {{"MetricId", id}, 55393f5d7c7SWludzik, Jozef {"MetricProperty", property}, 55493f5d7c7SWludzik, Jozef {"MetricValue", std::to_string(value)}, 55593f5d7c7SWludzik, Jozef {"Timestamp", crow::utility::getDateTime(timestamp)}}; 556156d6b00SAppaRao Puli } 557156d6b00SAppaRao Puli 558156d6b00SAppaRao Puli nlohmann::json msg = { 559156d6b00SAppaRao Puli {"@odata.id", "/redfish/v1/TelemetryService/MetricReports/" + id}, 560156d6b00SAppaRao Puli {"@odata.type", "#MetricReport.v1_3_0.MetricReport"}, 56123a21a1cSEd Tanous {"Id", id2}, 56223a21a1cSEd Tanous {"Name", id2}, 563156d6b00SAppaRao Puli {"Timestamp", readingsTs}, 564156d6b00SAppaRao Puli {"MetricReportDefinition", {{"@odata.id", metricReportDef}}}, 565156d6b00SAppaRao Puli {"MetricValues", metricValuesArray}}; 566156d6b00SAppaRao Puli 56771f52d96SEd Tanous this->sendEvent( 56871f52d96SEd Tanous msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 569156d6b00SAppaRao Puli } 570156d6b00SAppaRao Puli 571fe44eb0bSAyushi Smriti void updateRetryConfig(const uint32_t retryAttempts, 572fe44eb0bSAyushi Smriti const uint32_t retryTimeoutInterval) 573fe44eb0bSAyushi Smriti { 57462de0c68SAppaRao Puli if (conn != nullptr) 57562de0c68SAppaRao Puli { 576fe44eb0bSAyushi Smriti conn->setRetryConfig(retryAttempts, retryTimeoutInterval); 577fe44eb0bSAyushi Smriti } 57862de0c68SAppaRao Puli } 579fe44eb0bSAyushi Smriti 580fe44eb0bSAyushi Smriti void updateRetryPolicy() 581fe44eb0bSAyushi Smriti { 58262de0c68SAppaRao Puli if (conn != nullptr) 58362de0c68SAppaRao Puli { 584fe44eb0bSAyushi Smriti conn->setRetryPolicy(retryPolicy); 585fe44eb0bSAyushi Smriti } 58662de0c68SAppaRao Puli } 587fe44eb0bSAyushi Smriti 58896330b99SSunitha Harish uint64_t getEventSeqNum() 58996330b99SSunitha Harish { 59096330b99SSunitha Harish return eventSeqNum; 59196330b99SSunitha Harish } 59296330b99SSunitha Harish 593b52664e2SAppaRao Puli private: 5940b4bdd93SAppaRao Puli uint64_t eventSeqNum; 595b52664e2SAppaRao Puli std::string host; 596b52664e2SAppaRao Puli std::string port; 597b52664e2SAppaRao Puli std::string path; 598b52664e2SAppaRao Puli std::string uriProto; 5994bbf237fSAppaRao Puli std::shared_ptr<crow::HttpClient> conn = nullptr; 6004bbf237fSAppaRao Puli std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr; 601b52664e2SAppaRao Puli }; 602b52664e2SAppaRao Puli 6031bf712bcSAyushi Smriti static constexpr const bool defaultEnabledState = true; 6041bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryAttempts = 3; 6051bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryInterval = 30; 6061bf712bcSAyushi Smriti static constexpr const char* defaulEventFormatType = "Event"; 6071bf712bcSAyushi Smriti static constexpr const char* defaulSubscriptionType = "RedfishEvent"; 6081bf712bcSAyushi Smriti static constexpr const char* defaulRetryPolicy = "TerminateAfterRetries"; 6091bf712bcSAyushi Smriti 610b52664e2SAppaRao Puli class EventServiceManager 611b52664e2SAppaRao Puli { 612b52664e2SAppaRao Puli private: 6137d1cc387SAppaRao Puli bool serviceEnabled; 6147d1cc387SAppaRao Puli uint32_t retryAttempts; 6157d1cc387SAppaRao Puli uint32_t retryTimeoutInterval; 6167d1cc387SAppaRao Puli 6179f616dd1SEd Tanous EventServiceManager() 618b52664e2SAppaRao Puli { 6191bf712bcSAyushi Smriti // Load config from persist store. 6201bf712bcSAyushi Smriti initConfig(); 621b52664e2SAppaRao Puli } 622b52664e2SAppaRao Puli 6237f4eb588SAppaRao Puli std::string lastEventTStr; 6249f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 6259f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 626156d6b00SAppaRao Puli std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor; 627b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 628b52664e2SAppaRao Puli subscriptionsMap; 629b52664e2SAppaRao Puli 6309f616dd1SEd Tanous uint64_t eventId{1}; 63196330b99SSunitha Harish 632b52664e2SAppaRao Puli public: 6339f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 6349f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 6359f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 6369f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 6379f616dd1SEd Tanous 638b52664e2SAppaRao Puli static EventServiceManager& getInstance() 639b52664e2SAppaRao Puli { 640b52664e2SAppaRao Puli static EventServiceManager handler; 641b52664e2SAppaRao Puli return handler; 642b52664e2SAppaRao Puli } 643b52664e2SAppaRao Puli 6441bf712bcSAyushi Smriti void loadDefaultConfig() 6451bf712bcSAyushi Smriti { 6461bf712bcSAyushi Smriti serviceEnabled = defaultEnabledState; 6471bf712bcSAyushi Smriti retryAttempts = defaultRetryAttempts; 6481bf712bcSAyushi Smriti retryTimeoutInterval = defaultRetryInterval; 6491bf712bcSAyushi Smriti } 6501bf712bcSAyushi Smriti 6511bf712bcSAyushi Smriti void initConfig() 6521bf712bcSAyushi Smriti { 6531bf712bcSAyushi Smriti std::ifstream eventConfigFile(eventServiceFile); 6541bf712bcSAyushi Smriti if (!eventConfigFile.good()) 6551bf712bcSAyushi Smriti { 6561bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService config not exist"; 6571bf712bcSAyushi Smriti loadDefaultConfig(); 6581bf712bcSAyushi Smriti return; 6591bf712bcSAyushi Smriti } 6601bf712bcSAyushi Smriti auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 6611bf712bcSAyushi Smriti if (jsonData.is_discarded()) 6621bf712bcSAyushi Smriti { 6631bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "EventService config parse error."; 6641bf712bcSAyushi Smriti loadDefaultConfig(); 6651bf712bcSAyushi Smriti return; 6661bf712bcSAyushi Smriti } 6671bf712bcSAyushi Smriti 6681bf712bcSAyushi Smriti nlohmann::json jsonConfig; 6691bf712bcSAyushi Smriti if (json_util::getValueFromJsonObject(jsonData, "Configuration", 6701bf712bcSAyushi Smriti jsonConfig)) 6711bf712bcSAyushi Smriti { 6721bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonConfig, "ServiceEnabled", 6731bf712bcSAyushi Smriti serviceEnabled)) 6741bf712bcSAyushi Smriti { 6751bf712bcSAyushi Smriti serviceEnabled = defaultEnabledState; 6761bf712bcSAyushi Smriti } 6771bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 6781bf712bcSAyushi Smriti jsonConfig, "DeliveryRetryAttempts", retryAttempts)) 6791bf712bcSAyushi Smriti { 6801bf712bcSAyushi Smriti retryAttempts = defaultRetryAttempts; 6811bf712bcSAyushi Smriti } 6821bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 6831bf712bcSAyushi Smriti jsonConfig, "DeliveryRetryIntervalSeconds", 6841bf712bcSAyushi Smriti retryTimeoutInterval)) 6851bf712bcSAyushi Smriti { 6861bf712bcSAyushi Smriti retryTimeoutInterval = defaultRetryInterval; 6871bf712bcSAyushi Smriti } 6881bf712bcSAyushi Smriti } 6891bf712bcSAyushi Smriti else 6901bf712bcSAyushi Smriti { 6911bf712bcSAyushi Smriti loadDefaultConfig(); 6921bf712bcSAyushi Smriti } 6931bf712bcSAyushi Smriti 6941bf712bcSAyushi Smriti nlohmann::json subscriptionsList; 6951bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonData, "Subscriptions", 6961bf712bcSAyushi Smriti subscriptionsList)) 6971bf712bcSAyushi Smriti { 6981bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService: Subscriptions not exist."; 6991bf712bcSAyushi Smriti return; 7001bf712bcSAyushi Smriti } 7011bf712bcSAyushi Smriti 7021bf712bcSAyushi Smriti for (nlohmann::json& jsonObj : subscriptionsList) 7031bf712bcSAyushi Smriti { 7041bf712bcSAyushi Smriti std::string protocol; 7051bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "Protocol", 7061bf712bcSAyushi Smriti protocol)) 7071bf712bcSAyushi Smriti { 7081bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "Invalid subscription Protocol exist."; 7091bf712bcSAyushi Smriti continue; 7101bf712bcSAyushi Smriti } 7114bbf237fSAppaRao Puli 7124bbf237fSAppaRao Puli std::string subscriptionType; 7134bbf237fSAppaRao Puli if (!json_util::getValueFromJsonObject(jsonObj, "SubscriptionType", 7144bbf237fSAppaRao Puli subscriptionType)) 7154bbf237fSAppaRao Puli { 7164bbf237fSAppaRao Puli subscriptionType = defaulSubscriptionType; 7174bbf237fSAppaRao Puli } 7184bbf237fSAppaRao Puli // SSE connections are initiated from client 7194bbf237fSAppaRao Puli // and can't be re-established from server. 7204bbf237fSAppaRao Puli if (subscriptionType == "SSE") 7214bbf237fSAppaRao Puli { 7224bbf237fSAppaRao Puli BMCWEB_LOG_DEBUG 7234bbf237fSAppaRao Puli << "The subscription type is SSE, so skipping."; 7244bbf237fSAppaRao Puli continue; 7254bbf237fSAppaRao Puli } 7264bbf237fSAppaRao Puli 7271bf712bcSAyushi Smriti std::string destination; 7281bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "Destination", 7291bf712bcSAyushi Smriti destination)) 7301bf712bcSAyushi Smriti { 7311bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "Invalid subscription destination exist."; 7321bf712bcSAyushi Smriti continue; 7331bf712bcSAyushi Smriti } 7341bf712bcSAyushi Smriti std::string host; 7351bf712bcSAyushi Smriti std::string urlProto; 7361bf712bcSAyushi Smriti std::string port; 7371bf712bcSAyushi Smriti std::string path; 7381bf712bcSAyushi Smriti bool status = 7391bf712bcSAyushi Smriti validateAndSplitUrl(destination, urlProto, host, port, path); 7401bf712bcSAyushi Smriti 7411bf712bcSAyushi Smriti if (!status) 7421bf712bcSAyushi Smriti { 7431bf712bcSAyushi Smriti BMCWEB_LOG_ERROR 7441bf712bcSAyushi Smriti << "Failed to validate and split destination url"; 7451bf712bcSAyushi Smriti continue; 7461bf712bcSAyushi Smriti } 7471bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 7481bf712bcSAyushi Smriti std::make_shared<Subscription>(host, port, path, urlProto); 7491bf712bcSAyushi Smriti 7501bf712bcSAyushi Smriti subValue->destinationUrl = destination; 7511bf712bcSAyushi Smriti subValue->protocol = protocol; 7524bbf237fSAppaRao Puli subValue->subscriptionType = subscriptionType; 7531bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 7541bf712bcSAyushi Smriti jsonObj, "DeliveryRetryPolicy", subValue->retryPolicy)) 7551bf712bcSAyushi Smriti { 7561bf712bcSAyushi Smriti subValue->retryPolicy = defaulRetryPolicy; 7571bf712bcSAyushi Smriti } 7581bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "EventFormatType", 7591bf712bcSAyushi Smriti subValue->eventFormatType)) 7601bf712bcSAyushi Smriti { 7611bf712bcSAyushi Smriti subValue->eventFormatType = defaulEventFormatType; 7621bf712bcSAyushi Smriti } 7631bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "Context", 7641bf712bcSAyushi Smriti subValue->customText); 7651bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "MessageIds", 7661bf712bcSAyushi Smriti subValue->registryMsgIds); 7671bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "RegistryPrefixes", 7681bf712bcSAyushi Smriti subValue->registryPrefixes); 769e56f254cSSunitha Harish json_util::getValueFromJsonObject(jsonObj, "ResourceTypes", 770e56f254cSSunitha Harish subValue->resourceTypes); 7711bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "HttpHeaders", 7721bf712bcSAyushi Smriti subValue->httpHeaders); 7731bf712bcSAyushi Smriti json_util::getValueFromJsonObject( 7741bf712bcSAyushi Smriti jsonObj, "MetricReportDefinitions", 7751bf712bcSAyushi Smriti subValue->metricReportDefinitions); 7761bf712bcSAyushi Smriti 7771bf712bcSAyushi Smriti std::string id = addSubscription(subValue, false); 7781bf712bcSAyushi Smriti if (id.empty()) 7791bf712bcSAyushi Smriti { 7801bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Failed to add subscription"; 7811bf712bcSAyushi Smriti } 7821bf712bcSAyushi Smriti } 7831bf712bcSAyushi Smriti return; 7841bf712bcSAyushi Smriti } 7851bf712bcSAyushi Smriti 786b52664e2SAppaRao Puli void updateSubscriptionData() 787b52664e2SAppaRao Puli { 788b52664e2SAppaRao Puli // Persist the config and subscription data. 7891bf712bcSAyushi Smriti nlohmann::json jsonData; 7901bf712bcSAyushi Smriti 7911bf712bcSAyushi Smriti nlohmann::json& configObj = jsonData["Configuration"]; 7921bf712bcSAyushi Smriti configObj["ServiceEnabled"] = serviceEnabled; 7931bf712bcSAyushi Smriti configObj["DeliveryRetryAttempts"] = retryAttempts; 7941bf712bcSAyushi Smriti configObj["DeliveryRetryIntervalSeconds"] = retryTimeoutInterval; 7951bf712bcSAyushi Smriti 7961bf712bcSAyushi Smriti nlohmann::json& subListArray = jsonData["Subscriptions"]; 7971bf712bcSAyushi Smriti subListArray = nlohmann::json::array(); 7981bf712bcSAyushi Smriti 7991bf712bcSAyushi Smriti for (const auto& it : subscriptionsMap) 8001bf712bcSAyushi Smriti { 8011bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = it.second; 8024bbf237fSAppaRao Puli // Don't preserve SSE connections. Its initiated from 8034bbf237fSAppaRao Puli // client side and can't be re-established from server. 8044bbf237fSAppaRao Puli if (subValue->subscriptionType == "SSE") 8054bbf237fSAppaRao Puli { 8064bbf237fSAppaRao Puli BMCWEB_LOG_DEBUG 8074bbf237fSAppaRao Puli << "The subscription type is SSE, so skipping."; 8084bbf237fSAppaRao Puli continue; 8094bbf237fSAppaRao Puli } 8101bf712bcSAyushi Smriti 8114bbf237fSAppaRao Puli nlohmann::json entry; 8121bf712bcSAyushi Smriti entry["Context"] = subValue->customText; 8131bf712bcSAyushi Smriti entry["DeliveryRetryPolicy"] = subValue->retryPolicy; 8141bf712bcSAyushi Smriti entry["Destination"] = subValue->destinationUrl; 8151bf712bcSAyushi Smriti entry["EventFormatType"] = subValue->eventFormatType; 8161bf712bcSAyushi Smriti entry["HttpHeaders"] = subValue->httpHeaders; 8171bf712bcSAyushi Smriti entry["MessageIds"] = subValue->registryMsgIds; 8181bf712bcSAyushi Smriti entry["Protocol"] = subValue->protocol; 8191bf712bcSAyushi Smriti entry["RegistryPrefixes"] = subValue->registryPrefixes; 820e56f254cSSunitha Harish entry["ResourceTypes"] = subValue->resourceTypes; 8211bf712bcSAyushi Smriti entry["SubscriptionType"] = subValue->subscriptionType; 8221bf712bcSAyushi Smriti entry["MetricReportDefinitions"] = 8231bf712bcSAyushi Smriti subValue->metricReportDefinitions; 8241bf712bcSAyushi Smriti 8251bf712bcSAyushi Smriti subListArray.push_back(entry); 8261bf712bcSAyushi Smriti } 8271bf712bcSAyushi Smriti 8281bf712bcSAyushi Smriti const std::string tmpFile(std::string(eventServiceFile) + "_tmp"); 8291bf712bcSAyushi Smriti std::ofstream ofs(tmpFile, std::ios::out); 83071f52d96SEd Tanous const auto& writeData = jsonData.dump( 83171f52d96SEd Tanous 2, ' ', true, nlohmann::json::error_handler_t::replace); 8321bf712bcSAyushi Smriti ofs << writeData; 8331bf712bcSAyushi Smriti ofs.close(); 8341bf712bcSAyushi Smriti 8351bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService config updated to file."; 8361bf712bcSAyushi Smriti if (std::rename(tmpFile.c_str(), eventServiceFile) != 0) 8371bf712bcSAyushi Smriti { 8381bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Error in renaming temporary file: " 8391bf712bcSAyushi Smriti << tmpFile.c_str(); 8401bf712bcSAyushi Smriti } 841b52664e2SAppaRao Puli } 842b52664e2SAppaRao Puli 8437d1cc387SAppaRao Puli EventServiceConfig getEventServiceConfig() 8447d1cc387SAppaRao Puli { 8457d1cc387SAppaRao Puli return {serviceEnabled, retryAttempts, retryTimeoutInterval}; 8467d1cc387SAppaRao Puli } 8477d1cc387SAppaRao Puli 8487d1cc387SAppaRao Puli void setEventServiceConfig(const EventServiceConfig& cfg) 8497d1cc387SAppaRao Puli { 8507d1cc387SAppaRao Puli bool updateConfig = false; 851fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 8527d1cc387SAppaRao Puli 8537d1cc387SAppaRao Puli if (serviceEnabled != std::get<0>(cfg)) 8547d1cc387SAppaRao Puli { 8557d1cc387SAppaRao Puli serviceEnabled = std::get<0>(cfg); 8567d1cc387SAppaRao Puli if (serviceEnabled && noOfMetricReportSubscribers) 8577d1cc387SAppaRao Puli { 8587d1cc387SAppaRao Puli registerMetricReportSignal(); 8597d1cc387SAppaRao Puli } 8607d1cc387SAppaRao Puli else 8617d1cc387SAppaRao Puli { 8627d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8637d1cc387SAppaRao Puli } 8647d1cc387SAppaRao Puli updateConfig = true; 8657d1cc387SAppaRao Puli } 8667d1cc387SAppaRao Puli 8677d1cc387SAppaRao Puli if (retryAttempts != std::get<1>(cfg)) 8687d1cc387SAppaRao Puli { 8697d1cc387SAppaRao Puli retryAttempts = std::get<1>(cfg); 8707d1cc387SAppaRao Puli updateConfig = true; 871fe44eb0bSAyushi Smriti updateRetryCfg = true; 8727d1cc387SAppaRao Puli } 8737d1cc387SAppaRao Puli 8747d1cc387SAppaRao Puli if (retryTimeoutInterval != std::get<2>(cfg)) 8757d1cc387SAppaRao Puli { 8767d1cc387SAppaRao Puli retryTimeoutInterval = std::get<2>(cfg); 8777d1cc387SAppaRao Puli updateConfig = true; 878fe44eb0bSAyushi Smriti updateRetryCfg = true; 8797d1cc387SAppaRao Puli } 8807d1cc387SAppaRao Puli 8817d1cc387SAppaRao Puli if (updateConfig) 8827d1cc387SAppaRao Puli { 8837d1cc387SAppaRao Puli updateSubscriptionData(); 8847d1cc387SAppaRao Puli } 885fe44eb0bSAyushi Smriti 886fe44eb0bSAyushi Smriti if (updateRetryCfg) 887fe44eb0bSAyushi Smriti { 888fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 889fe44eb0bSAyushi Smriti for (const auto& it : 890fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 891fe44eb0bSAyushi Smriti { 892fe44eb0bSAyushi Smriti std::shared_ptr<Subscription> entry = it.second; 893fe44eb0bSAyushi Smriti entry->updateRetryConfig(retryAttempts, retryTimeoutInterval); 894fe44eb0bSAyushi Smriti } 895fe44eb0bSAyushi Smriti } 8967d1cc387SAppaRao Puli } 8977d1cc387SAppaRao Puli 8987d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8997d1cc387SAppaRao Puli { 9007d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 9017d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 9027d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 9037d1cc387SAppaRao Puli { 9047d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 9057d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 9067d1cc387SAppaRao Puli { 9077d1cc387SAppaRao Puli eventLogSubCount++; 9087d1cc387SAppaRao Puli } 9097d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 9107d1cc387SAppaRao Puli { 9117d1cc387SAppaRao Puli metricReportSubCount++; 9127d1cc387SAppaRao Puli } 9137d1cc387SAppaRao Puli } 9147d1cc387SAppaRao Puli 9157d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 9167d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 9177d1cc387SAppaRao Puli { 9187d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 9197d1cc387SAppaRao Puli if (noOfMetricReportSubscribers) 9207d1cc387SAppaRao Puli { 9217d1cc387SAppaRao Puli registerMetricReportSignal(); 9227d1cc387SAppaRao Puli } 9237d1cc387SAppaRao Puli else 9247d1cc387SAppaRao Puli { 9257d1cc387SAppaRao Puli unregisterMetricReportSignal(); 9267d1cc387SAppaRao Puli } 9277d1cc387SAppaRao Puli } 9287d1cc387SAppaRao Puli } 9297d1cc387SAppaRao Puli 930b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 931b52664e2SAppaRao Puli { 932b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 933b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 934b52664e2SAppaRao Puli { 935b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 936b52664e2SAppaRao Puli return nullptr; 937b52664e2SAppaRao Puli } 938b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 939b52664e2SAppaRao Puli return subValue; 940b52664e2SAppaRao Puli } 941b52664e2SAppaRao Puli 942b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 9431bf712bcSAyushi Smriti const bool updateFile = true) 944b52664e2SAppaRao Puli { 945fc76b8acSEd Tanous 946fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 947fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 948fc76b8acSEd Tanous 949b52664e2SAppaRao Puli std::string id; 950b52664e2SAppaRao Puli 951b52664e2SAppaRao Puli int retry = 3; 952b52664e2SAppaRao Puli while (retry) 953b52664e2SAppaRao Puli { 954fc76b8acSEd Tanous id = std::to_string(dist(gen)); 955fc76b8acSEd Tanous if (gen.error()) 956fc76b8acSEd Tanous { 957fc76b8acSEd Tanous retry = 0; 958fc76b8acSEd Tanous break; 959fc76b8acSEd Tanous } 960b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 961b52664e2SAppaRao Puli if (inserted.second) 962b52664e2SAppaRao Puli { 963b52664e2SAppaRao Puli break; 964b52664e2SAppaRao Puli } 965b52664e2SAppaRao Puli --retry; 96623a21a1cSEd Tanous } 967b52664e2SAppaRao Puli 968b52664e2SAppaRao Puli if (retry <= 0) 969b52664e2SAppaRao Puli { 970b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Failed to generate random number"; 971b52664e2SAppaRao Puli return std::string(""); 972b52664e2SAppaRao Puli } 973b52664e2SAppaRao Puli 9747d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 9751bf712bcSAyushi Smriti 9761bf712bcSAyushi Smriti if (updateFile) 9771bf712bcSAyushi Smriti { 978b52664e2SAppaRao Puli updateSubscriptionData(); 9791bf712bcSAyushi Smriti } 9807f4eb588SAppaRao Puli 9817f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 9827f4eb588SAppaRao Puli if (lastEventTStr.empty()) 9837f4eb588SAppaRao Puli { 9847f4eb588SAppaRao Puli cacheLastEventTimestamp(); 9857f4eb588SAppaRao Puli } 9867f4eb588SAppaRao Puli #endif 987fe44eb0bSAyushi Smriti // Update retry configuration. 988fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 989fe44eb0bSAyushi Smriti subValue->updateRetryPolicy(); 990fe44eb0bSAyushi Smriti 991b52664e2SAppaRao Puli return id; 992b52664e2SAppaRao Puli } 993b52664e2SAppaRao Puli 994b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 995b52664e2SAppaRao Puli { 996b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 997b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 998b52664e2SAppaRao Puli { 999b52664e2SAppaRao Puli return false; 1000b52664e2SAppaRao Puli } 1001b52664e2SAppaRao Puli return true; 1002b52664e2SAppaRao Puli } 1003b52664e2SAppaRao Puli 1004b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 1005b52664e2SAppaRao Puli { 1006b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 1007b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 1008b52664e2SAppaRao Puli { 1009b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 10107d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 1011b52664e2SAppaRao Puli updateSubscriptionData(); 1012b52664e2SAppaRao Puli } 1013b52664e2SAppaRao Puli } 1014b52664e2SAppaRao Puli 1015b52664e2SAppaRao Puli size_t getNumberOfSubscriptions() 1016b52664e2SAppaRao Puli { 1017b52664e2SAppaRao Puli return subscriptionsMap.size(); 1018b52664e2SAppaRao Puli } 1019b52664e2SAppaRao Puli 1020b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 1021b52664e2SAppaRao Puli { 1022b52664e2SAppaRao Puli std::vector<std::string> idList; 1023b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 1024b52664e2SAppaRao Puli { 1025b52664e2SAppaRao Puli idList.emplace_back(it.first); 1026b52664e2SAppaRao Puli } 1027b52664e2SAppaRao Puli return idList; 1028b52664e2SAppaRao Puli } 1029b52664e2SAppaRao Puli 1030b52664e2SAppaRao Puli bool isDestinationExist(const std::string& destUrl) 1031b52664e2SAppaRao Puli { 1032b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 1033b52664e2SAppaRao Puli { 1034b52664e2SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 1035b52664e2SAppaRao Puli if (entry->destinationUrl == destUrl) 1036b52664e2SAppaRao Puli { 1037b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 1038b52664e2SAppaRao Puli return true; 1039b52664e2SAppaRao Puli } 1040b52664e2SAppaRao Puli } 1041b52664e2SAppaRao Puli return false; 1042b52664e2SAppaRao Puli } 10430b4bdd93SAppaRao Puli 10440b4bdd93SAppaRao Puli void sendTestEventLog() 10450b4bdd93SAppaRao Puli { 10460b4bdd93SAppaRao Puli for (const auto& it : this->subscriptionsMap) 10470b4bdd93SAppaRao Puli { 10480b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 10490b4bdd93SAppaRao Puli entry->sendTestEventLog(); 10500b4bdd93SAppaRao Puli } 10510b4bdd93SAppaRao Puli } 1052e9a14131SAppaRao Puli 105396330b99SSunitha Harish void sendEvent(const nlohmann::json& eventMessageIn, 105496330b99SSunitha Harish const std::string& origin, const std::string& resType) 105596330b99SSunitha Harish { 105696330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 105796330b99SSunitha Harish nlohmann::json eventMessage = eventMessageIn; 105896330b99SSunitha Harish // MemberId is 0 : since we are sending one event record. 105996330b99SSunitha Harish uint64_t memberId = 0; 106096330b99SSunitha Harish 106196330b99SSunitha Harish nlohmann::json event = { 106296330b99SSunitha Harish {"EventId", eventId}, 106396330b99SSunitha Harish {"MemberId", memberId}, 106496330b99SSunitha Harish {"EventTimestamp", crow::utility::dateTimeNow()}, 106596330b99SSunitha Harish {"OriginOfCondition", origin}}; 106696330b99SSunitha Harish for (nlohmann::json::iterator it = event.begin(); it != event.end(); 106796330b99SSunitha Harish ++it) 106896330b99SSunitha Harish { 106996330b99SSunitha Harish eventMessage[it.key()] = it.value(); 107096330b99SSunitha Harish } 107196330b99SSunitha Harish eventRecord.push_back(eventMessage); 107296330b99SSunitha Harish 107396330b99SSunitha Harish for (const auto& it : this->subscriptionsMap) 107496330b99SSunitha Harish { 107596330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 107696330b99SSunitha Harish bool isSubscribed = false; 107796330b99SSunitha Harish // Search the resourceTypes list for the subscription. 107896330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 107996330b99SSunitha Harish // send everything. 108096330b99SSunitha Harish if (entry->resourceTypes.size()) 108196330b99SSunitha Harish { 10823174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 108396330b99SSunitha Harish { 108496330b99SSunitha Harish if (resType == resource) 108596330b99SSunitha Harish { 108696330b99SSunitha Harish BMCWEB_LOG_INFO << "ResourceType " << resource 108796330b99SSunitha Harish << " found in the subscribed list"; 108896330b99SSunitha Harish isSubscribed = true; 108996330b99SSunitha Harish break; 109096330b99SSunitha Harish } 109196330b99SSunitha Harish } 109296330b99SSunitha Harish } 109396330b99SSunitha Harish else // resourceTypes list is empty. 109496330b99SSunitha Harish { 109596330b99SSunitha Harish isSubscribed = true; 109696330b99SSunitha Harish } 109796330b99SSunitha Harish if (isSubscribed) 109896330b99SSunitha Harish { 109996330b99SSunitha Harish nlohmann::json msgJson = { 110096330b99SSunitha Harish {"@odata.type", "#Event.v1_4_0.Event"}, 110196330b99SSunitha Harish {"Name", "Event Log"}, 110296330b99SSunitha Harish {"Id", eventId}, 110396330b99SSunitha Harish {"Events", eventRecord}}; 110471f52d96SEd Tanous entry->sendEvent(msgJson.dump( 110571f52d96SEd Tanous 2, ' ', true, nlohmann::json::error_handler_t::replace)); 110696330b99SSunitha Harish eventId++; // increament the eventId 110796330b99SSunitha Harish } 110896330b99SSunitha Harish else 110996330b99SSunitha Harish { 111096330b99SSunitha Harish BMCWEB_LOG_INFO << "Not subscribed to this resource"; 111196330b99SSunitha Harish } 111296330b99SSunitha Harish } 111396330b99SSunitha Harish } 11145738de59SAsmitha Karunanithi void sendBroadcastMsg(const std::string& broadcastMsg) 11155738de59SAsmitha Karunanithi { 11165738de59SAsmitha Karunanithi for (const auto& it : this->subscriptionsMap) 11175738de59SAsmitha Karunanithi { 11185738de59SAsmitha Karunanithi std::shared_ptr<Subscription> entry = it.second; 11195738de59SAsmitha Karunanithi nlohmann::json msgJson = { 11205738de59SAsmitha Karunanithi {"Timestamp", crow::utility::dateTimeNow()}, 11215738de59SAsmitha Karunanithi {"OriginOfCondition", "/ibm/v1/HMC/BroadcastService"}, 11225738de59SAsmitha Karunanithi {"Name", "Broadcast Message"}, 11235738de59SAsmitha Karunanithi {"Message", broadcastMsg}}; 112471f52d96SEd Tanous entry->sendEvent(msgJson.dump( 112571f52d96SEd Tanous 2, ' ', true, nlohmann::json::error_handler_t::replace)); 11265738de59SAsmitha Karunanithi } 11275738de59SAsmitha Karunanithi } 112896330b99SSunitha Harish 11297f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 11307f4eb588SAppaRao Puli void cacheLastEventTimestamp() 11317f4eb588SAppaRao Puli { 1132016761afSAppaRao Puli lastEventTStr.clear(); 11337f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11347f4eb588SAppaRao Puli if (!logStream.good()) 11357f4eb588SAppaRao Puli { 11367f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 11377f4eb588SAppaRao Puli return; 11387f4eb588SAppaRao Puli } 11397f4eb588SAppaRao Puli std::string logEntry; 11407f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11417f4eb588SAppaRao Puli { 1142f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 11437f4eb588SAppaRao Puli if (space == std::string::npos) 11447f4eb588SAppaRao Puli { 11457f4eb588SAppaRao Puli // Shouldn't enter here but lets skip it. 11467f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid log entry found."; 11477f4eb588SAppaRao Puli continue; 11487f4eb588SAppaRao Puli } 11497f4eb588SAppaRao Puli lastEventTStr = logEntry.substr(0, space); 11507f4eb588SAppaRao Puli } 11517f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Last Event time stamp set: " << lastEventTStr; 11527f4eb588SAppaRao Puli } 11537f4eb588SAppaRao Puli 11547f4eb588SAppaRao Puli void readEventLogsFromFile() 11557f4eb588SAppaRao Puli { 11567f4eb588SAppaRao Puli if (!serviceEnabled || !noOfEventLogSubscribers) 11577f4eb588SAppaRao Puli { 11587f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 11597f4eb588SAppaRao Puli return; 11607f4eb588SAppaRao Puli } 11617f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11627f4eb588SAppaRao Puli if (!logStream.good()) 11637f4eb588SAppaRao Puli { 11647f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed"; 11657f4eb588SAppaRao Puli return; 11667f4eb588SAppaRao Puli } 11677f4eb588SAppaRao Puli 11687f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11697f4eb588SAppaRao Puli 11707f4eb588SAppaRao Puli bool startLogCollection = false; 11717f4eb588SAppaRao Puli bool firstEntry = true; 11727f4eb588SAppaRao Puli 11737f4eb588SAppaRao Puli std::string logEntry; 11747f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11757f4eb588SAppaRao Puli { 1176016761afSAppaRao Puli if (!startLogCollection && !lastEventTStr.empty()) 11777f4eb588SAppaRao Puli { 11787f4eb588SAppaRao Puli if (boost::starts_with(logEntry, lastEventTStr)) 11797f4eb588SAppaRao Puli { 11807f4eb588SAppaRao Puli startLogCollection = true; 11817f4eb588SAppaRao Puli } 11827f4eb588SAppaRao Puli continue; 11837f4eb588SAppaRao Puli } 11847f4eb588SAppaRao Puli 11857f4eb588SAppaRao Puli std::string idStr; 11867f4eb588SAppaRao Puli if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry)) 11877f4eb588SAppaRao Puli { 11887f4eb588SAppaRao Puli continue; 11897f4eb588SAppaRao Puli } 11907f4eb588SAppaRao Puli firstEntry = false; 11917f4eb588SAppaRao Puli 11927f4eb588SAppaRao Puli std::string timestamp; 11937f4eb588SAppaRao Puli std::string messageID; 11945e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11957f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11967f4eb588SAppaRao Puli messageArgs) != 0) 11977f4eb588SAppaRao Puli { 11987f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 11997f4eb588SAppaRao Puli continue; 12007f4eb588SAppaRao Puli } 12017f4eb588SAppaRao Puli 12027f4eb588SAppaRao Puli std::string registryName; 12037f4eb588SAppaRao Puli std::string messageKey; 12047f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 12057f4eb588SAppaRao Puli messageKey); 12067f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 12077f4eb588SAppaRao Puli { 12087f4eb588SAppaRao Puli continue; 12097f4eb588SAppaRao Puli } 12107f4eb588SAppaRao Puli 12117f4eb588SAppaRao Puli lastEventTStr = timestamp; 12127f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 12137f4eb588SAppaRao Puli messageKey, messageArgs); 12147f4eb588SAppaRao Puli } 12157f4eb588SAppaRao Puli 12167f4eb588SAppaRao Puli for (const auto& it : this->subscriptionsMap) 12177f4eb588SAppaRao Puli { 12187f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 12197f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 12207f4eb588SAppaRao Puli { 12217f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 12227f4eb588SAppaRao Puli } 12237f4eb588SAppaRao Puli } 12247f4eb588SAppaRao Puli } 12257f4eb588SAppaRao Puli 12267f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 12277f4eb588SAppaRao Puli { 12286a9f85f9SAppaRao Puli if (!inotifyConn) 12297f4eb588SAppaRao Puli { 12307f4eb588SAppaRao Puli return; 12317f4eb588SAppaRao Puli } 12327f4eb588SAppaRao Puli 12337f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 12347f4eb588SAppaRao Puli 12357f4eb588SAppaRao Puli inotifyConn->async_read_some( 12367f4eb588SAppaRao Puli boost::asio::buffer(readBuffer), 12377f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 12387f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 12397f4eb588SAppaRao Puli if (ec) 12407f4eb588SAppaRao Puli { 12417f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 12427f4eb588SAppaRao Puli return; 12437f4eb588SAppaRao Puli } 12447f4eb588SAppaRao Puli std::size_t index = 0; 1245b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 12467f4eb588SAppaRao Puli { 12477f4eb588SAppaRao Puli struct inotify_event event; 1248b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1249b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1250b792cc56SAppaRao Puli { 1251b792cc56SAppaRao Puli if ((event.len == 0) || 1252b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1253b792cc56SAppaRao Puli { 1254b792cc56SAppaRao Puli index += (iEventSize + event.len); 1255b792cc56SAppaRao Puli continue; 1256b792cc56SAppaRao Puli } 1257b792cc56SAppaRao Puli 1258b792cc56SAppaRao Puli std::string fileName(&readBuffer[index + iEventSize], 1259b792cc56SAppaRao Puli event.len); 1260b792cc56SAppaRao Puli if (std::strcmp(fileName.c_str(), "redfish") != 0) 1261b792cc56SAppaRao Puli { 1262b792cc56SAppaRao Puli index += (iEventSize + event.len); 1263b792cc56SAppaRao Puli continue; 1264b792cc56SAppaRao Puli } 1265b792cc56SAppaRao Puli 1266b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1267b792cc56SAppaRao Puli << "Redfish log file created/deleted. event.name: " 1268b792cc56SAppaRao Puli << fileName; 1269b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1270b792cc56SAppaRao Puli { 1271b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1272b792cc56SAppaRao Puli { 1273b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1274016761afSAppaRao Puli << "Remove and Add inotify watcher on " 1275016761afSAppaRao Puli "redfish event log file"; 1276016761afSAppaRao Puli // Remove existing inotify watcher and add 1277016761afSAppaRao Puli // with new redfish event log file. 1278016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1279016761afSAppaRao Puli fileWatchDesc = -1; 1280b792cc56SAppaRao Puli } 1281b792cc56SAppaRao Puli 1282b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1283b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1284b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1285b792cc56SAppaRao Puli { 1286b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1287b792cc56SAppaRao Puli << "inotify_add_watch failed for " 1288b792cc56SAppaRao Puli "redfish log file."; 1289b792cc56SAppaRao Puli return; 1290b792cc56SAppaRao Puli } 1291b792cc56SAppaRao Puli 1292b792cc56SAppaRao Puli EventServiceManager::getInstance() 1293b792cc56SAppaRao Puli .cacheLastEventTimestamp(); 1294b792cc56SAppaRao Puli EventServiceManager::getInstance() 1295b792cc56SAppaRao Puli .readEventLogsFromFile(); 1296b792cc56SAppaRao Puli } 1297b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1298b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1299b792cc56SAppaRao Puli { 1300b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1301b792cc56SAppaRao Puli { 1302b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1303b792cc56SAppaRao Puli fileWatchDesc = -1; 1304b792cc56SAppaRao Puli } 1305b792cc56SAppaRao Puli } 1306b792cc56SAppaRao Puli } 1307b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1308b792cc56SAppaRao Puli { 1309b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 13107f4eb588SAppaRao Puli { 13117f4eb588SAppaRao Puli EventServiceManager::getInstance() 13127f4eb588SAppaRao Puli .readEventLogsFromFile(); 13137f4eb588SAppaRao Puli } 1314b792cc56SAppaRao Puli } 1315b792cc56SAppaRao Puli index += (iEventSize + event.len); 13167f4eb588SAppaRao Puli } 13177f4eb588SAppaRao Puli 13187f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13197f4eb588SAppaRao Puli }); 13207f4eb588SAppaRao Puli } 13217f4eb588SAppaRao Puli 13227f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 13237f4eb588SAppaRao Puli { 132423a21a1cSEd Tanous inotifyConn.emplace(ioc); 1325b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1326b792cc56SAppaRao Puli if (inotifyFd == -1) 13277f4eb588SAppaRao Puli { 13287f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "inotify_init1 failed."; 13297f4eb588SAppaRao Puli return -1; 13307f4eb588SAppaRao Puli } 1331b792cc56SAppaRao Puli 1332b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1333b792cc56SAppaRao Puli // create/delete. 1334b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1335b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1336b792cc56SAppaRao Puli if (dirWatchDesc == -1) 13377f4eb588SAppaRao Puli { 13387f4eb588SAppaRao Puli BMCWEB_LOG_ERROR 1339b792cc56SAppaRao Puli << "inotify_add_watch failed for event log directory."; 13407f4eb588SAppaRao Puli return -1; 13417f4eb588SAppaRao Puli } 13427f4eb588SAppaRao Puli 1343b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 1344b792cc56SAppaRao Puli fileWatchDesc = 1345b792cc56SAppaRao Puli inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY); 1346b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1347b792cc56SAppaRao Puli { 1348b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1349b792cc56SAppaRao Puli << "inotify_add_watch failed for redfish log file."; 1350b792cc56SAppaRao Puli // Don't return error if file not exist. 1351b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1352b792cc56SAppaRao Puli } 1353b792cc56SAppaRao Puli 13547f4eb588SAppaRao Puli // monitor redfish event log file 1355b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13567f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13577f4eb588SAppaRao Puli 13587f4eb588SAppaRao Puli return 0; 13597f4eb588SAppaRao Puli } 13607f4eb588SAppaRao Puli 13617f4eb588SAppaRao Puli #endif 13627f4eb588SAppaRao Puli 1363156d6b00SAppaRao Puli void getMetricReading(const std::string& service, 1364156d6b00SAppaRao Puli const std::string& objPath, const std::string& intf) 1365156d6b00SAppaRao Puli { 1366f23b7296SEd Tanous std::size_t found = objPath.find_last_of('/'); 1367156d6b00SAppaRao Puli if (found == std::string::npos) 1368156d6b00SAppaRao Puli { 1369156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid objPath received"; 1370156d6b00SAppaRao Puli return; 1371156d6b00SAppaRao Puli } 1372156d6b00SAppaRao Puli 1373156d6b00SAppaRao Puli std::string idStr = objPath.substr(found + 1); 1374156d6b00SAppaRao Puli if (idStr.empty()) 1375156d6b00SAppaRao Puli { 1376156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid ID in objPath"; 1377156d6b00SAppaRao Puli return; 1378156d6b00SAppaRao Puli } 1379156d6b00SAppaRao Puli 1380156d6b00SAppaRao Puli crow::connections::systemBus->async_method_call( 1381156d6b00SAppaRao Puli [idStr{std::move(idStr)}]( 1382156d6b00SAppaRao Puli const boost::system::error_code ec, 1383156d6b00SAppaRao Puli boost::container::flat_map< 138493f5d7c7SWludzik, Jozef std::string, std::variant<int32_t, ReadingsObjType>>& 1385156d6b00SAppaRao Puli resp) { 1386156d6b00SAppaRao Puli if (ec) 1387156d6b00SAppaRao Puli { 1388156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG 1389156d6b00SAppaRao Puli << "D-Bus call failed to GetAll metric readings."; 1390156d6b00SAppaRao Puli return; 1391156d6b00SAppaRao Puli } 1392156d6b00SAppaRao Puli 139393f5d7c7SWludzik, Jozef const int32_t* timestampPtr = 139493f5d7c7SWludzik, Jozef std::get_if<int32_t>(&resp["Timestamp"]); 1395156d6b00SAppaRao Puli if (!timestampPtr) 1396156d6b00SAppaRao Puli { 1397156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Failed to Get timestamp."; 1398156d6b00SAppaRao Puli return; 1399156d6b00SAppaRao Puli } 1400156d6b00SAppaRao Puli 1401156d6b00SAppaRao Puli ReadingsObjType* readingsPtr = 1402156d6b00SAppaRao Puli std::get_if<ReadingsObjType>(&resp["Readings"]); 1403156d6b00SAppaRao Puli if (!readingsPtr) 1404156d6b00SAppaRao Puli { 1405156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Failed to Get Readings property."; 1406156d6b00SAppaRao Puli return; 1407156d6b00SAppaRao Puli } 1408156d6b00SAppaRao Puli 1409156d6b00SAppaRao Puli if (!readingsPtr->size()) 1410156d6b00SAppaRao Puli { 1411156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "No metrics report to be transferred"; 1412156d6b00SAppaRao Puli return; 1413156d6b00SAppaRao Puli } 1414156d6b00SAppaRao Puli 1415156d6b00SAppaRao Puli for (const auto& it : 1416156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1417156d6b00SAppaRao Puli { 1418156d6b00SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 1419156d6b00SAppaRao Puli if (entry->eventFormatType == metricReportFormatType) 1420156d6b00SAppaRao Puli { 142193f5d7c7SWludzik, Jozef entry->filterAndSendReports( 142293f5d7c7SWludzik, Jozef idStr, crow::utility::getDateTime(*timestampPtr), 1423156d6b00SAppaRao Puli *readingsPtr); 1424156d6b00SAppaRao Puli } 1425156d6b00SAppaRao Puli } 1426156d6b00SAppaRao Puli }, 1427156d6b00SAppaRao Puli service, objPath, "org.freedesktop.DBus.Properties", "GetAll", 1428156d6b00SAppaRao Puli intf); 1429156d6b00SAppaRao Puli } 1430156d6b00SAppaRao Puli 1431156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1432156d6b00SAppaRao Puli { 14337d1cc387SAppaRao Puli if (matchTelemetryMonitor) 14347d1cc387SAppaRao Puli { 1435156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1436156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1437156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1438156d6b00SAppaRao Puli } 14397d1cc387SAppaRao Puli } 1440156d6b00SAppaRao Puli 1441156d6b00SAppaRao Puli void registerMetricReportSignal() 1442156d6b00SAppaRao Puli { 14437d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1444156d6b00SAppaRao Puli { 14457d1cc387SAppaRao Puli BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1446156d6b00SAppaRao Puli return; 1447156d6b00SAppaRao Puli } 1448156d6b00SAppaRao Puli 1449156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1450156d6b00SAppaRao Puli std::string matchStr( 1451156d6b00SAppaRao Puli "type='signal',member='ReportUpdate', " 1452156d6b00SAppaRao Puli "interface='xyz.openbmc_project.MonitoringService.Report'"); 1453156d6b00SAppaRao Puli 1454156d6b00SAppaRao Puli matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>( 1455156d6b00SAppaRao Puli *crow::connections::systemBus, matchStr, 1456156d6b00SAppaRao Puli [this](sdbusplus::message::message& msg) { 1457156d6b00SAppaRao Puli if (msg.is_method_error()) 1458156d6b00SAppaRao Puli { 1459156d6b00SAppaRao Puli BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 1460156d6b00SAppaRao Puli return; 1461156d6b00SAppaRao Puli } 1462156d6b00SAppaRao Puli 1463156d6b00SAppaRao Puli std::string service = msg.get_sender(); 1464156d6b00SAppaRao Puli std::string objPath = msg.get_path(); 1465156d6b00SAppaRao Puli std::string intf = msg.get_interface(); 1466156d6b00SAppaRao Puli getMetricReading(service, objPath, intf); 1467156d6b00SAppaRao Puli }); 1468156d6b00SAppaRao Puli } 14691bf712bcSAyushi Smriti 14701bf712bcSAyushi Smriti bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto, 14711bf712bcSAyushi Smriti std::string& host, std::string& port, 14721bf712bcSAyushi Smriti std::string& path) 14731bf712bcSAyushi Smriti { 14741bf712bcSAyushi Smriti // Validate URL using regex expression 14751bf712bcSAyushi Smriti // Format: <protocol>://<host>:<port>/<path> 14761bf712bcSAyushi Smriti // protocol: http/https 14771bf712bcSAyushi Smriti const std::regex urlRegex( 14781bf712bcSAyushi Smriti "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/" 14791bf712bcSAyushi Smriti "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)"); 14801bf712bcSAyushi Smriti std::cmatch match; 14811bf712bcSAyushi Smriti if (!std::regex_match(destUrl.c_str(), match, urlRegex)) 14821bf712bcSAyushi Smriti { 14831bf712bcSAyushi Smriti BMCWEB_LOG_INFO << "Dest. url did not match "; 14841bf712bcSAyushi Smriti return false; 14851bf712bcSAyushi Smriti } 14861bf712bcSAyushi Smriti 14871bf712bcSAyushi Smriti urlProto = std::string(match[1].first, match[1].second); 14881bf712bcSAyushi Smriti if (urlProto == "http") 14891bf712bcSAyushi Smriti { 14901bf712bcSAyushi Smriti #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING 14911bf712bcSAyushi Smriti return false; 14921bf712bcSAyushi Smriti #endif 14931bf712bcSAyushi Smriti } 14941bf712bcSAyushi Smriti 14951bf712bcSAyushi Smriti host = std::string(match[2].first, match[2].second); 14961bf712bcSAyushi Smriti port = std::string(match[3].first, match[3].second); 14971bf712bcSAyushi Smriti path = std::string(match[4].first, match[4].second); 14981bf712bcSAyushi Smriti if (port.empty()) 14991bf712bcSAyushi Smriti { 15001bf712bcSAyushi Smriti if (urlProto == "http") 15011bf712bcSAyushi Smriti { 15021bf712bcSAyushi Smriti port = "80"; 15031bf712bcSAyushi Smriti } 15041bf712bcSAyushi Smriti else 15051bf712bcSAyushi Smriti { 15061bf712bcSAyushi Smriti port = "443"; 15071bf712bcSAyushi Smriti } 15081bf712bcSAyushi Smriti } 15091bf712bcSAyushi Smriti if (path.empty()) 15101bf712bcSAyushi Smriti { 15111bf712bcSAyushi Smriti path = "/"; 15121bf712bcSAyushi Smriti } 15131bf712bcSAyushi Smriti return true; 15141bf712bcSAyushi Smriti } 151523a21a1cSEd Tanous }; 1516b52664e2SAppaRao Puli 1517b52664e2SAppaRao Puli } // namespace redfish 1518