xref: /openbmc/bmcweb/features/redfish/include/event_service_manager.hpp (revision 4bbf237f4217afd1a0fa8c474a921a9175732b13)
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>
28*4bbf237fSAppaRao Puli #include <server_sent_events.hpp>
291214b7e7SGunnar Mills #include <utils/json_utils.hpp>
301214b7e7SGunnar Mills 
31b52664e2SAppaRao Puli #include <cstdlib>
32b52664e2SAppaRao Puli #include <ctime>
331bf712bcSAyushi Smriti #include <fstream>
34b52664e2SAppaRao Puli #include <memory>
35b52664e2SAppaRao Puli #include <variant>
36b52664e2SAppaRao Puli 
37b52664e2SAppaRao Puli namespace redfish
38b52664e2SAppaRao Puli {
39156d6b00SAppaRao Puli 
40156d6b00SAppaRao Puli using ReadingsObjType =
41156d6b00SAppaRao Puli     std::vector<std::tuple<std::string, std::string, double, std::string>>;
427d1cc387SAppaRao Puli using EventServiceConfig = std::tuple<bool, uint32_t, uint32_t>;
43156d6b00SAppaRao Puli 
44156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event";
45156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport";
46156d6b00SAppaRao Puli 
471bf712bcSAyushi Smriti static constexpr const char* eventServiceFile =
481bf712bcSAyushi Smriti     "/var/lib/bmcweb/eventservice_config.json";
491bf712bcSAyushi Smriti 
507f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
517f4eb588SAppaRao Puli std::shared_ptr<boost::asio::posix::stream_descriptor> inotifyConn = nullptr;
52b792cc56SAppaRao Puli static constexpr const char* redfishEventLogDir = "/var/log";
53b792cc56SAppaRao Puli static constexpr const char* redfishEventLogFile = "/var/log/redfish";
54b792cc56SAppaRao Puli static constexpr const size_t iEventSize = sizeof(inotify_event);
55b792cc56SAppaRao Puli static int inotifyFd = -1;
56b792cc56SAppaRao Puli static int dirWatchDesc = -1;
57b792cc56SAppaRao Puli static int fileWatchDesc = -1;
587f4eb588SAppaRao Puli 
597f4eb588SAppaRao Puli // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs>
607f4eb588SAppaRao Puli using EventLogObjectsType =
617f4eb588SAppaRao Puli     std::tuple<std::string, std::string, std::string, std::string, std::string,
625e715de6SAppaRao Puli                std::vector<std::string>>;
637f4eb588SAppaRao Puli 
647f4eb588SAppaRao Puli namespace message_registries
657f4eb588SAppaRao Puli {
667f4eb588SAppaRao Puli static const Message*
677f4eb588SAppaRao Puli     getMsgFromRegistry(const std::string& messageKey,
687f4eb588SAppaRao Puli                        const boost::beast::span<const MessageEntry>& registry)
697f4eb588SAppaRao Puli {
707f4eb588SAppaRao Puli     boost::beast::span<const MessageEntry>::const_iterator messageIt =
717f4eb588SAppaRao Puli         std::find_if(registry.cbegin(), registry.cend(),
727f4eb588SAppaRao Puli                      [&messageKey](const MessageEntry& messageEntry) {
737f4eb588SAppaRao Puli                          return !messageKey.compare(messageEntry.first);
747f4eb588SAppaRao Puli                      });
757f4eb588SAppaRao Puli     if (messageIt != registry.cend())
767f4eb588SAppaRao Puli     {
777f4eb588SAppaRao Puli         return &messageIt->second;
787f4eb588SAppaRao Puli     }
797f4eb588SAppaRao Puli 
807f4eb588SAppaRao Puli     return nullptr;
817f4eb588SAppaRao Puli }
827f4eb588SAppaRao Puli 
837f4eb588SAppaRao Puli static const Message* formatMessage(const std::string_view& messageID)
847f4eb588SAppaRao Puli {
857f4eb588SAppaRao Puli     // Redfish MessageIds are in the form
867f4eb588SAppaRao Puli     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
877f4eb588SAppaRao Puli     // the right Message
887f4eb588SAppaRao Puli     std::vector<std::string> fields;
897f4eb588SAppaRao Puli     fields.reserve(4);
907f4eb588SAppaRao Puli     boost::split(fields, messageID, boost::is_any_of("."));
917f4eb588SAppaRao Puli     if (fields.size() != 4)
927f4eb588SAppaRao Puli     {
937f4eb588SAppaRao Puli         return nullptr;
947f4eb588SAppaRao Puli     }
957f4eb588SAppaRao Puli     std::string& registryName = fields[0];
967f4eb588SAppaRao Puli     std::string& messageKey = fields[3];
977f4eb588SAppaRao Puli 
987f4eb588SAppaRao Puli     // Find the right registry and check it for the MessageKey
997f4eb588SAppaRao Puli     if (std::string(base::header.registryPrefix) == registryName)
1007f4eb588SAppaRao Puli     {
1017f4eb588SAppaRao Puli         return getMsgFromRegistry(
1027f4eb588SAppaRao Puli             messageKey, boost::beast::span<const MessageEntry>(base::registry));
1037f4eb588SAppaRao Puli     }
1047f4eb588SAppaRao Puli     if (std::string(openbmc::header.registryPrefix) == registryName)
1057f4eb588SAppaRao Puli     {
1067f4eb588SAppaRao Puli         return getMsgFromRegistry(
1077f4eb588SAppaRao Puli             messageKey,
1087f4eb588SAppaRao Puli             boost::beast::span<const MessageEntry>(openbmc::registry));
1097f4eb588SAppaRao Puli     }
1107f4eb588SAppaRao Puli     return nullptr;
1117f4eb588SAppaRao Puli }
1127f4eb588SAppaRao Puli } // namespace message_registries
1137f4eb588SAppaRao Puli 
1147f4eb588SAppaRao Puli namespace event_log
1157f4eb588SAppaRao Puli {
1167f4eb588SAppaRao Puli bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
1177f4eb588SAppaRao Puli                       const bool firstEntry = true)
1187f4eb588SAppaRao Puli {
1197f4eb588SAppaRao Puli     static time_t prevTs = 0;
1207f4eb588SAppaRao Puli     static int index = 0;
1217f4eb588SAppaRao Puli     if (firstEntry)
1227f4eb588SAppaRao Puli     {
1237f4eb588SAppaRao Puli         prevTs = 0;
1247f4eb588SAppaRao Puli     }
1257f4eb588SAppaRao Puli 
1267f4eb588SAppaRao Puli     // Get the entry timestamp
1277f4eb588SAppaRao Puli     std::time_t curTs = 0;
1287f4eb588SAppaRao Puli     std::tm timeStruct = {};
1297f4eb588SAppaRao Puli     std::istringstream entryStream(logEntry);
1307f4eb588SAppaRao Puli     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1317f4eb588SAppaRao Puli     {
1327f4eb588SAppaRao Puli         curTs = std::mktime(&timeStruct);
1337f4eb588SAppaRao Puli         if (curTs == -1)
1347f4eb588SAppaRao Puli         {
1357f4eb588SAppaRao Puli             return false;
1367f4eb588SAppaRao Puli         }
1377f4eb588SAppaRao Puli     }
1387f4eb588SAppaRao Puli     // If the timestamp isn't unique, increment the index
1397f4eb588SAppaRao Puli     index = (curTs == prevTs) ? index + 1 : 0;
1407f4eb588SAppaRao Puli 
1417f4eb588SAppaRao Puli     // Save the timestamp
1427f4eb588SAppaRao Puli     prevTs = curTs;
1437f4eb588SAppaRao Puli 
1447f4eb588SAppaRao Puli     entryID = std::to_string(curTs);
1457f4eb588SAppaRao Puli     if (index > 0)
1467f4eb588SAppaRao Puli     {
1477f4eb588SAppaRao Puli         entryID += "_" + std::to_string(index);
1487f4eb588SAppaRao Puli     }
1497f4eb588SAppaRao Puli     return true;
1507f4eb588SAppaRao Puli }
1517f4eb588SAppaRao Puli 
1527f4eb588SAppaRao Puli int getEventLogParams(const std::string& logEntry, std::string& timestamp,
1537f4eb588SAppaRao Puli                       std::string& messageID,
1545e715de6SAppaRao Puli                       std::vector<std::string>& messageArgs)
1557f4eb588SAppaRao Puli {
1567f4eb588SAppaRao Puli     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1577f4eb588SAppaRao Puli     // First get the Timestamp
1587f4eb588SAppaRao Puli     size_t space = logEntry.find_first_of(" ");
1597f4eb588SAppaRao Puli     if (space == std::string::npos)
1607f4eb588SAppaRao Puli     {
1617f4eb588SAppaRao Puli         return -EINVAL;
1627f4eb588SAppaRao Puli     }
1637f4eb588SAppaRao Puli     timestamp = logEntry.substr(0, space);
1647f4eb588SAppaRao Puli     // Then get the log contents
1657f4eb588SAppaRao Puli     size_t entryStart = logEntry.find_first_not_of(" ", space);
1667f4eb588SAppaRao Puli     if (entryStart == std::string::npos)
1677f4eb588SAppaRao Puli     {
1687f4eb588SAppaRao Puli         return -EINVAL;
1697f4eb588SAppaRao Puli     }
1707f4eb588SAppaRao Puli     std::string_view entry(logEntry);
1717f4eb588SAppaRao Puli     entry.remove_prefix(entryStart);
1727f4eb588SAppaRao Puli     // Use split to separate the entry into its fields
1737f4eb588SAppaRao Puli     std::vector<std::string> logEntryFields;
1747f4eb588SAppaRao Puli     boost::split(logEntryFields, entry, boost::is_any_of(","),
1757f4eb588SAppaRao Puli                  boost::token_compress_on);
1767f4eb588SAppaRao Puli     // We need at least a MessageId to be valid
1777f4eb588SAppaRao Puli     if (logEntryFields.size() < 1)
1787f4eb588SAppaRao Puli     {
1797f4eb588SAppaRao Puli         return -EINVAL;
1807f4eb588SAppaRao Puli     }
1817f4eb588SAppaRao Puli     messageID = logEntryFields[0];
1827f4eb588SAppaRao Puli 
1837f4eb588SAppaRao Puli     // Get the MessageArgs from the log if there are any
1847f4eb588SAppaRao Puli     if (logEntryFields.size() > 1)
1857f4eb588SAppaRao Puli     {
1867f4eb588SAppaRao Puli         std::string& messageArgsStart = logEntryFields[1];
1877f4eb588SAppaRao Puli         // If the first string is empty, assume there are no MessageArgs
1887f4eb588SAppaRao Puli         if (!messageArgsStart.empty())
1897f4eb588SAppaRao Puli         {
1905e715de6SAppaRao Puli             messageArgs.assign(logEntryFields.begin() + 1,
1915e715de6SAppaRao Puli                                logEntryFields.end());
1927f4eb588SAppaRao Puli         }
1937f4eb588SAppaRao Puli     }
1947f4eb588SAppaRao Puli 
1957f4eb588SAppaRao Puli     return 0;
1967f4eb588SAppaRao Puli }
1977f4eb588SAppaRao Puli 
1987f4eb588SAppaRao Puli void getRegistryAndMessageKey(const std::string& messageID,
1997f4eb588SAppaRao Puli                               std::string& registryName,
2007f4eb588SAppaRao Puli                               std::string& messageKey)
2017f4eb588SAppaRao Puli {
2027f4eb588SAppaRao Puli     // Redfish MessageIds are in the form
2037f4eb588SAppaRao Puli     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
2047f4eb588SAppaRao Puli     // the right Message
2057f4eb588SAppaRao Puli     std::vector<std::string> fields;
2067f4eb588SAppaRao Puli     fields.reserve(4);
2077f4eb588SAppaRao Puli     boost::split(fields, messageID, boost::is_any_of("."));
2087f4eb588SAppaRao Puli     if (fields.size() == 4)
2097f4eb588SAppaRao Puli     {
2107f4eb588SAppaRao Puli         registryName = fields[0];
2117f4eb588SAppaRao Puli         messageKey = fields[3];
2127f4eb588SAppaRao Puli     }
2137f4eb588SAppaRao Puli }
2147f4eb588SAppaRao Puli 
2157f4eb588SAppaRao Puli int formatEventLogEntry(const std::string& logEntryID,
2167f4eb588SAppaRao Puli                         const std::string& messageID,
2175e715de6SAppaRao Puli                         const std::vector<std::string>& messageArgs,
2187f4eb588SAppaRao Puli                         std::string timestamp, const std::string customText,
2197f4eb588SAppaRao Puli                         nlohmann::json& logEntryJson)
2207f4eb588SAppaRao Puli {
2217f4eb588SAppaRao Puli     // Get the Message from the MessageRegistry
2227f4eb588SAppaRao Puli     const message_registries::Message* message =
2237f4eb588SAppaRao Puli         message_registries::formatMessage(messageID);
2247f4eb588SAppaRao Puli 
2257f4eb588SAppaRao Puli     std::string msg;
2267f4eb588SAppaRao Puli     std::string severity;
2277f4eb588SAppaRao Puli     if (message != nullptr)
2287f4eb588SAppaRao Puli     {
2297f4eb588SAppaRao Puli         msg = message->message;
2307f4eb588SAppaRao Puli         severity = message->severity;
2317f4eb588SAppaRao Puli     }
2327f4eb588SAppaRao Puli 
2337f4eb588SAppaRao Puli     // Fill the MessageArgs into the Message
2347f4eb588SAppaRao Puli     int i = 0;
2357f4eb588SAppaRao Puli     for (const std::string& messageArg : messageArgs)
2367f4eb588SAppaRao Puli     {
2377f4eb588SAppaRao Puli         std::string argStr = "%" + std::to_string(++i);
2387f4eb588SAppaRao Puli         size_t argPos = msg.find(argStr);
2397f4eb588SAppaRao Puli         if (argPos != std::string::npos)
2407f4eb588SAppaRao Puli         {
2417f4eb588SAppaRao Puli             msg.replace(argPos, argStr.length(), messageArg);
2427f4eb588SAppaRao Puli         }
2437f4eb588SAppaRao Puli     }
2447f4eb588SAppaRao Puli 
2457f4eb588SAppaRao Puli     // Get the Created time from the timestamp. The log timestamp is in
2467f4eb588SAppaRao Puli     // RFC3339 format which matches the Redfish format except for the
2477f4eb588SAppaRao Puli     // fractional seconds between the '.' and the '+', so just remove them.
2487f4eb588SAppaRao Puli     std::size_t dot = timestamp.find_first_of(".");
2497f4eb588SAppaRao Puli     std::size_t plus = timestamp.find_first_of("+");
2507f4eb588SAppaRao Puli     if (dot != std::string::npos && plus != std::string::npos)
2517f4eb588SAppaRao Puli     {
2527f4eb588SAppaRao Puli         timestamp.erase(dot, plus - dot);
2537f4eb588SAppaRao Puli     }
2547f4eb588SAppaRao Puli 
2557f4eb588SAppaRao Puli     // Fill in the log entry with the gathered data
2567f4eb588SAppaRao Puli     logEntryJson = {{"EventId", logEntryID},
2577f4eb588SAppaRao Puli                     {"EventType", "Event"},
2587f4eb588SAppaRao Puli                     {"Severity", std::move(severity)},
2597f4eb588SAppaRao Puli                     {"Message", std::move(msg)},
2607f4eb588SAppaRao Puli                     {"MessageId", std::move(messageID)},
2617f4eb588SAppaRao Puli                     {"MessageArgs", std::move(messageArgs)},
2627f4eb588SAppaRao Puli                     {"EventTimestamp", std::move(timestamp)},
2637f4eb588SAppaRao Puli                     {"Context", customText}};
2647f4eb588SAppaRao Puli     return 0;
2657f4eb588SAppaRao Puli }
2667f4eb588SAppaRao Puli 
2677f4eb588SAppaRao Puli } // namespace event_log
2687f4eb588SAppaRao Puli #endif
2697f4eb588SAppaRao Puli 
270b52664e2SAppaRao Puli class Subscription
271b52664e2SAppaRao Puli {
272b52664e2SAppaRao Puli   public:
273b52664e2SAppaRao Puli     std::string id;
274b52664e2SAppaRao Puli     std::string destinationUrl;
275b52664e2SAppaRao Puli     std::string protocol;
276b52664e2SAppaRao Puli     std::string retryPolicy;
277b52664e2SAppaRao Puli     std::string customText;
278b52664e2SAppaRao Puli     std::string eventFormatType;
279b52664e2SAppaRao Puli     std::string subscriptionType;
280b52664e2SAppaRao Puli     std::vector<std::string> registryMsgIds;
281b52664e2SAppaRao Puli     std::vector<std::string> registryPrefixes;
282b52664e2SAppaRao Puli     std::vector<nlohmann::json> httpHeaders; // key-value pair
283156d6b00SAppaRao Puli     std::vector<nlohmann::json> metricReportDefinitions;
284b52664e2SAppaRao Puli 
285b52664e2SAppaRao Puli     Subscription(const Subscription&) = delete;
286b52664e2SAppaRao Puli     Subscription& operator=(const Subscription&) = delete;
287b52664e2SAppaRao Puli     Subscription(Subscription&&) = delete;
288b52664e2SAppaRao Puli     Subscription& operator=(Subscription&&) = delete;
289b52664e2SAppaRao Puli 
290b52664e2SAppaRao Puli     Subscription(const std::string& inHost, const std::string& inPort,
291b52664e2SAppaRao Puli                  const std::string& inPath, const std::string& inUriProto) :
2920b4bdd93SAppaRao Puli         eventSeqNum(1),
2930b4bdd93SAppaRao Puli         host(inHost), port(inPort), path(inPath), uriProto(inUriProto)
294b52664e2SAppaRao Puli     {
295b52664e2SAppaRao Puli         conn = std::make_shared<crow::HttpClient>(
296fe44eb0bSAyushi Smriti             crow::connections::systemBus->get_io_context(), id, host, port,
297fe44eb0bSAyushi Smriti             path);
298b52664e2SAppaRao Puli     }
299*4bbf237fSAppaRao Puli 
300*4bbf237fSAppaRao Puli     Subscription(const std::shared_ptr<crow::Request::Adaptor>& adaptor) :
301*4bbf237fSAppaRao Puli         eventSeqNum(1)
302*4bbf237fSAppaRao Puli     {
303*4bbf237fSAppaRao Puli         sseConn = std::make_shared<crow::ServerSentEvents>(adaptor);
304*4bbf237fSAppaRao Puli     }
305*4bbf237fSAppaRao Puli 
306b52664e2SAppaRao Puli     ~Subscription()
3071214b7e7SGunnar Mills     {}
308b52664e2SAppaRao Puli 
309b52664e2SAppaRao Puli     void sendEvent(const std::string& msg)
310b52664e2SAppaRao Puli     {
311*4bbf237fSAppaRao Puli         if (conn != nullptr)
312*4bbf237fSAppaRao Puli         {
313b52664e2SAppaRao Puli             std::vector<std::pair<std::string, std::string>> reqHeaders;
314b52664e2SAppaRao Puli             for (const auto& header : httpHeaders)
315b52664e2SAppaRao Puli             {
316b52664e2SAppaRao Puli                 for (const auto& item : header.items())
317b52664e2SAppaRao Puli                 {
318b52664e2SAppaRao Puli                     std::string key = item.key();
319b52664e2SAppaRao Puli                     std::string val = item.value();
320b52664e2SAppaRao Puli                     reqHeaders.emplace_back(std::pair(key, val));
321b52664e2SAppaRao Puli                 }
322b52664e2SAppaRao Puli             }
323b52664e2SAppaRao Puli             conn->setHeaders(reqHeaders);
3242a5689a7SAppaRao Puli             conn->sendData(msg);
325b52664e2SAppaRao Puli         }
326b52664e2SAppaRao Puli 
327*4bbf237fSAppaRao Puli         if (sseConn != nullptr)
328*4bbf237fSAppaRao Puli         {
329*4bbf237fSAppaRao Puli             sseConn->sendData(eventSeqNum, msg);
330*4bbf237fSAppaRao Puli         }
331*4bbf237fSAppaRao Puli     }
332*4bbf237fSAppaRao Puli 
3330b4bdd93SAppaRao Puli     void sendTestEventLog()
3340b4bdd93SAppaRao Puli     {
3350b4bdd93SAppaRao Puli         nlohmann::json logEntryArray;
3360b4bdd93SAppaRao Puli         logEntryArray.push_back({});
3370b4bdd93SAppaRao Puli         nlohmann::json& logEntryJson = logEntryArray.back();
3380b4bdd93SAppaRao Puli 
3390b4bdd93SAppaRao Puli         logEntryJson = {{"EventId", "TestID"},
3400b4bdd93SAppaRao Puli                         {"EventType", "Event"},
3410b4bdd93SAppaRao Puli                         {"Severity", "OK"},
3420b4bdd93SAppaRao Puli                         {"Message", "Generated test event"},
3430b4bdd93SAppaRao Puli                         {"MessageId", "OpenBMC.0.1.TestEventLog"},
3440b4bdd93SAppaRao Puli                         {"MessageArgs", nlohmann::json::array()},
3450b4bdd93SAppaRao Puli                         {"EventTimestamp", crow::utility::dateTimeNow()},
3460b4bdd93SAppaRao Puli                         {"Context", customText}};
3470b4bdd93SAppaRao Puli 
3480b4bdd93SAppaRao Puli         nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"},
3490b4bdd93SAppaRao Puli                               {"Id", std::to_string(eventSeqNum)},
3500b4bdd93SAppaRao Puli                               {"Name", "Event Log"},
3510b4bdd93SAppaRao Puli                               {"Events", logEntryArray}};
3520b4bdd93SAppaRao Puli 
3530b4bdd93SAppaRao Puli         this->sendEvent(msg.dump());
3540b4bdd93SAppaRao Puli         this->eventSeqNum++;
3550b4bdd93SAppaRao Puli     }
3560b4bdd93SAppaRao Puli 
3577f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
3587f4eb588SAppaRao Puli     void filterAndSendEventLogs(
3597f4eb588SAppaRao Puli         const std::vector<EventLogObjectsType>& eventRecords)
3607f4eb588SAppaRao Puli     {
3617f4eb588SAppaRao Puli         nlohmann::json logEntryArray;
3627f4eb588SAppaRao Puli         for (const EventLogObjectsType& logEntry : eventRecords)
3637f4eb588SAppaRao Puli         {
3647f4eb588SAppaRao Puli             const std::string& idStr = std::get<0>(logEntry);
3657f4eb588SAppaRao Puli             const std::string& timestamp = std::get<1>(logEntry);
3667f4eb588SAppaRao Puli             const std::string& messageID = std::get<2>(logEntry);
3677f4eb588SAppaRao Puli             const std::string& registryName = std::get<3>(logEntry);
3687f4eb588SAppaRao Puli             const std::string& messageKey = std::get<4>(logEntry);
3695e715de6SAppaRao Puli             const std::vector<std::string>& messageArgs = std::get<5>(logEntry);
3707f4eb588SAppaRao Puli 
3717f4eb588SAppaRao Puli             // If registryPrefixes list is empty, don't filter events
3727f4eb588SAppaRao Puli             // send everything.
3737f4eb588SAppaRao Puli             if (registryPrefixes.size())
3747f4eb588SAppaRao Puli             {
3757f4eb588SAppaRao Puli                 auto obj = std::find(registryPrefixes.begin(),
3767f4eb588SAppaRao Puli                                      registryPrefixes.end(), registryName);
3777f4eb588SAppaRao Puli                 if (obj == registryPrefixes.end())
3787f4eb588SAppaRao Puli                 {
3797f4eb588SAppaRao Puli                     continue;
3807f4eb588SAppaRao Puli                 }
3817f4eb588SAppaRao Puli             }
3827f4eb588SAppaRao Puli 
3837f4eb588SAppaRao Puli             // If registryMsgIds list is empty, don't filter events
3847f4eb588SAppaRao Puli             // send everything.
3857f4eb588SAppaRao Puli             if (registryMsgIds.size())
3867f4eb588SAppaRao Puli             {
3877f4eb588SAppaRao Puli                 auto obj = std::find(registryMsgIds.begin(),
3887f4eb588SAppaRao Puli                                      registryMsgIds.end(), messageKey);
3897f4eb588SAppaRao Puli                 if (obj == registryMsgIds.end())
3907f4eb588SAppaRao Puli                 {
3917f4eb588SAppaRao Puli                     continue;
3927f4eb588SAppaRao Puli                 }
3937f4eb588SAppaRao Puli             }
3947f4eb588SAppaRao Puli 
3957f4eb588SAppaRao Puli             logEntryArray.push_back({});
3967f4eb588SAppaRao Puli             nlohmann::json& bmcLogEntry = logEntryArray.back();
3977f4eb588SAppaRao Puli             if (event_log::formatEventLogEntry(idStr, messageID, messageArgs,
3987f4eb588SAppaRao Puli                                                timestamp, customText,
3997f4eb588SAppaRao Puli                                                bmcLogEntry) != 0)
4007f4eb588SAppaRao Puli             {
4017f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Read eventLog entry failed";
4027f4eb588SAppaRao Puli                 continue;
4037f4eb588SAppaRao Puli             }
4047f4eb588SAppaRao Puli         }
4057f4eb588SAppaRao Puli 
4067f4eb588SAppaRao Puli         if (logEntryArray.size() < 1)
4077f4eb588SAppaRao Puli         {
4087f4eb588SAppaRao Puli             BMCWEB_LOG_DEBUG << "No log entries available to be transferred.";
4097f4eb588SAppaRao Puli             return;
4107f4eb588SAppaRao Puli         }
4117f4eb588SAppaRao Puli 
4127f4eb588SAppaRao Puli         nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"},
4137f4eb588SAppaRao Puli                               {"Id", std::to_string(eventSeqNum)},
4147f4eb588SAppaRao Puli                               {"Name", "Event Log"},
4157f4eb588SAppaRao Puli                               {"Events", logEntryArray}};
4167f4eb588SAppaRao Puli 
4177f4eb588SAppaRao Puli         this->sendEvent(msg.dump());
4187f4eb588SAppaRao Puli         this->eventSeqNum++;
4197f4eb588SAppaRao Puli     }
4207f4eb588SAppaRao Puli #endif
4217f4eb588SAppaRao Puli 
422156d6b00SAppaRao Puli     void filterAndSendReports(const std::string& id,
423156d6b00SAppaRao Puli                               const std::string& readingsTs,
424156d6b00SAppaRao Puli                               const ReadingsObjType& readings)
425156d6b00SAppaRao Puli     {
426156d6b00SAppaRao Puli         std::string metricReportDef =
427156d6b00SAppaRao Puli             "/redfish/v1/TelemetryService/MetricReportDefinitions/" + id;
428156d6b00SAppaRao Puli 
429156d6b00SAppaRao Puli         // Empty list means no filter. Send everything.
430156d6b00SAppaRao Puli         if (metricReportDefinitions.size())
431156d6b00SAppaRao Puli         {
432156d6b00SAppaRao Puli             if (std::find(metricReportDefinitions.begin(),
433156d6b00SAppaRao Puli                           metricReportDefinitions.end(),
434156d6b00SAppaRao Puli                           metricReportDef) == metricReportDefinitions.end())
435156d6b00SAppaRao Puli             {
436156d6b00SAppaRao Puli                 return;
437156d6b00SAppaRao Puli             }
438156d6b00SAppaRao Puli         }
439156d6b00SAppaRao Puli 
440156d6b00SAppaRao Puli         nlohmann::json metricValuesArray = nlohmann::json::array();
441156d6b00SAppaRao Puli         for (const auto& it : readings)
442156d6b00SAppaRao Puli         {
443156d6b00SAppaRao Puli             metricValuesArray.push_back({});
444156d6b00SAppaRao Puli             nlohmann::json& entry = metricValuesArray.back();
445156d6b00SAppaRao Puli 
446156d6b00SAppaRao Puli             entry = {{"MetricId", std::get<0>(it)},
447156d6b00SAppaRao Puli                      {"MetricProperty", std::get<1>(it)},
448156d6b00SAppaRao Puli                      {"MetricValue", std::to_string(std::get<2>(it))},
449156d6b00SAppaRao Puli                      {"Timestamp", std::get<3>(it)}};
450156d6b00SAppaRao Puli         }
451156d6b00SAppaRao Puli 
452156d6b00SAppaRao Puli         nlohmann::json msg = {
453156d6b00SAppaRao Puli             {"@odata.id", "/redfish/v1/TelemetryService/MetricReports/" + id},
454156d6b00SAppaRao Puli             {"@odata.type", "#MetricReport.v1_3_0.MetricReport"},
455156d6b00SAppaRao Puli             {"Id", id},
456156d6b00SAppaRao Puli             {"Name", id},
457156d6b00SAppaRao Puli             {"Timestamp", readingsTs},
458156d6b00SAppaRao Puli             {"MetricReportDefinition", {{"@odata.id", metricReportDef}}},
459156d6b00SAppaRao Puli             {"MetricValues", metricValuesArray}};
460156d6b00SAppaRao Puli 
461156d6b00SAppaRao Puli         this->sendEvent(msg.dump());
462156d6b00SAppaRao Puli     }
463156d6b00SAppaRao Puli 
464fe44eb0bSAyushi Smriti     void updateRetryConfig(const uint32_t retryAttempts,
465fe44eb0bSAyushi Smriti                            const uint32_t retryTimeoutInterval)
466fe44eb0bSAyushi Smriti     {
467fe44eb0bSAyushi Smriti         conn->setRetryConfig(retryAttempts, retryTimeoutInterval);
468fe44eb0bSAyushi Smriti     }
469fe44eb0bSAyushi Smriti 
470fe44eb0bSAyushi Smriti     void updateRetryPolicy()
471fe44eb0bSAyushi Smriti     {
472fe44eb0bSAyushi Smriti         conn->setRetryPolicy(retryPolicy);
473fe44eb0bSAyushi Smriti     }
474fe44eb0bSAyushi Smriti 
475b52664e2SAppaRao Puli   private:
4760b4bdd93SAppaRao Puli     uint64_t eventSeqNum;
477b52664e2SAppaRao Puli     std::string host;
478b52664e2SAppaRao Puli     std::string port;
479b52664e2SAppaRao Puli     std::string path;
480b52664e2SAppaRao Puli     std::string uriProto;
481*4bbf237fSAppaRao Puli     std::shared_ptr<crow::HttpClient> conn = nullptr;
482*4bbf237fSAppaRao Puli     std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr;
483b52664e2SAppaRao Puli };
484b52664e2SAppaRao Puli 
4851bf712bcSAyushi Smriti static constexpr const bool defaultEnabledState = true;
4861bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryAttempts = 3;
4871bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryInterval = 30;
4881bf712bcSAyushi Smriti static constexpr const char* defaulEventFormatType = "Event";
4891bf712bcSAyushi Smriti static constexpr const char* defaulSubscriptionType = "RedfishEvent";
4901bf712bcSAyushi Smriti static constexpr const char* defaulRetryPolicy = "TerminateAfterRetries";
4911bf712bcSAyushi Smriti 
492b52664e2SAppaRao Puli class EventServiceManager
493b52664e2SAppaRao Puli {
494b52664e2SAppaRao Puli   private:
4957d1cc387SAppaRao Puli     bool serviceEnabled;
4967d1cc387SAppaRao Puli     uint32_t retryAttempts;
4977d1cc387SAppaRao Puli     uint32_t retryTimeoutInterval;
4987d1cc387SAppaRao Puli 
499b52664e2SAppaRao Puli     EventServiceManager(const EventServiceManager&) = delete;
500b52664e2SAppaRao Puli     EventServiceManager& operator=(const EventServiceManager&) = delete;
501b52664e2SAppaRao Puli     EventServiceManager(EventServiceManager&&) = delete;
502b52664e2SAppaRao Puli     EventServiceManager& operator=(EventServiceManager&&) = delete;
503b52664e2SAppaRao Puli 
5047d1cc387SAppaRao Puli     EventServiceManager() :
5057d1cc387SAppaRao Puli         noOfEventLogSubscribers(0), noOfMetricReportSubscribers(0)
506b52664e2SAppaRao Puli     {
5071bf712bcSAyushi Smriti         // Load config from persist store.
5081bf712bcSAyushi Smriti         initConfig();
509b52664e2SAppaRao Puli     }
510b52664e2SAppaRao Puli 
5117f4eb588SAppaRao Puli     std::string lastEventTStr;
5127d1cc387SAppaRao Puli     size_t noOfEventLogSubscribers;
513156d6b00SAppaRao Puli     size_t noOfMetricReportSubscribers;
514156d6b00SAppaRao Puli     std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor;
515b52664e2SAppaRao Puli     boost::container::flat_map<std::string, std::shared_ptr<Subscription>>
516b52664e2SAppaRao Puli         subscriptionsMap;
517b52664e2SAppaRao Puli 
518b52664e2SAppaRao Puli   public:
519b52664e2SAppaRao Puli     static EventServiceManager& getInstance()
520b52664e2SAppaRao Puli     {
521b52664e2SAppaRao Puli         static EventServiceManager handler;
522b52664e2SAppaRao Puli         return handler;
523b52664e2SAppaRao Puli     }
524b52664e2SAppaRao Puli 
5251bf712bcSAyushi Smriti     void loadDefaultConfig()
5261bf712bcSAyushi Smriti     {
5271bf712bcSAyushi Smriti         serviceEnabled = defaultEnabledState;
5281bf712bcSAyushi Smriti         retryAttempts = defaultRetryAttempts;
5291bf712bcSAyushi Smriti         retryTimeoutInterval = defaultRetryInterval;
5301bf712bcSAyushi Smriti     }
5311bf712bcSAyushi Smriti 
5321bf712bcSAyushi Smriti     void initConfig()
5331bf712bcSAyushi Smriti     {
5341bf712bcSAyushi Smriti         std::ifstream eventConfigFile(eventServiceFile);
5351bf712bcSAyushi Smriti         if (!eventConfigFile.good())
5361bf712bcSAyushi Smriti         {
5371bf712bcSAyushi Smriti             BMCWEB_LOG_DEBUG << "EventService config not exist";
5381bf712bcSAyushi Smriti             loadDefaultConfig();
5391bf712bcSAyushi Smriti             return;
5401bf712bcSAyushi Smriti         }
5411bf712bcSAyushi Smriti         auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false);
5421bf712bcSAyushi Smriti         if (jsonData.is_discarded())
5431bf712bcSAyushi Smriti         {
5441bf712bcSAyushi Smriti             BMCWEB_LOG_ERROR << "EventService config parse error.";
5451bf712bcSAyushi Smriti             loadDefaultConfig();
5461bf712bcSAyushi Smriti             return;
5471bf712bcSAyushi Smriti         }
5481bf712bcSAyushi Smriti 
5491bf712bcSAyushi Smriti         nlohmann::json jsonConfig;
5501bf712bcSAyushi Smriti         if (json_util::getValueFromJsonObject(jsonData, "Configuration",
5511bf712bcSAyushi Smriti                                               jsonConfig))
5521bf712bcSAyushi Smriti         {
5531bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonConfig, "ServiceEnabled",
5541bf712bcSAyushi Smriti                                                    serviceEnabled))
5551bf712bcSAyushi Smriti             {
5561bf712bcSAyushi Smriti                 serviceEnabled = defaultEnabledState;
5571bf712bcSAyushi Smriti             }
5581bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
5591bf712bcSAyushi Smriti                     jsonConfig, "DeliveryRetryAttempts", retryAttempts))
5601bf712bcSAyushi Smriti             {
5611bf712bcSAyushi Smriti                 retryAttempts = defaultRetryAttempts;
5621bf712bcSAyushi Smriti             }
5631bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
5641bf712bcSAyushi Smriti                     jsonConfig, "DeliveryRetryIntervalSeconds",
5651bf712bcSAyushi Smriti                     retryTimeoutInterval))
5661bf712bcSAyushi Smriti             {
5671bf712bcSAyushi Smriti                 retryTimeoutInterval = defaultRetryInterval;
5681bf712bcSAyushi Smriti             }
5691bf712bcSAyushi Smriti         }
5701bf712bcSAyushi Smriti         else
5711bf712bcSAyushi Smriti         {
5721bf712bcSAyushi Smriti             loadDefaultConfig();
5731bf712bcSAyushi Smriti         }
5741bf712bcSAyushi Smriti 
5751bf712bcSAyushi Smriti         nlohmann::json subscriptionsList;
5761bf712bcSAyushi Smriti         if (!json_util::getValueFromJsonObject(jsonData, "Subscriptions",
5771bf712bcSAyushi Smriti                                                subscriptionsList))
5781bf712bcSAyushi Smriti         {
5791bf712bcSAyushi Smriti             BMCWEB_LOG_DEBUG << "EventService: Subscriptions not exist.";
5801bf712bcSAyushi Smriti             return;
5811bf712bcSAyushi Smriti         }
5821bf712bcSAyushi Smriti 
5831bf712bcSAyushi Smriti         for (nlohmann::json& jsonObj : subscriptionsList)
5841bf712bcSAyushi Smriti         {
5851bf712bcSAyushi Smriti             std::string protocol;
5861bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "Protocol",
5871bf712bcSAyushi Smriti                                                    protocol))
5881bf712bcSAyushi Smriti             {
5891bf712bcSAyushi Smriti                 BMCWEB_LOG_DEBUG << "Invalid subscription Protocol exist.";
5901bf712bcSAyushi Smriti                 continue;
5911bf712bcSAyushi Smriti             }
592*4bbf237fSAppaRao Puli 
593*4bbf237fSAppaRao Puli             std::string subscriptionType;
594*4bbf237fSAppaRao Puli             if (!json_util::getValueFromJsonObject(jsonObj, "SubscriptionType",
595*4bbf237fSAppaRao Puli                                                    subscriptionType))
596*4bbf237fSAppaRao Puli             {
597*4bbf237fSAppaRao Puli                 subscriptionType = defaulSubscriptionType;
598*4bbf237fSAppaRao Puli             }
599*4bbf237fSAppaRao Puli             // SSE connections are initiated from client
600*4bbf237fSAppaRao Puli             // and can't be re-established from server.
601*4bbf237fSAppaRao Puli             if (subscriptionType == "SSE")
602*4bbf237fSAppaRao Puli             {
603*4bbf237fSAppaRao Puli                 BMCWEB_LOG_DEBUG
604*4bbf237fSAppaRao Puli                     << "The subscription type is SSE, so skipping.";
605*4bbf237fSAppaRao Puli                 continue;
606*4bbf237fSAppaRao Puli             }
607*4bbf237fSAppaRao Puli 
6081bf712bcSAyushi Smriti             std::string destination;
6091bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "Destination",
6101bf712bcSAyushi Smriti                                                    destination))
6111bf712bcSAyushi Smriti             {
6121bf712bcSAyushi Smriti                 BMCWEB_LOG_DEBUG << "Invalid subscription destination exist.";
6131bf712bcSAyushi Smriti                 continue;
6141bf712bcSAyushi Smriti             }
6151bf712bcSAyushi Smriti             std::string host;
6161bf712bcSAyushi Smriti             std::string urlProto;
6171bf712bcSAyushi Smriti             std::string port;
6181bf712bcSAyushi Smriti             std::string path;
6191bf712bcSAyushi Smriti             bool status =
6201bf712bcSAyushi Smriti                 validateAndSplitUrl(destination, urlProto, host, port, path);
6211bf712bcSAyushi Smriti 
6221bf712bcSAyushi Smriti             if (!status)
6231bf712bcSAyushi Smriti             {
6241bf712bcSAyushi Smriti                 BMCWEB_LOG_ERROR
6251bf712bcSAyushi Smriti                     << "Failed to validate and split destination url";
6261bf712bcSAyushi Smriti                 continue;
6271bf712bcSAyushi Smriti             }
6281bf712bcSAyushi Smriti             std::shared_ptr<Subscription> subValue =
6291bf712bcSAyushi Smriti                 std::make_shared<Subscription>(host, port, path, urlProto);
6301bf712bcSAyushi Smriti 
6311bf712bcSAyushi Smriti             subValue->destinationUrl = destination;
6321bf712bcSAyushi Smriti             subValue->protocol = protocol;
633*4bbf237fSAppaRao Puli             subValue->subscriptionType = subscriptionType;
6341bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
6351bf712bcSAyushi Smriti                     jsonObj, "DeliveryRetryPolicy", subValue->retryPolicy))
6361bf712bcSAyushi Smriti             {
6371bf712bcSAyushi Smriti                 subValue->retryPolicy = defaulRetryPolicy;
6381bf712bcSAyushi Smriti             }
6391bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "EventFormatType",
6401bf712bcSAyushi Smriti                                                    subValue->eventFormatType))
6411bf712bcSAyushi Smriti             {
6421bf712bcSAyushi Smriti                 subValue->eventFormatType = defaulEventFormatType;
6431bf712bcSAyushi Smriti             }
6441bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "Context",
6451bf712bcSAyushi Smriti                                               subValue->customText);
6461bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "MessageIds",
6471bf712bcSAyushi Smriti                                               subValue->registryMsgIds);
6481bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "RegistryPrefixes",
6491bf712bcSAyushi Smriti                                               subValue->registryPrefixes);
6501bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "HttpHeaders",
6511bf712bcSAyushi Smriti                                               subValue->httpHeaders);
6521bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(
6531bf712bcSAyushi Smriti                 jsonObj, "MetricReportDefinitions",
6541bf712bcSAyushi Smriti                 subValue->metricReportDefinitions);
6551bf712bcSAyushi Smriti 
6561bf712bcSAyushi Smriti             std::string id = addSubscription(subValue, false);
6571bf712bcSAyushi Smriti             if (id.empty())
6581bf712bcSAyushi Smriti             {
6591bf712bcSAyushi Smriti                 BMCWEB_LOG_ERROR << "Failed to add subscription";
6601bf712bcSAyushi Smriti             }
6611bf712bcSAyushi Smriti         }
6621bf712bcSAyushi Smriti         return;
6631bf712bcSAyushi Smriti     }
6641bf712bcSAyushi Smriti 
665b52664e2SAppaRao Puli     void updateSubscriptionData()
666b52664e2SAppaRao Puli     {
667b52664e2SAppaRao Puli         // Persist the config and subscription data.
6681bf712bcSAyushi Smriti         nlohmann::json jsonData;
6691bf712bcSAyushi Smriti 
6701bf712bcSAyushi Smriti         nlohmann::json& configObj = jsonData["Configuration"];
6711bf712bcSAyushi Smriti         configObj["ServiceEnabled"] = serviceEnabled;
6721bf712bcSAyushi Smriti         configObj["DeliveryRetryAttempts"] = retryAttempts;
6731bf712bcSAyushi Smriti         configObj["DeliveryRetryIntervalSeconds"] = retryTimeoutInterval;
6741bf712bcSAyushi Smriti 
6751bf712bcSAyushi Smriti         nlohmann::json& subListArray = jsonData["Subscriptions"];
6761bf712bcSAyushi Smriti         subListArray = nlohmann::json::array();
6771bf712bcSAyushi Smriti 
6781bf712bcSAyushi Smriti         for (const auto& it : subscriptionsMap)
6791bf712bcSAyushi Smriti         {
6801bf712bcSAyushi Smriti             std::shared_ptr<Subscription> subValue = it.second;
681*4bbf237fSAppaRao Puli             // Don't preserve SSE connections. Its initiated from
682*4bbf237fSAppaRao Puli             // client side and can't be re-established from server.
683*4bbf237fSAppaRao Puli             if (subValue->subscriptionType == "SSE")
684*4bbf237fSAppaRao Puli             {
685*4bbf237fSAppaRao Puli                 BMCWEB_LOG_DEBUG
686*4bbf237fSAppaRao Puli                     << "The subscription type is SSE, so skipping.";
687*4bbf237fSAppaRao Puli                 continue;
688*4bbf237fSAppaRao Puli             }
6891bf712bcSAyushi Smriti 
690*4bbf237fSAppaRao Puli             nlohmann::json entry;
6911bf712bcSAyushi Smriti             entry["Context"] = subValue->customText;
6921bf712bcSAyushi Smriti             entry["DeliveryRetryPolicy"] = subValue->retryPolicy;
6931bf712bcSAyushi Smriti             entry["Destination"] = subValue->destinationUrl;
6941bf712bcSAyushi Smriti             entry["EventFormatType"] = subValue->eventFormatType;
6951bf712bcSAyushi Smriti             entry["HttpHeaders"] = subValue->httpHeaders;
6961bf712bcSAyushi Smriti             entry["MessageIds"] = subValue->registryMsgIds;
6971bf712bcSAyushi Smriti             entry["Protocol"] = subValue->protocol;
6981bf712bcSAyushi Smriti             entry["RegistryPrefixes"] = subValue->registryPrefixes;
6991bf712bcSAyushi Smriti             entry["SubscriptionType"] = subValue->subscriptionType;
7001bf712bcSAyushi Smriti             entry["MetricReportDefinitions"] =
7011bf712bcSAyushi Smriti                 subValue->metricReportDefinitions;
7021bf712bcSAyushi Smriti 
7031bf712bcSAyushi Smriti             subListArray.push_back(entry);
7041bf712bcSAyushi Smriti         }
7051bf712bcSAyushi Smriti 
7061bf712bcSAyushi Smriti         const std::string tmpFile(std::string(eventServiceFile) + "_tmp");
7071bf712bcSAyushi Smriti         std::ofstream ofs(tmpFile, std::ios::out);
7081bf712bcSAyushi Smriti         const auto& writeData = jsonData.dump();
7091bf712bcSAyushi Smriti         ofs << writeData;
7101bf712bcSAyushi Smriti         ofs.close();
7111bf712bcSAyushi Smriti 
7121bf712bcSAyushi Smriti         BMCWEB_LOG_DEBUG << "EventService config updated to file.";
7131bf712bcSAyushi Smriti         if (std::rename(tmpFile.c_str(), eventServiceFile) != 0)
7141bf712bcSAyushi Smriti         {
7151bf712bcSAyushi Smriti             BMCWEB_LOG_ERROR << "Error in renaming temporary file: "
7161bf712bcSAyushi Smriti                              << tmpFile.c_str();
7171bf712bcSAyushi Smriti         }
718b52664e2SAppaRao Puli     }
719b52664e2SAppaRao Puli 
7207d1cc387SAppaRao Puli     EventServiceConfig getEventServiceConfig()
7217d1cc387SAppaRao Puli     {
7227d1cc387SAppaRao Puli         return {serviceEnabled, retryAttempts, retryTimeoutInterval};
7237d1cc387SAppaRao Puli     }
7247d1cc387SAppaRao Puli 
7257d1cc387SAppaRao Puli     void setEventServiceConfig(const EventServiceConfig& cfg)
7267d1cc387SAppaRao Puli     {
7277d1cc387SAppaRao Puli         bool updateConfig = false;
728fe44eb0bSAyushi Smriti         bool updateRetryCfg = false;
7297d1cc387SAppaRao Puli 
7307d1cc387SAppaRao Puli         if (serviceEnabled != std::get<0>(cfg))
7317d1cc387SAppaRao Puli         {
7327d1cc387SAppaRao Puli             serviceEnabled = std::get<0>(cfg);
7337d1cc387SAppaRao Puli             if (serviceEnabled && noOfMetricReportSubscribers)
7347d1cc387SAppaRao Puli             {
7357d1cc387SAppaRao Puli                 registerMetricReportSignal();
7367d1cc387SAppaRao Puli             }
7377d1cc387SAppaRao Puli             else
7387d1cc387SAppaRao Puli             {
7397d1cc387SAppaRao Puli                 unregisterMetricReportSignal();
7407d1cc387SAppaRao Puli             }
7417d1cc387SAppaRao Puli             updateConfig = true;
7427d1cc387SAppaRao Puli         }
7437d1cc387SAppaRao Puli 
7447d1cc387SAppaRao Puli         if (retryAttempts != std::get<1>(cfg))
7457d1cc387SAppaRao Puli         {
7467d1cc387SAppaRao Puli             retryAttempts = std::get<1>(cfg);
7477d1cc387SAppaRao Puli             updateConfig = true;
748fe44eb0bSAyushi Smriti             updateRetryCfg = true;
7497d1cc387SAppaRao Puli         }
7507d1cc387SAppaRao Puli 
7517d1cc387SAppaRao Puli         if (retryTimeoutInterval != std::get<2>(cfg))
7527d1cc387SAppaRao Puli         {
7537d1cc387SAppaRao Puli             retryTimeoutInterval = std::get<2>(cfg);
7547d1cc387SAppaRao Puli             updateConfig = true;
755fe44eb0bSAyushi Smriti             updateRetryCfg = true;
7567d1cc387SAppaRao Puli         }
7577d1cc387SAppaRao Puli 
7587d1cc387SAppaRao Puli         if (updateConfig)
7597d1cc387SAppaRao Puli         {
7607d1cc387SAppaRao Puli             updateSubscriptionData();
7617d1cc387SAppaRao Puli         }
762fe44eb0bSAyushi Smriti 
763fe44eb0bSAyushi Smriti         if (updateRetryCfg)
764fe44eb0bSAyushi Smriti         {
765fe44eb0bSAyushi Smriti             // Update the changed retry config to all subscriptions
766fe44eb0bSAyushi Smriti             for (const auto& it :
767fe44eb0bSAyushi Smriti                  EventServiceManager::getInstance().subscriptionsMap)
768fe44eb0bSAyushi Smriti             {
769fe44eb0bSAyushi Smriti                 std::shared_ptr<Subscription> entry = it.second;
770fe44eb0bSAyushi Smriti                 entry->updateRetryConfig(retryAttempts, retryTimeoutInterval);
771fe44eb0bSAyushi Smriti             }
772fe44eb0bSAyushi Smriti         }
7737d1cc387SAppaRao Puli     }
7747d1cc387SAppaRao Puli 
7757d1cc387SAppaRao Puli     void updateNoOfSubscribersCount()
7767d1cc387SAppaRao Puli     {
7777d1cc387SAppaRao Puli         size_t eventLogSubCount = 0;
7787d1cc387SAppaRao Puli         size_t metricReportSubCount = 0;
7797d1cc387SAppaRao Puli         for (const auto& it : subscriptionsMap)
7807d1cc387SAppaRao Puli         {
7817d1cc387SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
7827d1cc387SAppaRao Puli             if (entry->eventFormatType == eventFormatType)
7837d1cc387SAppaRao Puli             {
7847d1cc387SAppaRao Puli                 eventLogSubCount++;
7857d1cc387SAppaRao Puli             }
7867d1cc387SAppaRao Puli             else if (entry->eventFormatType == metricReportFormatType)
7877d1cc387SAppaRao Puli             {
7887d1cc387SAppaRao Puli                 metricReportSubCount++;
7897d1cc387SAppaRao Puli             }
7907d1cc387SAppaRao Puli         }
7917d1cc387SAppaRao Puli 
7927d1cc387SAppaRao Puli         noOfEventLogSubscribers = eventLogSubCount;
7937d1cc387SAppaRao Puli         if (noOfMetricReportSubscribers != metricReportSubCount)
7947d1cc387SAppaRao Puli         {
7957d1cc387SAppaRao Puli             noOfMetricReportSubscribers = metricReportSubCount;
7967d1cc387SAppaRao Puli             if (noOfMetricReportSubscribers)
7977d1cc387SAppaRao Puli             {
7987d1cc387SAppaRao Puli                 registerMetricReportSignal();
7997d1cc387SAppaRao Puli             }
8007d1cc387SAppaRao Puli             else
8017d1cc387SAppaRao Puli             {
8027d1cc387SAppaRao Puli                 unregisterMetricReportSignal();
8037d1cc387SAppaRao Puli             }
8047d1cc387SAppaRao Puli         }
8057d1cc387SAppaRao Puli     }
8067d1cc387SAppaRao Puli 
807b52664e2SAppaRao Puli     std::shared_ptr<Subscription> getSubscription(const std::string& id)
808b52664e2SAppaRao Puli     {
809b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
810b52664e2SAppaRao Puli         if (obj == subscriptionsMap.end())
811b52664e2SAppaRao Puli         {
812b52664e2SAppaRao Puli             BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id;
813b52664e2SAppaRao Puli             return nullptr;
814b52664e2SAppaRao Puli         }
815b52664e2SAppaRao Puli         std::shared_ptr<Subscription> subValue = obj->second;
816b52664e2SAppaRao Puli         return subValue;
817b52664e2SAppaRao Puli     }
818b52664e2SAppaRao Puli 
8191bf712bcSAyushi Smriti     std::string addSubscription(const std::shared_ptr<Subscription> subValue,
8201bf712bcSAyushi Smriti                                 const bool updateFile = true)
821b52664e2SAppaRao Puli     {
822b52664e2SAppaRao Puli         std::srand(static_cast<uint32_t>(std::time(0)));
823b52664e2SAppaRao Puli         std::string id;
824b52664e2SAppaRao Puli 
825b52664e2SAppaRao Puli         int retry = 3;
826b52664e2SAppaRao Puli         while (retry)
827b52664e2SAppaRao Puli         {
828b52664e2SAppaRao Puli             id = std::to_string(std::rand());
829b52664e2SAppaRao Puli             auto inserted = subscriptionsMap.insert(std::pair(id, subValue));
830b52664e2SAppaRao Puli             if (inserted.second)
831b52664e2SAppaRao Puli             {
832b52664e2SAppaRao Puli                 break;
833b52664e2SAppaRao Puli             }
834b52664e2SAppaRao Puli             --retry;
835b52664e2SAppaRao Puli         };
836b52664e2SAppaRao Puli 
837b52664e2SAppaRao Puli         if (retry <= 0)
838b52664e2SAppaRao Puli         {
839b52664e2SAppaRao Puli             BMCWEB_LOG_ERROR << "Failed to generate random number";
840b52664e2SAppaRao Puli             return std::string("");
841b52664e2SAppaRao Puli         }
842b52664e2SAppaRao Puli 
8437d1cc387SAppaRao Puli         updateNoOfSubscribersCount();
8441bf712bcSAyushi Smriti 
8451bf712bcSAyushi Smriti         if (updateFile)
8461bf712bcSAyushi Smriti         {
847b52664e2SAppaRao Puli             updateSubscriptionData();
8481bf712bcSAyushi Smriti         }
8497f4eb588SAppaRao Puli 
8507f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
8517f4eb588SAppaRao Puli         if (lastEventTStr.empty())
8527f4eb588SAppaRao Puli         {
8537f4eb588SAppaRao Puli             cacheLastEventTimestamp();
8547f4eb588SAppaRao Puli         }
8557f4eb588SAppaRao Puli #endif
856fe44eb0bSAyushi Smriti         // Update retry configuration.
857fe44eb0bSAyushi Smriti         subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval);
858fe44eb0bSAyushi Smriti         subValue->updateRetryPolicy();
859fe44eb0bSAyushi Smriti 
860b52664e2SAppaRao Puli         return id;
861b52664e2SAppaRao Puli     }
862b52664e2SAppaRao Puli 
863b52664e2SAppaRao Puli     bool isSubscriptionExist(const std::string& id)
864b52664e2SAppaRao Puli     {
865b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
866b52664e2SAppaRao Puli         if (obj == subscriptionsMap.end())
867b52664e2SAppaRao Puli         {
868b52664e2SAppaRao Puli             return false;
869b52664e2SAppaRao Puli         }
870b52664e2SAppaRao Puli         return true;
871b52664e2SAppaRao Puli     }
872b52664e2SAppaRao Puli 
873b52664e2SAppaRao Puli     void deleteSubscription(const std::string& id)
874b52664e2SAppaRao Puli     {
875b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
876b52664e2SAppaRao Puli         if (obj != subscriptionsMap.end())
877b52664e2SAppaRao Puli         {
878b52664e2SAppaRao Puli             subscriptionsMap.erase(obj);
8797d1cc387SAppaRao Puli             updateNoOfSubscribersCount();
880b52664e2SAppaRao Puli             updateSubscriptionData();
881b52664e2SAppaRao Puli         }
882b52664e2SAppaRao Puli     }
883b52664e2SAppaRao Puli 
884b52664e2SAppaRao Puli     size_t getNumberOfSubscriptions()
885b52664e2SAppaRao Puli     {
886b52664e2SAppaRao Puli         return subscriptionsMap.size();
887b52664e2SAppaRao Puli     }
888b52664e2SAppaRao Puli 
889b52664e2SAppaRao Puli     std::vector<std::string> getAllIDs()
890b52664e2SAppaRao Puli     {
891b52664e2SAppaRao Puli         std::vector<std::string> idList;
892b52664e2SAppaRao Puli         for (const auto& it : subscriptionsMap)
893b52664e2SAppaRao Puli         {
894b52664e2SAppaRao Puli             idList.emplace_back(it.first);
895b52664e2SAppaRao Puli         }
896b52664e2SAppaRao Puli         return idList;
897b52664e2SAppaRao Puli     }
898b52664e2SAppaRao Puli 
899b52664e2SAppaRao Puli     bool isDestinationExist(const std::string& destUrl)
900b52664e2SAppaRao Puli     {
901b52664e2SAppaRao Puli         for (const auto& it : subscriptionsMap)
902b52664e2SAppaRao Puli         {
903b52664e2SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
904b52664e2SAppaRao Puli             if (entry->destinationUrl == destUrl)
905b52664e2SAppaRao Puli             {
906b52664e2SAppaRao Puli                 BMCWEB_LOG_ERROR << "Destination exist already" << destUrl;
907b52664e2SAppaRao Puli                 return true;
908b52664e2SAppaRao Puli             }
909b52664e2SAppaRao Puli         }
910b52664e2SAppaRao Puli         return false;
911b52664e2SAppaRao Puli     }
9120b4bdd93SAppaRao Puli 
9130b4bdd93SAppaRao Puli     void sendTestEventLog()
9140b4bdd93SAppaRao Puli     {
9150b4bdd93SAppaRao Puli         for (const auto& it : this->subscriptionsMap)
9160b4bdd93SAppaRao Puli         {
9170b4bdd93SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
9180b4bdd93SAppaRao Puli             entry->sendTestEventLog();
9190b4bdd93SAppaRao Puli         }
9200b4bdd93SAppaRao Puli     }
921e9a14131SAppaRao Puli 
9227f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
9237f4eb588SAppaRao Puli     void cacheLastEventTimestamp()
9247f4eb588SAppaRao Puli     {
9257f4eb588SAppaRao Puli         std::ifstream logStream(redfishEventLogFile);
9267f4eb588SAppaRao Puli         if (!logStream.good())
9277f4eb588SAppaRao Puli         {
9287f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << " Redfish log file open failed \n";
9297f4eb588SAppaRao Puli             return;
9307f4eb588SAppaRao Puli         }
9317f4eb588SAppaRao Puli         std::string logEntry;
9327f4eb588SAppaRao Puli         while (std::getline(logStream, logEntry))
9337f4eb588SAppaRao Puli         {
9347f4eb588SAppaRao Puli             size_t space = logEntry.find_first_of(" ");
9357f4eb588SAppaRao Puli             if (space == std::string::npos)
9367f4eb588SAppaRao Puli             {
9377f4eb588SAppaRao Puli                 // Shouldn't enter here but lets skip it.
9387f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Invalid log entry found.";
9397f4eb588SAppaRao Puli                 continue;
9407f4eb588SAppaRao Puli             }
9417f4eb588SAppaRao Puli             lastEventTStr = logEntry.substr(0, space);
9427f4eb588SAppaRao Puli         }
9437f4eb588SAppaRao Puli         BMCWEB_LOG_DEBUG << "Last Event time stamp set: " << lastEventTStr;
9447f4eb588SAppaRao Puli     }
9457f4eb588SAppaRao Puli 
9467f4eb588SAppaRao Puli     void readEventLogsFromFile()
9477f4eb588SAppaRao Puli     {
9487f4eb588SAppaRao Puli         if (!serviceEnabled || !noOfEventLogSubscribers)
9497f4eb588SAppaRao Puli         {
9507f4eb588SAppaRao Puli             BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions.";
9517f4eb588SAppaRao Puli             return;
9527f4eb588SAppaRao Puli         }
9537f4eb588SAppaRao Puli         std::ifstream logStream(redfishEventLogFile);
9547f4eb588SAppaRao Puli         if (!logStream.good())
9557f4eb588SAppaRao Puli         {
9567f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << " Redfish log file open failed";
9577f4eb588SAppaRao Puli             return;
9587f4eb588SAppaRao Puli         }
9597f4eb588SAppaRao Puli 
9607f4eb588SAppaRao Puli         std::vector<EventLogObjectsType> eventRecords;
9617f4eb588SAppaRao Puli 
9627f4eb588SAppaRao Puli         bool startLogCollection = false;
9637f4eb588SAppaRao Puli         bool firstEntry = true;
9647f4eb588SAppaRao Puli 
9657f4eb588SAppaRao Puli         std::string logEntry;
9667f4eb588SAppaRao Puli         while (std::getline(logStream, logEntry))
9677f4eb588SAppaRao Puli         {
9687f4eb588SAppaRao Puli             if (!startLogCollection)
9697f4eb588SAppaRao Puli             {
9707f4eb588SAppaRao Puli                 if (boost::starts_with(logEntry, lastEventTStr))
9717f4eb588SAppaRao Puli                 {
9727f4eb588SAppaRao Puli                     startLogCollection = true;
9737f4eb588SAppaRao Puli                 }
9747f4eb588SAppaRao Puli                 continue;
9757f4eb588SAppaRao Puli             }
9767f4eb588SAppaRao Puli 
9777f4eb588SAppaRao Puli             std::string idStr;
9787f4eb588SAppaRao Puli             if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry))
9797f4eb588SAppaRao Puli             {
9807f4eb588SAppaRao Puli                 continue;
9817f4eb588SAppaRao Puli             }
9827f4eb588SAppaRao Puli             firstEntry = false;
9837f4eb588SAppaRao Puli 
9847f4eb588SAppaRao Puli             std::string timestamp;
9857f4eb588SAppaRao Puli             std::string messageID;
9865e715de6SAppaRao Puli             std::vector<std::string> messageArgs;
9877f4eb588SAppaRao Puli             if (event_log::getEventLogParams(logEntry, timestamp, messageID,
9887f4eb588SAppaRao Puli                                              messageArgs) != 0)
9897f4eb588SAppaRao Puli             {
9907f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Read eventLog entry params failed";
9917f4eb588SAppaRao Puli                 continue;
9927f4eb588SAppaRao Puli             }
9937f4eb588SAppaRao Puli 
9947f4eb588SAppaRao Puli             std::string registryName;
9957f4eb588SAppaRao Puli             std::string messageKey;
9967f4eb588SAppaRao Puli             event_log::getRegistryAndMessageKey(messageID, registryName,
9977f4eb588SAppaRao Puli                                                 messageKey);
9987f4eb588SAppaRao Puli             if (registryName.empty() || messageKey.empty())
9997f4eb588SAppaRao Puli             {
10007f4eb588SAppaRao Puli                 continue;
10017f4eb588SAppaRao Puli             }
10027f4eb588SAppaRao Puli 
10037f4eb588SAppaRao Puli             lastEventTStr = timestamp;
10047f4eb588SAppaRao Puli             eventRecords.emplace_back(idStr, timestamp, messageID, registryName,
10057f4eb588SAppaRao Puli                                       messageKey, messageArgs);
10067f4eb588SAppaRao Puli         }
10077f4eb588SAppaRao Puli 
10087f4eb588SAppaRao Puli         for (const auto& it : this->subscriptionsMap)
10097f4eb588SAppaRao Puli         {
10107f4eb588SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
10117f4eb588SAppaRao Puli             if (entry->eventFormatType == "Event")
10127f4eb588SAppaRao Puli             {
10137f4eb588SAppaRao Puli                 entry->filterAndSendEventLogs(eventRecords);
10147f4eb588SAppaRao Puli             }
10157f4eb588SAppaRao Puli         }
10167f4eb588SAppaRao Puli     }
10177f4eb588SAppaRao Puli 
10187f4eb588SAppaRao Puli     static void watchRedfishEventLogFile()
10197f4eb588SAppaRao Puli     {
10207f4eb588SAppaRao Puli         if (inotifyConn == nullptr)
10217f4eb588SAppaRao Puli         {
10227f4eb588SAppaRao Puli             return;
10237f4eb588SAppaRao Puli         }
10247f4eb588SAppaRao Puli 
10257f4eb588SAppaRao Puli         static std::array<char, 1024> readBuffer;
10267f4eb588SAppaRao Puli 
10277f4eb588SAppaRao Puli         inotifyConn->async_read_some(
10287f4eb588SAppaRao Puli             boost::asio::buffer(readBuffer),
10297f4eb588SAppaRao Puli             [&](const boost::system::error_code& ec,
10307f4eb588SAppaRao Puli                 const std::size_t& bytesTransferred) {
10317f4eb588SAppaRao Puli                 if (ec)
10327f4eb588SAppaRao Puli                 {
10337f4eb588SAppaRao Puli                     BMCWEB_LOG_ERROR << "Callback Error: " << ec.message();
10347f4eb588SAppaRao Puli                     return;
10357f4eb588SAppaRao Puli                 }
10367f4eb588SAppaRao Puli                 std::size_t index = 0;
1037b792cc56SAppaRao Puli                 while ((index + iEventSize) <= bytesTransferred)
10387f4eb588SAppaRao Puli                 {
10397f4eb588SAppaRao Puli                     struct inotify_event event;
1040b792cc56SAppaRao Puli                     std::memcpy(&event, &readBuffer[index], iEventSize);
1041b792cc56SAppaRao Puli                     if (event.wd == dirWatchDesc)
1042b792cc56SAppaRao Puli                     {
1043b792cc56SAppaRao Puli                         if ((event.len == 0) ||
1044b792cc56SAppaRao Puli                             (index + iEventSize + event.len > bytesTransferred))
1045b792cc56SAppaRao Puli                         {
1046b792cc56SAppaRao Puli                             index += (iEventSize + event.len);
1047b792cc56SAppaRao Puli                             continue;
1048b792cc56SAppaRao Puli                         }
1049b792cc56SAppaRao Puli 
1050b792cc56SAppaRao Puli                         std::string fileName(&readBuffer[index + iEventSize],
1051b792cc56SAppaRao Puli                                              event.len);
1052b792cc56SAppaRao Puli                         if (std::strcmp(fileName.c_str(), "redfish") != 0)
1053b792cc56SAppaRao Puli                         {
1054b792cc56SAppaRao Puli                             index += (iEventSize + event.len);
1055b792cc56SAppaRao Puli                             continue;
1056b792cc56SAppaRao Puli                         }
1057b792cc56SAppaRao Puli 
1058b792cc56SAppaRao Puli                         BMCWEB_LOG_DEBUG
1059b792cc56SAppaRao Puli                             << "Redfish log file created/deleted. event.name: "
1060b792cc56SAppaRao Puli                             << fileName;
1061b792cc56SAppaRao Puli                         if (event.mask == IN_CREATE)
1062b792cc56SAppaRao Puli                         {
1063b792cc56SAppaRao Puli                             if (fileWatchDesc != -1)
1064b792cc56SAppaRao Puli                             {
1065b792cc56SAppaRao Puli                                 BMCWEB_LOG_DEBUG
1066b792cc56SAppaRao Puli                                     << "Redfish log file is already on "
1067b792cc56SAppaRao Puli                                        "inotify_add_watch.";
1068b792cc56SAppaRao Puli                                 return;
1069b792cc56SAppaRao Puli                             }
1070b792cc56SAppaRao Puli 
1071b792cc56SAppaRao Puli                             fileWatchDesc = inotify_add_watch(
1072b792cc56SAppaRao Puli                                 inotifyFd, redfishEventLogFile, IN_MODIFY);
1073b792cc56SAppaRao Puli                             if (fileWatchDesc == -1)
1074b792cc56SAppaRao Puli                             {
1075b792cc56SAppaRao Puli                                 BMCWEB_LOG_ERROR
1076b792cc56SAppaRao Puli                                     << "inotify_add_watch failed for "
1077b792cc56SAppaRao Puli                                        "redfish log file.";
1078b792cc56SAppaRao Puli                                 return;
1079b792cc56SAppaRao Puli                             }
1080b792cc56SAppaRao Puli 
1081b792cc56SAppaRao Puli                             EventServiceManager::getInstance()
1082b792cc56SAppaRao Puli                                 .cacheLastEventTimestamp();
1083b792cc56SAppaRao Puli                             EventServiceManager::getInstance()
1084b792cc56SAppaRao Puli                                 .readEventLogsFromFile();
1085b792cc56SAppaRao Puli                         }
1086b792cc56SAppaRao Puli                         else if ((event.mask == IN_DELETE) ||
1087b792cc56SAppaRao Puli                                  (event.mask == IN_MOVED_TO))
1088b792cc56SAppaRao Puli                         {
1089b792cc56SAppaRao Puli                             if (fileWatchDesc != -1)
1090b792cc56SAppaRao Puli                             {
1091b792cc56SAppaRao Puli                                 inotify_rm_watch(inotifyFd, fileWatchDesc);
1092b792cc56SAppaRao Puli                                 fileWatchDesc = -1;
1093b792cc56SAppaRao Puli                             }
1094b792cc56SAppaRao Puli                         }
1095b792cc56SAppaRao Puli                     }
1096b792cc56SAppaRao Puli                     else if (event.wd == fileWatchDesc)
1097b792cc56SAppaRao Puli                     {
1098b792cc56SAppaRao Puli                         if (event.mask == IN_MODIFY)
10997f4eb588SAppaRao Puli                         {
11007f4eb588SAppaRao Puli                             EventServiceManager::getInstance()
11017f4eb588SAppaRao Puli                                 .readEventLogsFromFile();
11027f4eb588SAppaRao Puli                         }
1103b792cc56SAppaRao Puli                     }
1104b792cc56SAppaRao Puli                     index += (iEventSize + event.len);
11057f4eb588SAppaRao Puli                 }
11067f4eb588SAppaRao Puli 
11077f4eb588SAppaRao Puli                 watchRedfishEventLogFile();
11087f4eb588SAppaRao Puli             });
11097f4eb588SAppaRao Puli     }
11107f4eb588SAppaRao Puli 
11117f4eb588SAppaRao Puli     static int startEventLogMonitor(boost::asio::io_context& ioc)
11127f4eb588SAppaRao Puli     {
11137f4eb588SAppaRao Puli         inotifyConn =
11147f4eb588SAppaRao Puli             std::make_shared<boost::asio::posix::stream_descriptor>(ioc);
1115b792cc56SAppaRao Puli         inotifyFd = inotify_init1(IN_NONBLOCK);
1116b792cc56SAppaRao Puli         if (inotifyFd == -1)
11177f4eb588SAppaRao Puli         {
11187f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << "inotify_init1 failed.";
11197f4eb588SAppaRao Puli             return -1;
11207f4eb588SAppaRao Puli         }
1121b792cc56SAppaRao Puli 
1122b792cc56SAppaRao Puli         // Add watch on directory to handle redfish event log file
1123b792cc56SAppaRao Puli         // create/delete.
1124b792cc56SAppaRao Puli         dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir,
1125b792cc56SAppaRao Puli                                          IN_CREATE | IN_MOVED_TO | IN_DELETE);
1126b792cc56SAppaRao Puli         if (dirWatchDesc == -1)
11277f4eb588SAppaRao Puli         {
11287f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR
1129b792cc56SAppaRao Puli                 << "inotify_add_watch failed for event log directory.";
11307f4eb588SAppaRao Puli             return -1;
11317f4eb588SAppaRao Puli         }
11327f4eb588SAppaRao Puli 
1133b792cc56SAppaRao Puli         // Watch redfish event log file for modifications.
1134b792cc56SAppaRao Puli         fileWatchDesc =
1135b792cc56SAppaRao Puli             inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY);
1136b792cc56SAppaRao Puli         if (fileWatchDesc == -1)
1137b792cc56SAppaRao Puli         {
1138b792cc56SAppaRao Puli             BMCWEB_LOG_ERROR
1139b792cc56SAppaRao Puli                 << "inotify_add_watch failed for redfish log file.";
1140b792cc56SAppaRao Puli             // Don't return error if file not exist.
1141b792cc56SAppaRao Puli             // Watch on directory will handle create/delete of file.
1142b792cc56SAppaRao Puli         }
1143b792cc56SAppaRao Puli 
11447f4eb588SAppaRao Puli         // monitor redfish event log file
1145b792cc56SAppaRao Puli         inotifyConn->assign(inotifyFd);
11467f4eb588SAppaRao Puli         watchRedfishEventLogFile();
11477f4eb588SAppaRao Puli 
11487f4eb588SAppaRao Puli         return 0;
11497f4eb588SAppaRao Puli     }
11507f4eb588SAppaRao Puli 
11517f4eb588SAppaRao Puli #endif
11527f4eb588SAppaRao Puli 
1153156d6b00SAppaRao Puli     void getMetricReading(const std::string& service,
1154156d6b00SAppaRao Puli                           const std::string& objPath, const std::string& intf)
1155156d6b00SAppaRao Puli     {
1156156d6b00SAppaRao Puli         std::size_t found = objPath.find_last_of("/");
1157156d6b00SAppaRao Puli         if (found == std::string::npos)
1158156d6b00SAppaRao Puli         {
1159156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Invalid objPath received";
1160156d6b00SAppaRao Puli             return;
1161156d6b00SAppaRao Puli         }
1162156d6b00SAppaRao Puli 
1163156d6b00SAppaRao Puli         std::string idStr = objPath.substr(found + 1);
1164156d6b00SAppaRao Puli         if (idStr.empty())
1165156d6b00SAppaRao Puli         {
1166156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Invalid ID in objPath";
1167156d6b00SAppaRao Puli             return;
1168156d6b00SAppaRao Puli         }
1169156d6b00SAppaRao Puli 
1170156d6b00SAppaRao Puli         crow::connections::systemBus->async_method_call(
1171156d6b00SAppaRao Puli             [idStr{std::move(idStr)}](
1172156d6b00SAppaRao Puli                 const boost::system::error_code ec,
1173156d6b00SAppaRao Puli                 boost::container::flat_map<
1174156d6b00SAppaRao Puli                     std::string, std::variant<std::string, ReadingsObjType>>&
1175156d6b00SAppaRao Puli                     resp) {
1176156d6b00SAppaRao Puli                 if (ec)
1177156d6b00SAppaRao Puli                 {
1178156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG
1179156d6b00SAppaRao Puli                         << "D-Bus call failed to GetAll metric readings.";
1180156d6b00SAppaRao Puli                     return;
1181156d6b00SAppaRao Puli                 }
1182156d6b00SAppaRao Puli 
1183156d6b00SAppaRao Puli                 const std::string* timestampPtr =
1184156d6b00SAppaRao Puli                     std::get_if<std::string>(&resp["Timestamp"]);
1185156d6b00SAppaRao Puli                 if (!timestampPtr)
1186156d6b00SAppaRao Puli                 {
1187156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "Failed to Get timestamp.";
1188156d6b00SAppaRao Puli                     return;
1189156d6b00SAppaRao Puli                 }
1190156d6b00SAppaRao Puli 
1191156d6b00SAppaRao Puli                 ReadingsObjType* readingsPtr =
1192156d6b00SAppaRao Puli                     std::get_if<ReadingsObjType>(&resp["Readings"]);
1193156d6b00SAppaRao Puli                 if (!readingsPtr)
1194156d6b00SAppaRao Puli                 {
1195156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "Failed to Get Readings property.";
1196156d6b00SAppaRao Puli                     return;
1197156d6b00SAppaRao Puli                 }
1198156d6b00SAppaRao Puli 
1199156d6b00SAppaRao Puli                 if (!readingsPtr->size())
1200156d6b00SAppaRao Puli                 {
1201156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "No metrics report to be transferred";
1202156d6b00SAppaRao Puli                     return;
1203156d6b00SAppaRao Puli                 }
1204156d6b00SAppaRao Puli 
1205156d6b00SAppaRao Puli                 for (const auto& it :
1206156d6b00SAppaRao Puli                      EventServiceManager::getInstance().subscriptionsMap)
1207156d6b00SAppaRao Puli                 {
1208156d6b00SAppaRao Puli                     std::shared_ptr<Subscription> entry = it.second;
1209156d6b00SAppaRao Puli                     if (entry->eventFormatType == metricReportFormatType)
1210156d6b00SAppaRao Puli                     {
1211156d6b00SAppaRao Puli                         entry->filterAndSendReports(idStr, *timestampPtr,
1212156d6b00SAppaRao Puli                                                     *readingsPtr);
1213156d6b00SAppaRao Puli                     }
1214156d6b00SAppaRao Puli                 }
1215156d6b00SAppaRao Puli             },
1216156d6b00SAppaRao Puli             service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
1217156d6b00SAppaRao Puli             intf);
1218156d6b00SAppaRao Puli     }
1219156d6b00SAppaRao Puli 
1220156d6b00SAppaRao Puli     void unregisterMetricReportSignal()
1221156d6b00SAppaRao Puli     {
12227d1cc387SAppaRao Puli         if (matchTelemetryMonitor)
12237d1cc387SAppaRao Puli         {
1224156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister";
1225156d6b00SAppaRao Puli             matchTelemetryMonitor.reset();
1226156d6b00SAppaRao Puli             matchTelemetryMonitor = nullptr;
1227156d6b00SAppaRao Puli         }
12287d1cc387SAppaRao Puli     }
1229156d6b00SAppaRao Puli 
1230156d6b00SAppaRao Puli     void registerMetricReportSignal()
1231156d6b00SAppaRao Puli     {
12327d1cc387SAppaRao Puli         if (!serviceEnabled || matchTelemetryMonitor)
1233156d6b00SAppaRao Puli         {
12347d1cc387SAppaRao Puli             BMCWEB_LOG_DEBUG << "Not registering metric report signal.";
1235156d6b00SAppaRao Puli             return;
1236156d6b00SAppaRao Puli         }
1237156d6b00SAppaRao Puli 
1238156d6b00SAppaRao Puli         BMCWEB_LOG_DEBUG << "Metrics report signal - Register";
1239156d6b00SAppaRao Puli         std::string matchStr(
1240156d6b00SAppaRao Puli             "type='signal',member='ReportUpdate', "
1241156d6b00SAppaRao Puli             "interface='xyz.openbmc_project.MonitoringService.Report'");
1242156d6b00SAppaRao Puli 
1243156d6b00SAppaRao Puli         matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>(
1244156d6b00SAppaRao Puli             *crow::connections::systemBus, matchStr,
1245156d6b00SAppaRao Puli             [this](sdbusplus::message::message& msg) {
1246156d6b00SAppaRao Puli                 if (msg.is_method_error())
1247156d6b00SAppaRao Puli                 {
1248156d6b00SAppaRao Puli                     BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error";
1249156d6b00SAppaRao Puli                     return;
1250156d6b00SAppaRao Puli                 }
1251156d6b00SAppaRao Puli 
1252156d6b00SAppaRao Puli                 std::string service = msg.get_sender();
1253156d6b00SAppaRao Puli                 std::string objPath = msg.get_path();
1254156d6b00SAppaRao Puli                 std::string intf = msg.get_interface();
1255156d6b00SAppaRao Puli                 getMetricReading(service, objPath, intf);
1256156d6b00SAppaRao Puli             });
1257156d6b00SAppaRao Puli     }
12581bf712bcSAyushi Smriti 
12591bf712bcSAyushi Smriti     bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto,
12601bf712bcSAyushi Smriti                              std::string& host, std::string& port,
12611bf712bcSAyushi Smriti                              std::string& path)
12621bf712bcSAyushi Smriti     {
12631bf712bcSAyushi Smriti         // Validate URL using regex expression
12641bf712bcSAyushi Smriti         // Format: <protocol>://<host>:<port>/<path>
12651bf712bcSAyushi Smriti         // protocol: http/https
12661bf712bcSAyushi Smriti         const std::regex urlRegex(
12671bf712bcSAyushi Smriti             "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/"
12681bf712bcSAyushi Smriti             "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)");
12691bf712bcSAyushi Smriti         std::cmatch match;
12701bf712bcSAyushi Smriti         if (!std::regex_match(destUrl.c_str(), match, urlRegex))
12711bf712bcSAyushi Smriti         {
12721bf712bcSAyushi Smriti             BMCWEB_LOG_INFO << "Dest. url did not match ";
12731bf712bcSAyushi Smriti             return false;
12741bf712bcSAyushi Smriti         }
12751bf712bcSAyushi Smriti 
12761bf712bcSAyushi Smriti         urlProto = std::string(match[1].first, match[1].second);
12771bf712bcSAyushi Smriti         if (urlProto == "http")
12781bf712bcSAyushi Smriti         {
12791bf712bcSAyushi Smriti #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
12801bf712bcSAyushi Smriti             return false;
12811bf712bcSAyushi Smriti #endif
12821bf712bcSAyushi Smriti         }
12831bf712bcSAyushi Smriti 
12841bf712bcSAyushi Smriti         host = std::string(match[2].first, match[2].second);
12851bf712bcSAyushi Smriti         port = std::string(match[3].first, match[3].second);
12861bf712bcSAyushi Smriti         path = std::string(match[4].first, match[4].second);
12871bf712bcSAyushi Smriti         if (port.empty())
12881bf712bcSAyushi Smriti         {
12891bf712bcSAyushi Smriti             if (urlProto == "http")
12901bf712bcSAyushi Smriti             {
12911bf712bcSAyushi Smriti                 port = "80";
12921bf712bcSAyushi Smriti             }
12931bf712bcSAyushi Smriti             else
12941bf712bcSAyushi Smriti             {
12951bf712bcSAyushi Smriti                 port = "443";
12961bf712bcSAyushi Smriti             }
12971bf712bcSAyushi Smriti         }
12981bf712bcSAyushi Smriti         if (path.empty())
12991bf712bcSAyushi Smriti         {
13001bf712bcSAyushi Smriti             path = "/";
13011bf712bcSAyushi Smriti         }
13021bf712bcSAyushi Smriti         return true;
13031bf712bcSAyushi Smriti     }
13047f4eb588SAppaRao Puli }; // namespace redfish
1305b52664e2SAppaRao Puli 
1306b52664e2SAppaRao Puli } // namespace redfish
1307