xref: /openbmc/bmcweb/features/redfish/include/event_service_manager.hpp (revision 22daffd71eb246fb42f7f356025e92659b713151)
1 /*
2 // Copyright (c) 2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 #include "metric_report.hpp"
18 #include "registries.hpp"
19 #include "registries/base_message_registry.hpp"
20 #include "registries/openbmc_message_registry.hpp"
21 #include "registries/task_event_message_registry.hpp"
22 
23 #include <sys/inotify.h>
24 
25 #include <boost/asio/io_context.hpp>
26 #include <boost/container/flat_map.hpp>
27 #include <dbus_utility.hpp>
28 #include <error_messages.hpp>
29 #include <event_service_store.hpp>
30 #include <http/utility.hpp>
31 #include <http_client.hpp>
32 #include <persistent_data.hpp>
33 #include <random.hpp>
34 #include <sdbusplus/bus/match.hpp>
35 #include <server_sent_events.hpp>
36 #include <utils/json_utils.hpp>
37 
38 #include <cstdlib>
39 #include <ctime>
40 #include <fstream>
41 #include <memory>
42 #include <span>
43 
44 namespace redfish
45 {
46 
47 using ReadingsObjType =
48     std::vector<std::tuple<std::string, std::string, double, int32_t>>;
49 
50 static constexpr const char* eventFormatType = "Event";
51 static constexpr const char* metricReportFormatType = "MetricReport";
52 
53 static constexpr const char* eventServiceFile =
54     "/var/lib/bmcweb/eventservice_config.json";
55 
56 namespace registries
57 {
58 inline std::span<const MessageEntry>
59     getRegistryFromPrefix(const std::string& registryName)
60 {
61     if (task_event::header.registryPrefix == registryName)
62     {
63         return {task_event::registry};
64     }
65     if (openbmc::header.registryPrefix == registryName)
66     {
67         return {openbmc::registry};
68     }
69     if (base::header.registryPrefix == registryName)
70     {
71         return {base::registry};
72     }
73     return {openbmc::registry};
74 }
75 } // namespace registries
76 
77 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
78 static std::optional<boost::asio::posix::stream_descriptor> inotifyConn;
79 static constexpr const char* redfishEventLogDir = "/var/log";
80 static constexpr const char* redfishEventLogFile = "/var/log/redfish";
81 static constexpr const size_t iEventSize = sizeof(inotify_event);
82 static int inotifyFd = -1;
83 static int dirWatchDesc = -1;
84 static int fileWatchDesc = -1;
85 
86 // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs>
87 using EventLogObjectsType =
88     std::tuple<std::string, std::string, std::string, std::string, std::string,
89                std::vector<std::string>>;
90 
91 namespace registries
92 {
93 static const Message*
94     getMsgFromRegistry(const std::string& messageKey,
95                        const std::span<const MessageEntry>& registry)
96 {
97     std::span<const MessageEntry>::iterator messageIt =
98         std::find_if(registry.begin(), registry.end(),
99                      [&messageKey](const MessageEntry& messageEntry) {
100         return messageKey == messageEntry.first;
101         });
102     if (messageIt != registry.end())
103     {
104         return &messageIt->second;
105     }
106 
107     return nullptr;
108 }
109 
110 static const Message* formatMessage(const std::string_view& messageID)
111 {
112     // Redfish MessageIds are in the form
113     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
114     // the right Message
115     std::vector<std::string> fields;
116     fields.reserve(4);
117     boost::split(fields, messageID, boost::is_any_of("."));
118     if (fields.size() != 4)
119     {
120         return nullptr;
121     }
122     std::string& registryName = fields[0];
123     std::string& messageKey = fields[3];
124 
125     // Find the right registry and check it for the MessageKey
126     return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName));
127 }
128 } // namespace registries
129 
130 namespace event_log
131 {
132 inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID)
133 {
134     static time_t prevTs = 0;
135     static int index = 0;
136 
137     // Get the entry timestamp
138     std::time_t curTs = 0;
139     std::tm timeStruct = {};
140     std::istringstream entryStream(logEntry);
141     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
142     {
143         curTs = std::mktime(&timeStruct);
144         if (curTs == -1)
145         {
146             return false;
147         }
148     }
149     // If the timestamp isn't unique, increment the index
150     index = (curTs == prevTs) ? index + 1 : 0;
151 
152     // Save the timestamp
153     prevTs = curTs;
154 
155     entryID = std::to_string(curTs);
156     if (index > 0)
157     {
158         entryID += "_" + std::to_string(index);
159     }
160     return true;
161 }
162 
163 inline int getEventLogParams(const std::string& logEntry,
164                              std::string& timestamp, std::string& messageID,
165                              std::vector<std::string>& messageArgs)
166 {
167     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
168     // First get the Timestamp
169     size_t space = logEntry.find_first_of(' ');
170     if (space == std::string::npos)
171     {
172         return -EINVAL;
173     }
174     timestamp = logEntry.substr(0, space);
175     // Then get the log contents
176     size_t entryStart = logEntry.find_first_not_of(' ', space);
177     if (entryStart == std::string::npos)
178     {
179         return -EINVAL;
180     }
181     std::string_view entry(logEntry);
182     entry.remove_prefix(entryStart);
183     // Use split to separate the entry into its fields
184     std::vector<std::string> logEntryFields;
185     boost::split(logEntryFields, entry, boost::is_any_of(","),
186                  boost::token_compress_on);
187     // We need at least a MessageId to be valid
188     if (logEntryFields.empty())
189     {
190         return -EINVAL;
191     }
192     messageID = logEntryFields[0];
193 
194     // Get the MessageArgs from the log if there are any
195     if (logEntryFields.size() > 1)
196     {
197         std::string& messageArgsStart = logEntryFields[1];
198         // If the first string is empty, assume there are no MessageArgs
199         if (!messageArgsStart.empty())
200         {
201             messageArgs.assign(logEntryFields.begin() + 1,
202                                logEntryFields.end());
203         }
204     }
205 
206     return 0;
207 }
208 
209 inline void getRegistryAndMessageKey(const std::string& messageID,
210                                      std::string& registryName,
211                                      std::string& messageKey)
212 {
213     // Redfish MessageIds are in the form
214     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
215     // the right Message
216     std::vector<std::string> fields;
217     fields.reserve(4);
218     boost::split(fields, messageID, boost::is_any_of("."));
219     if (fields.size() == 4)
220     {
221         registryName = fields[0];
222         messageKey = fields[3];
223     }
224 }
225 
226 inline int formatEventLogEntry(const std::string& logEntryID,
227                                const std::string& messageID,
228                                const std::span<std::string_view> messageArgs,
229                                std::string timestamp,
230                                const std::string& customText,
231                                nlohmann::json& logEntryJson)
232 {
233     // Get the Message from the MessageRegistry
234     const registries::Message* message = registries::formatMessage(messageID);
235 
236     std::string msg;
237     std::string severity;
238     if (message != nullptr)
239     {
240         msg = message->message;
241         severity = message->messageSeverity;
242     }
243 
244     redfish::registries::fillMessageArgs(messageArgs, msg);
245 
246     // Get the Created time from the timestamp. The log timestamp is in
247     // RFC3339 format which matches the Redfish format except for the
248     // fractional seconds between the '.' and the '+', so just remove them.
249     std::size_t dot = timestamp.find_first_of('.');
250     std::size_t plus = timestamp.find_first_of('+');
251     if (dot != std::string::npos && plus != std::string::npos)
252     {
253         timestamp.erase(dot, plus - dot);
254     }
255 
256     // Fill in the log entry with the gathered data
257     logEntryJson["EventId"] = logEntryID;
258     logEntryJson["EventType"] = "Event";
259     logEntryJson["Severity"] = std::move(severity);
260     logEntryJson["Message"] = std::move(msg);
261     logEntryJson["MessageId"] = messageID;
262     logEntryJson["MessageArgs"] = messageArgs;
263     logEntryJson["EventTimestamp"] = std::move(timestamp);
264     logEntryJson["Context"] = customText;
265     return 0;
266 }
267 
268 } // namespace event_log
269 #endif
270 
271 inline bool isFilterQuerySpecialChar(char c)
272 {
273     switch (c)
274     {
275         case '(':
276         case ')':
277         case '\'':
278             return true;
279         default:
280             return false;
281     }
282 }
283 
284 inline bool
285     readSSEQueryParams(std::string sseFilter, std::string& formatType,
286                        std::vector<std::string>& messageIds,
287                        std::vector<std::string>& registryPrefixes,
288                        std::vector<std::string>& metricReportDefinitions)
289 {
290     sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(),
291                                    isFilterQuerySpecialChar),
292                     sseFilter.end());
293 
294     std::vector<std::string> result;
295     boost::split(result, sseFilter, boost::is_any_of(" "),
296                  boost::token_compress_on);
297 
298     BMCWEB_LOG_DEBUG << "No of tokens in SEE query: " << result.size();
299 
300     constexpr uint8_t divisor = 4;
301     constexpr uint8_t minTokenSize = 3;
302     if (result.size() % divisor != minTokenSize)
303     {
304         BMCWEB_LOG_ERROR << "Invalid SSE filter specified.";
305         return false;
306     }
307 
308     for (std::size_t i = 0; i < result.size(); i += divisor)
309     {
310         std::string& key = result[i];
311         std::string& op = result[i + 1];
312         std::string& value = result[i + 2];
313 
314         if ((i + minTokenSize) < result.size())
315         {
316             std::string& separator = result[i + minTokenSize];
317             // SSE supports only "or" and "and" in query params.
318             if ((separator != "or") && (separator != "and"))
319             {
320                 BMCWEB_LOG_ERROR
321                     << "Invalid group operator in SSE query parameters";
322                 return false;
323             }
324         }
325 
326         // SSE supports only "eq" as per spec.
327         if (op != "eq")
328         {
329             BMCWEB_LOG_ERROR
330                 << "Invalid assignment operator in SSE query parameters";
331             return false;
332         }
333 
334         BMCWEB_LOG_DEBUG << key << " : " << value;
335         if (key == "EventFormatType")
336         {
337             formatType = value;
338         }
339         else if (key == "MessageId")
340         {
341             messageIds.push_back(value);
342         }
343         else if (key == "RegistryPrefix")
344         {
345             registryPrefixes.push_back(value);
346         }
347         else if (key == "MetricReportDefinition")
348         {
349             metricReportDefinitions.push_back(value);
350         }
351         else
352         {
353             BMCWEB_LOG_ERROR << "Invalid property(" << key
354                              << ")in SSE filter query.";
355             return false;
356         }
357     }
358     return true;
359 }
360 
361 class Subscription : public persistent_data::UserSubscription
362 {
363   public:
364     Subscription(const Subscription&) = delete;
365     Subscription& operator=(const Subscription&) = delete;
366     Subscription(Subscription&&) = delete;
367     Subscription& operator=(Subscription&&) = delete;
368 
369     Subscription(const std::string& inHost, uint16_t inPort,
370                  const std::string& inPath, const std::string& inUriProto) :
371         eventSeqNum(1),
372         host(inHost), port(inPort), path(inPath), uriProto(inUriProto)
373     {
374         // Subscription constructor
375     }
376 
377     Subscription(const std::shared_ptr<boost::beast::tcp_stream>& adaptor) :
378         eventSeqNum(1)
379     {
380         sseConn = std::make_shared<crow::ServerSentEvents>(adaptor);
381     }
382 
383     ~Subscription() = default;
384 
385     bool sendEvent(std::string& msg)
386     {
387         persistent_data::EventServiceConfig eventServiceConfig =
388             persistent_data::EventServiceStore::getInstance()
389                 .getEventServiceConfig();
390         if (!eventServiceConfig.enabled)
391         {
392             return false;
393         }
394 
395         // A connection pool will be created if one does not already exist
396         crow::HttpClient::getInstance().sendData(
397             msg, id, host, port, path, httpHeaders,
398             boost::beast::http::verb::post, retryPolicyName);
399         eventSeqNum++;
400 
401         if (sseConn != nullptr)
402         {
403             sseConn->sendData(eventSeqNum, msg);
404         }
405         return true;
406     }
407 
408     bool sendTestEventLog()
409     {
410         nlohmann::json logEntryArray;
411         logEntryArray.push_back({});
412         nlohmann::json& logEntryJson = logEntryArray.back();
413 
414         logEntryJson = {
415             {"EventId", "TestID"},
416             {"EventType", "Event"},
417             {"Severity", "OK"},
418             {"Message", "Generated test event"},
419             {"MessageId", "OpenBMC.0.2.TestEventLog"},
420             {"MessageArgs", nlohmann::json::array()},
421             {"EventTimestamp", crow::utility::getDateTimeOffsetNow().first},
422             {"Context", customText}};
423 
424         nlohmann::json msg;
425         msg["@odata.type"] = "#Event.v1_4_0.Event";
426         msg["Id"] = std::to_string(eventSeqNum);
427         msg["Name"] = "Event Log";
428         msg["Events"] = logEntryArray;
429 
430         std::string strMsg =
431             msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace);
432         return this->sendEvent(strMsg);
433     }
434 
435 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
436     void filterAndSendEventLogs(
437         const std::vector<EventLogObjectsType>& eventRecords)
438     {
439         nlohmann::json logEntryArray;
440         for (const EventLogObjectsType& logEntry : eventRecords)
441         {
442             const std::string& idStr = std::get<0>(logEntry);
443             const std::string& timestamp = std::get<1>(logEntry);
444             const std::string& messageID = std::get<2>(logEntry);
445             const std::string& registryName = std::get<3>(logEntry);
446             const std::string& messageKey = std::get<4>(logEntry);
447             const std::vector<std::string>& messageArgs = std::get<5>(logEntry);
448 
449             // If registryPrefixes list is empty, don't filter events
450             // send everything.
451             if (!registryPrefixes.empty())
452             {
453                 auto obj = std::find(registryPrefixes.begin(),
454                                      registryPrefixes.end(), registryName);
455                 if (obj == registryPrefixes.end())
456                 {
457                     continue;
458                 }
459             }
460 
461             // If registryMsgIds list is empty, don't filter events
462             // send everything.
463             if (!registryMsgIds.empty())
464             {
465                 auto obj = std::find(registryMsgIds.begin(),
466                                      registryMsgIds.end(), messageKey);
467                 if (obj == registryMsgIds.end())
468                 {
469                     continue;
470                 }
471             }
472 
473             std::vector<std::string_view> messageArgsView(messageArgs.begin(),
474                                                           messageArgs.end());
475 
476             logEntryArray.push_back({});
477             nlohmann::json& bmcLogEntry = logEntryArray.back();
478             if (event_log::formatEventLogEntry(idStr, messageID,
479                                                messageArgsView, timestamp,
480                                                customText, bmcLogEntry) != 0)
481             {
482                 BMCWEB_LOG_DEBUG << "Read eventLog entry failed";
483                 continue;
484             }
485         }
486 
487         if (logEntryArray.empty())
488         {
489             BMCWEB_LOG_DEBUG << "No log entries available to be transferred.";
490             return;
491         }
492 
493         nlohmann::json msg;
494         msg["@odata.type"] = "#Event.v1_4_0.Event";
495         msg["Id"] = std::to_string(eventSeqNum);
496         msg["Name"] = "Event Log";
497         msg["Events"] = logEntryArray;
498 
499         std::string strMsg =
500             msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace);
501         this->sendEvent(strMsg);
502     }
503 #endif
504 
505     void filterAndSendReports(const std::string& reportId,
506                               const telemetry::TimestampReadings& var)
507     {
508         boost::urls::url mrdUri =
509             crow::utility::urlFromPieces("redfish", "v1", "TelemetryService",
510                                          "MetricReportDefinitions", reportId);
511 
512         // Empty list means no filter. Send everything.
513         if (!metricReportDefinitions.empty())
514         {
515             if (std::find(metricReportDefinitions.begin(),
516                           metricReportDefinitions.end(),
517                           mrdUri.string()) == metricReportDefinitions.end())
518             {
519                 return;
520             }
521         }
522 
523         nlohmann::json msg;
524         if (!telemetry::fillReport(msg, reportId, var))
525         {
526             BMCWEB_LOG_ERROR << "Failed to fill the MetricReport for DBus "
527                                 "Report with id "
528                              << reportId;
529             return;
530         }
531 
532         // Context is set by user during Event subscription and it must be
533         // set for MetricReport response.
534         if (!customText.empty())
535         {
536             msg["Context"] = customText;
537         }
538 
539         std::string strMsg =
540             msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace);
541         this->sendEvent(strMsg);
542     }
543 
544     void updateRetryConfig(const uint32_t retryAttempts,
545                            const uint32_t retryTimeoutInterval)
546     {
547         crow::HttpClient::getInstance().setRetryConfig(
548             retryAttempts, retryTimeoutInterval, retryRespHandler,
549             retryPolicyName);
550     }
551 
552     void updateRetryPolicy()
553     {
554         crow::HttpClient::getInstance().setRetryPolicy(retryPolicy,
555                                                        retryPolicyName);
556     }
557 
558     uint64_t getEventSeqNum() const
559     {
560         return eventSeqNum;
561     }
562 
563   private:
564     uint64_t eventSeqNum;
565     std::string host;
566     uint16_t port = 0;
567     std::string path;
568     std::string uriProto;
569     std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr;
570     std::string retryPolicyName = "SubscriptionEvent";
571 
572     // Check used to indicate what response codes are valid as part of our retry
573     // policy.  2XX is considered acceptable
574     static boost::system::error_code retryRespHandler(unsigned int respCode)
575     {
576         BMCWEB_LOG_DEBUG
577             << "Checking response code validity for SubscriptionEvent";
578         if ((respCode < 200) || (respCode >= 300))
579         {
580             return boost::system::errc::make_error_code(
581                 boost::system::errc::result_out_of_range);
582         }
583 
584         // Return 0 if the response code is valid
585         return boost::system::errc::make_error_code(
586             boost::system::errc::success);
587     }
588 };
589 
590 class EventServiceManager
591 {
592   private:
593     bool serviceEnabled = false;
594     uint32_t retryAttempts = 0;
595     uint32_t retryTimeoutInterval = 0;
596 
597     EventServiceManager()
598     {
599         // Load config from persist store.
600         initConfig();
601     }
602 
603     std::streampos redfishLogFilePosition{0};
604     size_t noOfEventLogSubscribers{0};
605     size_t noOfMetricReportSubscribers{0};
606     std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor;
607     boost::container::flat_map<std::string, std::shared_ptr<Subscription>>
608         subscriptionsMap;
609 
610     uint64_t eventId{1};
611 
612   public:
613     EventServiceManager(const EventServiceManager&) = delete;
614     EventServiceManager& operator=(const EventServiceManager&) = delete;
615     EventServiceManager(EventServiceManager&&) = delete;
616     EventServiceManager& operator=(EventServiceManager&&) = delete;
617     ~EventServiceManager() = default;
618 
619     static EventServiceManager& getInstance()
620     {
621         static EventServiceManager handler;
622         return handler;
623     }
624 
625     void initConfig()
626     {
627         loadOldBehavior();
628 
629         persistent_data::EventServiceConfig eventServiceConfig =
630             persistent_data::EventServiceStore::getInstance()
631                 .getEventServiceConfig();
632 
633         serviceEnabled = eventServiceConfig.enabled;
634         retryAttempts = eventServiceConfig.retryAttempts;
635         retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval;
636 
637         for (const auto& it : persistent_data::EventServiceStore::getInstance()
638                                   .subscriptionsConfigMap)
639         {
640             std::shared_ptr<persistent_data::UserSubscription> newSub =
641                 it.second;
642 
643             std::string host;
644             std::string urlProto;
645             uint16_t port = 0;
646             std::string path;
647             bool status = crow::utility::validateAndSplitUrl(
648                 newSub->destinationUrl, urlProto, host, port, path);
649 
650             if (!status)
651             {
652                 BMCWEB_LOG_ERROR
653                     << "Failed to validate and split destination url";
654                 continue;
655             }
656             std::shared_ptr<Subscription> subValue =
657                 std::make_shared<Subscription>(host, port, path, urlProto);
658 
659             subValue->id = newSub->id;
660             subValue->destinationUrl = newSub->destinationUrl;
661             subValue->protocol = newSub->protocol;
662             subValue->retryPolicy = newSub->retryPolicy;
663             subValue->customText = newSub->customText;
664             subValue->eventFormatType = newSub->eventFormatType;
665             subValue->subscriptionType = newSub->subscriptionType;
666             subValue->registryMsgIds = newSub->registryMsgIds;
667             subValue->registryPrefixes = newSub->registryPrefixes;
668             subValue->resourceTypes = newSub->resourceTypes;
669             subValue->httpHeaders = newSub->httpHeaders;
670             subValue->metricReportDefinitions = newSub->metricReportDefinitions;
671 
672             if (subValue->id.empty())
673             {
674                 BMCWEB_LOG_ERROR << "Failed to add subscription";
675             }
676             subscriptionsMap.insert(std::pair(subValue->id, subValue));
677 
678             updateNoOfSubscribersCount();
679 
680 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
681 
682             cacheRedfishLogFile();
683 
684 #endif
685             // Update retry configuration.
686             subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval);
687             subValue->updateRetryPolicy();
688         }
689     }
690 
691     static void loadOldBehavior()
692     {
693         std::ifstream eventConfigFile(eventServiceFile);
694         if (!eventConfigFile.good())
695         {
696             BMCWEB_LOG_DEBUG << "Old eventService config not exist";
697             return;
698         }
699         auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false);
700         if (jsonData.is_discarded())
701         {
702             BMCWEB_LOG_ERROR << "Old eventService config parse error.";
703             return;
704         }
705 
706         for (const auto& item : jsonData.items())
707         {
708             if (item.key() == "Configuration")
709             {
710                 persistent_data::EventServiceStore::getInstance()
711                     .getEventServiceConfig()
712                     .fromJson(item.value());
713             }
714             else if (item.key() == "Subscriptions")
715             {
716                 for (const auto& elem : item.value())
717                 {
718                     std::shared_ptr<persistent_data::UserSubscription>
719                         newSubscription =
720                             persistent_data::UserSubscription::fromJson(elem,
721                                                                         true);
722                     if (newSubscription == nullptr)
723                     {
724                         BMCWEB_LOG_ERROR << "Problem reading subscription "
725                                             "from old persistent store";
726                         continue;
727                     }
728 
729                     std::uniform_int_distribution<uint32_t> dist(0);
730                     bmcweb::OpenSSLGenerator gen;
731 
732                     std::string id;
733 
734                     int retry = 3;
735                     while (retry != 0)
736                     {
737                         id = std::to_string(dist(gen));
738                         if (gen.error())
739                         {
740                             retry = 0;
741                             break;
742                         }
743                         newSubscription->id = id;
744                         auto inserted =
745                             persistent_data::EventServiceStore::getInstance()
746                                 .subscriptionsConfigMap.insert(
747                                     std::pair(id, newSubscription));
748                         if (inserted.second)
749                         {
750                             break;
751                         }
752                         --retry;
753                     }
754 
755                     if (retry <= 0)
756                     {
757                         BMCWEB_LOG_ERROR
758                             << "Failed to generate random number from old "
759                                "persistent store";
760                         continue;
761                     }
762                 }
763             }
764 
765             persistent_data::getConfig().writeData();
766             std::remove(eventServiceFile);
767             BMCWEB_LOG_DEBUG << "Remove old eventservice config";
768         }
769     }
770 
771     void updateSubscriptionData() const
772     {
773         persistent_data::EventServiceStore::getInstance()
774             .eventServiceConfig.enabled = serviceEnabled;
775         persistent_data::EventServiceStore::getInstance()
776             .eventServiceConfig.retryAttempts = retryAttempts;
777         persistent_data::EventServiceStore::getInstance()
778             .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval;
779 
780         persistent_data::getConfig().writeData();
781     }
782 
783     void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg)
784     {
785         bool updateConfig = false;
786         bool updateRetryCfg = false;
787 
788         if (serviceEnabled != cfg.enabled)
789         {
790             serviceEnabled = cfg.enabled;
791             if (serviceEnabled && noOfMetricReportSubscribers != 0U)
792             {
793                 registerMetricReportSignal();
794             }
795             else
796             {
797                 unregisterMetricReportSignal();
798             }
799             updateConfig = true;
800         }
801 
802         if (retryAttempts != cfg.retryAttempts)
803         {
804             retryAttempts = cfg.retryAttempts;
805             updateConfig = true;
806             updateRetryCfg = true;
807         }
808 
809         if (retryTimeoutInterval != cfg.retryTimeoutInterval)
810         {
811             retryTimeoutInterval = cfg.retryTimeoutInterval;
812             updateConfig = true;
813             updateRetryCfg = true;
814         }
815 
816         if (updateConfig)
817         {
818             updateSubscriptionData();
819         }
820 
821         if (updateRetryCfg)
822         {
823             // Update the changed retry config to all subscriptions
824             for (const auto& it :
825                  EventServiceManager::getInstance().subscriptionsMap)
826             {
827                 std::shared_ptr<Subscription> entry = it.second;
828                 entry->updateRetryConfig(retryAttempts, retryTimeoutInterval);
829             }
830         }
831     }
832 
833     void updateNoOfSubscribersCount()
834     {
835         size_t eventLogSubCount = 0;
836         size_t metricReportSubCount = 0;
837         for (const auto& it : subscriptionsMap)
838         {
839             std::shared_ptr<Subscription> entry = it.second;
840             if (entry->eventFormatType == eventFormatType)
841             {
842                 eventLogSubCount++;
843             }
844             else if (entry->eventFormatType == metricReportFormatType)
845             {
846                 metricReportSubCount++;
847             }
848         }
849 
850         noOfEventLogSubscribers = eventLogSubCount;
851         if (noOfMetricReportSubscribers != metricReportSubCount)
852         {
853             noOfMetricReportSubscribers = metricReportSubCount;
854             if (noOfMetricReportSubscribers != 0U)
855             {
856                 registerMetricReportSignal();
857             }
858             else
859             {
860                 unregisterMetricReportSignal();
861             }
862         }
863     }
864 
865     std::shared_ptr<Subscription> getSubscription(const std::string& id)
866     {
867         auto obj = subscriptionsMap.find(id);
868         if (obj == subscriptionsMap.end())
869         {
870             BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id;
871             return nullptr;
872         }
873         std::shared_ptr<Subscription> subValue = obj->second;
874         return subValue;
875     }
876 
877     std::string addSubscription(const std::shared_ptr<Subscription>& subValue,
878                                 const bool updateFile = true)
879     {
880 
881         std::uniform_int_distribution<uint32_t> dist(0);
882         bmcweb::OpenSSLGenerator gen;
883 
884         std::string id;
885 
886         int retry = 3;
887         while (retry != 0)
888         {
889             id = std::to_string(dist(gen));
890             if (gen.error())
891             {
892                 retry = 0;
893                 break;
894             }
895             auto inserted = subscriptionsMap.insert(std::pair(id, subValue));
896             if (inserted.second)
897             {
898                 break;
899             }
900             --retry;
901         }
902 
903         if (retry <= 0)
904         {
905             BMCWEB_LOG_ERROR << "Failed to generate random number";
906             return "";
907         }
908 
909         std::shared_ptr<persistent_data::UserSubscription> newSub =
910             std::make_shared<persistent_data::UserSubscription>();
911         newSub->id = id;
912         newSub->destinationUrl = subValue->destinationUrl;
913         newSub->protocol = subValue->protocol;
914         newSub->retryPolicy = subValue->retryPolicy;
915         newSub->customText = subValue->customText;
916         newSub->eventFormatType = subValue->eventFormatType;
917         newSub->subscriptionType = subValue->subscriptionType;
918         newSub->registryMsgIds = subValue->registryMsgIds;
919         newSub->registryPrefixes = subValue->registryPrefixes;
920         newSub->resourceTypes = subValue->resourceTypes;
921         newSub->httpHeaders = subValue->httpHeaders;
922         newSub->metricReportDefinitions = subValue->metricReportDefinitions;
923         persistent_data::EventServiceStore::getInstance()
924             .subscriptionsConfigMap.emplace(newSub->id, newSub);
925 
926         updateNoOfSubscribersCount();
927 
928         if (updateFile)
929         {
930             updateSubscriptionData();
931         }
932 
933 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
934         if (redfishLogFilePosition != 0)
935         {
936             cacheRedfishLogFile();
937         }
938 #endif
939         // Update retry configuration.
940         subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval);
941         subValue->updateRetryPolicy();
942 
943         return id;
944     }
945 
946     bool isSubscriptionExist(const std::string& id)
947     {
948         auto obj = subscriptionsMap.find(id);
949         return obj != subscriptionsMap.end();
950     }
951 
952     void deleteSubscription(const std::string& id)
953     {
954         auto obj = subscriptionsMap.find(id);
955         if (obj != subscriptionsMap.end())
956         {
957             subscriptionsMap.erase(obj);
958             auto obj2 = persistent_data::EventServiceStore::getInstance()
959                             .subscriptionsConfigMap.find(id);
960             persistent_data::EventServiceStore::getInstance()
961                 .subscriptionsConfigMap.erase(obj2);
962             updateNoOfSubscribersCount();
963             updateSubscriptionData();
964         }
965     }
966 
967     size_t getNumberOfSubscriptions()
968     {
969         return subscriptionsMap.size();
970     }
971 
972     std::vector<std::string> getAllIDs()
973     {
974         std::vector<std::string> idList;
975         for (const auto& it : subscriptionsMap)
976         {
977             idList.emplace_back(it.first);
978         }
979         return idList;
980     }
981 
982     bool isDestinationExist(const std::string& destUrl)
983     {
984         for (const auto& it : subscriptionsMap)
985         {
986             std::shared_ptr<Subscription> entry = it.second;
987             if (entry->destinationUrl == destUrl)
988             {
989                 BMCWEB_LOG_ERROR << "Destination exist already" << destUrl;
990                 return true;
991             }
992         }
993         return false;
994     }
995 
996     bool sendTestEventLog()
997     {
998         for (const auto& it : this->subscriptionsMap)
999         {
1000             std::shared_ptr<Subscription> entry = it.second;
1001             if (!entry->sendTestEventLog())
1002             {
1003                 return false;
1004             }
1005         }
1006         return true;
1007     }
1008 
1009     void sendEvent(const nlohmann::json& eventMessageIn,
1010                    const std::string& origin, const std::string& resType)
1011     {
1012         if (!serviceEnabled || (noOfEventLogSubscribers == 0U))
1013         {
1014             BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions.";
1015             return;
1016         }
1017         nlohmann::json eventRecord = nlohmann::json::array();
1018         nlohmann::json eventMessage = eventMessageIn;
1019         // MemberId is 0 : since we are sending one event record.
1020         uint64_t memberId = 0;
1021 
1022         nlohmann::json event = {
1023             {"EventId", eventId},
1024             {"MemberId", memberId},
1025             {"EventTimestamp", crow::utility::getDateTimeOffsetNow().first},
1026             {"OriginOfCondition", origin}};
1027         for (nlohmann::json::iterator it = event.begin(); it != event.end();
1028              ++it)
1029         {
1030             eventMessage[it.key()] = it.value();
1031         }
1032         eventRecord.push_back(eventMessage);
1033 
1034         for (const auto& it : this->subscriptionsMap)
1035         {
1036             std::shared_ptr<Subscription> entry = it.second;
1037             bool isSubscribed = false;
1038             // Search the resourceTypes list for the subscription.
1039             // If resourceTypes list is empty, don't filter events
1040             // send everything.
1041             if (!entry->resourceTypes.empty())
1042             {
1043                 for (const auto& resource : entry->resourceTypes)
1044                 {
1045                     if (resType == resource)
1046                     {
1047                         BMCWEB_LOG_INFO << "ResourceType " << resource
1048                                         << " found in the subscribed list";
1049                         isSubscribed = true;
1050                         break;
1051                     }
1052                 }
1053             }
1054             else // resourceTypes list is empty.
1055             {
1056                 isSubscribed = true;
1057             }
1058             if (isSubscribed)
1059             {
1060                 nlohmann::json msgJson = {
1061                     {"@odata.type", "#Event.v1_4_0.Event"},
1062                     {"Name", "Event Log"},
1063                     {"Id", eventId},
1064                     {"Events", eventRecord}};
1065 
1066                 std::string strMsg = msgJson.dump(
1067                     2, ' ', true, nlohmann::json::error_handler_t::replace);
1068                 entry->sendEvent(strMsg);
1069                 eventId++; // increament the eventId
1070             }
1071             else
1072             {
1073                 BMCWEB_LOG_INFO << "Not subscribed to this resource";
1074             }
1075         }
1076     }
1077     void sendBroadcastMsg(const std::string& broadcastMsg)
1078     {
1079         for (const auto& it : this->subscriptionsMap)
1080         {
1081             std::shared_ptr<Subscription> entry = it.second;
1082             nlohmann::json msgJson = {
1083                 {"Timestamp", crow::utility::getDateTimeOffsetNow().first},
1084                 {"OriginOfCondition", "/ibm/v1/HMC/BroadcastService"},
1085                 {"Name", "Broadcast Message"},
1086                 {"Message", broadcastMsg}};
1087 
1088             std::string strMsg = msgJson.dump(
1089                 2, ' ', true, nlohmann::json::error_handler_t::replace);
1090             entry->sendEvent(strMsg);
1091         }
1092     }
1093 
1094 #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
1095 
1096     void resetRedfishFilePosition()
1097     {
1098         // Control would be here when Redfish file is created.
1099         // Reset File Position as new file is created
1100         redfishLogFilePosition = 0;
1101     }
1102 
1103     void cacheRedfishLogFile()
1104     {
1105         // Open the redfish file and read till the last record.
1106 
1107         std::ifstream logStream(redfishEventLogFile);
1108         if (!logStream.good())
1109         {
1110             BMCWEB_LOG_ERROR << " Redfish log file open failed \n";
1111             return;
1112         }
1113         std::string logEntry;
1114         while (std::getline(logStream, logEntry))
1115         {
1116             redfishLogFilePosition = logStream.tellg();
1117         }
1118 
1119         BMCWEB_LOG_DEBUG << "Next Log Position : " << redfishLogFilePosition;
1120     }
1121 
1122     void readEventLogsFromFile()
1123     {
1124         std::ifstream logStream(redfishEventLogFile);
1125         if (!logStream.good())
1126         {
1127             BMCWEB_LOG_ERROR << " Redfish log file open failed";
1128             return;
1129         }
1130 
1131         std::vector<EventLogObjectsType> eventRecords;
1132 
1133         std::string logEntry;
1134 
1135         // Get the read pointer to the next log to be read.
1136         logStream.seekg(redfishLogFilePosition);
1137 
1138         while (std::getline(logStream, logEntry))
1139         {
1140             // Update Pointer position
1141             redfishLogFilePosition = logStream.tellg();
1142 
1143             std::string idStr;
1144             if (!event_log::getUniqueEntryID(logEntry, idStr))
1145             {
1146                 continue;
1147             }
1148 
1149             if (!serviceEnabled || noOfEventLogSubscribers == 0)
1150             {
1151                 // If Service is not enabled, no need to compute
1152                 // the remaining items below.
1153                 // But, Loop must continue to keep track of Timestamp
1154                 continue;
1155             }
1156 
1157             std::string timestamp;
1158             std::string messageID;
1159             std::vector<std::string> messageArgs;
1160             if (event_log::getEventLogParams(logEntry, timestamp, messageID,
1161                                              messageArgs) != 0)
1162             {
1163                 BMCWEB_LOG_DEBUG << "Read eventLog entry params failed";
1164                 continue;
1165             }
1166 
1167             std::string registryName;
1168             std::string messageKey;
1169             event_log::getRegistryAndMessageKey(messageID, registryName,
1170                                                 messageKey);
1171             if (registryName.empty() || messageKey.empty())
1172             {
1173                 continue;
1174             }
1175 
1176             eventRecords.emplace_back(idStr, timestamp, messageID, registryName,
1177                                       messageKey, messageArgs);
1178         }
1179 
1180         if (!serviceEnabled || noOfEventLogSubscribers == 0)
1181         {
1182             BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions.";
1183             return;
1184         }
1185 
1186         if (eventRecords.empty())
1187         {
1188             // No Records to send
1189             BMCWEB_LOG_DEBUG << "No log entries available to be transferred.";
1190             return;
1191         }
1192 
1193         for (const auto& it : this->subscriptionsMap)
1194         {
1195             std::shared_ptr<Subscription> entry = it.second;
1196             if (entry->eventFormatType == "Event")
1197             {
1198                 entry->filterAndSendEventLogs(eventRecords);
1199             }
1200         }
1201     }
1202 
1203     static void watchRedfishEventLogFile()
1204     {
1205         if (!inotifyConn)
1206         {
1207             return;
1208         }
1209 
1210         static std::array<char, 1024> readBuffer;
1211 
1212         inotifyConn->async_read_some(boost::asio::buffer(readBuffer),
1213                                      [&](const boost::system::error_code& ec,
1214                                          const std::size_t& bytesTransferred) {
1215             if (ec)
1216             {
1217                 BMCWEB_LOG_ERROR << "Callback Error: " << ec.message();
1218                 return;
1219             }
1220             std::size_t index = 0;
1221             while ((index + iEventSize) <= bytesTransferred)
1222             {
1223                 struct inotify_event event
1224                 {};
1225                 std::memcpy(&event, &readBuffer[index], iEventSize);
1226                 if (event.wd == dirWatchDesc)
1227                 {
1228                     if ((event.len == 0) ||
1229                         (index + iEventSize + event.len > bytesTransferred))
1230                     {
1231                         index += (iEventSize + event.len);
1232                         continue;
1233                     }
1234 
1235                     std::string fileName(&readBuffer[index + iEventSize]);
1236                     if (fileName != "redfish")
1237                     {
1238                         index += (iEventSize + event.len);
1239                         continue;
1240                     }
1241 
1242                     BMCWEB_LOG_DEBUG
1243                         << "Redfish log file created/deleted. event.name: "
1244                         << fileName;
1245                     if (event.mask == IN_CREATE)
1246                     {
1247                         if (fileWatchDesc != -1)
1248                         {
1249                             BMCWEB_LOG_DEBUG
1250                                 << "Remove and Add inotify watcher on "
1251                                    "redfish event log file";
1252                             // Remove existing inotify watcher and add
1253                             // with new redfish event log file.
1254                             inotify_rm_watch(inotifyFd, fileWatchDesc);
1255                             fileWatchDesc = -1;
1256                         }
1257 
1258                         fileWatchDesc = inotify_add_watch(
1259                             inotifyFd, redfishEventLogFile, IN_MODIFY);
1260                         if (fileWatchDesc == -1)
1261                         {
1262                             BMCWEB_LOG_ERROR << "inotify_add_watch failed for "
1263                                                 "redfish log file.";
1264                             return;
1265                         }
1266 
1267                         EventServiceManager::getInstance()
1268                             .resetRedfishFilePosition();
1269                         EventServiceManager::getInstance()
1270                             .readEventLogsFromFile();
1271                     }
1272                     else if ((event.mask == IN_DELETE) ||
1273                              (event.mask == IN_MOVED_TO))
1274                     {
1275                         if (fileWatchDesc != -1)
1276                         {
1277                             inotify_rm_watch(inotifyFd, fileWatchDesc);
1278                             fileWatchDesc = -1;
1279                         }
1280                     }
1281                 }
1282                 else if (event.wd == fileWatchDesc)
1283                 {
1284                     if (event.mask == IN_MODIFY)
1285                     {
1286                         EventServiceManager::getInstance()
1287                             .readEventLogsFromFile();
1288                     }
1289                 }
1290                 index += (iEventSize + event.len);
1291             }
1292 
1293             watchRedfishEventLogFile();
1294         });
1295     }
1296 
1297     static int startEventLogMonitor(boost::asio::io_context& ioc)
1298     {
1299         inotifyConn.emplace(ioc);
1300         inotifyFd = inotify_init1(IN_NONBLOCK);
1301         if (inotifyFd == -1)
1302         {
1303             BMCWEB_LOG_ERROR << "inotify_init1 failed.";
1304             return -1;
1305         }
1306 
1307         // Add watch on directory to handle redfish event log file
1308         // create/delete.
1309         dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir,
1310                                          IN_CREATE | IN_MOVED_TO | IN_DELETE);
1311         if (dirWatchDesc == -1)
1312         {
1313             BMCWEB_LOG_ERROR
1314                 << "inotify_add_watch failed for event log directory.";
1315             return -1;
1316         }
1317 
1318         // Watch redfish event log file for modifications.
1319         fileWatchDesc =
1320             inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY);
1321         if (fileWatchDesc == -1)
1322         {
1323             BMCWEB_LOG_ERROR
1324                 << "inotify_add_watch failed for redfish log file.";
1325             // Don't return error if file not exist.
1326             // Watch on directory will handle create/delete of file.
1327         }
1328 
1329         // monitor redfish event log file
1330         inotifyConn->assign(inotifyFd);
1331         watchRedfishEventLogFile();
1332 
1333         return 0;
1334     }
1335 
1336 #endif
1337     static void getReadingsForReport(sdbusplus::message::message& msg)
1338     {
1339         if (msg.is_method_error())
1340         {
1341             BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error";
1342             return;
1343         }
1344 
1345         sdbusplus::message::object_path path(msg.get_path());
1346         std::string id = path.filename();
1347         if (id.empty())
1348         {
1349             BMCWEB_LOG_ERROR << "Failed to get Id from path";
1350             return;
1351         }
1352 
1353         std::string interface;
1354         dbus::utility::DBusPropertiesMap props;
1355         std::vector<std::string> invalidProps;
1356         msg.read(interface, props, invalidProps);
1357 
1358         auto found =
1359             std::find_if(props.begin(), props.end(),
1360                          [](const auto& x) { return x.first == "Readings"; });
1361         if (found == props.end())
1362         {
1363             BMCWEB_LOG_INFO << "Failed to get Readings from Report properties";
1364             return;
1365         }
1366 
1367         const telemetry::TimestampReadings* readings =
1368             std::get_if<telemetry::TimestampReadings>(&found->second);
1369         if (readings == nullptr)
1370         {
1371             BMCWEB_LOG_INFO << "Failed to get Readings from Report properties";
1372             return;
1373         }
1374 
1375         for (const auto& it :
1376              EventServiceManager::getInstance().subscriptionsMap)
1377         {
1378             Subscription& entry = *it.second;
1379             if (entry.eventFormatType == metricReportFormatType)
1380             {
1381                 entry.filterAndSendReports(id, *readings);
1382             }
1383         }
1384     }
1385 
1386     void unregisterMetricReportSignal()
1387     {
1388         if (matchTelemetryMonitor)
1389         {
1390             BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister";
1391             matchTelemetryMonitor.reset();
1392             matchTelemetryMonitor = nullptr;
1393         }
1394     }
1395 
1396     void registerMetricReportSignal()
1397     {
1398         if (!serviceEnabled || matchTelemetryMonitor)
1399         {
1400             BMCWEB_LOG_DEBUG << "Not registering metric report signal.";
1401             return;
1402         }
1403 
1404         BMCWEB_LOG_DEBUG << "Metrics report signal - Register";
1405         std::string matchStr = "type='signal',member='PropertiesChanged',"
1406                                "interface='org.freedesktop.DBus.Properties',"
1407                                "arg0=xyz.openbmc_project.Telemetry.Report";
1408 
1409         matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>(
1410             *crow::connections::systemBus, matchStr, getReadingsForReport);
1411     }
1412 };
1413 
1414 } // namespace redfish
1415