xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision cd225da87857c153e81f3abca6dfcc61c08272ea)
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>
30*cd225da8SJason 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 
26016428a1aSJason M. Bills static bool getUniqueEntryID(sd_journal *journal, std::string &entryID)
26116428a1aSJason M. Bills {
26216428a1aSJason M. Bills     int ret = 0;
26316428a1aSJason M. Bills     static uint64_t prevTs = 0;
26416428a1aSJason M. Bills     static int index = 0;
26516428a1aSJason M. Bills     // Get the entry timestamp
26616428a1aSJason M. Bills     uint64_t curTs = 0;
26716428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
26816428a1aSJason M. Bills     if (ret < 0)
26916428a1aSJason M. Bills     {
27016428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
27116428a1aSJason M. Bills                          << strerror(-ret);
27216428a1aSJason M. Bills         return false;
27316428a1aSJason M. Bills     }
27416428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
27516428a1aSJason M. Bills     if (curTs == prevTs)
27616428a1aSJason M. Bills     {
27716428a1aSJason M. Bills         index++;
27816428a1aSJason M. Bills     }
27916428a1aSJason M. Bills     else
28016428a1aSJason M. Bills     {
28116428a1aSJason M. Bills         // Otherwise, reset it
28216428a1aSJason M. Bills         index = 0;
28316428a1aSJason M. Bills     }
28416428a1aSJason M. Bills     // Save the timestamp
28516428a1aSJason M. Bills     prevTs = curTs;
28616428a1aSJason M. Bills 
28716428a1aSJason M. Bills     entryID = std::to_string(curTs);
28816428a1aSJason M. Bills     if (index > 0)
28916428a1aSJason M. Bills     {
29016428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
29116428a1aSJason M. Bills     }
29216428a1aSJason M. Bills     return true;
29316428a1aSJason M. Bills }
29416428a1aSJason M. Bills 
29595820184SJason M. Bills static bool getUniqueEntryID(const std::string &logEntry, std::string &entryID)
29695820184SJason M. Bills {
29795820184SJason M. Bills     static uint64_t prevTs = 0;
29895820184SJason M. Bills     static int index = 0;
29995820184SJason M. Bills     // Get the entry timestamp
30095820184SJason M. Bills     uint64_t curTs = 0;
30195820184SJason M. Bills     std::tm timeStruct = {};
30295820184SJason M. Bills     std::istringstream entryStream(logEntry);
30395820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
30495820184SJason M. Bills     {
30595820184SJason M. Bills         curTs = std::mktime(&timeStruct);
30695820184SJason M. Bills     }
30795820184SJason M. Bills     // If the timestamp isn't unique, increment the index
30895820184SJason M. Bills     if (curTs == prevTs)
30995820184SJason M. Bills     {
31095820184SJason M. Bills         index++;
31195820184SJason M. Bills     }
31295820184SJason M. Bills     else
31395820184SJason M. Bills     {
31495820184SJason M. Bills         // Otherwise, reset it
31595820184SJason M. Bills         index = 0;
31695820184SJason M. Bills     }
31795820184SJason M. Bills     // Save the timestamp
31895820184SJason M. Bills     prevTs = curTs;
31995820184SJason M. Bills 
32095820184SJason M. Bills     entryID = std::to_string(curTs);
32195820184SJason M. Bills     if (index > 0)
32295820184SJason M. Bills     {
32395820184SJason M. Bills         entryID += "_" + std::to_string(index);
32495820184SJason M. Bills     }
32595820184SJason M. Bills     return true;
32695820184SJason M. Bills }
32795820184SJason M. Bills 
32816428a1aSJason M. Bills static bool getTimestampFromID(crow::Response &res, const std::string &entryID,
32916428a1aSJason M. Bills                                uint64_t &timestamp, uint16_t &index)
33016428a1aSJason M. Bills {
33116428a1aSJason M. Bills     if (entryID.empty())
33216428a1aSJason M. Bills     {
33316428a1aSJason M. Bills         return false;
33416428a1aSJason M. Bills     }
33516428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
33639e77504SEd Tanous     std::string_view tsStr(entryID);
33716428a1aSJason M. Bills 
33816428a1aSJason M. Bills     auto underscorePos = tsStr.find("_");
33916428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
34016428a1aSJason M. Bills     {
34116428a1aSJason M. Bills         // Timestamp has an index
34216428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
34339e77504SEd Tanous         std::string_view indexStr(entryID);
34416428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
34516428a1aSJason M. Bills         std::size_t pos;
34616428a1aSJason M. Bills         try
34716428a1aSJason M. Bills         {
34839e77504SEd Tanous             index = std::stoul(std::string(indexStr), &pos);
34916428a1aSJason M. Bills         }
350b01bf299SEd Tanous         catch (std::invalid_argument)
35116428a1aSJason M. Bills         {
35216428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
35316428a1aSJason M. Bills             return false;
35416428a1aSJason M. Bills         }
355b01bf299SEd Tanous         catch (std::out_of_range)
35616428a1aSJason M. Bills         {
35716428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
35816428a1aSJason M. Bills             return false;
35916428a1aSJason M. Bills         }
36016428a1aSJason M. Bills         if (pos != indexStr.size())
36116428a1aSJason M. Bills         {
36216428a1aSJason M. Bills             messages::resourceMissingAtURI(res, entryID);
36316428a1aSJason M. Bills             return false;
36416428a1aSJason M. Bills         }
36516428a1aSJason M. Bills     }
36616428a1aSJason M. Bills     // Timestamp has no index
36716428a1aSJason M. Bills     std::size_t pos;
36816428a1aSJason M. Bills     try
36916428a1aSJason M. Bills     {
37039e77504SEd Tanous         timestamp = std::stoull(std::string(tsStr), &pos);
37116428a1aSJason M. Bills     }
372b01bf299SEd Tanous     catch (std::invalid_argument)
37316428a1aSJason M. Bills     {
37416428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
37516428a1aSJason M. Bills         return false;
37616428a1aSJason M. Bills     }
377b01bf299SEd Tanous     catch (std::out_of_range)
37816428a1aSJason M. Bills     {
37916428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
38016428a1aSJason M. Bills         return false;
38116428a1aSJason M. Bills     }
38216428a1aSJason M. Bills     if (pos != tsStr.size())
38316428a1aSJason M. Bills     {
38416428a1aSJason M. Bills         messages::resourceMissingAtURI(res, entryID);
38516428a1aSJason M. Bills         return false;
38616428a1aSJason M. Bills     }
38716428a1aSJason M. Bills     return true;
38816428a1aSJason M. Bills }
38916428a1aSJason M. Bills 
39095820184SJason M. Bills static bool
39195820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path> &redfishLogFiles)
39295820184SJason M. Bills {
39395820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
39495820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
39595820184SJason M. Bills 
39695820184SJason M. Bills     // Loop through the directory looking for redfish log files
39795820184SJason M. Bills     for (const std::filesystem::directory_entry &dirEnt :
39895820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
39995820184SJason M. Bills     {
40095820184SJason M. Bills         // If we find a redfish log file, save the path
40195820184SJason M. Bills         std::string filename = dirEnt.path().filename();
40295820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
40395820184SJason M. Bills         {
40495820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
40595820184SJason M. Bills         }
40695820184SJason M. Bills     }
40795820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
40895820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
40995820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
41095820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
41195820184SJason M. Bills 
41295820184SJason M. Bills     return !redfishLogFiles.empty();
41395820184SJason M. Bills }
41495820184SJason M. Bills 
415c4bf6374SJason M. Bills class SystemLogServiceCollection : public Node
4161da66f75SEd Tanous {
4171da66f75SEd Tanous   public:
4181da66f75SEd Tanous     template <typename CrowApp>
419c4bf6374SJason M. Bills     SystemLogServiceCollection(CrowApp &app) :
420029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/")
421c4bf6374SJason M. Bills     {
422c4bf6374SJason M. Bills         entityPrivileges = {
423c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
424c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
425c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
426c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
427c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
428c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
429c4bf6374SJason M. Bills     }
430c4bf6374SJason M. Bills 
431c4bf6374SJason M. Bills   private:
432c4bf6374SJason M. Bills     /**
433c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
434c4bf6374SJason M. Bills      */
435c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
436c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
437c4bf6374SJason M. Bills     {
438c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
439c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
440c4bf6374SJason M. Bills         // it has a duplicate entry for members
441c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
442c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
443c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
444c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection";
445c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
446029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
447c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
448c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
449c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
450c4bf6374SJason M. Bills         nlohmann::json &logServiceArray = asyncResp->res.jsonValue["Members"];
451c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
452029573d4SEd Tanous         logServiceArray.push_back(
453029573d4SEd Tanous             {{"@odata.id", "/redfish/v1/Systems/system/LogServices/EventLog"}});
454d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
455d53dd41fSJason M. Bills         logServiceArray.push_back(
456cb92c03bSAndrew Geissler             {{ "@odata.id",
457424c4176SJason M. Bills                "/redfish/v1/Systems/system/LogServices/Crashdump" }});
458d53dd41fSJason M. Bills #endif
459c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
460c4bf6374SJason M. Bills             logServiceArray.size();
461c4bf6374SJason M. Bills     }
462c4bf6374SJason M. Bills };
463c4bf6374SJason M. Bills 
464c4bf6374SJason M. Bills class EventLogService : public Node
465c4bf6374SJason M. Bills {
466c4bf6374SJason M. Bills   public:
467c4bf6374SJason M. Bills     template <typename CrowApp>
468c4bf6374SJason M. Bills     EventLogService(CrowApp &app) :
469029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
470c4bf6374SJason M. Bills     {
471c4bf6374SJason M. Bills         entityPrivileges = {
472c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
473c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
474c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
475c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
476c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
477c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
478c4bf6374SJason M. Bills     }
479c4bf6374SJason M. Bills 
480c4bf6374SJason M. Bills   private:
481c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
482c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
483c4bf6374SJason M. Bills     {
484c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
485c4bf6374SJason M. Bills 
486c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
487029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
488c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
489c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
490c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
491c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
492c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
493c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
494c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "Event Log";
495c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
496c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
497c4bf6374SJason M. Bills             {"@odata.id",
498029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
499c4bf6374SJason M. Bills     }
500c4bf6374SJason M. Bills };
501c4bf6374SJason M. Bills 
50295820184SJason M. Bills static int fillEventLogEntryJson(const std::string &logEntryID,
50395820184SJason M. Bills                                  const std::string logEntry,
50495820184SJason M. Bills                                  nlohmann::json &logEntryJson)
505c4bf6374SJason M. Bills {
50695820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
507*cd225da8SJason M. Bills     // First get the Timestamp
508*cd225da8SJason M. Bills     size_t space = logEntry.find_first_of(" ");
509*cd225da8SJason M. Bills     if (space == std::string::npos)
51095820184SJason M. Bills     {
51195820184SJason M. Bills         return 1;
51295820184SJason M. Bills     }
513*cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
514*cd225da8SJason M. Bills     // Then get the log contents
515*cd225da8SJason M. Bills     size_t entryStart = logEntry.find_first_not_of(" ", space);
516*cd225da8SJason M. Bills     if (entryStart == std::string::npos)
517*cd225da8SJason M. Bills     {
518*cd225da8SJason M. Bills         return 1;
519*cd225da8SJason M. Bills     }
520*cd225da8SJason M. Bills     std::string_view entry(logEntry);
521*cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
522*cd225da8SJason M. Bills     // Use split to separate the entry into its fields
523*cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
524*cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
525*cd225da8SJason M. Bills                  boost::token_compress_on);
526*cd225da8SJason M. Bills     // We need at least a MessageId to be valid
527*cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
528*cd225da8SJason M. Bills     {
529*cd225da8SJason M. Bills         return 1;
530*cd225da8SJason M. Bills     }
531*cd225da8SJason M. Bills     std::string &messageID = logEntryFields[0];
532*cd225da8SJason M. Bills     std::string &messageArgsStart = logEntryFields[1];
533*cd225da8SJason M. Bills     std::size_t messageArgsSize = logEntryFields.size() - 1;
53495820184SJason M. Bills 
5354851d45dSJason M. Bills     // Get the Message from the MessageRegistry
5364851d45dSJason M. Bills     const message_registries::Message *message =
5374851d45dSJason M. Bills         message_registries::getMessage(messageID);
538c4bf6374SJason M. Bills 
5394851d45dSJason M. Bills     std::string msg;
5404851d45dSJason M. Bills     std::string severity;
5414851d45dSJason M. Bills     if (message != nullptr)
542c4bf6374SJason M. Bills     {
5434851d45dSJason M. Bills         msg = message->message;
5444851d45dSJason M. Bills         severity = message->severity;
545c4bf6374SJason M. Bills     }
546c4bf6374SJason M. Bills 
54795820184SJason M. Bills     // Get the MessageArgs from the log
54895820184SJason M. Bills     boost::beast::span messageArgs(&messageArgsStart, messageArgsSize);
549c4bf6374SJason M. Bills 
5504851d45dSJason M. Bills     // Fill the MessageArgs into the Message
55195820184SJason M. Bills     int i = 0;
55295820184SJason M. Bills     for (const std::string &messageArg : messageArgs)
5534851d45dSJason M. Bills     {
55495820184SJason M. Bills         std::string argStr = "%" + std::to_string(++i);
5554851d45dSJason M. Bills         size_t argPos = msg.find(argStr);
5564851d45dSJason M. Bills         if (argPos != std::string::npos)
5574851d45dSJason M. Bills         {
55895820184SJason M. Bills             msg.replace(argPos, argStr.length(), messageArg);
5594851d45dSJason M. Bills         }
5604851d45dSJason M. Bills     }
5614851d45dSJason M. Bills 
56295820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
56395820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
56495820184SJason M. Bills     // between the '.' and the '+', so just remove them.
56595820184SJason M. Bills     std::size_t dot = timestamp.find_first_of(".");
56695820184SJason M. Bills     std::size_t plus = timestamp.find_first_of("+");
56795820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
568c4bf6374SJason M. Bills     {
56995820184SJason M. Bills         timestamp.erase(dot, plus - dot);
570c4bf6374SJason M. Bills     }
571c4bf6374SJason M. Bills 
572c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
57395820184SJason M. Bills     logEntryJson = {
574cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
575c4bf6374SJason M. Bills         {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
576029573d4SEd Tanous         {"@odata.id",
57795820184SJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/#" +
57895820184SJason M. Bills              logEntryID},
579c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
58095820184SJason M. Bills         {"Id", logEntryID},
58195820184SJason M. Bills         {"Message", std::move(msg)},
58295820184SJason M. Bills         {"MessageId", std::move(messageID)},
583c4bf6374SJason M. Bills         {"MessageArgs", std::move(messageArgs)},
584c4bf6374SJason M. Bills         {"EntryType", "Event"},
58595820184SJason M. Bills         {"Severity", std::move(severity)},
58695820184SJason M. Bills         {"Created", std::move(timestamp)}};
587c4bf6374SJason M. Bills     return 0;
588c4bf6374SJason M. Bills }
589c4bf6374SJason M. Bills 
590c4bf6374SJason M. Bills class EventLogEntryCollection : public Node
591c4bf6374SJason M. Bills {
592c4bf6374SJason M. Bills   public:
593c4bf6374SJason M. Bills     template <typename CrowApp>
594c4bf6374SJason M. Bills     EventLogEntryCollection(CrowApp &app) :
595029573d4SEd Tanous         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
596c4bf6374SJason M. Bills     {
597c4bf6374SJason M. Bills         entityPrivileges = {
598c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
599c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
600c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
601c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
602c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
603c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
604c4bf6374SJason M. Bills     }
605c4bf6374SJason M. Bills 
606c4bf6374SJason M. Bills   private:
607c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
608c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
609c4bf6374SJason M. Bills     {
610c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
611c4bf6374SJason M. Bills         long skip = 0;
612c4bf6374SJason M. Bills         long top = maxEntriesPerPage; // Show max entries by default
613c4bf6374SJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
614c4bf6374SJason M. Bills         {
615c4bf6374SJason M. Bills             return;
616c4bf6374SJason M. Bills         }
617c4bf6374SJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
618c4bf6374SJason M. Bills         {
619c4bf6374SJason M. Bills             return;
620c4bf6374SJason M. Bills         }
621c4bf6374SJason M. Bills         // Collections don't include the static data added by SubRoute because
622c4bf6374SJason M. Bills         // it has a duplicate entry for members
623c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
624c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
625c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
626c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
627c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
628029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
629c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
630c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
631c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
632cb92c03bSAndrew Geissler 
633cb92c03bSAndrew Geissler #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
634c4bf6374SJason M. Bills         nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
635c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
63695820184SJason M. Bills         // Go through the log files and create a unique ID for each entry
63795820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
63895820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
639b01bf299SEd Tanous         uint64_t entryCount = 0;
640*cd225da8SJason M. Bills         std::string logEntry;
64195820184SJason M. Bills 
64295820184SJason M. Bills         // Oldest logs are in the last file, so start there and loop backwards
643*cd225da8SJason M. Bills         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
644*cd225da8SJason M. Bills              it++)
645c4bf6374SJason M. Bills         {
646*cd225da8SJason M. Bills             std::ifstream logStream(*it);
64795820184SJason M. Bills             if (!logStream.is_open())
648c4bf6374SJason M. Bills             {
649c4bf6374SJason M. Bills                 continue;
650c4bf6374SJason M. Bills             }
651c4bf6374SJason M. Bills 
65295820184SJason M. Bills             while (std::getline(logStream, logEntry))
65395820184SJason M. Bills             {
654c4bf6374SJason M. Bills                 entryCount++;
655c4bf6374SJason M. Bills                 // Handle paging using skip (number of entries to skip from the
656c4bf6374SJason M. Bills                 // start) and top (number of entries to display)
657c4bf6374SJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
658c4bf6374SJason M. Bills                 {
659c4bf6374SJason M. Bills                     continue;
660c4bf6374SJason M. Bills                 }
661c4bf6374SJason M. Bills 
662c4bf6374SJason M. Bills                 std::string idStr;
66395820184SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr))
664c4bf6374SJason M. Bills                 {
665c4bf6374SJason M. Bills                     continue;
666c4bf6374SJason M. Bills                 }
667c4bf6374SJason M. Bills 
668c4bf6374SJason M. Bills                 logEntryArray.push_back({});
669c4bf6374SJason M. Bills                 nlohmann::json &bmcLogEntry = logEntryArray.back();
67095820184SJason M. Bills                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
671c4bf6374SJason M. Bills                 {
672c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
673c4bf6374SJason M. Bills                     return;
674c4bf6374SJason M. Bills                 }
675c4bf6374SJason M. Bills             }
67695820184SJason M. Bills         }
677c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
678c4bf6374SJason M. Bills         if (skip + top < entryCount)
679c4bf6374SJason M. Bills         {
680c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
68195820184SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/"
68295820184SJason M. Bills                 "Entries?$skip=" +
683c4bf6374SJason M. Bills                 std::to_string(skip + top);
684c4bf6374SJason M. Bills         }
685cb92c03bSAndrew Geissler #else
686cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
687cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
688cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
689cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
690cb92c03bSAndrew Geissler                         GetManagedObjectsType &resp) {
691cb92c03bSAndrew Geissler                 if (ec)
692cb92c03bSAndrew Geissler                 {
693cb92c03bSAndrew Geissler                     // TODO Handle for specific error code
694cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
695cb92c03bSAndrew Geissler                         << "getLogEntriesIfaceData resp_handler got error "
696cb92c03bSAndrew Geissler                         << ec;
697cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
698cb92c03bSAndrew Geissler                     return;
699cb92c03bSAndrew Geissler                 }
700cb92c03bSAndrew Geissler                 nlohmann::json &entriesArray =
701cb92c03bSAndrew Geissler                     asyncResp->res.jsonValue["Members"];
702cb92c03bSAndrew Geissler                 entriesArray = nlohmann::json::array();
703cb92c03bSAndrew Geissler                 for (auto &objectPath : resp)
704cb92c03bSAndrew Geissler                 {
705cb92c03bSAndrew Geissler                     for (auto &interfaceMap : objectPath.second)
706cb92c03bSAndrew Geissler                     {
707cb92c03bSAndrew Geissler                         if (interfaceMap.first !=
708cb92c03bSAndrew Geissler                             "xyz.openbmc_project.Logging.Entry")
709cb92c03bSAndrew Geissler                         {
710cb92c03bSAndrew Geissler                             BMCWEB_LOG_DEBUG << "Bailing early on "
711cb92c03bSAndrew Geissler                                              << interfaceMap.first;
712cb92c03bSAndrew Geissler                             continue;
713cb92c03bSAndrew Geissler                         }
714cb92c03bSAndrew Geissler                         entriesArray.push_back({});
715cb92c03bSAndrew Geissler                         nlohmann::json &thisEntry = entriesArray.back();
716cb92c03bSAndrew Geissler                         uint32_t *id;
717cb92c03bSAndrew Geissler                         std::time_t timestamp;
718cb92c03bSAndrew Geissler                         std::string *severity, *message;
719cb92c03bSAndrew Geissler                         bool *resolved;
720cb92c03bSAndrew Geissler                         for (auto &propertyMap : interfaceMap.second)
721cb92c03bSAndrew Geissler                         {
722cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
723cb92c03bSAndrew Geissler                             {
724cb92c03bSAndrew Geissler                                 id = sdbusplus::message::variant_ns::get_if<
725cb92c03bSAndrew Geissler                                     uint32_t>(&propertyMap.second);
726cb92c03bSAndrew Geissler                                 if (id == nullptr)
727cb92c03bSAndrew Geissler                                 {
728cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
729cb92c03bSAndrew Geissler                                                               "Id");
730cb92c03bSAndrew Geissler                                 }
731cb92c03bSAndrew Geissler                             }
732cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
733cb92c03bSAndrew Geissler                             {
734cb92c03bSAndrew Geissler                                 const uint64_t *millisTimeStamp =
735cb92c03bSAndrew Geissler                                     std::get_if<uint64_t>(&propertyMap.second);
736cb92c03bSAndrew Geissler                                 if (millisTimeStamp == nullptr)
737cb92c03bSAndrew Geissler                                 {
738cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
739cb92c03bSAndrew Geissler                                                               "Timestamp");
740cb92c03bSAndrew Geissler                                 }
741cb92c03bSAndrew Geissler                                 // Retrieve Created property with format:
742cb92c03bSAndrew Geissler                                 // yyyy-mm-ddThh:mm:ss
743cb92c03bSAndrew Geissler                                 std::chrono::milliseconds chronoTimeStamp(
744cb92c03bSAndrew Geissler                                     *millisTimeStamp);
745cb92c03bSAndrew Geissler                                 timestamp =
746cb92c03bSAndrew Geissler                                     std::chrono::duration_cast<
747cb92c03bSAndrew Geissler                                         std::chrono::seconds>(chronoTimeStamp)
748cb92c03bSAndrew Geissler                                         .count();
749cb92c03bSAndrew Geissler                             }
750cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
751cb92c03bSAndrew Geissler                             {
752cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
753cb92c03bSAndrew Geissler                                     &propertyMap.second);
754cb92c03bSAndrew Geissler                                 if (severity == nullptr)
755cb92c03bSAndrew Geissler                                 {
756cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
757cb92c03bSAndrew Geissler                                                               "Severity");
758cb92c03bSAndrew Geissler                                 }
759cb92c03bSAndrew Geissler                             }
760cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
761cb92c03bSAndrew Geissler                             {
762cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
763cb92c03bSAndrew Geissler                                     &propertyMap.second);
764cb92c03bSAndrew Geissler                                 if (message == nullptr)
765cb92c03bSAndrew Geissler                                 {
766cb92c03bSAndrew Geissler                                     messages::propertyMissing(asyncResp->res,
767cb92c03bSAndrew Geissler                                                               "Message");
768cb92c03bSAndrew Geissler                                 }
769cb92c03bSAndrew Geissler                             }
770cb92c03bSAndrew Geissler                         }
771cb92c03bSAndrew Geissler                         thisEntry = {
772cb92c03bSAndrew Geissler                             {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
773cb92c03bSAndrew Geissler                             {"@odata.context", "/redfish/v1/"
774cb92c03bSAndrew Geissler                                                "$metadata#LogEntry.LogEntry"},
775cb92c03bSAndrew Geissler                             {"@odata.id",
776cb92c03bSAndrew Geissler                              "/redfish/v1/Systems/system/LogServices/EventLog/"
777cb92c03bSAndrew Geissler                              "Entries/" +
778cb92c03bSAndrew Geissler                                  std::to_string(*id)},
779cb92c03bSAndrew Geissler                             {"Name", "System DBus Event Log Entry"},
780cb92c03bSAndrew Geissler                             {"Id", std::to_string(*id)},
781cb92c03bSAndrew Geissler                             {"Message", *message},
782cb92c03bSAndrew Geissler                             {"EntryType", "Event"},
783cb92c03bSAndrew Geissler                             {"Severity",
784cb92c03bSAndrew Geissler                              translateSeverityDbusToRedfish(*severity)},
785cb92c03bSAndrew Geissler                             {"Created", crow::utility::getDateTime(timestamp)}};
786cb92c03bSAndrew Geissler                     }
787cb92c03bSAndrew Geissler                 }
788cb92c03bSAndrew Geissler                 std::sort(entriesArray.begin(), entriesArray.end(),
789cb92c03bSAndrew Geissler                           [](const nlohmann::json &left,
790cb92c03bSAndrew Geissler                              const nlohmann::json &right) {
791cb92c03bSAndrew Geissler                               return (left["Id"] <= right["Id"]);
792cb92c03bSAndrew Geissler                           });
793cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue["Members@odata.count"] =
794cb92c03bSAndrew Geissler                     entriesArray.size();
795cb92c03bSAndrew Geissler             },
796cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
797cb92c03bSAndrew Geissler             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
798cb92c03bSAndrew Geissler #endif
799c4bf6374SJason M. Bills     }
800c4bf6374SJason M. Bills };
801c4bf6374SJason M. Bills 
802c4bf6374SJason M. Bills class EventLogEntry : public Node
803c4bf6374SJason M. Bills {
804c4bf6374SJason M. Bills   public:
805c4bf6374SJason M. Bills     EventLogEntry(CrowApp &app) :
806c4bf6374SJason M. Bills         Node(app,
807029573d4SEd Tanous              "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
808029573d4SEd Tanous              std::string())
809c4bf6374SJason M. Bills     {
810c4bf6374SJason M. Bills         entityPrivileges = {
811c4bf6374SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
812c4bf6374SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
813c4bf6374SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
814c4bf6374SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
815c4bf6374SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
816c4bf6374SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
817c4bf6374SJason M. Bills     }
818c4bf6374SJason M. Bills 
819c4bf6374SJason M. Bills   private:
820c4bf6374SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
821c4bf6374SJason M. Bills                const std::vector<std::string> &params) override
822c4bf6374SJason M. Bills     {
823c4bf6374SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
824029573d4SEd Tanous         if (params.size() != 1)
825c4bf6374SJason M. Bills         {
826c4bf6374SJason M. Bills             messages::internalError(asyncResp->res);
827c4bf6374SJason M. Bills             return;
828c4bf6374SJason M. Bills         }
829029573d4SEd Tanous         const std::string &entryID = params[0];
830cb92c03bSAndrew Geissler 
83195820184SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
832cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
833cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
834cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
835cb92c03bSAndrew Geissler             [asyncResp, entryID](const boost::system::error_code ec,
836cb92c03bSAndrew Geissler                                  GetManagedPropertyType &resp) {
837cb92c03bSAndrew Geissler                 if (ec)
838cb92c03bSAndrew Geissler                 {
839cb92c03bSAndrew Geissler                     BMCWEB_LOG_ERROR
840cb92c03bSAndrew Geissler                         << "EventLogEntry (DBus) resp_handler got error " << ec;
841cb92c03bSAndrew Geissler                     messages::internalError(asyncResp->res);
842cb92c03bSAndrew Geissler                     return;
843cb92c03bSAndrew Geissler                 }
844cb92c03bSAndrew Geissler                 uint32_t *id;
845cb92c03bSAndrew Geissler                 std::time_t timestamp;
846cb92c03bSAndrew Geissler                 std::string *severity, *message;
847cb92c03bSAndrew Geissler                 bool *resolved;
848cb92c03bSAndrew Geissler                 for (auto &propertyMap : resp)
849cb92c03bSAndrew Geissler                 {
850cb92c03bSAndrew Geissler                     if (propertyMap.first == "Id")
851cb92c03bSAndrew Geissler                     {
852cb92c03bSAndrew Geissler                         id = std::get_if<uint32_t>(&propertyMap.second);
853cb92c03bSAndrew Geissler                         if (id == nullptr)
854cb92c03bSAndrew Geissler                         {
855cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res, "Id");
856cb92c03bSAndrew Geissler                         }
857cb92c03bSAndrew Geissler                     }
858cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Timestamp")
859cb92c03bSAndrew Geissler                     {
860cb92c03bSAndrew Geissler                         const uint64_t *millisTimeStamp =
861cb92c03bSAndrew Geissler                             std::get_if<uint64_t>(&propertyMap.second);
862cb92c03bSAndrew Geissler                         if (millisTimeStamp == nullptr)
863cb92c03bSAndrew Geissler                         {
864cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
865cb92c03bSAndrew Geissler                                                       "Timestamp");
866cb92c03bSAndrew Geissler                         }
867cb92c03bSAndrew Geissler                         // Retrieve Created property with format:
868cb92c03bSAndrew Geissler                         // yyyy-mm-ddThh:mm:ss
869cb92c03bSAndrew Geissler                         std::chrono::milliseconds chronoTimeStamp(
870cb92c03bSAndrew Geissler                             *millisTimeStamp);
871cb92c03bSAndrew Geissler                         timestamp =
872cb92c03bSAndrew Geissler                             std::chrono::duration_cast<std::chrono::seconds>(
873cb92c03bSAndrew Geissler                                 chronoTimeStamp)
874cb92c03bSAndrew Geissler                                 .count();
875cb92c03bSAndrew Geissler                     }
876cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Severity")
877cb92c03bSAndrew Geissler                     {
878cb92c03bSAndrew Geissler                         severity =
879cb92c03bSAndrew Geissler                             std::get_if<std::string>(&propertyMap.second);
880cb92c03bSAndrew Geissler                         if (severity == nullptr)
881cb92c03bSAndrew Geissler                         {
882cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
883cb92c03bSAndrew Geissler                                                       "Severity");
884cb92c03bSAndrew Geissler                         }
885cb92c03bSAndrew Geissler                     }
886cb92c03bSAndrew Geissler                     else if (propertyMap.first == "Message")
887cb92c03bSAndrew Geissler                     {
888cb92c03bSAndrew Geissler                         message = std::get_if<std::string>(&propertyMap.second);
889cb92c03bSAndrew Geissler                         if (message == nullptr)
890cb92c03bSAndrew Geissler                         {
891cb92c03bSAndrew Geissler                             messages::propertyMissing(asyncResp->res,
892cb92c03bSAndrew Geissler                                                       "Message");
893cb92c03bSAndrew Geissler                         }
894cb92c03bSAndrew Geissler                     }
895cb92c03bSAndrew Geissler                 }
896cb92c03bSAndrew Geissler                 asyncResp->res.jsonValue = {
897cb92c03bSAndrew Geissler                     {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
898cb92c03bSAndrew Geissler                     {"@odata.context", "/redfish/v1/"
899cb92c03bSAndrew Geissler                                        "$metadata#LogEntry.LogEntry"},
900cb92c03bSAndrew Geissler                     {"@odata.id",
901cb92c03bSAndrew Geissler                      "/redfish/v1/Systems/system/LogServices/EventLog/"
902cb92c03bSAndrew Geissler                      "Entries/" +
903cb92c03bSAndrew Geissler                          std::to_string(*id)},
904cb92c03bSAndrew Geissler                     {"Name", "System DBus Event Log Entry"},
905cb92c03bSAndrew Geissler                     {"Id", std::to_string(*id)},
906cb92c03bSAndrew Geissler                     {"Message", *message},
907cb92c03bSAndrew Geissler                     {"EntryType", "Event"},
908cb92c03bSAndrew Geissler                     {"Severity", translateSeverityDbusToRedfish(*severity)},
90995820184SJason M. Bills                     { "Created",
91095820184SJason M. Bills                       crow::utility::getDateTime(timestamp) }};
911cb92c03bSAndrew Geissler             },
912cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
913cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
914cb92c03bSAndrew Geissler             "org.freedesktop.DBus.Properties", "GetAll",
915cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging.Entry");
916cb92c03bSAndrew Geissler #endif
917c4bf6374SJason M. Bills     }
918c4bf6374SJason M. Bills };
919c4bf6374SJason M. Bills 
920c4bf6374SJason M. Bills class BMCLogServiceCollection : public Node
921c4bf6374SJason M. Bills {
922c4bf6374SJason M. Bills   public:
923c4bf6374SJason M. Bills     template <typename CrowApp>
924c4bf6374SJason M. Bills     BMCLogServiceCollection(CrowApp &app) :
9254ed77cd5SEd Tanous         Node(app, "/redfish/v1/Managers/bmc/LogServices/")
9261da66f75SEd Tanous     {
9271da66f75SEd Tanous         entityPrivileges = {
928e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
929e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
930e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
931e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
932e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
933e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
9341da66f75SEd Tanous     }
9351da66f75SEd Tanous 
9361da66f75SEd Tanous   private:
9371da66f75SEd Tanous     /**
9381da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
9391da66f75SEd Tanous      */
9401da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
9411da66f75SEd Tanous                const std::vector<std::string> &params) override
9421da66f75SEd Tanous     {
943e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
9441da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
9451da66f75SEd Tanous         // it has a duplicate entry for members
946e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
9471da66f75SEd Tanous             "#LogServiceCollection.LogServiceCollection";
948e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
949c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogServiceCollection.LogServiceCollection";
950e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
951e1f26343SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices";
952e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
953e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
9541da66f75SEd Tanous             "Collection of LogServices for this Manager";
955c4bf6374SJason M. Bills         nlohmann::json &logServiceArray = asyncResp->res.jsonValue["Members"];
956c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
957c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
958c4bf6374SJason M. Bills         logServiceArray.push_back(
959cb92c03bSAndrew Geissler             {{ "@odata.id",
960cb92c03bSAndrew Geissler                "/redfish/v1/Managers/bmc/LogServices/Journal" }});
961c4bf6374SJason M. Bills #endif
962e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
963c4bf6374SJason M. Bills             logServiceArray.size();
9641da66f75SEd Tanous     }
9651da66f75SEd Tanous };
9661da66f75SEd Tanous 
967c4bf6374SJason M. Bills class BMCJournalLogService : public Node
9681da66f75SEd Tanous {
9691da66f75SEd Tanous   public:
9701da66f75SEd Tanous     template <typename CrowApp>
971c4bf6374SJason M. Bills     BMCJournalLogService(CrowApp &app) :
972c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
973e1f26343SJason M. Bills     {
974e1f26343SJason M. Bills         entityPrivileges = {
975e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
976e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
977e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
978e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
979e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
980e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
981e1f26343SJason M. Bills     }
982e1f26343SJason M. Bills 
983e1f26343SJason M. Bills   private:
984e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
985e1f26343SJason M. Bills                const std::vector<std::string> &params) override
986e1f26343SJason M. Bills     {
987e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
988e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
989e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
9900f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
9910f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
992e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
993e1f26343SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
994c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
995c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
996c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
997e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
998cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
999cd50aa42SJason M. Bills             {"@odata.id",
1000cd50aa42SJason M. Bills              "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/"}};
1001e1f26343SJason M. Bills     }
1002e1f26343SJason M. Bills };
1003e1f26343SJason M. Bills 
1004c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string &bmcJournalLogEntryID,
1005e1f26343SJason M. Bills                                       sd_journal *journal,
1006c4bf6374SJason M. Bills                                       nlohmann::json &bmcJournalLogEntryJson)
1007e1f26343SJason M. Bills {
1008e1f26343SJason M. Bills     // Get the Log Entry contents
1009e1f26343SJason M. Bills     int ret = 0;
1010e1f26343SJason M. Bills 
101139e77504SEd Tanous     std::string_view msg;
101216428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1013e1f26343SJason M. Bills     if (ret < 0)
1014e1f26343SJason M. Bills     {
1015e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1016e1f26343SJason M. Bills         return 1;
1017e1f26343SJason M. Bills     }
1018e1f26343SJason M. Bills 
1019e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1020e1f26343SJason M. Bills     int severity = 8; // Default to an invalid priority
102116428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1022e1f26343SJason M. Bills     if (ret < 0)
1023e1f26343SJason M. Bills     {
1024e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1025e1f26343SJason M. Bills         return 1;
1026e1f26343SJason M. Bills     }
1027e1f26343SJason M. Bills 
1028e1f26343SJason M. Bills     // Get the Created time from the timestamp
102916428a1aSJason M. Bills     std::string entryTimeStr;
103016428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
1031e1f26343SJason M. Bills     {
103216428a1aSJason M. Bills         return 1;
1033e1f26343SJason M. Bills     }
1034e1f26343SJason M. Bills 
1035e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
1036c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
1037cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1038e1f26343SJason M. Bills         {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
1039c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1040c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
1041e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
1042c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
104316428a1aSJason M. Bills         {"Message", msg},
1044e1f26343SJason M. Bills         {"EntryType", "Oem"},
1045e1f26343SJason M. Bills         {"Severity",
1046e1f26343SJason M. Bills          severity <= 2 ? "Critical"
1047e1f26343SJason M. Bills                        : severity <= 4 ? "Warning" : severity <= 7 ? "OK" : ""},
1048e1f26343SJason M. Bills         {"OemRecordFormat", "Intel BMC Journal Entry"},
1049e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
1050e1f26343SJason M. Bills     return 0;
1051e1f26343SJason M. Bills }
1052e1f26343SJason M. Bills 
1053c4bf6374SJason M. Bills class BMCJournalLogEntryCollection : public Node
1054e1f26343SJason M. Bills {
1055e1f26343SJason M. Bills   public:
1056e1f26343SJason M. Bills     template <typename CrowApp>
1057c4bf6374SJason M. Bills     BMCJournalLogEntryCollection(CrowApp &app) :
1058c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
1059e1f26343SJason M. Bills     {
1060e1f26343SJason M. Bills         entityPrivileges = {
1061e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1062e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1063e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1064e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1065e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1066e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1067e1f26343SJason M. Bills     }
1068e1f26343SJason M. Bills 
1069e1f26343SJason M. Bills   private:
1070e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1071e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1072e1f26343SJason M. Bills     {
1073e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1074193ad2faSJason M. Bills         static constexpr const long maxEntriesPerPage = 1000;
1075193ad2faSJason M. Bills         long skip = 0;
1076193ad2faSJason M. Bills         long top = maxEntriesPerPage; // Show max entries by default
107716428a1aSJason M. Bills         if (!getSkipParam(asyncResp->res, req, skip))
1078193ad2faSJason M. Bills         {
1079193ad2faSJason M. Bills             return;
1080193ad2faSJason M. Bills         }
108116428a1aSJason M. Bills         if (!getTopParam(asyncResp->res, req, top))
1082193ad2faSJason M. Bills         {
1083193ad2faSJason M. Bills             return;
1084193ad2faSJason M. Bills         }
1085e1f26343SJason M. Bills         // Collections don't include the static data added by SubRoute because
1086e1f26343SJason M. Bills         // it has a duplicate entry for members
1087e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1088e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
10890f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
10900f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1091e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1092c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogEntryCollection.LogEntryCollection";
1093e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1094c4bf6374SJason M. Bills             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1095e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1096e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1097e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
10980f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
10990f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/BmcLog/Entries";
1100e1f26343SJason M. Bills         nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
1101e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
1102e1f26343SJason M. Bills 
1103e1f26343SJason M. Bills         // Go through the journal and use the timestamp to create a unique ID
1104e1f26343SJason M. Bills         // for each entry
1105e1f26343SJason M. Bills         sd_journal *journalTmp = nullptr;
1106e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1107e1f26343SJason M. Bills         if (ret < 0)
1108e1f26343SJason M. Bills         {
1109e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1110f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1111e1f26343SJason M. Bills             return;
1112e1f26343SJason M. Bills         }
1113e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1114e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1115e1f26343SJason M. Bills         journalTmp = nullptr;
1116b01bf299SEd Tanous         uint64_t entryCount = 0;
1117e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
1118e1f26343SJason M. Bills         {
1119193ad2faSJason M. Bills             entryCount++;
1120193ad2faSJason M. Bills             // Handle paging using skip (number of entries to skip from the
1121193ad2faSJason M. Bills             // start) and top (number of entries to display)
1122193ad2faSJason M. Bills             if (entryCount <= skip || entryCount > skip + top)
1123193ad2faSJason M. Bills             {
1124193ad2faSJason M. Bills                 continue;
1125193ad2faSJason M. Bills             }
1126193ad2faSJason M. Bills 
112716428a1aSJason M. Bills             std::string idStr;
112816428a1aSJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr))
1129e1f26343SJason M. Bills             {
1130e1f26343SJason M. Bills                 continue;
1131e1f26343SJason M. Bills             }
1132e1f26343SJason M. Bills 
1133e1f26343SJason M. Bills             logEntryArray.push_back({});
1134c4bf6374SJason M. Bills             nlohmann::json &bmcJournalLogEntry = logEntryArray.back();
1135c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
1136c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
1137e1f26343SJason M. Bills             {
1138f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
1139e1f26343SJason M. Bills                 return;
1140e1f26343SJason M. Bills             }
1141e1f26343SJason M. Bills         }
1142193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1143193ad2faSJason M. Bills         if (skip + top < entryCount)
1144193ad2faSJason M. Bills         {
1145193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
1146c4bf6374SJason M. Bills                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
1147193ad2faSJason M. Bills                 std::to_string(skip + top);
1148193ad2faSJason M. Bills         }
1149e1f26343SJason M. Bills     }
1150e1f26343SJason M. Bills };
1151e1f26343SJason M. Bills 
1152c4bf6374SJason M. Bills class BMCJournalLogEntry : public Node
1153e1f26343SJason M. Bills {
1154e1f26343SJason M. Bills   public:
1155c4bf6374SJason M. Bills     BMCJournalLogEntry(CrowApp &app) :
1156c4bf6374SJason M. Bills         Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/",
1157e1f26343SJason M. Bills              std::string())
1158e1f26343SJason M. Bills     {
1159e1f26343SJason M. Bills         entityPrivileges = {
1160e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1161e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1162e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1163e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1164e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1165e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1166e1f26343SJason M. Bills     }
1167e1f26343SJason M. Bills 
1168e1f26343SJason M. Bills   private:
1169e1f26343SJason M. Bills     void doGet(crow::Response &res, const crow::Request &req,
1170e1f26343SJason M. Bills                const std::vector<std::string> &params) override
1171e1f26343SJason M. Bills     {
1172e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1173e1f26343SJason M. Bills         if (params.size() != 1)
1174e1f26343SJason M. Bills         {
1175f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1176e1f26343SJason M. Bills             return;
1177e1f26343SJason M. Bills         }
117816428a1aSJason M. Bills         const std::string &entryID = params[0];
1179e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
1180e1f26343SJason M. Bills         uint64_t ts = 0;
1181e1f26343SJason M. Bills         uint16_t index = 0;
118216428a1aSJason M. Bills         if (!getTimestampFromID(asyncResp->res, entryID, ts, index))
1183e1f26343SJason M. Bills         {
118416428a1aSJason M. Bills             return;
1185e1f26343SJason M. Bills         }
1186e1f26343SJason M. Bills 
1187e1f26343SJason M. Bills         sd_journal *journalTmp = nullptr;
1188e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1189e1f26343SJason M. Bills         if (ret < 0)
1190e1f26343SJason M. Bills         {
1191e1f26343SJason M. Bills             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
1192f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1193e1f26343SJason M. Bills             return;
1194e1f26343SJason M. Bills         }
1195e1f26343SJason M. Bills         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1196e1f26343SJason M. Bills             journalTmp, sd_journal_close);
1197e1f26343SJason M. Bills         journalTmp = nullptr;
1198e1f26343SJason M. Bills         // Go to the timestamp in the log and move to the entry at the index
1199e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
1200e1f26343SJason M. Bills         for (int i = 0; i <= index; i++)
1201e1f26343SJason M. Bills         {
1202e1f26343SJason M. Bills             sd_journal_next(journal.get());
1203e1f26343SJason M. Bills         }
1204c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
1205c4bf6374SJason M. Bills         std::string idStr;
1206c4bf6374SJason M. Bills         if (!getUniqueEntryID(journal.get(), idStr) || idStr != entryID)
1207c4bf6374SJason M. Bills         {
1208c4bf6374SJason M. Bills             messages::resourceMissingAtURI(asyncResp->res, entryID);
1209c4bf6374SJason M. Bills             return;
1210c4bf6374SJason M. Bills         }
1211c4bf6374SJason M. Bills 
1212c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
1213e1f26343SJason M. Bills                                        asyncResp->res.jsonValue) != 0)
1214e1f26343SJason M. Bills         {
1215f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1216e1f26343SJason M. Bills             return;
1217e1f26343SJason M. Bills         }
1218e1f26343SJason M. Bills     }
1219e1f26343SJason M. Bills };
1220e1f26343SJason M. Bills 
1221424c4176SJason M. Bills class CrashdumpService : public Node
1222e1f26343SJason M. Bills {
1223e1f26343SJason M. Bills   public:
1224e1f26343SJason M. Bills     template <typename CrowApp>
1225424c4176SJason M. Bills     CrashdumpService(CrowApp &app) :
1226424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
12271da66f75SEd Tanous     {
12281da66f75SEd Tanous         entityPrivileges = {
1229e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1230e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1231e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1232e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1233e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1234e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
12351da66f75SEd Tanous     }
12361da66f75SEd Tanous 
12371da66f75SEd Tanous   private:
12381da66f75SEd Tanous     /**
12391da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
12401da66f75SEd Tanous      */
12411da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
12421da66f75SEd Tanous                const std::vector<std::string> &params) override
12431da66f75SEd Tanous     {
1244e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12451da66f75SEd Tanous         // Copy over the static data to include the entries added by SubRoute
12460f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1247424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
1248e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1249e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
1250e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.context"] =
1251c4bf6374SJason M. Bills             "/redfish/v1/$metadata#LogService.LogService";
1252424c4176SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Service";
1253424c4176SJason M. Bills         asyncResp->res.jsonValue["Description"] = "Crashdump Service";
1254424c4176SJason M. Bills         asyncResp->res.jsonValue["Id"] = "Crashdump";
1255e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1256e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
1257cd50aa42SJason M. Bills         asyncResp->res.jsonValue["Entries"] = {
1258cd50aa42SJason M. Bills             {"@odata.id",
1259424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
1260e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"] = {
12611da66f75SEd Tanous             {"Oem",
1262424c4176SJason M. Bills              {{"#Crashdump.OnDemand",
1263424c4176SJason M. Bills                {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
1264424c4176SJason M. Bills                            "Actions/Oem/Crashdump.OnDemand"}}}}}};
12651da66f75SEd Tanous 
12661da66f75SEd Tanous #ifdef BMCWEB_ENABLE_REDFISH_RAW_PECI
1267e1f26343SJason M. Bills         asyncResp->res.jsonValue["Actions"]["Oem"].push_back(
1268424c4176SJason M. Bills             {"#Crashdump.SendRawPeci",
1269cb92c03bSAndrew Geissler              { { "target",
1270424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/"
1271424c4176SJason M. Bills                  "Actions/Oem/Crashdump.SendRawPeci" } }});
12721da66f75SEd Tanous #endif
12731da66f75SEd Tanous     }
12741da66f75SEd Tanous };
12751da66f75SEd Tanous 
1276424c4176SJason M. Bills class CrashdumpEntryCollection : public Node
12771da66f75SEd Tanous {
12781da66f75SEd Tanous   public:
12791da66f75SEd Tanous     template <typename CrowApp>
1280424c4176SJason M. Bills     CrashdumpEntryCollection(CrowApp &app) :
1281424c4176SJason M. Bills         Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
12821da66f75SEd Tanous     {
12831da66f75SEd Tanous         entityPrivileges = {
1284e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1285e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1286e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1287e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1288e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1289e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
12901da66f75SEd Tanous     }
12911da66f75SEd Tanous 
12921da66f75SEd Tanous   private:
12931da66f75SEd Tanous     /**
12941da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
12951da66f75SEd Tanous      */
12961da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
12971da66f75SEd Tanous                const std::vector<std::string> &params) override
12981da66f75SEd Tanous     {
1299e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
13001da66f75SEd Tanous         // Collections don't include the static data added by SubRoute because
13011da66f75SEd Tanous         // it has a duplicate entry for members
1302e1f26343SJason M. Bills         auto getLogEntriesCallback = [asyncResp](
1303e1f26343SJason M. Bills                                          const boost::system::error_code ec,
13041da66f75SEd Tanous                                          const std::vector<std::string> &resp) {
13051da66f75SEd Tanous             if (ec)
13061da66f75SEd Tanous             {
13071da66f75SEd Tanous                 if (ec.value() !=
13081da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
13091da66f75SEd Tanous                 {
13101da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
13111da66f75SEd Tanous                                      << ec.message();
1312f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
13131da66f75SEd Tanous                     return;
13141da66f75SEd Tanous                 }
13151da66f75SEd Tanous             }
1316e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
13171da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
13180f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1319424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
1320e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.context"] =
1321d53dd41fSJason M. Bills                 "/redfish/v1/"
1322d53dd41fSJason M. Bills                 "$metadata#LogEntryCollection.LogEntryCollection";
1323424c4176SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
1324e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
1325424c4176SJason M. Bills                 "Collection of Crashdump Entries";
1326e1f26343SJason M. Bills             nlohmann::json &logEntryArray = asyncResp->res.jsonValue["Members"];
1327e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
13281da66f75SEd Tanous             for (const std::string &objpath : resp)
13291da66f75SEd Tanous             {
133048e4639eSJason M. Bills                 // Don't list the on-demand log
1331424c4176SJason M. Bills                 if (objpath.compare(CrashdumpOnDemandPath) == 0)
13321da66f75SEd Tanous                 {
13331da66f75SEd Tanous                     continue;
13341da66f75SEd Tanous                 }
13354ed77cd5SEd Tanous                 std::size_t lastPos = objpath.rfind("/");
13364ed77cd5SEd Tanous                 if (lastPos != std::string::npos)
13371da66f75SEd Tanous                 {
1338e1f26343SJason M. Bills                     logEntryArray.push_back(
1339d53dd41fSJason M. Bills                         {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
1340424c4176SJason M. Bills                                        "Crashdump/Entries/" +
13414ed77cd5SEd Tanous                                            objpath.substr(lastPos + 1)}});
13421da66f75SEd Tanous                 }
13431da66f75SEd Tanous             }
1344e1f26343SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
1345e1f26343SJason M. Bills                 logEntryArray.size();
13461da66f75SEd Tanous         };
13471da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
13481da66f75SEd Tanous             std::move(getLogEntriesCallback),
13491da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
13501da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
13511da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
1352424c4176SJason M. Bills             std::array<const char *, 1>{CrashdumpInterface});
13531da66f75SEd Tanous     }
13541da66f75SEd Tanous };
13551da66f75SEd Tanous 
1356424c4176SJason M. Bills std::string getLogCreatedTime(const nlohmann::json &Crashdump)
13571da66f75SEd Tanous {
1358424c4176SJason M. Bills     nlohmann::json::const_iterator cdIt = Crashdump.find("crashlog_data");
1359424c4176SJason M. Bills     if (cdIt != Crashdump.end())
13601da66f75SEd Tanous     {
1361c4d00437SJason M. Bills         nlohmann::json::const_iterator siIt = cdIt->find("SYSTEM_INFO");
1362c4d00437SJason M. Bills         if (siIt != cdIt->end())
13631da66f75SEd Tanous         {
1364c4d00437SJason M. Bills             nlohmann::json::const_iterator tsIt = siIt->find("timestamp");
1365c4d00437SJason M. Bills             if (tsIt != siIt->end())
1366c4d00437SJason M. Bills             {
1367c4d00437SJason M. Bills                 const std::string *logTime =
1368c4d00437SJason M. Bills                     tsIt->get_ptr<const std::string *>();
13691da66f75SEd Tanous                 if (logTime != nullptr)
13701da66f75SEd Tanous                 {
13711da66f75SEd Tanous                     return *logTime;
13721da66f75SEd Tanous                 }
13731da66f75SEd Tanous             }
13741da66f75SEd Tanous         }
1375c4d00437SJason M. Bills     }
13761da66f75SEd Tanous     BMCWEB_LOG_DEBUG << "failed to find log timestamp";
13771da66f75SEd Tanous 
13781da66f75SEd Tanous     return std::string();
13791da66f75SEd Tanous }
13801da66f75SEd Tanous 
1381424c4176SJason M. Bills class CrashdumpEntry : public Node
13821da66f75SEd Tanous {
13831da66f75SEd Tanous   public:
1384424c4176SJason M. Bills     CrashdumpEntry(CrowApp &app) :
1385d53dd41fSJason M. Bills         Node(app,
1386424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/",
13871da66f75SEd Tanous              std::string())
13881da66f75SEd Tanous     {
13891da66f75SEd Tanous         entityPrivileges = {
1390e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1391e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1392e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1393e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1394e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1395e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
13961da66f75SEd Tanous     }
13971da66f75SEd Tanous 
13981da66f75SEd Tanous   private:
13991da66f75SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14001da66f75SEd Tanous                const std::vector<std::string> &params) override
14011da66f75SEd Tanous     {
1402e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14031da66f75SEd Tanous         if (params.size() != 1)
14041da66f75SEd Tanous         {
1405f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
14061da66f75SEd Tanous             return;
14071da66f75SEd Tanous         }
1408b01bf299SEd Tanous         const uint8_t logId = std::atoi(params[0].c_str());
1409abf2add6SEd Tanous         auto getStoredLogCallback = [asyncResp, logId](
1410abf2add6SEd Tanous                                         const boost::system::error_code ec,
1411abf2add6SEd Tanous                                         const std::variant<std::string> &resp) {
14121da66f75SEd Tanous             if (ec)
14131da66f75SEd Tanous             {
1414abf2add6SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
1415f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
14161da66f75SEd Tanous                 return;
14171da66f75SEd Tanous             }
1418abf2add6SEd Tanous             const std::string *log = std::get_if<std::string>(&resp);
14191da66f75SEd Tanous             if (log == nullptr)
14201da66f75SEd Tanous             {
1421f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
14221da66f75SEd Tanous                 return;
14231da66f75SEd Tanous             }
14241da66f75SEd Tanous             nlohmann::json j = nlohmann::json::parse(*log, nullptr, false);
14251da66f75SEd Tanous             if (j.is_discarded())
14261da66f75SEd Tanous             {
1427f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
14281da66f75SEd Tanous                 return;
14291da66f75SEd Tanous             }
14301da66f75SEd Tanous             std::string t = getLogCreatedTime(j);
1431e1f26343SJason M. Bills             asyncResp->res.jsonValue = {
1432cb92c03bSAndrew Geissler                 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1433abf2add6SEd Tanous                 {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
14341da66f75SEd Tanous                 {"@odata.id",
1435424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
14364ed77cd5SEd Tanous                      std::to_string(logId)},
1437424c4176SJason M. Bills                 {"Name", "CPU Crashdump"},
14384ed77cd5SEd Tanous                 {"Id", logId},
14391da66f75SEd Tanous                 {"EntryType", "Oem"},
1440424c4176SJason M. Bills                 {"OemRecordFormat", "Intel Crashdump"},
14411da66f75SEd Tanous                 {"Oem", {{"Intel", std::move(j)}}},
14421da66f75SEd Tanous                 {"Created", std::move(t)}};
14431da66f75SEd Tanous         };
14441da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1445424c4176SJason M. Bills             std::move(getStoredLogCallback), CrashdumpObject,
1446424c4176SJason M. Bills             CrashdumpPath + std::string("/") + std::to_string(logId),
1447424c4176SJason M. Bills             "org.freedesktop.DBus.Properties", "Get", CrashdumpInterface,
1448424c4176SJason M. Bills             "Log");
14491da66f75SEd Tanous     }
14501da66f75SEd Tanous };
14511da66f75SEd Tanous 
1452424c4176SJason M. Bills class OnDemandCrashdump : public Node
14531da66f75SEd Tanous {
14541da66f75SEd Tanous   public:
1455424c4176SJason M. Bills     OnDemandCrashdump(CrowApp &app) :
1456424c4176SJason M. Bills         Node(app,
1457424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
1458424c4176SJason M. Bills              "Crashdump.OnDemand/")
14591da66f75SEd Tanous     {
14601da66f75SEd Tanous         entityPrivileges = {
1461e1f26343SJason M. Bills             {boost::beast::http::verb::get, {{"Login"}}},
1462e1f26343SJason M. Bills             {boost::beast::http::verb::head, {{"Login"}}},
1463e1f26343SJason M. Bills             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1464e1f26343SJason M. Bills             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1465e1f26343SJason M. Bills             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1466e1f26343SJason M. Bills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
14671da66f75SEd Tanous     }
14681da66f75SEd Tanous 
14691da66f75SEd Tanous   private:
14701da66f75SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
14711da66f75SEd Tanous                 const std::vector<std::string> &params) override
14721da66f75SEd Tanous     {
1473e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
147448e4639eSJason M. Bills         static std::unique_ptr<sdbusplus::bus::match::match> onDemandLogMatcher;
14751da66f75SEd Tanous 
147648e4639eSJason M. Bills         // Only allow one OnDemand Log request at a time
147748e4639eSJason M. Bills         if (onDemandLogMatcher != nullptr)
14781da66f75SEd Tanous         {
1479e1f26343SJason M. Bills             asyncResp->res.addHeader("Retry-After", "30");
1480f12894f8SJason M. Bills             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
14811da66f75SEd Tanous             return;
14821da66f75SEd Tanous         }
14831da66f75SEd Tanous         // Make this static so it survives outside this method
14841da66f75SEd Tanous         static boost::asio::deadline_timer timeout(*req.ioService);
14851da66f75SEd Tanous 
14861da66f75SEd Tanous         timeout.expires_from_now(boost::posix_time::seconds(30));
1487e1f26343SJason M. Bills         timeout.async_wait([asyncResp](const boost::system::error_code &ec) {
148848e4639eSJason M. Bills             onDemandLogMatcher = nullptr;
14891da66f75SEd Tanous             if (ec)
14901da66f75SEd Tanous             {
14911da66f75SEd Tanous                 // operation_aborted is expected if timer is canceled before
14921da66f75SEd Tanous                 // completion.
14931da66f75SEd Tanous                 if (ec != boost::asio::error::operation_aborted)
14941da66f75SEd Tanous                 {
14951da66f75SEd Tanous                     BMCWEB_LOG_ERROR << "Async_wait failed " << ec;
14961da66f75SEd Tanous                 }
14971da66f75SEd Tanous                 return;
14981da66f75SEd Tanous             }
149948e4639eSJason M. Bills             BMCWEB_LOG_ERROR << "Timed out waiting for on-demand log";
15001da66f75SEd Tanous 
1501f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
15021da66f75SEd Tanous         });
15031da66f75SEd Tanous 
150448e4639eSJason M. Bills         auto onDemandLogMatcherCallback = [asyncResp](
15051da66f75SEd Tanous                                               sdbusplus::message::message &m) {
150648e4639eSJason M. Bills             BMCWEB_LOG_DEBUG << "OnDemand log available match fired";
15071da66f75SEd Tanous             boost::system::error_code ec;
15081da66f75SEd Tanous             timeout.cancel(ec);
15091da66f75SEd Tanous             if (ec)
15101da66f75SEd Tanous             {
15111da66f75SEd Tanous                 BMCWEB_LOG_ERROR << "error canceling timer " << ec;
15121da66f75SEd Tanous             }
15134ed77cd5SEd Tanous             sdbusplus::message::object_path objPath;
15141da66f75SEd Tanous             boost::container::flat_map<
1515abf2add6SEd Tanous                 std::string, boost::container::flat_map<
1516abf2add6SEd Tanous                                  std::string, std::variant<std::string>>>
15174ed77cd5SEd Tanous                 interfacesAdded;
15184ed77cd5SEd Tanous             m.read(objPath, interfacesAdded);
1519abf2add6SEd Tanous             const std::string *log = std::get_if<std::string>(
1520424c4176SJason M. Bills                 &interfacesAdded[CrashdumpInterface]["Log"]);
15211da66f75SEd Tanous             if (log == nullptr)
15221da66f75SEd Tanous             {
1523f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
152448e4639eSJason M. Bills                 // Careful with onDemandLogMatcher.  It is a unique_ptr to the
15251da66f75SEd Tanous                 // match object inside which this lambda is executing.  Once it
15261da66f75SEd Tanous                 // is set to nullptr, the match object will be destroyed and the
15271da66f75SEd Tanous                 // lambda will lose its context, including res, so it needs to
15281da66f75SEd Tanous                 // be the last thing done.
152948e4639eSJason M. Bills                 onDemandLogMatcher = nullptr;
15301da66f75SEd Tanous                 return;
15311da66f75SEd Tanous             }
15321da66f75SEd Tanous             nlohmann::json j = nlohmann::json::parse(*log, nullptr, false);
15331da66f75SEd Tanous             if (j.is_discarded())
15341da66f75SEd Tanous             {
1535f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
153648e4639eSJason M. Bills                 // Careful with onDemandLogMatcher.  It is a unique_ptr to the
15371da66f75SEd Tanous                 // match object inside which this lambda is executing.  Once it
15381da66f75SEd Tanous                 // is set to nullptr, the match object will be destroyed and the
15391da66f75SEd Tanous                 // lambda will lose its context, including res, so it needs to
15401da66f75SEd Tanous                 // be the last thing done.
154148e4639eSJason M. Bills                 onDemandLogMatcher = nullptr;
15421da66f75SEd Tanous                 return;
15431da66f75SEd Tanous             }
15441da66f75SEd Tanous             std::string t = getLogCreatedTime(j);
1545e1f26343SJason M. Bills             asyncResp->res.jsonValue = {
1546cb92c03bSAndrew Geissler                 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
15471da66f75SEd Tanous                 {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
1548424c4176SJason M. Bills                 {"Name", "CPU Crashdump"},
15491da66f75SEd Tanous                 {"EntryType", "Oem"},
1550424c4176SJason M. Bills                 {"OemRecordFormat", "Intel Crashdump"},
15511da66f75SEd Tanous                 {"Oem", {{"Intel", std::move(j)}}},
15521da66f75SEd Tanous                 {"Created", std::move(t)}};
155348e4639eSJason M. Bills             // Careful with onDemandLogMatcher.  It is a unique_ptr to the
15541da66f75SEd Tanous             // match object inside which this lambda is executing.  Once it is
15551da66f75SEd Tanous             // set to nullptr, the match object will be destroyed and the lambda
15561da66f75SEd Tanous             // will lose its context, including res, so it needs to be the last
15571da66f75SEd Tanous             // thing done.
155848e4639eSJason M. Bills             onDemandLogMatcher = nullptr;
15591da66f75SEd Tanous         };
156048e4639eSJason M. Bills         onDemandLogMatcher = std::make_unique<sdbusplus::bus::match::match>(
15611da66f75SEd Tanous             *crow::connections::systemBus,
15621da66f75SEd Tanous             sdbusplus::bus::match::rules::interfacesAdded() +
1563424c4176SJason M. Bills                 sdbusplus::bus::match::rules::argNpath(0,
1564424c4176SJason M. Bills                                                        CrashdumpOnDemandPath),
156548e4639eSJason M. Bills             std::move(onDemandLogMatcherCallback));
15661da66f75SEd Tanous 
156748e4639eSJason M. Bills         auto generateonDemandLogCallback =
1568e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
15691da66f75SEd Tanous                         const std::string &resp) {
15701da66f75SEd Tanous                 if (ec)
15711da66f75SEd Tanous                 {
15721da66f75SEd Tanous                     if (ec.value() ==
15731da66f75SEd Tanous                         boost::system::errc::operation_not_supported)
15741da66f75SEd Tanous                     {
1575f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
15761da66f75SEd Tanous                     }
15771da66f75SEd Tanous                     else
15781da66f75SEd Tanous                     {
1579f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
15801da66f75SEd Tanous                     }
15811da66f75SEd Tanous                     boost::system::error_code timeoutec;
15821da66f75SEd Tanous                     timeout.cancel(timeoutec);
15831da66f75SEd Tanous                     if (timeoutec)
15841da66f75SEd Tanous                     {
15851da66f75SEd Tanous                         BMCWEB_LOG_ERROR << "error canceling timer "
15861da66f75SEd Tanous                                          << timeoutec;
15871da66f75SEd Tanous                     }
158848e4639eSJason M. Bills                     onDemandLogMatcher = nullptr;
15891da66f75SEd Tanous                     return;
15901da66f75SEd Tanous                 }
15911da66f75SEd Tanous             };
15921da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1593424c4176SJason M. Bills             std::move(generateonDemandLogCallback), CrashdumpObject,
1594424c4176SJason M. Bills             CrashdumpPath, CrashdumpOnDemandInterface, "GenerateOnDemandLog");
15951da66f75SEd Tanous     }
15961da66f75SEd Tanous };
15971da66f75SEd Tanous 
1598e1f26343SJason M. Bills class SendRawPECI : public Node
15991da66f75SEd Tanous {
16001da66f75SEd Tanous   public:
1601e1f26343SJason M. Bills     SendRawPECI(CrowApp &app) :
1602424c4176SJason M. Bills         Node(app,
1603424c4176SJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
1604424c4176SJason M. Bills              "Crashdump.SendRawPeci/")
16051da66f75SEd Tanous     {
16061da66f75SEd Tanous         entityPrivileges = {
16071da66f75SEd Tanous             {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
16081da66f75SEd Tanous             {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
16091da66f75SEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
16101da66f75SEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
16111da66f75SEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
16121da66f75SEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
16131da66f75SEd Tanous     }
16141da66f75SEd Tanous 
16151da66f75SEd Tanous   private:
16161da66f75SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
16171da66f75SEd Tanous                 const std::vector<std::string> &params) override
16181da66f75SEd Tanous     {
1619e1f26343SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1620b1556427SEd Tanous         uint8_t clientAddress = 0;
1621b1556427SEd Tanous         uint8_t readLength = 0;
16221da66f75SEd Tanous         std::vector<uint8_t> peciCommand;
1623b1556427SEd Tanous         if (!json_util::readJson(req, res, "ClientAddress", clientAddress,
1624b1556427SEd Tanous                                  "ReadLength", readLength, "PECICommand",
1625b1556427SEd Tanous                                  peciCommand))
16261da66f75SEd Tanous         {
16271da66f75SEd Tanous             return;
16281da66f75SEd Tanous         }
1629b1556427SEd Tanous 
16301da66f75SEd Tanous         // Callback to return the Raw PECI response
1631e1f26343SJason M. Bills         auto sendRawPECICallback =
1632e1f26343SJason M. Bills             [asyncResp](const boost::system::error_code ec,
16331da66f75SEd Tanous                         const std::vector<uint8_t> &resp) {
16341da66f75SEd Tanous                 if (ec)
16351da66f75SEd Tanous                 {
16361da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to send PECI command ec: "
16371da66f75SEd Tanous                                      << ec.message();
1638f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
16391da66f75SEd Tanous                     return;
16401da66f75SEd Tanous                 }
1641e1f26343SJason M. Bills                 asyncResp->res.jsonValue = {{"Name", "PECI Command Response"},
16421da66f75SEd Tanous                                             {"PECIResponse", resp}};
16431da66f75SEd Tanous             };
16441da66f75SEd Tanous         // Call the SendRawPECI command with the provided data
16451da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
1646424c4176SJason M. Bills             std::move(sendRawPECICallback), CrashdumpObject, CrashdumpPath,
1647424c4176SJason M. Bills             CrashdumpRawPECIInterface, "SendRawPeci", clientAddress, readLength,
16484ed77cd5SEd Tanous             peciCommand);
16491da66f75SEd Tanous     }
16501da66f75SEd Tanous };
16511da66f75SEd Tanous 
1652cb92c03bSAndrew Geissler /**
1653cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
1654cb92c03bSAndrew Geissler  */
1655cb92c03bSAndrew Geissler class DBusLogServiceActionsClear : public Node
1656cb92c03bSAndrew Geissler {
1657cb92c03bSAndrew Geissler   public:
1658cb92c03bSAndrew Geissler     DBusLogServiceActionsClear(CrowApp &app) :
1659cb92c03bSAndrew Geissler         Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1660cb92c03bSAndrew Geissler                   "LogService.Reset")
1661cb92c03bSAndrew Geissler     {
1662cb92c03bSAndrew Geissler         entityPrivileges = {
1663cb92c03bSAndrew Geissler             {boost::beast::http::verb::get, {{"Login"}}},
1664cb92c03bSAndrew Geissler             {boost::beast::http::verb::head, {{"Login"}}},
1665cb92c03bSAndrew Geissler             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1666cb92c03bSAndrew Geissler             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1667cb92c03bSAndrew Geissler             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1668cb92c03bSAndrew Geissler             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1669cb92c03bSAndrew Geissler     }
1670cb92c03bSAndrew Geissler 
1671cb92c03bSAndrew Geissler   private:
1672cb92c03bSAndrew Geissler     /**
1673cb92c03bSAndrew Geissler      * Function handles POST method request.
1674cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
1675cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
1676cb92c03bSAndrew Geissler      */
1677cb92c03bSAndrew Geissler     void doPost(crow::Response &res, const crow::Request &req,
1678cb92c03bSAndrew Geissler                 const std::vector<std::string> &params) override
1679cb92c03bSAndrew Geissler     {
1680cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
1681cb92c03bSAndrew Geissler 
1682cb92c03bSAndrew Geissler         auto asyncResp = std::make_shared<AsyncResp>(res);
1683cb92c03bSAndrew Geissler         // Process response from Logging service.
1684cb92c03bSAndrew Geissler         auto resp_handler = [asyncResp](const boost::system::error_code ec) {
1685cb92c03bSAndrew Geissler             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
1686cb92c03bSAndrew Geissler             if (ec)
1687cb92c03bSAndrew Geissler             {
1688cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1689cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
1690cb92c03bSAndrew Geissler                 asyncResp->res.result(
1691cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
1692cb92c03bSAndrew Geissler                 return;
1693cb92c03bSAndrew Geissler             }
1694cb92c03bSAndrew Geissler 
1695cb92c03bSAndrew Geissler             asyncResp->res.result(boost::beast::http::status::no_content);
1696cb92c03bSAndrew Geissler         };
1697cb92c03bSAndrew Geissler 
1698cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
1699cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1700cb92c03bSAndrew Geissler             resp_handler, "xyz.openbmc_project.Logging",
1701cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
1702cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
1703cb92c03bSAndrew Geissler     }
1704cb92c03bSAndrew Geissler };
17051da66f75SEd Tanous } // namespace redfish
1706