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" 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> 271214b7e7SGunnar Mills #include <http_client.hpp> 281214b7e7SGunnar Mills #include <utils/json_utils.hpp> 291214b7e7SGunnar Mills 30b52664e2SAppaRao Puli #include <cstdlib> 31b52664e2SAppaRao Puli #include <ctime> 321bf712bcSAyushi Smriti #include <fstream> 33b52664e2SAppaRao Puli #include <memory> 34b52664e2SAppaRao Puli #include <variant> 35b52664e2SAppaRao Puli 36b52664e2SAppaRao Puli namespace redfish 37b52664e2SAppaRao Puli { 38156d6b00SAppaRao Puli 39156d6b00SAppaRao Puli using ReadingsObjType = 40156d6b00SAppaRao Puli std::vector<std::tuple<std::string, std::string, double, std::string>>; 417d1cc387SAppaRao Puli using EventServiceConfig = std::tuple<bool, uint32_t, uint32_t>; 42156d6b00SAppaRao Puli 43156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event"; 44156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport"; 45156d6b00SAppaRao Puli 461bf712bcSAyushi Smriti static constexpr const char* eventServiceFile = 471bf712bcSAyushi Smriti "/var/lib/bmcweb/eventservice_config.json"; 481bf712bcSAyushi Smriti 497f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 507f4eb588SAppaRao Puli std::shared_ptr<boost::asio::posix::stream_descriptor> inotifyConn = nullptr; 51b792cc56SAppaRao Puli static constexpr const char* redfishEventLogDir = "/var/log"; 52b792cc56SAppaRao Puli static constexpr const char* redfishEventLogFile = "/var/log/redfish"; 53b792cc56SAppaRao Puli static constexpr const size_t iEventSize = sizeof(inotify_event); 54b792cc56SAppaRao Puli static int inotifyFd = -1; 55b792cc56SAppaRao Puli static int dirWatchDesc = -1; 56b792cc56SAppaRao Puli static int fileWatchDesc = -1; 577f4eb588SAppaRao Puli 587f4eb588SAppaRao Puli // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs> 597f4eb588SAppaRao Puli using EventLogObjectsType = 607f4eb588SAppaRao Puli std::tuple<std::string, std::string, std::string, std::string, std::string, 615e715de6SAppaRao Puli std::vector<std::string>>; 627f4eb588SAppaRao Puli 637f4eb588SAppaRao Puli namespace message_registries 647f4eb588SAppaRao Puli { 657f4eb588SAppaRao Puli static const Message* 667f4eb588SAppaRao Puli getMsgFromRegistry(const std::string& messageKey, 677f4eb588SAppaRao Puli const boost::beast::span<const MessageEntry>& registry) 687f4eb588SAppaRao Puli { 697f4eb588SAppaRao Puli boost::beast::span<const MessageEntry>::const_iterator messageIt = 707f4eb588SAppaRao Puli std::find_if(registry.cbegin(), registry.cend(), 717f4eb588SAppaRao Puli [&messageKey](const MessageEntry& messageEntry) { 727f4eb588SAppaRao Puli return !messageKey.compare(messageEntry.first); 737f4eb588SAppaRao Puli }); 747f4eb588SAppaRao Puli if (messageIt != registry.cend()) 757f4eb588SAppaRao Puli { 767f4eb588SAppaRao Puli return &messageIt->second; 777f4eb588SAppaRao Puli } 787f4eb588SAppaRao Puli 797f4eb588SAppaRao Puli return nullptr; 807f4eb588SAppaRao Puli } 817f4eb588SAppaRao Puli 827f4eb588SAppaRao Puli static const Message* formatMessage(const std::string_view& messageID) 837f4eb588SAppaRao Puli { 847f4eb588SAppaRao Puli // Redfish MessageIds are in the form 857f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 867f4eb588SAppaRao Puli // the right Message 877f4eb588SAppaRao Puli std::vector<std::string> fields; 887f4eb588SAppaRao Puli fields.reserve(4); 897f4eb588SAppaRao Puli boost::split(fields, messageID, boost::is_any_of(".")); 907f4eb588SAppaRao Puli if (fields.size() != 4) 917f4eb588SAppaRao Puli { 927f4eb588SAppaRao Puli return nullptr; 937f4eb588SAppaRao Puli } 947f4eb588SAppaRao Puli std::string& registryName = fields[0]; 957f4eb588SAppaRao Puli std::string& messageKey = fields[3]; 967f4eb588SAppaRao Puli 977f4eb588SAppaRao Puli // Find the right registry and check it for the MessageKey 987f4eb588SAppaRao Puli if (std::string(base::header.registryPrefix) == registryName) 997f4eb588SAppaRao Puli { 1007f4eb588SAppaRao Puli return getMsgFromRegistry( 1017f4eb588SAppaRao Puli messageKey, boost::beast::span<const MessageEntry>(base::registry)); 1027f4eb588SAppaRao Puli } 1037f4eb588SAppaRao Puli if (std::string(openbmc::header.registryPrefix) == registryName) 1047f4eb588SAppaRao Puli { 1057f4eb588SAppaRao Puli return getMsgFromRegistry( 1067f4eb588SAppaRao Puli messageKey, 1077f4eb588SAppaRao Puli boost::beast::span<const MessageEntry>(openbmc::registry)); 1087f4eb588SAppaRao Puli } 1097f4eb588SAppaRao Puli return nullptr; 1107f4eb588SAppaRao Puli } 1117f4eb588SAppaRao Puli } // namespace message_registries 1127f4eb588SAppaRao Puli 1137f4eb588SAppaRao Puli namespace event_log 1147f4eb588SAppaRao Puli { 1157f4eb588SAppaRao Puli bool getUniqueEntryID(const std::string& logEntry, std::string& entryID, 1167f4eb588SAppaRao Puli const bool firstEntry = true) 1177f4eb588SAppaRao Puli { 1187f4eb588SAppaRao Puli static time_t prevTs = 0; 1197f4eb588SAppaRao Puli static int index = 0; 1207f4eb588SAppaRao Puli if (firstEntry) 1217f4eb588SAppaRao Puli { 1227f4eb588SAppaRao Puli prevTs = 0; 1237f4eb588SAppaRao Puli } 1247f4eb588SAppaRao Puli 1257f4eb588SAppaRao Puli // Get the entry timestamp 1267f4eb588SAppaRao Puli std::time_t curTs = 0; 1277f4eb588SAppaRao Puli std::tm timeStruct = {}; 1287f4eb588SAppaRao Puli std::istringstream entryStream(logEntry); 1297f4eb588SAppaRao Puli if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 1307f4eb588SAppaRao Puli { 1317f4eb588SAppaRao Puli curTs = std::mktime(&timeStruct); 1327f4eb588SAppaRao Puli if (curTs == -1) 1337f4eb588SAppaRao Puli { 1347f4eb588SAppaRao Puli return false; 1357f4eb588SAppaRao Puli } 1367f4eb588SAppaRao Puli } 1377f4eb588SAppaRao Puli // If the timestamp isn't unique, increment the index 1387f4eb588SAppaRao Puli index = (curTs == prevTs) ? index + 1 : 0; 1397f4eb588SAppaRao Puli 1407f4eb588SAppaRao Puli // Save the timestamp 1417f4eb588SAppaRao Puli prevTs = curTs; 1427f4eb588SAppaRao Puli 1437f4eb588SAppaRao Puli entryID = std::to_string(curTs); 1447f4eb588SAppaRao Puli if (index > 0) 1457f4eb588SAppaRao Puli { 1467f4eb588SAppaRao Puli entryID += "_" + std::to_string(index); 1477f4eb588SAppaRao Puli } 1487f4eb588SAppaRao Puli return true; 1497f4eb588SAppaRao Puli } 1507f4eb588SAppaRao Puli 1517f4eb588SAppaRao Puli int getEventLogParams(const std::string& logEntry, std::string& timestamp, 1527f4eb588SAppaRao Puli std::string& messageID, 1535e715de6SAppaRao Puli std::vector<std::string>& messageArgs) 1547f4eb588SAppaRao Puli { 1557f4eb588SAppaRao Puli // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1567f4eb588SAppaRao Puli // First get the Timestamp 1577f4eb588SAppaRao Puli size_t space = logEntry.find_first_of(" "); 1587f4eb588SAppaRao Puli if (space == std::string::npos) 1597f4eb588SAppaRao Puli { 1607f4eb588SAppaRao Puli return -EINVAL; 1617f4eb588SAppaRao Puli } 1627f4eb588SAppaRao Puli timestamp = logEntry.substr(0, space); 1637f4eb588SAppaRao Puli // Then get the log contents 1647f4eb588SAppaRao Puli size_t entryStart = logEntry.find_first_not_of(" ", space); 1657f4eb588SAppaRao Puli if (entryStart == std::string::npos) 1667f4eb588SAppaRao Puli { 1677f4eb588SAppaRao Puli return -EINVAL; 1687f4eb588SAppaRao Puli } 1697f4eb588SAppaRao Puli std::string_view entry(logEntry); 1707f4eb588SAppaRao Puli entry.remove_prefix(entryStart); 1717f4eb588SAppaRao Puli // Use split to separate the entry into its fields 1727f4eb588SAppaRao Puli std::vector<std::string> logEntryFields; 1737f4eb588SAppaRao Puli boost::split(logEntryFields, entry, boost::is_any_of(","), 1747f4eb588SAppaRao Puli boost::token_compress_on); 1757f4eb588SAppaRao Puli // We need at least a MessageId to be valid 1767f4eb588SAppaRao Puli if (logEntryFields.size() < 1) 1777f4eb588SAppaRao Puli { 1787f4eb588SAppaRao Puli return -EINVAL; 1797f4eb588SAppaRao Puli } 1807f4eb588SAppaRao Puli messageID = logEntryFields[0]; 1817f4eb588SAppaRao Puli 1827f4eb588SAppaRao Puli // Get the MessageArgs from the log if there are any 1837f4eb588SAppaRao Puli if (logEntryFields.size() > 1) 1847f4eb588SAppaRao Puli { 1857f4eb588SAppaRao Puli std::string& messageArgsStart = logEntryFields[1]; 1867f4eb588SAppaRao Puli // If the first string is empty, assume there are no MessageArgs 1877f4eb588SAppaRao Puli if (!messageArgsStart.empty()) 1887f4eb588SAppaRao Puli { 1895e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 1905e715de6SAppaRao Puli logEntryFields.end()); 1917f4eb588SAppaRao Puli } 1927f4eb588SAppaRao Puli } 1937f4eb588SAppaRao Puli 1947f4eb588SAppaRao Puli return 0; 1957f4eb588SAppaRao Puli } 1967f4eb588SAppaRao Puli 1977f4eb588SAppaRao Puli void getRegistryAndMessageKey(const std::string& messageID, 1987f4eb588SAppaRao Puli std::string& registryName, 1997f4eb588SAppaRao Puli std::string& messageKey) 2007f4eb588SAppaRao Puli { 2017f4eb588SAppaRao Puli // Redfish MessageIds are in the form 2027f4eb588SAppaRao Puli // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 2037f4eb588SAppaRao Puli // the right Message 2047f4eb588SAppaRao Puli std::vector<std::string> fields; 2057f4eb588SAppaRao Puli fields.reserve(4); 2067f4eb588SAppaRao Puli boost::split(fields, messageID, boost::is_any_of(".")); 2077f4eb588SAppaRao Puli if (fields.size() == 4) 2087f4eb588SAppaRao Puli { 2097f4eb588SAppaRao Puli registryName = fields[0]; 2107f4eb588SAppaRao Puli messageKey = fields[3]; 2117f4eb588SAppaRao Puli } 2127f4eb588SAppaRao Puli } 2137f4eb588SAppaRao Puli 2147f4eb588SAppaRao Puli int formatEventLogEntry(const std::string& logEntryID, 2157f4eb588SAppaRao Puli const std::string& messageID, 2165e715de6SAppaRao Puli const std::vector<std::string>& messageArgs, 2177f4eb588SAppaRao Puli std::string timestamp, const std::string customText, 2187f4eb588SAppaRao Puli nlohmann::json& logEntryJson) 2197f4eb588SAppaRao Puli { 2207f4eb588SAppaRao Puli // Get the Message from the MessageRegistry 2217f4eb588SAppaRao Puli const message_registries::Message* message = 2227f4eb588SAppaRao Puli message_registries::formatMessage(messageID); 2237f4eb588SAppaRao Puli 2247f4eb588SAppaRao Puli std::string msg; 2257f4eb588SAppaRao Puli std::string severity; 2267f4eb588SAppaRao Puli if (message != nullptr) 2277f4eb588SAppaRao Puli { 2287f4eb588SAppaRao Puli msg = message->message; 2297f4eb588SAppaRao Puli severity = message->severity; 2307f4eb588SAppaRao Puli } 2317f4eb588SAppaRao Puli 2327f4eb588SAppaRao Puli // Fill the MessageArgs into the Message 2337f4eb588SAppaRao Puli int i = 0; 2347f4eb588SAppaRao Puli for (const std::string& messageArg : messageArgs) 2357f4eb588SAppaRao Puli { 2367f4eb588SAppaRao Puli std::string argStr = "%" + std::to_string(++i); 2377f4eb588SAppaRao Puli size_t argPos = msg.find(argStr); 2387f4eb588SAppaRao Puli if (argPos != std::string::npos) 2397f4eb588SAppaRao Puli { 2407f4eb588SAppaRao Puli msg.replace(argPos, argStr.length(), messageArg); 2417f4eb588SAppaRao Puli } 2427f4eb588SAppaRao Puli } 2437f4eb588SAppaRao Puli 2447f4eb588SAppaRao Puli // Get the Created time from the timestamp. The log timestamp is in 2457f4eb588SAppaRao Puli // RFC3339 format which matches the Redfish format except for the 2467f4eb588SAppaRao Puli // fractional seconds between the '.' and the '+', so just remove them. 2477f4eb588SAppaRao Puli std::size_t dot = timestamp.find_first_of("."); 2487f4eb588SAppaRao Puli std::size_t plus = timestamp.find_first_of("+"); 2497f4eb588SAppaRao Puli if (dot != std::string::npos && plus != std::string::npos) 2507f4eb588SAppaRao Puli { 2517f4eb588SAppaRao Puli timestamp.erase(dot, plus - dot); 2527f4eb588SAppaRao Puli } 2537f4eb588SAppaRao Puli 2547f4eb588SAppaRao Puli // Fill in the log entry with the gathered data 2557f4eb588SAppaRao Puli logEntryJson = {{"EventId", logEntryID}, 2567f4eb588SAppaRao Puli {"EventType", "Event"}, 2577f4eb588SAppaRao Puli {"Severity", std::move(severity)}, 2587f4eb588SAppaRao Puli {"Message", std::move(msg)}, 2597f4eb588SAppaRao Puli {"MessageId", std::move(messageID)}, 2607f4eb588SAppaRao Puli {"MessageArgs", std::move(messageArgs)}, 2617f4eb588SAppaRao Puli {"EventTimestamp", std::move(timestamp)}, 2627f4eb588SAppaRao Puli {"Context", customText}}; 2637f4eb588SAppaRao Puli return 0; 2647f4eb588SAppaRao Puli } 2657f4eb588SAppaRao Puli 2667f4eb588SAppaRao Puli } // namespace event_log 2677f4eb588SAppaRao Puli #endif 2687f4eb588SAppaRao Puli 269b52664e2SAppaRao Puli class Subscription 270b52664e2SAppaRao Puli { 271b52664e2SAppaRao Puli public: 272b52664e2SAppaRao Puli std::string id; 273b52664e2SAppaRao Puli std::string destinationUrl; 274b52664e2SAppaRao Puli std::string protocol; 275b52664e2SAppaRao Puli std::string retryPolicy; 276b52664e2SAppaRao Puli std::string customText; 277b52664e2SAppaRao Puli std::string eventFormatType; 278b52664e2SAppaRao Puli std::string subscriptionType; 279b52664e2SAppaRao Puli std::vector<std::string> registryMsgIds; 280b52664e2SAppaRao Puli std::vector<std::string> registryPrefixes; 281b52664e2SAppaRao Puli std::vector<nlohmann::json> httpHeaders; // key-value pair 282156d6b00SAppaRao Puli std::vector<nlohmann::json> metricReportDefinitions; 283b52664e2SAppaRao Puli 284b52664e2SAppaRao Puli Subscription(const Subscription&) = delete; 285b52664e2SAppaRao Puli Subscription& operator=(const Subscription&) = delete; 286b52664e2SAppaRao Puli Subscription(Subscription&&) = delete; 287b52664e2SAppaRao Puli Subscription& operator=(Subscription&&) = delete; 288b52664e2SAppaRao Puli 289b52664e2SAppaRao Puli Subscription(const std::string& inHost, const std::string& inPort, 290b52664e2SAppaRao Puli const std::string& inPath, const std::string& inUriProto) : 2910b4bdd93SAppaRao Puli eventSeqNum(1), 2920b4bdd93SAppaRao Puli host(inHost), port(inPort), path(inPath), uriProto(inUriProto) 293b52664e2SAppaRao Puli { 294b52664e2SAppaRao Puli conn = std::make_shared<crow::HttpClient>( 295*fe44eb0bSAyushi Smriti crow::connections::systemBus->get_io_context(), id, host, port, 296*fe44eb0bSAyushi Smriti path); 297b52664e2SAppaRao Puli } 298b52664e2SAppaRao Puli ~Subscription() 2991214b7e7SGunnar Mills {} 300b52664e2SAppaRao Puli 301b52664e2SAppaRao Puli void sendEvent(const std::string& msg) 302b52664e2SAppaRao Puli { 303b52664e2SAppaRao Puli std::vector<std::pair<std::string, std::string>> reqHeaders; 304b52664e2SAppaRao Puli for (const auto& header : httpHeaders) 305b52664e2SAppaRao Puli { 306b52664e2SAppaRao Puli for (const auto& item : header.items()) 307b52664e2SAppaRao Puli { 308b52664e2SAppaRao Puli std::string key = item.key(); 309b52664e2SAppaRao Puli std::string val = item.value(); 310b52664e2SAppaRao Puli reqHeaders.emplace_back(std::pair(key, val)); 311b52664e2SAppaRao Puli } 312b52664e2SAppaRao Puli } 313b52664e2SAppaRao Puli conn->setHeaders(reqHeaders); 3142a5689a7SAppaRao Puli conn->sendData(msg); 315b52664e2SAppaRao Puli } 316b52664e2SAppaRao Puli 3170b4bdd93SAppaRao Puli void sendTestEventLog() 3180b4bdd93SAppaRao Puli { 3190b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 3200b4bdd93SAppaRao Puli logEntryArray.push_back({}); 3210b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 3220b4bdd93SAppaRao Puli 3230b4bdd93SAppaRao Puli logEntryJson = {{"EventId", "TestID"}, 3240b4bdd93SAppaRao Puli {"EventType", "Event"}, 3250b4bdd93SAppaRao Puli {"Severity", "OK"}, 3260b4bdd93SAppaRao Puli {"Message", "Generated test event"}, 3270b4bdd93SAppaRao Puli {"MessageId", "OpenBMC.0.1.TestEventLog"}, 3280b4bdd93SAppaRao Puli {"MessageArgs", nlohmann::json::array()}, 3290b4bdd93SAppaRao Puli {"EventTimestamp", crow::utility::dateTimeNow()}, 3300b4bdd93SAppaRao Puli {"Context", customText}}; 3310b4bdd93SAppaRao Puli 3320b4bdd93SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 3330b4bdd93SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 3340b4bdd93SAppaRao Puli {"Name", "Event Log"}, 3350b4bdd93SAppaRao Puli {"Events", logEntryArray}}; 3360b4bdd93SAppaRao Puli 3370b4bdd93SAppaRao Puli this->sendEvent(msg.dump()); 3380b4bdd93SAppaRao Puli this->eventSeqNum++; 3390b4bdd93SAppaRao Puli } 3400b4bdd93SAppaRao Puli 3417f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 3427f4eb588SAppaRao Puli void filterAndSendEventLogs( 3437f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 3447f4eb588SAppaRao Puli { 3457f4eb588SAppaRao Puli nlohmann::json logEntryArray; 3467f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 3477f4eb588SAppaRao Puli { 3487f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 3497f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 3507f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 3517f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 3527f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 3535e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 3547f4eb588SAppaRao Puli 3557f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 3567f4eb588SAppaRao Puli // send everything. 3577f4eb588SAppaRao Puli if (registryPrefixes.size()) 3587f4eb588SAppaRao Puli { 3597f4eb588SAppaRao Puli auto obj = std::find(registryPrefixes.begin(), 3607f4eb588SAppaRao Puli registryPrefixes.end(), registryName); 3617f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 3627f4eb588SAppaRao Puli { 3637f4eb588SAppaRao Puli continue; 3647f4eb588SAppaRao Puli } 3657f4eb588SAppaRao Puli } 3667f4eb588SAppaRao Puli 3677f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 3687f4eb588SAppaRao Puli // send everything. 3697f4eb588SAppaRao Puli if (registryMsgIds.size()) 3707f4eb588SAppaRao Puli { 3717f4eb588SAppaRao Puli auto obj = std::find(registryMsgIds.begin(), 3727f4eb588SAppaRao Puli registryMsgIds.end(), messageKey); 3737f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 3747f4eb588SAppaRao Puli { 3757f4eb588SAppaRao Puli continue; 3767f4eb588SAppaRao Puli } 3777f4eb588SAppaRao Puli } 3787f4eb588SAppaRao Puli 3797f4eb588SAppaRao Puli logEntryArray.push_back({}); 3807f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 3817f4eb588SAppaRao Puli if (event_log::formatEventLogEntry(idStr, messageID, messageArgs, 3827f4eb588SAppaRao Puli timestamp, customText, 3837f4eb588SAppaRao Puli bmcLogEntry) != 0) 3847f4eb588SAppaRao Puli { 3857f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 3867f4eb588SAppaRao Puli continue; 3877f4eb588SAppaRao Puli } 3887f4eb588SAppaRao Puli } 3897f4eb588SAppaRao Puli 3907f4eb588SAppaRao Puli if (logEntryArray.size() < 1) 3917f4eb588SAppaRao Puli { 3927f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 3937f4eb588SAppaRao Puli return; 3947f4eb588SAppaRao Puli } 3957f4eb588SAppaRao Puli 3967f4eb588SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 3977f4eb588SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 3987f4eb588SAppaRao Puli {"Name", "Event Log"}, 3997f4eb588SAppaRao Puli {"Events", logEntryArray}}; 4007f4eb588SAppaRao Puli 4017f4eb588SAppaRao Puli this->sendEvent(msg.dump()); 4027f4eb588SAppaRao Puli this->eventSeqNum++; 4037f4eb588SAppaRao Puli } 4047f4eb588SAppaRao Puli #endif 4057f4eb588SAppaRao Puli 406156d6b00SAppaRao Puli void filterAndSendReports(const std::string& id, 407156d6b00SAppaRao Puli const std::string& readingsTs, 408156d6b00SAppaRao Puli const ReadingsObjType& readings) 409156d6b00SAppaRao Puli { 410156d6b00SAppaRao Puli std::string metricReportDef = 411156d6b00SAppaRao Puli "/redfish/v1/TelemetryService/MetricReportDefinitions/" + id; 412156d6b00SAppaRao Puli 413156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 414156d6b00SAppaRao Puli if (metricReportDefinitions.size()) 415156d6b00SAppaRao Puli { 416156d6b00SAppaRao Puli if (std::find(metricReportDefinitions.begin(), 417156d6b00SAppaRao Puli metricReportDefinitions.end(), 418156d6b00SAppaRao Puli metricReportDef) == metricReportDefinitions.end()) 419156d6b00SAppaRao Puli { 420156d6b00SAppaRao Puli return; 421156d6b00SAppaRao Puli } 422156d6b00SAppaRao Puli } 423156d6b00SAppaRao Puli 424156d6b00SAppaRao Puli nlohmann::json metricValuesArray = nlohmann::json::array(); 425156d6b00SAppaRao Puli for (const auto& it : readings) 426156d6b00SAppaRao Puli { 427156d6b00SAppaRao Puli metricValuesArray.push_back({}); 428156d6b00SAppaRao Puli nlohmann::json& entry = metricValuesArray.back(); 429156d6b00SAppaRao Puli 430156d6b00SAppaRao Puli entry = {{"MetricId", std::get<0>(it)}, 431156d6b00SAppaRao Puli {"MetricProperty", std::get<1>(it)}, 432156d6b00SAppaRao Puli {"MetricValue", std::to_string(std::get<2>(it))}, 433156d6b00SAppaRao Puli {"Timestamp", std::get<3>(it)}}; 434156d6b00SAppaRao Puli } 435156d6b00SAppaRao Puli 436156d6b00SAppaRao Puli nlohmann::json msg = { 437156d6b00SAppaRao Puli {"@odata.id", "/redfish/v1/TelemetryService/MetricReports/" + id}, 438156d6b00SAppaRao Puli {"@odata.type", "#MetricReport.v1_3_0.MetricReport"}, 439156d6b00SAppaRao Puli {"Id", id}, 440156d6b00SAppaRao Puli {"Name", id}, 441156d6b00SAppaRao Puli {"Timestamp", readingsTs}, 442156d6b00SAppaRao Puli {"MetricReportDefinition", {{"@odata.id", metricReportDef}}}, 443156d6b00SAppaRao Puli {"MetricValues", metricValuesArray}}; 444156d6b00SAppaRao Puli 445156d6b00SAppaRao Puli this->sendEvent(msg.dump()); 446156d6b00SAppaRao Puli } 447156d6b00SAppaRao Puli 448*fe44eb0bSAyushi Smriti void updateRetryConfig(const uint32_t retryAttempts, 449*fe44eb0bSAyushi Smriti const uint32_t retryTimeoutInterval) 450*fe44eb0bSAyushi Smriti { 451*fe44eb0bSAyushi Smriti conn->setRetryConfig(retryAttempts, retryTimeoutInterval); 452*fe44eb0bSAyushi Smriti } 453*fe44eb0bSAyushi Smriti 454*fe44eb0bSAyushi Smriti void updateRetryPolicy() 455*fe44eb0bSAyushi Smriti { 456*fe44eb0bSAyushi Smriti conn->setRetryPolicy(retryPolicy); 457*fe44eb0bSAyushi Smriti } 458*fe44eb0bSAyushi Smriti 459b52664e2SAppaRao Puli private: 4600b4bdd93SAppaRao Puli uint64_t eventSeqNum; 461b52664e2SAppaRao Puli std::string host; 462b52664e2SAppaRao Puli std::string port; 463b52664e2SAppaRao Puli std::string path; 464b52664e2SAppaRao Puli std::string uriProto; 465b52664e2SAppaRao Puli std::shared_ptr<crow::HttpClient> conn; 466b52664e2SAppaRao Puli }; 467b52664e2SAppaRao Puli 4681bf712bcSAyushi Smriti static constexpr const bool defaultEnabledState = true; 4691bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryAttempts = 3; 4701bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryInterval = 30; 4711bf712bcSAyushi Smriti static constexpr const char* defaulEventFormatType = "Event"; 4721bf712bcSAyushi Smriti static constexpr const char* defaulSubscriptionType = "RedfishEvent"; 4731bf712bcSAyushi Smriti static constexpr const char* defaulRetryPolicy = "TerminateAfterRetries"; 4741bf712bcSAyushi Smriti 475b52664e2SAppaRao Puli class EventServiceManager 476b52664e2SAppaRao Puli { 477b52664e2SAppaRao Puli private: 4787d1cc387SAppaRao Puli bool serviceEnabled; 4797d1cc387SAppaRao Puli uint32_t retryAttempts; 4807d1cc387SAppaRao Puli uint32_t retryTimeoutInterval; 4817d1cc387SAppaRao Puli 482b52664e2SAppaRao Puli EventServiceManager(const EventServiceManager&) = delete; 483b52664e2SAppaRao Puli EventServiceManager& operator=(const EventServiceManager&) = delete; 484b52664e2SAppaRao Puli EventServiceManager(EventServiceManager&&) = delete; 485b52664e2SAppaRao Puli EventServiceManager& operator=(EventServiceManager&&) = delete; 486b52664e2SAppaRao Puli 4877d1cc387SAppaRao Puli EventServiceManager() : 4887d1cc387SAppaRao Puli noOfEventLogSubscribers(0), noOfMetricReportSubscribers(0) 489b52664e2SAppaRao Puli { 4901bf712bcSAyushi Smriti // Load config from persist store. 4911bf712bcSAyushi Smriti initConfig(); 492b52664e2SAppaRao Puli } 493b52664e2SAppaRao Puli 4947f4eb588SAppaRao Puli std::string lastEventTStr; 4957d1cc387SAppaRao Puli size_t noOfEventLogSubscribers; 496156d6b00SAppaRao Puli size_t noOfMetricReportSubscribers; 497156d6b00SAppaRao Puli std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor; 498b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 499b52664e2SAppaRao Puli subscriptionsMap; 500b52664e2SAppaRao Puli 501b52664e2SAppaRao Puli public: 502b52664e2SAppaRao Puli static EventServiceManager& getInstance() 503b52664e2SAppaRao Puli { 504b52664e2SAppaRao Puli static EventServiceManager handler; 505b52664e2SAppaRao Puli return handler; 506b52664e2SAppaRao Puli } 507b52664e2SAppaRao Puli 5081bf712bcSAyushi Smriti void loadDefaultConfig() 5091bf712bcSAyushi Smriti { 5101bf712bcSAyushi Smriti serviceEnabled = defaultEnabledState; 5111bf712bcSAyushi Smriti retryAttempts = defaultRetryAttempts; 5121bf712bcSAyushi Smriti retryTimeoutInterval = defaultRetryInterval; 5131bf712bcSAyushi Smriti } 5141bf712bcSAyushi Smriti 5151bf712bcSAyushi Smriti void initConfig() 5161bf712bcSAyushi Smriti { 5171bf712bcSAyushi Smriti std::ifstream eventConfigFile(eventServiceFile); 5181bf712bcSAyushi Smriti if (!eventConfigFile.good()) 5191bf712bcSAyushi Smriti { 5201bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService config not exist"; 5211bf712bcSAyushi Smriti loadDefaultConfig(); 5221bf712bcSAyushi Smriti return; 5231bf712bcSAyushi Smriti } 5241bf712bcSAyushi Smriti auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 5251bf712bcSAyushi Smriti if (jsonData.is_discarded()) 5261bf712bcSAyushi Smriti { 5271bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "EventService config parse error."; 5281bf712bcSAyushi Smriti loadDefaultConfig(); 5291bf712bcSAyushi Smriti return; 5301bf712bcSAyushi Smriti } 5311bf712bcSAyushi Smriti 5321bf712bcSAyushi Smriti nlohmann::json jsonConfig; 5331bf712bcSAyushi Smriti if (json_util::getValueFromJsonObject(jsonData, "Configuration", 5341bf712bcSAyushi Smriti jsonConfig)) 5351bf712bcSAyushi Smriti { 5361bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonConfig, "ServiceEnabled", 5371bf712bcSAyushi Smriti serviceEnabled)) 5381bf712bcSAyushi Smriti { 5391bf712bcSAyushi Smriti serviceEnabled = defaultEnabledState; 5401bf712bcSAyushi Smriti } 5411bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 5421bf712bcSAyushi Smriti jsonConfig, "DeliveryRetryAttempts", retryAttempts)) 5431bf712bcSAyushi Smriti { 5441bf712bcSAyushi Smriti retryAttempts = defaultRetryAttempts; 5451bf712bcSAyushi Smriti } 5461bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 5471bf712bcSAyushi Smriti jsonConfig, "DeliveryRetryIntervalSeconds", 5481bf712bcSAyushi Smriti retryTimeoutInterval)) 5491bf712bcSAyushi Smriti { 5501bf712bcSAyushi Smriti retryTimeoutInterval = defaultRetryInterval; 5511bf712bcSAyushi Smriti } 5521bf712bcSAyushi Smriti } 5531bf712bcSAyushi Smriti else 5541bf712bcSAyushi Smriti { 5551bf712bcSAyushi Smriti loadDefaultConfig(); 5561bf712bcSAyushi Smriti } 5571bf712bcSAyushi Smriti 5581bf712bcSAyushi Smriti nlohmann::json subscriptionsList; 5591bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonData, "Subscriptions", 5601bf712bcSAyushi Smriti subscriptionsList)) 5611bf712bcSAyushi Smriti { 5621bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService: Subscriptions not exist."; 5631bf712bcSAyushi Smriti return; 5641bf712bcSAyushi Smriti } 5651bf712bcSAyushi Smriti 5661bf712bcSAyushi Smriti for (nlohmann::json& jsonObj : subscriptionsList) 5671bf712bcSAyushi Smriti { 5681bf712bcSAyushi Smriti std::string protocol; 5691bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "Protocol", 5701bf712bcSAyushi Smriti protocol)) 5711bf712bcSAyushi Smriti { 5721bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "Invalid subscription Protocol exist."; 5731bf712bcSAyushi Smriti continue; 5741bf712bcSAyushi Smriti } 5751bf712bcSAyushi Smriti std::string destination; 5761bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "Destination", 5771bf712bcSAyushi Smriti destination)) 5781bf712bcSAyushi Smriti { 5791bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "Invalid subscription destination exist."; 5801bf712bcSAyushi Smriti continue; 5811bf712bcSAyushi Smriti } 5821bf712bcSAyushi Smriti std::string host; 5831bf712bcSAyushi Smriti std::string urlProto; 5841bf712bcSAyushi Smriti std::string port; 5851bf712bcSAyushi Smriti std::string path; 5861bf712bcSAyushi Smriti bool status = 5871bf712bcSAyushi Smriti validateAndSplitUrl(destination, urlProto, host, port, path); 5881bf712bcSAyushi Smriti 5891bf712bcSAyushi Smriti if (!status) 5901bf712bcSAyushi Smriti { 5911bf712bcSAyushi Smriti BMCWEB_LOG_ERROR 5921bf712bcSAyushi Smriti << "Failed to validate and split destination url"; 5931bf712bcSAyushi Smriti continue; 5941bf712bcSAyushi Smriti } 5951bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 5961bf712bcSAyushi Smriti std::make_shared<Subscription>(host, port, path, urlProto); 5971bf712bcSAyushi Smriti 5981bf712bcSAyushi Smriti subValue->destinationUrl = destination; 5991bf712bcSAyushi Smriti subValue->protocol = protocol; 6001bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 6011bf712bcSAyushi Smriti jsonObj, "DeliveryRetryPolicy", subValue->retryPolicy)) 6021bf712bcSAyushi Smriti { 6031bf712bcSAyushi Smriti subValue->retryPolicy = defaulRetryPolicy; 6041bf712bcSAyushi Smriti } 6051bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "EventFormatType", 6061bf712bcSAyushi Smriti subValue->eventFormatType)) 6071bf712bcSAyushi Smriti { 6081bf712bcSAyushi Smriti subValue->eventFormatType = defaulEventFormatType; 6091bf712bcSAyushi Smriti } 6101bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "SubscriptionType", 6111bf712bcSAyushi Smriti subValue->subscriptionType)) 6121bf712bcSAyushi Smriti { 6131bf712bcSAyushi Smriti subValue->subscriptionType = defaulSubscriptionType; 6141bf712bcSAyushi Smriti } 6151bf712bcSAyushi Smriti 6161bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "Context", 6171bf712bcSAyushi Smriti subValue->customText); 6181bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "MessageIds", 6191bf712bcSAyushi Smriti subValue->registryMsgIds); 6201bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "RegistryPrefixes", 6211bf712bcSAyushi Smriti subValue->registryPrefixes); 6221bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "HttpHeaders", 6231bf712bcSAyushi Smriti subValue->httpHeaders); 6241bf712bcSAyushi Smriti json_util::getValueFromJsonObject( 6251bf712bcSAyushi Smriti jsonObj, "MetricReportDefinitions", 6261bf712bcSAyushi Smriti subValue->metricReportDefinitions); 6271bf712bcSAyushi Smriti 6281bf712bcSAyushi Smriti std::string id = addSubscription(subValue, false); 6291bf712bcSAyushi Smriti if (id.empty()) 6301bf712bcSAyushi Smriti { 6311bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Failed to add subscription"; 6321bf712bcSAyushi Smriti } 6331bf712bcSAyushi Smriti } 6341bf712bcSAyushi Smriti return; 6351bf712bcSAyushi Smriti } 6361bf712bcSAyushi Smriti 637b52664e2SAppaRao Puli void updateSubscriptionData() 638b52664e2SAppaRao Puli { 639b52664e2SAppaRao Puli // Persist the config and subscription data. 6401bf712bcSAyushi Smriti nlohmann::json jsonData; 6411bf712bcSAyushi Smriti 6421bf712bcSAyushi Smriti nlohmann::json& configObj = jsonData["Configuration"]; 6431bf712bcSAyushi Smriti configObj["ServiceEnabled"] = serviceEnabled; 6441bf712bcSAyushi Smriti configObj["DeliveryRetryAttempts"] = retryAttempts; 6451bf712bcSAyushi Smriti configObj["DeliveryRetryIntervalSeconds"] = retryTimeoutInterval; 6461bf712bcSAyushi Smriti 6471bf712bcSAyushi Smriti nlohmann::json& subListArray = jsonData["Subscriptions"]; 6481bf712bcSAyushi Smriti subListArray = nlohmann::json::array(); 6491bf712bcSAyushi Smriti 6501bf712bcSAyushi Smriti for (const auto& it : subscriptionsMap) 6511bf712bcSAyushi Smriti { 6521bf712bcSAyushi Smriti nlohmann::json entry; 6531bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = it.second; 6541bf712bcSAyushi Smriti 6551bf712bcSAyushi Smriti entry["Context"] = subValue->customText; 6561bf712bcSAyushi Smriti entry["DeliveryRetryPolicy"] = subValue->retryPolicy; 6571bf712bcSAyushi Smriti entry["Destination"] = subValue->destinationUrl; 6581bf712bcSAyushi Smriti entry["EventFormatType"] = subValue->eventFormatType; 6591bf712bcSAyushi Smriti entry["HttpHeaders"] = subValue->httpHeaders; 6601bf712bcSAyushi Smriti entry["MessageIds"] = subValue->registryMsgIds; 6611bf712bcSAyushi Smriti entry["Protocol"] = subValue->protocol; 6621bf712bcSAyushi Smriti entry["RegistryPrefixes"] = subValue->registryPrefixes; 6631bf712bcSAyushi Smriti entry["SubscriptionType"] = subValue->subscriptionType; 6641bf712bcSAyushi Smriti entry["MetricReportDefinitions"] = 6651bf712bcSAyushi Smriti subValue->metricReportDefinitions; 6661bf712bcSAyushi Smriti 6671bf712bcSAyushi Smriti subListArray.push_back(entry); 6681bf712bcSAyushi Smriti } 6691bf712bcSAyushi Smriti 6701bf712bcSAyushi Smriti const std::string tmpFile(std::string(eventServiceFile) + "_tmp"); 6711bf712bcSAyushi Smriti std::ofstream ofs(tmpFile, std::ios::out); 6721bf712bcSAyushi Smriti const auto& writeData = jsonData.dump(); 6731bf712bcSAyushi Smriti ofs << writeData; 6741bf712bcSAyushi Smriti ofs.close(); 6751bf712bcSAyushi Smriti 6761bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService config updated to file."; 6771bf712bcSAyushi Smriti if (std::rename(tmpFile.c_str(), eventServiceFile) != 0) 6781bf712bcSAyushi Smriti { 6791bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Error in renaming temporary file: " 6801bf712bcSAyushi Smriti << tmpFile.c_str(); 6811bf712bcSAyushi Smriti } 682b52664e2SAppaRao Puli } 683b52664e2SAppaRao Puli 6847d1cc387SAppaRao Puli EventServiceConfig getEventServiceConfig() 6857d1cc387SAppaRao Puli { 6867d1cc387SAppaRao Puli return {serviceEnabled, retryAttempts, retryTimeoutInterval}; 6877d1cc387SAppaRao Puli } 6887d1cc387SAppaRao Puli 6897d1cc387SAppaRao Puli void setEventServiceConfig(const EventServiceConfig& cfg) 6907d1cc387SAppaRao Puli { 6917d1cc387SAppaRao Puli bool updateConfig = false; 692*fe44eb0bSAyushi Smriti bool updateRetryCfg = false; 6937d1cc387SAppaRao Puli 6947d1cc387SAppaRao Puli if (serviceEnabled != std::get<0>(cfg)) 6957d1cc387SAppaRao Puli { 6967d1cc387SAppaRao Puli serviceEnabled = std::get<0>(cfg); 6977d1cc387SAppaRao Puli if (serviceEnabled && noOfMetricReportSubscribers) 6987d1cc387SAppaRao Puli { 6997d1cc387SAppaRao Puli registerMetricReportSignal(); 7007d1cc387SAppaRao Puli } 7017d1cc387SAppaRao Puli else 7027d1cc387SAppaRao Puli { 7037d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7047d1cc387SAppaRao Puli } 7057d1cc387SAppaRao Puli updateConfig = true; 7067d1cc387SAppaRao Puli } 7077d1cc387SAppaRao Puli 7087d1cc387SAppaRao Puli if (retryAttempts != std::get<1>(cfg)) 7097d1cc387SAppaRao Puli { 7107d1cc387SAppaRao Puli retryAttempts = std::get<1>(cfg); 7117d1cc387SAppaRao Puli updateConfig = true; 712*fe44eb0bSAyushi Smriti updateRetryCfg = true; 7137d1cc387SAppaRao Puli } 7147d1cc387SAppaRao Puli 7157d1cc387SAppaRao Puli if (retryTimeoutInterval != std::get<2>(cfg)) 7167d1cc387SAppaRao Puli { 7177d1cc387SAppaRao Puli retryTimeoutInterval = std::get<2>(cfg); 7187d1cc387SAppaRao Puli updateConfig = true; 719*fe44eb0bSAyushi Smriti updateRetryCfg = true; 7207d1cc387SAppaRao Puli } 7217d1cc387SAppaRao Puli 7227d1cc387SAppaRao Puli if (updateConfig) 7237d1cc387SAppaRao Puli { 7247d1cc387SAppaRao Puli updateSubscriptionData(); 7257d1cc387SAppaRao Puli } 726*fe44eb0bSAyushi Smriti 727*fe44eb0bSAyushi Smriti if (updateRetryCfg) 728*fe44eb0bSAyushi Smriti { 729*fe44eb0bSAyushi Smriti // Update the changed retry config to all subscriptions 730*fe44eb0bSAyushi Smriti for (const auto& it : 731*fe44eb0bSAyushi Smriti EventServiceManager::getInstance().subscriptionsMap) 732*fe44eb0bSAyushi Smriti { 733*fe44eb0bSAyushi Smriti std::shared_ptr<Subscription> entry = it.second; 734*fe44eb0bSAyushi Smriti entry->updateRetryConfig(retryAttempts, retryTimeoutInterval); 735*fe44eb0bSAyushi Smriti } 736*fe44eb0bSAyushi Smriti } 7377d1cc387SAppaRao Puli } 7387d1cc387SAppaRao Puli 7397d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 7407d1cc387SAppaRao Puli { 7417d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 7427d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 7437d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 7447d1cc387SAppaRao Puli { 7457d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 7467d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 7477d1cc387SAppaRao Puli { 7487d1cc387SAppaRao Puli eventLogSubCount++; 7497d1cc387SAppaRao Puli } 7507d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 7517d1cc387SAppaRao Puli { 7527d1cc387SAppaRao Puli metricReportSubCount++; 7537d1cc387SAppaRao Puli } 7547d1cc387SAppaRao Puli } 7557d1cc387SAppaRao Puli 7567d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 7577d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 7587d1cc387SAppaRao Puli { 7597d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 7607d1cc387SAppaRao Puli if (noOfMetricReportSubscribers) 7617d1cc387SAppaRao Puli { 7627d1cc387SAppaRao Puli registerMetricReportSignal(); 7637d1cc387SAppaRao Puli } 7647d1cc387SAppaRao Puli else 7657d1cc387SAppaRao Puli { 7667d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7677d1cc387SAppaRao Puli } 7687d1cc387SAppaRao Puli } 7697d1cc387SAppaRao Puli } 7707d1cc387SAppaRao Puli 771b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 772b52664e2SAppaRao Puli { 773b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 774b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 775b52664e2SAppaRao Puli { 776b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 777b52664e2SAppaRao Puli return nullptr; 778b52664e2SAppaRao Puli } 779b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 780b52664e2SAppaRao Puli return subValue; 781b52664e2SAppaRao Puli } 782b52664e2SAppaRao Puli 7831bf712bcSAyushi Smriti std::string addSubscription(const std::shared_ptr<Subscription> subValue, 7841bf712bcSAyushi Smriti const bool updateFile = true) 785b52664e2SAppaRao Puli { 786b52664e2SAppaRao Puli std::srand(static_cast<uint32_t>(std::time(0))); 787b52664e2SAppaRao Puli std::string id; 788b52664e2SAppaRao Puli 789b52664e2SAppaRao Puli int retry = 3; 790b52664e2SAppaRao Puli while (retry) 791b52664e2SAppaRao Puli { 792b52664e2SAppaRao Puli id = std::to_string(std::rand()); 793b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 794b52664e2SAppaRao Puli if (inserted.second) 795b52664e2SAppaRao Puli { 796b52664e2SAppaRao Puli break; 797b52664e2SAppaRao Puli } 798b52664e2SAppaRao Puli --retry; 799b52664e2SAppaRao Puli }; 800b52664e2SAppaRao Puli 801b52664e2SAppaRao Puli if (retry <= 0) 802b52664e2SAppaRao Puli { 803b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Failed to generate random number"; 804b52664e2SAppaRao Puli return std::string(""); 805b52664e2SAppaRao Puli } 806b52664e2SAppaRao Puli 8077d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 8081bf712bcSAyushi Smriti 8091bf712bcSAyushi Smriti if (updateFile) 8101bf712bcSAyushi Smriti { 811b52664e2SAppaRao Puli updateSubscriptionData(); 8121bf712bcSAyushi Smriti } 8137f4eb588SAppaRao Puli 8147f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 8157f4eb588SAppaRao Puli if (lastEventTStr.empty()) 8167f4eb588SAppaRao Puli { 8177f4eb588SAppaRao Puli cacheLastEventTimestamp(); 8187f4eb588SAppaRao Puli } 8197f4eb588SAppaRao Puli #endif 820*fe44eb0bSAyushi Smriti // Update retry configuration. 821*fe44eb0bSAyushi Smriti subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval); 822*fe44eb0bSAyushi Smriti subValue->updateRetryPolicy(); 823*fe44eb0bSAyushi Smriti 824b52664e2SAppaRao Puli return id; 825b52664e2SAppaRao Puli } 826b52664e2SAppaRao Puli 827b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 828b52664e2SAppaRao Puli { 829b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 830b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 831b52664e2SAppaRao Puli { 832b52664e2SAppaRao Puli return false; 833b52664e2SAppaRao Puli } 834b52664e2SAppaRao Puli return true; 835b52664e2SAppaRao Puli } 836b52664e2SAppaRao Puli 837b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 838b52664e2SAppaRao Puli { 839b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 840b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 841b52664e2SAppaRao Puli { 842b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 8437d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 844b52664e2SAppaRao Puli updateSubscriptionData(); 845b52664e2SAppaRao Puli } 846b52664e2SAppaRao Puli } 847b52664e2SAppaRao Puli 848b52664e2SAppaRao Puli size_t getNumberOfSubscriptions() 849b52664e2SAppaRao Puli { 850b52664e2SAppaRao Puli return subscriptionsMap.size(); 851b52664e2SAppaRao Puli } 852b52664e2SAppaRao Puli 853b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 854b52664e2SAppaRao Puli { 855b52664e2SAppaRao Puli std::vector<std::string> idList; 856b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 857b52664e2SAppaRao Puli { 858b52664e2SAppaRao Puli idList.emplace_back(it.first); 859b52664e2SAppaRao Puli } 860b52664e2SAppaRao Puli return idList; 861b52664e2SAppaRao Puli } 862b52664e2SAppaRao Puli 863b52664e2SAppaRao Puli bool isDestinationExist(const std::string& destUrl) 864b52664e2SAppaRao Puli { 865b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 866b52664e2SAppaRao Puli { 867b52664e2SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 868b52664e2SAppaRao Puli if (entry->destinationUrl == destUrl) 869b52664e2SAppaRao Puli { 870b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 871b52664e2SAppaRao Puli return true; 872b52664e2SAppaRao Puli } 873b52664e2SAppaRao Puli } 874b52664e2SAppaRao Puli return false; 875b52664e2SAppaRao Puli } 8760b4bdd93SAppaRao Puli 8770b4bdd93SAppaRao Puli void sendTestEventLog() 8780b4bdd93SAppaRao Puli { 8790b4bdd93SAppaRao Puli for (const auto& it : this->subscriptionsMap) 8800b4bdd93SAppaRao Puli { 8810b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8820b4bdd93SAppaRao Puli entry->sendTestEventLog(); 8830b4bdd93SAppaRao Puli } 8840b4bdd93SAppaRao Puli } 885e9a14131SAppaRao Puli 8867f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 8877f4eb588SAppaRao Puli void cacheLastEventTimestamp() 8887f4eb588SAppaRao Puli { 8897f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 8907f4eb588SAppaRao Puli if (!logStream.good()) 8917f4eb588SAppaRao Puli { 8927f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 8937f4eb588SAppaRao Puli return; 8947f4eb588SAppaRao Puli } 8957f4eb588SAppaRao Puli std::string logEntry; 8967f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 8977f4eb588SAppaRao Puli { 8987f4eb588SAppaRao Puli size_t space = logEntry.find_first_of(" "); 8997f4eb588SAppaRao Puli if (space == std::string::npos) 9007f4eb588SAppaRao Puli { 9017f4eb588SAppaRao Puli // Shouldn't enter here but lets skip it. 9027f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid log entry found."; 9037f4eb588SAppaRao Puli continue; 9047f4eb588SAppaRao Puli } 9057f4eb588SAppaRao Puli lastEventTStr = logEntry.substr(0, space); 9067f4eb588SAppaRao Puli } 9077f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Last Event time stamp set: " << lastEventTStr; 9087f4eb588SAppaRao Puli } 9097f4eb588SAppaRao Puli 9107f4eb588SAppaRao Puli void readEventLogsFromFile() 9117f4eb588SAppaRao Puli { 9127f4eb588SAppaRao Puli if (!serviceEnabled || !noOfEventLogSubscribers) 9137f4eb588SAppaRao Puli { 9147f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 9157f4eb588SAppaRao Puli return; 9167f4eb588SAppaRao Puli } 9177f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 9187f4eb588SAppaRao Puli if (!logStream.good()) 9197f4eb588SAppaRao Puli { 9207f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed"; 9217f4eb588SAppaRao Puli return; 9227f4eb588SAppaRao Puli } 9237f4eb588SAppaRao Puli 9247f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 9257f4eb588SAppaRao Puli 9267f4eb588SAppaRao Puli bool startLogCollection = false; 9277f4eb588SAppaRao Puli bool firstEntry = true; 9287f4eb588SAppaRao Puli 9297f4eb588SAppaRao Puli std::string logEntry; 9307f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 9317f4eb588SAppaRao Puli { 9327f4eb588SAppaRao Puli if (!startLogCollection) 9337f4eb588SAppaRao Puli { 9347f4eb588SAppaRao Puli if (boost::starts_with(logEntry, lastEventTStr)) 9357f4eb588SAppaRao Puli { 9367f4eb588SAppaRao Puli startLogCollection = true; 9377f4eb588SAppaRao Puli } 9387f4eb588SAppaRao Puli continue; 9397f4eb588SAppaRao Puli } 9407f4eb588SAppaRao Puli 9417f4eb588SAppaRao Puli std::string idStr; 9427f4eb588SAppaRao Puli if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry)) 9437f4eb588SAppaRao Puli { 9447f4eb588SAppaRao Puli continue; 9457f4eb588SAppaRao Puli } 9467f4eb588SAppaRao Puli firstEntry = false; 9477f4eb588SAppaRao Puli 9487f4eb588SAppaRao Puli std::string timestamp; 9497f4eb588SAppaRao Puli std::string messageID; 9505e715de6SAppaRao Puli std::vector<std::string> messageArgs; 9517f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 9527f4eb588SAppaRao Puli messageArgs) != 0) 9537f4eb588SAppaRao Puli { 9547f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 9557f4eb588SAppaRao Puli continue; 9567f4eb588SAppaRao Puli } 9577f4eb588SAppaRao Puli 9587f4eb588SAppaRao Puli std::string registryName; 9597f4eb588SAppaRao Puli std::string messageKey; 9607f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 9617f4eb588SAppaRao Puli messageKey); 9627f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 9637f4eb588SAppaRao Puli { 9647f4eb588SAppaRao Puli continue; 9657f4eb588SAppaRao Puli } 9667f4eb588SAppaRao Puli 9677f4eb588SAppaRao Puli lastEventTStr = timestamp; 9687f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 9697f4eb588SAppaRao Puli messageKey, messageArgs); 9707f4eb588SAppaRao Puli } 9717f4eb588SAppaRao Puli 9727f4eb588SAppaRao Puli for (const auto& it : this->subscriptionsMap) 9737f4eb588SAppaRao Puli { 9747f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 9757f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 9767f4eb588SAppaRao Puli { 9777f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 9787f4eb588SAppaRao Puli } 9797f4eb588SAppaRao Puli } 9807f4eb588SAppaRao Puli } 9817f4eb588SAppaRao Puli 9827f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 9837f4eb588SAppaRao Puli { 9847f4eb588SAppaRao Puli if (inotifyConn == nullptr) 9857f4eb588SAppaRao Puli { 9867f4eb588SAppaRao Puli return; 9877f4eb588SAppaRao Puli } 9887f4eb588SAppaRao Puli 9897f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 9907f4eb588SAppaRao Puli 9917f4eb588SAppaRao Puli inotifyConn->async_read_some( 9927f4eb588SAppaRao Puli boost::asio::buffer(readBuffer), 9937f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 9947f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 9957f4eb588SAppaRao Puli if (ec) 9967f4eb588SAppaRao Puli { 9977f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 9987f4eb588SAppaRao Puli return; 9997f4eb588SAppaRao Puli } 10007f4eb588SAppaRao Puli std::size_t index = 0; 1001b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 10027f4eb588SAppaRao Puli { 10037f4eb588SAppaRao Puli struct inotify_event event; 1004b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 1005b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 1006b792cc56SAppaRao Puli { 1007b792cc56SAppaRao Puli if ((event.len == 0) || 1008b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 1009b792cc56SAppaRao Puli { 1010b792cc56SAppaRao Puli index += (iEventSize + event.len); 1011b792cc56SAppaRao Puli continue; 1012b792cc56SAppaRao Puli } 1013b792cc56SAppaRao Puli 1014b792cc56SAppaRao Puli std::string fileName(&readBuffer[index + iEventSize], 1015b792cc56SAppaRao Puli event.len); 1016b792cc56SAppaRao Puli if (std::strcmp(fileName.c_str(), "redfish") != 0) 1017b792cc56SAppaRao Puli { 1018b792cc56SAppaRao Puli index += (iEventSize + event.len); 1019b792cc56SAppaRao Puli continue; 1020b792cc56SAppaRao Puli } 1021b792cc56SAppaRao Puli 1022b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1023b792cc56SAppaRao Puli << "Redfish log file created/deleted. event.name: " 1024b792cc56SAppaRao Puli << fileName; 1025b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 1026b792cc56SAppaRao Puli { 1027b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1028b792cc56SAppaRao Puli { 1029b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1030b792cc56SAppaRao Puli << "Redfish log file is already on " 1031b792cc56SAppaRao Puli "inotify_add_watch."; 1032b792cc56SAppaRao Puli return; 1033b792cc56SAppaRao Puli } 1034b792cc56SAppaRao Puli 1035b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1036b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1037b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1038b792cc56SAppaRao Puli { 1039b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1040b792cc56SAppaRao Puli << "inotify_add_watch failed for " 1041b792cc56SAppaRao Puli "redfish log file."; 1042b792cc56SAppaRao Puli return; 1043b792cc56SAppaRao Puli } 1044b792cc56SAppaRao Puli 1045b792cc56SAppaRao Puli EventServiceManager::getInstance() 1046b792cc56SAppaRao Puli .cacheLastEventTimestamp(); 1047b792cc56SAppaRao Puli EventServiceManager::getInstance() 1048b792cc56SAppaRao Puli .readEventLogsFromFile(); 1049b792cc56SAppaRao Puli } 1050b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1051b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1052b792cc56SAppaRao Puli { 1053b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1054b792cc56SAppaRao Puli { 1055b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1056b792cc56SAppaRao Puli fileWatchDesc = -1; 1057b792cc56SAppaRao Puli } 1058b792cc56SAppaRao Puli } 1059b792cc56SAppaRao Puli } 1060b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1061b792cc56SAppaRao Puli { 1062b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 10637f4eb588SAppaRao Puli { 10647f4eb588SAppaRao Puli EventServiceManager::getInstance() 10657f4eb588SAppaRao Puli .readEventLogsFromFile(); 10667f4eb588SAppaRao Puli } 1067b792cc56SAppaRao Puli } 1068b792cc56SAppaRao Puli index += (iEventSize + event.len); 10697f4eb588SAppaRao Puli } 10707f4eb588SAppaRao Puli 10717f4eb588SAppaRao Puli watchRedfishEventLogFile(); 10727f4eb588SAppaRao Puli }); 10737f4eb588SAppaRao Puli } 10747f4eb588SAppaRao Puli 10757f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 10767f4eb588SAppaRao Puli { 10777f4eb588SAppaRao Puli inotifyConn = 10787f4eb588SAppaRao Puli std::make_shared<boost::asio::posix::stream_descriptor>(ioc); 1079b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1080b792cc56SAppaRao Puli if (inotifyFd == -1) 10817f4eb588SAppaRao Puli { 10827f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "inotify_init1 failed."; 10837f4eb588SAppaRao Puli return -1; 10847f4eb588SAppaRao Puli } 1085b792cc56SAppaRao Puli 1086b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1087b792cc56SAppaRao Puli // create/delete. 1088b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1089b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1090b792cc56SAppaRao Puli if (dirWatchDesc == -1) 10917f4eb588SAppaRao Puli { 10927f4eb588SAppaRao Puli BMCWEB_LOG_ERROR 1093b792cc56SAppaRao Puli << "inotify_add_watch failed for event log directory."; 10947f4eb588SAppaRao Puli return -1; 10957f4eb588SAppaRao Puli } 10967f4eb588SAppaRao Puli 1097b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 1098b792cc56SAppaRao Puli fileWatchDesc = 1099b792cc56SAppaRao Puli inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY); 1100b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1101b792cc56SAppaRao Puli { 1102b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1103b792cc56SAppaRao Puli << "inotify_add_watch failed for redfish log file."; 1104b792cc56SAppaRao Puli // Don't return error if file not exist. 1105b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1106b792cc56SAppaRao Puli } 1107b792cc56SAppaRao Puli 11087f4eb588SAppaRao Puli // monitor redfish event log file 1109b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 11107f4eb588SAppaRao Puli watchRedfishEventLogFile(); 11117f4eb588SAppaRao Puli 11127f4eb588SAppaRao Puli return 0; 11137f4eb588SAppaRao Puli } 11147f4eb588SAppaRao Puli 11157f4eb588SAppaRao Puli #endif 11167f4eb588SAppaRao Puli 1117156d6b00SAppaRao Puli void getMetricReading(const std::string& service, 1118156d6b00SAppaRao Puli const std::string& objPath, const std::string& intf) 1119156d6b00SAppaRao Puli { 1120156d6b00SAppaRao Puli std::size_t found = objPath.find_last_of("/"); 1121156d6b00SAppaRao Puli if (found == std::string::npos) 1122156d6b00SAppaRao Puli { 1123156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid objPath received"; 1124156d6b00SAppaRao Puli return; 1125156d6b00SAppaRao Puli } 1126156d6b00SAppaRao Puli 1127156d6b00SAppaRao Puli std::string idStr = objPath.substr(found + 1); 1128156d6b00SAppaRao Puli if (idStr.empty()) 1129156d6b00SAppaRao Puli { 1130156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid ID in objPath"; 1131156d6b00SAppaRao Puli return; 1132156d6b00SAppaRao Puli } 1133156d6b00SAppaRao Puli 1134156d6b00SAppaRao Puli crow::connections::systemBus->async_method_call( 1135156d6b00SAppaRao Puli [idStr{std::move(idStr)}]( 1136156d6b00SAppaRao Puli const boost::system::error_code ec, 1137156d6b00SAppaRao Puli boost::container::flat_map< 1138156d6b00SAppaRao Puli std::string, std::variant<std::string, ReadingsObjType>>& 1139156d6b00SAppaRao Puli resp) { 1140156d6b00SAppaRao Puli if (ec) 1141156d6b00SAppaRao Puli { 1142156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG 1143156d6b00SAppaRao Puli << "D-Bus call failed to GetAll metric readings."; 1144156d6b00SAppaRao Puli return; 1145156d6b00SAppaRao Puli } 1146156d6b00SAppaRao Puli 1147156d6b00SAppaRao Puli const std::string* timestampPtr = 1148156d6b00SAppaRao Puli std::get_if<std::string>(&resp["Timestamp"]); 1149156d6b00SAppaRao Puli if (!timestampPtr) 1150156d6b00SAppaRao Puli { 1151156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Failed to Get timestamp."; 1152156d6b00SAppaRao Puli return; 1153156d6b00SAppaRao Puli } 1154156d6b00SAppaRao Puli 1155156d6b00SAppaRao Puli ReadingsObjType* readingsPtr = 1156156d6b00SAppaRao Puli std::get_if<ReadingsObjType>(&resp["Readings"]); 1157156d6b00SAppaRao Puli if (!readingsPtr) 1158156d6b00SAppaRao Puli { 1159156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Failed to Get Readings property."; 1160156d6b00SAppaRao Puli return; 1161156d6b00SAppaRao Puli } 1162156d6b00SAppaRao Puli 1163156d6b00SAppaRao Puli if (!readingsPtr->size()) 1164156d6b00SAppaRao Puli { 1165156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "No metrics report to be transferred"; 1166156d6b00SAppaRao Puli return; 1167156d6b00SAppaRao Puli } 1168156d6b00SAppaRao Puli 1169156d6b00SAppaRao Puli for (const auto& it : 1170156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1171156d6b00SAppaRao Puli { 1172156d6b00SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 1173156d6b00SAppaRao Puli if (entry->eventFormatType == metricReportFormatType) 1174156d6b00SAppaRao Puli { 1175156d6b00SAppaRao Puli entry->filterAndSendReports(idStr, *timestampPtr, 1176156d6b00SAppaRao Puli *readingsPtr); 1177156d6b00SAppaRao Puli } 1178156d6b00SAppaRao Puli } 1179156d6b00SAppaRao Puli }, 1180156d6b00SAppaRao Puli service, objPath, "org.freedesktop.DBus.Properties", "GetAll", 1181156d6b00SAppaRao Puli intf); 1182156d6b00SAppaRao Puli } 1183156d6b00SAppaRao Puli 1184156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1185156d6b00SAppaRao Puli { 11867d1cc387SAppaRao Puli if (matchTelemetryMonitor) 11877d1cc387SAppaRao Puli { 1188156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1189156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1190156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1191156d6b00SAppaRao Puli } 11927d1cc387SAppaRao Puli } 1193156d6b00SAppaRao Puli 1194156d6b00SAppaRao Puli void registerMetricReportSignal() 1195156d6b00SAppaRao Puli { 11967d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1197156d6b00SAppaRao Puli { 11987d1cc387SAppaRao Puli BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1199156d6b00SAppaRao Puli return; 1200156d6b00SAppaRao Puli } 1201156d6b00SAppaRao Puli 1202156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1203156d6b00SAppaRao Puli std::string matchStr( 1204156d6b00SAppaRao Puli "type='signal',member='ReportUpdate', " 1205156d6b00SAppaRao Puli "interface='xyz.openbmc_project.MonitoringService.Report'"); 1206156d6b00SAppaRao Puli 1207156d6b00SAppaRao Puli matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>( 1208156d6b00SAppaRao Puli *crow::connections::systemBus, matchStr, 1209156d6b00SAppaRao Puli [this](sdbusplus::message::message& msg) { 1210156d6b00SAppaRao Puli if (msg.is_method_error()) 1211156d6b00SAppaRao Puli { 1212156d6b00SAppaRao Puli BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 1213156d6b00SAppaRao Puli return; 1214156d6b00SAppaRao Puli } 1215156d6b00SAppaRao Puli 1216156d6b00SAppaRao Puli std::string service = msg.get_sender(); 1217156d6b00SAppaRao Puli std::string objPath = msg.get_path(); 1218156d6b00SAppaRao Puli std::string intf = msg.get_interface(); 1219156d6b00SAppaRao Puli getMetricReading(service, objPath, intf); 1220156d6b00SAppaRao Puli }); 1221156d6b00SAppaRao Puli } 12221bf712bcSAyushi Smriti 12231bf712bcSAyushi Smriti bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto, 12241bf712bcSAyushi Smriti std::string& host, std::string& port, 12251bf712bcSAyushi Smriti std::string& path) 12261bf712bcSAyushi Smriti { 12271bf712bcSAyushi Smriti // Validate URL using regex expression 12281bf712bcSAyushi Smriti // Format: <protocol>://<host>:<port>/<path> 12291bf712bcSAyushi Smriti // protocol: http/https 12301bf712bcSAyushi Smriti const std::regex urlRegex( 12311bf712bcSAyushi Smriti "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/" 12321bf712bcSAyushi Smriti "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)"); 12331bf712bcSAyushi Smriti std::cmatch match; 12341bf712bcSAyushi Smriti if (!std::regex_match(destUrl.c_str(), match, urlRegex)) 12351bf712bcSAyushi Smriti { 12361bf712bcSAyushi Smriti BMCWEB_LOG_INFO << "Dest. url did not match "; 12371bf712bcSAyushi Smriti return false; 12381bf712bcSAyushi Smriti } 12391bf712bcSAyushi Smriti 12401bf712bcSAyushi Smriti urlProto = std::string(match[1].first, match[1].second); 12411bf712bcSAyushi Smriti if (urlProto == "http") 12421bf712bcSAyushi Smriti { 12431bf712bcSAyushi Smriti #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING 12441bf712bcSAyushi Smriti return false; 12451bf712bcSAyushi Smriti #endif 12461bf712bcSAyushi Smriti } 12471bf712bcSAyushi Smriti 12481bf712bcSAyushi Smriti host = std::string(match[2].first, match[2].second); 12491bf712bcSAyushi Smriti port = std::string(match[3].first, match[3].second); 12501bf712bcSAyushi Smriti path = std::string(match[4].first, match[4].second); 12511bf712bcSAyushi Smriti if (port.empty()) 12521bf712bcSAyushi Smriti { 12531bf712bcSAyushi Smriti if (urlProto == "http") 12541bf712bcSAyushi Smriti { 12551bf712bcSAyushi Smriti port = "80"; 12561bf712bcSAyushi Smriti } 12571bf712bcSAyushi Smriti else 12581bf712bcSAyushi Smriti { 12591bf712bcSAyushi Smriti port = "443"; 12601bf712bcSAyushi Smriti } 12611bf712bcSAyushi Smriti } 12621bf712bcSAyushi Smriti if (path.empty()) 12631bf712bcSAyushi Smriti { 12641bf712bcSAyushi Smriti path = "/"; 12651bf712bcSAyushi Smriti } 12661bf712bcSAyushi Smriti return true; 12671bf712bcSAyushi Smriti } 12687f4eb588SAppaRao Puli }; // namespace redfish 1269b52664e2SAppaRao Puli 1270b52664e2SAppaRao Puli } // namespace redfish 1271