blob: f71f81b66d2f3692343c9a724f04c7e003f5e16c [file] [log] [blame]
Ed Tanous1da66f72018-07-27 16:13:37 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Spencer Kub7028eb2021-10-26 15:27:35 +080018#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080019#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080020#include "human_sort.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070021#include "registries.hpp"
22#include "registries/base_message_registry.hpp"
23#include "registries/openbmc_message_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080024#include "task.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070025
Jason M. Billse1f26342018-07-18 12:12:00 -070026#include <systemd/sd-journal.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060027#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070028
John Edward Broadbent7e860f12021-04-08 15:57:16 -070029#include <app.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060030#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070031#include <boost/algorithm/string/split.hpp>
32#include <boost/beast/core/span.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060033#include <boost/beast/http.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070034#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080035#include <boost/system/linux_error.hpp>
Andrew Geisslercb92c032018-08-17 07:56:14 -070036#include <error_messages.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070037#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038
George Liu647b3cd2021-07-05 12:43:56 +080039#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070040#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080041#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070042#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070043#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080044#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070045
46namespace redfish
47{
48
Gunnar Mills1214b7e2020-06-04 10:11:30 -050049constexpr char const* crashdumpObject = "com.intel.crashdump";
50constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050051constexpr char const* crashdumpInterface = "com.intel.crashdump";
52constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070053 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050054constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070055 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070056constexpr char const* crashdumpTelemetryInterface =
57 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070058
Jason M. Bills4851d452019-03-28 11:27:48 -070059namespace message_registries
60{
Ed Tanous26702d02021-11-03 15:02:33 -070061static const Message*
62 getMessageFromRegistry(const std::string& messageKey,
63 const std::span<const MessageEntry> registry)
Jason M. Bills4851d452019-03-28 11:27:48 -070064{
Ed Tanous26702d02021-11-03 15:02:33 -070065 std::span<const MessageEntry>::iterator messageIt = std::find_if(
66 registry.begin(), registry.end(),
67 [&messageKey](const MessageEntry& messageEntry) {
68 return !std::strcmp(messageEntry.first, messageKey.c_str());
69 });
70 if (messageIt != registry.end())
Jason M. Bills4851d452019-03-28 11:27:48 -070071 {
72 return &messageIt->second;
73 }
74
75 return nullptr;
76}
77
Gunnar Mills1214b7e2020-06-04 10:11:30 -050078static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070079{
80 // Redfish MessageIds are in the form
81 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
82 // the right Message
83 std::vector<std::string> fields;
84 fields.reserve(4);
85 boost::split(fields, messageID, boost::is_any_of("."));
Gunnar Mills1214b7e2020-06-04 10:11:30 -050086 std::string& registryName = fields[0];
87 std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -070088
89 // Find the right registry and check it for the MessageKey
90 if (std::string(base::header.registryPrefix) == registryName)
91 {
92 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070093 messageKey, std::span<const MessageEntry>(base::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070094 }
95 if (std::string(openbmc::header.registryPrefix) == registryName)
96 {
97 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070098 messageKey, std::span<const MessageEntry>(openbmc::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070099 }
100 return nullptr;
101}
102} // namespace message_registries
103
James Feistf6150402019-01-08 10:36:20 -0800104namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700105
Andrew Geisslercb92c032018-08-17 07:56:14 -0700106using GetManagedPropertyType = boost::container::flat_map<
Patrick Williams19bd78d2020-05-13 17:38:24 -0500107 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
108 int32_t, uint32_t, int64_t, uint64_t, double>>;
Andrew Geisslercb92c032018-08-17 07:56:14 -0700109
110using GetManagedObjectsType = boost::container::flat_map<
111 sdbusplus::message::object_path,
112 boost::container::flat_map<std::string, GetManagedPropertyType>>;
113
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500114inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700115{
Ed Tanousd4d25792020-09-29 15:15:03 -0700116 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
117 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
118 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
119 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700120 {
121 return "Critical";
122 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700123 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
124 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
125 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700126 {
127 return "OK";
128 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700129 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700130 {
131 return "Warning";
132 }
133 return "";
134}
135
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700136inline static int getJournalMetadata(sd_journal* journal,
137 const std::string_view& field,
138 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700139{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500140 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700141 size_t length = 0;
142 int ret = 0;
143 // Get the metadata from the requested field of the journal entry
Ed Tanous271584a2019-07-09 16:24:22 -0700144 ret = sd_journal_get_data(journal, field.data(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500145 reinterpret_cast<const void**>(&data), &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700146 if (ret < 0)
147 {
148 return ret;
149 }
Ed Tanous39e77502019-03-04 17:35:53 -0800150 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700151 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000152 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700153 return ret;
154}
155
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700156inline static int getJournalMetadata(sd_journal* journal,
157 const std::string_view& field,
158 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700159{
160 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800161 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700162 // Get the metadata from the requested field of the journal entry
163 ret = getJournalMetadata(journal, field, metadata);
164 if (ret < 0)
165 {
166 return ret;
167 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000168 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700169 return ret;
170}
171
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700172inline static bool getEntryTimestamp(sd_journal* journal,
173 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800174{
175 int ret = 0;
176 uint64_t timestamp = 0;
177 ret = sd_journal_get_realtime_usec(journal, &timestamp);
178 if (ret < 0)
179 {
180 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
181 << strerror(-ret);
182 return false;
183 }
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500184 entryTimestamp = crow::utility::getDateTime(
185 static_cast<std::time_t>(timestamp / 1000 / 1000));
186 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800187}
188
zhanghch058d1b46d2021-04-01 11:18:24 +0800189static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
190 const crow::Request& req, uint64_t& skip)
Jason M. Bills16428a12018-11-02 12:42:29 -0700191{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700192 boost::urls::query_params_view::iterator it = req.urlParams.find("$skip");
James Feist5a7e8772020-07-22 09:08:38 -0700193 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700194 {
James Feist5a7e8772020-07-22 09:08:38 -0700195 std::string skipParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500196 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700197 skip = std::strtoul(skipParam.c_str(), &ptr, 10);
198 if (skipParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700199 {
200
zhanghch058d1b46d2021-04-01 11:18:24 +0800201 messages::queryParameterValueTypeError(
202 asyncResp->res, std::string(skipParam), "$skip");
Jason M. Bills16428a12018-11-02 12:42:29 -0700203 return false;
204 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700205 }
206 return true;
207}
208
Ed Tanous271584a2019-07-09 16:24:22 -0700209static constexpr const uint64_t maxEntriesPerPage = 1000;
zhanghch058d1b46d2021-04-01 11:18:24 +0800210static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
211 const crow::Request& req, uint64_t& top)
Jason M. Bills16428a12018-11-02 12:42:29 -0700212{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700213 boost::urls::query_params_view::iterator it = req.urlParams.find("$top");
James Feist5a7e8772020-07-22 09:08:38 -0700214 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700215 {
James Feist5a7e8772020-07-22 09:08:38 -0700216 std::string topParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500217 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700218 top = std::strtoul(topParam.c_str(), &ptr, 10);
219 if (topParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700220 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800221 messages::queryParameterValueTypeError(
222 asyncResp->res, std::string(topParam), "$top");
Jason M. Bills16428a12018-11-02 12:42:29 -0700223 return false;
224 }
Ed Tanous271584a2019-07-09 16:24:22 -0700225 if (top < 1U || top > maxEntriesPerPage)
Jason M. Bills16428a12018-11-02 12:42:29 -0700226 {
227
228 messages::queryParameterOutOfRange(
zhanghch058d1b46d2021-04-01 11:18:24 +0800229 asyncResp->res, std::to_string(top), "$top",
Jason M. Bills16428a12018-11-02 12:42:29 -0700230 "1-" + std::to_string(maxEntriesPerPage));
231 return false;
232 }
233 }
234 return true;
235}
236
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700237inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
238 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700239{
240 int ret = 0;
241 static uint64_t prevTs = 0;
242 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700243 if (firstEntry)
244 {
245 prevTs = 0;
246 }
247
Jason M. Bills16428a12018-11-02 12:42:29 -0700248 // Get the entry timestamp
249 uint64_t curTs = 0;
250 ret = sd_journal_get_realtime_usec(journal, &curTs);
251 if (ret < 0)
252 {
253 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
254 << strerror(-ret);
255 return false;
256 }
257 // If the timestamp isn't unique, increment the index
258 if (curTs == prevTs)
259 {
260 index++;
261 }
262 else
263 {
264 // Otherwise, reset it
265 index = 0;
266 }
267 // Save the timestamp
268 prevTs = curTs;
269
270 entryID = std::to_string(curTs);
271 if (index > 0)
272 {
273 entryID += "_" + std::to_string(index);
274 }
275 return true;
276}
277
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500278static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700279 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700280{
Ed Tanous271584a2019-07-09 16:24:22 -0700281 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700282 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700283 if (firstEntry)
284 {
285 prevTs = 0;
286 }
287
Jason M. Bills95820182019-04-22 16:25:34 -0700288 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700289 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700290 std::tm timeStruct = {};
291 std::istringstream entryStream(logEntry);
292 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
293 {
294 curTs = std::mktime(&timeStruct);
295 }
296 // If the timestamp isn't unique, increment the index
297 if (curTs == prevTs)
298 {
299 index++;
300 }
301 else
302 {
303 // Otherwise, reset it
304 index = 0;
305 }
306 // Save the timestamp
307 prevTs = curTs;
308
309 entryID = std::to_string(curTs);
310 if (index > 0)
311 {
312 entryID += "_" + std::to_string(index);
313 }
314 return true;
315}
316
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700317inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800318 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
319 const std::string& entryID, uint64_t& timestamp,
320 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700321{
322 if (entryID.empty())
323 {
324 return false;
325 }
326 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800327 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700328
Ed Tanous81ce6092020-12-17 16:54:55 +0000329 auto underscorePos = tsStr.find('_');
Jason M. Bills16428a12018-11-02 12:42:29 -0700330 if (underscorePos != tsStr.npos)
331 {
332 // Timestamp has an index
333 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800334 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700335 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700336 auto [ptr, ec] = std::from_chars(
337 indexStr.data(), indexStr.data() + indexStr.size(), index);
338 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700339 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800340 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700341 return false;
342 }
343 }
344 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700345 auto [ptr, ec] =
346 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
347 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700348 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800349 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700350 return false;
351 }
352 return true;
353}
354
Jason M. Bills95820182019-04-22 16:25:34 -0700355static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500356 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700357{
358 static const std::filesystem::path redfishLogDir = "/var/log";
359 static const std::string redfishLogFilename = "redfish";
360
361 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500362 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700363 std::filesystem::directory_iterator(redfishLogDir))
364 {
365 // If we find a redfish log file, save the path
366 std::string filename = dirEnt.path().filename();
367 if (boost::starts_with(filename, redfishLogFilename))
368 {
369 redfishLogFiles.emplace_back(redfishLogDir / filename);
370 }
371 }
372 // As the log files rotate, they are appended with a ".#" that is higher for
373 // the older logs. Since we don't expect more than 10 log files, we
374 // can just sort the list to get them in order from newest to oldest
375 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
376
377 return !redfishLogFiles.empty();
378}
379
zhanghch058d1b46d2021-04-01 11:18:24 +0800380inline void
381 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
382 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500383{
384 std::string dumpPath;
385 if (dumpType == "BMC")
386 {
387 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
388 }
389 else if (dumpType == "System")
390 {
391 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
392 }
393 else
394 {
395 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
396 messages::internalError(asyncResp->res);
397 return;
398 }
399
400 crow::connections::systemBus->async_method_call(
401 [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
402 GetManagedObjectsType& resp) {
403 if (ec)
404 {
405 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
406 messages::internalError(asyncResp->res);
407 return;
408 }
409
410 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
411 entriesArray = nlohmann::json::array();
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500412 std::string dumpEntryPath =
413 "/xyz/openbmc_project/dump/" +
414 std::string(boost::algorithm::to_lower_copy(dumpType)) +
415 "/entry/";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500416
417 for (auto& object : resp)
418 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500419 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500420 {
421 continue;
422 }
423 std::time_t timestamp;
424 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500425 std::string dumpStatus;
426 nlohmann::json thisEntry;
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000427
428 std::string entryID = object.first.filename();
429 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500430 {
431 continue;
432 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500433
434 for (auto& interfaceMap : object.second)
435 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500436 if (interfaceMap.first ==
437 "xyz.openbmc_project.Common.Progress")
438 {
439 for (auto& propertyMap : interfaceMap.second)
440 {
441 if (propertyMap.first == "Status")
442 {
443 auto status = std::get_if<std::string>(
444 &propertyMap.second);
445 if (status == nullptr)
446 {
447 messages::internalError(asyncResp->res);
448 break;
449 }
450 dumpStatus = *status;
451 }
452 }
453 }
454 else if (interfaceMap.first ==
455 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500456 {
457
458 for (auto& propertyMap : interfaceMap.second)
459 {
460 if (propertyMap.first == "Size")
461 {
462 auto sizePtr =
463 std::get_if<uint64_t>(&propertyMap.second);
464 if (sizePtr == nullptr)
465 {
466 messages::internalError(asyncResp->res);
467 break;
468 }
469 size = *sizePtr;
470 break;
471 }
472 }
473 }
474 else if (interfaceMap.first ==
475 "xyz.openbmc_project.Time.EpochTime")
476 {
477
478 for (auto& propertyMap : interfaceMap.second)
479 {
480 if (propertyMap.first == "Elapsed")
481 {
482 const uint64_t* usecsTimeStamp =
483 std::get_if<uint64_t>(&propertyMap.second);
484 if (usecsTimeStamp == nullptr)
485 {
486 messages::internalError(asyncResp->res);
487 break;
488 }
489 timestamp =
490 static_cast<std::time_t>(*usecsTimeStamp);
491 break;
492 }
493 }
494 }
495 }
496
George Liu0fda0f12021-11-16 10:06:17 +0800497 if (dumpStatus !=
498 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500499 !dumpStatus.empty())
500 {
501 // Dump status is not Complete, no need to enumerate
502 continue;
503 }
504
George Liu647b3cd2021-07-05 12:43:56 +0800505 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500506 thisEntry["@odata.id"] = dumpPath + entryID;
507 thisEntry["Id"] = entryID;
508 thisEntry["EntryType"] = "Event";
509 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
510 thisEntry["Name"] = dumpType + " Dump Entry";
511
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500512 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500513
514 if (dumpType == "BMC")
515 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500516 thisEntry["DiagnosticDataType"] = "Manager";
517 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500518 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
519 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500520 }
521 else if (dumpType == "System")
522 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500523 thisEntry["DiagnosticDataType"] = "OEM";
524 thisEntry["OEMDiagnosticDataType"] = "System";
525 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500526 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
527 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500528 }
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500529 entriesArray.push_back(std::move(thisEntry));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500530 }
531 asyncResp->res.jsonValue["Members@odata.count"] =
532 entriesArray.size();
533 },
534 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
535 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
536}
537
zhanghch058d1b46d2021-04-01 11:18:24 +0800538inline void
539 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
540 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500541{
542 std::string dumpPath;
543 if (dumpType == "BMC")
544 {
545 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
546 }
547 else if (dumpType == "System")
548 {
549 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
550 }
551 else
552 {
553 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
554 messages::internalError(asyncResp->res);
555 return;
556 }
557
558 crow::connections::systemBus->async_method_call(
559 [asyncResp, entryID, dumpPath, dumpType](
560 const boost::system::error_code ec, GetManagedObjectsType& resp) {
561 if (ec)
562 {
563 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
564 messages::internalError(asyncResp->res);
565 return;
566 }
567
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500568 bool foundDumpEntry = false;
569 std::string dumpEntryPath =
570 "/xyz/openbmc_project/dump/" +
571 std::string(boost::algorithm::to_lower_copy(dumpType)) +
572 "/entry/";
573
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500574 for (auto& objectPath : resp)
575 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500576 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500577 {
578 continue;
579 }
580
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500581 foundDumpEntry = true;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500582 std::time_t timestamp;
583 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500584 std::string dumpStatus;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500585
586 for (auto& interfaceMap : objectPath.second)
587 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500588 if (interfaceMap.first ==
589 "xyz.openbmc_project.Common.Progress")
590 {
591 for (auto& propertyMap : interfaceMap.second)
592 {
593 if (propertyMap.first == "Status")
594 {
595 auto status = std::get_if<std::string>(
596 &propertyMap.second);
597 if (status == nullptr)
598 {
599 messages::internalError(asyncResp->res);
600 break;
601 }
602 dumpStatus = *status;
603 }
604 }
605 }
606 else if (interfaceMap.first ==
607 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500608 {
609 for (auto& propertyMap : interfaceMap.second)
610 {
611 if (propertyMap.first == "Size")
612 {
613 auto sizePtr =
614 std::get_if<uint64_t>(&propertyMap.second);
615 if (sizePtr == nullptr)
616 {
617 messages::internalError(asyncResp->res);
618 break;
619 }
620 size = *sizePtr;
621 break;
622 }
623 }
624 }
625 else if (interfaceMap.first ==
626 "xyz.openbmc_project.Time.EpochTime")
627 {
628 for (auto& propertyMap : interfaceMap.second)
629 {
630 if (propertyMap.first == "Elapsed")
631 {
632 const uint64_t* usecsTimeStamp =
633 std::get_if<uint64_t>(&propertyMap.second);
634 if (usecsTimeStamp == nullptr)
635 {
636 messages::internalError(asyncResp->res);
637 break;
638 }
639 timestamp =
640 static_cast<std::time_t>(*usecsTimeStamp);
641 break;
642 }
643 }
644 }
645 }
646
George Liu0fda0f12021-11-16 10:06:17 +0800647 if (dumpStatus !=
648 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500649 !dumpStatus.empty())
650 {
651 // Dump status is not Complete
652 // return not found until status is changed to Completed
653 messages::resourceNotFound(asyncResp->res,
654 dumpType + " dump", entryID);
655 return;
656 }
657
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500658 asyncResp->res.jsonValue["@odata.type"] =
George Liu647b3cd2021-07-05 12:43:56 +0800659 "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500660 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
661 asyncResp->res.jsonValue["Id"] = entryID;
662 asyncResp->res.jsonValue["EntryType"] = "Event";
663 asyncResp->res.jsonValue["Created"] =
664 crow::utility::getDateTime(timestamp);
665 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
666
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500667 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500668
669 if (dumpType == "BMC")
670 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500671 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
672 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500673 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
674 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500675 }
676 else if (dumpType == "System")
677 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500678 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
679 asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500680 "System";
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500681 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500682 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
683 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500684 }
685 }
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500686 if (foundDumpEntry == false)
687 {
688 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
689 messages::internalError(asyncResp->res);
690 return;
691 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500692 },
693 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
694 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
695}
696
zhanghch058d1b46d2021-04-01 11:18:24 +0800697inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800698 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500699 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500700{
George Liu3de8d8b2021-03-22 17:49:39 +0800701 auto respHandler = [asyncResp,
702 entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500703 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
704 if (ec)
705 {
George Liu3de8d8b2021-03-22 17:49:39 +0800706 if (ec.value() == EBADR)
707 {
708 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
709 return;
710 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500711 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
712 << ec;
713 messages::internalError(asyncResp->res);
714 return;
715 }
716 };
717 crow::connections::systemBus->async_method_call(
718 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500719 "/xyz/openbmc_project/dump/" +
720 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
721 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500722 "xyz.openbmc_project.Object.Delete", "Delete");
723}
724
zhanghch058d1b46d2021-04-01 11:18:24 +0800725inline void
726 createDumpTaskCallback(const crow::Request& req,
727 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
728 const uint32_t& dumpId, const std::string& dumpPath,
729 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500730{
731 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500732 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500733 boost::system::error_code err, sdbusplus::message::message& m,
734 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000735 if (err)
736 {
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500737 BMCWEB_LOG_ERROR << "Error in creating a dump";
738 taskData->state = "Cancelled";
739 return task::completed;
Ed Tanouscb13a392020-07-25 19:02:03 +0000740 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500741 std::vector<std::pair<
742 std::string,
743 std::vector<std::pair<std::string, std::variant<std::string>>>>>
744 interfacesList;
745
746 sdbusplus::message::object_path objPath;
747
748 m.read(objPath, interfacesList);
749
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500750 if (objPath.str ==
751 "/xyz/openbmc_project/dump/" +
752 std::string(boost::algorithm::to_lower_copy(dumpType)) +
753 "/entry/" + std::to_string(dumpId))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500754 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500755 nlohmann::json retMessage = messages::success();
756 taskData->messages.emplace_back(retMessage);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500757
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500758 std::string headerLoc =
759 "Location: " + dumpPath + std::to_string(dumpId);
760 taskData->payload->httpHeaders.emplace_back(
761 std::move(headerLoc));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500762
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500763 taskData->state = "Completed";
764 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500765 }
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500766 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500767 },
768 "type='signal',interface='org.freedesktop.DBus."
769 "ObjectManager',"
770 "member='InterfacesAdded', "
771 "path='/xyz/openbmc_project/dump'");
772
773 task->startTimer(std::chrono::minutes(3));
774 task->populateResp(asyncResp->res);
775 task->payload.emplace(req);
776}
777
zhanghch058d1b46d2021-04-01 11:18:24 +0800778inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
779 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500780{
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500781
782 std::string dumpPath;
783 if (dumpType == "BMC")
784 {
785 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
786 }
787 else if (dumpType == "System")
788 {
789 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
790 }
791 else
792 {
793 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
794 messages::internalError(asyncResp->res);
795 return;
796 }
797
798 std::optional<std::string> diagnosticDataType;
799 std::optional<std::string> oemDiagnosticDataType;
800
801 if (!redfish::json_util::readJson(
802 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
803 "OEMDiagnosticDataType", oemDiagnosticDataType))
804 {
805 return;
806 }
807
808 if (dumpType == "System")
809 {
810 if (!oemDiagnosticDataType || !diagnosticDataType)
811 {
812 BMCWEB_LOG_ERROR << "CreateDump action parameter "
813 "'DiagnosticDataType'/"
814 "'OEMDiagnosticDataType' value not found!";
815 messages::actionParameterMissing(
816 asyncResp->res, "CollectDiagnosticData",
817 "DiagnosticDataType & OEMDiagnosticDataType");
818 return;
819 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700820 if ((*oemDiagnosticDataType != "System") ||
821 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500822 {
823 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
824 messages::invalidObject(asyncResp->res,
825 "System Dump creation parameters");
826 return;
827 }
828 }
829 else if (dumpType == "BMC")
830 {
831 if (!diagnosticDataType)
832 {
George Liu0fda0f12021-11-16 10:06:17 +0800833 BMCWEB_LOG_ERROR
834 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500835 messages::actionParameterMissing(
836 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
837 return;
838 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700839 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500840 {
841 BMCWEB_LOG_ERROR
842 << "Wrong parameter value passed for 'DiagnosticDataType'";
843 messages::invalidObject(asyncResp->res,
844 "BMC Dump creation parameters");
845 return;
846 }
847 }
848
849 crow::connections::systemBus->async_method_call(
850 [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
851 const uint32_t& dumpId) {
852 if (ec)
853 {
854 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
855 messages::internalError(asyncResp->res);
856 return;
857 }
858 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
859
860 createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
861 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500862 "xyz.openbmc_project.Dump.Manager",
863 "/xyz/openbmc_project/dump/" +
864 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500865 "xyz.openbmc_project.Dump.Create", "CreateDump");
866}
867
zhanghch058d1b46d2021-04-01 11:18:24 +0800868inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
869 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500870{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500871 std::string dumpTypeLowerCopy =
872 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800873
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500874 crow::connections::systemBus->async_method_call(
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500875 [asyncResp, dumpType](const boost::system::error_code ec,
876 const std::vector<std::string>& subTreePaths) {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500877 if (ec)
878 {
879 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
880 messages::internalError(asyncResp->res);
881 return;
882 }
883
884 for (const std::string& path : subTreePaths)
885 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000886 sdbusplus::message::object_path objPath(path);
887 std::string logID = objPath.filename();
888 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500889 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000890 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500891 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000892 deleteDumpEntry(asyncResp, logID, dumpType);
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500893 }
894 },
895 "xyz.openbmc_project.ObjectMapper",
896 "/xyz/openbmc_project/object_mapper",
897 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500898 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
899 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
900 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500901}
902
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700903inline static void parseCrashdumpParameters(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500904 const std::vector<std::pair<std::string, VariantType>>& params,
905 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700906{
907 for (auto property : params)
908 {
909 if (property.first == "Timestamp")
910 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500911 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500912 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700913 if (value != nullptr)
914 {
915 timestamp = *value;
916 }
917 }
918 else if (property.first == "Filename")
919 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500920 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500921 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700922 if (value != nullptr)
923 {
924 filename = *value;
925 }
926 }
927 else if (property.first == "Log")
928 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500929 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500930 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700931 if (value != nullptr)
932 {
933 logfile = *value;
934 }
935 }
936 }
937}
938
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500939constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700940inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700941{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800942 /**
943 * Functions triggers appropriate requests on DBus
944 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700945 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700946 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700947 .methods(boost::beast::http::verb::get)(
948 [](const crow::Request&,
949 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
950
951 {
952 // Collections don't include the static data added by SubRoute
953 // because it has a duplicate entry for members
954 asyncResp->res.jsonValue["@odata.type"] =
955 "#LogServiceCollection.LogServiceCollection";
956 asyncResp->res.jsonValue["@odata.id"] =
957 "/redfish/v1/Systems/system/LogServices";
958 asyncResp->res.jsonValue["Name"] =
959 "System Log Services Collection";
960 asyncResp->res.jsonValue["Description"] =
961 "Collection of LogServices for this Computer System";
962 nlohmann::json& logServiceArray =
963 asyncResp->res.jsonValue["Members"];
964 logServiceArray = nlohmann::json::array();
965 logServiceArray.push_back(
966 {{"@odata.id",
967 "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500968#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700969 logServiceArray.push_back(
970 {{"@odata.id",
971 "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600972#endif
973
Jason M. Billsd53dd412019-02-12 17:16:22 -0800974#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700975 logServiceArray.push_back(
976 {{"@odata.id",
977 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800978#endif
Spencer Kub7028eb2021-10-26 15:27:35 +0800979
980#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
981 logServiceArray.push_back(
982 {{"@odata.id",
983 "/redfish/v1/Systems/system/LogServices/HostLogger"}});
984#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700985 asyncResp->res.jsonValue["Members@odata.count"] =
986 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800987
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700988 crow::connections::systemBus->async_method_call(
989 [asyncResp](const boost::system::error_code ec,
990 const std::vector<std::string>& subtreePath) {
991 if (ec)
992 {
993 BMCWEB_LOG_ERROR << ec;
994 return;
995 }
ZhikuiRena3316fc2020-01-29 14:58:08 -0800996
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700997 for (auto& pathStr : subtreePath)
998 {
999 if (pathStr.find("PostCode") != std::string::npos)
1000 {
1001 nlohmann::json& logServiceArrayLocal =
1002 asyncResp->res.jsonValue["Members"];
1003 logServiceArrayLocal.push_back(
George Liu0fda0f12021-11-16 10:06:17 +08001004 {{"@odata.id",
1005 "/redfish/v1/Systems/system/LogServices/PostCodes"}});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001006 asyncResp->res
1007 .jsonValue["Members@odata.count"] =
1008 logServiceArrayLocal.size();
1009 return;
1010 }
1011 }
1012 },
1013 "xyz.openbmc_project.ObjectMapper",
1014 "/xyz/openbmc_project/object_mapper",
1015 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1016 0, std::array<const char*, 1>{postCodeIface});
1017 });
1018}
1019
1020inline void requestRoutesEventLogService(App& app)
1021{
1022 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001023 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001024 .methods(
1025 boost::beast::http::verb::
1026 get)([](const crow::Request&,
1027 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1028 asyncResp->res.jsonValue["@odata.id"] =
1029 "/redfish/v1/Systems/system/LogServices/EventLog";
1030 asyncResp->res.jsonValue["@odata.type"] =
1031 "#LogService.v1_1_0.LogService";
1032 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1033 asyncResp->res.jsonValue["Description"] =
1034 "System Event Log Service";
1035 asyncResp->res.jsonValue["Id"] = "EventLog";
1036 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301037
1038 std::pair<std::string, std::string> redfishDateTimeOffset =
1039 crow::utility::getDateTimeOffsetNow();
1040
1041 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1042 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1043 redfishDateTimeOffset.second;
1044
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001045 asyncResp->res.jsonValue["Entries"] = {
1046 {"@odata.id",
1047 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1048 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1049
George Liu0fda0f12021-11-16 10:06:17 +08001050 {"target",
1051 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001052 });
1053}
1054
1055inline void requestRoutesJournalEventLogClear(App& app)
1056{
1057 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1058 "LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001059 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001060 .methods(boost::beast::http::verb::post)(
1061 [](const crow::Request&,
1062 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1063 // Clear the EventLog by deleting the log files
1064 std::vector<std::filesystem::path> redfishLogFiles;
1065 if (getRedfishLogFiles(redfishLogFiles))
ZhikuiRena3316fc2020-01-29 14:58:08 -08001066 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001067 for (const std::filesystem::path& file : redfishLogFiles)
ZhikuiRena3316fc2020-01-29 14:58:08 -08001068 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001069 std::error_code ec;
1070 std::filesystem::remove(file, ec);
ZhikuiRena3316fc2020-01-29 14:58:08 -08001071 }
1072 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001073
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001074 // Reload rsyslog so it knows to start new log files
1075 crow::connections::systemBus->async_method_call(
1076 [asyncResp](const boost::system::error_code ec) {
1077 if (ec)
1078 {
1079 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1080 << ec;
1081 messages::internalError(asyncResp->res);
1082 return;
1083 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001084
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001085 messages::success(asyncResp->res);
1086 },
1087 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1088 "org.freedesktop.systemd1.Manager", "ReloadUnit",
1089 "rsyslog.service", "replace");
1090 });
1091}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001092
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001093static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001094 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001095 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001096{
Jason M. Bills95820182019-04-22 16:25:34 -07001097 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001098 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001099 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001100 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001101 {
1102 return 1;
1103 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001104 std::string timestamp = logEntry.substr(0, space);
1105 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001106 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001107 if (entryStart == std::string::npos)
1108 {
1109 return 1;
1110 }
1111 std::string_view entry(logEntry);
1112 entry.remove_prefix(entryStart);
1113 // Use split to separate the entry into its fields
1114 std::vector<std::string> logEntryFields;
1115 boost::split(logEntryFields, entry, boost::is_any_of(","),
1116 boost::token_compress_on);
1117 // We need at least a MessageId to be valid
1118 if (logEntryFields.size() < 1)
1119 {
1120 return 1;
1121 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001122 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001123
Jason M. Bills4851d452019-03-28 11:27:48 -07001124 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001125 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001126 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001127
Jason M. Bills4851d452019-03-28 11:27:48 -07001128 std::string msg;
1129 std::string severity;
1130 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001131 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001132 msg = message->message;
1133 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001134 }
1135
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001136 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001137 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001138 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001139 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001140 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001141 // If the first string is empty, assume there are no MessageArgs
1142 std::size_t messageArgsSize = 0;
1143 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001144 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001145 messageArgsSize = logEntryFields.size() - 1;
1146 }
1147
Ed Tanous23a21a12020-07-25 04:45:05 +00001148 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001149
1150 // Fill the MessageArgs into the Message
1151 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001152 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001153 {
1154 std::string argStr = "%" + std::to_string(++i);
1155 size_t argPos = msg.find(argStr);
1156 if (argPos != std::string::npos)
1157 {
1158 msg.replace(argPos, argStr.length(), messageArg);
1159 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001160 }
1161 }
1162
Jason M. Bills95820182019-04-22 16:25:34 -07001163 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1164 // format which matches the Redfish format except for the fractional seconds
1165 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001166 std::size_t dot = timestamp.find_first_of('.');
1167 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001168 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001169 {
Jason M. Bills95820182019-04-22 16:25:34 -07001170 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001171 }
1172
1173 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001174 logEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08001175 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001176 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001177 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001178 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001179 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001180 {"Id", logEntryID},
1181 {"Message", std::move(msg)},
1182 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001183 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001184 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001185 {"Severity", std::move(severity)},
1186 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001187 return 0;
1188}
1189
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001190inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001191{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001192 BMCWEB_ROUTE(app,
1193 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001194 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001195 .methods(boost::beast::http::verb::get)(
1196 [](const crow::Request& req,
1197 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1198 uint64_t skip = 0;
1199 uint64_t top = maxEntriesPerPage; // Show max entries by default
1200 if (!getSkipParam(asyncResp, req, skip))
Jason M. Bills95820182019-04-22 16:25:34 -07001201 {
Jason M. Bills95820182019-04-22 16:25:34 -07001202 return;
1203 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001204 if (!getTopParam(asyncResp, req, top))
Jason M. Bills897967d2019-07-29 17:05:30 -07001205 {
Jason M. Bills897967d2019-07-29 17:05:30 -07001206 return;
1207 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001208 // Collections don't include the static data added by SubRoute
1209 // because it has a duplicate entry for members
1210 asyncResp->res.jsonValue["@odata.type"] =
1211 "#LogEntryCollection.LogEntryCollection";
1212 asyncResp->res.jsonValue["@odata.id"] =
1213 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1214 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1215 asyncResp->res.jsonValue["Description"] =
1216 "Collection of System Event Log Entries";
Jason M. Bills897967d2019-07-29 17:05:30 -07001217
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001218 nlohmann::json& logEntryArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001219 asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001220 logEntryArray = nlohmann::json::array();
1221 // Go through the log files and create a unique ID for each
1222 // entry
1223 std::vector<std::filesystem::path> redfishLogFiles;
1224 getRedfishLogFiles(redfishLogFiles);
1225 uint64_t entryCount = 0;
1226 std::string logEntry;
1227
1228 // Oldest logs are in the last file, so start there and loop
1229 // backwards
1230 for (auto it = redfishLogFiles.rbegin();
1231 it < redfishLogFiles.rend(); it++)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001232 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001233 std::ifstream logStream(*it);
1234 if (!logStream.is_open())
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001235 {
1236 continue;
1237 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001238
1239 // Reset the unique ID on the first entry
1240 bool firstEntry = true;
1241 while (std::getline(logStream, logEntry))
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001242 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001243 entryCount++;
1244 // Handle paging using skip (number of entries to skip
1245 // from the start) and top (number of entries to
1246 // display)
1247 if (entryCount <= skip || entryCount > skip + top)
George Liuebd45902020-08-26 14:21:10 +08001248 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001249 continue;
George Liuebd45902020-08-26 14:21:10 +08001250 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001251
1252 std::string idStr;
1253 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
George Liuebd45902020-08-26 14:21:10 +08001254 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001255 continue;
George Liuebd45902020-08-26 14:21:10 +08001256 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001257
1258 if (firstEntry)
1259 {
1260 firstEntry = false;
1261 }
1262
1263 logEntryArray.push_back({});
1264 nlohmann::json& bmcLogEntry = logEntryArray.back();
1265 if (fillEventLogEntryJson(idStr, logEntry,
1266 bmcLogEntry) != 0)
Xiaochao Ma75710de2021-01-21 17:56:02 +08001267 {
1268 messages::internalError(asyncResp->res);
1269 return;
1270 }
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001271 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001272 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001273 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1274 if (skip + top < entryCount)
Ed Tanous271584a2019-07-09 16:24:22 -07001275 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001276 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001277 "/redfish/v1/Systems/system/LogServices/EventLog/"
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001278 "Entries?$skip=" +
1279 std::to_string(skip + top);
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001280 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001281 });
1282}
Chicago Duan336e96c2019-07-15 14:22:08 +08001283
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001284inline void requestRoutesJournalEventLogEntry(App& app)
1285{
1286 BMCWEB_ROUTE(
1287 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001288 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001289 .methods(boost::beast::http::verb::get)(
1290 [](const crow::Request&,
1291 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1292 const std::string& param) {
1293 const std::string& targetID = param;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001294
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001295 // Go through the log files and check the unique ID for each
1296 // entry to find the target entry
1297 std::vector<std::filesystem::path> redfishLogFiles;
1298 getRedfishLogFiles(redfishLogFiles);
1299 std::string logEntry;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001300
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001301 // Oldest logs are in the last file, so start there and loop
1302 // backwards
1303 for (auto it = redfishLogFiles.rbegin();
1304 it < redfishLogFiles.rend(); it++)
1305 {
1306 std::ifstream logStream(*it);
1307 if (!logStream.is_open())
1308 {
1309 continue;
1310 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001311
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001312 // Reset the unique ID on the first entry
1313 bool firstEntry = true;
1314 while (std::getline(logStream, logEntry))
1315 {
1316 std::string idStr;
1317 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1318 {
1319 continue;
1320 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001321
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001322 if (firstEntry)
1323 {
1324 firstEntry = false;
1325 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001326
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001327 if (idStr == targetID)
1328 {
1329 if (fillEventLogEntryJson(
1330 idStr, logEntry,
1331 asyncResp->res.jsonValue) != 0)
1332 {
1333 messages::internalError(asyncResp->res);
1334 return;
1335 }
1336 return;
1337 }
1338 }
1339 }
1340 // Requested ID was not found
1341 messages::resourceMissingAtURI(asyncResp->res, targetID);
1342 });
1343}
1344
1345inline void requestRoutesDBusEventLogEntryCollection(App& app)
1346{
1347 BMCWEB_ROUTE(app,
1348 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001349 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001350 .methods(
1351 boost::beast::http::verb::
1352 get)([](const crow::Request&,
1353 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1354 // Collections don't include the static data added by SubRoute
1355 // because it has a duplicate entry for members
1356 asyncResp->res.jsonValue["@odata.type"] =
1357 "#LogEntryCollection.LogEntryCollection";
1358 asyncResp->res.jsonValue["@odata.id"] =
1359 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1360 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1361 asyncResp->res.jsonValue["Description"] =
1362 "Collection of System Event Log Entries";
1363
1364 // DBus implementation of EventLog/Entries
1365 // Make call to Logging Service to find all log entry objects
Xiaochao Ma75710de2021-01-21 17:56:02 +08001366 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001367 [asyncResp](const boost::system::error_code ec,
1368 GetManagedObjectsType& resp) {
Xiaochao Ma75710de2021-01-21 17:56:02 +08001369 if (ec)
1370 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001371 // TODO Handle for specific error code
1372 BMCWEB_LOG_ERROR
1373 << "getLogEntriesIfaceData resp_handler got error "
1374 << ec;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001375 messages::internalError(asyncResp->res);
1376 return;
1377 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001378 nlohmann::json& entriesArray =
1379 asyncResp->res.jsonValue["Members"];
1380 entriesArray = nlohmann::json::array();
1381 for (auto& objectPath : resp)
1382 {
1383 uint32_t* id = nullptr;
1384 std::time_t timestamp{};
1385 std::time_t updateTimestamp{};
1386 std::string* severity = nullptr;
1387 std::string* message = nullptr;
1388 std::string* filePath = nullptr;
1389 bool resolved = false;
1390 for (auto& interfaceMap : objectPath.second)
1391 {
1392 if (interfaceMap.first ==
1393 "xyz.openbmc_project.Logging.Entry")
1394 {
1395 for (auto& propertyMap : interfaceMap.second)
1396 {
1397 if (propertyMap.first == "Id")
1398 {
1399 id = std::get_if<uint32_t>(
1400 &propertyMap.second);
1401 }
1402 else if (propertyMap.first == "Timestamp")
1403 {
1404 const uint64_t* millisTimeStamp =
1405 std::get_if<uint64_t>(
1406 &propertyMap.second);
1407 if (millisTimeStamp != nullptr)
1408 {
1409 timestamp =
1410 crow::utility::getTimestamp(
1411 *millisTimeStamp);
1412 }
1413 }
1414 else if (propertyMap.first ==
1415 "UpdateTimestamp")
1416 {
1417 const uint64_t* millisTimeStamp =
1418 std::get_if<uint64_t>(
1419 &propertyMap.second);
1420 if (millisTimeStamp != nullptr)
1421 {
1422 updateTimestamp =
1423 crow::utility::getTimestamp(
1424 *millisTimeStamp);
1425 }
1426 }
1427 else if (propertyMap.first == "Severity")
1428 {
1429 severity = std::get_if<std::string>(
1430 &propertyMap.second);
1431 }
1432 else if (propertyMap.first == "Message")
1433 {
1434 message = std::get_if<std::string>(
1435 &propertyMap.second);
1436 }
1437 else if (propertyMap.first == "Resolved")
1438 {
1439 bool* resolveptr = std::get_if<bool>(
1440 &propertyMap.second);
1441 if (resolveptr == nullptr)
1442 {
1443 messages::internalError(
1444 asyncResp->res);
1445 return;
1446 }
1447 resolved = *resolveptr;
1448 }
1449 }
1450 if (id == nullptr || message == nullptr ||
1451 severity == nullptr)
1452 {
1453 messages::internalError(asyncResp->res);
1454 return;
1455 }
1456 }
1457 else if (interfaceMap.first ==
1458 "xyz.openbmc_project.Common.FilePath")
1459 {
1460 for (auto& propertyMap : interfaceMap.second)
1461 {
1462 if (propertyMap.first == "Path")
1463 {
1464 filePath = std::get_if<std::string>(
1465 &propertyMap.second);
1466 }
1467 }
1468 }
1469 }
1470 // Object path without the
1471 // xyz.openbmc_project.Logging.Entry interface, ignore
1472 // and continue.
1473 if (id == nullptr || message == nullptr ||
1474 severity == nullptr)
1475 {
1476 continue;
1477 }
1478 entriesArray.push_back({});
1479 nlohmann::json& thisEntry = entriesArray.back();
1480 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1481 thisEntry["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001482 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001483 std::to_string(*id);
1484 thisEntry["Name"] = "System Event Log Entry";
1485 thisEntry["Id"] = std::to_string(*id);
1486 thisEntry["Message"] = *message;
1487 thisEntry["Resolved"] = resolved;
1488 thisEntry["EntryType"] = "Event";
1489 thisEntry["Severity"] =
1490 translateSeverityDbusToRedfish(*severity);
1491 thisEntry["Created"] =
1492 crow::utility::getDateTime(timestamp);
1493 thisEntry["Modified"] =
1494 crow::utility::getDateTime(updateTimestamp);
1495 if (filePath != nullptr)
1496 {
1497 thisEntry["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001498 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001499 std::to_string(*id) + "/attachment";
1500 }
1501 }
1502 std::sort(entriesArray.begin(), entriesArray.end(),
1503 [](const nlohmann::json& left,
1504 const nlohmann::json& right) {
1505 return (left["Id"] <= right["Id"]);
1506 });
1507 asyncResp->res.jsonValue["Members@odata.count"] =
1508 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001509 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001510 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1511 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1512 });
1513}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001514
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001515inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001516{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001517 BMCWEB_ROUTE(
1518 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001519 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001520 .methods(boost::beast::http::verb::get)(
1521 [](const crow::Request&,
1522 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1523 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001524
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001525 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001526 std::string entryID = param;
1527 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001528
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001529 // DBus implementation of EventLog/Entries
1530 // Make call to Logging Service to find all log entry objects
1531 crow::connections::systemBus->async_method_call(
1532 [asyncResp, entryID](const boost::system::error_code ec,
1533 GetManagedPropertyType& resp) {
1534 if (ec.value() == EBADR)
1535 {
1536 messages::resourceNotFound(
1537 asyncResp->res, "EventLogEntry", entryID);
1538 return;
1539 }
1540 if (ec)
1541 {
George Liu0fda0f12021-11-16 10:06:17 +08001542 BMCWEB_LOG_ERROR
1543 << "EventLogEntry (DBus) resp_handler got error "
1544 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001545 messages::internalError(asyncResp->res);
1546 return;
1547 }
1548 uint32_t* id = nullptr;
1549 std::time_t timestamp{};
1550 std::time_t updateTimestamp{};
1551 std::string* severity = nullptr;
1552 std::string* message = nullptr;
1553 std::string* filePath = nullptr;
1554 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001555
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001556 for (auto& propertyMap : resp)
1557 {
1558 if (propertyMap.first == "Id")
1559 {
1560 id = std::get_if<uint32_t>(&propertyMap.second);
1561 }
1562 else if (propertyMap.first == "Timestamp")
1563 {
1564 const uint64_t* millisTimeStamp =
1565 std::get_if<uint64_t>(&propertyMap.second);
1566 if (millisTimeStamp != nullptr)
1567 {
1568 timestamp = crow::utility::getTimestamp(
1569 *millisTimeStamp);
1570 }
1571 }
1572 else if (propertyMap.first == "UpdateTimestamp")
1573 {
1574 const uint64_t* millisTimeStamp =
1575 std::get_if<uint64_t>(&propertyMap.second);
1576 if (millisTimeStamp != nullptr)
1577 {
1578 updateTimestamp =
1579 crow::utility::getTimestamp(
1580 *millisTimeStamp);
1581 }
1582 }
1583 else if (propertyMap.first == "Severity")
1584 {
1585 severity = std::get_if<std::string>(
1586 &propertyMap.second);
1587 }
1588 else if (propertyMap.first == "Message")
1589 {
1590 message = std::get_if<std::string>(
1591 &propertyMap.second);
1592 }
1593 else if (propertyMap.first == "Resolved")
1594 {
1595 bool* resolveptr =
1596 std::get_if<bool>(&propertyMap.second);
1597 if (resolveptr == nullptr)
1598 {
1599 messages::internalError(asyncResp->res);
1600 return;
1601 }
1602 resolved = *resolveptr;
1603 }
1604 else if (propertyMap.first == "Path")
1605 {
1606 filePath = std::get_if<std::string>(
1607 &propertyMap.second);
1608 }
1609 }
1610 if (id == nullptr || message == nullptr ||
1611 severity == nullptr)
1612 {
1613 messages::internalError(asyncResp->res);
1614 return;
1615 }
1616 asyncResp->res.jsonValue["@odata.type"] =
1617 "#LogEntry.v1_8_0.LogEntry";
1618 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001619 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001620 std::to_string(*id);
1621 asyncResp->res.jsonValue["Name"] =
1622 "System Event Log Entry";
1623 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1624 asyncResp->res.jsonValue["Message"] = *message;
1625 asyncResp->res.jsonValue["Resolved"] = resolved;
1626 asyncResp->res.jsonValue["EntryType"] = "Event";
1627 asyncResp->res.jsonValue["Severity"] =
1628 translateSeverityDbusToRedfish(*severity);
1629 asyncResp->res.jsonValue["Created"] =
1630 crow::utility::getDateTime(timestamp);
1631 asyncResp->res.jsonValue["Modified"] =
1632 crow::utility::getDateTime(updateTimestamp);
1633 if (filePath != nullptr)
1634 {
1635 asyncResp->res.jsonValue["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001636 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001637 std::to_string(*id);
1638 }
1639 },
1640 "xyz.openbmc_project.Logging",
1641 "/xyz/openbmc_project/logging/entry/" + entryID,
1642 "org.freedesktop.DBus.Properties", "GetAll", "");
1643 });
1644
1645 BMCWEB_ROUTE(
1646 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001647 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001648 .methods(boost::beast::http::verb::patch)(
1649 [](const crow::Request& req,
1650 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1651 const std::string& entryId) {
1652 std::optional<bool> resolved;
1653
1654 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1655 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001656 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001657 return;
1658 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001659 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001660
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001661 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001662 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001663 if (ec)
1664 {
1665 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1666 messages::internalError(asyncResp->res);
1667 return;
1668 }
1669 },
1670 "xyz.openbmc_project.Logging",
1671 "/xyz/openbmc_project/logging/entry/" + entryId,
1672 "org.freedesktop.DBus.Properties", "Set",
1673 "xyz.openbmc_project.Logging.Entry", "Resolved",
1674 std::variant<bool>(*resolved));
1675 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001676
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001677 BMCWEB_ROUTE(
1678 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001679 .privileges(redfish::privileges::deleteLogEntry)
1680
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001681 .methods(boost::beast::http::verb::delete_)(
1682 [](const crow::Request&,
1683 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1684 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001685
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001686 {
1687 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001688
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001689 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001690
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001691 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001692
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001693 // Process response from Logging service.
1694 auto respHandler = [asyncResp, entryID](
1695 const boost::system::error_code ec) {
1696 BMCWEB_LOG_DEBUG
1697 << "EventLogEntry (DBus) doDelete callback: Done";
1698 if (ec)
1699 {
1700 if (ec.value() == EBADR)
1701 {
1702 messages::resourceNotFound(asyncResp->res,
1703 "LogEntry", entryID);
1704 return;
1705 }
1706 // TODO Handle for specific error code
George Liu0fda0f12021-11-16 10:06:17 +08001707 BMCWEB_LOG_ERROR
1708 << "EventLogEntry (DBus) doDelete respHandler got error "
1709 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001710 asyncResp->res.result(
1711 boost::beast::http::status::internal_server_error);
1712 return;
1713 }
1714
1715 asyncResp->res.result(boost::beast::http::status::ok);
1716 };
1717
1718 // Make call to Logging service to request Delete Log
1719 crow::connections::systemBus->async_method_call(
1720 respHandler, "xyz.openbmc_project.Logging",
1721 "/xyz/openbmc_project/logging/entry/" + entryID,
1722 "xyz.openbmc_project.Object.Delete", "Delete");
1723 });
1724}
1725
1726inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001727{
George Liu0fda0f12021-11-16 10:06:17 +08001728 BMCWEB_ROUTE(
1729 app,
1730 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001731 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001732 .methods(boost::beast::http::verb::get)(
1733 [](const crow::Request& req,
1734 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1735 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001736
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001737 {
George Liu647b3cd2021-07-05 12:43:56 +08001738 if (!http_helpers::isOctetAccepted(
1739 req.getHeaderValue("Accept")))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001740 {
1741 asyncResp->res.result(
1742 boost::beast::http::status::bad_request);
1743 return;
1744 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001745
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001746 std::string entryID = param;
1747 dbus::utility::escapePathForDbus(entryID);
1748
1749 crow::connections::systemBus->async_method_call(
1750 [asyncResp,
1751 entryID](const boost::system::error_code ec,
1752 const sdbusplus::message::unix_fd& unixfd) {
1753 if (ec.value() == EBADR)
1754 {
1755 messages::resourceNotFound(
1756 asyncResp->res, "EventLogAttachment", entryID);
1757 return;
1758 }
1759 if (ec)
1760 {
1761 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1762 messages::internalError(asyncResp->res);
1763 return;
1764 }
1765
1766 int fd = -1;
1767 fd = dup(unixfd);
1768 if (fd == -1)
1769 {
1770 messages::internalError(asyncResp->res);
1771 return;
1772 }
1773
1774 long long int size = lseek(fd, 0, SEEK_END);
1775 if (size == -1)
1776 {
1777 messages::internalError(asyncResp->res);
1778 return;
1779 }
1780
1781 // Arbitrary max size of 64kb
1782 constexpr int maxFileSize = 65536;
1783 if (size > maxFileSize)
1784 {
1785 BMCWEB_LOG_ERROR
1786 << "File size exceeds maximum allowed size of "
1787 << maxFileSize;
1788 messages::internalError(asyncResp->res);
1789 return;
1790 }
1791 std::vector<char> data(static_cast<size_t>(size));
1792 long long int rc = lseek(fd, 0, SEEK_SET);
1793 if (rc == -1)
1794 {
1795 messages::internalError(asyncResp->res);
1796 return;
1797 }
1798 rc = read(fd, data.data(), data.size());
1799 if ((rc == -1) || (rc != size))
1800 {
1801 messages::internalError(asyncResp->res);
1802 return;
1803 }
1804 close(fd);
1805
1806 std::string_view strData(data.data(), data.size());
1807 std::string output =
1808 crow::utility::base64encode(strData);
1809
1810 asyncResp->res.addHeader("Content-Type",
1811 "application/octet-stream");
1812 asyncResp->res.addHeader("Content-Transfer-Encoding",
1813 "Base64");
1814 asyncResp->res.body() = std::move(output);
1815 },
1816 "xyz.openbmc_project.Logging",
1817 "/xyz/openbmc_project/logging/entry/" + entryID,
1818 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1819 });
1820}
1821
Spencer Kub7028eb2021-10-26 15:27:35 +08001822constexpr const char* hostLoggerFolderPath = "/var/log/console";
1823
1824inline bool
1825 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1826 std::vector<std::filesystem::path>& hostLoggerFiles)
1827{
1828 std::error_code ec;
1829 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1830 if (ec)
1831 {
1832 BMCWEB_LOG_ERROR << ec.message();
1833 return false;
1834 }
1835 for (const std::filesystem::directory_entry& it : logPath)
1836 {
1837 std::string filename = it.path().filename();
1838 // Prefix of each log files is "log". Find the file and save the
1839 // path
1840 if (boost::starts_with(filename, "log"))
1841 {
1842 hostLoggerFiles.emplace_back(it.path());
1843 }
1844 }
1845 // As the log files rotate, they are appended with a ".#" that is higher for
1846 // the older logs. Since we start from oldest logs, sort the name in
1847 // descending order.
1848 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1849 AlphanumLess<std::string>());
1850
1851 return true;
1852}
1853
1854inline bool
1855 getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1856 uint64_t& skip, uint64_t& top,
1857 std::vector<std::string>& logEntries, size_t& logCount)
1858{
1859 GzFileReader logFile;
1860
1861 // Go though all log files and expose host logs.
1862 for (const std::filesystem::path& it : hostLoggerFiles)
1863 {
1864 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1865 {
1866 BMCWEB_LOG_ERROR << "fail to expose host logs";
1867 return false;
1868 }
1869 }
1870 // Get lastMessage from constructor by getter
1871 std::string lastMessage = logFile.getLastMessage();
1872 if (!lastMessage.empty())
1873 {
1874 logCount++;
1875 if (logCount > skip && logCount <= (skip + top))
1876 {
1877 logEntries.push_back(lastMessage);
1878 }
1879 }
1880 return true;
1881}
1882
1883inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1884 const std::string& msg,
1885 nlohmann::json& logEntryJson)
1886{
1887 // Fill in the log entry with the gathered data.
1888 logEntryJson = {
1889 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1890 {"@odata.id",
1891 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1892 logEntryID},
1893 {"Name", "Host Logger Entry"},
1894 {"Id", logEntryID},
1895 {"Message", msg},
1896 {"EntryType", "Oem"},
1897 {"Severity", "OK"},
1898 {"OemRecordFormat", "Host Logger Entry"}};
1899}
1900
1901inline void requestRoutesSystemHostLogger(App& app)
1902{
1903 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1904 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08001905 .methods(
1906 boost::beast::http::verb::
1907 get)([](const crow::Request&,
1908 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1909 asyncResp->res.jsonValue["@odata.id"] =
1910 "/redfish/v1/Systems/system/LogServices/HostLogger";
1911 asyncResp->res.jsonValue["@odata.type"] =
1912 "#LogService.v1_1_0.LogService";
1913 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1914 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1915 asyncResp->res.jsonValue["Id"] = "HostLogger";
1916 asyncResp->res.jsonValue["Entries"] = {
1917 {"@odata.id",
1918 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1919 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001920}
1921
1922inline void requestRoutesSystemHostLoggerCollection(App& app)
1923{
1924 BMCWEB_ROUTE(app,
1925 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1926 .privileges(redfish::privileges::getLogEntry)
George Liu0fda0f12021-11-16 10:06:17 +08001927 .methods(
1928 boost::beast::http::verb::
1929 get)([](const crow::Request& req,
1930 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1931 uint64_t skip = 0;
1932 uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1933 // default, allow range 1 to
1934 // 1000 entries per page.
1935 if (!getSkipParam(asyncResp, req, skip))
1936 {
1937 return;
1938 }
1939 if (!getTopParam(asyncResp, req, top))
1940 {
1941 return;
1942 }
1943 asyncResp->res.jsonValue["@odata.id"] =
1944 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1945 asyncResp->res.jsonValue["@odata.type"] =
1946 "#LogEntryCollection.LogEntryCollection";
1947 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1948 asyncResp->res.jsonValue["Description"] =
1949 "Collection of HostLogger Entries";
1950 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1951 logEntryArray = nlohmann::json::array();
1952 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001953
George Liu0fda0f12021-11-16 10:06:17 +08001954 std::vector<std::filesystem::path> hostLoggerFiles;
1955 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1956 {
1957 BMCWEB_LOG_ERROR << "fail to get host log file path";
1958 return;
1959 }
1960
1961 size_t logCount = 0;
1962 // This vector only store the entries we want to expose that
1963 // control by skip and top.
1964 std::vector<std::string> logEntries;
1965 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1966 logCount))
1967 {
1968 messages::internalError(asyncResp->res);
1969 return;
1970 }
1971 // If vector is empty, that means skip value larger than total
1972 // log count
1973 if (logEntries.size() == 0)
1974 {
1975 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1976 return;
1977 }
1978 if (logEntries.size() > 0)
1979 {
1980 for (size_t i = 0; i < logEntries.size(); i++)
Spencer Kub7028eb2021-10-26 15:27:35 +08001981 {
George Liu0fda0f12021-11-16 10:06:17 +08001982 logEntryArray.push_back({});
1983 nlohmann::json& hostLogEntry = logEntryArray.back();
1984 fillHostLoggerEntryJson(std::to_string(skip + i),
1985 logEntries[i], hostLogEntry);
Spencer Kub7028eb2021-10-26 15:27:35 +08001986 }
1987
George Liu0fda0f12021-11-16 10:06:17 +08001988 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1989 if (skip + top < logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001990 {
George Liu0fda0f12021-11-16 10:06:17 +08001991 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1992 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1993 std::to_string(skip + top);
Spencer Kub7028eb2021-10-26 15:27:35 +08001994 }
George Liu0fda0f12021-11-16 10:06:17 +08001995 }
1996 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001997}
1998
1999inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2000{
2001 BMCWEB_ROUTE(
2002 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2003 .privileges(redfish::privileges::getLogEntry)
2004 .methods(boost::beast::http::verb::get)(
2005 [](const crow::Request&,
2006 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2007 const std::string& param) {
2008 const std::string& targetID = param;
2009
2010 uint64_t idInt = 0;
2011 auto [ptr, ec] = std::from_chars(
2012 targetID.data(), targetID.data() + targetID.size(), idInt);
2013 if (ec == std::errc::invalid_argument)
2014 {
2015 messages::resourceMissingAtURI(asyncResp->res, targetID);
2016 return;
2017 }
2018 if (ec == std::errc::result_out_of_range)
2019 {
2020 messages::resourceMissingAtURI(asyncResp->res, targetID);
2021 return;
2022 }
2023
2024 std::vector<std::filesystem::path> hostLoggerFiles;
2025 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2026 {
2027 BMCWEB_LOG_ERROR << "fail to get host log file path";
2028 return;
2029 }
2030
2031 size_t logCount = 0;
2032 uint64_t top = 1;
2033 std::vector<std::string> logEntries;
2034 // We can get specific entry by skip and top. For example, if we
2035 // want to get nth entry, we can set skip = n-1 and top = 1 to
2036 // get that entry
2037 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2038 logEntries, logCount))
2039 {
2040 messages::internalError(asyncResp->res);
2041 return;
2042 }
2043
2044 if (!logEntries.empty())
2045 {
2046 fillHostLoggerEntryJson(targetID, logEntries[0],
2047 asyncResp->res.jsonValue);
2048 return;
2049 }
2050
2051 // Requested ID was not found
2052 messages::resourceMissingAtURI(asyncResp->res, targetID);
2053 });
2054}
2055
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002056inline void requestRoutesBMCLogServiceCollection(App& app)
2057{
2058 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002059 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002060 .methods(boost::beast::http::verb::get)(
2061 [](const crow::Request&,
2062 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2063 // Collections don't include the static data added by SubRoute
2064 // because it has a duplicate entry for members
2065 asyncResp->res.jsonValue["@odata.type"] =
2066 "#LogServiceCollection.LogServiceCollection";
2067 asyncResp->res.jsonValue["@odata.id"] =
2068 "/redfish/v1/Managers/bmc/LogServices";
2069 asyncResp->res.jsonValue["Name"] =
2070 "Open BMC Log Services Collection";
2071 asyncResp->res.jsonValue["Description"] =
2072 "Collection of LogServices for this Manager";
2073 nlohmann::json& logServiceArray =
2074 asyncResp->res.jsonValue["Members"];
2075 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002076#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002077 logServiceArray.push_back(
2078 {{"@odata.id",
2079 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002080#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002081#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002082 logServiceArray.push_back(
2083 {{"@odata.id",
2084 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002085#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002086 asyncResp->res.jsonValue["Members@odata.count"] =
2087 logServiceArray.size();
2088 });
2089}
Ed Tanous1da66f72018-07-27 16:13:37 -07002090
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002091inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002092{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002093 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002094 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002095 .methods(boost::beast::http::verb::get)(
2096 [](const crow::Request&,
2097 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07002098
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002099 {
2100 asyncResp->res.jsonValue["@odata.type"] =
2101 "#LogService.v1_1_0.LogService";
2102 asyncResp->res.jsonValue["@odata.id"] =
2103 "/redfish/v1/Managers/bmc/LogServices/Journal";
2104 asyncResp->res.jsonValue["Name"] =
2105 "Open BMC Journal Log Service";
2106 asyncResp->res.jsonValue["Description"] =
2107 "BMC Journal Log Service";
2108 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2109 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302110
2111 std::pair<std::string, std::string> redfishDateTimeOffset =
2112 crow::utility::getDateTimeOffsetNow();
2113 asyncResp->res.jsonValue["DateTime"] =
2114 redfishDateTimeOffset.first;
2115 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2116 redfishDateTimeOffset.second;
2117
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002118 asyncResp->res.jsonValue["Entries"] = {
2119 {"@odata.id",
2120 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
2121 });
2122}
Jason M. Billse1f26342018-07-18 12:12:00 -07002123
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002124static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2125 sd_journal* journal,
2126 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002127{
2128 // Get the Log Entry contents
2129 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002130
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002131 std::string message;
2132 std::string_view syslogID;
2133 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2134 if (ret < 0)
2135 {
2136 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2137 << strerror(-ret);
2138 }
2139 if (!syslogID.empty())
2140 {
2141 message += std::string(syslogID) + ": ";
2142 }
2143
Ed Tanous39e77502019-03-04 17:35:53 -08002144 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002145 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002146 if (ret < 0)
2147 {
2148 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2149 return 1;
2150 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002151 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002152
2153 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002154 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002155 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002156 if (ret < 0)
2157 {
2158 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002159 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002160
2161 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002162 std::string entryTimeStr;
2163 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002164 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002165 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002166 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002167
2168 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002169 bmcJournalLogEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08002170 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002171 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2172 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07002173 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002174 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002175 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07002176 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06002177 {"Severity", severity <= 2 ? "Critical"
2178 : severity <= 4 ? "Warning"
2179 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07002180 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07002181 {"Created", std::move(entryTimeStr)}};
2182 return 0;
2183}
2184
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002185inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002186{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002187 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002188 .privileges(redfish::privileges::getLogEntryCollection)
George Liu0fda0f12021-11-16 10:06:17 +08002189 .methods(
2190 boost::beast::http::verb::
2191 get)([](const crow::Request& req,
2192 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2193 static constexpr const long maxEntriesPerPage = 1000;
2194 uint64_t skip = 0;
2195 uint64_t top = maxEntriesPerPage; // Show max entries by default
2196 if (!getSkipParam(asyncResp, req, skip))
2197 {
2198 return;
2199 }
2200 if (!getTopParam(asyncResp, req, top))
2201 {
2202 return;
2203 }
2204 // Collections don't include the static data added by SubRoute
2205 // because it has a duplicate entry for members
2206 asyncResp->res.jsonValue["@odata.type"] =
2207 "#LogEntryCollection.LogEntryCollection";
2208 asyncResp->res.jsonValue["@odata.id"] =
2209 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2210 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2211 asyncResp->res.jsonValue["Description"] =
2212 "Collection of BMC Journal Entries";
2213 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2214 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002215
George Liu0fda0f12021-11-16 10:06:17 +08002216 // Go through the journal and use the timestamp to create a
2217 // unique ID for each entry
2218 sd_journal* journalTmp = nullptr;
2219 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2220 if (ret < 0)
2221 {
2222 BMCWEB_LOG_ERROR << "failed to open journal: "
2223 << strerror(-ret);
2224 messages::internalError(asyncResp->res);
2225 return;
2226 }
2227 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2228 journalTmp, sd_journal_close);
2229 journalTmp = nullptr;
2230 uint64_t entryCount = 0;
2231 // Reset the unique ID on the first entry
2232 bool firstEntry = true;
2233 SD_JOURNAL_FOREACH(journal.get())
2234 {
2235 entryCount++;
2236 // Handle paging using skip (number of entries to skip from
2237 // the start) and top (number of entries to display)
2238 if (entryCount <= skip || entryCount > skip + top)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002239 {
George Liu0fda0f12021-11-16 10:06:17 +08002240 continue;
2241 }
2242
2243 std::string idStr;
2244 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2245 {
2246 continue;
2247 }
2248
2249 if (firstEntry)
2250 {
2251 firstEntry = false;
2252 }
2253
2254 logEntryArray.push_back({});
2255 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2256 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2257 bmcJournalLogEntry) != 0)
2258 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002259 messages::internalError(asyncResp->res);
2260 return;
2261 }
George Liu0fda0f12021-11-16 10:06:17 +08002262 }
2263 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2264 if (skip + top < entryCount)
2265 {
2266 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2267 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2268 std::to_string(skip + top);
2269 }
2270 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002271}
Jason M. Billse1f26342018-07-18 12:12:00 -07002272
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002273inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002274{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002275 BMCWEB_ROUTE(app,
2276 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002277 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002278 .methods(boost::beast::http::verb::get)(
2279 [](const crow::Request&,
2280 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2281 const std::string& entryID) {
2282 // Convert the unique ID back to a timestamp to find the entry
2283 uint64_t ts = 0;
2284 uint64_t index = 0;
2285 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2286 {
2287 return;
2288 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002289
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002290 sd_journal* journalTmp = nullptr;
2291 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2292 if (ret < 0)
2293 {
2294 BMCWEB_LOG_ERROR << "failed to open journal: "
2295 << strerror(-ret);
2296 messages::internalError(asyncResp->res);
2297 return;
2298 }
2299 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2300 journal(journalTmp, sd_journal_close);
2301 journalTmp = nullptr;
2302 // Go to the timestamp in the log and move to the entry at the
2303 // index tracking the unique ID
2304 std::string idStr;
2305 bool firstEntry = true;
2306 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2307 if (ret < 0)
2308 {
2309 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2310 << strerror(-ret);
2311 messages::internalError(asyncResp->res);
2312 return;
2313 }
2314 for (uint64_t i = 0; i <= index; i++)
2315 {
2316 sd_journal_next(journal.get());
2317 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2318 {
2319 messages::internalError(asyncResp->res);
2320 return;
2321 }
2322 if (firstEntry)
2323 {
2324 firstEntry = false;
2325 }
2326 }
2327 // Confirm that the entry ID matches what was requested
2328 if (idStr != entryID)
2329 {
2330 messages::resourceMissingAtURI(asyncResp->res, entryID);
2331 return;
2332 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002333
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002334 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2335 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002336 {
2337 messages::internalError(asyncResp->res);
2338 return;
2339 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002340 });
2341}
2342
2343inline void requestRoutesBMCDumpService(App& app)
2344{
2345 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002346 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08002347 .methods(
2348 boost::beast::http::verb::
2349 get)([](const crow::Request&,
2350 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2351 asyncResp->res.jsonValue["@odata.id"] =
2352 "/redfish/v1/Managers/bmc/LogServices/Dump";
2353 asyncResp->res.jsonValue["@odata.type"] =
2354 "#LogService.v1_2_0.LogService";
2355 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2356 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2357 asyncResp->res.jsonValue["Id"] = "Dump";
2358 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302359
George Liu0fda0f12021-11-16 10:06:17 +08002360 std::pair<std::string, std::string> redfishDateTimeOffset =
2361 crow::utility::getDateTimeOffsetNow();
2362 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2363 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2364 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302365
George Liu0fda0f12021-11-16 10:06:17 +08002366 asyncResp->res.jsonValue["Entries"] = {
2367 {"@odata.id",
2368 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2369 asyncResp->res.jsonValue["Actions"] = {
2370 {"#LogService.ClearLog",
2371 {{"target",
2372 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2373 {"#LogService.CollectDiagnosticData",
2374 {{"target",
2375 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
2376 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002377}
2378
2379inline void requestRoutesBMCDumpEntryCollection(App& app)
2380{
2381
2382 /**
2383 * Functions triggers appropriate requests on DBus
2384 */
2385 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002386 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002387 .methods(boost::beast::http::verb::get)(
2388 [](const crow::Request&,
2389 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2390 asyncResp->res.jsonValue["@odata.type"] =
2391 "#LogEntryCollection.LogEntryCollection";
2392 asyncResp->res.jsonValue["@odata.id"] =
2393 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2394 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2395 asyncResp->res.jsonValue["Description"] =
2396 "Collection of BMC Dump Entries";
2397
2398 getDumpEntryCollection(asyncResp, "BMC");
2399 });
2400}
2401
2402inline void requestRoutesBMCDumpEntry(App& app)
2403{
2404 BMCWEB_ROUTE(app,
2405 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002406 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002407 .methods(boost::beast::http::verb::get)(
2408 [](const crow::Request&,
2409 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2410 const std::string& param) {
2411 getDumpEntryById(asyncResp, param, "BMC");
2412 });
2413 BMCWEB_ROUTE(app,
2414 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002415 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002416 .methods(boost::beast::http::verb::delete_)(
2417 [](const crow::Request&,
2418 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2419 const std::string& param) {
2420 deleteDumpEntry(asyncResp, param, "bmc");
2421 });
2422}
2423
2424inline void requestRoutesBMCDumpCreate(App& app)
2425{
2426
George Liu0fda0f12021-11-16 10:06:17 +08002427 BMCWEB_ROUTE(
2428 app,
2429 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002430 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002431 .methods(boost::beast::http::verb::post)(
2432 [](const crow::Request& req,
2433 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2434 createDump(asyncResp, req, "BMC");
2435 });
2436}
2437
2438inline void requestRoutesBMCDumpClear(App& app)
2439{
George Liu0fda0f12021-11-16 10:06:17 +08002440 BMCWEB_ROUTE(
2441 app,
2442 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002443 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002444 .methods(boost::beast::http::verb::post)(
2445 [](const crow::Request&,
2446 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2447 clearDump(asyncResp, "BMC");
2448 });
2449}
2450
2451inline void requestRoutesSystemDumpService(App& app)
2452{
2453 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002454 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002455 .methods(boost::beast::http::verb::get)(
2456 [](const crow::Request&,
2457 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2458
2459 {
2460 asyncResp->res.jsonValue["@odata.id"] =
2461 "/redfish/v1/Systems/system/LogServices/Dump";
2462 asyncResp->res.jsonValue["@odata.type"] =
2463 "#LogService.v1_2_0.LogService";
2464 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2465 asyncResp->res.jsonValue["Description"] =
2466 "System Dump LogService";
2467 asyncResp->res.jsonValue["Id"] = "Dump";
2468 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302469
2470 std::pair<std::string, std::string> redfishDateTimeOffset =
2471 crow::utility::getDateTimeOffsetNow();
2472 asyncResp->res.jsonValue["DateTime"] =
2473 redfishDateTimeOffset.first;
2474 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2475 redfishDateTimeOffset.second;
2476
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002477 asyncResp->res.jsonValue["Entries"] = {
2478 {"@odata.id",
2479 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2480 asyncResp->res.jsonValue["Actions"] = {
2481 {"#LogService.ClearLog",
2482 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002483 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002484 {"#LogService.CollectDiagnosticData",
2485 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002486 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002487 });
2488}
2489
2490inline void requestRoutesSystemDumpEntryCollection(App& app)
2491{
2492
2493 /**
2494 * Functions triggers appropriate requests on DBus
2495 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002496 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002497 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002498 .methods(boost::beast::http::verb::get)(
2499 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002500 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002501 asyncResp->res.jsonValue["@odata.type"] =
2502 "#LogEntryCollection.LogEntryCollection";
2503 asyncResp->res.jsonValue["@odata.id"] =
2504 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2505 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2506 asyncResp->res.jsonValue["Description"] =
2507 "Collection of System Dump Entries";
2508
2509 getDumpEntryCollection(asyncResp, "System");
2510 });
2511}
2512
2513inline void requestRoutesSystemDumpEntry(App& app)
2514{
2515 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002516 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002517 .privileges(redfish::privileges::getLogEntry)
2518
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002519 .methods(boost::beast::http::verb::get)(
2520 [](const crow::Request&,
2521 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2522 const std::string& param) {
2523 getDumpEntryById(asyncResp, param, "System");
2524 });
2525
2526 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002527 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002528 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002529 .methods(boost::beast::http::verb::delete_)(
2530 [](const crow::Request&,
2531 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2532 const std::string& param) {
2533 deleteDumpEntry(asyncResp, param, "system");
2534 });
2535}
2536
2537inline void requestRoutesSystemDumpCreate(App& app)
2538{
George Liu0fda0f12021-11-16 10:06:17 +08002539 BMCWEB_ROUTE(
2540 app,
2541 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002542 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002543 .methods(boost::beast::http::verb::post)(
2544 [](const crow::Request& req,
2545 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2546
2547 { createDump(asyncResp, req, "System"); });
2548}
2549
2550inline void requestRoutesSystemDumpClear(App& app)
2551{
George Liu0fda0f12021-11-16 10:06:17 +08002552 BMCWEB_ROUTE(
2553 app,
2554 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002555 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002556 .methods(boost::beast::http::verb::post)(
2557 [](const crow::Request&,
2558 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2559
2560 { clearDump(asyncResp, "System"); });
2561}
2562
2563inline void requestRoutesCrashdumpService(App& app)
2564{
2565 // Note: Deviated from redfish privilege registry for GET & HEAD
2566 // method for security reasons.
2567 /**
2568 * Functions triggers appropriate requests on DBus
2569 */
2570 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002571 // This is incorrect, should be:
2572 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002573 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002574 .methods(
2575 boost::beast::http::verb::
2576 get)([](const crow::Request&,
2577 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2578 // Copy over the static data to include the entries added by
2579 // SubRoute
2580 asyncResp->res.jsonValue["@odata.id"] =
2581 "/redfish/v1/Systems/system/LogServices/Crashdump";
2582 asyncResp->res.jsonValue["@odata.type"] =
2583 "#LogService.v1_2_0.LogService";
2584 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2585 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2586 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2587 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2588 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302589
2590 std::pair<std::string, std::string> redfishDateTimeOffset =
2591 crow::utility::getDateTimeOffsetNow();
2592 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2593 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2594 redfishDateTimeOffset.second;
2595
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002596 asyncResp->res.jsonValue["Entries"] = {
2597 {"@odata.id",
2598 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2599 asyncResp->res.jsonValue["Actions"] = {
2600 {"#LogService.ClearLog",
George Liu0fda0f12021-11-16 10:06:17 +08002601 {{"target",
2602 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002603 {"#LogService.CollectDiagnosticData",
George Liu0fda0f12021-11-16 10:06:17 +08002604 {{"target",
2605 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002606 });
2607}
2608
2609void inline requestRoutesCrashdumpClear(App& app)
2610{
George Liu0fda0f12021-11-16 10:06:17 +08002611 BMCWEB_ROUTE(
2612 app,
2613 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002614 // This is incorrect, should be:
2615 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002616 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002617 .methods(boost::beast::http::verb::post)(
2618 [](const crow::Request&,
2619 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2620 crow::connections::systemBus->async_method_call(
2621 [asyncResp](const boost::system::error_code ec,
2622 const std::string&) {
2623 if (ec)
2624 {
2625 messages::internalError(asyncResp->res);
2626 return;
2627 }
2628 messages::success(asyncResp->res);
2629 },
2630 crashdumpObject, crashdumpPath, deleteAllInterface,
2631 "DeleteAll");
2632 });
2633}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002634
zhanghch058d1b46d2021-04-01 11:18:24 +08002635static void
2636 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2637 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002638{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002639 auto getStoredLogCallback =
2640 [asyncResp, logID, &logEntryJson](
2641 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002642 const std::vector<std::pair<std::string, VariantType>>& params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002643 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002644 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002645 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2646 if (ec.value() ==
2647 boost::system::linux_error::bad_request_descriptor)
2648 {
2649 messages::resourceNotFound(asyncResp->res, "LogEntry",
2650 logID);
2651 }
2652 else
2653 {
2654 messages::internalError(asyncResp->res);
2655 }
2656 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002657 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002658
Johnathan Mantey043a0532020-03-10 17:15:28 -07002659 std::string timestamp{};
2660 std::string filename{};
2661 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002662 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002663
2664 if (filename.empty() || timestamp.empty())
2665 {
2666 messages::resourceMissingAtURI(asyncResp->res, logID);
2667 return;
2668 }
2669
2670 std::string crashdumpURI =
2671 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2672 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002673 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002674 {"@odata.id", "/redfish/v1/Systems/system/"
2675 "LogServices/Crashdump/Entries/" +
2676 logID},
2677 {"Name", "CPU Crashdump"},
2678 {"Id", logID},
2679 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002680 {"AdditionalDataURI", std::move(crashdumpURI)},
2681 {"DiagnosticDataType", "OEM"},
2682 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002683 {"Created", std::move(timestamp)}};
2684 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002685 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002686 std::move(getStoredLogCallback), crashdumpObject,
2687 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002688 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002689}
2690
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002691inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002692{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002693 // Note: Deviated from redfish privilege registry for GET & HEAD
2694 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002695 /**
2696 * Functions triggers appropriate requests on DBus
2697 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002698 BMCWEB_ROUTE(app,
2699 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002700 // This is incorrect, should be.
2701 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002702 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002703 .methods(
2704 boost::beast::http::verb::
2705 get)([](const crow::Request&,
2706 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2707 // Collections don't include the static data added by SubRoute
2708 // because it has a duplicate entry for members
2709 auto getLogEntriesCallback = [asyncResp](
2710 const boost::system::error_code ec,
2711 const std::vector<std::string>&
2712 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002713 if (ec)
2714 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002715 if (ec.value() !=
2716 boost::system::errc::no_such_file_or_directory)
2717 {
2718 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2719 << ec.message();
2720 messages::internalError(asyncResp->res);
2721 return;
2722 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002723 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002724 asyncResp->res.jsonValue["@odata.type"] =
2725 "#LogEntryCollection.LogEntryCollection";
2726 asyncResp->res.jsonValue["@odata.id"] =
2727 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2728 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2729 asyncResp->res.jsonValue["Description"] =
2730 "Collection of Crashdump Entries";
2731 nlohmann::json& logEntryArray =
2732 asyncResp->res.jsonValue["Members"];
2733 logEntryArray = nlohmann::json::array();
2734 std::vector<std::string> logIDs;
2735 // Get the list of log entries and build up an empty array big
2736 // enough to hold them
2737 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002738 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002739 // Get the log ID
2740 std::size_t lastPos = objpath.rfind('/');
2741 if (lastPos == std::string::npos)
2742 {
2743 continue;
2744 }
2745 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002746
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002747 // Add a space for the log entry to the array
2748 logEntryArray.push_back({});
2749 }
2750 // Now go through and set up async calls to fill in the entries
2751 size_t index = 0;
2752 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002753 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002754 // Add the log entry to the array
2755 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002756 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002757 asyncResp->res.jsonValue["Members@odata.count"] =
2758 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002759 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002760 crow::connections::systemBus->async_method_call(
2761 std::move(getLogEntriesCallback),
2762 "xyz.openbmc_project.ObjectMapper",
2763 "/xyz/openbmc_project/object_mapper",
2764 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2765 std::array<const char*, 1>{crashdumpInterface});
2766 });
2767}
Ed Tanous1da66f72018-07-27 16:13:37 -07002768
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002769inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002770{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002771 // Note: Deviated from redfish privilege registry for GET & HEAD
2772 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002773
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002774 BMCWEB_ROUTE(
2775 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002776 // this is incorrect, should be
2777 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002778 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002779 .methods(boost::beast::http::verb::get)(
2780 [](const crow::Request&,
2781 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2782 const std::string& param) {
2783 const std::string& logID = param;
2784 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2785 });
2786}
Ed Tanous1da66f72018-07-27 16:13:37 -07002787
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002788inline void requestRoutesCrashdumpFile(App& app)
2789{
2790 // Note: Deviated from redfish privilege registry for GET & HEAD
2791 // method for security reasons.
2792 BMCWEB_ROUTE(
2793 app,
2794 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002795 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002796 .methods(boost::beast::http::verb::get)(
2797 [](const crow::Request&,
2798 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2799 const std::string& logID, const std::string& fileName) {
2800 auto getStoredLogCallback =
2801 [asyncResp, logID, fileName](
2802 const boost::system::error_code ec,
2803 const std::vector<std::pair<std::string, VariantType>>&
2804 resp) {
2805 if (ec)
2806 {
2807 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2808 << ec.message();
2809 messages::internalError(asyncResp->res);
2810 return;
2811 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002812
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002813 std::string dbusFilename{};
2814 std::string dbusTimestamp{};
2815 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002816
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002817 parseCrashdumpParameters(resp, dbusFilename,
2818 dbusTimestamp, dbusFilepath);
2819
2820 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2821 dbusFilepath.empty())
2822 {
2823 messages::resourceMissingAtURI(asyncResp->res,
2824 fileName);
2825 return;
2826 }
2827
2828 // Verify the file name parameter is correct
2829 if (fileName != dbusFilename)
2830 {
2831 messages::resourceMissingAtURI(asyncResp->res,
2832 fileName);
2833 return;
2834 }
2835
2836 if (!std::filesystem::exists(dbusFilepath))
2837 {
2838 messages::resourceMissingAtURI(asyncResp->res,
2839 fileName);
2840 return;
2841 }
2842 std::ifstream ifs(dbusFilepath, std::ios::in |
2843 std::ios::binary |
2844 std::ios::ate);
2845 std::ifstream::pos_type fileSize = ifs.tellg();
2846 if (fileSize < 0)
2847 {
2848 messages::generalError(asyncResp->res);
2849 return;
2850 }
2851 ifs.seekg(0, std::ios::beg);
2852
2853 auto crashData = std::make_unique<char[]>(
2854 static_cast<unsigned int>(fileSize));
2855
2856 ifs.read(crashData.get(), static_cast<int>(fileSize));
2857
2858 // The cast to std::string is intentional in order to
2859 // use the assign() that applies move mechanics
2860 asyncResp->res.body().assign(
2861 static_cast<std::string>(crashData.get()));
2862
2863 // Configure this to be a file download when accessed
2864 // from a browser
2865 asyncResp->res.addHeader("Content-Disposition",
2866 "attachment");
2867 };
2868 crow::connections::systemBus->async_method_call(
2869 std::move(getStoredLogCallback), crashdumpObject,
2870 crashdumpPath + std::string("/") + logID,
2871 "org.freedesktop.DBus.Properties", "GetAll",
2872 crashdumpInterface);
2873 });
2874}
2875
2876inline void requestRoutesCrashdumpCollect(App& app)
2877{
2878 // Note: Deviated from redfish privilege registry for GET & HEAD
2879 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08002880 BMCWEB_ROUTE(
2881 app,
2882 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002883 // The below is incorrect; Should be ConfigureManager
2884 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002885 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002886 .methods(
2887 boost::beast::http::verb::
2888 post)([](const crow::Request& req,
2889 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2890 std::string diagnosticDataType;
2891 std::string oemDiagnosticDataType;
2892 if (!redfish::json_util::readJson(
2893 req, asyncResp->res, "DiagnosticDataType",
2894 diagnosticDataType, "OEMDiagnosticDataType",
2895 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002896 {
James Feist46229572020-02-19 15:11:58 -08002897 return;
2898 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002899
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002900 if (diagnosticDataType != "OEM")
2901 {
2902 BMCWEB_LOG_ERROR
2903 << "Only OEM DiagnosticDataType supported for Crashdump";
2904 messages::actionParameterValueFormatError(
2905 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2906 "CollectDiagnosticData");
2907 return;
2908 }
2909
2910 auto collectCrashdumpCallback = [asyncResp, req](
2911 const boost::system::error_code
2912 ec,
2913 const std::string&) {
2914 if (ec)
2915 {
2916 if (ec.value() ==
2917 boost::system::errc::operation_not_supported)
2918 {
2919 messages::resourceInStandby(asyncResp->res);
2920 }
2921 else if (ec.value() ==
2922 boost::system::errc::device_or_resource_busy)
2923 {
2924 messages::serviceTemporarilyUnavailable(asyncResp->res,
2925 "60");
2926 }
2927 else
2928 {
2929 messages::internalError(asyncResp->res);
2930 }
2931 return;
2932 }
George Liu0fda0f12021-11-16 10:06:17 +08002933 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
2934 [](boost::system::error_code err,
2935 sdbusplus::message::message&,
2936 const std::shared_ptr<task::TaskData>& taskData) {
2937 if (!err)
2938 {
2939 taskData->messages.emplace_back(
2940 messages::taskCompletedOK(
2941 std::to_string(taskData->index)));
2942 taskData->state = "Completed";
2943 }
2944 return task::completed;
2945 },
2946 "type='signal',interface='org.freedesktop.DBus."
2947 "Properties',"
2948 "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002949 task->startTimer(std::chrono::minutes(5));
2950 task->populateResp(asyncResp->res);
2951 task->payload.emplace(req);
2952 };
2953
2954 if (oemDiagnosticDataType == "OnDemand")
2955 {
2956 crow::connections::systemBus->async_method_call(
2957 std::move(collectCrashdumpCallback), crashdumpObject,
2958 crashdumpPath, crashdumpOnDemandInterface,
2959 "GenerateOnDemandLog");
2960 }
2961 else if (oemDiagnosticDataType == "Telemetry")
2962 {
2963 crow::connections::systemBus->async_method_call(
2964 std::move(collectCrashdumpCallback), crashdumpObject,
2965 crashdumpPath, crashdumpTelemetryInterface,
2966 "GenerateTelemetryLog");
2967 }
2968 else
2969 {
2970 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2971 << oemDiagnosticDataType;
2972 messages::actionParameterValueFormatError(
2973 asyncResp->res, oemDiagnosticDataType,
2974 "OEMDiagnosticDataType", "CollectDiagnosticData");
2975 return;
2976 }
2977 });
2978}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002979
Andrew Geisslercb92c032018-08-17 07:56:14 -07002980/**
2981 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2982 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002983inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002984{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002985 /**
2986 * Function handles POST method request.
2987 * The Clear Log actions does not require any parameter.The action deletes
2988 * all entries found in the Entries collection for this Log Service.
2989 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002990
George Liu0fda0f12021-11-16 10:06:17 +08002991 BMCWEB_ROUTE(
2992 app,
2993 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002994 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002995 .methods(boost::beast::http::verb::post)(
2996 [](const crow::Request&,
2997 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2998 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07002999
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003000 // Process response from Logging service.
3001 auto respHandler = [asyncResp](
3002 const boost::system::error_code ec) {
3003 BMCWEB_LOG_DEBUG
3004 << "doClearLog resp_handler callback: Done";
3005 if (ec)
3006 {
3007 // TODO Handle for specific error code
3008 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
3009 << ec;
3010 asyncResp->res.result(
3011 boost::beast::http::status::internal_server_error);
3012 return;
3013 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003014
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003015 asyncResp->res.result(
3016 boost::beast::http::status::no_content);
3017 };
3018
3019 // Make call to Logging service to request Clear Log
3020 crow::connections::systemBus->async_method_call(
3021 respHandler, "xyz.openbmc_project.Logging",
3022 "/xyz/openbmc_project/logging",
3023 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3024 });
3025}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003026
3027/****************************************************
3028 * Redfish PostCode interfaces
3029 * using DBUS interface: getPostCodesTS
3030 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003031inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003032{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003033 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003034 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08003035 .methods(
3036 boost::beast::http::verb::
3037 get)([](const crow::Request&,
3038 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3039 asyncResp->res.jsonValue = {
3040 {"@odata.id",
3041 "/redfish/v1/Systems/system/LogServices/PostCodes"},
3042 {"@odata.type", "#LogService.v1_1_0.LogService"},
3043 {"Name", "POST Code Log Service"},
3044 {"Description", "POST Code Log Service"},
3045 {"Id", "BIOS POST Code Log"},
3046 {"OverWritePolicy", "WrapsWhenFull"},
3047 {"Entries",
3048 {{"@odata.id",
3049 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05303050
George Liu0fda0f12021-11-16 10:06:17 +08003051 std::pair<std::string, std::string> redfishDateTimeOffset =
3052 crow::utility::getDateTimeOffsetNow();
3053 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3054 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3055 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303056
George Liu0fda0f12021-11-16 10:06:17 +08003057 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3058 {"target",
3059 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
3060 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003061}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003062
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003063inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003064{
George Liu0fda0f12021-11-16 10:06:17 +08003065 BMCWEB_ROUTE(
3066 app,
3067 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003068 // The following privilege is incorrect; It should be ConfigureManager
3069 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003070 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003071 .methods(boost::beast::http::verb::post)(
3072 [](const crow::Request&,
3073 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3074 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003075
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003076 // Make call to post-code service to request clear all
3077 crow::connections::systemBus->async_method_call(
3078 [asyncResp](const boost::system::error_code ec) {
3079 if (ec)
3080 {
3081 // TODO Handle for specific error code
3082 BMCWEB_LOG_ERROR
3083 << "doClearPostCodes resp_handler got error "
3084 << ec;
3085 asyncResp->res.result(boost::beast::http::status::
3086 internal_server_error);
3087 messages::internalError(asyncResp->res);
3088 return;
3089 }
3090 },
3091 "xyz.openbmc_project.State.Boot.PostCode0",
3092 "/xyz/openbmc_project/State/Boot/PostCode0",
3093 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3094 });
3095}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003096
3097static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003098 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303099 const boost::container::flat_map<
3100 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003101 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3102 const uint64_t skip = 0, const uint64_t top = 0)
3103{
3104 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003105 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05303106 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003107
3108 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003109 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003110
3111 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303112 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3113 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003114 {
3115 currentCodeIndex++;
3116 std::string postcodeEntryID =
3117 "B" + std::to_string(bootIndex) + "-" +
3118 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3119
3120 uint64_t usecSinceEpoch = code.first;
3121 uint64_t usTimeOffset = 0;
3122
3123 if (1 == currentCodeIndex)
3124 { // already incremented
3125 firstCodeTimeUs = code.first;
3126 }
3127 else
3128 {
3129 usTimeOffset = code.first - firstCodeTimeUs;
3130 }
3131
3132 // skip if no specific codeIndex is specified and currentCodeIndex does
3133 // not fall between top and skip
3134 if ((codeIndex == 0) &&
3135 (currentCodeIndex <= skip || currentCodeIndex > top))
3136 {
3137 continue;
3138 }
3139
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003140 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003141 // currentIndex
3142 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3143 {
3144 // This is done for simplicity. 1st entry is needed to calculate
3145 // time offset. To improve efficiency, one can get to the entry
3146 // directly (possibly with flatmap's nth method)
3147 continue;
3148 }
3149
3150 // currentCodeIndex is within top and skip or equal to specified code
3151 // index
3152
3153 // Get the Created time from the timestamp
3154 std::string entryTimeStr;
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -05003155 entryTimeStr = crow::utility::getDateTime(
3156 static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003157
3158 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3159 std::ostringstream hexCode;
3160 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303161 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003162 std::ostringstream timeOffsetStr;
3163 // Set Fixed -Point Notation
3164 timeOffsetStr << std::fixed;
3165 // Set precision to 4 digits
3166 timeOffsetStr << std::setprecision(4);
3167 // Add double to stream
3168 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3169 std::vector<std::string> messageArgs = {
3170 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3171
3172 // Get MessageArgs template from message registry
3173 std::string msg;
3174 if (message != nullptr)
3175 {
3176 msg = message->message;
3177
3178 // fill in this post code value
3179 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003180 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003181 {
3182 std::string argStr = "%" + std::to_string(++i);
3183 size_t argPos = msg.find(argStr);
3184 if (argPos != std::string::npos)
3185 {
3186 msg.replace(argPos, argStr.length(), messageArg);
3187 }
3188 }
3189 }
3190
Tim Leed4342a92020-04-27 11:47:58 +08003191 // Get Severity template from message registry
3192 std::string severity;
3193 if (message != nullptr)
3194 {
3195 severity = message->severity;
3196 }
3197
ZhikuiRena3316fc2020-01-29 14:58:08 -08003198 // add to AsyncResp
3199 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003200 nlohmann::json& bmcLogEntry = logEntryArray.back();
George Liu0fda0f12021-11-16 10:06:17 +08003201 bmcLogEntry = {
3202 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
3203 {"@odata.id",
3204 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3205 postcodeEntryID},
3206 {"Name", "POST Code Log Entry"},
3207 {"Id", postcodeEntryID},
3208 {"Message", std::move(msg)},
3209 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3210 {"MessageArgs", std::move(messageArgs)},
3211 {"EntryType", "Event"},
3212 {"Severity", std::move(severity)},
3213 {"Created", entryTimeStr}};
George Liu647b3cd2021-07-05 12:43:56 +08003214 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3215 {
3216 bmcLogEntry["AdditionalDataURI"] =
3217 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3218 postcodeEntryID + "/attachment";
3219 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003220 }
3221}
3222
zhanghch058d1b46d2021-04-01 11:18:24 +08003223static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003224 const uint16_t bootIndex,
3225 const uint64_t codeIndex)
3226{
3227 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303228 [aResp, bootIndex,
3229 codeIndex](const boost::system::error_code ec,
3230 const boost::container::flat_map<
3231 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3232 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003233 if (ec)
3234 {
3235 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3236 messages::internalError(aResp->res);
3237 return;
3238 }
3239
3240 // skip the empty postcode boots
3241 if (postcode.empty())
3242 {
3243 return;
3244 }
3245
3246 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3247
3248 aResp->res.jsonValue["Members@odata.count"] =
3249 aResp->res.jsonValue["Members"].size();
3250 },
Jonathan Doman15124762021-01-07 17:54:17 -08003251 "xyz.openbmc_project.State.Boot.PostCode0",
3252 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003253 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3254 bootIndex);
3255}
3256
zhanghch058d1b46d2021-04-01 11:18:24 +08003257static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003258 const uint16_t bootIndex,
3259 const uint16_t bootCount,
3260 const uint64_t entryCount, const uint64_t skip,
3261 const uint64_t top)
3262{
3263 crow::connections::systemBus->async_method_call(
3264 [aResp, bootIndex, bootCount, entryCount, skip,
3265 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303266 const boost::container::flat_map<
3267 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3268 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003269 if (ec)
3270 {
3271 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3272 messages::internalError(aResp->res);
3273 return;
3274 }
3275
3276 uint64_t endCount = entryCount;
3277 if (!postcode.empty())
3278 {
3279 endCount = entryCount + postcode.size();
3280
3281 if ((skip < endCount) && ((top + skip) > entryCount))
3282 {
3283 uint64_t thisBootSkip =
3284 std::max(skip, entryCount) - entryCount;
3285 uint64_t thisBootTop =
3286 std::min(top + skip, endCount) - entryCount;
3287
3288 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3289 thisBootSkip, thisBootTop);
3290 }
3291 aResp->res.jsonValue["Members@odata.count"] = endCount;
3292 }
3293
3294 // continue to previous bootIndex
3295 if (bootIndex < bootCount)
3296 {
3297 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3298 bootCount, endCount, skip, top);
3299 }
3300 else
3301 {
3302 aResp->res.jsonValue["Members@odata.nextLink"] =
George Liu0fda0f12021-11-16 10:06:17 +08003303 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
ZhikuiRena3316fc2020-01-29 14:58:08 -08003304 std::to_string(skip + top);
3305 }
3306 },
Jonathan Doman15124762021-01-07 17:54:17 -08003307 "xyz.openbmc_project.State.Boot.PostCode0",
3308 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003309 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3310 bootIndex);
3311}
3312
zhanghch058d1b46d2021-04-01 11:18:24 +08003313static void
3314 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3315 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003316{
3317 uint64_t entryCount = 0;
3318 crow::connections::systemBus->async_method_call(
3319 [aResp, entryCount, skip,
3320 top](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003321 const std::variant<uint16_t>& bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003322 if (ec)
3323 {
3324 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3325 messages::internalError(aResp->res);
3326 return;
3327 }
3328 auto pVal = std::get_if<uint16_t>(&bootCount);
3329 if (pVal)
3330 {
3331 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3332 }
3333 else
3334 {
3335 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3336 }
3337 },
Jonathan Doman15124762021-01-07 17:54:17 -08003338 "xyz.openbmc_project.State.Boot.PostCode0",
3339 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003340 "org.freedesktop.DBus.Properties", "Get",
3341 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3342}
3343
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003344inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003345{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003346 BMCWEB_ROUTE(app,
3347 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003348 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003349 .methods(boost::beast::http::verb::get)(
3350 [](const crow::Request& req,
3351 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3352 asyncResp->res.jsonValue["@odata.type"] =
3353 "#LogEntryCollection.LogEntryCollection";
3354 asyncResp->res.jsonValue["@odata.id"] =
3355 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3356 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3357 asyncResp->res.jsonValue["Description"] =
3358 "Collection of POST Code Log Entries";
3359 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3360 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003361
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003362 uint64_t skip = 0;
3363 uint64_t top = maxEntriesPerPage; // Show max entries by default
3364 if (!getSkipParam(asyncResp, req, skip))
3365 {
3366 return;
3367 }
3368 if (!getTopParam(asyncResp, req, top))
3369 {
3370 return;
3371 }
3372 getCurrentBootNumber(asyncResp, skip, top);
3373 });
3374}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003375
George Liu647b3cd2021-07-05 12:43:56 +08003376/**
3377 * @brief Parse post code ID and get the current value and index value
3378 * eg: postCodeID=B1-2, currentValue=1, index=2
3379 *
3380 * @param[in] postCodeID Post Code ID
3381 * @param[out] currentValue Current value
3382 * @param[out] index Index value
3383 *
3384 * @return bool true if the parsing is successful, false the parsing fails
3385 */
3386inline static bool parsePostCode(const std::string& postCodeID,
3387 uint64_t& currentValue, uint16_t& index)
3388{
3389 std::vector<std::string> split;
3390 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3391 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3392 {
3393 return false;
3394 }
3395
3396 const char* start = split[0].data() + 1;
3397 const char* end = split[0].data() + split[0].size();
3398 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3399
3400 if (ptrIndex != end || ecIndex != std::errc())
3401 {
3402 return false;
3403 }
3404
3405 start = split[1].data();
3406 end = split[1].data() + split[1].size();
3407 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3408 if (ptrValue != end || ecValue != std::errc())
3409 {
3410 return false;
3411 }
3412
3413 return true;
3414}
3415
3416inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3417{
George Liu0fda0f12021-11-16 10:06:17 +08003418 BMCWEB_ROUTE(
3419 app,
3420 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003421 .privileges(redfish::privileges::getLogEntry)
3422 .methods(boost::beast::http::verb::get)(
3423 [](const crow::Request& req,
3424 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3425 const std::string& postCodeID) {
3426 if (!http_helpers::isOctetAccepted(
3427 req.getHeaderValue("Accept")))
3428 {
3429 asyncResp->res.result(
3430 boost::beast::http::status::bad_request);
3431 return;
3432 }
3433
3434 uint64_t currentValue = 0;
3435 uint16_t index = 0;
3436 if (!parsePostCode(postCodeID, currentValue, index))
3437 {
3438 messages::resourceNotFound(asyncResp->res, "LogEntry",
3439 postCodeID);
3440 return;
3441 }
3442
3443 crow::connections::systemBus->async_method_call(
3444 [asyncResp, postCodeID, currentValue](
3445 const boost::system::error_code ec,
3446 const std::vector<std::tuple<
3447 uint64_t, std::vector<uint8_t>>>& postcodes) {
3448 if (ec.value() == EBADR)
3449 {
3450 messages::resourceNotFound(asyncResp->res,
3451 "LogEntry", postCodeID);
3452 return;
3453 }
3454 if (ec)
3455 {
3456 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3457 messages::internalError(asyncResp->res);
3458 return;
3459 }
3460
3461 size_t value = static_cast<size_t>(currentValue) - 1;
3462 if (value == std::string::npos ||
3463 postcodes.size() < currentValue)
3464 {
3465 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3466 messages::resourceNotFound(asyncResp->res,
3467 "LogEntry", postCodeID);
3468 return;
3469 }
3470
3471 auto& [tID, code] = postcodes[value];
3472 if (code.empty())
3473 {
3474 BMCWEB_LOG_INFO << "No found post code data";
3475 messages::resourceNotFound(asyncResp->res,
3476 "LogEntry", postCodeID);
3477 return;
3478 }
3479
3480 std::string_view strData(
3481 reinterpret_cast<const char*>(code.data()),
3482 code.size());
3483
3484 asyncResp->res.addHeader("Content-Type",
3485 "application/octet-stream");
3486 asyncResp->res.addHeader("Content-Transfer-Encoding",
3487 "Base64");
3488 asyncResp->res.body() =
3489 crow::utility::base64encode(strData);
3490 },
3491 "xyz.openbmc_project.State.Boot.PostCode0",
3492 "/xyz/openbmc_project/State/Boot/PostCode0",
3493 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3494 index);
3495 });
3496}
3497
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003498inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003499{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003500 BMCWEB_ROUTE(
3501 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003502 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003503 .methods(boost::beast::http::verb::get)(
3504 [](const crow::Request&,
3505 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3506 const std::string& targetID) {
George Liu647b3cd2021-07-05 12:43:56 +08003507 uint16_t bootIndex = 0;
3508 uint64_t codeIndex = 0;
3509 if (!parsePostCode(targetID, codeIndex, bootIndex))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003510 {
3511 // Requested ID was not found
3512 messages::resourceMissingAtURI(asyncResp->res, targetID);
3513 return;
3514 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003515 if (bootIndex == 0 || codeIndex == 0)
3516 {
3517 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3518 << targetID;
3519 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003520
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003521 asyncResp->res.jsonValue["@odata.type"] =
3522 "#LogEntry.v1_4_0.LogEntry";
3523 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08003524 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003525 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3526 asyncResp->res.jsonValue["Description"] =
3527 "Collection of POST Code Log Entries";
3528 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3529 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003530
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003531 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3532 });
3533}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003534
Ed Tanous1da66f72018-07-27 16:13:37 -07003535} // namespace redfish