blob: 34625d3abcfae9f7bb9de52529a476cd560085ad [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "error_messages.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080021#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080022#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080023#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "query.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070025#include "registries.hpp"
26#include "registries/base_message_registry.hpp"
27#include "registries/openbmc_message_registry.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080028#include "registries/privilege_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080029#include "task.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080030#include "utils/dbus_utils.hpp"
31#include "utils/time_utils.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070032
Jason M. Billse1f26342018-07-18 12:12:00 -070033#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060034#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060035#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070036
Ed Tanous9896eae2022-07-23 15:07:33 -070037#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070038#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060039#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070040#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070041#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070042#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080043#include <boost/system/linux_error.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020044#include <sdbusplus/asio/property.hpp>
45#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050046
George Liu7a1dbc42022-12-07 16:03:22 +080047#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080048#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070049#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080050#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070051#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070052#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080053#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070054
55namespace redfish
56{
57
Gunnar Mills1214b7e2020-06-04 10:11:30 -050058constexpr char const* crashdumpObject = "com.intel.crashdump";
59constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050060constexpr char const* crashdumpInterface = "com.intel.crashdump";
61constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070062 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050063constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070064 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070065constexpr char const* crashdumpTelemetryInterface =
66 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070067
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060068enum class DumpCreationProgress
69{
70 DUMP_CREATE_SUCCESS,
71 DUMP_CREATE_FAILED,
72 DUMP_CREATE_INPROGRESS
73};
74
James Feistf6150402019-01-08 10:36:20 -080075namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070076
Gunnar Mills1214b7e2020-06-04 10:11:30 -050077inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070078{
Ed Tanousd4d25792020-09-29 15:15:03 -070079 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
80 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
81 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
82 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070083 {
84 return "Critical";
85 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070086 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
87 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
88 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070089 {
90 return "OK";
91 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070092 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070093 {
94 return "Warning";
95 }
96 return "";
97}
98
Abhishek Patel9017faf2021-09-14 22:48:55 -050099inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
100{
101 std::optional<bool> notifyAction;
102 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
103 {
104 notifyAction = true;
105 }
106 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
107 {
108 notifyAction = false;
109 }
110
111 return notifyAction;
112}
113
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700114inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800115 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700116 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700117{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500118 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700119 size_t length = 0;
120 int ret = 0;
121 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800122 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
123 const void** dataVoid = reinterpret_cast<const void**>(&data);
124
125 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700126 if (ret < 0)
127 {
128 return ret;
129 }
Ed Tanous39e77502019-03-04 17:35:53 -0800130 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700131 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000132 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700133 return ret;
134}
135
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700136inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800137 std::string_view field, const int& base,
138 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700139{
140 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800141 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700142 // Get the metadata from the requested field of the journal entry
143 ret = getJournalMetadata(journal, field, metadata);
144 if (ret < 0)
145 {
146 return ret;
147 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000148 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700149 return ret;
150}
151
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700152inline static bool getEntryTimestamp(sd_journal* journal,
153 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800154{
155 int ret = 0;
156 uint64_t timestamp = 0;
157 ret = sd_journal_get_realtime_usec(journal, &timestamp);
158 if (ret < 0)
159 {
160 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
161 << strerror(-ret);
162 return false;
163 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300164 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500165 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800166}
Ed Tanous50b8a432022-02-03 16:29:50 -0800167
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700168inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
169 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700170{
171 int ret = 0;
172 static uint64_t prevTs = 0;
173 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700174 if (firstEntry)
175 {
176 prevTs = 0;
177 }
178
Jason M. Bills16428a12018-11-02 12:42:29 -0700179 // Get the entry timestamp
180 uint64_t curTs = 0;
181 ret = sd_journal_get_realtime_usec(journal, &curTs);
182 if (ret < 0)
183 {
184 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
185 << strerror(-ret);
186 return false;
187 }
188 // If the timestamp isn't unique, increment the index
189 if (curTs == prevTs)
190 {
191 index++;
192 }
193 else
194 {
195 // Otherwise, reset it
196 index = 0;
197 }
198 // Save the timestamp
199 prevTs = curTs;
200
201 entryID = std::to_string(curTs);
202 if (index > 0)
203 {
204 entryID += "_" + std::to_string(index);
205 }
206 return true;
207}
208
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500209static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700210 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700211{
Ed Tanous271584a2019-07-09 16:24:22 -0700212 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700213 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700214 if (firstEntry)
215 {
216 prevTs = 0;
217 }
218
Jason M. Bills95820182019-04-22 16:25:34 -0700219 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700220 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700221 std::tm timeStruct = {};
222 std::istringstream entryStream(logEntry);
223 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
224 {
225 curTs = std::mktime(&timeStruct);
226 }
227 // If the timestamp isn't unique, increment the index
228 if (curTs == prevTs)
229 {
230 index++;
231 }
232 else
233 {
234 // Otherwise, reset it
235 index = 0;
236 }
237 // Save the timestamp
238 prevTs = curTs;
239
240 entryID = std::to_string(curTs);
241 if (index > 0)
242 {
243 entryID += "_" + std::to_string(index);
244 }
245 return true;
246}
247
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700248inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800249 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
250 const std::string& entryID, uint64_t& timestamp,
251 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700252{
253 if (entryID.empty())
254 {
255 return false;
256 }
257 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800258 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700259
Ed Tanous81ce6092020-12-17 16:54:55 +0000260 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800261 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700262 {
263 // Timestamp has an index
264 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800265 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700266 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700267 auto [ptr, ec] = std::from_chars(
268 indexStr.data(), indexStr.data() + indexStr.size(), index);
269 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700270 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800271 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700272 return false;
273 }
274 }
275 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700276 auto [ptr, ec] =
277 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
278 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700279 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800280 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700281 return false;
282 }
283 return true;
284}
285
Jason M. Bills95820182019-04-22 16:25:34 -0700286static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500287 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700288{
289 static const std::filesystem::path redfishLogDir = "/var/log";
290 static const std::string redfishLogFilename = "redfish";
291
292 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500293 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700294 std::filesystem::directory_iterator(redfishLogDir))
295 {
296 // If we find a redfish log file, save the path
297 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700298 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700299 {
300 redfishLogFiles.emplace_back(redfishLogDir / filename);
301 }
302 }
303 // As the log files rotate, they are appended with a ".#" that is higher for
304 // the older logs. Since we don't expect more than 10 log files, we
305 // can just sort the list to get them in order from newest to oldest
306 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
307
308 return !redfishLogFiles.empty();
309}
310
Claire Weinanaefe3782022-07-15 19:17:19 -0700311inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800312 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700313 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700314 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
315{
316 for (const auto& interfaceMap : object.second)
317 {
318 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
319 {
320 for (const auto& propertyMap : interfaceMap.second)
321 {
322 if (propertyMap.first == "Status")
323 {
324 const auto* status =
325 std::get_if<std::string>(&propertyMap.second);
326 if (status == nullptr)
327 {
328 messages::internalError(asyncResp->res);
329 break;
330 }
331 dumpStatus = *status;
332 }
333 }
334 }
335 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
336 {
337 for (const auto& propertyMap : interfaceMap.second)
338 {
339 if (propertyMap.first == "Size")
340 {
341 const auto* sizePtr =
342 std::get_if<uint64_t>(&propertyMap.second);
343 if (sizePtr == nullptr)
344 {
345 messages::internalError(asyncResp->res);
346 break;
347 }
348 size = *sizePtr;
349 break;
350 }
351 }
352 }
353 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
354 {
355 for (const auto& propertyMap : interfaceMap.second)
356 {
357 if (propertyMap.first == "Elapsed")
358 {
359 const uint64_t* usecsTimeStamp =
360 std::get_if<uint64_t>(&propertyMap.second);
361 if (usecsTimeStamp == nullptr)
362 {
363 messages::internalError(asyncResp->res);
364 break;
365 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700366 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700367 break;
368 }
369 }
370 }
371 }
372}
373
Nan Zhou21ab4042022-06-26 23:07:40 +0000374static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800375{
376 std::string entriesPath;
377
378 if (dumpType == "BMC")
379 {
380 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
381 }
382 else if (dumpType == "FaultLog")
383 {
384 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
385 }
386 else if (dumpType == "System")
387 {
388 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
389 }
390 else
391 {
392 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
393 << dumpType;
394 }
395
396 // Returns empty string on error
397 return entriesPath;
398}
399
zhanghch058d1b46d2021-04-01 11:18:24 +0800400inline void
401 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
402 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500403{
Claire Weinanfdd26902022-03-01 14:18:25 -0800404 std::string entriesPath = getDumpEntriesPath(dumpType);
405 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500406 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500407 messages::internalError(asyncResp->res);
408 return;
409 }
410
411 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800412 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800413 dumpType](const boost::system::error_code& ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800414 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700415 if (ec)
416 {
417 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
418 messages::internalError(asyncResp->res);
419 return;
420 }
421
Claire Weinanfdd26902022-03-01 14:18:25 -0800422 // Remove ending slash
423 std::string odataIdStr = entriesPath;
424 if (!odataIdStr.empty())
425 {
426 odataIdStr.pop_back();
427 }
428
429 asyncResp->res.jsonValue["@odata.type"] =
430 "#LogEntryCollection.LogEntryCollection";
431 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
432 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
433 asyncResp->res.jsonValue["Description"] =
434 "Collection of " + dumpType + " Dump Entries";
435
Ed Tanous002d39b2022-05-31 08:59:27 -0700436 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
437 entriesArray = nlohmann::json::array();
438 std::string dumpEntryPath =
439 "/xyz/openbmc_project/dump/" +
440 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
441
442 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
443 return AlphanumLess<std::string>()(l.first.filename(),
444 r.first.filename());
445 });
446
447 for (auto& object : resp)
448 {
449 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500450 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700451 continue;
452 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700453 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700454 uint64_t size = 0;
455 std::string dumpStatus;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700456 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700457
458 std::string entryID = object.first.filename();
459 if (entryID.empty())
460 {
461 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500462 }
463
Claire Weinanc6fecda2022-07-15 10:43:25 -0700464 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700465 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700466
467 if (dumpStatus !=
468 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
469 !dumpStatus.empty())
470 {
471 // Dump status is not Complete, no need to enumerate
472 continue;
473 }
474
Vijay Lobo9c11a172021-10-07 16:53:16 -0500475 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800476 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700477 thisEntry["Id"] = entryID;
478 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700479 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700480 thisEntry["Created"] =
481 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700482
Ed Tanous002d39b2022-05-31 08:59:27 -0700483 if (dumpType == "BMC")
484 {
485 thisEntry["DiagnosticDataType"] = "Manager";
486 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800487 entriesPath + entryID + "/attachment";
488 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700489 }
490 else if (dumpType == "System")
491 {
492 thisEntry["DiagnosticDataType"] = "OEM";
493 thisEntry["OEMDiagnosticDataType"] = "System";
494 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800495 entriesPath + entryID + "/attachment";
496 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 }
498 entriesArray.push_back(std::move(thisEntry));
499 }
500 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500501 },
502 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
503 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
504}
505
zhanghch058d1b46d2021-04-01 11:18:24 +0800506inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700507 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800508 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500509{
Claire Weinanfdd26902022-03-01 14:18:25 -0800510 std::string entriesPath = getDumpEntriesPath(dumpType);
511 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500513 messages::internalError(asyncResp->res);
514 return;
515 }
516
517 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800518 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800519 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700520 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700521 if (ec)
522 {
523 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
524 messages::internalError(asyncResp->res);
525 return;
526 }
527
528 bool foundDumpEntry = false;
529 std::string dumpEntryPath =
530 "/xyz/openbmc_project/dump/" +
531 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
532
533 for (const auto& objectPath : resp)
534 {
535 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500536 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700537 continue;
538 }
539
540 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700541 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700542 uint64_t size = 0;
543 std::string dumpStatus;
544
Claire Weinanaefe3782022-07-15 19:17:19 -0700545 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700546 timestampUs, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700547
548 if (dumpStatus !=
549 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
550 !dumpStatus.empty())
551 {
552 // Dump status is not Complete
553 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200554 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
555 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500556 return;
557 }
558
Ed Tanous002d39b2022-05-31 08:59:27 -0700559 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -0500560 "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800561 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700562 asyncResp->res.jsonValue["Id"] = entryID;
563 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700564 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700565 asyncResp->res.jsonValue["Created"] =
566 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500567
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500569 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700570 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
571 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800572 entriesPath + entryID + "/attachment";
573 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500574 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700575 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500576 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700577 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
578 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
579 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800580 entriesPath + entryID + "/attachment";
581 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500582 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700583 }
584 if (!foundDumpEntry)
585 {
586 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
587 messages::internalError(asyncResp->res);
588 return;
589 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500590 },
591 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
592 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
593}
594
zhanghch058d1b46d2021-04-01 11:18:24 +0800595inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800596 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500597 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500598{
Ed Tanous002d39b2022-05-31 08:59:27 -0700599 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800600 [asyncResp, entryID](const boost::system::error_code& ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500601 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
602 if (ec)
603 {
George Liu3de8d8b2021-03-22 17:49:39 +0800604 if (ec.value() == EBADR)
605 {
606 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
607 return;
608 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500609 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800610 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500611 messages::internalError(asyncResp->res);
612 return;
613 }
614 };
615 crow::connections::systemBus->async_method_call(
616 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500617 "/xyz/openbmc_project/dump/" +
618 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
619 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500620 "xyz.openbmc_project.Object.Delete", "Delete");
621}
622
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600623inline DumpCreationProgress
624 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500625{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600626 if (status ==
627 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
628 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
629 {
630 return DumpCreationProgress::DUMP_CREATE_FAILED;
631 }
632 if (status ==
633 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
634 {
635 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
636 }
637 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
638}
639
640inline DumpCreationProgress
641 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
642{
643 for (const auto& [key, val] : values)
644 {
645 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700646 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600647 const std::string* value = std::get_if<std::string>(&val);
648 if (value == nullptr)
649 {
650 BMCWEB_LOG_ERROR << "Status property value is null";
651 return DumpCreationProgress::DUMP_CREATE_FAILED;
652 }
653 return mapDbusStatusToDumpProgress(*value);
654 }
655 }
656 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
657}
658
659inline std::string getDumpEntryPath(const std::string& dumpPath)
660{
661 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
662 {
663 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
664 }
665 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
666 {
667 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
668 }
669 return "";
670}
671
672inline void createDumpTaskCallback(
673 task::Payload&& payload,
674 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
675 const sdbusplus::message::object_path& createdObjPath)
676{
677 const std::string dumpPath = createdObjPath.parent_path().str;
678 const std::string dumpId = createdObjPath.filename();
679
680 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
681
682 if (dumpEntryPath.empty())
683 {
684 BMCWEB_LOG_ERROR << "Invalid dump type received";
685 messages::internalError(asyncResp->res);
686 return;
687 }
688
689 crow::connections::systemBus->async_method_call(
690 [asyncResp, payload, createdObjPath,
691 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800692 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600693 const std::string& introspectXml) {
694 if (ec)
695 {
696 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
697 << ec.message();
698 messages::internalError(asyncResp->res);
699 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700700 }
701
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600702 // Check if the created dump object has implemented Progress
703 // interface to track dump completion. If yes, fetch the "Status"
704 // property of the interface, modify the task state accordingly.
705 // Else, return task completed.
706 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700707
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600708 doc.Parse(introspectXml.data(), introspectXml.size());
709 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
710 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700711 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600712 BMCWEB_LOG_ERROR << "XML document failed to parse";
713 messages::internalError(asyncResp->res);
714 return;
715 }
716 tinyxml2::XMLElement* interfaceNode =
717 pRoot->FirstChildElement("interface");
718
719 bool isProgressIntfPresent = false;
720 while (interfaceNode != nullptr)
721 {
722 const char* thisInterfaceName = interfaceNode->Attribute("name");
723 if (thisInterfaceName != nullptr)
724 {
725 if (thisInterfaceName ==
726 std::string_view("xyz.openbmc_project.Common.Progress"))
727 {
728 interfaceNode =
729 interfaceNode->NextSiblingElement("interface");
730 continue;
731 }
732 isProgressIntfPresent = true;
733 break;
734 }
735 interfaceNode = interfaceNode->NextSiblingElement("interface");
736 }
737
738 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
739 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800740 const boost::system::error_code& err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600741 const std::shared_ptr<task::TaskData>& taskData) {
742 if (err)
743 {
744 BMCWEB_LOG_ERROR << createdObjPath.str
745 << ": Error in creating dump";
746 taskData->messages.emplace_back(messages::internalError());
747 taskData->state = "Cancelled";
748 return task::completed;
749 }
750
751 if (isProgressIntfPresent)
752 {
753 dbus::utility::DBusPropertiesMap values;
754 std::string prop;
755 msg.read(prop, values);
756
757 DumpCreationProgress dumpStatus =
758 getDumpCompletionStatus(values);
759 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
760 {
761 BMCWEB_LOG_ERROR << createdObjPath.str
762 << ": Error in creating dump";
763 taskData->state = "Cancelled";
764 return task::completed;
765 }
766
767 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
768 {
769 BMCWEB_LOG_DEBUG << createdObjPath.str
770 << ": Dump creation task is in progress";
771 return !task::completed;
772 }
773 }
774
Ed Tanous002d39b2022-05-31 08:59:27 -0700775 nlohmann::json retMessage = messages::success();
776 taskData->messages.emplace_back(retMessage);
777
778 std::string headerLoc =
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600779 "Location: " + dumpEntryPath + http_helpers::urlEncode(dumpId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700780 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
781
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600782 BMCWEB_LOG_DEBUG << createdObjPath.str
783 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700784 taskData->state = "Completed";
785 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600786 },
787 "type='signal',interface='org.freedesktop.DBus.Properties',"
788 "member='PropertiesChanged',path='" +
789 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500790
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600791 // The task timer is set to max time limit within which the
792 // requested dump will be collected.
793 task->startTimer(std::chrono::minutes(6));
794 task->populateResp(asyncResp->res);
795 task->payload.emplace(payload);
796 },
797 "xyz.openbmc_project.Dump.Manager", createdObjPath,
798 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500799}
800
zhanghch058d1b46d2021-04-01 11:18:24 +0800801inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
802 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500803{
Claire Weinanfdd26902022-03-01 14:18:25 -0800804 std::string dumpPath = getDumpEntriesPath(dumpType);
805 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500806 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500807 messages::internalError(asyncResp->res);
808 return;
809 }
810
811 std::optional<std::string> diagnosticDataType;
812 std::optional<std::string> oemDiagnosticDataType;
813
Willy Tu15ed6782021-12-14 11:03:16 -0800814 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500815 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
816 "OEMDiagnosticDataType", oemDiagnosticDataType))
817 {
818 return;
819 }
820
821 if (dumpType == "System")
822 {
823 if (!oemDiagnosticDataType || !diagnosticDataType)
824 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800825 BMCWEB_LOG_ERROR
826 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500827 messages::actionParameterMissing(
828 asyncResp->res, "CollectDiagnosticData",
829 "DiagnosticDataType & OEMDiagnosticDataType");
830 return;
831 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700832 if ((*oemDiagnosticDataType != "System") ||
833 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500834 {
835 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700836 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500837 return;
838 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500839 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500840 }
841 else if (dumpType == "BMC")
842 {
843 if (!diagnosticDataType)
844 {
George Liu0fda0f12021-11-16 10:06:17 +0800845 BMCWEB_LOG_ERROR
846 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500847 messages::actionParameterMissing(
848 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
849 return;
850 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700851 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500852 {
853 BMCWEB_LOG_ERROR
854 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700855 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500856 return;
857 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500858 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
859 }
860 else
861 {
862 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
863 messages::internalError(asyncResp->res);
864 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500865 }
866
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600867 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
868 createDumpParamVec;
869
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500870 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800871 [asyncResp, payload(task::Payload(req)),
872 dumpPath](const boost::system::error_code& ec,
873 const sdbusplus::message_t& msg,
874 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700875 if (ec)
876 {
877 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500878 const sd_bus_error* dbusError = msg.get_error();
879 if (dbusError == nullptr)
880 {
881 messages::internalError(asyncResp->res);
882 return;
883 }
884
885 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
886 << " and error msg: " << dbusError->message;
887 if (std::string_view(
888 "xyz.openbmc_project.Common.Error.NotAllowed") ==
889 dbusError->name)
890 {
891 messages::resourceInStandby(asyncResp->res);
892 return;
893 }
894 if (std::string_view(
895 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
896 dbusError->name)
897 {
898 messages::serviceDisabled(asyncResp->res, dumpPath);
899 return;
900 }
901 if (std::string_view(
902 "xyz.openbmc_project.Common.Error.Unavailable") ==
903 dbusError->name)
904 {
905 messages::resourceInUse(asyncResp->res);
906 return;
907 }
908 // Other Dbus errors such as:
909 // xyz.openbmc_project.Common.Error.InvalidArgument &
910 // org.freedesktop.DBus.Error.InvalidArgs are all related to
911 // the dbus call that is made here in the bmcweb
912 // implementation and has nothing to do with the client's
913 // input in the request. Hence, returning internal error
914 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -0700915 messages::internalError(asyncResp->res);
916 return;
917 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600918 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
919 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500920 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500921 "xyz.openbmc_project.Dump.Manager",
922 "/xyz/openbmc_project/dump/" +
923 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600924 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500925}
926
zhanghch058d1b46d2021-04-01 11:18:24 +0800927inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
928 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500929{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500930 std::string dumpTypeLowerCopy =
931 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800932
Claire Weinan0d946212022-07-13 19:40:19 -0700933 crow::connections::systemBus->async_method_call(
934 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700935 if (ec)
936 {
Claire Weinan0d946212022-07-13 19:40:19 -0700937 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -0700938 messages::internalError(asyncResp->res);
939 return;
940 }
Claire Weinan0d946212022-07-13 19:40:19 -0700941 },
942 "xyz.openbmc_project.Dump.Manager",
943 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
944 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500945}
946
Ed Tanousb9d36b42022-02-26 21:42:46 -0800947inline static void
948 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
949 std::string& filename, std::string& timestamp,
950 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700951{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200952 const std::string* filenamePtr = nullptr;
953 const std::string* timestampPtr = nullptr;
954 const std::string* logfilePtr = nullptr;
955
956 const bool success = sdbusplus::unpackPropertiesNoThrow(
957 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
958 "Filename", filenamePtr, "Log", logfilePtr);
959
960 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700961 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200962 return;
963 }
964
965 if (filenamePtr != nullptr)
966 {
967 filename = *filenamePtr;
968 }
969
970 if (timestampPtr != nullptr)
971 {
972 timestamp = *timestampPtr;
973 }
974
975 if (logfilePtr != nullptr)
976 {
977 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -0700978 }
979}
980
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700981inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700982{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800983 /**
984 * Functions triggers appropriate requests on DBus
985 */
Ed Tanous22d268c2022-05-19 09:39:07 -0700986 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700987 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -0700988 .methods(boost::beast::http::verb::get)(
989 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -0700990 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
991 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000992 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700993 {
994 return;
995 }
Ed Tanous22d268c2022-05-19 09:39:07 -0700996 if (systemName != "system")
997 {
998 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
999 systemName);
1000 return;
1001 }
1002
Ed Tanous002d39b2022-05-31 08:59:27 -07001003 // Collections don't include the static data added by SubRoute
1004 // because it has a duplicate entry for members
1005 asyncResp->res.jsonValue["@odata.type"] =
1006 "#LogServiceCollection.LogServiceCollection";
1007 asyncResp->res.jsonValue["@odata.id"] =
1008 "/redfish/v1/Systems/system/LogServices";
1009 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1010 asyncResp->res.jsonValue["Description"] =
1011 "Collection of LogServices for this Computer System";
1012 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1013 logServiceArray = nlohmann::json::array();
1014 nlohmann::json::object_t eventLog;
1015 eventLog["@odata.id"] =
1016 "/redfish/v1/Systems/system/LogServices/EventLog";
1017 logServiceArray.push_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001018#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001019 nlohmann::json::object_t dumpLog;
1020 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
1021 logServiceArray.push_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001022#endif
1023
Jason M. Billsd53dd412019-02-12 17:16:22 -08001024#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001025 nlohmann::json::object_t crashdump;
1026 crashdump["@odata.id"] =
1027 "/redfish/v1/Systems/system/LogServices/Crashdump";
1028 logServiceArray.push_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001029#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001030
1031#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001032 nlohmann::json::object_t hostlogger;
1033 hostlogger["@odata.id"] =
1034 "/redfish/v1/Systems/system/LogServices/HostLogger";
1035 logServiceArray.push_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001036#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001037 asyncResp->res.jsonValue["Members@odata.count"] =
1038 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001039
George Liu7a1dbc42022-12-07 16:03:22 +08001040 constexpr std::array<std::string_view, 1> interfaces = {
1041 "xyz.openbmc_project.State.Boot.PostCode"};
1042 dbus::utility::getSubTreePaths(
1043 "/", 0, interfaces,
1044 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001045 const dbus::utility::MapperGetSubTreePathsResponse&
1046 subtreePath) {
1047 if (ec)
1048 {
1049 BMCWEB_LOG_ERROR << ec;
1050 return;
1051 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001052
Ed Tanous002d39b2022-05-31 08:59:27 -07001053 for (const auto& pathStr : subtreePath)
1054 {
1055 if (pathStr.find("PostCode") != std::string::npos)
1056 {
1057 nlohmann::json& logServiceArrayLocal =
1058 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001059 nlohmann::json::object_t member;
1060 member["@odata.id"] =
1061 "/redfish/v1/Systems/system/LogServices/PostCodes";
1062
1063 logServiceArrayLocal.push_back(std::move(member));
1064
Ed Tanous002d39b2022-05-31 08:59:27 -07001065 asyncResp->res.jsonValue["Members@odata.count"] =
1066 logServiceArrayLocal.size();
1067 return;
1068 }
1069 }
George Liu7a1dbc42022-12-07 16:03:22 +08001070 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001071 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001072}
1073
1074inline void requestRoutesEventLogService(App& app)
1075{
Ed Tanous22d268c2022-05-19 09:39:07 -07001076 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001077 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001078 .methods(boost::beast::http::verb::get)(
1079 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001080 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1081 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001082 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001083 {
1084 return;
1085 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001086 if (systemName != "system")
1087 {
1088 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1089 systemName);
1090 return;
1091 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001092 asyncResp->res.jsonValue["@odata.id"] =
1093 "/redfish/v1/Systems/system/LogServices/EventLog";
1094 asyncResp->res.jsonValue["@odata.type"] =
1095 "#LogService.v1_1_0.LogService";
1096 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1097 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1098 asyncResp->res.jsonValue["Id"] = "EventLog";
1099 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301100
Ed Tanous002d39b2022-05-31 08:59:27 -07001101 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001102 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301103
Ed Tanous002d39b2022-05-31 08:59:27 -07001104 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1105 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1106 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301107
Ed Tanous002d39b2022-05-31 08:59:27 -07001108 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1109 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1110 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001111
Ed Tanous002d39b2022-05-31 08:59:27 -07001112 {"target",
1113 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001114 });
1115}
1116
1117inline void requestRoutesJournalEventLogClear(App& app)
1118{
Jason M. Bills4978b632022-02-22 14:17:43 -08001119 BMCWEB_ROUTE(
1120 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001121 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001122 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001123 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001124 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001125 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1126 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001127 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 {
1129 return;
1130 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001131 if (systemName != "system")
1132 {
1133 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1134 systemName);
1135 return;
1136 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001137 // Clear the EventLog by deleting the log files
1138 std::vector<std::filesystem::path> redfishLogFiles;
1139 if (getRedfishLogFiles(redfishLogFiles))
1140 {
1141 for (const std::filesystem::path& file : redfishLogFiles)
1142 {
1143 std::error_code ec;
1144 std::filesystem::remove(file, ec);
1145 }
1146 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001147
Ed Tanous002d39b2022-05-31 08:59:27 -07001148 // Reload rsyslog so it knows to start new log files
1149 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001150 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001151 if (ec)
1152 {
1153 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1154 messages::internalError(asyncResp->res);
1155 return;
1156 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001157
Ed Tanous002d39b2022-05-31 08:59:27 -07001158 messages::success(asyncResp->res);
1159 },
1160 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1161 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1162 "replace");
1163 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001164}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001165
Jason M. Billsac992cd2022-06-24 13:31:46 -07001166enum class LogParseError
1167{
1168 success,
1169 parseFailed,
1170 messageIdNotInRegistry,
1171};
1172
1173static LogParseError
1174 fillEventLogEntryJson(const std::string& logEntryID,
1175 const std::string& logEntry,
1176 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001177{
Jason M. Bills95820182019-04-22 16:25:34 -07001178 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001179 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001180 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001181 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001182 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001183 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001184 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001185 std::string timestamp = logEntry.substr(0, space);
1186 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001187 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001188 if (entryStart == std::string::npos)
1189 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001190 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001191 }
1192 std::string_view entry(logEntry);
1193 entry.remove_prefix(entryStart);
1194 // Use split to separate the entry into its fields
1195 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001196 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001197 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001198 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001199 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001200 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001201 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001202 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001203
Jason M. Bills4851d452019-03-28 11:27:48 -07001204 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001205 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001206
Sui Chen54417b02022-03-24 14:59:52 -07001207 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001208 {
Sui Chen54417b02022-03-24 14:59:52 -07001209 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001210 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001211 }
1212
Sui Chen54417b02022-03-24 14:59:52 -07001213 std::string msg = message->message;
1214
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001215 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001216 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001217 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001218 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001219 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001220 // If the first string is empty, assume there are no MessageArgs
1221 std::size_t messageArgsSize = 0;
1222 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001223 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001224 messageArgsSize = logEntryFields.size() - 1;
1225 }
1226
Ed Tanous23a21a12020-07-25 04:45:05 +00001227 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001228
1229 // Fill the MessageArgs into the Message
1230 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001231 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001232 {
1233 std::string argStr = "%" + std::to_string(++i);
1234 size_t argPos = msg.find(argStr);
1235 if (argPos != std::string::npos)
1236 {
1237 msg.replace(argPos, argStr.length(), messageArg);
1238 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001239 }
1240 }
1241
Jason M. Bills95820182019-04-22 16:25:34 -07001242 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1243 // format which matches the Redfish format except for the fractional seconds
1244 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001245 std::size_t dot = timestamp.find_first_of('.');
1246 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001247 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001248 {
Jason M. Bills95820182019-04-22 16:25:34 -07001249 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001250 }
1251
1252 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001253 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001254 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
1255 "redfish", "v1", "Systems", "system", "LogServices", "EventLog",
1256 "Entries", logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001257 logEntryJson["Name"] = "System Event Log Entry";
1258 logEntryJson["Id"] = logEntryID;
1259 logEntryJson["Message"] = std::move(msg);
1260 logEntryJson["MessageId"] = std::move(messageID);
1261 logEntryJson["MessageArgs"] = messageArgs;
1262 logEntryJson["EntryType"] = "Event";
1263 logEntryJson["Severity"] = message->messageSeverity;
1264 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001265 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001266}
1267
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001268inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001269{
Ed Tanous22d268c2022-05-19 09:39:07 -07001270 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001271 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001272 .methods(boost::beast::http::verb::get)(
1273 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001274 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1275 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001276 query_param::QueryCapabilities capabilities = {
1277 .canDelegateTop = true,
1278 .canDelegateSkip = true,
1279 };
1280 query_param::Query delegatedQuery;
1281 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001282 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001283 {
1284 return;
1285 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001286 if (systemName != "system")
1287 {
1288 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1289 systemName);
1290 return;
1291 }
1292
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001293 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001294 size_t skip = delegatedQuery.skip.value_or(0);
1295
Ed Tanous002d39b2022-05-31 08:59:27 -07001296 // Collections don't include the static data added by SubRoute
1297 // because it has a duplicate entry for members
1298 asyncResp->res.jsonValue["@odata.type"] =
1299 "#LogEntryCollection.LogEntryCollection";
1300 asyncResp->res.jsonValue["@odata.id"] =
1301 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1302 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1303 asyncResp->res.jsonValue["Description"] =
1304 "Collection of System Event Log Entries";
1305
1306 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1307 logEntryArray = nlohmann::json::array();
1308 // Go through the log files and create a unique ID for each
1309 // entry
1310 std::vector<std::filesystem::path> redfishLogFiles;
1311 getRedfishLogFiles(redfishLogFiles);
1312 uint64_t entryCount = 0;
1313 std::string logEntry;
1314
1315 // Oldest logs are in the last file, so start there and loop
1316 // backwards
1317 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1318 it++)
1319 {
1320 std::ifstream logStream(*it);
1321 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001322 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001323 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001324 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001325
Ed Tanous002d39b2022-05-31 08:59:27 -07001326 // Reset the unique ID on the first entry
1327 bool firstEntry = true;
1328 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001329 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001330 std::string idStr;
1331 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001332 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001333 continue;
1334 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001335 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001336
Jason M. Billsde703c52022-06-23 14:19:04 -07001337 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001338 LogParseError status =
1339 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1340 if (status == LogParseError::messageIdNotInRegistry)
1341 {
1342 continue;
1343 }
1344 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001345 {
1346 messages::internalError(asyncResp->res);
1347 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001348 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001349
Jason M. Billsde703c52022-06-23 14:19:04 -07001350 entryCount++;
1351 // Handle paging using skip (number of entries to skip from the
1352 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001353 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001354 {
1355 continue;
1356 }
1357
1358 logEntryArray.push_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001359 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001360 }
1361 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001362 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001363 {
1364 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1365 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001366 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001367 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001368 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001369}
Chicago Duan336e96c2019-07-15 14:22:08 +08001370
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001371inline void requestRoutesJournalEventLogEntry(App& app)
1372{
1373 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001374 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001375 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001376 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001377 [&app](const crow::Request& req,
1378 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001379 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001380 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001381 {
1382 return;
1383 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001384
1385 if (systemName != "system")
1386 {
1387 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1388 systemName);
1389 return;
1390 }
1391
Ed Tanous002d39b2022-05-31 08:59:27 -07001392 const std::string& targetID = param;
1393
1394 // Go through the log files and check the unique ID for each
1395 // entry to find the target entry
1396 std::vector<std::filesystem::path> redfishLogFiles;
1397 getRedfishLogFiles(redfishLogFiles);
1398 std::string logEntry;
1399
1400 // Oldest logs are in the last file, so start there and loop
1401 // backwards
1402 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1403 it++)
1404 {
1405 std::ifstream logStream(*it);
1406 if (!logStream.is_open())
1407 {
1408 continue;
1409 }
1410
1411 // Reset the unique ID on the first entry
1412 bool firstEntry = true;
1413 while (std::getline(logStream, logEntry))
1414 {
1415 std::string idStr;
1416 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001417 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001418 continue;
1419 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001420 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001421
1422 if (idStr == targetID)
1423 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001424 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001425 LogParseError status =
1426 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1427 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001428 {
1429 messages::internalError(asyncResp->res);
1430 return;
1431 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001432 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001433 return;
1434 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001435 }
1436 }
1437 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001438 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001439 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001440}
1441
1442inline void requestRoutesDBusEventLogEntryCollection(App& app)
1443{
Ed Tanous22d268c2022-05-19 09:39:07 -07001444 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001445 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001446 .methods(boost::beast::http::verb::get)(
1447 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001448 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1449 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001450 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001451 {
1452 return;
1453 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001454 if (systemName != "system")
1455 {
1456 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1457 systemName);
1458 return;
1459 }
1460
Ed Tanous002d39b2022-05-31 08:59:27 -07001461 // Collections don't include the static data added by SubRoute
1462 // because it has a duplicate entry for members
1463 asyncResp->res.jsonValue["@odata.type"] =
1464 "#LogEntryCollection.LogEntryCollection";
1465 asyncResp->res.jsonValue["@odata.id"] =
1466 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1467 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1468 asyncResp->res.jsonValue["Description"] =
1469 "Collection of System Event Log Entries";
1470
1471 // DBus implementation of EventLog/Entries
1472 // Make call to Logging Service to find all log entry objects
1473 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001474 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001475 const dbus::utility::ManagedObjectType& resp) {
1476 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001477 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001478 // TODO Handle for specific error code
1479 BMCWEB_LOG_ERROR
1480 << "getLogEntriesIfaceData resp_handler got error " << ec;
1481 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001482 return;
1483 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001484 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1485 entriesArray = nlohmann::json::array();
1486 for (const auto& objectPath : resp)
1487 {
1488 const uint32_t* id = nullptr;
1489 const uint64_t* timestamp = nullptr;
1490 const uint64_t* updateTimestamp = nullptr;
1491 const std::string* severity = nullptr;
1492 const std::string* message = nullptr;
1493 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001494 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001495 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001496 const std::string* notify = nullptr;
1497
Ed Tanous002d39b2022-05-31 08:59:27 -07001498 for (const auto& interfaceMap : objectPath.second)
1499 {
1500 if (interfaceMap.first ==
1501 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001502 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001503 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001504 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001505 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001506 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001507 id = std::get_if<uint32_t>(&propertyMap.second);
1508 }
1509 else if (propertyMap.first == "Timestamp")
1510 {
1511 timestamp =
1512 std::get_if<uint64_t>(&propertyMap.second);
1513 }
1514 else if (propertyMap.first == "UpdateTimestamp")
1515 {
1516 updateTimestamp =
1517 std::get_if<uint64_t>(&propertyMap.second);
1518 }
1519 else if (propertyMap.first == "Severity")
1520 {
1521 severity = std::get_if<std::string>(
1522 &propertyMap.second);
1523 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001524 else if (propertyMap.first == "Resolution")
1525 {
1526 resolution = std::get_if<std::string>(
1527 &propertyMap.second);
1528 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001529 else if (propertyMap.first == "Message")
1530 {
1531 message = std::get_if<std::string>(
1532 &propertyMap.second);
1533 }
1534 else if (propertyMap.first == "Resolved")
1535 {
1536 const bool* resolveptr =
1537 std::get_if<bool>(&propertyMap.second);
1538 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001539 {
1540 messages::internalError(asyncResp->res);
1541 return;
1542 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001543 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001544 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001545 else if (propertyMap.first ==
1546 "ServiceProviderNotify")
1547 {
1548 notify = std::get_if<std::string>(
1549 &propertyMap.second);
1550 if (notify == nullptr)
1551 {
1552 messages::internalError(asyncResp->res);
1553 return;
1554 }
1555 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001556 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001557 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001558 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001559 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001560 messages::internalError(asyncResp->res);
1561 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001562 }
1563 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001564 else if (interfaceMap.first ==
1565 "xyz.openbmc_project.Common.FilePath")
1566 {
1567 for (const auto& propertyMap : interfaceMap.second)
1568 {
1569 if (propertyMap.first == "Path")
1570 {
1571 filePath = std::get_if<std::string>(
1572 &propertyMap.second);
1573 }
1574 }
1575 }
1576 }
1577 // Object path without the
1578 // xyz.openbmc_project.Logging.Entry interface, ignore
1579 // and continue.
1580 if (id == nullptr || message == nullptr ||
1581 severity == nullptr || timestamp == nullptr ||
1582 updateTimestamp == nullptr)
1583 {
1584 continue;
1585 }
1586 entriesArray.push_back({});
1587 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001588 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001589 thisEntry["@odata.id"] = crow::utility::urlFromPieces(
1590 "redfish", "v1", "Systems", "system", "LogServices",
1591 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001592 thisEntry["Name"] = "System Event Log Entry";
1593 thisEntry["Id"] = std::to_string(*id);
1594 thisEntry["Message"] = *message;
1595 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001596 if ((resolution != nullptr) && (!(*resolution).empty()))
1597 {
1598 thisEntry["Resolution"] = *resolution;
1599 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001600 std::optional<bool> notifyAction =
1601 getProviderNotifyAction(*notify);
1602 if (notifyAction)
1603 {
1604 thisEntry["ServiceProviderNotified"] = *notifyAction;
1605 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001606 thisEntry["EntryType"] = "Event";
1607 thisEntry["Severity"] =
1608 translateSeverityDbusToRedfish(*severity);
1609 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001610 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001611 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001612 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001613 if (filePath != nullptr)
1614 {
1615 thisEntry["AdditionalDataURI"] =
1616 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1617 std::to_string(*id) + "/attachment";
1618 }
1619 }
1620 std::sort(
1621 entriesArray.begin(), entriesArray.end(),
1622 [](const nlohmann::json& left, const nlohmann::json& right) {
1623 return (left["Id"] <= right["Id"]);
1624 });
1625 asyncResp->res.jsonValue["Members@odata.count"] =
1626 entriesArray.size();
1627 },
1628 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1629 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001630 });
1631}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001632
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001633inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001634{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001635 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001636 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001637 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001638 .methods(boost::beast::http::verb::get)(
1639 [&app](const crow::Request& req,
1640 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001641 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001642 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001643 {
1644 return;
1645 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001646 if (systemName != "system")
1647 {
1648 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1649 systemName);
1650 return;
1651 }
1652
Ed Tanous002d39b2022-05-31 08:59:27 -07001653 std::string entryID = param;
1654 dbus::utility::escapePathForDbus(entryID);
1655
1656 // DBus implementation of EventLog/Entries
1657 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001658 sdbusplus::asio::getAllProperties(
1659 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1660 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001661 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001662 const dbus::utility::DBusPropertiesMap& resp) {
1663 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001664 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001665 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1666 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001667 return;
1668 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001669 if (ec)
1670 {
1671 BMCWEB_LOG_ERROR
1672 << "EventLogEntry (DBus) resp_handler got error " << ec;
1673 messages::internalError(asyncResp->res);
1674 return;
1675 }
1676 const uint32_t* id = nullptr;
1677 const uint64_t* timestamp = nullptr;
1678 const uint64_t* updateTimestamp = nullptr;
1679 const std::string* severity = nullptr;
1680 const std::string* message = nullptr;
1681 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001682 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001683 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001684 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001685
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001686 const bool success = sdbusplus::unpackPropertiesNoThrow(
1687 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1688 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001689 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001690 "Resolution", resolution, "Path", filePath,
1691 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001692
1693 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001694 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001695 messages::internalError(asyncResp->res);
1696 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001697 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001698
Ed Tanous002d39b2022-05-31 08:59:27 -07001699 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001700 timestamp == nullptr || updateTimestamp == nullptr ||
1701 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001702 {
1703 messages::internalError(asyncResp->res);
1704 return;
1705 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001706
Ed Tanous002d39b2022-05-31 08:59:27 -07001707 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001708 "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001709 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00001710 crow::utility::urlFromPieces(
1711 "redfish", "v1", "Systems", "system", "LogServices",
1712 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001713 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1714 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1715 asyncResp->res.jsonValue["Message"] = *message;
1716 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001717 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1718 if (notifyAction)
1719 {
1720 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1721 *notifyAction;
1722 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001723 if ((resolution != nullptr) && (!(*resolution).empty()))
1724 {
1725 asyncResp->res.jsonValue["Resolution"] = *resolution;
1726 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001727 asyncResp->res.jsonValue["EntryType"] = "Event";
1728 asyncResp->res.jsonValue["Severity"] =
1729 translateSeverityDbusToRedfish(*severity);
1730 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001731 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001732 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001733 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001734 if (filePath != nullptr)
1735 {
1736 asyncResp->res.jsonValue["AdditionalDataURI"] =
1737 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1738 std::to_string(*id) + "/attachment";
1739 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001740 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001741 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001742
1743 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001744 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001745 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001746 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001747 [&app](const crow::Request& req,
1748 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001749 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001750 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001751 {
1752 return;
1753 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001754 if (systemName != "system")
1755 {
1756 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1757 systemName);
1758 return;
1759 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001760 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001761
Ed Tanous002d39b2022-05-31 08:59:27 -07001762 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1763 resolved))
1764 {
1765 return;
1766 }
1767 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001768
Ed Tanous002d39b2022-05-31 08:59:27 -07001769 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001770 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001771 if (ec)
1772 {
1773 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1774 messages::internalError(asyncResp->res);
1775 return;
1776 }
1777 },
1778 "xyz.openbmc_project.Logging",
1779 "/xyz/openbmc_project/logging/entry/" + entryId,
1780 "org.freedesktop.DBus.Properties", "Set",
1781 "xyz.openbmc_project.Logging.Entry", "Resolved",
1782 dbus::utility::DbusVariantType(*resolved));
1783 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001784
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001785 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001786 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001787 .privileges(redfish::privileges::deleteLogEntry)
1788
Ed Tanous002d39b2022-05-31 08:59:27 -07001789 .methods(boost::beast::http::verb::delete_)(
1790 [&app](const crow::Request& req,
1791 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001792 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001793 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001794 {
1795 return;
1796 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001797 if (systemName != "system")
1798 {
1799 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1800 systemName);
1801 return;
1802 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1804
1805 std::string entryID = param;
1806
1807 dbus::utility::escapePathForDbus(entryID);
1808
1809 // Process response from Logging service.
1810 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001811 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001812 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1813 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001814 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001815 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001816 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001817 messages::resourceNotFound(asyncResp->res, "LogEntry",
1818 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001819 return;
1820 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001821 // TODO Handle for specific error code
1822 BMCWEB_LOG_ERROR
1823 << "EventLogEntry (DBus) doDelete respHandler got error "
1824 << ec;
1825 asyncResp->res.result(
1826 boost::beast::http::status::internal_server_error);
1827 return;
1828 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001829
Ed Tanous002d39b2022-05-31 08:59:27 -07001830 asyncResp->res.result(boost::beast::http::status::ok);
1831 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001832
Ed Tanous002d39b2022-05-31 08:59:27 -07001833 // Make call to Logging service to request Delete Log
1834 crow::connections::systemBus->async_method_call(
1835 respHandler, "xyz.openbmc_project.Logging",
1836 "/xyz/openbmc_project/logging/entry/" + entryID,
1837 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001838 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001839}
1840
1841inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001842{
George Liu0fda0f12021-11-16 10:06:17 +08001843 BMCWEB_ROUTE(
1844 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001845 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001846 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001847 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001848 [&app](const crow::Request& req,
1849 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001850 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001851 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001852 {
1853 return;
1854 }
Ed Tanous99351cd2022-08-07 16:42:51 -07001855 if (http_helpers::isContentTypeAllowed(
1856 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001857 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001858 {
1859 asyncResp->res.result(boost::beast::http::status::bad_request);
1860 return;
1861 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001862 if (systemName != "system")
1863 {
1864 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1865 systemName);
1866 return;
1867 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001868
Ed Tanous002d39b2022-05-31 08:59:27 -07001869 std::string entryID = param;
1870 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001871
Ed Tanous002d39b2022-05-31 08:59:27 -07001872 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001873 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001874 const sdbusplus::message::unix_fd& unixfd) {
1875 if (ec.value() == EBADR)
1876 {
1877 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1878 entryID);
1879 return;
1880 }
1881 if (ec)
1882 {
1883 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1884 messages::internalError(asyncResp->res);
1885 return;
1886 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001887
Ed Tanous002d39b2022-05-31 08:59:27 -07001888 int fd = -1;
1889 fd = dup(unixfd);
1890 if (fd == -1)
1891 {
1892 messages::internalError(asyncResp->res);
1893 return;
1894 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001895
Ed Tanous002d39b2022-05-31 08:59:27 -07001896 long long int size = lseek(fd, 0, SEEK_END);
1897 if (size == -1)
1898 {
1899 messages::internalError(asyncResp->res);
1900 return;
1901 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001902
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 // Arbitrary max size of 64kb
1904 constexpr int maxFileSize = 65536;
1905 if (size > maxFileSize)
1906 {
1907 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1908 << maxFileSize;
1909 messages::internalError(asyncResp->res);
1910 return;
1911 }
1912 std::vector<char> data(static_cast<size_t>(size));
1913 long long int rc = lseek(fd, 0, SEEK_SET);
1914 if (rc == -1)
1915 {
1916 messages::internalError(asyncResp->res);
1917 return;
1918 }
1919 rc = read(fd, data.data(), data.size());
1920 if ((rc == -1) || (rc != size))
1921 {
1922 messages::internalError(asyncResp->res);
1923 return;
1924 }
1925 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001926
Ed Tanous002d39b2022-05-31 08:59:27 -07001927 std::string_view strData(data.data(), data.size());
1928 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001929
Ed Tanousd9f6c622022-03-17 09:12:17 -07001930 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07001931 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07001932 asyncResp->res.addHeader(
1933 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07001934 asyncResp->res.body() = std::move(output);
1935 },
1936 "xyz.openbmc_project.Logging",
1937 "/xyz/openbmc_project/logging/entry/" + entryID,
1938 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1939 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001940}
1941
Spencer Kub7028eb2021-10-26 15:27:35 +08001942constexpr const char* hostLoggerFolderPath = "/var/log/console";
1943
1944inline bool
1945 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1946 std::vector<std::filesystem::path>& hostLoggerFiles)
1947{
1948 std::error_code ec;
1949 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1950 if (ec)
1951 {
1952 BMCWEB_LOG_ERROR << ec.message();
1953 return false;
1954 }
1955 for (const std::filesystem::directory_entry& it : logPath)
1956 {
1957 std::string filename = it.path().filename();
1958 // Prefix of each log files is "log". Find the file and save the
1959 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07001960 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08001961 {
1962 hostLoggerFiles.emplace_back(it.path());
1963 }
1964 }
1965 // As the log files rotate, they are appended with a ".#" that is higher for
1966 // the older logs. Since we start from oldest logs, sort the name in
1967 // descending order.
1968 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1969 AlphanumLess<std::string>());
1970
1971 return true;
1972}
1973
Ed Tanous02cad962022-06-30 16:50:15 -07001974inline bool getHostLoggerEntries(
1975 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
1976 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001977{
1978 GzFileReader logFile;
1979
1980 // Go though all log files and expose host logs.
1981 for (const std::filesystem::path& it : hostLoggerFiles)
1982 {
1983 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1984 {
1985 BMCWEB_LOG_ERROR << "fail to expose host logs";
1986 return false;
1987 }
1988 }
1989 // Get lastMessage from constructor by getter
1990 std::string lastMessage = logFile.getLastMessage();
1991 if (!lastMessage.empty())
1992 {
1993 logCount++;
1994 if (logCount > skip && logCount <= (skip + top))
1995 {
1996 logEntries.push_back(lastMessage);
1997 }
1998 }
1999 return true;
2000}
2001
2002inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2003 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002004 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002005{
2006 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002007 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002008 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2009 "redfish", "v1", "Systems", "system", "LogServices", "HostLogger",
2010 "Entries", logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002011 logEntryJson["Name"] = "Host Logger Entry";
2012 logEntryJson["Id"] = logEntryID;
2013 logEntryJson["Message"] = msg;
2014 logEntryJson["EntryType"] = "Oem";
2015 logEntryJson["Severity"] = "OK";
2016 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002017}
2018
2019inline void requestRoutesSystemHostLogger(App& app)
2020{
Ed Tanous22d268c2022-05-19 09:39:07 -07002021 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002022 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002023 .methods(boost::beast::http::verb::get)(
2024 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002025 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2026 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002027 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002028 {
2029 return;
2030 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002031 if (systemName != "system")
2032 {
2033 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2034 systemName);
2035 return;
2036 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002037 asyncResp->res.jsonValue["@odata.id"] =
2038 "/redfish/v1/Systems/system/LogServices/HostLogger";
2039 asyncResp->res.jsonValue["@odata.type"] =
2040 "#LogService.v1_1_0.LogService";
2041 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2042 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2043 asyncResp->res.jsonValue["Id"] = "HostLogger";
2044 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2045 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2046 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002047}
2048
2049inline void requestRoutesSystemHostLoggerCollection(App& app)
2050{
2051 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002052 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002053 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 .methods(boost::beast::http::verb::get)(
2055 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002056 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2057 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002058 query_param::QueryCapabilities capabilities = {
2059 .canDelegateTop = true,
2060 .canDelegateSkip = true,
2061 };
2062 query_param::Query delegatedQuery;
2063 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002064 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002065 {
2066 return;
2067 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002068 if (systemName != "system")
2069 {
2070 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2071 systemName);
2072 return;
2073 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002074 asyncResp->res.jsonValue["@odata.id"] =
2075 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2076 asyncResp->res.jsonValue["@odata.type"] =
2077 "#LogEntryCollection.LogEntryCollection";
2078 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2079 asyncResp->res.jsonValue["Description"] =
2080 "Collection of HostLogger Entries";
2081 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2082 logEntryArray = nlohmann::json::array();
2083 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002084
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 std::vector<std::filesystem::path> hostLoggerFiles;
2086 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2087 {
2088 BMCWEB_LOG_ERROR << "fail to get host log file path";
2089 return;
2090 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002091 // If we weren't provided top and skip limits, use the defaults.
2092 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002093 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002094 size_t logCount = 0;
2095 // This vector only store the entries we want to expose that
2096 // control by skip and top.
2097 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002098 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2099 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002100 {
2101 messages::internalError(asyncResp->res);
2102 return;
2103 }
2104 // If vector is empty, that means skip value larger than total
2105 // log count
2106 if (logEntries.empty())
2107 {
2108 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2109 return;
2110 }
2111 if (!logEntries.empty())
2112 {
2113 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002114 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002115 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002116 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2117 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002118 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002119 }
2120
Ed Tanous002d39b2022-05-31 08:59:27 -07002121 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002122 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002123 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002124 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2125 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002126 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002127 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002128 }
George Liu0fda0f12021-11-16 10:06:17 +08002129 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002130}
2131
2132inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2133{
2134 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002135 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002136 .privileges(redfish::privileges::getLogEntry)
2137 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002138 [&app](const crow::Request& req,
2139 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002140 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002141 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002142 {
2143 return;
2144 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002145 if (systemName != "system")
2146 {
2147 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2148 systemName);
2149 return;
2150 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002151 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002152
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002154
Ed Tanous002d39b2022-05-31 08:59:27 -07002155 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2156 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08002157
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002159 if (ec == std::errc::invalid_argument ||
2160 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002161 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002162 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002163 return;
2164 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002165
Ed Tanous002d39b2022-05-31 08:59:27 -07002166 std::vector<std::filesystem::path> hostLoggerFiles;
2167 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2168 {
2169 BMCWEB_LOG_ERROR << "fail to get host log file path";
2170 return;
2171 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002172
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002174 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002175 std::vector<std::string> logEntries;
2176 // We can get specific entry by skip and top. For example, if we
2177 // want to get nth entry, we can set skip = n-1 and top = 1 to
2178 // get that entry
2179 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2180 logCount))
2181 {
2182 messages::internalError(asyncResp->res);
2183 return;
2184 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002185
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 if (!logEntries.empty())
2187 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002188 nlohmann::json::object_t hostLogEntry;
2189 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2190 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002191 return;
2192 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002193
Ed Tanous002d39b2022-05-31 08:59:27 -07002194 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002195 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002196 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002197}
2198
Claire Weinandd72e872022-08-15 14:20:06 -07002199inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002200 crow::App& app, const crow::Request& req,
2201 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2202{
2203 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2204 {
2205 return;
2206 }
2207 // Collections don't include the static data added by SubRoute
2208 // because it has a duplicate entry for members
2209 asyncResp->res.jsonValue["@odata.type"] =
2210 "#LogServiceCollection.LogServiceCollection";
2211 asyncResp->res.jsonValue["@odata.id"] =
2212 "/redfish/v1/Managers/bmc/LogServices";
2213 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2214 asyncResp->res.jsonValue["Description"] =
2215 "Collection of LogServices for this Manager";
2216 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2217 logServiceArray = nlohmann::json::array();
2218
2219#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002220 nlohmann::json::object_t journal;
2221 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2222 logServiceArray.push_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002223#endif
2224
2225 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2226
2227#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002228 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002229 "xyz.openbmc_project.Collection.DeleteAll"};
2230 dbus::utility::getSubTreePaths(
2231 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002232 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002233 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002234 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2235 if (ec)
2236 {
2237 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002238 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002239 << ec;
2240 // Assume that getting an error simply means there are no dump
2241 // LogServices. Return without adding any error response.
2242 return;
2243 }
2244
2245 nlohmann::json& logServiceArrayLocal =
2246 asyncResp->res.jsonValue["Members"];
2247
2248 for (const std::string& path : subTreePaths)
2249 {
2250 if (path == "/xyz/openbmc_project/dump/bmc")
2251 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002252 nlohmann::json::object_t member;
2253 member["@odata.id"] =
2254 "/redfish/v1/Managers/bmc/LogServices/Dump";
2255 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002256 }
2257 else if (path == "/xyz/openbmc_project/dump/faultlog")
2258 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002259 nlohmann::json::object_t member;
2260 member["@odata.id"] =
2261 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2262 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002263 }
2264 }
2265
2266 asyncResp->res.jsonValue["Members@odata.count"] =
2267 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002268 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002269#endif
2270}
2271
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002272inline void requestRoutesBMCLogServiceCollection(App& app)
2273{
2274 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002275 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002276 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002277 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002278}
Ed Tanous1da66f72018-07-27 16:13:37 -07002279
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002280inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002281{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002282 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002283 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002284 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002285 [&app](const crow::Request& req,
2286 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002287 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002288 {
2289 return;
2290 }
2291 asyncResp->res.jsonValue["@odata.type"] =
2292 "#LogService.v1_1_0.LogService";
2293 asyncResp->res.jsonValue["@odata.id"] =
2294 "/redfish/v1/Managers/bmc/LogServices/Journal";
2295 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2296 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002297 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002298 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302299
Ed Tanous002d39b2022-05-31 08:59:27 -07002300 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002301 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002302 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2303 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2304 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302305
Ed Tanous002d39b2022-05-31 08:59:27 -07002306 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2307 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2308 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002309}
Jason M. Billse1f26342018-07-18 12:12:00 -07002310
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002311static int
2312 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2313 sd_journal* journal,
2314 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002315{
2316 // Get the Log Entry contents
2317 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002318
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002319 std::string message;
2320 std::string_view syslogID;
2321 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2322 if (ret < 0)
2323 {
2324 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2325 << strerror(-ret);
2326 }
2327 if (!syslogID.empty())
2328 {
2329 message += std::string(syslogID) + ": ";
2330 }
2331
Ed Tanous39e77502019-03-04 17:35:53 -08002332 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002333 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002334 if (ret < 0)
2335 {
2336 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2337 return 1;
2338 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002339 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002340
2341 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002342 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002343 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002344 if (ret < 0)
2345 {
2346 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002347 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002348
2349 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002350 std::string entryTimeStr;
2351 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002352 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002353 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002354 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002355
2356 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002357 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002358 bmcJournalLogEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2359 "redfish", "v1", "Managers", "bmc", "LogServices", "Journal", "Entries",
2360 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002361 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2362 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2363 bmcJournalLogEntryJson["Message"] = std::move(message);
2364 bmcJournalLogEntryJson["EntryType"] = "Oem";
2365 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2366 : severity <= 4 ? "Warning"
2367 : "OK";
2368 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2369 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002370 return 0;
2371}
2372
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002373inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002374{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002375 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002376 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002377 .methods(boost::beast::http::verb::get)(
2378 [&app](const crow::Request& req,
2379 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2380 query_param::QueryCapabilities capabilities = {
2381 .canDelegateTop = true,
2382 .canDelegateSkip = true,
2383 };
2384 query_param::Query delegatedQuery;
2385 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002386 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002387 {
2388 return;
2389 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002390
2391 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002392 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002393
Ed Tanous002d39b2022-05-31 08:59:27 -07002394 // Collections don't include the static data added by SubRoute
2395 // because it has a duplicate entry for members
2396 asyncResp->res.jsonValue["@odata.type"] =
2397 "#LogEntryCollection.LogEntryCollection";
2398 asyncResp->res.jsonValue["@odata.id"] =
2399 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2400 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2401 asyncResp->res.jsonValue["Description"] =
2402 "Collection of BMC Journal Entries";
2403 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2404 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002405
Ed Tanous002d39b2022-05-31 08:59:27 -07002406 // Go through the journal and use the timestamp to create a
2407 // unique ID for each entry
2408 sd_journal* journalTmp = nullptr;
2409 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2410 if (ret < 0)
2411 {
2412 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2413 messages::internalError(asyncResp->res);
2414 return;
2415 }
2416 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2417 journalTmp, sd_journal_close);
2418 journalTmp = nullptr;
2419 uint64_t entryCount = 0;
2420 // Reset the unique ID on the first entry
2421 bool firstEntry = true;
2422 SD_JOURNAL_FOREACH(journal.get())
2423 {
2424 entryCount++;
2425 // Handle paging using skip (number of entries to skip from
2426 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002427 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002428 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002429 continue;
2430 }
2431
2432 std::string idStr;
2433 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2434 {
2435 continue;
2436 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002437 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002438
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002439 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002440 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2441 bmcJournalLogEntry) != 0)
2442 {
George Liu0fda0f12021-11-16 10:06:17 +08002443 messages::internalError(asyncResp->res);
2444 return;
2445 }
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002446 logEntryArray.push_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002447 }
2448 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002449 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002450 {
2451 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2452 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002453 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002454 }
George Liu0fda0f12021-11-16 10:06:17 +08002455 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002456}
Jason M. Billse1f26342018-07-18 12:12:00 -07002457
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002458inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002459{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002460 BMCWEB_ROUTE(app,
2461 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002462 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002463 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002464 [&app](const crow::Request& req,
2465 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2466 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002467 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002468 {
2469 return;
2470 }
2471 // Convert the unique ID back to a timestamp to find the entry
2472 uint64_t ts = 0;
2473 uint64_t index = 0;
2474 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2475 {
2476 return;
2477 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002478
Ed Tanous002d39b2022-05-31 08:59:27 -07002479 sd_journal* journalTmp = nullptr;
2480 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2481 if (ret < 0)
2482 {
2483 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2484 messages::internalError(asyncResp->res);
2485 return;
2486 }
2487 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2488 journalTmp, sd_journal_close);
2489 journalTmp = nullptr;
2490 // Go to the timestamp in the log and move to the entry at the
2491 // index tracking the unique ID
2492 std::string idStr;
2493 bool firstEntry = true;
2494 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2495 if (ret < 0)
2496 {
2497 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2498 << strerror(-ret);
2499 messages::internalError(asyncResp->res);
2500 return;
2501 }
2502 for (uint64_t i = 0; i <= index; i++)
2503 {
2504 sd_journal_next(journal.get());
2505 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2506 {
2507 messages::internalError(asyncResp->res);
2508 return;
2509 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002510 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002511 }
2512 // Confirm that the entry ID matches what was requested
2513 if (idStr != entryID)
2514 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002515 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002516 return;
2517 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002518
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002519 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002520 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002521 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002522 {
2523 messages::internalError(asyncResp->res);
2524 return;
2525 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002526 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002527 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002528}
2529
Claire Weinanfdd26902022-03-01 14:18:25 -08002530inline void
2531 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2532 const std::string& dumpType)
2533{
2534 std::string dumpPath;
2535 std::string overWritePolicy;
2536 bool collectDiagnosticDataSupported = false;
2537
2538 if (dumpType == "BMC")
2539 {
2540 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2541 overWritePolicy = "WrapsWhenFull";
2542 collectDiagnosticDataSupported = true;
2543 }
2544 else if (dumpType == "FaultLog")
2545 {
2546 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2547 overWritePolicy = "Unknown";
2548 collectDiagnosticDataSupported = false;
2549 }
2550 else if (dumpType == "System")
2551 {
2552 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2553 overWritePolicy = "WrapsWhenFull";
2554 collectDiagnosticDataSupported = true;
2555 }
2556 else
2557 {
2558 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2559 << dumpType;
2560 messages::internalError(asyncResp->res);
2561 return;
2562 }
2563
2564 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2565 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2566 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2567 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2568 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2569 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2570
2571 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002572 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002573 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2574 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2575 redfishDateTimeOffset.second;
2576
2577 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002578
2579 if (collectDiagnosticDataSupported)
2580 {
2581 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2582 ["target"] =
2583 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2584 }
Claire Weinan0d946212022-07-13 19:40:19 -07002585
2586 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2587 dbus::utility::getSubTreePaths(
2588 "/xyz/openbmc_project/dump", 0, interfaces,
2589 [asyncResp, dumpType, dumpPath](
2590 const boost::system::error_code& ec,
2591 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2592 if (ec)
2593 {
2594 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2595 << ec;
2596 // Assume that getting an error simply means there are no dump
2597 // LogServices. Return without adding any error response.
2598 return;
2599 }
2600
2601 const std::string dbusDumpPath =
2602 "/xyz/openbmc_project/dump/" +
2603 boost::algorithm::to_lower_copy(dumpType);
2604
2605 for (const std::string& path : subTreePaths)
2606 {
2607 if (path == dbusDumpPath)
2608 {
2609 asyncResp->res
2610 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2611 dumpPath + "/Actions/LogService.ClearLog";
2612 break;
2613 }
2614 }
2615 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002616}
2617
2618inline void handleLogServicesDumpServiceGet(
2619 crow::App& app, const std::string& dumpType, const crow::Request& req,
2620 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2621{
2622 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2623 {
2624 return;
2625 }
2626 getDumpServiceInfo(asyncResp, dumpType);
2627}
2628
Ed Tanous22d268c2022-05-19 09:39:07 -07002629inline void handleLogServicesDumpServiceComputerSystemGet(
2630 crow::App& app, const crow::Request& req,
2631 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2632 const std::string& chassisId)
2633{
2634 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2635 {
2636 return;
2637 }
2638 if (chassisId != "system")
2639 {
2640 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2641 return;
2642 }
2643 getDumpServiceInfo(asyncResp, "System");
2644}
2645
Claire Weinanfdd26902022-03-01 14:18:25 -08002646inline void handleLogServicesDumpEntriesCollectionGet(
2647 crow::App& app, const std::string& dumpType, const crow::Request& req,
2648 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2649{
2650 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2651 {
2652 return;
2653 }
2654 getDumpEntryCollection(asyncResp, dumpType);
2655}
2656
Ed Tanous22d268c2022-05-19 09:39:07 -07002657inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2658 crow::App& app, const crow::Request& req,
2659 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2660 const std::string& chassisId)
2661{
2662 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2663 {
2664 return;
2665 }
2666 if (chassisId != "system")
2667 {
2668 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2669 return;
2670 }
2671 getDumpEntryCollection(asyncResp, "System");
2672}
2673
Claire Weinanfdd26902022-03-01 14:18:25 -08002674inline void handleLogServicesDumpEntryGet(
2675 crow::App& app, const std::string& dumpType, const crow::Request& req,
2676 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2677 const std::string& dumpId)
2678{
2679 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2680 {
2681 return;
2682 }
2683 getDumpEntryById(asyncResp, dumpId, dumpType);
2684}
Ed Tanous22d268c2022-05-19 09:39:07 -07002685inline void handleLogServicesDumpEntryComputerSystemGet(
2686 crow::App& app, const crow::Request& req,
2687 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2688 const std::string& chassisId, const std::string& dumpId)
2689{
2690 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2691 {
2692 return;
2693 }
2694 if (chassisId != "system")
2695 {
2696 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2697 return;
2698 }
2699 getDumpEntryById(asyncResp, dumpId, "System");
2700}
Claire Weinanfdd26902022-03-01 14:18:25 -08002701
2702inline void handleLogServicesDumpEntryDelete(
2703 crow::App& app, const std::string& dumpType, const crow::Request& req,
2704 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2705 const std::string& dumpId)
2706{
2707 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2708 {
2709 return;
2710 }
2711 deleteDumpEntry(asyncResp, dumpId, dumpType);
2712}
2713
Ed Tanous22d268c2022-05-19 09:39:07 -07002714inline void handleLogServicesDumpEntryComputerSystemDelete(
2715 crow::App& app, const crow::Request& req,
2716 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2717 const std::string& chassisId, const std::string& dumpId)
2718{
2719 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2720 {
2721 return;
2722 }
2723 if (chassisId != "system")
2724 {
2725 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2726 return;
2727 }
2728 deleteDumpEntry(asyncResp, dumpId, "System");
2729}
2730
Claire Weinanfdd26902022-03-01 14:18:25 -08002731inline void handleLogServicesDumpCollectDiagnosticDataPost(
2732 crow::App& app, const std::string& dumpType, const crow::Request& req,
2733 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2734{
2735 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2736 {
2737 return;
2738 }
2739 createDump(asyncResp, req, dumpType);
2740}
2741
Ed Tanous22d268c2022-05-19 09:39:07 -07002742inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2743 crow::App& app, const crow::Request& req,
2744 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2745 const std::string& chassisId)
2746{
2747 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2748 {
2749 return;
2750 }
2751 if (chassisId != "system")
2752 {
2753 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2754 return;
2755 }
2756 createDump(asyncResp, req, "System");
2757}
2758
Claire Weinanfdd26902022-03-01 14:18:25 -08002759inline void handleLogServicesDumpClearLogPost(
2760 crow::App& app, const std::string& dumpType, const crow::Request& req,
2761 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2762{
2763 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2764 {
2765 return;
2766 }
2767 clearDump(asyncResp, dumpType);
2768}
2769
Ed Tanous22d268c2022-05-19 09:39:07 -07002770inline void handleLogServicesDumpClearLogComputerSystemPost(
2771 crow::App& app, const crow::Request& req,
2772 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2773 const std::string& chassisId)
2774{
2775 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2776 {
2777 return;
2778 }
2779 if (chassisId != "system")
2780 {
2781 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2782 return;
2783 }
2784 clearDump(asyncResp, "System");
2785}
2786
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002787inline void requestRoutesBMCDumpService(App& app)
2788{
2789 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002790 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002791 .methods(boost::beast::http::verb::get)(std::bind_front(
2792 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002793}
2794
2795inline void requestRoutesBMCDumpEntryCollection(App& app)
2796{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002797 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002798 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002799 .methods(boost::beast::http::verb::get)(std::bind_front(
2800 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002801}
2802
2803inline void requestRoutesBMCDumpEntry(App& app)
2804{
2805 BMCWEB_ROUTE(app,
2806 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002807 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002808 .methods(boost::beast::http::verb::get)(std::bind_front(
2809 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2810
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002811 BMCWEB_ROUTE(app,
2812 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002813 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002814 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2815 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002816}
2817
2818inline void requestRoutesBMCDumpCreate(App& app)
2819{
George Liu0fda0f12021-11-16 10:06:17 +08002820 BMCWEB_ROUTE(
2821 app,
2822 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002823 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002824 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002825 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2826 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002827}
2828
2829inline void requestRoutesBMCDumpClear(App& app)
2830{
George Liu0fda0f12021-11-16 10:06:17 +08002831 BMCWEB_ROUTE(
2832 app,
2833 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002834 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002835 .methods(boost::beast::http::verb::post)(std::bind_front(
2836 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2837}
2838
2839inline void requestRoutesFaultLogDumpService(App& app)
2840{
2841 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2842 .privileges(redfish::privileges::getLogService)
2843 .methods(boost::beast::http::verb::get)(std::bind_front(
2844 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2845}
2846
2847inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2848{
2849 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2850 .privileges(redfish::privileges::getLogEntryCollection)
2851 .methods(boost::beast::http::verb::get)(
2852 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2853 std::ref(app), "FaultLog"));
2854}
2855
2856inline void requestRoutesFaultLogDumpEntry(App& app)
2857{
2858 BMCWEB_ROUTE(app,
2859 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2860 .privileges(redfish::privileges::getLogEntry)
2861 .methods(boost::beast::http::verb::get)(std::bind_front(
2862 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2863
2864 BMCWEB_ROUTE(app,
2865 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2866 .privileges(redfish::privileges::deleteLogEntry)
2867 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2868 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2869}
2870
2871inline void requestRoutesFaultLogDumpClear(App& app)
2872{
2873 BMCWEB_ROUTE(
2874 app,
2875 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2876 .privileges(redfish::privileges::postLogService)
2877 .methods(boost::beast::http::verb::post)(std::bind_front(
2878 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002879}
2880
2881inline void requestRoutesSystemDumpService(App& app)
2882{
Ed Tanous22d268c2022-05-19 09:39:07 -07002883 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002884 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002885 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002886 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002887}
2888
2889inline void requestRoutesSystemDumpEntryCollection(App& app)
2890{
Ed Tanous22d268c2022-05-19 09:39:07 -07002891 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002892 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002893 .methods(boost::beast::http::verb::get)(std::bind_front(
2894 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2895 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002896}
2897
2898inline void requestRoutesSystemDumpEntry(App& app)
2899{
2900 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002901 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002902 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002903 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002904 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002905
2906 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002907 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002908 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002909 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002910 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002911}
2912
2913inline void requestRoutesSystemDumpCreate(App& app)
2914{
George Liu0fda0f12021-11-16 10:06:17 +08002915 BMCWEB_ROUTE(
2916 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002917 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002918 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07002919 .methods(boost::beast::http::verb::post)(std::bind_front(
2920 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
2921 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002922}
2923
2924inline void requestRoutesSystemDumpClear(App& app)
2925{
George Liu0fda0f12021-11-16 10:06:17 +08002926 BMCWEB_ROUTE(
2927 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002928 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002929 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002930 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002931 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002932}
2933
2934inline void requestRoutesCrashdumpService(App& app)
2935{
2936 // Note: Deviated from redfish privilege registry for GET & HEAD
2937 // method for security reasons.
2938 /**
2939 * Functions triggers appropriate requests on DBus
2940 */
Ed Tanous22d268c2022-05-19 09:39:07 -07002941 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002942 // This is incorrect, should be:
2943 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002944 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002945 .methods(boost::beast::http::verb::get)(
2946 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002947 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2948 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002949 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002950 {
2951 return;
2952 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002953 if (systemName != "system")
2954 {
2955 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2956 systemName);
2957 return;
2958 }
2959
Ed Tanous002d39b2022-05-31 08:59:27 -07002960 // Copy over the static data to include the entries added by
2961 // SubRoute
2962 asyncResp->res.jsonValue["@odata.id"] =
2963 "/redfish/v1/Systems/system/LogServices/Crashdump";
2964 asyncResp->res.jsonValue["@odata.type"] =
2965 "#LogService.v1_2_0.LogService";
2966 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2967 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2968 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2969 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2970 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302971
Ed Tanous002d39b2022-05-31 08:59:27 -07002972 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002973 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002974 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2975 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2976 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302977
Ed Tanous002d39b2022-05-31 08:59:27 -07002978 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002979 crow::utility::urlFromPieces("redfish", "v1", "Systems", "system",
2980 "LogServices", "Crashdump", "Entries");
Ed Tanous002d39b2022-05-31 08:59:27 -07002981 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2982 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2983 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2984 ["target"] =
2985 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002986 });
2987}
2988
2989void inline requestRoutesCrashdumpClear(App& app)
2990{
George Liu0fda0f12021-11-16 10:06:17 +08002991 BMCWEB_ROUTE(
2992 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002993 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002994 // This is incorrect, should be:
2995 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002996 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002997 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002998 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002999 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3000 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003001 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003002 {
3003 return;
3004 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003005 if (systemName != "system")
3006 {
3007 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3008 systemName);
3009 return;
3010 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003011 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003012 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003013 const std::string&) {
3014 if (ec)
3015 {
3016 messages::internalError(asyncResp->res);
3017 return;
3018 }
3019 messages::success(asyncResp->res);
3020 },
3021 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3022 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003023}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003024
zhanghch058d1b46d2021-04-01 11:18:24 +08003025static void
3026 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3027 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003028{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003029 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003030 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003031 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003032 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003033 if (ec)
3034 {
3035 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3036 if (ec.value() ==
3037 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003038 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003039 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003040 }
3041 else
3042 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003043 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003044 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003045 return;
3046 }
3047
3048 std::string timestamp{};
3049 std::string filename{};
3050 std::string logfile{};
3051 parseCrashdumpParameters(params, filename, timestamp, logfile);
3052
3053 if (filename.empty() || timestamp.empty())
3054 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003055 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003056 return;
3057 }
3058
3059 std::string crashdumpURI =
3060 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3061 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003062 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003063 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003064 logEntry["@odata.id"] = crow::utility::urlFromPieces(
3065 "redfish", "v1", "Systems", "system", "LogServices", "Crashdump",
3066 "Entries", logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003067 logEntry["Name"] = "CPU Crashdump";
3068 logEntry["Id"] = logID;
3069 logEntry["EntryType"] = "Oem";
3070 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3071 logEntry["DiagnosticDataType"] = "OEM";
3072 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3073 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003074
3075 // If logEntryJson references an array of LogEntry resources
3076 // ('Members' list), then push this as a new entry, otherwise set it
3077 // directly
3078 if (logEntryJson.is_array())
3079 {
3080 logEntryJson.push_back(logEntry);
3081 asyncResp->res.jsonValue["Members@odata.count"] =
3082 logEntryJson.size();
3083 }
3084 else
3085 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003086 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003087 }
3088 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003089 sdbusplus::asio::getAllProperties(
3090 *crow::connections::systemBus, crashdumpObject,
3091 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3092 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003093}
3094
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003095inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003096{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003097 // Note: Deviated from redfish privilege registry for GET & HEAD
3098 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003099 /**
3100 * Functions triggers appropriate requests on DBus
3101 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003102 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003103 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003104 // This is incorrect, should be.
3105 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003106 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003107 .methods(boost::beast::http::verb::get)(
3108 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003109 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3110 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003111 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003112 {
3113 return;
3114 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003115 if (systemName != "system")
3116 {
3117 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3118 systemName);
3119 return;
3120 }
3121
George Liu7a1dbc42022-12-07 16:03:22 +08003122 constexpr std::array<std::string_view, 1> interfaces = {
3123 crashdumpInterface};
3124 dbus::utility::getSubTreePaths(
3125 "/", 0, interfaces,
3126 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003127 const std::vector<std::string>& resp) {
3128 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003129 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003130 if (ec.value() !=
3131 boost::system::errc::no_such_file_or_directory)
3132 {
3133 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3134 << ec.message();
3135 messages::internalError(asyncResp->res);
3136 return;
3137 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003138 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003139 asyncResp->res.jsonValue["@odata.type"] =
3140 "#LogEntryCollection.LogEntryCollection";
3141 asyncResp->res.jsonValue["@odata.id"] =
3142 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3143 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3144 asyncResp->res.jsonValue["Description"] =
3145 "Collection of Crashdump Entries";
3146 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3147 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003148
Ed Tanous002d39b2022-05-31 08:59:27 -07003149 for (const std::string& path : resp)
3150 {
3151 const sdbusplus::message::object_path objPath(path);
3152 // Get the log ID
3153 std::string logID = objPath.filename();
3154 if (logID.empty())
3155 {
3156 continue;
3157 }
3158 // Add the log entry to the array
3159 logCrashdumpEntry(asyncResp, logID,
3160 asyncResp->res.jsonValue["Members"]);
3161 }
George Liu7a1dbc42022-12-07 16:03:22 +08003162 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003163 });
3164}
Ed Tanous1da66f72018-07-27 16:13:37 -07003165
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003166inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003167{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003168 // Note: Deviated from redfish privilege registry for GET & HEAD
3169 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003170
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003171 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003172 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003173 // this is incorrect, should be
3174 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003175 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003176 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003177 [&app](const crow::Request& req,
3178 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003179 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003180 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003181 {
3182 return;
3183 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003184 if (systemName != "system")
3185 {
3186 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3187 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003188 return;
3189 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003190 const std::string& logID = param;
3191 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3192 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003193}
Ed Tanous1da66f72018-07-27 16:13:37 -07003194
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003195inline void requestRoutesCrashdumpFile(App& app)
3196{
3197 // Note: Deviated from redfish privilege registry for GET & HEAD
3198 // method for security reasons.
3199 BMCWEB_ROUTE(
3200 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003201 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003202 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003203 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003204 [](const crow::Request& req,
3205 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003206 const std::string& systemName, const std::string& logID,
3207 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003208 // Do not call getRedfishRoute here since the crashdump file is not a
3209 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003210
3211 if (systemName != "system")
3212 {
3213 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3214 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003215 return;
3216 }
3217
Ed Tanous002d39b2022-05-31 08:59:27 -07003218 auto getStoredLogCallback =
3219 [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003220 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003221 const std::vector<
3222 std::pair<std::string, dbus::utility::DbusVariantType>>&
3223 resp) {
3224 if (ec)
3225 {
3226 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3227 messages::internalError(asyncResp->res);
3228 return;
3229 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003230
Ed Tanous002d39b2022-05-31 08:59:27 -07003231 std::string dbusFilename{};
3232 std::string dbusTimestamp{};
3233 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003234
Ed Tanous002d39b2022-05-31 08:59:27 -07003235 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3236 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003237
Ed Tanous002d39b2022-05-31 08:59:27 -07003238 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3239 dbusFilepath.empty())
3240 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003241 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003242 return;
3243 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003244
Ed Tanous002d39b2022-05-31 08:59:27 -07003245 // Verify the file name parameter is correct
3246 if (fileName != dbusFilename)
3247 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003248 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003249 return;
3250 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003251
Ed Tanous002d39b2022-05-31 08:59:27 -07003252 if (!std::filesystem::exists(dbusFilepath))
3253 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003254 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003255 return;
3256 }
3257 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3258 asyncResp->res.body() =
3259 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003260
Ed Tanous002d39b2022-05-31 08:59:27 -07003261 // Configure this to be a file download when accessed
3262 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003263 asyncResp->res.addHeader(
3264 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003265 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003266 sdbusplus::asio::getAllProperties(
3267 *crow::connections::systemBus, crashdumpObject,
3268 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3269 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003270 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003271}
3272
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003273enum class OEMDiagnosticType
3274{
3275 onDemand,
3276 telemetry,
3277 invalid,
3278};
3279
Ed Tanous26ccae32023-02-16 10:28:44 -08003280inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003281{
3282 if (oemDiagStr == "OnDemand")
3283 {
3284 return OEMDiagnosticType::onDemand;
3285 }
3286 if (oemDiagStr == "Telemetry")
3287 {
3288 return OEMDiagnosticType::telemetry;
3289 }
3290
3291 return OEMDiagnosticType::invalid;
3292}
3293
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003294inline void requestRoutesCrashdumpCollect(App& app)
3295{
3296 // Note: Deviated from redfish privilege registry for GET & HEAD
3297 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003298 BMCWEB_ROUTE(
3299 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003300 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003301 // The below is incorrect; Should be ConfigureManager
3302 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003303 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003304 .methods(boost::beast::http::verb::post)(
3305 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003306 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3307 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003308 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003309 {
3310 return;
3311 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003312
3313 if (systemName != "system")
3314 {
3315 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3316 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003317 return;
3318 }
3319
Ed Tanous002d39b2022-05-31 08:59:27 -07003320 std::string diagnosticDataType;
3321 std::string oemDiagnosticDataType;
3322 if (!redfish::json_util::readJsonAction(
3323 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3324 "OEMDiagnosticDataType", oemDiagnosticDataType))
3325 {
3326 return;
3327 }
3328
3329 if (diagnosticDataType != "OEM")
3330 {
3331 BMCWEB_LOG_ERROR
3332 << "Only OEM DiagnosticDataType supported for Crashdump";
3333 messages::actionParameterValueFormatError(
3334 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3335 "CollectDiagnosticData");
3336 return;
3337 }
3338
3339 OEMDiagnosticType oemDiagType =
3340 getOEMDiagnosticType(oemDiagnosticDataType);
3341
3342 std::string iface;
3343 std::string method;
3344 std::string taskMatchStr;
3345 if (oemDiagType == OEMDiagnosticType::onDemand)
3346 {
3347 iface = crashdumpOnDemandInterface;
3348 method = "GenerateOnDemandLog";
3349 taskMatchStr = "type='signal',"
3350 "interface='org.freedesktop.DBus.Properties',"
3351 "member='PropertiesChanged',"
3352 "arg0namespace='com.intel.crashdump'";
3353 }
3354 else if (oemDiagType == OEMDiagnosticType::telemetry)
3355 {
3356 iface = crashdumpTelemetryInterface;
3357 method = "GenerateTelemetryLog";
3358 taskMatchStr = "type='signal',"
3359 "interface='org.freedesktop.DBus.Properties',"
3360 "member='PropertiesChanged',"
3361 "arg0namespace='com.intel.crashdump'";
3362 }
3363 else
3364 {
3365 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3366 << oemDiagnosticDataType;
3367 messages::actionParameterValueFormatError(
3368 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3369 "CollectDiagnosticData");
3370 return;
3371 }
3372
3373 auto collectCrashdumpCallback =
3374 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003375 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003376 const std::string&) mutable {
3377 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003378 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003379 if (ec.value() == boost::system::errc::operation_not_supported)
3380 {
3381 messages::resourceInStandby(asyncResp->res);
3382 }
3383 else if (ec.value() ==
3384 boost::system::errc::device_or_resource_busy)
3385 {
3386 messages::serviceTemporarilyUnavailable(asyncResp->res,
3387 "60");
3388 }
3389 else
3390 {
3391 messages::internalError(asyncResp->res);
3392 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003393 return;
3394 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003395 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003396 [](const boost::system::error_code& err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003397 const std::shared_ptr<task::TaskData>& taskData) {
3398 if (!err)
3399 {
3400 taskData->messages.emplace_back(messages::taskCompletedOK(
3401 std::to_string(taskData->index)));
3402 taskData->state = "Completed";
3403 }
3404 return task::completed;
3405 },
3406 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003407
Ed Tanous002d39b2022-05-31 08:59:27 -07003408 task->startTimer(std::chrono::minutes(5));
3409 task->populateResp(asyncResp->res);
3410 task->payload.emplace(std::move(payload));
3411 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003412
Ed Tanous002d39b2022-05-31 08:59:27 -07003413 crow::connections::systemBus->async_method_call(
3414 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3415 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003416 });
3417}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003418
Andrew Geisslercb92c032018-08-17 07:56:14 -07003419/**
3420 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3421 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003422inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003423{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003424 /**
3425 * Function handles POST method request.
3426 * The Clear Log actions does not require any parameter.The action deletes
3427 * all entries found in the Entries collection for this Log Service.
3428 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003429
George Liu0fda0f12021-11-16 10:06:17 +08003430 BMCWEB_ROUTE(
3431 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003432 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003433 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003434 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003435 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003436 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3437 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003438 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003439 {
3440 return;
3441 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003442 if (systemName != "system")
3443 {
3444 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3445 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003446 return;
3447 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003448 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003449
Ed Tanous002d39b2022-05-31 08:59:27 -07003450 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003451 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003452 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3453 if (ec)
3454 {
3455 // TODO Handle for specific error code
3456 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3457 asyncResp->res.result(
3458 boost::beast::http::status::internal_server_error);
3459 return;
3460 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003461
Ed Tanous002d39b2022-05-31 08:59:27 -07003462 asyncResp->res.result(boost::beast::http::status::no_content);
3463 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003464
Ed Tanous002d39b2022-05-31 08:59:27 -07003465 // Make call to Logging service to request Clear Log
3466 crow::connections::systemBus->async_method_call(
3467 respHandler, "xyz.openbmc_project.Logging",
3468 "/xyz/openbmc_project/logging",
3469 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3470 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003471}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003472
3473/****************************************************
3474 * Redfish PostCode interfaces
3475 * using DBUS interface: getPostCodesTS
3476 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003477inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003478{
Ed Tanous22d268c2022-05-19 09:39:07 -07003479 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003480 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003481 .methods(boost::beast::http::verb::get)(
3482 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003483 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3484 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003485 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003486 {
3487 return;
3488 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003489 if (systemName != "system")
3490 {
3491 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3492 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003493 return;
3494 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003495 asyncResp->res.jsonValue["@odata.id"] =
3496 "/redfish/v1/Systems/system/LogServices/PostCodes";
3497 asyncResp->res.jsonValue["@odata.type"] =
3498 "#LogService.v1_1_0.LogService";
3499 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3500 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003501 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003502 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3503 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3504 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303505
Ed Tanous002d39b2022-05-31 08:59:27 -07003506 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003507 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003508 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3509 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3510 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303511
Ed Tanous002d39b2022-05-31 08:59:27 -07003512 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3513 {"target",
3514 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003515 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003516}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003517
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003518inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003519{
George Liu0fda0f12021-11-16 10:06:17 +08003520 BMCWEB_ROUTE(
3521 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003522 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003523 // The following privilege is incorrect; It should be ConfigureManager
3524 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003525 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003526 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003527 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003528 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3529 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003530 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003531 {
3532 return;
3533 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003534 if (systemName != "system")
3535 {
3536 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3537 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003538 return;
3539 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003540 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003541
Ed Tanous002d39b2022-05-31 08:59:27 -07003542 // Make call to post-code service to request clear all
3543 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003544 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003545 if (ec)
3546 {
3547 // TODO Handle for specific error code
3548 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3549 << ec;
3550 asyncResp->res.result(
3551 boost::beast::http::status::internal_server_error);
3552 messages::internalError(asyncResp->res);
3553 return;
3554 }
3555 },
3556 "xyz.openbmc_project.State.Boot.PostCode0",
3557 "/xyz/openbmc_project/State/Boot/PostCode0",
3558 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3559 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003560}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003561
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003562/**
3563 * @brief Parse post code ID and get the current value and index value
3564 * eg: postCodeID=B1-2, currentValue=1, index=2
3565 *
3566 * @param[in] postCodeID Post Code ID
3567 * @param[out] currentValue Current value
3568 * @param[out] index Index value
3569 *
3570 * @return bool true if the parsing is successful, false the parsing fails
3571 */
3572inline static bool parsePostCode(const std::string& postCodeID,
3573 uint64_t& currentValue, uint16_t& index)
3574{
3575 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003576 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003577 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3578 {
3579 return false;
3580 }
3581
3582 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3583 const char* start = split[0].data() + 1;
3584 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3585 const char* end = split[0].data() + split[0].size();
3586 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3587
3588 if (ptrIndex != end || ecIndex != std::errc())
3589 {
3590 return false;
3591 }
3592
3593 start = split[1].data();
3594
3595 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3596 end = split[1].data() + split[1].size();
3597 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3598
3599 return ptrValue == end && ecValue == std::errc();
3600}
3601
3602static bool fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003603 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303604 const boost::container::flat_map<
3605 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003606 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3607 const uint64_t skip = 0, const uint64_t top = 0)
3608{
3609 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003610 const registries::Message* message =
3611 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003612
3613 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003614 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303615 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3616 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003617 {
3618 currentCodeIndex++;
3619 std::string postcodeEntryID =
3620 "B" + std::to_string(bootIndex) + "-" +
3621 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3622
3623 uint64_t usecSinceEpoch = code.first;
3624 uint64_t usTimeOffset = 0;
3625
3626 if (1 == currentCodeIndex)
3627 { // already incremented
3628 firstCodeTimeUs = code.first;
3629 }
3630 else
3631 {
3632 usTimeOffset = code.first - firstCodeTimeUs;
3633 }
3634
3635 // skip if no specific codeIndex is specified and currentCodeIndex does
3636 // not fall between top and skip
3637 if ((codeIndex == 0) &&
3638 (currentCodeIndex <= skip || currentCodeIndex > top))
3639 {
3640 continue;
3641 }
3642
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003643 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003644 // currentIndex
3645 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3646 {
3647 // This is done for simplicity. 1st entry is needed to calculate
3648 // time offset. To improve efficiency, one can get to the entry
3649 // directly (possibly with flatmap's nth method)
3650 continue;
3651 }
3652
3653 // currentCodeIndex is within top and skip or equal to specified code
3654 // index
3655
3656 // Get the Created time from the timestamp
3657 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003658 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003659
3660 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3661 std::ostringstream hexCode;
3662 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303663 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003664 std::ostringstream timeOffsetStr;
3665 // Set Fixed -Point Notation
3666 timeOffsetStr << std::fixed;
3667 // Set precision to 4 digits
3668 timeOffsetStr << std::setprecision(4);
3669 // Add double to stream
3670 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3671 std::vector<std::string> messageArgs = {
3672 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3673
3674 // Get MessageArgs template from message registry
3675 std::string msg;
3676 if (message != nullptr)
3677 {
3678 msg = message->message;
3679
3680 // fill in this post code value
3681 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003682 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003683 {
3684 std::string argStr = "%" + std::to_string(++i);
3685 size_t argPos = msg.find(argStr);
3686 if (argPos != std::string::npos)
3687 {
3688 msg.replace(argPos, argStr.length(), messageArg);
3689 }
3690 }
3691 }
3692
Tim Leed4342a92020-04-27 11:47:58 +08003693 // Get Severity template from message registry
3694 std::string severity;
3695 if (message != nullptr)
3696 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003697 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003698 }
3699
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003700 // Format entry
3701 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003702 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003703 bmcLogEntry["@odata.id"] = crow::utility::urlFromPieces(
3704 "redfish", "v1", "Systems", "system", "LogServices", "PostCodes",
3705 "Entries", postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003706 bmcLogEntry["Name"] = "POST Code Log Entry";
3707 bmcLogEntry["Id"] = postcodeEntryID;
3708 bmcLogEntry["Message"] = std::move(msg);
3709 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3710 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3711 bmcLogEntry["EntryType"] = "Event";
3712 bmcLogEntry["Severity"] = std::move(severity);
3713 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003714 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3715 {
3716 bmcLogEntry["AdditionalDataURI"] =
3717 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3718 postcodeEntryID + "/attachment";
3719 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003720
3721 // codeIndex is only specified when querying single entry, return only
3722 // that entry in this case
3723 if (codeIndex != 0)
3724 {
3725 aResp->res.jsonValue.update(bmcLogEntry);
3726 return true;
3727 }
3728
3729 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3730 logEntryArray.push_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003731 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003732
3733 // Return value is always false when querying multiple entries
3734 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003735}
3736
zhanghch058d1b46d2021-04-01 11:18:24 +08003737static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003738 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003739{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003740 uint16_t bootIndex = 0;
3741 uint64_t codeIndex = 0;
3742 if (!parsePostCode(entryId, codeIndex, bootIndex))
3743 {
3744 // Requested ID was not found
3745 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3746 return;
3747 }
3748
3749 if (bootIndex == 0 || codeIndex == 0)
3750 {
3751 // 0 is an invalid index
3752 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3753 return;
3754 }
3755
ZhikuiRena3316fc2020-01-29 14:58:08 -08003756 crow::connections::systemBus->async_method_call(
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003757 [aResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003758 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303759 const boost::container::flat_map<
3760 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3761 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003762 if (ec)
3763 {
3764 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3765 messages::internalError(aResp->res);
3766 return;
3767 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003768
Ed Tanous002d39b2022-05-31 08:59:27 -07003769 if (postcode.empty())
3770 {
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003771 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003772 return;
3773 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003774
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003775 if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
3776 {
3777 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3778 return;
3779 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003780 },
Jonathan Doman15124762021-01-07 17:54:17 -08003781 "xyz.openbmc_project.State.Boot.PostCode0",
3782 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003783 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3784 bootIndex);
3785}
3786
zhanghch058d1b46d2021-04-01 11:18:24 +08003787static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003788 const uint16_t bootIndex,
3789 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003790 const uint64_t entryCount, size_t skip,
3791 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003792{
3793 crow::connections::systemBus->async_method_call(
3794 [aResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003795 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303796 const boost::container::flat_map<
3797 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3798 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003799 if (ec)
3800 {
3801 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3802 messages::internalError(aResp->res);
3803 return;
3804 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003805
Ed Tanous002d39b2022-05-31 08:59:27 -07003806 uint64_t endCount = entryCount;
3807 if (!postcode.empty())
3808 {
3809 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003810 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003811 {
Ed Tanous3648c8b2022-07-25 13:39:59 -07003812 uint64_t thisBootSkip =
3813 std::max(static_cast<uint64_t>(skip), entryCount) -
3814 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003815 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003816 std::min(static_cast<uint64_t>(top + skip), endCount) -
3817 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003818
3819 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3820 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003821 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003822 aResp->res.jsonValue["Members@odata.count"] = endCount;
3823 }
3824
3825 // continue to previous bootIndex
3826 if (bootIndex < bootCount)
3827 {
3828 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3829 bootCount, endCount, skip, top);
3830 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003831 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003832 {
3833 aResp->res.jsonValue["Members@odata.nextLink"] =
3834 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3835 std::to_string(skip + top);
3836 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003837 },
Jonathan Doman15124762021-01-07 17:54:17 -08003838 "xyz.openbmc_project.State.Boot.PostCode0",
3839 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003840 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3841 bootIndex);
3842}
3843
zhanghch058d1b46d2021-04-01 11:18:24 +08003844static void
3845 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003846 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003847{
3848 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003849 sdbusplus::asio::getProperty<uint16_t>(
3850 *crow::connections::systemBus,
3851 "xyz.openbmc_project.State.Boot.PostCode0",
3852 "/xyz/openbmc_project/State/Boot/PostCode0",
3853 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003854 [aResp, entryCount, skip, top](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003855 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003856 if (ec)
3857 {
3858 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3859 messages::internalError(aResp->res);
3860 return;
3861 }
3862 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003863 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003864}
3865
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003866inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003867{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003868 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003869 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003870 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003871 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003872 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003873 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3874 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003875 query_param::QueryCapabilities capabilities = {
3876 .canDelegateTop = true,
3877 .canDelegateSkip = true,
3878 };
3879 query_param::Query delegatedQuery;
3880 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003881 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003882 {
3883 return;
3884 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003885
3886 if (systemName != "system")
3887 {
3888 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3889 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003890 return;
3891 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003892 asyncResp->res.jsonValue["@odata.type"] =
3893 "#LogEntryCollection.LogEntryCollection";
3894 asyncResp->res.jsonValue["@odata.id"] =
3895 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3896 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3897 asyncResp->res.jsonValue["Description"] =
3898 "Collection of POST Code Log Entries";
3899 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3900 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003901 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003902 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003903 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003904 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003905}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003906
George Liu647b3cd2021-07-05 12:43:56 +08003907inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3908{
George Liu0fda0f12021-11-16 10:06:17 +08003909 BMCWEB_ROUTE(
3910 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003911 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003912 .privileges(redfish::privileges::getLogEntry)
3913 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003914 [&app](const crow::Request& req,
3915 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003916 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07003917 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003918 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003919 {
3920 return;
3921 }
Ed Tanous99351cd2022-08-07 16:42:51 -07003922 if (http_helpers::isContentTypeAllowed(
3923 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07003924 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07003925 {
3926 asyncResp->res.result(boost::beast::http::status::bad_request);
3927 return;
3928 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003929 if (systemName != "system")
3930 {
3931 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3932 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003933 return;
3934 }
George Liu647b3cd2021-07-05 12:43:56 +08003935
Ed Tanous002d39b2022-05-31 08:59:27 -07003936 uint64_t currentValue = 0;
3937 uint16_t index = 0;
3938 if (!parsePostCode(postCodeID, currentValue, index))
3939 {
3940 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3941 return;
3942 }
George Liu647b3cd2021-07-05 12:43:56 +08003943
Ed Tanous002d39b2022-05-31 08:59:27 -07003944 crow::connections::systemBus->async_method_call(
3945 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003946 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003947 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3948 postcodes) {
3949 if (ec.value() == EBADR)
3950 {
3951 messages::resourceNotFound(asyncResp->res, "LogEntry",
3952 postCodeID);
3953 return;
3954 }
3955 if (ec)
3956 {
3957 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3958 messages::internalError(asyncResp->res);
3959 return;
3960 }
George Liu647b3cd2021-07-05 12:43:56 +08003961
Ed Tanous002d39b2022-05-31 08:59:27 -07003962 size_t value = static_cast<size_t>(currentValue) - 1;
3963 if (value == std::string::npos || postcodes.size() < currentValue)
3964 {
3965 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3966 messages::resourceNotFound(asyncResp->res, "LogEntry",
3967 postCodeID);
3968 return;
3969 }
George Liu647b3cd2021-07-05 12:43:56 +08003970
Ed Tanous002d39b2022-05-31 08:59:27 -07003971 const auto& [tID, c] = postcodes[value];
3972 if (c.empty())
3973 {
3974 BMCWEB_LOG_INFO << "No found post code data";
3975 messages::resourceNotFound(asyncResp->res, "LogEntry",
3976 postCodeID);
3977 return;
3978 }
3979 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3980 const char* d = reinterpret_cast<const char*>(c.data());
3981 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08003982
Ed Tanousd9f6c622022-03-17 09:12:17 -07003983 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07003984 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07003985 asyncResp->res.addHeader(
3986 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07003987 asyncResp->res.body() = crow::utility::base64encode(strData);
3988 },
3989 "xyz.openbmc_project.State.Boot.PostCode0",
3990 "/xyz/openbmc_project/State/Boot/PostCode0",
3991 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3992 });
George Liu647b3cd2021-07-05 12:43:56 +08003993}
3994
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003995inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003996{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003997 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003998 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003999 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004000 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004001 [&app](const crow::Request& req,
4002 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004003 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004004 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004005 {
4006 return;
4007 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004008 if (systemName != "system")
4009 {
4010 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4011 systemName);
4012 return;
4013 }
4014
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004015 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004016 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004017}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004018
Ed Tanous1da66f72018-07-27 16:13:37 -07004019} // namespace redfish