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, 61*5e715de6SAppaRao 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, 153*5e715de6SAppaRao 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 { 189*5e715de6SAppaRao Puli messageArgs.assign(logEntryFields.begin() + 1, 190*5e715de6SAppaRao 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, 216*5e715de6SAppaRao 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>( 2952a5689a7SAppaRao Puli crow::connections::systemBus->get_io_context(), host, port, path); 296b52664e2SAppaRao Puli } 297b52664e2SAppaRao Puli ~Subscription() 2981214b7e7SGunnar Mills {} 299b52664e2SAppaRao Puli 300b52664e2SAppaRao Puli void sendEvent(const std::string& msg) 301b52664e2SAppaRao Puli { 302b52664e2SAppaRao Puli std::vector<std::pair<std::string, std::string>> reqHeaders; 303b52664e2SAppaRao Puli for (const auto& header : httpHeaders) 304b52664e2SAppaRao Puli { 305b52664e2SAppaRao Puli for (const auto& item : header.items()) 306b52664e2SAppaRao Puli { 307b52664e2SAppaRao Puli std::string key = item.key(); 308b52664e2SAppaRao Puli std::string val = item.value(); 309b52664e2SAppaRao Puli reqHeaders.emplace_back(std::pair(key, val)); 310b52664e2SAppaRao Puli } 311b52664e2SAppaRao Puli } 312b52664e2SAppaRao Puli conn->setHeaders(reqHeaders); 3132a5689a7SAppaRao Puli conn->sendData(msg); 314b52664e2SAppaRao Puli } 315b52664e2SAppaRao Puli 3160b4bdd93SAppaRao Puli void sendTestEventLog() 3170b4bdd93SAppaRao Puli { 3180b4bdd93SAppaRao Puli nlohmann::json logEntryArray; 3190b4bdd93SAppaRao Puli logEntryArray.push_back({}); 3200b4bdd93SAppaRao Puli nlohmann::json& logEntryJson = logEntryArray.back(); 3210b4bdd93SAppaRao Puli 3220b4bdd93SAppaRao Puli logEntryJson = {{"EventId", "TestID"}, 3230b4bdd93SAppaRao Puli {"EventType", "Event"}, 3240b4bdd93SAppaRao Puli {"Severity", "OK"}, 3250b4bdd93SAppaRao Puli {"Message", "Generated test event"}, 3260b4bdd93SAppaRao Puli {"MessageId", "OpenBMC.0.1.TestEventLog"}, 3270b4bdd93SAppaRao Puli {"MessageArgs", nlohmann::json::array()}, 3280b4bdd93SAppaRao Puli {"EventTimestamp", crow::utility::dateTimeNow()}, 3290b4bdd93SAppaRao Puli {"Context", customText}}; 3300b4bdd93SAppaRao Puli 3310b4bdd93SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 3320b4bdd93SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 3330b4bdd93SAppaRao Puli {"Name", "Event Log"}, 3340b4bdd93SAppaRao Puli {"Events", logEntryArray}}; 3350b4bdd93SAppaRao Puli 3360b4bdd93SAppaRao Puli this->sendEvent(msg.dump()); 3370b4bdd93SAppaRao Puli this->eventSeqNum++; 3380b4bdd93SAppaRao Puli } 3390b4bdd93SAppaRao Puli 3407f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 3417f4eb588SAppaRao Puli void filterAndSendEventLogs( 3427f4eb588SAppaRao Puli const std::vector<EventLogObjectsType>& eventRecords) 3437f4eb588SAppaRao Puli { 3447f4eb588SAppaRao Puli nlohmann::json logEntryArray; 3457f4eb588SAppaRao Puli for (const EventLogObjectsType& logEntry : eventRecords) 3467f4eb588SAppaRao Puli { 3477f4eb588SAppaRao Puli const std::string& idStr = std::get<0>(logEntry); 3487f4eb588SAppaRao Puli const std::string& timestamp = std::get<1>(logEntry); 3497f4eb588SAppaRao Puli const std::string& messageID = std::get<2>(logEntry); 3507f4eb588SAppaRao Puli const std::string& registryName = std::get<3>(logEntry); 3517f4eb588SAppaRao Puli const std::string& messageKey = std::get<4>(logEntry); 352*5e715de6SAppaRao Puli const std::vector<std::string>& messageArgs = std::get<5>(logEntry); 3537f4eb588SAppaRao Puli 3547f4eb588SAppaRao Puli // If registryPrefixes list is empty, don't filter events 3557f4eb588SAppaRao Puli // send everything. 3567f4eb588SAppaRao Puli if (registryPrefixes.size()) 3577f4eb588SAppaRao Puli { 3587f4eb588SAppaRao Puli auto obj = std::find(registryPrefixes.begin(), 3597f4eb588SAppaRao Puli registryPrefixes.end(), registryName); 3607f4eb588SAppaRao Puli if (obj == registryPrefixes.end()) 3617f4eb588SAppaRao Puli { 3627f4eb588SAppaRao Puli continue; 3637f4eb588SAppaRao Puli } 3647f4eb588SAppaRao Puli } 3657f4eb588SAppaRao Puli 3667f4eb588SAppaRao Puli // If registryMsgIds list is empty, don't filter events 3677f4eb588SAppaRao Puli // send everything. 3687f4eb588SAppaRao Puli if (registryMsgIds.size()) 3697f4eb588SAppaRao Puli { 3707f4eb588SAppaRao Puli auto obj = std::find(registryMsgIds.begin(), 3717f4eb588SAppaRao Puli registryMsgIds.end(), messageKey); 3727f4eb588SAppaRao Puli if (obj == registryMsgIds.end()) 3737f4eb588SAppaRao Puli { 3747f4eb588SAppaRao Puli continue; 3757f4eb588SAppaRao Puli } 3767f4eb588SAppaRao Puli } 3777f4eb588SAppaRao Puli 3787f4eb588SAppaRao Puli logEntryArray.push_back({}); 3797f4eb588SAppaRao Puli nlohmann::json& bmcLogEntry = logEntryArray.back(); 3807f4eb588SAppaRao Puli if (event_log::formatEventLogEntry(idStr, messageID, messageArgs, 3817f4eb588SAppaRao Puli timestamp, customText, 3827f4eb588SAppaRao Puli bmcLogEntry) != 0) 3837f4eb588SAppaRao Puli { 3847f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry failed"; 3857f4eb588SAppaRao Puli continue; 3867f4eb588SAppaRao Puli } 3877f4eb588SAppaRao Puli } 3887f4eb588SAppaRao Puli 3897f4eb588SAppaRao Puli if (logEntryArray.size() < 1) 3907f4eb588SAppaRao Puli { 3917f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "No log entries available to be transferred."; 3927f4eb588SAppaRao Puli return; 3937f4eb588SAppaRao Puli } 3947f4eb588SAppaRao Puli 3957f4eb588SAppaRao Puli nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"}, 3967f4eb588SAppaRao Puli {"Id", std::to_string(eventSeqNum)}, 3977f4eb588SAppaRao Puli {"Name", "Event Log"}, 3987f4eb588SAppaRao Puli {"Events", logEntryArray}}; 3997f4eb588SAppaRao Puli 4007f4eb588SAppaRao Puli this->sendEvent(msg.dump()); 4017f4eb588SAppaRao Puli this->eventSeqNum++; 4027f4eb588SAppaRao Puli } 4037f4eb588SAppaRao Puli #endif 4047f4eb588SAppaRao Puli 405156d6b00SAppaRao Puli void filterAndSendReports(const std::string& id, 406156d6b00SAppaRao Puli const std::string& readingsTs, 407156d6b00SAppaRao Puli const ReadingsObjType& readings) 408156d6b00SAppaRao Puli { 409156d6b00SAppaRao Puli std::string metricReportDef = 410156d6b00SAppaRao Puli "/redfish/v1/TelemetryService/MetricReportDefinitions/" + id; 411156d6b00SAppaRao Puli 412156d6b00SAppaRao Puli // Empty list means no filter. Send everything. 413156d6b00SAppaRao Puli if (metricReportDefinitions.size()) 414156d6b00SAppaRao Puli { 415156d6b00SAppaRao Puli if (std::find(metricReportDefinitions.begin(), 416156d6b00SAppaRao Puli metricReportDefinitions.end(), 417156d6b00SAppaRao Puli metricReportDef) == metricReportDefinitions.end()) 418156d6b00SAppaRao Puli { 419156d6b00SAppaRao Puli return; 420156d6b00SAppaRao Puli } 421156d6b00SAppaRao Puli } 422156d6b00SAppaRao Puli 423156d6b00SAppaRao Puli nlohmann::json metricValuesArray = nlohmann::json::array(); 424156d6b00SAppaRao Puli for (const auto& it : readings) 425156d6b00SAppaRao Puli { 426156d6b00SAppaRao Puli metricValuesArray.push_back({}); 427156d6b00SAppaRao Puli nlohmann::json& entry = metricValuesArray.back(); 428156d6b00SAppaRao Puli 429156d6b00SAppaRao Puli entry = {{"MetricId", std::get<0>(it)}, 430156d6b00SAppaRao Puli {"MetricProperty", std::get<1>(it)}, 431156d6b00SAppaRao Puli {"MetricValue", std::to_string(std::get<2>(it))}, 432156d6b00SAppaRao Puli {"Timestamp", std::get<3>(it)}}; 433156d6b00SAppaRao Puli } 434156d6b00SAppaRao Puli 435156d6b00SAppaRao Puli nlohmann::json msg = { 436156d6b00SAppaRao Puli {"@odata.id", "/redfish/v1/TelemetryService/MetricReports/" + id}, 437156d6b00SAppaRao Puli {"@odata.type", "#MetricReport.v1_3_0.MetricReport"}, 438156d6b00SAppaRao Puli {"Id", id}, 439156d6b00SAppaRao Puli {"Name", id}, 440156d6b00SAppaRao Puli {"Timestamp", readingsTs}, 441156d6b00SAppaRao Puli {"MetricReportDefinition", {{"@odata.id", metricReportDef}}}, 442156d6b00SAppaRao Puli {"MetricValues", metricValuesArray}}; 443156d6b00SAppaRao Puli 444156d6b00SAppaRao Puli this->sendEvent(msg.dump()); 445156d6b00SAppaRao Puli } 446156d6b00SAppaRao Puli 447b52664e2SAppaRao Puli private: 4480b4bdd93SAppaRao Puli uint64_t eventSeqNum; 449b52664e2SAppaRao Puli std::string host; 450b52664e2SAppaRao Puli std::string port; 451b52664e2SAppaRao Puli std::string path; 452b52664e2SAppaRao Puli std::string uriProto; 453b52664e2SAppaRao Puli std::shared_ptr<crow::HttpClient> conn; 454b52664e2SAppaRao Puli }; 455b52664e2SAppaRao Puli 4561bf712bcSAyushi Smriti static constexpr const bool defaultEnabledState = true; 4571bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryAttempts = 3; 4581bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryInterval = 30; 4591bf712bcSAyushi Smriti static constexpr const char* defaulEventFormatType = "Event"; 4601bf712bcSAyushi Smriti static constexpr const char* defaulSubscriptionType = "RedfishEvent"; 4611bf712bcSAyushi Smriti static constexpr const char* defaulRetryPolicy = "TerminateAfterRetries"; 4621bf712bcSAyushi Smriti 463b52664e2SAppaRao Puli class EventServiceManager 464b52664e2SAppaRao Puli { 465b52664e2SAppaRao Puli private: 4667d1cc387SAppaRao Puli bool serviceEnabled; 4677d1cc387SAppaRao Puli uint32_t retryAttempts; 4687d1cc387SAppaRao Puli uint32_t retryTimeoutInterval; 4697d1cc387SAppaRao Puli 470b52664e2SAppaRao Puli EventServiceManager(const EventServiceManager&) = delete; 471b52664e2SAppaRao Puli EventServiceManager& operator=(const EventServiceManager&) = delete; 472b52664e2SAppaRao Puli EventServiceManager(EventServiceManager&&) = delete; 473b52664e2SAppaRao Puli EventServiceManager& operator=(EventServiceManager&&) = delete; 474b52664e2SAppaRao Puli 4757d1cc387SAppaRao Puli EventServiceManager() : 4767d1cc387SAppaRao Puli noOfEventLogSubscribers(0), noOfMetricReportSubscribers(0) 477b52664e2SAppaRao Puli { 4781bf712bcSAyushi Smriti // Load config from persist store. 4791bf712bcSAyushi Smriti initConfig(); 480b52664e2SAppaRao Puli } 481b52664e2SAppaRao Puli 4827f4eb588SAppaRao Puli std::string lastEventTStr; 4837d1cc387SAppaRao Puli size_t noOfEventLogSubscribers; 484156d6b00SAppaRao Puli size_t noOfMetricReportSubscribers; 485156d6b00SAppaRao Puli std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor; 486b52664e2SAppaRao Puli boost::container::flat_map<std::string, std::shared_ptr<Subscription>> 487b52664e2SAppaRao Puli subscriptionsMap; 488b52664e2SAppaRao Puli 489b52664e2SAppaRao Puli public: 490b52664e2SAppaRao Puli static EventServiceManager& getInstance() 491b52664e2SAppaRao Puli { 492b52664e2SAppaRao Puli static EventServiceManager handler; 493b52664e2SAppaRao Puli return handler; 494b52664e2SAppaRao Puli } 495b52664e2SAppaRao Puli 4961bf712bcSAyushi Smriti void loadDefaultConfig() 4971bf712bcSAyushi Smriti { 4981bf712bcSAyushi Smriti serviceEnabled = defaultEnabledState; 4991bf712bcSAyushi Smriti retryAttempts = defaultRetryAttempts; 5001bf712bcSAyushi Smriti retryTimeoutInterval = defaultRetryInterval; 5011bf712bcSAyushi Smriti } 5021bf712bcSAyushi Smriti 5031bf712bcSAyushi Smriti void initConfig() 5041bf712bcSAyushi Smriti { 5051bf712bcSAyushi Smriti std::ifstream eventConfigFile(eventServiceFile); 5061bf712bcSAyushi Smriti if (!eventConfigFile.good()) 5071bf712bcSAyushi Smriti { 5081bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService config not exist"; 5091bf712bcSAyushi Smriti loadDefaultConfig(); 5101bf712bcSAyushi Smriti return; 5111bf712bcSAyushi Smriti } 5121bf712bcSAyushi Smriti auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false); 5131bf712bcSAyushi Smriti if (jsonData.is_discarded()) 5141bf712bcSAyushi Smriti { 5151bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "EventService config parse error."; 5161bf712bcSAyushi Smriti loadDefaultConfig(); 5171bf712bcSAyushi Smriti return; 5181bf712bcSAyushi Smriti } 5191bf712bcSAyushi Smriti 5201bf712bcSAyushi Smriti nlohmann::json jsonConfig; 5211bf712bcSAyushi Smriti if (json_util::getValueFromJsonObject(jsonData, "Configuration", 5221bf712bcSAyushi Smriti jsonConfig)) 5231bf712bcSAyushi Smriti { 5241bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonConfig, "ServiceEnabled", 5251bf712bcSAyushi Smriti serviceEnabled)) 5261bf712bcSAyushi Smriti { 5271bf712bcSAyushi Smriti serviceEnabled = defaultEnabledState; 5281bf712bcSAyushi Smriti } 5291bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 5301bf712bcSAyushi Smriti jsonConfig, "DeliveryRetryAttempts", retryAttempts)) 5311bf712bcSAyushi Smriti { 5321bf712bcSAyushi Smriti retryAttempts = defaultRetryAttempts; 5331bf712bcSAyushi Smriti } 5341bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 5351bf712bcSAyushi Smriti jsonConfig, "DeliveryRetryIntervalSeconds", 5361bf712bcSAyushi Smriti retryTimeoutInterval)) 5371bf712bcSAyushi Smriti { 5381bf712bcSAyushi Smriti retryTimeoutInterval = defaultRetryInterval; 5391bf712bcSAyushi Smriti } 5401bf712bcSAyushi Smriti } 5411bf712bcSAyushi Smriti else 5421bf712bcSAyushi Smriti { 5431bf712bcSAyushi Smriti loadDefaultConfig(); 5441bf712bcSAyushi Smriti } 5451bf712bcSAyushi Smriti 5461bf712bcSAyushi Smriti nlohmann::json subscriptionsList; 5471bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonData, "Subscriptions", 5481bf712bcSAyushi Smriti subscriptionsList)) 5491bf712bcSAyushi Smriti { 5501bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService: Subscriptions not exist."; 5511bf712bcSAyushi Smriti return; 5521bf712bcSAyushi Smriti } 5531bf712bcSAyushi Smriti 5541bf712bcSAyushi Smriti for (nlohmann::json& jsonObj : subscriptionsList) 5551bf712bcSAyushi Smriti { 5561bf712bcSAyushi Smriti std::string protocol; 5571bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "Protocol", 5581bf712bcSAyushi Smriti protocol)) 5591bf712bcSAyushi Smriti { 5601bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "Invalid subscription Protocol exist."; 5611bf712bcSAyushi Smriti continue; 5621bf712bcSAyushi Smriti } 5631bf712bcSAyushi Smriti std::string destination; 5641bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "Destination", 5651bf712bcSAyushi Smriti destination)) 5661bf712bcSAyushi Smriti { 5671bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "Invalid subscription destination exist."; 5681bf712bcSAyushi Smriti continue; 5691bf712bcSAyushi Smriti } 5701bf712bcSAyushi Smriti std::string host; 5711bf712bcSAyushi Smriti std::string urlProto; 5721bf712bcSAyushi Smriti std::string port; 5731bf712bcSAyushi Smriti std::string path; 5741bf712bcSAyushi Smriti bool status = 5751bf712bcSAyushi Smriti validateAndSplitUrl(destination, urlProto, host, port, path); 5761bf712bcSAyushi Smriti 5771bf712bcSAyushi Smriti if (!status) 5781bf712bcSAyushi Smriti { 5791bf712bcSAyushi Smriti BMCWEB_LOG_ERROR 5801bf712bcSAyushi Smriti << "Failed to validate and split destination url"; 5811bf712bcSAyushi Smriti continue; 5821bf712bcSAyushi Smriti } 5831bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = 5841bf712bcSAyushi Smriti std::make_shared<Subscription>(host, port, path, urlProto); 5851bf712bcSAyushi Smriti 5861bf712bcSAyushi Smriti subValue->destinationUrl = destination; 5871bf712bcSAyushi Smriti subValue->protocol = protocol; 5881bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject( 5891bf712bcSAyushi Smriti jsonObj, "DeliveryRetryPolicy", subValue->retryPolicy)) 5901bf712bcSAyushi Smriti { 5911bf712bcSAyushi Smriti subValue->retryPolicy = defaulRetryPolicy; 5921bf712bcSAyushi Smriti } 5931bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "EventFormatType", 5941bf712bcSAyushi Smriti subValue->eventFormatType)) 5951bf712bcSAyushi Smriti { 5961bf712bcSAyushi Smriti subValue->eventFormatType = defaulEventFormatType; 5971bf712bcSAyushi Smriti } 5981bf712bcSAyushi Smriti if (!json_util::getValueFromJsonObject(jsonObj, "SubscriptionType", 5991bf712bcSAyushi Smriti subValue->subscriptionType)) 6001bf712bcSAyushi Smriti { 6011bf712bcSAyushi Smriti subValue->subscriptionType = defaulSubscriptionType; 6021bf712bcSAyushi Smriti } 6031bf712bcSAyushi Smriti 6041bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "Context", 6051bf712bcSAyushi Smriti subValue->customText); 6061bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "MessageIds", 6071bf712bcSAyushi Smriti subValue->registryMsgIds); 6081bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "RegistryPrefixes", 6091bf712bcSAyushi Smriti subValue->registryPrefixes); 6101bf712bcSAyushi Smriti json_util::getValueFromJsonObject(jsonObj, "HttpHeaders", 6111bf712bcSAyushi Smriti subValue->httpHeaders); 6121bf712bcSAyushi Smriti json_util::getValueFromJsonObject( 6131bf712bcSAyushi Smriti jsonObj, "MetricReportDefinitions", 6141bf712bcSAyushi Smriti subValue->metricReportDefinitions); 6151bf712bcSAyushi Smriti 6161bf712bcSAyushi Smriti std::string id = addSubscription(subValue, false); 6171bf712bcSAyushi Smriti if (id.empty()) 6181bf712bcSAyushi Smriti { 6191bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Failed to add subscription"; 6201bf712bcSAyushi Smriti } 6211bf712bcSAyushi Smriti } 6221bf712bcSAyushi Smriti return; 6231bf712bcSAyushi Smriti } 6241bf712bcSAyushi Smriti 625b52664e2SAppaRao Puli void updateSubscriptionData() 626b52664e2SAppaRao Puli { 627b52664e2SAppaRao Puli // Persist the config and subscription data. 6281bf712bcSAyushi Smriti nlohmann::json jsonData; 6291bf712bcSAyushi Smriti 6301bf712bcSAyushi Smriti nlohmann::json& configObj = jsonData["Configuration"]; 6311bf712bcSAyushi Smriti configObj["ServiceEnabled"] = serviceEnabled; 6321bf712bcSAyushi Smriti configObj["DeliveryRetryAttempts"] = retryAttempts; 6331bf712bcSAyushi Smriti configObj["DeliveryRetryIntervalSeconds"] = retryTimeoutInterval; 6341bf712bcSAyushi Smriti 6351bf712bcSAyushi Smriti nlohmann::json& subListArray = jsonData["Subscriptions"]; 6361bf712bcSAyushi Smriti subListArray = nlohmann::json::array(); 6371bf712bcSAyushi Smriti 6381bf712bcSAyushi Smriti for (const auto& it : subscriptionsMap) 6391bf712bcSAyushi Smriti { 6401bf712bcSAyushi Smriti nlohmann::json entry; 6411bf712bcSAyushi Smriti std::shared_ptr<Subscription> subValue = it.second; 6421bf712bcSAyushi Smriti 6431bf712bcSAyushi Smriti entry["Context"] = subValue->customText; 6441bf712bcSAyushi Smriti entry["DeliveryRetryPolicy"] = subValue->retryPolicy; 6451bf712bcSAyushi Smriti entry["Destination"] = subValue->destinationUrl; 6461bf712bcSAyushi Smriti entry["EventFormatType"] = subValue->eventFormatType; 6471bf712bcSAyushi Smriti entry["HttpHeaders"] = subValue->httpHeaders; 6481bf712bcSAyushi Smriti entry["MessageIds"] = subValue->registryMsgIds; 6491bf712bcSAyushi Smriti entry["Protocol"] = subValue->protocol; 6501bf712bcSAyushi Smriti entry["RegistryPrefixes"] = subValue->registryPrefixes; 6511bf712bcSAyushi Smriti entry["SubscriptionType"] = subValue->subscriptionType; 6521bf712bcSAyushi Smriti entry["MetricReportDefinitions"] = 6531bf712bcSAyushi Smriti subValue->metricReportDefinitions; 6541bf712bcSAyushi Smriti 6551bf712bcSAyushi Smriti subListArray.push_back(entry); 6561bf712bcSAyushi Smriti } 6571bf712bcSAyushi Smriti 6581bf712bcSAyushi Smriti const std::string tmpFile(std::string(eventServiceFile) + "_tmp"); 6591bf712bcSAyushi Smriti std::ofstream ofs(tmpFile, std::ios::out); 6601bf712bcSAyushi Smriti const auto& writeData = jsonData.dump(); 6611bf712bcSAyushi Smriti ofs << writeData; 6621bf712bcSAyushi Smriti ofs.close(); 6631bf712bcSAyushi Smriti 6641bf712bcSAyushi Smriti BMCWEB_LOG_DEBUG << "EventService config updated to file."; 6651bf712bcSAyushi Smriti if (std::rename(tmpFile.c_str(), eventServiceFile) != 0) 6661bf712bcSAyushi Smriti { 6671bf712bcSAyushi Smriti BMCWEB_LOG_ERROR << "Error in renaming temporary file: " 6681bf712bcSAyushi Smriti << tmpFile.c_str(); 6691bf712bcSAyushi Smriti } 670b52664e2SAppaRao Puli } 671b52664e2SAppaRao Puli 6727d1cc387SAppaRao Puli EventServiceConfig getEventServiceConfig() 6737d1cc387SAppaRao Puli { 6747d1cc387SAppaRao Puli return {serviceEnabled, retryAttempts, retryTimeoutInterval}; 6757d1cc387SAppaRao Puli } 6767d1cc387SAppaRao Puli 6777d1cc387SAppaRao Puli void setEventServiceConfig(const EventServiceConfig& cfg) 6787d1cc387SAppaRao Puli { 6797d1cc387SAppaRao Puli bool updateConfig = false; 6807d1cc387SAppaRao Puli 6817d1cc387SAppaRao Puli if (serviceEnabled != std::get<0>(cfg)) 6827d1cc387SAppaRao Puli { 6837d1cc387SAppaRao Puli serviceEnabled = std::get<0>(cfg); 6847d1cc387SAppaRao Puli if (serviceEnabled && noOfMetricReportSubscribers) 6857d1cc387SAppaRao Puli { 6867d1cc387SAppaRao Puli registerMetricReportSignal(); 6877d1cc387SAppaRao Puli } 6887d1cc387SAppaRao Puli else 6897d1cc387SAppaRao Puli { 6907d1cc387SAppaRao Puli unregisterMetricReportSignal(); 6917d1cc387SAppaRao Puli } 6927d1cc387SAppaRao Puli updateConfig = true; 6937d1cc387SAppaRao Puli } 6947d1cc387SAppaRao Puli 6957d1cc387SAppaRao Puli if (retryAttempts != std::get<1>(cfg)) 6967d1cc387SAppaRao Puli { 6977d1cc387SAppaRao Puli retryAttempts = std::get<1>(cfg); 6987d1cc387SAppaRao Puli updateConfig = true; 6997d1cc387SAppaRao Puli } 7007d1cc387SAppaRao Puli 7017d1cc387SAppaRao Puli if (retryTimeoutInterval != std::get<2>(cfg)) 7027d1cc387SAppaRao Puli { 7037d1cc387SAppaRao Puli retryTimeoutInterval = std::get<2>(cfg); 7047d1cc387SAppaRao Puli updateConfig = true; 7057d1cc387SAppaRao Puli } 7067d1cc387SAppaRao Puli 7077d1cc387SAppaRao Puli if (updateConfig) 7087d1cc387SAppaRao Puli { 7097d1cc387SAppaRao Puli updateSubscriptionData(); 7107d1cc387SAppaRao Puli } 7117d1cc387SAppaRao Puli } 7127d1cc387SAppaRao Puli 7137d1cc387SAppaRao Puli void updateNoOfSubscribersCount() 7147d1cc387SAppaRao Puli { 7157d1cc387SAppaRao Puli size_t eventLogSubCount = 0; 7167d1cc387SAppaRao Puli size_t metricReportSubCount = 0; 7177d1cc387SAppaRao Puli for (const auto& it : subscriptionsMap) 7187d1cc387SAppaRao Puli { 7197d1cc387SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 7207d1cc387SAppaRao Puli if (entry->eventFormatType == eventFormatType) 7217d1cc387SAppaRao Puli { 7227d1cc387SAppaRao Puli eventLogSubCount++; 7237d1cc387SAppaRao Puli } 7247d1cc387SAppaRao Puli else if (entry->eventFormatType == metricReportFormatType) 7257d1cc387SAppaRao Puli { 7267d1cc387SAppaRao Puli metricReportSubCount++; 7277d1cc387SAppaRao Puli } 7287d1cc387SAppaRao Puli } 7297d1cc387SAppaRao Puli 7307d1cc387SAppaRao Puli noOfEventLogSubscribers = eventLogSubCount; 7317d1cc387SAppaRao Puli if (noOfMetricReportSubscribers != metricReportSubCount) 7327d1cc387SAppaRao Puli { 7337d1cc387SAppaRao Puli noOfMetricReportSubscribers = metricReportSubCount; 7347d1cc387SAppaRao Puli if (noOfMetricReportSubscribers) 7357d1cc387SAppaRao Puli { 7367d1cc387SAppaRao Puli registerMetricReportSignal(); 7377d1cc387SAppaRao Puli } 7387d1cc387SAppaRao Puli else 7397d1cc387SAppaRao Puli { 7407d1cc387SAppaRao Puli unregisterMetricReportSignal(); 7417d1cc387SAppaRao Puli } 7427d1cc387SAppaRao Puli } 7437d1cc387SAppaRao Puli } 7447d1cc387SAppaRao Puli 745b52664e2SAppaRao Puli std::shared_ptr<Subscription> getSubscription(const std::string& id) 746b52664e2SAppaRao Puli { 747b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 748b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 749b52664e2SAppaRao Puli { 750b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id; 751b52664e2SAppaRao Puli return nullptr; 752b52664e2SAppaRao Puli } 753b52664e2SAppaRao Puli std::shared_ptr<Subscription> subValue = obj->second; 754b52664e2SAppaRao Puli return subValue; 755b52664e2SAppaRao Puli } 756b52664e2SAppaRao Puli 7571bf712bcSAyushi Smriti std::string addSubscription(const std::shared_ptr<Subscription> subValue, 7581bf712bcSAyushi Smriti const bool updateFile = true) 759b52664e2SAppaRao Puli { 760b52664e2SAppaRao Puli std::srand(static_cast<uint32_t>(std::time(0))); 761b52664e2SAppaRao Puli std::string id; 762b52664e2SAppaRao Puli 763b52664e2SAppaRao Puli int retry = 3; 764b52664e2SAppaRao Puli while (retry) 765b52664e2SAppaRao Puli { 766b52664e2SAppaRao Puli id = std::to_string(std::rand()); 767b52664e2SAppaRao Puli auto inserted = subscriptionsMap.insert(std::pair(id, subValue)); 768b52664e2SAppaRao Puli if (inserted.second) 769b52664e2SAppaRao Puli { 770b52664e2SAppaRao Puli break; 771b52664e2SAppaRao Puli } 772b52664e2SAppaRao Puli --retry; 773b52664e2SAppaRao Puli }; 774b52664e2SAppaRao Puli 775b52664e2SAppaRao Puli if (retry <= 0) 776b52664e2SAppaRao Puli { 777b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Failed to generate random number"; 778b52664e2SAppaRao Puli return std::string(""); 779b52664e2SAppaRao Puli } 780b52664e2SAppaRao Puli 7817d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 7821bf712bcSAyushi Smriti 7831bf712bcSAyushi Smriti if (updateFile) 7841bf712bcSAyushi Smriti { 785b52664e2SAppaRao Puli updateSubscriptionData(); 7861bf712bcSAyushi Smriti } 7877f4eb588SAppaRao Puli 7887f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 7897f4eb588SAppaRao Puli if (lastEventTStr.empty()) 7907f4eb588SAppaRao Puli { 7917f4eb588SAppaRao Puli cacheLastEventTimestamp(); 7927f4eb588SAppaRao Puli } 7937f4eb588SAppaRao Puli #endif 794b52664e2SAppaRao Puli return id; 795b52664e2SAppaRao Puli } 796b52664e2SAppaRao Puli 797b52664e2SAppaRao Puli bool isSubscriptionExist(const std::string& id) 798b52664e2SAppaRao Puli { 799b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 800b52664e2SAppaRao Puli if (obj == subscriptionsMap.end()) 801b52664e2SAppaRao Puli { 802b52664e2SAppaRao Puli return false; 803b52664e2SAppaRao Puli } 804b52664e2SAppaRao Puli return true; 805b52664e2SAppaRao Puli } 806b52664e2SAppaRao Puli 807b52664e2SAppaRao Puli void deleteSubscription(const std::string& id) 808b52664e2SAppaRao Puli { 809b52664e2SAppaRao Puli auto obj = subscriptionsMap.find(id); 810b52664e2SAppaRao Puli if (obj != subscriptionsMap.end()) 811b52664e2SAppaRao Puli { 812b52664e2SAppaRao Puli subscriptionsMap.erase(obj); 8137d1cc387SAppaRao Puli updateNoOfSubscribersCount(); 814b52664e2SAppaRao Puli updateSubscriptionData(); 815b52664e2SAppaRao Puli } 816b52664e2SAppaRao Puli } 817b52664e2SAppaRao Puli 818b52664e2SAppaRao Puli size_t getNumberOfSubscriptions() 819b52664e2SAppaRao Puli { 820b52664e2SAppaRao Puli return subscriptionsMap.size(); 821b52664e2SAppaRao Puli } 822b52664e2SAppaRao Puli 823b52664e2SAppaRao Puli std::vector<std::string> getAllIDs() 824b52664e2SAppaRao Puli { 825b52664e2SAppaRao Puli std::vector<std::string> idList; 826b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 827b52664e2SAppaRao Puli { 828b52664e2SAppaRao Puli idList.emplace_back(it.first); 829b52664e2SAppaRao Puli } 830b52664e2SAppaRao Puli return idList; 831b52664e2SAppaRao Puli } 832b52664e2SAppaRao Puli 833b52664e2SAppaRao Puli bool isDestinationExist(const std::string& destUrl) 834b52664e2SAppaRao Puli { 835b52664e2SAppaRao Puli for (const auto& it : subscriptionsMap) 836b52664e2SAppaRao Puli { 837b52664e2SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 838b52664e2SAppaRao Puli if (entry->destinationUrl == destUrl) 839b52664e2SAppaRao Puli { 840b52664e2SAppaRao Puli BMCWEB_LOG_ERROR << "Destination exist already" << destUrl; 841b52664e2SAppaRao Puli return true; 842b52664e2SAppaRao Puli } 843b52664e2SAppaRao Puli } 844b52664e2SAppaRao Puli return false; 845b52664e2SAppaRao Puli } 8460b4bdd93SAppaRao Puli 8470b4bdd93SAppaRao Puli void sendTestEventLog() 8480b4bdd93SAppaRao Puli { 8490b4bdd93SAppaRao Puli for (const auto& it : this->subscriptionsMap) 8500b4bdd93SAppaRao Puli { 8510b4bdd93SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 8520b4bdd93SAppaRao Puli entry->sendTestEventLog(); 8530b4bdd93SAppaRao Puli } 8540b4bdd93SAppaRao Puli } 855e9a14131SAppaRao Puli 8567f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES 8577f4eb588SAppaRao Puli void cacheLastEventTimestamp() 8587f4eb588SAppaRao Puli { 8597f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 8607f4eb588SAppaRao Puli if (!logStream.good()) 8617f4eb588SAppaRao Puli { 8627f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed \n"; 8637f4eb588SAppaRao Puli return; 8647f4eb588SAppaRao Puli } 8657f4eb588SAppaRao Puli std::string logEntry; 8667f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 8677f4eb588SAppaRao Puli { 8687f4eb588SAppaRao Puli size_t space = logEntry.find_first_of(" "); 8697f4eb588SAppaRao Puli if (space == std::string::npos) 8707f4eb588SAppaRao Puli { 8717f4eb588SAppaRao Puli // Shouldn't enter here but lets skip it. 8727f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid log entry found."; 8737f4eb588SAppaRao Puli continue; 8747f4eb588SAppaRao Puli } 8757f4eb588SAppaRao Puli lastEventTStr = logEntry.substr(0, space); 8767f4eb588SAppaRao Puli } 8777f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Last Event time stamp set: " << lastEventTStr; 8787f4eb588SAppaRao Puli } 8797f4eb588SAppaRao Puli 8807f4eb588SAppaRao Puli void readEventLogsFromFile() 8817f4eb588SAppaRao Puli { 8827f4eb588SAppaRao Puli if (!serviceEnabled || !noOfEventLogSubscribers) 8837f4eb588SAppaRao Puli { 8847f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions."; 8857f4eb588SAppaRao Puli return; 8867f4eb588SAppaRao Puli } 8877f4eb588SAppaRao Puli std::ifstream logStream(redfishEventLogFile); 8887f4eb588SAppaRao Puli if (!logStream.good()) 8897f4eb588SAppaRao Puli { 8907f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << " Redfish log file open failed"; 8917f4eb588SAppaRao Puli return; 8927f4eb588SAppaRao Puli } 8937f4eb588SAppaRao Puli 8947f4eb588SAppaRao Puli std::vector<EventLogObjectsType> eventRecords; 8957f4eb588SAppaRao Puli 8967f4eb588SAppaRao Puli bool startLogCollection = false; 8977f4eb588SAppaRao Puli bool firstEntry = true; 8987f4eb588SAppaRao Puli 8997f4eb588SAppaRao Puli std::string logEntry; 9007f4eb588SAppaRao Puli while (std::getline(logStream, logEntry)) 9017f4eb588SAppaRao Puli { 9027f4eb588SAppaRao Puli if (!startLogCollection) 9037f4eb588SAppaRao Puli { 9047f4eb588SAppaRao Puli if (boost::starts_with(logEntry, lastEventTStr)) 9057f4eb588SAppaRao Puli { 9067f4eb588SAppaRao Puli startLogCollection = true; 9077f4eb588SAppaRao Puli } 9087f4eb588SAppaRao Puli continue; 9097f4eb588SAppaRao Puli } 9107f4eb588SAppaRao Puli 9117f4eb588SAppaRao Puli std::string idStr; 9127f4eb588SAppaRao Puli if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry)) 9137f4eb588SAppaRao Puli { 9147f4eb588SAppaRao Puli continue; 9157f4eb588SAppaRao Puli } 9167f4eb588SAppaRao Puli firstEntry = false; 9177f4eb588SAppaRao Puli 9187f4eb588SAppaRao Puli std::string timestamp; 9197f4eb588SAppaRao Puli std::string messageID; 920*5e715de6SAppaRao Puli std::vector<std::string> messageArgs; 9217f4eb588SAppaRao Puli if (event_log::getEventLogParams(logEntry, timestamp, messageID, 9227f4eb588SAppaRao Puli messageArgs) != 0) 9237f4eb588SAppaRao Puli { 9247f4eb588SAppaRao Puli BMCWEB_LOG_DEBUG << "Read eventLog entry params failed"; 9257f4eb588SAppaRao Puli continue; 9267f4eb588SAppaRao Puli } 9277f4eb588SAppaRao Puli 9287f4eb588SAppaRao Puli std::string registryName; 9297f4eb588SAppaRao Puli std::string messageKey; 9307f4eb588SAppaRao Puli event_log::getRegistryAndMessageKey(messageID, registryName, 9317f4eb588SAppaRao Puli messageKey); 9327f4eb588SAppaRao Puli if (registryName.empty() || messageKey.empty()) 9337f4eb588SAppaRao Puli { 9347f4eb588SAppaRao Puli continue; 9357f4eb588SAppaRao Puli } 9367f4eb588SAppaRao Puli 9377f4eb588SAppaRao Puli lastEventTStr = timestamp; 9387f4eb588SAppaRao Puli eventRecords.emplace_back(idStr, timestamp, messageID, registryName, 9397f4eb588SAppaRao Puli messageKey, messageArgs); 9407f4eb588SAppaRao Puli } 9417f4eb588SAppaRao Puli 9427f4eb588SAppaRao Puli for (const auto& it : this->subscriptionsMap) 9437f4eb588SAppaRao Puli { 9447f4eb588SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 9457f4eb588SAppaRao Puli if (entry->eventFormatType == "Event") 9467f4eb588SAppaRao Puli { 9477f4eb588SAppaRao Puli entry->filterAndSendEventLogs(eventRecords); 9487f4eb588SAppaRao Puli } 9497f4eb588SAppaRao Puli } 9507f4eb588SAppaRao Puli } 9517f4eb588SAppaRao Puli 9527f4eb588SAppaRao Puli static void watchRedfishEventLogFile() 9537f4eb588SAppaRao Puli { 9547f4eb588SAppaRao Puli if (inotifyConn == nullptr) 9557f4eb588SAppaRao Puli { 9567f4eb588SAppaRao Puli return; 9577f4eb588SAppaRao Puli } 9587f4eb588SAppaRao Puli 9597f4eb588SAppaRao Puli static std::array<char, 1024> readBuffer; 9607f4eb588SAppaRao Puli 9617f4eb588SAppaRao Puli inotifyConn->async_read_some( 9627f4eb588SAppaRao Puli boost::asio::buffer(readBuffer), 9637f4eb588SAppaRao Puli [&](const boost::system::error_code& ec, 9647f4eb588SAppaRao Puli const std::size_t& bytesTransferred) { 9657f4eb588SAppaRao Puli if (ec) 9667f4eb588SAppaRao Puli { 9677f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "Callback Error: " << ec.message(); 9687f4eb588SAppaRao Puli return; 9697f4eb588SAppaRao Puli } 9707f4eb588SAppaRao Puli std::size_t index = 0; 971b792cc56SAppaRao Puli while ((index + iEventSize) <= bytesTransferred) 9727f4eb588SAppaRao Puli { 9737f4eb588SAppaRao Puli struct inotify_event event; 974b792cc56SAppaRao Puli std::memcpy(&event, &readBuffer[index], iEventSize); 975b792cc56SAppaRao Puli if (event.wd == dirWatchDesc) 976b792cc56SAppaRao Puli { 977b792cc56SAppaRao Puli if ((event.len == 0) || 978b792cc56SAppaRao Puli (index + iEventSize + event.len > bytesTransferred)) 979b792cc56SAppaRao Puli { 980b792cc56SAppaRao Puli index += (iEventSize + event.len); 981b792cc56SAppaRao Puli continue; 982b792cc56SAppaRao Puli } 983b792cc56SAppaRao Puli 984b792cc56SAppaRao Puli std::string fileName(&readBuffer[index + iEventSize], 985b792cc56SAppaRao Puli event.len); 986b792cc56SAppaRao Puli if (std::strcmp(fileName.c_str(), "redfish") != 0) 987b792cc56SAppaRao Puli { 988b792cc56SAppaRao Puli index += (iEventSize + event.len); 989b792cc56SAppaRao Puli continue; 990b792cc56SAppaRao Puli } 991b792cc56SAppaRao Puli 992b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 993b792cc56SAppaRao Puli << "Redfish log file created/deleted. event.name: " 994b792cc56SAppaRao Puli << fileName; 995b792cc56SAppaRao Puli if (event.mask == IN_CREATE) 996b792cc56SAppaRao Puli { 997b792cc56SAppaRao Puli if (fileWatchDesc != -1) 998b792cc56SAppaRao Puli { 999b792cc56SAppaRao Puli BMCWEB_LOG_DEBUG 1000b792cc56SAppaRao Puli << "Redfish log file is already on " 1001b792cc56SAppaRao Puli "inotify_add_watch."; 1002b792cc56SAppaRao Puli return; 1003b792cc56SAppaRao Puli } 1004b792cc56SAppaRao Puli 1005b792cc56SAppaRao Puli fileWatchDesc = inotify_add_watch( 1006b792cc56SAppaRao Puli inotifyFd, redfishEventLogFile, IN_MODIFY); 1007b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1008b792cc56SAppaRao Puli { 1009b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1010b792cc56SAppaRao Puli << "inotify_add_watch failed for " 1011b792cc56SAppaRao Puli "redfish log file."; 1012b792cc56SAppaRao Puli return; 1013b792cc56SAppaRao Puli } 1014b792cc56SAppaRao Puli 1015b792cc56SAppaRao Puli EventServiceManager::getInstance() 1016b792cc56SAppaRao Puli .cacheLastEventTimestamp(); 1017b792cc56SAppaRao Puli EventServiceManager::getInstance() 1018b792cc56SAppaRao Puli .readEventLogsFromFile(); 1019b792cc56SAppaRao Puli } 1020b792cc56SAppaRao Puli else if ((event.mask == IN_DELETE) || 1021b792cc56SAppaRao Puli (event.mask == IN_MOVED_TO)) 1022b792cc56SAppaRao Puli { 1023b792cc56SAppaRao Puli if (fileWatchDesc != -1) 1024b792cc56SAppaRao Puli { 1025b792cc56SAppaRao Puli inotify_rm_watch(inotifyFd, fileWatchDesc); 1026b792cc56SAppaRao Puli fileWatchDesc = -1; 1027b792cc56SAppaRao Puli } 1028b792cc56SAppaRao Puli } 1029b792cc56SAppaRao Puli } 1030b792cc56SAppaRao Puli else if (event.wd == fileWatchDesc) 1031b792cc56SAppaRao Puli { 1032b792cc56SAppaRao Puli if (event.mask == IN_MODIFY) 10337f4eb588SAppaRao Puli { 10347f4eb588SAppaRao Puli EventServiceManager::getInstance() 10357f4eb588SAppaRao Puli .readEventLogsFromFile(); 10367f4eb588SAppaRao Puli } 1037b792cc56SAppaRao Puli } 1038b792cc56SAppaRao Puli index += (iEventSize + event.len); 10397f4eb588SAppaRao Puli } 10407f4eb588SAppaRao Puli 10417f4eb588SAppaRao Puli watchRedfishEventLogFile(); 10427f4eb588SAppaRao Puli }); 10437f4eb588SAppaRao Puli } 10447f4eb588SAppaRao Puli 10457f4eb588SAppaRao Puli static int startEventLogMonitor(boost::asio::io_context& ioc) 10467f4eb588SAppaRao Puli { 10477f4eb588SAppaRao Puli inotifyConn = 10487f4eb588SAppaRao Puli std::make_shared<boost::asio::posix::stream_descriptor>(ioc); 1049b792cc56SAppaRao Puli inotifyFd = inotify_init1(IN_NONBLOCK); 1050b792cc56SAppaRao Puli if (inotifyFd == -1) 10517f4eb588SAppaRao Puli { 10527f4eb588SAppaRao Puli BMCWEB_LOG_ERROR << "inotify_init1 failed."; 10537f4eb588SAppaRao Puli return -1; 10547f4eb588SAppaRao Puli } 1055b792cc56SAppaRao Puli 1056b792cc56SAppaRao Puli // Add watch on directory to handle redfish event log file 1057b792cc56SAppaRao Puli // create/delete. 1058b792cc56SAppaRao Puli dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir, 1059b792cc56SAppaRao Puli IN_CREATE | IN_MOVED_TO | IN_DELETE); 1060b792cc56SAppaRao Puli if (dirWatchDesc == -1) 10617f4eb588SAppaRao Puli { 10627f4eb588SAppaRao Puli BMCWEB_LOG_ERROR 1063b792cc56SAppaRao Puli << "inotify_add_watch failed for event log directory."; 10647f4eb588SAppaRao Puli return -1; 10657f4eb588SAppaRao Puli } 10667f4eb588SAppaRao Puli 1067b792cc56SAppaRao Puli // Watch redfish event log file for modifications. 1068b792cc56SAppaRao Puli fileWatchDesc = 1069b792cc56SAppaRao Puli inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY); 1070b792cc56SAppaRao Puli if (fileWatchDesc == -1) 1071b792cc56SAppaRao Puli { 1072b792cc56SAppaRao Puli BMCWEB_LOG_ERROR 1073b792cc56SAppaRao Puli << "inotify_add_watch failed for redfish log file."; 1074b792cc56SAppaRao Puli // Don't return error if file not exist. 1075b792cc56SAppaRao Puli // Watch on directory will handle create/delete of file. 1076b792cc56SAppaRao Puli } 1077b792cc56SAppaRao Puli 10787f4eb588SAppaRao Puli // monitor redfish event log file 1079b792cc56SAppaRao Puli inotifyConn->assign(inotifyFd); 10807f4eb588SAppaRao Puli watchRedfishEventLogFile(); 10817f4eb588SAppaRao Puli 10827f4eb588SAppaRao Puli return 0; 10837f4eb588SAppaRao Puli } 10847f4eb588SAppaRao Puli 10857f4eb588SAppaRao Puli #endif 10867f4eb588SAppaRao Puli 1087156d6b00SAppaRao Puli void getMetricReading(const std::string& service, 1088156d6b00SAppaRao Puli const std::string& objPath, const std::string& intf) 1089156d6b00SAppaRao Puli { 1090156d6b00SAppaRao Puli std::size_t found = objPath.find_last_of("/"); 1091156d6b00SAppaRao Puli if (found == std::string::npos) 1092156d6b00SAppaRao Puli { 1093156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid objPath received"; 1094156d6b00SAppaRao Puli return; 1095156d6b00SAppaRao Puli } 1096156d6b00SAppaRao Puli 1097156d6b00SAppaRao Puli std::string idStr = objPath.substr(found + 1); 1098156d6b00SAppaRao Puli if (idStr.empty()) 1099156d6b00SAppaRao Puli { 1100156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Invalid ID in objPath"; 1101156d6b00SAppaRao Puli return; 1102156d6b00SAppaRao Puli } 1103156d6b00SAppaRao Puli 1104156d6b00SAppaRao Puli crow::connections::systemBus->async_method_call( 1105156d6b00SAppaRao Puli [idStr{std::move(idStr)}]( 1106156d6b00SAppaRao Puli const boost::system::error_code ec, 1107156d6b00SAppaRao Puli boost::container::flat_map< 1108156d6b00SAppaRao Puli std::string, std::variant<std::string, ReadingsObjType>>& 1109156d6b00SAppaRao Puli resp) { 1110156d6b00SAppaRao Puli if (ec) 1111156d6b00SAppaRao Puli { 1112156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG 1113156d6b00SAppaRao Puli << "D-Bus call failed to GetAll metric readings."; 1114156d6b00SAppaRao Puli return; 1115156d6b00SAppaRao Puli } 1116156d6b00SAppaRao Puli 1117156d6b00SAppaRao Puli const std::string* timestampPtr = 1118156d6b00SAppaRao Puli std::get_if<std::string>(&resp["Timestamp"]); 1119156d6b00SAppaRao Puli if (!timestampPtr) 1120156d6b00SAppaRao Puli { 1121156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Failed to Get timestamp."; 1122156d6b00SAppaRao Puli return; 1123156d6b00SAppaRao Puli } 1124156d6b00SAppaRao Puli 1125156d6b00SAppaRao Puli ReadingsObjType* readingsPtr = 1126156d6b00SAppaRao Puli std::get_if<ReadingsObjType>(&resp["Readings"]); 1127156d6b00SAppaRao Puli if (!readingsPtr) 1128156d6b00SAppaRao Puli { 1129156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Failed to Get Readings property."; 1130156d6b00SAppaRao Puli return; 1131156d6b00SAppaRao Puli } 1132156d6b00SAppaRao Puli 1133156d6b00SAppaRao Puli if (!readingsPtr->size()) 1134156d6b00SAppaRao Puli { 1135156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "No metrics report to be transferred"; 1136156d6b00SAppaRao Puli return; 1137156d6b00SAppaRao Puli } 1138156d6b00SAppaRao Puli 1139156d6b00SAppaRao Puli for (const auto& it : 1140156d6b00SAppaRao Puli EventServiceManager::getInstance().subscriptionsMap) 1141156d6b00SAppaRao Puli { 1142156d6b00SAppaRao Puli std::shared_ptr<Subscription> entry = it.second; 1143156d6b00SAppaRao Puli if (entry->eventFormatType == metricReportFormatType) 1144156d6b00SAppaRao Puli { 1145156d6b00SAppaRao Puli entry->filterAndSendReports(idStr, *timestampPtr, 1146156d6b00SAppaRao Puli *readingsPtr); 1147156d6b00SAppaRao Puli } 1148156d6b00SAppaRao Puli } 1149156d6b00SAppaRao Puli }, 1150156d6b00SAppaRao Puli service, objPath, "org.freedesktop.DBus.Properties", "GetAll", 1151156d6b00SAppaRao Puli intf); 1152156d6b00SAppaRao Puli } 1153156d6b00SAppaRao Puli 1154156d6b00SAppaRao Puli void unregisterMetricReportSignal() 1155156d6b00SAppaRao Puli { 11567d1cc387SAppaRao Puli if (matchTelemetryMonitor) 11577d1cc387SAppaRao Puli { 1158156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister"; 1159156d6b00SAppaRao Puli matchTelemetryMonitor.reset(); 1160156d6b00SAppaRao Puli matchTelemetryMonitor = nullptr; 1161156d6b00SAppaRao Puli } 11627d1cc387SAppaRao Puli } 1163156d6b00SAppaRao Puli 1164156d6b00SAppaRao Puli void registerMetricReportSignal() 1165156d6b00SAppaRao Puli { 11667d1cc387SAppaRao Puli if (!serviceEnabled || matchTelemetryMonitor) 1167156d6b00SAppaRao Puli { 11687d1cc387SAppaRao Puli BMCWEB_LOG_DEBUG << "Not registering metric report signal."; 1169156d6b00SAppaRao Puli return; 1170156d6b00SAppaRao Puli } 1171156d6b00SAppaRao Puli 1172156d6b00SAppaRao Puli BMCWEB_LOG_DEBUG << "Metrics report signal - Register"; 1173156d6b00SAppaRao Puli std::string matchStr( 1174156d6b00SAppaRao Puli "type='signal',member='ReportUpdate', " 1175156d6b00SAppaRao Puli "interface='xyz.openbmc_project.MonitoringService.Report'"); 1176156d6b00SAppaRao Puli 1177156d6b00SAppaRao Puli matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>( 1178156d6b00SAppaRao Puli *crow::connections::systemBus, matchStr, 1179156d6b00SAppaRao Puli [this](sdbusplus::message::message& msg) { 1180156d6b00SAppaRao Puli if (msg.is_method_error()) 1181156d6b00SAppaRao Puli { 1182156d6b00SAppaRao Puli BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error"; 1183156d6b00SAppaRao Puli return; 1184156d6b00SAppaRao Puli } 1185156d6b00SAppaRao Puli 1186156d6b00SAppaRao Puli std::string service = msg.get_sender(); 1187156d6b00SAppaRao Puli std::string objPath = msg.get_path(); 1188156d6b00SAppaRao Puli std::string intf = msg.get_interface(); 1189156d6b00SAppaRao Puli getMetricReading(service, objPath, intf); 1190156d6b00SAppaRao Puli }); 1191156d6b00SAppaRao Puli } 11921bf712bcSAyushi Smriti 11931bf712bcSAyushi Smriti bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto, 11941bf712bcSAyushi Smriti std::string& host, std::string& port, 11951bf712bcSAyushi Smriti std::string& path) 11961bf712bcSAyushi Smriti { 11971bf712bcSAyushi Smriti // Validate URL using regex expression 11981bf712bcSAyushi Smriti // Format: <protocol>://<host>:<port>/<path> 11991bf712bcSAyushi Smriti // protocol: http/https 12001bf712bcSAyushi Smriti const std::regex urlRegex( 12011bf712bcSAyushi Smriti "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/" 12021bf712bcSAyushi Smriti "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)"); 12031bf712bcSAyushi Smriti std::cmatch match; 12041bf712bcSAyushi Smriti if (!std::regex_match(destUrl.c_str(), match, urlRegex)) 12051bf712bcSAyushi Smriti { 12061bf712bcSAyushi Smriti BMCWEB_LOG_INFO << "Dest. url did not match "; 12071bf712bcSAyushi Smriti return false; 12081bf712bcSAyushi Smriti } 12091bf712bcSAyushi Smriti 12101bf712bcSAyushi Smriti urlProto = std::string(match[1].first, match[1].second); 12111bf712bcSAyushi Smriti if (urlProto == "http") 12121bf712bcSAyushi Smriti { 12131bf712bcSAyushi Smriti #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING 12141bf712bcSAyushi Smriti return false; 12151bf712bcSAyushi Smriti #endif 12161bf712bcSAyushi Smriti } 12171bf712bcSAyushi Smriti 12181bf712bcSAyushi Smriti host = std::string(match[2].first, match[2].second); 12191bf712bcSAyushi Smriti port = std::string(match[3].first, match[3].second); 12201bf712bcSAyushi Smriti path = std::string(match[4].first, match[4].second); 12211bf712bcSAyushi Smriti if (port.empty()) 12221bf712bcSAyushi Smriti { 12231bf712bcSAyushi Smriti if (urlProto == "http") 12241bf712bcSAyushi Smriti { 12251bf712bcSAyushi Smriti port = "80"; 12261bf712bcSAyushi Smriti } 12271bf712bcSAyushi Smriti else 12281bf712bcSAyushi Smriti { 12291bf712bcSAyushi Smriti port = "443"; 12301bf712bcSAyushi Smriti } 12311bf712bcSAyushi Smriti } 12321bf712bcSAyushi Smriti if (path.empty()) 12331bf712bcSAyushi Smriti { 12341bf712bcSAyushi Smriti path = "/"; 12351bf712bcSAyushi Smriti } 12361bf712bcSAyushi Smriti return true; 12371bf712bcSAyushi Smriti } 12387f4eb588SAppaRao Puli }; // namespace redfish 1239b52664e2SAppaRao Puli 1240b52664e2SAppaRao Puli } // namespace redfish 1241