blob: e6341911516e99c950c15c94cdb1be5395b05780 [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
Myung Bae75e8e212023-11-30 12:53:46 -080034#include <systemd/sd-id128.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070035#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060036#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060037#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070038
Ed Tanous9896eae2022-07-23 15:07:33 -070039#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070040#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060041#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070042#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070043#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070044#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080045#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070046#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020047#include <sdbusplus/asio/property.hpp>
48#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050049
George Liu7a1dbc42022-12-07 16:03:22 +080050#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080051#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070052#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080053#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070054#include <ranges>
Ed Tanous26702d02021-11-03 15:02:33 -070055#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070056#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080057#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070058
59namespace redfish
60{
61
Patrick Williams89492a12023-05-10 07:51:34 -050062constexpr const char* crashdumpObject = "com.intel.crashdump";
63constexpr const char* crashdumpPath = "/com/intel/crashdump";
64constexpr const char* crashdumpInterface = "com.intel.crashdump";
65constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070066 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050067constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070068 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050069constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070070 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070071
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060072enum class DumpCreationProgress
73{
74 DUMP_CREATE_SUCCESS,
75 DUMP_CREATE_FAILED,
76 DUMP_CREATE_INPROGRESS
77};
78
James Feistf6150402019-01-08 10:36:20 -080079namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070080
Gunnar Mills1214b7e2020-06-04 10:11:30 -050081inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070082{
Ed Tanousd4d25792020-09-29 15:15:03 -070083 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
84 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
85 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
86 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070087 {
88 return "Critical";
89 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070090 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
91 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
92 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070093 {
94 return "OK";
95 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070096 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070097 {
98 return "Warning";
99 }
100 return "";
101}
102
Abhishek Patel9017faf2021-09-14 22:48:55 -0500103inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
104{
105 std::optional<bool> notifyAction;
106 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
107 {
108 notifyAction = true;
109 }
110 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
111 {
112 notifyAction = false;
113 }
114
115 return notifyAction;
116}
117
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700118inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800119 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700120 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700121{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500122 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700123 size_t length = 0;
124 int ret = 0;
125 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800126 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
127 const void** dataVoid = reinterpret_cast<const void**>(&data);
128
129 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700130 if (ret < 0)
131 {
132 return ret;
133 }
Ed Tanous39e77502019-03-04 17:35:53 -0800134 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700135 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000136 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700137 return ret;
138}
139
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700140inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800141 std::string_view field, const int& base,
142 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700143{
144 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800145 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700146 // Get the metadata from the requested field of the journal entry
147 ret = getJournalMetadata(journal, field, metadata);
148 if (ret < 0)
149 {
150 return ret;
151 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000152 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700153 return ret;
154}
155
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700156inline static bool getEntryTimestamp(sd_journal* journal,
157 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800158{
159 int ret = 0;
160 uint64_t timestamp = 0;
161 ret = sd_journal_get_realtime_usec(journal, &timestamp);
162 if (ret < 0)
163 {
Ed Tanous62598e32023-07-17 17:06:25 -0700164 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
ZhikuiRena3316fc2020-01-29 14:58:08 -0800165 return false;
166 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300167 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500168 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800169}
Ed Tanous50b8a432022-02-03 16:29:50 -0800170
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700171inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
172 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700173{
174 int ret = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800175 static sd_id128_t prevBootID{};
Jason M. Bills16428a12018-11-02 12:42:29 -0700176 static uint64_t prevTs = 0;
177 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700178 if (firstEntry)
179 {
Myung Bae75e8e212023-11-30 12:53:46 -0800180 prevBootID = {};
Jason M. Billse85d6b12019-07-29 17:01:15 -0700181 prevTs = 0;
182 }
183
Jason M. Bills16428a12018-11-02 12:42:29 -0700184 // Get the entry timestamp
185 uint64_t curTs = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800186 sd_id128_t curBootID{};
187 ret = sd_journal_get_monotonic_usec(journal, &curTs, &curBootID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700188 if (ret < 0)
189 {
Ed Tanous62598e32023-07-17 17:06:25 -0700190 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
Jason M. Bills16428a12018-11-02 12:42:29 -0700191 return false;
192 }
Myung Bae75e8e212023-11-30 12:53:46 -0800193 // If the timestamp isn't unique on the same boot, increment the index
194 bool sameBootIDs = sd_id128_equal(curBootID, prevBootID) != 0;
195 if (sameBootIDs && (curTs == prevTs))
Jason M. Bills16428a12018-11-02 12:42:29 -0700196 {
197 index++;
198 }
199 else
200 {
201 // Otherwise, reset it
202 index = 0;
203 }
Myung Bae75e8e212023-11-30 12:53:46 -0800204
205 if (!sameBootIDs)
206 {
207 // Save the bootID
208 prevBootID = curBootID;
209 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700210 // Save the timestamp
211 prevTs = curTs;
212
Myung Bae75e8e212023-11-30 12:53:46 -0800213 // make entryID as <bootID>_<timestamp>[_<index>]
214 std::array<char, SD_ID128_STRING_MAX> bootIDStr{};
215 sd_id128_to_string(curBootID, bootIDStr.data());
216 entryID = std::format("{}_{}", bootIDStr.data(), curTs);
Jason M. Bills16428a12018-11-02 12:42:29 -0700217 if (index > 0)
218 {
219 entryID += "_" + std::to_string(index);
220 }
221 return true;
222}
223
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500224static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700225 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700226{
Ed Tanous271584a2019-07-09 16:24:22 -0700227 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700228 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700229 if (firstEntry)
230 {
231 prevTs = 0;
232 }
233
Jason M. Bills95820182019-04-22 16:25:34 -0700234 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700235 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700236 std::tm timeStruct = {};
237 std::istringstream entryStream(logEntry);
238 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
239 {
240 curTs = std::mktime(&timeStruct);
241 }
242 // If the timestamp isn't unique, increment the index
243 if (curTs == prevTs)
244 {
245 index++;
246 }
247 else
248 {
249 // Otherwise, reset it
250 index = 0;
251 }
252 // Save the timestamp
253 prevTs = curTs;
254
255 entryID = std::to_string(curTs);
256 if (index > 0)
257 {
258 entryID += "_" + std::to_string(index);
259 }
260 return true;
261}
262
Myung Bae75e8e212023-11-30 12:53:46 -0800263// Entry is formed like "BootID_timestamp" or "BootID_timestamp_index"
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700264inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800265 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Myung Bae75e8e212023-11-30 12:53:46 -0800266 const std::string& entryID, sd_id128_t& bootID,
267 uint64_t& timestamp, uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700268{
269 if (entryID.empty())
270 {
271 return false;
272 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700273
Myung Bae75e8e212023-11-30 12:53:46 -0800274 // Convert the unique ID back to a bootID + timestamp to find the entry
275 std::string_view entryIDStrView(entryID);
276 auto underscore1Pos = entryIDStrView.find('_');
277 if (underscore1Pos == std::string_view::npos)
278 {
279 // EntryID has no bootID or timestamp
280 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
281 return false;
282 }
283
284 // EntryID has bootID + timestamp
285
286 // Convert entryIDViewString to BootID
287 // NOTE: bootID string which needs to be null-terminated for
288 // sd_id128_from_string()
289 std::string bootIDStr(entryID, 0, underscore1Pos);
290 if (sd_id128_from_string(bootIDStr.c_str(), &bootID) < 0)
291 {
292 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
293 return false;
294 }
295
296 // Get the timestamp from entryID
297 std::string_view timestampStrView = entryIDStrView;
298 timestampStrView.remove_prefix(underscore1Pos + 1);
299
300 // Check the index in timestamp
301 auto underscore2Pos = timestampStrView.find('_');
302 if (underscore2Pos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700303 {
304 // Timestamp has an index
Myung Bae75e8e212023-11-30 12:53:46 -0800305 timestampStrView.remove_suffix(timestampStrView.size() -
306 underscore2Pos);
307 std::string_view indexStr(timestampStrView);
308 indexStr.remove_prefix(underscore2Pos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500309 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
310 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700311 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700312 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800313 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700314 return false;
315 }
316 }
Myung Bae75e8e212023-11-30 12:53:46 -0800317
318 // Now timestamp has no index
319 auto [ptr, ec] = std::from_chars(timestampStrView.begin(),
320 timestampStrView.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700321 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700322 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800323 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700324 return false;
325 }
326 return true;
327}
328
Jason M. Bills95820182019-04-22 16:25:34 -0700329static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500330 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700331{
332 static const std::filesystem::path redfishLogDir = "/var/log";
333 static const std::string redfishLogFilename = "redfish";
334
335 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500336 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700337 std::filesystem::directory_iterator(redfishLogDir))
338 {
339 // If we find a redfish log file, save the path
340 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700341 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700342 {
343 redfishLogFiles.emplace_back(redfishLogDir / filename);
344 }
345 }
346 // As the log files rotate, they are appended with a ".#" that is higher for
347 // the older logs. Since we don't expect more than 10 log files, we
348 // can just sort the list to get them in order from newest to oldest
Ed Tanous3544d2a2023-08-06 18:12:20 -0700349 std::ranges::sort(redfishLogFiles);
Jason M. Bills95820182019-04-22 16:25:34 -0700350
351 return !redfishLogFiles.empty();
352}
353
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600354inline log_entry::OriginatorTypes
355 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
356{
357 if (originatorType ==
358 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
359 {
360 return log_entry::OriginatorTypes::Client;
361 }
362 if (originatorType ==
363 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
364 {
365 return log_entry::OriginatorTypes::Internal;
366 }
367 if (originatorType ==
368 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
369 {
370 return log_entry::OriginatorTypes::SupportingService;
371 }
372 return log_entry::OriginatorTypes::Invalid;
373}
374
Claire Weinanaefe3782022-07-15 19:17:19 -0700375inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800376 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700377 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600378 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700379 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
380{
381 for (const auto& interfaceMap : object.second)
382 {
383 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
384 {
385 for (const auto& propertyMap : interfaceMap.second)
386 {
387 if (propertyMap.first == "Status")
388 {
389 const auto* status =
390 std::get_if<std::string>(&propertyMap.second);
391 if (status == nullptr)
392 {
393 messages::internalError(asyncResp->res);
394 break;
395 }
396 dumpStatus = *status;
397 }
398 }
399 }
400 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
401 {
402 for (const auto& propertyMap : interfaceMap.second)
403 {
404 if (propertyMap.first == "Size")
405 {
406 const auto* sizePtr =
407 std::get_if<uint64_t>(&propertyMap.second);
408 if (sizePtr == nullptr)
409 {
410 messages::internalError(asyncResp->res);
411 break;
412 }
413 size = *sizePtr;
414 break;
415 }
416 }
417 }
418 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
419 {
420 for (const auto& propertyMap : interfaceMap.second)
421 {
422 if (propertyMap.first == "Elapsed")
423 {
424 const uint64_t* usecsTimeStamp =
425 std::get_if<uint64_t>(&propertyMap.second);
426 if (usecsTimeStamp == nullptr)
427 {
428 messages::internalError(asyncResp->res);
429 break;
430 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700431 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700432 break;
433 }
434 }
435 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600436 else if (interfaceMap.first ==
437 "xyz.openbmc_project.Common.OriginatedBy")
438 {
439 for (const auto& propertyMap : interfaceMap.second)
440 {
441 if (propertyMap.first == "OriginatorId")
442 {
443 const std::string* id =
444 std::get_if<std::string>(&propertyMap.second);
445 if (id == nullptr)
446 {
447 messages::internalError(asyncResp->res);
448 break;
449 }
450 originatorId = *id;
451 }
452
453 if (propertyMap.first == "OriginatorType")
454 {
455 const std::string* type =
456 std::get_if<std::string>(&propertyMap.second);
457 if (type == nullptr)
458 {
459 messages::internalError(asyncResp->res);
460 break;
461 }
462
463 originatorType = mapDbusOriginatorTypeToRedfish(*type);
464 if (originatorType == log_entry::OriginatorTypes::Invalid)
465 {
466 messages::internalError(asyncResp->res);
467 break;
468 }
469 }
470 }
471 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700472 }
473}
474
Nan Zhou21ab4042022-06-26 23:07:40 +0000475static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800476{
477 std::string entriesPath;
478
479 if (dumpType == "BMC")
480 {
481 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
482 }
483 else if (dumpType == "FaultLog")
484 {
485 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
486 }
487 else if (dumpType == "System")
488 {
489 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
490 }
491 else
492 {
Ed Tanous62598e32023-07-17 17:06:25 -0700493 BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
494 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -0800495 }
496
497 // Returns empty string on error
498 return entriesPath;
499}
500
zhanghch058d1b46d2021-04-01 11:18:24 +0800501inline void
502 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
503 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500504{
Claire Weinanfdd26902022-03-01 14:18:25 -0800505 std::string entriesPath = getDumpEntriesPath(dumpType);
506 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500507 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500508 messages::internalError(asyncResp->res);
509 return;
510 }
511
George Liu5eb468d2023-06-20 17:03:24 +0800512 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
513 dbus::utility::getManagedObjects(
514 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800515 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800516 dumpType](const boost::system::error_code& ec,
George Liu5eb468d2023-06-20 17:03:24 +0800517 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700518 if (ec)
519 {
Ed Tanous62598e32023-07-17 17:06:25 -0700520 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700521 messages::internalError(asyncResp->res);
522 return;
523 }
524
Claire Weinanfdd26902022-03-01 14:18:25 -0800525 // Remove ending slash
526 std::string odataIdStr = entriesPath;
527 if (!odataIdStr.empty())
528 {
529 odataIdStr.pop_back();
530 }
531
532 asyncResp->res.jsonValue["@odata.type"] =
533 "#LogEntryCollection.LogEntryCollection";
534 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
535 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500536 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
537 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800538
Ed Tanous3544d2a2023-08-06 18:12:20 -0700539 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -0700540 std::string dumpEntryPath =
541 "/xyz/openbmc_project/dump/" +
542 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
543
George Liu5eb468d2023-06-20 17:03:24 +0800544 dbus::utility::ManagedObjectType resp(objects);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700545 std::ranges::sort(resp, [](const auto& l, const auto& r) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700546 return AlphanumLess<std::string>()(l.first.filename(),
547 r.first.filename());
548 });
549
550 for (auto& object : resp)
551 {
552 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500553 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700554 continue;
555 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700556 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700557 uint64_t size = 0;
558 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600559 std::string originatorId;
560 log_entry::OriginatorTypes originatorType =
561 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700562 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700563
564 std::string entryID = object.first.filename();
565 if (entryID.empty())
566 {
567 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500568 }
569
Claire Weinanc6fecda2022-07-15 10:43:25 -0700570 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600571 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700572 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700573
574 if (dumpStatus !=
575 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
576 !dumpStatus.empty())
577 {
578 // Dump status is not Complete, no need to enumerate
579 continue;
580 }
581
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600582 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800583 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700584 thisEntry["Id"] = entryID;
585 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700587 thisEntry["Created"] =
588 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700589
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600590 if (!originatorId.empty())
591 {
592 thisEntry["Originator"] = originatorId;
593 thisEntry["OriginatorType"] = originatorType;
594 }
595
Ed Tanous002d39b2022-05-31 08:59:27 -0700596 if (dumpType == "BMC")
597 {
598 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500599 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
600 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800601 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700602 }
603 else if (dumpType == "System")
604 {
605 thisEntry["DiagnosticDataType"] = "OEM";
606 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500607 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
608 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800609 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700610 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500611 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 }
613 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -0700614 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
Patrick Williams5a39f772023-10-20 11:20:21 -0500615 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500616}
617
zhanghch058d1b46d2021-04-01 11:18:24 +0800618inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700619 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800620 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500621{
Claire Weinanfdd26902022-03-01 14:18:25 -0800622 std::string entriesPath = getDumpEntriesPath(dumpType);
623 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500624 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500625 messages::internalError(asyncResp->res);
626 return;
627 }
628
George Liu5eb468d2023-06-20 17:03:24 +0800629 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
630 dbus::utility::getManagedObjects(
631 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800632 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800633 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700634 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700635 if (ec)
636 {
Ed Tanous62598e32023-07-17 17:06:25 -0700637 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700638 messages::internalError(asyncResp->res);
639 return;
640 }
641
642 bool foundDumpEntry = false;
643 std::string dumpEntryPath =
644 "/xyz/openbmc_project/dump/" +
645 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
646
647 for (const auto& objectPath : resp)
648 {
649 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500650 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700651 continue;
652 }
653
654 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700655 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 uint64_t size = 0;
657 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600658 std::string originatorId;
659 log_entry::OriginatorTypes originatorType =
660 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700661
Claire Weinanaefe3782022-07-15 19:17:19 -0700662 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600663 timestampUs, originatorId,
664 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700665
666 if (dumpStatus !=
667 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
668 !dumpStatus.empty())
669 {
670 // Dump status is not Complete
671 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200672 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
673 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500674 return;
675 }
676
Ed Tanous002d39b2022-05-31 08:59:27 -0700677 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600678 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800679 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 asyncResp->res.jsonValue["Id"] = entryID;
681 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700682 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700683 asyncResp->res.jsonValue["Created"] =
684 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500685
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600686 if (!originatorId.empty())
687 {
688 asyncResp->res.jsonValue["Originator"] = originatorId;
689 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
690 }
691
Ed Tanous002d39b2022-05-31 08:59:27 -0700692 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500693 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700694 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
695 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800696 entriesPath + entryID + "/attachment";
697 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500698 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700699 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500700 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700701 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
702 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
703 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800704 entriesPath + entryID + "/attachment";
705 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500706 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700707 }
708 if (!foundDumpEntry)
709 {
Ed Tanous62598e32023-07-17 17:06:25 -0700710 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
Myung Baeb90d14f2023-05-31 14:40:39 -0500711 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
712 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700713 return;
714 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500715 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500716}
717
zhanghch058d1b46d2021-04-01 11:18:24 +0800718inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800719 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500720 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500721{
Patrick Williams5a39f772023-10-20 11:20:21 -0500722 auto respHandler = [asyncResp,
723 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -0700724 BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500725 if (ec)
726 {
George Liu3de8d8b2021-03-22 17:49:39 +0800727 if (ec.value() == EBADR)
728 {
729 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
730 return;
731 }
Ed Tanous62598e32023-07-17 17:06:25 -0700732 BMCWEB_LOG_ERROR(
733 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
734 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500735 messages::internalError(asyncResp->res);
736 return;
737 }
738 };
739 crow::connections::systemBus->async_method_call(
740 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500741 "/xyz/openbmc_project/dump/" +
742 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
743 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500744 "xyz.openbmc_project.Object.Delete", "Delete");
745}
746
Carson Labrado168d1b12023-03-27 17:04:46 +0000747inline void
748 downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
749 const std::string& entryID,
750 const std::string& downloadEntryType,
751 const boost::system::error_code& ec,
752 const sdbusplus::message::unix_fd& unixfd)
753{
754 if (ec.value() == EBADR)
755 {
756 messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
757 return;
758 }
759 if (ec)
760 {
761 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
762 messages::internalError(asyncResp->res);
763 return;
764 }
765
766 // Make sure we know how to process the retrieved entry attachment
767 if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
768 {
769 BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
770 downloadEntryType);
771 messages::internalError(asyncResp->res);
772 }
773
774 int fd = -1;
775 fd = dup(unixfd);
776 if (fd < 0)
777 {
778 BMCWEB_LOG_ERROR("Failed to open file");
779 messages::internalError(asyncResp->res);
780 return;
781 }
782
783 long long int size = lseek(fd, 0, SEEK_END);
784 if (size <= 0)
785 {
786 BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
787 size);
788 messages::internalError(asyncResp->res);
789 close(fd);
790 return;
791 }
792
793 // Arbitrary max size of 20MB to accommodate BMC dumps
794 constexpr int maxFileSize = 20 * 1024 * 1024;
795 if (size > maxFileSize)
796 {
797 BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
798 size, maxFileSize);
799 messages::internalError(asyncResp->res);
800 close(fd);
801 return;
802 }
803 long long int rc = lseek(fd, 0, SEEK_SET);
804 if (rc < 0)
805 {
806 BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
807 messages::internalError(asyncResp->res);
808 close(fd);
809 return;
810 }
811
Ed Tanous27b0cf92023-08-07 12:02:40 -0700812 std::string body;
813 body.resize(static_cast<size_t>(size), '\0');
814 rc = read(fd, body.data(), body.size());
Carson Labrado168d1b12023-03-27 17:04:46 +0000815 if ((rc == -1) || (rc != size))
816 {
817 BMCWEB_LOG_ERROR("Failed to read in file");
818 messages::internalError(asyncResp->res);
819 close(fd);
820 return;
821 }
822 close(fd);
Carson Labrado168d1b12023-03-27 17:04:46 +0000823 if (downloadEntryType == "System")
824 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700825 // Base64 encode response.
826 asyncResp->res.write(crow::utility::base64encode(body));
Carson Labrado168d1b12023-03-27 17:04:46 +0000827 asyncResp->res.addHeader(
828 boost::beast::http::field::content_transfer_encoding, "Base64");
829 }
Ed Tanous27b0cf92023-08-07 12:02:40 -0700830 else
831 {
832 asyncResp->res.write(std::move(body));
833 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000834
835 asyncResp->res.addHeader(boost::beast::http::field::content_type,
836 "application/octet-stream");
837}
838
839inline void
840 downloadDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
841 const std::string& entryID, const std::string& dumpType)
842{
843 if (dumpType != "BMC")
844 {
845 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
846 messages::resourceNotFound(asyncResp->res, dumpType + " dump", entryID);
847 return;
848 }
849
850 std::string dumpEntryPath =
851 sdbusplus::message::object_path("/xyz/openbmc_project/dump/") /
852 std::string(boost::algorithm::to_lower_copy(dumpType)) / "entry" /
853 entryID;
854
855 auto downloadDumpEntryHandler =
856 [asyncResp, entryID,
857 dumpType](const boost::system::error_code& ec,
858 const sdbusplus::message::unix_fd& unixfd) {
859 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
860 };
861
862 crow::connections::systemBus->async_method_call(
863 std::move(downloadDumpEntryHandler), "xyz.openbmc_project.Dump.Manager",
864 dumpEntryPath, "xyz.openbmc_project.Dump.Entry", "GetFileHandle");
865}
866
867inline void
868 downloadEventLogEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
869 const std::string& systemName,
870 const std::string& entryID,
871 const std::string& dumpType)
872{
873 if constexpr (bmcwebEnableMultiHost)
874 {
875 // Option currently returns no systems. TBD
876 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
877 systemName);
878 return;
879 }
880 if (systemName != "system")
881 {
882 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
883 systemName);
884 return;
885 }
886
887 std::string entryPath =
888 sdbusplus::message::object_path("/xyz/openbmc_project/logging/entry") /
889 entryID;
890
891 auto downloadEventLogEntryHandler =
892 [asyncResp, entryID,
893 dumpType](const boost::system::error_code& ec,
894 const sdbusplus::message::unix_fd& unixfd) {
895 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
896 };
897
898 crow::connections::systemBus->async_method_call(
899 std::move(downloadEventLogEntryHandler), "xyz.openbmc_project.Logging",
900 entryPath, "xyz.openbmc_project.Logging.Entry", "GetEntry");
901}
902
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600903inline DumpCreationProgress
904 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500905{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600906 if (status ==
907 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
908 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
909 {
910 return DumpCreationProgress::DUMP_CREATE_FAILED;
911 }
912 if (status ==
913 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
914 {
915 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
916 }
917 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
918}
919
920inline DumpCreationProgress
921 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
922{
923 for (const auto& [key, val] : values)
924 {
925 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700926 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600927 const std::string* value = std::get_if<std::string>(&val);
928 if (value == nullptr)
929 {
Ed Tanous62598e32023-07-17 17:06:25 -0700930 BMCWEB_LOG_ERROR("Status property value is null");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600931 return DumpCreationProgress::DUMP_CREATE_FAILED;
932 }
933 return mapDbusStatusToDumpProgress(*value);
934 }
935 }
936 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
937}
938
939inline std::string getDumpEntryPath(const std::string& dumpPath)
940{
941 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
942 {
943 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
944 }
945 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
946 {
947 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
948 }
949 return "";
950}
951
952inline void createDumpTaskCallback(
953 task::Payload&& payload,
954 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
955 const sdbusplus::message::object_path& createdObjPath)
956{
957 const std::string dumpPath = createdObjPath.parent_path().str;
958 const std::string dumpId = createdObjPath.filename();
959
960 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
961
962 if (dumpEntryPath.empty())
963 {
Ed Tanous62598e32023-07-17 17:06:25 -0700964 BMCWEB_LOG_ERROR("Invalid dump type received");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600965 messages::internalError(asyncResp->res);
966 return;
967 }
968
969 crow::connections::systemBus->async_method_call(
970 [asyncResp, payload, createdObjPath,
971 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800972 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600973 const std::string& introspectXml) {
974 if (ec)
975 {
Ed Tanous62598e32023-07-17 17:06:25 -0700976 BMCWEB_LOG_ERROR("Introspect call failed with error: {}",
977 ec.message());
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600978 messages::internalError(asyncResp->res);
979 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700980 }
981
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600982 // Check if the created dump object has implemented Progress
983 // interface to track dump completion. If yes, fetch the "Status"
984 // property of the interface, modify the task state accordingly.
985 // Else, return task completed.
986 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700987
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600988 doc.Parse(introspectXml.data(), introspectXml.size());
989 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
990 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700991 {
Ed Tanous62598e32023-07-17 17:06:25 -0700992 BMCWEB_LOG_ERROR("XML document failed to parse");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600993 messages::internalError(asyncResp->res);
994 return;
995 }
996 tinyxml2::XMLElement* interfaceNode =
997 pRoot->FirstChildElement("interface");
998
999 bool isProgressIntfPresent = false;
1000 while (interfaceNode != nullptr)
1001 {
1002 const char* thisInterfaceName = interfaceNode->Attribute("name");
1003 if (thisInterfaceName != nullptr)
1004 {
1005 if (thisInterfaceName ==
1006 std::string_view("xyz.openbmc_project.Common.Progress"))
1007 {
1008 interfaceNode =
1009 interfaceNode->NextSiblingElement("interface");
1010 continue;
1011 }
1012 isProgressIntfPresent = true;
1013 break;
1014 }
1015 interfaceNode = interfaceNode->NextSiblingElement("interface");
1016 }
1017
1018 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
1019 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous8b242752023-06-27 17:17:13 -07001020 const boost::system::error_code& ec2, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001021 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07001022 if (ec2)
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001023 {
Ed Tanous62598e32023-07-17 17:06:25 -07001024 BMCWEB_LOG_ERROR("{}: Error in creating dump",
1025 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001026 taskData->messages.emplace_back(messages::internalError());
1027 taskData->state = "Cancelled";
1028 return task::completed;
1029 }
1030
1031 if (isProgressIntfPresent)
1032 {
1033 dbus::utility::DBusPropertiesMap values;
1034 std::string prop;
1035 msg.read(prop, values);
1036
1037 DumpCreationProgress dumpStatus =
1038 getDumpCompletionStatus(values);
1039 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
1040 {
Ed Tanous62598e32023-07-17 17:06:25 -07001041 BMCWEB_LOG_ERROR("{}: Error in creating dump",
1042 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001043 taskData->state = "Cancelled";
1044 return task::completed;
1045 }
1046
1047 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
1048 {
Ed Tanous62598e32023-07-17 17:06:25 -07001049 BMCWEB_LOG_DEBUG("{}: Dump creation task is in progress",
1050 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001051 return !task::completed;
1052 }
1053 }
1054
Ed Tanous002d39b2022-05-31 08:59:27 -07001055 nlohmann::json retMessage = messages::success();
1056 taskData->messages.emplace_back(retMessage);
1057
Ed Tanousc51a58e2023-03-27 14:43:19 -07001058 boost::urls::url url = boost::urls::format(
1059 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
1060
1061 std::string headerLoc = "Location: ";
1062 headerLoc += url.buffer();
1063
Ed Tanous002d39b2022-05-31 08:59:27 -07001064 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
1065
Ed Tanous62598e32023-07-17 17:06:25 -07001066 BMCWEB_LOG_DEBUG("{}: Dump creation task completed",
1067 createdObjPath.str);
Ed Tanous002d39b2022-05-31 08:59:27 -07001068 taskData->state = "Completed";
1069 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05001070 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001071 "type='signal',interface='org.freedesktop.DBus.Properties',"
1072 "member='PropertiesChanged',path='" +
1073 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001074
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001075 // The task timer is set to max time limit within which the
1076 // requested dump will be collected.
1077 task->startTimer(std::chrono::minutes(6));
1078 task->populateResp(asyncResp->res);
1079 task->payload.emplace(payload);
Patrick Williams5a39f772023-10-20 11:20:21 -05001080 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001081 "xyz.openbmc_project.Dump.Manager", createdObjPath,
1082 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001083}
1084
zhanghch058d1b46d2021-04-01 11:18:24 +08001085inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1086 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001087{
Claire Weinanfdd26902022-03-01 14:18:25 -08001088 std::string dumpPath = getDumpEntriesPath(dumpType);
1089 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001090 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001091 messages::internalError(asyncResp->res);
1092 return;
1093 }
1094
1095 std::optional<std::string> diagnosticDataType;
1096 std::optional<std::string> oemDiagnosticDataType;
1097
Willy Tu15ed6782021-12-14 11:03:16 -08001098 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001099 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
1100 "OEMDiagnosticDataType", oemDiagnosticDataType))
1101 {
1102 return;
1103 }
1104
1105 if (dumpType == "System")
1106 {
1107 if (!oemDiagnosticDataType || !diagnosticDataType)
1108 {
Ed Tanous62598e32023-07-17 17:06:25 -07001109 BMCWEB_LOG_ERROR(
1110 "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001111 messages::actionParameterMissing(
1112 asyncResp->res, "CollectDiagnosticData",
1113 "DiagnosticDataType & OEMDiagnosticDataType");
1114 return;
1115 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001116 if ((*oemDiagnosticDataType != "System") ||
1117 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001118 {
Ed Tanous62598e32023-07-17 17:06:25 -07001119 BMCWEB_LOG_ERROR("Wrong parameter values passed");
Ed Tanousace85d62021-10-26 12:45:59 -07001120 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001121 return;
1122 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001123 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001124 }
1125 else if (dumpType == "BMC")
1126 {
1127 if (!diagnosticDataType)
1128 {
Ed Tanous62598e32023-07-17 17:06:25 -07001129 BMCWEB_LOG_ERROR(
1130 "CreateDump action parameter 'DiagnosticDataType' not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001131 messages::actionParameterMissing(
1132 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
1133 return;
1134 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001135 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001136 {
Ed Tanous62598e32023-07-17 17:06:25 -07001137 BMCWEB_LOG_ERROR(
1138 "Wrong parameter value passed for 'DiagnosticDataType'");
Ed Tanousace85d62021-10-26 12:45:59 -07001139 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001140 return;
1141 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001142 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
1143 }
1144 else
1145 {
Ed Tanous62598e32023-07-17 17:06:25 -07001146 BMCWEB_LOG_ERROR("CreateDump failed. Unknown dump type");
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001147 messages::internalError(asyncResp->res);
1148 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001149 }
1150
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001151 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
1152 createDumpParamVec;
1153
Carson Labradof574a8e2023-03-22 02:26:00 +00001154 if (req.session != nullptr)
1155 {
1156 createDumpParamVec.emplace_back(
1157 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
1158 req.session->clientIp);
1159 createDumpParamVec.emplace_back(
1160 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
1161 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
1162 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -06001163
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001164 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001165 [asyncResp, payload(task::Payload(req)),
1166 dumpPath](const boost::system::error_code& ec,
1167 const sdbusplus::message_t& msg,
1168 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07001169 if (ec)
1170 {
Ed Tanous62598e32023-07-17 17:06:25 -07001171 BMCWEB_LOG_ERROR("CreateDump resp_handler got error {}", ec);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001172 const sd_bus_error* dbusError = msg.get_error();
1173 if (dbusError == nullptr)
1174 {
1175 messages::internalError(asyncResp->res);
1176 return;
1177 }
1178
Ed Tanous62598e32023-07-17 17:06:25 -07001179 BMCWEB_LOG_ERROR("CreateDump DBus error: {} and error msg: {}",
1180 dbusError->name, dbusError->message);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001181 if (std::string_view(
1182 "xyz.openbmc_project.Common.Error.NotAllowed") ==
1183 dbusError->name)
1184 {
1185 messages::resourceInStandby(asyncResp->res);
1186 return;
1187 }
1188 if (std::string_view(
1189 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
1190 dbusError->name)
1191 {
1192 messages::serviceDisabled(asyncResp->res, dumpPath);
1193 return;
1194 }
1195 if (std::string_view(
1196 "xyz.openbmc_project.Common.Error.Unavailable") ==
1197 dbusError->name)
1198 {
1199 messages::resourceInUse(asyncResp->res);
1200 return;
1201 }
1202 // Other Dbus errors such as:
1203 // xyz.openbmc_project.Common.Error.InvalidArgument &
1204 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1205 // the dbus call that is made here in the bmcweb
1206 // implementation and has nothing to do with the client's
1207 // input in the request. Hence, returning internal error
1208 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001209 messages::internalError(asyncResp->res);
1210 return;
1211 }
Ed Tanous62598e32023-07-17 17:06:25 -07001212 BMCWEB_LOG_DEBUG("Dump Created. Path: {}", objPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001213 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Patrick Williams5a39f772023-10-20 11:20:21 -05001214 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001215 "xyz.openbmc_project.Dump.Manager",
1216 "/xyz/openbmc_project/dump/" +
1217 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001218 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001219}
1220
zhanghch058d1b46d2021-04-01 11:18:24 +08001221inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1222 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001223{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001224 std::string dumpTypeLowerCopy =
1225 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001226
Claire Weinan0d946212022-07-13 19:40:19 -07001227 crow::connections::systemBus->async_method_call(
1228 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001229 if (ec)
1230 {
Ed Tanous62598e32023-07-17 17:06:25 -07001231 BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001232 messages::internalError(asyncResp->res);
1233 return;
1234 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001235 },
Claire Weinan0d946212022-07-13 19:40:19 -07001236 "xyz.openbmc_project.Dump.Manager",
1237 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1238 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001239}
1240
Ed Tanousb9d36b42022-02-26 21:42:46 -08001241inline static void
1242 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1243 std::string& filename, std::string& timestamp,
1244 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001245{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001246 const std::string* filenamePtr = nullptr;
1247 const std::string* timestampPtr = nullptr;
1248 const std::string* logfilePtr = nullptr;
1249
1250 const bool success = sdbusplus::unpackPropertiesNoThrow(
1251 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1252 "Filename", filenamePtr, "Log", logfilePtr);
1253
1254 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001255 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001256 return;
1257 }
1258
1259 if (filenamePtr != nullptr)
1260 {
1261 filename = *filenamePtr;
1262 }
1263
1264 if (timestampPtr != nullptr)
1265 {
1266 timestamp = *timestampPtr;
1267 }
1268
1269 if (logfilePtr != nullptr)
1270 {
1271 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001272 }
1273}
1274
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001275inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001276{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001277 /**
1278 * Functions triggers appropriate requests on DBus
1279 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001280 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001281 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001282 .methods(boost::beast::http::verb::get)(
1283 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001284 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1285 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001286 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001287 {
1288 return;
1289 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001290 if constexpr (bmcwebEnableMultiHost)
1291 {
1292 // Option currently returns no systems. TBD
1293 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1294 systemName);
1295 return;
1296 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001297 if (systemName != "system")
1298 {
1299 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1300 systemName);
1301 return;
1302 }
1303
Ed Tanous002d39b2022-05-31 08:59:27 -07001304 // Collections don't include the static data added by SubRoute
1305 // because it has a duplicate entry for members
1306 asyncResp->res.jsonValue["@odata.type"] =
1307 "#LogServiceCollection.LogServiceCollection";
1308 asyncResp->res.jsonValue["@odata.id"] =
1309 "/redfish/v1/Systems/system/LogServices";
1310 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1311 asyncResp->res.jsonValue["Description"] =
1312 "Collection of LogServices for this Computer System";
1313 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1314 logServiceArray = nlohmann::json::array();
1315 nlohmann::json::object_t eventLog;
1316 eventLog["@odata.id"] =
1317 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001318 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001319#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001320 nlohmann::json::object_t dumpLog;
1321 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001322 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001323#endif
1324
Jason M. Billsd53dd412019-02-12 17:16:22 -08001325#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001326 nlohmann::json::object_t crashdump;
1327 crashdump["@odata.id"] =
1328 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001329 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001330#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001331
1332#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001333 nlohmann::json::object_t hostlogger;
1334 hostlogger["@odata.id"] =
1335 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001336 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001337#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001338 asyncResp->res.jsonValue["Members@odata.count"] =
1339 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001340
George Liu7a1dbc42022-12-07 16:03:22 +08001341 constexpr std::array<std::string_view, 1> interfaces = {
1342 "xyz.openbmc_project.State.Boot.PostCode"};
1343 dbus::utility::getSubTreePaths(
1344 "/", 0, interfaces,
1345 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001346 const dbus::utility::MapperGetSubTreePathsResponse&
1347 subtreePath) {
1348 if (ec)
1349 {
Ed Tanous62598e32023-07-17 17:06:25 -07001350 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001351 return;
1352 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001353
Ed Tanous002d39b2022-05-31 08:59:27 -07001354 for (const auto& pathStr : subtreePath)
1355 {
1356 if (pathStr.find("PostCode") != std::string::npos)
1357 {
1358 nlohmann::json& logServiceArrayLocal =
1359 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001360 nlohmann::json::object_t member;
1361 member["@odata.id"] =
1362 "/redfish/v1/Systems/system/LogServices/PostCodes";
1363
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001364 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001365
Ed Tanous002d39b2022-05-31 08:59:27 -07001366 asyncResp->res.jsonValue["Members@odata.count"] =
1367 logServiceArrayLocal.size();
1368 return;
1369 }
1370 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001371 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001372 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001373}
1374
1375inline void requestRoutesEventLogService(App& app)
1376{
Ed Tanous22d268c2022-05-19 09:39:07 -07001377 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001378 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001379 .methods(boost::beast::http::verb::get)(
1380 [&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 asyncResp->res.jsonValue["@odata.id"] =
1394 "/redfish/v1/Systems/system/LogServices/EventLog";
1395 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05001396 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07001397 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1398 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1399 asyncResp->res.jsonValue["Id"] = "EventLog";
1400 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301401
Ed Tanous002d39b2022-05-31 08:59:27 -07001402 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001403 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301404
Ed Tanous002d39b2022-05-31 08:59:27 -07001405 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1406 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1407 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301408
Ed Tanous002d39b2022-05-31 08:59:27 -07001409 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1410 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1411 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001412
Ed Tanous002d39b2022-05-31 08:59:27 -07001413 {"target",
1414 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05001415 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001416}
1417
1418inline void requestRoutesJournalEventLogClear(App& app)
1419{
Jason M. Bills4978b632022-02-22 14:17:43 -08001420 BMCWEB_ROUTE(
1421 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001422 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001423 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001424 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001425 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001426 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1427 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001428 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001429 {
1430 return;
1431 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001432 if (systemName != "system")
1433 {
1434 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1435 systemName);
1436 return;
1437 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001438 // Clear the EventLog by deleting the log files
1439 std::vector<std::filesystem::path> redfishLogFiles;
1440 if (getRedfishLogFiles(redfishLogFiles))
1441 {
1442 for (const std::filesystem::path& file : redfishLogFiles)
1443 {
1444 std::error_code ec;
1445 std::filesystem::remove(file, ec);
1446 }
1447 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001448
Ed Tanous002d39b2022-05-31 08:59:27 -07001449 // Reload rsyslog so it knows to start new log files
1450 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001451 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001452 if (ec)
1453 {
Ed Tanous62598e32023-07-17 17:06:25 -07001454 BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001455 messages::internalError(asyncResp->res);
1456 return;
1457 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001458
Ed Tanous002d39b2022-05-31 08:59:27 -07001459 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001460 },
Ed Tanous002d39b2022-05-31 08:59:27 -07001461 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1462 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1463 "replace");
Patrick Williams5a39f772023-10-20 11:20:21 -05001464 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001465}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001466
Jason M. Billsac992cd2022-06-24 13:31:46 -07001467enum class LogParseError
1468{
1469 success,
1470 parseFailed,
1471 messageIdNotInRegistry,
1472};
1473
1474static LogParseError
1475 fillEventLogEntryJson(const std::string& logEntryID,
1476 const std::string& logEntry,
1477 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001478{
Jason M. Bills95820182019-04-22 16:25:34 -07001479 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001480 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001481 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001482 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001483 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001484 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001485 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001486 std::string timestamp = logEntry.substr(0, space);
1487 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001488 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001489 if (entryStart == std::string::npos)
1490 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001491 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001492 }
1493 std::string_view entry(logEntry);
1494 entry.remove_prefix(entryStart);
1495 // Use split to separate the entry into its fields
1496 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001497 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001498 // We need at least a MessageId to be valid
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001499 auto logEntryIter = logEntryFields.begin();
1500 if (logEntryIter == logEntryFields.end())
Jason M. Billscd225da2019-05-08 15:31:57 -07001501 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001502 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001503 }
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001504 std::string& messageID = *logEntryIter;
Jason M. Bills4851d452019-03-28 11:27:48 -07001505 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001506 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001507
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001508 logEntryIter++;
Sui Chen54417b02022-03-24 14:59:52 -07001509 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001510 {
Ed Tanous62598e32023-07-17 17:06:25 -07001511 BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001512 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001513 }
1514
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001515 std::vector<std::string_view> messageArgs(logEntryIter,
1516 logEntryFields.end());
Ed Tanousc05bba42023-06-28 08:33:29 -07001517 messageArgs.resize(message->numberOfArgs);
1518
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001519 std::string msg = redfish::registries::fillMessageArgs(messageArgs,
1520 message->message);
1521 if (msg.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001522 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001523 return LogParseError::parseFailed;
Jason M. Bills4851d452019-03-28 11:27:48 -07001524 }
1525
Jason M. Bills95820182019-04-22 16:25:34 -07001526 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1527 // format which matches the Redfish format except for the fractional seconds
1528 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001529 std::size_t dot = timestamp.find_first_of('.');
1530 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001531 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001532 {
Jason M. Bills95820182019-04-22 16:25:34 -07001533 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001534 }
1535
1536 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001537 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001538 logEntryJson["@odata.id"] = boost::urls::format(
1539 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1540 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001541 logEntryJson["Name"] = "System Event Log Entry";
1542 logEntryJson["Id"] = logEntryID;
1543 logEntryJson["Message"] = std::move(msg);
1544 logEntryJson["MessageId"] = std::move(messageID);
1545 logEntryJson["MessageArgs"] = messageArgs;
1546 logEntryJson["EntryType"] = "Event";
1547 logEntryJson["Severity"] = message->messageSeverity;
1548 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001549 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001550}
1551
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001552inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001553{
Ed Tanous22d268c2022-05-19 09:39:07 -07001554 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001555 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001556 .methods(boost::beast::http::verb::get)(
1557 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001558 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1559 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001560 query_param::QueryCapabilities capabilities = {
1561 .canDelegateTop = true,
1562 .canDelegateSkip = true,
1563 };
1564 query_param::Query delegatedQuery;
1565 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001566 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001567 {
1568 return;
1569 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001570 if constexpr (bmcwebEnableMultiHost)
1571 {
1572 // Option currently returns no systems. TBD
1573 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1574 systemName);
1575 return;
1576 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001577 if (systemName != "system")
1578 {
1579 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1580 systemName);
1581 return;
1582 }
1583
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001584 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001585 size_t skip = delegatedQuery.skip.value_or(0);
1586
Ed Tanous002d39b2022-05-31 08:59:27 -07001587 // Collections don't include the static data added by SubRoute
1588 // because it has a duplicate entry for members
1589 asyncResp->res.jsonValue["@odata.type"] =
1590 "#LogEntryCollection.LogEntryCollection";
1591 asyncResp->res.jsonValue["@odata.id"] =
1592 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1593 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1594 asyncResp->res.jsonValue["Description"] =
1595 "Collection of System Event Log Entries";
1596
1597 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1598 logEntryArray = nlohmann::json::array();
1599 // Go through the log files and create a unique ID for each
1600 // entry
1601 std::vector<std::filesystem::path> redfishLogFiles;
1602 getRedfishLogFiles(redfishLogFiles);
1603 uint64_t entryCount = 0;
1604 std::string logEntry;
1605
1606 // Oldest logs are in the last file, so start there and loop
1607 // backwards
1608 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1609 it++)
1610 {
1611 std::ifstream logStream(*it);
1612 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001613 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001614 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001615 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001616
Ed Tanous002d39b2022-05-31 08:59:27 -07001617 // Reset the unique ID on the first entry
1618 bool firstEntry = true;
1619 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001620 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001621 std::string idStr;
1622 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001623 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001624 continue;
1625 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001626 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001627
Jason M. Billsde703c52022-06-23 14:19:04 -07001628 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001629 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1630 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001631 if (status == LogParseError::messageIdNotInRegistry)
1632 {
1633 continue;
1634 }
1635 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001636 {
1637 messages::internalError(asyncResp->res);
1638 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001639 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001640
Jason M. Billsde703c52022-06-23 14:19:04 -07001641 entryCount++;
1642 // Handle paging using skip (number of entries to skip from the
1643 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001644 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001645 {
1646 continue;
1647 }
1648
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001649 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001650 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001651 }
1652 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001653 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001654 {
1655 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1656 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001657 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001658 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001659 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001660}
Chicago Duan336e96c2019-07-15 14:22:08 +08001661
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001662inline void requestRoutesJournalEventLogEntry(App& app)
1663{
1664 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001665 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001666 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001667 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001668 [&app](const crow::Request& req,
1669 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001670 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001671 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001672 {
1673 return;
1674 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001675 if constexpr (bmcwebEnableMultiHost)
1676 {
1677 // Option currently returns no systems. TBD
1678 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1679 systemName);
1680 return;
1681 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001682
1683 if (systemName != "system")
1684 {
1685 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1686 systemName);
1687 return;
1688 }
1689
Ed Tanous002d39b2022-05-31 08:59:27 -07001690 const std::string& targetID = param;
1691
1692 // Go through the log files and check the unique ID for each
1693 // entry to find the target entry
1694 std::vector<std::filesystem::path> redfishLogFiles;
1695 getRedfishLogFiles(redfishLogFiles);
1696 std::string logEntry;
1697
1698 // Oldest logs are in the last file, so start there and loop
1699 // backwards
1700 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1701 it++)
1702 {
1703 std::ifstream logStream(*it);
1704 if (!logStream.is_open())
1705 {
1706 continue;
1707 }
1708
1709 // Reset the unique ID on the first entry
1710 bool firstEntry = true;
1711 while (std::getline(logStream, logEntry))
1712 {
1713 std::string idStr;
1714 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001715 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001716 continue;
1717 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001718 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001719
1720 if (idStr == targetID)
1721 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001722 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001723 LogParseError status =
1724 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1725 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001726 {
1727 messages::internalError(asyncResp->res);
1728 return;
1729 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001730 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001731 return;
1732 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001733 }
1734 }
1735 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001736 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05001737 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001738}
1739
1740inline void requestRoutesDBusEventLogEntryCollection(App& app)
1741{
Ed Tanous22d268c2022-05-19 09:39:07 -07001742 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001743 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001744 .methods(boost::beast::http::verb::get)(
1745 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001746 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1747 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001748 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001749 {
1750 return;
1751 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001752 if constexpr (bmcwebEnableMultiHost)
1753 {
1754 // Option currently returns no systems. TBD
1755 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1756 systemName);
1757 return;
1758 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001759 if (systemName != "system")
1760 {
1761 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1762 systemName);
1763 return;
1764 }
1765
Ed Tanous002d39b2022-05-31 08:59:27 -07001766 // Collections don't include the static data added by SubRoute
1767 // because it has a duplicate entry for members
1768 asyncResp->res.jsonValue["@odata.type"] =
1769 "#LogEntryCollection.LogEntryCollection";
1770 asyncResp->res.jsonValue["@odata.id"] =
1771 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1772 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1773 asyncResp->res.jsonValue["Description"] =
1774 "Collection of System Event Log Entries";
1775
1776 // DBus implementation of EventLog/Entries
1777 // Make call to Logging Service to find all log entry objects
George Liu5eb468d2023-06-20 17:03:24 +08001778 sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1779 dbus::utility::getManagedObjects(
1780 "xyz.openbmc_project.Logging", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001781 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001782 const dbus::utility::ManagedObjectType& resp) {
1783 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001784 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07001786 BMCWEB_LOG_ERROR(
1787 "getLogEntriesIfaceData resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001788 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001789 return;
1790 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001791 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -07001792 for (const auto& objectPath : resp)
1793 {
1794 const uint32_t* id = nullptr;
1795 const uint64_t* timestamp = nullptr;
1796 const uint64_t* updateTimestamp = nullptr;
1797 const std::string* severity = nullptr;
1798 const std::string* message = nullptr;
1799 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001800 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001801 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001802 const std::string* notify = nullptr;
1803
Ed Tanous002d39b2022-05-31 08:59:27 -07001804 for (const auto& interfaceMap : objectPath.second)
1805 {
1806 if (interfaceMap.first ==
1807 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001808 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001809 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001810 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001811 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001812 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 id = std::get_if<uint32_t>(&propertyMap.second);
1814 }
1815 else if (propertyMap.first == "Timestamp")
1816 {
1817 timestamp =
1818 std::get_if<uint64_t>(&propertyMap.second);
1819 }
1820 else if (propertyMap.first == "UpdateTimestamp")
1821 {
1822 updateTimestamp =
1823 std::get_if<uint64_t>(&propertyMap.second);
1824 }
1825 else if (propertyMap.first == "Severity")
1826 {
1827 severity = std::get_if<std::string>(
1828 &propertyMap.second);
1829 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001830 else if (propertyMap.first == "Resolution")
1831 {
1832 resolution = std::get_if<std::string>(
1833 &propertyMap.second);
1834 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001835 else if (propertyMap.first == "Message")
1836 {
1837 message = std::get_if<std::string>(
1838 &propertyMap.second);
1839 }
1840 else if (propertyMap.first == "Resolved")
1841 {
1842 const bool* resolveptr =
1843 std::get_if<bool>(&propertyMap.second);
1844 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001845 {
1846 messages::internalError(asyncResp->res);
1847 return;
1848 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001849 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001850 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001851 else if (propertyMap.first ==
1852 "ServiceProviderNotify")
1853 {
1854 notify = std::get_if<std::string>(
1855 &propertyMap.second);
1856 if (notify == nullptr)
1857 {
1858 messages::internalError(asyncResp->res);
1859 return;
1860 }
1861 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001862 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001863 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001864 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001865 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001866 messages::internalError(asyncResp->res);
1867 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001868 }
1869 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001870 else if (interfaceMap.first ==
1871 "xyz.openbmc_project.Common.FilePath")
1872 {
1873 for (const auto& propertyMap : interfaceMap.second)
1874 {
1875 if (propertyMap.first == "Path")
1876 {
1877 filePath = std::get_if<std::string>(
1878 &propertyMap.second);
1879 }
1880 }
1881 }
1882 }
1883 // Object path without the
1884 // xyz.openbmc_project.Logging.Entry interface, ignore
1885 // and continue.
1886 if (id == nullptr || message == nullptr ||
1887 severity == nullptr || timestamp == nullptr ||
1888 updateTimestamp == nullptr)
1889 {
1890 continue;
1891 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001892 nlohmann::json& thisEntry = entriesArray.emplace_back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001893 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001894 thisEntry["@odata.id"] = boost::urls::format(
1895 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1896 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001897 thisEntry["Name"] = "System Event Log Entry";
1898 thisEntry["Id"] = std::to_string(*id);
1899 thisEntry["Message"] = *message;
1900 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001901 if ((resolution != nullptr) && (!(*resolution).empty()))
1902 {
1903 thisEntry["Resolution"] = *resolution;
1904 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001905 std::optional<bool> notifyAction =
1906 getProviderNotifyAction(*notify);
1907 if (notifyAction)
1908 {
1909 thisEntry["ServiceProviderNotified"] = *notifyAction;
1910 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 thisEntry["EntryType"] = "Event";
1912 thisEntry["Severity"] =
1913 translateSeverityDbusToRedfish(*severity);
1914 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001915 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001916 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001917 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001918 if (filePath != nullptr)
1919 {
1920 thisEntry["AdditionalDataURI"] =
1921 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1922 std::to_string(*id) + "/attachment";
1923 }
1924 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001925 std::ranges::sort(entriesArray, [](const nlohmann::json& left,
1926 const nlohmann::json& right) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001927 return (left["Id"] <= right["Id"]);
Ed Tanous3544d2a2023-08-06 18:12:20 -07001928 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001929 asyncResp->res.jsonValue["Members@odata.count"] =
1930 entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -07001931 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001932 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001933 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001934}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001935
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001936inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001937{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001938 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001939 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001940 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001941 .methods(boost::beast::http::verb::get)(
1942 [&app](const crow::Request& req,
1943 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001944 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001945 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001946 {
1947 return;
1948 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001949 if constexpr (bmcwebEnableMultiHost)
1950 {
1951 // Option currently returns no systems. TBD
1952 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1953 systemName);
1954 return;
1955 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001956 if (systemName != "system")
1957 {
1958 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1959 systemName);
1960 return;
1961 }
1962
Ed Tanous002d39b2022-05-31 08:59:27 -07001963 std::string entryID = param;
1964 dbus::utility::escapePathForDbus(entryID);
1965
1966 // DBus implementation of EventLog/Entries
1967 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001968 sdbusplus::asio::getAllProperties(
1969 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1970 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001971 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 const dbus::utility::DBusPropertiesMap& resp) {
1973 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001974 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001975 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1976 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001977 return;
1978 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001979 if (ec)
1980 {
Ed Tanous62598e32023-07-17 17:06:25 -07001981 BMCWEB_LOG_ERROR(
1982 "EventLogEntry (DBus) resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001983 messages::internalError(asyncResp->res);
1984 return;
1985 }
1986 const uint32_t* id = nullptr;
1987 const uint64_t* timestamp = nullptr;
1988 const uint64_t* updateTimestamp = nullptr;
1989 const std::string* severity = nullptr;
1990 const std::string* message = nullptr;
1991 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001992 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001993 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001994 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001995
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001996 const bool success = sdbusplus::unpackPropertiesNoThrow(
1997 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1998 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001999 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05002000 "Resolution", resolution, "Path", filePath,
2001 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002002
2003 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07002004 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002005 messages::internalError(asyncResp->res);
2006 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002007 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002008
Ed Tanous002d39b2022-05-31 08:59:27 -07002009 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05002010 timestamp == nullptr || updateTimestamp == nullptr ||
2011 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07002012 {
2013 messages::internalError(asyncResp->res);
2014 return;
2015 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05002016
Ed Tanous002d39b2022-05-31 08:59:27 -07002017 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05002018 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002019 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2020 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
2021 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07002022 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
2023 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
2024 asyncResp->res.jsonValue["Message"] = *message;
2025 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05002026 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
2027 if (notifyAction)
2028 {
2029 asyncResp->res.jsonValue["ServiceProviderNotified"] =
2030 *notifyAction;
2031 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05002032 if ((resolution != nullptr) && (!(*resolution).empty()))
2033 {
2034 asyncResp->res.jsonValue["Resolution"] = *resolution;
2035 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002036 asyncResp->res.jsonValue["EntryType"] = "Event";
2037 asyncResp->res.jsonValue["Severity"] =
2038 translateSeverityDbusToRedfish(*severity);
2039 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002040 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002041 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002042 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002043 if (filePath != nullptr)
2044 {
2045 asyncResp->res.jsonValue["AdditionalDataURI"] =
2046 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
2047 std::to_string(*id) + "/attachment";
2048 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002049 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002050 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002051
2052 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002053 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002054 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002055 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002056 [&app](const crow::Request& req,
2057 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002058 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002059 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002060 {
2061 return;
2062 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002063 if constexpr (bmcwebEnableMultiHost)
2064 {
2065 // Option currently returns no systems. TBD
2066 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2067 systemName);
2068 return;
2069 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002070 if (systemName != "system")
2071 {
2072 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2073 systemName);
2074 return;
2075 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002076 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002077
Ed Tanous002d39b2022-05-31 08:59:27 -07002078 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
2079 resolved))
2080 {
2081 return;
2082 }
Ed Tanous62598e32023-07-17 17:06:25 -07002083 BMCWEB_LOG_DEBUG("Set Resolved");
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002084
George Liu9ae226f2023-06-21 17:56:46 +08002085 sdbusplus::asio::setProperty(
2086 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
2087 "/xyz/openbmc_project/logging/entry/" + entryId,
2088 "xyz.openbmc_project.Logging.Entry", "Resolved", *resolved,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002089 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002090 if (ec)
2091 {
Ed Tanous62598e32023-07-17 17:06:25 -07002092 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002093 messages::internalError(asyncResp->res);
2094 return;
2095 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002096 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002097 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002098
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002099 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002100 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002101 .privileges(redfish::privileges::deleteLogEntry)
2102
Ed Tanous002d39b2022-05-31 08:59:27 -07002103 .methods(boost::beast::http::verb::delete_)(
2104 [&app](const crow::Request& req,
2105 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002106 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002107 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 {
2109 return;
2110 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002111 if constexpr (bmcwebEnableMultiHost)
2112 {
2113 // Option currently returns no systems. TBD
2114 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2115 systemName);
2116 return;
2117 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002118 if (systemName != "system")
2119 {
2120 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2121 systemName);
2122 return;
2123 }
Ed Tanous62598e32023-07-17 17:06:25 -07002124 BMCWEB_LOG_DEBUG("Do delete single event entries.");
Ed Tanous002d39b2022-05-31 08:59:27 -07002125
2126 std::string entryID = param;
2127
2128 dbus::utility::escapePathForDbus(entryID);
2129
2130 // Process response from Logging service.
Patrick Williams5a39f772023-10-20 11:20:21 -05002131 auto respHandler = [asyncResp,
2132 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07002133 BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07002134 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002135 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002136 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002137 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002138 messages::resourceNotFound(asyncResp->res, "LogEntry",
2139 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07002140 return;
2141 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002142 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07002143 BMCWEB_LOG_ERROR(
2144 "EventLogEntry (DBus) doDelete respHandler got error {}",
2145 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 asyncResp->res.result(
2147 boost::beast::http::status::internal_server_error);
2148 return;
2149 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002150
Ed Tanous002d39b2022-05-31 08:59:27 -07002151 asyncResp->res.result(boost::beast::http::status::ok);
2152 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002153
Ed Tanous002d39b2022-05-31 08:59:27 -07002154 // Make call to Logging service to request Delete Log
2155 crow::connections::systemBus->async_method_call(
2156 respHandler, "xyz.openbmc_project.Logging",
2157 "/xyz/openbmc_project/logging/entry/" + entryID,
2158 "xyz.openbmc_project.Object.Delete", "Delete");
Patrick Williams5a39f772023-10-20 11:20:21 -05002159 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002160}
2161
Spencer Kub7028eb2021-10-26 15:27:35 +08002162constexpr const char* hostLoggerFolderPath = "/var/log/console";
2163
2164inline bool
2165 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2166 std::vector<std::filesystem::path>& hostLoggerFiles)
2167{
2168 std::error_code ec;
2169 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2170 if (ec)
2171 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002172 BMCWEB_LOG_WARNING("{}", ec.message());
Spencer Kub7028eb2021-10-26 15:27:35 +08002173 return false;
2174 }
2175 for (const std::filesystem::directory_entry& it : logPath)
2176 {
2177 std::string filename = it.path().filename();
2178 // Prefix of each log files is "log". Find the file and save the
2179 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002180 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002181 {
2182 hostLoggerFiles.emplace_back(it.path());
2183 }
2184 }
2185 // As the log files rotate, they are appended with a ".#" that is higher for
2186 // the older logs. Since we start from oldest logs, sort the name in
2187 // descending order.
2188 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2189 AlphanumLess<std::string>());
2190
2191 return true;
2192}
2193
Ed Tanous02cad962022-06-30 16:50:15 -07002194inline bool getHostLoggerEntries(
2195 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2196 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002197{
2198 GzFileReader logFile;
2199
2200 // Go though all log files and expose host logs.
2201 for (const std::filesystem::path& it : hostLoggerFiles)
2202 {
2203 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2204 {
Ed Tanous62598e32023-07-17 17:06:25 -07002205 BMCWEB_LOG_ERROR("fail to expose host logs");
Spencer Kub7028eb2021-10-26 15:27:35 +08002206 return false;
2207 }
2208 }
2209 // Get lastMessage from constructor by getter
2210 std::string lastMessage = logFile.getLastMessage();
2211 if (!lastMessage.empty())
2212 {
2213 logCount++;
2214 if (logCount > skip && logCount <= (skip + top))
2215 {
2216 logEntries.push_back(lastMessage);
2217 }
2218 }
2219 return true;
2220}
2221
2222inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2223 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002224 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002225{
2226 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002227 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002228 logEntryJson["@odata.id"] = boost::urls::format(
2229 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2230 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002231 logEntryJson["Name"] = "Host Logger Entry";
2232 logEntryJson["Id"] = logEntryID;
2233 logEntryJson["Message"] = msg;
2234 logEntryJson["EntryType"] = "Oem";
2235 logEntryJson["Severity"] = "OK";
2236 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002237}
2238
2239inline void requestRoutesSystemHostLogger(App& app)
2240{
Ed Tanous22d268c2022-05-19 09:39:07 -07002241 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002242 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002243 .methods(boost::beast::http::verb::get)(
2244 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2246 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002247 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002248 {
2249 return;
2250 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002251 if constexpr (bmcwebEnableMultiHost)
2252 {
2253 // Option currently returns no systems. TBD
2254 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2255 systemName);
2256 return;
2257 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002258 if (systemName != "system")
2259 {
2260 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2261 systemName);
2262 return;
2263 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002264 asyncResp->res.jsonValue["@odata.id"] =
2265 "/redfish/v1/Systems/system/LogServices/HostLogger";
2266 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002267 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002268 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2269 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2270 asyncResp->res.jsonValue["Id"] = "HostLogger";
2271 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2272 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002273 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002274}
2275
2276inline void requestRoutesSystemHostLoggerCollection(App& app)
2277{
2278 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002279 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002280 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002281 .methods(boost::beast::http::verb::get)(
2282 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002283 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2284 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002285 query_param::QueryCapabilities capabilities = {
2286 .canDelegateTop = true,
2287 .canDelegateSkip = true,
2288 };
2289 query_param::Query delegatedQuery;
2290 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002291 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002292 {
2293 return;
2294 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002295 if constexpr (bmcwebEnableMultiHost)
2296 {
2297 // Option currently returns no systems. TBD
2298 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2299 systemName);
2300 return;
2301 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002302 if (systemName != "system")
2303 {
2304 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2305 systemName);
2306 return;
2307 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002308 asyncResp->res.jsonValue["@odata.id"] =
2309 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2310 asyncResp->res.jsonValue["@odata.type"] =
2311 "#LogEntryCollection.LogEntryCollection";
2312 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2313 asyncResp->res.jsonValue["Description"] =
2314 "Collection of HostLogger Entries";
2315 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2316 logEntryArray = nlohmann::json::array();
2317 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002318
Ed Tanous002d39b2022-05-31 08:59:27 -07002319 std::vector<std::filesystem::path> hostLoggerFiles;
2320 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2321 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002322 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002323 return;
2324 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002325 // If we weren't provided top and skip limits, use the defaults.
2326 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002327 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002328 size_t logCount = 0;
2329 // This vector only store the entries we want to expose that
2330 // control by skip and top.
2331 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002332 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2333 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002334 {
2335 messages::internalError(asyncResp->res);
2336 return;
2337 }
2338 // If vector is empty, that means skip value larger than total
2339 // log count
2340 if (logEntries.empty())
2341 {
2342 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2343 return;
2344 }
2345 if (!logEntries.empty())
2346 {
2347 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002348 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002349 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002350 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2351 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002352 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002353 }
2354
Ed Tanous002d39b2022-05-31 08:59:27 -07002355 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002356 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002357 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002358 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2359 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002360 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002361 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002362 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002363 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002364}
2365
2366inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2367{
2368 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002369 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002370 .privileges(redfish::privileges::getLogEntry)
2371 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002372 [&app](const crow::Request& req,
2373 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002374 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002375 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002376 {
2377 return;
2378 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002379 if constexpr (bmcwebEnableMultiHost)
2380 {
2381 // Option currently returns no systems. TBD
2382 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2383 systemName);
2384 return;
2385 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002386 if (systemName != "system")
2387 {
2388 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2389 systemName);
2390 return;
2391 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002392 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002393
Ed Tanous002d39b2022-05-31 08:59:27 -07002394 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002395
Patrick Williams84396af2023-05-11 11:47:45 -05002396 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2397 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002398 if (ec == std::errc::invalid_argument ||
2399 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002400 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002401 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002402 return;
2403 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002404
Ed Tanous002d39b2022-05-31 08:59:27 -07002405 std::vector<std::filesystem::path> hostLoggerFiles;
2406 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2407 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002408 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002409 return;
2410 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002411
Ed Tanous002d39b2022-05-31 08:59:27 -07002412 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002413 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002414 std::vector<std::string> logEntries;
2415 // We can get specific entry by skip and top. For example, if we
2416 // want to get nth entry, we can set skip = n-1 and top = 1 to
2417 // get that entry
2418 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2419 logCount))
2420 {
2421 messages::internalError(asyncResp->res);
2422 return;
2423 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002424
Ed Tanous002d39b2022-05-31 08:59:27 -07002425 if (!logEntries.empty())
2426 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002427 nlohmann::json::object_t hostLogEntry;
2428 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2429 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002430 return;
2431 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002432
Ed Tanous002d39b2022-05-31 08:59:27 -07002433 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002434 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Patrick Williams5a39f772023-10-20 11:20:21 -05002435 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002436}
2437
Claire Weinandd72e872022-08-15 14:20:06 -07002438inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002439 crow::App& app, const crow::Request& req,
2440 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2441{
2442 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2443 {
2444 return;
2445 }
2446 // Collections don't include the static data added by SubRoute
2447 // because it has a duplicate entry for members
2448 asyncResp->res.jsonValue["@odata.type"] =
2449 "#LogServiceCollection.LogServiceCollection";
2450 asyncResp->res.jsonValue["@odata.id"] =
2451 "/redfish/v1/Managers/bmc/LogServices";
2452 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2453 asyncResp->res.jsonValue["Description"] =
2454 "Collection of LogServices for this Manager";
2455 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2456 logServiceArray = nlohmann::json::array();
2457
2458#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002459 nlohmann::json::object_t journal;
2460 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002461 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002462#endif
2463
2464 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2465
2466#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002467 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002468 "xyz.openbmc_project.Collection.DeleteAll"};
2469 dbus::utility::getSubTreePaths(
2470 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002471 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002472 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002473 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2474 if (ec)
2475 {
Ed Tanous62598e32023-07-17 17:06:25 -07002476 BMCWEB_LOG_ERROR(
2477 "handleBMCLogServicesCollectionGet respHandler got error {}",
2478 ec);
Claire Weinanfdd26902022-03-01 14:18:25 -08002479 // Assume that getting an error simply means there are no dump
2480 // LogServices. Return without adding any error response.
2481 return;
2482 }
2483
2484 nlohmann::json& logServiceArrayLocal =
2485 asyncResp->res.jsonValue["Members"];
2486
2487 for (const std::string& path : subTreePaths)
2488 {
2489 if (path == "/xyz/openbmc_project/dump/bmc")
2490 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002491 nlohmann::json::object_t member;
2492 member["@odata.id"] =
2493 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002494 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002495 }
2496 else if (path == "/xyz/openbmc_project/dump/faultlog")
2497 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002498 nlohmann::json::object_t member;
2499 member["@odata.id"] =
2500 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002501 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002502 }
2503 }
2504
2505 asyncResp->res.jsonValue["Members@odata.count"] =
2506 logServiceArrayLocal.size();
Patrick Williams5a39f772023-10-20 11:20:21 -05002507 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002508#endif
2509}
2510
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002511inline void requestRoutesBMCLogServiceCollection(App& app)
2512{
2513 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002514 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002515 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002516 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002517}
Ed Tanous1da66f72018-07-27 16:13:37 -07002518
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002519inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002520{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002521 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002522 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002523 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002524 [&app](const crow::Request& req,
2525 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002526 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002527 {
2528 return;
2529 }
2530 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002531 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002532 asyncResp->res.jsonValue["@odata.id"] =
2533 "/redfish/v1/Managers/bmc/LogServices/Journal";
2534 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2535 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002536 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002537 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302538
Ed Tanous002d39b2022-05-31 08:59:27 -07002539 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002540 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002541 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2542 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2543 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302544
Ed Tanous002d39b2022-05-31 08:59:27 -07002545 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2546 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002547 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002548}
Jason M. Billse1f26342018-07-18 12:12:00 -07002549
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002550static int
2551 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2552 sd_journal* journal,
2553 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002554{
2555 // Get the Log Entry contents
2556 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002557
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002558 std::string message;
2559 std::string_view syslogID;
2560 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2561 if (ret < 0)
2562 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002563 BMCWEB_LOG_DEBUG("Failed to read SYSLOG_IDENTIFIER field: {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002564 strerror(-ret));
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002565 }
2566 if (!syslogID.empty())
2567 {
2568 message += std::string(syslogID) + ": ";
2569 }
2570
Ed Tanous39e77502019-03-04 17:35:53 -08002571 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002572 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002573 if (ret < 0)
2574 {
Ed Tanous62598e32023-07-17 17:06:25 -07002575 BMCWEB_LOG_ERROR("Failed to read MESSAGE field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002576 return 1;
2577 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002578 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002579
2580 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002581 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002582 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002583 if (ret < 0)
2584 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002585 BMCWEB_LOG_DEBUG("Failed to read PRIORITY field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002586 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002587
2588 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002589 std::string entryTimeStr;
2590 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002591 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002592 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002593 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002594
2595 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002596 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002597 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2598 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002599 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002600 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2601 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2602 bmcJournalLogEntryJson["Message"] = std::move(message);
2603 bmcJournalLogEntryJson["EntryType"] = "Oem";
2604 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2605 : severity <= 4 ? "Warning"
2606 : "OK";
2607 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2608 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002609 return 0;
2610}
2611
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002612inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002613{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002614 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002615 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002616 .methods(boost::beast::http::verb::get)(
2617 [&app](const crow::Request& req,
2618 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2619 query_param::QueryCapabilities capabilities = {
2620 .canDelegateTop = true,
2621 .canDelegateSkip = true,
2622 };
2623 query_param::Query delegatedQuery;
2624 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002625 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002626 {
2627 return;
2628 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002629
2630 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002631 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002632
Ed Tanous002d39b2022-05-31 08:59:27 -07002633 // Collections don't include the static data added by SubRoute
2634 // because it has a duplicate entry for members
2635 asyncResp->res.jsonValue["@odata.type"] =
2636 "#LogEntryCollection.LogEntryCollection";
2637 asyncResp->res.jsonValue["@odata.id"] =
2638 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2639 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2640 asyncResp->res.jsonValue["Description"] =
2641 "Collection of BMC Journal Entries";
2642 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2643 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002644
Ed Tanous002d39b2022-05-31 08:59:27 -07002645 // Go through the journal and use the timestamp to create a
2646 // unique ID for each entry
2647 sd_journal* journalTmp = nullptr;
2648 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2649 if (ret < 0)
2650 {
Ed Tanous62598e32023-07-17 17:06:25 -07002651 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002652 messages::internalError(asyncResp->res);
2653 return;
2654 }
2655 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2656 journalTmp, sd_journal_close);
2657 journalTmp = nullptr;
2658 uint64_t entryCount = 0;
2659 // Reset the unique ID on the first entry
2660 bool firstEntry = true;
2661 SD_JOURNAL_FOREACH(journal.get())
2662 {
2663 entryCount++;
2664 // Handle paging using skip (number of entries to skip from
2665 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002666 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002667 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002668 continue;
2669 }
2670
2671 std::string idStr;
2672 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2673 {
2674 continue;
2675 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002676 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002677
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002678 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002679 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2680 bmcJournalLogEntry) != 0)
2681 {
George Liu0fda0f12021-11-16 10:06:17 +08002682 messages::internalError(asyncResp->res);
2683 return;
2684 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002685 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002686 }
2687 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002688 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002689 {
2690 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2691 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002692 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002693 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002694 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002695}
Jason M. Billse1f26342018-07-18 12:12:00 -07002696
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002697inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002698{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002699 BMCWEB_ROUTE(app,
2700 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002701 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002702 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002703 [&app](const crow::Request& req,
2704 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2705 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002706 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002707 {
2708 return;
2709 }
2710 // Convert the unique ID back to a timestamp to find the entry
Myung Bae75e8e212023-11-30 12:53:46 -08002711 sd_id128_t bootID{};
Ed Tanous002d39b2022-05-31 08:59:27 -07002712 uint64_t ts = 0;
2713 uint64_t index = 0;
Myung Bae75e8e212023-11-30 12:53:46 -08002714 if (!getTimestampFromID(asyncResp, entryID, bootID, ts, index))
Ed Tanous002d39b2022-05-31 08:59:27 -07002715 {
2716 return;
2717 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002718
Ed Tanous002d39b2022-05-31 08:59:27 -07002719 sd_journal* journalTmp = nullptr;
2720 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2721 if (ret < 0)
2722 {
Ed Tanous62598e32023-07-17 17:06:25 -07002723 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002724 messages::internalError(asyncResp->res);
2725 return;
2726 }
2727 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2728 journalTmp, sd_journal_close);
2729 journalTmp = nullptr;
2730 // Go to the timestamp in the log and move to the entry at the
2731 // index tracking the unique ID
2732 std::string idStr;
2733 bool firstEntry = true;
Myung Bae75e8e212023-11-30 12:53:46 -08002734 ret = sd_journal_seek_monotonic_usec(journal.get(), bootID, ts);
Ed Tanous002d39b2022-05-31 08:59:27 -07002735 if (ret < 0)
2736 {
Ed Tanous62598e32023-07-17 17:06:25 -07002737 BMCWEB_LOG_ERROR("failed to seek to an entry in journal{}",
2738 strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002739 messages::internalError(asyncResp->res);
2740 return;
2741 }
2742 for (uint64_t i = 0; i <= index; i++)
2743 {
2744 sd_journal_next(journal.get());
2745 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2746 {
2747 messages::internalError(asyncResp->res);
2748 return;
2749 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002750 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002751 }
2752 // Confirm that the entry ID matches what was requested
2753 if (idStr != entryID)
2754 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002755 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002756 return;
2757 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002758
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002759 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002760 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002761 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002762 {
2763 messages::internalError(asyncResp->res);
2764 return;
2765 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002766 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Patrick Williams5a39f772023-10-20 11:20:21 -05002767 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002768}
2769
Claire Weinanfdd26902022-03-01 14:18:25 -08002770inline void
2771 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2772 const std::string& dumpType)
2773{
2774 std::string dumpPath;
2775 std::string overWritePolicy;
2776 bool collectDiagnosticDataSupported = false;
2777
2778 if (dumpType == "BMC")
2779 {
2780 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2781 overWritePolicy = "WrapsWhenFull";
2782 collectDiagnosticDataSupported = true;
2783 }
2784 else if (dumpType == "FaultLog")
2785 {
2786 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2787 overWritePolicy = "Unknown";
2788 collectDiagnosticDataSupported = false;
2789 }
2790 else if (dumpType == "System")
2791 {
2792 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2793 overWritePolicy = "WrapsWhenFull";
2794 collectDiagnosticDataSupported = true;
2795 }
2796 else
2797 {
Ed Tanous62598e32023-07-17 17:06:25 -07002798 BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
2799 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -08002800 messages::internalError(asyncResp->res);
2801 return;
2802 }
2803
2804 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2805 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2806 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2807 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2808 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2809 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2810
2811 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002812 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002813 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2814 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2815 redfishDateTimeOffset.second;
2816
2817 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002818
2819 if (collectDiagnosticDataSupported)
2820 {
2821 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2822 ["target"] =
2823 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2824 }
Claire Weinan0d946212022-07-13 19:40:19 -07002825
2826 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2827 dbus::utility::getSubTreePaths(
2828 "/xyz/openbmc_project/dump", 0, interfaces,
2829 [asyncResp, dumpType, dumpPath](
2830 const boost::system::error_code& ec,
2831 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2832 if (ec)
2833 {
Ed Tanous62598e32023-07-17 17:06:25 -07002834 BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
Claire Weinan0d946212022-07-13 19:40:19 -07002835 // Assume that getting an error simply means there are no dump
2836 // LogServices. Return without adding any error response.
2837 return;
2838 }
2839
2840 const std::string dbusDumpPath =
2841 "/xyz/openbmc_project/dump/" +
2842 boost::algorithm::to_lower_copy(dumpType);
2843
2844 for (const std::string& path : subTreePaths)
2845 {
2846 if (path == dbusDumpPath)
2847 {
2848 asyncResp->res
2849 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2850 dumpPath + "/Actions/LogService.ClearLog";
2851 break;
2852 }
2853 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002854 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002855}
2856
2857inline void handleLogServicesDumpServiceGet(
2858 crow::App& app, const std::string& dumpType, const crow::Request& req,
2859 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2860{
2861 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2862 {
2863 return;
2864 }
2865 getDumpServiceInfo(asyncResp, dumpType);
2866}
2867
Ed Tanous22d268c2022-05-19 09:39:07 -07002868inline void handleLogServicesDumpServiceComputerSystemGet(
2869 crow::App& app, const crow::Request& req,
2870 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2871 const std::string& chassisId)
2872{
2873 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2874 {
2875 return;
2876 }
2877 if (chassisId != "system")
2878 {
2879 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2880 return;
2881 }
2882 getDumpServiceInfo(asyncResp, "System");
2883}
2884
Claire Weinanfdd26902022-03-01 14:18:25 -08002885inline void handleLogServicesDumpEntriesCollectionGet(
2886 crow::App& app, const std::string& dumpType, const crow::Request& req,
2887 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2888{
2889 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2890 {
2891 return;
2892 }
2893 getDumpEntryCollection(asyncResp, dumpType);
2894}
2895
Ed Tanous22d268c2022-05-19 09:39:07 -07002896inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2897 crow::App& app, const crow::Request& req,
2898 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2899 const std::string& chassisId)
2900{
2901 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2902 {
2903 return;
2904 }
2905 if (chassisId != "system")
2906 {
2907 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2908 return;
2909 }
2910 getDumpEntryCollection(asyncResp, "System");
2911}
2912
Claire Weinanfdd26902022-03-01 14:18:25 -08002913inline void handleLogServicesDumpEntryGet(
2914 crow::App& app, const std::string& dumpType, const crow::Request& req,
2915 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2916 const std::string& dumpId)
2917{
2918 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2919 {
2920 return;
2921 }
2922 getDumpEntryById(asyncResp, dumpId, dumpType);
2923}
Carson Labrado168d1b12023-03-27 17:04:46 +00002924
Ed Tanous22d268c2022-05-19 09:39:07 -07002925inline void handleLogServicesDumpEntryComputerSystemGet(
2926 crow::App& app, const crow::Request& req,
2927 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2928 const std::string& chassisId, const std::string& dumpId)
2929{
2930 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2931 {
2932 return;
2933 }
2934 if (chassisId != "system")
2935 {
2936 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2937 return;
2938 }
2939 getDumpEntryById(asyncResp, dumpId, "System");
2940}
Claire Weinanfdd26902022-03-01 14:18:25 -08002941
2942inline void handleLogServicesDumpEntryDelete(
2943 crow::App& app, const std::string& dumpType, const crow::Request& req,
2944 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2945 const std::string& dumpId)
2946{
2947 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2948 {
2949 return;
2950 }
2951 deleteDumpEntry(asyncResp, dumpId, dumpType);
2952}
2953
Ed Tanous22d268c2022-05-19 09:39:07 -07002954inline void handleLogServicesDumpEntryComputerSystemDelete(
2955 crow::App& app, const crow::Request& req,
2956 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2957 const std::string& chassisId, const std::string& dumpId)
2958{
2959 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2960 {
2961 return;
2962 }
2963 if (chassisId != "system")
2964 {
2965 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2966 return;
2967 }
2968 deleteDumpEntry(asyncResp, dumpId, "System");
2969}
2970
Carson Labrado168d1b12023-03-27 17:04:46 +00002971inline void handleLogServicesDumpEntryDownloadGet(
2972 crow::App& app, const std::string& dumpType, const crow::Request& req,
2973 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2974 const std::string& dumpId)
2975{
2976 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2977 {
2978 return;
2979 }
2980 downloadDumpEntry(asyncResp, dumpId, dumpType);
2981}
2982
2983inline void handleDBusEventLogEntryDownloadGet(
2984 crow::App& app, const std::string& dumpType, const crow::Request& req,
2985 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2986 const std::string& systemName, const std::string& entryID)
2987{
2988 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2989 {
2990 return;
2991 }
2992 if (!http_helpers::isContentTypeAllowed(
2993 req.getHeaderValue("Accept"),
2994 http_helpers::ContentType::OctetStream, true))
2995 {
2996 asyncResp->res.result(boost::beast::http::status::bad_request);
2997 return;
2998 }
2999 downloadEventLogEntry(asyncResp, systemName, entryID, dumpType);
3000}
3001
Claire Weinanfdd26902022-03-01 14:18:25 -08003002inline void handleLogServicesDumpCollectDiagnosticDataPost(
3003 crow::App& app, const std::string& dumpType, const crow::Request& req,
3004 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3005{
3006 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3007 {
3008 return;
3009 }
3010 createDump(asyncResp, req, dumpType);
3011}
3012
Ed Tanous22d268c2022-05-19 09:39:07 -07003013inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
3014 crow::App& app, const crow::Request& req,
3015 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003016 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003017{
3018 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3019 {
3020 return;
3021 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003022
3023 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003024 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003025 // Option currently returns no systems. TBD
3026 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3027 systemName);
3028 return;
3029 }
3030 if (systemName != "system")
3031 {
3032 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3033 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003034 return;
3035 }
3036 createDump(asyncResp, req, "System");
3037}
3038
Claire Weinanfdd26902022-03-01 14:18:25 -08003039inline void handleLogServicesDumpClearLogPost(
3040 crow::App& app, const std::string& dumpType, const crow::Request& req,
3041 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3042{
3043 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3044 {
3045 return;
3046 }
3047 clearDump(asyncResp, dumpType);
3048}
3049
Ed Tanous22d268c2022-05-19 09:39:07 -07003050inline void handleLogServicesDumpClearLogComputerSystemPost(
3051 crow::App& app, const crow::Request& req,
3052 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003053 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003054{
3055 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3056 {
3057 return;
3058 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003059 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003060 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003061 // Option currently returns no systems. TBD
3062 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3063 systemName);
3064 return;
3065 }
3066 if (systemName != "system")
3067 {
3068 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3069 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003070 return;
3071 }
3072 clearDump(asyncResp, "System");
3073}
3074
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003075inline void requestRoutesBMCDumpService(App& app)
3076{
3077 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003078 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003079 .methods(boost::beast::http::verb::get)(std::bind_front(
3080 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003081}
3082
3083inline void requestRoutesBMCDumpEntryCollection(App& app)
3084{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003085 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003086 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08003087 .methods(boost::beast::http::verb::get)(std::bind_front(
3088 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003089}
3090
3091inline void requestRoutesBMCDumpEntry(App& app)
3092{
3093 BMCWEB_ROUTE(app,
3094 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003095 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003096 .methods(boost::beast::http::verb::get)(std::bind_front(
3097 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
3098
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003099 BMCWEB_ROUTE(app,
3100 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003101 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003102 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3103 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003104}
3105
Carson Labrado168d1b12023-03-27 17:04:46 +00003106inline void requestRoutesBMCDumpEntryDownload(App& app)
3107{
3108 BMCWEB_ROUTE(
3109 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003110 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003111 .privileges(redfish::privileges::getLogEntry)
3112 .methods(boost::beast::http::verb::get)(std::bind_front(
3113 handleLogServicesDumpEntryDownloadGet, std::ref(app), "BMC"));
3114}
3115
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003116inline void requestRoutesBMCDumpCreate(App& app)
3117{
George Liu0fda0f12021-11-16 10:06:17 +08003118 BMCWEB_ROUTE(
3119 app,
3120 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003121 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003122 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08003123 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
3124 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003125}
3126
3127inline void requestRoutesBMCDumpClear(App& app)
3128{
George Liu0fda0f12021-11-16 10:06:17 +08003129 BMCWEB_ROUTE(
3130 app,
3131 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003132 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003133 .methods(boost::beast::http::verb::post)(std::bind_front(
3134 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
3135}
3136
Carson Labrado168d1b12023-03-27 17:04:46 +00003137inline void requestRoutesDBusEventLogEntryDownload(App& app)
3138{
3139 BMCWEB_ROUTE(
3140 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003141 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003142 .privileges(redfish::privileges::getLogEntry)
3143 .methods(boost::beast::http::verb::get)(std::bind_front(
3144 handleDBusEventLogEntryDownloadGet, std::ref(app), "System"));
3145}
3146
Claire Weinanfdd26902022-03-01 14:18:25 -08003147inline void requestRoutesFaultLogDumpService(App& app)
3148{
3149 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3150 .privileges(redfish::privileges::getLogService)
3151 .methods(boost::beast::http::verb::get)(std::bind_front(
3152 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3153}
3154
3155inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3156{
3157 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3158 .privileges(redfish::privileges::getLogEntryCollection)
3159 .methods(boost::beast::http::verb::get)(
3160 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3161 std::ref(app), "FaultLog"));
3162}
3163
3164inline void requestRoutesFaultLogDumpEntry(App& app)
3165{
3166 BMCWEB_ROUTE(app,
3167 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3168 .privileges(redfish::privileges::getLogEntry)
3169 .methods(boost::beast::http::verb::get)(std::bind_front(
3170 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3171
3172 BMCWEB_ROUTE(app,
3173 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3174 .privileges(redfish::privileges::deleteLogEntry)
3175 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3176 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3177}
3178
3179inline void requestRoutesFaultLogDumpClear(App& app)
3180{
3181 BMCWEB_ROUTE(
3182 app,
3183 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3184 .privileges(redfish::privileges::postLogService)
3185 .methods(boost::beast::http::verb::post)(std::bind_front(
3186 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003187}
3188
3189inline void requestRoutesSystemDumpService(App& app)
3190{
Ed Tanous22d268c2022-05-19 09:39:07 -07003191 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003192 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003193 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003194 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003195}
3196
3197inline void requestRoutesSystemDumpEntryCollection(App& app)
3198{
Ed Tanous22d268c2022-05-19 09:39:07 -07003199 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003200 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07003201 .methods(boost::beast::http::verb::get)(std::bind_front(
3202 handleLogServicesDumpEntriesCollectionComputerSystemGet,
3203 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003204}
3205
3206inline void requestRoutesSystemDumpEntry(App& app)
3207{
3208 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003209 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003210 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003211 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003212 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003213
3214 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003215 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003216 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003217 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003218 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003219}
3220
3221inline void requestRoutesSystemDumpCreate(App& app)
3222{
George Liu0fda0f12021-11-16 10:06:17 +08003223 BMCWEB_ROUTE(
3224 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003225 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003226 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003227 .methods(boost::beast::http::verb::post)(std::bind_front(
3228 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3229 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003230}
3231
3232inline void requestRoutesSystemDumpClear(App& app)
3233{
George Liu0fda0f12021-11-16 10:06:17 +08003234 BMCWEB_ROUTE(
3235 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003236 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003237 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003238 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003239 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003240}
3241
3242inline void requestRoutesCrashdumpService(App& app)
3243{
3244 // Note: Deviated from redfish privilege registry for GET & HEAD
3245 // method for security reasons.
3246 /**
3247 * Functions triggers appropriate requests on DBus
3248 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003249 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003250 // This is incorrect, should be:
3251 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003252 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003253 .methods(boost::beast::http::verb::get)(
3254 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003255 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3256 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003257 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003258 {
3259 return;
3260 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003261 if constexpr (bmcwebEnableMultiHost)
3262 {
3263 // Option currently returns no systems. TBD
3264 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3265 systemName);
3266 return;
3267 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003268 if (systemName != "system")
3269 {
3270 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3271 systemName);
3272 return;
3273 }
3274
Ed Tanous002d39b2022-05-31 08:59:27 -07003275 // Copy over the static data to include the entries added by
3276 // SubRoute
3277 asyncResp->res.jsonValue["@odata.id"] =
3278 "/redfish/v1/Systems/system/LogServices/Crashdump";
3279 asyncResp->res.jsonValue["@odata.type"] =
3280 "#LogService.v1_2_0.LogService";
3281 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3282 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303283 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003284 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3285 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303286
Ed Tanous002d39b2022-05-31 08:59:27 -07003287 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003288 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003289 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3290 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3291 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303292
Ed Tanous002d39b2022-05-31 08:59:27 -07003293 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003294 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003295 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3296 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3297 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3298 ["target"] =
3299 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
Patrick Williams5a39f772023-10-20 11:20:21 -05003300 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003301}
3302
3303void inline requestRoutesCrashdumpClear(App& app)
3304{
George Liu0fda0f12021-11-16 10:06:17 +08003305 BMCWEB_ROUTE(
3306 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003307 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003308 // This is incorrect, should be:
3309 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003310 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003311 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003312 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003313 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3314 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003315 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003316 {
3317 return;
3318 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003319 if constexpr (bmcwebEnableMultiHost)
3320 {
3321 // Option currently returns no systems. TBD
3322 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3323 systemName);
3324 return;
3325 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003326 if (systemName != "system")
3327 {
3328 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3329 systemName);
3330 return;
3331 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003332 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003333 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003334 const std::string&) {
3335 if (ec)
3336 {
3337 messages::internalError(asyncResp->res);
3338 return;
3339 }
3340 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003341 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003342 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003343 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003344}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003345
zhanghch058d1b46d2021-04-01 11:18:24 +08003346static void
3347 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3348 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003349{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003350 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003351 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003352 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003353 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003354 if (ec)
3355 {
Ed Tanous62598e32023-07-17 17:06:25 -07003356 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003357 if (ec.value() ==
3358 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003359 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003360 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003361 }
3362 else
3363 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003364 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003365 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003366 return;
3367 }
3368
3369 std::string timestamp{};
3370 std::string filename{};
3371 std::string logfile{};
3372 parseCrashdumpParameters(params, filename, timestamp, logfile);
3373
3374 if (filename.empty() || timestamp.empty())
3375 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003376 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003377 return;
3378 }
3379
3380 std::string crashdumpURI =
3381 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3382 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003383 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003384 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003385 logEntry["@odata.id"] = boost::urls::format(
3386 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3387 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003388 logEntry["Name"] = "CPU Crashdump";
3389 logEntry["Id"] = logID;
3390 logEntry["EntryType"] = "Oem";
3391 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3392 logEntry["DiagnosticDataType"] = "OEM";
3393 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3394 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003395
3396 // If logEntryJson references an array of LogEntry resources
3397 // ('Members' list), then push this as a new entry, otherwise set it
3398 // directly
3399 if (logEntryJson.is_array())
3400 {
3401 logEntryJson.push_back(logEntry);
3402 asyncResp->res.jsonValue["Members@odata.count"] =
3403 logEntryJson.size();
3404 }
3405 else
3406 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003407 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003408 }
3409 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003410 sdbusplus::asio::getAllProperties(
3411 *crow::connections::systemBus, crashdumpObject,
3412 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3413 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003414}
3415
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003416inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003417{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003418 // Note: Deviated from redfish privilege registry for GET & HEAD
3419 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003420 /**
3421 * Functions triggers appropriate requests on DBus
3422 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003423 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003424 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003425 // This is incorrect, should be.
3426 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003427 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003428 .methods(boost::beast::http::verb::get)(
3429 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003430 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3431 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003432 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003433 {
3434 return;
3435 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003436 if constexpr (bmcwebEnableMultiHost)
3437 {
3438 // Option currently returns no systems. TBD
3439 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3440 systemName);
3441 return;
3442 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003443 if (systemName != "system")
3444 {
3445 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3446 systemName);
3447 return;
3448 }
3449
George Liu7a1dbc42022-12-07 16:03:22 +08003450 constexpr std::array<std::string_view, 1> interfaces = {
3451 crashdumpInterface};
3452 dbus::utility::getSubTreePaths(
3453 "/", 0, interfaces,
3454 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003455 const std::vector<std::string>& resp) {
3456 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003457 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003458 if (ec.value() !=
3459 boost::system::errc::no_such_file_or_directory)
3460 {
Ed Tanous62598e32023-07-17 17:06:25 -07003461 BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
3462 ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003463 messages::internalError(asyncResp->res);
3464 return;
3465 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003466 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003467 asyncResp->res.jsonValue["@odata.type"] =
3468 "#LogEntryCollection.LogEntryCollection";
3469 asyncResp->res.jsonValue["@odata.id"] =
3470 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3471 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3472 asyncResp->res.jsonValue["Description"] =
3473 "Collection of Crashdump Entries";
3474 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3475 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003476
Ed Tanous002d39b2022-05-31 08:59:27 -07003477 for (const std::string& path : resp)
3478 {
3479 const sdbusplus::message::object_path objPath(path);
3480 // Get the log ID
3481 std::string logID = objPath.filename();
3482 if (logID.empty())
3483 {
3484 continue;
3485 }
3486 // Add the log entry to the array
3487 logCrashdumpEntry(asyncResp, logID,
3488 asyncResp->res.jsonValue["Members"]);
3489 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003490 });
Patrick Williams5a39f772023-10-20 11:20:21 -05003491 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003492}
Ed Tanous1da66f72018-07-27 16:13:37 -07003493
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003494inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003495{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003496 // Note: Deviated from redfish privilege registry for GET & HEAD
3497 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003498
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003499 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003500 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003501 // this is incorrect, should be
3502 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003503 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003504 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003505 [&app](const crow::Request& req,
3506 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003507 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003508 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003509 {
3510 return;
3511 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003512 if constexpr (bmcwebEnableMultiHost)
3513 {
3514 // Option currently returns no systems. TBD
3515 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3516 systemName);
3517 return;
3518 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003519 if (systemName != "system")
3520 {
3521 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3522 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003523 return;
3524 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003525 const std::string& logID = param;
3526 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
Patrick Williams5a39f772023-10-20 11:20:21 -05003527 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003528}
Ed Tanous1da66f72018-07-27 16:13:37 -07003529
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003530inline void requestRoutesCrashdumpFile(App& app)
3531{
3532 // Note: Deviated from redfish privilege registry for GET & HEAD
3533 // method for security reasons.
3534 BMCWEB_ROUTE(
3535 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003536 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003537 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003538 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003539 [](const crow::Request& req,
3540 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003541 const std::string& systemName, const std::string& logID,
3542 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003543 // Do not call getRedfishRoute here since the crashdump file is not a
3544 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003545
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003546 if constexpr (bmcwebEnableMultiHost)
3547 {
3548 // Option currently returns no systems. TBD
3549 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3550 systemName);
3551 return;
3552 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003553 if (systemName != "system")
3554 {
3555 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3556 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003557 return;
3558 }
3559
Ed Tanous002d39b2022-05-31 08:59:27 -07003560 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003561 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003562 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003563 const std::vector<
3564 std::pair<std::string, dbus::utility::DbusVariantType>>&
3565 resp) {
3566 if (ec)
3567 {
Ed Tanous62598e32023-07-17 17:06:25 -07003568 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003569 messages::internalError(asyncResp->res);
3570 return;
3571 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003572
Ed Tanous002d39b2022-05-31 08:59:27 -07003573 std::string dbusFilename{};
3574 std::string dbusTimestamp{};
3575 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003576
Ed Tanous002d39b2022-05-31 08:59:27 -07003577 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3578 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003579
Ed Tanous002d39b2022-05-31 08:59:27 -07003580 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3581 dbusFilepath.empty())
3582 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003583 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003584 return;
3585 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003586
Ed Tanous002d39b2022-05-31 08:59:27 -07003587 // Verify the file name parameter is correct
3588 if (fileName != dbusFilename)
3589 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003590 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003591 return;
3592 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003593
Ed Tanous27b0cf92023-08-07 12:02:40 -07003594 if (!asyncResp->res.openFile(dbusFilepath))
Ed Tanous002d39b2022-05-31 08:59:27 -07003595 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003596 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003597 return;
3598 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003599
Ed Tanous002d39b2022-05-31 08:59:27 -07003600 // Configure this to be a file download when accessed
3601 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003602 asyncResp->res.addHeader(
3603 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003604 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003605 sdbusplus::asio::getAllProperties(
3606 *crow::connections::systemBus, crashdumpObject,
3607 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3608 std::move(getStoredLogCallback));
Patrick Williams5a39f772023-10-20 11:20:21 -05003609 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003610}
3611
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003612enum class OEMDiagnosticType
3613{
3614 onDemand,
3615 telemetry,
3616 invalid,
3617};
3618
Ed Tanous26ccae32023-02-16 10:28:44 -08003619inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003620{
3621 if (oemDiagStr == "OnDemand")
3622 {
3623 return OEMDiagnosticType::onDemand;
3624 }
3625 if (oemDiagStr == "Telemetry")
3626 {
3627 return OEMDiagnosticType::telemetry;
3628 }
3629
3630 return OEMDiagnosticType::invalid;
3631}
3632
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003633inline void requestRoutesCrashdumpCollect(App& app)
3634{
3635 // Note: Deviated from redfish privilege registry for GET & HEAD
3636 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003637 BMCWEB_ROUTE(
3638 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003639 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003640 // The below is incorrect; Should be ConfigureManager
3641 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003642 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003643 .methods(boost::beast::http::verb::post)(
3644 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003645 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3646 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003647 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003648 {
3649 return;
3650 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003651
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003652 if constexpr (bmcwebEnableMultiHost)
3653 {
3654 // Option currently returns no systems. TBD
3655 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3656 systemName);
3657 return;
3658 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003659 if (systemName != "system")
3660 {
3661 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3662 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003663 return;
3664 }
3665
Ed Tanous002d39b2022-05-31 08:59:27 -07003666 std::string diagnosticDataType;
3667 std::string oemDiagnosticDataType;
3668 if (!redfish::json_util::readJsonAction(
3669 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3670 "OEMDiagnosticDataType", oemDiagnosticDataType))
3671 {
3672 return;
3673 }
3674
3675 if (diagnosticDataType != "OEM")
3676 {
Ed Tanous62598e32023-07-17 17:06:25 -07003677 BMCWEB_LOG_ERROR(
3678 "Only OEM DiagnosticDataType supported for Crashdump");
Ed Tanous002d39b2022-05-31 08:59:27 -07003679 messages::actionParameterValueFormatError(
3680 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3681 "CollectDiagnosticData");
3682 return;
3683 }
3684
3685 OEMDiagnosticType oemDiagType =
3686 getOEMDiagnosticType(oemDiagnosticDataType);
3687
3688 std::string iface;
3689 std::string method;
3690 std::string taskMatchStr;
3691 if (oemDiagType == OEMDiagnosticType::onDemand)
3692 {
3693 iface = crashdumpOnDemandInterface;
3694 method = "GenerateOnDemandLog";
3695 taskMatchStr = "type='signal',"
3696 "interface='org.freedesktop.DBus.Properties',"
3697 "member='PropertiesChanged',"
3698 "arg0namespace='com.intel.crashdump'";
3699 }
3700 else if (oemDiagType == OEMDiagnosticType::telemetry)
3701 {
3702 iface = crashdumpTelemetryInterface;
3703 method = "GenerateTelemetryLog";
3704 taskMatchStr = "type='signal',"
3705 "interface='org.freedesktop.DBus.Properties',"
3706 "member='PropertiesChanged',"
3707 "arg0namespace='com.intel.crashdump'";
3708 }
3709 else
3710 {
Ed Tanous62598e32023-07-17 17:06:25 -07003711 BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
3712 oemDiagnosticDataType);
Ed Tanous002d39b2022-05-31 08:59:27 -07003713 messages::actionParameterValueFormatError(
3714 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3715 "CollectDiagnosticData");
3716 return;
3717 }
3718
3719 auto collectCrashdumpCallback =
3720 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003721 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003722 const std::string&) mutable {
3723 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003724 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003725 if (ec.value() == boost::system::errc::operation_not_supported)
3726 {
3727 messages::resourceInStandby(asyncResp->res);
3728 }
3729 else if (ec.value() ==
3730 boost::system::errc::device_or_resource_busy)
3731 {
3732 messages::serviceTemporarilyUnavailable(asyncResp->res,
3733 "60");
3734 }
3735 else
3736 {
3737 messages::internalError(asyncResp->res);
3738 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003739 return;
3740 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003741 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -07003742 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003743 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07003744 if (!ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -07003745 {
3746 taskData->messages.emplace_back(messages::taskCompletedOK(
3747 std::to_string(taskData->index)));
3748 taskData->state = "Completed";
3749 }
3750 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05003751 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003752 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003753
Ed Tanous002d39b2022-05-31 08:59:27 -07003754 task->startTimer(std::chrono::minutes(5));
3755 task->populateResp(asyncResp->res);
3756 task->payload.emplace(std::move(payload));
3757 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003758
Ed Tanous002d39b2022-05-31 08:59:27 -07003759 crow::connections::systemBus->async_method_call(
3760 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3761 iface, method);
Patrick Williams5a39f772023-10-20 11:20:21 -05003762 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003763}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003764
Andrew Geisslercb92c032018-08-17 07:56:14 -07003765/**
3766 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3767 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003768inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003769{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003770 /**
3771 * Function handles POST method request.
3772 * The Clear Log actions does not require any parameter.The action deletes
3773 * all entries found in the Entries collection for this Log Service.
3774 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003775
George Liu0fda0f12021-11-16 10:06:17 +08003776 BMCWEB_ROUTE(
3777 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003778 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003779 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003780 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003781 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003782 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3783 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003784 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003785 {
3786 return;
3787 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003788 if constexpr (bmcwebEnableMultiHost)
3789 {
3790 // Option currently returns no systems. TBD
3791 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3792 systemName);
3793 return;
3794 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003795 if (systemName != "system")
3796 {
3797 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3798 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003799 return;
3800 }
Ed Tanous62598e32023-07-17 17:06:25 -07003801 BMCWEB_LOG_DEBUG("Do delete all entries.");
Andrew Geisslercb92c032018-08-17 07:56:14 -07003802
Ed Tanous002d39b2022-05-31 08:59:27 -07003803 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003804 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07003805 BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07003806 if (ec)
3807 {
3808 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003809 BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003810 asyncResp->res.result(
3811 boost::beast::http::status::internal_server_error);
3812 return;
3813 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003814
Ed Tanous002d39b2022-05-31 08:59:27 -07003815 asyncResp->res.result(boost::beast::http::status::no_content);
3816 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003817
Ed Tanous002d39b2022-05-31 08:59:27 -07003818 // Make call to Logging service to request Clear Log
3819 crow::connections::systemBus->async_method_call(
3820 respHandler, "xyz.openbmc_project.Logging",
3821 "/xyz/openbmc_project/logging",
3822 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003823 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003824}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003825
3826/****************************************************
3827 * Redfish PostCode interfaces
3828 * using DBUS interface: getPostCodesTS
3829 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003830inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003831{
Ed Tanous22d268c2022-05-19 09:39:07 -07003832 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003833 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003834 .methods(boost::beast::http::verb::get)(
3835 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003836 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3837 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003838 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003839 {
3840 return;
3841 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003842 if constexpr (bmcwebEnableMultiHost)
3843 {
3844 // Option currently returns no systems. TBD
3845 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3846 systemName);
3847 return;
3848 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003849 if (systemName != "system")
3850 {
3851 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3852 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003853 return;
3854 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003855 asyncResp->res.jsonValue["@odata.id"] =
3856 "/redfish/v1/Systems/system/LogServices/PostCodes";
3857 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05003858 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07003859 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3860 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003861 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003862 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3863 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3864 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303865
Ed Tanous002d39b2022-05-31 08:59:27 -07003866 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003867 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003868 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3869 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3870 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303871
Ed Tanous002d39b2022-05-31 08:59:27 -07003872 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3873 {"target",
3874 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05003875 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003876}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003877
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003878inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003879{
George Liu0fda0f12021-11-16 10:06:17 +08003880 BMCWEB_ROUTE(
3881 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003882 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003883 // The following privilege is incorrect; It should be ConfigureManager
3884 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003885 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003886 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003887 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003888 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3889 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003890 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003891 {
3892 return;
3893 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003894 if constexpr (bmcwebEnableMultiHost)
3895 {
3896 // Option currently returns no systems. TBD
3897 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3898 systemName);
3899 return;
3900 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003901 if (systemName != "system")
3902 {
3903 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3904 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003905 return;
3906 }
Ed Tanous62598e32023-07-17 17:06:25 -07003907 BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003908
Ed Tanous002d39b2022-05-31 08:59:27 -07003909 // Make call to post-code service to request clear all
3910 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003911 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003912 if (ec)
3913 {
3914 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003915 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
3916 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003917 asyncResp->res.result(
3918 boost::beast::http::status::internal_server_error);
3919 messages::internalError(asyncResp->res);
3920 return;
3921 }
Tony Lee18fc70c2023-08-24 16:15:54 +08003922 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003923 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003924 "xyz.openbmc_project.State.Boot.PostCode0",
3925 "/xyz/openbmc_project/State/Boot/PostCode0",
3926 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003927 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003928}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003929
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003930/**
3931 * @brief Parse post code ID and get the current value and index value
3932 * eg: postCodeID=B1-2, currentValue=1, index=2
3933 *
3934 * @param[in] postCodeID Post Code ID
3935 * @param[out] currentValue Current value
3936 * @param[out] index Index value
3937 *
3938 * @return bool true if the parsing is successful, false the parsing fails
3939 */
3940inline static bool parsePostCode(const std::string& postCodeID,
3941 uint64_t& currentValue, uint16_t& index)
3942{
3943 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003944 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003945 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3946 {
3947 return false;
3948 }
3949
Patrick Williams84396af2023-05-11 11:47:45 -05003950 auto start = std::next(split[0].begin());
3951 auto end = split[0].end();
3952 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003953
Patrick Williams84396af2023-05-11 11:47:45 -05003954 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003955 {
3956 return false;
3957 }
3958
Patrick Williams84396af2023-05-11 11:47:45 -05003959 start = split[1].begin();
3960 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003961
Patrick Williams84396af2023-05-11 11:47:45 -05003962 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003963
Patrick Williams84396af2023-05-11 11:47:45 -05003964 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003965}
3966
3967static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003968 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303969 const boost::container::flat_map<
3970 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003971 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3972 const uint64_t skip = 0, const uint64_t top = 0)
3973{
3974 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003975 const registries::Message* message =
3976 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003977
3978 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003979 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303980 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3981 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003982 {
3983 currentCodeIndex++;
3984 std::string postcodeEntryID =
3985 "B" + std::to_string(bootIndex) + "-" +
3986 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3987
3988 uint64_t usecSinceEpoch = code.first;
3989 uint64_t usTimeOffset = 0;
3990
3991 if (1 == currentCodeIndex)
3992 { // already incremented
3993 firstCodeTimeUs = code.first;
3994 }
3995 else
3996 {
3997 usTimeOffset = code.first - firstCodeTimeUs;
3998 }
3999
4000 // skip if no specific codeIndex is specified and currentCodeIndex does
4001 // not fall between top and skip
4002 if ((codeIndex == 0) &&
4003 (currentCodeIndex <= skip || currentCodeIndex > top))
4004 {
4005 continue;
4006 }
4007
Gunnar Mills4e0453b2020-07-08 14:00:30 -05004008 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08004009 // currentIndex
4010 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
4011 {
4012 // This is done for simplicity. 1st entry is needed to calculate
4013 // time offset. To improve efficiency, one can get to the entry
4014 // directly (possibly with flatmap's nth method)
4015 continue;
4016 }
4017
4018 // currentCodeIndex is within top and skip or equal to specified code
4019 // index
4020
4021 // Get the Created time from the timestamp
4022 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03004023 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004024
4025 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
4026 std::ostringstream hexCode;
4027 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304028 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004029 std::ostringstream timeOffsetStr;
4030 // Set Fixed -Point Notation
4031 timeOffsetStr << std::fixed;
4032 // Set precision to 4 digits
4033 timeOffsetStr << std::setprecision(4);
4034 // Add double to stream
4035 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004036
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004037 std::string bootIndexStr = std::to_string(bootIndex);
4038 std::string timeOffsetString = timeOffsetStr.str();
4039 std::string hexCodeStr = hexCode.str();
4040
4041 std::array<std::string_view, 3> messageArgs = {
4042 bootIndexStr, timeOffsetString, hexCodeStr};
4043
4044 std::string msg =
4045 redfish::registries::fillMessageArgs(messageArgs, message->message);
4046 if (msg.empty())
ZhikuiRena3316fc2020-01-29 14:58:08 -08004047 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004048 messages::internalError(asyncResp->res);
4049 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004050 }
4051
Tim Leed4342a92020-04-27 11:47:58 +08004052 // Get Severity template from message registry
4053 std::string severity;
4054 if (message != nullptr)
4055 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08004056 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08004057 }
4058
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004059 // Format entry
4060 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05004061 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07004062 bmcLogEntry["@odata.id"] = boost::urls::format(
4063 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
4064 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07004065 bmcLogEntry["Name"] = "POST Code Log Entry";
4066 bmcLogEntry["Id"] = postcodeEntryID;
4067 bmcLogEntry["Message"] = std::move(msg);
4068 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004069 bmcLogEntry["MessageArgs"] = messageArgs;
Jason M. Bills84afc482022-06-24 12:38:23 -07004070 bmcLogEntry["EntryType"] = "Event";
4071 bmcLogEntry["Severity"] = std::move(severity);
4072 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08004073 if (!std::get<std::vector<uint8_t>>(code.second).empty())
4074 {
4075 bmcLogEntry["AdditionalDataURI"] =
4076 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
4077 postcodeEntryID + "/attachment";
4078 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004079
4080 // codeIndex is only specified when querying single entry, return only
4081 // that entry in this case
4082 if (codeIndex != 0)
4083 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004084 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004085 return true;
4086 }
4087
Ed Tanousac106bf2023-06-07 09:24:59 -07004088 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05004089 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08004090 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004091
4092 // Return value is always false when querying multiple entries
4093 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004094}
4095
Ed Tanousac106bf2023-06-07 09:24:59 -07004096static void
4097 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4098 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004099{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004100 uint16_t bootIndex = 0;
4101 uint64_t codeIndex = 0;
4102 if (!parsePostCode(entryId, codeIndex, bootIndex))
4103 {
4104 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07004105 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004106 return;
4107 }
4108
4109 if (bootIndex == 0 || codeIndex == 0)
4110 {
4111 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07004112 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004113 return;
4114 }
4115
ZhikuiRena3316fc2020-01-29 14:58:08 -08004116 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004117 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004118 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304119 const boost::container::flat_map<
4120 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4121 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004122 if (ec)
4123 {
Ed Tanous62598e32023-07-17 17:06:25 -07004124 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004125 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004126 return;
4127 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004128
Ed Tanous002d39b2022-05-31 08:59:27 -07004129 if (postcode.empty())
4130 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004131 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07004132 return;
4133 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004134
Ed Tanousac106bf2023-06-07 09:24:59 -07004135 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004136 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004137 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004138 return;
4139 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004140 },
Jonathan Doman15124762021-01-07 17:54:17 -08004141 "xyz.openbmc_project.State.Boot.PostCode0",
4142 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004143 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4144 bootIndex);
4145}
4146
Ed Tanousac106bf2023-06-07 09:24:59 -07004147static void
4148 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4149 const uint16_t bootIndex, const uint16_t bootCount,
4150 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004151{
4152 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004153 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004154 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304155 const boost::container::flat_map<
4156 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4157 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004158 if (ec)
4159 {
Ed Tanous62598e32023-07-17 17:06:25 -07004160 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004161 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004162 return;
4163 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004164
Ed Tanous002d39b2022-05-31 08:59:27 -07004165 uint64_t endCount = entryCount;
4166 if (!postcode.empty())
4167 {
4168 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07004169 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004170 {
Patrick Williams89492a12023-05-10 07:51:34 -05004171 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
4172 entryCount) -
4173 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004174 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07004175 std::min(static_cast<uint64_t>(top + skip), endCount) -
4176 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004177
Ed Tanousac106bf2023-06-07 09:24:59 -07004178 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4179 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004180 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004181 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004182 }
4183
4184 // continue to previous bootIndex
4185 if (bootIndex < bootCount)
4186 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004187 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07004188 bootCount, endCount, skip, top);
4189 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08004190 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07004191 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004192 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07004193 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4194 std::to_string(skip + top);
4195 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004196 },
Jonathan Doman15124762021-01-07 17:54:17 -08004197 "xyz.openbmc_project.State.Boot.PostCode0",
4198 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004199 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4200 bootIndex);
4201}
4202
zhanghch058d1b46d2021-04-01 11:18:24 +08004203static void
Ed Tanousac106bf2023-06-07 09:24:59 -07004204 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07004205 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004206{
4207 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004208 sdbusplus::asio::getProperty<uint16_t>(
4209 *crow::connections::systemBus,
4210 "xyz.openbmc_project.State.Boot.PostCode0",
4211 "/xyz/openbmc_project/State/Boot/PostCode0",
4212 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07004213 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
4214 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004215 if (ec)
4216 {
Ed Tanous62598e32023-07-17 17:06:25 -07004217 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -07004218 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004219 return;
4220 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004221 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004222 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08004223}
4224
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004225inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004226{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004227 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004228 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07004229 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004230 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004231 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07004232 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4233 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004234 query_param::QueryCapabilities capabilities = {
4235 .canDelegateTop = true,
4236 .canDelegateSkip = true,
4237 };
4238 query_param::Query delegatedQuery;
4239 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00004240 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07004241 {
4242 return;
4243 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004244 if constexpr (bmcwebEnableMultiHost)
4245 {
4246 // Option currently returns no systems. TBD
4247 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4248 systemName);
4249 return;
4250 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004251
4252 if (systemName != "system")
4253 {
4254 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4255 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004256 return;
4257 }
Ed Tanous002d39b2022-05-31 08:59:27 -07004258 asyncResp->res.jsonValue["@odata.type"] =
4259 "#LogEntryCollection.LogEntryCollection";
4260 asyncResp->res.jsonValue["@odata.id"] =
4261 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4262 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4263 asyncResp->res.jsonValue["Description"] =
4264 "Collection of POST Code Log Entries";
4265 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4266 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07004267 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08004268 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07004269 getCurrentBootNumber(asyncResp, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004270 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004271}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004272
George Liu647b3cd2021-07-05 12:43:56 +08004273inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4274{
George Liu0fda0f12021-11-16 10:06:17 +08004275 BMCWEB_ROUTE(
4276 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004277 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004278 .privileges(redfish::privileges::getLogEntry)
4279 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004280 [&app](const crow::Request& req,
4281 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004282 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004283 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004284 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004285 {
4286 return;
4287 }
Matt Spinler72e21372023-04-19 12:53:33 -05004288 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004289 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004290 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004291 {
4292 asyncResp->res.result(boost::beast::http::status::bad_request);
4293 return;
4294 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004295 if constexpr (bmcwebEnableMultiHost)
4296 {
4297 // Option currently returns no systems. TBD
4298 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4299 systemName);
4300 return;
4301 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004302 if (systemName != "system")
4303 {
4304 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4305 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004306 return;
4307 }
George Liu647b3cd2021-07-05 12:43:56 +08004308
Ed Tanous002d39b2022-05-31 08:59:27 -07004309 uint64_t currentValue = 0;
4310 uint16_t index = 0;
4311 if (!parsePostCode(postCodeID, currentValue, index))
4312 {
4313 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4314 return;
4315 }
George Liu647b3cd2021-07-05 12:43:56 +08004316
Ed Tanous002d39b2022-05-31 08:59:27 -07004317 crow::connections::systemBus->async_method_call(
4318 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004319 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004320 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4321 postcodes) {
4322 if (ec.value() == EBADR)
4323 {
4324 messages::resourceNotFound(asyncResp->res, "LogEntry",
4325 postCodeID);
4326 return;
4327 }
4328 if (ec)
4329 {
Ed Tanous62598e32023-07-17 17:06:25 -07004330 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07004331 messages::internalError(asyncResp->res);
4332 return;
4333 }
George Liu647b3cd2021-07-05 12:43:56 +08004334
Ed Tanous002d39b2022-05-31 08:59:27 -07004335 size_t value = static_cast<size_t>(currentValue) - 1;
4336 if (value == std::string::npos || postcodes.size() < currentValue)
4337 {
Ed Tanous62598e32023-07-17 17:06:25 -07004338 BMCWEB_LOG_WARNING("Wrong currentValue value");
Ed Tanous002d39b2022-05-31 08:59:27 -07004339 messages::resourceNotFound(asyncResp->res, "LogEntry",
4340 postCodeID);
4341 return;
4342 }
George Liu647b3cd2021-07-05 12:43:56 +08004343
Ed Tanous002d39b2022-05-31 08:59:27 -07004344 const auto& [tID, c] = postcodes[value];
4345 if (c.empty())
4346 {
Ed Tanous62598e32023-07-17 17:06:25 -07004347 BMCWEB_LOG_WARNING("No found post code data");
Ed Tanous002d39b2022-05-31 08:59:27 -07004348 messages::resourceNotFound(asyncResp->res, "LogEntry",
4349 postCodeID);
4350 return;
4351 }
4352 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4353 const char* d = reinterpret_cast<const char*>(c.data());
4354 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004355
Ed Tanousd9f6c622022-03-17 09:12:17 -07004356 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004357 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004358 asyncResp->res.addHeader(
4359 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous27b0cf92023-08-07 12:02:40 -07004360 asyncResp->res.write(crow::utility::base64encode(strData));
Patrick Williams5a39f772023-10-20 11:20:21 -05004361 },
Ed Tanous002d39b2022-05-31 08:59:27 -07004362 "xyz.openbmc_project.State.Boot.PostCode0",
4363 "/xyz/openbmc_project/State/Boot/PostCode0",
4364 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
Patrick Williams5a39f772023-10-20 11:20:21 -05004365 });
George Liu647b3cd2021-07-05 12:43:56 +08004366}
4367
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004368inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004369{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004370 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004371 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004372 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004373 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004374 [&app](const crow::Request& req,
4375 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004376 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004377 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004378 {
4379 return;
4380 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004381 if constexpr (bmcwebEnableMultiHost)
4382 {
4383 // Option currently returns no systems. TBD
4384 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4385 systemName);
4386 return;
4387 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004388 if (systemName != "system")
4389 {
4390 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4391 systemName);
4392 return;
4393 }
4394
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004395 getPostCodeForEntry(asyncResp, targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05004396 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004397}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004398
Ed Tanous1da66f72018-07-27 16:13:37 -07004399} // namespace redfish