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 177f4eb588SAppaRao Puli #include "registries.hpp" 187f4eb588SAppaRao Puli #include "registries/base_message_registry.hpp" 197f4eb588SAppaRao Puli #include "registries/openbmc_message_registry.hpp" 20b304bd79SP Dheeraj Srujan Kumar #include "registries/task_event_message_registry.hpp" 217f4eb588SAppaRao Puli 227f4eb588SAppaRao Puli #include <sys/inotify.h> 23b52664e2SAppaRao Puli 24fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp> 25b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp> 261214b7e7SGunnar Mills #include <error_messages.hpp> 2728afb49cSJunLin Chen #include <event_service_store.hpp> 281214b7e7SGunnar Mills #include <http_client.hpp> 2928afb49cSJunLin Chen #include <persistent_data.hpp> 30fc76b8acSEd Tanous #include <random.hpp> 314bbf237fSAppaRao Puli #include <server_sent_events.hpp> 321214b7e7SGunnar Mills #include <utils/json_utils.hpp> 331214b7e7SGunnar Mills 34b52664e2SAppaRao Puli #include <cstdlib> 35b52664e2SAppaRao Puli #include <ctime> 361bf712bcSAyushi Smriti #include <fstream> 37b52664e2SAppaRao Puli #include <memory> 38b52664e2SAppaRao Puli #include <variant> 39b52664e2SAppaRao Puli 40b52664e2SAppaRao Puli namespace redfish 41b52664e2SAppaRao Puli { 42156d6b00SAppaRao Puli 43156d6b00SAppaRao Puli using ReadingsObjType = 4493f5d7c7SWludzik, Jozef std::vector<std::tuple<std::string, std::string, double, int32_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 } 714642bf8fSGeorge Liu } // namespace message_registries 72b304bd79SP Dheeraj Srujan Kumar 734642bf8fSGeorge Liu #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 744642bf8fSGeorge Liu static std::optional<boost::asio::posix::stream_descriptor> inotifyConn; 754642bf8fSGeorge Liu static constexpr const char* redfishEventLogDir = "/var/log"; 764642bf8fSGeorge Liu static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 774642bf8fSGeorge Liu static constexpr const size_t iEventSize = sizeof(inotify_event); 784642bf8fSGeorge Liu static int inotifyFd = -1; 794642bf8fSGeorge Liu static int dirWatchDesc = -1; 804642bf8fSGeorge Liu static int fileWatchDesc = -1; 814642bf8fSGeorge Liu 824642bf8fSGeorge Liu // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 834642bf8fSGeorge Liu using EventLogObjectsType = 844642bf8fSGeorge Liu std::tuple<std::string, std::string, std::string, std::string, std::string, 854642bf8fSGeorge Liu std::vector<std::string>>; 864642bf8fSGeorge Liu 874642bf8fSGeorge Liu namespace message_registries 884642bf8fSGeorge 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 37328afb49cSJunLin Chen class Subscription : public persistent_data::UserSubscription 374b52664e2SAppaRao Puli { 375b52664e2SAppaRao Puli public: 376b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 377b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 378b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 379b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 380b52664e2SAppaRao Puli 381b52664e2SAppaRao Puli Subscription(const std::string& inHost, const std::string& inPort, 382b52664e2SAppaRao Puli const std::string& inPath, const std::string& inUriProto) : 3830b4bdd93SAppaRao Puli eventSeqNum(1), 3840b4bdd93SAppaRao Puli host(inHost), port(inPort), path(inPath), uriProto(inUriProto) 385b52664e2SAppaRao Puli { 386*7adb85acSSunitha Harish // Subscription constructor 387b52664e2SAppaRao Puli } 3884bbf237fSAppaRao Puli 38923e64207SEd Tanous Subscription(const std::shared_ptr<boost::beast::tcp_stream>& adaptor) : 3904bbf237fSAppaRao Puli eventSeqNum(1) 3914bbf237fSAppaRao Puli { 3924bbf237fSAppaRao Puli sseConn = std::make_shared<crow::ServerSentEvents>(adaptor); 3934bbf237fSAppaRao Puli } 3944bbf237fSAppaRao Puli 3959f616dd1SEd Tanous ~Subscription() = default; 396b52664e2SAppaRao Puli 397b52664e2SAppaRao Puli void sendEvent(const std::string& msg) 398b52664e2SAppaRao Puli { 399*7adb85acSSunitha Harish if (conn == nullptr) 4004bbf237fSAppaRao Puli { 401*7adb85acSSunitha Harish // create the HttpClient connection 402*7adb85acSSunitha Harish conn = std::make_shared<crow::HttpClient>( 403*7adb85acSSunitha Harish crow::connections::systemBus->get_io_context(), id, host, port, 404*7adb85acSSunitha Harish path, httpHeaders); 405b52664e2SAppaRao Puli } 406b52664e2SAppaRao Puli 407*7adb85acSSunitha Harish conn->sendData(msg); 408*7adb85acSSunitha Harish eventSeqNum++; 409*7adb85acSSunitha Harish 4104bbf237fSAppaRao Puli if (sseConn != nullptr) 4114bbf237fSAppaRao Puli { 4124bbf237fSAppaRao Puli sseConn->sendData(eventSeqNum, msg); 4134bbf237fSAppaRao Puli } 4144bbf237fSAppaRao Puli } 4154bbf237fSAppaRao Puli 4160b4bdd93SAppaRao Puli void sendTestEventLog() 4170b4bdd93SAppaRao Puli { 4180b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 4190b4bdd93SAppaRao Puli logEntryArray.push_back({}); 4200b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 4210b4bdd93SAppaRao Puli 4227c8c4058STejas Patil logEntryJson = { 4237c8c4058STejas Patil {"EventId", "TestID"}, 4240b4bdd93SAppaRao Puli {"EventType", "Event"}, 4250b4bdd93SAppaRao Puli {"Severity", "OK"}, 4260b4bdd93SAppaRao Puli {"Message", "Generated test event"}, 4274a0bf539SManojkiran Eda {"MessageId", "OpenBMC.0.2.TestEventLog"}, 4280b4bdd93SAppaRao Puli {"MessageArgs", nlohmann::json::array()}, 4297c8c4058STejas Patil {"EventTimestamp", crow::utility::getDateTimeOffsetNow().first}, 4300b4bdd93SAppaRao Puli {"Context", customText}}; 4310b4bdd93SAppaRao Puli 4320b4bdd93SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 4330b4bdd93SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 4340b4bdd93SAppaRao Puli {"Name", "Event Log"}, 4350b4bdd93SAppaRao Puli {"Events", logEntryArray}}; 4360b4bdd93SAppaRao Puli 43771f52d96SEd Tanous this->sendEvent( 43871f52d96SEd Tanous msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 4390b4bdd93SAppaRao Puli } 4400b4bdd93SAppaRao Puli 4417f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 4427f4eb588SAppaRao Puli void filterAndSendEventLogs( 4437f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 4447f4eb588SAppaRao Puli { 4457f4eb588SAppaRao Puli nlohmann::json logEntryArray; 4467f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 4477f4eb588SAppaRao Puli { 4487f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 4497f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 4507f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 4517f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 4527f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 4535e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 4547f4eb588SAppaRao Puli 4557f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 4567f4eb588SAppaRao Puli // send everything. 4577f4eb588SAppaRao Puli if (registryPrefixes.size()) 4587f4eb588SAppaRao Puli { 4597f4eb588SAppaRao Puli auto obj = std::find(registryPrefixes.begin(), 4607f4eb588SAppaRao Puli registryPrefixes.end(), registryName); 4617f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 4627f4eb588SAppaRao Puli { 4637f4eb588SAppaRao Puli continue; 4647f4eb588SAppaRao Puli } 4657f4eb588SAppaRao Puli } 4667f4eb588SAppaRao Puli 4677f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 4687f4eb588SAppaRao Puli // send everything. 4697f4eb588SAppaRao Puli if (registryMsgIds.size()) 4707f4eb588SAppaRao Puli { 4717f4eb588SAppaRao Puli auto obj = std::find(registryMsgIds.begin(), 4727f4eb588SAppaRao Puli registryMsgIds.end(), messageKey); 4737f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 4747f4eb588SAppaRao Puli { 4757f4eb588SAppaRao Puli continue; 4767f4eb588SAppaRao Puli } 4777f4eb588SAppaRao Puli } 4787f4eb588SAppaRao Puli 4797f4eb588SAppaRao Puli logEntryArray.push_back({}); 4807f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 4817f4eb588SAppaRao Puli if (event_log::formatEventLogEntry(idStr, messageID, messageArgs, 4827f4eb588SAppaRao Puli timestamp, customText, 4837f4eb588SAppaRao Puli bmcLogEntry) != 0) 4847f4eb588SAppaRao Puli { 4857f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 4867f4eb588SAppaRao Puli continue; 4877f4eb588SAppaRao Puli } 4887f4eb588SAppaRao Puli } 4897f4eb588SAppaRao Puli 4907f4eb588SAppaRao Puli if (logEntryArray.size() < 1) 4917f4eb588SAppaRao Puli { 4927f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 4937f4eb588SAppaRao Puli return; 4947f4eb588SAppaRao Puli } 4957f4eb588SAppaRao Puli 4967f4eb588SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 4977f4eb588SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 4987f4eb588SAppaRao Puli {"Name", "Event Log"}, 4997f4eb588SAppaRao Puli {"Events", logEntryArray}}; 5007f4eb588SAppaRao Puli 50171f52d96SEd Tanous this->sendEvent( 50271f52d96SEd Tanous msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 5037f4eb588SAppaRao Puli } 5047f4eb588SAppaRao Puli #endif 5057f4eb588SAppaRao Puli 50623a21a1cSEd Tanous void filterAndSendReports(const std::string& id2, 507156d6b00SAppaRao Puli const std::string& readingsTs, 508156d6b00SAppaRao Puli const ReadingsObjType& readings) 509156d6b00SAppaRao Puli { 510156d6b00SAppaRao Puli std::string metricReportDef = 51123a21a1cSEd Tanous "/redfish/v1/TelemetryService/MetricReportDefinitions/" + id2; 512156d6b00SAppaRao Puli 513156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 514156d6b00SAppaRao Puli if (metricReportDefinitions.size()) 515156d6b00SAppaRao Puli { 516156d6b00SAppaRao Puli if (std::find(metricReportDefinitions.begin(), 517156d6b00SAppaRao Puli metricReportDefinitions.end(), 518156d6b00SAppaRao Puli metricReportDef) == metricReportDefinitions.end()) 519156d6b00SAppaRao Puli { 520156d6b00SAppaRao Puli return; 521156d6b00SAppaRao Puli } 522156d6b00SAppaRao Puli } 523156d6b00SAppaRao Puli 524156d6b00SAppaRao Puli nlohmann::json metricValuesArray = nlohmann::json::array(); 525156d6b00SAppaRao Puli for (const auto& it : readings) 526156d6b00SAppaRao Puli { 527156d6b00SAppaRao Puli metricValuesArray.push_back({}); 528156d6b00SAppaRao Puli nlohmann::json& entry = metricValuesArray.back(); 529156d6b00SAppaRao Puli 53093f5d7c7SWludzik, Jozef auto& [id, property, value, timestamp] = it; 53193f5d7c7SWludzik, Jozef 53293f5d7c7SWludzik, Jozef entry = {{"MetricId", id}, 53393f5d7c7SWludzik, Jozef {"MetricProperty", property}, 53493f5d7c7SWludzik, Jozef {"MetricValue", std::to_string(value)}, 53593f5d7c7SWludzik, Jozef {"Timestamp", crow::utility::getDateTime(timestamp)}}; 536156d6b00SAppaRao Puli } 537156d6b00SAppaRao Puli 538156d6b00SAppaRao Puli nlohmann::json msg = { 539156d6b00SAppaRao Puli {"@odata.id", "/redfish/v1/TelemetryService/MetricReports/" + id}, 540156d6b00SAppaRao Puli {"@odata.type", "#MetricReport.v1_3_0.MetricReport"}, 54123a21a1cSEd Tanous {"Id", id2}, 54223a21a1cSEd Tanous {"Name", id2}, 543156d6b00SAppaRao Puli {"Timestamp", readingsTs}, 544156d6b00SAppaRao Puli {"MetricReportDefinition", {{"@odata.id", metricReportDef}}}, 545156d6b00SAppaRao Puli {"MetricValues", metricValuesArray}}; 546156d6b00SAppaRao Puli 54771f52d96SEd Tanous this->sendEvent( 54871f52d96SEd Tanous msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); 549156d6b00SAppaRao Puli } 550156d6b00SAppaRao Puli 551fe44eb0bSAyushi Smriti void updateRetryConfig(const uint32_t retryAttempts, 552fe44eb0bSAyushi Smriti const uint32_t retryTimeoutInterval) 553fe44eb0bSAyushi Smriti { 55462de0c68SAppaRao Puli if (conn != nullptr) 55562de0c68SAppaRao Puli { 556fe44eb0bSAyushi Smriti conn->setRetryConfig(retryAttempts, retryTimeoutInterval); 557fe44eb0bSAyushi Smriti } 55862de0c68SAppaRao Puli } 559fe44eb0bSAyushi Smriti 560fe44eb0bSAyushi Smriti void updateRetryPolicy() 561fe44eb0bSAyushi Smriti { 56262de0c68SAppaRao Puli if (conn != nullptr) 56362de0c68SAppaRao Puli { 564fe44eb0bSAyushi Smriti conn->setRetryPolicy(retryPolicy); 565fe44eb0bSAyushi Smriti } 56662de0c68SAppaRao Puli } 567fe44eb0bSAyushi Smriti 56896330b99SSunitha Harish uint64_t getEventSeqNum() 56996330b99SSunitha Harish { 57096330b99SSunitha Harish return eventSeqNum; 57196330b99SSunitha Harish } 57296330b99SSunitha Harish 573b52664e2SAppaRao Puli private: 5740b4bdd93SAppaRao Puli uint64_t eventSeqNum; 575b52664e2SAppaRao Puli std::string host; 576b52664e2SAppaRao Puli std::string port; 577b52664e2SAppaRao Puli std::string path; 578b52664e2SAppaRao Puli std::string uriProto; 5794bbf237fSAppaRao Puli std::shared_ptr<crow::HttpClient> conn = nullptr; 5804bbf237fSAppaRao Puli std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr; 581b52664e2SAppaRao Puli }; 582b52664e2SAppaRao Puli 583b52664e2SAppaRao Puli class EventServiceManager 584b52664e2SAppaRao Puli { 585b52664e2SAppaRao Puli private: 5867d1cc387SAppaRao Puli bool serviceEnabled; 5877d1cc387SAppaRao Puli uint32_t retryAttempts; 5887d1cc387SAppaRao Puli uint32_t retryTimeoutInterval; 5897d1cc387SAppaRao Puli 5909f616dd1SEd Tanous EventServiceManager() 591b52664e2SAppaRao Puli { 5921bf712bcSAyushi Smriti // Load config from persist store. 5931bf712bcSAyushi Smriti initConfig(); 594b52664e2SAppaRao Puli } 595b52664e2SAppaRao Puli 5967f4eb588SAppaRao Puli std::string lastEventTStr; 5979f616dd1SEd Tanous size_t noOfEventLogSubscribers{0}; 5989f616dd1SEd Tanous size_t noOfMetricReportSubscribers{0}; 599156d6b00SAppaRao Puli std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor; 600b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 601b52664e2SAppaRao Puli subscriptionsMap; 602b52664e2SAppaRao Puli 6039f616dd1SEd Tanous uint64_t eventId{1}; 60496330b99SSunitha Harish 605b52664e2SAppaRao Puli public: 6069f616dd1SEd Tanous EventServiceManager(const EventServiceManager&) = delete; 6079f616dd1SEd Tanous EventServiceManager& operator=(const EventServiceManager&) = delete; 6089f616dd1SEd Tanous EventServiceManager(EventServiceManager&&) = delete; 6099f616dd1SEd Tanous EventServiceManager& operator=(EventServiceManager&&) = delete; 6109f616dd1SEd Tanous 611b52664e2SAppaRao Puli static EventServiceManager& getInstance() 612b52664e2SAppaRao Puli { 613b52664e2SAppaRao Puli static EventServiceManager handler; 614b52664e2SAppaRao Puli return handler; 615b52664e2SAppaRao Puli } 616b52664e2SAppaRao Puli 6171bf712bcSAyushi Smriti void initConfig() 6181bf712bcSAyushi Smriti { 61928afb49cSJunLin Chen loadOldBehavior(); 6201bf712bcSAyushi Smriti 62128afb49cSJunLin Chen persistent_data::EventServiceConfig eventServiceConfig = 62228afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 62328afb49cSJunLin Chen .getEventServiceConfig(); 6241bf712bcSAyushi Smriti 62528afb49cSJunLin Chen serviceEnabled = eventServiceConfig.enabled; 62628afb49cSJunLin Chen retryAttempts = eventServiceConfig.retryAttempts; 62728afb49cSJunLin Chen retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval; 6281bf712bcSAyushi Smriti 62928afb49cSJunLin Chen for (const auto& it : persistent_data::EventServiceStore::getInstance() 63028afb49cSJunLin Chen .subscriptionsConfigMap) 6311bf712bcSAyushi Smriti { 63228afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 63328afb49cSJunLin Chen it.second; 6344bbf237fSAppaRao Puli 6351bf712bcSAyushi Smriti std::string host; 6361bf712bcSAyushi Smriti std::string urlProto; 6371bf712bcSAyushi Smriti std::string port; 6381bf712bcSAyushi Smriti std::string path; 63928afb49cSJunLin Chen bool status = validateAndSplitUrl(newSub->destinationUrl, urlProto, 64028afb49cSJunLin Chen host, port, path); 6411bf712bcSAyushi Smriti 6421bf712bcSAyushi Smriti if (!status) 6431bf712bcSAyushi Smriti { 6441bf712bcSAyushi Smriti BMCWEB_LOG_ERROR 6451bf712bcSAyushi Smriti << "Failed to validate and split destination url"; 6461bf712bcSAyushi Smriti continue; 6471bf712bcSAyushi Smriti } 6481bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 6491bf712bcSAyushi Smriti std::make_shared<Subscription>(host, port, path, urlProto); 6501bf712bcSAyushi Smriti 65128afb49cSJunLin Chen subValue->id = newSub->id; 65228afb49cSJunLin Chen subValue->destinationUrl = newSub->destinationUrl; 65328afb49cSJunLin Chen subValue->protocol = newSub->protocol; 65428afb49cSJunLin Chen subValue->retryPolicy = newSub->retryPolicy; 65528afb49cSJunLin Chen subValue->customText = newSub->customText; 65628afb49cSJunLin Chen subValue->eventFormatType = newSub->eventFormatType; 65728afb49cSJunLin Chen subValue->subscriptionType = newSub->subscriptionType; 65828afb49cSJunLin Chen subValue->registryMsgIds = newSub->registryMsgIds; 65928afb49cSJunLin Chen subValue->registryPrefixes = newSub->registryPrefixes; 66028afb49cSJunLin Chen subValue->resourceTypes = newSub->resourceTypes; 66128afb49cSJunLin Chen subValue->httpHeaders = newSub->httpHeaders; 66228afb49cSJunLin Chen subValue->metricReportDefinitions = newSub->metricReportDefinitions; 6631bf712bcSAyushi Smriti 66428afb49cSJunLin Chen if (subValue->id.empty()) 6651bf712bcSAyushi Smriti { 6661bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Failed to add subscription"; 6671bf712bcSAyushi Smriti } 66828afb49cSJunLin Chen subscriptionsMap.insert(std::pair(subValue->id, subValue)); 66928afb49cSJunLin Chen 67028afb49cSJunLin Chen updateNoOfSubscribersCount(); 67128afb49cSJunLin Chen 67228afb49cSJunLin Chen #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 67328afb49cSJunLin Chen if (lastEventTStr.empty()) 67428afb49cSJunLin Chen { 67528afb49cSJunLin Chen cacheLastEventTimestamp(); 67628afb49cSJunLin Chen } 67728afb49cSJunLin Chen #endif 67828afb49cSJunLin Chen // Update retry configuration. 67928afb49cSJunLin Chen subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 68028afb49cSJunLin Chen subValue->updateRetryPolicy(); 6811bf712bcSAyushi Smriti } 6821bf712bcSAyushi Smriti return; 6831bf712bcSAyushi Smriti } 6841bf712bcSAyushi Smriti 68528afb49cSJunLin Chen void loadOldBehavior() 686b52664e2SAppaRao Puli { 68728afb49cSJunLin Chen std::ifstream eventConfigFile(eventServiceFile); 68828afb49cSJunLin Chen if (!eventConfigFile.good()) 6891bf712bcSAyushi Smriti { 69028afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Old eventService config not exist"; 69128afb49cSJunLin Chen return; 69228afb49cSJunLin Chen } 69328afb49cSJunLin Chen auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 69428afb49cSJunLin Chen if (jsonData.is_discarded()) 6954bbf237fSAppaRao Puli { 69628afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Old eventService config parse error."; 69728afb49cSJunLin Chen return; 69828afb49cSJunLin Chen } 69928afb49cSJunLin Chen 70028afb49cSJunLin Chen for (const auto& item : jsonData.items()) 70128afb49cSJunLin Chen { 70228afb49cSJunLin Chen if (item.key() == "Configuration") 70328afb49cSJunLin Chen { 70428afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 70528afb49cSJunLin Chen .getEventServiceConfig() 70628afb49cSJunLin Chen .fromJson(item.value()); 70728afb49cSJunLin Chen } 70828afb49cSJunLin Chen else if (item.key() == "Subscriptions") 70928afb49cSJunLin Chen { 71028afb49cSJunLin Chen for (const auto& elem : item.value()) 71128afb49cSJunLin Chen { 71228afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> 71328afb49cSJunLin Chen newSubscription = 71428afb49cSJunLin Chen persistent_data::UserSubscription::fromJson(elem, 71528afb49cSJunLin Chen true); 71628afb49cSJunLin Chen if (newSubscription == nullptr) 71728afb49cSJunLin Chen { 71828afb49cSJunLin Chen BMCWEB_LOG_ERROR << "Problem reading subscription " 71928afb49cSJunLin Chen "from old persistent store"; 7204bbf237fSAppaRao Puli continue; 7214bbf237fSAppaRao Puli } 7221bf712bcSAyushi Smriti 72328afb49cSJunLin Chen std::uniform_int_distribution<uint32_t> dist(0); 72428afb49cSJunLin Chen bmcweb::OpenSSLGenerator gen; 7251bf712bcSAyushi Smriti 72628afb49cSJunLin Chen std::string id; 7271bf712bcSAyushi Smriti 72828afb49cSJunLin Chen int retry = 3; 72928afb49cSJunLin Chen while (retry) 7301bf712bcSAyushi Smriti { 73128afb49cSJunLin Chen id = std::to_string(dist(gen)); 73228afb49cSJunLin Chen if (gen.error()) 7337d1cc387SAppaRao Puli { 73428afb49cSJunLin Chen retry = 0; 73528afb49cSJunLin Chen break; 73628afb49cSJunLin Chen } 73728afb49cSJunLin Chen newSubscription->id = id; 73828afb49cSJunLin Chen auto inserted = 73928afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 74028afb49cSJunLin Chen .subscriptionsConfigMap.insert( 74128afb49cSJunLin Chen std::pair(id, newSubscription)); 74228afb49cSJunLin Chen if (inserted.second) 74328afb49cSJunLin Chen { 74428afb49cSJunLin Chen break; 74528afb49cSJunLin Chen } 74628afb49cSJunLin Chen --retry; 7477d1cc387SAppaRao Puli } 7487d1cc387SAppaRao Puli 74928afb49cSJunLin Chen if (retry <= 0) 75028afb49cSJunLin Chen { 75128afb49cSJunLin Chen BMCWEB_LOG_ERROR 75228afb49cSJunLin Chen << "Failed to generate random number from old " 75328afb49cSJunLin Chen "persistent store"; 75428afb49cSJunLin Chen continue; 75528afb49cSJunLin Chen } 75628afb49cSJunLin Chen } 75728afb49cSJunLin Chen } 75828afb49cSJunLin Chen 75928afb49cSJunLin Chen persistent_data::getConfig().writeData(); 76028afb49cSJunLin Chen std::remove(eventServiceFile); 76128afb49cSJunLin Chen BMCWEB_LOG_DEBUG << "Remove old eventservice config"; 76228afb49cSJunLin Chen } 76328afb49cSJunLin Chen } 76428afb49cSJunLin Chen 76528afb49cSJunLin Chen void updateSubscriptionData() 76628afb49cSJunLin Chen { 76728afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 76828afb49cSJunLin Chen .eventServiceConfig.enabled = serviceEnabled; 76928afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 77028afb49cSJunLin Chen .eventServiceConfig.retryAttempts = retryAttempts; 77128afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 77228afb49cSJunLin Chen .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval; 77328afb49cSJunLin Chen 77428afb49cSJunLin Chen persistent_data::getConfig().writeData(); 77528afb49cSJunLin Chen } 77628afb49cSJunLin Chen 77728afb49cSJunLin Chen void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg) 7787d1cc387SAppaRao Puli { 7797d1cc387SAppaRao Puli bool updateConfig = false; 780fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 7817d1cc387SAppaRao Puli 78228afb49cSJunLin Chen if (serviceEnabled != cfg.enabled) 7837d1cc387SAppaRao Puli { 78428afb49cSJunLin Chen serviceEnabled = cfg.enabled; 7857d1cc387SAppaRao Puli if (serviceEnabled && noOfMetricReportSubscribers) 7867d1cc387SAppaRao Puli { 7877d1cc387SAppaRao Puli registerMetricReportSignal(); 7887d1cc387SAppaRao Puli } 7897d1cc387SAppaRao Puli else 7907d1cc387SAppaRao Puli { 7917d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7927d1cc387SAppaRao Puli } 7937d1cc387SAppaRao Puli updateConfig = true; 7947d1cc387SAppaRao Puli } 7957d1cc387SAppaRao Puli 79628afb49cSJunLin Chen if (retryAttempts != cfg.retryAttempts) 7977d1cc387SAppaRao Puli { 79828afb49cSJunLin Chen retryAttempts = cfg.retryAttempts; 7997d1cc387SAppaRao Puli updateConfig = true; 800fe44eb0bSAyushi Smriti updateRetryCfg = true; 8017d1cc387SAppaRao Puli } 8027d1cc387SAppaRao Puli 80328afb49cSJunLin Chen if (retryTimeoutInterval != cfg.retryTimeoutInterval) 8047d1cc387SAppaRao Puli { 80528afb49cSJunLin Chen retryTimeoutInterval = cfg.retryTimeoutInterval; 8067d1cc387SAppaRao Puli updateConfig = true; 807fe44eb0bSAyushi Smriti updateRetryCfg = true; 8087d1cc387SAppaRao Puli } 8097d1cc387SAppaRao Puli 8107d1cc387SAppaRao Puli if (updateConfig) 8117d1cc387SAppaRao Puli { 8127d1cc387SAppaRao Puli updateSubscriptionData(); 8137d1cc387SAppaRao Puli } 814fe44eb0bSAyushi Smriti 815fe44eb0bSAyushi Smriti if (updateRetryCfg) 816fe44eb0bSAyushi Smriti { 817fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 818fe44eb0bSAyushi Smriti for (const auto& it : 819fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 820fe44eb0bSAyushi Smriti { 821fe44eb0bSAyushi Smriti std::shared_ptr<Subscription> entry = it.second; 822fe44eb0bSAyushi Smriti entry->updateRetryConfig(retryAttempts, retryTimeoutInterval); 823fe44eb0bSAyushi Smriti } 824fe44eb0bSAyushi Smriti } 8257d1cc387SAppaRao Puli } 8267d1cc387SAppaRao Puli 8277d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 8287d1cc387SAppaRao Puli { 8297d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 8307d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 8317d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 8327d1cc387SAppaRao Puli { 8337d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8347d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 8357d1cc387SAppaRao Puli { 8367d1cc387SAppaRao Puli eventLogSubCount++; 8377d1cc387SAppaRao Puli } 8387d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 8397d1cc387SAppaRao Puli { 8407d1cc387SAppaRao Puli metricReportSubCount++; 8417d1cc387SAppaRao Puli } 8427d1cc387SAppaRao Puli } 8437d1cc387SAppaRao Puli 8447d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 8457d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 8467d1cc387SAppaRao Puli { 8477d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 8487d1cc387SAppaRao Puli if (noOfMetricReportSubscribers) 8497d1cc387SAppaRao Puli { 8507d1cc387SAppaRao Puli registerMetricReportSignal(); 8517d1cc387SAppaRao Puli } 8527d1cc387SAppaRao Puli else 8537d1cc387SAppaRao Puli { 8547d1cc387SAppaRao Puli unregisterMetricReportSignal(); 8557d1cc387SAppaRao Puli } 8567d1cc387SAppaRao Puli } 8577d1cc387SAppaRao Puli } 8587d1cc387SAppaRao Puli 859b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 860b52664e2SAppaRao Puli { 861b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 862b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 863b52664e2SAppaRao Puli { 864b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 865b52664e2SAppaRao Puli return nullptr; 866b52664e2SAppaRao Puli } 867b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 868b52664e2SAppaRao Puli return subValue; 869b52664e2SAppaRao Puli } 870b52664e2SAppaRao Puli 871b5a76932SEd Tanous std::string addSubscription(const std::shared_ptr<Subscription>& subValue, 8721bf712bcSAyushi Smriti const bool updateFile = true) 873b52664e2SAppaRao Puli { 874fc76b8acSEd Tanous 875fc76b8acSEd Tanous std::uniform_int_distribution<uint32_t> dist(0); 876fc76b8acSEd Tanous bmcweb::OpenSSLGenerator gen; 877fc76b8acSEd Tanous 878b52664e2SAppaRao Puli std::string id; 879b52664e2SAppaRao Puli 880b52664e2SAppaRao Puli int retry = 3; 881b52664e2SAppaRao Puli while (retry) 882b52664e2SAppaRao Puli { 883fc76b8acSEd Tanous id = std::to_string(dist(gen)); 884fc76b8acSEd Tanous if (gen.error()) 885fc76b8acSEd Tanous { 886fc76b8acSEd Tanous retry = 0; 887fc76b8acSEd Tanous break; 888fc76b8acSEd Tanous } 889b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 890b52664e2SAppaRao Puli if (inserted.second) 891b52664e2SAppaRao Puli { 892b52664e2SAppaRao Puli break; 893b52664e2SAppaRao Puli } 894b52664e2SAppaRao Puli --retry; 89523a21a1cSEd Tanous } 896b52664e2SAppaRao Puli 897b52664e2SAppaRao Puli if (retry <= 0) 898b52664e2SAppaRao Puli { 899b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Failed to generate random number"; 900abb93cddSEd Tanous return ""; 901b52664e2SAppaRao Puli } 902b52664e2SAppaRao Puli 90328afb49cSJunLin Chen std::shared_ptr<persistent_data::UserSubscription> newSub = 90428afb49cSJunLin Chen std::make_shared<persistent_data::UserSubscription>(); 90528afb49cSJunLin Chen newSub->id = id; 90628afb49cSJunLin Chen newSub->destinationUrl = subValue->destinationUrl; 90728afb49cSJunLin Chen newSub->protocol = subValue->protocol; 90828afb49cSJunLin Chen newSub->retryPolicy = subValue->retryPolicy; 90928afb49cSJunLin Chen newSub->customText = subValue->customText; 91028afb49cSJunLin Chen newSub->eventFormatType = subValue->eventFormatType; 91128afb49cSJunLin Chen newSub->subscriptionType = subValue->subscriptionType; 91228afb49cSJunLin Chen newSub->registryMsgIds = subValue->registryMsgIds; 91328afb49cSJunLin Chen newSub->registryPrefixes = subValue->registryPrefixes; 91428afb49cSJunLin Chen newSub->resourceTypes = subValue->resourceTypes; 91528afb49cSJunLin Chen newSub->httpHeaders = subValue->httpHeaders; 91628afb49cSJunLin Chen newSub->metricReportDefinitions = subValue->metricReportDefinitions; 91728afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 91828afb49cSJunLin Chen .subscriptionsConfigMap.emplace(newSub->id, newSub); 91928afb49cSJunLin Chen 9207d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 9211bf712bcSAyushi Smriti 9221bf712bcSAyushi Smriti if (updateFile) 9231bf712bcSAyushi Smriti { 924b52664e2SAppaRao Puli updateSubscriptionData(); 9251bf712bcSAyushi Smriti } 9267f4eb588SAppaRao Puli 9277f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 9287f4eb588SAppaRao Puli if (lastEventTStr.empty()) 9297f4eb588SAppaRao Puli { 9307f4eb588SAppaRao Puli cacheLastEventTimestamp(); 9317f4eb588SAppaRao Puli } 9327f4eb588SAppaRao Puli #endif 933fe44eb0bSAyushi Smriti // Update retry configuration. 934fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 935fe44eb0bSAyushi Smriti subValue->updateRetryPolicy(); 936fe44eb0bSAyushi Smriti 937b52664e2SAppaRao Puli return id; 938b52664e2SAppaRao Puli } 939b52664e2SAppaRao Puli 940b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 941b52664e2SAppaRao Puli { 942b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 943b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 944b52664e2SAppaRao Puli { 945b52664e2SAppaRao Puli return false; 946b52664e2SAppaRao Puli } 947b52664e2SAppaRao Puli return true; 948b52664e2SAppaRao Puli } 949b52664e2SAppaRao Puli 950b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 951b52664e2SAppaRao Puli { 952b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 953b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 954b52664e2SAppaRao Puli { 955b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 95628afb49cSJunLin Chen auto obj2 = persistent_data::EventServiceStore::getInstance() 95728afb49cSJunLin Chen .subscriptionsConfigMap.find(id); 95828afb49cSJunLin Chen persistent_data::EventServiceStore::getInstance() 95928afb49cSJunLin Chen .subscriptionsConfigMap.erase(obj2); 9607d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 961b52664e2SAppaRao Puli updateSubscriptionData(); 962b52664e2SAppaRao Puli } 963b52664e2SAppaRao Puli } 964b52664e2SAppaRao Puli 965b52664e2SAppaRao Puli size_t getNumberOfSubscriptions() 966b52664e2SAppaRao Puli { 967b52664e2SAppaRao Puli return subscriptionsMap.size(); 968b52664e2SAppaRao Puli } 969b52664e2SAppaRao Puli 970b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 971b52664e2SAppaRao Puli { 972b52664e2SAppaRao Puli std::vector<std::string> idList; 973b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 974b52664e2SAppaRao Puli { 975b52664e2SAppaRao Puli idList.emplace_back(it.first); 976b52664e2SAppaRao Puli } 977b52664e2SAppaRao Puli return idList; 978b52664e2SAppaRao Puli } 979b52664e2SAppaRao Puli 980b52664e2SAppaRao Puli bool isDestinationExist(const std::string& destUrl) 981b52664e2SAppaRao Puli { 982b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 983b52664e2SAppaRao Puli { 984b52664e2SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 985b52664e2SAppaRao Puli if (entry->destinationUrl == destUrl) 986b52664e2SAppaRao Puli { 987b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 988b52664e2SAppaRao Puli return true; 989b52664e2SAppaRao Puli } 990b52664e2SAppaRao Puli } 991b52664e2SAppaRao Puli return false; 992b52664e2SAppaRao Puli } 9930b4bdd93SAppaRao Puli 9940b4bdd93SAppaRao Puli void sendTestEventLog() 9950b4bdd93SAppaRao Puli { 9960b4bdd93SAppaRao Puli for (const auto& it : this->subscriptionsMap) 9970b4bdd93SAppaRao Puli { 9980b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 9990b4bdd93SAppaRao Puli entry->sendTestEventLog(); 10000b4bdd93SAppaRao Puli } 10010b4bdd93SAppaRao Puli } 1002e9a14131SAppaRao Puli 100396330b99SSunitha Harish void sendEvent(const nlohmann::json& eventMessageIn, 100496330b99SSunitha Harish const std::string& origin, const std::string& resType) 100596330b99SSunitha Harish { 100696330b99SSunitha Harish nlohmann::json eventRecord = nlohmann::json::array(); 100796330b99SSunitha Harish nlohmann::json eventMessage = eventMessageIn; 100896330b99SSunitha Harish // MemberId is 0 : since we are sending one event record. 100996330b99SSunitha Harish uint64_t memberId = 0; 101096330b99SSunitha Harish 101196330b99SSunitha Harish nlohmann::json event = { 101296330b99SSunitha Harish {"EventId", eventId}, 101396330b99SSunitha Harish {"MemberId", memberId}, 10147c8c4058STejas Patil {"EventTimestamp", crow::utility::getDateTimeOffsetNow().first}, 101596330b99SSunitha Harish {"OriginOfCondition", origin}}; 101696330b99SSunitha Harish for (nlohmann::json::iterator it = event.begin(); it != event.end(); 101796330b99SSunitha Harish ++it) 101896330b99SSunitha Harish { 101996330b99SSunitha Harish eventMessage[it.key()] = it.value(); 102096330b99SSunitha Harish } 102196330b99SSunitha Harish eventRecord.push_back(eventMessage); 102296330b99SSunitha Harish 102396330b99SSunitha Harish for (const auto& it : this->subscriptionsMap) 102496330b99SSunitha Harish { 102596330b99SSunitha Harish std::shared_ptr<Subscription> entry = it.second; 102696330b99SSunitha Harish bool isSubscribed = false; 102796330b99SSunitha Harish // Search the resourceTypes list for the subscription. 102896330b99SSunitha Harish // If resourceTypes list is empty, don't filter events 102996330b99SSunitha Harish // send everything. 103096330b99SSunitha Harish if (entry->resourceTypes.size()) 103196330b99SSunitha Harish { 10323174e4dfSEd Tanous for (const auto& resource : entry->resourceTypes) 103396330b99SSunitha Harish { 103496330b99SSunitha Harish if (resType == resource) 103596330b99SSunitha Harish { 103696330b99SSunitha Harish BMCWEB_LOG_INFO << "ResourceType " << resource 103796330b99SSunitha Harish << " found in the subscribed list"; 103896330b99SSunitha Harish isSubscribed = true; 103996330b99SSunitha Harish break; 104096330b99SSunitha Harish } 104196330b99SSunitha Harish } 104296330b99SSunitha Harish } 104396330b99SSunitha Harish else // resourceTypes list is empty. 104496330b99SSunitha Harish { 104596330b99SSunitha Harish isSubscribed = true; 104696330b99SSunitha Harish } 104796330b99SSunitha Harish if (isSubscribed) 104896330b99SSunitha Harish { 104996330b99SSunitha Harish nlohmann::json msgJson = { 105096330b99SSunitha Harish {"@odata.type", "#Event.v1_4_0.Event"}, 105196330b99SSunitha Harish {"Name", "Event Log"}, 105296330b99SSunitha Harish {"Id", eventId}, 105396330b99SSunitha Harish {"Events", eventRecord}}; 105471f52d96SEd Tanous entry->sendEvent(msgJson.dump( 105571f52d96SEd Tanous 2, ' ', true, nlohmann::json::error_handler_t::replace)); 105696330b99SSunitha Harish eventId++; // increament the eventId 105796330b99SSunitha Harish } 105896330b99SSunitha Harish else 105996330b99SSunitha Harish { 106096330b99SSunitha Harish BMCWEB_LOG_INFO << "Not subscribed to this resource"; 106196330b99SSunitha Harish } 106296330b99SSunitha Harish } 106396330b99SSunitha Harish } 10645738de59SAsmitha Karunanithi void sendBroadcastMsg(const std::string& broadcastMsg) 10655738de59SAsmitha Karunanithi { 10665738de59SAsmitha Karunanithi for (const auto& it : this->subscriptionsMap) 10675738de59SAsmitha Karunanithi { 10685738de59SAsmitha Karunanithi std::shared_ptr<Subscription> entry = it.second; 10695738de59SAsmitha Karunanithi nlohmann::json msgJson = { 10707c8c4058STejas Patil {"Timestamp", crow::utility::getDateTimeOffsetNow().first}, 10715738de59SAsmitha Karunanithi {"OriginOfCondition", "/ibm/v1/HMC/BroadcastService"}, 10725738de59SAsmitha Karunanithi {"Name", "Broadcast Message"}, 10735738de59SAsmitha Karunanithi {"Message", broadcastMsg}}; 107471f52d96SEd Tanous entry->sendEvent(msgJson.dump( 107571f52d96SEd Tanous 2, ' ', true, nlohmann::json::error_handler_t::replace)); 10765738de59SAsmitha Karunanithi } 10775738de59SAsmitha Karunanithi } 107896330b99SSunitha Harish 10797f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 10807f4eb588SAppaRao Puli void cacheLastEventTimestamp() 10817f4eb588SAppaRao Puli { 1082016761afSAppaRao Puli lastEventTStr.clear(); 10837f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 10847f4eb588SAppaRao Puli if (!logStream.good()) 10857f4eb588SAppaRao Puli { 10867f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 10877f4eb588SAppaRao Puli return; 10887f4eb588SAppaRao Puli } 10897f4eb588SAppaRao Puli std::string logEntry; 10907f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 10917f4eb588SAppaRao Puli { 1092f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 10937f4eb588SAppaRao Puli if (space == std::string::npos) 10947f4eb588SAppaRao Puli { 10957f4eb588SAppaRao Puli // Shouldn't enter here but lets skip it. 10967f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid log entry found."; 10977f4eb588SAppaRao Puli continue; 10987f4eb588SAppaRao Puli } 10997f4eb588SAppaRao Puli lastEventTStr = logEntry.substr(0, space); 11007f4eb588SAppaRao Puli } 11017f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Last Event time stamp set: " << lastEventTStr; 11027f4eb588SAppaRao Puli } 11037f4eb588SAppaRao Puli 11047f4eb588SAppaRao Puli void readEventLogsFromFile() 11057f4eb588SAppaRao Puli { 11067f4eb588SAppaRao Puli if (!serviceEnabled || !noOfEventLogSubscribers) 11077f4eb588SAppaRao Puli { 11087f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 11097f4eb588SAppaRao Puli return; 11107f4eb588SAppaRao Puli } 11117f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 11127f4eb588SAppaRao Puli if (!logStream.good()) 11137f4eb588SAppaRao Puli { 11147f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed"; 11157f4eb588SAppaRao Puli return; 11167f4eb588SAppaRao Puli } 11177f4eb588SAppaRao Puli 11187f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 11197f4eb588SAppaRao Puli 11207f4eb588SAppaRao Puli bool startLogCollection = false; 11217f4eb588SAppaRao Puli bool firstEntry = true; 11227f4eb588SAppaRao Puli 11237f4eb588SAppaRao Puli std::string logEntry; 11247f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 11257f4eb588SAppaRao Puli { 1126016761afSAppaRao Puli if (!startLogCollection && !lastEventTStr.empty()) 11277f4eb588SAppaRao Puli { 11287f4eb588SAppaRao Puli if (boost::starts_with(logEntry, lastEventTStr)) 11297f4eb588SAppaRao Puli { 11307f4eb588SAppaRao Puli startLogCollection = true; 11317f4eb588SAppaRao Puli } 11327f4eb588SAppaRao Puli continue; 11337f4eb588SAppaRao Puli } 11347f4eb588SAppaRao Puli 11357f4eb588SAppaRao Puli std::string idStr; 11367f4eb588SAppaRao Puli if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry)) 11377f4eb588SAppaRao Puli { 11387f4eb588SAppaRao Puli continue; 11397f4eb588SAppaRao Puli } 11407f4eb588SAppaRao Puli firstEntry = false; 11417f4eb588SAppaRao Puli 11427f4eb588SAppaRao Puli std::string timestamp; 11437f4eb588SAppaRao Puli std::string messageID; 11445e715de6SAppaRao Puli std::vector<std::string> messageArgs; 11457f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 11467f4eb588SAppaRao Puli messageArgs) != 0) 11477f4eb588SAppaRao Puli { 11487f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 11497f4eb588SAppaRao Puli continue; 11507f4eb588SAppaRao Puli } 11517f4eb588SAppaRao Puli 11527f4eb588SAppaRao Puli std::string registryName; 11537f4eb588SAppaRao Puli std::string messageKey; 11547f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 11557f4eb588SAppaRao Puli messageKey); 11567f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 11577f4eb588SAppaRao Puli { 11587f4eb588SAppaRao Puli continue; 11597f4eb588SAppaRao Puli } 11607f4eb588SAppaRao Puli 11617f4eb588SAppaRao Puli lastEventTStr = timestamp; 11627f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 11637f4eb588SAppaRao Puli messageKey, messageArgs); 11647f4eb588SAppaRao Puli } 11657f4eb588SAppaRao Puli 11667f4eb588SAppaRao Puli for (const auto& it : this->subscriptionsMap) 11677f4eb588SAppaRao Puli { 11687f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 11697f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 11707f4eb588SAppaRao Puli { 11717f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 11727f4eb588SAppaRao Puli } 11737f4eb588SAppaRao Puli } 11747f4eb588SAppaRao Puli } 11757f4eb588SAppaRao Puli 11767f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 11777f4eb588SAppaRao Puli { 11786a9f85f9SAppaRao Puli if (!inotifyConn) 11797f4eb588SAppaRao Puli { 11807f4eb588SAppaRao Puli return; 11817f4eb588SAppaRao Puli } 11827f4eb588SAppaRao Puli 11837f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 11847f4eb588SAppaRao Puli 11857f4eb588SAppaRao Puli inotifyConn->async_read_some( 11867f4eb588SAppaRao Puli boost::asio::buffer(readBuffer), 11877f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 11887f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 11897f4eb588SAppaRao Puli if (ec) 11907f4eb588SAppaRao Puli { 11917f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 11927f4eb588SAppaRao Puli return; 11937f4eb588SAppaRao Puli } 11947f4eb588SAppaRao Puli std::size_t index = 0; 1195b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 11967f4eb588SAppaRao Puli { 11977f4eb588SAppaRao Puli struct inotify_event event; 1198b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1199b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1200b792cc56SAppaRao Puli { 1201b792cc56SAppaRao Puli if ((event.len == 0) || 1202b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1203b792cc56SAppaRao Puli { 1204b792cc56SAppaRao Puli index += (iEventSize + event.len); 1205b792cc56SAppaRao Puli continue; 1206b792cc56SAppaRao Puli } 1207b792cc56SAppaRao Puli 1208b792cc56SAppaRao Puli std::string fileName(&readBuffer[index + iEventSize], 1209b792cc56SAppaRao Puli event.len); 1210b792cc56SAppaRao Puli if (std::strcmp(fileName.c_str(), "redfish") != 0) 1211b792cc56SAppaRao Puli { 1212b792cc56SAppaRao Puli index += (iEventSize + event.len); 1213b792cc56SAppaRao Puli continue; 1214b792cc56SAppaRao Puli } 1215b792cc56SAppaRao Puli 1216b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1217b792cc56SAppaRao Puli << "Redfish log file created/deleted. event.name: " 1218b792cc56SAppaRao Puli << fileName; 1219b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1220b792cc56SAppaRao Puli { 1221b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1222b792cc56SAppaRao Puli { 1223b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1224016761afSAppaRao Puli << "Remove and Add inotify watcher on " 1225016761afSAppaRao Puli "redfish event log file"; 1226016761afSAppaRao Puli // Remove existing inotify watcher and add 1227016761afSAppaRao Puli // with new redfish event log file. 1228016761afSAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1229016761afSAppaRao Puli fileWatchDesc = -1; 1230b792cc56SAppaRao Puli } 1231b792cc56SAppaRao Puli 1232b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1233b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1234b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1235b792cc56SAppaRao Puli { 1236b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1237b792cc56SAppaRao Puli << "inotify_add_watch failed for " 1238b792cc56SAppaRao Puli "redfish log file."; 1239b792cc56SAppaRao Puli return; 1240b792cc56SAppaRao Puli } 1241b792cc56SAppaRao Puli 1242b792cc56SAppaRao Puli EventServiceManager::getInstance() 1243b792cc56SAppaRao Puli .cacheLastEventTimestamp(); 1244b792cc56SAppaRao Puli EventServiceManager::getInstance() 1245b792cc56SAppaRao Puli .readEventLogsFromFile(); 1246b792cc56SAppaRao Puli } 1247b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1248b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1249b792cc56SAppaRao Puli { 1250b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1251b792cc56SAppaRao Puli { 1252b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1253b792cc56SAppaRao Puli fileWatchDesc = -1; 1254b792cc56SAppaRao Puli } 1255b792cc56SAppaRao Puli } 1256b792cc56SAppaRao Puli } 1257b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1258b792cc56SAppaRao Puli { 1259b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 12607f4eb588SAppaRao Puli { 12617f4eb588SAppaRao Puli EventServiceManager::getInstance() 12627f4eb588SAppaRao Puli .readEventLogsFromFile(); 12637f4eb588SAppaRao Puli } 1264b792cc56SAppaRao Puli } 1265b792cc56SAppaRao Puli index += (iEventSize + event.len); 12667f4eb588SAppaRao Puli } 12677f4eb588SAppaRao Puli 12687f4eb588SAppaRao Puli watchRedfishEventLogFile(); 12697f4eb588SAppaRao Puli }); 12707f4eb588SAppaRao Puli } 12717f4eb588SAppaRao Puli 12727f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 12737f4eb588SAppaRao Puli { 127423a21a1cSEd Tanous inotifyConn.emplace(ioc); 1275b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1276b792cc56SAppaRao Puli if (inotifyFd == -1) 12777f4eb588SAppaRao Puli { 12787f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "inotify_init1 failed."; 12797f4eb588SAppaRao Puli return -1; 12807f4eb588SAppaRao Puli } 1281b792cc56SAppaRao Puli 1282b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1283b792cc56SAppaRao Puli // create/delete. 1284b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1285b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1286b792cc56SAppaRao Puli if (dirWatchDesc == -1) 12877f4eb588SAppaRao Puli { 12887f4eb588SAppaRao Puli BMCWEB_LOG_ERROR 1289b792cc56SAppaRao Puli << "inotify_add_watch failed for event log directory."; 12907f4eb588SAppaRao Puli return -1; 12917f4eb588SAppaRao Puli } 12927f4eb588SAppaRao Puli 1293b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 1294b792cc56SAppaRao Puli fileWatchDesc = 1295b792cc56SAppaRao Puli inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY); 1296b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1297b792cc56SAppaRao Puli { 1298b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1299b792cc56SAppaRao Puli << "inotify_add_watch failed for redfish log file."; 1300b792cc56SAppaRao Puli // Don't return error if file not exist. 1301b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1302b792cc56SAppaRao Puli } 1303b792cc56SAppaRao Puli 13047f4eb588SAppaRao Puli // monitor redfish event log file 1305b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 13067f4eb588SAppaRao Puli watchRedfishEventLogFile(); 13077f4eb588SAppaRao Puli 13087f4eb588SAppaRao Puli return 0; 13097f4eb588SAppaRao Puli } 13107f4eb588SAppaRao Puli 13117f4eb588SAppaRao Puli #endif 13127f4eb588SAppaRao Puli 1313156d6b00SAppaRao Puli void getMetricReading(const std::string& service, 1314156d6b00SAppaRao Puli const std::string& objPath, const std::string& intf) 1315156d6b00SAppaRao Puli { 1316f23b7296SEd Tanous std::size_t found = objPath.find_last_of('/'); 1317156d6b00SAppaRao Puli if (found == std::string::npos) 1318156d6b00SAppaRao Puli { 1319156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid objPath received"; 1320156d6b00SAppaRao Puli return; 1321156d6b00SAppaRao Puli } 1322156d6b00SAppaRao Puli 1323156d6b00SAppaRao Puli std::string idStr = objPath.substr(found + 1); 1324156d6b00SAppaRao Puli if (idStr.empty()) 1325156d6b00SAppaRao Puli { 1326156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid ID in objPath"; 1327156d6b00SAppaRao Puli return; 1328156d6b00SAppaRao Puli } 1329156d6b00SAppaRao Puli 1330156d6b00SAppaRao Puli crow::connections::systemBus->async_method_call( 1331156d6b00SAppaRao Puli [idStr{std::move(idStr)}]( 1332156d6b00SAppaRao Puli const boost::system::error_code ec, 1333156d6b00SAppaRao Puli boost::container::flat_map< 133493f5d7c7SWludzik, Jozef std::string, std::variant<int32_t, ReadingsObjType>>& 1335156d6b00SAppaRao Puli resp) { 1336156d6b00SAppaRao Puli if (ec) 1337156d6b00SAppaRao Puli { 1338156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG 1339156d6b00SAppaRao Puli << "D-Bus call failed to GetAll metric readings."; 1340156d6b00SAppaRao Puli return; 1341156d6b00SAppaRao Puli } 1342156d6b00SAppaRao Puli 134393f5d7c7SWludzik, Jozef const int32_t* timestampPtr = 134493f5d7c7SWludzik, Jozef std::get_if<int32_t>(&resp["Timestamp"]); 1345156d6b00SAppaRao Puli if (!timestampPtr) 1346156d6b00SAppaRao Puli { 1347156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Failed to Get timestamp."; 1348156d6b00SAppaRao Puli return; 1349156d6b00SAppaRao Puli } 1350156d6b00SAppaRao Puli 1351156d6b00SAppaRao Puli ReadingsObjType* readingsPtr = 1352156d6b00SAppaRao Puli std::get_if<ReadingsObjType>(&resp["Readings"]); 1353156d6b00SAppaRao Puli if (!readingsPtr) 1354156d6b00SAppaRao Puli { 1355156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Failed to Get Readings property."; 1356156d6b00SAppaRao Puli return; 1357156d6b00SAppaRao Puli } 1358156d6b00SAppaRao Puli 1359156d6b00SAppaRao Puli if (!readingsPtr->size()) 1360156d6b00SAppaRao Puli { 1361156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "No metrics report to be transferred"; 1362156d6b00SAppaRao Puli return; 1363156d6b00SAppaRao Puli } 1364156d6b00SAppaRao Puli 1365156d6b00SAppaRao Puli for (const auto& it : 1366156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1367156d6b00SAppaRao Puli { 1368156d6b00SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 1369156d6b00SAppaRao Puli if (entry->eventFormatType == metricReportFormatType) 1370156d6b00SAppaRao Puli { 137193f5d7c7SWludzik, Jozef entry->filterAndSendReports( 137293f5d7c7SWludzik, Jozef idStr, crow::utility::getDateTime(*timestampPtr), 1373156d6b00SAppaRao Puli *readingsPtr); 1374156d6b00SAppaRao Puli } 1375156d6b00SAppaRao Puli } 1376156d6b00SAppaRao Puli }, 1377156d6b00SAppaRao Puli service, objPath, "org.freedesktop.DBus.Properties", "GetAll", 1378156d6b00SAppaRao Puli intf); 1379156d6b00SAppaRao Puli } 1380156d6b00SAppaRao Puli 1381156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1382156d6b00SAppaRao Puli { 13837d1cc387SAppaRao Puli if (matchTelemetryMonitor) 13847d1cc387SAppaRao Puli { 1385156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1386156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1387156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1388156d6b00SAppaRao Puli } 13897d1cc387SAppaRao Puli } 1390156d6b00SAppaRao Puli 1391156d6b00SAppaRao Puli void registerMetricReportSignal() 1392156d6b00SAppaRao Puli { 13937d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1394156d6b00SAppaRao Puli { 13957d1cc387SAppaRao Puli BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1396156d6b00SAppaRao Puli return; 1397156d6b00SAppaRao Puli } 1398156d6b00SAppaRao Puli 1399156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1400156d6b00SAppaRao Puli std::string matchStr( 1401156d6b00SAppaRao Puli "type='signal',member='ReportUpdate', " 1402156d6b00SAppaRao Puli "interface='xyz.openbmc_project.MonitoringService.Report'"); 1403156d6b00SAppaRao Puli 1404156d6b00SAppaRao Puli matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>( 1405156d6b00SAppaRao Puli *crow::connections::systemBus, matchStr, 1406156d6b00SAppaRao Puli [this](sdbusplus::message::message& msg) { 1407156d6b00SAppaRao Puli if (msg.is_method_error()) 1408156d6b00SAppaRao Puli { 1409156d6b00SAppaRao Puli BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 1410156d6b00SAppaRao Puli return; 1411156d6b00SAppaRao Puli } 1412156d6b00SAppaRao Puli 1413156d6b00SAppaRao Puli std::string service = msg.get_sender(); 1414156d6b00SAppaRao Puli std::string objPath = msg.get_path(); 1415156d6b00SAppaRao Puli std::string intf = msg.get_interface(); 1416156d6b00SAppaRao Puli getMetricReading(service, objPath, intf); 1417156d6b00SAppaRao Puli }); 1418156d6b00SAppaRao Puli } 14191bf712bcSAyushi Smriti 14201bf712bcSAyushi Smriti bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto, 14211bf712bcSAyushi Smriti std::string& host, std::string& port, 14221bf712bcSAyushi Smriti std::string& path) 14231bf712bcSAyushi Smriti { 14241bf712bcSAyushi Smriti // Validate URL using regex expression 14251bf712bcSAyushi Smriti // Format: <protocol>://<host>:<port>/<path> 14261bf712bcSAyushi Smriti // protocol: http/https 14271bf712bcSAyushi Smriti const std::regex urlRegex( 14281bf712bcSAyushi Smriti "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/" 14291bf712bcSAyushi Smriti "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)"); 14301bf712bcSAyushi Smriti std::cmatch match; 14311bf712bcSAyushi Smriti if (!std::regex_match(destUrl.c_str(), match, urlRegex)) 14321bf712bcSAyushi Smriti { 14331bf712bcSAyushi Smriti BMCWEB_LOG_INFO << "Dest. url did not match "; 14341bf712bcSAyushi Smriti return false; 14351bf712bcSAyushi Smriti } 14361bf712bcSAyushi Smriti 14371bf712bcSAyushi Smriti urlProto = std::string(match[1].first, match[1].second); 14381bf712bcSAyushi Smriti if (urlProto == "http") 14391bf712bcSAyushi Smriti { 14401bf712bcSAyushi Smriti #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING 14411bf712bcSAyushi Smriti return false; 14421bf712bcSAyushi Smriti #endif 14431bf712bcSAyushi Smriti } 14441bf712bcSAyushi Smriti 14451bf712bcSAyushi Smriti host = std::string(match[2].first, match[2].second); 14461bf712bcSAyushi Smriti port = std::string(match[3].first, match[3].second); 14471bf712bcSAyushi Smriti path = std::string(match[4].first, match[4].second); 14481bf712bcSAyushi Smriti if (port.empty()) 14491bf712bcSAyushi Smriti { 14501bf712bcSAyushi Smriti if (urlProto == "http") 14511bf712bcSAyushi Smriti { 14521bf712bcSAyushi Smriti port = "80"; 14531bf712bcSAyushi Smriti } 14541bf712bcSAyushi Smriti else 14551bf712bcSAyushi Smriti { 14561bf712bcSAyushi Smriti port = "443"; 14571bf712bcSAyushi Smriti } 14581bf712bcSAyushi Smriti } 14591bf712bcSAyushi Smriti if (path.empty()) 14601bf712bcSAyushi Smriti { 14611bf712bcSAyushi Smriti path = "/"; 14621bf712bcSAyushi Smriti } 14631bf712bcSAyushi Smriti return true; 14641bf712bcSAyushi Smriti } 146523a21a1cSEd Tanous }; 1466b52664e2SAppaRao Puli 1467b52664e2SAppaRao Puli } // namespace redfish 1468