blob: 2899b011f6f073e17c5c816a1de2d599a9230f80 [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"
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -060021#include "generated/enums/log_entry.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080022#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080023#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080024#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "query.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070026#include "registries.hpp"
27#include "registries/base_message_registry.hpp"
28#include "registries/openbmc_message_registry.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include "registries/privilege_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080030#include "task.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080031#include "utils/dbus_utils.hpp"
32#include "utils/time_utils.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070033
Jason M. Billse1f26342018-07-18 12:12:00 -070034#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060035#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060036#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070037
Ed Tanous9896eae2022-07-23 15:07:33 -070038#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070039#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060040#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070041#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070042#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070043#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080044#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070045#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020046#include <sdbusplus/asio/property.hpp>
47#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050048
George Liu7a1dbc42022-12-07 16:03:22 +080049#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080050#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070051#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080052#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070053#include <ranges>
Ed Tanous26702d02021-11-03 15:02:33 -070054#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070055#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080056#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070057
58namespace redfish
59{
60
Patrick Williams89492a12023-05-10 07:51:34 -050061constexpr const char* crashdumpObject = "com.intel.crashdump";
62constexpr const char* crashdumpPath = "/com/intel/crashdump";
63constexpr const char* crashdumpInterface = "com.intel.crashdump";
64constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070065 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050066constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070067 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050068constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070069 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070070
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060071enum class DumpCreationProgress
72{
73 DUMP_CREATE_SUCCESS,
74 DUMP_CREATE_FAILED,
75 DUMP_CREATE_INPROGRESS
76};
77
James Feistf6150402019-01-08 10:36:20 -080078namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070079
Gunnar Mills1214b7e2020-06-04 10:11:30 -050080inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070081{
Ed Tanousd4d25792020-09-29 15:15:03 -070082 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
83 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
84 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
85 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070086 {
87 return "Critical";
88 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070089 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
90 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
91 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070092 {
93 return "OK";
94 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070095 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070096 {
97 return "Warning";
98 }
99 return "";
100}
101
Abhishek Patel9017faf2021-09-14 22:48:55 -0500102inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
103{
104 std::optional<bool> notifyAction;
105 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
106 {
107 notifyAction = true;
108 }
109 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
110 {
111 notifyAction = false;
112 }
113
114 return notifyAction;
115}
116
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700117inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800118 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700119 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700120{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500121 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700122 size_t length = 0;
123 int ret = 0;
124 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800125 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
126 const void** dataVoid = reinterpret_cast<const void**>(&data);
127
128 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700129 if (ret < 0)
130 {
131 return ret;
132 }
Ed Tanous39e77502019-03-04 17:35:53 -0800133 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700134 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000135 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700136 return ret;
137}
138
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700139inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800140 std::string_view field, const int& base,
141 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700142{
143 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800144 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700145 // Get the metadata from the requested field of the journal entry
146 ret = getJournalMetadata(journal, field, metadata);
147 if (ret < 0)
148 {
149 return ret;
150 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000151 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700152 return ret;
153}
154
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700155inline static bool getEntryTimestamp(sd_journal* journal,
156 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800157{
158 int ret = 0;
159 uint64_t timestamp = 0;
160 ret = sd_journal_get_realtime_usec(journal, &timestamp);
161 if (ret < 0)
162 {
Ed Tanous62598e32023-07-17 17:06:25 -0700163 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
ZhikuiRena3316fc2020-01-29 14:58:08 -0800164 return false;
165 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300166 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500167 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800168}
Ed Tanous50b8a432022-02-03 16:29:50 -0800169
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
171 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700172{
173 int ret = 0;
174 static uint64_t prevTs = 0;
175 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700176 if (firstEntry)
177 {
178 prevTs = 0;
179 }
180
Jason M. Bills16428a12018-11-02 12:42:29 -0700181 // Get the entry timestamp
182 uint64_t curTs = 0;
183 ret = sd_journal_get_realtime_usec(journal, &curTs);
184 if (ret < 0)
185 {
Ed Tanous62598e32023-07-17 17:06:25 -0700186 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
Jason M. Bills16428a12018-11-02 12:42:29 -0700187 return false;
188 }
189 // If the timestamp isn't unique, increment the index
190 if (curTs == prevTs)
191 {
192 index++;
193 }
194 else
195 {
196 // Otherwise, reset it
197 index = 0;
198 }
199 // Save the timestamp
200 prevTs = curTs;
201
202 entryID = std::to_string(curTs);
203 if (index > 0)
204 {
205 entryID += "_" + std::to_string(index);
206 }
207 return true;
208}
209
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500210static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700211 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700212{
Ed Tanous271584a2019-07-09 16:24:22 -0700213 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700214 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700215 if (firstEntry)
216 {
217 prevTs = 0;
218 }
219
Jason M. Bills95820182019-04-22 16:25:34 -0700220 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700221 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700222 std::tm timeStruct = {};
223 std::istringstream entryStream(logEntry);
224 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
225 {
226 curTs = std::mktime(&timeStruct);
227 }
228 // If the timestamp isn't unique, increment the index
229 if (curTs == prevTs)
230 {
231 index++;
232 }
233 else
234 {
235 // Otherwise, reset it
236 index = 0;
237 }
238 // Save the timestamp
239 prevTs = curTs;
240
241 entryID = std::to_string(curTs);
242 if (index > 0)
243 {
244 entryID += "_" + std::to_string(index);
245 }
246 return true;
247}
248
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700249inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
251 const std::string& entryID, uint64_t& timestamp,
252 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700253{
254 if (entryID.empty())
255 {
256 return false;
257 }
258 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800259 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700260
Ed Tanous81ce6092020-12-17 16:54:55 +0000261 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800262 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700263 {
264 // Timestamp has an index
265 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800266 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700267 indexStr.remove_prefix(underscorePos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500268 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
269 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700270 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700271 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800272 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700273 return false;
274 }
275 }
276 // Timestamp has no index
Patrick Williams84396af2023-05-11 11:47:45 -0500277 auto [ptr, ec] = std::from_chars(tsStr.begin(), tsStr.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700278 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
Ed Tanous3544d2a2023-08-06 18:12:20 -0700306 std::ranges::sort(redfishLogFiles);
Jason M. Bills95820182019-04-22 16:25:34 -0700307
308 return !redfishLogFiles.empty();
309}
310
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600311inline log_entry::OriginatorTypes
312 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
313{
314 if (originatorType ==
315 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
316 {
317 return log_entry::OriginatorTypes::Client;
318 }
319 if (originatorType ==
320 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
321 {
322 return log_entry::OriginatorTypes::Internal;
323 }
324 if (originatorType ==
325 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
326 {
327 return log_entry::OriginatorTypes::SupportingService;
328 }
329 return log_entry::OriginatorTypes::Invalid;
330}
331
Claire Weinanaefe3782022-07-15 19:17:19 -0700332inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800333 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700334 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600335 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700336 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
337{
338 for (const auto& interfaceMap : object.second)
339 {
340 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
341 {
342 for (const auto& propertyMap : interfaceMap.second)
343 {
344 if (propertyMap.first == "Status")
345 {
346 const auto* status =
347 std::get_if<std::string>(&propertyMap.second);
348 if (status == nullptr)
349 {
350 messages::internalError(asyncResp->res);
351 break;
352 }
353 dumpStatus = *status;
354 }
355 }
356 }
357 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
358 {
359 for (const auto& propertyMap : interfaceMap.second)
360 {
361 if (propertyMap.first == "Size")
362 {
363 const auto* sizePtr =
364 std::get_if<uint64_t>(&propertyMap.second);
365 if (sizePtr == nullptr)
366 {
367 messages::internalError(asyncResp->res);
368 break;
369 }
370 size = *sizePtr;
371 break;
372 }
373 }
374 }
375 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
376 {
377 for (const auto& propertyMap : interfaceMap.second)
378 {
379 if (propertyMap.first == "Elapsed")
380 {
381 const uint64_t* usecsTimeStamp =
382 std::get_if<uint64_t>(&propertyMap.second);
383 if (usecsTimeStamp == nullptr)
384 {
385 messages::internalError(asyncResp->res);
386 break;
387 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700388 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700389 break;
390 }
391 }
392 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600393 else if (interfaceMap.first ==
394 "xyz.openbmc_project.Common.OriginatedBy")
395 {
396 for (const auto& propertyMap : interfaceMap.second)
397 {
398 if (propertyMap.first == "OriginatorId")
399 {
400 const std::string* id =
401 std::get_if<std::string>(&propertyMap.second);
402 if (id == nullptr)
403 {
404 messages::internalError(asyncResp->res);
405 break;
406 }
407 originatorId = *id;
408 }
409
410 if (propertyMap.first == "OriginatorType")
411 {
412 const std::string* type =
413 std::get_if<std::string>(&propertyMap.second);
414 if (type == nullptr)
415 {
416 messages::internalError(asyncResp->res);
417 break;
418 }
419
420 originatorType = mapDbusOriginatorTypeToRedfish(*type);
421 if (originatorType == log_entry::OriginatorTypes::Invalid)
422 {
423 messages::internalError(asyncResp->res);
424 break;
425 }
426 }
427 }
428 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700429 }
430}
431
Nan Zhou21ab4042022-06-26 23:07:40 +0000432static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800433{
434 std::string entriesPath;
435
436 if (dumpType == "BMC")
437 {
438 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
439 }
440 else if (dumpType == "FaultLog")
441 {
442 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
443 }
444 else if (dumpType == "System")
445 {
446 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
447 }
448 else
449 {
Ed Tanous62598e32023-07-17 17:06:25 -0700450 BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
451 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -0800452 }
453
454 // Returns empty string on error
455 return entriesPath;
456}
457
zhanghch058d1b46d2021-04-01 11:18:24 +0800458inline void
459 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
460 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500461{
Claire Weinanfdd26902022-03-01 14:18:25 -0800462 std::string entriesPath = getDumpEntriesPath(dumpType);
463 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500464 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500465 messages::internalError(asyncResp->res);
466 return;
467 }
468
George Liu5eb468d2023-06-20 17:03:24 +0800469 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
470 dbus::utility::getManagedObjects(
471 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800472 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800473 dumpType](const boost::system::error_code& ec,
George Liu5eb468d2023-06-20 17:03:24 +0800474 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700475 if (ec)
476 {
Ed Tanous62598e32023-07-17 17:06:25 -0700477 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700478 messages::internalError(asyncResp->res);
479 return;
480 }
481
Claire Weinanfdd26902022-03-01 14:18:25 -0800482 // Remove ending slash
483 std::string odataIdStr = entriesPath;
484 if (!odataIdStr.empty())
485 {
486 odataIdStr.pop_back();
487 }
488
489 asyncResp->res.jsonValue["@odata.type"] =
490 "#LogEntryCollection.LogEntryCollection";
491 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
492 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500493 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
494 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800495
Ed Tanous3544d2a2023-08-06 18:12:20 -0700496 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 std::string dumpEntryPath =
498 "/xyz/openbmc_project/dump/" +
499 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
500
George Liu5eb468d2023-06-20 17:03:24 +0800501 dbus::utility::ManagedObjectType resp(objects);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700502 std::ranges::sort(resp, [](const auto& l, const auto& r) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700503 return AlphanumLess<std::string>()(l.first.filename(),
504 r.first.filename());
505 });
506
507 for (auto& object : resp)
508 {
509 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500510 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700511 continue;
512 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700513 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700514 uint64_t size = 0;
515 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600516 std::string originatorId;
517 log_entry::OriginatorTypes originatorType =
518 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700519 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700520
521 std::string entryID = object.first.filename();
522 if (entryID.empty())
523 {
524 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500525 }
526
Claire Weinanc6fecda2022-07-15 10:43:25 -0700527 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600528 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700529 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700530
531 if (dumpStatus !=
532 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
533 !dumpStatus.empty())
534 {
535 // Dump status is not Complete, no need to enumerate
536 continue;
537 }
538
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600539 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800540 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700541 thisEntry["Id"] = entryID;
542 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700544 thisEntry["Created"] =
545 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700546
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600547 if (!originatorId.empty())
548 {
549 thisEntry["Originator"] = originatorId;
550 thisEntry["OriginatorType"] = originatorType;
551 }
552
Ed Tanous002d39b2022-05-31 08:59:27 -0700553 if (dumpType == "BMC")
554 {
555 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500556 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
557 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800558 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700559 }
560 else if (dumpType == "System")
561 {
562 thisEntry["DiagnosticDataType"] = "OEM";
563 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500564 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
565 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800566 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700567 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500568 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700569 }
570 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -0700571 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
George Liu5eb468d2023-06-20 17:03:24 +0800572 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500573}
574
zhanghch058d1b46d2021-04-01 11:18:24 +0800575inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700576 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500578{
Claire Weinanfdd26902022-03-01 14:18:25 -0800579 std::string entriesPath = getDumpEntriesPath(dumpType);
580 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500582 messages::internalError(asyncResp->res);
583 return;
584 }
585
George Liu5eb468d2023-06-20 17:03:24 +0800586 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
587 dbus::utility::getManagedObjects(
588 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800589 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800590 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700591 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 if (ec)
593 {
Ed Tanous62598e32023-07-17 17:06:25 -0700594 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 messages::internalError(asyncResp->res);
596 return;
597 }
598
599 bool foundDumpEntry = false;
600 std::string dumpEntryPath =
601 "/xyz/openbmc_project/dump/" +
602 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
603
604 for (const auto& objectPath : resp)
605 {
606 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500607 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700608 continue;
609 }
610
611 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700612 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 uint64_t size = 0;
614 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600615 std::string originatorId;
616 log_entry::OriginatorTypes originatorType =
617 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700618
Claire Weinanaefe3782022-07-15 19:17:19 -0700619 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600620 timestampUs, originatorId,
621 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700622
623 if (dumpStatus !=
624 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
625 !dumpStatus.empty())
626 {
627 // Dump status is not Complete
628 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200629 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
630 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500631 return;
632 }
633
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600635 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800636 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 asyncResp->res.jsonValue["Id"] = entryID;
638 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700639 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700640 asyncResp->res.jsonValue["Created"] =
641 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500642
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600643 if (!originatorId.empty())
644 {
645 asyncResp->res.jsonValue["Originator"] = originatorId;
646 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
647 }
648
Ed Tanous002d39b2022-05-31 08:59:27 -0700649 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500650 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700651 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
652 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800653 entriesPath + entryID + "/attachment";
654 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500655 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500657 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700658 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
659 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
660 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800661 entriesPath + entryID + "/attachment";
662 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500663 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700664 }
665 if (!foundDumpEntry)
666 {
Ed Tanous62598e32023-07-17 17:06:25 -0700667 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
Myung Baeb90d14f2023-05-31 14:40:39 -0500668 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
669 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700670 return;
671 }
George Liu5eb468d2023-06-20 17:03:24 +0800672 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500673}
674
zhanghch058d1b46d2021-04-01 11:18:24 +0800675inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800676 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500677 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500678{
Ed Tanous002d39b2022-05-31 08:59:27 -0700679 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800680 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -0700681 BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500682 if (ec)
683 {
George Liu3de8d8b2021-03-22 17:49:39 +0800684 if (ec.value() == EBADR)
685 {
686 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
687 return;
688 }
Ed Tanous62598e32023-07-17 17:06:25 -0700689 BMCWEB_LOG_ERROR(
690 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
691 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500692 messages::internalError(asyncResp->res);
693 return;
694 }
695 };
696 crow::connections::systemBus->async_method_call(
697 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500698 "/xyz/openbmc_project/dump/" +
699 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
700 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500701 "xyz.openbmc_project.Object.Delete", "Delete");
702}
703
Carson Labrado168d1b12023-03-27 17:04:46 +0000704inline void
705 downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
706 const std::string& entryID,
707 const std::string& downloadEntryType,
708 const boost::system::error_code& ec,
709 const sdbusplus::message::unix_fd& unixfd)
710{
711 if (ec.value() == EBADR)
712 {
713 messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
714 return;
715 }
716 if (ec)
717 {
718 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
719 messages::internalError(asyncResp->res);
720 return;
721 }
722
723 // Make sure we know how to process the retrieved entry attachment
724 if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
725 {
726 BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
727 downloadEntryType);
728 messages::internalError(asyncResp->res);
729 }
730
731 int fd = -1;
732 fd = dup(unixfd);
733 if (fd < 0)
734 {
735 BMCWEB_LOG_ERROR("Failed to open file");
736 messages::internalError(asyncResp->res);
737 return;
738 }
739
740 long long int size = lseek(fd, 0, SEEK_END);
741 if (size <= 0)
742 {
743 BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
744 size);
745 messages::internalError(asyncResp->res);
746 close(fd);
747 return;
748 }
749
750 // Arbitrary max size of 20MB to accommodate BMC dumps
751 constexpr int maxFileSize = 20 * 1024 * 1024;
752 if (size > maxFileSize)
753 {
754 BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
755 size, maxFileSize);
756 messages::internalError(asyncResp->res);
757 close(fd);
758 return;
759 }
760 long long int rc = lseek(fd, 0, SEEK_SET);
761 if (rc < 0)
762 {
763 BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
764 messages::internalError(asyncResp->res);
765 close(fd);
766 return;
767 }
768
769 asyncResp->res.body().resize(static_cast<size_t>(size), '\0');
770 rc = read(fd, asyncResp->res.body().data(), asyncResp->res.body().size());
771 if ((rc == -1) || (rc != size))
772 {
773 BMCWEB_LOG_ERROR("Failed to read in file");
774 messages::internalError(asyncResp->res);
775 close(fd);
776 return;
777 }
778 close(fd);
779
780 if (downloadEntryType == "System")
781 {
782 // We need to encode the data. Doing it this way saves the other paths
783 // from having to make a copy into a temporary variable.
784 asyncResp->res.body() =
785 crow::utility::base64encode(asyncResp->res.body());
786 asyncResp->res.addHeader(
787 boost::beast::http::field::content_transfer_encoding, "Base64");
788 }
789
790 asyncResp->res.addHeader(boost::beast::http::field::content_type,
791 "application/octet-stream");
792}
793
794inline void
795 downloadDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
796 const std::string& entryID, const std::string& dumpType)
797{
798 if (dumpType != "BMC")
799 {
800 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
801 messages::resourceNotFound(asyncResp->res, dumpType + " dump", entryID);
802 return;
803 }
804
805 std::string dumpEntryPath =
806 sdbusplus::message::object_path("/xyz/openbmc_project/dump/") /
807 std::string(boost::algorithm::to_lower_copy(dumpType)) / "entry" /
808 entryID;
809
810 auto downloadDumpEntryHandler =
811 [asyncResp, entryID,
812 dumpType](const boost::system::error_code& ec,
813 const sdbusplus::message::unix_fd& unixfd) {
814 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
815 };
816
817 crow::connections::systemBus->async_method_call(
818 std::move(downloadDumpEntryHandler), "xyz.openbmc_project.Dump.Manager",
819 dumpEntryPath, "xyz.openbmc_project.Dump.Entry", "GetFileHandle");
820}
821
822inline void
823 downloadEventLogEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
824 const std::string& systemName,
825 const std::string& entryID,
826 const std::string& dumpType)
827{
828 if constexpr (bmcwebEnableMultiHost)
829 {
830 // Option currently returns no systems. TBD
831 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
832 systemName);
833 return;
834 }
835 if (systemName != "system")
836 {
837 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
838 systemName);
839 return;
840 }
841
842 std::string entryPath =
843 sdbusplus::message::object_path("/xyz/openbmc_project/logging/entry") /
844 entryID;
845
846 auto downloadEventLogEntryHandler =
847 [asyncResp, entryID,
848 dumpType](const boost::system::error_code& ec,
849 const sdbusplus::message::unix_fd& unixfd) {
850 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
851 };
852
853 crow::connections::systemBus->async_method_call(
854 std::move(downloadEventLogEntryHandler), "xyz.openbmc_project.Logging",
855 entryPath, "xyz.openbmc_project.Logging.Entry", "GetEntry");
856}
857
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600858inline DumpCreationProgress
859 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500860{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600861 if (status ==
862 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
863 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
864 {
865 return DumpCreationProgress::DUMP_CREATE_FAILED;
866 }
867 if (status ==
868 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
869 {
870 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
871 }
872 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
873}
874
875inline DumpCreationProgress
876 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
877{
878 for (const auto& [key, val] : values)
879 {
880 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700881 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600882 const std::string* value = std::get_if<std::string>(&val);
883 if (value == nullptr)
884 {
Ed Tanous62598e32023-07-17 17:06:25 -0700885 BMCWEB_LOG_ERROR("Status property value is null");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600886 return DumpCreationProgress::DUMP_CREATE_FAILED;
887 }
888 return mapDbusStatusToDumpProgress(*value);
889 }
890 }
891 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
892}
893
894inline std::string getDumpEntryPath(const std::string& dumpPath)
895{
896 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
897 {
898 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
899 }
900 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
901 {
902 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
903 }
904 return "";
905}
906
907inline void createDumpTaskCallback(
908 task::Payload&& payload,
909 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
910 const sdbusplus::message::object_path& createdObjPath)
911{
912 const std::string dumpPath = createdObjPath.parent_path().str;
913 const std::string dumpId = createdObjPath.filename();
914
915 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
916
917 if (dumpEntryPath.empty())
918 {
Ed Tanous62598e32023-07-17 17:06:25 -0700919 BMCWEB_LOG_ERROR("Invalid dump type received");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600920 messages::internalError(asyncResp->res);
921 return;
922 }
923
924 crow::connections::systemBus->async_method_call(
925 [asyncResp, payload, createdObjPath,
926 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800927 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600928 const std::string& introspectXml) {
929 if (ec)
930 {
Ed Tanous62598e32023-07-17 17:06:25 -0700931 BMCWEB_LOG_ERROR("Introspect call failed with error: {}",
932 ec.message());
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600933 messages::internalError(asyncResp->res);
934 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700935 }
936
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600937 // Check if the created dump object has implemented Progress
938 // interface to track dump completion. If yes, fetch the "Status"
939 // property of the interface, modify the task state accordingly.
940 // Else, return task completed.
941 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700942
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600943 doc.Parse(introspectXml.data(), introspectXml.size());
944 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
945 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700946 {
Ed Tanous62598e32023-07-17 17:06:25 -0700947 BMCWEB_LOG_ERROR("XML document failed to parse");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600948 messages::internalError(asyncResp->res);
949 return;
950 }
951 tinyxml2::XMLElement* interfaceNode =
952 pRoot->FirstChildElement("interface");
953
954 bool isProgressIntfPresent = false;
955 while (interfaceNode != nullptr)
956 {
957 const char* thisInterfaceName = interfaceNode->Attribute("name");
958 if (thisInterfaceName != nullptr)
959 {
960 if (thisInterfaceName ==
961 std::string_view("xyz.openbmc_project.Common.Progress"))
962 {
963 interfaceNode =
964 interfaceNode->NextSiblingElement("interface");
965 continue;
966 }
967 isProgressIntfPresent = true;
968 break;
969 }
970 interfaceNode = interfaceNode->NextSiblingElement("interface");
971 }
972
973 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
974 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous8b242752023-06-27 17:17:13 -0700975 const boost::system::error_code& ec2, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600976 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -0700977 if (ec2)
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600978 {
Ed Tanous62598e32023-07-17 17:06:25 -0700979 BMCWEB_LOG_ERROR("{}: Error in creating dump",
980 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600981 taskData->messages.emplace_back(messages::internalError());
982 taskData->state = "Cancelled";
983 return task::completed;
984 }
985
986 if (isProgressIntfPresent)
987 {
988 dbus::utility::DBusPropertiesMap values;
989 std::string prop;
990 msg.read(prop, values);
991
992 DumpCreationProgress dumpStatus =
993 getDumpCompletionStatus(values);
994 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
995 {
Ed Tanous62598e32023-07-17 17:06:25 -0700996 BMCWEB_LOG_ERROR("{}: Error in creating dump",
997 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600998 taskData->state = "Cancelled";
999 return task::completed;
1000 }
1001
1002 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
1003 {
Ed Tanous62598e32023-07-17 17:06:25 -07001004 BMCWEB_LOG_DEBUG("{}: Dump creation task is in progress",
1005 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001006 return !task::completed;
1007 }
1008 }
1009
Ed Tanous002d39b2022-05-31 08:59:27 -07001010 nlohmann::json retMessage = messages::success();
1011 taskData->messages.emplace_back(retMessage);
1012
Ed Tanousc51a58e2023-03-27 14:43:19 -07001013 boost::urls::url url = boost::urls::format(
1014 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
1015
1016 std::string headerLoc = "Location: ";
1017 headerLoc += url.buffer();
1018
Ed Tanous002d39b2022-05-31 08:59:27 -07001019 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
1020
Ed Tanous62598e32023-07-17 17:06:25 -07001021 BMCWEB_LOG_DEBUG("{}: Dump creation task completed",
1022 createdObjPath.str);
Ed Tanous002d39b2022-05-31 08:59:27 -07001023 taskData->state = "Completed";
1024 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001025 },
1026 "type='signal',interface='org.freedesktop.DBus.Properties',"
1027 "member='PropertiesChanged',path='" +
1028 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001029
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001030 // The task timer is set to max time limit within which the
1031 // requested dump will be collected.
1032 task->startTimer(std::chrono::minutes(6));
1033 task->populateResp(asyncResp->res);
1034 task->payload.emplace(payload);
1035 },
1036 "xyz.openbmc_project.Dump.Manager", createdObjPath,
1037 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001038}
1039
zhanghch058d1b46d2021-04-01 11:18:24 +08001040inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1041 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001042{
Claire Weinanfdd26902022-03-01 14:18:25 -08001043 std::string dumpPath = getDumpEntriesPath(dumpType);
1044 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001045 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001046 messages::internalError(asyncResp->res);
1047 return;
1048 }
1049
1050 std::optional<std::string> diagnosticDataType;
1051 std::optional<std::string> oemDiagnosticDataType;
1052
Willy Tu15ed6782021-12-14 11:03:16 -08001053 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001054 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
1055 "OEMDiagnosticDataType", oemDiagnosticDataType))
1056 {
1057 return;
1058 }
1059
1060 if (dumpType == "System")
1061 {
1062 if (!oemDiagnosticDataType || !diagnosticDataType)
1063 {
Ed Tanous62598e32023-07-17 17:06:25 -07001064 BMCWEB_LOG_ERROR(
1065 "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001066 messages::actionParameterMissing(
1067 asyncResp->res, "CollectDiagnosticData",
1068 "DiagnosticDataType & OEMDiagnosticDataType");
1069 return;
1070 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001071 if ((*oemDiagnosticDataType != "System") ||
1072 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001073 {
Ed Tanous62598e32023-07-17 17:06:25 -07001074 BMCWEB_LOG_ERROR("Wrong parameter values passed");
Ed Tanousace85d62021-10-26 12:45:59 -07001075 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001076 return;
1077 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001078 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001079 }
1080 else if (dumpType == "BMC")
1081 {
1082 if (!diagnosticDataType)
1083 {
Ed Tanous62598e32023-07-17 17:06:25 -07001084 BMCWEB_LOG_ERROR(
1085 "CreateDump action parameter 'DiagnosticDataType' not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001086 messages::actionParameterMissing(
1087 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
1088 return;
1089 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001090 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001091 {
Ed Tanous62598e32023-07-17 17:06:25 -07001092 BMCWEB_LOG_ERROR(
1093 "Wrong parameter value passed for 'DiagnosticDataType'");
Ed Tanousace85d62021-10-26 12:45:59 -07001094 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001095 return;
1096 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001097 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
1098 }
1099 else
1100 {
Ed Tanous62598e32023-07-17 17:06:25 -07001101 BMCWEB_LOG_ERROR("CreateDump failed. Unknown dump type");
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001102 messages::internalError(asyncResp->res);
1103 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001104 }
1105
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001106 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
1107 createDumpParamVec;
1108
Carson Labradof574a8e2023-03-22 02:26:00 +00001109 if (req.session != nullptr)
1110 {
1111 createDumpParamVec.emplace_back(
1112 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
1113 req.session->clientIp);
1114 createDumpParamVec.emplace_back(
1115 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
1116 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
1117 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -06001118
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001119 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001120 [asyncResp, payload(task::Payload(req)),
1121 dumpPath](const boost::system::error_code& ec,
1122 const sdbusplus::message_t& msg,
1123 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07001124 if (ec)
1125 {
Ed Tanous62598e32023-07-17 17:06:25 -07001126 BMCWEB_LOG_ERROR("CreateDump resp_handler got error {}", ec);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001127 const sd_bus_error* dbusError = msg.get_error();
1128 if (dbusError == nullptr)
1129 {
1130 messages::internalError(asyncResp->res);
1131 return;
1132 }
1133
Ed Tanous62598e32023-07-17 17:06:25 -07001134 BMCWEB_LOG_ERROR("CreateDump DBus error: {} and error msg: {}",
1135 dbusError->name, dbusError->message);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001136 if (std::string_view(
1137 "xyz.openbmc_project.Common.Error.NotAllowed") ==
1138 dbusError->name)
1139 {
1140 messages::resourceInStandby(asyncResp->res);
1141 return;
1142 }
1143 if (std::string_view(
1144 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
1145 dbusError->name)
1146 {
1147 messages::serviceDisabled(asyncResp->res, dumpPath);
1148 return;
1149 }
1150 if (std::string_view(
1151 "xyz.openbmc_project.Common.Error.Unavailable") ==
1152 dbusError->name)
1153 {
1154 messages::resourceInUse(asyncResp->res);
1155 return;
1156 }
1157 // Other Dbus errors such as:
1158 // xyz.openbmc_project.Common.Error.InvalidArgument &
1159 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1160 // the dbus call that is made here in the bmcweb
1161 // implementation and has nothing to do with the client's
1162 // input in the request. Hence, returning internal error
1163 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001164 messages::internalError(asyncResp->res);
1165 return;
1166 }
Ed Tanous62598e32023-07-17 17:06:25 -07001167 BMCWEB_LOG_DEBUG("Dump Created. Path: {}", objPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001168 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001169 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001170 "xyz.openbmc_project.Dump.Manager",
1171 "/xyz/openbmc_project/dump/" +
1172 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001173 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001174}
1175
zhanghch058d1b46d2021-04-01 11:18:24 +08001176inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1177 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001178{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001179 std::string dumpTypeLowerCopy =
1180 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001181
Claire Weinan0d946212022-07-13 19:40:19 -07001182 crow::connections::systemBus->async_method_call(
1183 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001184 if (ec)
1185 {
Ed Tanous62598e32023-07-17 17:06:25 -07001186 BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001187 messages::internalError(asyncResp->res);
1188 return;
1189 }
Claire Weinan0d946212022-07-13 19:40:19 -07001190 },
1191 "xyz.openbmc_project.Dump.Manager",
1192 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1193 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001194}
1195
Ed Tanousb9d36b42022-02-26 21:42:46 -08001196inline static void
1197 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1198 std::string& filename, std::string& timestamp,
1199 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001200{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001201 const std::string* filenamePtr = nullptr;
1202 const std::string* timestampPtr = nullptr;
1203 const std::string* logfilePtr = nullptr;
1204
1205 const bool success = sdbusplus::unpackPropertiesNoThrow(
1206 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1207 "Filename", filenamePtr, "Log", logfilePtr);
1208
1209 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001210 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001211 return;
1212 }
1213
1214 if (filenamePtr != nullptr)
1215 {
1216 filename = *filenamePtr;
1217 }
1218
1219 if (timestampPtr != nullptr)
1220 {
1221 timestamp = *timestampPtr;
1222 }
1223
1224 if (logfilePtr != nullptr)
1225 {
1226 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001227 }
1228}
1229
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001230inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001231{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001232 /**
1233 * Functions triggers appropriate requests on DBus
1234 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001235 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001236 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001237 .methods(boost::beast::http::verb::get)(
1238 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001239 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1240 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001241 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001242 {
1243 return;
1244 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001245 if constexpr (bmcwebEnableMultiHost)
1246 {
1247 // Option currently returns no systems. TBD
1248 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1249 systemName);
1250 return;
1251 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001252 if (systemName != "system")
1253 {
1254 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1255 systemName);
1256 return;
1257 }
1258
Ed Tanous002d39b2022-05-31 08:59:27 -07001259 // Collections don't include the static data added by SubRoute
1260 // because it has a duplicate entry for members
1261 asyncResp->res.jsonValue["@odata.type"] =
1262 "#LogServiceCollection.LogServiceCollection";
1263 asyncResp->res.jsonValue["@odata.id"] =
1264 "/redfish/v1/Systems/system/LogServices";
1265 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1266 asyncResp->res.jsonValue["Description"] =
1267 "Collection of LogServices for this Computer System";
1268 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1269 logServiceArray = nlohmann::json::array();
1270 nlohmann::json::object_t eventLog;
1271 eventLog["@odata.id"] =
1272 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001273 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001274#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001275 nlohmann::json::object_t dumpLog;
1276 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001277 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001278#endif
1279
Jason M. Billsd53dd412019-02-12 17:16:22 -08001280#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001281 nlohmann::json::object_t crashdump;
1282 crashdump["@odata.id"] =
1283 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001284 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001285#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001286
1287#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001288 nlohmann::json::object_t hostlogger;
1289 hostlogger["@odata.id"] =
1290 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001291 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001292#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001293 asyncResp->res.jsonValue["Members@odata.count"] =
1294 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001295
George Liu7a1dbc42022-12-07 16:03:22 +08001296 constexpr std::array<std::string_view, 1> interfaces = {
1297 "xyz.openbmc_project.State.Boot.PostCode"};
1298 dbus::utility::getSubTreePaths(
1299 "/", 0, interfaces,
1300 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001301 const dbus::utility::MapperGetSubTreePathsResponse&
1302 subtreePath) {
1303 if (ec)
1304 {
Ed Tanous62598e32023-07-17 17:06:25 -07001305 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001306 return;
1307 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001308
Ed Tanous002d39b2022-05-31 08:59:27 -07001309 for (const auto& pathStr : subtreePath)
1310 {
1311 if (pathStr.find("PostCode") != std::string::npos)
1312 {
1313 nlohmann::json& logServiceArrayLocal =
1314 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001315 nlohmann::json::object_t member;
1316 member["@odata.id"] =
1317 "/redfish/v1/Systems/system/LogServices/PostCodes";
1318
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001319 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001320
Ed Tanous002d39b2022-05-31 08:59:27 -07001321 asyncResp->res.jsonValue["Members@odata.count"] =
1322 logServiceArrayLocal.size();
1323 return;
1324 }
1325 }
George Liu7a1dbc42022-12-07 16:03:22 +08001326 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001327 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001328}
1329
1330inline void requestRoutesEventLogService(App& app)
1331{
Ed Tanous22d268c2022-05-19 09:39:07 -07001332 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001333 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001334 .methods(boost::beast::http::verb::get)(
1335 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001336 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1337 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001338 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001339 {
1340 return;
1341 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001342 if (systemName != "system")
1343 {
1344 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1345 systemName);
1346 return;
1347 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001348 asyncResp->res.jsonValue["@odata.id"] =
1349 "/redfish/v1/Systems/system/LogServices/EventLog";
1350 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05001351 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07001352 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1353 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1354 asyncResp->res.jsonValue["Id"] = "EventLog";
1355 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301356
Ed Tanous002d39b2022-05-31 08:59:27 -07001357 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001358 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301359
Ed Tanous002d39b2022-05-31 08:59:27 -07001360 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1361 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1362 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301363
Ed Tanous002d39b2022-05-31 08:59:27 -07001364 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1365 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1366 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001367
Ed Tanous002d39b2022-05-31 08:59:27 -07001368 {"target",
1369 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001370 });
1371}
1372
1373inline void requestRoutesJournalEventLogClear(App& app)
1374{
Jason M. Bills4978b632022-02-22 14:17:43 -08001375 BMCWEB_ROUTE(
1376 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001377 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001378 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001379 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001380 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001381 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1382 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001383 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001384 {
1385 return;
1386 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001387 if (systemName != "system")
1388 {
1389 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1390 systemName);
1391 return;
1392 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001393 // Clear the EventLog by deleting the log files
1394 std::vector<std::filesystem::path> redfishLogFiles;
1395 if (getRedfishLogFiles(redfishLogFiles))
1396 {
1397 for (const std::filesystem::path& file : redfishLogFiles)
1398 {
1399 std::error_code ec;
1400 std::filesystem::remove(file, ec);
1401 }
1402 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001403
Ed Tanous002d39b2022-05-31 08:59:27 -07001404 // Reload rsyslog so it knows to start new log files
1405 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001406 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001407 if (ec)
1408 {
Ed Tanous62598e32023-07-17 17:06:25 -07001409 BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001410 messages::internalError(asyncResp->res);
1411 return;
1412 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001413
Ed Tanous002d39b2022-05-31 08:59:27 -07001414 messages::success(asyncResp->res);
1415 },
1416 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1417 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1418 "replace");
1419 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001420}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001421
Jason M. Billsac992cd2022-06-24 13:31:46 -07001422enum class LogParseError
1423{
1424 success,
1425 parseFailed,
1426 messageIdNotInRegistry,
1427};
1428
1429static LogParseError
1430 fillEventLogEntryJson(const std::string& logEntryID,
1431 const std::string& logEntry,
1432 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001433{
Jason M. Bills95820182019-04-22 16:25:34 -07001434 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001435 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001436 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001437 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001438 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001439 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001440 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001441 std::string timestamp = logEntry.substr(0, space);
1442 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001443 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001444 if (entryStart == std::string::npos)
1445 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001446 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001447 }
1448 std::string_view entry(logEntry);
1449 entry.remove_prefix(entryStart);
1450 // Use split to separate the entry into its fields
1451 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001452 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001453 // We need at least a MessageId to be valid
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001454 auto logEntryIter = logEntryFields.begin();
1455 if (logEntryIter == logEntryFields.end())
Jason M. Billscd225da2019-05-08 15:31:57 -07001456 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001457 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001458 }
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001459 std::string& messageID = *logEntryIter;
Jason M. Bills4851d452019-03-28 11:27:48 -07001460 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001461 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001462
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001463 logEntryIter++;
Sui Chen54417b02022-03-24 14:59:52 -07001464 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001465 {
Ed Tanous62598e32023-07-17 17:06:25 -07001466 BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001467 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001468 }
1469
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001470 std::vector<std::string_view> messageArgs(logEntryIter,
1471 logEntryFields.end());
Ed Tanousc05bba42023-06-28 08:33:29 -07001472 messageArgs.resize(message->numberOfArgs);
1473
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001474 std::string msg = redfish::registries::fillMessageArgs(messageArgs,
1475 message->message);
1476 if (msg.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001477 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001478 return LogParseError::parseFailed;
Jason M. Bills4851d452019-03-28 11:27:48 -07001479 }
1480
Jason M. Bills95820182019-04-22 16:25:34 -07001481 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1482 // format which matches the Redfish format except for the fractional seconds
1483 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001484 std::size_t dot = timestamp.find_first_of('.');
1485 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001486 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001487 {
Jason M. Bills95820182019-04-22 16:25:34 -07001488 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001489 }
1490
1491 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001492 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001493 logEntryJson["@odata.id"] = boost::urls::format(
1494 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1495 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001496 logEntryJson["Name"] = "System Event Log Entry";
1497 logEntryJson["Id"] = logEntryID;
1498 logEntryJson["Message"] = std::move(msg);
1499 logEntryJson["MessageId"] = std::move(messageID);
1500 logEntryJson["MessageArgs"] = messageArgs;
1501 logEntryJson["EntryType"] = "Event";
1502 logEntryJson["Severity"] = message->messageSeverity;
1503 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001504 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001505}
1506
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001507inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001508{
Ed Tanous22d268c2022-05-19 09:39:07 -07001509 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001510 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001511 .methods(boost::beast::http::verb::get)(
1512 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001513 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1514 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001515 query_param::QueryCapabilities capabilities = {
1516 .canDelegateTop = true,
1517 .canDelegateSkip = true,
1518 };
1519 query_param::Query delegatedQuery;
1520 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001521 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001522 {
1523 return;
1524 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001525 if constexpr (bmcwebEnableMultiHost)
1526 {
1527 // Option currently returns no systems. TBD
1528 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1529 systemName);
1530 return;
1531 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001532 if (systemName != "system")
1533 {
1534 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1535 systemName);
1536 return;
1537 }
1538
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001539 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001540 size_t skip = delegatedQuery.skip.value_or(0);
1541
Ed Tanous002d39b2022-05-31 08:59:27 -07001542 // Collections don't include the static data added by SubRoute
1543 // because it has a duplicate entry for members
1544 asyncResp->res.jsonValue["@odata.type"] =
1545 "#LogEntryCollection.LogEntryCollection";
1546 asyncResp->res.jsonValue["@odata.id"] =
1547 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1548 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1549 asyncResp->res.jsonValue["Description"] =
1550 "Collection of System Event Log Entries";
1551
1552 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1553 logEntryArray = nlohmann::json::array();
1554 // Go through the log files and create a unique ID for each
1555 // entry
1556 std::vector<std::filesystem::path> redfishLogFiles;
1557 getRedfishLogFiles(redfishLogFiles);
1558 uint64_t entryCount = 0;
1559 std::string logEntry;
1560
1561 // Oldest logs are in the last file, so start there and loop
1562 // backwards
1563 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1564 it++)
1565 {
1566 std::ifstream logStream(*it);
1567 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001568 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001569 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001570 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001571
Ed Tanous002d39b2022-05-31 08:59:27 -07001572 // Reset the unique ID on the first entry
1573 bool firstEntry = true;
1574 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001575 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001576 std::string idStr;
1577 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001578 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001579 continue;
1580 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001581 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001582
Jason M. Billsde703c52022-06-23 14:19:04 -07001583 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001584 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1585 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001586 if (status == LogParseError::messageIdNotInRegistry)
1587 {
1588 continue;
1589 }
1590 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001591 {
1592 messages::internalError(asyncResp->res);
1593 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001594 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001595
Jason M. Billsde703c52022-06-23 14:19:04 -07001596 entryCount++;
1597 // Handle paging using skip (number of entries to skip from the
1598 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001599 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001600 {
1601 continue;
1602 }
1603
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001604 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001605 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001606 }
1607 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001608 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001609 {
1610 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1611 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001612 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001613 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001614 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001615}
Chicago Duan336e96c2019-07-15 14:22:08 +08001616
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001617inline void requestRoutesJournalEventLogEntry(App& app)
1618{
1619 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001620 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001621 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001622 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001623 [&app](const crow::Request& req,
1624 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001625 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001626 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001627 {
1628 return;
1629 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001630 if constexpr (bmcwebEnableMultiHost)
1631 {
1632 // Option currently returns no systems. TBD
1633 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1634 systemName);
1635 return;
1636 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001637
1638 if (systemName != "system")
1639 {
1640 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1641 systemName);
1642 return;
1643 }
1644
Ed Tanous002d39b2022-05-31 08:59:27 -07001645 const std::string& targetID = param;
1646
1647 // Go through the log files and check the unique ID for each
1648 // entry to find the target entry
1649 std::vector<std::filesystem::path> redfishLogFiles;
1650 getRedfishLogFiles(redfishLogFiles);
1651 std::string logEntry;
1652
1653 // Oldest logs are in the last file, so start there and loop
1654 // backwards
1655 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1656 it++)
1657 {
1658 std::ifstream logStream(*it);
1659 if (!logStream.is_open())
1660 {
1661 continue;
1662 }
1663
1664 // Reset the unique ID on the first entry
1665 bool firstEntry = true;
1666 while (std::getline(logStream, logEntry))
1667 {
1668 std::string idStr;
1669 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001670 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001671 continue;
1672 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001673 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001674
1675 if (idStr == targetID)
1676 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001677 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001678 LogParseError status =
1679 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1680 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001681 {
1682 messages::internalError(asyncResp->res);
1683 return;
1684 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001685 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001686 return;
1687 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001688 }
1689 }
1690 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001691 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001692 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001693}
1694
1695inline void requestRoutesDBusEventLogEntryCollection(App& app)
1696{
Ed Tanous22d268c2022-05-19 09:39:07 -07001697 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001698 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001699 .methods(boost::beast::http::verb::get)(
1700 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001701 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1702 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001703 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001704 {
1705 return;
1706 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001707 if constexpr (bmcwebEnableMultiHost)
1708 {
1709 // Option currently returns no systems. TBD
1710 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1711 systemName);
1712 return;
1713 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001714 if (systemName != "system")
1715 {
1716 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1717 systemName);
1718 return;
1719 }
1720
Ed Tanous002d39b2022-05-31 08:59:27 -07001721 // Collections don't include the static data added by SubRoute
1722 // because it has a duplicate entry for members
1723 asyncResp->res.jsonValue["@odata.type"] =
1724 "#LogEntryCollection.LogEntryCollection";
1725 asyncResp->res.jsonValue["@odata.id"] =
1726 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1727 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1728 asyncResp->res.jsonValue["Description"] =
1729 "Collection of System Event Log Entries";
1730
1731 // DBus implementation of EventLog/Entries
1732 // Make call to Logging Service to find all log entry objects
George Liu5eb468d2023-06-20 17:03:24 +08001733 sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1734 dbus::utility::getManagedObjects(
1735 "xyz.openbmc_project.Logging", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001736 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001737 const dbus::utility::ManagedObjectType& resp) {
1738 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001739 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001740 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07001741 BMCWEB_LOG_ERROR(
1742 "getLogEntriesIfaceData resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001743 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001744 return;
1745 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001746 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -07001747 for (const auto& objectPath : resp)
1748 {
1749 const uint32_t* id = nullptr;
1750 const uint64_t* timestamp = nullptr;
1751 const uint64_t* updateTimestamp = nullptr;
1752 const std::string* severity = nullptr;
1753 const std::string* message = nullptr;
1754 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001755 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001756 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001757 const std::string* notify = nullptr;
1758
Ed Tanous002d39b2022-05-31 08:59:27 -07001759 for (const auto& interfaceMap : objectPath.second)
1760 {
1761 if (interfaceMap.first ==
1762 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001763 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001764 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001765 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001766 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001767 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001768 id = std::get_if<uint32_t>(&propertyMap.second);
1769 }
1770 else if (propertyMap.first == "Timestamp")
1771 {
1772 timestamp =
1773 std::get_if<uint64_t>(&propertyMap.second);
1774 }
1775 else if (propertyMap.first == "UpdateTimestamp")
1776 {
1777 updateTimestamp =
1778 std::get_if<uint64_t>(&propertyMap.second);
1779 }
1780 else if (propertyMap.first == "Severity")
1781 {
1782 severity = std::get_if<std::string>(
1783 &propertyMap.second);
1784 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001785 else if (propertyMap.first == "Resolution")
1786 {
1787 resolution = std::get_if<std::string>(
1788 &propertyMap.second);
1789 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001790 else if (propertyMap.first == "Message")
1791 {
1792 message = std::get_if<std::string>(
1793 &propertyMap.second);
1794 }
1795 else if (propertyMap.first == "Resolved")
1796 {
1797 const bool* resolveptr =
1798 std::get_if<bool>(&propertyMap.second);
1799 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001800 {
1801 messages::internalError(asyncResp->res);
1802 return;
1803 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001804 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001805 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001806 else if (propertyMap.first ==
1807 "ServiceProviderNotify")
1808 {
1809 notify = std::get_if<std::string>(
1810 &propertyMap.second);
1811 if (notify == nullptr)
1812 {
1813 messages::internalError(asyncResp->res);
1814 return;
1815 }
1816 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001817 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001818 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001819 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001820 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001821 messages::internalError(asyncResp->res);
1822 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001823 }
1824 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001825 else if (interfaceMap.first ==
1826 "xyz.openbmc_project.Common.FilePath")
1827 {
1828 for (const auto& propertyMap : interfaceMap.second)
1829 {
1830 if (propertyMap.first == "Path")
1831 {
1832 filePath = std::get_if<std::string>(
1833 &propertyMap.second);
1834 }
1835 }
1836 }
1837 }
1838 // Object path without the
1839 // xyz.openbmc_project.Logging.Entry interface, ignore
1840 // and continue.
1841 if (id == nullptr || message == nullptr ||
1842 severity == nullptr || timestamp == nullptr ||
1843 updateTimestamp == nullptr)
1844 {
1845 continue;
1846 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001847 nlohmann::json& thisEntry = entriesArray.emplace_back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001848 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001849 thisEntry["@odata.id"] = boost::urls::format(
1850 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1851 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001852 thisEntry["Name"] = "System Event Log Entry";
1853 thisEntry["Id"] = std::to_string(*id);
1854 thisEntry["Message"] = *message;
1855 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001856 if ((resolution != nullptr) && (!(*resolution).empty()))
1857 {
1858 thisEntry["Resolution"] = *resolution;
1859 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001860 std::optional<bool> notifyAction =
1861 getProviderNotifyAction(*notify);
1862 if (notifyAction)
1863 {
1864 thisEntry["ServiceProviderNotified"] = *notifyAction;
1865 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001866 thisEntry["EntryType"] = "Event";
1867 thisEntry["Severity"] =
1868 translateSeverityDbusToRedfish(*severity);
1869 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001870 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001871 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001872 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001873 if (filePath != nullptr)
1874 {
1875 thisEntry["AdditionalDataURI"] =
1876 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1877 std::to_string(*id) + "/attachment";
1878 }
1879 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001880 std::ranges::sort(entriesArray, [](const nlohmann::json& left,
1881 const nlohmann::json& right) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001882 return (left["Id"] <= right["Id"]);
Ed Tanous3544d2a2023-08-06 18:12:20 -07001883 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001884 asyncResp->res.jsonValue["Members@odata.count"] =
1885 entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -07001886 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
George Liu5eb468d2023-06-20 17:03:24 +08001887 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001888 });
1889}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001890
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001891inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001892{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001893 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001894 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001895 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001896 .methods(boost::beast::http::verb::get)(
1897 [&app](const crow::Request& req,
1898 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001899 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001900 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001901 {
1902 return;
1903 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001904 if constexpr (bmcwebEnableMultiHost)
1905 {
1906 // Option currently returns no systems. TBD
1907 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1908 systemName);
1909 return;
1910 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001911 if (systemName != "system")
1912 {
1913 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1914 systemName);
1915 return;
1916 }
1917
Ed Tanous002d39b2022-05-31 08:59:27 -07001918 std::string entryID = param;
1919 dbus::utility::escapePathForDbus(entryID);
1920
1921 // DBus implementation of EventLog/Entries
1922 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001923 sdbusplus::asio::getAllProperties(
1924 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1925 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001926 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001927 const dbus::utility::DBusPropertiesMap& resp) {
1928 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001929 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001930 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1931 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001932 return;
1933 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001934 if (ec)
1935 {
Ed Tanous62598e32023-07-17 17:06:25 -07001936 BMCWEB_LOG_ERROR(
1937 "EventLogEntry (DBus) resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001938 messages::internalError(asyncResp->res);
1939 return;
1940 }
1941 const uint32_t* id = nullptr;
1942 const uint64_t* timestamp = nullptr;
1943 const uint64_t* updateTimestamp = nullptr;
1944 const std::string* severity = nullptr;
1945 const std::string* message = nullptr;
1946 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001947 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001949 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001950
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001951 const bool success = sdbusplus::unpackPropertiesNoThrow(
1952 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1953 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001954 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001955 "Resolution", resolution, "Path", filePath,
1956 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001957
1958 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001959 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001960 messages::internalError(asyncResp->res);
1961 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001963
Ed Tanous002d39b2022-05-31 08:59:27 -07001964 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001965 timestamp == nullptr || updateTimestamp == nullptr ||
1966 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 {
1968 messages::internalError(asyncResp->res);
1969 return;
1970 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001971
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001973 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001974 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1975 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1976 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001977 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1978 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1979 asyncResp->res.jsonValue["Message"] = *message;
1980 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001981 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1982 if (notifyAction)
1983 {
1984 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1985 *notifyAction;
1986 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001987 if ((resolution != nullptr) && (!(*resolution).empty()))
1988 {
1989 asyncResp->res.jsonValue["Resolution"] = *resolution;
1990 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 asyncResp->res.jsonValue["EntryType"] = "Event";
1992 asyncResp->res.jsonValue["Severity"] =
1993 translateSeverityDbusToRedfish(*severity);
1994 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001995 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001996 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001997 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001998 if (filePath != nullptr)
1999 {
2000 asyncResp->res.jsonValue["AdditionalDataURI"] =
2001 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
2002 std::to_string(*id) + "/attachment";
2003 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002004 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07002005 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002006
2007 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002008 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002009 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002010 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002011 [&app](const crow::Request& req,
2012 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002013 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002014 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002015 {
2016 return;
2017 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002018 if constexpr (bmcwebEnableMultiHost)
2019 {
2020 // Option currently returns no systems. TBD
2021 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2022 systemName);
2023 return;
2024 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002025 if (systemName != "system")
2026 {
2027 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2028 systemName);
2029 return;
2030 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002031 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002032
Ed Tanous002d39b2022-05-31 08:59:27 -07002033 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
2034 resolved))
2035 {
2036 return;
2037 }
Ed Tanous62598e32023-07-17 17:06:25 -07002038 BMCWEB_LOG_DEBUG("Set Resolved");
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002039
George Liu9ae226f2023-06-21 17:56:46 +08002040 sdbusplus::asio::setProperty(
2041 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
2042 "/xyz/openbmc_project/logging/entry/" + entryId,
2043 "xyz.openbmc_project.Logging.Entry", "Resolved", *resolved,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002044 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002045 if (ec)
2046 {
Ed Tanous62598e32023-07-17 17:06:25 -07002047 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002048 messages::internalError(asyncResp->res);
2049 return;
2050 }
George Liu9ae226f2023-06-21 17:56:46 +08002051 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002052 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002053
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002054 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002055 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002056 .privileges(redfish::privileges::deleteLogEntry)
2057
Ed Tanous002d39b2022-05-31 08:59:27 -07002058 .methods(boost::beast::http::verb::delete_)(
2059 [&app](const crow::Request& req,
2060 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002061 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002062 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002063 {
2064 return;
2065 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002066 if constexpr (bmcwebEnableMultiHost)
2067 {
2068 // Option currently returns no systems. TBD
2069 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2070 systemName);
2071 return;
2072 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002073 if (systemName != "system")
2074 {
2075 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2076 systemName);
2077 return;
2078 }
Ed Tanous62598e32023-07-17 17:06:25 -07002079 BMCWEB_LOG_DEBUG("Do delete single event entries.");
Ed Tanous002d39b2022-05-31 08:59:27 -07002080
2081 std::string entryID = param;
2082
2083 dbus::utility::escapePathForDbus(entryID);
2084
2085 // Process response from Logging service.
2086 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002087 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07002088 BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07002089 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002090 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002091 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002092 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002093 messages::resourceNotFound(asyncResp->res, "LogEntry",
2094 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07002095 return;
2096 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002097 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07002098 BMCWEB_LOG_ERROR(
2099 "EventLogEntry (DBus) doDelete respHandler got error {}",
2100 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002101 asyncResp->res.result(
2102 boost::beast::http::status::internal_server_error);
2103 return;
2104 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002105
Ed Tanous002d39b2022-05-31 08:59:27 -07002106 asyncResp->res.result(boost::beast::http::status::ok);
2107 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002108
Ed Tanous002d39b2022-05-31 08:59:27 -07002109 // Make call to Logging service to request Delete Log
2110 crow::connections::systemBus->async_method_call(
2111 respHandler, "xyz.openbmc_project.Logging",
2112 "/xyz/openbmc_project/logging/entry/" + entryID,
2113 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07002114 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002115}
2116
Spencer Kub7028eb2021-10-26 15:27:35 +08002117constexpr const char* hostLoggerFolderPath = "/var/log/console";
2118
2119inline bool
2120 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2121 std::vector<std::filesystem::path>& hostLoggerFiles)
2122{
2123 std::error_code ec;
2124 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2125 if (ec)
2126 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002127 BMCWEB_LOG_WARNING("{}", ec.message());
Spencer Kub7028eb2021-10-26 15:27:35 +08002128 return false;
2129 }
2130 for (const std::filesystem::directory_entry& it : logPath)
2131 {
2132 std::string filename = it.path().filename();
2133 // Prefix of each log files is "log". Find the file and save the
2134 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002135 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002136 {
2137 hostLoggerFiles.emplace_back(it.path());
2138 }
2139 }
2140 // As the log files rotate, they are appended with a ".#" that is higher for
2141 // the older logs. Since we start from oldest logs, sort the name in
2142 // descending order.
2143 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2144 AlphanumLess<std::string>());
2145
2146 return true;
2147}
2148
Ed Tanous02cad962022-06-30 16:50:15 -07002149inline bool getHostLoggerEntries(
2150 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2151 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002152{
2153 GzFileReader logFile;
2154
2155 // Go though all log files and expose host logs.
2156 for (const std::filesystem::path& it : hostLoggerFiles)
2157 {
2158 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2159 {
Ed Tanous62598e32023-07-17 17:06:25 -07002160 BMCWEB_LOG_ERROR("fail to expose host logs");
Spencer Kub7028eb2021-10-26 15:27:35 +08002161 return false;
2162 }
2163 }
2164 // Get lastMessage from constructor by getter
2165 std::string lastMessage = logFile.getLastMessage();
2166 if (!lastMessage.empty())
2167 {
2168 logCount++;
2169 if (logCount > skip && logCount <= (skip + top))
2170 {
2171 logEntries.push_back(lastMessage);
2172 }
2173 }
2174 return true;
2175}
2176
2177inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2178 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002179 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002180{
2181 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002182 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002183 logEntryJson["@odata.id"] = boost::urls::format(
2184 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2185 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002186 logEntryJson["Name"] = "Host Logger Entry";
2187 logEntryJson["Id"] = logEntryID;
2188 logEntryJson["Message"] = msg;
2189 logEntryJson["EntryType"] = "Oem";
2190 logEntryJson["Severity"] = "OK";
2191 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002192}
2193
2194inline void requestRoutesSystemHostLogger(App& app)
2195{
Ed Tanous22d268c2022-05-19 09:39:07 -07002196 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002197 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002198 .methods(boost::beast::http::verb::get)(
2199 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002200 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2201 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002202 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 {
2204 return;
2205 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002206 if constexpr (bmcwebEnableMultiHost)
2207 {
2208 // Option currently returns no systems. TBD
2209 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2210 systemName);
2211 return;
2212 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002213 if (systemName != "system")
2214 {
2215 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2216 systemName);
2217 return;
2218 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002219 asyncResp->res.jsonValue["@odata.id"] =
2220 "/redfish/v1/Systems/system/LogServices/HostLogger";
2221 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002222 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002223 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2224 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2225 asyncResp->res.jsonValue["Id"] = "HostLogger";
2226 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2227 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2228 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002229}
2230
2231inline void requestRoutesSystemHostLoggerCollection(App& app)
2232{
2233 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002234 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002235 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002236 .methods(boost::beast::http::verb::get)(
2237 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002238 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2239 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002240 query_param::QueryCapabilities capabilities = {
2241 .canDelegateTop = true,
2242 .canDelegateSkip = true,
2243 };
2244 query_param::Query delegatedQuery;
2245 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002246 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002247 {
2248 return;
2249 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002250 if constexpr (bmcwebEnableMultiHost)
2251 {
2252 // Option currently returns no systems. TBD
2253 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2254 systemName);
2255 return;
2256 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002257 if (systemName != "system")
2258 {
2259 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2260 systemName);
2261 return;
2262 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002263 asyncResp->res.jsonValue["@odata.id"] =
2264 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2265 asyncResp->res.jsonValue["@odata.type"] =
2266 "#LogEntryCollection.LogEntryCollection";
2267 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2268 asyncResp->res.jsonValue["Description"] =
2269 "Collection of HostLogger Entries";
2270 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2271 logEntryArray = nlohmann::json::array();
2272 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002273
Ed Tanous002d39b2022-05-31 08:59:27 -07002274 std::vector<std::filesystem::path> hostLoggerFiles;
2275 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2276 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002277 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 return;
2279 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002280 // If we weren't provided top and skip limits, use the defaults.
2281 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002282 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002283 size_t logCount = 0;
2284 // This vector only store the entries we want to expose that
2285 // control by skip and top.
2286 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002287 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2288 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002289 {
2290 messages::internalError(asyncResp->res);
2291 return;
2292 }
2293 // If vector is empty, that means skip value larger than total
2294 // log count
2295 if (logEntries.empty())
2296 {
2297 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2298 return;
2299 }
2300 if (!logEntries.empty())
2301 {
2302 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002303 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002304 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002305 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2306 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002307 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002308 }
2309
Ed Tanous002d39b2022-05-31 08:59:27 -07002310 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002311 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002312 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002313 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2314 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002315 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002316 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002317 }
George Liu0fda0f12021-11-16 10:06:17 +08002318 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002319}
2320
2321inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2322{
2323 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002324 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002325 .privileges(redfish::privileges::getLogEntry)
2326 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002327 [&app](const crow::Request& req,
2328 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002329 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002330 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002331 {
2332 return;
2333 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002334 if constexpr (bmcwebEnableMultiHost)
2335 {
2336 // Option currently returns no systems. TBD
2337 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2338 systemName);
2339 return;
2340 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002341 if (systemName != "system")
2342 {
2343 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2344 systemName);
2345 return;
2346 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002347 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002348
Ed Tanous002d39b2022-05-31 08:59:27 -07002349 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002350
Patrick Williams84396af2023-05-11 11:47:45 -05002351 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2352 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002353 if (ec == std::errc::invalid_argument ||
2354 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002355 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002356 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002357 return;
2358 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002359
Ed Tanous002d39b2022-05-31 08:59:27 -07002360 std::vector<std::filesystem::path> hostLoggerFiles;
2361 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2362 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002363 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002364 return;
2365 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002366
Ed Tanous002d39b2022-05-31 08:59:27 -07002367 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002368 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002369 std::vector<std::string> logEntries;
2370 // We can get specific entry by skip and top. For example, if we
2371 // want to get nth entry, we can set skip = n-1 and top = 1 to
2372 // get that entry
2373 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2374 logCount))
2375 {
2376 messages::internalError(asyncResp->res);
2377 return;
2378 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002379
Ed Tanous002d39b2022-05-31 08:59:27 -07002380 if (!logEntries.empty())
2381 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002382 nlohmann::json::object_t hostLogEntry;
2383 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2384 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002385 return;
2386 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002387
Ed Tanous002d39b2022-05-31 08:59:27 -07002388 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002389 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002390 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002391}
2392
Claire Weinandd72e872022-08-15 14:20:06 -07002393inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002394 crow::App& app, const crow::Request& req,
2395 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2396{
2397 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2398 {
2399 return;
2400 }
2401 // Collections don't include the static data added by SubRoute
2402 // because it has a duplicate entry for members
2403 asyncResp->res.jsonValue["@odata.type"] =
2404 "#LogServiceCollection.LogServiceCollection";
2405 asyncResp->res.jsonValue["@odata.id"] =
2406 "/redfish/v1/Managers/bmc/LogServices";
2407 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2408 asyncResp->res.jsonValue["Description"] =
2409 "Collection of LogServices for this Manager";
2410 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2411 logServiceArray = nlohmann::json::array();
2412
2413#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002414 nlohmann::json::object_t journal;
2415 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002416 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002417#endif
2418
2419 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2420
2421#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002422 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002423 "xyz.openbmc_project.Collection.DeleteAll"};
2424 dbus::utility::getSubTreePaths(
2425 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002426 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002427 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002428 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2429 if (ec)
2430 {
Ed Tanous62598e32023-07-17 17:06:25 -07002431 BMCWEB_LOG_ERROR(
2432 "handleBMCLogServicesCollectionGet respHandler got error {}",
2433 ec);
Claire Weinanfdd26902022-03-01 14:18:25 -08002434 // Assume that getting an error simply means there are no dump
2435 // LogServices. Return without adding any error response.
2436 return;
2437 }
2438
2439 nlohmann::json& logServiceArrayLocal =
2440 asyncResp->res.jsonValue["Members"];
2441
2442 for (const std::string& path : subTreePaths)
2443 {
2444 if (path == "/xyz/openbmc_project/dump/bmc")
2445 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002446 nlohmann::json::object_t member;
2447 member["@odata.id"] =
2448 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002449 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002450 }
2451 else if (path == "/xyz/openbmc_project/dump/faultlog")
2452 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002453 nlohmann::json::object_t member;
2454 member["@odata.id"] =
2455 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002456 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002457 }
2458 }
2459
2460 asyncResp->res.jsonValue["Members@odata.count"] =
2461 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002462 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002463#endif
2464}
2465
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002466inline void requestRoutesBMCLogServiceCollection(App& app)
2467{
2468 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002469 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002470 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002471 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002472}
Ed Tanous1da66f72018-07-27 16:13:37 -07002473
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002474inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002475{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002476 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002477 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002478 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002479 [&app](const crow::Request& req,
2480 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002481 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002482 {
2483 return;
2484 }
2485 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002486 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002487 asyncResp->res.jsonValue["@odata.id"] =
2488 "/redfish/v1/Managers/bmc/LogServices/Journal";
2489 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2490 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002491 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002492 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302493
Ed Tanous002d39b2022-05-31 08:59:27 -07002494 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002495 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002496 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2497 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2498 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302499
Ed Tanous002d39b2022-05-31 08:59:27 -07002500 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2501 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2502 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002503}
Jason M. Billse1f26342018-07-18 12:12:00 -07002504
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002505static int
2506 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2507 sd_journal* journal,
2508 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002509{
2510 // Get the Log Entry contents
2511 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002512
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002513 std::string message;
2514 std::string_view syslogID;
2515 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2516 if (ret < 0)
2517 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002518 BMCWEB_LOG_DEBUG("Failed to read SYSLOG_IDENTIFIER field: {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002519 strerror(-ret));
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002520 }
2521 if (!syslogID.empty())
2522 {
2523 message += std::string(syslogID) + ": ";
2524 }
2525
Ed Tanous39e77502019-03-04 17:35:53 -08002526 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002527 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002528 if (ret < 0)
2529 {
Ed Tanous62598e32023-07-17 17:06:25 -07002530 BMCWEB_LOG_ERROR("Failed to read MESSAGE field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002531 return 1;
2532 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002533 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002534
2535 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002536 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002537 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002538 if (ret < 0)
2539 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002540 BMCWEB_LOG_DEBUG("Failed to read PRIORITY field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002541 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002542
2543 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002544 std::string entryTimeStr;
2545 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002546 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002547 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002548 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002549
2550 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002551 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002552 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2553 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002554 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002555 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2556 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2557 bmcJournalLogEntryJson["Message"] = std::move(message);
2558 bmcJournalLogEntryJson["EntryType"] = "Oem";
2559 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2560 : severity <= 4 ? "Warning"
2561 : "OK";
2562 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2563 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002564 return 0;
2565}
2566
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002567inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002568{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002569 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002570 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002571 .methods(boost::beast::http::verb::get)(
2572 [&app](const crow::Request& req,
2573 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2574 query_param::QueryCapabilities capabilities = {
2575 .canDelegateTop = true,
2576 .canDelegateSkip = true,
2577 };
2578 query_param::Query delegatedQuery;
2579 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002580 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002581 {
2582 return;
2583 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002584
2585 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002586 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002587
Ed Tanous002d39b2022-05-31 08:59:27 -07002588 // Collections don't include the static data added by SubRoute
2589 // because it has a duplicate entry for members
2590 asyncResp->res.jsonValue["@odata.type"] =
2591 "#LogEntryCollection.LogEntryCollection";
2592 asyncResp->res.jsonValue["@odata.id"] =
2593 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2594 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2595 asyncResp->res.jsonValue["Description"] =
2596 "Collection of BMC Journal Entries";
2597 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2598 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002599
Ed Tanous002d39b2022-05-31 08:59:27 -07002600 // Go through the journal and use the timestamp to create a
2601 // unique ID for each entry
2602 sd_journal* journalTmp = nullptr;
2603 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2604 if (ret < 0)
2605 {
Ed Tanous62598e32023-07-17 17:06:25 -07002606 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002607 messages::internalError(asyncResp->res);
2608 return;
2609 }
2610 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2611 journalTmp, sd_journal_close);
2612 journalTmp = nullptr;
2613 uint64_t entryCount = 0;
2614 // Reset the unique ID on the first entry
2615 bool firstEntry = true;
2616 SD_JOURNAL_FOREACH(journal.get())
2617 {
2618 entryCount++;
2619 // Handle paging using skip (number of entries to skip from
2620 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002621 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002622 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002623 continue;
2624 }
2625
2626 std::string idStr;
2627 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2628 {
2629 continue;
2630 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002631 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002632
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002633 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002634 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2635 bmcJournalLogEntry) != 0)
2636 {
George Liu0fda0f12021-11-16 10:06:17 +08002637 messages::internalError(asyncResp->res);
2638 return;
2639 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002640 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002641 }
2642 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002643 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002644 {
2645 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2646 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002647 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002648 }
George Liu0fda0f12021-11-16 10:06:17 +08002649 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002650}
Jason M. Billse1f26342018-07-18 12:12:00 -07002651
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002652inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002653{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002654 BMCWEB_ROUTE(app,
2655 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002656 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002657 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002658 [&app](const crow::Request& req,
2659 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2660 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002661 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002662 {
2663 return;
2664 }
2665 // Convert the unique ID back to a timestamp to find the entry
2666 uint64_t ts = 0;
2667 uint64_t index = 0;
2668 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2669 {
2670 return;
2671 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002672
Ed Tanous002d39b2022-05-31 08:59:27 -07002673 sd_journal* journalTmp = nullptr;
2674 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2675 if (ret < 0)
2676 {
Ed Tanous62598e32023-07-17 17:06:25 -07002677 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002678 messages::internalError(asyncResp->res);
2679 return;
2680 }
2681 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2682 journalTmp, sd_journal_close);
2683 journalTmp = nullptr;
2684 // Go to the timestamp in the log and move to the entry at the
2685 // index tracking the unique ID
2686 std::string idStr;
2687 bool firstEntry = true;
2688 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2689 if (ret < 0)
2690 {
Ed Tanous62598e32023-07-17 17:06:25 -07002691 BMCWEB_LOG_ERROR("failed to seek to an entry in journal{}",
2692 strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002693 messages::internalError(asyncResp->res);
2694 return;
2695 }
2696 for (uint64_t i = 0; i <= index; i++)
2697 {
2698 sd_journal_next(journal.get());
2699 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2700 {
2701 messages::internalError(asyncResp->res);
2702 return;
2703 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002704 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002705 }
2706 // Confirm that the entry ID matches what was requested
2707 if (idStr != entryID)
2708 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002709 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002710 return;
2711 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002712
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002713 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002714 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002715 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002716 {
2717 messages::internalError(asyncResp->res);
2718 return;
2719 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002720 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002721 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002722}
2723
Claire Weinanfdd26902022-03-01 14:18:25 -08002724inline void
2725 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2726 const std::string& dumpType)
2727{
2728 std::string dumpPath;
2729 std::string overWritePolicy;
2730 bool collectDiagnosticDataSupported = false;
2731
2732 if (dumpType == "BMC")
2733 {
2734 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2735 overWritePolicy = "WrapsWhenFull";
2736 collectDiagnosticDataSupported = true;
2737 }
2738 else if (dumpType == "FaultLog")
2739 {
2740 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2741 overWritePolicy = "Unknown";
2742 collectDiagnosticDataSupported = false;
2743 }
2744 else if (dumpType == "System")
2745 {
2746 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2747 overWritePolicy = "WrapsWhenFull";
2748 collectDiagnosticDataSupported = true;
2749 }
2750 else
2751 {
Ed Tanous62598e32023-07-17 17:06:25 -07002752 BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
2753 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -08002754 messages::internalError(asyncResp->res);
2755 return;
2756 }
2757
2758 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2759 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2760 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2761 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2762 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2763 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2764
2765 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002766 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002767 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2768 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2769 redfishDateTimeOffset.second;
2770
2771 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002772
2773 if (collectDiagnosticDataSupported)
2774 {
2775 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2776 ["target"] =
2777 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2778 }
Claire Weinan0d946212022-07-13 19:40:19 -07002779
2780 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2781 dbus::utility::getSubTreePaths(
2782 "/xyz/openbmc_project/dump", 0, interfaces,
2783 [asyncResp, dumpType, dumpPath](
2784 const boost::system::error_code& ec,
2785 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2786 if (ec)
2787 {
Ed Tanous62598e32023-07-17 17:06:25 -07002788 BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
Claire Weinan0d946212022-07-13 19:40:19 -07002789 // Assume that getting an error simply means there are no dump
2790 // LogServices. Return without adding any error response.
2791 return;
2792 }
2793
2794 const std::string dbusDumpPath =
2795 "/xyz/openbmc_project/dump/" +
2796 boost::algorithm::to_lower_copy(dumpType);
2797
2798 for (const std::string& path : subTreePaths)
2799 {
2800 if (path == dbusDumpPath)
2801 {
2802 asyncResp->res
2803 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2804 dumpPath + "/Actions/LogService.ClearLog";
2805 break;
2806 }
2807 }
2808 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002809}
2810
2811inline void handleLogServicesDumpServiceGet(
2812 crow::App& app, const std::string& dumpType, const crow::Request& req,
2813 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2814{
2815 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2816 {
2817 return;
2818 }
2819 getDumpServiceInfo(asyncResp, dumpType);
2820}
2821
Ed Tanous22d268c2022-05-19 09:39:07 -07002822inline void handleLogServicesDumpServiceComputerSystemGet(
2823 crow::App& app, const crow::Request& req,
2824 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2825 const std::string& chassisId)
2826{
2827 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2828 {
2829 return;
2830 }
2831 if (chassisId != "system")
2832 {
2833 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2834 return;
2835 }
2836 getDumpServiceInfo(asyncResp, "System");
2837}
2838
Claire Weinanfdd26902022-03-01 14:18:25 -08002839inline void handleLogServicesDumpEntriesCollectionGet(
2840 crow::App& app, const std::string& dumpType, const crow::Request& req,
2841 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2842{
2843 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2844 {
2845 return;
2846 }
2847 getDumpEntryCollection(asyncResp, dumpType);
2848}
2849
Ed Tanous22d268c2022-05-19 09:39:07 -07002850inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2851 crow::App& app, const crow::Request& req,
2852 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2853 const std::string& chassisId)
2854{
2855 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2856 {
2857 return;
2858 }
2859 if (chassisId != "system")
2860 {
2861 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2862 return;
2863 }
2864 getDumpEntryCollection(asyncResp, "System");
2865}
2866
Claire Weinanfdd26902022-03-01 14:18:25 -08002867inline void handleLogServicesDumpEntryGet(
2868 crow::App& app, const std::string& dumpType, const crow::Request& req,
2869 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2870 const std::string& dumpId)
2871{
2872 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2873 {
2874 return;
2875 }
2876 getDumpEntryById(asyncResp, dumpId, dumpType);
2877}
Carson Labrado168d1b12023-03-27 17:04:46 +00002878
Ed Tanous22d268c2022-05-19 09:39:07 -07002879inline void handleLogServicesDumpEntryComputerSystemGet(
2880 crow::App& app, const crow::Request& req,
2881 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2882 const std::string& chassisId, const std::string& dumpId)
2883{
2884 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2885 {
2886 return;
2887 }
2888 if (chassisId != "system")
2889 {
2890 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2891 return;
2892 }
2893 getDumpEntryById(asyncResp, dumpId, "System");
2894}
Claire Weinanfdd26902022-03-01 14:18:25 -08002895
2896inline void handleLogServicesDumpEntryDelete(
2897 crow::App& app, const std::string& dumpType, const crow::Request& req,
2898 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2899 const std::string& dumpId)
2900{
2901 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2902 {
2903 return;
2904 }
2905 deleteDumpEntry(asyncResp, dumpId, dumpType);
2906}
2907
Ed Tanous22d268c2022-05-19 09:39:07 -07002908inline void handleLogServicesDumpEntryComputerSystemDelete(
2909 crow::App& app, const crow::Request& req,
2910 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2911 const std::string& chassisId, const std::string& dumpId)
2912{
2913 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2914 {
2915 return;
2916 }
2917 if (chassisId != "system")
2918 {
2919 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2920 return;
2921 }
2922 deleteDumpEntry(asyncResp, dumpId, "System");
2923}
2924
Carson Labrado168d1b12023-03-27 17:04:46 +00002925inline void handleLogServicesDumpEntryDownloadGet(
2926 crow::App& app, const std::string& dumpType, const crow::Request& req,
2927 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2928 const std::string& dumpId)
2929{
2930 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2931 {
2932 return;
2933 }
2934 downloadDumpEntry(asyncResp, dumpId, dumpType);
2935}
2936
2937inline void handleDBusEventLogEntryDownloadGet(
2938 crow::App& app, const std::string& dumpType, const crow::Request& req,
2939 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2940 const std::string& systemName, const std::string& entryID)
2941{
2942 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2943 {
2944 return;
2945 }
2946 if (!http_helpers::isContentTypeAllowed(
2947 req.getHeaderValue("Accept"),
2948 http_helpers::ContentType::OctetStream, true))
2949 {
2950 asyncResp->res.result(boost::beast::http::status::bad_request);
2951 return;
2952 }
2953 downloadEventLogEntry(asyncResp, systemName, entryID, dumpType);
2954}
2955
Claire Weinanfdd26902022-03-01 14:18:25 -08002956inline void handleLogServicesDumpCollectDiagnosticDataPost(
2957 crow::App& app, const std::string& dumpType, const crow::Request& req,
2958 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2959{
2960 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2961 {
2962 return;
2963 }
2964 createDump(asyncResp, req, dumpType);
2965}
2966
Ed Tanous22d268c2022-05-19 09:39:07 -07002967inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2968 crow::App& app, const crow::Request& req,
2969 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002970 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07002971{
2972 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2973 {
2974 return;
2975 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002976
2977 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07002978 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002979 // Option currently returns no systems. TBD
2980 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2981 systemName);
2982 return;
2983 }
2984 if (systemName != "system")
2985 {
2986 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2987 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07002988 return;
2989 }
2990 createDump(asyncResp, req, "System");
2991}
2992
Claire Weinanfdd26902022-03-01 14:18:25 -08002993inline void handleLogServicesDumpClearLogPost(
2994 crow::App& app, const std::string& dumpType, const crow::Request& req,
2995 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2996{
2997 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2998 {
2999 return;
3000 }
3001 clearDump(asyncResp, dumpType);
3002}
3003
Ed Tanous22d268c2022-05-19 09:39:07 -07003004inline void handleLogServicesDumpClearLogComputerSystemPost(
3005 crow::App& app, const crow::Request& req,
3006 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003007 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003008{
3009 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3010 {
3011 return;
3012 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003013 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003014 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003015 // Option currently returns no systems. TBD
3016 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3017 systemName);
3018 return;
3019 }
3020 if (systemName != "system")
3021 {
3022 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3023 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003024 return;
3025 }
3026 clearDump(asyncResp, "System");
3027}
3028
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003029inline void requestRoutesBMCDumpService(App& app)
3030{
3031 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003032 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003033 .methods(boost::beast::http::verb::get)(std::bind_front(
3034 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003035}
3036
3037inline void requestRoutesBMCDumpEntryCollection(App& app)
3038{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003039 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003040 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08003041 .methods(boost::beast::http::verb::get)(std::bind_front(
3042 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003043}
3044
3045inline void requestRoutesBMCDumpEntry(App& app)
3046{
3047 BMCWEB_ROUTE(app,
3048 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003049 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003050 .methods(boost::beast::http::verb::get)(std::bind_front(
3051 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
3052
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003053 BMCWEB_ROUTE(app,
3054 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003055 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003056 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3057 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003058}
3059
Carson Labrado168d1b12023-03-27 17:04:46 +00003060inline void requestRoutesBMCDumpEntryDownload(App& app)
3061{
3062 BMCWEB_ROUTE(
3063 app,
3064 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/attachment")
3065 .privileges(redfish::privileges::getLogEntry)
3066 .methods(boost::beast::http::verb::get)(std::bind_front(
3067 handleLogServicesDumpEntryDownloadGet, std::ref(app), "BMC"));
3068}
3069
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003070inline void requestRoutesBMCDumpCreate(App& app)
3071{
George Liu0fda0f12021-11-16 10:06:17 +08003072 BMCWEB_ROUTE(
3073 app,
3074 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003075 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003076 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08003077 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
3078 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003079}
3080
3081inline void requestRoutesBMCDumpClear(App& app)
3082{
George Liu0fda0f12021-11-16 10:06:17 +08003083 BMCWEB_ROUTE(
3084 app,
3085 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003086 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003087 .methods(boost::beast::http::verb::post)(std::bind_front(
3088 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
3089}
3090
Carson Labrado168d1b12023-03-27 17:04:46 +00003091inline void requestRoutesDBusEventLogEntryDownload(App& app)
3092{
3093 BMCWEB_ROUTE(
3094 app,
3095 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
3096 .privileges(redfish::privileges::getLogEntry)
3097 .methods(boost::beast::http::verb::get)(std::bind_front(
3098 handleDBusEventLogEntryDownloadGet, std::ref(app), "System"));
3099}
3100
Claire Weinanfdd26902022-03-01 14:18:25 -08003101inline void requestRoutesFaultLogDumpService(App& app)
3102{
3103 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3104 .privileges(redfish::privileges::getLogService)
3105 .methods(boost::beast::http::verb::get)(std::bind_front(
3106 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3107}
3108
3109inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3110{
3111 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3112 .privileges(redfish::privileges::getLogEntryCollection)
3113 .methods(boost::beast::http::verb::get)(
3114 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3115 std::ref(app), "FaultLog"));
3116}
3117
3118inline void requestRoutesFaultLogDumpEntry(App& app)
3119{
3120 BMCWEB_ROUTE(app,
3121 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3122 .privileges(redfish::privileges::getLogEntry)
3123 .methods(boost::beast::http::verb::get)(std::bind_front(
3124 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3125
3126 BMCWEB_ROUTE(app,
3127 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3128 .privileges(redfish::privileges::deleteLogEntry)
3129 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3130 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3131}
3132
3133inline void requestRoutesFaultLogDumpClear(App& app)
3134{
3135 BMCWEB_ROUTE(
3136 app,
3137 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3138 .privileges(redfish::privileges::postLogService)
3139 .methods(boost::beast::http::verb::post)(std::bind_front(
3140 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003141}
3142
3143inline void requestRoutesSystemDumpService(App& app)
3144{
Ed Tanous22d268c2022-05-19 09:39:07 -07003145 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003146 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003147 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003148 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003149}
3150
3151inline void requestRoutesSystemDumpEntryCollection(App& app)
3152{
Ed Tanous22d268c2022-05-19 09:39:07 -07003153 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003154 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07003155 .methods(boost::beast::http::verb::get)(std::bind_front(
3156 handleLogServicesDumpEntriesCollectionComputerSystemGet,
3157 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003158}
3159
3160inline void requestRoutesSystemDumpEntry(App& app)
3161{
3162 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003163 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003164 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003165 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003166 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003167
3168 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003169 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003170 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003171 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003172 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003173}
3174
3175inline void requestRoutesSystemDumpCreate(App& app)
3176{
George Liu0fda0f12021-11-16 10:06:17 +08003177 BMCWEB_ROUTE(
3178 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003179 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003180 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003181 .methods(boost::beast::http::verb::post)(std::bind_front(
3182 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3183 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003184}
3185
3186inline void requestRoutesSystemDumpClear(App& app)
3187{
George Liu0fda0f12021-11-16 10:06:17 +08003188 BMCWEB_ROUTE(
3189 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003190 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003191 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003192 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003193 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003194}
3195
3196inline void requestRoutesCrashdumpService(App& app)
3197{
3198 // Note: Deviated from redfish privilege registry for GET & HEAD
3199 // method for security reasons.
3200 /**
3201 * Functions triggers appropriate requests on DBus
3202 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003203 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003204 // This is incorrect, should be:
3205 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003206 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003207 .methods(boost::beast::http::verb::get)(
3208 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003209 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3210 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003211 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003212 {
3213 return;
3214 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003215 if constexpr (bmcwebEnableMultiHost)
3216 {
3217 // Option currently returns no systems. TBD
3218 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3219 systemName);
3220 return;
3221 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003222 if (systemName != "system")
3223 {
3224 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3225 systemName);
3226 return;
3227 }
3228
Ed Tanous002d39b2022-05-31 08:59:27 -07003229 // Copy over the static data to include the entries added by
3230 // SubRoute
3231 asyncResp->res.jsonValue["@odata.id"] =
3232 "/redfish/v1/Systems/system/LogServices/Crashdump";
3233 asyncResp->res.jsonValue["@odata.type"] =
3234 "#LogService.v1_2_0.LogService";
3235 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3236 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303237 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003238 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3239 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303240
Ed Tanous002d39b2022-05-31 08:59:27 -07003241 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003242 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003243 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3244 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3245 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303246
Ed Tanous002d39b2022-05-31 08:59:27 -07003247 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003248 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003249 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3250 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3251 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3252 ["target"] =
3253 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003254 });
3255}
3256
3257void inline requestRoutesCrashdumpClear(App& app)
3258{
George Liu0fda0f12021-11-16 10:06:17 +08003259 BMCWEB_ROUTE(
3260 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003261 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003262 // This is incorrect, should be:
3263 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003264 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003265 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003266 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003267 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3268 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003269 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003270 {
3271 return;
3272 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003273 if constexpr (bmcwebEnableMultiHost)
3274 {
3275 // Option currently returns no systems. TBD
3276 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3277 systemName);
3278 return;
3279 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003280 if (systemName != "system")
3281 {
3282 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3283 systemName);
3284 return;
3285 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003286 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003287 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003288 const std::string&) {
3289 if (ec)
3290 {
3291 messages::internalError(asyncResp->res);
3292 return;
3293 }
3294 messages::success(asyncResp->res);
3295 },
3296 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3297 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003298}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003299
zhanghch058d1b46d2021-04-01 11:18:24 +08003300static void
3301 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3302 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003303{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003304 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003305 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003306 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003307 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003308 if (ec)
3309 {
Ed Tanous62598e32023-07-17 17:06:25 -07003310 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003311 if (ec.value() ==
3312 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003313 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003314 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003315 }
3316 else
3317 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003318 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003319 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003320 return;
3321 }
3322
3323 std::string timestamp{};
3324 std::string filename{};
3325 std::string logfile{};
3326 parseCrashdumpParameters(params, filename, timestamp, logfile);
3327
3328 if (filename.empty() || timestamp.empty())
3329 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003330 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003331 return;
3332 }
3333
3334 std::string crashdumpURI =
3335 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3336 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003337 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003338 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003339 logEntry["@odata.id"] = boost::urls::format(
3340 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3341 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003342 logEntry["Name"] = "CPU Crashdump";
3343 logEntry["Id"] = logID;
3344 logEntry["EntryType"] = "Oem";
3345 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3346 logEntry["DiagnosticDataType"] = "OEM";
3347 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3348 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003349
3350 // If logEntryJson references an array of LogEntry resources
3351 // ('Members' list), then push this as a new entry, otherwise set it
3352 // directly
3353 if (logEntryJson.is_array())
3354 {
3355 logEntryJson.push_back(logEntry);
3356 asyncResp->res.jsonValue["Members@odata.count"] =
3357 logEntryJson.size();
3358 }
3359 else
3360 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003361 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003362 }
3363 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003364 sdbusplus::asio::getAllProperties(
3365 *crow::connections::systemBus, crashdumpObject,
3366 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3367 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003368}
3369
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003370inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003371{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003372 // Note: Deviated from redfish privilege registry for GET & HEAD
3373 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003374 /**
3375 * Functions triggers appropriate requests on DBus
3376 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003377 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003378 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003379 // This is incorrect, should be.
3380 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003381 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003382 .methods(boost::beast::http::verb::get)(
3383 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003384 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3385 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003386 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003387 {
3388 return;
3389 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003390 if constexpr (bmcwebEnableMultiHost)
3391 {
3392 // Option currently returns no systems. TBD
3393 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3394 systemName);
3395 return;
3396 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003397 if (systemName != "system")
3398 {
3399 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3400 systemName);
3401 return;
3402 }
3403
George Liu7a1dbc42022-12-07 16:03:22 +08003404 constexpr std::array<std::string_view, 1> interfaces = {
3405 crashdumpInterface};
3406 dbus::utility::getSubTreePaths(
3407 "/", 0, interfaces,
3408 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003409 const std::vector<std::string>& resp) {
3410 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003411 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003412 if (ec.value() !=
3413 boost::system::errc::no_such_file_or_directory)
3414 {
Ed Tanous62598e32023-07-17 17:06:25 -07003415 BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
3416 ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003417 messages::internalError(asyncResp->res);
3418 return;
3419 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003420 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003421 asyncResp->res.jsonValue["@odata.type"] =
3422 "#LogEntryCollection.LogEntryCollection";
3423 asyncResp->res.jsonValue["@odata.id"] =
3424 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3425 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3426 asyncResp->res.jsonValue["Description"] =
3427 "Collection of Crashdump Entries";
3428 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3429 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003430
Ed Tanous002d39b2022-05-31 08:59:27 -07003431 for (const std::string& path : resp)
3432 {
3433 const sdbusplus::message::object_path objPath(path);
3434 // Get the log ID
3435 std::string logID = objPath.filename();
3436 if (logID.empty())
3437 {
3438 continue;
3439 }
3440 // Add the log entry to the array
3441 logCrashdumpEntry(asyncResp, logID,
3442 asyncResp->res.jsonValue["Members"]);
3443 }
George Liu7a1dbc42022-12-07 16:03:22 +08003444 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003445 });
3446}
Ed Tanous1da66f72018-07-27 16:13:37 -07003447
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003448inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003449{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003450 // Note: Deviated from redfish privilege registry for GET & HEAD
3451 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003452
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003453 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003454 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003455 // this is incorrect, should be
3456 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003457 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003458 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003459 [&app](const crow::Request& req,
3460 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003461 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003462 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003463 {
3464 return;
3465 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003466 if constexpr (bmcwebEnableMultiHost)
3467 {
3468 // Option currently returns no systems. TBD
3469 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3470 systemName);
3471 return;
3472 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003473 if (systemName != "system")
3474 {
3475 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3476 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003477 return;
3478 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003479 const std::string& logID = param;
3480 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3481 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003482}
Ed Tanous1da66f72018-07-27 16:13:37 -07003483
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003484inline void requestRoutesCrashdumpFile(App& app)
3485{
3486 // Note: Deviated from redfish privilege registry for GET & HEAD
3487 // method for security reasons.
3488 BMCWEB_ROUTE(
3489 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003490 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003491 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003492 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003493 [](const crow::Request& req,
3494 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003495 const std::string& systemName, const std::string& logID,
3496 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003497 // Do not call getRedfishRoute here since the crashdump file is not a
3498 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003499
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003500 if constexpr (bmcwebEnableMultiHost)
3501 {
3502 // Option currently returns no systems. TBD
3503 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3504 systemName);
3505 return;
3506 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003507 if (systemName != "system")
3508 {
3509 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3510 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003511 return;
3512 }
3513
Ed Tanous002d39b2022-05-31 08:59:27 -07003514 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003515 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003516 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003517 const std::vector<
3518 std::pair<std::string, dbus::utility::DbusVariantType>>&
3519 resp) {
3520 if (ec)
3521 {
Ed Tanous62598e32023-07-17 17:06:25 -07003522 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003523 messages::internalError(asyncResp->res);
3524 return;
3525 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003526
Ed Tanous002d39b2022-05-31 08:59:27 -07003527 std::string dbusFilename{};
3528 std::string dbusTimestamp{};
3529 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003530
Ed Tanous002d39b2022-05-31 08:59:27 -07003531 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3532 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003533
Ed Tanous002d39b2022-05-31 08:59:27 -07003534 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3535 dbusFilepath.empty())
3536 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003537 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003538 return;
3539 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003540
Ed Tanous002d39b2022-05-31 08:59:27 -07003541 // Verify the file name parameter is correct
3542 if (fileName != dbusFilename)
3543 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003544 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003545 return;
3546 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003547
Ed Tanous002d39b2022-05-31 08:59:27 -07003548 if (!std::filesystem::exists(dbusFilepath))
3549 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003550 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003551 return;
3552 }
3553 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3554 asyncResp->res.body() =
3555 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003556
Ed Tanous002d39b2022-05-31 08:59:27 -07003557 // Configure this to be a file download when accessed
3558 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003559 asyncResp->res.addHeader(
3560 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003561 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003562 sdbusplus::asio::getAllProperties(
3563 *crow::connections::systemBus, crashdumpObject,
3564 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3565 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003566 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003567}
3568
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003569enum class OEMDiagnosticType
3570{
3571 onDemand,
3572 telemetry,
3573 invalid,
3574};
3575
Ed Tanous26ccae32023-02-16 10:28:44 -08003576inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003577{
3578 if (oemDiagStr == "OnDemand")
3579 {
3580 return OEMDiagnosticType::onDemand;
3581 }
3582 if (oemDiagStr == "Telemetry")
3583 {
3584 return OEMDiagnosticType::telemetry;
3585 }
3586
3587 return OEMDiagnosticType::invalid;
3588}
3589
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003590inline void requestRoutesCrashdumpCollect(App& app)
3591{
3592 // Note: Deviated from redfish privilege registry for GET & HEAD
3593 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003594 BMCWEB_ROUTE(
3595 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003596 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003597 // The below is incorrect; Should be ConfigureManager
3598 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003599 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003600 .methods(boost::beast::http::verb::post)(
3601 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003602 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3603 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003604 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003605 {
3606 return;
3607 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003608
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003609 if constexpr (bmcwebEnableMultiHost)
3610 {
3611 // Option currently returns no systems. TBD
3612 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3613 systemName);
3614 return;
3615 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003616 if (systemName != "system")
3617 {
3618 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3619 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003620 return;
3621 }
3622
Ed Tanous002d39b2022-05-31 08:59:27 -07003623 std::string diagnosticDataType;
3624 std::string oemDiagnosticDataType;
3625 if (!redfish::json_util::readJsonAction(
3626 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3627 "OEMDiagnosticDataType", oemDiagnosticDataType))
3628 {
3629 return;
3630 }
3631
3632 if (diagnosticDataType != "OEM")
3633 {
Ed Tanous62598e32023-07-17 17:06:25 -07003634 BMCWEB_LOG_ERROR(
3635 "Only OEM DiagnosticDataType supported for Crashdump");
Ed Tanous002d39b2022-05-31 08:59:27 -07003636 messages::actionParameterValueFormatError(
3637 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3638 "CollectDiagnosticData");
3639 return;
3640 }
3641
3642 OEMDiagnosticType oemDiagType =
3643 getOEMDiagnosticType(oemDiagnosticDataType);
3644
3645 std::string iface;
3646 std::string method;
3647 std::string taskMatchStr;
3648 if (oemDiagType == OEMDiagnosticType::onDemand)
3649 {
3650 iface = crashdumpOnDemandInterface;
3651 method = "GenerateOnDemandLog";
3652 taskMatchStr = "type='signal',"
3653 "interface='org.freedesktop.DBus.Properties',"
3654 "member='PropertiesChanged',"
3655 "arg0namespace='com.intel.crashdump'";
3656 }
3657 else if (oemDiagType == OEMDiagnosticType::telemetry)
3658 {
3659 iface = crashdumpTelemetryInterface;
3660 method = "GenerateTelemetryLog";
3661 taskMatchStr = "type='signal',"
3662 "interface='org.freedesktop.DBus.Properties',"
3663 "member='PropertiesChanged',"
3664 "arg0namespace='com.intel.crashdump'";
3665 }
3666 else
3667 {
Ed Tanous62598e32023-07-17 17:06:25 -07003668 BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
3669 oemDiagnosticDataType);
Ed Tanous002d39b2022-05-31 08:59:27 -07003670 messages::actionParameterValueFormatError(
3671 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3672 "CollectDiagnosticData");
3673 return;
3674 }
3675
3676 auto collectCrashdumpCallback =
3677 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003678 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003679 const std::string&) mutable {
3680 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003681 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003682 if (ec.value() == boost::system::errc::operation_not_supported)
3683 {
3684 messages::resourceInStandby(asyncResp->res);
3685 }
3686 else if (ec.value() ==
3687 boost::system::errc::device_or_resource_busy)
3688 {
3689 messages::serviceTemporarilyUnavailable(asyncResp->res,
3690 "60");
3691 }
3692 else
3693 {
3694 messages::internalError(asyncResp->res);
3695 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003696 return;
3697 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003698 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -07003699 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003700 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07003701 if (!ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -07003702 {
3703 taskData->messages.emplace_back(messages::taskCompletedOK(
3704 std::to_string(taskData->index)));
3705 taskData->state = "Completed";
3706 }
3707 return task::completed;
3708 },
3709 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003710
Ed Tanous002d39b2022-05-31 08:59:27 -07003711 task->startTimer(std::chrono::minutes(5));
3712 task->populateResp(asyncResp->res);
3713 task->payload.emplace(std::move(payload));
3714 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003715
Ed Tanous002d39b2022-05-31 08:59:27 -07003716 crow::connections::systemBus->async_method_call(
3717 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3718 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003719 });
3720}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003721
Andrew Geisslercb92c032018-08-17 07:56:14 -07003722/**
3723 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3724 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003725inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003726{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003727 /**
3728 * Function handles POST method request.
3729 * The Clear Log actions does not require any parameter.The action deletes
3730 * all entries found in the Entries collection for this Log Service.
3731 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003732
George Liu0fda0f12021-11-16 10:06:17 +08003733 BMCWEB_ROUTE(
3734 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003735 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003736 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003737 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003738 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003739 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3740 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003741 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003742 {
3743 return;
3744 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003745 if constexpr (bmcwebEnableMultiHost)
3746 {
3747 // Option currently returns no systems. TBD
3748 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3749 systemName);
3750 return;
3751 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003752 if (systemName != "system")
3753 {
3754 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3755 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003756 return;
3757 }
Ed Tanous62598e32023-07-17 17:06:25 -07003758 BMCWEB_LOG_DEBUG("Do delete all entries.");
Andrew Geisslercb92c032018-08-17 07:56:14 -07003759
Ed Tanous002d39b2022-05-31 08:59:27 -07003760 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003761 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07003762 BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07003763 if (ec)
3764 {
3765 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003766 BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003767 asyncResp->res.result(
3768 boost::beast::http::status::internal_server_error);
3769 return;
3770 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003771
Ed Tanous002d39b2022-05-31 08:59:27 -07003772 asyncResp->res.result(boost::beast::http::status::no_content);
3773 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003774
Ed Tanous002d39b2022-05-31 08:59:27 -07003775 // Make call to Logging service to request Clear Log
3776 crow::connections::systemBus->async_method_call(
3777 respHandler, "xyz.openbmc_project.Logging",
3778 "/xyz/openbmc_project/logging",
3779 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3780 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003781}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003782
3783/****************************************************
3784 * Redfish PostCode interfaces
3785 * using DBUS interface: getPostCodesTS
3786 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003787inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003788{
Ed Tanous22d268c2022-05-19 09:39:07 -07003789 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003790 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003791 .methods(boost::beast::http::verb::get)(
3792 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003793 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3794 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003795 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003796 {
3797 return;
3798 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003799 if constexpr (bmcwebEnableMultiHost)
3800 {
3801 // Option currently returns no systems. TBD
3802 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3803 systemName);
3804 return;
3805 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003806 if (systemName != "system")
3807 {
3808 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3809 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003810 return;
3811 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003812 asyncResp->res.jsonValue["@odata.id"] =
3813 "/redfish/v1/Systems/system/LogServices/PostCodes";
3814 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05003815 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07003816 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3817 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003818 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003819 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3820 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3821 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303822
Ed Tanous002d39b2022-05-31 08:59:27 -07003823 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003824 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003825 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3826 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3827 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303828
Ed Tanous002d39b2022-05-31 08:59:27 -07003829 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3830 {"target",
3831 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003832 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003833}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003834
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003835inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003836{
George Liu0fda0f12021-11-16 10:06:17 +08003837 BMCWEB_ROUTE(
3838 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003839 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003840 // The following privilege is incorrect; It should be ConfigureManager
3841 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003842 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003843 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003844 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003845 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3846 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003847 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003848 {
3849 return;
3850 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003851 if constexpr (bmcwebEnableMultiHost)
3852 {
3853 // Option currently returns no systems. TBD
3854 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3855 systemName);
3856 return;
3857 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003858 if (systemName != "system")
3859 {
3860 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3861 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003862 return;
3863 }
Ed Tanous62598e32023-07-17 17:06:25 -07003864 BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003865
Ed Tanous002d39b2022-05-31 08:59:27 -07003866 // Make call to post-code service to request clear all
3867 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003868 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003869 if (ec)
3870 {
3871 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003872 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
3873 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003874 asyncResp->res.result(
3875 boost::beast::http::status::internal_server_error);
3876 messages::internalError(asyncResp->res);
3877 return;
3878 }
Tony Lee18fc70c2023-08-24 16:15:54 +08003879 messages::success(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07003880 },
3881 "xyz.openbmc_project.State.Boot.PostCode0",
3882 "/xyz/openbmc_project/State/Boot/PostCode0",
3883 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3884 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003885}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003886
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003887/**
3888 * @brief Parse post code ID and get the current value and index value
3889 * eg: postCodeID=B1-2, currentValue=1, index=2
3890 *
3891 * @param[in] postCodeID Post Code ID
3892 * @param[out] currentValue Current value
3893 * @param[out] index Index value
3894 *
3895 * @return bool true if the parsing is successful, false the parsing fails
3896 */
3897inline static bool parsePostCode(const std::string& postCodeID,
3898 uint64_t& currentValue, uint16_t& index)
3899{
3900 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003901 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003902 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3903 {
3904 return false;
3905 }
3906
Patrick Williams84396af2023-05-11 11:47:45 -05003907 auto start = std::next(split[0].begin());
3908 auto end = split[0].end();
3909 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003910
Patrick Williams84396af2023-05-11 11:47:45 -05003911 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003912 {
3913 return false;
3914 }
3915
Patrick Williams84396af2023-05-11 11:47:45 -05003916 start = split[1].begin();
3917 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003918
Patrick Williams84396af2023-05-11 11:47:45 -05003919 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003920
Patrick Williams84396af2023-05-11 11:47:45 -05003921 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003922}
3923
3924static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003925 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303926 const boost::container::flat_map<
3927 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003928 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3929 const uint64_t skip = 0, const uint64_t top = 0)
3930{
3931 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003932 const registries::Message* message =
3933 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003934
3935 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003936 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303937 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3938 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003939 {
3940 currentCodeIndex++;
3941 std::string postcodeEntryID =
3942 "B" + std::to_string(bootIndex) + "-" +
3943 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3944
3945 uint64_t usecSinceEpoch = code.first;
3946 uint64_t usTimeOffset = 0;
3947
3948 if (1 == currentCodeIndex)
3949 { // already incremented
3950 firstCodeTimeUs = code.first;
3951 }
3952 else
3953 {
3954 usTimeOffset = code.first - firstCodeTimeUs;
3955 }
3956
3957 // skip if no specific codeIndex is specified and currentCodeIndex does
3958 // not fall between top and skip
3959 if ((codeIndex == 0) &&
3960 (currentCodeIndex <= skip || currentCodeIndex > top))
3961 {
3962 continue;
3963 }
3964
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003965 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003966 // currentIndex
3967 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3968 {
3969 // This is done for simplicity. 1st entry is needed to calculate
3970 // time offset. To improve efficiency, one can get to the entry
3971 // directly (possibly with flatmap's nth method)
3972 continue;
3973 }
3974
3975 // currentCodeIndex is within top and skip or equal to specified code
3976 // index
3977
3978 // Get the Created time from the timestamp
3979 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003980 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003981
3982 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3983 std::ostringstream hexCode;
3984 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303985 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003986 std::ostringstream timeOffsetStr;
3987 // Set Fixed -Point Notation
3988 timeOffsetStr << std::fixed;
3989 // Set precision to 4 digits
3990 timeOffsetStr << std::setprecision(4);
3991 // Add double to stream
3992 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003993
Ed Tanous1e6deaf2022-02-17 11:32:37 -08003994 std::string bootIndexStr = std::to_string(bootIndex);
3995 std::string timeOffsetString = timeOffsetStr.str();
3996 std::string hexCodeStr = hexCode.str();
3997
3998 std::array<std::string_view, 3> messageArgs = {
3999 bootIndexStr, timeOffsetString, hexCodeStr};
4000
4001 std::string msg =
4002 redfish::registries::fillMessageArgs(messageArgs, message->message);
4003 if (msg.empty())
ZhikuiRena3316fc2020-01-29 14:58:08 -08004004 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004005 messages::internalError(asyncResp->res);
4006 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004007 }
4008
Tim Leed4342a92020-04-27 11:47:58 +08004009 // Get Severity template from message registry
4010 std::string severity;
4011 if (message != nullptr)
4012 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08004013 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08004014 }
4015
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004016 // Format entry
4017 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05004018 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07004019 bmcLogEntry["@odata.id"] = boost::urls::format(
4020 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
4021 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07004022 bmcLogEntry["Name"] = "POST Code Log Entry";
4023 bmcLogEntry["Id"] = postcodeEntryID;
4024 bmcLogEntry["Message"] = std::move(msg);
4025 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004026 bmcLogEntry["MessageArgs"] = messageArgs;
Jason M. Bills84afc482022-06-24 12:38:23 -07004027 bmcLogEntry["EntryType"] = "Event";
4028 bmcLogEntry["Severity"] = std::move(severity);
4029 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08004030 if (!std::get<std::vector<uint8_t>>(code.second).empty())
4031 {
4032 bmcLogEntry["AdditionalDataURI"] =
4033 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
4034 postcodeEntryID + "/attachment";
4035 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004036
4037 // codeIndex is only specified when querying single entry, return only
4038 // that entry in this case
4039 if (codeIndex != 0)
4040 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004041 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004042 return true;
4043 }
4044
Ed Tanousac106bf2023-06-07 09:24:59 -07004045 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05004046 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08004047 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004048
4049 // Return value is always false when querying multiple entries
4050 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004051}
4052
Ed Tanousac106bf2023-06-07 09:24:59 -07004053static void
4054 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4055 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004056{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004057 uint16_t bootIndex = 0;
4058 uint64_t codeIndex = 0;
4059 if (!parsePostCode(entryId, codeIndex, bootIndex))
4060 {
4061 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07004062 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004063 return;
4064 }
4065
4066 if (bootIndex == 0 || codeIndex == 0)
4067 {
4068 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07004069 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004070 return;
4071 }
4072
ZhikuiRena3316fc2020-01-29 14:58:08 -08004073 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004074 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004075 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304076 const boost::container::flat_map<
4077 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4078 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004079 if (ec)
4080 {
Ed Tanous62598e32023-07-17 17:06:25 -07004081 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004082 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004083 return;
4084 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004085
Ed Tanous002d39b2022-05-31 08:59:27 -07004086 if (postcode.empty())
4087 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004088 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07004089 return;
4090 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004091
Ed Tanousac106bf2023-06-07 09:24:59 -07004092 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004093 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004094 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004095 return;
4096 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004097 },
Jonathan Doman15124762021-01-07 17:54:17 -08004098 "xyz.openbmc_project.State.Boot.PostCode0",
4099 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004100 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4101 bootIndex);
4102}
4103
Ed Tanousac106bf2023-06-07 09:24:59 -07004104static void
4105 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4106 const uint16_t bootIndex, const uint16_t bootCount,
4107 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004108{
4109 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004110 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004111 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304112 const boost::container::flat_map<
4113 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4114 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004115 if (ec)
4116 {
Ed Tanous62598e32023-07-17 17:06:25 -07004117 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004118 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004119 return;
4120 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004121
Ed Tanous002d39b2022-05-31 08:59:27 -07004122 uint64_t endCount = entryCount;
4123 if (!postcode.empty())
4124 {
4125 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07004126 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004127 {
Patrick Williams89492a12023-05-10 07:51:34 -05004128 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
4129 entryCount) -
4130 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004131 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07004132 std::min(static_cast<uint64_t>(top + skip), endCount) -
4133 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004134
Ed Tanousac106bf2023-06-07 09:24:59 -07004135 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4136 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004137 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004138 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004139 }
4140
4141 // continue to previous bootIndex
4142 if (bootIndex < bootCount)
4143 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004144 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07004145 bootCount, endCount, skip, top);
4146 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08004147 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07004148 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004149 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07004150 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4151 std::to_string(skip + top);
4152 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004153 },
Jonathan Doman15124762021-01-07 17:54:17 -08004154 "xyz.openbmc_project.State.Boot.PostCode0",
4155 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004156 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4157 bootIndex);
4158}
4159
zhanghch058d1b46d2021-04-01 11:18:24 +08004160static void
Ed Tanousac106bf2023-06-07 09:24:59 -07004161 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07004162 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004163{
4164 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004165 sdbusplus::asio::getProperty<uint16_t>(
4166 *crow::connections::systemBus,
4167 "xyz.openbmc_project.State.Boot.PostCode0",
4168 "/xyz/openbmc_project/State/Boot/PostCode0",
4169 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07004170 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
4171 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004172 if (ec)
4173 {
Ed Tanous62598e32023-07-17 17:06:25 -07004174 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -07004175 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004176 return;
4177 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004178 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004179 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08004180}
4181
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004182inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004183{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004184 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004185 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07004186 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004187 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004188 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07004189 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4190 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004191 query_param::QueryCapabilities capabilities = {
4192 .canDelegateTop = true,
4193 .canDelegateSkip = true,
4194 };
4195 query_param::Query delegatedQuery;
4196 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00004197 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07004198 {
4199 return;
4200 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004201 if constexpr (bmcwebEnableMultiHost)
4202 {
4203 // Option currently returns no systems. TBD
4204 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4205 systemName);
4206 return;
4207 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004208
4209 if (systemName != "system")
4210 {
4211 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4212 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004213 return;
4214 }
Ed Tanous002d39b2022-05-31 08:59:27 -07004215 asyncResp->res.jsonValue["@odata.type"] =
4216 "#LogEntryCollection.LogEntryCollection";
4217 asyncResp->res.jsonValue["@odata.id"] =
4218 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4219 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4220 asyncResp->res.jsonValue["Description"] =
4221 "Collection of POST Code Log Entries";
4222 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4223 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07004224 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08004225 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07004226 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07004227 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004228}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004229
George Liu647b3cd2021-07-05 12:43:56 +08004230inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4231{
George Liu0fda0f12021-11-16 10:06:17 +08004232 BMCWEB_ROUTE(
4233 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004234 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004235 .privileges(redfish::privileges::getLogEntry)
4236 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004237 [&app](const crow::Request& req,
4238 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004239 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004240 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004241 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004242 {
4243 return;
4244 }
Matt Spinler72e21372023-04-19 12:53:33 -05004245 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004246 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004247 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004248 {
4249 asyncResp->res.result(boost::beast::http::status::bad_request);
4250 return;
4251 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004252 if constexpr (bmcwebEnableMultiHost)
4253 {
4254 // Option currently returns no systems. TBD
4255 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4256 systemName);
4257 return;
4258 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004259 if (systemName != "system")
4260 {
4261 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4262 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004263 return;
4264 }
George Liu647b3cd2021-07-05 12:43:56 +08004265
Ed Tanous002d39b2022-05-31 08:59:27 -07004266 uint64_t currentValue = 0;
4267 uint16_t index = 0;
4268 if (!parsePostCode(postCodeID, currentValue, index))
4269 {
4270 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4271 return;
4272 }
George Liu647b3cd2021-07-05 12:43:56 +08004273
Ed Tanous002d39b2022-05-31 08:59:27 -07004274 crow::connections::systemBus->async_method_call(
4275 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004276 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004277 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4278 postcodes) {
4279 if (ec.value() == EBADR)
4280 {
4281 messages::resourceNotFound(asyncResp->res, "LogEntry",
4282 postCodeID);
4283 return;
4284 }
4285 if (ec)
4286 {
Ed Tanous62598e32023-07-17 17:06:25 -07004287 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07004288 messages::internalError(asyncResp->res);
4289 return;
4290 }
George Liu647b3cd2021-07-05 12:43:56 +08004291
Ed Tanous002d39b2022-05-31 08:59:27 -07004292 size_t value = static_cast<size_t>(currentValue) - 1;
4293 if (value == std::string::npos || postcodes.size() < currentValue)
4294 {
Ed Tanous62598e32023-07-17 17:06:25 -07004295 BMCWEB_LOG_WARNING("Wrong currentValue value");
Ed Tanous002d39b2022-05-31 08:59:27 -07004296 messages::resourceNotFound(asyncResp->res, "LogEntry",
4297 postCodeID);
4298 return;
4299 }
George Liu647b3cd2021-07-05 12:43:56 +08004300
Ed Tanous002d39b2022-05-31 08:59:27 -07004301 const auto& [tID, c] = postcodes[value];
4302 if (c.empty())
4303 {
Ed Tanous62598e32023-07-17 17:06:25 -07004304 BMCWEB_LOG_WARNING("No found post code data");
Ed Tanous002d39b2022-05-31 08:59:27 -07004305 messages::resourceNotFound(asyncResp->res, "LogEntry",
4306 postCodeID);
4307 return;
4308 }
4309 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4310 const char* d = reinterpret_cast<const char*>(c.data());
4311 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004312
Ed Tanousd9f6c622022-03-17 09:12:17 -07004313 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004314 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004315 asyncResp->res.addHeader(
4316 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004317 asyncResp->res.body() = crow::utility::base64encode(strData);
4318 },
4319 "xyz.openbmc_project.State.Boot.PostCode0",
4320 "/xyz/openbmc_project/State/Boot/PostCode0",
4321 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4322 });
George Liu647b3cd2021-07-05 12:43:56 +08004323}
4324
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004325inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004326{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004327 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004328 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004329 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004330 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004331 [&app](const crow::Request& req,
4332 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004333 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004334 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004335 {
4336 return;
4337 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004338 if constexpr (bmcwebEnableMultiHost)
4339 {
4340 // Option currently returns no systems. TBD
4341 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4342 systemName);
4343 return;
4344 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004345 if (systemName != "system")
4346 {
4347 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4348 systemName);
4349 return;
4350 }
4351
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004352 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004353 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004354}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004355
Ed Tanous1da66f72018-07-27 16:13:37 -07004356} // namespace redfish