xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision e855dd28ff68c13fea57b49f34da9302e6b2f6cd)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
181da66f75SEd Tanous #include "node.hpp"
194851d45dSJason M. Bills #include "registries.hpp"
204851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
214851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
221da66f75SEd Tanous 
23e1f26343SJason M. Bills #include <systemd/sd-journal.h>
24e1f26343SJason M. Bills 
254851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
264851d45dSJason M. Bills #include <boost/beast/core/span.hpp>
271da66f75SEd Tanous #include <boost/container/flat_map.hpp>
28cb92c03bSAndrew Geissler #include <error_messages.hpp>
294418c7f0SJames Feist #include <filesystem>
30cd225da8SJason M. Bills #include <string_view>
31abf2add6SEd Tanous #include <variant>
321da66f75SEd Tanous 
331da66f75SEd Tanous namespace redfish
341da66f75SEd Tanous {
351da66f75SEd Tanous 
36424c4176SJason M. Bills constexpr char const *CrashdumpObject = "com.intel.crashdump";
37424c4176SJason M. Bills constexpr char const *CrashdumpPath = "/com/intel/crashdump";
38424c4176SJason M. Bills constexpr char const *CrashdumpOnDemandPath = "/com/intel/crashdump/OnDemand";
39424c4176SJason M. Bills constexpr char const *CrashdumpInterface = "com.intel.crashdump";
40424c4176SJason M. Bills constexpr char const *CrashdumpOnDemandInterface =
41424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
42424c4176SJason M. Bills constexpr char const *CrashdumpRawPECIInterface =
43424c4176SJason M. Bills     "com.intel.crashdump.SendRawPeci";
441da66f75SEd Tanous 
454851d45dSJason M. Bills namespace message_registries
464851d45dSJason M. Bills {
474851d45dSJason M. Bills static const Message *getMessageFromRegistry(
484851d45dSJason M. Bills     const std::string &messageKey,
494851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
504851d45dSJason M. Bills {
514851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
524851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
534851d45dSJason M. Bills                      [&messageKey](const MessageEntry &messageEntry) {
544851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
554851d45dSJason M. Bills                                              messageKey.c_str());
564851d45dSJason M. Bills                      });
574851d45dSJason M. Bills     if (messageIt != registry.cend())
584851d45dSJason M. Bills     {
594851d45dSJason M. Bills         return &messageIt->second;
604851d45dSJason M. Bills     }
614851d45dSJason M. Bills 
624851d45dSJason M. Bills     return nullptr;
634851d45dSJason M. Bills }
644851d45dSJason M. Bills 
654851d45dSJason M. Bills static const Message *getMessage(const std::string_view &messageID)
664851d45dSJason M. Bills {
674851d45dSJason M. Bills     // Redfish MessageIds are in the form
684851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
694851d45dSJason M. Bills     // the right Message
704851d45dSJason M. Bills     std::vector<std::string> fields;
714851d45dSJason M. Bills     fields.reserve(4);
724851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
734851d45dSJason M. Bills     std::string &registryName = fields[0];
744851d45dSJason M. Bills     std::string &messageKey = fields[3];
754851d45dSJason M. Bills 
764851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
774851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
784851d45dSJason M. Bills     {
794851d45dSJason M. Bills         return getMessageFromRegistry(
804851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
814851d45dSJason M. Bills     }
824851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
834851d45dSJason M. Bills     {
844851d45dSJason M. Bills         return getMessageFromRegistry(
854851d45dSJason M. Bills             messageKey,
864851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
874851d45dSJason M. Bills     }
884851d45dSJason M. Bills     return nullptr;
894851d45dSJason M. Bills }
904851d45dSJason M. Bills } // namespace message_registries
914851d45dSJason M. Bills 
92f6150403SJames Feist namespace fs = std::filesystem;
931da66f75SEd Tanous 
94cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
95cb92c03bSAndrew Geissler     std::string,
96cb92c03bSAndrew Geissler     sdbusplus::message::variant<std::string, bool, uint8_t, int16_t, uint16_t,
97cb92c03bSAndrew Geissler                                 int32_t, uint32_t, int64_t, uint64_t, double>>;
98cb92c03bSAndrew Geissler 
99cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
100cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
101cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
102cb92c03bSAndrew Geissler 
103cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string &s)
104cb92c03bSAndrew Geissler {
105cb92c03bSAndrew Geissler     if (s == "xyz.openbmc_project.Logging.Entry.Level.Alert")
106cb92c03bSAndrew Geissler     {
107cb92c03bSAndrew Geissler         return "Critical";
108cb92c03bSAndrew Geissler     }
109cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Critical")
110cb92c03bSAndrew Geissler     {
111cb92c03bSAndrew Geissler         return "Critical";
112cb92c03bSAndrew Geissler     }
113cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Debug")
114cb92c03bSAndrew Geissler     {
115cb92c03bSAndrew Geissler         return "OK";
116cb92c03bSAndrew Geissler     }
117cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency")
118cb92c03bSAndrew Geissler     {
119cb92c03bSAndrew Geissler         return "Critical";
120cb92c03bSAndrew Geissler     }
121cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Error")
122cb92c03bSAndrew Geissler     {
123cb92c03bSAndrew Geissler         return "Critical";
124cb92c03bSAndrew Geissler     }
125cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Informational")
126cb92c03bSAndrew Geissler     {
127cb92c03bSAndrew Geissler         return "OK";
128cb92c03bSAndrew Geissler     }
129cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Notice")
130cb92c03bSAndrew Geissler     {
131cb92c03bSAndrew Geissler         return "OK";
132cb92c03bSAndrew Geissler     }
133cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
134cb92c03bSAndrew Geissler     {
135cb92c03bSAndrew Geissler         return "Warning";
136cb92c03bSAndrew Geissler     }
137cb92c03bSAndrew Geissler     return "";
138cb92c03bSAndrew Geissler }
139cb92c03bSAndrew Geissler 
14016428a1aSJason M. Bills static int getJournalMetadata(sd_journal *journal,
14139e77504SEd Tanous                               const std::string_view &field,
14239e77504SEd Tanous                               std::string_view &contents)
14316428a1aSJason M. Bills {
14416428a1aSJason M. Bills     const char *data = nullptr;
14516428a1aSJason M. Bills     size_t length = 0;
14616428a1aSJason M. Bills     int ret = 0;
14716428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
148271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
149271584abSEd Tanous                               reinterpret_cast<const void **>(&data), &length);
15016428a1aSJason M. Bills     if (ret < 0)
15116428a1aSJason M. Bills     {
15216428a1aSJason M. Bills         return ret;
15316428a1aSJason M. Bills     }
15439e77504SEd Tanous     contents = std::string_view(data, length);
15516428a1aSJason M. Bills     // Only use the content after the "=" character.
15616428a1aSJason M. Bills     contents.remove_prefix(std::min(contents.find("=") + 1, contents.size()));
15716428a1aSJason M. Bills     return ret;
15816428a1aSJason M. Bills }
15916428a1aSJason M. Bills 
16016428a1aSJason M. Bills static int getJournalMetadata(sd_journal *journal,
16139e77504SEd Tanous                               const std::string_view &field, const int &base,
162271584abSEd Tanous                               long int &contents)
16316428a1aSJason M. Bills {
16416428a1aSJason M. Bills     int ret = 0;
16539e77504SEd Tanous     std::string_view metadata;
16616428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16716428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16816428a1aSJason M. Bills     if (ret < 0)
16916428a1aSJason M. Bills     {
17016428a1aSJason M. Bills         return ret;
17116428a1aSJason M. Bills     }
172b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
17316428a1aSJason M. Bills     return ret;
17416428a1aSJason M. Bills }
17516428a1aSJason M. Bills 
17616428a1aSJason M. Bills static bool getEntryTimestamp(sd_journal *journal, std::string &entryTimestamp)
17716428a1aSJason M. Bills {
17816428a1aSJason M. Bills     int ret = 0;
17916428a1aSJason M. Bills     uint64_t timestamp = 0;
18016428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &timestamp);
18116428a1aSJason M. Bills     if (ret < 0)
18216428a1aSJason M. Bills     {
18316428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
18416428a1aSJason M. Bills                          << strerror(-ret);
18516428a1aSJason M. Bills         return false;
18616428a1aSJason M. Bills     }
18716428a1aSJason M. Bills     time_t t =
18816428a1aSJason M. Bills         static_cast<time_t>(timestamp / 1000 / 1000); // Convert from us to s
18916428a1aSJason M. Bills     struct tm *loctime = localtime(&t);
19016428a1aSJason M. Bills     char entryTime[64] = {};
19116428a1aSJason M. Bills     if (NULL != loctime)
19216428a1aSJason M. Bills     {
19316428a1aSJason M. Bills         strftime(entryTime, sizeof(entryTime), "%FT%T%z", loctime);
19416428a1aSJason M. Bills     }
19516428a1aSJason M. Bills     // Insert the ':' into the timezone
19639e77504SEd Tanous     std::string_view t1(entryTime);
19739e77504SEd Tanous     std::string_view t2(entryTime);
19816428a1aSJason M. Bills     if (t1.size() > 2 && t2.size() > 2)
19916428a1aSJason M. Bills     {
20016428a1aSJason M. Bills         t1.remove_suffix(2);
20116428a1aSJason M. Bills         t2.remove_prefix(t2.size() - 2);
20216428a1aSJason M. Bills     }
20339e77504SEd Tanous     entryTimestamp = std::string(t1) + ":" + std::string(t2);
20416428a1aSJason M. Bills     return true;
20516428a1aSJason M. Bills }
20616428a1aSJason M. Bills 
20716428a1aSJason M. Bills static bool getSkipParam(crow::Response &res, const crow::Request &req,
208271584abSEd Tanous                          uint64_t &skip)
20916428a1aSJason M. Bills {
21016428a1aSJason M. Bills     char *skipParam = req.urlParams.get("$skip");
21116428a1aSJason M. Bills     if (skipParam != nullptr)
21216428a1aSJason M. Bills     {
21316428a1aSJason M. Bills         char *ptr = nullptr;
214271584abSEd Tanous         skip = std::strtoul(skipParam, &ptr, 10);
21516428a1aSJason M. Bills         if (*skipParam == '\0' || *ptr != '\0')
21616428a1aSJason M. Bills         {
21716428a1aSJason M. Bills 
21816428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(skipParam),
21916428a1aSJason M. Bills                                                    "$skip");
22016428a1aSJason M. Bills             return false;
22116428a1aSJason M. Bills         }
22216428a1aSJason M. Bills     }
22316428a1aSJason M. Bills     return true;
22416428a1aSJason M. Bills }
22516428a1aSJason M. Bills 
226271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
22716428a1aSJason M. Bills static bool getTopParam(crow::Response &res, const crow::Request &req,
228271584abSEd Tanous                         uint64_t &top)
22916428a1aSJason M. Bills {
23016428a1aSJason M. Bills     char *topParam = req.urlParams.get("$top");
23116428a1aSJason M. Bills     if (topParam != nullptr)
23216428a1aSJason M. Bills     {
23316428a1aSJason M. Bills         char *ptr = nullptr;
234271584abSEd Tanous         top = std::strtoul(topParam, &ptr, 10);
23516428a1aSJason M. Bills         if (*topParam == '\0' || *ptr != '\0')
23616428a1aSJason M. Bills         {
23716428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(topParam),
23816428a1aSJason M. Bills                                                    "$top");
23916428a1aSJason M. Bills             return false;
24016428a1aSJason M. Bills         }
241271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
24216428a1aSJason M. Bills         {
24316428a1aSJason M. Bills 
24416428a1aSJason M. Bills             messages::queryParameterOutOfRange(
24516428a1aSJason M. Bills                 res, std::to_string(top), "$top",
24616428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
24716428a1aSJason M. Bills             return false;
24816428a1aSJason M. Bills         }
24916428a1aSJason M. Bills     }
25016428a1aSJason M. Bills     return true;
25116428a1aSJason M. Bills }
25216428a1aSJason M. Bills 
253e85d6b16SJason M. Bills static bool getUniqueEntryID(sd_journal *journal, std::string &entryID,
254e85d6b16SJason M. Bills                              const bool firstEntry = true)
25516428a1aSJason M. Bills {
25616428a1aSJason M. Bills     int ret = 0;
25716428a1aSJason M. Bills     static uint64_t prevTs = 0;
25816428a1aSJason M. Bills     static int index = 0;
259e85d6b16SJason M. Bills     if (firstEntry)
260e85d6b16SJason M. Bills     {
261e85d6b16SJason M. Bills         prevTs = 0;
262e85d6b16SJason M. Bills     }
263e85d6b16SJason M. Bills 
26416428a1aSJason M. Bills     // Get the entry timestamp
26516428a1aSJason M. Bills     uint64_t curTs = 0;
26616428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
26716428a1aSJason M. Bills     if (ret < 0)
26816428a1aSJason M. Bills     {
26916428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
27016428a1aSJason M. Bills                          << strerror(-ret);
27116428a1aSJason M. Bills         return false;
27216428a1aSJason M. Bills     }
27316428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
27416428a1aSJason M. Bills     if (curTs == prevTs)
27516428a1aSJason M. Bills     {
27616428a1aSJason M. Bills         index++;
27716428a1aSJason M. Bills     }
27816428a1aSJason M. Bills     else
27916428a1aSJason M. Bills     {
28016428a1aSJason M. Bills         // Otherwise, reset it
28116428a1aSJason M. Bills         index = 0;
28216428a1aSJason M. Bills     }
28316428a1aSJason M. Bills     // Save the timestamp
28416428a1aSJason M. Bills     prevTs = curTs;
28516428a1aSJason M. Bills 
28616428a1aSJason M. Bills     entryID = std::to_string(curTs);
28716428a1aSJason M. Bills     if (index > 0)
28816428a1aSJason M. Bills     {
28916428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
29016428a1aSJason M. Bills     }
29116428a1aSJason M. Bills     return true;
29216428a1aSJason M. Bills }
29316428a1aSJason M. Bills 
294e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string &logEntry, std::string &entryID,
295e85d6b16SJason M. Bills                              const bool firstEntry = true)
29695820184SJason M. Bills {
297271584abSEd Tanous     static time_t prevTs = 0;
29895820184SJason M. Bills     static int index = 0;
299e85d6b16SJason M. Bills     if (firstEntry)
300e85d6b16SJason M. Bills     {
301e85d6b16SJason M. Bills         prevTs = 0;
302e85d6b16SJason M. Bills     }
303e85d6b16SJason M. Bills 
30495820184SJason M. Bills     // Get the entry timestamp
305271584abSEd Tanous     std::time_t curTs = 0;
30695820184SJason M. Bills     std::tm timeStruct = {};
30795820184SJason M. Bills     std::istringstream entryStream(logEntry);
30895820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
30995820184SJason M. Bills     {
31095820184SJason M. Bills         curTs = std::mktime(&timeStruct);
31195820184SJason M. Bills     }
31295820184SJason M. Bills     // If the timestamp isn't unique, increment the index
31395820184SJason M. Bills     if (curTs == prevTs)
31495820184SJason M. Bills     {
31595820184SJason M. Bills         index++;
31695820184SJason M. Bills     }
31795820184SJason M. Bills     else
31895820184SJason M. Bills     {
31995820184SJason M. Bills         // Otherwise, reset it
32095820184SJason M. Bills         index = 0;
32195820184SJason M. Bills     }
32295820184SJason M. Bills     // Save the timestamp
32395820184SJason M. Bills     prevTs = curTs;
32495820184SJason M. Bills 
32595820184SJason M. Bills     entryID = std::to_string(curTs);
32695820184SJason M. Bills     if (index > 0)
32795820184SJason M. Bills     {
32895820184SJason M. Bills         entryID += "_" + std::to_string(index);
32995820184SJason M. Bills     }
33095820184SJason M. Bills     return true;
33195820184SJason M. Bills }
33295820184SJason M. Bills 
33316428a1aSJason M. Bills static bool getTimestampFromID(crow::Response &res, const std::string &entryID,
334271584abSEd Tanous                                uint64_t &timestamp, uint64_t &index)
33516428a1aSJason M. Bills {
33616428a1aSJason M. Bills     if (entryID.empty())
33716428a1aSJason M. Bills     {
33816428a1aSJason M. Bills         return false;
33916428a1aSJason M. Bills     }
34016428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
34139e77504SEd Tanous     std::string_view tsStr(entryID);
34216428a1aSJason M. Bills 
34316428a1aSJason M. Bills     auto underscorePos = tsStr.find("_");
34416428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
34516428a1aSJason M. Bills     {
34616428a1aSJason M. Bills         // Timestamp has an index
34716428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
34839e77504SEd Tanous         std::string_view indexStr(entryID);
34916428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
35016428a1aSJason M. Bills         std::size_t pos;
35116428a1aSJason M. Bills         try
35216428a1aSJason M. Bills         {
35339e77504SEd Tanous             index = std::stoul(std::string(indexStr), &pos);
35416428a1aSJason M. Bills         }
355271584abSEd Tanous         catch (std::invalid_argument &)
35616428a1aSJason M. Bills         {
35716428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
35816428a1aSJason M. Bills             return false;
35916428a1aSJason M. Bills         }
360271584abSEd Tanous         catch (std::out_of_range &)
36116428a1aSJason M. Bills         {
36216428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
36316428a1aSJason M. Bills             return false;
36416428a1aSJason M. Bills         }
36516428a1aSJason M. Bills         if (pos != indexStr.size())
36616428a1aSJason M. Bills         {
36716428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
36816428a1aSJason M. Bills             return false;
36916428a1aSJason M. Bills         }
37016428a1aSJason M. Bills     }
37116428a1aSJason M. Bills     // Timestamp has no index
37216428a1aSJason M. Bills     std::size_t pos;
37316428a1aSJason M. Bills     try
37416428a1aSJason M. Bills     {
37539e77504SEd Tanous         timestamp = std::stoull(std::string(tsStr), &pos);
37616428a1aSJason M. Bills     }
377271584abSEd Tanous     catch (std::invalid_argument &)
37816428a1aSJason M. Bills     {
37916428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
38016428a1aSJason M. Bills         return false;
38116428a1aSJason M. Bills     }
382271584abSEd Tanous     catch (std::out_of_range &)
38316428a1aSJason M. Bills     {
38416428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
38516428a1aSJason M. Bills         return false;
38616428a1aSJason M. Bills     }
38716428a1aSJason M. Bills     if (pos != tsStr.size())
38816428a1aSJason M. Bills     {
38916428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
39016428a1aSJason M. Bills         return false;
39116428a1aSJason M. Bills     }
39216428a1aSJason M. Bills     return true;
39316428a1aSJason M. Bills }
39416428a1aSJason M. Bills 
39595820184SJason M. Bills static bool
39695820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path> &redfishLogFiles)
39795820184SJason M. Bills {
39895820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
39995820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
40095820184SJason M. Bills 
40195820184SJason M. Bills     // Loop through the directory looking for redfish log files
40295820184SJason M. Bills     for (const std::filesystem::directory_entry &dirEnt :
40395820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
40495820184SJason M. Bills     {
40595820184SJason M. Bills         // If we find a redfish log file, save the path
40695820184SJason M. Bills         std::string filename = dirEnt.path().filename();
40795820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
40895820184SJason M. Bills         {
40995820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
41095820184SJason M. Bills         }
41195820184SJason M. Bills     }
41295820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
41395820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
41495820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
41595820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
41695820184SJason M. Bills 
41795820184SJason M. Bills     return !redfishLogFiles.empty();
41895820184SJason M. Bills }
41995820184SJason M. Bills 
420c4bf6374SJason M. Bills class SystemLogServiceCollection : public Node
4211da66f75SEd Tanous {
4221da66f75SEd Tanous   public:
4231da66f75SEd Tanous     template <typename CrowApp>
424c4bf6374SJason M. Bills     SystemLogServiceCollection(CrowApp &app) :
425029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/")
426c4bf6374SJason M. Bills     {
427c4bf6374SJason M. Bills         entityPrivileges = {
428c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
429c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
430c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
431c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
432c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
433c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
434c4bf6374SJason M. Bills     }
435c4bf6374SJason M. Bills 
436c4bf6374SJason M. Bills   private:
437c4bf6374SJason M. Bills     /**
438c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
439c4bf6374SJason M. Bills      */
440c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
441c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
442c4bf6374SJason M. Bills     {
443c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
444c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
445c4bf6374SJason M. Bills         // it has a duplicate entry for members
446c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
447c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
448c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
449c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection";
450c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
451029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
452c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
453c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
454c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
455c4bf6374SJason M. Bills         nlohmann::json &logServiceArray = asyncResp->res.jsonValue["Members"];
456c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
457029573d4SEd Tanous         logServiceArray.push_back(
458029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/EventLog"}});
459d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
460d53dd41fSJason M. Bills         logServiceArray.push_back(
461cb92c03bSAndrew Geissler             {{"@odata.id",
462424c4176SJason M. Bills               "/redfish/v1/Systems/system/LogServices/Crashdump"}});
463d53dd41fSJason M. Bills #endif
464c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
465c4bf6374SJason M. Bills             logServiceArray.size();
466c4bf6374SJason M. Bills     }
467c4bf6374SJason M. Bills };
468c4bf6374SJason M. Bills 
469c4bf6374SJason M. Bills class EventLogService : public Node
470c4bf6374SJason M. Bills {
471c4bf6374SJason M. Bills   public:
472c4bf6374SJason M. Bills     template <typename CrowApp>
473c4bf6374SJason M. Bills     EventLogService(CrowApp &app) :
474029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
475c4bf6374SJason M. Bills     {
476c4bf6374SJason M. Bills         entityPrivileges = {
477c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
478c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
479c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
480c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
481c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
482c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
483c4bf6374SJason M. Bills     }
484c4bf6374SJason M. Bills 
485c4bf6374SJason M. Bills   private:
486c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
487c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
488c4bf6374SJason M. Bills     {
489c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
490c4bf6374SJason M. Bills 
491c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
492029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
493c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
494c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
495c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
496c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
497c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
498c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
499c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "Event Log";
500c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
501c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
502c4bf6374SJason M. Bills             {"@odata.id",
503029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
504e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
505e7d6c8b2SGunnar Mills 
506e7d6c8b2SGunnar Mills             {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
507e7d6c8b2SGunnar Mills                        "Actions/LogService.ClearLog"}};
508489640c6SJason M. Bills     }
509489640c6SJason M. Bills };
510489640c6SJason M. Bills 
5111f56a3a6STim Lee class JournalEventLogClear : public Node
512489640c6SJason M. Bills {
513489640c6SJason M. Bills   public:
5141f56a3a6STim Lee     JournalEventLogClear(CrowApp &app) :
515489640c6SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
516489640c6SJason M. Bills                   "LogService.ClearLog/")
517489640c6SJason M. Bills     {
518489640c6SJason M. Bills         entityPrivileges = {
519489640c6SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
520489640c6SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
521489640c6SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
522489640c6SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
523489640c6SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
524489640c6SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
525489640c6SJason M. Bills     }
526489640c6SJason M. Bills 
527489640c6SJason M. Bills   private:
528489640c6SJason M. Bills     void doPost(crow::Response &res, const crow::Request &req,
529489640c6SJason M. Bills                 const std::vector<std::string> &params) override
530489640c6SJason M. Bills     {
531489640c6SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
532489640c6SJason M. Bills 
533489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
534489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
535489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
536489640c6SJason M. Bills         {
537489640c6SJason M. Bills             for (const std::filesystem::path &file : redfishLogFiles)
538489640c6SJason M. Bills             {
539489640c6SJason M. Bills                 std::error_code ec;
540489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
541489640c6SJason M. Bills             }
542489640c6SJason M. Bills         }
543489640c6SJason M. Bills 
544489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
545489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
546489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
547489640c6SJason M. Bills                 if (ec)
548489640c6SJason M. Bills                 {
549489640c6SJason M. Bills                     BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
550489640c6SJason M. Bills                     messages::internalError(asyncResp->res);
551489640c6SJason M. Bills                     return;
552489640c6SJason M. Bills                 }
553489640c6SJason M. Bills 
554489640c6SJason M. Bills                 messages::success(asyncResp->res);
555489640c6SJason M. Bills             },
556489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
557489640c6SJason M. Bills             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
558489640c6SJason M. Bills             "replace");
559c4bf6374SJason M. Bills     }
560c4bf6374SJason M. Bills };
561c4bf6374SJason M. Bills 
56295820184SJason M. Bills static int fillEventLogEntryJson(const std::string &logEntryID,
56395820184SJason M. Bills                                  const std::string logEntry,
56495820184SJason M. Bills                                  nlohmann::json &logEntryJson)
565c4bf6374SJason M. Bills {
56695820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
567cd225da8SJason M. Bills     // First get the Timestamp
568cd225da8SJason M. Bills     size_t space = logEntry.find_first_of(" ");
569cd225da8SJason M. Bills     if (space == std::string::npos)
57095820184SJason M. Bills     {
57195820184SJason M. Bills         return 1;
57295820184SJason M. Bills     }
573cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
574cd225da8SJason M. Bills     // Then get the log contents
575cd225da8SJason M. Bills     size_t entryStart = logEntry.find_first_not_of(" ", space);
576cd225da8SJason M. Bills     if (entryStart == std::string::npos)
577cd225da8SJason M. Bills     {
578cd225da8SJason M. Bills         return 1;
579cd225da8SJason M. Bills     }
580cd225da8SJason M. Bills     std::string_view entry(logEntry);
581cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
582cd225da8SJason M. Bills     // Use split to separate the entry into its fields
583cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
584cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
585cd225da8SJason M. Bills                  boost::token_compress_on);
586cd225da8SJason M. Bills     // We need at least a MessageId to be valid
587cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
588cd225da8SJason M. Bills     {
589cd225da8SJason M. Bills         return 1;
590cd225da8SJason M. Bills     }
591cd225da8SJason M. Bills     std::string &messageID = logEntryFields[0];
59295820184SJason M. Bills 
5934851d45dSJason M. Bills     // Get the Message from the MessageRegistry
5944851d45dSJason M. Bills     const message_registries::Message *message =
5954851d45dSJason M. Bills         message_registries::getMessage(messageID);
596c4bf6374SJason M. Bills 
5974851d45dSJason M. Bills     std::string msg;
5984851d45dSJason M. Bills     std::string severity;
5994851d45dSJason M. Bills     if (message != nullptr)
600c4bf6374SJason M. Bills     {
6014851d45dSJason M. Bills         msg = message->message;
6024851d45dSJason M. Bills         severity = message->severity;
603c4bf6374SJason M. Bills     }
604c4bf6374SJason M. Bills 
60515a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
60615a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
60715a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
60815a86ff6SJason M. Bills     {
60915a86ff6SJason M. Bills         std::string &messageArgsStart = logEntryFields[1];
61015a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
61115a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
61215a86ff6SJason M. Bills         if (!messageArgsStart.empty())
61315a86ff6SJason M. Bills         {
61415a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
61515a86ff6SJason M. Bills         }
61615a86ff6SJason M. Bills 
61715a86ff6SJason M. Bills         messageArgs = boost::beast::span(&messageArgsStart, messageArgsSize);
618c4bf6374SJason M. Bills 
6194851d45dSJason M. Bills         // Fill the MessageArgs into the Message
62095820184SJason M. Bills         int i = 0;
62195820184SJason M. Bills         for (const std::string &messageArg : messageArgs)
6224851d45dSJason M. Bills         {
62395820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
6244851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
6254851d45dSJason M. Bills             if (argPos != std::string::npos)
6264851d45dSJason M. Bills             {
62795820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
6284851d45dSJason M. Bills             }
6294851d45dSJason M. Bills         }
63015a86ff6SJason M. Bills     }
6314851d45dSJason M. Bills 
63295820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
63395820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
63495820184SJason M. Bills     // between the '.' and the '+', so just remove them.
63595820184SJason M. Bills     std::size_t dot = timestamp.find_first_of(".");
63695820184SJason M. Bills     std::size_t plus = timestamp.find_first_of("+");
63795820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
638c4bf6374SJason M. Bills     {
63995820184SJason M. Bills         timestamp.erase(dot, plus - dot);
640c4bf6374SJason M. Bills     }
641c4bf6374SJason M. Bills 
642c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
64395820184SJason M. Bills     logEntryJson = {
644cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
645c4bf6374SJason M. Bills         {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
646029573d4SEd Tanous         {"@odata.id",
647897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
64895820184SJason M. Bills              logEntryID},
649c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
65095820184SJason M. Bills         {"Id", logEntryID},
65195820184SJason M. Bills         {"Message", std::move(msg)},
65295820184SJason M. Bills         {"MessageId", std::move(messageID)},
653c4bf6374SJason M. Bills         {"MessageArgs", std::move(messageArgs)},
654c4bf6374SJason M. Bills         {"EntryType", "Event"},
65595820184SJason M. Bills         {"Severity", std::move(severity)},
65695820184SJason M. Bills         {"Created", std::move(timestamp)}};
657c4bf6374SJason M. Bills     return 0;
658c4bf6374SJason M. Bills }
659c4bf6374SJason M. Bills 
66027062605SAnthony Wilson class JournalEventLogEntryCollection : public Node
661c4bf6374SJason M. Bills {
662c4bf6374SJason M. Bills   public:
663c4bf6374SJason M. Bills     template <typename CrowApp>
66427062605SAnthony Wilson     JournalEventLogEntryCollection(CrowApp &app) :
665029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
666c4bf6374SJason M. Bills     {
667c4bf6374SJason M. Bills         entityPrivileges = {
668c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
669c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
670c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
671c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
672c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
673c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
674c4bf6374SJason M. Bills     }
675c4bf6374SJason M. Bills 
676c4bf6374SJason M. Bills   private:
677c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
678c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
679c4bf6374SJason M. Bills     {
680c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
681271584abSEd Tanous         uint64_t skip = 0;
682271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
683c4bf6374SJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
684c4bf6374SJason M. Bills         {
685c4bf6374SJason M. Bills             return;
686c4bf6374SJason M. Bills         }
687c4bf6374SJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
688c4bf6374SJason M. Bills         {
689c4bf6374SJason M. Bills             return;
690c4bf6374SJason M. Bills         }
691c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
692c4bf6374SJason M. Bills         // it has a duplicate entry for members
693c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
694c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
695c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
696c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
697c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
698029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
699c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
700c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
701c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
702cb92c03bSAndrew Geissler 
703c4bf6374SJason M. Bills         nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
704c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
70595820184SJason M. Bills         // Go through the log files and create a unique ID for each entry
70695820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
70795820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
708b01bf299SEd Tanous         uint64_t entryCount = 0;
709cd225da8SJason M. Bills         std::string logEntry;
71095820184SJason M. Bills 
71195820184SJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
712cd225da8SJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
713cd225da8SJason M. Bills              it++)
714c4bf6374SJason M. Bills         {
715cd225da8SJason M. Bills             std::ifstream logStream(*it);
71695820184SJason M. Bills             if (!logStream.is_open())
717c4bf6374SJason M. Bills             {
718c4bf6374SJason M. Bills                 continue;
719c4bf6374SJason M. Bills             }
720c4bf6374SJason M. Bills 
721e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
722e85d6b16SJason M. Bills             bool firstEntry = true;
72395820184SJason M. Bills             while (std::getline(logStream, logEntry))
72495820184SJason M. Bills             {
725c4bf6374SJason M. Bills                 entryCount++;
726c4bf6374SJason M. Bills                 // Handle paging using skip (number of entries to skip from the
727c4bf6374SJason M. Bills                 // start) and top (number of entries to display)
728c4bf6374SJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
729c4bf6374SJason M. Bills                 {
730c4bf6374SJason M. Bills                     continue;
731c4bf6374SJason M. Bills                 }
732c4bf6374SJason M. Bills 
733c4bf6374SJason M. Bills                 std::string idStr;
734e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
735c4bf6374SJason M. Bills                 {
736c4bf6374SJason M. Bills                     continue;
737c4bf6374SJason M. Bills                 }
738c4bf6374SJason M. Bills 
739e85d6b16SJason M. Bills                 if (firstEntry)
740e85d6b16SJason M. Bills                 {
741e85d6b16SJason M. Bills                     firstEntry = false;
742e85d6b16SJason M. Bills                 }
743e85d6b16SJason M. Bills 
744c4bf6374SJason M. Bills                 logEntryArray.push_back({});
745c4bf6374SJason M. Bills                 nlohmann::json &bmcLogEntry = logEntryArray.back();
74695820184SJason M. Bills                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
747c4bf6374SJason M. Bills                 {
748c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
749c4bf6374SJason M. Bills                     return;
750c4bf6374SJason M. Bills                 }
751c4bf6374SJason M. Bills             }
75295820184SJason M. Bills         }
753c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
754c4bf6374SJason M. Bills         if (skip + top < entryCount)
755c4bf6374SJason M. Bills         {
756c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
75795820184SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/"
75895820184SJason M. Bills                 "Entries?$skip=" +
759c4bf6374SJason M. Bills                 std::to_string(skip + top);
760c4bf6374SJason M. Bills         }
76108a4e4b5SAnthony Wilson     }
76208a4e4b5SAnthony Wilson };
76308a4e4b5SAnthony Wilson 
764897967deSJason M. Bills class JournalEventLogEntry : public Node
765897967deSJason M. Bills {
766897967deSJason M. Bills   public:
767897967deSJason M. Bills     JournalEventLogEntry(CrowApp &app) :
768897967deSJason M. Bills         Node(app,
769897967deSJason M. Bills              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
770897967deSJason M. Bills              std::string())
771897967deSJason M. Bills     {
772897967deSJason M. Bills         entityPrivileges = {
773897967deSJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
774897967deSJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
775897967deSJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
776897967deSJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
777897967deSJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
778897967deSJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
779897967deSJason M. Bills     }
780897967deSJason M. Bills 
781897967deSJason M. Bills   private:
782897967deSJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
783897967deSJason M. Bills                const std::vector<std::string> &params) override
784897967deSJason M. Bills     {
785897967deSJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
786897967deSJason M. Bills         if (params.size() != 1)
787897967deSJason M. Bills         {
788897967deSJason M. Bills             messages::internalError(asyncResp->res);
789897967deSJason M. Bills             return;
790897967deSJason M. Bills         }
791897967deSJason M. Bills         const std::string &targetID = params[0];
792897967deSJason M. Bills 
793897967deSJason M. Bills         // Go through the log files and check the unique ID for each entry to
794897967deSJason M. Bills         // find the target entry
795897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
796897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
797897967deSJason M. Bills         std::string logEntry;
798897967deSJason M. Bills 
799897967deSJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
800897967deSJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
801897967deSJason M. Bills              it++)
802897967deSJason M. Bills         {
803897967deSJason M. Bills             std::ifstream logStream(*it);
804897967deSJason M. Bills             if (!logStream.is_open())
805897967deSJason M. Bills             {
806897967deSJason M. Bills                 continue;
807897967deSJason M. Bills             }
808897967deSJason M. Bills 
809897967deSJason M. Bills             // Reset the unique ID on the first entry
810897967deSJason M. Bills             bool firstEntry = true;
811897967deSJason M. Bills             while (std::getline(logStream, logEntry))
812897967deSJason M. Bills             {
813897967deSJason M. Bills                 std::string idStr;
814897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
815897967deSJason M. Bills                 {
816897967deSJason M. Bills                     continue;
817897967deSJason M. Bills                 }
818897967deSJason M. Bills 
819897967deSJason M. Bills                 if (firstEntry)
820897967deSJason M. Bills                 {
821897967deSJason M. Bills                     firstEntry = false;
822897967deSJason M. Bills                 }
823897967deSJason M. Bills 
824897967deSJason M. Bills                 if (idStr == targetID)
825897967deSJason M. Bills                 {
826897967deSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry,
827897967deSJason M. Bills                                               asyncResp->res.jsonValue) != 0)
828897967deSJason M. Bills                     {
829897967deSJason M. Bills                         messages::internalError(asyncResp->res);
830897967deSJason M. Bills                         return;
831897967deSJason M. Bills                     }
832897967deSJason M. Bills                     return;
833897967deSJason M. Bills                 }
834897967deSJason M. Bills             }
835897967deSJason M. Bills         }
836897967deSJason M. Bills         // Requested ID was not found
837897967deSJason M. Bills         messages::resourceMissingAtURI(asyncResp->res, targetID);
838897967deSJason M. Bills     }
839897967deSJason M. Bills };
840897967deSJason M. Bills 
84108a4e4b5SAnthony Wilson class DBusEventLogEntryCollection : public Node
84208a4e4b5SAnthony Wilson {
84308a4e4b5SAnthony Wilson   public:
84408a4e4b5SAnthony Wilson     template <typename CrowApp>
84508a4e4b5SAnthony Wilson     DBusEventLogEntryCollection(CrowApp &app) :
84608a4e4b5SAnthony Wilson         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
84708a4e4b5SAnthony Wilson     {
84808a4e4b5SAnthony Wilson         entityPrivileges = {
84908a4e4b5SAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
85008a4e4b5SAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
85108a4e4b5SAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
85208a4e4b5SAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
85308a4e4b5SAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
85408a4e4b5SAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
85508a4e4b5SAnthony Wilson     }
85608a4e4b5SAnthony Wilson 
85708a4e4b5SAnthony Wilson   private:
85808a4e4b5SAnthony Wilson     void doGet(crow::Response &res, const crow::Request &req,
85908a4e4b5SAnthony Wilson                const std::vector<std::string> &params) override
86008a4e4b5SAnthony Wilson     {
86108a4e4b5SAnthony Wilson         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
86208a4e4b5SAnthony Wilson 
86308a4e4b5SAnthony Wilson         // Collections don't include the static data added by SubRoute because
86408a4e4b5SAnthony Wilson         // it has a duplicate entry for members
86508a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
86608a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
86708a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.context"] =
86808a4e4b5SAnthony Wilson             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
86908a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
87008a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
87108a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
87208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
87308a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
87408a4e4b5SAnthony Wilson 
875cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
876cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
877cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
878cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
879cb92c03bSAndrew Geissler                         GetManagedObjectsType &resp) {
880cb92c03bSAndrew Geissler                 if (ec)
881cb92c03bSAndrew Geissler                 {
882cb92c03bSAndrew Geissler                     // TODO Handle for specific error code
883cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
884cb92c03bSAndrew Geissler                         << "getLogEntriesIfaceData resp_handler got error "
885cb92c03bSAndrew Geissler                         << ec;
886cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
887cb92c03bSAndrew Geissler                     return;
888cb92c03bSAndrew Geissler                 }
889cb92c03bSAndrew Geissler                 nlohmann::json &entriesArray =
890cb92c03bSAndrew Geissler                     asyncResp->res.jsonValue["Members"];
891cb92c03bSAndrew Geissler                 entriesArray = nlohmann::json::array();
892cb92c03bSAndrew Geissler                 for (auto &objectPath : resp)
893cb92c03bSAndrew Geissler                 {
894cb92c03bSAndrew Geissler                     for (auto &interfaceMap : objectPath.second)
895cb92c03bSAndrew Geissler                     {
896cb92c03bSAndrew Geissler                         if (interfaceMap.first !=
897cb92c03bSAndrew Geissler                             "xyz.openbmc_project.Logging.Entry")
898cb92c03bSAndrew Geissler                         {
899cb92c03bSAndrew Geissler                             BMCWEB_LOG_DEBUG << "Bailing early on "
900cb92c03bSAndrew Geissler                                              << interfaceMap.first;
901cb92c03bSAndrew Geissler                             continue;
902cb92c03bSAndrew Geissler                         }
903cb92c03bSAndrew Geissler                         entriesArray.push_back({});
904cb92c03bSAndrew Geissler                         nlohmann::json &thisEntry = entriesArray.back();
90566664f25SEd Tanous                         uint32_t *id = nullptr;
90666664f25SEd Tanous                         std::time_t timestamp{};
90766664f25SEd Tanous                         std::string *severity = nullptr;
90866664f25SEd Tanous                         std::string *message = nullptr;
909cb92c03bSAndrew Geissler                         for (auto &propertyMap : interfaceMap.second)
910cb92c03bSAndrew Geissler                         {
911cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
912cb92c03bSAndrew Geissler                             {
913cb92c03bSAndrew Geissler                                 id = sdbusplus::message::variant_ns::get_if<
914cb92c03bSAndrew Geissler                                     uint32_t>(&propertyMap.second);
915cb92c03bSAndrew Geissler                                 if (id == nullptr)
916cb92c03bSAndrew Geissler                                 {
917cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
918cb92c03bSAndrew Geissler                                                               "Id");
919cb92c03bSAndrew Geissler                                 }
920cb92c03bSAndrew Geissler                             }
921cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
922cb92c03bSAndrew Geissler                             {
923cb92c03bSAndrew Geissler                                 const uint64_t *millisTimeStamp =
924cb92c03bSAndrew Geissler                                     std::get_if<uint64_t>(&propertyMap.second);
925cb92c03bSAndrew Geissler                                 if (millisTimeStamp == nullptr)
926cb92c03bSAndrew Geissler                                 {
927cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
928cb92c03bSAndrew Geissler                                                               "Timestamp");
929271584abSEd Tanous                                     continue;
930cb92c03bSAndrew Geissler                                 }
931cb92c03bSAndrew Geissler                                 // Retrieve Created property with format:
932cb92c03bSAndrew Geissler                                 // yyyy-mm-ddThh:mm:ss
933cb92c03bSAndrew Geissler                                 std::chrono::milliseconds chronoTimeStamp(
934cb92c03bSAndrew Geissler                                     *millisTimeStamp);
935271584abSEd Tanous                                 timestamp = std::chrono::duration_cast<
936271584abSEd Tanous                                                 std::chrono::duration<int>>(
937271584abSEd Tanous                                                 chronoTimeStamp)
938cb92c03bSAndrew Geissler                                                 .count();
939cb92c03bSAndrew Geissler                             }
940cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
941cb92c03bSAndrew Geissler                             {
942cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
943cb92c03bSAndrew Geissler                                     &propertyMap.second);
944cb92c03bSAndrew Geissler                                 if (severity == nullptr)
945cb92c03bSAndrew Geissler                                 {
946cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
947cb92c03bSAndrew Geissler                                                               "Severity");
948cb92c03bSAndrew Geissler                                 }
949cb92c03bSAndrew Geissler                             }
950cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
951cb92c03bSAndrew Geissler                             {
952cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
953cb92c03bSAndrew Geissler                                     &propertyMap.second);
954cb92c03bSAndrew Geissler                                 if (message == nullptr)
955cb92c03bSAndrew Geissler                                 {
956cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
957cb92c03bSAndrew Geissler                                                               "Message");
958cb92c03bSAndrew Geissler                                 }
959cb92c03bSAndrew Geissler                             }
960cb92c03bSAndrew Geissler                         }
961cb92c03bSAndrew Geissler                         thisEntry = {
962cb92c03bSAndrew Geissler                             {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
963cb92c03bSAndrew Geissler                             {"@odata.context", "/redfish/v1/"
964cb92c03bSAndrew Geissler                                                "$metadata#LogEntry.LogEntry"},
965cb92c03bSAndrew Geissler                             {"@odata.id",
966cb92c03bSAndrew Geissler                              "/redfish/v1/Systems/system/LogServices/EventLog/"
967cb92c03bSAndrew Geissler                              "Entries/" +
968cb92c03bSAndrew Geissler                                  std::to_string(*id)},
96927062605SAnthony Wilson                             {"Name", "System Event Log Entry"},
970cb92c03bSAndrew Geissler                             {"Id", std::to_string(*id)},
971cb92c03bSAndrew Geissler                             {"Message", *message},
972cb92c03bSAndrew Geissler                             {"EntryType", "Event"},
973cb92c03bSAndrew Geissler                             {"Severity",
974cb92c03bSAndrew Geissler                              translateSeverityDbusToRedfish(*severity)},
975cb92c03bSAndrew Geissler                             {"Created", crow::utility::getDateTime(timestamp)}};
976cb92c03bSAndrew Geissler                     }
977cb92c03bSAndrew Geissler                 }
978cb92c03bSAndrew Geissler                 std::sort(entriesArray.begin(), entriesArray.end(),
979cb92c03bSAndrew Geissler                           [](const nlohmann::json &left,
980cb92c03bSAndrew Geissler                              const nlohmann::json &right) {
981cb92c03bSAndrew Geissler                               return (left["Id"] <= right["Id"]);
982cb92c03bSAndrew Geissler                           });
983cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
984cb92c03bSAndrew Geissler                     entriesArray.size();
985cb92c03bSAndrew Geissler             },
986cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
987cb92c03bSAndrew Geissler             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
988c4bf6374SJason M. Bills     }
989c4bf6374SJason M. Bills };
990c4bf6374SJason M. Bills 
99108a4e4b5SAnthony Wilson class DBusEventLogEntry : public Node
992c4bf6374SJason M. Bills {
993c4bf6374SJason M. Bills   public:
99408a4e4b5SAnthony Wilson     DBusEventLogEntry(CrowApp &app) :
995c4bf6374SJason M. Bills         Node(app,
996029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
997029573d4SEd Tanous              std::string())
998c4bf6374SJason M. Bills     {
999c4bf6374SJason M. Bills         entityPrivileges = {
1000c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1001c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1002c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1003c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1004c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1005c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1006c4bf6374SJason M. Bills     }
1007c4bf6374SJason M. Bills 
1008c4bf6374SJason M. Bills   private:
1009c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1010c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
1011c4bf6374SJason M. Bills     {
1012c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1013029573d4SEd Tanous         if (params.size() != 1)
1014c4bf6374SJason M. Bills         {
1015c4bf6374SJason M. Bills             messages::internalError(asyncResp->res);
1016c4bf6374SJason M. Bills             return;
1017c4bf6374SJason M. Bills         }
1018029573d4SEd Tanous         const std::string &entryID = params[0];
1019cb92c03bSAndrew Geissler 
1020cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1021cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1022cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1023cb92c03bSAndrew Geissler             [asyncResp, entryID](const boost::system::error_code ec,
1024cb92c03bSAndrew Geissler                                  GetManagedPropertyType &resp) {
1025cb92c03bSAndrew Geissler                 if (ec)
1026cb92c03bSAndrew Geissler                 {
1027cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
1028cb92c03bSAndrew Geissler                         << "EventLogEntry (DBus) resp_handler got error " << ec;
1029cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
1030cb92c03bSAndrew Geissler                     return;
1031cb92c03bSAndrew Geissler                 }
103266664f25SEd Tanous                 uint32_t *id = nullptr;
103366664f25SEd Tanous                 std::time_t timestamp{};
103466664f25SEd Tanous                 std::string *severity = nullptr;
103566664f25SEd Tanous                 std::string *message = nullptr;
1036cb92c03bSAndrew Geissler                 for (auto &propertyMap : resp)
1037cb92c03bSAndrew Geissler                 {
1038cb92c03bSAndrew Geissler                     if (propertyMap.first == "Id")
1039cb92c03bSAndrew Geissler                     {
1040cb92c03bSAndrew Geissler                         id = std::get_if<uint32_t>(&propertyMap.second);
1041cb92c03bSAndrew Geissler                         if (id == nullptr)
1042cb92c03bSAndrew Geissler                         {
1043cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res, "Id");
1044cb92c03bSAndrew Geissler                         }
1045cb92c03bSAndrew Geissler                     }
1046cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Timestamp")
1047cb92c03bSAndrew Geissler                     {
1048cb92c03bSAndrew Geissler                         const uint64_t *millisTimeStamp =
1049cb92c03bSAndrew Geissler                             std::get_if<uint64_t>(&propertyMap.second);
1050cb92c03bSAndrew Geissler                         if (millisTimeStamp == nullptr)
1051cb92c03bSAndrew Geissler                         {
1052cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1053cb92c03bSAndrew Geissler                                                       "Timestamp");
1054271584abSEd Tanous                             continue;
1055cb92c03bSAndrew Geissler                         }
1056cb92c03bSAndrew Geissler                         // Retrieve Created property with format:
1057cb92c03bSAndrew Geissler                         // yyyy-mm-ddThh:mm:ss
1058cb92c03bSAndrew Geissler                         std::chrono::milliseconds chronoTimeStamp(
1059cb92c03bSAndrew Geissler                             *millisTimeStamp);
1060cb92c03bSAndrew Geissler                         timestamp =
1061271584abSEd Tanous                             std::chrono::duration_cast<
1062271584abSEd Tanous                                 std::chrono::duration<int>>(chronoTimeStamp)
1063cb92c03bSAndrew Geissler                                 .count();
1064cb92c03bSAndrew Geissler                     }
1065cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Severity")
1066cb92c03bSAndrew Geissler                     {
1067cb92c03bSAndrew Geissler                         severity =
1068cb92c03bSAndrew Geissler                             std::get_if<std::string>(&propertyMap.second);
1069cb92c03bSAndrew Geissler                         if (severity == nullptr)
1070cb92c03bSAndrew Geissler                         {
1071cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1072cb92c03bSAndrew Geissler                                                       "Severity");
1073cb92c03bSAndrew Geissler                         }
1074cb92c03bSAndrew Geissler                     }
1075cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Message")
1076cb92c03bSAndrew Geissler                     {
1077cb92c03bSAndrew Geissler                         message = std::get_if<std::string>(&propertyMap.second);
1078cb92c03bSAndrew Geissler                         if (message == nullptr)
1079cb92c03bSAndrew Geissler                         {
1080cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1081cb92c03bSAndrew Geissler                                                       "Message");
1082cb92c03bSAndrew Geissler                         }
1083cb92c03bSAndrew Geissler                     }
1084cb92c03bSAndrew Geissler                 }
1085271584abSEd Tanous                 if (id == nullptr || message == nullptr || severity == nullptr)
1086271584abSEd Tanous                 {
1087271584abSEd Tanous                     return;
1088271584abSEd Tanous                 }
1089cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue = {
1090cb92c03bSAndrew Geissler                     {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1091cb92c03bSAndrew Geissler                     {"@odata.context", "/redfish/v1/"
1092cb92c03bSAndrew Geissler                                        "$metadata#LogEntry.LogEntry"},
1093cb92c03bSAndrew Geissler                     {"@odata.id",
1094cb92c03bSAndrew Geissler                      "/redfish/v1/Systems/system/LogServices/EventLog/"
1095cb92c03bSAndrew Geissler                      "Entries/" +
1096cb92c03bSAndrew Geissler                          std::to_string(*id)},
109727062605SAnthony Wilson                     {"Name", "System Event Log Entry"},
1098cb92c03bSAndrew Geissler                     {"Id", std::to_string(*id)},
1099cb92c03bSAndrew Geissler                     {"Message", *message},
1100cb92c03bSAndrew Geissler                     {"EntryType", "Event"},
1101cb92c03bSAndrew Geissler                     {"Severity", translateSeverityDbusToRedfish(*severity)},
110208a4e4b5SAnthony Wilson                     {"Created", crow::utility::getDateTime(timestamp)}};
1103cb92c03bSAndrew Geissler             },
1104cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1105cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1106cb92c03bSAndrew Geissler             "org.freedesktop.DBus.Properties", "GetAll",
1107cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging.Entry");
1108c4bf6374SJason M. Bills     }
1109336e96c6SChicago Duan 
1110336e96c6SChicago Duan     void doDelete(crow::Response &res, const crow::Request &req,
1111336e96c6SChicago Duan                   const std::vector<std::string> &params) override
1112336e96c6SChicago Duan     {
1113336e96c6SChicago Duan 
1114336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1115336e96c6SChicago Duan 
1116336e96c6SChicago Duan         auto asyncResp = std::make_shared<AsyncResp>(res);
1117336e96c6SChicago Duan 
1118336e96c6SChicago Duan         if (params.size() != 1)
1119336e96c6SChicago Duan         {
1120336e96c6SChicago Duan             messages::internalError(asyncResp->res);
1121336e96c6SChicago Duan             return;
1122336e96c6SChicago Duan         }
1123336e96c6SChicago Duan         std::string entryID = params[0];
1124336e96c6SChicago Duan 
1125336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1126336e96c6SChicago Duan 
1127336e96c6SChicago Duan         // Process response from Logging service.
1128336e96c6SChicago Duan         auto respHandler = [asyncResp](const boost::system::error_code ec) {
1129336e96c6SChicago Duan             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1130336e96c6SChicago Duan             if (ec)
1131336e96c6SChicago Duan             {
1132336e96c6SChicago Duan                 // TODO Handle for specific error code
1133336e96c6SChicago Duan                 BMCWEB_LOG_ERROR
1134336e96c6SChicago Duan                     << "EventLogEntry (DBus) doDelete respHandler got error "
1135336e96c6SChicago Duan                     << ec;
1136336e96c6SChicago Duan                 asyncResp->res.result(
1137336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1138336e96c6SChicago Duan                 return;
1139336e96c6SChicago Duan             }
1140336e96c6SChicago Duan 
1141336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1142336e96c6SChicago Duan         };
1143336e96c6SChicago Duan 
1144336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1145336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1146336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1147336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1148336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
1149336e96c6SChicago Duan     }
1150c4bf6374SJason M. Bills };
1151c4bf6374SJason M. Bills 
1152c4bf6374SJason M. Bills class BMCLogServiceCollection : public Node
1153c4bf6374SJason M. Bills {
1154c4bf6374SJason M. Bills   public:
1155c4bf6374SJason M. Bills     template <typename CrowApp>
1156c4bf6374SJason M. Bills     BMCLogServiceCollection(CrowApp &app) :
11574ed77cd5SEd Tanous         Node(app, "/redfish/v1/Managers/bmc/LogServices/")
11581da66f75SEd Tanous     {
11591da66f75SEd Tanous         entityPrivileges = {
1160e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1161e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1162e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1163e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1164e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1165e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
11661da66f75SEd Tanous     }
11671da66f75SEd Tanous 
11681da66f75SEd Tanous   private:
11691da66f75SEd Tanous     /**
11701da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
11711da66f75SEd Tanous      */
11721da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
11731da66f75SEd Tanous                const std::vector<std::string> &params) override
11741da66f75SEd Tanous     {
1175e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
11761da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
11771da66f75SEd Tanous         // it has a duplicate entry for members
1178e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
11791da66f75SEd Tanous             "#LogServiceCollection.LogServiceCollection";
1180e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1181c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection";
1182e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1183e1f26343SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices";
1184e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1185e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
11861da66f75SEd Tanous             "Collection of LogServices for this Manager";
1187c4bf6374SJason M. Bills         nlohmann::json &logServiceArray = asyncResp->res.jsonValue["Members"];
1188c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
1189c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1190c4bf6374SJason M. Bills         logServiceArray.push_back(
119108a4e4b5SAnthony Wilson             {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1192c4bf6374SJason M. Bills #endif
1193e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1194c4bf6374SJason M. Bills             logServiceArray.size();
11951da66f75SEd Tanous     }
11961da66f75SEd Tanous };
11971da66f75SEd Tanous 
1198c4bf6374SJason M. Bills class BMCJournalLogService : public Node
11991da66f75SEd Tanous {
12001da66f75SEd Tanous   public:
12011da66f75SEd Tanous     template <typename CrowApp>
1202c4bf6374SJason M. Bills     BMCJournalLogService(CrowApp &app) :
1203c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1204e1f26343SJason M. Bills     {
1205e1f26343SJason M. Bills         entityPrivileges = {
1206e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1207e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1208e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1209e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1210e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1211e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1212e1f26343SJason M. Bills     }
1213e1f26343SJason M. Bills 
1214e1f26343SJason M. Bills   private:
1215e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1216e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1217e1f26343SJason M. Bills     {
1218e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1219e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1220e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
12210f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
12220f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
1223e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1224e1f26343SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
1225c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
1226c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
1227c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
1228e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1229cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1230cd50aa42SJason M. Bills             {"@odata.id",
1231086be238SEd Tanous              "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1232e1f26343SJason M. Bills     }
1233e1f26343SJason M. Bills };
1234e1f26343SJason M. Bills 
1235c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string &bmcJournalLogEntryID,
1236e1f26343SJason M. Bills                                       sd_journal *journal,
1237c4bf6374SJason M. Bills                                       nlohmann::json &bmcJournalLogEntryJson)
1238e1f26343SJason M. Bills {
1239e1f26343SJason M. Bills     // Get the Log Entry contents
1240e1f26343SJason M. Bills     int ret = 0;
1241e1f26343SJason M. Bills 
124239e77504SEd Tanous     std::string_view msg;
124316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1244e1f26343SJason M. Bills     if (ret < 0)
1245e1f26343SJason M. Bills     {
1246e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1247e1f26343SJason M. Bills         return 1;
1248e1f26343SJason M. Bills     }
1249e1f26343SJason M. Bills 
1250e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1251271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
125216428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1253e1f26343SJason M. Bills     if (ret < 0)
1254e1f26343SJason M. Bills     {
1255e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1256e1f26343SJason M. Bills     }
1257e1f26343SJason M. Bills 
1258e1f26343SJason M. Bills     // Get the Created time from the timestamp
125916428a1aSJason M. Bills     std::string entryTimeStr;
126016428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
1261e1f26343SJason M. Bills     {
126216428a1aSJason M. Bills         return 1;
1263e1f26343SJason M. Bills     }
1264e1f26343SJason M. Bills 
1265e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
1266c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
1267cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1268e1f26343SJason M. Bills         {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
1269c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1270c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
1271e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
1272c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
127316428a1aSJason M. Bills         {"Message", msg},
1274e1f26343SJason M. Bills         {"EntryType", "Oem"},
1275e1f26343SJason M. Bills         {"Severity",
1276b6a61a5eSJason M. Bills          severity <= 2 ? "Critical" : severity <= 4 ? "Warning" : "OK"},
1277086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
1278e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
1279e1f26343SJason M. Bills     return 0;
1280e1f26343SJason M. Bills }
1281e1f26343SJason M. Bills 
1282c4bf6374SJason M. Bills class BMCJournalLogEntryCollection : public Node
1283e1f26343SJason M. Bills {
1284e1f26343SJason M. Bills   public:
1285e1f26343SJason M. Bills     template <typename CrowApp>
1286c4bf6374SJason M. Bills     BMCJournalLogEntryCollection(CrowApp &app) :
1287c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
1288e1f26343SJason M. Bills     {
1289e1f26343SJason M. Bills         entityPrivileges = {
1290e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1291e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1292e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1293e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1294e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1295e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1296e1f26343SJason M. Bills     }
1297e1f26343SJason M. Bills 
1298e1f26343SJason M. Bills   private:
1299e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1300e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1301e1f26343SJason M. Bills     {
1302e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1303193ad2faSJason M. Bills         static constexpr const long maxEntriesPerPage = 1000;
1304271584abSEd Tanous         uint64_t skip = 0;
1305271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
130616428a1aSJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
1307193ad2faSJason M. Bills         {
1308193ad2faSJason M. Bills             return;
1309193ad2faSJason M. Bills         }
131016428a1aSJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
1311193ad2faSJason M. Bills         {
1312193ad2faSJason M. Bills             return;
1313193ad2faSJason M. Bills         }
1314e1f26343SJason M. Bills         // Collections don't include the static data added by SubRoute because
1315e1f26343SJason M. Bills         // it has a duplicate entry for members
1316e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1317e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
13180f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
13190f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1320e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1321c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
1322e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1323c4bf6374SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1324e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1325e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1326e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
13270f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
13280f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/BmcLog/Entries";
1329e1f26343SJason M. Bills         nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
1330e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
1331e1f26343SJason M. Bills 
1332e1f26343SJason M. Bills         // Go through the journal and use the timestamp to create a unique ID
1333e1f26343SJason M. Bills         // for each entry
1334e1f26343SJason M. Bills         sd_journal *journalTmp = nullptr;
1335e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1336e1f26343SJason M. Bills         if (ret < 0)
1337e1f26343SJason M. Bills         {
1338e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1339f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1340e1f26343SJason M. Bills             return;
1341e1f26343SJason M. Bills         }
1342e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1343e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1344e1f26343SJason M. Bills         journalTmp = nullptr;
1345b01bf299SEd Tanous         uint64_t entryCount = 0;
1346e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
1347e85d6b16SJason M. Bills         bool firstEntry = true;
1348e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
1349e1f26343SJason M. Bills         {
1350193ad2faSJason M. Bills             entryCount++;
1351193ad2faSJason M. Bills             // Handle paging using skip (number of entries to skip from the
1352193ad2faSJason M. Bills             // start) and top (number of entries to display)
1353193ad2faSJason M. Bills             if (entryCount <= skip || entryCount > skip + top)
1354193ad2faSJason M. Bills             {
1355193ad2faSJason M. Bills                 continue;
1356193ad2faSJason M. Bills             }
1357193ad2faSJason M. Bills 
135816428a1aSJason M. Bills             std::string idStr;
1359e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1360e1f26343SJason M. Bills             {
1361e1f26343SJason M. Bills                 continue;
1362e1f26343SJason M. Bills             }
1363e1f26343SJason M. Bills 
1364e85d6b16SJason M. Bills             if (firstEntry)
1365e85d6b16SJason M. Bills             {
1366e85d6b16SJason M. Bills                 firstEntry = false;
1367e85d6b16SJason M. Bills             }
1368e85d6b16SJason M. Bills 
1369e1f26343SJason M. Bills             logEntryArray.push_back({});
1370c4bf6374SJason M. Bills             nlohmann::json &bmcJournalLogEntry = logEntryArray.back();
1371c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
1372c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
1373e1f26343SJason M. Bills             {
1374f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1375e1f26343SJason M. Bills                 return;
1376e1f26343SJason M. Bills             }
1377e1f26343SJason M. Bills         }
1378193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1379193ad2faSJason M. Bills         if (skip + top < entryCount)
1380193ad2faSJason M. Bills         {
1381193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
1382c4bf6374SJason M. Bills                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
1383193ad2faSJason M. Bills                 std::to_string(skip + top);
1384193ad2faSJason M. Bills         }
1385e1f26343SJason M. Bills     }
1386e1f26343SJason M. Bills };
1387e1f26343SJason M. Bills 
1388c4bf6374SJason M. Bills class BMCJournalLogEntry : public Node
1389e1f26343SJason M. Bills {
1390e1f26343SJason M. Bills   public:
1391c4bf6374SJason M. Bills     BMCJournalLogEntry(CrowApp &app) :
1392c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/",
1393e1f26343SJason M. Bills              std::string())
1394e1f26343SJason M. Bills     {
1395e1f26343SJason M. Bills         entityPrivileges = {
1396e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1397e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1398e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1399e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1400e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1401e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1402e1f26343SJason M. Bills     }
1403e1f26343SJason M. Bills 
1404e1f26343SJason M. Bills   private:
1405e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1406e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1407e1f26343SJason M. Bills     {
1408e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1409e1f26343SJason M. Bills         if (params.size() != 1)
1410e1f26343SJason M. Bills         {
1411f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1412e1f26343SJason M. Bills             return;
1413e1f26343SJason M. Bills         }
141416428a1aSJason M. Bills         const std::string &entryID = params[0];
1415e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
1416e1f26343SJason M. Bills         uint64_t ts = 0;
1417271584abSEd Tanous         uint64_t index = 0;
141816428a1aSJason M. Bills         if (!getTimestampFromID(asyncResp->res, entryID, ts, index))
1419e1f26343SJason M. Bills         {
142016428a1aSJason M. Bills             return;
1421e1f26343SJason M. Bills         }
1422e1f26343SJason M. Bills 
1423e1f26343SJason M. Bills         sd_journal *journalTmp = nullptr;
1424e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1425e1f26343SJason M. Bills         if (ret < 0)
1426e1f26343SJason M. Bills         {
1427e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1428f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1429e1f26343SJason M. Bills             return;
1430e1f26343SJason M. Bills         }
1431e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1432e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1433e1f26343SJason M. Bills         journalTmp = nullptr;
1434e1f26343SJason M. Bills         // Go to the timestamp in the log and move to the entry at the index
1435af07e3f5SJason M. Bills         // tracking the unique ID
1436af07e3f5SJason M. Bills         std::string idStr;
1437af07e3f5SJason M. Bills         bool firstEntry = true;
1438e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
1439271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
1440e1f26343SJason M. Bills         {
1441e1f26343SJason M. Bills             sd_journal_next(journal.get());
1442af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1443af07e3f5SJason M. Bills             {
1444af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
1445af07e3f5SJason M. Bills                 return;
1446af07e3f5SJason M. Bills             }
1447af07e3f5SJason M. Bills             if (firstEntry)
1448af07e3f5SJason M. Bills             {
1449af07e3f5SJason M. Bills                 firstEntry = false;
1450af07e3f5SJason M. Bills             }
1451e1f26343SJason M. Bills         }
1452c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
1453af07e3f5SJason M. Bills         if (idStr != entryID)
1454c4bf6374SJason M. Bills         {
1455c4bf6374SJason M. Bills             messages::resourceMissingAtURI(asyncResp->res, entryID);
1456c4bf6374SJason M. Bills             return;
1457c4bf6374SJason M. Bills         }
1458c4bf6374SJason M. Bills 
1459c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
1460e1f26343SJason M. Bills                                        asyncResp->res.jsonValue) != 0)
1461e1f26343SJason M. Bills         {
1462f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1463e1f26343SJason M. Bills             return;
1464e1f26343SJason M. Bills         }
1465e1f26343SJason M. Bills     }
1466e1f26343SJason M. Bills };
1467e1f26343SJason M. Bills 
1468424c4176SJason M. Bills class CrashdumpService : public Node
1469e1f26343SJason M. Bills {
1470e1f26343SJason M. Bills   public:
1471e1f26343SJason M. Bills     template <typename CrowApp>
1472424c4176SJason M. Bills     CrashdumpService(CrowApp &app) :
1473424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
14741da66f75SEd Tanous     {
14751da66f75SEd Tanous         entityPrivileges = {
1476e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1477e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1478e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1479e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1480e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1481e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
14821da66f75SEd Tanous     }
14831da66f75SEd Tanous 
14841da66f75SEd Tanous   private:
14851da66f75SEd Tanous     /**
14861da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
14871da66f75SEd Tanous      */
14881da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14891da66f75SEd Tanous                const std::vector<std::string> &params) override
14901da66f75SEd Tanous     {
1491e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14921da66f75SEd Tanous         // Copy over the static data to include the entries added by SubRoute
14930f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1494424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
1495e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1496e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
1497e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1498c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
1499424c4176SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Service";
1500424c4176SJason M. Bills         asyncResp->res.jsonValue["Description"] = "Crashdump Service";
1501424c4176SJason M. Bills         asyncResp->res.jsonValue["Id"] = "Crashdump";
1502e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1503e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
1504cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1505cd50aa42SJason M. Bills             {"@odata.id",
1506424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
1507e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"] = {
15081da66f75SEd Tanous             {"Oem",
1509424c4176SJason M. Bills              {{"#Crashdump.OnDemand",
1510424c4176SJason M. Bills                {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
1511424c4176SJason M. Bills                            "Actions/Oem/Crashdump.OnDemand"}}}}}};
15121da66f75SEd Tanous 
15131da66f75SEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_RAW_PECI
1514e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"]["Oem"].push_back(
1515424c4176SJason M. Bills             {"#Crashdump.SendRawPeci",
151608a4e4b5SAnthony Wilson              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
1517424c4176SJason M. Bills                          "Actions/Oem/Crashdump.SendRawPeci"}}});
15181da66f75SEd Tanous #endif
15191da66f75SEd Tanous     }
15201da66f75SEd Tanous };
15211da66f75SEd Tanous 
1522*e855dd28SJason M. Bills std::string getLogCreatedTime(const std::string &crashdump)
1523*e855dd28SJason M. Bills {
1524*e855dd28SJason M. Bills     nlohmann::json crashdumpJson =
1525*e855dd28SJason M. Bills         nlohmann::json::parse(crashdump, nullptr, false);
1526*e855dd28SJason M. Bills     if (crashdumpJson.is_discarded())
1527*e855dd28SJason M. Bills     {
1528*e855dd28SJason M. Bills         return std::string();
1529*e855dd28SJason M. Bills     }
1530*e855dd28SJason M. Bills 
1531*e855dd28SJason M. Bills     nlohmann::json::const_iterator cdIt = crashdumpJson.find("crash_data");
1532*e855dd28SJason M. Bills     if (cdIt == crashdumpJson.end())
1533*e855dd28SJason M. Bills     {
1534*e855dd28SJason M. Bills         return std::string();
1535*e855dd28SJason M. Bills     }
1536*e855dd28SJason M. Bills 
1537*e855dd28SJason M. Bills     nlohmann::json::const_iterator siIt = cdIt->find("METADATA");
1538*e855dd28SJason M. Bills     if (siIt == cdIt->end())
1539*e855dd28SJason M. Bills     {
1540*e855dd28SJason M. Bills         return std::string();
1541*e855dd28SJason M. Bills     }
1542*e855dd28SJason M. Bills 
1543*e855dd28SJason M. Bills     nlohmann::json::const_iterator tsIt = siIt->find("timestamp");
1544*e855dd28SJason M. Bills     if (tsIt == siIt->end())
1545*e855dd28SJason M. Bills     {
1546*e855dd28SJason M. Bills         return std::string();
1547*e855dd28SJason M. Bills     }
1548*e855dd28SJason M. Bills 
1549*e855dd28SJason M. Bills     const std::string *logTime = tsIt->get_ptr<const std::string *>();
1550*e855dd28SJason M. Bills     if (logTime == nullptr)
1551*e855dd28SJason M. Bills     {
1552*e855dd28SJason M. Bills         return std::string();
1553*e855dd28SJason M. Bills     }
1554*e855dd28SJason M. Bills 
1555*e855dd28SJason M. Bills     std::string redfishDateTime = *logTime;
1556*e855dd28SJason M. Bills     if (redfishDateTime.length() > 2)
1557*e855dd28SJason M. Bills     {
1558*e855dd28SJason M. Bills         redfishDateTime.insert(redfishDateTime.end() - 2, ':');
1559*e855dd28SJason M. Bills     }
1560*e855dd28SJason M. Bills 
1561*e855dd28SJason M. Bills     return redfishDateTime;
1562*e855dd28SJason M. Bills }
1563*e855dd28SJason M. Bills 
1564*e855dd28SJason M. Bills std::string getLogFileName(const std::string &logTime)
1565*e855dd28SJason M. Bills {
1566*e855dd28SJason M. Bills     // Set the crashdump file name to "crashdump_<logTime>.json" using the
1567*e855dd28SJason M. Bills     // created time without the timezone info
1568*e855dd28SJason M. Bills     std::string fileTime = logTime;
1569*e855dd28SJason M. Bills     size_t plusPos = fileTime.rfind('+');
1570*e855dd28SJason M. Bills     if (plusPos != std::string::npos)
1571*e855dd28SJason M. Bills     {
1572*e855dd28SJason M. Bills         fileTime.erase(plusPos);
1573*e855dd28SJason M. Bills     }
1574*e855dd28SJason M. Bills     return "crashdump_" + fileTime + ".json";
1575*e855dd28SJason M. Bills }
1576*e855dd28SJason M. Bills 
1577*e855dd28SJason M. Bills static void logCrashdumpEntry(std::shared_ptr<AsyncResp> asyncResp,
1578*e855dd28SJason M. Bills                               const std::string &logID,
1579*e855dd28SJason M. Bills                               nlohmann::json &logEntryJson)
1580*e855dd28SJason M. Bills {
1581*e855dd28SJason M. Bills     auto getStoredLogCallback = [asyncResp, logID, &logEntryJson](
1582*e855dd28SJason M. Bills                                     const boost::system::error_code ec,
1583*e855dd28SJason M. Bills                                     const std::variant<std::string> &resp) {
1584*e855dd28SJason M. Bills         if (ec)
1585*e855dd28SJason M. Bills         {
1586*e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
1587*e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
1588*e855dd28SJason M. Bills             return;
1589*e855dd28SJason M. Bills         }
1590*e855dd28SJason M. Bills         const std::string *log = std::get_if<std::string>(&resp);
1591*e855dd28SJason M. Bills         if (log == nullptr)
1592*e855dd28SJason M. Bills         {
1593*e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
1594*e855dd28SJason M. Bills             return;
1595*e855dd28SJason M. Bills         }
1596*e855dd28SJason M. Bills         std::string logTime = getLogCreatedTime(*log);
1597*e855dd28SJason M. Bills         std::string fileName = getLogFileName(logTime);
1598*e855dd28SJason M. Bills 
1599*e855dd28SJason M. Bills         logEntryJson = {
1600*e855dd28SJason M. Bills             {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1601*e855dd28SJason M. Bills             {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
1602*e855dd28SJason M. Bills             {"@odata.id",
1603*e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
1604*e855dd28SJason M. Bills                  logID},
1605*e855dd28SJason M. Bills             {"Name", "CPU Crashdump"},
1606*e855dd28SJason M. Bills             {"Id", logID},
1607*e855dd28SJason M. Bills             {"EntryType", "Oem"},
1608*e855dd28SJason M. Bills             {"OemRecordFormat", "Crashdump URI"},
1609*e855dd28SJason M. Bills             {"Message",
1610*e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
1611*e855dd28SJason M. Bills                  logID + "/" + fileName},
1612*e855dd28SJason M. Bills             {"Created", std::move(logTime)}};
1613*e855dd28SJason M. Bills     };
1614*e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
1615*e855dd28SJason M. Bills         std::move(getStoredLogCallback), CrashdumpObject,
1616*e855dd28SJason M. Bills         CrashdumpPath + std::string("/") + logID,
1617*e855dd28SJason M. Bills         "org.freedesktop.DBus.Properties", "Get", CrashdumpInterface, "Log");
1618*e855dd28SJason M. Bills }
1619*e855dd28SJason M. Bills 
1620424c4176SJason M. Bills class CrashdumpEntryCollection : public Node
16211da66f75SEd Tanous {
16221da66f75SEd Tanous   public:
16231da66f75SEd Tanous     template <typename CrowApp>
1624424c4176SJason M. Bills     CrashdumpEntryCollection(CrowApp &app) :
1625424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
16261da66f75SEd Tanous     {
16271da66f75SEd Tanous         entityPrivileges = {
1628e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1629e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1630e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1631e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1632e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1633e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
16341da66f75SEd Tanous     }
16351da66f75SEd Tanous 
16361da66f75SEd Tanous   private:
16371da66f75SEd Tanous     /**
16381da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
16391da66f75SEd Tanous      */
16401da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16411da66f75SEd Tanous                const std::vector<std::string> &params) override
16421da66f75SEd Tanous     {
1643e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16441da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
16451da66f75SEd Tanous         // it has a duplicate entry for members
1646e1f26343SJason M. Bills         auto getLogEntriesCallback = [asyncResp](
1647e1f26343SJason M. Bills                                          const boost::system::error_code ec,
16481da66f75SEd Tanous                                          const std::vector<std::string> &resp) {
16491da66f75SEd Tanous             if (ec)
16501da66f75SEd Tanous             {
16511da66f75SEd Tanous                 if (ec.value() !=
16521da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
16531da66f75SEd Tanous                 {
16541da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
16551da66f75SEd Tanous                                      << ec.message();
1656f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
16571da66f75SEd Tanous                     return;
16581da66f75SEd Tanous                 }
16591da66f75SEd Tanous             }
1660e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
16611da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
16620f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1663424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
1664e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.context"] =
1665*e855dd28SJason M. Bills                 "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
1666424c4176SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
1667e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
1668424c4176SJason M. Bills                 "Collection of Crashdump Entries";
1669e1f26343SJason M. Bills             nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
1670e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
1671*e855dd28SJason M. Bills             std::vector<std::string> logIDs;
1672*e855dd28SJason M. Bills             // Get the list of log entries and build up an empty array big
1673*e855dd28SJason M. Bills             // enough to hold them
16741da66f75SEd Tanous             for (const std::string &objpath : resp)
16751da66f75SEd Tanous             {
1676*e855dd28SJason M. Bills                 // Ignore the on-demand log
1677424c4176SJason M. Bills                 if (objpath.compare(CrashdumpOnDemandPath) == 0)
16781da66f75SEd Tanous                 {
16791da66f75SEd Tanous                     continue;
16801da66f75SEd Tanous                 }
1681*e855dd28SJason M. Bills 
1682*e855dd28SJason M. Bills                 // Get the log ID
16834ed77cd5SEd Tanous                 std::size_t lastPos = objpath.rfind("/");
1684*e855dd28SJason M. Bills                 if (lastPos == std::string::npos)
16851da66f75SEd Tanous                 {
1686*e855dd28SJason M. Bills                     continue;
16871da66f75SEd Tanous                 }
1688*e855dd28SJason M. Bills                 logIDs.emplace_back(objpath.substr(lastPos + 1));
1689*e855dd28SJason M. Bills 
1690*e855dd28SJason M. Bills                 // Add a space for the log entry to the array
1691*e855dd28SJason M. Bills                 logEntryArray.push_back({});
1692*e855dd28SJason M. Bills             }
1693*e855dd28SJason M. Bills             // Now go through and set up async calls to fill in the entries
1694*e855dd28SJason M. Bills             size_t index = 0;
1695*e855dd28SJason M. Bills             for (const std::string &logID : logIDs)
1696*e855dd28SJason M. Bills             {
1697*e855dd28SJason M. Bills                 // Add the log entry to the array
1698*e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
16991da66f75SEd Tanous             }
1700e1f26343SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
1701e1f26343SJason M. Bills                 logEntryArray.size();
17021da66f75SEd Tanous         };
17031da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
17041da66f75SEd Tanous             std::move(getLogEntriesCallback),
17051da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
17061da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
17071da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
1708424c4176SJason M. Bills             std::array<const char *, 1>{CrashdumpInterface});
17091da66f75SEd Tanous     }
17101da66f75SEd Tanous };
17111da66f75SEd Tanous 
1712424c4176SJason M. Bills class CrashdumpEntry : public Node
17131da66f75SEd Tanous {
17141da66f75SEd Tanous   public:
1715424c4176SJason M. Bills     CrashdumpEntry(CrowApp &app) :
1716d53dd41fSJason M. Bills         Node(app,
1717424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/",
17181da66f75SEd Tanous              std::string())
17191da66f75SEd Tanous     {
17201da66f75SEd Tanous         entityPrivileges = {
1721e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1722e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1723e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1724e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1725e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1726e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
17271da66f75SEd Tanous     }
17281da66f75SEd Tanous 
17291da66f75SEd Tanous   private:
17301da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
17311da66f75SEd Tanous                const std::vector<std::string> &params) override
17321da66f75SEd Tanous     {
1733e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17341da66f75SEd Tanous         if (params.size() != 1)
17351da66f75SEd Tanous         {
1736f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
17371da66f75SEd Tanous             return;
17381da66f75SEd Tanous         }
1739*e855dd28SJason M. Bills         const std::string &logID = params[0];
1740*e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
1741*e855dd28SJason M. Bills     }
1742*e855dd28SJason M. Bills };
1743*e855dd28SJason M. Bills 
1744*e855dd28SJason M. Bills class CrashdumpFile : public Node
1745*e855dd28SJason M. Bills {
1746*e855dd28SJason M. Bills   public:
1747*e855dd28SJason M. Bills     CrashdumpFile(CrowApp &app) :
1748*e855dd28SJason M. Bills         Node(app,
1749*e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/"
1750*e855dd28SJason M. Bills              "<str>/",
1751*e855dd28SJason M. Bills              std::string(), std::string())
1752*e855dd28SJason M. Bills     {
1753*e855dd28SJason M. Bills         entityPrivileges = {
1754*e855dd28SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1755*e855dd28SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1756*e855dd28SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1757*e855dd28SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1758*e855dd28SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1759*e855dd28SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1760*e855dd28SJason M. Bills     }
1761*e855dd28SJason M. Bills 
1762*e855dd28SJason M. Bills   private:
1763*e855dd28SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1764*e855dd28SJason M. Bills                const std::vector<std::string> &params) override
1765*e855dd28SJason M. Bills     {
1766*e855dd28SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1767*e855dd28SJason M. Bills         if (params.size() != 2)
1768*e855dd28SJason M. Bills         {
1769*e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
1770*e855dd28SJason M. Bills             return;
1771*e855dd28SJason M. Bills         }
1772*e855dd28SJason M. Bills         const std::string &logID = params[0];
1773*e855dd28SJason M. Bills         const std::string &fileName = params[1];
1774*e855dd28SJason M. Bills 
1775*e855dd28SJason M. Bills         auto getStoredLogCallback = [asyncResp, logID, fileName](
1776abf2add6SEd Tanous                                         const boost::system::error_code ec,
1777abf2add6SEd Tanous                                         const std::variant<std::string> &resp) {
17781da66f75SEd Tanous             if (ec)
17791da66f75SEd Tanous             {
1780abf2add6SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
1781f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17821da66f75SEd Tanous                 return;
17831da66f75SEd Tanous             }
1784abf2add6SEd Tanous             const std::string *log = std::get_if<std::string>(&resp);
17851da66f75SEd Tanous             if (log == nullptr)
17861da66f75SEd Tanous             {
1787f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17881da66f75SEd Tanous                 return;
17891da66f75SEd Tanous             }
1790*e855dd28SJason M. Bills 
1791*e855dd28SJason M. Bills             // Verify the file name parameter is correct
1792*e855dd28SJason M. Bills             if (fileName != getLogFileName(getLogCreatedTime(*log)))
17931da66f75SEd Tanous             {
1794*e855dd28SJason M. Bills                 messages::resourceMissingAtURI(asyncResp->res, fileName);
17951da66f75SEd Tanous                 return;
17961da66f75SEd Tanous             }
1797*e855dd28SJason M. Bills 
1798*e855dd28SJason M. Bills             // Configure this to be a file download when accessed from a browser
1799*e855dd28SJason M. Bills             asyncResp->res.addHeader("Content-Disposition", "attachment");
1800*e855dd28SJason M. Bills             asyncResp->res.body() = *log;
18011da66f75SEd Tanous         };
18021da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1803424c4176SJason M. Bills             std::move(getStoredLogCallback), CrashdumpObject,
1804*e855dd28SJason M. Bills             CrashdumpPath + std::string("/") + logID,
1805424c4176SJason M. Bills             "org.freedesktop.DBus.Properties", "Get", CrashdumpInterface,
1806424c4176SJason M. Bills             "Log");
18071da66f75SEd Tanous     }
18081da66f75SEd Tanous };
18091da66f75SEd Tanous 
1810424c4176SJason M. Bills class OnDemandCrashdump : public Node
18111da66f75SEd Tanous {
18121da66f75SEd Tanous   public:
1813424c4176SJason M. Bills     OnDemandCrashdump(CrowApp &app) :
1814424c4176SJason M. Bills         Node(app,
1815424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
1816424c4176SJason M. Bills              "Crashdump.OnDemand/")
18171da66f75SEd Tanous     {
18181da66f75SEd Tanous         entityPrivileges = {
1819e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1820e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1821e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1822e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1823e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1824e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
18251da66f75SEd Tanous     }
18261da66f75SEd Tanous 
18271da66f75SEd Tanous   private:
18281da66f75SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
18291da66f75SEd Tanous                 const std::vector<std::string> &params) override
18301da66f75SEd Tanous     {
1831e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
183248e4639eSJason M. Bills         static std::unique_ptr<sdbusplus::bus::match::match> onDemandLogMatcher;
18331da66f75SEd Tanous 
183448e4639eSJason M. Bills         // Only allow one OnDemand Log request at a time
183548e4639eSJason M. Bills         if (onDemandLogMatcher != nullptr)
18361da66f75SEd Tanous         {
1837e1f26343SJason M. Bills             asyncResp->res.addHeader("Retry-After", "30");
1838f12894f8SJason M. Bills             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
18391da66f75SEd Tanous             return;
18401da66f75SEd Tanous         }
18411da66f75SEd Tanous         // Make this static so it survives outside this method
1842271584abSEd Tanous         static boost::asio::steady_timer timeout(*req.ioService);
18431da66f75SEd Tanous 
1844271584abSEd Tanous         timeout.expires_after(std::chrono::seconds(30));
1845e1f26343SJason M. Bills         timeout.async_wait([asyncResp](const boost::system::error_code &ec) {
184648e4639eSJason M. Bills             onDemandLogMatcher = nullptr;
18471da66f75SEd Tanous             if (ec)
18481da66f75SEd Tanous             {
18491da66f75SEd Tanous                 // operation_aborted is expected if timer is canceled before
18501da66f75SEd Tanous                 // completion.
18511da66f75SEd Tanous                 if (ec != boost::asio::error::operation_aborted)
18521da66f75SEd Tanous                 {
18531da66f75SEd Tanous                     BMCWEB_LOG_ERROR << "Async_wait failed " << ec;
18541da66f75SEd Tanous                 }
18551da66f75SEd Tanous                 return;
18561da66f75SEd Tanous             }
185748e4639eSJason M. Bills             BMCWEB_LOG_ERROR << "Timed out waiting for on-demand log";
18581da66f75SEd Tanous 
1859f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
18601da66f75SEd Tanous         });
18611da66f75SEd Tanous 
18623cf8ea3cSJason M. Bills         auto onDemandLogMatcherCallback =
18633cf8ea3cSJason M. Bills             [asyncResp](sdbusplus::message::message &m) {
186448e4639eSJason M. Bills                 BMCWEB_LOG_DEBUG << "OnDemand log available match fired";
1865271584abSEd Tanous                 timeout.cancel();
1866271584abSEd Tanous 
18674ed77cd5SEd Tanous                 sdbusplus::message::object_path objPath;
18681da66f75SEd Tanous                 boost::container::flat_map<
1869abf2add6SEd Tanous                     std::string, boost::container::flat_map<
1870abf2add6SEd Tanous                                      std::string, std::variant<std::string>>>
18714ed77cd5SEd Tanous                     interfacesAdded;
18724ed77cd5SEd Tanous                 m.read(objPath, interfacesAdded);
1873abf2add6SEd Tanous                 const std::string *log = std::get_if<std::string>(
1874424c4176SJason M. Bills                     &interfacesAdded[CrashdumpInterface]["Log"]);
18751da66f75SEd Tanous                 if (log == nullptr)
18761da66f75SEd Tanous                 {
1877f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18783cf8ea3cSJason M. Bills                     // Careful with onDemandLogMatcher.  It is a unique_ptr to
18793cf8ea3cSJason M. Bills                     // the match object inside which this lambda is executing.
18803cf8ea3cSJason M. Bills                     // Once it is set to nullptr, the match object will be
18813cf8ea3cSJason M. Bills                     // destroyed and the lambda will lose its context, including
18823cf8ea3cSJason M. Bills                     // res, so it needs to be the last thing done.
188348e4639eSJason M. Bills                     onDemandLogMatcher = nullptr;
18841da66f75SEd Tanous                     return;
18851da66f75SEd Tanous                 }
18863cf8ea3cSJason M. Bills                 nlohmann::json crashdumpJson =
18873cf8ea3cSJason M. Bills                     nlohmann::json::parse(*log, nullptr, false);
18883cf8ea3cSJason M. Bills                 if (crashdumpJson.is_discarded())
18891da66f75SEd Tanous                 {
1890f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18913cf8ea3cSJason M. Bills                     // Careful with onDemandLogMatcher.  It is a unique_ptr to
18923cf8ea3cSJason M. Bills                     // the match object inside which this lambda is executing.
18933cf8ea3cSJason M. Bills                     // Once it is set to nullptr, the match object will be
18943cf8ea3cSJason M. Bills                     // destroyed and the lambda will lose its context, including
18953cf8ea3cSJason M. Bills                     // res, so it needs to be the last thing done.
18963cf8ea3cSJason M. Bills                     onDemandLogMatcher = nullptr;
18973cf8ea3cSJason M. Bills                     return;
18983cf8ea3cSJason M. Bills                 }
18993cf8ea3cSJason M. Bills                 asyncResp->res.jsonValue = crashdumpJson;
190048e4639eSJason M. Bills                 // Careful with onDemandLogMatcher.  It is a unique_ptr to the
19011da66f75SEd Tanous                 // match object inside which this lambda is executing.  Once it
19021da66f75SEd Tanous                 // is set to nullptr, the match object will be destroyed and the
19031da66f75SEd Tanous                 // lambda will lose its context, including res, so it needs to
19041da66f75SEd Tanous                 // be the last thing done.
190548e4639eSJason M. Bills                 onDemandLogMatcher = nullptr;
19061da66f75SEd Tanous             };
190748e4639eSJason M. Bills         onDemandLogMatcher = std::make_unique<sdbusplus::bus::match::match>(
19081da66f75SEd Tanous             *crow::connections::systemBus,
19091da66f75SEd Tanous             sdbusplus::bus::match::rules::interfacesAdded() +
1910424c4176SJason M. Bills                 sdbusplus::bus::match::rules::argNpath(0,
1911424c4176SJason M. Bills                                                        CrashdumpOnDemandPath),
191248e4639eSJason M. Bills             std::move(onDemandLogMatcherCallback));
19131da66f75SEd Tanous 
191448e4639eSJason M. Bills         auto generateonDemandLogCallback =
1915e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
19161da66f75SEd Tanous                         const std::string &resp) {
19171da66f75SEd Tanous                 if (ec)
19181da66f75SEd Tanous                 {
19191da66f75SEd Tanous                     if (ec.value() ==
19201da66f75SEd Tanous                         boost::system::errc::operation_not_supported)
19211da66f75SEd Tanous                     {
1922f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
19231da66f75SEd Tanous                     }
19241da66f75SEd Tanous                     else
19251da66f75SEd Tanous                     {
1926f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
19271da66f75SEd Tanous                     }
1928271584abSEd Tanous 
1929271584abSEd Tanous                     timeout.cancel();
193048e4639eSJason M. Bills                     onDemandLogMatcher = nullptr;
19311da66f75SEd Tanous                     return;
19321da66f75SEd Tanous                 }
19331da66f75SEd Tanous             };
19341da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1935424c4176SJason M. Bills             std::move(generateonDemandLogCallback), CrashdumpObject,
1936424c4176SJason M. Bills             CrashdumpPath, CrashdumpOnDemandInterface, "GenerateOnDemandLog");
19371da66f75SEd Tanous     }
19381da66f75SEd Tanous };
19391da66f75SEd Tanous 
1940e1f26343SJason M. Bills class SendRawPECI : public Node
19411da66f75SEd Tanous {
19421da66f75SEd Tanous   public:
1943e1f26343SJason M. Bills     SendRawPECI(CrowApp &app) :
1944424c4176SJason M. Bills         Node(app,
1945424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
1946424c4176SJason M. Bills              "Crashdump.SendRawPeci/")
19471da66f75SEd Tanous     {
19481da66f75SEd Tanous         entityPrivileges = {
19491da66f75SEd Tanous             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
19501da66f75SEd Tanous             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
19511da66f75SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
19521da66f75SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
19531da66f75SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
19541da66f75SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
19551da66f75SEd Tanous     }
19561da66f75SEd Tanous 
19571da66f75SEd Tanous   private:
19581da66f75SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
19591da66f75SEd Tanous                 const std::vector<std::string> &params) override
19601da66f75SEd Tanous     {
1961e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1962b1556427SEd Tanous         uint8_t clientAddress = 0;
1963b1556427SEd Tanous         uint8_t readLength = 0;
19641da66f75SEd Tanous         std::vector<uint8_t> peciCommand;
1965b1556427SEd Tanous         if (!json_util::readJson(req, res, "ClientAddress", clientAddress,
1966b1556427SEd Tanous                                  "ReadLength", readLength, "PECICommand",
1967b1556427SEd Tanous                                  peciCommand))
19681da66f75SEd Tanous         {
19691da66f75SEd Tanous             return;
19701da66f75SEd Tanous         }
1971b1556427SEd Tanous 
19721da66f75SEd Tanous         // Callback to return the Raw PECI response
1973e1f26343SJason M. Bills         auto sendRawPECICallback =
1974e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
19751da66f75SEd Tanous                         const std::vector<uint8_t> &resp) {
19761da66f75SEd Tanous                 if (ec)
19771da66f75SEd Tanous                 {
19781da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to send PECI command ec: "
19791da66f75SEd Tanous                                      << ec.message();
1980f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
19811da66f75SEd Tanous                     return;
19821da66f75SEd Tanous                 }
1983e1f26343SJason M. Bills                 asyncResp->res.jsonValue = {{"Name", "PECI Command Response"},
19841da66f75SEd Tanous                                             {"PECIResponse", resp}};
19851da66f75SEd Tanous             };
19861da66f75SEd Tanous         // Call the SendRawPECI command with the provided data
19871da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1988424c4176SJason M. Bills             std::move(sendRawPECICallback), CrashdumpObject, CrashdumpPath,
1989424c4176SJason M. Bills             CrashdumpRawPECIInterface, "SendRawPeci", clientAddress, readLength,
19904ed77cd5SEd Tanous             peciCommand);
19911da66f75SEd Tanous     }
19921da66f75SEd Tanous };
19931da66f75SEd Tanous 
1994cb92c03bSAndrew Geissler /**
1995cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
1996cb92c03bSAndrew Geissler  */
1997cb92c03bSAndrew Geissler class DBusLogServiceActionsClear : public Node
1998cb92c03bSAndrew Geissler {
1999cb92c03bSAndrew Geissler   public:
2000cb92c03bSAndrew Geissler     DBusLogServiceActionsClear(CrowApp &app) :
2001cb92c03bSAndrew Geissler         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
20021f56a3a6STim Lee                   "LogService.ClearLog")
2003cb92c03bSAndrew Geissler     {
2004cb92c03bSAndrew Geissler         entityPrivileges = {
2005cb92c03bSAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
2006cb92c03bSAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
2007cb92c03bSAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2008cb92c03bSAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2009cb92c03bSAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2010cb92c03bSAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2011cb92c03bSAndrew Geissler     }
2012cb92c03bSAndrew Geissler 
2013cb92c03bSAndrew Geissler   private:
2014cb92c03bSAndrew Geissler     /**
2015cb92c03bSAndrew Geissler      * Function handles POST method request.
2016cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
2017cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
2018cb92c03bSAndrew Geissler      */
2019cb92c03bSAndrew Geissler     void doPost(crow::Response &res, const crow::Request &req,
2020cb92c03bSAndrew Geissler                 const std::vector<std::string> &params) override
2021cb92c03bSAndrew Geissler     {
2022cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
2023cb92c03bSAndrew Geissler 
2024cb92c03bSAndrew Geissler         auto asyncResp = std::make_shared<AsyncResp>(res);
2025cb92c03bSAndrew Geissler         // Process response from Logging service.
2026cb92c03bSAndrew Geissler         auto resp_handler = [asyncResp](const boost::system::error_code ec) {
2027cb92c03bSAndrew Geissler             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
2028cb92c03bSAndrew Geissler             if (ec)
2029cb92c03bSAndrew Geissler             {
2030cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
2031cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
2032cb92c03bSAndrew Geissler                 asyncResp->res.result(
2033cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
2034cb92c03bSAndrew Geissler                 return;
2035cb92c03bSAndrew Geissler             }
2036cb92c03bSAndrew Geissler 
2037cb92c03bSAndrew Geissler             asyncResp->res.result(boost::beast::http::status::no_content);
2038cb92c03bSAndrew Geissler         };
2039cb92c03bSAndrew Geissler 
2040cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
2041cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
2042cb92c03bSAndrew Geissler             resp_handler, "xyz.openbmc_project.Logging",
2043cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
2044cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2045cb92c03bSAndrew Geissler     }
2046cb92c03bSAndrew Geissler };
20471da66f75SEd Tanous } // namespace redfish
2048