blob: 24f0251ee18c1f2bdb7d2c2a84fbc7c869073e41 [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>
Abhilash Rajub5f288d2023-11-08 22:32:44 -060048#include <cstddef>
James Feist4418c7f2019-04-15 11:09:15 -070049#include <filesystem>
Ed Tanous18f8f602023-07-18 10:07:23 -070050#include <iterator>
Xiaochao Ma75710de2021-01-21 17:56:02 +080051#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070052#include <ranges>
Ed Tanous26702d02021-11-03 15:02:33 -070053#include <span>
Ed Tanous18f8f602023-07-18 10:07:23 -070054#include <string>
Jason M. Billscd225da2019-05-08 15:31:57 -070055#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080056#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070057
58namespace redfish
59{
60
Patrick Williams89492a12023-05-10 07:51:34 -050061constexpr const char* crashdumpObject = "com.intel.crashdump";
62constexpr const char* crashdumpPath = "/com/intel/crashdump";
63constexpr const char* crashdumpInterface = "com.intel.crashdump";
64constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070065 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050066constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070067 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050068constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070069 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070070
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060071enum class DumpCreationProgress
72{
73 DUMP_CREATE_SUCCESS,
74 DUMP_CREATE_FAILED,
75 DUMP_CREATE_INPROGRESS
76};
77
James Feistf6150402019-01-08 10:36:20 -080078namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070079
Gunnar Mills1214b7e2020-06-04 10:11:30 -050080inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070081{
Ed Tanousd4d25792020-09-29 15:15:03 -070082 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
83 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
84 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
85 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070086 {
87 return "Critical";
88 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070089 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
90 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
91 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070092 {
93 return "OK";
94 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070095 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070096 {
97 return "Warning";
98 }
99 return "";
100}
101
Abhishek Patel9017faf2021-09-14 22:48:55 -0500102inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
103{
104 std::optional<bool> notifyAction;
105 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
106 {
107 notifyAction = true;
108 }
109 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
110 {
111 notifyAction = false;
112 }
113
114 return notifyAction;
115}
116
Ed Tanous18f8f602023-07-18 10:07:23 -0700117inline std::string getDumpPath(std::string_view dumpType)
118{
119 std::string dbusDumpPath = "/xyz/openbmc_project/dump/";
120 std::ranges::transform(dumpType, std::back_inserter(dbusDumpPath),
121 bmcweb::asciiToLower);
122
123 return dbusDumpPath;
124}
125
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700126inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800127 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700128 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700129{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500130 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700131 size_t length = 0;
132 int ret = 0;
133 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800134 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
135 const void** dataVoid = reinterpret_cast<const void**>(&data);
136
137 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700138 if (ret < 0)
139 {
140 return ret;
141 }
Ed Tanous39e77502019-03-04 17:35:53 -0800142 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700143 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000144 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700145 return ret;
146}
147
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700148inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800149 std::string_view field, const int& base,
150 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700151{
152 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800153 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700154 // Get the metadata from the requested field of the journal entry
155 ret = getJournalMetadata(journal, field, metadata);
156 if (ret < 0)
157 {
158 return ret;
159 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000160 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700161 return ret;
162}
163
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700164inline static bool getEntryTimestamp(sd_journal* journal,
165 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800166{
167 int ret = 0;
168 uint64_t timestamp = 0;
169 ret = sd_journal_get_realtime_usec(journal, &timestamp);
170 if (ret < 0)
171 {
Ed Tanous62598e32023-07-17 17:06:25 -0700172 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
ZhikuiRena3316fc2020-01-29 14:58:08 -0800173 return false;
174 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300175 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500176 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800177}
Ed Tanous50b8a432022-02-03 16:29:50 -0800178
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700179inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
180 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700181{
182 int ret = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800183 static sd_id128_t prevBootID{};
Jason M. Bills16428a12018-11-02 12:42:29 -0700184 static uint64_t prevTs = 0;
185 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700186 if (firstEntry)
187 {
Myung Bae75e8e212023-11-30 12:53:46 -0800188 prevBootID = {};
Jason M. Billse85d6b12019-07-29 17:01:15 -0700189 prevTs = 0;
190 }
191
Jason M. Bills16428a12018-11-02 12:42:29 -0700192 // Get the entry timestamp
193 uint64_t curTs = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800194 sd_id128_t curBootID{};
195 ret = sd_journal_get_monotonic_usec(journal, &curTs, &curBootID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700196 if (ret < 0)
197 {
Ed Tanous62598e32023-07-17 17:06:25 -0700198 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
Jason M. Bills16428a12018-11-02 12:42:29 -0700199 return false;
200 }
Myung Bae75e8e212023-11-30 12:53:46 -0800201 // If the timestamp isn't unique on the same boot, increment the index
202 bool sameBootIDs = sd_id128_equal(curBootID, prevBootID) != 0;
203 if (sameBootIDs && (curTs == prevTs))
Jason M. Bills16428a12018-11-02 12:42:29 -0700204 {
205 index++;
206 }
207 else
208 {
209 // Otherwise, reset it
210 index = 0;
211 }
Myung Bae75e8e212023-11-30 12:53:46 -0800212
213 if (!sameBootIDs)
214 {
215 // Save the bootID
216 prevBootID = curBootID;
217 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700218 // Save the timestamp
219 prevTs = curTs;
220
Myung Bae75e8e212023-11-30 12:53:46 -0800221 // make entryID as <bootID>_<timestamp>[_<index>]
222 std::array<char, SD_ID128_STRING_MAX> bootIDStr{};
223 sd_id128_to_string(curBootID, bootIDStr.data());
224 entryID = std::format("{}_{}", bootIDStr.data(), curTs);
Jason M. Bills16428a12018-11-02 12:42:29 -0700225 if (index > 0)
226 {
227 entryID += "_" + std::to_string(index);
228 }
229 return true;
230}
231
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500232static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700233 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700234{
Ed Tanous271584a2019-07-09 16:24:22 -0700235 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700236 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700237 if (firstEntry)
238 {
239 prevTs = 0;
240 }
241
Jason M. Bills95820182019-04-22 16:25:34 -0700242 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700243 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700244 std::tm timeStruct = {};
245 std::istringstream entryStream(logEntry);
246 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
247 {
248 curTs = std::mktime(&timeStruct);
249 }
250 // If the timestamp isn't unique, increment the index
251 if (curTs == prevTs)
252 {
253 index++;
254 }
255 else
256 {
257 // Otherwise, reset it
258 index = 0;
259 }
260 // Save the timestamp
261 prevTs = curTs;
262
263 entryID = std::to_string(curTs);
264 if (index > 0)
265 {
266 entryID += "_" + std::to_string(index);
267 }
268 return true;
269}
270
Myung Bae75e8e212023-11-30 12:53:46 -0800271// Entry is formed like "BootID_timestamp" or "BootID_timestamp_index"
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700272inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800273 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Myung Bae75e8e212023-11-30 12:53:46 -0800274 const std::string& entryID, sd_id128_t& bootID,
275 uint64_t& timestamp, uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700276{
277 if (entryID.empty())
278 {
279 return false;
280 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700281
Myung Bae75e8e212023-11-30 12:53:46 -0800282 // Convert the unique ID back to a bootID + timestamp to find the entry
283 std::string_view entryIDStrView(entryID);
284 auto underscore1Pos = entryIDStrView.find('_');
285 if (underscore1Pos == std::string_view::npos)
286 {
287 // EntryID has no bootID or timestamp
288 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
289 return false;
290 }
291
292 // EntryID has bootID + timestamp
293
294 // Convert entryIDViewString to BootID
295 // NOTE: bootID string which needs to be null-terminated for
296 // sd_id128_from_string()
297 std::string bootIDStr(entryID, 0, underscore1Pos);
298 if (sd_id128_from_string(bootIDStr.c_str(), &bootID) < 0)
299 {
300 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
301 return false;
302 }
303
304 // Get the timestamp from entryID
305 std::string_view timestampStrView = entryIDStrView;
306 timestampStrView.remove_prefix(underscore1Pos + 1);
307
308 // Check the index in timestamp
309 auto underscore2Pos = timestampStrView.find('_');
310 if (underscore2Pos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700311 {
312 // Timestamp has an index
Myung Bae75e8e212023-11-30 12:53:46 -0800313 timestampStrView.remove_suffix(timestampStrView.size() -
314 underscore2Pos);
315 std::string_view indexStr(timestampStrView);
316 indexStr.remove_prefix(underscore2Pos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500317 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
318 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700319 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700320 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800321 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700322 return false;
323 }
324 }
Myung Bae75e8e212023-11-30 12:53:46 -0800325
326 // Now timestamp has no index
327 auto [ptr, ec] = std::from_chars(timestampStrView.begin(),
328 timestampStrView.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700329 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700330 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800331 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700332 return false;
333 }
334 return true;
335}
336
Jason M. Bills95820182019-04-22 16:25:34 -0700337static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500338 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700339{
340 static const std::filesystem::path redfishLogDir = "/var/log";
341 static const std::string redfishLogFilename = "redfish";
342
343 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500344 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700345 std::filesystem::directory_iterator(redfishLogDir))
346 {
347 // If we find a redfish log file, save the path
348 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700349 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700350 {
351 redfishLogFiles.emplace_back(redfishLogDir / filename);
352 }
353 }
354 // As the log files rotate, they are appended with a ".#" that is higher for
355 // the older logs. Since we don't expect more than 10 log files, we
356 // can just sort the list to get them in order from newest to oldest
Ed Tanous3544d2a2023-08-06 18:12:20 -0700357 std::ranges::sort(redfishLogFiles);
Jason M. Bills95820182019-04-22 16:25:34 -0700358
359 return !redfishLogFiles.empty();
360}
361
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600362inline log_entry::OriginatorTypes
363 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
364{
365 if (originatorType ==
366 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
367 {
368 return log_entry::OriginatorTypes::Client;
369 }
370 if (originatorType ==
371 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
372 {
373 return log_entry::OriginatorTypes::Internal;
374 }
375 if (originatorType ==
376 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
377 {
378 return log_entry::OriginatorTypes::SupportingService;
379 }
380 return log_entry::OriginatorTypes::Invalid;
381}
382
Claire Weinanaefe3782022-07-15 19:17:19 -0700383inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800384 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700385 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600386 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700387 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
388{
389 for (const auto& interfaceMap : object.second)
390 {
391 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
392 {
393 for (const auto& propertyMap : interfaceMap.second)
394 {
395 if (propertyMap.first == "Status")
396 {
397 const auto* status =
398 std::get_if<std::string>(&propertyMap.second);
399 if (status == nullptr)
400 {
401 messages::internalError(asyncResp->res);
402 break;
403 }
404 dumpStatus = *status;
405 }
406 }
407 }
408 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
409 {
410 for (const auto& propertyMap : interfaceMap.second)
411 {
412 if (propertyMap.first == "Size")
413 {
414 const auto* sizePtr =
415 std::get_if<uint64_t>(&propertyMap.second);
416 if (sizePtr == nullptr)
417 {
418 messages::internalError(asyncResp->res);
419 break;
420 }
421 size = *sizePtr;
422 break;
423 }
424 }
425 }
426 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
427 {
428 for (const auto& propertyMap : interfaceMap.second)
429 {
430 if (propertyMap.first == "Elapsed")
431 {
432 const uint64_t* usecsTimeStamp =
433 std::get_if<uint64_t>(&propertyMap.second);
434 if (usecsTimeStamp == nullptr)
435 {
436 messages::internalError(asyncResp->res);
437 break;
438 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700439 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700440 break;
441 }
442 }
443 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600444 else if (interfaceMap.first ==
445 "xyz.openbmc_project.Common.OriginatedBy")
446 {
447 for (const auto& propertyMap : interfaceMap.second)
448 {
449 if (propertyMap.first == "OriginatorId")
450 {
451 const std::string* id =
452 std::get_if<std::string>(&propertyMap.second);
453 if (id == nullptr)
454 {
455 messages::internalError(asyncResp->res);
456 break;
457 }
458 originatorId = *id;
459 }
460
461 if (propertyMap.first == "OriginatorType")
462 {
463 const std::string* type =
464 std::get_if<std::string>(&propertyMap.second);
465 if (type == nullptr)
466 {
467 messages::internalError(asyncResp->res);
468 break;
469 }
470
471 originatorType = mapDbusOriginatorTypeToRedfish(*type);
472 if (originatorType == log_entry::OriginatorTypes::Invalid)
473 {
474 messages::internalError(asyncResp->res);
475 break;
476 }
477 }
478 }
479 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700480 }
481}
482
Nan Zhou21ab4042022-06-26 23:07:40 +0000483static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800484{
485 std::string entriesPath;
486
487 if (dumpType == "BMC")
488 {
489 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
490 }
491 else if (dumpType == "FaultLog")
492 {
493 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
494 }
495 else if (dumpType == "System")
496 {
497 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
498 }
499 else
500 {
Ed Tanous62598e32023-07-17 17:06:25 -0700501 BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
502 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -0800503 }
504
505 // Returns empty string on error
506 return entriesPath;
507}
508
zhanghch058d1b46d2021-04-01 11:18:24 +0800509inline void
510 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
511 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512{
Claire Weinanfdd26902022-03-01 14:18:25 -0800513 std::string entriesPath = getDumpEntriesPath(dumpType);
514 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500515 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500516 messages::internalError(asyncResp->res);
517 return;
518 }
519
George Liu5eb468d2023-06-20 17:03:24 +0800520 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
521 dbus::utility::getManagedObjects(
522 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800523 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800524 dumpType](const boost::system::error_code& ec,
George Liu5eb468d2023-06-20 17:03:24 +0800525 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700526 if (ec)
527 {
Ed Tanous62598e32023-07-17 17:06:25 -0700528 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700529 messages::internalError(asyncResp->res);
530 return;
531 }
532
Claire Weinanfdd26902022-03-01 14:18:25 -0800533 // Remove ending slash
534 std::string odataIdStr = entriesPath;
535 if (!odataIdStr.empty())
536 {
537 odataIdStr.pop_back();
538 }
539
540 asyncResp->res.jsonValue["@odata.type"] =
541 "#LogEntryCollection.LogEntryCollection";
542 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
543 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500544 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
545 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800546
Ed Tanous3544d2a2023-08-06 18:12:20 -0700547 nlohmann::json::array_t entriesArray;
Ed Tanous18f8f602023-07-18 10:07:23 -0700548 std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
Ed Tanous002d39b2022-05-31 08:59:27 -0700549
George Liu5eb468d2023-06-20 17:03:24 +0800550 dbus::utility::ManagedObjectType resp(objects);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700551 std::ranges::sort(resp, [](const auto& l, const auto& r) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700552 return AlphanumLess<std::string>()(l.first.filename(),
553 r.first.filename());
554 });
555
556 for (auto& object : resp)
557 {
558 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500559 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700560 continue;
561 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700562 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700563 uint64_t size = 0;
564 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600565 std::string originatorId;
566 log_entry::OriginatorTypes originatorType =
567 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700568 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700569
570 std::string entryID = object.first.filename();
571 if (entryID.empty())
572 {
573 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500574 }
575
Claire Weinanc6fecda2022-07-15 10:43:25 -0700576 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600577 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700578 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700579
580 if (dumpStatus !=
581 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
582 !dumpStatus.empty())
583 {
584 // Dump status is not Complete, no need to enumerate
585 continue;
586 }
587
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600588 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800589 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 thisEntry["Id"] = entryID;
591 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700593 thisEntry["Created"] =
594 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700595
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600596 if (!originatorId.empty())
597 {
598 thisEntry["Originator"] = originatorId;
599 thisEntry["OriginatorType"] = originatorType;
600 }
601
Ed Tanous002d39b2022-05-31 08:59:27 -0700602 if (dumpType == "BMC")
603 {
604 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500605 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
606 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800607 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700608 }
609 else if (dumpType == "System")
610 {
611 thisEntry["DiagnosticDataType"] = "OEM";
612 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500613 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
614 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800615 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700616 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500617 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700618 }
619 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -0700620 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
Patrick Williams5a39f772023-10-20 11:20:21 -0500621 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500622}
623
zhanghch058d1b46d2021-04-01 11:18:24 +0800624inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700625 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800626 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500627{
Claire Weinanfdd26902022-03-01 14:18:25 -0800628 std::string entriesPath = getDumpEntriesPath(dumpType);
629 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500630 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500631 messages::internalError(asyncResp->res);
632 return;
633 }
634
George Liu5eb468d2023-06-20 17:03:24 +0800635 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
636 dbus::utility::getManagedObjects(
637 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800638 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800639 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700640 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700641 if (ec)
642 {
Ed Tanous62598e32023-07-17 17:06:25 -0700643 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 messages::internalError(asyncResp->res);
645 return;
646 }
647
648 bool foundDumpEntry = false;
Ed Tanous18f8f602023-07-18 10:07:23 -0700649 std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
Ed Tanous002d39b2022-05-31 08:59:27 -0700650
651 for (const auto& objectPath : resp)
652 {
653 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500654 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700655 continue;
656 }
657
658 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700659 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700660 uint64_t size = 0;
661 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600662 std::string originatorId;
663 log_entry::OriginatorTypes originatorType =
664 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700665
Claire Weinanaefe3782022-07-15 19:17:19 -0700666 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600667 timestampUs, originatorId,
668 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700669
670 if (dumpStatus !=
671 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
672 !dumpStatus.empty())
673 {
674 // Dump status is not Complete
675 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200676 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
677 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500678 return;
679 }
680
Ed Tanous002d39b2022-05-31 08:59:27 -0700681 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600682 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800683 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700684 asyncResp->res.jsonValue["Id"] = entryID;
685 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700686 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700687 asyncResp->res.jsonValue["Created"] =
688 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500689
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600690 if (!originatorId.empty())
691 {
692 asyncResp->res.jsonValue["Originator"] = originatorId;
693 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
694 }
695
Ed Tanous002d39b2022-05-31 08:59:27 -0700696 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500697 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700698 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
699 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800700 entriesPath + entryID + "/attachment";
701 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500702 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700703 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500704 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700705 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
706 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
707 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800708 entriesPath + entryID + "/attachment";
709 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500710 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700711 }
712 if (!foundDumpEntry)
713 {
Ed Tanous62598e32023-07-17 17:06:25 -0700714 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
Myung Baeb90d14f2023-05-31 14:40:39 -0500715 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
716 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700717 return;
718 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500719 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500720}
721
zhanghch058d1b46d2021-04-01 11:18:24 +0800722inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800723 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500724 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500725{
Patrick Williams5a39f772023-10-20 11:20:21 -0500726 auto respHandler = [asyncResp,
727 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -0700728 BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500729 if (ec)
730 {
George Liu3de8d8b2021-03-22 17:49:39 +0800731 if (ec.value() == EBADR)
732 {
733 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
734 return;
735 }
Ed Tanous62598e32023-07-17 17:06:25 -0700736 BMCWEB_LOG_ERROR(
737 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
738 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500739 messages::internalError(asyncResp->res);
740 return;
741 }
742 };
Ed Tanous18f8f602023-07-18 10:07:23 -0700743
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500744 crow::connections::systemBus->async_method_call(
745 respHandler, "xyz.openbmc_project.Dump.Manager",
Ed Tanous18f8f602023-07-18 10:07:23 -0700746 std::format("{}/entry/{}", getDumpPath(dumpType), entryID),
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500747 "xyz.openbmc_project.Object.Delete", "Delete");
748}
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600749inline bool checkSizeLimit(int fd, crow::Response& res)
750{
751 long long int size = lseek(fd, 0, SEEK_END);
752 if (size <= 0)
753 {
754 BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
755 size);
756 messages::internalError(res);
757 return false;
758 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500759
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600760 // Arbitrary max size of 20MB to accommodate BMC dumps
761 constexpr long long int maxFileSize = 20LL * 1024LL * 1024LL;
762 if (size > maxFileSize)
763 {
764 BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
765 size, maxFileSize);
766 messages::internalError(res);
767 return false;
768 }
769 off_t rc = lseek(fd, 0, SEEK_SET);
770 if (rc < 0)
771 {
772 BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
773 messages::internalError(res);
774 return false;
775 }
776 return true;
777}
Carson Labrado168d1b12023-03-27 17:04:46 +0000778inline void
779 downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
780 const std::string& entryID,
781 const std::string& downloadEntryType,
782 const boost::system::error_code& ec,
783 const sdbusplus::message::unix_fd& unixfd)
784{
785 if (ec.value() == EBADR)
786 {
787 messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
788 return;
789 }
790 if (ec)
791 {
792 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
793 messages::internalError(asyncResp->res);
794 return;
795 }
796
797 // Make sure we know how to process the retrieved entry attachment
798 if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
799 {
800 BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
801 downloadEntryType);
802 messages::internalError(asyncResp->res);
803 }
804
805 int fd = -1;
806 fd = dup(unixfd);
807 if (fd < 0)
808 {
809 BMCWEB_LOG_ERROR("Failed to open file");
810 messages::internalError(asyncResp->res);
811 return;
812 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600813 if (!checkSizeLimit(fd, asyncResp->res))
Carson Labrado168d1b12023-03-27 17:04:46 +0000814 {
Carson Labrado168d1b12023-03-27 17:04:46 +0000815 close(fd);
816 return;
817 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000818 if (downloadEntryType == "System")
819 {
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600820 if (!asyncResp->res.openFd(fd, bmcweb::EncodingType::Base64))
821 {
822 messages::internalError(asyncResp->res);
823 close(fd);
824 return;
825 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000826 asyncResp->res.addHeader(
827 boost::beast::http::field::content_transfer_encoding, "Base64");
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600828 return;
Carson Labrado168d1b12023-03-27 17:04:46 +0000829 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600830 if (!asyncResp->res.openFd(fd))
Ed Tanous27b0cf92023-08-07 12:02:40 -0700831 {
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600832 messages::internalError(asyncResp->res);
833 close(fd);
834 return;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700835 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000836 asyncResp->res.addHeader(boost::beast::http::field::content_type,
837 "application/octet-stream");
838}
839
840inline void
841 downloadDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
842 const std::string& entryID, const std::string& dumpType)
843{
844 if (dumpType != "BMC")
845 {
846 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
847 messages::resourceNotFound(asyncResp->res, dumpType + " dump", entryID);
848 return;
849 }
850
Ed Tanous18f8f602023-07-18 10:07:23 -0700851 std::string dumpEntryPath = std::format("{}/entry/{}",
852 getDumpPath(dumpType), entryID);
Carson Labrado168d1b12023-03-27 17:04:46 +0000853
854 auto downloadDumpEntryHandler =
855 [asyncResp, entryID,
856 dumpType](const boost::system::error_code& ec,
857 const sdbusplus::message::unix_fd& unixfd) {
858 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
859 };
860
861 crow::connections::systemBus->async_method_call(
862 std::move(downloadDumpEntryHandler), "xyz.openbmc_project.Dump.Manager",
863 dumpEntryPath, "xyz.openbmc_project.Dump.Entry", "GetFileHandle");
864}
865
866inline void
867 downloadEventLogEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
868 const std::string& systemName,
869 const std::string& entryID,
870 const std::string& dumpType)
871{
872 if constexpr (bmcwebEnableMultiHost)
873 {
874 // Option currently returns no systems. TBD
875 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
876 systemName);
877 return;
878 }
879 if (systemName != "system")
880 {
881 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
882 systemName);
883 return;
884 }
885
886 std::string entryPath =
887 sdbusplus::message::object_path("/xyz/openbmc_project/logging/entry") /
888 entryID;
889
890 auto downloadEventLogEntryHandler =
891 [asyncResp, entryID,
892 dumpType](const boost::system::error_code& ec,
893 const sdbusplus::message::unix_fd& unixfd) {
894 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
895 };
896
897 crow::connections::systemBus->async_method_call(
898 std::move(downloadEventLogEntryHandler), "xyz.openbmc_project.Logging",
899 entryPath, "xyz.openbmc_project.Logging.Entry", "GetEntry");
900}
901
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600902inline DumpCreationProgress
903 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500904{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600905 if (status ==
906 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
907 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
908 {
909 return DumpCreationProgress::DUMP_CREATE_FAILED;
910 }
911 if (status ==
912 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
913 {
914 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
915 }
916 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
917}
918
919inline DumpCreationProgress
920 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
921{
922 for (const auto& [key, val] : values)
923 {
924 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700925 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600926 const std::string* value = std::get_if<std::string>(&val);
927 if (value == nullptr)
928 {
Ed Tanous62598e32023-07-17 17:06:25 -0700929 BMCWEB_LOG_ERROR("Status property value is null");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600930 return DumpCreationProgress::DUMP_CREATE_FAILED;
931 }
932 return mapDbusStatusToDumpProgress(*value);
933 }
934 }
935 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
936}
937
938inline std::string getDumpEntryPath(const std::string& dumpPath)
939{
940 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
941 {
942 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
943 }
944 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
945 {
946 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
947 }
948 return "";
949}
950
951inline void createDumpTaskCallback(
952 task::Payload&& payload,
953 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
954 const sdbusplus::message::object_path& createdObjPath)
955{
956 const std::string dumpPath = createdObjPath.parent_path().str;
957 const std::string dumpId = createdObjPath.filename();
958
959 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
960
961 if (dumpEntryPath.empty())
962 {
Ed Tanous62598e32023-07-17 17:06:25 -0700963 BMCWEB_LOG_ERROR("Invalid dump type received");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600964 messages::internalError(asyncResp->res);
965 return;
966 }
967
968 crow::connections::systemBus->async_method_call(
969 [asyncResp, payload, createdObjPath,
970 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800971 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600972 const std::string& introspectXml) {
973 if (ec)
974 {
Ed Tanous62598e32023-07-17 17:06:25 -0700975 BMCWEB_LOG_ERROR("Introspect call failed with error: {}",
976 ec.message());
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600977 messages::internalError(asyncResp->res);
978 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700979 }
980
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600981 // Check if the created dump object has implemented Progress
982 // interface to track dump completion. If yes, fetch the "Status"
983 // property of the interface, modify the task state accordingly.
984 // Else, return task completed.
985 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700986
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600987 doc.Parse(introspectXml.data(), introspectXml.size());
988 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
989 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700990 {
Ed Tanous62598e32023-07-17 17:06:25 -0700991 BMCWEB_LOG_ERROR("XML document failed to parse");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600992 messages::internalError(asyncResp->res);
993 return;
994 }
995 tinyxml2::XMLElement* interfaceNode =
996 pRoot->FirstChildElement("interface");
997
998 bool isProgressIntfPresent = false;
999 while (interfaceNode != nullptr)
1000 {
1001 const char* thisInterfaceName = interfaceNode->Attribute("name");
1002 if (thisInterfaceName != nullptr)
1003 {
1004 if (thisInterfaceName ==
1005 std::string_view("xyz.openbmc_project.Common.Progress"))
1006 {
1007 interfaceNode =
1008 interfaceNode->NextSiblingElement("interface");
1009 continue;
1010 }
1011 isProgressIntfPresent = true;
1012 break;
1013 }
1014 interfaceNode = interfaceNode->NextSiblingElement("interface");
1015 }
1016
1017 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
1018 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous8b242752023-06-27 17:17:13 -07001019 const boost::system::error_code& ec2, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001020 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07001021 if (ec2)
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001022 {
Ed Tanous62598e32023-07-17 17:06:25 -07001023 BMCWEB_LOG_ERROR("{}: Error in creating dump",
1024 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001025 taskData->messages.emplace_back(messages::internalError());
1026 taskData->state = "Cancelled";
1027 return task::completed;
1028 }
1029
1030 if (isProgressIntfPresent)
1031 {
1032 dbus::utility::DBusPropertiesMap values;
1033 std::string prop;
1034 msg.read(prop, values);
1035
1036 DumpCreationProgress dumpStatus =
1037 getDumpCompletionStatus(values);
1038 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
1039 {
Ed Tanous62598e32023-07-17 17:06:25 -07001040 BMCWEB_LOG_ERROR("{}: Error in creating dump",
1041 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001042 taskData->state = "Cancelled";
1043 return task::completed;
1044 }
1045
1046 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
1047 {
Ed Tanous62598e32023-07-17 17:06:25 -07001048 BMCWEB_LOG_DEBUG("{}: Dump creation task is in progress",
1049 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001050 return !task::completed;
1051 }
1052 }
1053
Ed Tanous002d39b2022-05-31 08:59:27 -07001054 nlohmann::json retMessage = messages::success();
1055 taskData->messages.emplace_back(retMessage);
1056
Ed Tanousc51a58e2023-03-27 14:43:19 -07001057 boost::urls::url url = boost::urls::format(
1058 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
1059
1060 std::string headerLoc = "Location: ";
1061 headerLoc += url.buffer();
1062
Ed Tanous002d39b2022-05-31 08:59:27 -07001063 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
1064
Ed Tanous62598e32023-07-17 17:06:25 -07001065 BMCWEB_LOG_DEBUG("{}: Dump creation task completed",
1066 createdObjPath.str);
Ed Tanous002d39b2022-05-31 08:59:27 -07001067 taskData->state = "Completed";
1068 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05001069 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001070 "type='signal',interface='org.freedesktop.DBus.Properties',"
1071 "member='PropertiesChanged',path='" +
1072 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001073
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001074 // The task timer is set to max time limit within which the
1075 // requested dump will be collected.
1076 task->startTimer(std::chrono::minutes(6));
1077 task->populateResp(asyncResp->res);
1078 task->payload.emplace(payload);
Patrick Williams5a39f772023-10-20 11:20:21 -05001079 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001080 "xyz.openbmc_project.Dump.Manager", createdObjPath,
1081 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001082}
1083
zhanghch058d1b46d2021-04-01 11:18:24 +08001084inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1085 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001086{
Claire Weinanfdd26902022-03-01 14:18:25 -08001087 std::string dumpPath = getDumpEntriesPath(dumpType);
1088 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001089 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001090 messages::internalError(asyncResp->res);
1091 return;
1092 }
1093
1094 std::optional<std::string> diagnosticDataType;
1095 std::optional<std::string> oemDiagnosticDataType;
1096
Willy Tu15ed6782021-12-14 11:03:16 -08001097 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001098 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
1099 "OEMDiagnosticDataType", oemDiagnosticDataType))
1100 {
1101 return;
1102 }
1103
1104 if (dumpType == "System")
1105 {
1106 if (!oemDiagnosticDataType || !diagnosticDataType)
1107 {
Ed Tanous62598e32023-07-17 17:06:25 -07001108 BMCWEB_LOG_ERROR(
1109 "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001110 messages::actionParameterMissing(
1111 asyncResp->res, "CollectDiagnosticData",
1112 "DiagnosticDataType & OEMDiagnosticDataType");
1113 return;
1114 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001115 if ((*oemDiagnosticDataType != "System") ||
1116 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001117 {
Ed Tanous62598e32023-07-17 17:06:25 -07001118 BMCWEB_LOG_ERROR("Wrong parameter values passed");
Ed Tanousace85d62021-10-26 12:45:59 -07001119 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001120 return;
1121 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001122 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001123 }
1124 else if (dumpType == "BMC")
1125 {
1126 if (!diagnosticDataType)
1127 {
Ed Tanous62598e32023-07-17 17:06:25 -07001128 BMCWEB_LOG_ERROR(
1129 "CreateDump action parameter 'DiagnosticDataType' not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001130 messages::actionParameterMissing(
1131 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
1132 return;
1133 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001134 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001135 {
Ed Tanous62598e32023-07-17 17:06:25 -07001136 BMCWEB_LOG_ERROR(
1137 "Wrong parameter value passed for 'DiagnosticDataType'");
Ed Tanousace85d62021-10-26 12:45:59 -07001138 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001139 return;
1140 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001141 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
1142 }
1143 else
1144 {
Ed Tanous62598e32023-07-17 17:06:25 -07001145 BMCWEB_LOG_ERROR("CreateDump failed. Unknown dump type");
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001146 messages::internalError(asyncResp->res);
1147 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001148 }
1149
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001150 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
1151 createDumpParamVec;
1152
Carson Labradof574a8e2023-03-22 02:26:00 +00001153 if (req.session != nullptr)
1154 {
1155 createDumpParamVec.emplace_back(
1156 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
1157 req.session->clientIp);
1158 createDumpParamVec.emplace_back(
1159 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
1160 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
1161 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -06001162
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001163 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001164 [asyncResp, payload(task::Payload(req)),
1165 dumpPath](const boost::system::error_code& ec,
1166 const sdbusplus::message_t& msg,
1167 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07001168 if (ec)
1169 {
Ed Tanous62598e32023-07-17 17:06:25 -07001170 BMCWEB_LOG_ERROR("CreateDump resp_handler got error {}", ec);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001171 const sd_bus_error* dbusError = msg.get_error();
1172 if (dbusError == nullptr)
1173 {
1174 messages::internalError(asyncResp->res);
1175 return;
1176 }
1177
Ed Tanous62598e32023-07-17 17:06:25 -07001178 BMCWEB_LOG_ERROR("CreateDump DBus error: {} and error msg: {}",
1179 dbusError->name, dbusError->message);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001180 if (std::string_view(
1181 "xyz.openbmc_project.Common.Error.NotAllowed") ==
1182 dbusError->name)
1183 {
1184 messages::resourceInStandby(asyncResp->res);
1185 return;
1186 }
1187 if (std::string_view(
1188 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
1189 dbusError->name)
1190 {
1191 messages::serviceDisabled(asyncResp->res, dumpPath);
1192 return;
1193 }
1194 if (std::string_view(
1195 "xyz.openbmc_project.Common.Error.Unavailable") ==
1196 dbusError->name)
1197 {
1198 messages::resourceInUse(asyncResp->res);
1199 return;
1200 }
1201 // Other Dbus errors such as:
1202 // xyz.openbmc_project.Common.Error.InvalidArgument &
1203 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1204 // the dbus call that is made here in the bmcweb
1205 // implementation and has nothing to do with the client's
1206 // input in the request. Hence, returning internal error
1207 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001208 messages::internalError(asyncResp->res);
1209 return;
1210 }
Ed Tanous62598e32023-07-17 17:06:25 -07001211 BMCWEB_LOG_DEBUG("Dump Created. Path: {}", objPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001212 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Patrick Williams5a39f772023-10-20 11:20:21 -05001213 },
Ed Tanous18f8f602023-07-18 10:07:23 -07001214 "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001215 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001216}
1217
zhanghch058d1b46d2021-04-01 11:18:24 +08001218inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1219 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001220{
Claire Weinan0d946212022-07-13 19:40:19 -07001221 crow::connections::systemBus->async_method_call(
1222 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001223 if (ec)
1224 {
Ed Tanous62598e32023-07-17 17:06:25 -07001225 BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 messages::internalError(asyncResp->res);
1227 return;
1228 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001229 },
Ed Tanous18f8f602023-07-18 10:07:23 -07001230 "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
Claire Weinan0d946212022-07-13 19:40:19 -07001231 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001232}
1233
Ed Tanousb9d36b42022-02-26 21:42:46 -08001234inline static void
1235 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1236 std::string& filename, std::string& timestamp,
1237 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001238{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001239 const std::string* filenamePtr = nullptr;
1240 const std::string* timestampPtr = nullptr;
1241 const std::string* logfilePtr = nullptr;
1242
1243 const bool success = sdbusplus::unpackPropertiesNoThrow(
1244 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1245 "Filename", filenamePtr, "Log", logfilePtr);
1246
1247 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001248 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001249 return;
1250 }
1251
1252 if (filenamePtr != nullptr)
1253 {
1254 filename = *filenamePtr;
1255 }
1256
1257 if (timestampPtr != nullptr)
1258 {
1259 timestamp = *timestampPtr;
1260 }
1261
1262 if (logfilePtr != nullptr)
1263 {
1264 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001265 }
1266}
1267
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001268inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001269{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001270 /**
1271 * Functions triggers appropriate requests on DBus
1272 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001273 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001274 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001275 .methods(boost::beast::http::verb::get)(
1276 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001277 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1278 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001279 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001280 {
1281 return;
1282 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001283 if constexpr (bmcwebEnableMultiHost)
1284 {
1285 // Option currently returns no systems. TBD
1286 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1287 systemName);
1288 return;
1289 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001290 if (systemName != "system")
1291 {
1292 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1293 systemName);
1294 return;
1295 }
1296
Ed Tanous002d39b2022-05-31 08:59:27 -07001297 // Collections don't include the static data added by SubRoute
1298 // because it has a duplicate entry for members
1299 asyncResp->res.jsonValue["@odata.type"] =
1300 "#LogServiceCollection.LogServiceCollection";
1301 asyncResp->res.jsonValue["@odata.id"] =
1302 "/redfish/v1/Systems/system/LogServices";
1303 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1304 asyncResp->res.jsonValue["Description"] =
1305 "Collection of LogServices for this Computer System";
1306 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1307 logServiceArray = nlohmann::json::array();
1308 nlohmann::json::object_t eventLog;
1309 eventLog["@odata.id"] =
1310 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001311 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001312#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001313 nlohmann::json::object_t dumpLog;
1314 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001315 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001316#endif
1317
Jason M. Billsd53dd412019-02-12 17:16:22 -08001318#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001319 nlohmann::json::object_t crashdump;
1320 crashdump["@odata.id"] =
1321 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001322 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001323#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001324
1325#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001326 nlohmann::json::object_t hostlogger;
1327 hostlogger["@odata.id"] =
1328 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001329 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001330#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001331 asyncResp->res.jsonValue["Members@odata.count"] =
1332 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001333
George Liu7a1dbc42022-12-07 16:03:22 +08001334 constexpr std::array<std::string_view, 1> interfaces = {
1335 "xyz.openbmc_project.State.Boot.PostCode"};
1336 dbus::utility::getSubTreePaths(
1337 "/", 0, interfaces,
1338 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001339 const dbus::utility::MapperGetSubTreePathsResponse&
1340 subtreePath) {
1341 if (ec)
1342 {
Ed Tanous62598e32023-07-17 17:06:25 -07001343 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001344 return;
1345 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001346
Ed Tanous002d39b2022-05-31 08:59:27 -07001347 for (const auto& pathStr : subtreePath)
1348 {
1349 if (pathStr.find("PostCode") != std::string::npos)
1350 {
1351 nlohmann::json& logServiceArrayLocal =
1352 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001353 nlohmann::json::object_t member;
1354 member["@odata.id"] =
1355 "/redfish/v1/Systems/system/LogServices/PostCodes";
1356
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001357 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001358
Ed Tanous002d39b2022-05-31 08:59:27 -07001359 asyncResp->res.jsonValue["Members@odata.count"] =
1360 logServiceArrayLocal.size();
1361 return;
1362 }
1363 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001364 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001365 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001366}
1367
1368inline void requestRoutesEventLogService(App& app)
1369{
Ed Tanous22d268c2022-05-19 09:39:07 -07001370 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001371 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001372 .methods(boost::beast::http::verb::get)(
1373 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001374 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1375 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001376 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001377 {
1378 return;
1379 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001380 if (systemName != "system")
1381 {
1382 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1383 systemName);
1384 return;
1385 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001386 asyncResp->res.jsonValue["@odata.id"] =
1387 "/redfish/v1/Systems/system/LogServices/EventLog";
1388 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05001389 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07001390 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1391 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1392 asyncResp->res.jsonValue["Id"] = "EventLog";
1393 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301394
Ed Tanous002d39b2022-05-31 08:59:27 -07001395 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001396 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301397
Ed Tanous002d39b2022-05-31 08:59:27 -07001398 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1399 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1400 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301401
Ed Tanous002d39b2022-05-31 08:59:27 -07001402 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1403 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1404 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001405
Ed Tanous002d39b2022-05-31 08:59:27 -07001406 {"target",
1407 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05001408 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001409}
1410
1411inline void requestRoutesJournalEventLogClear(App& app)
1412{
Jason M. Bills4978b632022-02-22 14:17:43 -08001413 BMCWEB_ROUTE(
1414 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001415 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001416 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001417 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001418 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001419 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1420 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001421 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001422 {
1423 return;
1424 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001425 if (systemName != "system")
1426 {
1427 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1428 systemName);
1429 return;
1430 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001431 // Clear the EventLog by deleting the log files
1432 std::vector<std::filesystem::path> redfishLogFiles;
1433 if (getRedfishLogFiles(redfishLogFiles))
1434 {
1435 for (const std::filesystem::path& file : redfishLogFiles)
1436 {
1437 std::error_code ec;
1438 std::filesystem::remove(file, ec);
1439 }
1440 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001441
Ed Tanous002d39b2022-05-31 08:59:27 -07001442 // Reload rsyslog so it knows to start new log files
1443 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001444 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001445 if (ec)
1446 {
Ed Tanous62598e32023-07-17 17:06:25 -07001447 BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001448 messages::internalError(asyncResp->res);
1449 return;
1450 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001451
Ed Tanous002d39b2022-05-31 08:59:27 -07001452 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001453 },
Ed Tanous002d39b2022-05-31 08:59:27 -07001454 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1455 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1456 "replace");
Patrick Williams5a39f772023-10-20 11:20:21 -05001457 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001458}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001459
Jason M. Billsac992cd2022-06-24 13:31:46 -07001460enum class LogParseError
1461{
1462 success,
1463 parseFailed,
1464 messageIdNotInRegistry,
1465};
1466
1467static LogParseError
1468 fillEventLogEntryJson(const std::string& logEntryID,
1469 const std::string& logEntry,
1470 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001471{
Jason M. Bills95820182019-04-22 16:25:34 -07001472 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001473 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001474 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001475 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001476 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001477 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001478 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001479 std::string timestamp = logEntry.substr(0, space);
1480 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001481 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001482 if (entryStart == std::string::npos)
1483 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001484 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001485 }
1486 std::string_view entry(logEntry);
1487 entry.remove_prefix(entryStart);
1488 // Use split to separate the entry into its fields
1489 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001490 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001491 // We need at least a MessageId to be valid
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001492 auto logEntryIter = logEntryFields.begin();
1493 if (logEntryIter == logEntryFields.end())
Jason M. Billscd225da2019-05-08 15:31:57 -07001494 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001495 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001496 }
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001497 std::string& messageID = *logEntryIter;
Jason M. Bills4851d452019-03-28 11:27:48 -07001498 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001499 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001500
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001501 logEntryIter++;
Sui Chen54417b02022-03-24 14:59:52 -07001502 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001503 {
Ed Tanous62598e32023-07-17 17:06:25 -07001504 BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001505 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001506 }
1507
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001508 std::vector<std::string_view> messageArgs(logEntryIter,
1509 logEntryFields.end());
Ed Tanousc05bba42023-06-28 08:33:29 -07001510 messageArgs.resize(message->numberOfArgs);
1511
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001512 std::string msg = redfish::registries::fillMessageArgs(messageArgs,
1513 message->message);
1514 if (msg.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001515 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001516 return LogParseError::parseFailed;
Jason M. Bills4851d452019-03-28 11:27:48 -07001517 }
1518
Jason M. Bills95820182019-04-22 16:25:34 -07001519 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1520 // format which matches the Redfish format except for the fractional seconds
1521 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001522 std::size_t dot = timestamp.find_first_of('.');
1523 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001524 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001525 {
Jason M. Bills95820182019-04-22 16:25:34 -07001526 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001527 }
1528
1529 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001530 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001531 logEntryJson["@odata.id"] = boost::urls::format(
1532 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1533 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001534 logEntryJson["Name"] = "System Event Log Entry";
1535 logEntryJson["Id"] = logEntryID;
1536 logEntryJson["Message"] = std::move(msg);
1537 logEntryJson["MessageId"] = std::move(messageID);
1538 logEntryJson["MessageArgs"] = messageArgs;
1539 logEntryJson["EntryType"] = "Event";
1540 logEntryJson["Severity"] = message->messageSeverity;
1541 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001542 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001543}
1544
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001545inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001546{
Ed Tanous22d268c2022-05-19 09:39:07 -07001547 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001548 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001549 .methods(boost::beast::http::verb::get)(
1550 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001551 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1552 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001553 query_param::QueryCapabilities capabilities = {
1554 .canDelegateTop = true,
1555 .canDelegateSkip = true,
1556 };
1557 query_param::Query delegatedQuery;
1558 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001559 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001560 {
1561 return;
1562 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001563 if constexpr (bmcwebEnableMultiHost)
1564 {
1565 // Option currently returns no systems. TBD
1566 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1567 systemName);
1568 return;
1569 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001570 if (systemName != "system")
1571 {
1572 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1573 systemName);
1574 return;
1575 }
1576
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001577 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001578 size_t skip = delegatedQuery.skip.value_or(0);
1579
Ed Tanous002d39b2022-05-31 08:59:27 -07001580 // Collections don't include the static data added by SubRoute
1581 // because it has a duplicate entry for members
1582 asyncResp->res.jsonValue["@odata.type"] =
1583 "#LogEntryCollection.LogEntryCollection";
1584 asyncResp->res.jsonValue["@odata.id"] =
1585 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1586 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1587 asyncResp->res.jsonValue["Description"] =
1588 "Collection of System Event Log Entries";
1589
1590 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1591 logEntryArray = nlohmann::json::array();
1592 // Go through the log files and create a unique ID for each
1593 // entry
1594 std::vector<std::filesystem::path> redfishLogFiles;
1595 getRedfishLogFiles(redfishLogFiles);
1596 uint64_t entryCount = 0;
1597 std::string logEntry;
1598
1599 // Oldest logs are in the last file, so start there and loop
1600 // backwards
1601 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1602 it++)
1603 {
1604 std::ifstream logStream(*it);
1605 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001606 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001607 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001608 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001609
Ed Tanous002d39b2022-05-31 08:59:27 -07001610 // Reset the unique ID on the first entry
1611 bool firstEntry = true;
1612 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001613 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001614 std::string idStr;
1615 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001616 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001617 continue;
1618 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001619 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001620
Jason M. Billsde703c52022-06-23 14:19:04 -07001621 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001622 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1623 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001624 if (status == LogParseError::messageIdNotInRegistry)
1625 {
1626 continue;
1627 }
1628 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001629 {
1630 messages::internalError(asyncResp->res);
1631 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001632 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001633
Jason M. Billsde703c52022-06-23 14:19:04 -07001634 entryCount++;
1635 // Handle paging using skip (number of entries to skip from the
1636 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001637 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001638 {
1639 continue;
1640 }
1641
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001642 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001643 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001644 }
1645 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001646 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001647 {
1648 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1649 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001650 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001651 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001652 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001653}
Chicago Duan336e96c2019-07-15 14:22:08 +08001654
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001655inline void requestRoutesJournalEventLogEntry(App& app)
1656{
1657 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001658 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001659 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001660 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001661 [&app](const crow::Request& req,
1662 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001663 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001664 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001665 {
1666 return;
1667 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001668 if constexpr (bmcwebEnableMultiHost)
1669 {
1670 // Option currently returns no systems. TBD
1671 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1672 systemName);
1673 return;
1674 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001675
1676 if (systemName != "system")
1677 {
1678 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1679 systemName);
1680 return;
1681 }
1682
Ed Tanous002d39b2022-05-31 08:59:27 -07001683 const std::string& targetID = param;
1684
1685 // Go through the log files and check the unique ID for each
1686 // entry to find the target entry
1687 std::vector<std::filesystem::path> redfishLogFiles;
1688 getRedfishLogFiles(redfishLogFiles);
1689 std::string logEntry;
1690
1691 // Oldest logs are in the last file, so start there and loop
1692 // backwards
1693 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1694 it++)
1695 {
1696 std::ifstream logStream(*it);
1697 if (!logStream.is_open())
1698 {
1699 continue;
1700 }
1701
1702 // Reset the unique ID on the first entry
1703 bool firstEntry = true;
1704 while (std::getline(logStream, logEntry))
1705 {
1706 std::string idStr;
1707 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001708 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001709 continue;
1710 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001711 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001712
1713 if (idStr == targetID)
1714 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001715 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001716 LogParseError status =
1717 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1718 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001719 {
1720 messages::internalError(asyncResp->res);
1721 return;
1722 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001723 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001724 return;
1725 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001726 }
1727 }
1728 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001729 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05001730 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001731}
1732
1733inline void requestRoutesDBusEventLogEntryCollection(App& app)
1734{
Ed Tanous22d268c2022-05-19 09:39:07 -07001735 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001736 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001737 .methods(boost::beast::http::verb::get)(
1738 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001739 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1740 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001741 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001742 {
1743 return;
1744 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001745 if constexpr (bmcwebEnableMultiHost)
1746 {
1747 // Option currently returns no systems. TBD
1748 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1749 systemName);
1750 return;
1751 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001752 if (systemName != "system")
1753 {
1754 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1755 systemName);
1756 return;
1757 }
1758
Ed Tanous002d39b2022-05-31 08:59:27 -07001759 // Collections don't include the static data added by SubRoute
1760 // because it has a duplicate entry for members
1761 asyncResp->res.jsonValue["@odata.type"] =
1762 "#LogEntryCollection.LogEntryCollection";
1763 asyncResp->res.jsonValue["@odata.id"] =
1764 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1765 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1766 asyncResp->res.jsonValue["Description"] =
1767 "Collection of System Event Log Entries";
1768
1769 // DBus implementation of EventLog/Entries
1770 // Make call to Logging Service to find all log entry objects
George Liu5eb468d2023-06-20 17:03:24 +08001771 sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1772 dbus::utility::getManagedObjects(
1773 "xyz.openbmc_project.Logging", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001774 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001775 const dbus::utility::ManagedObjectType& resp) {
1776 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001777 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001778 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07001779 BMCWEB_LOG_ERROR(
1780 "getLogEntriesIfaceData resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001781 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001782 return;
1783 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001784 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 for (const auto& objectPath : resp)
1786 {
1787 const uint32_t* id = nullptr;
1788 const uint64_t* timestamp = nullptr;
1789 const uint64_t* updateTimestamp = nullptr;
1790 const std::string* severity = nullptr;
1791 const std::string* message = nullptr;
1792 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001793 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001794 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001795 const std::string* notify = nullptr;
1796
Ed Tanous002d39b2022-05-31 08:59:27 -07001797 for (const auto& interfaceMap : objectPath.second)
1798 {
1799 if (interfaceMap.first ==
1800 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001801 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001803 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001804 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001805 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001806 id = std::get_if<uint32_t>(&propertyMap.second);
1807 }
1808 else if (propertyMap.first == "Timestamp")
1809 {
1810 timestamp =
1811 std::get_if<uint64_t>(&propertyMap.second);
1812 }
1813 else if (propertyMap.first == "UpdateTimestamp")
1814 {
1815 updateTimestamp =
1816 std::get_if<uint64_t>(&propertyMap.second);
1817 }
1818 else if (propertyMap.first == "Severity")
1819 {
1820 severity = std::get_if<std::string>(
1821 &propertyMap.second);
1822 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001823 else if (propertyMap.first == "Resolution")
1824 {
1825 resolution = std::get_if<std::string>(
1826 &propertyMap.second);
1827 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001828 else if (propertyMap.first == "Message")
1829 {
1830 message = std::get_if<std::string>(
1831 &propertyMap.second);
1832 }
1833 else if (propertyMap.first == "Resolved")
1834 {
1835 const bool* resolveptr =
1836 std::get_if<bool>(&propertyMap.second);
1837 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001838 {
1839 messages::internalError(asyncResp->res);
1840 return;
1841 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001842 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001843 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001844 else if (propertyMap.first ==
1845 "ServiceProviderNotify")
1846 {
1847 notify = std::get_if<std::string>(
1848 &propertyMap.second);
1849 if (notify == nullptr)
1850 {
1851 messages::internalError(asyncResp->res);
1852 return;
1853 }
1854 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001855 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001856 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001857 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001858 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 messages::internalError(asyncResp->res);
1860 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001861 }
1862 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001863 else if (interfaceMap.first ==
1864 "xyz.openbmc_project.Common.FilePath")
1865 {
1866 for (const auto& propertyMap : interfaceMap.second)
1867 {
1868 if (propertyMap.first == "Path")
1869 {
1870 filePath = std::get_if<std::string>(
1871 &propertyMap.second);
1872 }
1873 }
1874 }
1875 }
1876 // Object path without the
1877 // xyz.openbmc_project.Logging.Entry interface, ignore
1878 // and continue.
1879 if (id == nullptr || message == nullptr ||
1880 severity == nullptr || timestamp == nullptr ||
1881 updateTimestamp == nullptr)
1882 {
1883 continue;
1884 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001885 nlohmann::json& thisEntry = entriesArray.emplace_back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001886 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001887 thisEntry["@odata.id"] = boost::urls::format(
1888 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1889 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001890 thisEntry["Name"] = "System Event Log Entry";
1891 thisEntry["Id"] = std::to_string(*id);
1892 thisEntry["Message"] = *message;
1893 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001894 if ((resolution != nullptr) && (!(*resolution).empty()))
1895 {
1896 thisEntry["Resolution"] = *resolution;
1897 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001898 std::optional<bool> notifyAction =
1899 getProviderNotifyAction(*notify);
1900 if (notifyAction)
1901 {
1902 thisEntry["ServiceProviderNotified"] = *notifyAction;
1903 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001904 thisEntry["EntryType"] = "Event";
1905 thisEntry["Severity"] =
1906 translateSeverityDbusToRedfish(*severity);
1907 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001908 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001910 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 if (filePath != nullptr)
1912 {
1913 thisEntry["AdditionalDataURI"] =
1914 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1915 std::to_string(*id) + "/attachment";
1916 }
1917 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001918 std::ranges::sort(entriesArray, [](const nlohmann::json& left,
1919 const nlohmann::json& right) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001920 return (left["Id"] <= right["Id"]);
Ed Tanous3544d2a2023-08-06 18:12:20 -07001921 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 asyncResp->res.jsonValue["Members@odata.count"] =
1923 entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -07001924 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001925 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001926 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001927}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001928
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001929inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001930{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001931 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001932 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001933 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001934 .methods(boost::beast::http::verb::get)(
1935 [&app](const crow::Request& req,
1936 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001937 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001938 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001939 {
1940 return;
1941 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001942 if constexpr (bmcwebEnableMultiHost)
1943 {
1944 // Option currently returns no systems. TBD
1945 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1946 systemName);
1947 return;
1948 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001949 if (systemName != "system")
1950 {
1951 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1952 systemName);
1953 return;
1954 }
1955
Ed Tanous002d39b2022-05-31 08:59:27 -07001956 std::string entryID = param;
1957 dbus::utility::escapePathForDbus(entryID);
1958
1959 // DBus implementation of EventLog/Entries
1960 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001961 sdbusplus::asio::getAllProperties(
1962 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1963 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001964 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001965 const dbus::utility::DBusPropertiesMap& resp) {
1966 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001967 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001968 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1969 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001970 return;
1971 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 if (ec)
1973 {
Ed Tanous62598e32023-07-17 17:06:25 -07001974 BMCWEB_LOG_ERROR(
1975 "EventLogEntry (DBus) resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 messages::internalError(asyncResp->res);
1977 return;
1978 }
1979 const uint32_t* id = nullptr;
1980 const uint64_t* timestamp = nullptr;
1981 const uint64_t* updateTimestamp = nullptr;
1982 const std::string* severity = nullptr;
1983 const std::string* message = nullptr;
1984 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001985 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001986 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001987 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001988
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001989 const bool success = sdbusplus::unpackPropertiesNoThrow(
1990 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1991 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001992 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001993 "Resolution", resolution, "Path", filePath,
1994 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001995
1996 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001997 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001998 messages::internalError(asyncResp->res);
1999 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002000 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002001
Ed Tanous002d39b2022-05-31 08:59:27 -07002002 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05002003 timestamp == nullptr || updateTimestamp == nullptr ||
2004 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07002005 {
2006 messages::internalError(asyncResp->res);
2007 return;
2008 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05002009
Ed Tanous002d39b2022-05-31 08:59:27 -07002010 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05002011 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002012 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2013 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
2014 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07002015 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
2016 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
2017 asyncResp->res.jsonValue["Message"] = *message;
2018 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05002019 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
2020 if (notifyAction)
2021 {
2022 asyncResp->res.jsonValue["ServiceProviderNotified"] =
2023 *notifyAction;
2024 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05002025 if ((resolution != nullptr) && (!(*resolution).empty()))
2026 {
2027 asyncResp->res.jsonValue["Resolution"] = *resolution;
2028 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002029 asyncResp->res.jsonValue["EntryType"] = "Event";
2030 asyncResp->res.jsonValue["Severity"] =
2031 translateSeverityDbusToRedfish(*severity);
2032 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002033 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002034 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002035 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002036 if (filePath != nullptr)
2037 {
2038 asyncResp->res.jsonValue["AdditionalDataURI"] =
2039 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
2040 std::to_string(*id) + "/attachment";
2041 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002042 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002043 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002044
2045 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002046 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002047 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002048 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002049 [&app](const crow::Request& req,
2050 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002051 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002052 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002053 {
2054 return;
2055 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002056 if constexpr (bmcwebEnableMultiHost)
2057 {
2058 // Option currently returns no systems. TBD
2059 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2060 systemName);
2061 return;
2062 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002063 if (systemName != "system")
2064 {
2065 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2066 systemName);
2067 return;
2068 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002069 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002070
Ed Tanous002d39b2022-05-31 08:59:27 -07002071 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
2072 resolved))
2073 {
2074 return;
2075 }
Ed Tanous62598e32023-07-17 17:06:25 -07002076 BMCWEB_LOG_DEBUG("Set Resolved");
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002077
George Liu9ae226f2023-06-21 17:56:46 +08002078 sdbusplus::asio::setProperty(
2079 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
2080 "/xyz/openbmc_project/logging/entry/" + entryId,
2081 "xyz.openbmc_project.Logging.Entry", "Resolved", *resolved,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002082 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 if (ec)
2084 {
Ed Tanous62598e32023-07-17 17:06:25 -07002085 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002086 messages::internalError(asyncResp->res);
2087 return;
2088 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002089 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002090 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002091
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002092 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002093 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002094 .privileges(redfish::privileges::deleteLogEntry)
2095
Ed Tanous002d39b2022-05-31 08:59:27 -07002096 .methods(boost::beast::http::verb::delete_)(
2097 [&app](const crow::Request& req,
2098 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002099 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002100 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002101 {
2102 return;
2103 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002104 if constexpr (bmcwebEnableMultiHost)
2105 {
2106 // Option currently returns no systems. TBD
2107 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2108 systemName);
2109 return;
2110 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002111 if (systemName != "system")
2112 {
2113 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2114 systemName);
2115 return;
2116 }
Ed Tanous62598e32023-07-17 17:06:25 -07002117 BMCWEB_LOG_DEBUG("Do delete single event entries.");
Ed Tanous002d39b2022-05-31 08:59:27 -07002118
2119 std::string entryID = param;
2120
2121 dbus::utility::escapePathForDbus(entryID);
2122
2123 // Process response from Logging service.
Patrick Williams5a39f772023-10-20 11:20:21 -05002124 auto respHandler = [asyncResp,
2125 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07002126 BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07002127 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002128 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002130 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 messages::resourceNotFound(asyncResp->res, "LogEntry",
2132 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07002133 return;
2134 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002135 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07002136 BMCWEB_LOG_ERROR(
2137 "EventLogEntry (DBus) doDelete respHandler got error {}",
2138 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002139 asyncResp->res.result(
2140 boost::beast::http::status::internal_server_error);
2141 return;
2142 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002143
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 asyncResp->res.result(boost::beast::http::status::ok);
2145 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002146
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 // Make call to Logging service to request Delete Log
2148 crow::connections::systemBus->async_method_call(
2149 respHandler, "xyz.openbmc_project.Logging",
2150 "/xyz/openbmc_project/logging/entry/" + entryID,
2151 "xyz.openbmc_project.Object.Delete", "Delete");
Patrick Williams5a39f772023-10-20 11:20:21 -05002152 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002153}
2154
Spencer Kub7028eb2021-10-26 15:27:35 +08002155constexpr const char* hostLoggerFolderPath = "/var/log/console";
2156
2157inline bool
2158 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2159 std::vector<std::filesystem::path>& hostLoggerFiles)
2160{
2161 std::error_code ec;
2162 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2163 if (ec)
2164 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002165 BMCWEB_LOG_WARNING("{}", ec.message());
Spencer Kub7028eb2021-10-26 15:27:35 +08002166 return false;
2167 }
2168 for (const std::filesystem::directory_entry& it : logPath)
2169 {
2170 std::string filename = it.path().filename();
2171 // Prefix of each log files is "log". Find the file and save the
2172 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002173 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002174 {
2175 hostLoggerFiles.emplace_back(it.path());
2176 }
2177 }
2178 // As the log files rotate, they are appended with a ".#" that is higher for
2179 // the older logs. Since we start from oldest logs, sort the name in
2180 // descending order.
2181 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2182 AlphanumLess<std::string>());
2183
2184 return true;
2185}
2186
Ed Tanous02cad962022-06-30 16:50:15 -07002187inline bool getHostLoggerEntries(
2188 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2189 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002190{
2191 GzFileReader logFile;
2192
2193 // Go though all log files and expose host logs.
2194 for (const std::filesystem::path& it : hostLoggerFiles)
2195 {
2196 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2197 {
Ed Tanous62598e32023-07-17 17:06:25 -07002198 BMCWEB_LOG_ERROR("fail to expose host logs");
Spencer Kub7028eb2021-10-26 15:27:35 +08002199 return false;
2200 }
2201 }
2202 // Get lastMessage from constructor by getter
2203 std::string lastMessage = logFile.getLastMessage();
2204 if (!lastMessage.empty())
2205 {
2206 logCount++;
2207 if (logCount > skip && logCount <= (skip + top))
2208 {
2209 logEntries.push_back(lastMessage);
2210 }
2211 }
2212 return true;
2213}
2214
2215inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2216 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002217 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002218{
2219 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002220 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002221 logEntryJson["@odata.id"] = boost::urls::format(
2222 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2223 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002224 logEntryJson["Name"] = "Host Logger Entry";
2225 logEntryJson["Id"] = logEntryID;
2226 logEntryJson["Message"] = msg;
2227 logEntryJson["EntryType"] = "Oem";
2228 logEntryJson["Severity"] = "OK";
2229 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002230}
2231
2232inline void requestRoutesSystemHostLogger(App& app)
2233{
Ed Tanous22d268c2022-05-19 09:39:07 -07002234 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002235 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002236 .methods(boost::beast::http::verb::get)(
2237 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002238 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2239 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002240 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 {
2242 return;
2243 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002244 if constexpr (bmcwebEnableMultiHost)
2245 {
2246 // Option currently returns no systems. TBD
2247 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2248 systemName);
2249 return;
2250 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002251 if (systemName != "system")
2252 {
2253 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2254 systemName);
2255 return;
2256 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002257 asyncResp->res.jsonValue["@odata.id"] =
2258 "/redfish/v1/Systems/system/LogServices/HostLogger";
2259 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002260 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002261 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2262 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2263 asyncResp->res.jsonValue["Id"] = "HostLogger";
2264 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2265 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002266 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002267}
2268
2269inline void requestRoutesSystemHostLoggerCollection(App& app)
2270{
2271 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002272 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002273 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002274 .methods(boost::beast::http::verb::get)(
2275 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002276 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2277 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 query_param::QueryCapabilities capabilities = {
2279 .canDelegateTop = true,
2280 .canDelegateSkip = true,
2281 };
2282 query_param::Query delegatedQuery;
2283 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002284 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002285 {
2286 return;
2287 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002288 if constexpr (bmcwebEnableMultiHost)
2289 {
2290 // Option currently returns no systems. TBD
2291 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2292 systemName);
2293 return;
2294 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002295 if (systemName != "system")
2296 {
2297 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2298 systemName);
2299 return;
2300 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002301 asyncResp->res.jsonValue["@odata.id"] =
2302 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2303 asyncResp->res.jsonValue["@odata.type"] =
2304 "#LogEntryCollection.LogEntryCollection";
2305 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2306 asyncResp->res.jsonValue["Description"] =
2307 "Collection of HostLogger Entries";
2308 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2309 logEntryArray = nlohmann::json::array();
2310 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002311
Ed Tanous002d39b2022-05-31 08:59:27 -07002312 std::vector<std::filesystem::path> hostLoggerFiles;
2313 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2314 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002315 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002316 return;
2317 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002318 // If we weren't provided top and skip limits, use the defaults.
2319 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002320 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002321 size_t logCount = 0;
2322 // This vector only store the entries we want to expose that
2323 // control by skip and top.
2324 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002325 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2326 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002327 {
2328 messages::internalError(asyncResp->res);
2329 return;
2330 }
2331 // If vector is empty, that means skip value larger than total
2332 // log count
2333 if (logEntries.empty())
2334 {
2335 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2336 return;
2337 }
2338 if (!logEntries.empty())
2339 {
2340 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002341 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002342 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002343 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2344 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002345 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002346 }
2347
Ed Tanous002d39b2022-05-31 08:59:27 -07002348 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002349 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002350 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002351 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2352 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002353 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002354 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002355 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002356 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002357}
2358
2359inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2360{
2361 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002362 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002363 .privileges(redfish::privileges::getLogEntry)
2364 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002365 [&app](const crow::Request& req,
2366 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002367 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002368 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002369 {
2370 return;
2371 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002372 if constexpr (bmcwebEnableMultiHost)
2373 {
2374 // Option currently returns no systems. TBD
2375 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2376 systemName);
2377 return;
2378 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002379 if (systemName != "system")
2380 {
2381 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2382 systemName);
2383 return;
2384 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002385 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002386
Ed Tanous002d39b2022-05-31 08:59:27 -07002387 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002388
Patrick Williams84396af2023-05-11 11:47:45 -05002389 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2390 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002391 if (ec == std::errc::invalid_argument ||
2392 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002393 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002394 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002395 return;
2396 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002397
Ed Tanous002d39b2022-05-31 08:59:27 -07002398 std::vector<std::filesystem::path> hostLoggerFiles;
2399 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2400 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002401 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002402 return;
2403 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002404
Ed Tanous002d39b2022-05-31 08:59:27 -07002405 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002406 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002407 std::vector<std::string> logEntries;
2408 // We can get specific entry by skip and top. For example, if we
2409 // want to get nth entry, we can set skip = n-1 and top = 1 to
2410 // get that entry
2411 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2412 logCount))
2413 {
2414 messages::internalError(asyncResp->res);
2415 return;
2416 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002417
Ed Tanous002d39b2022-05-31 08:59:27 -07002418 if (!logEntries.empty())
2419 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002420 nlohmann::json::object_t hostLogEntry;
2421 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2422 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002423 return;
2424 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002425
Ed Tanous002d39b2022-05-31 08:59:27 -07002426 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002427 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Patrick Williams5a39f772023-10-20 11:20:21 -05002428 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002429}
2430
Claire Weinandd72e872022-08-15 14:20:06 -07002431inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002432 crow::App& app, const crow::Request& req,
2433 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2434{
2435 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2436 {
2437 return;
2438 }
2439 // Collections don't include the static data added by SubRoute
2440 // because it has a duplicate entry for members
2441 asyncResp->res.jsonValue["@odata.type"] =
2442 "#LogServiceCollection.LogServiceCollection";
2443 asyncResp->res.jsonValue["@odata.id"] =
2444 "/redfish/v1/Managers/bmc/LogServices";
2445 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2446 asyncResp->res.jsonValue["Description"] =
2447 "Collection of LogServices for this Manager";
2448 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2449 logServiceArray = nlohmann::json::array();
2450
2451#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002452 nlohmann::json::object_t journal;
2453 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002454 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002455#endif
2456
2457 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2458
2459#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002460 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002461 "xyz.openbmc_project.Collection.DeleteAll"};
2462 dbus::utility::getSubTreePaths(
2463 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002464 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002465 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002466 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2467 if (ec)
2468 {
Ed Tanous62598e32023-07-17 17:06:25 -07002469 BMCWEB_LOG_ERROR(
2470 "handleBMCLogServicesCollectionGet respHandler got error {}",
2471 ec);
Claire Weinanfdd26902022-03-01 14:18:25 -08002472 // Assume that getting an error simply means there are no dump
2473 // LogServices. Return without adding any error response.
2474 return;
2475 }
2476
2477 nlohmann::json& logServiceArrayLocal =
2478 asyncResp->res.jsonValue["Members"];
2479
2480 for (const std::string& path : subTreePaths)
2481 {
2482 if (path == "/xyz/openbmc_project/dump/bmc")
2483 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002484 nlohmann::json::object_t member;
2485 member["@odata.id"] =
2486 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002487 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002488 }
2489 else if (path == "/xyz/openbmc_project/dump/faultlog")
2490 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002491 nlohmann::json::object_t member;
2492 member["@odata.id"] =
2493 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002494 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002495 }
2496 }
2497
2498 asyncResp->res.jsonValue["Members@odata.count"] =
2499 logServiceArrayLocal.size();
Patrick Williams5a39f772023-10-20 11:20:21 -05002500 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002501#endif
2502}
2503
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002504inline void requestRoutesBMCLogServiceCollection(App& app)
2505{
2506 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002507 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002508 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002509 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002510}
Ed Tanous1da66f72018-07-27 16:13:37 -07002511
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002512inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002513{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002514 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002515 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002516 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002517 [&app](const crow::Request& req,
2518 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002519 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002520 {
2521 return;
2522 }
2523 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002524 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002525 asyncResp->res.jsonValue["@odata.id"] =
2526 "/redfish/v1/Managers/bmc/LogServices/Journal";
2527 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2528 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002529 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002530 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302531
Ed Tanous002d39b2022-05-31 08:59:27 -07002532 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002533 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002534 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2535 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2536 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302537
Ed Tanous002d39b2022-05-31 08:59:27 -07002538 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2539 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002540 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002541}
Jason M. Billse1f26342018-07-18 12:12:00 -07002542
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002543static int
2544 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2545 sd_journal* journal,
2546 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002547{
2548 // Get the Log Entry contents
2549 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002550
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002551 std::string message;
2552 std::string_view syslogID;
2553 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2554 if (ret < 0)
2555 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002556 BMCWEB_LOG_DEBUG("Failed to read SYSLOG_IDENTIFIER field: {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002557 strerror(-ret));
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002558 }
2559 if (!syslogID.empty())
2560 {
2561 message += std::string(syslogID) + ": ";
2562 }
2563
Ed Tanous39e77502019-03-04 17:35:53 -08002564 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002565 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002566 if (ret < 0)
2567 {
Ed Tanous62598e32023-07-17 17:06:25 -07002568 BMCWEB_LOG_ERROR("Failed to read MESSAGE field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002569 return 1;
2570 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002571 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002572
2573 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002574 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002575 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002576 if (ret < 0)
2577 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002578 BMCWEB_LOG_DEBUG("Failed to read PRIORITY field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002579 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002580
2581 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002582 std::string entryTimeStr;
2583 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002584 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002585 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002586 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002587
2588 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002589 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002590 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2591 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002592 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002593 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2594 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2595 bmcJournalLogEntryJson["Message"] = std::move(message);
2596 bmcJournalLogEntryJson["EntryType"] = "Oem";
2597 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2598 : severity <= 4 ? "Warning"
2599 : "OK";
2600 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2601 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002602 return 0;
2603}
2604
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002605inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002606{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002607 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002608 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002609 .methods(boost::beast::http::verb::get)(
2610 [&app](const crow::Request& req,
2611 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2612 query_param::QueryCapabilities capabilities = {
2613 .canDelegateTop = true,
2614 .canDelegateSkip = true,
2615 };
2616 query_param::Query delegatedQuery;
2617 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002618 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002619 {
2620 return;
2621 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002622
2623 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002624 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002625
Ed Tanous002d39b2022-05-31 08:59:27 -07002626 // Collections don't include the static data added by SubRoute
2627 // because it has a duplicate entry for members
2628 asyncResp->res.jsonValue["@odata.type"] =
2629 "#LogEntryCollection.LogEntryCollection";
2630 asyncResp->res.jsonValue["@odata.id"] =
2631 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2632 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2633 asyncResp->res.jsonValue["Description"] =
2634 "Collection of BMC Journal Entries";
2635 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2636 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002637
Ed Tanous002d39b2022-05-31 08:59:27 -07002638 // Go through the journal and use the timestamp to create a
2639 // unique ID for each entry
2640 sd_journal* journalTmp = nullptr;
2641 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2642 if (ret < 0)
2643 {
Ed Tanous62598e32023-07-17 17:06:25 -07002644 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002645 messages::internalError(asyncResp->res);
2646 return;
2647 }
2648 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2649 journalTmp, sd_journal_close);
2650 journalTmp = nullptr;
2651 uint64_t entryCount = 0;
2652 // Reset the unique ID on the first entry
2653 bool firstEntry = true;
2654 SD_JOURNAL_FOREACH(journal.get())
2655 {
2656 entryCount++;
2657 // Handle paging using skip (number of entries to skip from
2658 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002659 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002660 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002661 continue;
2662 }
2663
2664 std::string idStr;
2665 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2666 {
2667 continue;
2668 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002669 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002670
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002671 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002672 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2673 bmcJournalLogEntry) != 0)
2674 {
George Liu0fda0f12021-11-16 10:06:17 +08002675 messages::internalError(asyncResp->res);
2676 return;
2677 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002678 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002679 }
2680 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002681 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002682 {
2683 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2684 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002685 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002686 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002687 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002688}
Jason M. Billse1f26342018-07-18 12:12:00 -07002689
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002690inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002691{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002692 BMCWEB_ROUTE(app,
2693 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002694 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002695 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002696 [&app](const crow::Request& req,
2697 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2698 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002699 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002700 {
2701 return;
2702 }
2703 // Convert the unique ID back to a timestamp to find the entry
Myung Bae75e8e212023-11-30 12:53:46 -08002704 sd_id128_t bootID{};
Ed Tanous002d39b2022-05-31 08:59:27 -07002705 uint64_t ts = 0;
2706 uint64_t index = 0;
Myung Bae75e8e212023-11-30 12:53:46 -08002707 if (!getTimestampFromID(asyncResp, entryID, bootID, ts, index))
Ed Tanous002d39b2022-05-31 08:59:27 -07002708 {
2709 return;
2710 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002711
Ed Tanous002d39b2022-05-31 08:59:27 -07002712 sd_journal* journalTmp = nullptr;
2713 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2714 if (ret < 0)
2715 {
Ed Tanous62598e32023-07-17 17:06:25 -07002716 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002717 messages::internalError(asyncResp->res);
2718 return;
2719 }
2720 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2721 journalTmp, sd_journal_close);
2722 journalTmp = nullptr;
2723 // Go to the timestamp in the log and move to the entry at the
2724 // index tracking the unique ID
2725 std::string idStr;
2726 bool firstEntry = true;
Myung Bae75e8e212023-11-30 12:53:46 -08002727 ret = sd_journal_seek_monotonic_usec(journal.get(), bootID, ts);
Ed Tanous002d39b2022-05-31 08:59:27 -07002728 if (ret < 0)
2729 {
Ed Tanous62598e32023-07-17 17:06:25 -07002730 BMCWEB_LOG_ERROR("failed to seek to an entry in journal{}",
2731 strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002732 messages::internalError(asyncResp->res);
2733 return;
2734 }
2735 for (uint64_t i = 0; i <= index; i++)
2736 {
2737 sd_journal_next(journal.get());
2738 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2739 {
2740 messages::internalError(asyncResp->res);
2741 return;
2742 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002743 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002744 }
2745 // Confirm that the entry ID matches what was requested
2746 if (idStr != entryID)
2747 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002748 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002749 return;
2750 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002751
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002752 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002753 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002754 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002755 {
2756 messages::internalError(asyncResp->res);
2757 return;
2758 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002759 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Patrick Williams5a39f772023-10-20 11:20:21 -05002760 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002761}
2762
Claire Weinanfdd26902022-03-01 14:18:25 -08002763inline void
2764 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2765 const std::string& dumpType)
2766{
2767 std::string dumpPath;
2768 std::string overWritePolicy;
2769 bool collectDiagnosticDataSupported = false;
2770
2771 if (dumpType == "BMC")
2772 {
2773 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2774 overWritePolicy = "WrapsWhenFull";
2775 collectDiagnosticDataSupported = true;
2776 }
2777 else if (dumpType == "FaultLog")
2778 {
2779 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2780 overWritePolicy = "Unknown";
2781 collectDiagnosticDataSupported = false;
2782 }
2783 else if (dumpType == "System")
2784 {
2785 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2786 overWritePolicy = "WrapsWhenFull";
2787 collectDiagnosticDataSupported = true;
2788 }
2789 else
2790 {
Ed Tanous62598e32023-07-17 17:06:25 -07002791 BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
2792 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -08002793 messages::internalError(asyncResp->res);
2794 return;
2795 }
2796
2797 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2798 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2799 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2800 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2801 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2802 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2803
2804 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002805 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002806 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2807 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2808 redfishDateTimeOffset.second;
2809
2810 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002811
2812 if (collectDiagnosticDataSupported)
2813 {
2814 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2815 ["target"] =
2816 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2817 }
Claire Weinan0d946212022-07-13 19:40:19 -07002818
2819 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2820 dbus::utility::getSubTreePaths(
2821 "/xyz/openbmc_project/dump", 0, interfaces,
2822 [asyncResp, dumpType, dumpPath](
2823 const boost::system::error_code& ec,
2824 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2825 if (ec)
2826 {
Ed Tanous62598e32023-07-17 17:06:25 -07002827 BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
Claire Weinan0d946212022-07-13 19:40:19 -07002828 // Assume that getting an error simply means there are no dump
2829 // LogServices. Return without adding any error response.
2830 return;
2831 }
Ed Tanous18f8f602023-07-18 10:07:23 -07002832 std::string dbusDumpPath = getDumpPath(dumpType);
Claire Weinan0d946212022-07-13 19:40:19 -07002833 for (const std::string& path : subTreePaths)
2834 {
2835 if (path == dbusDumpPath)
2836 {
2837 asyncResp->res
2838 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2839 dumpPath + "/Actions/LogService.ClearLog";
2840 break;
2841 }
2842 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002843 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002844}
2845
2846inline void handleLogServicesDumpServiceGet(
2847 crow::App& app, const std::string& dumpType, const crow::Request& req,
2848 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2849{
2850 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2851 {
2852 return;
2853 }
2854 getDumpServiceInfo(asyncResp, dumpType);
2855}
2856
Ed Tanous22d268c2022-05-19 09:39:07 -07002857inline void handleLogServicesDumpServiceComputerSystemGet(
2858 crow::App& app, const crow::Request& req,
2859 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2860 const std::string& chassisId)
2861{
2862 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2863 {
2864 return;
2865 }
2866 if (chassisId != "system")
2867 {
2868 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2869 return;
2870 }
2871 getDumpServiceInfo(asyncResp, "System");
2872}
2873
Claire Weinanfdd26902022-03-01 14:18:25 -08002874inline void handleLogServicesDumpEntriesCollectionGet(
2875 crow::App& app, const std::string& dumpType, const crow::Request& req,
2876 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2877{
2878 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2879 {
2880 return;
2881 }
2882 getDumpEntryCollection(asyncResp, dumpType);
2883}
2884
Ed Tanous22d268c2022-05-19 09:39:07 -07002885inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2886 crow::App& app, const crow::Request& req,
2887 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2888 const std::string& chassisId)
2889{
2890 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2891 {
2892 return;
2893 }
2894 if (chassisId != "system")
2895 {
2896 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2897 return;
2898 }
2899 getDumpEntryCollection(asyncResp, "System");
2900}
2901
Claire Weinanfdd26902022-03-01 14:18:25 -08002902inline void handleLogServicesDumpEntryGet(
2903 crow::App& app, const std::string& dumpType, const crow::Request& req,
2904 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2905 const std::string& dumpId)
2906{
2907 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2908 {
2909 return;
2910 }
2911 getDumpEntryById(asyncResp, dumpId, dumpType);
2912}
Carson Labrado168d1b12023-03-27 17:04:46 +00002913
Ed Tanous22d268c2022-05-19 09:39:07 -07002914inline void handleLogServicesDumpEntryComputerSystemGet(
2915 crow::App& app, const crow::Request& req,
2916 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2917 const std::string& chassisId, const std::string& dumpId)
2918{
2919 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2920 {
2921 return;
2922 }
2923 if (chassisId != "system")
2924 {
2925 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2926 return;
2927 }
2928 getDumpEntryById(asyncResp, dumpId, "System");
2929}
Claire Weinanfdd26902022-03-01 14:18:25 -08002930
2931inline void handleLogServicesDumpEntryDelete(
2932 crow::App& app, const std::string& dumpType, const crow::Request& req,
2933 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2934 const std::string& dumpId)
2935{
2936 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2937 {
2938 return;
2939 }
2940 deleteDumpEntry(asyncResp, dumpId, dumpType);
2941}
2942
Ed Tanous22d268c2022-05-19 09:39:07 -07002943inline void handleLogServicesDumpEntryComputerSystemDelete(
2944 crow::App& app, const crow::Request& req,
2945 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2946 const std::string& chassisId, const std::string& dumpId)
2947{
2948 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2949 {
2950 return;
2951 }
2952 if (chassisId != "system")
2953 {
2954 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2955 return;
2956 }
2957 deleteDumpEntry(asyncResp, dumpId, "System");
2958}
2959
Carson Labrado168d1b12023-03-27 17:04:46 +00002960inline void handleLogServicesDumpEntryDownloadGet(
2961 crow::App& app, const std::string& dumpType, const crow::Request& req,
2962 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2963 const std::string& dumpId)
2964{
2965 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2966 {
2967 return;
2968 }
2969 downloadDumpEntry(asyncResp, dumpId, dumpType);
2970}
2971
2972inline void handleDBusEventLogEntryDownloadGet(
2973 crow::App& app, const std::string& dumpType, const crow::Request& req,
2974 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2975 const std::string& systemName, const std::string& entryID)
2976{
2977 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2978 {
2979 return;
2980 }
2981 if (!http_helpers::isContentTypeAllowed(
2982 req.getHeaderValue("Accept"),
2983 http_helpers::ContentType::OctetStream, true))
2984 {
2985 asyncResp->res.result(boost::beast::http::status::bad_request);
2986 return;
2987 }
2988 downloadEventLogEntry(asyncResp, systemName, entryID, dumpType);
2989}
2990
Claire Weinanfdd26902022-03-01 14:18:25 -08002991inline void handleLogServicesDumpCollectDiagnosticDataPost(
2992 crow::App& app, const std::string& dumpType, const crow::Request& req,
2993 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2994{
2995 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2996 {
2997 return;
2998 }
2999 createDump(asyncResp, req, dumpType);
3000}
3001
Ed Tanous22d268c2022-05-19 09:39:07 -07003002inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
3003 crow::App& app, const crow::Request& req,
3004 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003005 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003006{
3007 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3008 {
3009 return;
3010 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003011
3012 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003013 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003014 // Option currently returns no systems. TBD
3015 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3016 systemName);
3017 return;
3018 }
3019 if (systemName != "system")
3020 {
3021 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3022 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003023 return;
3024 }
3025 createDump(asyncResp, req, "System");
3026}
3027
Claire Weinanfdd26902022-03-01 14:18:25 -08003028inline void handleLogServicesDumpClearLogPost(
3029 crow::App& app, const std::string& dumpType, const crow::Request& req,
3030 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3031{
3032 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3033 {
3034 return;
3035 }
3036 clearDump(asyncResp, dumpType);
3037}
3038
Ed Tanous22d268c2022-05-19 09:39:07 -07003039inline void handleLogServicesDumpClearLogComputerSystemPost(
3040 crow::App& app, const crow::Request& req,
3041 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003042 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003043{
3044 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3045 {
3046 return;
3047 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003048 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003049 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003050 // Option currently returns no systems. TBD
3051 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3052 systemName);
3053 return;
3054 }
3055 if (systemName != "system")
3056 {
3057 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3058 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003059 return;
3060 }
3061 clearDump(asyncResp, "System");
3062}
3063
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003064inline void requestRoutesBMCDumpService(App& app)
3065{
3066 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003067 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003068 .methods(boost::beast::http::verb::get)(std::bind_front(
3069 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003070}
3071
3072inline void requestRoutesBMCDumpEntryCollection(App& app)
3073{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003074 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003075 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08003076 .methods(boost::beast::http::verb::get)(std::bind_front(
3077 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003078}
3079
3080inline void requestRoutesBMCDumpEntry(App& app)
3081{
3082 BMCWEB_ROUTE(app,
3083 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003084 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003085 .methods(boost::beast::http::verb::get)(std::bind_front(
3086 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
3087
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003088 BMCWEB_ROUTE(app,
3089 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003090 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003091 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3092 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003093}
3094
Carson Labrado168d1b12023-03-27 17:04:46 +00003095inline void requestRoutesBMCDumpEntryDownload(App& app)
3096{
3097 BMCWEB_ROUTE(
3098 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003099 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003100 .privileges(redfish::privileges::getLogEntry)
3101 .methods(boost::beast::http::verb::get)(std::bind_front(
3102 handleLogServicesDumpEntryDownloadGet, std::ref(app), "BMC"));
3103}
3104
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003105inline void requestRoutesBMCDumpCreate(App& app)
3106{
George Liu0fda0f12021-11-16 10:06:17 +08003107 BMCWEB_ROUTE(
3108 app,
3109 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003110 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003111 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08003112 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
3113 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003114}
3115
3116inline void requestRoutesBMCDumpClear(App& app)
3117{
George Liu0fda0f12021-11-16 10:06:17 +08003118 BMCWEB_ROUTE(
3119 app,
3120 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003121 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003122 .methods(boost::beast::http::verb::post)(std::bind_front(
3123 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
3124}
3125
Carson Labrado168d1b12023-03-27 17:04:46 +00003126inline void requestRoutesDBusEventLogEntryDownload(App& app)
3127{
3128 BMCWEB_ROUTE(
3129 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003130 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003131 .privileges(redfish::privileges::getLogEntry)
3132 .methods(boost::beast::http::verb::get)(std::bind_front(
3133 handleDBusEventLogEntryDownloadGet, std::ref(app), "System"));
3134}
3135
Claire Weinanfdd26902022-03-01 14:18:25 -08003136inline void requestRoutesFaultLogDumpService(App& app)
3137{
3138 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3139 .privileges(redfish::privileges::getLogService)
3140 .methods(boost::beast::http::verb::get)(std::bind_front(
3141 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3142}
3143
3144inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3145{
3146 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3147 .privileges(redfish::privileges::getLogEntryCollection)
3148 .methods(boost::beast::http::verb::get)(
3149 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3150 std::ref(app), "FaultLog"));
3151}
3152
3153inline void requestRoutesFaultLogDumpEntry(App& app)
3154{
3155 BMCWEB_ROUTE(app,
3156 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3157 .privileges(redfish::privileges::getLogEntry)
3158 .methods(boost::beast::http::verb::get)(std::bind_front(
3159 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3160
3161 BMCWEB_ROUTE(app,
3162 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3163 .privileges(redfish::privileges::deleteLogEntry)
3164 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3165 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3166}
3167
3168inline void requestRoutesFaultLogDumpClear(App& app)
3169{
3170 BMCWEB_ROUTE(
3171 app,
3172 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3173 .privileges(redfish::privileges::postLogService)
3174 .methods(boost::beast::http::verb::post)(std::bind_front(
3175 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003176}
3177
3178inline void requestRoutesSystemDumpService(App& app)
3179{
Ed Tanous22d268c2022-05-19 09:39:07 -07003180 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003181 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003182 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003183 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003184}
3185
3186inline void requestRoutesSystemDumpEntryCollection(App& app)
3187{
Ed Tanous22d268c2022-05-19 09:39:07 -07003188 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003189 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07003190 .methods(boost::beast::http::verb::get)(std::bind_front(
3191 handleLogServicesDumpEntriesCollectionComputerSystemGet,
3192 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003193}
3194
3195inline void requestRoutesSystemDumpEntry(App& app)
3196{
3197 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003198 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003199 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003200 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003201 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003202
3203 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003204 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003205 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003206 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003207 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003208}
3209
3210inline void requestRoutesSystemDumpCreate(App& app)
3211{
George Liu0fda0f12021-11-16 10:06:17 +08003212 BMCWEB_ROUTE(
3213 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003214 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003215 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003216 .methods(boost::beast::http::verb::post)(std::bind_front(
3217 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3218 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003219}
3220
3221inline void requestRoutesSystemDumpClear(App& app)
3222{
George Liu0fda0f12021-11-16 10:06:17 +08003223 BMCWEB_ROUTE(
3224 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003225 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003226 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003227 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003228 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003229}
3230
3231inline void requestRoutesCrashdumpService(App& app)
3232{
3233 // Note: Deviated from redfish privilege registry for GET & HEAD
3234 // method for security reasons.
3235 /**
3236 * Functions triggers appropriate requests on DBus
3237 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003238 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003239 // This is incorrect, should be:
3240 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003241 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003242 .methods(boost::beast::http::verb::get)(
3243 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003244 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3245 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003246 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003247 {
3248 return;
3249 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003250 if constexpr (bmcwebEnableMultiHost)
3251 {
3252 // Option currently returns no systems. TBD
3253 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3254 systemName);
3255 return;
3256 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003257 if (systemName != "system")
3258 {
3259 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3260 systemName);
3261 return;
3262 }
3263
Ed Tanous002d39b2022-05-31 08:59:27 -07003264 // Copy over the static data to include the entries added by
3265 // SubRoute
3266 asyncResp->res.jsonValue["@odata.id"] =
3267 "/redfish/v1/Systems/system/LogServices/Crashdump";
3268 asyncResp->res.jsonValue["@odata.type"] =
3269 "#LogService.v1_2_0.LogService";
3270 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3271 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303272 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003273 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3274 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303275
Ed Tanous002d39b2022-05-31 08:59:27 -07003276 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003277 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003278 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3279 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3280 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303281
Ed Tanous002d39b2022-05-31 08:59:27 -07003282 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003283 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003284 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3285 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3286 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3287 ["target"] =
3288 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
Patrick Williams5a39f772023-10-20 11:20:21 -05003289 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003290}
3291
3292void inline requestRoutesCrashdumpClear(App& app)
3293{
George Liu0fda0f12021-11-16 10:06:17 +08003294 BMCWEB_ROUTE(
3295 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003296 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003297 // This is incorrect, should be:
3298 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003299 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003300 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003301 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003302 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3303 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003304 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003305 {
3306 return;
3307 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003308 if constexpr (bmcwebEnableMultiHost)
3309 {
3310 // Option currently returns no systems. TBD
3311 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3312 systemName);
3313 return;
3314 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003315 if (systemName != "system")
3316 {
3317 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3318 systemName);
3319 return;
3320 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003321 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003322 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003323 const std::string&) {
3324 if (ec)
3325 {
3326 messages::internalError(asyncResp->res);
3327 return;
3328 }
3329 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003330 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003331 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003332 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003333}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003334
zhanghch058d1b46d2021-04-01 11:18:24 +08003335static void
3336 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3337 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003338{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003339 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003340 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003341 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003342 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003343 if (ec)
3344 {
Ed Tanous62598e32023-07-17 17:06:25 -07003345 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003346 if (ec.value() ==
3347 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003348 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003349 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003350 }
3351 else
3352 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003353 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003354 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003355 return;
3356 }
3357
3358 std::string timestamp{};
3359 std::string filename{};
3360 std::string logfile{};
3361 parseCrashdumpParameters(params, filename, timestamp, logfile);
3362
3363 if (filename.empty() || timestamp.empty())
3364 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003365 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003366 return;
3367 }
3368
3369 std::string crashdumpURI =
3370 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3371 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003372 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003373 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003374 logEntry["@odata.id"] = boost::urls::format(
3375 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3376 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003377 logEntry["Name"] = "CPU Crashdump";
3378 logEntry["Id"] = logID;
3379 logEntry["EntryType"] = "Oem";
3380 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3381 logEntry["DiagnosticDataType"] = "OEM";
3382 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3383 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003384
3385 // If logEntryJson references an array of LogEntry resources
3386 // ('Members' list), then push this as a new entry, otherwise set it
3387 // directly
3388 if (logEntryJson.is_array())
3389 {
3390 logEntryJson.push_back(logEntry);
3391 asyncResp->res.jsonValue["Members@odata.count"] =
3392 logEntryJson.size();
3393 }
3394 else
3395 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003396 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003397 }
3398 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003399 sdbusplus::asio::getAllProperties(
3400 *crow::connections::systemBus, crashdumpObject,
3401 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3402 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003403}
3404
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003405inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003406{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003407 // Note: Deviated from redfish privilege registry for GET & HEAD
3408 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003409 /**
3410 * Functions triggers appropriate requests on DBus
3411 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003412 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003413 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003414 // This is incorrect, should be.
3415 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003416 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003417 .methods(boost::beast::http::verb::get)(
3418 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003419 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3420 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003421 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003422 {
3423 return;
3424 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003425 if constexpr (bmcwebEnableMultiHost)
3426 {
3427 // Option currently returns no systems. TBD
3428 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3429 systemName);
3430 return;
3431 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003432 if (systemName != "system")
3433 {
3434 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3435 systemName);
3436 return;
3437 }
3438
George Liu7a1dbc42022-12-07 16:03:22 +08003439 constexpr std::array<std::string_view, 1> interfaces = {
3440 crashdumpInterface};
3441 dbus::utility::getSubTreePaths(
3442 "/", 0, interfaces,
3443 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003444 const std::vector<std::string>& resp) {
3445 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003446 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003447 if (ec.value() !=
3448 boost::system::errc::no_such_file_or_directory)
3449 {
Ed Tanous62598e32023-07-17 17:06:25 -07003450 BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
3451 ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003452 messages::internalError(asyncResp->res);
3453 return;
3454 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003455 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003456 asyncResp->res.jsonValue["@odata.type"] =
3457 "#LogEntryCollection.LogEntryCollection";
3458 asyncResp->res.jsonValue["@odata.id"] =
3459 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3460 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3461 asyncResp->res.jsonValue["Description"] =
3462 "Collection of Crashdump Entries";
3463 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3464 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003465
Ed Tanous002d39b2022-05-31 08:59:27 -07003466 for (const std::string& path : resp)
3467 {
3468 const sdbusplus::message::object_path objPath(path);
3469 // Get the log ID
3470 std::string logID = objPath.filename();
3471 if (logID.empty())
3472 {
3473 continue;
3474 }
3475 // Add the log entry to the array
3476 logCrashdumpEntry(asyncResp, logID,
3477 asyncResp->res.jsonValue["Members"]);
3478 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003479 });
Patrick Williams5a39f772023-10-20 11:20:21 -05003480 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003481}
Ed Tanous1da66f72018-07-27 16:13:37 -07003482
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003483inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003484{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003485 // Note: Deviated from redfish privilege registry for GET & HEAD
3486 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003487
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003488 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003489 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003490 // this is incorrect, should be
3491 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003492 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003493 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003494 [&app](const crow::Request& req,
3495 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003496 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003497 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003498 {
3499 return;
3500 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003501 if constexpr (bmcwebEnableMultiHost)
3502 {
3503 // Option currently returns no systems. TBD
3504 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3505 systemName);
3506 return;
3507 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003508 if (systemName != "system")
3509 {
3510 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3511 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003512 return;
3513 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003514 const std::string& logID = param;
3515 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
Patrick Williams5a39f772023-10-20 11:20:21 -05003516 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003517}
Ed Tanous1da66f72018-07-27 16:13:37 -07003518
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003519inline void requestRoutesCrashdumpFile(App& app)
3520{
3521 // Note: Deviated from redfish privilege registry for GET & HEAD
3522 // method for security reasons.
3523 BMCWEB_ROUTE(
3524 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003525 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003526 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003527 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003528 [](const crow::Request& req,
3529 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003530 const std::string& systemName, const std::string& logID,
3531 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003532 // Do not call getRedfishRoute here since the crashdump file is not a
3533 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003534
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003535 if constexpr (bmcwebEnableMultiHost)
3536 {
3537 // Option currently returns no systems. TBD
3538 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3539 systemName);
3540 return;
3541 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003542 if (systemName != "system")
3543 {
3544 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3545 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003546 return;
3547 }
3548
Ed Tanous002d39b2022-05-31 08:59:27 -07003549 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003550 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003551 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003552 const std::vector<
3553 std::pair<std::string, dbus::utility::DbusVariantType>>&
3554 resp) {
3555 if (ec)
3556 {
Ed Tanous62598e32023-07-17 17:06:25 -07003557 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003558 messages::internalError(asyncResp->res);
3559 return;
3560 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003561
Ed Tanous002d39b2022-05-31 08:59:27 -07003562 std::string dbusFilename{};
3563 std::string dbusTimestamp{};
3564 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003565
Ed Tanous002d39b2022-05-31 08:59:27 -07003566 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3567 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003568
Ed Tanous002d39b2022-05-31 08:59:27 -07003569 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3570 dbusFilepath.empty())
3571 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003572 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003573 return;
3574 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003575
Ed Tanous002d39b2022-05-31 08:59:27 -07003576 // Verify the file name parameter is correct
3577 if (fileName != dbusFilename)
3578 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003579 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003580 return;
3581 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003582
Ed Tanous27b0cf92023-08-07 12:02:40 -07003583 if (!asyncResp->res.openFile(dbusFilepath))
Ed Tanous002d39b2022-05-31 08:59:27 -07003584 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003585 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003586 return;
3587 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003588
Ed Tanous002d39b2022-05-31 08:59:27 -07003589 // Configure this to be a file download when accessed
3590 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003591 asyncResp->res.addHeader(
3592 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003593 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003594 sdbusplus::asio::getAllProperties(
3595 *crow::connections::systemBus, crashdumpObject,
3596 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3597 std::move(getStoredLogCallback));
Patrick Williams5a39f772023-10-20 11:20:21 -05003598 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003599}
3600
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003601enum class OEMDiagnosticType
3602{
3603 onDemand,
3604 telemetry,
3605 invalid,
3606};
3607
Ed Tanous26ccae32023-02-16 10:28:44 -08003608inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003609{
3610 if (oemDiagStr == "OnDemand")
3611 {
3612 return OEMDiagnosticType::onDemand;
3613 }
3614 if (oemDiagStr == "Telemetry")
3615 {
3616 return OEMDiagnosticType::telemetry;
3617 }
3618
3619 return OEMDiagnosticType::invalid;
3620}
3621
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003622inline void requestRoutesCrashdumpCollect(App& app)
3623{
3624 // Note: Deviated from redfish privilege registry for GET & HEAD
3625 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003626 BMCWEB_ROUTE(
3627 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003628 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003629 // The below is incorrect; Should be ConfigureManager
3630 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003631 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003632 .methods(boost::beast::http::verb::post)(
3633 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003634 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3635 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003636 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003637 {
3638 return;
3639 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003640
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003641 if constexpr (bmcwebEnableMultiHost)
3642 {
3643 // Option currently returns no systems. TBD
3644 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3645 systemName);
3646 return;
3647 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003648 if (systemName != "system")
3649 {
3650 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3651 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003652 return;
3653 }
3654
Ed Tanous002d39b2022-05-31 08:59:27 -07003655 std::string diagnosticDataType;
3656 std::string oemDiagnosticDataType;
3657 if (!redfish::json_util::readJsonAction(
3658 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3659 "OEMDiagnosticDataType", oemDiagnosticDataType))
3660 {
3661 return;
3662 }
3663
3664 if (diagnosticDataType != "OEM")
3665 {
Ed Tanous62598e32023-07-17 17:06:25 -07003666 BMCWEB_LOG_ERROR(
3667 "Only OEM DiagnosticDataType supported for Crashdump");
Ed Tanous002d39b2022-05-31 08:59:27 -07003668 messages::actionParameterValueFormatError(
3669 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3670 "CollectDiagnosticData");
3671 return;
3672 }
3673
3674 OEMDiagnosticType oemDiagType =
3675 getOEMDiagnosticType(oemDiagnosticDataType);
3676
3677 std::string iface;
3678 std::string method;
3679 std::string taskMatchStr;
3680 if (oemDiagType == OEMDiagnosticType::onDemand)
3681 {
3682 iface = crashdumpOnDemandInterface;
3683 method = "GenerateOnDemandLog";
3684 taskMatchStr = "type='signal',"
3685 "interface='org.freedesktop.DBus.Properties',"
3686 "member='PropertiesChanged',"
3687 "arg0namespace='com.intel.crashdump'";
3688 }
3689 else if (oemDiagType == OEMDiagnosticType::telemetry)
3690 {
3691 iface = crashdumpTelemetryInterface;
3692 method = "GenerateTelemetryLog";
3693 taskMatchStr = "type='signal',"
3694 "interface='org.freedesktop.DBus.Properties',"
3695 "member='PropertiesChanged',"
3696 "arg0namespace='com.intel.crashdump'";
3697 }
3698 else
3699 {
Ed Tanous62598e32023-07-17 17:06:25 -07003700 BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
3701 oemDiagnosticDataType);
Ed Tanous002d39b2022-05-31 08:59:27 -07003702 messages::actionParameterValueFormatError(
3703 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3704 "CollectDiagnosticData");
3705 return;
3706 }
3707
3708 auto collectCrashdumpCallback =
3709 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003710 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003711 const std::string&) mutable {
3712 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003713 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003714 if (ec.value() == boost::system::errc::operation_not_supported)
3715 {
3716 messages::resourceInStandby(asyncResp->res);
3717 }
3718 else if (ec.value() ==
3719 boost::system::errc::device_or_resource_busy)
3720 {
3721 messages::serviceTemporarilyUnavailable(asyncResp->res,
3722 "60");
3723 }
3724 else
3725 {
3726 messages::internalError(asyncResp->res);
3727 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003728 return;
3729 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003730 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -07003731 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003732 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07003733 if (!ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -07003734 {
3735 taskData->messages.emplace_back(messages::taskCompletedOK(
3736 std::to_string(taskData->index)));
3737 taskData->state = "Completed";
3738 }
3739 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05003740 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003741 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003742
Ed Tanous002d39b2022-05-31 08:59:27 -07003743 task->startTimer(std::chrono::minutes(5));
3744 task->populateResp(asyncResp->res);
3745 task->payload.emplace(std::move(payload));
3746 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003747
Ed Tanous002d39b2022-05-31 08:59:27 -07003748 crow::connections::systemBus->async_method_call(
3749 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3750 iface, method);
Patrick Williams5a39f772023-10-20 11:20:21 -05003751 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003752}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003753
Andrew Geisslercb92c032018-08-17 07:56:14 -07003754/**
3755 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3756 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003757inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003758{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003759 /**
3760 * Function handles POST method request.
3761 * The Clear Log actions does not require any parameter.The action deletes
3762 * all entries found in the Entries collection for this Log Service.
3763 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003764
George Liu0fda0f12021-11-16 10:06:17 +08003765 BMCWEB_ROUTE(
3766 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003767 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003768 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003769 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003770 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003771 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3772 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003773 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003774 {
3775 return;
3776 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003777 if constexpr (bmcwebEnableMultiHost)
3778 {
3779 // Option currently returns no systems. TBD
3780 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3781 systemName);
3782 return;
3783 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003784 if (systemName != "system")
3785 {
3786 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3787 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003788 return;
3789 }
Ed Tanous62598e32023-07-17 17:06:25 -07003790 BMCWEB_LOG_DEBUG("Do delete all entries.");
Andrew Geisslercb92c032018-08-17 07:56:14 -07003791
Ed Tanous002d39b2022-05-31 08:59:27 -07003792 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003793 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07003794 BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07003795 if (ec)
3796 {
3797 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003798 BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003799 asyncResp->res.result(
3800 boost::beast::http::status::internal_server_error);
3801 return;
3802 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003803
Ed Tanous002d39b2022-05-31 08:59:27 -07003804 asyncResp->res.result(boost::beast::http::status::no_content);
3805 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003806
Ed Tanous002d39b2022-05-31 08:59:27 -07003807 // Make call to Logging service to request Clear Log
3808 crow::connections::systemBus->async_method_call(
3809 respHandler, "xyz.openbmc_project.Logging",
3810 "/xyz/openbmc_project/logging",
3811 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003812 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003813}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003814
3815/****************************************************
3816 * Redfish PostCode interfaces
3817 * using DBUS interface: getPostCodesTS
3818 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003819inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003820{
Ed Tanous22d268c2022-05-19 09:39:07 -07003821 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003822 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003823 .methods(boost::beast::http::verb::get)(
3824 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003825 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3826 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003827 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003828 {
3829 return;
3830 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003831 if constexpr (bmcwebEnableMultiHost)
3832 {
3833 // Option currently returns no systems. TBD
3834 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3835 systemName);
3836 return;
3837 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003838 if (systemName != "system")
3839 {
3840 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3841 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003842 return;
3843 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003844 asyncResp->res.jsonValue["@odata.id"] =
3845 "/redfish/v1/Systems/system/LogServices/PostCodes";
3846 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05003847 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07003848 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3849 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003850 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003851 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3852 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3853 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303854
Ed Tanous002d39b2022-05-31 08:59:27 -07003855 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003856 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003857 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3858 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3859 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303860
Ed Tanous002d39b2022-05-31 08:59:27 -07003861 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3862 {"target",
3863 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05003864 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003865}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003866
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003867inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003868{
George Liu0fda0f12021-11-16 10:06:17 +08003869 BMCWEB_ROUTE(
3870 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003871 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003872 // The following privilege is incorrect; It should be ConfigureManager
3873 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003874 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003875 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003876 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003877 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3878 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003879 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003880 {
3881 return;
3882 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003883 if constexpr (bmcwebEnableMultiHost)
3884 {
3885 // Option currently returns no systems. TBD
3886 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3887 systemName);
3888 return;
3889 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003890 if (systemName != "system")
3891 {
3892 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3893 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003894 return;
3895 }
Ed Tanous62598e32023-07-17 17:06:25 -07003896 BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003897
Ed Tanous002d39b2022-05-31 08:59:27 -07003898 // Make call to post-code service to request clear all
3899 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003900 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003901 if (ec)
3902 {
3903 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003904 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
3905 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003906 asyncResp->res.result(
3907 boost::beast::http::status::internal_server_error);
3908 messages::internalError(asyncResp->res);
3909 return;
3910 }
Tony Lee18fc70c2023-08-24 16:15:54 +08003911 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003912 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003913 "xyz.openbmc_project.State.Boot.PostCode0",
3914 "/xyz/openbmc_project/State/Boot/PostCode0",
3915 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003916 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003917}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003918
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003919/**
3920 * @brief Parse post code ID and get the current value and index value
3921 * eg: postCodeID=B1-2, currentValue=1, index=2
3922 *
3923 * @param[in] postCodeID Post Code ID
3924 * @param[out] currentValue Current value
3925 * @param[out] index Index value
3926 *
3927 * @return bool true if the parsing is successful, false the parsing fails
3928 */
3929inline static bool parsePostCode(const std::string& postCodeID,
3930 uint64_t& currentValue, uint16_t& index)
3931{
3932 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003933 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003934 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3935 {
3936 return false;
3937 }
3938
Patrick Williams84396af2023-05-11 11:47:45 -05003939 auto start = std::next(split[0].begin());
3940 auto end = split[0].end();
3941 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003942
Patrick Williams84396af2023-05-11 11:47:45 -05003943 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003944 {
3945 return false;
3946 }
3947
Patrick Williams84396af2023-05-11 11:47:45 -05003948 start = split[1].begin();
3949 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003950
Patrick Williams84396af2023-05-11 11:47:45 -05003951 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003952
Patrick Williams84396af2023-05-11 11:47:45 -05003953 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003954}
3955
3956static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003957 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303958 const boost::container::flat_map<
3959 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003960 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3961 const uint64_t skip = 0, const uint64_t top = 0)
3962{
3963 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003964 const registries::Message* message =
3965 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003966
3967 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003968 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303969 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3970 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003971 {
3972 currentCodeIndex++;
3973 std::string postcodeEntryID =
3974 "B" + std::to_string(bootIndex) + "-" +
3975 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3976
3977 uint64_t usecSinceEpoch = code.first;
3978 uint64_t usTimeOffset = 0;
3979
3980 if (1 == currentCodeIndex)
3981 { // already incremented
3982 firstCodeTimeUs = code.first;
3983 }
3984 else
3985 {
3986 usTimeOffset = code.first - firstCodeTimeUs;
3987 }
3988
3989 // skip if no specific codeIndex is specified and currentCodeIndex does
3990 // not fall between top and skip
3991 if ((codeIndex == 0) &&
3992 (currentCodeIndex <= skip || currentCodeIndex > top))
3993 {
3994 continue;
3995 }
3996
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003997 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003998 // currentIndex
3999 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
4000 {
4001 // This is done for simplicity. 1st entry is needed to calculate
4002 // time offset. To improve efficiency, one can get to the entry
4003 // directly (possibly with flatmap's nth method)
4004 continue;
4005 }
4006
4007 // currentCodeIndex is within top and skip or equal to specified code
4008 // index
4009
4010 // Get the Created time from the timestamp
4011 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03004012 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004013
4014 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
4015 std::ostringstream hexCode;
4016 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304017 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004018 std::ostringstream timeOffsetStr;
4019 // Set Fixed -Point Notation
4020 timeOffsetStr << std::fixed;
4021 // Set precision to 4 digits
4022 timeOffsetStr << std::setprecision(4);
4023 // Add double to stream
4024 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004025
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004026 std::string bootIndexStr = std::to_string(bootIndex);
4027 std::string timeOffsetString = timeOffsetStr.str();
4028 std::string hexCodeStr = hexCode.str();
4029
4030 std::array<std::string_view, 3> messageArgs = {
4031 bootIndexStr, timeOffsetString, hexCodeStr};
4032
4033 std::string msg =
4034 redfish::registries::fillMessageArgs(messageArgs, message->message);
4035 if (msg.empty())
ZhikuiRena3316fc2020-01-29 14:58:08 -08004036 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004037 messages::internalError(asyncResp->res);
4038 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004039 }
4040
Tim Leed4342a92020-04-27 11:47:58 +08004041 // Get Severity template from message registry
4042 std::string severity;
4043 if (message != nullptr)
4044 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08004045 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08004046 }
4047
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004048 // Format entry
4049 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05004050 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07004051 bmcLogEntry["@odata.id"] = boost::urls::format(
4052 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
4053 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07004054 bmcLogEntry["Name"] = "POST Code Log Entry";
4055 bmcLogEntry["Id"] = postcodeEntryID;
4056 bmcLogEntry["Message"] = std::move(msg);
4057 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004058 bmcLogEntry["MessageArgs"] = messageArgs;
Jason M. Bills84afc482022-06-24 12:38:23 -07004059 bmcLogEntry["EntryType"] = "Event";
4060 bmcLogEntry["Severity"] = std::move(severity);
4061 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08004062 if (!std::get<std::vector<uint8_t>>(code.second).empty())
4063 {
4064 bmcLogEntry["AdditionalDataURI"] =
4065 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
4066 postcodeEntryID + "/attachment";
4067 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004068
4069 // codeIndex is only specified when querying single entry, return only
4070 // that entry in this case
4071 if (codeIndex != 0)
4072 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004073 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004074 return true;
4075 }
4076
Ed Tanousac106bf2023-06-07 09:24:59 -07004077 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05004078 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08004079 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004080
4081 // Return value is always false when querying multiple entries
4082 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004083}
4084
Ed Tanousac106bf2023-06-07 09:24:59 -07004085static void
4086 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4087 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004088{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004089 uint16_t bootIndex = 0;
4090 uint64_t codeIndex = 0;
4091 if (!parsePostCode(entryId, codeIndex, bootIndex))
4092 {
4093 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07004094 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004095 return;
4096 }
4097
4098 if (bootIndex == 0 || codeIndex == 0)
4099 {
4100 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07004101 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004102 return;
4103 }
4104
ZhikuiRena3316fc2020-01-29 14:58:08 -08004105 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004106 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004107 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304108 const boost::container::flat_map<
4109 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4110 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004111 if (ec)
4112 {
Ed Tanous62598e32023-07-17 17:06:25 -07004113 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004114 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004115 return;
4116 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004117
Ed Tanous002d39b2022-05-31 08:59:27 -07004118 if (postcode.empty())
4119 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004120 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07004121 return;
4122 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004123
Ed Tanousac106bf2023-06-07 09:24:59 -07004124 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004125 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004126 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004127 return;
4128 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004129 },
Jonathan Doman15124762021-01-07 17:54:17 -08004130 "xyz.openbmc_project.State.Boot.PostCode0",
4131 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004132 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4133 bootIndex);
4134}
4135
Ed Tanousac106bf2023-06-07 09:24:59 -07004136static void
4137 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4138 const uint16_t bootIndex, const uint16_t bootCount,
4139 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004140{
4141 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004142 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004143 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304144 const boost::container::flat_map<
4145 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4146 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004147 if (ec)
4148 {
Ed Tanous62598e32023-07-17 17:06:25 -07004149 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004150 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004151 return;
4152 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004153
Ed Tanous002d39b2022-05-31 08:59:27 -07004154 uint64_t endCount = entryCount;
4155 if (!postcode.empty())
4156 {
4157 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07004158 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004159 {
Patrick Williams89492a12023-05-10 07:51:34 -05004160 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
4161 entryCount) -
4162 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004163 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07004164 std::min(static_cast<uint64_t>(top + skip), endCount) -
4165 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004166
Ed Tanousac106bf2023-06-07 09:24:59 -07004167 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4168 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004169 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004170 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004171 }
4172
4173 // continue to previous bootIndex
4174 if (bootIndex < bootCount)
4175 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004176 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07004177 bootCount, endCount, skip, top);
4178 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08004179 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07004180 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004181 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07004182 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4183 std::to_string(skip + top);
4184 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004185 },
Jonathan Doman15124762021-01-07 17:54:17 -08004186 "xyz.openbmc_project.State.Boot.PostCode0",
4187 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004188 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4189 bootIndex);
4190}
4191
zhanghch058d1b46d2021-04-01 11:18:24 +08004192static void
Ed Tanousac106bf2023-06-07 09:24:59 -07004193 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07004194 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004195{
4196 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004197 sdbusplus::asio::getProperty<uint16_t>(
4198 *crow::connections::systemBus,
4199 "xyz.openbmc_project.State.Boot.PostCode0",
4200 "/xyz/openbmc_project/State/Boot/PostCode0",
4201 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07004202 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
4203 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004204 if (ec)
4205 {
Ed Tanous62598e32023-07-17 17:06:25 -07004206 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -07004207 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004208 return;
4209 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004210 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004211 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08004212}
4213
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004214inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004215{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004216 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004217 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07004218 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004219 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004220 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07004221 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4222 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004223 query_param::QueryCapabilities capabilities = {
4224 .canDelegateTop = true,
4225 .canDelegateSkip = true,
4226 };
4227 query_param::Query delegatedQuery;
4228 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00004229 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07004230 {
4231 return;
4232 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004233 if constexpr (bmcwebEnableMultiHost)
4234 {
4235 // Option currently returns no systems. TBD
4236 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4237 systemName);
4238 return;
4239 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004240
4241 if (systemName != "system")
4242 {
4243 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4244 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004245 return;
4246 }
Ed Tanous002d39b2022-05-31 08:59:27 -07004247 asyncResp->res.jsonValue["@odata.type"] =
4248 "#LogEntryCollection.LogEntryCollection";
4249 asyncResp->res.jsonValue["@odata.id"] =
4250 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4251 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4252 asyncResp->res.jsonValue["Description"] =
4253 "Collection of POST Code Log Entries";
4254 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4255 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07004256 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08004257 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07004258 getCurrentBootNumber(asyncResp, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004259 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004260}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004261
George Liu647b3cd2021-07-05 12:43:56 +08004262inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4263{
George Liu0fda0f12021-11-16 10:06:17 +08004264 BMCWEB_ROUTE(
4265 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004266 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004267 .privileges(redfish::privileges::getLogEntry)
4268 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004269 [&app](const crow::Request& req,
4270 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004271 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004272 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004273 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004274 {
4275 return;
4276 }
Matt Spinler72e21372023-04-19 12:53:33 -05004277 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004278 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004279 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004280 {
4281 asyncResp->res.result(boost::beast::http::status::bad_request);
4282 return;
4283 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004284 if constexpr (bmcwebEnableMultiHost)
4285 {
4286 // Option currently returns no systems. TBD
4287 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4288 systemName);
4289 return;
4290 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004291 if (systemName != "system")
4292 {
4293 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4294 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004295 return;
4296 }
George Liu647b3cd2021-07-05 12:43:56 +08004297
Ed Tanous002d39b2022-05-31 08:59:27 -07004298 uint64_t currentValue = 0;
4299 uint16_t index = 0;
4300 if (!parsePostCode(postCodeID, currentValue, index))
4301 {
4302 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4303 return;
4304 }
George Liu647b3cd2021-07-05 12:43:56 +08004305
Ed Tanous002d39b2022-05-31 08:59:27 -07004306 crow::connections::systemBus->async_method_call(
4307 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004308 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004309 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4310 postcodes) {
4311 if (ec.value() == EBADR)
4312 {
4313 messages::resourceNotFound(asyncResp->res, "LogEntry",
4314 postCodeID);
4315 return;
4316 }
4317 if (ec)
4318 {
Ed Tanous62598e32023-07-17 17:06:25 -07004319 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07004320 messages::internalError(asyncResp->res);
4321 return;
4322 }
George Liu647b3cd2021-07-05 12:43:56 +08004323
Ed Tanous002d39b2022-05-31 08:59:27 -07004324 size_t value = static_cast<size_t>(currentValue) - 1;
4325 if (value == std::string::npos || postcodes.size() < currentValue)
4326 {
Ed Tanous62598e32023-07-17 17:06:25 -07004327 BMCWEB_LOG_WARNING("Wrong currentValue value");
Ed Tanous002d39b2022-05-31 08:59:27 -07004328 messages::resourceNotFound(asyncResp->res, "LogEntry",
4329 postCodeID);
4330 return;
4331 }
George Liu647b3cd2021-07-05 12:43:56 +08004332
Ed Tanous002d39b2022-05-31 08:59:27 -07004333 const auto& [tID, c] = postcodes[value];
4334 if (c.empty())
4335 {
Ed Tanous62598e32023-07-17 17:06:25 -07004336 BMCWEB_LOG_WARNING("No found post code data");
Ed Tanous002d39b2022-05-31 08:59:27 -07004337 messages::resourceNotFound(asyncResp->res, "LogEntry",
4338 postCodeID);
4339 return;
4340 }
4341 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4342 const char* d = reinterpret_cast<const char*>(c.data());
4343 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004344
Ed Tanousd9f6c622022-03-17 09:12:17 -07004345 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004346 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004347 asyncResp->res.addHeader(
4348 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous27b0cf92023-08-07 12:02:40 -07004349 asyncResp->res.write(crow::utility::base64encode(strData));
Patrick Williams5a39f772023-10-20 11:20:21 -05004350 },
Ed Tanous002d39b2022-05-31 08:59:27 -07004351 "xyz.openbmc_project.State.Boot.PostCode0",
4352 "/xyz/openbmc_project/State/Boot/PostCode0",
4353 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
Patrick Williams5a39f772023-10-20 11:20:21 -05004354 });
George Liu647b3cd2021-07-05 12:43:56 +08004355}
4356
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004357inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004358{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004359 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004360 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004361 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004362 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004363 [&app](const crow::Request& req,
4364 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004365 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004366 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004367 {
4368 return;
4369 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004370 if constexpr (bmcwebEnableMultiHost)
4371 {
4372 // Option currently returns no systems. TBD
4373 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4374 systemName);
4375 return;
4376 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004377 if (systemName != "system")
4378 {
4379 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4380 systemName);
4381 return;
4382 }
4383
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004384 getPostCodeForEntry(asyncResp, targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05004385 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004386}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004387
Ed Tanous1da66f72018-07-27 16:13:37 -07004388} // namespace redfish