xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision b6a61a5ec6aa3509c7dccad521e3850c71008a7a)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
181da66f75SEd Tanous #include "node.hpp"
194851d45dSJason M. Bills #include "registries.hpp"
204851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
214851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
221da66f75SEd Tanous 
23e1f26343SJason M. Bills #include <systemd/sd-journal.h>
24e1f26343SJason M. Bills 
254851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
264851d45dSJason M. Bills #include <boost/beast/core/span.hpp>
271da66f75SEd Tanous #include <boost/container/flat_map.hpp>
28cb92c03bSAndrew Geissler #include <error_messages.hpp>
294418c7f0SJames Feist #include <filesystem>
30cd225da8SJason M. Bills #include <string_view>
31abf2add6SEd Tanous #include <variant>
321da66f75SEd Tanous 
331da66f75SEd Tanous namespace redfish
341da66f75SEd Tanous {
351da66f75SEd Tanous 
36424c4176SJason M. Bills constexpr char const *CrashdumpObject = "com.intel.crashdump";
37424c4176SJason M. Bills constexpr char const *CrashdumpPath = "/com/intel/crashdump";
38424c4176SJason M. Bills constexpr char const *CrashdumpOnDemandPath = "/com/intel/crashdump/OnDemand";
39424c4176SJason M. Bills constexpr char const *CrashdumpInterface = "com.intel.crashdump";
40424c4176SJason M. Bills constexpr char const *CrashdumpOnDemandInterface =
41424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
42424c4176SJason M. Bills constexpr char const *CrashdumpRawPECIInterface =
43424c4176SJason M. Bills     "com.intel.crashdump.SendRawPeci";
441da66f75SEd Tanous 
454851d45dSJason M. Bills namespace message_registries
464851d45dSJason M. Bills {
474851d45dSJason M. Bills static const Message *getMessageFromRegistry(
484851d45dSJason M. Bills     const std::string &messageKey,
494851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
504851d45dSJason M. Bills {
514851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
524851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
534851d45dSJason M. Bills                      [&messageKey](const MessageEntry &messageEntry) {
544851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
554851d45dSJason M. Bills                                              messageKey.c_str());
564851d45dSJason M. Bills                      });
574851d45dSJason M. Bills     if (messageIt != registry.cend())
584851d45dSJason M. Bills     {
594851d45dSJason M. Bills         return &messageIt->second;
604851d45dSJason M. Bills     }
614851d45dSJason M. Bills 
624851d45dSJason M. Bills     return nullptr;
634851d45dSJason M. Bills }
644851d45dSJason M. Bills 
654851d45dSJason M. Bills static const Message *getMessage(const std::string_view &messageID)
664851d45dSJason M. Bills {
674851d45dSJason M. Bills     // Redfish MessageIds are in the form
684851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
694851d45dSJason M. Bills     // the right Message
704851d45dSJason M. Bills     std::vector<std::string> fields;
714851d45dSJason M. Bills     fields.reserve(4);
724851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
734851d45dSJason M. Bills     std::string &registryName = fields[0];
744851d45dSJason M. Bills     std::string &messageKey = fields[3];
754851d45dSJason M. Bills 
764851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
774851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
784851d45dSJason M. Bills     {
794851d45dSJason M. Bills         return getMessageFromRegistry(
804851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
814851d45dSJason M. Bills     }
824851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
834851d45dSJason M. Bills     {
844851d45dSJason M. Bills         return getMessageFromRegistry(
854851d45dSJason M. Bills             messageKey,
864851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
874851d45dSJason M. Bills     }
884851d45dSJason M. Bills     return nullptr;
894851d45dSJason M. Bills }
904851d45dSJason M. Bills } // namespace message_registries
914851d45dSJason M. Bills 
92f6150403SJames Feist namespace fs = std::filesystem;
931da66f75SEd Tanous 
94cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
95cb92c03bSAndrew Geissler     std::string,
96cb92c03bSAndrew Geissler     sdbusplus::message::variant<std::string, bool, uint8_t, int16_t, uint16_t,
97cb92c03bSAndrew Geissler                                 int32_t, uint32_t, int64_t, uint64_t, double>>;
98cb92c03bSAndrew Geissler 
99cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
100cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
101cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
102cb92c03bSAndrew Geissler 
103cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string &s)
104cb92c03bSAndrew Geissler {
105cb92c03bSAndrew Geissler     if (s == "xyz.openbmc_project.Logging.Entry.Level.Alert")
106cb92c03bSAndrew Geissler     {
107cb92c03bSAndrew Geissler         return "Critical";
108cb92c03bSAndrew Geissler     }
109cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Critical")
110cb92c03bSAndrew Geissler     {
111cb92c03bSAndrew Geissler         return "Critical";
112cb92c03bSAndrew Geissler     }
113cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Debug")
114cb92c03bSAndrew Geissler     {
115cb92c03bSAndrew Geissler         return "OK";
116cb92c03bSAndrew Geissler     }
117cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency")
118cb92c03bSAndrew Geissler     {
119cb92c03bSAndrew Geissler         return "Critical";
120cb92c03bSAndrew Geissler     }
121cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Error")
122cb92c03bSAndrew Geissler     {
123cb92c03bSAndrew Geissler         return "Critical";
124cb92c03bSAndrew Geissler     }
125cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Informational")
126cb92c03bSAndrew Geissler     {
127cb92c03bSAndrew Geissler         return "OK";
128cb92c03bSAndrew Geissler     }
129cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Notice")
130cb92c03bSAndrew Geissler     {
131cb92c03bSAndrew Geissler         return "OK";
132cb92c03bSAndrew Geissler     }
133cb92c03bSAndrew Geissler     else if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
134cb92c03bSAndrew Geissler     {
135cb92c03bSAndrew Geissler         return "Warning";
136cb92c03bSAndrew Geissler     }
137cb92c03bSAndrew Geissler     return "";
138cb92c03bSAndrew Geissler }
139cb92c03bSAndrew Geissler 
14016428a1aSJason M. Bills static int getJournalMetadata(sd_journal *journal,
14139e77504SEd Tanous                               const std::string_view &field,
14239e77504SEd Tanous                               std::string_view &contents)
14316428a1aSJason M. Bills {
14416428a1aSJason M. Bills     const char *data = nullptr;
14516428a1aSJason M. Bills     size_t length = 0;
14616428a1aSJason M. Bills     int ret = 0;
14716428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
148b01bf299SEd Tanous     ret = sd_journal_get_data(journal, field.data(), (const void **)&data,
149b01bf299SEd Tanous                               &length);
15016428a1aSJason M. Bills     if (ret < 0)
15116428a1aSJason M. Bills     {
15216428a1aSJason M. Bills         return ret;
15316428a1aSJason M. Bills     }
15439e77504SEd Tanous     contents = std::string_view(data, length);
15516428a1aSJason M. Bills     // Only use the content after the "=" character.
15616428a1aSJason M. Bills     contents.remove_prefix(std::min(contents.find("=") + 1, contents.size()));
15716428a1aSJason M. Bills     return ret;
15816428a1aSJason M. Bills }
15916428a1aSJason M. Bills 
16016428a1aSJason M. Bills static int getJournalMetadata(sd_journal *journal,
16139e77504SEd Tanous                               const std::string_view &field, const int &base,
16216428a1aSJason M. Bills                               int &contents)
16316428a1aSJason M. Bills {
16416428a1aSJason M. Bills     int ret = 0;
16539e77504SEd Tanous     std::string_view metadata;
16616428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16716428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16816428a1aSJason M. Bills     if (ret < 0)
16916428a1aSJason M. Bills     {
17016428a1aSJason M. Bills         return ret;
17116428a1aSJason M. Bills     }
172b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
17316428a1aSJason M. Bills     return ret;
17416428a1aSJason M. Bills }
17516428a1aSJason M. Bills 
17616428a1aSJason M. Bills static bool getEntryTimestamp(sd_journal *journal, std::string &entryTimestamp)
17716428a1aSJason M. Bills {
17816428a1aSJason M. Bills     int ret = 0;
17916428a1aSJason M. Bills     uint64_t timestamp = 0;
18016428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &timestamp);
18116428a1aSJason M. Bills     if (ret < 0)
18216428a1aSJason M. Bills     {
18316428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
18416428a1aSJason M. Bills                          << strerror(-ret);
18516428a1aSJason M. Bills         return false;
18616428a1aSJason M. Bills     }
18716428a1aSJason M. Bills     time_t t =
18816428a1aSJason M. Bills         static_cast<time_t>(timestamp / 1000 / 1000); // Convert from us to s
18916428a1aSJason M. Bills     struct tm *loctime = localtime(&t);
19016428a1aSJason M. Bills     char entryTime[64] = {};
19116428a1aSJason M. Bills     if (NULL != loctime)
19216428a1aSJason M. Bills     {
19316428a1aSJason M. Bills         strftime(entryTime, sizeof(entryTime), "%FT%T%z", loctime);
19416428a1aSJason M. Bills     }
19516428a1aSJason M. Bills     // Insert the ':' into the timezone
19639e77504SEd Tanous     std::string_view t1(entryTime);
19739e77504SEd Tanous     std::string_view t2(entryTime);
19816428a1aSJason M. Bills     if (t1.size() > 2 && t2.size() > 2)
19916428a1aSJason M. Bills     {
20016428a1aSJason M. Bills         t1.remove_suffix(2);
20116428a1aSJason M. Bills         t2.remove_prefix(t2.size() - 2);
20216428a1aSJason M. Bills     }
20339e77504SEd Tanous     entryTimestamp = std::string(t1) + ":" + std::string(t2);
20416428a1aSJason M. Bills     return true;
20516428a1aSJason M. Bills }
20616428a1aSJason M. Bills 
20716428a1aSJason M. Bills static bool getSkipParam(crow::Response &res, const crow::Request &req,
20816428a1aSJason M. Bills                          long &skip)
20916428a1aSJason M. Bills {
21016428a1aSJason M. Bills     char *skipParam = req.urlParams.get("$skip");
21116428a1aSJason M. Bills     if (skipParam != nullptr)
21216428a1aSJason M. Bills     {
21316428a1aSJason M. Bills         char *ptr = nullptr;
21416428a1aSJason M. Bills         skip = std::strtol(skipParam, &ptr, 10);
21516428a1aSJason M. Bills         if (*skipParam == '\0' || *ptr != '\0')
21616428a1aSJason M. Bills         {
21716428a1aSJason M. Bills 
21816428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(skipParam),
21916428a1aSJason M. Bills                                                    "$skip");
22016428a1aSJason M. Bills             return false;
22116428a1aSJason M. Bills         }
22216428a1aSJason M. Bills         if (skip < 0)
22316428a1aSJason M. Bills         {
22416428a1aSJason M. Bills 
22516428a1aSJason M. Bills             messages::queryParameterOutOfRange(res, std::to_string(skip),
22616428a1aSJason M. Bills                                                "$skip", "greater than 0");
22716428a1aSJason M. Bills             return false;
22816428a1aSJason M. Bills         }
22916428a1aSJason M. Bills     }
23016428a1aSJason M. Bills     return true;
23116428a1aSJason M. Bills }
23216428a1aSJason M. Bills 
23316428a1aSJason M. Bills static constexpr const long maxEntriesPerPage = 1000;
23416428a1aSJason M. Bills static bool getTopParam(crow::Response &res, const crow::Request &req,
23516428a1aSJason M. Bills                         long &top)
23616428a1aSJason M. Bills {
23716428a1aSJason M. Bills     char *topParam = req.urlParams.get("$top");
23816428a1aSJason M. Bills     if (topParam != nullptr)
23916428a1aSJason M. Bills     {
24016428a1aSJason M. Bills         char *ptr = nullptr;
24116428a1aSJason M. Bills         top = std::strtol(topParam, &ptr, 10);
24216428a1aSJason M. Bills         if (*topParam == '\0' || *ptr != '\0')
24316428a1aSJason M. Bills         {
24416428a1aSJason M. Bills             messages::queryParameterValueTypeError(res, std::string(topParam),
24516428a1aSJason M. Bills                                                    "$top");
24616428a1aSJason M. Bills             return false;
24716428a1aSJason M. Bills         }
24816428a1aSJason M. Bills         if (top < 1 || top > maxEntriesPerPage)
24916428a1aSJason M. Bills         {
25016428a1aSJason M. Bills 
25116428a1aSJason M. Bills             messages::queryParameterOutOfRange(
25216428a1aSJason M. Bills                 res, std::to_string(top), "$top",
25316428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
25416428a1aSJason M. Bills             return false;
25516428a1aSJason M. Bills         }
25616428a1aSJason M. Bills     }
25716428a1aSJason M. Bills     return true;
25816428a1aSJason M. Bills }
25916428a1aSJason M. Bills 
260e85d6b16SJason M. Bills static bool getUniqueEntryID(sd_journal *journal, std::string &entryID,
261e85d6b16SJason M. Bills                              const bool firstEntry = true)
26216428a1aSJason M. Bills {
26316428a1aSJason M. Bills     int ret = 0;
26416428a1aSJason M. Bills     static uint64_t prevTs = 0;
26516428a1aSJason M. Bills     static int index = 0;
266e85d6b16SJason M. Bills     if (firstEntry)
267e85d6b16SJason M. Bills     {
268e85d6b16SJason M. Bills         prevTs = 0;
269e85d6b16SJason M. Bills     }
270e85d6b16SJason M. Bills 
27116428a1aSJason M. Bills     // Get the entry timestamp
27216428a1aSJason M. Bills     uint64_t curTs = 0;
27316428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
27416428a1aSJason M. Bills     if (ret < 0)
27516428a1aSJason M. Bills     {
27616428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
27716428a1aSJason M. Bills                          << strerror(-ret);
27816428a1aSJason M. Bills         return false;
27916428a1aSJason M. Bills     }
28016428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
28116428a1aSJason M. Bills     if (curTs == prevTs)
28216428a1aSJason M. Bills     {
28316428a1aSJason M. Bills         index++;
28416428a1aSJason M. Bills     }
28516428a1aSJason M. Bills     else
28616428a1aSJason M. Bills     {
28716428a1aSJason M. Bills         // Otherwise, reset it
28816428a1aSJason M. Bills         index = 0;
28916428a1aSJason M. Bills     }
29016428a1aSJason M. Bills     // Save the timestamp
29116428a1aSJason M. Bills     prevTs = curTs;
29216428a1aSJason M. Bills 
29316428a1aSJason M. Bills     entryID = std::to_string(curTs);
29416428a1aSJason M. Bills     if (index > 0)
29516428a1aSJason M. Bills     {
29616428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
29716428a1aSJason M. Bills     }
29816428a1aSJason M. Bills     return true;
29916428a1aSJason M. Bills }
30016428a1aSJason M. Bills 
301e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string &logEntry, std::string &entryID,
302e85d6b16SJason M. Bills                              const bool firstEntry = true)
30395820184SJason M. Bills {
30495820184SJason M. Bills     static uint64_t prevTs = 0;
30595820184SJason M. Bills     static int index = 0;
306e85d6b16SJason M. Bills     if (firstEntry)
307e85d6b16SJason M. Bills     {
308e85d6b16SJason M. Bills         prevTs = 0;
309e85d6b16SJason M. Bills     }
310e85d6b16SJason M. Bills 
31195820184SJason M. Bills     // Get the entry timestamp
31295820184SJason M. Bills     uint64_t curTs = 0;
31395820184SJason M. Bills     std::tm timeStruct = {};
31495820184SJason M. Bills     std::istringstream entryStream(logEntry);
31595820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
31695820184SJason M. Bills     {
31795820184SJason M. Bills         curTs = std::mktime(&timeStruct);
31895820184SJason M. Bills     }
31995820184SJason M. Bills     // If the timestamp isn't unique, increment the index
32095820184SJason M. Bills     if (curTs == prevTs)
32195820184SJason M. Bills     {
32295820184SJason M. Bills         index++;
32395820184SJason M. Bills     }
32495820184SJason M. Bills     else
32595820184SJason M. Bills     {
32695820184SJason M. Bills         // Otherwise, reset it
32795820184SJason M. Bills         index = 0;
32895820184SJason M. Bills     }
32995820184SJason M. Bills     // Save the timestamp
33095820184SJason M. Bills     prevTs = curTs;
33195820184SJason M. Bills 
33295820184SJason M. Bills     entryID = std::to_string(curTs);
33395820184SJason M. Bills     if (index > 0)
33495820184SJason M. Bills     {
33595820184SJason M. Bills         entryID += "_" + std::to_string(index);
33695820184SJason M. Bills     }
33795820184SJason M. Bills     return true;
33895820184SJason M. Bills }
33995820184SJason M. Bills 
34016428a1aSJason M. Bills static bool getTimestampFromID(crow::Response &res, const std::string &entryID,
34116428a1aSJason M. Bills                                uint64_t &timestamp, uint16_t &index)
34216428a1aSJason M. Bills {
34316428a1aSJason M. Bills     if (entryID.empty())
34416428a1aSJason M. Bills     {
34516428a1aSJason M. Bills         return false;
34616428a1aSJason M. Bills     }
34716428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
34839e77504SEd Tanous     std::string_view tsStr(entryID);
34916428a1aSJason M. Bills 
35016428a1aSJason M. Bills     auto underscorePos = tsStr.find("_");
35116428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
35216428a1aSJason M. Bills     {
35316428a1aSJason M. Bills         // Timestamp has an index
35416428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
35539e77504SEd Tanous         std::string_view indexStr(entryID);
35616428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
35716428a1aSJason M. Bills         std::size_t pos;
35816428a1aSJason M. Bills         try
35916428a1aSJason M. Bills         {
36039e77504SEd Tanous             index = std::stoul(std::string(indexStr), &pos);
36116428a1aSJason M. Bills         }
362b01bf299SEd Tanous         catch (std::invalid_argument)
36316428a1aSJason M. Bills         {
36416428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
36516428a1aSJason M. Bills             return false;
36616428a1aSJason M. Bills         }
367b01bf299SEd Tanous         catch (std::out_of_range)
36816428a1aSJason M. Bills         {
36916428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
37016428a1aSJason M. Bills             return false;
37116428a1aSJason M. Bills         }
37216428a1aSJason M. Bills         if (pos != indexStr.size())
37316428a1aSJason M. Bills         {
37416428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
37516428a1aSJason M. Bills             return false;
37616428a1aSJason M. Bills         }
37716428a1aSJason M. Bills     }
37816428a1aSJason M. Bills     // Timestamp has no index
37916428a1aSJason M. Bills     std::size_t pos;
38016428a1aSJason M. Bills     try
38116428a1aSJason M. Bills     {
38239e77504SEd Tanous         timestamp = std::stoull(std::string(tsStr), &pos);
38316428a1aSJason M. Bills     }
384b01bf299SEd Tanous     catch (std::invalid_argument)
38516428a1aSJason M. Bills     {
38616428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
38716428a1aSJason M. Bills         return false;
38816428a1aSJason M. Bills     }
389b01bf299SEd Tanous     catch (std::out_of_range)
39016428a1aSJason M. Bills     {
39116428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
39216428a1aSJason M. Bills         return false;
39316428a1aSJason M. Bills     }
39416428a1aSJason M. Bills     if (pos != tsStr.size())
39516428a1aSJason M. Bills     {
39616428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
39716428a1aSJason M. Bills         return false;
39816428a1aSJason M. Bills     }
39916428a1aSJason M. Bills     return true;
40016428a1aSJason M. Bills }
40116428a1aSJason M. Bills 
40295820184SJason M. Bills static bool
40395820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path> &redfishLogFiles)
40495820184SJason M. Bills {
40595820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
40695820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
40795820184SJason M. Bills 
40895820184SJason M. Bills     // Loop through the directory looking for redfish log files
40995820184SJason M. Bills     for (const std::filesystem::directory_entry &dirEnt :
41095820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
41195820184SJason M. Bills     {
41295820184SJason M. Bills         // If we find a redfish log file, save the path
41395820184SJason M. Bills         std::string filename = dirEnt.path().filename();
41495820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
41595820184SJason M. Bills         {
41695820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
41795820184SJason M. Bills         }
41895820184SJason M. Bills     }
41995820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
42095820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
42195820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
42295820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
42395820184SJason M. Bills 
42495820184SJason M. Bills     return !redfishLogFiles.empty();
42595820184SJason M. Bills }
42695820184SJason M. Bills 
427c4bf6374SJason M. Bills class SystemLogServiceCollection : public Node
4281da66f75SEd Tanous {
4291da66f75SEd Tanous   public:
4301da66f75SEd Tanous     template <typename CrowApp>
431c4bf6374SJason M. Bills     SystemLogServiceCollection(CrowApp &app) :
432029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/")
433c4bf6374SJason M. Bills     {
434c4bf6374SJason M. Bills         entityPrivileges = {
435c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
436c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
437c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
438c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
439c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
440c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
441c4bf6374SJason M. Bills     }
442c4bf6374SJason M. Bills 
443c4bf6374SJason M. Bills   private:
444c4bf6374SJason M. Bills     /**
445c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
446c4bf6374SJason M. Bills      */
447c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
448c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
449c4bf6374SJason M. Bills     {
450c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
451c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
452c4bf6374SJason M. Bills         // it has a duplicate entry for members
453c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
454c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
455c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
456c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection";
457c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
458029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
459c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
460c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
461c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
462c4bf6374SJason M. Bills         nlohmann::json &logServiceArray = asyncResp->res.jsonValue["Members"];
463c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
464029573d4SEd Tanous         logServiceArray.push_back(
465029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/EventLog"}});
466d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
467d53dd41fSJason M. Bills         logServiceArray.push_back(
468cb92c03bSAndrew Geissler             {{"@odata.id",
469424c4176SJason M. Bills               "/redfish/v1/Systems/system/LogServices/Crashdump"}});
470d53dd41fSJason M. Bills #endif
471c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
472c4bf6374SJason M. Bills             logServiceArray.size();
473c4bf6374SJason M. Bills     }
474c4bf6374SJason M. Bills };
475c4bf6374SJason M. Bills 
476c4bf6374SJason M. Bills class EventLogService : public Node
477c4bf6374SJason M. Bills {
478c4bf6374SJason M. Bills   public:
479c4bf6374SJason M. Bills     template <typename CrowApp>
480c4bf6374SJason M. Bills     EventLogService(CrowApp &app) :
481029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
482c4bf6374SJason M. Bills     {
483c4bf6374SJason M. Bills         entityPrivileges = {
484c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
485c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
486c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
487c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
488c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
489c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
490c4bf6374SJason M. Bills     }
491c4bf6374SJason M. Bills 
492c4bf6374SJason M. Bills   private:
493c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
494c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
495c4bf6374SJason M. Bills     {
496c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
497c4bf6374SJason M. Bills 
498c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
499029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
500c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
501c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
502c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
503c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
504c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
505c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
506c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "Event Log";
507c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
508c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
509c4bf6374SJason M. Bills             {"@odata.id",
510029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
511e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
512e7d6c8b2SGunnar Mills 
513e7d6c8b2SGunnar Mills             {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
514e7d6c8b2SGunnar Mills                        "Actions/LogService.ClearLog"}};
515489640c6SJason M. Bills     }
516489640c6SJason M. Bills };
517489640c6SJason M. Bills 
518489640c6SJason M. Bills class EventLogClear : public Node
519489640c6SJason M. Bills {
520489640c6SJason M. Bills   public:
521489640c6SJason M. Bills     EventLogClear(CrowApp &app) :
522489640c6SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
523489640c6SJason M. Bills                   "LogService.ClearLog/")
524489640c6SJason M. Bills     {
525489640c6SJason M. Bills         entityPrivileges = {
526489640c6SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
527489640c6SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
528489640c6SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
529489640c6SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
530489640c6SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
531489640c6SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
532489640c6SJason M. Bills     }
533489640c6SJason M. Bills 
534489640c6SJason M. Bills   private:
535489640c6SJason M. Bills     void doPost(crow::Response &res, const crow::Request &req,
536489640c6SJason M. Bills                 const std::vector<std::string> &params) override
537489640c6SJason M. Bills     {
538489640c6SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
539489640c6SJason M. Bills 
540489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
541489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
542489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
543489640c6SJason M. Bills         {
544489640c6SJason M. Bills             for (const std::filesystem::path &file : redfishLogFiles)
545489640c6SJason M. Bills             {
546489640c6SJason M. Bills                 std::error_code ec;
547489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
548489640c6SJason M. Bills             }
549489640c6SJason M. Bills         }
550489640c6SJason M. Bills 
551489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
552489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
553489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
554489640c6SJason M. Bills                 if (ec)
555489640c6SJason M. Bills                 {
556489640c6SJason M. Bills                     BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
557489640c6SJason M. Bills                     messages::internalError(asyncResp->res);
558489640c6SJason M. Bills                     return;
559489640c6SJason M. Bills                 }
560489640c6SJason M. Bills 
561489640c6SJason M. Bills                 messages::success(asyncResp->res);
562489640c6SJason M. Bills             },
563489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
564489640c6SJason M. Bills             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
565489640c6SJason M. Bills             "replace");
566c4bf6374SJason M. Bills     }
567c4bf6374SJason M. Bills };
568c4bf6374SJason M. Bills 
56995820184SJason M. Bills static int fillEventLogEntryJson(const std::string &logEntryID,
57095820184SJason M. Bills                                  const std::string logEntry,
57195820184SJason M. Bills                                  nlohmann::json &logEntryJson)
572c4bf6374SJason M. Bills {
57395820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
574cd225da8SJason M. Bills     // First get the Timestamp
575cd225da8SJason M. Bills     size_t space = logEntry.find_first_of(" ");
576cd225da8SJason M. Bills     if (space == std::string::npos)
57795820184SJason M. Bills     {
57895820184SJason M. Bills         return 1;
57995820184SJason M. Bills     }
580cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
581cd225da8SJason M. Bills     // Then get the log contents
582cd225da8SJason M. Bills     size_t entryStart = logEntry.find_first_not_of(" ", space);
583cd225da8SJason M. Bills     if (entryStart == std::string::npos)
584cd225da8SJason M. Bills     {
585cd225da8SJason M. Bills         return 1;
586cd225da8SJason M. Bills     }
587cd225da8SJason M. Bills     std::string_view entry(logEntry);
588cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
589cd225da8SJason M. Bills     // Use split to separate the entry into its fields
590cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
591cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
592cd225da8SJason M. Bills                  boost::token_compress_on);
593cd225da8SJason M. Bills     // We need at least a MessageId to be valid
594cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
595cd225da8SJason M. Bills     {
596cd225da8SJason M. Bills         return 1;
597cd225da8SJason M. Bills     }
598cd225da8SJason M. Bills     std::string &messageID = logEntryFields[0];
59995820184SJason M. Bills 
6004851d45dSJason M. Bills     // Get the Message from the MessageRegistry
6014851d45dSJason M. Bills     const message_registries::Message *message =
6024851d45dSJason M. Bills         message_registries::getMessage(messageID);
603c4bf6374SJason M. Bills 
6044851d45dSJason M. Bills     std::string msg;
6054851d45dSJason M. Bills     std::string severity;
6064851d45dSJason M. Bills     if (message != nullptr)
607c4bf6374SJason M. Bills     {
6084851d45dSJason M. Bills         msg = message->message;
6094851d45dSJason M. Bills         severity = message->severity;
610c4bf6374SJason M. Bills     }
611c4bf6374SJason M. Bills 
61215a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
61315a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
61415a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
61515a86ff6SJason M. Bills     {
61615a86ff6SJason M. Bills         std::string &messageArgsStart = logEntryFields[1];
61715a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
61815a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
61915a86ff6SJason M. Bills         if (!messageArgsStart.empty())
62015a86ff6SJason M. Bills         {
62115a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
62215a86ff6SJason M. Bills         }
62315a86ff6SJason M. Bills 
62415a86ff6SJason M. Bills         messageArgs = boost::beast::span(&messageArgsStart, messageArgsSize);
625c4bf6374SJason M. Bills 
6264851d45dSJason M. Bills         // Fill the MessageArgs into the Message
62795820184SJason M. Bills         int i = 0;
62895820184SJason M. Bills         for (const std::string &messageArg : messageArgs)
6294851d45dSJason M. Bills         {
63095820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
6314851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
6324851d45dSJason M. Bills             if (argPos != std::string::npos)
6334851d45dSJason M. Bills             {
63495820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
6354851d45dSJason M. Bills             }
6364851d45dSJason M. Bills         }
63715a86ff6SJason M. Bills     }
6384851d45dSJason M. Bills 
63995820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
64095820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
64195820184SJason M. Bills     // between the '.' and the '+', so just remove them.
64295820184SJason M. Bills     std::size_t dot = timestamp.find_first_of(".");
64395820184SJason M. Bills     std::size_t plus = timestamp.find_first_of("+");
64495820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
645c4bf6374SJason M. Bills     {
64695820184SJason M. Bills         timestamp.erase(dot, plus - dot);
647c4bf6374SJason M. Bills     }
648c4bf6374SJason M. Bills 
649c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
65095820184SJason M. Bills     logEntryJson = {
651cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
652c4bf6374SJason M. Bills         {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
653029573d4SEd Tanous         {"@odata.id",
654897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
65595820184SJason M. Bills              logEntryID},
656c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
65795820184SJason M. Bills         {"Id", logEntryID},
65895820184SJason M. Bills         {"Message", std::move(msg)},
65995820184SJason M. Bills         {"MessageId", std::move(messageID)},
660c4bf6374SJason M. Bills         {"MessageArgs", std::move(messageArgs)},
661c4bf6374SJason M. Bills         {"EntryType", "Event"},
66295820184SJason M. Bills         {"Severity", std::move(severity)},
66395820184SJason M. Bills         {"Created", std::move(timestamp)}};
664c4bf6374SJason M. Bills     return 0;
665c4bf6374SJason M. Bills }
666c4bf6374SJason M. Bills 
66727062605SAnthony Wilson class JournalEventLogEntryCollection : public Node
668c4bf6374SJason M. Bills {
669c4bf6374SJason M. Bills   public:
670c4bf6374SJason M. Bills     template <typename CrowApp>
67127062605SAnthony Wilson     JournalEventLogEntryCollection(CrowApp &app) :
672029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
673c4bf6374SJason M. Bills     {
674c4bf6374SJason M. Bills         entityPrivileges = {
675c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
676c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
677c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
678c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
679c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
680c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
681c4bf6374SJason M. Bills     }
682c4bf6374SJason M. Bills 
683c4bf6374SJason M. Bills   private:
684c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
685c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
686c4bf6374SJason M. Bills     {
687c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
688c4bf6374SJason M. Bills         long skip = 0;
689c4bf6374SJason M. Bills         long top = maxEntriesPerPage; // Show max entries by default
690c4bf6374SJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
691c4bf6374SJason M. Bills         {
692c4bf6374SJason M. Bills             return;
693c4bf6374SJason M. Bills         }
694c4bf6374SJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
695c4bf6374SJason M. Bills         {
696c4bf6374SJason M. Bills             return;
697c4bf6374SJason M. Bills         }
698c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
699c4bf6374SJason M. Bills         // it has a duplicate entry for members
700c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
701c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
702c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
703c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
704c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
705029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
706c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
707c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
708c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
709cb92c03bSAndrew Geissler 
710c4bf6374SJason M. Bills         nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
711c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
71295820184SJason M. Bills         // Go through the log files and create a unique ID for each entry
71395820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
71495820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
715b01bf299SEd Tanous         uint64_t entryCount = 0;
716cd225da8SJason M. Bills         std::string logEntry;
71795820184SJason M. Bills 
71895820184SJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
719cd225da8SJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
720cd225da8SJason M. Bills              it++)
721c4bf6374SJason M. Bills         {
722cd225da8SJason M. Bills             std::ifstream logStream(*it);
72395820184SJason M. Bills             if (!logStream.is_open())
724c4bf6374SJason M. Bills             {
725c4bf6374SJason M. Bills                 continue;
726c4bf6374SJason M. Bills             }
727c4bf6374SJason M. Bills 
728e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
729e85d6b16SJason M. Bills             bool firstEntry = true;
73095820184SJason M. Bills             while (std::getline(logStream, logEntry))
73195820184SJason M. Bills             {
732c4bf6374SJason M. Bills                 entryCount++;
733c4bf6374SJason M. Bills                 // Handle paging using skip (number of entries to skip from the
734c4bf6374SJason M. Bills                 // start) and top (number of entries to display)
735c4bf6374SJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
736c4bf6374SJason M. Bills                 {
737c4bf6374SJason M. Bills                     continue;
738c4bf6374SJason M. Bills                 }
739c4bf6374SJason M. Bills 
740c4bf6374SJason M. Bills                 std::string idStr;
741e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
742c4bf6374SJason M. Bills                 {
743c4bf6374SJason M. Bills                     continue;
744c4bf6374SJason M. Bills                 }
745c4bf6374SJason M. Bills 
746e85d6b16SJason M. Bills                 if (firstEntry)
747e85d6b16SJason M. Bills                 {
748e85d6b16SJason M. Bills                     firstEntry = false;
749e85d6b16SJason M. Bills                 }
750e85d6b16SJason M. Bills 
751c4bf6374SJason M. Bills                 logEntryArray.push_back({});
752c4bf6374SJason M. Bills                 nlohmann::json &bmcLogEntry = logEntryArray.back();
75395820184SJason M. Bills                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
754c4bf6374SJason M. Bills                 {
755c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
756c4bf6374SJason M. Bills                     return;
757c4bf6374SJason M. Bills                 }
758c4bf6374SJason M. Bills             }
75995820184SJason M. Bills         }
760c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
761c4bf6374SJason M. Bills         if (skip + top < entryCount)
762c4bf6374SJason M. Bills         {
763c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
76495820184SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/"
76595820184SJason M. Bills                 "Entries?$skip=" +
766c4bf6374SJason M. Bills                 std::to_string(skip + top);
767c4bf6374SJason M. Bills         }
76808a4e4b5SAnthony Wilson     }
76908a4e4b5SAnthony Wilson };
77008a4e4b5SAnthony Wilson 
771897967deSJason M. Bills class JournalEventLogEntry : public Node
772897967deSJason M. Bills {
773897967deSJason M. Bills   public:
774897967deSJason M. Bills     JournalEventLogEntry(CrowApp &app) :
775897967deSJason M. Bills         Node(app,
776897967deSJason M. Bills              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
777897967deSJason M. Bills              std::string())
778897967deSJason M. Bills     {
779897967deSJason M. Bills         entityPrivileges = {
780897967deSJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
781897967deSJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
782897967deSJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
783897967deSJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
784897967deSJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
785897967deSJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
786897967deSJason M. Bills     }
787897967deSJason M. Bills 
788897967deSJason M. Bills   private:
789897967deSJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
790897967deSJason M. Bills                const std::vector<std::string> &params) override
791897967deSJason M. Bills     {
792897967deSJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
793897967deSJason M. Bills         if (params.size() != 1)
794897967deSJason M. Bills         {
795897967deSJason M. Bills             messages::internalError(asyncResp->res);
796897967deSJason M. Bills             return;
797897967deSJason M. Bills         }
798897967deSJason M. Bills         const std::string &targetID = params[0];
799897967deSJason M. Bills 
800897967deSJason M. Bills         // Go through the log files and check the unique ID for each entry to
801897967deSJason M. Bills         // find the target entry
802897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
803897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
804897967deSJason M. Bills         std::string logEntry;
805897967deSJason M. Bills 
806897967deSJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
807897967deSJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
808897967deSJason M. Bills              it++)
809897967deSJason M. Bills         {
810897967deSJason M. Bills             std::ifstream logStream(*it);
811897967deSJason M. Bills             if (!logStream.is_open())
812897967deSJason M. Bills             {
813897967deSJason M. Bills                 continue;
814897967deSJason M. Bills             }
815897967deSJason M. Bills 
816897967deSJason M. Bills             // Reset the unique ID on the first entry
817897967deSJason M. Bills             bool firstEntry = true;
818897967deSJason M. Bills             while (std::getline(logStream, logEntry))
819897967deSJason M. Bills             {
820897967deSJason M. Bills                 std::string idStr;
821897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
822897967deSJason M. Bills                 {
823897967deSJason M. Bills                     continue;
824897967deSJason M. Bills                 }
825897967deSJason M. Bills 
826897967deSJason M. Bills                 if (firstEntry)
827897967deSJason M. Bills                 {
828897967deSJason M. Bills                     firstEntry = false;
829897967deSJason M. Bills                 }
830897967deSJason M. Bills 
831897967deSJason M. Bills                 if (idStr == targetID)
832897967deSJason M. Bills                 {
833897967deSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry,
834897967deSJason M. Bills                                               asyncResp->res.jsonValue) != 0)
835897967deSJason M. Bills                     {
836897967deSJason M. Bills                         messages::internalError(asyncResp->res);
837897967deSJason M. Bills                         return;
838897967deSJason M. Bills                     }
839897967deSJason M. Bills                     return;
840897967deSJason M. Bills                 }
841897967deSJason M. Bills             }
842897967deSJason M. Bills         }
843897967deSJason M. Bills         // Requested ID was not found
844897967deSJason M. Bills         messages::resourceMissingAtURI(asyncResp->res, targetID);
845897967deSJason M. Bills     }
846897967deSJason M. Bills };
847897967deSJason M. Bills 
84808a4e4b5SAnthony Wilson class DBusEventLogEntryCollection : public Node
84908a4e4b5SAnthony Wilson {
85008a4e4b5SAnthony Wilson   public:
85108a4e4b5SAnthony Wilson     template <typename CrowApp>
85208a4e4b5SAnthony Wilson     DBusEventLogEntryCollection(CrowApp &app) :
85308a4e4b5SAnthony Wilson         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
85408a4e4b5SAnthony Wilson     {
85508a4e4b5SAnthony Wilson         entityPrivileges = {
85608a4e4b5SAnthony Wilson             {boost::beast::http::verb::get, {{"Login"}}},
85708a4e4b5SAnthony Wilson             {boost::beast::http::verb::head, {{"Login"}}},
85808a4e4b5SAnthony Wilson             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
85908a4e4b5SAnthony Wilson             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
86008a4e4b5SAnthony Wilson             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
86108a4e4b5SAnthony Wilson             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
86208a4e4b5SAnthony Wilson     }
86308a4e4b5SAnthony Wilson 
86408a4e4b5SAnthony Wilson   private:
86508a4e4b5SAnthony Wilson     void doGet(crow::Response &res, const crow::Request &req,
86608a4e4b5SAnthony Wilson                const std::vector<std::string> &params) override
86708a4e4b5SAnthony Wilson     {
86808a4e4b5SAnthony Wilson         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
86908a4e4b5SAnthony Wilson 
87008a4e4b5SAnthony Wilson         // Collections don't include the static data added by SubRoute because
87108a4e4b5SAnthony Wilson         // it has a duplicate entry for members
87208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
87308a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
87408a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.context"] =
87508a4e4b5SAnthony Wilson             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
87608a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
87708a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
87808a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
87908a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
88008a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
88108a4e4b5SAnthony Wilson 
882cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
883cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
884cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
885cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
886cb92c03bSAndrew Geissler                         GetManagedObjectsType &resp) {
887cb92c03bSAndrew Geissler                 if (ec)
888cb92c03bSAndrew Geissler                 {
889cb92c03bSAndrew Geissler                     // TODO Handle for specific error code
890cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
891cb92c03bSAndrew Geissler                         << "getLogEntriesIfaceData resp_handler got error "
892cb92c03bSAndrew Geissler                         << ec;
893cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
894cb92c03bSAndrew Geissler                     return;
895cb92c03bSAndrew Geissler                 }
896cb92c03bSAndrew Geissler                 nlohmann::json &entriesArray =
897cb92c03bSAndrew Geissler                     asyncResp->res.jsonValue["Members"];
898cb92c03bSAndrew Geissler                 entriesArray = nlohmann::json::array();
899cb92c03bSAndrew Geissler                 for (auto &objectPath : resp)
900cb92c03bSAndrew Geissler                 {
901cb92c03bSAndrew Geissler                     for (auto &interfaceMap : objectPath.second)
902cb92c03bSAndrew Geissler                     {
903cb92c03bSAndrew Geissler                         if (interfaceMap.first !=
904cb92c03bSAndrew Geissler                             "xyz.openbmc_project.Logging.Entry")
905cb92c03bSAndrew Geissler                         {
906cb92c03bSAndrew Geissler                             BMCWEB_LOG_DEBUG << "Bailing early on "
907cb92c03bSAndrew Geissler                                              << interfaceMap.first;
908cb92c03bSAndrew Geissler                             continue;
909cb92c03bSAndrew Geissler                         }
910cb92c03bSAndrew Geissler                         entriesArray.push_back({});
911cb92c03bSAndrew Geissler                         nlohmann::json &thisEntry = entriesArray.back();
912cb92c03bSAndrew Geissler                         uint32_t *id;
913cb92c03bSAndrew Geissler                         std::time_t timestamp;
914cb92c03bSAndrew Geissler                         std::string *severity, *message;
915cb92c03bSAndrew Geissler                         bool *resolved;
916cb92c03bSAndrew Geissler                         for (auto &propertyMap : interfaceMap.second)
917cb92c03bSAndrew Geissler                         {
918cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
919cb92c03bSAndrew Geissler                             {
920cb92c03bSAndrew Geissler                                 id = sdbusplus::message::variant_ns::get_if<
921cb92c03bSAndrew Geissler                                     uint32_t>(&propertyMap.second);
922cb92c03bSAndrew Geissler                                 if (id == nullptr)
923cb92c03bSAndrew Geissler                                 {
924cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
925cb92c03bSAndrew Geissler                                                               "Id");
926cb92c03bSAndrew Geissler                                 }
927cb92c03bSAndrew Geissler                             }
928cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
929cb92c03bSAndrew Geissler                             {
930cb92c03bSAndrew Geissler                                 const uint64_t *millisTimeStamp =
931cb92c03bSAndrew Geissler                                     std::get_if<uint64_t>(&propertyMap.second);
932cb92c03bSAndrew Geissler                                 if (millisTimeStamp == nullptr)
933cb92c03bSAndrew Geissler                                 {
934cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
935cb92c03bSAndrew Geissler                                                               "Timestamp");
936cb92c03bSAndrew Geissler                                 }
937cb92c03bSAndrew Geissler                                 // Retrieve Created property with format:
938cb92c03bSAndrew Geissler                                 // yyyy-mm-ddThh:mm:ss
939cb92c03bSAndrew Geissler                                 std::chrono::milliseconds chronoTimeStamp(
940cb92c03bSAndrew Geissler                                     *millisTimeStamp);
941cb92c03bSAndrew Geissler                                 timestamp =
942cb92c03bSAndrew Geissler                                     std::chrono::duration_cast<
943cb92c03bSAndrew Geissler                                         std::chrono::seconds>(chronoTimeStamp)
944cb92c03bSAndrew Geissler                                         .count();
945cb92c03bSAndrew Geissler                             }
946cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
947cb92c03bSAndrew Geissler                             {
948cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
949cb92c03bSAndrew Geissler                                     &propertyMap.second);
950cb92c03bSAndrew Geissler                                 if (severity == nullptr)
951cb92c03bSAndrew Geissler                                 {
952cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
953cb92c03bSAndrew Geissler                                                               "Severity");
954cb92c03bSAndrew Geissler                                 }
955cb92c03bSAndrew Geissler                             }
956cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
957cb92c03bSAndrew Geissler                             {
958cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
959cb92c03bSAndrew Geissler                                     &propertyMap.second);
960cb92c03bSAndrew Geissler                                 if (message == nullptr)
961cb92c03bSAndrew Geissler                                 {
962cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
963cb92c03bSAndrew Geissler                                                               "Message");
964cb92c03bSAndrew Geissler                                 }
965cb92c03bSAndrew Geissler                             }
966cb92c03bSAndrew Geissler                         }
967cb92c03bSAndrew Geissler                         thisEntry = {
968cb92c03bSAndrew Geissler                             {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
969cb92c03bSAndrew Geissler                             {"@odata.context", "/redfish/v1/"
970cb92c03bSAndrew Geissler                                                "$metadata#LogEntry.LogEntry"},
971cb92c03bSAndrew Geissler                             {"@odata.id",
972cb92c03bSAndrew Geissler                              "/redfish/v1/Systems/system/LogServices/EventLog/"
973cb92c03bSAndrew Geissler                              "Entries/" +
974cb92c03bSAndrew Geissler                                  std::to_string(*id)},
97527062605SAnthony Wilson                             {"Name", "System Event Log Entry"},
976cb92c03bSAndrew Geissler                             {"Id", std::to_string(*id)},
977cb92c03bSAndrew Geissler                             {"Message", *message},
978cb92c03bSAndrew Geissler                             {"EntryType", "Event"},
979cb92c03bSAndrew Geissler                             {"Severity",
980cb92c03bSAndrew Geissler                              translateSeverityDbusToRedfish(*severity)},
981cb92c03bSAndrew Geissler                             {"Created", crow::utility::getDateTime(timestamp)}};
982cb92c03bSAndrew Geissler                     }
983cb92c03bSAndrew Geissler                 }
984cb92c03bSAndrew Geissler                 std::sort(entriesArray.begin(), entriesArray.end(),
985cb92c03bSAndrew Geissler                           [](const nlohmann::json &left,
986cb92c03bSAndrew Geissler                              const nlohmann::json &right) {
987cb92c03bSAndrew Geissler                               return (left["Id"] <= right["Id"]);
988cb92c03bSAndrew Geissler                           });
989cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
990cb92c03bSAndrew Geissler                     entriesArray.size();
991cb92c03bSAndrew Geissler             },
992cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
993cb92c03bSAndrew Geissler             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
994c4bf6374SJason M. Bills     }
995c4bf6374SJason M. Bills };
996c4bf6374SJason M. Bills 
99708a4e4b5SAnthony Wilson class DBusEventLogEntry : public Node
998c4bf6374SJason M. Bills {
999c4bf6374SJason M. Bills   public:
100008a4e4b5SAnthony Wilson     DBusEventLogEntry(CrowApp &app) :
1001c4bf6374SJason M. Bills         Node(app,
1002029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
1003029573d4SEd Tanous              std::string())
1004c4bf6374SJason M. Bills     {
1005c4bf6374SJason M. Bills         entityPrivileges = {
1006c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1007c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1008c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1009c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1010c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1011c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1012c4bf6374SJason M. Bills     }
1013c4bf6374SJason M. Bills 
1014c4bf6374SJason M. Bills   private:
1015c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1016c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
1017c4bf6374SJason M. Bills     {
1018c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1019029573d4SEd Tanous         if (params.size() != 1)
1020c4bf6374SJason M. Bills         {
1021c4bf6374SJason M. Bills             messages::internalError(asyncResp->res);
1022c4bf6374SJason M. Bills             return;
1023c4bf6374SJason M. Bills         }
1024029573d4SEd Tanous         const std::string &entryID = params[0];
1025cb92c03bSAndrew Geissler 
1026cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1027cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1028cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1029cb92c03bSAndrew Geissler             [asyncResp, entryID](const boost::system::error_code ec,
1030cb92c03bSAndrew Geissler                                  GetManagedPropertyType &resp) {
1031cb92c03bSAndrew Geissler                 if (ec)
1032cb92c03bSAndrew Geissler                 {
1033cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
1034cb92c03bSAndrew Geissler                         << "EventLogEntry (DBus) resp_handler got error " << ec;
1035cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
1036cb92c03bSAndrew Geissler                     return;
1037cb92c03bSAndrew Geissler                 }
1038cb92c03bSAndrew Geissler                 uint32_t *id;
1039cb92c03bSAndrew Geissler                 std::time_t timestamp;
1040cb92c03bSAndrew Geissler                 std::string *severity, *message;
1041cb92c03bSAndrew Geissler                 bool *resolved;
1042cb92c03bSAndrew Geissler                 for (auto &propertyMap : resp)
1043cb92c03bSAndrew Geissler                 {
1044cb92c03bSAndrew Geissler                     if (propertyMap.first == "Id")
1045cb92c03bSAndrew Geissler                     {
1046cb92c03bSAndrew Geissler                         id = std::get_if<uint32_t>(&propertyMap.second);
1047cb92c03bSAndrew Geissler                         if (id == nullptr)
1048cb92c03bSAndrew Geissler                         {
1049cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res, "Id");
1050cb92c03bSAndrew Geissler                         }
1051cb92c03bSAndrew Geissler                     }
1052cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Timestamp")
1053cb92c03bSAndrew Geissler                     {
1054cb92c03bSAndrew Geissler                         const uint64_t *millisTimeStamp =
1055cb92c03bSAndrew Geissler                             std::get_if<uint64_t>(&propertyMap.second);
1056cb92c03bSAndrew Geissler                         if (millisTimeStamp == nullptr)
1057cb92c03bSAndrew Geissler                         {
1058cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1059cb92c03bSAndrew Geissler                                                       "Timestamp");
1060cb92c03bSAndrew Geissler                         }
1061cb92c03bSAndrew Geissler                         // Retrieve Created property with format:
1062cb92c03bSAndrew Geissler                         // yyyy-mm-ddThh:mm:ss
1063cb92c03bSAndrew Geissler                         std::chrono::milliseconds chronoTimeStamp(
1064cb92c03bSAndrew Geissler                             *millisTimeStamp);
1065cb92c03bSAndrew Geissler                         timestamp =
1066cb92c03bSAndrew Geissler                             std::chrono::duration_cast<std::chrono::seconds>(
1067cb92c03bSAndrew Geissler                                 chronoTimeStamp)
1068cb92c03bSAndrew Geissler                                 .count();
1069cb92c03bSAndrew Geissler                     }
1070cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Severity")
1071cb92c03bSAndrew Geissler                     {
1072cb92c03bSAndrew Geissler                         severity =
1073cb92c03bSAndrew Geissler                             std::get_if<std::string>(&propertyMap.second);
1074cb92c03bSAndrew Geissler                         if (severity == nullptr)
1075cb92c03bSAndrew Geissler                         {
1076cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1077cb92c03bSAndrew Geissler                                                       "Severity");
1078cb92c03bSAndrew Geissler                         }
1079cb92c03bSAndrew Geissler                     }
1080cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Message")
1081cb92c03bSAndrew Geissler                     {
1082cb92c03bSAndrew Geissler                         message = std::get_if<std::string>(&propertyMap.second);
1083cb92c03bSAndrew Geissler                         if (message == nullptr)
1084cb92c03bSAndrew Geissler                         {
1085cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
1086cb92c03bSAndrew Geissler                                                       "Message");
1087cb92c03bSAndrew Geissler                         }
1088cb92c03bSAndrew Geissler                     }
1089cb92c03bSAndrew Geissler                 }
1090cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue = {
1091cb92c03bSAndrew Geissler                     {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1092cb92c03bSAndrew Geissler                     {"@odata.context", "/redfish/v1/"
1093cb92c03bSAndrew Geissler                                        "$metadata#LogEntry.LogEntry"},
1094cb92c03bSAndrew Geissler                     {"@odata.id",
1095cb92c03bSAndrew Geissler                      "/redfish/v1/Systems/system/LogServices/EventLog/"
1096cb92c03bSAndrew Geissler                      "Entries/" +
1097cb92c03bSAndrew Geissler                          std::to_string(*id)},
109827062605SAnthony Wilson                     {"Name", "System Event Log Entry"},
1099cb92c03bSAndrew Geissler                     {"Id", std::to_string(*id)},
1100cb92c03bSAndrew Geissler                     {"Message", *message},
1101cb92c03bSAndrew Geissler                     {"EntryType", "Event"},
1102cb92c03bSAndrew Geissler                     {"Severity", translateSeverityDbusToRedfish(*severity)},
110308a4e4b5SAnthony Wilson                     {"Created", crow::utility::getDateTime(timestamp)}};
1104cb92c03bSAndrew Geissler             },
1105cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1106cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1107cb92c03bSAndrew Geissler             "org.freedesktop.DBus.Properties", "GetAll",
1108cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging.Entry");
1109c4bf6374SJason M. Bills     }
1110336e96c6SChicago Duan 
1111336e96c6SChicago Duan     void doDelete(crow::Response &res, const crow::Request &req,
1112336e96c6SChicago Duan                   const std::vector<std::string> &params) override
1113336e96c6SChicago Duan     {
1114336e96c6SChicago Duan 
1115336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1116336e96c6SChicago Duan 
1117336e96c6SChicago Duan         auto asyncResp = std::make_shared<AsyncResp>(res);
1118336e96c6SChicago Duan 
1119336e96c6SChicago Duan         if (params.size() != 1)
1120336e96c6SChicago Duan         {
1121336e96c6SChicago Duan             messages::internalError(asyncResp->res);
1122336e96c6SChicago Duan             return;
1123336e96c6SChicago Duan         }
1124336e96c6SChicago Duan         std::string entryID = params[0];
1125336e96c6SChicago Duan 
1126336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1127336e96c6SChicago Duan 
1128336e96c6SChicago Duan         // Process response from Logging service.
1129336e96c6SChicago Duan         auto respHandler = [asyncResp](const boost::system::error_code ec) {
1130336e96c6SChicago Duan             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1131336e96c6SChicago Duan             if (ec)
1132336e96c6SChicago Duan             {
1133336e96c6SChicago Duan                 // TODO Handle for specific error code
1134336e96c6SChicago Duan                 BMCWEB_LOG_ERROR
1135336e96c6SChicago Duan                     << "EventLogEntry (DBus) doDelete respHandler got error "
1136336e96c6SChicago Duan                     << ec;
1137336e96c6SChicago Duan                 asyncResp->res.result(
1138336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1139336e96c6SChicago Duan                 return;
1140336e96c6SChicago Duan             }
1141336e96c6SChicago Duan 
1142336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1143336e96c6SChicago Duan         };
1144336e96c6SChicago Duan 
1145336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1146336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1147336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1148336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1149336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
1150336e96c6SChicago Duan     }
1151c4bf6374SJason M. Bills };
1152c4bf6374SJason M. Bills 
1153c4bf6374SJason M. Bills class BMCLogServiceCollection : public Node
1154c4bf6374SJason M. Bills {
1155c4bf6374SJason M. Bills   public:
1156c4bf6374SJason M. Bills     template <typename CrowApp>
1157c4bf6374SJason M. Bills     BMCLogServiceCollection(CrowApp &app) :
11584ed77cd5SEd Tanous         Node(app, "/redfish/v1/Managers/bmc/LogServices/")
11591da66f75SEd Tanous     {
11601da66f75SEd Tanous         entityPrivileges = {
1161e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1162e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1163e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1164e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1165e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1166e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
11671da66f75SEd Tanous     }
11681da66f75SEd Tanous 
11691da66f75SEd Tanous   private:
11701da66f75SEd Tanous     /**
11711da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
11721da66f75SEd Tanous      */
11731da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
11741da66f75SEd Tanous                const std::vector<std::string> &params) override
11751da66f75SEd Tanous     {
1176e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
11771da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
11781da66f75SEd Tanous         // it has a duplicate entry for members
1179e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
11801da66f75SEd Tanous             "#LogServiceCollection.LogServiceCollection";
1181e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1182c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection";
1183e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1184e1f26343SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices";
1185e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1186e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
11871da66f75SEd Tanous             "Collection of LogServices for this Manager";
1188c4bf6374SJason M. Bills         nlohmann::json &logServiceArray = asyncResp->res.jsonValue["Members"];
1189c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
1190c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1191c4bf6374SJason M. Bills         logServiceArray.push_back(
119208a4e4b5SAnthony Wilson             {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1193c4bf6374SJason M. Bills #endif
1194e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1195c4bf6374SJason M. Bills             logServiceArray.size();
11961da66f75SEd Tanous     }
11971da66f75SEd Tanous };
11981da66f75SEd Tanous 
1199c4bf6374SJason M. Bills class BMCJournalLogService : public Node
12001da66f75SEd Tanous {
12011da66f75SEd Tanous   public:
12021da66f75SEd Tanous     template <typename CrowApp>
1203c4bf6374SJason M. Bills     BMCJournalLogService(CrowApp &app) :
1204c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1205e1f26343SJason M. Bills     {
1206e1f26343SJason M. Bills         entityPrivileges = {
1207e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1208e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1209e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1210e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1211e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1212e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1213e1f26343SJason M. Bills     }
1214e1f26343SJason M. Bills 
1215e1f26343SJason M. Bills   private:
1216e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1217e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1218e1f26343SJason M. Bills     {
1219e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1220e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1221e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
12220f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
12230f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
1224e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1225e1f26343SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
1226c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
1227c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
1228c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
1229e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1230cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1231cd50aa42SJason M. Bills             {"@odata.id",
1232086be238SEd Tanous              "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1233e1f26343SJason M. Bills     }
1234e1f26343SJason M. Bills };
1235e1f26343SJason M. Bills 
1236c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string &bmcJournalLogEntryID,
1237e1f26343SJason M. Bills                                       sd_journal *journal,
1238c4bf6374SJason M. Bills                                       nlohmann::json &bmcJournalLogEntryJson)
1239e1f26343SJason M. Bills {
1240e1f26343SJason M. Bills     // Get the Log Entry contents
1241e1f26343SJason M. Bills     int ret = 0;
1242e1f26343SJason M. Bills 
124339e77504SEd Tanous     std::string_view msg;
124416428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1245e1f26343SJason M. Bills     if (ret < 0)
1246e1f26343SJason M. Bills     {
1247e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1248e1f26343SJason M. Bills         return 1;
1249e1f26343SJason M. Bills     }
1250e1f26343SJason M. Bills 
1251e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1252e1f26343SJason M. Bills     int severity = 8; // Default to an invalid priority
125316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1254e1f26343SJason M. Bills     if (ret < 0)
1255e1f26343SJason M. Bills     {
1256e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1257e1f26343SJason M. Bills     }
1258e1f26343SJason M. Bills 
1259e1f26343SJason M. Bills     // Get the Created time from the timestamp
126016428a1aSJason M. Bills     std::string entryTimeStr;
126116428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
1262e1f26343SJason M. Bills     {
126316428a1aSJason M. Bills         return 1;
1264e1f26343SJason M. Bills     }
1265e1f26343SJason M. Bills 
1266e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
1267c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
1268cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1269e1f26343SJason M. Bills         {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
1270c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1271c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
1272e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
1273c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
127416428a1aSJason M. Bills         {"Message", msg},
1275e1f26343SJason M. Bills         {"EntryType", "Oem"},
1276e1f26343SJason M. Bills         {"Severity",
1277*b6a61a5eSJason M. Bills          severity <= 2 ? "Critical" : severity <= 4 ? "Warning" : "OK"},
1278086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
1279e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
1280e1f26343SJason M. Bills     return 0;
1281e1f26343SJason M. Bills }
1282e1f26343SJason M. Bills 
1283c4bf6374SJason M. Bills class BMCJournalLogEntryCollection : public Node
1284e1f26343SJason M. Bills {
1285e1f26343SJason M. Bills   public:
1286e1f26343SJason M. Bills     template <typename CrowApp>
1287c4bf6374SJason M. Bills     BMCJournalLogEntryCollection(CrowApp &app) :
1288c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
1289e1f26343SJason M. Bills     {
1290e1f26343SJason M. Bills         entityPrivileges = {
1291e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1292e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1293e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1294e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1295e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1296e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1297e1f26343SJason M. Bills     }
1298e1f26343SJason M. Bills 
1299e1f26343SJason M. Bills   private:
1300e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1301e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1302e1f26343SJason M. Bills     {
1303e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1304193ad2faSJason M. Bills         static constexpr const long maxEntriesPerPage = 1000;
1305193ad2faSJason M. Bills         long skip = 0;
1306193ad2faSJason M. Bills         long top = maxEntriesPerPage; // Show max entries by default
130716428a1aSJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
1308193ad2faSJason M. Bills         {
1309193ad2faSJason M. Bills             return;
1310193ad2faSJason M. Bills         }
131116428a1aSJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
1312193ad2faSJason M. Bills         {
1313193ad2faSJason M. Bills             return;
1314193ad2faSJason M. Bills         }
1315e1f26343SJason M. Bills         // Collections don't include the static data added by SubRoute because
1316e1f26343SJason M. Bills         // it has a duplicate entry for members
1317e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1318e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
13190f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
13200f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1321e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1322c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
1323e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1324c4bf6374SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1325e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1326e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1327e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
13280f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
13290f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/BmcLog/Entries";
1330e1f26343SJason M. Bills         nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
1331e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
1332e1f26343SJason M. Bills 
1333e1f26343SJason M. Bills         // Go through the journal and use the timestamp to create a unique ID
1334e1f26343SJason M. Bills         // for each entry
1335e1f26343SJason M. Bills         sd_journal *journalTmp = nullptr;
1336e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1337e1f26343SJason M. Bills         if (ret < 0)
1338e1f26343SJason M. Bills         {
1339e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1340f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1341e1f26343SJason M. Bills             return;
1342e1f26343SJason M. Bills         }
1343e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1344e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1345e1f26343SJason M. Bills         journalTmp = nullptr;
1346b01bf299SEd Tanous         uint64_t entryCount = 0;
1347e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
1348e85d6b16SJason M. Bills         bool firstEntry = true;
1349e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
1350e1f26343SJason M. Bills         {
1351193ad2faSJason M. Bills             entryCount++;
1352193ad2faSJason M. Bills             // Handle paging using skip (number of entries to skip from the
1353193ad2faSJason M. Bills             // start) and top (number of entries to display)
1354193ad2faSJason M. Bills             if (entryCount <= skip || entryCount > skip + top)
1355193ad2faSJason M. Bills             {
1356193ad2faSJason M. Bills                 continue;
1357193ad2faSJason M. Bills             }
1358193ad2faSJason M. Bills 
135916428a1aSJason M. Bills             std::string idStr;
1360e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1361e1f26343SJason M. Bills             {
1362e1f26343SJason M. Bills                 continue;
1363e1f26343SJason M. Bills             }
1364e1f26343SJason M. Bills 
1365e85d6b16SJason M. Bills             if (firstEntry)
1366e85d6b16SJason M. Bills             {
1367e85d6b16SJason M. Bills                 firstEntry = false;
1368e85d6b16SJason M. Bills             }
1369e85d6b16SJason M. Bills 
1370e1f26343SJason M. Bills             logEntryArray.push_back({});
1371c4bf6374SJason M. Bills             nlohmann::json &bmcJournalLogEntry = logEntryArray.back();
1372c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
1373c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
1374e1f26343SJason M. Bills             {
1375f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1376e1f26343SJason M. Bills                 return;
1377e1f26343SJason M. Bills             }
1378e1f26343SJason M. Bills         }
1379193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1380193ad2faSJason M. Bills         if (skip + top < entryCount)
1381193ad2faSJason M. Bills         {
1382193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
1383c4bf6374SJason M. Bills                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
1384193ad2faSJason M. Bills                 std::to_string(skip + top);
1385193ad2faSJason M. Bills         }
1386e1f26343SJason M. Bills     }
1387e1f26343SJason M. Bills };
1388e1f26343SJason M. Bills 
1389c4bf6374SJason M. Bills class BMCJournalLogEntry : public Node
1390e1f26343SJason M. Bills {
1391e1f26343SJason M. Bills   public:
1392c4bf6374SJason M. Bills     BMCJournalLogEntry(CrowApp &app) :
1393c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/",
1394e1f26343SJason M. Bills              std::string())
1395e1f26343SJason M. Bills     {
1396e1f26343SJason M. Bills         entityPrivileges = {
1397e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1398e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1399e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1400e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1401e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1402e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1403e1f26343SJason M. Bills     }
1404e1f26343SJason M. Bills 
1405e1f26343SJason M. Bills   private:
1406e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1407e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1408e1f26343SJason M. Bills     {
1409e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1410e1f26343SJason M. Bills         if (params.size() != 1)
1411e1f26343SJason M. Bills         {
1412f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1413e1f26343SJason M. Bills             return;
1414e1f26343SJason M. Bills         }
141516428a1aSJason M. Bills         const std::string &entryID = params[0];
1416e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
1417e1f26343SJason M. Bills         uint64_t ts = 0;
1418e1f26343SJason M. Bills         uint16_t index = 0;
141916428a1aSJason M. Bills         if (!getTimestampFromID(asyncResp->res, entryID, ts, index))
1420e1f26343SJason M. Bills         {
142116428a1aSJason M. Bills             return;
1422e1f26343SJason M. Bills         }
1423e1f26343SJason M. Bills 
1424e1f26343SJason M. Bills         sd_journal *journalTmp = nullptr;
1425e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1426e1f26343SJason M. Bills         if (ret < 0)
1427e1f26343SJason M. Bills         {
1428e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1429f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1430e1f26343SJason M. Bills             return;
1431e1f26343SJason M. Bills         }
1432e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1433e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1434e1f26343SJason M. Bills         journalTmp = nullptr;
1435e1f26343SJason M. Bills         // Go to the timestamp in the log and move to the entry at the index
1436af07e3f5SJason M. Bills         // tracking the unique ID
1437af07e3f5SJason M. Bills         std::string idStr;
1438af07e3f5SJason M. Bills         bool firstEntry = true;
1439e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
1440e1f26343SJason M. Bills         for (int i = 0; i <= index; i++)
1441e1f26343SJason M. Bills         {
1442e1f26343SJason M. Bills             sd_journal_next(journal.get());
1443af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1444af07e3f5SJason M. Bills             {
1445af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
1446af07e3f5SJason M. Bills                 return;
1447af07e3f5SJason M. Bills             }
1448af07e3f5SJason M. Bills             if (firstEntry)
1449af07e3f5SJason M. Bills             {
1450af07e3f5SJason M. Bills                 firstEntry = false;
1451af07e3f5SJason M. Bills             }
1452e1f26343SJason M. Bills         }
1453c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
1454af07e3f5SJason M. Bills         if (idStr != entryID)
1455c4bf6374SJason M. Bills         {
1456c4bf6374SJason M. Bills             messages::resourceMissingAtURI(asyncResp->res, entryID);
1457c4bf6374SJason M. Bills             return;
1458c4bf6374SJason M. Bills         }
1459c4bf6374SJason M. Bills 
1460c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
1461e1f26343SJason M. Bills                                        asyncResp->res.jsonValue) != 0)
1462e1f26343SJason M. Bills         {
1463f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1464e1f26343SJason M. Bills             return;
1465e1f26343SJason M. Bills         }
1466e1f26343SJason M. Bills     }
1467e1f26343SJason M. Bills };
1468e1f26343SJason M. Bills 
1469424c4176SJason M. Bills class CrashdumpService : public Node
1470e1f26343SJason M. Bills {
1471e1f26343SJason M. Bills   public:
1472e1f26343SJason M. Bills     template <typename CrowApp>
1473424c4176SJason M. Bills     CrashdumpService(CrowApp &app) :
1474424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
14751da66f75SEd Tanous     {
14761da66f75SEd Tanous         entityPrivileges = {
1477e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1478e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1479e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1480e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1481e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1482e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
14831da66f75SEd Tanous     }
14841da66f75SEd Tanous 
14851da66f75SEd Tanous   private:
14861da66f75SEd Tanous     /**
14871da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
14881da66f75SEd Tanous      */
14891da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14901da66f75SEd Tanous                const std::vector<std::string> &params) override
14911da66f75SEd Tanous     {
1492e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14931da66f75SEd Tanous         // Copy over the static data to include the entries added by SubRoute
14940f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1495424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
1496e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1497e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
1498e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1499c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
1500424c4176SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Service";
1501424c4176SJason M. Bills         asyncResp->res.jsonValue["Description"] = "Crashdump Service";
1502424c4176SJason M. Bills         asyncResp->res.jsonValue["Id"] = "Crashdump";
1503e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1504e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
1505cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1506cd50aa42SJason M. Bills             {"@odata.id",
1507424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
1508e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"] = {
15091da66f75SEd Tanous             {"Oem",
1510424c4176SJason M. Bills              {{"#Crashdump.OnDemand",
1511424c4176SJason M. Bills                {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
1512424c4176SJason M. Bills                            "Actions/Oem/Crashdump.OnDemand"}}}}}};
15131da66f75SEd Tanous 
15141da66f75SEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_RAW_PECI
1515e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"]["Oem"].push_back(
1516424c4176SJason M. Bills             {"#Crashdump.SendRawPeci",
151708a4e4b5SAnthony Wilson              {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
1518424c4176SJason M. Bills                          "Actions/Oem/Crashdump.SendRawPeci"}}});
15191da66f75SEd Tanous #endif
15201da66f75SEd Tanous     }
15211da66f75SEd Tanous };
15221da66f75SEd Tanous 
1523424c4176SJason M. Bills class CrashdumpEntryCollection : public Node
15241da66f75SEd Tanous {
15251da66f75SEd Tanous   public:
15261da66f75SEd Tanous     template <typename CrowApp>
1527424c4176SJason M. Bills     CrashdumpEntryCollection(CrowApp &app) :
1528424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
15291da66f75SEd Tanous     {
15301da66f75SEd Tanous         entityPrivileges = {
1531e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1532e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1533e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1534e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1535e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1536e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
15371da66f75SEd Tanous     }
15381da66f75SEd Tanous 
15391da66f75SEd Tanous   private:
15401da66f75SEd Tanous     /**
15411da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
15421da66f75SEd Tanous      */
15431da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
15441da66f75SEd Tanous                const std::vector<std::string> &params) override
15451da66f75SEd Tanous     {
1546e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15471da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
15481da66f75SEd Tanous         // it has a duplicate entry for members
1549e1f26343SJason M. Bills         auto getLogEntriesCallback = [asyncResp](
1550e1f26343SJason M. Bills                                          const boost::system::error_code ec,
15511da66f75SEd Tanous                                          const std::vector<std::string> &resp) {
15521da66f75SEd Tanous             if (ec)
15531da66f75SEd Tanous             {
15541da66f75SEd Tanous                 if (ec.value() !=
15551da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
15561da66f75SEd Tanous                 {
15571da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
15581da66f75SEd Tanous                                      << ec.message();
1559f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
15601da66f75SEd Tanous                     return;
15611da66f75SEd Tanous                 }
15621da66f75SEd Tanous             }
1563e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
15641da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
15650f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1566424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
1567e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.context"] =
1568d53dd41fSJason M. Bills                 "/redfish/v1/"
1569d53dd41fSJason M. Bills                 "$metadata#LogEntryCollection.LogEntryCollection";
1570424c4176SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
1571e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
1572424c4176SJason M. Bills                 "Collection of Crashdump Entries";
1573e1f26343SJason M. Bills             nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
1574e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
15751da66f75SEd Tanous             for (const std::string &objpath : resp)
15761da66f75SEd Tanous             {
157748e4639eSJason M. Bills                 // Don't list the on-demand log
1578424c4176SJason M. Bills                 if (objpath.compare(CrashdumpOnDemandPath) == 0)
15791da66f75SEd Tanous                 {
15801da66f75SEd Tanous                     continue;
15811da66f75SEd Tanous                 }
15824ed77cd5SEd Tanous                 std::size_t lastPos = objpath.rfind("/");
15834ed77cd5SEd Tanous                 if (lastPos != std::string::npos)
15841da66f75SEd Tanous                 {
1585e1f26343SJason M. Bills                     logEntryArray.push_back(
1586d53dd41fSJason M. Bills                         {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
1587424c4176SJason M. Bills                                        "Crashdump/Entries/" +
15884ed77cd5SEd Tanous                                            objpath.substr(lastPos + 1)}});
15891da66f75SEd Tanous                 }
15901da66f75SEd Tanous             }
1591e1f26343SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
1592e1f26343SJason M. Bills                 logEntryArray.size();
15931da66f75SEd Tanous         };
15941da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
15951da66f75SEd Tanous             std::move(getLogEntriesCallback),
15961da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
15971da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
15981da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
1599424c4176SJason M. Bills             std::array<const char *, 1>{CrashdumpInterface});
16001da66f75SEd Tanous     }
16011da66f75SEd Tanous };
16021da66f75SEd Tanous 
1603424c4176SJason M. Bills std::string getLogCreatedTime(const nlohmann::json &Crashdump)
16041da66f75SEd Tanous {
1605424c4176SJason M. Bills     nlohmann::json::const_iterator cdIt = Crashdump.find("crashlog_data");
1606424c4176SJason M. Bills     if (cdIt != Crashdump.end())
16071da66f75SEd Tanous     {
1608c4d00437SJason M. Bills         nlohmann::json::const_iterator siIt = cdIt->find("SYSTEM_INFO");
1609c4d00437SJason M. Bills         if (siIt != cdIt->end())
16101da66f75SEd Tanous         {
1611c4d00437SJason M. Bills             nlohmann::json::const_iterator tsIt = siIt->find("timestamp");
1612c4d00437SJason M. Bills             if (tsIt != siIt->end())
1613c4d00437SJason M. Bills             {
1614c4d00437SJason M. Bills                 const std::string *logTime =
1615c4d00437SJason M. Bills                     tsIt->get_ptr<const std::string *>();
16161da66f75SEd Tanous                 if (logTime != nullptr)
16171da66f75SEd Tanous                 {
16181da66f75SEd Tanous                     return *logTime;
16191da66f75SEd Tanous                 }
16201da66f75SEd Tanous             }
16211da66f75SEd Tanous         }
1622c4d00437SJason M. Bills     }
16231da66f75SEd Tanous     BMCWEB_LOG_DEBUG << "failed to find log timestamp";
16241da66f75SEd Tanous 
16251da66f75SEd Tanous     return std::string();
16261da66f75SEd Tanous }
16271da66f75SEd Tanous 
1628424c4176SJason M. Bills class CrashdumpEntry : public Node
16291da66f75SEd Tanous {
16301da66f75SEd Tanous   public:
1631424c4176SJason M. Bills     CrashdumpEntry(CrowApp &app) :
1632d53dd41fSJason M. Bills         Node(app,
1633424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/",
16341da66f75SEd Tanous              std::string())
16351da66f75SEd Tanous     {
16361da66f75SEd Tanous         entityPrivileges = {
1637e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1638e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1639e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1640e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1641e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1642e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
16431da66f75SEd Tanous     }
16441da66f75SEd Tanous 
16451da66f75SEd Tanous   private:
16461da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16471da66f75SEd Tanous                const std::vector<std::string> &params) override
16481da66f75SEd Tanous     {
1649e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16501da66f75SEd Tanous         if (params.size() != 1)
16511da66f75SEd Tanous         {
1652f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
16531da66f75SEd Tanous             return;
16541da66f75SEd Tanous         }
1655b01bf299SEd Tanous         const uint8_t logId = std::atoi(params[0].c_str());
1656abf2add6SEd Tanous         auto getStoredLogCallback = [asyncResp, logId](
1657abf2add6SEd Tanous                                         const boost::system::error_code ec,
1658abf2add6SEd Tanous                                         const std::variant<std::string> &resp) {
16591da66f75SEd Tanous             if (ec)
16601da66f75SEd Tanous             {
1661abf2add6SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
1662f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
16631da66f75SEd Tanous                 return;
16641da66f75SEd Tanous             }
1665abf2add6SEd Tanous             const std::string *log = std::get_if<std::string>(&resp);
16661da66f75SEd Tanous             if (log == nullptr)
16671da66f75SEd Tanous             {
1668f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
16691da66f75SEd Tanous                 return;
16701da66f75SEd Tanous             }
16711da66f75SEd Tanous             nlohmann::json j = nlohmann::json::parse(*log, nullptr, false);
16721da66f75SEd Tanous             if (j.is_discarded())
16731da66f75SEd Tanous             {
1674f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
16751da66f75SEd Tanous                 return;
16761da66f75SEd Tanous             }
16771da66f75SEd Tanous             std::string t = getLogCreatedTime(j);
1678e1f26343SJason M. Bills             asyncResp->res.jsonValue = {
1679cb92c03bSAndrew Geissler                 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1680abf2add6SEd Tanous                 {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
16811da66f75SEd Tanous                 {"@odata.id",
1682424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
16834ed77cd5SEd Tanous                      std::to_string(logId)},
1684424c4176SJason M. Bills                 {"Name", "CPU Crashdump"},
16854ed77cd5SEd Tanous                 {"Id", logId},
16861da66f75SEd Tanous                 {"EntryType", "Oem"},
1687424c4176SJason M. Bills                 {"OemRecordFormat", "Intel Crashdump"},
16881da66f75SEd Tanous                 {"Oem", {{"Intel", std::move(j)}}},
16891da66f75SEd Tanous                 {"Created", std::move(t)}};
16901da66f75SEd Tanous         };
16911da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1692424c4176SJason M. Bills             std::move(getStoredLogCallback), CrashdumpObject,
1693424c4176SJason M. Bills             CrashdumpPath + std::string("/") + std::to_string(logId),
1694424c4176SJason M. Bills             "org.freedesktop.DBus.Properties", "Get", CrashdumpInterface,
1695424c4176SJason M. Bills             "Log");
16961da66f75SEd Tanous     }
16971da66f75SEd Tanous };
16981da66f75SEd Tanous 
1699424c4176SJason M. Bills class OnDemandCrashdump : public Node
17001da66f75SEd Tanous {
17011da66f75SEd Tanous   public:
1702424c4176SJason M. Bills     OnDemandCrashdump(CrowApp &app) :
1703424c4176SJason M. Bills         Node(app,
1704424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
1705424c4176SJason M. Bills              "Crashdump.OnDemand/")
17061da66f75SEd Tanous     {
17071da66f75SEd Tanous         entityPrivileges = {
1708e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1709e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1710e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1711e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1712e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1713e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
17141da66f75SEd Tanous     }
17151da66f75SEd Tanous 
17161da66f75SEd Tanous   private:
17171da66f75SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
17181da66f75SEd Tanous                 const std::vector<std::string> &params) override
17191da66f75SEd Tanous     {
1720e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
172148e4639eSJason M. Bills         static std::unique_ptr<sdbusplus::bus::match::match> onDemandLogMatcher;
17221da66f75SEd Tanous 
172348e4639eSJason M. Bills         // Only allow one OnDemand Log request at a time
172448e4639eSJason M. Bills         if (onDemandLogMatcher != nullptr)
17251da66f75SEd Tanous         {
1726e1f26343SJason M. Bills             asyncResp->res.addHeader("Retry-After", "30");
1727f12894f8SJason M. Bills             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
17281da66f75SEd Tanous             return;
17291da66f75SEd Tanous         }
17301da66f75SEd Tanous         // Make this static so it survives outside this method
17311da66f75SEd Tanous         static boost::asio::deadline_timer timeout(*req.ioService);
17321da66f75SEd Tanous 
17331da66f75SEd Tanous         timeout.expires_from_now(boost::posix_time::seconds(30));
1734e1f26343SJason M. Bills         timeout.async_wait([asyncResp](const boost::system::error_code &ec) {
173548e4639eSJason M. Bills             onDemandLogMatcher = nullptr;
17361da66f75SEd Tanous             if (ec)
17371da66f75SEd Tanous             {
17381da66f75SEd Tanous                 // operation_aborted is expected if timer is canceled before
17391da66f75SEd Tanous                 // completion.
17401da66f75SEd Tanous                 if (ec != boost::asio::error::operation_aborted)
17411da66f75SEd Tanous                 {
17421da66f75SEd Tanous                     BMCWEB_LOG_ERROR << "Async_wait failed " << ec;
17431da66f75SEd Tanous                 }
17441da66f75SEd Tanous                 return;
17451da66f75SEd Tanous             }
174648e4639eSJason M. Bills             BMCWEB_LOG_ERROR << "Timed out waiting for on-demand log";
17471da66f75SEd Tanous 
1748f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
17491da66f75SEd Tanous         });
17501da66f75SEd Tanous 
175148e4639eSJason M. Bills         auto onDemandLogMatcherCallback = [asyncResp](
17521da66f75SEd Tanous                                               sdbusplus::message::message &m) {
175348e4639eSJason M. Bills             BMCWEB_LOG_DEBUG << "OnDemand log available match fired";
17541da66f75SEd Tanous             boost::system::error_code ec;
17551da66f75SEd Tanous             timeout.cancel(ec);
17561da66f75SEd Tanous             if (ec)
17571da66f75SEd Tanous             {
17581da66f75SEd Tanous                 BMCWEB_LOG_ERROR << "error canceling timer " << ec;
17591da66f75SEd Tanous             }
17604ed77cd5SEd Tanous             sdbusplus::message::object_path objPath;
17611da66f75SEd Tanous             boost::container::flat_map<
1762abf2add6SEd Tanous                 std::string, boost::container::flat_map<
1763abf2add6SEd Tanous                                  std::string, std::variant<std::string>>>
17644ed77cd5SEd Tanous                 interfacesAdded;
17654ed77cd5SEd Tanous             m.read(objPath, interfacesAdded);
1766abf2add6SEd Tanous             const std::string *log = std::get_if<std::string>(
1767424c4176SJason M. Bills                 &interfacesAdded[CrashdumpInterface]["Log"]);
17681da66f75SEd Tanous             if (log == nullptr)
17691da66f75SEd Tanous             {
1770f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
177148e4639eSJason M. Bills                 // Careful with onDemandLogMatcher.  It is a unique_ptr to the
17721da66f75SEd Tanous                 // match object inside which this lambda is executing.  Once it
17731da66f75SEd Tanous                 // is set to nullptr, the match object will be destroyed and the
17741da66f75SEd Tanous                 // lambda will lose its context, including res, so it needs to
17751da66f75SEd Tanous                 // be the last thing done.
177648e4639eSJason M. Bills                 onDemandLogMatcher = nullptr;
17771da66f75SEd Tanous                 return;
17781da66f75SEd Tanous             }
17791da66f75SEd Tanous             nlohmann::json j = nlohmann::json::parse(*log, nullptr, false);
17801da66f75SEd Tanous             if (j.is_discarded())
17811da66f75SEd Tanous             {
1782f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
178348e4639eSJason M. Bills                 // Careful with onDemandLogMatcher.  It is a unique_ptr to the
17841da66f75SEd Tanous                 // match object inside which this lambda is executing.  Once it
17851da66f75SEd Tanous                 // is set to nullptr, the match object will be destroyed and the
17861da66f75SEd Tanous                 // lambda will lose its context, including res, so it needs to
17871da66f75SEd Tanous                 // be the last thing done.
178848e4639eSJason M. Bills                 onDemandLogMatcher = nullptr;
17891da66f75SEd Tanous                 return;
17901da66f75SEd Tanous             }
17911da66f75SEd Tanous             std::string t = getLogCreatedTime(j);
1792e1f26343SJason M. Bills             asyncResp->res.jsonValue = {
1793cb92c03bSAndrew Geissler                 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
17941da66f75SEd Tanous                 {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
1795424c4176SJason M. Bills                 {"Name", "CPU Crashdump"},
17961da66f75SEd Tanous                 {"EntryType", "Oem"},
1797424c4176SJason M. Bills                 {"OemRecordFormat", "Intel Crashdump"},
17981da66f75SEd Tanous                 {"Oem", {{"Intel", std::move(j)}}},
17991da66f75SEd Tanous                 {"Created", std::move(t)}};
180048e4639eSJason M. Bills             // Careful with onDemandLogMatcher.  It is a unique_ptr to the
18011da66f75SEd Tanous             // match object inside which this lambda is executing.  Once it is
18021da66f75SEd Tanous             // set to nullptr, the match object will be destroyed and the lambda
18031da66f75SEd Tanous             // will lose its context, including res, so it needs to be the last
18041da66f75SEd Tanous             // thing done.
180548e4639eSJason M. Bills             onDemandLogMatcher = nullptr;
18061da66f75SEd Tanous         };
180748e4639eSJason M. Bills         onDemandLogMatcher = std::make_unique<sdbusplus::bus::match::match>(
18081da66f75SEd Tanous             *crow::connections::systemBus,
18091da66f75SEd Tanous             sdbusplus::bus::match::rules::interfacesAdded() +
1810424c4176SJason M. Bills                 sdbusplus::bus::match::rules::argNpath(0,
1811424c4176SJason M. Bills                                                        CrashdumpOnDemandPath),
181248e4639eSJason M. Bills             std::move(onDemandLogMatcherCallback));
18131da66f75SEd Tanous 
181448e4639eSJason M. Bills         auto generateonDemandLogCallback =
1815e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
18161da66f75SEd Tanous                         const std::string &resp) {
18171da66f75SEd Tanous                 if (ec)
18181da66f75SEd Tanous                 {
18191da66f75SEd Tanous                     if (ec.value() ==
18201da66f75SEd Tanous                         boost::system::errc::operation_not_supported)
18211da66f75SEd Tanous                     {
1822f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
18231da66f75SEd Tanous                     }
18241da66f75SEd Tanous                     else
18251da66f75SEd Tanous                     {
1826f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
18271da66f75SEd Tanous                     }
18281da66f75SEd Tanous                     boost::system::error_code timeoutec;
18291da66f75SEd Tanous                     timeout.cancel(timeoutec);
18301da66f75SEd Tanous                     if (timeoutec)
18311da66f75SEd Tanous                     {
18321da66f75SEd Tanous                         BMCWEB_LOG_ERROR << "error canceling timer "
18331da66f75SEd Tanous                                          << timeoutec;
18341da66f75SEd Tanous                     }
183548e4639eSJason M. Bills                     onDemandLogMatcher = nullptr;
18361da66f75SEd Tanous                     return;
18371da66f75SEd Tanous                 }
18381da66f75SEd Tanous             };
18391da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1840424c4176SJason M. Bills             std::move(generateonDemandLogCallback), CrashdumpObject,
1841424c4176SJason M. Bills             CrashdumpPath, CrashdumpOnDemandInterface, "GenerateOnDemandLog");
18421da66f75SEd Tanous     }
18431da66f75SEd Tanous };
18441da66f75SEd Tanous 
1845e1f26343SJason M. Bills class SendRawPECI : public Node
18461da66f75SEd Tanous {
18471da66f75SEd Tanous   public:
1848e1f26343SJason M. Bills     SendRawPECI(CrowApp &app) :
1849424c4176SJason M. Bills         Node(app,
1850424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
1851424c4176SJason M. Bills              "Crashdump.SendRawPeci/")
18521da66f75SEd Tanous     {
18531da66f75SEd Tanous         entityPrivileges = {
18541da66f75SEd Tanous             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
18551da66f75SEd Tanous             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
18561da66f75SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
18571da66f75SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
18581da66f75SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
18591da66f75SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
18601da66f75SEd Tanous     }
18611da66f75SEd Tanous 
18621da66f75SEd Tanous   private:
18631da66f75SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
18641da66f75SEd Tanous                 const std::vector<std::string> &params) override
18651da66f75SEd Tanous     {
1866e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1867b1556427SEd Tanous         uint8_t clientAddress = 0;
1868b1556427SEd Tanous         uint8_t readLength = 0;
18691da66f75SEd Tanous         std::vector<uint8_t> peciCommand;
1870b1556427SEd Tanous         if (!json_util::readJson(req, res, "ClientAddress", clientAddress,
1871b1556427SEd Tanous                                  "ReadLength", readLength, "PECICommand",
1872b1556427SEd Tanous                                  peciCommand))
18731da66f75SEd Tanous         {
18741da66f75SEd Tanous             return;
18751da66f75SEd Tanous         }
1876b1556427SEd Tanous 
18771da66f75SEd Tanous         // Callback to return the Raw PECI response
1878e1f26343SJason M. Bills         auto sendRawPECICallback =
1879e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
18801da66f75SEd Tanous                         const std::vector<uint8_t> &resp) {
18811da66f75SEd Tanous                 if (ec)
18821da66f75SEd Tanous                 {
18831da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to send PECI command ec: "
18841da66f75SEd Tanous                                      << ec.message();
1885f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18861da66f75SEd Tanous                     return;
18871da66f75SEd Tanous                 }
1888e1f26343SJason M. Bills                 asyncResp->res.jsonValue = {{"Name", "PECI Command Response"},
18891da66f75SEd Tanous                                             {"PECIResponse", resp}};
18901da66f75SEd Tanous             };
18911da66f75SEd Tanous         // Call the SendRawPECI command with the provided data
18921da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1893424c4176SJason M. Bills             std::move(sendRawPECICallback), CrashdumpObject, CrashdumpPath,
1894424c4176SJason M. Bills             CrashdumpRawPECIInterface, "SendRawPeci", clientAddress, readLength,
18954ed77cd5SEd Tanous             peciCommand);
18961da66f75SEd Tanous     }
18971da66f75SEd Tanous };
18981da66f75SEd Tanous 
1899cb92c03bSAndrew Geissler /**
1900cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
1901cb92c03bSAndrew Geissler  */
1902cb92c03bSAndrew Geissler class DBusLogServiceActionsClear : public Node
1903cb92c03bSAndrew Geissler {
1904cb92c03bSAndrew Geissler   public:
1905cb92c03bSAndrew Geissler     DBusLogServiceActionsClear(CrowApp &app) :
1906cb92c03bSAndrew Geissler         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1907cb92c03bSAndrew Geissler                   "LogService.Reset")
1908cb92c03bSAndrew Geissler     {
1909cb92c03bSAndrew Geissler         entityPrivileges = {
1910cb92c03bSAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
1911cb92c03bSAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
1912cb92c03bSAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1913cb92c03bSAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1914cb92c03bSAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1915cb92c03bSAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1916cb92c03bSAndrew Geissler     }
1917cb92c03bSAndrew Geissler 
1918cb92c03bSAndrew Geissler   private:
1919cb92c03bSAndrew Geissler     /**
1920cb92c03bSAndrew Geissler      * Function handles POST method request.
1921cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
1922cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
1923cb92c03bSAndrew Geissler      */
1924cb92c03bSAndrew Geissler     void doPost(crow::Response &res, const crow::Request &req,
1925cb92c03bSAndrew Geissler                 const std::vector<std::string> &params) override
1926cb92c03bSAndrew Geissler     {
1927cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
1928cb92c03bSAndrew Geissler 
1929cb92c03bSAndrew Geissler         auto asyncResp = std::make_shared<AsyncResp>(res);
1930cb92c03bSAndrew Geissler         // Process response from Logging service.
1931cb92c03bSAndrew Geissler         auto resp_handler = [asyncResp](const boost::system::error_code ec) {
1932cb92c03bSAndrew Geissler             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
1933cb92c03bSAndrew Geissler             if (ec)
1934cb92c03bSAndrew Geissler             {
1935cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1936cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
1937cb92c03bSAndrew Geissler                 asyncResp->res.result(
1938cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
1939cb92c03bSAndrew Geissler                 return;
1940cb92c03bSAndrew Geissler             }
1941cb92c03bSAndrew Geissler 
1942cb92c03bSAndrew Geissler             asyncResp->res.result(boost::beast::http::status::no_content);
1943cb92c03bSAndrew Geissler         };
1944cb92c03bSAndrew Geissler 
1945cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
1946cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1947cb92c03bSAndrew Geissler             resp_handler, "xyz.openbmc_project.Logging",
1948cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
1949cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
1950cb92c03bSAndrew Geissler     }
1951cb92c03bSAndrew Geissler };
19521da66f75SEd Tanous } // namespace redfish
1953