xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 99131cd04bfc4a187e7a8d63803cb8911e64a08e)
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 
365b61b5e8SJason M. Bills constexpr char const *crashdumpObject = "com.intel.crashdump";
375b61b5e8SJason M. Bills constexpr char const *crashdumpPath = "/com/intel/crashdump";
385b61b5e8SJason M. Bills constexpr char const *crashdumpOnDemandPath = "/com/intel/crashdump/OnDemand";
395b61b5e8SJason M. Bills constexpr char const *crashdumpInterface = "com.intel.crashdump";
405b61b5e8SJason M. Bills constexpr char const *deleteAllInterface =
415b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
425b61b5e8SJason M. Bills constexpr char const *crashdumpOnDemandInterface =
43424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
445b61b5e8SJason M. Bills constexpr char const *crashdumpRawPECIInterface =
45424c4176SJason M. Bills     "com.intel.crashdump.SendRawPeci";
461da66f75SEd Tanous 
474851d45dSJason M. Bills namespace message_registries
484851d45dSJason M. Bills {
494851d45dSJason M. Bills static const Message *getMessageFromRegistry(
504851d45dSJason M. Bills     const std::string &messageKey,
514851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
524851d45dSJason M. Bills {
534851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
544851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
554851d45dSJason M. Bills                      [&messageKey](const MessageEntry &messageEntry) {
564851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
574851d45dSJason M. Bills                                              messageKey.c_str());
584851d45dSJason M. Bills                      });
594851d45dSJason M. Bills     if (messageIt != registry.cend())
604851d45dSJason M. Bills     {
614851d45dSJason M. Bills         return &messageIt->second;
624851d45dSJason M. Bills     }
634851d45dSJason M. Bills 
644851d45dSJason M. Bills     return nullptr;
654851d45dSJason M. Bills }
664851d45dSJason M. Bills 
674851d45dSJason M. Bills static const Message *getMessage(const std::string_view &messageID)
684851d45dSJason M. Bills {
694851d45dSJason M. Bills     // Redfish MessageIds are in the form
704851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
714851d45dSJason M. Bills     // the right Message
724851d45dSJason M. Bills     std::vector<std::string> fields;
734851d45dSJason M. Bills     fields.reserve(4);
744851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
754851d45dSJason M. Bills     std::string &registryName = fields[0];
764851d45dSJason M. Bills     std::string &messageKey = fields[3];
774851d45dSJason M. Bills 
784851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
794851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
804851d45dSJason M. Bills     {
814851d45dSJason M. Bills         return getMessageFromRegistry(
824851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
834851d45dSJason M. Bills     }
844851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
854851d45dSJason M. Bills     {
864851d45dSJason M. Bills         return getMessageFromRegistry(
874851d45dSJason M. Bills             messageKey,
884851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
894851d45dSJason M. Bills     }
904851d45dSJason M. Bills     return nullptr;
914851d45dSJason M. Bills }
924851d45dSJason M. Bills } // namespace message_registries
934851d45dSJason M. Bills 
94f6150403SJames Feist namespace fs = std::filesystem;
951da66f75SEd Tanous 
96cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
97cb92c03bSAndrew Geissler     std::string,
98cb92c03bSAndrew Geissler     sdbusplus::message::variant<std::string, bool, uint8_t, int16_t, uint16_t,
99cb92c03bSAndrew Geissler                                 int32_t, uint32_t, int64_t, uint64_t, double>>;
100cb92c03bSAndrew Geissler 
101cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
102cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
103cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
104cb92c03bSAndrew Geissler 
105cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string &s)
106cb92c03bSAndrew Geissler {
107cb92c03bSAndrew Geissler     if (s == "xyz.openbmc_project.Logging.Entry.Level.Alert")
108cb92c03bSAndrew Geissler     {
109cb92c03bSAndrew Geissler         return "Critical";
110cb92c03bSAndrew Geissler     }
111cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Critical")
112cb92c03bSAndrew Geissler     {
113cb92c03bSAndrew Geissler         return "Critical";
114cb92c03bSAndrew Geissler     }
115cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Debug")
116cb92c03bSAndrew Geissler     {
117cb92c03bSAndrew Geissler         return "OK";
118cb92c03bSAndrew Geissler     }
119cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency")
120cb92c03bSAndrew Geissler     {
121cb92c03bSAndrew Geissler         return "Critical";
122cb92c03bSAndrew Geissler     }
123cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Error")
124cb92c03bSAndrew Geissler     {
125cb92c03bSAndrew Geissler         return "Critical";
126cb92c03bSAndrew Geissler     }
127cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Informational")
128cb92c03bSAndrew Geissler     {
129cb92c03bSAndrew Geissler         return "OK";
130cb92c03bSAndrew Geissler     }
131cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Notice")
132cb92c03bSAndrew Geissler     {
133cb92c03bSAndrew Geissler         return "OK";
134cb92c03bSAndrew Geissler     }
135cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
136cb92c03bSAndrew Geissler     {
137cb92c03bSAndrew Geissler         return "Warning";
138cb92c03bSAndrew Geissler     }
139cb92c03bSAndrew Geissler     return "";
140cb92c03bSAndrew Geissler }
141cb92c03bSAndrew Geissler 
14216428a1aSJason M. Bills static int getJournalMetadata(sd_journal *journal,
14339e77504SEd Tanous                               const std::string_view &field,
14439e77504SEd Tanous                               std::string_view &contents)
14516428a1aSJason M. Bills {
14616428a1aSJason M. Bills     const char *data = nullptr;
14716428a1aSJason M. Bills     size_t length = 0;
14816428a1aSJason M. Bills     int ret = 0;
14916428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
150271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
151271584abSEd Tanous                               reinterpret_cast<const void **>(&data), &length);
15216428a1aSJason M. Bills     if (ret < 0)
15316428a1aSJason M. Bills     {
15416428a1aSJason M. Bills         return ret;
15516428a1aSJason M. Bills     }
15639e77504SEd Tanous     contents = std::string_view(data, length);
15716428a1aSJason M. Bills     // Only use the content after the "=" character.
15816428a1aSJason M. Bills     contents.remove_prefix(std::min(contents.find("=") + 1, contents.size()));
15916428a1aSJason M. Bills     return ret;
16016428a1aSJason M. Bills }
16116428a1aSJason M. Bills 
16216428a1aSJason M. Bills static int getJournalMetadata(sd_journal *journal,
16339e77504SEd Tanous                               const std::string_view &field, const int &base,
164271584abSEd Tanous                               long int &contents)
16516428a1aSJason M. Bills {
16616428a1aSJason M. Bills     int ret = 0;
16739e77504SEd Tanous     std::string_view metadata;
16816428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16916428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
17016428a1aSJason M. Bills     if (ret < 0)
17116428a1aSJason M. Bills     {
17216428a1aSJason M. Bills         return ret;
17316428a1aSJason M. Bills     }
174b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
17516428a1aSJason M. Bills     return ret;
17616428a1aSJason M. Bills }
17716428a1aSJason M. Bills 
17816428a1aSJason M. Bills static bool getEntryTimestamp(sd_journal *journal, std::string &entryTimestamp)
17916428a1aSJason M. Bills {
18016428a1aSJason M. Bills     int ret = 0;
18116428a1aSJason M. Bills     uint64_t timestamp = 0;
18216428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &timestamp);
18316428a1aSJason M. Bills     if (ret < 0)
18416428a1aSJason M. Bills     {
18516428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
18616428a1aSJason M. Bills                          << strerror(-ret);
18716428a1aSJason M. Bills         return false;
18816428a1aSJason M. Bills     }
18916428a1aSJason M. Bills     time_t t =
19016428a1aSJason M. Bills         static_cast<time_t>(timestamp / 1000 / 1000); // Convert from us to s
19116428a1aSJason M. Bills     struct tm *loctime = localtime(&t);
19216428a1aSJason M. Bills     char entryTime[64] = {};
193*99131cd0SEd Tanous     if (nullptr != loctime)
19416428a1aSJason M. Bills     {
19516428a1aSJason M. Bills         strftime(entryTime, sizeof(entryTime), "%FT%T%z", loctime);
19616428a1aSJason M. Bills     }
19716428a1aSJason M. Bills     // Insert the ':' into the timezone
19839e77504SEd Tanous     std::string_view t1(entryTime);
19939e77504SEd Tanous     std::string_view t2(entryTime);
20016428a1aSJason M. Bills     if (t1.size() > 2 && t2.size() > 2)
20116428a1aSJason M. Bills     {
20216428a1aSJason M. Bills         t1.remove_suffix(2);
20316428a1aSJason M. Bills         t2.remove_prefix(t2.size() - 2);
20416428a1aSJason M. Bills     }
20539e77504SEd Tanous     entryTimestamp = std::string(t1) + ":" + std::string(t2);
20616428a1aSJason M. Bills     return true;
20716428a1aSJason M. Bills }
20816428a1aSJason M. Bills 
20916428a1aSJason M. Bills static bool getSkipParam(crow::Response &res, const crow::Request &req,
210271584abSEd Tanous                          uint64_t &skip)
21116428a1aSJason M. Bills {
21216428a1aSJason M. Bills     char *skipParam = req.urlParams.get("$skip");
21316428a1aSJason M. Bills     if (skipParam != nullptr)
21416428a1aSJason M. Bills     {
21516428a1aSJason M. Bills         char *ptr = nullptr;
216271584abSEd Tanous         skip = std::strtoul(skipParam, &ptr, 10);
21716428a1aSJason M. Bills         if (*skipParam == '\0' || *ptr != '\0')
21816428a1aSJason M. Bills         {
21916428a1aSJason M. Bills 
22016428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(skipParam),
22116428a1aSJason M. Bills                                                    "$skip");
22216428a1aSJason M. Bills             return false;
22316428a1aSJason M. Bills         }
22416428a1aSJason M. Bills     }
22516428a1aSJason M. Bills     return true;
22616428a1aSJason M. Bills }
22716428a1aSJason M. Bills 
228271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
22916428a1aSJason M. Bills static bool getTopParam(crow::Response &res, const crow::Request &req,
230271584abSEd Tanous                         uint64_t &top)
23116428a1aSJason M. Bills {
23216428a1aSJason M. Bills     char *topParam = req.urlParams.get("$top");
23316428a1aSJason M. Bills     if (topParam != nullptr)
23416428a1aSJason M. Bills     {
23516428a1aSJason M. Bills         char *ptr = nullptr;
236271584abSEd Tanous         top = std::strtoul(topParam, &ptr, 10);
23716428a1aSJason M. Bills         if (*topParam == '\0' || *ptr != '\0')
23816428a1aSJason M. Bills         {
23916428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(topParam),
24016428a1aSJason M. Bills                                                    "$top");
24116428a1aSJason M. Bills             return false;
24216428a1aSJason M. Bills         }
243271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
24416428a1aSJason M. Bills         {
24516428a1aSJason M. Bills 
24616428a1aSJason M. Bills             messages::queryParameterOutOfRange(
24716428a1aSJason M. Bills                 res, std::to_string(top), "$top",
24816428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
24916428a1aSJason M. Bills             return false;
25016428a1aSJason M. Bills         }
25116428a1aSJason M. Bills     }
25216428a1aSJason M. Bills     return true;
25316428a1aSJason M. Bills }
25416428a1aSJason M. Bills 
255e85d6b16SJason M. Bills static bool getUniqueEntryID(sd_journal *journal, std::string &entryID,
256e85d6b16SJason M. Bills                              const bool firstEntry = true)
25716428a1aSJason M. Bills {
25816428a1aSJason M. Bills     int ret = 0;
25916428a1aSJason M. Bills     static uint64_t prevTs = 0;
26016428a1aSJason M. Bills     static int index = 0;
261e85d6b16SJason M. Bills     if (firstEntry)
262e85d6b16SJason M. Bills     {
263e85d6b16SJason M. Bills         prevTs = 0;
264e85d6b16SJason M. Bills     }
265e85d6b16SJason M. Bills 
26616428a1aSJason M. Bills     // Get the entry timestamp
26716428a1aSJason M. Bills     uint64_t curTs = 0;
26816428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
26916428a1aSJason M. Bills     if (ret < 0)
27016428a1aSJason M. Bills     {
27116428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
27216428a1aSJason M. Bills                          << strerror(-ret);
27316428a1aSJason M. Bills         return false;
27416428a1aSJason M. Bills     }
27516428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
27616428a1aSJason M. Bills     if (curTs == prevTs)
27716428a1aSJason M. Bills     {
27816428a1aSJason M. Bills         index++;
27916428a1aSJason M. Bills     }
28016428a1aSJason M. Bills     else
28116428a1aSJason M. Bills     {
28216428a1aSJason M. Bills         // Otherwise, reset it
28316428a1aSJason M. Bills         index = 0;
28416428a1aSJason M. Bills     }
28516428a1aSJason M. Bills     // Save the timestamp
28616428a1aSJason M. Bills     prevTs = curTs;
28716428a1aSJason M. Bills 
28816428a1aSJason M. Bills     entryID = std::to_string(curTs);
28916428a1aSJason M. Bills     if (index > 0)
29016428a1aSJason M. Bills     {
29116428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
29216428a1aSJason M. Bills     }
29316428a1aSJason M. Bills     return true;
29416428a1aSJason M. Bills }
29516428a1aSJason M. Bills 
296e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string &logEntry, std::string &entryID,
297e85d6b16SJason M. Bills                              const bool firstEntry = true)
29895820184SJason M. Bills {
299271584abSEd Tanous     static time_t prevTs = 0;
30095820184SJason M. Bills     static int index = 0;
301e85d6b16SJason M. Bills     if (firstEntry)
302e85d6b16SJason M. Bills     {
303e85d6b16SJason M. Bills         prevTs = 0;
304e85d6b16SJason M. Bills     }
305e85d6b16SJason M. Bills 
30695820184SJason M. Bills     // Get the entry timestamp
307271584abSEd Tanous     std::time_t curTs = 0;
30895820184SJason M. Bills     std::tm timeStruct = {};
30995820184SJason M. Bills     std::istringstream entryStream(logEntry);
31095820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
31195820184SJason M. Bills     {
31295820184SJason M. Bills         curTs = std::mktime(&timeStruct);
31395820184SJason M. Bills     }
31495820184SJason M. Bills     // If the timestamp isn't unique, increment the index
31595820184SJason M. Bills     if (curTs == prevTs)
31695820184SJason M. Bills     {
31795820184SJason M. Bills         index++;
31895820184SJason M. Bills     }
31995820184SJason M. Bills     else
32095820184SJason M. Bills     {
32195820184SJason M. Bills         // Otherwise, reset it
32295820184SJason M. Bills         index = 0;
32395820184SJason M. Bills     }
32495820184SJason M. Bills     // Save the timestamp
32595820184SJason M. Bills     prevTs = curTs;
32695820184SJason M. Bills 
32795820184SJason M. Bills     entryID = std::to_string(curTs);
32895820184SJason M. Bills     if (index > 0)
32995820184SJason M. Bills     {
33095820184SJason M. Bills         entryID += "_" + std::to_string(index);
33195820184SJason M. Bills     }
33295820184SJason M. Bills     return true;
33395820184SJason M. Bills }
33495820184SJason M. Bills 
33516428a1aSJason M. Bills static bool getTimestampFromID(crow::Response &res, const std::string &entryID,
336271584abSEd Tanous                                uint64_t &timestamp, uint64_t &index)
33716428a1aSJason M. Bills {
33816428a1aSJason M. Bills     if (entryID.empty())
33916428a1aSJason M. Bills     {
34016428a1aSJason M. Bills         return false;
34116428a1aSJason M. Bills     }
34216428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
34339e77504SEd Tanous     std::string_view tsStr(entryID);
34416428a1aSJason M. Bills 
34516428a1aSJason M. Bills     auto underscorePos = tsStr.find("_");
34616428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
34716428a1aSJason M. Bills     {
34816428a1aSJason M. Bills         // Timestamp has an index
34916428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
35039e77504SEd Tanous         std::string_view indexStr(entryID);
35116428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
35216428a1aSJason M. Bills         std::size_t pos;
35316428a1aSJason M. Bills         try
35416428a1aSJason M. Bills         {
35539e77504SEd Tanous             index = std::stoul(std::string(indexStr), &pos);
35616428a1aSJason M. Bills         }
357271584abSEd Tanous         catch (std::invalid_argument &)
35816428a1aSJason M. Bills         {
35916428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
36016428a1aSJason M. Bills             return false;
36116428a1aSJason M. Bills         }
362271584abSEd Tanous         catch (std::out_of_range &)
36316428a1aSJason M. Bills         {
36416428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
36516428a1aSJason M. Bills             return false;
36616428a1aSJason M. Bills         }
36716428a1aSJason M. Bills         if (pos != indexStr.size())
36816428a1aSJason M. Bills         {
36916428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
37016428a1aSJason M. Bills             return false;
37116428a1aSJason M. Bills         }
37216428a1aSJason M. Bills     }
37316428a1aSJason M. Bills     // Timestamp has no index
37416428a1aSJason M. Bills     std::size_t pos;
37516428a1aSJason M. Bills     try
37616428a1aSJason M. Bills     {
37739e77504SEd Tanous         timestamp = std::stoull(std::string(tsStr), &pos);
37816428a1aSJason M. Bills     }
379271584abSEd Tanous     catch (std::invalid_argument &)
38016428a1aSJason M. Bills     {
38116428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
38216428a1aSJason M. Bills         return false;
38316428a1aSJason M. Bills     }
384271584abSEd Tanous     catch (std::out_of_range &)
38516428a1aSJason M. Bills     {
38616428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
38716428a1aSJason M. Bills         return false;
38816428a1aSJason M. Bills     }
38916428a1aSJason M. Bills     if (pos != tsStr.size())
39016428a1aSJason M. Bills     {
39116428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
39216428a1aSJason M. Bills         return false;
39316428a1aSJason M. Bills     }
39416428a1aSJason M. Bills     return true;
39516428a1aSJason M. Bills }
39616428a1aSJason M. Bills 
39795820184SJason M. Bills static bool
39895820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path> &redfishLogFiles)
39995820184SJason M. Bills {
40095820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
40195820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
40295820184SJason M. Bills 
40395820184SJason M. Bills     // Loop through the directory looking for redfish log files
40495820184SJason M. Bills     for (const std::filesystem::directory_entry &dirEnt :
40595820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
40695820184SJason M. Bills     {
40795820184SJason M. Bills         // If we find a redfish log file, save the path
40895820184SJason M. Bills         std::string filename = dirEnt.path().filename();
40995820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
41095820184SJason M. Bills         {
41195820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
41295820184SJason M. Bills         }
41395820184SJason M. Bills     }
41495820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
41595820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
41695820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
41795820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
41895820184SJason M. Bills 
41995820184SJason M. Bills     return !redfishLogFiles.empty();
42095820184SJason M. Bills }
42195820184SJason M. Bills 
422c4bf6374SJason M. Bills class SystemLogServiceCollection : public Node
4231da66f75SEd Tanous {
4241da66f75SEd Tanous   public:
4251da66f75SEd Tanous     template <typename CrowApp>
426c4bf6374SJason M. Bills     SystemLogServiceCollection(CrowApp &app) :
427029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/")
428c4bf6374SJason M. Bills     {
429c4bf6374SJason M. Bills         entityPrivileges = {
430c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
431c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
432c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
433c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
434c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
435c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
436c4bf6374SJason M. Bills     }
437c4bf6374SJason M. Bills 
438c4bf6374SJason M. Bills   private:
439c4bf6374SJason M. Bills     /**
440c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
441c4bf6374SJason M. Bills      */
442c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
443c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
444c4bf6374SJason M. Bills     {
445c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
446c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
447c4bf6374SJason M. Bills         // it has a duplicate entry for members
448c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
449c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
450c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
451c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection";
452c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
453029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
454c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
455c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
456c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
457c4bf6374SJason M. Bills         nlohmann::json &logServiceArray = asyncResp->res.jsonValue["Members"];
458c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
459029573d4SEd Tanous         logServiceArray.push_back(
460029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/EventLog"}});
461d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
462d53dd41fSJason M. Bills         logServiceArray.push_back(
463cb92c03bSAndrew Geissler             {{"@odata.id",
464424c4176SJason M. Bills               "/redfish/v1/Systems/system/LogServices/Crashdump"}});
465d53dd41fSJason M. Bills #endif
466c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
467c4bf6374SJason M. Bills             logServiceArray.size();
468c4bf6374SJason M. Bills     }
469c4bf6374SJason M. Bills };
470c4bf6374SJason M. Bills 
471c4bf6374SJason M. Bills class EventLogService : public Node
472c4bf6374SJason M. Bills {
473c4bf6374SJason M. Bills   public:
474c4bf6374SJason M. Bills     template <typename CrowApp>
475c4bf6374SJason M. Bills     EventLogService(CrowApp &app) :
476029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
477c4bf6374SJason M. Bills     {
478c4bf6374SJason M. Bills         entityPrivileges = {
479c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
480c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
481c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
482c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
483c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
484c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
485c4bf6374SJason M. Bills     }
486c4bf6374SJason M. Bills 
487c4bf6374SJason M. Bills   private:
488c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
489c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
490c4bf6374SJason M. Bills     {
491c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
492c4bf6374SJason M. Bills 
493c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
494029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
495c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
496c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
497c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
498c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
499c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
500c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
501c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "Event Log";
502c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
503c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
504c4bf6374SJason M. Bills             {"@odata.id",
505029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
506e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
507e7d6c8b2SGunnar Mills 
508e7d6c8b2SGunnar Mills             {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
509e7d6c8b2SGunnar Mills                        "Actions/LogService.ClearLog"}};
510489640c6SJason M. Bills     }
511489640c6SJason M. Bills };
512489640c6SJason M. Bills 
5131f56a3a6STim Lee class JournalEventLogClear : public Node
514489640c6SJason M. Bills {
515489640c6SJason M. Bills   public:
5161f56a3a6STim Lee     JournalEventLogClear(CrowApp &app) :
517489640c6SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
518489640c6SJason M. Bills                   "LogService.ClearLog/")
519489640c6SJason M. Bills     {
520489640c6SJason M. Bills         entityPrivileges = {
521489640c6SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
522489640c6SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
523489640c6SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
524489640c6SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
525489640c6SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
526489640c6SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
527489640c6SJason M. Bills     }
528489640c6SJason M. Bills 
529489640c6SJason M. Bills   private:
530489640c6SJason M. Bills     void doPost(crow::Response &res, const crow::Request &req,
531489640c6SJason M. Bills                 const std::vector<std::string> &params) override
532489640c6SJason M. Bills     {
533489640c6SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
534489640c6SJason M. Bills 
535489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
536489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
537489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
538489640c6SJason M. Bills         {
539489640c6SJason M. Bills             for (const std::filesystem::path &file : redfishLogFiles)
540489640c6SJason M. Bills             {
541489640c6SJason M. Bills                 std::error_code ec;
542489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
543489640c6SJason M. Bills             }
544489640c6SJason M. Bills         }
545489640c6SJason M. Bills 
546489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
547489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
548489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
549489640c6SJason M. Bills                 if (ec)
550489640c6SJason M. Bills                 {
551489640c6SJason M. Bills                     BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
552489640c6SJason M. Bills                     messages::internalError(asyncResp->res);
553489640c6SJason M. Bills                     return;
554489640c6SJason M. Bills                 }
555489640c6SJason M. Bills 
556489640c6SJason M. Bills                 messages::success(asyncResp->res);
557489640c6SJason M. Bills             },
558489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
559489640c6SJason M. Bills             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
560489640c6SJason M. Bills             "replace");
561c4bf6374SJason M. Bills     }
562c4bf6374SJason M. Bills };
563c4bf6374SJason M. Bills 
56495820184SJason M. Bills static int fillEventLogEntryJson(const std::string &logEntryID,
56595820184SJason M. Bills                                  const std::string logEntry,
56695820184SJason M. Bills                                  nlohmann::json &logEntryJson)
567c4bf6374SJason M. Bills {
56895820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
569cd225da8SJason M. Bills     // First get the Timestamp
570cd225da8SJason M. Bills     size_t space = logEntry.find_first_of(" ");
571cd225da8SJason M. Bills     if (space == std::string::npos)
57295820184SJason M. Bills     {
57395820184SJason M. Bills         return 1;
57495820184SJason M. Bills     }
575cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
576cd225da8SJason M. Bills     // Then get the log contents
577cd225da8SJason M. Bills     size_t entryStart = logEntry.find_first_not_of(" ", space);
578cd225da8SJason M. Bills     if (entryStart == std::string::npos)
579cd225da8SJason M. Bills     {
580cd225da8SJason M. Bills         return 1;
581cd225da8SJason M. Bills     }
582cd225da8SJason M. Bills     std::string_view entry(logEntry);
583cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
584cd225da8SJason M. Bills     // Use split to separate the entry into its fields
585cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
586cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
587cd225da8SJason M. Bills                  boost::token_compress_on);
588cd225da8SJason M. Bills     // We need at least a MessageId to be valid
589cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
590cd225da8SJason M. Bills     {
591cd225da8SJason M. Bills         return 1;
592cd225da8SJason M. Bills     }
593cd225da8SJason M. Bills     std::string &messageID = logEntryFields[0];
59495820184SJason M. Bills 
5954851d45dSJason M. Bills     // Get the Message from the MessageRegistry
5964851d45dSJason M. Bills     const message_registries::Message *message =
5974851d45dSJason M. Bills         message_registries::getMessage(messageID);
598c4bf6374SJason M. Bills 
5994851d45dSJason M. Bills     std::string msg;
6004851d45dSJason M. Bills     std::string severity;
6014851d45dSJason M. Bills     if (message != nullptr)
602c4bf6374SJason M. Bills     {
6034851d45dSJason M. Bills         msg = message->message;
6044851d45dSJason M. Bills         severity = message->severity;
605c4bf6374SJason M. Bills     }
606c4bf6374SJason M. Bills 
60715a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
60815a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
60915a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
61015a86ff6SJason M. Bills     {
61115a86ff6SJason M. Bills         std::string &messageArgsStart = logEntryFields[1];
61215a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
61315a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
61415a86ff6SJason M. Bills         if (!messageArgsStart.empty())
61515a86ff6SJason M. Bills         {
61615a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
61715a86ff6SJason M. Bills         }
61815a86ff6SJason M. Bills 
61915a86ff6SJason M. Bills         messageArgs = boost::beast::span(&messageArgsStart, messageArgsSize);
620c4bf6374SJason M. Bills 
6214851d45dSJason M. Bills         // Fill the MessageArgs into the Message
62295820184SJason M. Bills         int i = 0;
62395820184SJason M. Bills         for (const std::string &messageArg : messageArgs)
6244851d45dSJason M. Bills         {
62595820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
6264851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
6274851d45dSJason M. Bills             if (argPos != std::string::npos)
6284851d45dSJason M. Bills             {
62995820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
6304851d45dSJason M. Bills             }
6314851d45dSJason M. Bills         }
63215a86ff6SJason M. Bills     }
6334851d45dSJason M. Bills 
63495820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
63595820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
63695820184SJason M. Bills     // between the '.' and the '+', so just remove them.
63795820184SJason M. Bills     std::size_t dot = timestamp.find_first_of(".");
63895820184SJason M. Bills     std::size_t plus = timestamp.find_first_of("+");
63995820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
640c4bf6374SJason M. Bills     {
64195820184SJason M. Bills         timestamp.erase(dot, plus - dot);
642c4bf6374SJason M. Bills     }
643c4bf6374SJason M. Bills 
644c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
64595820184SJason M. Bills     logEntryJson = {
646cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
647c4bf6374SJason M. Bills         {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
648029573d4SEd Tanous         {"@odata.id",
649897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
65095820184SJason M. Bills              logEntryID},
651c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
65295820184SJason M. Bills         {"Id", logEntryID},
65395820184SJason M. Bills         {"Message", std::move(msg)},
65495820184SJason M. Bills         {"MessageId", std::move(messageID)},
655c4bf6374SJason M. Bills         {"MessageArgs", std::move(messageArgs)},
656c4bf6374SJason M. Bills         {"EntryType", "Event"},
65795820184SJason M. Bills         {"Severity", std::move(severity)},
65895820184SJason M. Bills         {"Created", std::move(timestamp)}};
659c4bf6374SJason M. Bills     return 0;
660c4bf6374SJason M. Bills }
661c4bf6374SJason M. Bills 
66227062605SAnthony Wilson class JournalEventLogEntryCollection : public Node
663c4bf6374SJason M. Bills {
664c4bf6374SJason M. Bills   public:
665c4bf6374SJason M. Bills     template <typename CrowApp>
66627062605SAnthony Wilson     JournalEventLogEntryCollection(CrowApp &app) :
667029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
668c4bf6374SJason M. Bills     {
669c4bf6374SJason M. Bills         entityPrivileges = {
670c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
671c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
672c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
673c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
674c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
675c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
676c4bf6374SJason M. Bills     }
677c4bf6374SJason M. Bills 
678c4bf6374SJason M. Bills   private:
679c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
680c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
681c4bf6374SJason M. Bills     {
682c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
683271584abSEd Tanous         uint64_t skip = 0;
684271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
685c4bf6374SJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
686c4bf6374SJason M. Bills         {
687c4bf6374SJason M. Bills             return;
688c4bf6374SJason M. Bills         }
689c4bf6374SJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
690c4bf6374SJason M. Bills         {
691c4bf6374SJason M. Bills             return;
692c4bf6374SJason M. Bills         }
693c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
694c4bf6374SJason M. Bills         // it has a duplicate entry for members
695c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
696c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
697c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
698c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
699c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
700029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
701c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
702c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
703c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
704cb92c03bSAndrew Geissler 
705c4bf6374SJason M. Bills         nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
706c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
70795820184SJason M. Bills         // Go through the log files and create a unique ID for each entry
70895820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
70995820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
710b01bf299SEd Tanous         uint64_t entryCount = 0;
711cd225da8SJason M. Bills         std::string logEntry;
71295820184SJason M. Bills 
71395820184SJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
714cd225da8SJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
715cd225da8SJason M. Bills              it++)
716c4bf6374SJason M. Bills         {
717cd225da8SJason M. Bills             std::ifstream logStream(*it);
71895820184SJason M. Bills             if (!logStream.is_open())
719c4bf6374SJason M. Bills             {
720c4bf6374SJason M. Bills                 continue;
721c4bf6374SJason M. Bills             }
722c4bf6374SJason M. Bills 
723e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
724e85d6b16SJason M. Bills             bool firstEntry = true;
72595820184SJason M. Bills             while (std::getline(logStream, logEntry))
72695820184SJason M. Bills             {
727c4bf6374SJason M. Bills                 entryCount++;
728c4bf6374SJason M. Bills                 // Handle paging using skip (number of entries to skip from the
729c4bf6374SJason M. Bills                 // start) and top (number of entries to display)
730c4bf6374SJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
731c4bf6374SJason M. Bills                 {
732c4bf6374SJason M. Bills                     continue;
733c4bf6374SJason M. Bills                 }
734c4bf6374SJason M. Bills 
735c4bf6374SJason M. Bills                 std::string idStr;
736e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
737c4bf6374SJason M. Bills                 {
738c4bf6374SJason M. Bills                     continue;
739c4bf6374SJason M. Bills                 }
740c4bf6374SJason M. Bills 
741e85d6b16SJason M. Bills                 if (firstEntry)
742e85d6b16SJason M. Bills                 {
743e85d6b16SJason M. Bills                     firstEntry = false;
744e85d6b16SJason M. Bills                 }
745e85d6b16SJason M. Bills 
746c4bf6374SJason M. Bills                 logEntryArray.push_back({});
747c4bf6374SJason M. Bills                 nlohmann::json &bmcLogEntry = logEntryArray.back();
74895820184SJason M. Bills                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
749c4bf6374SJason M. Bills                 {
750c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
751c4bf6374SJason M. Bills                     return;
752c4bf6374SJason M. Bills                 }
753c4bf6374SJason M. Bills             }
75495820184SJason M. Bills         }
755c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
756c4bf6374SJason M. Bills         if (skip + top < entryCount)
757c4bf6374SJason M. Bills         {
758c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
75995820184SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/"
76095820184SJason M. Bills                 "Entries?$skip=" +
761c4bf6374SJason M. Bills                 std::to_string(skip + top);
762c4bf6374SJason M. Bills         }
76308a4e4b5SAnthony Wilson     }
76408a4e4b5SAnthony Wilson };
76508a4e4b5SAnthony Wilson 
766897967deSJason M. Bills class JournalEventLogEntry : public Node
767897967deSJason M. Bills {
768897967deSJason M. Bills   public:
769897967deSJason M. Bills     JournalEventLogEntry(CrowApp &app) :
770897967deSJason M. Bills         Node(app,
771897967deSJason M. Bills              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
772897967deSJason M. Bills              std::string())
773897967deSJason M. Bills     {
774897967deSJason M. Bills         entityPrivileges = {
775897967deSJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
776897967deSJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
777897967deSJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
778897967deSJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
779897967deSJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
780897967deSJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
781897967deSJason M. Bills     }
782897967deSJason M. Bills 
783897967deSJason M. Bills   private:
784897967deSJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
785897967deSJason M. Bills                const std::vector<std::string> &params) override
786897967deSJason M. Bills     {
787897967deSJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
788897967deSJason M. Bills         if (params.size() != 1)
789897967deSJason M. Bills         {
790897967deSJason M. Bills             messages::internalError(asyncResp->res);
791897967deSJason M. Bills             return;
792897967deSJason M. Bills         }
793897967deSJason M. Bills         const std::string &targetID = params[0];
794897967deSJason M. Bills 
795897967deSJason M. Bills         // Go through the log files and check the unique ID for each entry to
796897967deSJason M. Bills         // find the target entry
797897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
798897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
799897967deSJason M. Bills         std::string logEntry;
800897967deSJason M. Bills 
801897967deSJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
802897967deSJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
803897967deSJason M. Bills              it++)
804897967deSJason M. Bills         {
805897967deSJason M. Bills             std::ifstream logStream(*it);
806897967deSJason M. Bills             if (!logStream.is_open())
807897967deSJason M. Bills             {
808897967deSJason M. Bills                 continue;
809897967deSJason M. Bills             }
810897967deSJason M. Bills 
811897967deSJason M. Bills             // Reset the unique ID on the first entry
812897967deSJason M. Bills             bool firstEntry = true;
813897967deSJason M. Bills             while (std::getline(logStream, logEntry))
814897967deSJason M. Bills             {
815897967deSJason M. Bills                 std::string idStr;
816897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
817897967deSJason M. Bills                 {
818897967deSJason M. Bills                     continue;
819897967deSJason M. Bills                 }
820897967deSJason M. Bills 
821897967deSJason M. Bills                 if (firstEntry)
822897967deSJason M. Bills                 {
823897967deSJason M. Bills                     firstEntry = false;
824897967deSJason M. Bills                 }
825897967deSJason M. Bills 
826897967deSJason M. Bills                 if (idStr == targetID)
827897967deSJason M. Bills                 {
828897967deSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry,
829897967deSJason M. Bills                                               asyncResp->res.jsonValue) != 0)
830897967deSJason M. Bills                     {
831897967deSJason M. Bills                         messages::internalError(asyncResp->res);
832897967deSJason M. Bills                         return;
833897967deSJason M. Bills                     }
834897967deSJason M. Bills                     return;
835897967deSJason M. Bills                 }
836897967deSJason M. Bills             }
837897967deSJason M. Bills         }
838897967deSJason M. Bills         // Requested ID was not found
839897967deSJason M. Bills         messages::resourceMissingAtURI(asyncResp->res, targetID);
840897967deSJason M. Bills     }
841897967deSJason M. Bills };
842897967deSJason M. Bills 
84308a4e4b5SAnthony Wilson class DBusEventLogEntryCollection : public Node
84408a4e4b5SAnthony Wilson {
84508a4e4b5SAnthony Wilson   public:
84608a4e4b5SAnthony Wilson     template <typename CrowApp>
84708a4e4b5SAnthony Wilson     DBusEventLogEntryCollection(CrowApp &app) :
84808a4e4b5SAnthony Wilson         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
84908a4e4b5SAnthony Wilson     {
85008a4e4b5SAnthony Wilson         entityPrivileges = {
85108a4e4b5SAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
85208a4e4b5SAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
85308a4e4b5SAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
85408a4e4b5SAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
85508a4e4b5SAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
85608a4e4b5SAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
85708a4e4b5SAnthony Wilson     }
85808a4e4b5SAnthony Wilson 
85908a4e4b5SAnthony Wilson   private:
86008a4e4b5SAnthony Wilson     void doGet(crow::Response &res, const crow::Request &req,
86108a4e4b5SAnthony Wilson                const std::vector<std::string> &params) override
86208a4e4b5SAnthony Wilson     {
86308a4e4b5SAnthony Wilson         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
86408a4e4b5SAnthony Wilson 
86508a4e4b5SAnthony Wilson         // Collections don't include the static data added by SubRoute because
86608a4e4b5SAnthony Wilson         // it has a duplicate entry for members
86708a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
86808a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
86908a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.context"] =
87008a4e4b5SAnthony Wilson             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
87108a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
87208a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
87308a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
87408a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
87508a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
87608a4e4b5SAnthony Wilson 
877cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
878cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
879cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
880cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
881cb92c03bSAndrew Geissler                         GetManagedObjectsType &resp) {
882cb92c03bSAndrew Geissler                 if (ec)
883cb92c03bSAndrew Geissler                 {
884cb92c03bSAndrew Geissler                     // TODO Handle for specific error code
885cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
886cb92c03bSAndrew Geissler                         << "getLogEntriesIfaceData resp_handler got error "
887cb92c03bSAndrew Geissler                         << ec;
888cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
889cb92c03bSAndrew Geissler                     return;
890cb92c03bSAndrew Geissler                 }
891cb92c03bSAndrew Geissler                 nlohmann::json &entriesArray =
892cb92c03bSAndrew Geissler                     asyncResp->res.jsonValue["Members"];
893cb92c03bSAndrew Geissler                 entriesArray = nlohmann::json::array();
894cb92c03bSAndrew Geissler                 for (auto &objectPath : resp)
895cb92c03bSAndrew Geissler                 {
896cb92c03bSAndrew Geissler                     for (auto &interfaceMap : objectPath.second)
897cb92c03bSAndrew Geissler                     {
898cb92c03bSAndrew Geissler                         if (interfaceMap.first !=
899cb92c03bSAndrew Geissler                             "xyz.openbmc_project.Logging.Entry")
900cb92c03bSAndrew Geissler                         {
901cb92c03bSAndrew Geissler                             BMCWEB_LOG_DEBUG << "Bailing early on "
902cb92c03bSAndrew Geissler                                              << interfaceMap.first;
903cb92c03bSAndrew Geissler                             continue;
904cb92c03bSAndrew Geissler                         }
905cb92c03bSAndrew Geissler                         entriesArray.push_back({});
906cb92c03bSAndrew Geissler                         nlohmann::json &thisEntry = entriesArray.back();
90766664f25SEd Tanous                         uint32_t *id = nullptr;
90866664f25SEd Tanous                         std::time_t timestamp{};
90966664f25SEd Tanous                         std::string *severity = nullptr;
91066664f25SEd Tanous                         std::string *message = nullptr;
911cb92c03bSAndrew Geissler                         for (auto &propertyMap : interfaceMap.second)
912cb92c03bSAndrew Geissler                         {
913cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
914cb92c03bSAndrew Geissler                             {
915cb92c03bSAndrew Geissler                                 id = sdbusplus::message::variant_ns::get_if<
916cb92c03bSAndrew Geissler                                     uint32_t>(&propertyMap.second);
917cb92c03bSAndrew Geissler                                 if (id == nullptr)
918cb92c03bSAndrew Geissler                                 {
919cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
920cb92c03bSAndrew Geissler                                                               "Id");
921cb92c03bSAndrew Geissler                                 }
922cb92c03bSAndrew Geissler                             }
923cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
924cb92c03bSAndrew Geissler                             {
925cb92c03bSAndrew Geissler                                 const uint64_t *millisTimeStamp =
926cb92c03bSAndrew Geissler                                     std::get_if<uint64_t>(&propertyMap.second);
927cb92c03bSAndrew Geissler                                 if (millisTimeStamp == nullptr)
928cb92c03bSAndrew Geissler                                 {
929cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
930cb92c03bSAndrew Geissler                                                               "Timestamp");
931271584abSEd Tanous                                     continue;
932cb92c03bSAndrew Geissler                                 }
933cb92c03bSAndrew Geissler                                 // Retrieve Created property with format:
934cb92c03bSAndrew Geissler                                 // yyyy-mm-ddThh:mm:ss
935cb92c03bSAndrew Geissler                                 std::chrono::milliseconds chronoTimeStamp(
936cb92c03bSAndrew Geissler                                     *millisTimeStamp);
937271584abSEd Tanous                                 timestamp = std::chrono::duration_cast<
938271584abSEd Tanous                                                 std::chrono::duration<int>>(
939271584abSEd Tanous                                                 chronoTimeStamp)
940cb92c03bSAndrew Geissler                                                 .count();
941cb92c03bSAndrew Geissler                             }
942cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
943cb92c03bSAndrew Geissler                             {
944cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
945cb92c03bSAndrew Geissler                                     &propertyMap.second);
946cb92c03bSAndrew Geissler                                 if (severity == nullptr)
947cb92c03bSAndrew Geissler                                 {
948cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
949cb92c03bSAndrew Geissler                                                               "Severity");
950cb92c03bSAndrew Geissler                                 }
951cb92c03bSAndrew Geissler                             }
952cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
953cb92c03bSAndrew Geissler                             {
954cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
955cb92c03bSAndrew Geissler                                     &propertyMap.second);
956cb92c03bSAndrew Geissler                                 if (message == nullptr)
957cb92c03bSAndrew Geissler                                 {
958cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
959cb92c03bSAndrew Geissler                                                               "Message");
960cb92c03bSAndrew Geissler                                 }
961cb92c03bSAndrew Geissler                             }
962cb92c03bSAndrew Geissler                         }
963cb92c03bSAndrew Geissler                         thisEntry = {
964cb92c03bSAndrew Geissler                             {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
965cb92c03bSAndrew Geissler                             {"@odata.context", "/redfish/v1/"
966cb92c03bSAndrew Geissler                                                "$metadata#LogEntry.LogEntry"},
967cb92c03bSAndrew Geissler                             {"@odata.id",
968cb92c03bSAndrew Geissler                              "/redfish/v1/Systems/system/LogServices/EventLog/"
969cb92c03bSAndrew Geissler                              "Entries/" +
970cb92c03bSAndrew Geissler                                  std::to_string(*id)},
97127062605SAnthony Wilson                             {"Name", "System Event Log Entry"},
972cb92c03bSAndrew Geissler                             {"Id", std::to_string(*id)},
973cb92c03bSAndrew Geissler                             {"Message", *message},
974cb92c03bSAndrew Geissler                             {"EntryType", "Event"},
975cb92c03bSAndrew Geissler                             {"Severity",
976cb92c03bSAndrew Geissler                              translateSeverityDbusToRedfish(*severity)},
977cb92c03bSAndrew Geissler                             {"Created", crow::utility::getDateTime(timestamp)}};
978cb92c03bSAndrew Geissler                     }
979cb92c03bSAndrew Geissler                 }
980cb92c03bSAndrew Geissler                 std::sort(entriesArray.begin(), entriesArray.end(),
981cb92c03bSAndrew Geissler                           [](const nlohmann::json &left,
982cb92c03bSAndrew Geissler                              const nlohmann::json &right) {
983cb92c03bSAndrew Geissler                               return (left["Id"] <= right["Id"]);
984cb92c03bSAndrew Geissler                           });
985cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
986cb92c03bSAndrew Geissler                     entriesArray.size();
987cb92c03bSAndrew Geissler             },
988cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
989cb92c03bSAndrew Geissler             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
990c4bf6374SJason M. Bills     }
991c4bf6374SJason M. Bills };
992c4bf6374SJason M. Bills 
99308a4e4b5SAnthony Wilson class DBusEventLogEntry : public Node
994c4bf6374SJason M. Bills {
995c4bf6374SJason M. Bills   public:
99608a4e4b5SAnthony Wilson     DBusEventLogEntry(CrowApp &app) :
997c4bf6374SJason M. Bills         Node(app,
998029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
999029573d4SEd Tanous              std::string())
1000c4bf6374SJason M. Bills     {
1001c4bf6374SJason M. Bills         entityPrivileges = {
1002c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1003c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1004c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1005c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1006c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1007c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1008c4bf6374SJason M. Bills     }
1009c4bf6374SJason M. Bills 
1010c4bf6374SJason M. Bills   private:
1011c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1012c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
1013c4bf6374SJason M. Bills     {
1014c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1015029573d4SEd Tanous         if (params.size() != 1)
1016c4bf6374SJason M. Bills         {
1017c4bf6374SJason M. Bills             messages::internalError(asyncResp->res);
1018c4bf6374SJason M. Bills             return;
1019c4bf6374SJason M. Bills         }
1020029573d4SEd Tanous         const std::string &entryID = params[0];
1021cb92c03bSAndrew Geissler 
1022cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1023cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1024cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1025cb92c03bSAndrew Geissler             [asyncResp, entryID](const boost::system::error_code ec,
1026cb92c03bSAndrew Geissler                                  GetManagedPropertyType &resp) {
1027cb92c03bSAndrew Geissler                 if (ec)
1028cb92c03bSAndrew Geissler                 {
1029cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
1030cb92c03bSAndrew Geissler                         << "EventLogEntry (DBus) resp_handler got error " << ec;
1031cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
1032cb92c03bSAndrew Geissler                     return;
1033cb92c03bSAndrew Geissler                 }
103466664f25SEd Tanous                 uint32_t *id = nullptr;
103566664f25SEd Tanous                 std::time_t timestamp{};
103666664f25SEd Tanous                 std::string *severity = nullptr;
103766664f25SEd Tanous                 std::string *message = nullptr;
1038cb92c03bSAndrew Geissler                 for (auto &propertyMap : resp)
1039cb92c03bSAndrew Geissler                 {
1040cb92c03bSAndrew Geissler                     if (propertyMap.first == "Id")
1041cb92c03bSAndrew Geissler                     {
1042cb92c03bSAndrew Geissler                         id = std::get_if<uint32_t>(&propertyMap.second);
1043cb92c03bSAndrew Geissler                         if (id == nullptr)
1044cb92c03bSAndrew Geissler                         {
1045cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res, "Id");
1046cb92c03bSAndrew Geissler                         }
1047cb92c03bSAndrew Geissler                     }
1048cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Timestamp")
1049cb92c03bSAndrew Geissler                     {
1050cb92c03bSAndrew Geissler                         const uint64_t *millisTimeStamp =
1051cb92c03bSAndrew Geissler                             std::get_if<uint64_t>(&propertyMap.second);
1052cb92c03bSAndrew Geissler                         if (millisTimeStamp == nullptr)
1053cb92c03bSAndrew Geissler                         {
1054cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1055cb92c03bSAndrew Geissler                                                       "Timestamp");
1056271584abSEd Tanous                             continue;
1057cb92c03bSAndrew Geissler                         }
1058cb92c03bSAndrew Geissler                         // Retrieve Created property with format:
1059cb92c03bSAndrew Geissler                         // yyyy-mm-ddThh:mm:ss
1060cb92c03bSAndrew Geissler                         std::chrono::milliseconds chronoTimeStamp(
1061cb92c03bSAndrew Geissler                             *millisTimeStamp);
1062cb92c03bSAndrew Geissler                         timestamp =
1063271584abSEd Tanous                             std::chrono::duration_cast<
1064271584abSEd Tanous                                 std::chrono::duration<int>>(chronoTimeStamp)
1065cb92c03bSAndrew Geissler                                 .count();
1066cb92c03bSAndrew Geissler                     }
1067cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Severity")
1068cb92c03bSAndrew Geissler                     {
1069cb92c03bSAndrew Geissler                         severity =
1070cb92c03bSAndrew Geissler                             std::get_if<std::string>(&propertyMap.second);
1071cb92c03bSAndrew Geissler                         if (severity == nullptr)
1072cb92c03bSAndrew Geissler                         {
1073cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1074cb92c03bSAndrew Geissler                                                       "Severity");
1075cb92c03bSAndrew Geissler                         }
1076cb92c03bSAndrew Geissler                     }
1077cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Message")
1078cb92c03bSAndrew Geissler                     {
1079cb92c03bSAndrew Geissler                         message = std::get_if<std::string>(&propertyMap.second);
1080cb92c03bSAndrew Geissler                         if (message == nullptr)
1081cb92c03bSAndrew Geissler                         {
1082cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1083cb92c03bSAndrew Geissler                                                       "Message");
1084cb92c03bSAndrew Geissler                         }
1085cb92c03bSAndrew Geissler                     }
1086cb92c03bSAndrew Geissler                 }
1087271584abSEd Tanous                 if (id == nullptr || message == nullptr || severity == nullptr)
1088271584abSEd Tanous                 {
1089271584abSEd Tanous                     return;
1090271584abSEd Tanous                 }
1091cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue = {
1092cb92c03bSAndrew Geissler                     {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1093cb92c03bSAndrew Geissler                     {"@odata.context", "/redfish/v1/"
1094cb92c03bSAndrew Geissler                                        "$metadata#LogEntry.LogEntry"},
1095cb92c03bSAndrew Geissler                     {"@odata.id",
1096cb92c03bSAndrew Geissler                      "/redfish/v1/Systems/system/LogServices/EventLog/"
1097cb92c03bSAndrew Geissler                      "Entries/" +
1098cb92c03bSAndrew Geissler                          std::to_string(*id)},
109927062605SAnthony Wilson                     {"Name", "System Event Log Entry"},
1100cb92c03bSAndrew Geissler                     {"Id", std::to_string(*id)},
1101cb92c03bSAndrew Geissler                     {"Message", *message},
1102cb92c03bSAndrew Geissler                     {"EntryType", "Event"},
1103cb92c03bSAndrew Geissler                     {"Severity", translateSeverityDbusToRedfish(*severity)},
110408a4e4b5SAnthony Wilson                     {"Created", crow::utility::getDateTime(timestamp)}};
1105cb92c03bSAndrew Geissler             },
1106cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1107cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1108cb92c03bSAndrew Geissler             "org.freedesktop.DBus.Properties", "GetAll",
1109cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging.Entry");
1110c4bf6374SJason M. Bills     }
1111336e96c6SChicago Duan 
1112336e96c6SChicago Duan     void doDelete(crow::Response &res, const crow::Request &req,
1113336e96c6SChicago Duan                   const std::vector<std::string> &params) override
1114336e96c6SChicago Duan     {
1115336e96c6SChicago Duan 
1116336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1117336e96c6SChicago Duan 
1118336e96c6SChicago Duan         auto asyncResp = std::make_shared<AsyncResp>(res);
1119336e96c6SChicago Duan 
1120336e96c6SChicago Duan         if (params.size() != 1)
1121336e96c6SChicago Duan         {
1122336e96c6SChicago Duan             messages::internalError(asyncResp->res);
1123336e96c6SChicago Duan             return;
1124336e96c6SChicago Duan         }
1125336e96c6SChicago Duan         std::string entryID = params[0];
1126336e96c6SChicago Duan 
1127336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1128336e96c6SChicago Duan 
1129336e96c6SChicago Duan         // Process response from Logging service.
1130336e96c6SChicago Duan         auto respHandler = [asyncResp](const boost::system::error_code ec) {
1131336e96c6SChicago Duan             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1132336e96c6SChicago Duan             if (ec)
1133336e96c6SChicago Duan             {
1134336e96c6SChicago Duan                 // TODO Handle for specific error code
1135336e96c6SChicago Duan                 BMCWEB_LOG_ERROR
1136336e96c6SChicago Duan                     << "EventLogEntry (DBus) doDelete respHandler got error "
1137336e96c6SChicago Duan                     << ec;
1138336e96c6SChicago Duan                 asyncResp->res.result(
1139336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1140336e96c6SChicago Duan                 return;
1141336e96c6SChicago Duan             }
1142336e96c6SChicago Duan 
1143336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1144336e96c6SChicago Duan         };
1145336e96c6SChicago Duan 
1146336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1147336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1148336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1149336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1150336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
1151336e96c6SChicago Duan     }
1152c4bf6374SJason M. Bills };
1153c4bf6374SJason M. Bills 
1154c4bf6374SJason M. Bills class BMCLogServiceCollection : public Node
1155c4bf6374SJason M. Bills {
1156c4bf6374SJason M. Bills   public:
1157c4bf6374SJason M. Bills     template <typename CrowApp>
1158c4bf6374SJason M. Bills     BMCLogServiceCollection(CrowApp &app) :
11594ed77cd5SEd Tanous         Node(app, "/redfish/v1/Managers/bmc/LogServices/")
11601da66f75SEd Tanous     {
11611da66f75SEd Tanous         entityPrivileges = {
1162e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1163e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1164e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1165e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1166e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1167e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
11681da66f75SEd Tanous     }
11691da66f75SEd Tanous 
11701da66f75SEd Tanous   private:
11711da66f75SEd Tanous     /**
11721da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
11731da66f75SEd Tanous      */
11741da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
11751da66f75SEd Tanous                const std::vector<std::string> &params) override
11761da66f75SEd Tanous     {
1177e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
11781da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
11791da66f75SEd Tanous         // it has a duplicate entry for members
1180e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
11811da66f75SEd Tanous             "#LogServiceCollection.LogServiceCollection";
1182e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1183c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection";
1184e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1185e1f26343SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices";
1186e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1187e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
11881da66f75SEd Tanous             "Collection of LogServices for this Manager";
1189c4bf6374SJason M. Bills         nlohmann::json &logServiceArray = asyncResp->res.jsonValue["Members"];
1190c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
1191c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1192c4bf6374SJason M. Bills         logServiceArray.push_back(
119308a4e4b5SAnthony Wilson             {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1194c4bf6374SJason M. Bills #endif
1195e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1196c4bf6374SJason M. Bills             logServiceArray.size();
11971da66f75SEd Tanous     }
11981da66f75SEd Tanous };
11991da66f75SEd Tanous 
1200c4bf6374SJason M. Bills class BMCJournalLogService : public Node
12011da66f75SEd Tanous {
12021da66f75SEd Tanous   public:
12031da66f75SEd Tanous     template <typename CrowApp>
1204c4bf6374SJason M. Bills     BMCJournalLogService(CrowApp &app) :
1205c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1206e1f26343SJason M. Bills     {
1207e1f26343SJason M. Bills         entityPrivileges = {
1208e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1209e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1210e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1211e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1212e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1213e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1214e1f26343SJason M. Bills     }
1215e1f26343SJason M. Bills 
1216e1f26343SJason M. Bills   private:
1217e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1218e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1219e1f26343SJason M. Bills     {
1220e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1221e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1222e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
12230f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
12240f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
1225e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1226e1f26343SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
1227c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
1228c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
1229c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
1230e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1231cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1232cd50aa42SJason M. Bills             {"@odata.id",
1233086be238SEd Tanous              "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1234e1f26343SJason M. Bills     }
1235e1f26343SJason M. Bills };
1236e1f26343SJason M. Bills 
1237c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string &bmcJournalLogEntryID,
1238e1f26343SJason M. Bills                                       sd_journal *journal,
1239c4bf6374SJason M. Bills                                       nlohmann::json &bmcJournalLogEntryJson)
1240e1f26343SJason M. Bills {
1241e1f26343SJason M. Bills     // Get the Log Entry contents
1242e1f26343SJason M. Bills     int ret = 0;
1243e1f26343SJason M. Bills 
124439e77504SEd Tanous     std::string_view msg;
124516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1246e1f26343SJason M. Bills     if (ret < 0)
1247e1f26343SJason M. Bills     {
1248e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1249e1f26343SJason M. Bills         return 1;
1250e1f26343SJason M. Bills     }
1251e1f26343SJason M. Bills 
1252e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1253271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
125416428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1255e1f26343SJason M. Bills     if (ret < 0)
1256e1f26343SJason M. Bills     {
1257e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1258e1f26343SJason M. Bills     }
1259e1f26343SJason M. Bills 
1260e1f26343SJason M. Bills     // Get the Created time from the timestamp
126116428a1aSJason M. Bills     std::string entryTimeStr;
126216428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
1263e1f26343SJason M. Bills     {
126416428a1aSJason M. Bills         return 1;
1265e1f26343SJason M. Bills     }
1266e1f26343SJason M. Bills 
1267e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
1268c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
1269cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1270e1f26343SJason M. Bills         {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
1271c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1272c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
1273e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
1274c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
127516428a1aSJason M. Bills         {"Message", msg},
1276e1f26343SJason M. Bills         {"EntryType", "Oem"},
1277e1f26343SJason M. Bills         {"Severity",
1278b6a61a5eSJason M. Bills          severity <= 2 ? "Critical" : severity <= 4 ? "Warning" : "OK"},
1279086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
1280e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
1281e1f26343SJason M. Bills     return 0;
1282e1f26343SJason M. Bills }
1283e1f26343SJason M. Bills 
1284c4bf6374SJason M. Bills class BMCJournalLogEntryCollection : public Node
1285e1f26343SJason M. Bills {
1286e1f26343SJason M. Bills   public:
1287e1f26343SJason M. Bills     template <typename CrowApp>
1288c4bf6374SJason M. Bills     BMCJournalLogEntryCollection(CrowApp &app) :
1289c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
1290e1f26343SJason M. Bills     {
1291e1f26343SJason M. Bills         entityPrivileges = {
1292e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1293e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1294e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1295e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1296e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1297e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1298e1f26343SJason M. Bills     }
1299e1f26343SJason M. Bills 
1300e1f26343SJason M. Bills   private:
1301e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1302e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1303e1f26343SJason M. Bills     {
1304e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1305193ad2faSJason M. Bills         static constexpr const long maxEntriesPerPage = 1000;
1306271584abSEd Tanous         uint64_t skip = 0;
1307271584abSEd Tanous         uint64_t top = maxEntriesPerPage; // Show max entries by default
130816428a1aSJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
1309193ad2faSJason M. Bills         {
1310193ad2faSJason M. Bills             return;
1311193ad2faSJason M. Bills         }
131216428a1aSJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
1313193ad2faSJason M. Bills         {
1314193ad2faSJason M. Bills             return;
1315193ad2faSJason M. Bills         }
1316e1f26343SJason M. Bills         // Collections don't include the static data added by SubRoute because
1317e1f26343SJason M. Bills         // it has a duplicate entry for members
1318e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1319e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
13200f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
13210f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1322e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1323c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
1324e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1325c4bf6374SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1326e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1327e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1328e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
13290f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
13300f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/BmcLog/Entries";
1331e1f26343SJason M. Bills         nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
1332e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
1333e1f26343SJason M. Bills 
1334e1f26343SJason M. Bills         // Go through the journal and use the timestamp to create a unique ID
1335e1f26343SJason M. Bills         // for each entry
1336e1f26343SJason M. Bills         sd_journal *journalTmp = nullptr;
1337e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1338e1f26343SJason M. Bills         if (ret < 0)
1339e1f26343SJason M. Bills         {
1340e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1341f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1342e1f26343SJason M. Bills             return;
1343e1f26343SJason M. Bills         }
1344e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1345e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1346e1f26343SJason M. Bills         journalTmp = nullptr;
1347b01bf299SEd Tanous         uint64_t entryCount = 0;
1348e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
1349e85d6b16SJason M. Bills         bool firstEntry = true;
1350e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
1351e1f26343SJason M. Bills         {
1352193ad2faSJason M. Bills             entryCount++;
1353193ad2faSJason M. Bills             // Handle paging using skip (number of entries to skip from the
1354193ad2faSJason M. Bills             // start) and top (number of entries to display)
1355193ad2faSJason M. Bills             if (entryCount <= skip || entryCount > skip + top)
1356193ad2faSJason M. Bills             {
1357193ad2faSJason M. Bills                 continue;
1358193ad2faSJason M. Bills             }
1359193ad2faSJason M. Bills 
136016428a1aSJason M. Bills             std::string idStr;
1361e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1362e1f26343SJason M. Bills             {
1363e1f26343SJason M. Bills                 continue;
1364e1f26343SJason M. Bills             }
1365e1f26343SJason M. Bills 
1366e85d6b16SJason M. Bills             if (firstEntry)
1367e85d6b16SJason M. Bills             {
1368e85d6b16SJason M. Bills                 firstEntry = false;
1369e85d6b16SJason M. Bills             }
1370e85d6b16SJason M. Bills 
1371e1f26343SJason M. Bills             logEntryArray.push_back({});
1372c4bf6374SJason M. Bills             nlohmann::json &bmcJournalLogEntry = logEntryArray.back();
1373c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
1374c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
1375e1f26343SJason M. Bills             {
1376f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1377e1f26343SJason M. Bills                 return;
1378e1f26343SJason M. Bills             }
1379e1f26343SJason M. Bills         }
1380193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1381193ad2faSJason M. Bills         if (skip + top < entryCount)
1382193ad2faSJason M. Bills         {
1383193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
1384c4bf6374SJason M. Bills                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
1385193ad2faSJason M. Bills                 std::to_string(skip + top);
1386193ad2faSJason M. Bills         }
1387e1f26343SJason M. Bills     }
1388e1f26343SJason M. Bills };
1389e1f26343SJason M. Bills 
1390c4bf6374SJason M. Bills class BMCJournalLogEntry : public Node
1391e1f26343SJason M. Bills {
1392e1f26343SJason M. Bills   public:
1393c4bf6374SJason M. Bills     BMCJournalLogEntry(CrowApp &app) :
1394c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/",
1395e1f26343SJason M. Bills              std::string())
1396e1f26343SJason M. Bills     {
1397e1f26343SJason M. Bills         entityPrivileges = {
1398e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1399e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1400e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1401e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1402e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1403e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1404e1f26343SJason M. Bills     }
1405e1f26343SJason M. Bills 
1406e1f26343SJason M. Bills   private:
1407e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1408e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1409e1f26343SJason M. Bills     {
1410e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1411e1f26343SJason M. Bills         if (params.size() != 1)
1412e1f26343SJason M. Bills         {
1413f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1414e1f26343SJason M. Bills             return;
1415e1f26343SJason M. Bills         }
141616428a1aSJason M. Bills         const std::string &entryID = params[0];
1417e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
1418e1f26343SJason M. Bills         uint64_t ts = 0;
1419271584abSEd Tanous         uint64_t index = 0;
142016428a1aSJason M. Bills         if (!getTimestampFromID(asyncResp->res, entryID, ts, index))
1421e1f26343SJason M. Bills         {
142216428a1aSJason M. Bills             return;
1423e1f26343SJason M. Bills         }
1424e1f26343SJason M. Bills 
1425e1f26343SJason M. Bills         sd_journal *journalTmp = nullptr;
1426e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1427e1f26343SJason M. Bills         if (ret < 0)
1428e1f26343SJason M. Bills         {
1429e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1430f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1431e1f26343SJason M. Bills             return;
1432e1f26343SJason M. Bills         }
1433e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1434e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1435e1f26343SJason M. Bills         journalTmp = nullptr;
1436e1f26343SJason M. Bills         // Go to the timestamp in the log and move to the entry at the index
1437af07e3f5SJason M. Bills         // tracking the unique ID
1438af07e3f5SJason M. Bills         std::string idStr;
1439af07e3f5SJason M. Bills         bool firstEntry = true;
1440e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
1441271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
1442e1f26343SJason M. Bills         {
1443e1f26343SJason M. Bills             sd_journal_next(journal.get());
1444af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1445af07e3f5SJason M. Bills             {
1446af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
1447af07e3f5SJason M. Bills                 return;
1448af07e3f5SJason M. Bills             }
1449af07e3f5SJason M. Bills             if (firstEntry)
1450af07e3f5SJason M. Bills             {
1451af07e3f5SJason M. Bills                 firstEntry = false;
1452af07e3f5SJason M. Bills             }
1453e1f26343SJason M. Bills         }
1454c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
1455af07e3f5SJason M. Bills         if (idStr != entryID)
1456c4bf6374SJason M. Bills         {
1457c4bf6374SJason M. Bills             messages::resourceMissingAtURI(asyncResp->res, entryID);
1458c4bf6374SJason M. Bills             return;
1459c4bf6374SJason M. Bills         }
1460c4bf6374SJason M. Bills 
1461c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
1462e1f26343SJason M. Bills                                        asyncResp->res.jsonValue) != 0)
1463e1f26343SJason M. Bills         {
1464f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1465e1f26343SJason M. Bills             return;
1466e1f26343SJason M. Bills         }
1467e1f26343SJason M. Bills     }
1468e1f26343SJason M. Bills };
1469e1f26343SJason M. Bills 
1470424c4176SJason M. Bills class CrashdumpService : public Node
1471e1f26343SJason M. Bills {
1472e1f26343SJason M. Bills   public:
1473e1f26343SJason M. Bills     template <typename CrowApp>
1474424c4176SJason M. Bills     CrashdumpService(CrowApp &app) :
1475424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
14761da66f75SEd Tanous     {
14771da66f75SEd Tanous         entityPrivileges = {
1478e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1479e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1480e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1481e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1482e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1483e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
14841da66f75SEd Tanous     }
14851da66f75SEd Tanous 
14861da66f75SEd Tanous   private:
14871da66f75SEd Tanous     /**
14881da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
14891da66f75SEd Tanous      */
14901da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14911da66f75SEd Tanous                const std::vector<std::string> &params) override
14921da66f75SEd Tanous     {
1493e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14941da66f75SEd Tanous         // Copy over the static data to include the entries added by SubRoute
14950f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1496424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
1497e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1498e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
1499e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1500c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
1501424c4176SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Service";
1502424c4176SJason M. Bills         asyncResp->res.jsonValue["Description"] = "Crashdump Service";
1503424c4176SJason M. Bills         asyncResp->res.jsonValue["Id"] = "Crashdump";
1504e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1505e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
1506cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1507cd50aa42SJason M. Bills             {"@odata.id",
1508424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
1509e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"] = {
15105b61b5e8SJason M. Bills             {"#LogService.ClearLog",
15115b61b5e8SJason M. Bills              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
15125b61b5e8SJason M. Bills                          "Actions/LogService.ClearLog"}}},
15131da66f75SEd Tanous             {"Oem",
1514424c4176SJason M. Bills              {{"#Crashdump.OnDemand",
1515424c4176SJason M. Bills                {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
1516424c4176SJason M. Bills                            "Actions/Oem/Crashdump.OnDemand"}}}}}};
15171da66f75SEd Tanous 
15181da66f75SEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_RAW_PECI
1519e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"]["Oem"].push_back(
1520424c4176SJason M. Bills             {"#Crashdump.SendRawPeci",
152108a4e4b5SAnthony Wilson              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
1522424c4176SJason M. Bills                          "Actions/Oem/Crashdump.SendRawPeci"}}});
15231da66f75SEd Tanous #endif
15241da66f75SEd Tanous     }
15251da66f75SEd Tanous };
15261da66f75SEd Tanous 
15275b61b5e8SJason M. Bills class CrashdumpClear : public Node
15285b61b5e8SJason M. Bills {
15295b61b5e8SJason M. Bills   public:
15305b61b5e8SJason M. Bills     CrashdumpClear(CrowApp &app) :
15315b61b5e8SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
15325b61b5e8SJason M. Bills                   "LogService.ClearLog/")
15335b61b5e8SJason M. Bills     {
15345b61b5e8SJason M. Bills         entityPrivileges = {
15355b61b5e8SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
15365b61b5e8SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
15375b61b5e8SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
15385b61b5e8SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
15395b61b5e8SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
15405b61b5e8SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
15415b61b5e8SJason M. Bills     }
15425b61b5e8SJason M. Bills 
15435b61b5e8SJason M. Bills   private:
15445b61b5e8SJason M. Bills     void doPost(crow::Response &res, const crow::Request &req,
15455b61b5e8SJason M. Bills                 const std::vector<std::string> &params) override
15465b61b5e8SJason M. Bills     {
15475b61b5e8SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15485b61b5e8SJason M. Bills 
15495b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
15505b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
15515b61b5e8SJason M. Bills                         const std::string &resp) {
15525b61b5e8SJason M. Bills                 if (ec)
15535b61b5e8SJason M. Bills                 {
15545b61b5e8SJason M. Bills                     messages::internalError(asyncResp->res);
15555b61b5e8SJason M. Bills                     return;
15565b61b5e8SJason M. Bills                 }
15575b61b5e8SJason M. Bills                 messages::success(asyncResp->res);
15585b61b5e8SJason M. Bills             },
15595b61b5e8SJason M. Bills             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
15605b61b5e8SJason M. Bills     }
15615b61b5e8SJason M. Bills };
15625b61b5e8SJason M. Bills 
1563e855dd28SJason M. Bills std::string getLogCreatedTime(const std::string &crashdump)
1564e855dd28SJason M. Bills {
1565e855dd28SJason M. Bills     nlohmann::json crashdumpJson =
1566e855dd28SJason M. Bills         nlohmann::json::parse(crashdump, nullptr, false);
1567e855dd28SJason M. Bills     if (crashdumpJson.is_discarded())
1568e855dd28SJason M. Bills     {
1569e855dd28SJason M. Bills         return std::string();
1570e855dd28SJason M. Bills     }
1571e855dd28SJason M. Bills 
1572e855dd28SJason M. Bills     nlohmann::json::const_iterator cdIt = crashdumpJson.find("crash_data");
1573e855dd28SJason M. Bills     if (cdIt == crashdumpJson.end())
1574e855dd28SJason M. Bills     {
1575e855dd28SJason M. Bills         return std::string();
1576e855dd28SJason M. Bills     }
1577e855dd28SJason M. Bills 
1578e855dd28SJason M. Bills     nlohmann::json::const_iterator siIt = cdIt->find("METADATA");
1579e855dd28SJason M. Bills     if (siIt == cdIt->end())
1580e855dd28SJason M. Bills     {
1581e855dd28SJason M. Bills         return std::string();
1582e855dd28SJason M. Bills     }
1583e855dd28SJason M. Bills 
1584e855dd28SJason M. Bills     nlohmann::json::const_iterator tsIt = siIt->find("timestamp");
1585e855dd28SJason M. Bills     if (tsIt == siIt->end())
1586e855dd28SJason M. Bills     {
1587e855dd28SJason M. Bills         return std::string();
1588e855dd28SJason M. Bills     }
1589e855dd28SJason M. Bills 
1590e855dd28SJason M. Bills     const std::string *logTime = tsIt->get_ptr<const std::string *>();
1591e855dd28SJason M. Bills     if (logTime == nullptr)
1592e855dd28SJason M. Bills     {
1593e855dd28SJason M. Bills         return std::string();
1594e855dd28SJason M. Bills     }
1595e855dd28SJason M. Bills 
1596e855dd28SJason M. Bills     std::string redfishDateTime = *logTime;
1597e855dd28SJason M. Bills     if (redfishDateTime.length() > 2)
1598e855dd28SJason M. Bills     {
1599e855dd28SJason M. Bills         redfishDateTime.insert(redfishDateTime.end() - 2, ':');
1600e855dd28SJason M. Bills     }
1601e855dd28SJason M. Bills 
1602e855dd28SJason M. Bills     return redfishDateTime;
1603e855dd28SJason M. Bills }
1604e855dd28SJason M. Bills 
1605e855dd28SJason M. Bills std::string getLogFileName(const std::string &logTime)
1606e855dd28SJason M. Bills {
1607e855dd28SJason M. Bills     // Set the crashdump file name to "crashdump_<logTime>.json" using the
1608e855dd28SJason M. Bills     // created time without the timezone info
1609e855dd28SJason M. Bills     std::string fileTime = logTime;
1610e855dd28SJason M. Bills     size_t plusPos = fileTime.rfind('+');
1611e855dd28SJason M. Bills     if (plusPos != std::string::npos)
1612e855dd28SJason M. Bills     {
1613e855dd28SJason M. Bills         fileTime.erase(plusPos);
1614e855dd28SJason M. Bills     }
1615e855dd28SJason M. Bills     return "crashdump_" + fileTime + ".json";
1616e855dd28SJason M. Bills }
1617e855dd28SJason M. Bills 
1618e855dd28SJason M. Bills static void logCrashdumpEntry(std::shared_ptr<AsyncResp> asyncResp,
1619e855dd28SJason M. Bills                               const std::string &logID,
1620e855dd28SJason M. Bills                               nlohmann::json &logEntryJson)
1621e855dd28SJason M. Bills {
1622e855dd28SJason M. Bills     auto getStoredLogCallback = [asyncResp, logID, &logEntryJson](
1623e855dd28SJason M. Bills                                     const boost::system::error_code ec,
1624e855dd28SJason M. Bills                                     const std::variant<std::string> &resp) {
1625e855dd28SJason M. Bills         if (ec)
1626e855dd28SJason M. Bills         {
1627e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
1628e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
1629e855dd28SJason M. Bills             return;
1630e855dd28SJason M. Bills         }
1631e855dd28SJason M. Bills         const std::string *log = std::get_if<std::string>(&resp);
1632e855dd28SJason M. Bills         if (log == nullptr)
1633e855dd28SJason M. Bills         {
1634e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
1635e855dd28SJason M. Bills             return;
1636e855dd28SJason M. Bills         }
1637e855dd28SJason M. Bills         std::string logTime = getLogCreatedTime(*log);
1638e855dd28SJason M. Bills         std::string fileName = getLogFileName(logTime);
1639e855dd28SJason M. Bills 
1640e855dd28SJason M. Bills         logEntryJson = {
1641e855dd28SJason M. Bills             {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1642e855dd28SJason M. Bills             {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
1643e855dd28SJason M. Bills             {"@odata.id",
1644e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
1645e855dd28SJason M. Bills                  logID},
1646e855dd28SJason M. Bills             {"Name", "CPU Crashdump"},
1647e855dd28SJason M. Bills             {"Id", logID},
1648e855dd28SJason M. Bills             {"EntryType", "Oem"},
1649e855dd28SJason M. Bills             {"OemRecordFormat", "Crashdump URI"},
1650e855dd28SJason M. Bills             {"Message",
1651e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
1652e855dd28SJason M. Bills                  logID + "/" + fileName},
1653e855dd28SJason M. Bills             {"Created", std::move(logTime)}};
1654e855dd28SJason M. Bills     };
1655e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
16565b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
16575b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
16585b61b5e8SJason M. Bills         "org.freedesktop.DBus.Properties", "Get", crashdumpInterface, "Log");
1659e855dd28SJason M. Bills }
1660e855dd28SJason M. Bills 
1661424c4176SJason M. Bills class CrashdumpEntryCollection : public Node
16621da66f75SEd Tanous {
16631da66f75SEd Tanous   public:
16641da66f75SEd Tanous     template <typename CrowApp>
1665424c4176SJason M. Bills     CrashdumpEntryCollection(CrowApp &app) :
1666424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
16671da66f75SEd Tanous     {
16681da66f75SEd Tanous         entityPrivileges = {
1669e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1670e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1671e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1672e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1673e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1674e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
16751da66f75SEd Tanous     }
16761da66f75SEd Tanous 
16771da66f75SEd Tanous   private:
16781da66f75SEd Tanous     /**
16791da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
16801da66f75SEd Tanous      */
16811da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16821da66f75SEd Tanous                const std::vector<std::string> &params) override
16831da66f75SEd Tanous     {
1684e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16851da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
16861da66f75SEd Tanous         // it has a duplicate entry for members
1687e1f26343SJason M. Bills         auto getLogEntriesCallback = [asyncResp](
1688e1f26343SJason M. Bills                                          const boost::system::error_code ec,
16891da66f75SEd Tanous                                          const std::vector<std::string> &resp) {
16901da66f75SEd Tanous             if (ec)
16911da66f75SEd Tanous             {
16921da66f75SEd Tanous                 if (ec.value() !=
16931da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
16941da66f75SEd Tanous                 {
16951da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
16961da66f75SEd Tanous                                      << ec.message();
1697f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
16981da66f75SEd Tanous                     return;
16991da66f75SEd Tanous                 }
17001da66f75SEd Tanous             }
1701e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
17021da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
17030f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1704424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
1705e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.context"] =
1706e855dd28SJason M. Bills                 "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
1707424c4176SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
1708e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
1709424c4176SJason M. Bills                 "Collection of Crashdump Entries";
1710e1f26343SJason M. Bills             nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
1711e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
1712e855dd28SJason M. Bills             std::vector<std::string> logIDs;
1713e855dd28SJason M. Bills             // Get the list of log entries and build up an empty array big
1714e855dd28SJason M. Bills             // enough to hold them
17151da66f75SEd Tanous             for (const std::string &objpath : resp)
17161da66f75SEd Tanous             {
1717e855dd28SJason M. Bills                 // Ignore the on-demand log
17185b61b5e8SJason M. Bills                 if (objpath.compare(crashdumpOnDemandPath) == 0)
17191da66f75SEd Tanous                 {
17201da66f75SEd Tanous                     continue;
17211da66f75SEd Tanous                 }
1722e855dd28SJason M. Bills 
1723e855dd28SJason M. Bills                 // Get the log ID
17244ed77cd5SEd Tanous                 std::size_t lastPos = objpath.rfind("/");
1725e855dd28SJason M. Bills                 if (lastPos == std::string::npos)
17261da66f75SEd Tanous                 {
1727e855dd28SJason M. Bills                     continue;
17281da66f75SEd Tanous                 }
1729e855dd28SJason M. Bills                 logIDs.emplace_back(objpath.substr(lastPos + 1));
1730e855dd28SJason M. Bills 
1731e855dd28SJason M. Bills                 // Add a space for the log entry to the array
1732e855dd28SJason M. Bills                 logEntryArray.push_back({});
1733e855dd28SJason M. Bills             }
1734e855dd28SJason M. Bills             // Now go through and set up async calls to fill in the entries
1735e855dd28SJason M. Bills             size_t index = 0;
1736e855dd28SJason M. Bills             for (const std::string &logID : logIDs)
1737e855dd28SJason M. Bills             {
1738e855dd28SJason M. Bills                 // Add the log entry to the array
1739e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
17401da66f75SEd Tanous             }
1741e1f26343SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
1742e1f26343SJason M. Bills                 logEntryArray.size();
17431da66f75SEd Tanous         };
17441da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
17451da66f75SEd Tanous             std::move(getLogEntriesCallback),
17461da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
17471da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
17481da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
17495b61b5e8SJason M. Bills             std::array<const char *, 1>{crashdumpInterface});
17501da66f75SEd Tanous     }
17511da66f75SEd Tanous };
17521da66f75SEd Tanous 
1753424c4176SJason M. Bills class CrashdumpEntry : public Node
17541da66f75SEd Tanous {
17551da66f75SEd Tanous   public:
1756424c4176SJason M. Bills     CrashdumpEntry(CrowApp &app) :
1757d53dd41fSJason M. Bills         Node(app,
1758424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/",
17591da66f75SEd Tanous              std::string())
17601da66f75SEd Tanous     {
17611da66f75SEd Tanous         entityPrivileges = {
1762e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1763e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1764e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1765e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1766e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1767e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
17681da66f75SEd Tanous     }
17691da66f75SEd Tanous 
17701da66f75SEd Tanous   private:
17711da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
17721da66f75SEd Tanous                const std::vector<std::string> &params) override
17731da66f75SEd Tanous     {
1774e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17751da66f75SEd Tanous         if (params.size() != 1)
17761da66f75SEd Tanous         {
1777f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
17781da66f75SEd Tanous             return;
17791da66f75SEd Tanous         }
1780e855dd28SJason M. Bills         const std::string &logID = params[0];
1781e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
1782e855dd28SJason M. Bills     }
1783e855dd28SJason M. Bills };
1784e855dd28SJason M. Bills 
1785e855dd28SJason M. Bills class CrashdumpFile : public Node
1786e855dd28SJason M. Bills {
1787e855dd28SJason M. Bills   public:
1788e855dd28SJason M. Bills     CrashdumpFile(CrowApp &app) :
1789e855dd28SJason M. Bills         Node(app,
1790e855dd28SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/"
1791e855dd28SJason M. Bills              "<str>/",
1792e855dd28SJason M. Bills              std::string(), std::string())
1793e855dd28SJason M. Bills     {
1794e855dd28SJason M. Bills         entityPrivileges = {
1795e855dd28SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1796e855dd28SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1797e855dd28SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1798e855dd28SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1799e855dd28SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1800e855dd28SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1801e855dd28SJason M. Bills     }
1802e855dd28SJason M. Bills 
1803e855dd28SJason M. Bills   private:
1804e855dd28SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1805e855dd28SJason M. Bills                const std::vector<std::string> &params) override
1806e855dd28SJason M. Bills     {
1807e855dd28SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1808e855dd28SJason M. Bills         if (params.size() != 2)
1809e855dd28SJason M. Bills         {
1810e855dd28SJason M. Bills             messages::internalError(asyncResp->res);
1811e855dd28SJason M. Bills             return;
1812e855dd28SJason M. Bills         }
1813e855dd28SJason M. Bills         const std::string &logID = params[0];
1814e855dd28SJason M. Bills         const std::string &fileName = params[1];
1815e855dd28SJason M. Bills 
1816e855dd28SJason M. Bills         auto getStoredLogCallback = [asyncResp, logID, fileName](
1817abf2add6SEd Tanous                                         const boost::system::error_code ec,
1818abf2add6SEd Tanous                                         const std::variant<std::string> &resp) {
18191da66f75SEd Tanous             if (ec)
18201da66f75SEd Tanous             {
1821abf2add6SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
1822f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
18231da66f75SEd Tanous                 return;
18241da66f75SEd Tanous             }
1825abf2add6SEd Tanous             const std::string *log = std::get_if<std::string>(&resp);
18261da66f75SEd Tanous             if (log == nullptr)
18271da66f75SEd Tanous             {
1828f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
18291da66f75SEd Tanous                 return;
18301da66f75SEd Tanous             }
1831e855dd28SJason M. Bills 
1832e855dd28SJason M. Bills             // Verify the file name parameter is correct
1833e855dd28SJason M. Bills             if (fileName != getLogFileName(getLogCreatedTime(*log)))
18341da66f75SEd Tanous             {
1835e855dd28SJason M. Bills                 messages::resourceMissingAtURI(asyncResp->res, fileName);
18361da66f75SEd Tanous                 return;
18371da66f75SEd Tanous             }
1838e855dd28SJason M. Bills 
1839e855dd28SJason M. Bills             // Configure this to be a file download when accessed from a browser
1840e855dd28SJason M. Bills             asyncResp->res.addHeader("Content-Disposition", "attachment");
1841e855dd28SJason M. Bills             asyncResp->res.body() = *log;
18421da66f75SEd Tanous         };
18431da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
18445b61b5e8SJason M. Bills             std::move(getStoredLogCallback), crashdumpObject,
18455b61b5e8SJason M. Bills             crashdumpPath + std::string("/") + logID,
18465b61b5e8SJason M. Bills             "org.freedesktop.DBus.Properties", "Get", crashdumpInterface,
1847424c4176SJason M. Bills             "Log");
18481da66f75SEd Tanous     }
18491da66f75SEd Tanous };
18501da66f75SEd Tanous 
1851424c4176SJason M. Bills class OnDemandCrashdump : public Node
18521da66f75SEd Tanous {
18531da66f75SEd Tanous   public:
1854424c4176SJason M. Bills     OnDemandCrashdump(CrowApp &app) :
1855424c4176SJason M. Bills         Node(app,
1856424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
1857424c4176SJason M. Bills              "Crashdump.OnDemand/")
18581da66f75SEd Tanous     {
18591da66f75SEd Tanous         entityPrivileges = {
1860e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1861e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1862e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1863e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1864e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1865e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
18661da66f75SEd Tanous     }
18671da66f75SEd Tanous 
18681da66f75SEd Tanous   private:
18691da66f75SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
18701da66f75SEd Tanous                 const std::vector<std::string> &params) override
18711da66f75SEd Tanous     {
1872e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
187348e4639eSJason M. Bills         static std::unique_ptr<sdbusplus::bus::match::match> onDemandLogMatcher;
18741da66f75SEd Tanous 
187548e4639eSJason M. Bills         // Only allow one OnDemand Log request at a time
187648e4639eSJason M. Bills         if (onDemandLogMatcher != nullptr)
18771da66f75SEd Tanous         {
1878e1f26343SJason M. Bills             asyncResp->res.addHeader("Retry-After", "30");
1879f12894f8SJason M. Bills             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
18801da66f75SEd Tanous             return;
18811da66f75SEd Tanous         }
18821da66f75SEd Tanous         // Make this static so it survives outside this method
1883271584abSEd Tanous         static boost::asio::steady_timer timeout(*req.ioService);
18841da66f75SEd Tanous 
1885271584abSEd Tanous         timeout.expires_after(std::chrono::seconds(30));
1886e1f26343SJason M. Bills         timeout.async_wait([asyncResp](const boost::system::error_code &ec) {
188748e4639eSJason M. Bills             onDemandLogMatcher = nullptr;
18881da66f75SEd Tanous             if (ec)
18891da66f75SEd Tanous             {
18901da66f75SEd Tanous                 // operation_aborted is expected if timer is canceled before
18911da66f75SEd Tanous                 // completion.
18921da66f75SEd Tanous                 if (ec != boost::asio::error::operation_aborted)
18931da66f75SEd Tanous                 {
18941da66f75SEd Tanous                     BMCWEB_LOG_ERROR << "Async_wait failed " << ec;
18951da66f75SEd Tanous                 }
18961da66f75SEd Tanous                 return;
18971da66f75SEd Tanous             }
189848e4639eSJason M. Bills             BMCWEB_LOG_ERROR << "Timed out waiting for on-demand log";
18991da66f75SEd Tanous 
1900f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
19011da66f75SEd Tanous         });
19021da66f75SEd Tanous 
19033cf8ea3cSJason M. Bills         auto onDemandLogMatcherCallback =
19043cf8ea3cSJason M. Bills             [asyncResp](sdbusplus::message::message &m) {
190548e4639eSJason M. Bills                 BMCWEB_LOG_DEBUG << "OnDemand log available match fired";
1906271584abSEd Tanous                 timeout.cancel();
1907271584abSEd Tanous 
19084ed77cd5SEd Tanous                 sdbusplus::message::object_path objPath;
19091da66f75SEd Tanous                 boost::container::flat_map<
1910abf2add6SEd Tanous                     std::string, boost::container::flat_map<
1911abf2add6SEd Tanous                                      std::string, std::variant<std::string>>>
19124ed77cd5SEd Tanous                     interfacesAdded;
19134ed77cd5SEd Tanous                 m.read(objPath, interfacesAdded);
1914abf2add6SEd Tanous                 const std::string *log = std::get_if<std::string>(
19155b61b5e8SJason M. Bills                     &interfacesAdded[crashdumpInterface]["Log"]);
19161da66f75SEd Tanous                 if (log == nullptr)
19171da66f75SEd Tanous                 {
1918f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
19193cf8ea3cSJason M. Bills                     // Careful with onDemandLogMatcher.  It is a unique_ptr to
19203cf8ea3cSJason M. Bills                     // the match object inside which this lambda is executing.
19213cf8ea3cSJason M. Bills                     // Once it is set to nullptr, the match object will be
19223cf8ea3cSJason M. Bills                     // destroyed and the lambda will lose its context, including
19233cf8ea3cSJason M. Bills                     // res, so it needs to be the last thing done.
192448e4639eSJason M. Bills                     onDemandLogMatcher = nullptr;
19251da66f75SEd Tanous                     return;
19261da66f75SEd Tanous                 }
19273cf8ea3cSJason M. Bills                 nlohmann::json crashdumpJson =
19283cf8ea3cSJason M. Bills                     nlohmann::json::parse(*log, nullptr, false);
19293cf8ea3cSJason M. Bills                 if (crashdumpJson.is_discarded())
19301da66f75SEd Tanous                 {
1931f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
19323cf8ea3cSJason M. Bills                     // Careful with onDemandLogMatcher.  It is a unique_ptr to
19333cf8ea3cSJason M. Bills                     // the match object inside which this lambda is executing.
19343cf8ea3cSJason M. Bills                     // Once it is set to nullptr, the match object will be
19353cf8ea3cSJason M. Bills                     // destroyed and the lambda will lose its context, including
19363cf8ea3cSJason M. Bills                     // res, so it needs to be the last thing done.
19373cf8ea3cSJason M. Bills                     onDemandLogMatcher = nullptr;
19383cf8ea3cSJason M. Bills                     return;
19393cf8ea3cSJason M. Bills                 }
19403cf8ea3cSJason M. Bills                 asyncResp->res.jsonValue = crashdumpJson;
194148e4639eSJason M. Bills                 // Careful with onDemandLogMatcher.  It is a unique_ptr to the
19421da66f75SEd Tanous                 // match object inside which this lambda is executing.  Once it
19431da66f75SEd Tanous                 // is set to nullptr, the match object will be destroyed and the
19441da66f75SEd Tanous                 // lambda will lose its context, including res, so it needs to
19451da66f75SEd Tanous                 // be the last thing done.
194648e4639eSJason M. Bills                 onDemandLogMatcher = nullptr;
19471da66f75SEd Tanous             };
194848e4639eSJason M. Bills         onDemandLogMatcher = std::make_unique<sdbusplus::bus::match::match>(
19491da66f75SEd Tanous             *crow::connections::systemBus,
19501da66f75SEd Tanous             sdbusplus::bus::match::rules::interfacesAdded() +
1951424c4176SJason M. Bills                 sdbusplus::bus::match::rules::argNpath(0,
19525b61b5e8SJason M. Bills                                                        crashdumpOnDemandPath),
195348e4639eSJason M. Bills             std::move(onDemandLogMatcherCallback));
19541da66f75SEd Tanous 
195548e4639eSJason M. Bills         auto generateonDemandLogCallback =
1956e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
19571da66f75SEd Tanous                         const std::string &resp) {
19581da66f75SEd Tanous                 if (ec)
19591da66f75SEd Tanous                 {
19601da66f75SEd Tanous                     if (ec.value() ==
19611da66f75SEd Tanous                         boost::system::errc::operation_not_supported)
19621da66f75SEd Tanous                     {
1963f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
19641da66f75SEd Tanous                     }
19651da66f75SEd Tanous                     else
19661da66f75SEd Tanous                     {
1967f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
19681da66f75SEd Tanous                     }
1969271584abSEd Tanous 
1970271584abSEd Tanous                     timeout.cancel();
197148e4639eSJason M. Bills                     onDemandLogMatcher = nullptr;
19721da66f75SEd Tanous                     return;
19731da66f75SEd Tanous                 }
19741da66f75SEd Tanous             };
19751da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
19765b61b5e8SJason M. Bills             std::move(generateonDemandLogCallback), crashdumpObject,
19775b61b5e8SJason M. Bills             crashdumpPath, crashdumpOnDemandInterface, "GenerateOnDemandLog");
19781da66f75SEd Tanous     }
19791da66f75SEd Tanous };
19801da66f75SEd Tanous 
1981e1f26343SJason M. Bills class SendRawPECI : public Node
19821da66f75SEd Tanous {
19831da66f75SEd Tanous   public:
1984e1f26343SJason M. Bills     SendRawPECI(CrowApp &app) :
1985424c4176SJason M. Bills         Node(app,
1986424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
1987424c4176SJason M. Bills              "Crashdump.SendRawPeci/")
19881da66f75SEd Tanous     {
19891da66f75SEd Tanous         entityPrivileges = {
19901da66f75SEd Tanous             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
19911da66f75SEd Tanous             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
19921da66f75SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
19931da66f75SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
19941da66f75SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
19951da66f75SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
19961da66f75SEd Tanous     }
19971da66f75SEd Tanous 
19981da66f75SEd Tanous   private:
19991da66f75SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
20001da66f75SEd Tanous                 const std::vector<std::string> &params) override
20011da66f75SEd Tanous     {
2002e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2003b1556427SEd Tanous         uint8_t clientAddress = 0;
2004b1556427SEd Tanous         uint8_t readLength = 0;
20051da66f75SEd Tanous         std::vector<uint8_t> peciCommand;
2006b1556427SEd Tanous         if (!json_util::readJson(req, res, "ClientAddress", clientAddress,
2007b1556427SEd Tanous                                  "ReadLength", readLength, "PECICommand",
2008b1556427SEd Tanous                                  peciCommand))
20091da66f75SEd Tanous         {
20101da66f75SEd Tanous             return;
20111da66f75SEd Tanous         }
2012b1556427SEd Tanous 
20131da66f75SEd Tanous         // Callback to return the Raw PECI response
2014e1f26343SJason M. Bills         auto sendRawPECICallback =
2015e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
20161da66f75SEd Tanous                         const std::vector<uint8_t> &resp) {
20171da66f75SEd Tanous                 if (ec)
20181da66f75SEd Tanous                 {
20191da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to send PECI command ec: "
20201da66f75SEd Tanous                                      << ec.message();
2021f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
20221da66f75SEd Tanous                     return;
20231da66f75SEd Tanous                 }
2024e1f26343SJason M. Bills                 asyncResp->res.jsonValue = {{"Name", "PECI Command Response"},
20251da66f75SEd Tanous                                             {"PECIResponse", resp}};
20261da66f75SEd Tanous             };
20271da66f75SEd Tanous         // Call the SendRawPECI command with the provided data
20281da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
20295b61b5e8SJason M. Bills             std::move(sendRawPECICallback), crashdumpObject, crashdumpPath,
20305b61b5e8SJason M. Bills             crashdumpRawPECIInterface, "SendRawPeci", clientAddress, readLength,
20314ed77cd5SEd Tanous             peciCommand);
20321da66f75SEd Tanous     }
20331da66f75SEd Tanous };
20341da66f75SEd Tanous 
2035cb92c03bSAndrew Geissler /**
2036cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2037cb92c03bSAndrew Geissler  */
2038cb92c03bSAndrew Geissler class DBusLogServiceActionsClear : public Node
2039cb92c03bSAndrew Geissler {
2040cb92c03bSAndrew Geissler   public:
2041cb92c03bSAndrew Geissler     DBusLogServiceActionsClear(CrowApp &app) :
2042cb92c03bSAndrew Geissler         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
20431f56a3a6STim Lee                   "LogService.ClearLog")
2044cb92c03bSAndrew Geissler     {
2045cb92c03bSAndrew Geissler         entityPrivileges = {
2046cb92c03bSAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
2047cb92c03bSAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
2048cb92c03bSAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2049cb92c03bSAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2050cb92c03bSAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2051cb92c03bSAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2052cb92c03bSAndrew Geissler     }
2053cb92c03bSAndrew Geissler 
2054cb92c03bSAndrew Geissler   private:
2055cb92c03bSAndrew Geissler     /**
2056cb92c03bSAndrew Geissler      * Function handles POST method request.
2057cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
2058cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
2059cb92c03bSAndrew Geissler      */
2060cb92c03bSAndrew Geissler     void doPost(crow::Response &res, const crow::Request &req,
2061cb92c03bSAndrew Geissler                 const std::vector<std::string> &params) override
2062cb92c03bSAndrew Geissler     {
2063cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
2064cb92c03bSAndrew Geissler 
2065cb92c03bSAndrew Geissler         auto asyncResp = std::make_shared<AsyncResp>(res);
2066cb92c03bSAndrew Geissler         // Process response from Logging service.
2067cb92c03bSAndrew Geissler         auto resp_handler = [asyncResp](const boost::system::error_code ec) {
2068cb92c03bSAndrew Geissler             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
2069cb92c03bSAndrew Geissler             if (ec)
2070cb92c03bSAndrew Geissler             {
2071cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
2072cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
2073cb92c03bSAndrew Geissler                 asyncResp->res.result(
2074cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
2075cb92c03bSAndrew Geissler                 return;
2076cb92c03bSAndrew Geissler             }
2077cb92c03bSAndrew Geissler 
2078cb92c03bSAndrew Geissler             asyncResp->res.result(boost::beast::http::status::no_content);
2079cb92c03bSAndrew Geissler         };
2080cb92c03bSAndrew Geissler 
2081cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
2082cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
2083cb92c03bSAndrew Geissler             resp_handler, "xyz.openbmc_project.Logging",
2084cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
2085cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2086cb92c03bSAndrew Geissler     }
2087cb92c03bSAndrew Geissler };
20881da66f75SEd Tanous } // namespace redfish
2089