blob: 371ae4486734135e6db1a39ab4ffc4b7360ac277 [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 Tanous07c8c202022-07-11 10:08:08 -070039#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070040#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080041#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070042#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020043#include <sdbusplus/asio/property.hpp>
44#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045
George Liu7a1dbc42022-12-07 16:03:22 +080046#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080047#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070048#include <filesystem>
Ed Tanous18f8f602023-07-18 10:07:23 -070049#include <iterator>
Xiaochao Ma75710de2021-01-21 17:56:02 +080050#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070051#include <ranges>
Ed Tanous26702d02021-11-03 15:02:33 -070052#include <span>
Ed Tanous18f8f602023-07-18 10:07:23 -070053#include <string>
Jason M. Billscd225da2019-05-08 15:31:57 -070054#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080055#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070056
57namespace redfish
58{
59
Patrick Williams89492a12023-05-10 07:51:34 -050060constexpr const char* crashdumpObject = "com.intel.crashdump";
61constexpr const char* crashdumpPath = "/com/intel/crashdump";
62constexpr const char* crashdumpInterface = "com.intel.crashdump";
63constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070064 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050065constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070066 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050067constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070068 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070069
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060070enum class DumpCreationProgress
71{
72 DUMP_CREATE_SUCCESS,
73 DUMP_CREATE_FAILED,
74 DUMP_CREATE_INPROGRESS
75};
76
James Feistf6150402019-01-08 10:36:20 -080077namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070078
Gunnar Mills1214b7e2020-06-04 10:11:30 -050079inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070080{
Ed Tanousd4d25792020-09-29 15:15:03 -070081 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
82 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
83 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
84 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070085 {
86 return "Critical";
87 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070088 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
89 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
90 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070091 {
92 return "OK";
93 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070094 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070095 {
96 return "Warning";
97 }
98 return "";
99}
100
Abhishek Patel9017faf2021-09-14 22:48:55 -0500101inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
102{
103 std::optional<bool> notifyAction;
104 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
105 {
106 notifyAction = true;
107 }
108 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
109 {
110 notifyAction = false;
111 }
112
113 return notifyAction;
114}
115
Ed Tanous18f8f602023-07-18 10:07:23 -0700116inline std::string getDumpPath(std::string_view dumpType)
117{
118 std::string dbusDumpPath = "/xyz/openbmc_project/dump/";
119 std::ranges::transform(dumpType, std::back_inserter(dbusDumpPath),
120 bmcweb::asciiToLower);
121
122 return dbusDumpPath;
123}
124
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700125inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800126 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700127 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700128{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500129 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700130 size_t length = 0;
131 int ret = 0;
132 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800133 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
134 const void** dataVoid = reinterpret_cast<const void**>(&data);
135
136 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700137 if (ret < 0)
138 {
139 return ret;
140 }
Ed Tanous39e77502019-03-04 17:35:53 -0800141 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700142 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000143 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700144 return ret;
145}
146
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700147inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800148 std::string_view field, const int& base,
149 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700150{
151 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800152 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700153 // Get the metadata from the requested field of the journal entry
154 ret = getJournalMetadata(journal, field, metadata);
155 if (ret < 0)
156 {
157 return ret;
158 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000159 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700160 return ret;
161}
162
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700163inline static bool getEntryTimestamp(sd_journal* journal,
164 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800165{
166 int ret = 0;
167 uint64_t timestamp = 0;
168 ret = sd_journal_get_realtime_usec(journal, &timestamp);
169 if (ret < 0)
170 {
Ed Tanous62598e32023-07-17 17:06:25 -0700171 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
ZhikuiRena3316fc2020-01-29 14:58:08 -0800172 return false;
173 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300174 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500175 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800176}
Ed Tanous50b8a432022-02-03 16:29:50 -0800177
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700178inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
179 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700180{
181 int ret = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800182 static sd_id128_t prevBootID{};
Jason M. Bills16428a12018-11-02 12:42:29 -0700183 static uint64_t prevTs = 0;
184 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700185 if (firstEntry)
186 {
Myung Bae75e8e212023-11-30 12:53:46 -0800187 prevBootID = {};
Jason M. Billse85d6b12019-07-29 17:01:15 -0700188 prevTs = 0;
189 }
190
Jason M. Bills16428a12018-11-02 12:42:29 -0700191 // Get the entry timestamp
192 uint64_t curTs = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800193 sd_id128_t curBootID{};
194 ret = sd_journal_get_monotonic_usec(journal, &curTs, &curBootID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700195 if (ret < 0)
196 {
Ed Tanous62598e32023-07-17 17:06:25 -0700197 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
Jason M. Bills16428a12018-11-02 12:42:29 -0700198 return false;
199 }
Myung Bae75e8e212023-11-30 12:53:46 -0800200 // If the timestamp isn't unique on the same boot, increment the index
201 bool sameBootIDs = sd_id128_equal(curBootID, prevBootID) != 0;
202 if (sameBootIDs && (curTs == prevTs))
Jason M. Bills16428a12018-11-02 12:42:29 -0700203 {
204 index++;
205 }
206 else
207 {
208 // Otherwise, reset it
209 index = 0;
210 }
Myung Bae75e8e212023-11-30 12:53:46 -0800211
212 if (!sameBootIDs)
213 {
214 // Save the bootID
215 prevBootID = curBootID;
216 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700217 // Save the timestamp
218 prevTs = curTs;
219
Myung Bae75e8e212023-11-30 12:53:46 -0800220 // make entryID as <bootID>_<timestamp>[_<index>]
221 std::array<char, SD_ID128_STRING_MAX> bootIDStr{};
222 sd_id128_to_string(curBootID, bootIDStr.data());
223 entryID = std::format("{}_{}", bootIDStr.data(), curTs);
Jason M. Bills16428a12018-11-02 12:42:29 -0700224 if (index > 0)
225 {
226 entryID += "_" + std::to_string(index);
227 }
228 return true;
229}
230
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500231static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700232 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700233{
Ed Tanous271584a2019-07-09 16:24:22 -0700234 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700235 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700236 if (firstEntry)
237 {
238 prevTs = 0;
239 }
240
Jason M. Bills95820182019-04-22 16:25:34 -0700241 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700242 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700243 std::tm timeStruct = {};
244 std::istringstream entryStream(logEntry);
245 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
246 {
247 curTs = std::mktime(&timeStruct);
248 }
249 // If the timestamp isn't unique, increment the index
250 if (curTs == prevTs)
251 {
252 index++;
253 }
254 else
255 {
256 // Otherwise, reset it
257 index = 0;
258 }
259 // Save the timestamp
260 prevTs = curTs;
261
262 entryID = std::to_string(curTs);
263 if (index > 0)
264 {
265 entryID += "_" + std::to_string(index);
266 }
267 return true;
268}
269
Myung Bae75e8e212023-11-30 12:53:46 -0800270// Entry is formed like "BootID_timestamp" or "BootID_timestamp_index"
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700271inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800272 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Myung Bae75e8e212023-11-30 12:53:46 -0800273 const std::string& entryID, sd_id128_t& bootID,
274 uint64_t& timestamp, uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700275{
276 if (entryID.empty())
277 {
278 return false;
279 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700280
Myung Bae75e8e212023-11-30 12:53:46 -0800281 // Convert the unique ID back to a bootID + timestamp to find the entry
282 std::string_view entryIDStrView(entryID);
283 auto underscore1Pos = entryIDStrView.find('_');
284 if (underscore1Pos == std::string_view::npos)
285 {
286 // EntryID has no bootID or timestamp
287 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
288 return false;
289 }
290
291 // EntryID has bootID + timestamp
292
293 // Convert entryIDViewString to BootID
294 // NOTE: bootID string which needs to be null-terminated for
295 // sd_id128_from_string()
296 std::string bootIDStr(entryID, 0, underscore1Pos);
297 if (sd_id128_from_string(bootIDStr.c_str(), &bootID) < 0)
298 {
299 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
300 return false;
301 }
302
303 // Get the timestamp from entryID
304 std::string_view timestampStrView = entryIDStrView;
305 timestampStrView.remove_prefix(underscore1Pos + 1);
306
307 // Check the index in timestamp
308 auto underscore2Pos = timestampStrView.find('_');
309 if (underscore2Pos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700310 {
311 // Timestamp has an index
Myung Bae75e8e212023-11-30 12:53:46 -0800312 timestampStrView.remove_suffix(timestampStrView.size() -
313 underscore2Pos);
314 std::string_view indexStr(timestampStrView);
315 indexStr.remove_prefix(underscore2Pos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500316 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
317 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700318 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700319 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800320 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700321 return false;
322 }
323 }
Myung Bae75e8e212023-11-30 12:53:46 -0800324
325 // Now timestamp has no index
326 auto [ptr, ec] = std::from_chars(timestampStrView.begin(),
327 timestampStrView.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700328 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700329 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800330 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700331 return false;
332 }
333 return true;
334}
335
Jason M. Bills95820182019-04-22 16:25:34 -0700336static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500337 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700338{
339 static const std::filesystem::path redfishLogDir = "/var/log";
340 static const std::string redfishLogFilename = "redfish";
341
342 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500343 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700344 std::filesystem::directory_iterator(redfishLogDir))
345 {
346 // If we find a redfish log file, save the path
347 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700348 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700349 {
350 redfishLogFiles.emplace_back(redfishLogDir / filename);
351 }
352 }
353 // As the log files rotate, they are appended with a ".#" that is higher for
354 // the older logs. Since we don't expect more than 10 log files, we
355 // can just sort the list to get them in order from newest to oldest
Ed Tanous3544d2a2023-08-06 18:12:20 -0700356 std::ranges::sort(redfishLogFiles);
Jason M. Bills95820182019-04-22 16:25:34 -0700357
358 return !redfishLogFiles.empty();
359}
360
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600361inline log_entry::OriginatorTypes
362 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
363{
364 if (originatorType ==
365 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
366 {
367 return log_entry::OriginatorTypes::Client;
368 }
369 if (originatorType ==
370 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
371 {
372 return log_entry::OriginatorTypes::Internal;
373 }
374 if (originatorType ==
375 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
376 {
377 return log_entry::OriginatorTypes::SupportingService;
378 }
379 return log_entry::OriginatorTypes::Invalid;
380}
381
Claire Weinanaefe3782022-07-15 19:17:19 -0700382inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800383 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700384 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600385 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700386 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
387{
388 for (const auto& interfaceMap : object.second)
389 {
390 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
391 {
392 for (const auto& propertyMap : interfaceMap.second)
393 {
394 if (propertyMap.first == "Status")
395 {
396 const auto* status =
397 std::get_if<std::string>(&propertyMap.second);
398 if (status == nullptr)
399 {
400 messages::internalError(asyncResp->res);
401 break;
402 }
403 dumpStatus = *status;
404 }
405 }
406 }
407 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
408 {
409 for (const auto& propertyMap : interfaceMap.second)
410 {
411 if (propertyMap.first == "Size")
412 {
413 const auto* sizePtr =
414 std::get_if<uint64_t>(&propertyMap.second);
415 if (sizePtr == nullptr)
416 {
417 messages::internalError(asyncResp->res);
418 break;
419 }
420 size = *sizePtr;
421 break;
422 }
423 }
424 }
425 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
426 {
427 for (const auto& propertyMap : interfaceMap.second)
428 {
429 if (propertyMap.first == "Elapsed")
430 {
431 const uint64_t* usecsTimeStamp =
432 std::get_if<uint64_t>(&propertyMap.second);
433 if (usecsTimeStamp == nullptr)
434 {
435 messages::internalError(asyncResp->res);
436 break;
437 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700438 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700439 break;
440 }
441 }
442 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600443 else if (interfaceMap.first ==
444 "xyz.openbmc_project.Common.OriginatedBy")
445 {
446 for (const auto& propertyMap : interfaceMap.second)
447 {
448 if (propertyMap.first == "OriginatorId")
449 {
450 const std::string* id =
451 std::get_if<std::string>(&propertyMap.second);
452 if (id == nullptr)
453 {
454 messages::internalError(asyncResp->res);
455 break;
456 }
457 originatorId = *id;
458 }
459
460 if (propertyMap.first == "OriginatorType")
461 {
462 const std::string* type =
463 std::get_if<std::string>(&propertyMap.second);
464 if (type == nullptr)
465 {
466 messages::internalError(asyncResp->res);
467 break;
468 }
469
470 originatorType = mapDbusOriginatorTypeToRedfish(*type);
471 if (originatorType == log_entry::OriginatorTypes::Invalid)
472 {
473 messages::internalError(asyncResp->res);
474 break;
475 }
476 }
477 }
478 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700479 }
480}
481
Nan Zhou21ab4042022-06-26 23:07:40 +0000482static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800483{
484 std::string entriesPath;
485
486 if (dumpType == "BMC")
487 {
488 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
489 }
490 else if (dumpType == "FaultLog")
491 {
492 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
493 }
494 else if (dumpType == "System")
495 {
496 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
497 }
498 else
499 {
Ed Tanous62598e32023-07-17 17:06:25 -0700500 BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
501 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -0800502 }
503
504 // Returns empty string on error
505 return entriesPath;
506}
507
zhanghch058d1b46d2021-04-01 11:18:24 +0800508inline void
509 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
510 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500511{
Claire Weinanfdd26902022-03-01 14:18:25 -0800512 std::string entriesPath = getDumpEntriesPath(dumpType);
513 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500514 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500515 messages::internalError(asyncResp->res);
516 return;
517 }
518
George Liu5eb468d2023-06-20 17:03:24 +0800519 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
520 dbus::utility::getManagedObjects(
521 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800522 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800523 dumpType](const boost::system::error_code& ec,
George Liu5eb468d2023-06-20 17:03:24 +0800524 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700525 if (ec)
526 {
Ed Tanous62598e32023-07-17 17:06:25 -0700527 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700528 messages::internalError(asyncResp->res);
529 return;
530 }
531
Claire Weinanfdd26902022-03-01 14:18:25 -0800532 // Remove ending slash
533 std::string odataIdStr = entriesPath;
534 if (!odataIdStr.empty())
535 {
536 odataIdStr.pop_back();
537 }
538
539 asyncResp->res.jsonValue["@odata.type"] =
540 "#LogEntryCollection.LogEntryCollection";
541 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
542 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500543 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
544 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800545
Ed Tanous3544d2a2023-08-06 18:12:20 -0700546 nlohmann::json::array_t entriesArray;
Ed Tanous18f8f602023-07-18 10:07:23 -0700547 std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
Ed Tanous002d39b2022-05-31 08:59:27 -0700548
George Liu5eb468d2023-06-20 17:03:24 +0800549 dbus::utility::ManagedObjectType resp(objects);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700550 std::ranges::sort(resp, [](const auto& l, const auto& r) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700551 return AlphanumLess<std::string>()(l.first.filename(),
552 r.first.filename());
553 });
554
555 for (auto& object : resp)
556 {
557 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500558 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700559 continue;
560 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700561 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700562 uint64_t size = 0;
563 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600564 std::string originatorId;
565 log_entry::OriginatorTypes originatorType =
566 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700567 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700568
569 std::string entryID = object.first.filename();
570 if (entryID.empty())
571 {
572 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500573 }
574
Claire Weinanc6fecda2022-07-15 10:43:25 -0700575 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600576 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700577 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700578
579 if (dumpStatus !=
580 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
581 !dumpStatus.empty())
582 {
583 // Dump status is not Complete, no need to enumerate
584 continue;
585 }
586
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600587 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800588 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 thisEntry["Id"] = entryID;
590 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700591 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700592 thisEntry["Created"] =
593 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700594
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600595 if (!originatorId.empty())
596 {
597 thisEntry["Originator"] = originatorId;
598 thisEntry["OriginatorType"] = originatorType;
599 }
600
Ed Tanous002d39b2022-05-31 08:59:27 -0700601 if (dumpType == "BMC")
602 {
603 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500604 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
605 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800606 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700607 }
608 else if (dumpType == "System")
609 {
610 thisEntry["DiagnosticDataType"] = "OEM";
611 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500612 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
613 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800614 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700615 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500616 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700617 }
618 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -0700619 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
Patrick Williams5a39f772023-10-20 11:20:21 -0500620 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500621}
622
zhanghch058d1b46d2021-04-01 11:18:24 +0800623inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700624 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800625 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500626{
Claire Weinanfdd26902022-03-01 14:18:25 -0800627 std::string entriesPath = getDumpEntriesPath(dumpType);
628 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500629 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500630 messages::internalError(asyncResp->res);
631 return;
632 }
633
George Liu5eb468d2023-06-20 17:03:24 +0800634 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
635 dbus::utility::getManagedObjects(
636 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800637 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800638 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700639 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700640 if (ec)
641 {
Ed Tanous62598e32023-07-17 17:06:25 -0700642 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700643 messages::internalError(asyncResp->res);
644 return;
645 }
646
647 bool foundDumpEntry = false;
Ed Tanous18f8f602023-07-18 10:07:23 -0700648 std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
Ed Tanous002d39b2022-05-31 08:59:27 -0700649
650 for (const auto& objectPath : resp)
651 {
652 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500653 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700654 continue;
655 }
656
657 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700658 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700659 uint64_t size = 0;
660 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600661 std::string originatorId;
662 log_entry::OriginatorTypes originatorType =
663 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700664
Claire Weinanaefe3782022-07-15 19:17:19 -0700665 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600666 timestampUs, originatorId,
667 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700668
669 if (dumpStatus !=
670 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
671 !dumpStatus.empty())
672 {
673 // Dump status is not Complete
674 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200675 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
676 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500677 return;
678 }
679
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600681 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800682 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700683 asyncResp->res.jsonValue["Id"] = entryID;
684 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700685 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700686 asyncResp->res.jsonValue["Created"] =
687 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500688
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600689 if (!originatorId.empty())
690 {
691 asyncResp->res.jsonValue["Originator"] = originatorId;
692 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
693 }
694
Ed Tanous002d39b2022-05-31 08:59:27 -0700695 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500696 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700697 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
698 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800699 entriesPath + entryID + "/attachment";
700 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500701 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700702 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500703 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700704 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
705 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
706 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800707 entriesPath + entryID + "/attachment";
708 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500709 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700710 }
711 if (!foundDumpEntry)
712 {
Ed Tanous62598e32023-07-17 17:06:25 -0700713 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
Myung Baeb90d14f2023-05-31 14:40:39 -0500714 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
715 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700716 return;
717 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500718 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500719}
720
zhanghch058d1b46d2021-04-01 11:18:24 +0800721inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800722 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500723 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500724{
Patrick Williams5a39f772023-10-20 11:20:21 -0500725 auto respHandler = [asyncResp,
726 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -0700727 BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500728 if (ec)
729 {
George Liu3de8d8b2021-03-22 17:49:39 +0800730 if (ec.value() == EBADR)
731 {
732 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
733 return;
734 }
Ed Tanous62598e32023-07-17 17:06:25 -0700735 BMCWEB_LOG_ERROR(
736 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
737 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500738 messages::internalError(asyncResp->res);
739 return;
740 }
741 };
Ed Tanous18f8f602023-07-18 10:07:23 -0700742
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500743 crow::connections::systemBus->async_method_call(
744 respHandler, "xyz.openbmc_project.Dump.Manager",
Ed Tanous18f8f602023-07-18 10:07:23 -0700745 std::format("{}/entry/{}", getDumpPath(dumpType), entryID),
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500746 "xyz.openbmc_project.Object.Delete", "Delete");
747}
748
Carson Labrado168d1b12023-03-27 17:04:46 +0000749inline void
750 downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
751 const std::string& entryID,
752 const std::string& downloadEntryType,
753 const boost::system::error_code& ec,
754 const sdbusplus::message::unix_fd& unixfd)
755{
756 if (ec.value() == EBADR)
757 {
758 messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
759 return;
760 }
761 if (ec)
762 {
763 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
764 messages::internalError(asyncResp->res);
765 return;
766 }
767
768 // Make sure we know how to process the retrieved entry attachment
769 if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
770 {
771 BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
772 downloadEntryType);
773 messages::internalError(asyncResp->res);
774 }
775
776 int fd = -1;
777 fd = dup(unixfd);
778 if (fd < 0)
779 {
780 BMCWEB_LOG_ERROR("Failed to open file");
781 messages::internalError(asyncResp->res);
782 return;
783 }
784
785 long long int size = lseek(fd, 0, SEEK_END);
786 if (size <= 0)
787 {
788 BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
789 size);
790 messages::internalError(asyncResp->res);
791 close(fd);
792 return;
793 }
794
795 // Arbitrary max size of 20MB to accommodate BMC dumps
796 constexpr int maxFileSize = 20 * 1024 * 1024;
797 if (size > maxFileSize)
798 {
799 BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
800 size, maxFileSize);
801 messages::internalError(asyncResp->res);
802 close(fd);
803 return;
804 }
805 long long int rc = lseek(fd, 0, SEEK_SET);
806 if (rc < 0)
807 {
808 BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
809 messages::internalError(asyncResp->res);
810 close(fd);
811 return;
812 }
813
Ed Tanous27b0cf92023-08-07 12:02:40 -0700814 std::string body;
815 body.resize(static_cast<size_t>(size), '\0');
816 rc = read(fd, body.data(), body.size());
Carson Labrado168d1b12023-03-27 17:04:46 +0000817 if ((rc == -1) || (rc != size))
818 {
819 BMCWEB_LOG_ERROR("Failed to read in file");
820 messages::internalError(asyncResp->res);
821 close(fd);
822 return;
823 }
824 close(fd);
Carson Labrado168d1b12023-03-27 17:04:46 +0000825 if (downloadEntryType == "System")
826 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700827 // Base64 encode response.
828 asyncResp->res.write(crow::utility::base64encode(body));
Carson Labrado168d1b12023-03-27 17:04:46 +0000829 asyncResp->res.addHeader(
830 boost::beast::http::field::content_transfer_encoding, "Base64");
831 }
Ed Tanous27b0cf92023-08-07 12:02:40 -0700832 else
833 {
834 asyncResp->res.write(std::move(body));
835 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000836
837 asyncResp->res.addHeader(boost::beast::http::field::content_type,
838 "application/octet-stream");
839}
840
841inline void
842 downloadDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
843 const std::string& entryID, const std::string& dumpType)
844{
845 if (dumpType != "BMC")
846 {
847 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
848 messages::resourceNotFound(asyncResp->res, dumpType + " dump", entryID);
849 return;
850 }
851
Ed Tanous18f8f602023-07-18 10:07:23 -0700852 std::string dumpEntryPath = std::format("{}/entry/{}",
853 getDumpPath(dumpType), entryID);
Carson Labrado168d1b12023-03-27 17:04:46 +0000854
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 },
Ed Tanous18f8f602023-07-18 10:07:23 -07001215 "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001216 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001217}
1218
zhanghch058d1b46d2021-04-01 11:18:24 +08001219inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1220 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001221{
Claire Weinan0d946212022-07-13 19:40:19 -07001222 crow::connections::systemBus->async_method_call(
1223 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001224 if (ec)
1225 {
Ed Tanous62598e32023-07-17 17:06:25 -07001226 BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001227 messages::internalError(asyncResp->res);
1228 return;
1229 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001230 },
Ed Tanous18f8f602023-07-18 10:07:23 -07001231 "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
Claire Weinan0d946212022-07-13 19:40:19 -07001232 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001233}
1234
Ed Tanousb9d36b42022-02-26 21:42:46 -08001235inline static void
1236 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1237 std::string& filename, std::string& timestamp,
1238 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001239{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001240 const std::string* filenamePtr = nullptr;
1241 const std::string* timestampPtr = nullptr;
1242 const std::string* logfilePtr = nullptr;
1243
1244 const bool success = sdbusplus::unpackPropertiesNoThrow(
1245 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1246 "Filename", filenamePtr, "Log", logfilePtr);
1247
1248 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001249 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001250 return;
1251 }
1252
1253 if (filenamePtr != nullptr)
1254 {
1255 filename = *filenamePtr;
1256 }
1257
1258 if (timestampPtr != nullptr)
1259 {
1260 timestamp = *timestampPtr;
1261 }
1262
1263 if (logfilePtr != nullptr)
1264 {
1265 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001266 }
1267}
1268
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001269inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001270{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001271 /**
1272 * Functions triggers appropriate requests on DBus
1273 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001274 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001275 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001276 .methods(boost::beast::http::verb::get)(
1277 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001278 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1279 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001280 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001281 {
1282 return;
1283 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001284 if constexpr (bmcwebEnableMultiHost)
1285 {
1286 // Option currently returns no systems. TBD
1287 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1288 systemName);
1289 return;
1290 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001291 if (systemName != "system")
1292 {
1293 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1294 systemName);
1295 return;
1296 }
1297
Ed Tanous002d39b2022-05-31 08:59:27 -07001298 // Collections don't include the static data added by SubRoute
1299 // because it has a duplicate entry for members
1300 asyncResp->res.jsonValue["@odata.type"] =
1301 "#LogServiceCollection.LogServiceCollection";
1302 asyncResp->res.jsonValue["@odata.id"] =
1303 "/redfish/v1/Systems/system/LogServices";
1304 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1305 asyncResp->res.jsonValue["Description"] =
1306 "Collection of LogServices for this Computer System";
1307 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1308 logServiceArray = nlohmann::json::array();
1309 nlohmann::json::object_t eventLog;
1310 eventLog["@odata.id"] =
1311 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001312 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001313#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001314 nlohmann::json::object_t dumpLog;
1315 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001316 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001317#endif
1318
Jason M. Billsd53dd412019-02-12 17:16:22 -08001319#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001320 nlohmann::json::object_t crashdump;
1321 crashdump["@odata.id"] =
1322 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001323 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001324#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001325
1326#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001327 nlohmann::json::object_t hostlogger;
1328 hostlogger["@odata.id"] =
1329 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001330 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001331#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001332 asyncResp->res.jsonValue["Members@odata.count"] =
1333 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001334
George Liu7a1dbc42022-12-07 16:03:22 +08001335 constexpr std::array<std::string_view, 1> interfaces = {
1336 "xyz.openbmc_project.State.Boot.PostCode"};
1337 dbus::utility::getSubTreePaths(
1338 "/", 0, interfaces,
1339 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001340 const dbus::utility::MapperGetSubTreePathsResponse&
1341 subtreePath) {
1342 if (ec)
1343 {
Ed Tanous62598e32023-07-17 17:06:25 -07001344 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001345 return;
1346 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001347
Ed Tanous002d39b2022-05-31 08:59:27 -07001348 for (const auto& pathStr : subtreePath)
1349 {
1350 if (pathStr.find("PostCode") != std::string::npos)
1351 {
1352 nlohmann::json& logServiceArrayLocal =
1353 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001354 nlohmann::json::object_t member;
1355 member["@odata.id"] =
1356 "/redfish/v1/Systems/system/LogServices/PostCodes";
1357
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001358 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001359
Ed Tanous002d39b2022-05-31 08:59:27 -07001360 asyncResp->res.jsonValue["Members@odata.count"] =
1361 logServiceArrayLocal.size();
1362 return;
1363 }
1364 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001365 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001366 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001367}
1368
1369inline void requestRoutesEventLogService(App& app)
1370{
Ed Tanous22d268c2022-05-19 09:39:07 -07001371 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001372 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001373 .methods(boost::beast::http::verb::get)(
1374 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001375 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1376 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001377 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001378 {
1379 return;
1380 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001381 if (systemName != "system")
1382 {
1383 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1384 systemName);
1385 return;
1386 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001387 asyncResp->res.jsonValue["@odata.id"] =
1388 "/redfish/v1/Systems/system/LogServices/EventLog";
1389 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05001390 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07001391 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1392 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1393 asyncResp->res.jsonValue["Id"] = "EventLog";
1394 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301395
Ed Tanous002d39b2022-05-31 08:59:27 -07001396 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001397 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301398
Ed Tanous002d39b2022-05-31 08:59:27 -07001399 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1400 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1401 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301402
Ed Tanous002d39b2022-05-31 08:59:27 -07001403 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1404 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1405 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001406
Ed Tanous002d39b2022-05-31 08:59:27 -07001407 {"target",
1408 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05001409 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001410}
1411
1412inline void requestRoutesJournalEventLogClear(App& app)
1413{
Jason M. Bills4978b632022-02-22 14:17:43 -08001414 BMCWEB_ROUTE(
1415 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001416 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001417 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001418 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001419 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001420 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1421 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001422 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001423 {
1424 return;
1425 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001426 if (systemName != "system")
1427 {
1428 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1429 systemName);
1430 return;
1431 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001432 // Clear the EventLog by deleting the log files
1433 std::vector<std::filesystem::path> redfishLogFiles;
1434 if (getRedfishLogFiles(redfishLogFiles))
1435 {
1436 for (const std::filesystem::path& file : redfishLogFiles)
1437 {
1438 std::error_code ec;
1439 std::filesystem::remove(file, ec);
1440 }
1441 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001442
Ed Tanous002d39b2022-05-31 08:59:27 -07001443 // Reload rsyslog so it knows to start new log files
1444 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001445 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001446 if (ec)
1447 {
Ed Tanous62598e32023-07-17 17:06:25 -07001448 BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001449 messages::internalError(asyncResp->res);
1450 return;
1451 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001452
Ed Tanous002d39b2022-05-31 08:59:27 -07001453 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001454 },
Ed Tanous002d39b2022-05-31 08:59:27 -07001455 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1456 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1457 "replace");
Patrick Williams5a39f772023-10-20 11:20:21 -05001458 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001459}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001460
Jason M. Billsac992cd2022-06-24 13:31:46 -07001461enum class LogParseError
1462{
1463 success,
1464 parseFailed,
1465 messageIdNotInRegistry,
1466};
1467
1468static LogParseError
1469 fillEventLogEntryJson(const std::string& logEntryID,
1470 const std::string& logEntry,
1471 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001472{
Jason M. Bills95820182019-04-22 16:25:34 -07001473 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001474 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001475 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001476 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001477 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001478 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001479 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001480 std::string timestamp = logEntry.substr(0, space);
1481 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001482 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001483 if (entryStart == std::string::npos)
1484 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001485 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001486 }
1487 std::string_view entry(logEntry);
1488 entry.remove_prefix(entryStart);
1489 // Use split to separate the entry into its fields
1490 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001491 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001492 // We need at least a MessageId to be valid
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001493 auto logEntryIter = logEntryFields.begin();
1494 if (logEntryIter == logEntryFields.end())
Jason M. Billscd225da2019-05-08 15:31:57 -07001495 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001496 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001497 }
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001498 std::string& messageID = *logEntryIter;
Jason M. Bills4851d452019-03-28 11:27:48 -07001499 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001500 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001501
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001502 logEntryIter++;
Sui Chen54417b02022-03-24 14:59:52 -07001503 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001504 {
Ed Tanous62598e32023-07-17 17:06:25 -07001505 BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001506 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001507 }
1508
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001509 std::vector<std::string_view> messageArgs(logEntryIter,
1510 logEntryFields.end());
Ed Tanousc05bba42023-06-28 08:33:29 -07001511 messageArgs.resize(message->numberOfArgs);
1512
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001513 std::string msg = redfish::registries::fillMessageArgs(messageArgs,
1514 message->message);
1515 if (msg.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001516 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001517 return LogParseError::parseFailed;
Jason M. Bills4851d452019-03-28 11:27:48 -07001518 }
1519
Jason M. Bills95820182019-04-22 16:25:34 -07001520 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1521 // format which matches the Redfish format except for the fractional seconds
1522 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001523 std::size_t dot = timestamp.find_first_of('.');
1524 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001525 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001526 {
Jason M. Bills95820182019-04-22 16:25:34 -07001527 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001528 }
1529
1530 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001531 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001532 logEntryJson["@odata.id"] = boost::urls::format(
1533 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1534 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001535 logEntryJson["Name"] = "System Event Log Entry";
1536 logEntryJson["Id"] = logEntryID;
1537 logEntryJson["Message"] = std::move(msg);
1538 logEntryJson["MessageId"] = std::move(messageID);
1539 logEntryJson["MessageArgs"] = messageArgs;
1540 logEntryJson["EntryType"] = "Event";
1541 logEntryJson["Severity"] = message->messageSeverity;
1542 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001543 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001544}
1545
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001546inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001547{
Ed Tanous22d268c2022-05-19 09:39:07 -07001548 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001549 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001550 .methods(boost::beast::http::verb::get)(
1551 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001552 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1553 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001554 query_param::QueryCapabilities capabilities = {
1555 .canDelegateTop = true,
1556 .canDelegateSkip = true,
1557 };
1558 query_param::Query delegatedQuery;
1559 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001560 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001561 {
1562 return;
1563 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001564 if constexpr (bmcwebEnableMultiHost)
1565 {
1566 // Option currently returns no systems. TBD
1567 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1568 systemName);
1569 return;
1570 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001571 if (systemName != "system")
1572 {
1573 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1574 systemName);
1575 return;
1576 }
1577
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001578 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001579 size_t skip = delegatedQuery.skip.value_or(0);
1580
Ed Tanous002d39b2022-05-31 08:59:27 -07001581 // Collections don't include the static data added by SubRoute
1582 // because it has a duplicate entry for members
1583 asyncResp->res.jsonValue["@odata.type"] =
1584 "#LogEntryCollection.LogEntryCollection";
1585 asyncResp->res.jsonValue["@odata.id"] =
1586 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1587 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1588 asyncResp->res.jsonValue["Description"] =
1589 "Collection of System Event Log Entries";
1590
1591 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1592 logEntryArray = nlohmann::json::array();
1593 // Go through the log files and create a unique ID for each
1594 // entry
1595 std::vector<std::filesystem::path> redfishLogFiles;
1596 getRedfishLogFiles(redfishLogFiles);
1597 uint64_t entryCount = 0;
1598 std::string logEntry;
1599
1600 // Oldest logs are in the last file, so start there and loop
1601 // backwards
1602 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1603 it++)
1604 {
1605 std::ifstream logStream(*it);
1606 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001607 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001608 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001609 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001610
Ed Tanous002d39b2022-05-31 08:59:27 -07001611 // Reset the unique ID on the first entry
1612 bool firstEntry = true;
1613 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001614 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001615 std::string idStr;
1616 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001617 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001618 continue;
1619 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001620 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001621
Jason M. Billsde703c52022-06-23 14:19:04 -07001622 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001623 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1624 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001625 if (status == LogParseError::messageIdNotInRegistry)
1626 {
1627 continue;
1628 }
1629 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001630 {
1631 messages::internalError(asyncResp->res);
1632 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001633 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001634
Jason M. Billsde703c52022-06-23 14:19:04 -07001635 entryCount++;
1636 // Handle paging using skip (number of entries to skip from the
1637 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001638 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001639 {
1640 continue;
1641 }
1642
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001643 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001644 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001645 }
1646 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001647 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001648 {
1649 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1650 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001651 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001652 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001653 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001654}
Chicago Duan336e96c2019-07-15 14:22:08 +08001655
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001656inline void requestRoutesJournalEventLogEntry(App& app)
1657{
1658 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001659 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001660 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001661 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001662 [&app](const crow::Request& req,
1663 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001664 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001665 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001666 {
1667 return;
1668 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001669 if constexpr (bmcwebEnableMultiHost)
1670 {
1671 // Option currently returns no systems. TBD
1672 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1673 systemName);
1674 return;
1675 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001676
1677 if (systemName != "system")
1678 {
1679 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1680 systemName);
1681 return;
1682 }
1683
Ed Tanous002d39b2022-05-31 08:59:27 -07001684 const std::string& targetID = param;
1685
1686 // Go through the log files and check the unique ID for each
1687 // entry to find the target entry
1688 std::vector<std::filesystem::path> redfishLogFiles;
1689 getRedfishLogFiles(redfishLogFiles);
1690 std::string logEntry;
1691
1692 // Oldest logs are in the last file, so start there and loop
1693 // backwards
1694 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1695 it++)
1696 {
1697 std::ifstream logStream(*it);
1698 if (!logStream.is_open())
1699 {
1700 continue;
1701 }
1702
1703 // Reset the unique ID on the first entry
1704 bool firstEntry = true;
1705 while (std::getline(logStream, logEntry))
1706 {
1707 std::string idStr;
1708 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001709 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001710 continue;
1711 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001712 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001713
1714 if (idStr == targetID)
1715 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001716 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001717 LogParseError status =
1718 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1719 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001720 {
1721 messages::internalError(asyncResp->res);
1722 return;
1723 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001724 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001725 return;
1726 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001727 }
1728 }
1729 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001730 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05001731 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001732}
1733
1734inline void requestRoutesDBusEventLogEntryCollection(App& app)
1735{
Ed Tanous22d268c2022-05-19 09:39:07 -07001736 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001737 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001738 .methods(boost::beast::http::verb::get)(
1739 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001740 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1741 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001742 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001743 {
1744 return;
1745 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001746 if constexpr (bmcwebEnableMultiHost)
1747 {
1748 // Option currently returns no systems. TBD
1749 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1750 systemName);
1751 return;
1752 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001753 if (systemName != "system")
1754 {
1755 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1756 systemName);
1757 return;
1758 }
1759
Ed Tanous002d39b2022-05-31 08:59:27 -07001760 // Collections don't include the static data added by SubRoute
1761 // because it has a duplicate entry for members
1762 asyncResp->res.jsonValue["@odata.type"] =
1763 "#LogEntryCollection.LogEntryCollection";
1764 asyncResp->res.jsonValue["@odata.id"] =
1765 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1766 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1767 asyncResp->res.jsonValue["Description"] =
1768 "Collection of System Event Log Entries";
1769
1770 // DBus implementation of EventLog/Entries
1771 // Make call to Logging Service to find all log entry objects
George Liu5eb468d2023-06-20 17:03:24 +08001772 sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1773 dbus::utility::getManagedObjects(
1774 "xyz.openbmc_project.Logging", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001775 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001776 const dbus::utility::ManagedObjectType& resp) {
1777 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001778 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001779 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07001780 BMCWEB_LOG_ERROR(
1781 "getLogEntriesIfaceData resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001782 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001783 return;
1784 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001785 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -07001786 for (const auto& objectPath : resp)
1787 {
1788 const uint32_t* id = nullptr;
1789 const uint64_t* timestamp = nullptr;
1790 const uint64_t* updateTimestamp = nullptr;
1791 const std::string* severity = nullptr;
1792 const std::string* message = nullptr;
1793 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001794 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001796 const std::string* notify = nullptr;
1797
Ed Tanous002d39b2022-05-31 08:59:27 -07001798 for (const auto& interfaceMap : objectPath.second)
1799 {
1800 if (interfaceMap.first ==
1801 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001802 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001804 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001806 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001807 id = std::get_if<uint32_t>(&propertyMap.second);
1808 }
1809 else if (propertyMap.first == "Timestamp")
1810 {
1811 timestamp =
1812 std::get_if<uint64_t>(&propertyMap.second);
1813 }
1814 else if (propertyMap.first == "UpdateTimestamp")
1815 {
1816 updateTimestamp =
1817 std::get_if<uint64_t>(&propertyMap.second);
1818 }
1819 else if (propertyMap.first == "Severity")
1820 {
1821 severity = std::get_if<std::string>(
1822 &propertyMap.second);
1823 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001824 else if (propertyMap.first == "Resolution")
1825 {
1826 resolution = std::get_if<std::string>(
1827 &propertyMap.second);
1828 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001829 else if (propertyMap.first == "Message")
1830 {
1831 message = std::get_if<std::string>(
1832 &propertyMap.second);
1833 }
1834 else if (propertyMap.first == "Resolved")
1835 {
1836 const bool* resolveptr =
1837 std::get_if<bool>(&propertyMap.second);
1838 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001839 {
1840 messages::internalError(asyncResp->res);
1841 return;
1842 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001843 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001844 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001845 else if (propertyMap.first ==
1846 "ServiceProviderNotify")
1847 {
1848 notify = std::get_if<std::string>(
1849 &propertyMap.second);
1850 if (notify == nullptr)
1851 {
1852 messages::internalError(asyncResp->res);
1853 return;
1854 }
1855 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001856 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001857 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001858 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001859 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001860 messages::internalError(asyncResp->res);
1861 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001862 }
1863 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001864 else if (interfaceMap.first ==
1865 "xyz.openbmc_project.Common.FilePath")
1866 {
1867 for (const auto& propertyMap : interfaceMap.second)
1868 {
1869 if (propertyMap.first == "Path")
1870 {
1871 filePath = std::get_if<std::string>(
1872 &propertyMap.second);
1873 }
1874 }
1875 }
1876 }
1877 // Object path without the
1878 // xyz.openbmc_project.Logging.Entry interface, ignore
1879 // and continue.
1880 if (id == nullptr || message == nullptr ||
1881 severity == nullptr || timestamp == nullptr ||
1882 updateTimestamp == nullptr)
1883 {
1884 continue;
1885 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001886 nlohmann::json& thisEntry = entriesArray.emplace_back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001887 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001888 thisEntry["@odata.id"] = boost::urls::format(
1889 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1890 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001891 thisEntry["Name"] = "System Event Log Entry";
1892 thisEntry["Id"] = std::to_string(*id);
1893 thisEntry["Message"] = *message;
1894 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001895 if ((resolution != nullptr) && (!(*resolution).empty()))
1896 {
1897 thisEntry["Resolution"] = *resolution;
1898 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001899 std::optional<bool> notifyAction =
1900 getProviderNotifyAction(*notify);
1901 if (notifyAction)
1902 {
1903 thisEntry["ServiceProviderNotified"] = *notifyAction;
1904 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001905 thisEntry["EntryType"] = "Event";
1906 thisEntry["Severity"] =
1907 translateSeverityDbusToRedfish(*severity);
1908 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001909 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001910 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001911 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001912 if (filePath != nullptr)
1913 {
1914 thisEntry["AdditionalDataURI"] =
1915 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1916 std::to_string(*id) + "/attachment";
1917 }
1918 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001919 std::ranges::sort(entriesArray, [](const nlohmann::json& left,
1920 const nlohmann::json& right) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001921 return (left["Id"] <= right["Id"]);
Ed Tanous3544d2a2023-08-06 18:12:20 -07001922 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001923 asyncResp->res.jsonValue["Members@odata.count"] =
1924 entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -07001925 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001926 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001927 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001928}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001929
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001930inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001931{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001932 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001933 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001934 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001935 .methods(boost::beast::http::verb::get)(
1936 [&app](const crow::Request& req,
1937 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001938 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001939 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001940 {
1941 return;
1942 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001943 if constexpr (bmcwebEnableMultiHost)
1944 {
1945 // Option currently returns no systems. TBD
1946 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1947 systemName);
1948 return;
1949 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001950 if (systemName != "system")
1951 {
1952 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1953 systemName);
1954 return;
1955 }
1956
Ed Tanous002d39b2022-05-31 08:59:27 -07001957 std::string entryID = param;
1958 dbus::utility::escapePathForDbus(entryID);
1959
1960 // DBus implementation of EventLog/Entries
1961 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001962 sdbusplus::asio::getAllProperties(
1963 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1964 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001965 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001966 const dbus::utility::DBusPropertiesMap& resp) {
1967 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001968 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001969 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1970 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001971 return;
1972 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001973 if (ec)
1974 {
Ed Tanous62598e32023-07-17 17:06:25 -07001975 BMCWEB_LOG_ERROR(
1976 "EventLogEntry (DBus) resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001977 messages::internalError(asyncResp->res);
1978 return;
1979 }
1980 const uint32_t* id = nullptr;
1981 const uint64_t* timestamp = nullptr;
1982 const uint64_t* updateTimestamp = nullptr;
1983 const std::string* severity = nullptr;
1984 const std::string* message = nullptr;
1985 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001986 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001987 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001988 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001989
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001990 const bool success = sdbusplus::unpackPropertiesNoThrow(
1991 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1992 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001993 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001994 "Resolution", resolution, "Path", filePath,
1995 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001996
1997 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001998 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001999 messages::internalError(asyncResp->res);
2000 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002001 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002002
Ed Tanous002d39b2022-05-31 08:59:27 -07002003 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05002004 timestamp == nullptr || updateTimestamp == nullptr ||
2005 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07002006 {
2007 messages::internalError(asyncResp->res);
2008 return;
2009 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05002010
Ed Tanous002d39b2022-05-31 08:59:27 -07002011 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05002012 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002013 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2014 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
2015 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07002016 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
2017 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
2018 asyncResp->res.jsonValue["Message"] = *message;
2019 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05002020 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
2021 if (notifyAction)
2022 {
2023 asyncResp->res.jsonValue["ServiceProviderNotified"] =
2024 *notifyAction;
2025 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05002026 if ((resolution != nullptr) && (!(*resolution).empty()))
2027 {
2028 asyncResp->res.jsonValue["Resolution"] = *resolution;
2029 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002030 asyncResp->res.jsonValue["EntryType"] = "Event";
2031 asyncResp->res.jsonValue["Severity"] =
2032 translateSeverityDbusToRedfish(*severity);
2033 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002034 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002035 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002036 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002037 if (filePath != nullptr)
2038 {
2039 asyncResp->res.jsonValue["AdditionalDataURI"] =
2040 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
2041 std::to_string(*id) + "/attachment";
2042 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002043 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002044 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002045
2046 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002047 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002048 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002049 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002050 [&app](const crow::Request& req,
2051 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002052 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002053 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 {
2055 return;
2056 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002057 if constexpr (bmcwebEnableMultiHost)
2058 {
2059 // Option currently returns no systems. TBD
2060 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2061 systemName);
2062 return;
2063 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002064 if (systemName != "system")
2065 {
2066 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2067 systemName);
2068 return;
2069 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002070 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002071
Ed Tanous002d39b2022-05-31 08:59:27 -07002072 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
2073 resolved))
2074 {
2075 return;
2076 }
Ed Tanous62598e32023-07-17 17:06:25 -07002077 BMCWEB_LOG_DEBUG("Set Resolved");
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002078
George Liu9ae226f2023-06-21 17:56:46 +08002079 sdbusplus::asio::setProperty(
2080 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
2081 "/xyz/openbmc_project/logging/entry/" + entryId,
2082 "xyz.openbmc_project.Logging.Entry", "Resolved", *resolved,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002083 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 if (ec)
2085 {
Ed Tanous62598e32023-07-17 17:06:25 -07002086 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002087 messages::internalError(asyncResp->res);
2088 return;
2089 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002090 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002091 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002092
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002093 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002094 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002095 .privileges(redfish::privileges::deleteLogEntry)
2096
Ed Tanous002d39b2022-05-31 08:59:27 -07002097 .methods(boost::beast::http::verb::delete_)(
2098 [&app](const crow::Request& req,
2099 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002100 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002101 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002102 {
2103 return;
2104 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002105 if constexpr (bmcwebEnableMultiHost)
2106 {
2107 // Option currently returns no systems. TBD
2108 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2109 systemName);
2110 return;
2111 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002112 if (systemName != "system")
2113 {
2114 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2115 systemName);
2116 return;
2117 }
Ed Tanous62598e32023-07-17 17:06:25 -07002118 BMCWEB_LOG_DEBUG("Do delete single event entries.");
Ed Tanous002d39b2022-05-31 08:59:27 -07002119
2120 std::string entryID = param;
2121
2122 dbus::utility::escapePathForDbus(entryID);
2123
2124 // Process response from Logging service.
Patrick Williams5a39f772023-10-20 11:20:21 -05002125 auto respHandler = [asyncResp,
2126 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07002127 BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07002128 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002129 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002130 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002131 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002132 messages::resourceNotFound(asyncResp->res, "LogEntry",
2133 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07002134 return;
2135 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002136 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07002137 BMCWEB_LOG_ERROR(
2138 "EventLogEntry (DBus) doDelete respHandler got error {}",
2139 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002140 asyncResp->res.result(
2141 boost::beast::http::status::internal_server_error);
2142 return;
2143 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002144
Ed Tanous002d39b2022-05-31 08:59:27 -07002145 asyncResp->res.result(boost::beast::http::status::ok);
2146 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002147
Ed Tanous002d39b2022-05-31 08:59:27 -07002148 // Make call to Logging service to request Delete Log
2149 crow::connections::systemBus->async_method_call(
2150 respHandler, "xyz.openbmc_project.Logging",
2151 "/xyz/openbmc_project/logging/entry/" + entryID,
2152 "xyz.openbmc_project.Object.Delete", "Delete");
Patrick Williams5a39f772023-10-20 11:20:21 -05002153 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002154}
2155
Spencer Kub7028eb2021-10-26 15:27:35 +08002156constexpr const char* hostLoggerFolderPath = "/var/log/console";
2157
2158inline bool
2159 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2160 std::vector<std::filesystem::path>& hostLoggerFiles)
2161{
2162 std::error_code ec;
2163 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2164 if (ec)
2165 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002166 BMCWEB_LOG_WARNING("{}", ec.message());
Spencer Kub7028eb2021-10-26 15:27:35 +08002167 return false;
2168 }
2169 for (const std::filesystem::directory_entry& it : logPath)
2170 {
2171 std::string filename = it.path().filename();
2172 // Prefix of each log files is "log". Find the file and save the
2173 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002174 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002175 {
2176 hostLoggerFiles.emplace_back(it.path());
2177 }
2178 }
2179 // As the log files rotate, they are appended with a ".#" that is higher for
2180 // the older logs. Since we start from oldest logs, sort the name in
2181 // descending order.
2182 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2183 AlphanumLess<std::string>());
2184
2185 return true;
2186}
2187
Ed Tanous02cad962022-06-30 16:50:15 -07002188inline bool getHostLoggerEntries(
2189 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2190 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002191{
2192 GzFileReader logFile;
2193
2194 // Go though all log files and expose host logs.
2195 for (const std::filesystem::path& it : hostLoggerFiles)
2196 {
2197 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2198 {
Ed Tanous62598e32023-07-17 17:06:25 -07002199 BMCWEB_LOG_ERROR("fail to expose host logs");
Spencer Kub7028eb2021-10-26 15:27:35 +08002200 return false;
2201 }
2202 }
2203 // Get lastMessage from constructor by getter
2204 std::string lastMessage = logFile.getLastMessage();
2205 if (!lastMessage.empty())
2206 {
2207 logCount++;
2208 if (logCount > skip && logCount <= (skip + top))
2209 {
2210 logEntries.push_back(lastMessage);
2211 }
2212 }
2213 return true;
2214}
2215
2216inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2217 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002218 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002219{
2220 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002221 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002222 logEntryJson["@odata.id"] = boost::urls::format(
2223 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2224 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002225 logEntryJson["Name"] = "Host Logger Entry";
2226 logEntryJson["Id"] = logEntryID;
2227 logEntryJson["Message"] = msg;
2228 logEntryJson["EntryType"] = "Oem";
2229 logEntryJson["Severity"] = "OK";
2230 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002231}
2232
2233inline void requestRoutesSystemHostLogger(App& app)
2234{
Ed Tanous22d268c2022-05-19 09:39:07 -07002235 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002236 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002237 .methods(boost::beast::http::verb::get)(
2238 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002239 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2240 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002241 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002242 {
2243 return;
2244 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002245 if constexpr (bmcwebEnableMultiHost)
2246 {
2247 // Option currently returns no systems. TBD
2248 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2249 systemName);
2250 return;
2251 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002252 if (systemName != "system")
2253 {
2254 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2255 systemName);
2256 return;
2257 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002258 asyncResp->res.jsonValue["@odata.id"] =
2259 "/redfish/v1/Systems/system/LogServices/HostLogger";
2260 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002261 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002262 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2263 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2264 asyncResp->res.jsonValue["Id"] = "HostLogger";
2265 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2266 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002267 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002268}
2269
2270inline void requestRoutesSystemHostLoggerCollection(App& app)
2271{
2272 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002273 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002274 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002275 .methods(boost::beast::http::verb::get)(
2276 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002277 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2278 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002279 query_param::QueryCapabilities capabilities = {
2280 .canDelegateTop = true,
2281 .canDelegateSkip = true,
2282 };
2283 query_param::Query delegatedQuery;
2284 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002285 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002286 {
2287 return;
2288 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002289 if constexpr (bmcwebEnableMultiHost)
2290 {
2291 // Option currently returns no systems. TBD
2292 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2293 systemName);
2294 return;
2295 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002296 if (systemName != "system")
2297 {
2298 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2299 systemName);
2300 return;
2301 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002302 asyncResp->res.jsonValue["@odata.id"] =
2303 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2304 asyncResp->res.jsonValue["@odata.type"] =
2305 "#LogEntryCollection.LogEntryCollection";
2306 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2307 asyncResp->res.jsonValue["Description"] =
2308 "Collection of HostLogger Entries";
2309 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2310 logEntryArray = nlohmann::json::array();
2311 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002312
Ed Tanous002d39b2022-05-31 08:59:27 -07002313 std::vector<std::filesystem::path> hostLoggerFiles;
2314 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2315 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002316 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002317 return;
2318 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002319 // If we weren't provided top and skip limits, use the defaults.
2320 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002321 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002322 size_t logCount = 0;
2323 // This vector only store the entries we want to expose that
2324 // control by skip and top.
2325 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002326 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2327 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002328 {
2329 messages::internalError(asyncResp->res);
2330 return;
2331 }
2332 // If vector is empty, that means skip value larger than total
2333 // log count
2334 if (logEntries.empty())
2335 {
2336 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2337 return;
2338 }
2339 if (!logEntries.empty())
2340 {
2341 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002342 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002343 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002344 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2345 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002346 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002347 }
2348
Ed Tanous002d39b2022-05-31 08:59:27 -07002349 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002350 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002351 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002352 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2353 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002354 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002355 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002356 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002357 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002358}
2359
2360inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2361{
2362 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002363 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002364 .privileges(redfish::privileges::getLogEntry)
2365 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002366 [&app](const crow::Request& req,
2367 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002368 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002369 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002370 {
2371 return;
2372 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002373 if constexpr (bmcwebEnableMultiHost)
2374 {
2375 // Option currently returns no systems. TBD
2376 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2377 systemName);
2378 return;
2379 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002380 if (systemName != "system")
2381 {
2382 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2383 systemName);
2384 return;
2385 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002386 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002387
Ed Tanous002d39b2022-05-31 08:59:27 -07002388 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002389
Patrick Williams84396af2023-05-11 11:47:45 -05002390 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2391 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002392 if (ec == std::errc::invalid_argument ||
2393 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002394 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002395 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002396 return;
2397 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002398
Ed Tanous002d39b2022-05-31 08:59:27 -07002399 std::vector<std::filesystem::path> hostLoggerFiles;
2400 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2401 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002402 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002403 return;
2404 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002405
Ed Tanous002d39b2022-05-31 08:59:27 -07002406 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002407 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002408 std::vector<std::string> logEntries;
2409 // We can get specific entry by skip and top. For example, if we
2410 // want to get nth entry, we can set skip = n-1 and top = 1 to
2411 // get that entry
2412 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2413 logCount))
2414 {
2415 messages::internalError(asyncResp->res);
2416 return;
2417 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002418
Ed Tanous002d39b2022-05-31 08:59:27 -07002419 if (!logEntries.empty())
2420 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002421 nlohmann::json::object_t hostLogEntry;
2422 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2423 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002424 return;
2425 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002426
Ed Tanous002d39b2022-05-31 08:59:27 -07002427 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002428 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Patrick Williams5a39f772023-10-20 11:20:21 -05002429 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002430}
2431
Claire Weinandd72e872022-08-15 14:20:06 -07002432inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002433 crow::App& app, const crow::Request& req,
2434 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2435{
2436 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2437 {
2438 return;
2439 }
2440 // Collections don't include the static data added by SubRoute
2441 // because it has a duplicate entry for members
2442 asyncResp->res.jsonValue["@odata.type"] =
2443 "#LogServiceCollection.LogServiceCollection";
2444 asyncResp->res.jsonValue["@odata.id"] =
2445 "/redfish/v1/Managers/bmc/LogServices";
2446 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2447 asyncResp->res.jsonValue["Description"] =
2448 "Collection of LogServices for this Manager";
2449 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2450 logServiceArray = nlohmann::json::array();
2451
2452#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002453 nlohmann::json::object_t journal;
2454 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002455 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002456#endif
2457
2458 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2459
2460#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002461 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002462 "xyz.openbmc_project.Collection.DeleteAll"};
2463 dbus::utility::getSubTreePaths(
2464 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002465 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002466 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002467 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2468 if (ec)
2469 {
Ed Tanous62598e32023-07-17 17:06:25 -07002470 BMCWEB_LOG_ERROR(
2471 "handleBMCLogServicesCollectionGet respHandler got error {}",
2472 ec);
Claire Weinanfdd26902022-03-01 14:18:25 -08002473 // Assume that getting an error simply means there are no dump
2474 // LogServices. Return without adding any error response.
2475 return;
2476 }
2477
2478 nlohmann::json& logServiceArrayLocal =
2479 asyncResp->res.jsonValue["Members"];
2480
2481 for (const std::string& path : subTreePaths)
2482 {
2483 if (path == "/xyz/openbmc_project/dump/bmc")
2484 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002485 nlohmann::json::object_t member;
2486 member["@odata.id"] =
2487 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002488 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002489 }
2490 else if (path == "/xyz/openbmc_project/dump/faultlog")
2491 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002492 nlohmann::json::object_t member;
2493 member["@odata.id"] =
2494 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002495 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002496 }
2497 }
2498
2499 asyncResp->res.jsonValue["Members@odata.count"] =
2500 logServiceArrayLocal.size();
Patrick Williams5a39f772023-10-20 11:20:21 -05002501 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002502#endif
2503}
2504
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002505inline void requestRoutesBMCLogServiceCollection(App& app)
2506{
2507 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002508 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002509 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002510 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002511}
Ed Tanous1da66f72018-07-27 16:13:37 -07002512
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002513inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002514{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002515 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002516 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002517 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002518 [&app](const crow::Request& req,
2519 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002520 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002521 {
2522 return;
2523 }
2524 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002525 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002526 asyncResp->res.jsonValue["@odata.id"] =
2527 "/redfish/v1/Managers/bmc/LogServices/Journal";
2528 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2529 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002530 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002531 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302532
Ed Tanous002d39b2022-05-31 08:59:27 -07002533 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002534 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002535 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2536 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2537 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302538
Ed Tanous002d39b2022-05-31 08:59:27 -07002539 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2540 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002541 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002542}
Jason M. Billse1f26342018-07-18 12:12:00 -07002543
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002544static int
2545 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2546 sd_journal* journal,
2547 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002548{
2549 // Get the Log Entry contents
2550 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002551
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002552 std::string message;
2553 std::string_view syslogID;
2554 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2555 if (ret < 0)
2556 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002557 BMCWEB_LOG_DEBUG("Failed to read SYSLOG_IDENTIFIER field: {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002558 strerror(-ret));
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002559 }
2560 if (!syslogID.empty())
2561 {
2562 message += std::string(syslogID) + ": ";
2563 }
2564
Ed Tanous39e77502019-03-04 17:35:53 -08002565 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002566 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002567 if (ret < 0)
2568 {
Ed Tanous62598e32023-07-17 17:06:25 -07002569 BMCWEB_LOG_ERROR("Failed to read MESSAGE field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002570 return 1;
2571 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002572 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002573
2574 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002575 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002576 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002577 if (ret < 0)
2578 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002579 BMCWEB_LOG_DEBUG("Failed to read PRIORITY field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002580 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002581
2582 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002583 std::string entryTimeStr;
2584 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002585 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002586 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002587 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002588
2589 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002590 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002591 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2592 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002593 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002594 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2595 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2596 bmcJournalLogEntryJson["Message"] = std::move(message);
2597 bmcJournalLogEntryJson["EntryType"] = "Oem";
2598 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2599 : severity <= 4 ? "Warning"
2600 : "OK";
2601 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2602 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002603 return 0;
2604}
2605
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002606inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002607{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002608 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002609 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002610 .methods(boost::beast::http::verb::get)(
2611 [&app](const crow::Request& req,
2612 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2613 query_param::QueryCapabilities capabilities = {
2614 .canDelegateTop = true,
2615 .canDelegateSkip = true,
2616 };
2617 query_param::Query delegatedQuery;
2618 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002619 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002620 {
2621 return;
2622 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002623
2624 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002625 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002626
Ed Tanous002d39b2022-05-31 08:59:27 -07002627 // Collections don't include the static data added by SubRoute
2628 // because it has a duplicate entry for members
2629 asyncResp->res.jsonValue["@odata.type"] =
2630 "#LogEntryCollection.LogEntryCollection";
2631 asyncResp->res.jsonValue["@odata.id"] =
2632 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2633 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2634 asyncResp->res.jsonValue["Description"] =
2635 "Collection of BMC Journal Entries";
2636 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2637 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002638
Ed Tanous002d39b2022-05-31 08:59:27 -07002639 // Go through the journal and use the timestamp to create a
2640 // unique ID for each entry
2641 sd_journal* journalTmp = nullptr;
2642 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2643 if (ret < 0)
2644 {
Ed Tanous62598e32023-07-17 17:06:25 -07002645 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002646 messages::internalError(asyncResp->res);
2647 return;
2648 }
2649 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2650 journalTmp, sd_journal_close);
2651 journalTmp = nullptr;
2652 uint64_t entryCount = 0;
2653 // Reset the unique ID on the first entry
2654 bool firstEntry = true;
2655 SD_JOURNAL_FOREACH(journal.get())
2656 {
2657 entryCount++;
2658 // Handle paging using skip (number of entries to skip from
2659 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002660 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002661 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002662 continue;
2663 }
2664
2665 std::string idStr;
2666 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2667 {
2668 continue;
2669 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002670 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002671
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002672 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002673 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2674 bmcJournalLogEntry) != 0)
2675 {
George Liu0fda0f12021-11-16 10:06:17 +08002676 messages::internalError(asyncResp->res);
2677 return;
2678 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002679 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002680 }
2681 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002682 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002683 {
2684 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2685 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002686 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002687 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002688 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002689}
Jason M. Billse1f26342018-07-18 12:12:00 -07002690
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002691inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002692{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002693 BMCWEB_ROUTE(app,
2694 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002695 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002696 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002697 [&app](const crow::Request& req,
2698 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2699 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002700 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002701 {
2702 return;
2703 }
2704 // Convert the unique ID back to a timestamp to find the entry
Myung Bae75e8e212023-11-30 12:53:46 -08002705 sd_id128_t bootID{};
Ed Tanous002d39b2022-05-31 08:59:27 -07002706 uint64_t ts = 0;
2707 uint64_t index = 0;
Myung Bae75e8e212023-11-30 12:53:46 -08002708 if (!getTimestampFromID(asyncResp, entryID, bootID, ts, index))
Ed Tanous002d39b2022-05-31 08:59:27 -07002709 {
2710 return;
2711 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002712
Ed Tanous002d39b2022-05-31 08:59:27 -07002713 sd_journal* journalTmp = nullptr;
2714 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2715 if (ret < 0)
2716 {
Ed Tanous62598e32023-07-17 17:06:25 -07002717 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002718 messages::internalError(asyncResp->res);
2719 return;
2720 }
2721 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2722 journalTmp, sd_journal_close);
2723 journalTmp = nullptr;
2724 // Go to the timestamp in the log and move to the entry at the
2725 // index tracking the unique ID
2726 std::string idStr;
2727 bool firstEntry = true;
Myung Bae75e8e212023-11-30 12:53:46 -08002728 ret = sd_journal_seek_monotonic_usec(journal.get(), bootID, ts);
Ed Tanous002d39b2022-05-31 08:59:27 -07002729 if (ret < 0)
2730 {
Ed Tanous62598e32023-07-17 17:06:25 -07002731 BMCWEB_LOG_ERROR("failed to seek to an entry in journal{}",
2732 strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002733 messages::internalError(asyncResp->res);
2734 return;
2735 }
2736 for (uint64_t i = 0; i <= index; i++)
2737 {
2738 sd_journal_next(journal.get());
2739 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2740 {
2741 messages::internalError(asyncResp->res);
2742 return;
2743 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002744 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002745 }
2746 // Confirm that the entry ID matches what was requested
2747 if (idStr != entryID)
2748 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002749 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002750 return;
2751 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002752
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002753 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002754 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002755 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002756 {
2757 messages::internalError(asyncResp->res);
2758 return;
2759 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002760 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Patrick Williams5a39f772023-10-20 11:20:21 -05002761 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002762}
2763
Claire Weinanfdd26902022-03-01 14:18:25 -08002764inline void
2765 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2766 const std::string& dumpType)
2767{
2768 std::string dumpPath;
2769 std::string overWritePolicy;
2770 bool collectDiagnosticDataSupported = false;
2771
2772 if (dumpType == "BMC")
2773 {
2774 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2775 overWritePolicy = "WrapsWhenFull";
2776 collectDiagnosticDataSupported = true;
2777 }
2778 else if (dumpType == "FaultLog")
2779 {
2780 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2781 overWritePolicy = "Unknown";
2782 collectDiagnosticDataSupported = false;
2783 }
2784 else if (dumpType == "System")
2785 {
2786 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2787 overWritePolicy = "WrapsWhenFull";
2788 collectDiagnosticDataSupported = true;
2789 }
2790 else
2791 {
Ed Tanous62598e32023-07-17 17:06:25 -07002792 BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
2793 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -08002794 messages::internalError(asyncResp->res);
2795 return;
2796 }
2797
2798 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2799 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2800 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2801 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2802 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2803 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2804
2805 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002806 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002807 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2808 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2809 redfishDateTimeOffset.second;
2810
2811 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002812
2813 if (collectDiagnosticDataSupported)
2814 {
2815 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2816 ["target"] =
2817 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2818 }
Claire Weinan0d946212022-07-13 19:40:19 -07002819
2820 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2821 dbus::utility::getSubTreePaths(
2822 "/xyz/openbmc_project/dump", 0, interfaces,
2823 [asyncResp, dumpType, dumpPath](
2824 const boost::system::error_code& ec,
2825 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2826 if (ec)
2827 {
Ed Tanous62598e32023-07-17 17:06:25 -07002828 BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
Claire Weinan0d946212022-07-13 19:40:19 -07002829 // Assume that getting an error simply means there are no dump
2830 // LogServices. Return without adding any error response.
2831 return;
2832 }
Ed Tanous18f8f602023-07-18 10:07:23 -07002833 std::string dbusDumpPath = getDumpPath(dumpType);
Claire Weinan0d946212022-07-13 19:40:19 -07002834 for (const std::string& path : subTreePaths)
2835 {
2836 if (path == dbusDumpPath)
2837 {
2838 asyncResp->res
2839 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2840 dumpPath + "/Actions/LogService.ClearLog";
2841 break;
2842 }
2843 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002844 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002845}
2846
2847inline void handleLogServicesDumpServiceGet(
2848 crow::App& app, const std::string& dumpType, const crow::Request& req,
2849 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2850{
2851 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2852 {
2853 return;
2854 }
2855 getDumpServiceInfo(asyncResp, dumpType);
2856}
2857
Ed Tanous22d268c2022-05-19 09:39:07 -07002858inline void handleLogServicesDumpServiceComputerSystemGet(
2859 crow::App& app, const crow::Request& req,
2860 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2861 const std::string& chassisId)
2862{
2863 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2864 {
2865 return;
2866 }
2867 if (chassisId != "system")
2868 {
2869 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2870 return;
2871 }
2872 getDumpServiceInfo(asyncResp, "System");
2873}
2874
Claire Weinanfdd26902022-03-01 14:18:25 -08002875inline void handleLogServicesDumpEntriesCollectionGet(
2876 crow::App& app, const std::string& dumpType, const crow::Request& req,
2877 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2878{
2879 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2880 {
2881 return;
2882 }
2883 getDumpEntryCollection(asyncResp, dumpType);
2884}
2885
Ed Tanous22d268c2022-05-19 09:39:07 -07002886inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2887 crow::App& app, const crow::Request& req,
2888 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2889 const std::string& chassisId)
2890{
2891 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2892 {
2893 return;
2894 }
2895 if (chassisId != "system")
2896 {
2897 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2898 return;
2899 }
2900 getDumpEntryCollection(asyncResp, "System");
2901}
2902
Claire Weinanfdd26902022-03-01 14:18:25 -08002903inline void handleLogServicesDumpEntryGet(
2904 crow::App& app, const std::string& dumpType, const crow::Request& req,
2905 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2906 const std::string& dumpId)
2907{
2908 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2909 {
2910 return;
2911 }
2912 getDumpEntryById(asyncResp, dumpId, dumpType);
2913}
Carson Labrado168d1b12023-03-27 17:04:46 +00002914
Ed Tanous22d268c2022-05-19 09:39:07 -07002915inline void handleLogServicesDumpEntryComputerSystemGet(
2916 crow::App& app, const crow::Request& req,
2917 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2918 const std::string& chassisId, const std::string& dumpId)
2919{
2920 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2921 {
2922 return;
2923 }
2924 if (chassisId != "system")
2925 {
2926 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2927 return;
2928 }
2929 getDumpEntryById(asyncResp, dumpId, "System");
2930}
Claire Weinanfdd26902022-03-01 14:18:25 -08002931
2932inline void handleLogServicesDumpEntryDelete(
2933 crow::App& app, const std::string& dumpType, const crow::Request& req,
2934 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2935 const std::string& dumpId)
2936{
2937 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2938 {
2939 return;
2940 }
2941 deleteDumpEntry(asyncResp, dumpId, dumpType);
2942}
2943
Ed Tanous22d268c2022-05-19 09:39:07 -07002944inline void handleLogServicesDumpEntryComputerSystemDelete(
2945 crow::App& app, const crow::Request& req,
2946 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2947 const std::string& chassisId, const std::string& dumpId)
2948{
2949 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2950 {
2951 return;
2952 }
2953 if (chassisId != "system")
2954 {
2955 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2956 return;
2957 }
2958 deleteDumpEntry(asyncResp, dumpId, "System");
2959}
2960
Carson Labrado168d1b12023-03-27 17:04:46 +00002961inline void handleLogServicesDumpEntryDownloadGet(
2962 crow::App& app, const std::string& dumpType, const crow::Request& req,
2963 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2964 const std::string& dumpId)
2965{
2966 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2967 {
2968 return;
2969 }
2970 downloadDumpEntry(asyncResp, dumpId, dumpType);
2971}
2972
2973inline void handleDBusEventLogEntryDownloadGet(
2974 crow::App& app, const std::string& dumpType, const crow::Request& req,
2975 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2976 const std::string& systemName, const std::string& entryID)
2977{
2978 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2979 {
2980 return;
2981 }
2982 if (!http_helpers::isContentTypeAllowed(
2983 req.getHeaderValue("Accept"),
2984 http_helpers::ContentType::OctetStream, true))
2985 {
2986 asyncResp->res.result(boost::beast::http::status::bad_request);
2987 return;
2988 }
2989 downloadEventLogEntry(asyncResp, systemName, entryID, dumpType);
2990}
2991
Claire Weinanfdd26902022-03-01 14:18:25 -08002992inline void handleLogServicesDumpCollectDiagnosticDataPost(
2993 crow::App& app, const std::string& dumpType, const crow::Request& req,
2994 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2995{
2996 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2997 {
2998 return;
2999 }
3000 createDump(asyncResp, req, dumpType);
3001}
3002
Ed Tanous22d268c2022-05-19 09:39:07 -07003003inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
3004 crow::App& app, const crow::Request& req,
3005 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003006 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003007{
3008 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3009 {
3010 return;
3011 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003012
3013 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003014 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003015 // Option currently returns no systems. TBD
3016 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3017 systemName);
3018 return;
3019 }
3020 if (systemName != "system")
3021 {
3022 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3023 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003024 return;
3025 }
3026 createDump(asyncResp, req, "System");
3027}
3028
Claire Weinanfdd26902022-03-01 14:18:25 -08003029inline void handleLogServicesDumpClearLogPost(
3030 crow::App& app, const std::string& dumpType, const crow::Request& req,
3031 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3032{
3033 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3034 {
3035 return;
3036 }
3037 clearDump(asyncResp, dumpType);
3038}
3039
Ed Tanous22d268c2022-05-19 09:39:07 -07003040inline void handleLogServicesDumpClearLogComputerSystemPost(
3041 crow::App& app, const crow::Request& req,
3042 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003043 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003044{
3045 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3046 {
3047 return;
3048 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003049 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003050 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003051 // Option currently returns no systems. TBD
3052 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3053 systemName);
3054 return;
3055 }
3056 if (systemName != "system")
3057 {
3058 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3059 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003060 return;
3061 }
3062 clearDump(asyncResp, "System");
3063}
3064
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003065inline void requestRoutesBMCDumpService(App& app)
3066{
3067 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003068 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003069 .methods(boost::beast::http::verb::get)(std::bind_front(
3070 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003071}
3072
3073inline void requestRoutesBMCDumpEntryCollection(App& app)
3074{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003075 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003076 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08003077 .methods(boost::beast::http::verb::get)(std::bind_front(
3078 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003079}
3080
3081inline void requestRoutesBMCDumpEntry(App& app)
3082{
3083 BMCWEB_ROUTE(app,
3084 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003085 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003086 .methods(boost::beast::http::verb::get)(std::bind_front(
3087 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
3088
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003089 BMCWEB_ROUTE(app,
3090 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003091 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003092 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3093 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003094}
3095
Carson Labrado168d1b12023-03-27 17:04:46 +00003096inline void requestRoutesBMCDumpEntryDownload(App& app)
3097{
3098 BMCWEB_ROUTE(
3099 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003100 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003101 .privileges(redfish::privileges::getLogEntry)
3102 .methods(boost::beast::http::verb::get)(std::bind_front(
3103 handleLogServicesDumpEntryDownloadGet, std::ref(app), "BMC"));
3104}
3105
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003106inline void requestRoutesBMCDumpCreate(App& app)
3107{
George Liu0fda0f12021-11-16 10:06:17 +08003108 BMCWEB_ROUTE(
3109 app,
3110 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003111 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003112 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08003113 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
3114 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003115}
3116
3117inline void requestRoutesBMCDumpClear(App& app)
3118{
George Liu0fda0f12021-11-16 10:06:17 +08003119 BMCWEB_ROUTE(
3120 app,
3121 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003122 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003123 .methods(boost::beast::http::verb::post)(std::bind_front(
3124 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
3125}
3126
Carson Labrado168d1b12023-03-27 17:04:46 +00003127inline void requestRoutesDBusEventLogEntryDownload(App& app)
3128{
3129 BMCWEB_ROUTE(
3130 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003131 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003132 .privileges(redfish::privileges::getLogEntry)
3133 .methods(boost::beast::http::verb::get)(std::bind_front(
3134 handleDBusEventLogEntryDownloadGet, std::ref(app), "System"));
3135}
3136
Claire Weinanfdd26902022-03-01 14:18:25 -08003137inline void requestRoutesFaultLogDumpService(App& app)
3138{
3139 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3140 .privileges(redfish::privileges::getLogService)
3141 .methods(boost::beast::http::verb::get)(std::bind_front(
3142 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3143}
3144
3145inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3146{
3147 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3148 .privileges(redfish::privileges::getLogEntryCollection)
3149 .methods(boost::beast::http::verb::get)(
3150 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3151 std::ref(app), "FaultLog"));
3152}
3153
3154inline void requestRoutesFaultLogDumpEntry(App& app)
3155{
3156 BMCWEB_ROUTE(app,
3157 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3158 .privileges(redfish::privileges::getLogEntry)
3159 .methods(boost::beast::http::verb::get)(std::bind_front(
3160 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3161
3162 BMCWEB_ROUTE(app,
3163 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3164 .privileges(redfish::privileges::deleteLogEntry)
3165 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3166 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3167}
3168
3169inline void requestRoutesFaultLogDumpClear(App& app)
3170{
3171 BMCWEB_ROUTE(
3172 app,
3173 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3174 .privileges(redfish::privileges::postLogService)
3175 .methods(boost::beast::http::verb::post)(std::bind_front(
3176 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003177}
3178
3179inline void requestRoutesSystemDumpService(App& app)
3180{
Ed Tanous22d268c2022-05-19 09:39:07 -07003181 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003182 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003183 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003184 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003185}
3186
3187inline void requestRoutesSystemDumpEntryCollection(App& app)
3188{
Ed Tanous22d268c2022-05-19 09:39:07 -07003189 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003190 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07003191 .methods(boost::beast::http::verb::get)(std::bind_front(
3192 handleLogServicesDumpEntriesCollectionComputerSystemGet,
3193 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003194}
3195
3196inline void requestRoutesSystemDumpEntry(App& app)
3197{
3198 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003199 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003200 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003201 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003202 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003203
3204 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003205 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003206 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003207 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003208 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003209}
3210
3211inline void requestRoutesSystemDumpCreate(App& app)
3212{
George Liu0fda0f12021-11-16 10:06:17 +08003213 BMCWEB_ROUTE(
3214 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003215 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003216 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003217 .methods(boost::beast::http::verb::post)(std::bind_front(
3218 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3219 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003220}
3221
3222inline void requestRoutesSystemDumpClear(App& app)
3223{
George Liu0fda0f12021-11-16 10:06:17 +08003224 BMCWEB_ROUTE(
3225 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003226 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003227 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003228 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003229 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003230}
3231
3232inline void requestRoutesCrashdumpService(App& app)
3233{
3234 // Note: Deviated from redfish privilege registry for GET & HEAD
3235 // method for security reasons.
3236 /**
3237 * Functions triggers appropriate requests on DBus
3238 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003239 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003240 // This is incorrect, should be:
3241 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003242 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003243 .methods(boost::beast::http::verb::get)(
3244 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3246 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003247 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003248 {
3249 return;
3250 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003251 if constexpr (bmcwebEnableMultiHost)
3252 {
3253 // Option currently returns no systems. TBD
3254 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3255 systemName);
3256 return;
3257 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003258 if (systemName != "system")
3259 {
3260 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3261 systemName);
3262 return;
3263 }
3264
Ed Tanous002d39b2022-05-31 08:59:27 -07003265 // Copy over the static data to include the entries added by
3266 // SubRoute
3267 asyncResp->res.jsonValue["@odata.id"] =
3268 "/redfish/v1/Systems/system/LogServices/Crashdump";
3269 asyncResp->res.jsonValue["@odata.type"] =
3270 "#LogService.v1_2_0.LogService";
3271 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3272 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303273 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003274 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3275 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303276
Ed Tanous002d39b2022-05-31 08:59:27 -07003277 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003278 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003279 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3280 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3281 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303282
Ed Tanous002d39b2022-05-31 08:59:27 -07003283 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003284 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003285 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3286 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3287 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3288 ["target"] =
3289 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
Patrick Williams5a39f772023-10-20 11:20:21 -05003290 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003291}
3292
3293void inline requestRoutesCrashdumpClear(App& app)
3294{
George Liu0fda0f12021-11-16 10:06:17 +08003295 BMCWEB_ROUTE(
3296 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003297 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003298 // This is incorrect, should be:
3299 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003300 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003301 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003302 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003303 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3304 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003305 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003306 {
3307 return;
3308 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003309 if constexpr (bmcwebEnableMultiHost)
3310 {
3311 // Option currently returns no systems. TBD
3312 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3313 systemName);
3314 return;
3315 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003316 if (systemName != "system")
3317 {
3318 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3319 systemName);
3320 return;
3321 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003322 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003323 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003324 const std::string&) {
3325 if (ec)
3326 {
3327 messages::internalError(asyncResp->res);
3328 return;
3329 }
3330 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003331 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003332 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003333 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003334}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003335
zhanghch058d1b46d2021-04-01 11:18:24 +08003336static void
3337 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3338 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003339{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003340 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003341 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003342 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003343 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003344 if (ec)
3345 {
Ed Tanous62598e32023-07-17 17:06:25 -07003346 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003347 if (ec.value() ==
3348 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003349 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003350 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003351 }
3352 else
3353 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003354 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003355 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003356 return;
3357 }
3358
3359 std::string timestamp{};
3360 std::string filename{};
3361 std::string logfile{};
3362 parseCrashdumpParameters(params, filename, timestamp, logfile);
3363
3364 if (filename.empty() || timestamp.empty())
3365 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003366 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003367 return;
3368 }
3369
3370 std::string crashdumpURI =
3371 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3372 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003373 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003374 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003375 logEntry["@odata.id"] = boost::urls::format(
3376 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3377 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003378 logEntry["Name"] = "CPU Crashdump";
3379 logEntry["Id"] = logID;
3380 logEntry["EntryType"] = "Oem";
3381 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3382 logEntry["DiagnosticDataType"] = "OEM";
3383 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3384 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003385
3386 // If logEntryJson references an array of LogEntry resources
3387 // ('Members' list), then push this as a new entry, otherwise set it
3388 // directly
3389 if (logEntryJson.is_array())
3390 {
3391 logEntryJson.push_back(logEntry);
3392 asyncResp->res.jsonValue["Members@odata.count"] =
3393 logEntryJson.size();
3394 }
3395 else
3396 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003397 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003398 }
3399 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003400 sdbusplus::asio::getAllProperties(
3401 *crow::connections::systemBus, crashdumpObject,
3402 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3403 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003404}
3405
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003406inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003407{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003408 // Note: Deviated from redfish privilege registry for GET & HEAD
3409 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003410 /**
3411 * Functions triggers appropriate requests on DBus
3412 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003413 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003414 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003415 // This is incorrect, should be.
3416 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003417 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003418 .methods(boost::beast::http::verb::get)(
3419 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003420 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3421 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003422 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003423 {
3424 return;
3425 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003426 if constexpr (bmcwebEnableMultiHost)
3427 {
3428 // Option currently returns no systems. TBD
3429 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3430 systemName);
3431 return;
3432 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003433 if (systemName != "system")
3434 {
3435 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3436 systemName);
3437 return;
3438 }
3439
George Liu7a1dbc42022-12-07 16:03:22 +08003440 constexpr std::array<std::string_view, 1> interfaces = {
3441 crashdumpInterface};
3442 dbus::utility::getSubTreePaths(
3443 "/", 0, interfaces,
3444 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003445 const std::vector<std::string>& resp) {
3446 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003447 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003448 if (ec.value() !=
3449 boost::system::errc::no_such_file_or_directory)
3450 {
Ed Tanous62598e32023-07-17 17:06:25 -07003451 BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
3452 ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003453 messages::internalError(asyncResp->res);
3454 return;
3455 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003456 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003457 asyncResp->res.jsonValue["@odata.type"] =
3458 "#LogEntryCollection.LogEntryCollection";
3459 asyncResp->res.jsonValue["@odata.id"] =
3460 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3461 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3462 asyncResp->res.jsonValue["Description"] =
3463 "Collection of Crashdump Entries";
3464 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3465 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003466
Ed Tanous002d39b2022-05-31 08:59:27 -07003467 for (const std::string& path : resp)
3468 {
3469 const sdbusplus::message::object_path objPath(path);
3470 // Get the log ID
3471 std::string logID = objPath.filename();
3472 if (logID.empty())
3473 {
3474 continue;
3475 }
3476 // Add the log entry to the array
3477 logCrashdumpEntry(asyncResp, logID,
3478 asyncResp->res.jsonValue["Members"]);
3479 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003480 });
Patrick Williams5a39f772023-10-20 11:20:21 -05003481 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003482}
Ed Tanous1da66f72018-07-27 16:13:37 -07003483
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003484inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003485{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003486 // Note: Deviated from redfish privilege registry for GET & HEAD
3487 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003488
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003489 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003490 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003491 // this is incorrect, should be
3492 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003493 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003494 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003495 [&app](const crow::Request& req,
3496 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003497 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003498 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003499 {
3500 return;
3501 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003502 if constexpr (bmcwebEnableMultiHost)
3503 {
3504 // Option currently returns no systems. TBD
3505 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3506 systemName);
3507 return;
3508 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003509 if (systemName != "system")
3510 {
3511 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3512 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003513 return;
3514 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003515 const std::string& logID = param;
3516 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
Patrick Williams5a39f772023-10-20 11:20:21 -05003517 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003518}
Ed Tanous1da66f72018-07-27 16:13:37 -07003519
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003520inline void requestRoutesCrashdumpFile(App& app)
3521{
3522 // Note: Deviated from redfish privilege registry for GET & HEAD
3523 // method for security reasons.
3524 BMCWEB_ROUTE(
3525 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003526 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003527 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003528 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003529 [](const crow::Request& req,
3530 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003531 const std::string& systemName, const std::string& logID,
3532 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003533 // Do not call getRedfishRoute here since the crashdump file is not a
3534 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003535
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003536 if constexpr (bmcwebEnableMultiHost)
3537 {
3538 // Option currently returns no systems. TBD
3539 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3540 systemName);
3541 return;
3542 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003543 if (systemName != "system")
3544 {
3545 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3546 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003547 return;
3548 }
3549
Ed Tanous002d39b2022-05-31 08:59:27 -07003550 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003551 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003552 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003553 const std::vector<
3554 std::pair<std::string, dbus::utility::DbusVariantType>>&
3555 resp) {
3556 if (ec)
3557 {
Ed Tanous62598e32023-07-17 17:06:25 -07003558 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003559 messages::internalError(asyncResp->res);
3560 return;
3561 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003562
Ed Tanous002d39b2022-05-31 08:59:27 -07003563 std::string dbusFilename{};
3564 std::string dbusTimestamp{};
3565 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003566
Ed Tanous002d39b2022-05-31 08:59:27 -07003567 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3568 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003569
Ed Tanous002d39b2022-05-31 08:59:27 -07003570 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3571 dbusFilepath.empty())
3572 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003573 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003574 return;
3575 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003576
Ed Tanous002d39b2022-05-31 08:59:27 -07003577 // Verify the file name parameter is correct
3578 if (fileName != dbusFilename)
3579 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003580 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003581 return;
3582 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003583
Ed Tanous27b0cf92023-08-07 12:02:40 -07003584 if (!asyncResp->res.openFile(dbusFilepath))
Ed Tanous002d39b2022-05-31 08:59:27 -07003585 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003586 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003587 return;
3588 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003589
Ed Tanous002d39b2022-05-31 08:59:27 -07003590 // Configure this to be a file download when accessed
3591 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003592 asyncResp->res.addHeader(
3593 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003594 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003595 sdbusplus::asio::getAllProperties(
3596 *crow::connections::systemBus, crashdumpObject,
3597 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3598 std::move(getStoredLogCallback));
Patrick Williams5a39f772023-10-20 11:20:21 -05003599 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003600}
3601
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003602enum class OEMDiagnosticType
3603{
3604 onDemand,
3605 telemetry,
3606 invalid,
3607};
3608
Ed Tanous26ccae32023-02-16 10:28:44 -08003609inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003610{
3611 if (oemDiagStr == "OnDemand")
3612 {
3613 return OEMDiagnosticType::onDemand;
3614 }
3615 if (oemDiagStr == "Telemetry")
3616 {
3617 return OEMDiagnosticType::telemetry;
3618 }
3619
3620 return OEMDiagnosticType::invalid;
3621}
3622
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003623inline void requestRoutesCrashdumpCollect(App& app)
3624{
3625 // Note: Deviated from redfish privilege registry for GET & HEAD
3626 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003627 BMCWEB_ROUTE(
3628 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003629 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003630 // The below is incorrect; Should be ConfigureManager
3631 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003632 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003633 .methods(boost::beast::http::verb::post)(
3634 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003635 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3636 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003637 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003638 {
3639 return;
3640 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003641
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003642 if constexpr (bmcwebEnableMultiHost)
3643 {
3644 // Option currently returns no systems. TBD
3645 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3646 systemName);
3647 return;
3648 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003649 if (systemName != "system")
3650 {
3651 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3652 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003653 return;
3654 }
3655
Ed Tanous002d39b2022-05-31 08:59:27 -07003656 std::string diagnosticDataType;
3657 std::string oemDiagnosticDataType;
3658 if (!redfish::json_util::readJsonAction(
3659 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3660 "OEMDiagnosticDataType", oemDiagnosticDataType))
3661 {
3662 return;
3663 }
3664
3665 if (diagnosticDataType != "OEM")
3666 {
Ed Tanous62598e32023-07-17 17:06:25 -07003667 BMCWEB_LOG_ERROR(
3668 "Only OEM DiagnosticDataType supported for Crashdump");
Ed Tanous002d39b2022-05-31 08:59:27 -07003669 messages::actionParameterValueFormatError(
3670 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3671 "CollectDiagnosticData");
3672 return;
3673 }
3674
3675 OEMDiagnosticType oemDiagType =
3676 getOEMDiagnosticType(oemDiagnosticDataType);
3677
3678 std::string iface;
3679 std::string method;
3680 std::string taskMatchStr;
3681 if (oemDiagType == OEMDiagnosticType::onDemand)
3682 {
3683 iface = crashdumpOnDemandInterface;
3684 method = "GenerateOnDemandLog";
3685 taskMatchStr = "type='signal',"
3686 "interface='org.freedesktop.DBus.Properties',"
3687 "member='PropertiesChanged',"
3688 "arg0namespace='com.intel.crashdump'";
3689 }
3690 else if (oemDiagType == OEMDiagnosticType::telemetry)
3691 {
3692 iface = crashdumpTelemetryInterface;
3693 method = "GenerateTelemetryLog";
3694 taskMatchStr = "type='signal',"
3695 "interface='org.freedesktop.DBus.Properties',"
3696 "member='PropertiesChanged',"
3697 "arg0namespace='com.intel.crashdump'";
3698 }
3699 else
3700 {
Ed Tanous62598e32023-07-17 17:06:25 -07003701 BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
3702 oemDiagnosticDataType);
Ed Tanous002d39b2022-05-31 08:59:27 -07003703 messages::actionParameterValueFormatError(
3704 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3705 "CollectDiagnosticData");
3706 return;
3707 }
3708
3709 auto collectCrashdumpCallback =
3710 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003711 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003712 const std::string&) mutable {
3713 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003714 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003715 if (ec.value() == boost::system::errc::operation_not_supported)
3716 {
3717 messages::resourceInStandby(asyncResp->res);
3718 }
3719 else if (ec.value() ==
3720 boost::system::errc::device_or_resource_busy)
3721 {
3722 messages::serviceTemporarilyUnavailable(asyncResp->res,
3723 "60");
3724 }
3725 else
3726 {
3727 messages::internalError(asyncResp->res);
3728 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003729 return;
3730 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003731 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -07003732 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003733 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07003734 if (!ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -07003735 {
3736 taskData->messages.emplace_back(messages::taskCompletedOK(
3737 std::to_string(taskData->index)));
3738 taskData->state = "Completed";
3739 }
3740 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05003741 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003742 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003743
Ed Tanous002d39b2022-05-31 08:59:27 -07003744 task->startTimer(std::chrono::minutes(5));
3745 task->populateResp(asyncResp->res);
3746 task->payload.emplace(std::move(payload));
3747 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003748
Ed Tanous002d39b2022-05-31 08:59:27 -07003749 crow::connections::systemBus->async_method_call(
3750 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3751 iface, method);
Patrick Williams5a39f772023-10-20 11:20:21 -05003752 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003753}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003754
Andrew Geisslercb92c032018-08-17 07:56:14 -07003755/**
3756 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3757 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003758inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003759{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003760 /**
3761 * Function handles POST method request.
3762 * The Clear Log actions does not require any parameter.The action deletes
3763 * all entries found in the Entries collection for this Log Service.
3764 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003765
George Liu0fda0f12021-11-16 10:06:17 +08003766 BMCWEB_ROUTE(
3767 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003768 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003769 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003770 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003771 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003772 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3773 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003774 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003775 {
3776 return;
3777 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003778 if constexpr (bmcwebEnableMultiHost)
3779 {
3780 // Option currently returns no systems. TBD
3781 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3782 systemName);
3783 return;
3784 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003785 if (systemName != "system")
3786 {
3787 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3788 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003789 return;
3790 }
Ed Tanous62598e32023-07-17 17:06:25 -07003791 BMCWEB_LOG_DEBUG("Do delete all entries.");
Andrew Geisslercb92c032018-08-17 07:56:14 -07003792
Ed Tanous002d39b2022-05-31 08:59:27 -07003793 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003794 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07003795 BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07003796 if (ec)
3797 {
3798 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003799 BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003800 asyncResp->res.result(
3801 boost::beast::http::status::internal_server_error);
3802 return;
3803 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003804
Ed Tanous002d39b2022-05-31 08:59:27 -07003805 asyncResp->res.result(boost::beast::http::status::no_content);
3806 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003807
Ed Tanous002d39b2022-05-31 08:59:27 -07003808 // Make call to Logging service to request Clear Log
3809 crow::connections::systemBus->async_method_call(
3810 respHandler, "xyz.openbmc_project.Logging",
3811 "/xyz/openbmc_project/logging",
3812 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003813 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003814}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003815
3816/****************************************************
3817 * Redfish PostCode interfaces
3818 * using DBUS interface: getPostCodesTS
3819 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003820inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003821{
Ed Tanous22d268c2022-05-19 09:39:07 -07003822 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003823 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003824 .methods(boost::beast::http::verb::get)(
3825 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003826 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3827 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003828 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003829 {
3830 return;
3831 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003832 if constexpr (bmcwebEnableMultiHost)
3833 {
3834 // Option currently returns no systems. TBD
3835 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3836 systemName);
3837 return;
3838 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003839 if (systemName != "system")
3840 {
3841 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3842 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003843 return;
3844 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003845 asyncResp->res.jsonValue["@odata.id"] =
3846 "/redfish/v1/Systems/system/LogServices/PostCodes";
3847 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05003848 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07003849 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3850 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003851 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003852 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3853 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3854 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303855
Ed Tanous002d39b2022-05-31 08:59:27 -07003856 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003857 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003858 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3859 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3860 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303861
Ed Tanous002d39b2022-05-31 08:59:27 -07003862 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3863 {"target",
3864 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05003865 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003866}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003867
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003868inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003869{
George Liu0fda0f12021-11-16 10:06:17 +08003870 BMCWEB_ROUTE(
3871 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003872 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003873 // The following privilege is incorrect; It should be ConfigureManager
3874 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003875 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003876 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003877 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003878 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3879 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003880 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003881 {
3882 return;
3883 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003884 if constexpr (bmcwebEnableMultiHost)
3885 {
3886 // Option currently returns no systems. TBD
3887 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3888 systemName);
3889 return;
3890 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003891 if (systemName != "system")
3892 {
3893 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3894 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003895 return;
3896 }
Ed Tanous62598e32023-07-17 17:06:25 -07003897 BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003898
Ed Tanous002d39b2022-05-31 08:59:27 -07003899 // Make call to post-code service to request clear all
3900 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003901 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003902 if (ec)
3903 {
3904 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003905 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
3906 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003907 asyncResp->res.result(
3908 boost::beast::http::status::internal_server_error);
3909 messages::internalError(asyncResp->res);
3910 return;
3911 }
Tony Lee18fc70c2023-08-24 16:15:54 +08003912 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003913 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003914 "xyz.openbmc_project.State.Boot.PostCode0",
3915 "/xyz/openbmc_project/State/Boot/PostCode0",
3916 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003917 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003918}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003919
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003920/**
3921 * @brief Parse post code ID and get the current value and index value
3922 * eg: postCodeID=B1-2, currentValue=1, index=2
3923 *
3924 * @param[in] postCodeID Post Code ID
3925 * @param[out] currentValue Current value
3926 * @param[out] index Index value
3927 *
3928 * @return bool true if the parsing is successful, false the parsing fails
3929 */
3930inline static bool parsePostCode(const std::string& postCodeID,
3931 uint64_t& currentValue, uint16_t& index)
3932{
3933 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003934 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003935 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3936 {
3937 return false;
3938 }
3939
Patrick Williams84396af2023-05-11 11:47:45 -05003940 auto start = std::next(split[0].begin());
3941 auto end = split[0].end();
3942 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003943
Patrick Williams84396af2023-05-11 11:47:45 -05003944 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003945 {
3946 return false;
3947 }
3948
Patrick Williams84396af2023-05-11 11:47:45 -05003949 start = split[1].begin();
3950 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003951
Patrick Williams84396af2023-05-11 11:47:45 -05003952 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003953
Patrick Williams84396af2023-05-11 11:47:45 -05003954 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003955}
3956
3957static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003958 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303959 const boost::container::flat_map<
3960 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003961 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3962 const uint64_t skip = 0, const uint64_t top = 0)
3963{
3964 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003965 const registries::Message* message =
3966 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003967
3968 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003969 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303970 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3971 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003972 {
3973 currentCodeIndex++;
3974 std::string postcodeEntryID =
3975 "B" + std::to_string(bootIndex) + "-" +
3976 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3977
3978 uint64_t usecSinceEpoch = code.first;
3979 uint64_t usTimeOffset = 0;
3980
3981 if (1 == currentCodeIndex)
3982 { // already incremented
3983 firstCodeTimeUs = code.first;
3984 }
3985 else
3986 {
3987 usTimeOffset = code.first - firstCodeTimeUs;
3988 }
3989
3990 // skip if no specific codeIndex is specified and currentCodeIndex does
3991 // not fall between top and skip
3992 if ((codeIndex == 0) &&
3993 (currentCodeIndex <= skip || currentCodeIndex > top))
3994 {
3995 continue;
3996 }
3997
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003998 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003999 // currentIndex
4000 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
4001 {
4002 // This is done for simplicity. 1st entry is needed to calculate
4003 // time offset. To improve efficiency, one can get to the entry
4004 // directly (possibly with flatmap's nth method)
4005 continue;
4006 }
4007
4008 // currentCodeIndex is within top and skip or equal to specified code
4009 // index
4010
4011 // Get the Created time from the timestamp
4012 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03004013 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004014
4015 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
4016 std::ostringstream hexCode;
4017 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304018 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004019 std::ostringstream timeOffsetStr;
4020 // Set Fixed -Point Notation
4021 timeOffsetStr << std::fixed;
4022 // Set precision to 4 digits
4023 timeOffsetStr << std::setprecision(4);
4024 // Add double to stream
4025 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004026
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004027 std::string bootIndexStr = std::to_string(bootIndex);
4028 std::string timeOffsetString = timeOffsetStr.str();
4029 std::string hexCodeStr = hexCode.str();
4030
4031 std::array<std::string_view, 3> messageArgs = {
4032 bootIndexStr, timeOffsetString, hexCodeStr};
4033
4034 std::string msg =
4035 redfish::registries::fillMessageArgs(messageArgs, message->message);
4036 if (msg.empty())
ZhikuiRena3316fc2020-01-29 14:58:08 -08004037 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004038 messages::internalError(asyncResp->res);
4039 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004040 }
4041
Tim Leed4342a92020-04-27 11:47:58 +08004042 // Get Severity template from message registry
4043 std::string severity;
4044 if (message != nullptr)
4045 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08004046 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08004047 }
4048
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004049 // Format entry
4050 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05004051 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07004052 bmcLogEntry["@odata.id"] = boost::urls::format(
4053 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
4054 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07004055 bmcLogEntry["Name"] = "POST Code Log Entry";
4056 bmcLogEntry["Id"] = postcodeEntryID;
4057 bmcLogEntry["Message"] = std::move(msg);
4058 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004059 bmcLogEntry["MessageArgs"] = messageArgs;
Jason M. Bills84afc482022-06-24 12:38:23 -07004060 bmcLogEntry["EntryType"] = "Event";
4061 bmcLogEntry["Severity"] = std::move(severity);
4062 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08004063 if (!std::get<std::vector<uint8_t>>(code.second).empty())
4064 {
4065 bmcLogEntry["AdditionalDataURI"] =
4066 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
4067 postcodeEntryID + "/attachment";
4068 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004069
4070 // codeIndex is only specified when querying single entry, return only
4071 // that entry in this case
4072 if (codeIndex != 0)
4073 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004074 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004075 return true;
4076 }
4077
Ed Tanousac106bf2023-06-07 09:24:59 -07004078 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05004079 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08004080 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004081
4082 // Return value is always false when querying multiple entries
4083 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004084}
4085
Ed Tanousac106bf2023-06-07 09:24:59 -07004086static void
4087 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4088 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004089{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004090 uint16_t bootIndex = 0;
4091 uint64_t codeIndex = 0;
4092 if (!parsePostCode(entryId, codeIndex, bootIndex))
4093 {
4094 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07004095 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004096 return;
4097 }
4098
4099 if (bootIndex == 0 || codeIndex == 0)
4100 {
4101 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07004102 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004103 return;
4104 }
4105
ZhikuiRena3316fc2020-01-29 14:58:08 -08004106 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004107 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004108 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304109 const boost::container::flat_map<
4110 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4111 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004112 if (ec)
4113 {
Ed Tanous62598e32023-07-17 17:06:25 -07004114 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004115 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004116 return;
4117 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004118
Ed Tanous002d39b2022-05-31 08:59:27 -07004119 if (postcode.empty())
4120 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004121 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07004122 return;
4123 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004124
Ed Tanousac106bf2023-06-07 09:24:59 -07004125 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004126 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004127 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004128 return;
4129 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004130 },
Jonathan Doman15124762021-01-07 17:54:17 -08004131 "xyz.openbmc_project.State.Boot.PostCode0",
4132 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004133 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4134 bootIndex);
4135}
4136
Ed Tanousac106bf2023-06-07 09:24:59 -07004137static void
4138 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4139 const uint16_t bootIndex, const uint16_t bootCount,
4140 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004141{
4142 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004143 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004144 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304145 const boost::container::flat_map<
4146 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4147 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004148 if (ec)
4149 {
Ed Tanous62598e32023-07-17 17:06:25 -07004150 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004151 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004152 return;
4153 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004154
Ed Tanous002d39b2022-05-31 08:59:27 -07004155 uint64_t endCount = entryCount;
4156 if (!postcode.empty())
4157 {
4158 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07004159 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004160 {
Patrick Williams89492a12023-05-10 07:51:34 -05004161 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
4162 entryCount) -
4163 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004164 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07004165 std::min(static_cast<uint64_t>(top + skip), endCount) -
4166 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004167
Ed Tanousac106bf2023-06-07 09:24:59 -07004168 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4169 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004170 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004171 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004172 }
4173
4174 // continue to previous bootIndex
4175 if (bootIndex < bootCount)
4176 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004177 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07004178 bootCount, endCount, skip, top);
4179 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08004180 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07004181 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004182 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07004183 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4184 std::to_string(skip + top);
4185 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004186 },
Jonathan Doman15124762021-01-07 17:54:17 -08004187 "xyz.openbmc_project.State.Boot.PostCode0",
4188 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004189 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4190 bootIndex);
4191}
4192
zhanghch058d1b46d2021-04-01 11:18:24 +08004193static void
Ed Tanousac106bf2023-06-07 09:24:59 -07004194 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07004195 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004196{
4197 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004198 sdbusplus::asio::getProperty<uint16_t>(
4199 *crow::connections::systemBus,
4200 "xyz.openbmc_project.State.Boot.PostCode0",
4201 "/xyz/openbmc_project/State/Boot/PostCode0",
4202 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07004203 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
4204 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004205 if (ec)
4206 {
Ed Tanous62598e32023-07-17 17:06:25 -07004207 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -07004208 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004209 return;
4210 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004211 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004212 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08004213}
4214
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004215inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004216{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004217 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004218 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07004219 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004220 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004221 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07004222 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4223 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004224 query_param::QueryCapabilities capabilities = {
4225 .canDelegateTop = true,
4226 .canDelegateSkip = true,
4227 };
4228 query_param::Query delegatedQuery;
4229 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00004230 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07004231 {
4232 return;
4233 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004234 if constexpr (bmcwebEnableMultiHost)
4235 {
4236 // Option currently returns no systems. TBD
4237 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4238 systemName);
4239 return;
4240 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004241
4242 if (systemName != "system")
4243 {
4244 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4245 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004246 return;
4247 }
Ed Tanous002d39b2022-05-31 08:59:27 -07004248 asyncResp->res.jsonValue["@odata.type"] =
4249 "#LogEntryCollection.LogEntryCollection";
4250 asyncResp->res.jsonValue["@odata.id"] =
4251 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4252 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4253 asyncResp->res.jsonValue["Description"] =
4254 "Collection of POST Code Log Entries";
4255 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4256 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07004257 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08004258 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07004259 getCurrentBootNumber(asyncResp, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004260 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004261}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004262
George Liu647b3cd2021-07-05 12:43:56 +08004263inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4264{
George Liu0fda0f12021-11-16 10:06:17 +08004265 BMCWEB_ROUTE(
4266 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004267 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004268 .privileges(redfish::privileges::getLogEntry)
4269 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004270 [&app](const crow::Request& req,
4271 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004272 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004273 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004274 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004275 {
4276 return;
4277 }
Matt Spinler72e21372023-04-19 12:53:33 -05004278 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004279 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004280 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004281 {
4282 asyncResp->res.result(boost::beast::http::status::bad_request);
4283 return;
4284 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004285 if constexpr (bmcwebEnableMultiHost)
4286 {
4287 // Option currently returns no systems. TBD
4288 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4289 systemName);
4290 return;
4291 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004292 if (systemName != "system")
4293 {
4294 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4295 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004296 return;
4297 }
George Liu647b3cd2021-07-05 12:43:56 +08004298
Ed Tanous002d39b2022-05-31 08:59:27 -07004299 uint64_t currentValue = 0;
4300 uint16_t index = 0;
4301 if (!parsePostCode(postCodeID, currentValue, index))
4302 {
4303 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4304 return;
4305 }
George Liu647b3cd2021-07-05 12:43:56 +08004306
Ed Tanous002d39b2022-05-31 08:59:27 -07004307 crow::connections::systemBus->async_method_call(
4308 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004309 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004310 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4311 postcodes) {
4312 if (ec.value() == EBADR)
4313 {
4314 messages::resourceNotFound(asyncResp->res, "LogEntry",
4315 postCodeID);
4316 return;
4317 }
4318 if (ec)
4319 {
Ed Tanous62598e32023-07-17 17:06:25 -07004320 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07004321 messages::internalError(asyncResp->res);
4322 return;
4323 }
George Liu647b3cd2021-07-05 12:43:56 +08004324
Ed Tanous002d39b2022-05-31 08:59:27 -07004325 size_t value = static_cast<size_t>(currentValue) - 1;
4326 if (value == std::string::npos || postcodes.size() < currentValue)
4327 {
Ed Tanous62598e32023-07-17 17:06:25 -07004328 BMCWEB_LOG_WARNING("Wrong currentValue value");
Ed Tanous002d39b2022-05-31 08:59:27 -07004329 messages::resourceNotFound(asyncResp->res, "LogEntry",
4330 postCodeID);
4331 return;
4332 }
George Liu647b3cd2021-07-05 12:43:56 +08004333
Ed Tanous002d39b2022-05-31 08:59:27 -07004334 const auto& [tID, c] = postcodes[value];
4335 if (c.empty())
4336 {
Ed Tanous62598e32023-07-17 17:06:25 -07004337 BMCWEB_LOG_WARNING("No found post code data");
Ed Tanous002d39b2022-05-31 08:59:27 -07004338 messages::resourceNotFound(asyncResp->res, "LogEntry",
4339 postCodeID);
4340 return;
4341 }
4342 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4343 const char* d = reinterpret_cast<const char*>(c.data());
4344 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004345
Ed Tanousd9f6c622022-03-17 09:12:17 -07004346 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004347 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004348 asyncResp->res.addHeader(
4349 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous27b0cf92023-08-07 12:02:40 -07004350 asyncResp->res.write(crow::utility::base64encode(strData));
Patrick Williams5a39f772023-10-20 11:20:21 -05004351 },
Ed Tanous002d39b2022-05-31 08:59:27 -07004352 "xyz.openbmc_project.State.Boot.PostCode0",
4353 "/xyz/openbmc_project/State/Boot/PostCode0",
4354 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
Patrick Williams5a39f772023-10-20 11:20:21 -05004355 });
George Liu647b3cd2021-07-05 12:43:56 +08004356}
4357
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004358inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004359{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004360 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004361 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004362 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004363 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004364 [&app](const crow::Request& req,
4365 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004366 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004367 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004368 {
4369 return;
4370 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004371 if constexpr (bmcwebEnableMultiHost)
4372 {
4373 // Option currently returns no systems. TBD
4374 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4375 systemName);
4376 return;
4377 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004378 if (systemName != "system")
4379 {
4380 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4381 systemName);
4382 return;
4383 }
4384
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004385 getPostCodeForEntry(asyncResp, targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05004386 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004387}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004388
Ed Tanous1da66f72018-07-27 16:13:37 -07004389} // namespace redfish