blob: be26f656963eb65c81113bdc9649533caa5b1be7 [file] [log] [blame]
Ed Tanous1da66f72018-07-27 16:13:37 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "error_messages.hpp"
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -060021#include "generated/enums/log_entry.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080022#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080023#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080024#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "query.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070026#include "registries.hpp"
27#include "registries/base_message_registry.hpp"
28#include "registries/openbmc_message_registry.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include "registries/privilege_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080030#include "task.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080031#include "utils/dbus_utils.hpp"
32#include "utils/time_utils.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070033
Jason M. Billse1f26342018-07-18 12:12:00 -070034#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060035#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060036#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070037
Ed Tanous9896eae2022-07-23 15:07:33 -070038#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070039#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060040#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070041#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070042#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070043#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080044#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070045#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020046#include <sdbusplus/asio/property.hpp>
47#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050048
George Liu7a1dbc42022-12-07 16:03:22 +080049#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080050#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070051#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080052#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070053#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070054#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080055#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070056
57namespace redfish
58{
59
Patrick Williams89492a12023-05-10 07:51:34 -050060constexpr const char* crashdumpObject = "com.intel.crashdump";
61constexpr const char* crashdumpPath = "/com/intel/crashdump";
62constexpr const char* crashdumpInterface = "com.intel.crashdump";
63constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070064 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050065constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070066 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050067constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070068 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070069
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060070enum class DumpCreationProgress
71{
72 DUMP_CREATE_SUCCESS,
73 DUMP_CREATE_FAILED,
74 DUMP_CREATE_INPROGRESS
75};
76
James Feistf6150402019-01-08 10:36:20 -080077namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070078
Gunnar Mills1214b7e2020-06-04 10:11:30 -050079inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070080{
Ed Tanousd4d25792020-09-29 15:15:03 -070081 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
82 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
83 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
84 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070085 {
86 return "Critical";
87 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070088 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
89 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
90 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070091 {
92 return "OK";
93 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070094 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070095 {
96 return "Warning";
97 }
98 return "";
99}
100
Abhishek Patel9017faf2021-09-14 22:48:55 -0500101inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
102{
103 std::optional<bool> notifyAction;
104 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
105 {
106 notifyAction = true;
107 }
108 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
109 {
110 notifyAction = false;
111 }
112
113 return notifyAction;
114}
115
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700116inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800117 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700118 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700119{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500120 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700121 size_t length = 0;
122 int ret = 0;
123 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800124 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
125 const void** dataVoid = reinterpret_cast<const void**>(&data);
126
127 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700128 if (ret < 0)
129 {
130 return ret;
131 }
Ed Tanous39e77502019-03-04 17:35:53 -0800132 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700133 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000134 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700135 return ret;
136}
137
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700138inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800139 std::string_view field, const int& base,
140 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700141{
142 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800143 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700144 // Get the metadata from the requested field of the journal entry
145 ret = getJournalMetadata(journal, field, metadata);
146 if (ret < 0)
147 {
148 return ret;
149 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000150 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700151 return ret;
152}
153
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700154inline static bool getEntryTimestamp(sd_journal* journal,
155 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800156{
157 int ret = 0;
158 uint64_t timestamp = 0;
159 ret = sd_journal_get_realtime_usec(journal, &timestamp);
160 if (ret < 0)
161 {
162 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
163 << strerror(-ret);
164 return false;
165 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300166 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500167 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800168}
Ed Tanous50b8a432022-02-03 16:29:50 -0800169
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
171 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700172{
173 int ret = 0;
174 static uint64_t prevTs = 0;
175 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700176 if (firstEntry)
177 {
178 prevTs = 0;
179 }
180
Jason M. Bills16428a12018-11-02 12:42:29 -0700181 // Get the entry timestamp
182 uint64_t curTs = 0;
183 ret = sd_journal_get_realtime_usec(journal, &curTs);
184 if (ret < 0)
185 {
186 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
187 << strerror(-ret);
188 return false;
189 }
190 // If the timestamp isn't unique, increment the index
191 if (curTs == prevTs)
192 {
193 index++;
194 }
195 else
196 {
197 // Otherwise, reset it
198 index = 0;
199 }
200 // Save the timestamp
201 prevTs = curTs;
202
203 entryID = std::to_string(curTs);
204 if (index > 0)
205 {
206 entryID += "_" + std::to_string(index);
207 }
208 return true;
209}
210
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500211static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700212 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700213{
Ed Tanous271584a2019-07-09 16:24:22 -0700214 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700215 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700216 if (firstEntry)
217 {
218 prevTs = 0;
219 }
220
Jason M. Bills95820182019-04-22 16:25:34 -0700221 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700222 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700223 std::tm timeStruct = {};
224 std::istringstream entryStream(logEntry);
225 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
226 {
227 curTs = std::mktime(&timeStruct);
228 }
229 // If the timestamp isn't unique, increment the index
230 if (curTs == prevTs)
231 {
232 index++;
233 }
234 else
235 {
236 // Otherwise, reset it
237 index = 0;
238 }
239 // Save the timestamp
240 prevTs = curTs;
241
242 entryID = std::to_string(curTs);
243 if (index > 0)
244 {
245 entryID += "_" + std::to_string(index);
246 }
247 return true;
248}
249
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700250inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800251 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
252 const std::string& entryID, uint64_t& timestamp,
253 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700254{
255 if (entryID.empty())
256 {
257 return false;
258 }
259 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800260 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700261
Ed Tanous81ce6092020-12-17 16:54:55 +0000262 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800263 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700264 {
265 // Timestamp has an index
266 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800267 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700268 indexStr.remove_prefix(underscorePos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500269 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
270 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700271 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700272 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800273 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700274 return false;
275 }
276 }
277 // Timestamp has no index
Patrick Williams84396af2023-05-11 11:47:45 -0500278 auto [ptr, ec] = std::from_chars(tsStr.begin(), tsStr.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700279 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700280 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800281 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700282 return false;
283 }
284 return true;
285}
286
Jason M. Bills95820182019-04-22 16:25:34 -0700287static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500288 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700289{
290 static const std::filesystem::path redfishLogDir = "/var/log";
291 static const std::string redfishLogFilename = "redfish";
292
293 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500294 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700295 std::filesystem::directory_iterator(redfishLogDir))
296 {
297 // If we find a redfish log file, save the path
298 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700299 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700300 {
301 redfishLogFiles.emplace_back(redfishLogDir / filename);
302 }
303 }
304 // As the log files rotate, they are appended with a ".#" that is higher for
305 // the older logs. Since we don't expect more than 10 log files, we
306 // can just sort the list to get them in order from newest to oldest
307 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
308
309 return !redfishLogFiles.empty();
310}
311
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600312inline log_entry::OriginatorTypes
313 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
314{
315 if (originatorType ==
316 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
317 {
318 return log_entry::OriginatorTypes::Client;
319 }
320 if (originatorType ==
321 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
322 {
323 return log_entry::OriginatorTypes::Internal;
324 }
325 if (originatorType ==
326 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
327 {
328 return log_entry::OriginatorTypes::SupportingService;
329 }
330 return log_entry::OriginatorTypes::Invalid;
331}
332
Claire Weinanaefe3782022-07-15 19:17:19 -0700333inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800334 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700335 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600336 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700337 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
338{
339 for (const auto& interfaceMap : object.second)
340 {
341 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
342 {
343 for (const auto& propertyMap : interfaceMap.second)
344 {
345 if (propertyMap.first == "Status")
346 {
347 const auto* status =
348 std::get_if<std::string>(&propertyMap.second);
349 if (status == nullptr)
350 {
351 messages::internalError(asyncResp->res);
352 break;
353 }
354 dumpStatus = *status;
355 }
356 }
357 }
358 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
359 {
360 for (const auto& propertyMap : interfaceMap.second)
361 {
362 if (propertyMap.first == "Size")
363 {
364 const auto* sizePtr =
365 std::get_if<uint64_t>(&propertyMap.second);
366 if (sizePtr == nullptr)
367 {
368 messages::internalError(asyncResp->res);
369 break;
370 }
371 size = *sizePtr;
372 break;
373 }
374 }
375 }
376 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
377 {
378 for (const auto& propertyMap : interfaceMap.second)
379 {
380 if (propertyMap.first == "Elapsed")
381 {
382 const uint64_t* usecsTimeStamp =
383 std::get_if<uint64_t>(&propertyMap.second);
384 if (usecsTimeStamp == nullptr)
385 {
386 messages::internalError(asyncResp->res);
387 break;
388 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700389 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700390 break;
391 }
392 }
393 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600394 else if (interfaceMap.first ==
395 "xyz.openbmc_project.Common.OriginatedBy")
396 {
397 for (const auto& propertyMap : interfaceMap.second)
398 {
399 if (propertyMap.first == "OriginatorId")
400 {
401 const std::string* id =
402 std::get_if<std::string>(&propertyMap.second);
403 if (id == nullptr)
404 {
405 messages::internalError(asyncResp->res);
406 break;
407 }
408 originatorId = *id;
409 }
410
411 if (propertyMap.first == "OriginatorType")
412 {
413 const std::string* type =
414 std::get_if<std::string>(&propertyMap.second);
415 if (type == nullptr)
416 {
417 messages::internalError(asyncResp->res);
418 break;
419 }
420
421 originatorType = mapDbusOriginatorTypeToRedfish(*type);
422 if (originatorType == log_entry::OriginatorTypes::Invalid)
423 {
424 messages::internalError(asyncResp->res);
425 break;
426 }
427 }
428 }
429 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700430 }
431}
432
Nan Zhou21ab4042022-06-26 23:07:40 +0000433static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800434{
435 std::string entriesPath;
436
437 if (dumpType == "BMC")
438 {
439 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
440 }
441 else if (dumpType == "FaultLog")
442 {
443 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
444 }
445 else if (dumpType == "System")
446 {
447 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
448 }
449 else
450 {
451 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
452 << dumpType;
453 }
454
455 // Returns empty string on error
456 return entriesPath;
457}
458
zhanghch058d1b46d2021-04-01 11:18:24 +0800459inline void
460 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
461 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500462{
Claire Weinanfdd26902022-03-01 14:18:25 -0800463 std::string entriesPath = getDumpEntriesPath(dumpType);
464 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500465 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500466 messages::internalError(asyncResp->res);
467 return;
468 }
469
470 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800471 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800472 dumpType](const boost::system::error_code& ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800473 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700474 if (ec)
475 {
476 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
477 messages::internalError(asyncResp->res);
478 return;
479 }
480
Claire Weinanfdd26902022-03-01 14:18:25 -0800481 // Remove ending slash
482 std::string odataIdStr = entriesPath;
483 if (!odataIdStr.empty())
484 {
485 odataIdStr.pop_back();
486 }
487
488 asyncResp->res.jsonValue["@odata.type"] =
489 "#LogEntryCollection.LogEntryCollection";
490 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
491 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500492 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
493 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800494
Ed Tanous002d39b2022-05-31 08:59:27 -0700495 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
496 entriesArray = nlohmann::json::array();
497 std::string dumpEntryPath =
498 "/xyz/openbmc_project/dump/" +
499 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
500
501 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
502 return AlphanumLess<std::string>()(l.first.filename(),
503 r.first.filename());
504 });
505
506 for (auto& object : resp)
507 {
508 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500509 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700510 continue;
511 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700512 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700513 uint64_t size = 0;
514 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600515 std::string originatorId;
516 log_entry::OriginatorTypes originatorType =
517 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700518 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700519
520 std::string entryID = object.first.filename();
521 if (entryID.empty())
522 {
523 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500524 }
525
Claire Weinanc6fecda2022-07-15 10:43:25 -0700526 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600527 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700528 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700529
530 if (dumpStatus !=
531 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
532 !dumpStatus.empty())
533 {
534 // Dump status is not Complete, no need to enumerate
535 continue;
536 }
537
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600538 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800539 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700540 thisEntry["Id"] = entryID;
541 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700542 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700543 thisEntry["Created"] =
544 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700545
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600546 if (!originatorId.empty())
547 {
548 thisEntry["Originator"] = originatorId;
549 thisEntry["OriginatorType"] = originatorType;
550 }
551
Ed Tanous002d39b2022-05-31 08:59:27 -0700552 if (dumpType == "BMC")
553 {
554 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500555 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
556 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800557 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700558 }
559 else if (dumpType == "System")
560 {
561 thisEntry["DiagnosticDataType"] = "OEM";
562 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500563 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
564 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800565 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700566 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500567 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 }
569 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500570 },
571 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
572 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
573}
574
zhanghch058d1b46d2021-04-01 11:18:24 +0800575inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700576 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500578{
Claire Weinanfdd26902022-03-01 14:18:25 -0800579 std::string entriesPath = getDumpEntriesPath(dumpType);
580 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500582 messages::internalError(asyncResp->res);
583 return;
584 }
585
586 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800587 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800588 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700589 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 if (ec)
591 {
592 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
593 messages::internalError(asyncResp->res);
594 return;
595 }
596
597 bool foundDumpEntry = false;
598 std::string dumpEntryPath =
599 "/xyz/openbmc_project/dump/" +
600 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
601
602 for (const auto& objectPath : resp)
603 {
604 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500605 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700606 continue;
607 }
608
609 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700610 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700611 uint64_t size = 0;
612 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600613 std::string originatorId;
614 log_entry::OriginatorTypes originatorType =
615 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700616
Claire Weinanaefe3782022-07-15 19:17:19 -0700617 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600618 timestampUs, originatorId,
619 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700620
621 if (dumpStatus !=
622 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
623 !dumpStatus.empty())
624 {
625 // Dump status is not Complete
626 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200627 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
628 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500629 return;
630 }
631
Ed Tanous002d39b2022-05-31 08:59:27 -0700632 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600633 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800634 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700635 asyncResp->res.jsonValue["Id"] = entryID;
636 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700638 asyncResp->res.jsonValue["Created"] =
639 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500640
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600641 if (!originatorId.empty())
642 {
643 asyncResp->res.jsonValue["Originator"] = originatorId;
644 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
645 }
646
Ed Tanous002d39b2022-05-31 08:59:27 -0700647 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500648 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700649 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
650 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800651 entriesPath + entryID + "/attachment";
652 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500653 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700654 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500655 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
657 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
658 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800659 entriesPath + entryID + "/attachment";
660 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500661 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700662 }
663 if (!foundDumpEntry)
664 {
665 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
666 messages::internalError(asyncResp->res);
667 return;
668 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500669 },
670 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
671 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
672}
673
zhanghch058d1b46d2021-04-01 11:18:24 +0800674inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800675 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500676 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500677{
Ed Tanous002d39b2022-05-31 08:59:27 -0700678 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800679 [asyncResp, entryID](const boost::system::error_code& ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500680 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
681 if (ec)
682 {
George Liu3de8d8b2021-03-22 17:49:39 +0800683 if (ec.value() == EBADR)
684 {
685 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
686 return;
687 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500688 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800689 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500690 messages::internalError(asyncResp->res);
691 return;
692 }
693 };
694 crow::connections::systemBus->async_method_call(
695 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500696 "/xyz/openbmc_project/dump/" +
697 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
698 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500699 "xyz.openbmc_project.Object.Delete", "Delete");
700}
701
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600702inline DumpCreationProgress
703 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500704{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600705 if (status ==
706 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
707 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
708 {
709 return DumpCreationProgress::DUMP_CREATE_FAILED;
710 }
711 if (status ==
712 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
713 {
714 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
715 }
716 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
717}
718
719inline DumpCreationProgress
720 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
721{
722 for (const auto& [key, val] : values)
723 {
724 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700725 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600726 const std::string* value = std::get_if<std::string>(&val);
727 if (value == nullptr)
728 {
729 BMCWEB_LOG_ERROR << "Status property value is null";
730 return DumpCreationProgress::DUMP_CREATE_FAILED;
731 }
732 return mapDbusStatusToDumpProgress(*value);
733 }
734 }
735 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
736}
737
738inline std::string getDumpEntryPath(const std::string& dumpPath)
739{
740 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
741 {
742 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
743 }
744 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
745 {
746 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
747 }
748 return "";
749}
750
751inline void createDumpTaskCallback(
752 task::Payload&& payload,
753 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
754 const sdbusplus::message::object_path& createdObjPath)
755{
756 const std::string dumpPath = createdObjPath.parent_path().str;
757 const std::string dumpId = createdObjPath.filename();
758
759 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
760
761 if (dumpEntryPath.empty())
762 {
763 BMCWEB_LOG_ERROR << "Invalid dump type received";
764 messages::internalError(asyncResp->res);
765 return;
766 }
767
768 crow::connections::systemBus->async_method_call(
769 [asyncResp, payload, createdObjPath,
770 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800771 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600772 const std::string& introspectXml) {
773 if (ec)
774 {
775 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
776 << ec.message();
777 messages::internalError(asyncResp->res);
778 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700779 }
780
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600781 // Check if the created dump object has implemented Progress
782 // interface to track dump completion. If yes, fetch the "Status"
783 // property of the interface, modify the task state accordingly.
784 // Else, return task completed.
785 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700786
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600787 doc.Parse(introspectXml.data(), introspectXml.size());
788 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
789 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700790 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600791 BMCWEB_LOG_ERROR << "XML document failed to parse";
792 messages::internalError(asyncResp->res);
793 return;
794 }
795 tinyxml2::XMLElement* interfaceNode =
796 pRoot->FirstChildElement("interface");
797
798 bool isProgressIntfPresent = false;
799 while (interfaceNode != nullptr)
800 {
801 const char* thisInterfaceName = interfaceNode->Attribute("name");
802 if (thisInterfaceName != nullptr)
803 {
804 if (thisInterfaceName ==
805 std::string_view("xyz.openbmc_project.Common.Progress"))
806 {
807 interfaceNode =
808 interfaceNode->NextSiblingElement("interface");
809 continue;
810 }
811 isProgressIntfPresent = true;
812 break;
813 }
814 interfaceNode = interfaceNode->NextSiblingElement("interface");
815 }
816
817 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
818 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800819 const boost::system::error_code& err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600820 const std::shared_ptr<task::TaskData>& taskData) {
821 if (err)
822 {
823 BMCWEB_LOG_ERROR << createdObjPath.str
824 << ": Error in creating dump";
825 taskData->messages.emplace_back(messages::internalError());
826 taskData->state = "Cancelled";
827 return task::completed;
828 }
829
830 if (isProgressIntfPresent)
831 {
832 dbus::utility::DBusPropertiesMap values;
833 std::string prop;
834 msg.read(prop, values);
835
836 DumpCreationProgress dumpStatus =
837 getDumpCompletionStatus(values);
838 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
839 {
840 BMCWEB_LOG_ERROR << createdObjPath.str
841 << ": Error in creating dump";
842 taskData->state = "Cancelled";
843 return task::completed;
844 }
845
846 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
847 {
848 BMCWEB_LOG_DEBUG << createdObjPath.str
849 << ": Dump creation task is in progress";
850 return !task::completed;
851 }
852 }
853
Ed Tanous002d39b2022-05-31 08:59:27 -0700854 nlohmann::json retMessage = messages::success();
855 taskData->messages.emplace_back(retMessage);
856
Patrick Williams89492a12023-05-10 07:51:34 -0500857 std::string headerLoc = "Location: " + dumpEntryPath +
858 http_helpers::urlEncode(dumpId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700859 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
860
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600861 BMCWEB_LOG_DEBUG << createdObjPath.str
862 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700863 taskData->state = "Completed";
864 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600865 },
866 "type='signal',interface='org.freedesktop.DBus.Properties',"
867 "member='PropertiesChanged',path='" +
868 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500869
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600870 // The task timer is set to max time limit within which the
871 // requested dump will be collected.
872 task->startTimer(std::chrono::minutes(6));
873 task->populateResp(asyncResp->res);
874 task->payload.emplace(payload);
875 },
876 "xyz.openbmc_project.Dump.Manager", createdObjPath,
877 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500878}
879
zhanghch058d1b46d2021-04-01 11:18:24 +0800880inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
881 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500882{
Claire Weinanfdd26902022-03-01 14:18:25 -0800883 std::string dumpPath = getDumpEntriesPath(dumpType);
884 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500885 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500886 messages::internalError(asyncResp->res);
887 return;
888 }
889
890 std::optional<std::string> diagnosticDataType;
891 std::optional<std::string> oemDiagnosticDataType;
892
Willy Tu15ed6782021-12-14 11:03:16 -0800893 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500894 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
895 "OEMDiagnosticDataType", oemDiagnosticDataType))
896 {
897 return;
898 }
899
900 if (dumpType == "System")
901 {
902 if (!oemDiagnosticDataType || !diagnosticDataType)
903 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800904 BMCWEB_LOG_ERROR
905 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500906 messages::actionParameterMissing(
907 asyncResp->res, "CollectDiagnosticData",
908 "DiagnosticDataType & OEMDiagnosticDataType");
909 return;
910 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700911 if ((*oemDiagnosticDataType != "System") ||
912 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500913 {
914 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700915 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500916 return;
917 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500918 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500919 }
920 else if (dumpType == "BMC")
921 {
922 if (!diagnosticDataType)
923 {
George Liu0fda0f12021-11-16 10:06:17 +0800924 BMCWEB_LOG_ERROR
925 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500926 messages::actionParameterMissing(
927 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
928 return;
929 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700930 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500931 {
932 BMCWEB_LOG_ERROR
933 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700934 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500935 return;
936 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500937 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
938 }
939 else
940 {
941 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
942 messages::internalError(asyncResp->res);
943 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500944 }
945
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600946 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
947 createDumpParamVec;
948
Carson Labradof574a8e2023-03-22 02:26:00 +0000949 if (req.session != nullptr)
950 {
951 createDumpParamVec.emplace_back(
952 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
953 req.session->clientIp);
954 createDumpParamVec.emplace_back(
955 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
956 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
957 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600958
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500959 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800960 [asyncResp, payload(task::Payload(req)),
961 dumpPath](const boost::system::error_code& ec,
962 const sdbusplus::message_t& msg,
963 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700964 if (ec)
965 {
966 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500967 const sd_bus_error* dbusError = msg.get_error();
968 if (dbusError == nullptr)
969 {
970 messages::internalError(asyncResp->res);
971 return;
972 }
973
974 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
975 << " and error msg: " << dbusError->message;
976 if (std::string_view(
977 "xyz.openbmc_project.Common.Error.NotAllowed") ==
978 dbusError->name)
979 {
980 messages::resourceInStandby(asyncResp->res);
981 return;
982 }
983 if (std::string_view(
984 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
985 dbusError->name)
986 {
987 messages::serviceDisabled(asyncResp->res, dumpPath);
988 return;
989 }
990 if (std::string_view(
991 "xyz.openbmc_project.Common.Error.Unavailable") ==
992 dbusError->name)
993 {
994 messages::resourceInUse(asyncResp->res);
995 return;
996 }
997 // Other Dbus errors such as:
998 // xyz.openbmc_project.Common.Error.InvalidArgument &
999 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1000 // the dbus call that is made here in the bmcweb
1001 // implementation and has nothing to do with the client's
1002 // input in the request. Hence, returning internal error
1003 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001004 messages::internalError(asyncResp->res);
1005 return;
1006 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001007 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
1008 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001009 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001010 "xyz.openbmc_project.Dump.Manager",
1011 "/xyz/openbmc_project/dump/" +
1012 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001013 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001014}
1015
zhanghch058d1b46d2021-04-01 11:18:24 +08001016inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1017 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001018{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001019 std::string dumpTypeLowerCopy =
1020 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001021
Claire Weinan0d946212022-07-13 19:40:19 -07001022 crow::connections::systemBus->async_method_call(
1023 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001024 if (ec)
1025 {
Claire Weinan0d946212022-07-13 19:40:19 -07001026 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -07001027 messages::internalError(asyncResp->res);
1028 return;
1029 }
Claire Weinan0d946212022-07-13 19:40:19 -07001030 },
1031 "xyz.openbmc_project.Dump.Manager",
1032 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1033 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001034}
1035
Ed Tanousb9d36b42022-02-26 21:42:46 -08001036inline static void
1037 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1038 std::string& filename, std::string& timestamp,
1039 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001040{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001041 const std::string* filenamePtr = nullptr;
1042 const std::string* timestampPtr = nullptr;
1043 const std::string* logfilePtr = nullptr;
1044
1045 const bool success = sdbusplus::unpackPropertiesNoThrow(
1046 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1047 "Filename", filenamePtr, "Log", logfilePtr);
1048
1049 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001050 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001051 return;
1052 }
1053
1054 if (filenamePtr != nullptr)
1055 {
1056 filename = *filenamePtr;
1057 }
1058
1059 if (timestampPtr != nullptr)
1060 {
1061 timestamp = *timestampPtr;
1062 }
1063
1064 if (logfilePtr != nullptr)
1065 {
1066 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001067 }
1068}
1069
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001070inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001071{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001072 /**
1073 * Functions triggers appropriate requests on DBus
1074 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001075 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001076 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001077 .methods(boost::beast::http::verb::get)(
1078 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001079 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1080 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001081 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001082 {
1083 return;
1084 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001085 if (systemName != "system")
1086 {
1087 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1088 systemName);
1089 return;
1090 }
1091
Ed Tanous002d39b2022-05-31 08:59:27 -07001092 // Collections don't include the static data added by SubRoute
1093 // because it has a duplicate entry for members
1094 asyncResp->res.jsonValue["@odata.type"] =
1095 "#LogServiceCollection.LogServiceCollection";
1096 asyncResp->res.jsonValue["@odata.id"] =
1097 "/redfish/v1/Systems/system/LogServices";
1098 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1099 asyncResp->res.jsonValue["Description"] =
1100 "Collection of LogServices for this Computer System";
1101 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1102 logServiceArray = nlohmann::json::array();
1103 nlohmann::json::object_t eventLog;
1104 eventLog["@odata.id"] =
1105 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001106 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001107#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001108 nlohmann::json::object_t dumpLog;
1109 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001110 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001111#endif
1112
Jason M. Billsd53dd412019-02-12 17:16:22 -08001113#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001114 nlohmann::json::object_t crashdump;
1115 crashdump["@odata.id"] =
1116 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001117 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001118#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001119
1120#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 nlohmann::json::object_t hostlogger;
1122 hostlogger["@odata.id"] =
1123 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001124 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001125#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001126 asyncResp->res.jsonValue["Members@odata.count"] =
1127 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001128
George Liu7a1dbc42022-12-07 16:03:22 +08001129 constexpr std::array<std::string_view, 1> interfaces = {
1130 "xyz.openbmc_project.State.Boot.PostCode"};
1131 dbus::utility::getSubTreePaths(
1132 "/", 0, interfaces,
1133 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001134 const dbus::utility::MapperGetSubTreePathsResponse&
1135 subtreePath) {
1136 if (ec)
1137 {
1138 BMCWEB_LOG_ERROR << ec;
1139 return;
1140 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001141
Ed Tanous002d39b2022-05-31 08:59:27 -07001142 for (const auto& pathStr : subtreePath)
1143 {
1144 if (pathStr.find("PostCode") != std::string::npos)
1145 {
1146 nlohmann::json& logServiceArrayLocal =
1147 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001148 nlohmann::json::object_t member;
1149 member["@odata.id"] =
1150 "/redfish/v1/Systems/system/LogServices/PostCodes";
1151
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001152 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001153
Ed Tanous002d39b2022-05-31 08:59:27 -07001154 asyncResp->res.jsonValue["Members@odata.count"] =
1155 logServiceArrayLocal.size();
1156 return;
1157 }
1158 }
George Liu7a1dbc42022-12-07 16:03:22 +08001159 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001160 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001161}
1162
1163inline void requestRoutesEventLogService(App& app)
1164{
Ed Tanous22d268c2022-05-19 09:39:07 -07001165 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001166 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001167 .methods(boost::beast::http::verb::get)(
1168 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001169 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1170 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001171 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 {
1173 return;
1174 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001175 if (systemName != "system")
1176 {
1177 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1178 systemName);
1179 return;
1180 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001181 asyncResp->res.jsonValue["@odata.id"] =
1182 "/redfish/v1/Systems/system/LogServices/EventLog";
1183 asyncResp->res.jsonValue["@odata.type"] =
1184 "#LogService.v1_1_0.LogService";
1185 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1186 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1187 asyncResp->res.jsonValue["Id"] = "EventLog";
1188 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301189
Ed Tanous002d39b2022-05-31 08:59:27 -07001190 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001191 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301192
Ed Tanous002d39b2022-05-31 08:59:27 -07001193 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1194 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1195 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301196
Ed Tanous002d39b2022-05-31 08:59:27 -07001197 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1198 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1199 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001200
Ed Tanous002d39b2022-05-31 08:59:27 -07001201 {"target",
1202 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001203 });
1204}
1205
1206inline void requestRoutesJournalEventLogClear(App& app)
1207{
Jason M. Bills4978b632022-02-22 14:17:43 -08001208 BMCWEB_ROUTE(
1209 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001210 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001211 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001212 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001213 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001214 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1215 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001216 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001217 {
1218 return;
1219 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001220 if (systemName != "system")
1221 {
1222 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1223 systemName);
1224 return;
1225 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 // Clear the EventLog by deleting the log files
1227 std::vector<std::filesystem::path> redfishLogFiles;
1228 if (getRedfishLogFiles(redfishLogFiles))
1229 {
1230 for (const std::filesystem::path& file : redfishLogFiles)
1231 {
1232 std::error_code ec;
1233 std::filesystem::remove(file, ec);
1234 }
1235 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001236
Ed Tanous002d39b2022-05-31 08:59:27 -07001237 // Reload rsyslog so it knows to start new log files
1238 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001239 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001240 if (ec)
1241 {
1242 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1243 messages::internalError(asyncResp->res);
1244 return;
1245 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001246
Ed Tanous002d39b2022-05-31 08:59:27 -07001247 messages::success(asyncResp->res);
1248 },
1249 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1250 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1251 "replace");
1252 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001253}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001254
Jason M. Billsac992cd2022-06-24 13:31:46 -07001255enum class LogParseError
1256{
1257 success,
1258 parseFailed,
1259 messageIdNotInRegistry,
1260};
1261
1262static LogParseError
1263 fillEventLogEntryJson(const std::string& logEntryID,
1264 const std::string& logEntry,
1265 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001266{
Jason M. Bills95820182019-04-22 16:25:34 -07001267 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001268 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001269 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001270 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001271 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001272 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001273 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001274 std::string timestamp = logEntry.substr(0, space);
1275 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001276 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001277 if (entryStart == std::string::npos)
1278 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001279 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001280 }
1281 std::string_view entry(logEntry);
1282 entry.remove_prefix(entryStart);
1283 // Use split to separate the entry into its fields
1284 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001285 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001286 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001287 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001288 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001289 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001290 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001291 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001292
Jason M. Bills4851d452019-03-28 11:27:48 -07001293 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001294 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001295
Sui Chen54417b02022-03-24 14:59:52 -07001296 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001297 {
Sui Chen54417b02022-03-24 14:59:52 -07001298 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001299 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001300 }
1301
Sui Chen54417b02022-03-24 14:59:52 -07001302 std::string msg = message->message;
1303
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001304 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001305 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001306 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001307 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001308 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001309 // If the first string is empty, assume there are no MessageArgs
1310 std::size_t messageArgsSize = 0;
1311 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001312 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001313 messageArgsSize = logEntryFields.size() - 1;
1314 }
1315
Ed Tanous23a21a12020-07-25 04:45:05 +00001316 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001317
1318 // Fill the MessageArgs into the Message
1319 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001320 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001321 {
1322 std::string argStr = "%" + std::to_string(++i);
1323 size_t argPos = msg.find(argStr);
1324 if (argPos != std::string::npos)
1325 {
1326 msg.replace(argPos, argStr.length(), messageArg);
1327 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001328 }
1329 }
1330
Jason M. Bills95820182019-04-22 16:25:34 -07001331 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1332 // format which matches the Redfish format except for the fractional seconds
1333 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001334 std::size_t dot = timestamp.find_first_of('.');
1335 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001336 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001337 {
Jason M. Bills95820182019-04-22 16:25:34 -07001338 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001339 }
1340
1341 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001342 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001343 logEntryJson["@odata.id"] = boost::urls::format(
1344 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1345 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001346 logEntryJson["Name"] = "System Event Log Entry";
1347 logEntryJson["Id"] = logEntryID;
1348 logEntryJson["Message"] = std::move(msg);
1349 logEntryJson["MessageId"] = std::move(messageID);
1350 logEntryJson["MessageArgs"] = messageArgs;
1351 logEntryJson["EntryType"] = "Event";
1352 logEntryJson["Severity"] = message->messageSeverity;
1353 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001354 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001355}
1356
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001357inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001358{
Ed Tanous22d268c2022-05-19 09:39:07 -07001359 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001360 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001361 .methods(boost::beast::http::verb::get)(
1362 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001363 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1364 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001365 query_param::QueryCapabilities capabilities = {
1366 .canDelegateTop = true,
1367 .canDelegateSkip = true,
1368 };
1369 query_param::Query delegatedQuery;
1370 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001371 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001372 {
1373 return;
1374 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001375 if (systemName != "system")
1376 {
1377 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1378 systemName);
1379 return;
1380 }
1381
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001382 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001383 size_t skip = delegatedQuery.skip.value_or(0);
1384
Ed Tanous002d39b2022-05-31 08:59:27 -07001385 // Collections don't include the static data added by SubRoute
1386 // because it has a duplicate entry for members
1387 asyncResp->res.jsonValue["@odata.type"] =
1388 "#LogEntryCollection.LogEntryCollection";
1389 asyncResp->res.jsonValue["@odata.id"] =
1390 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1391 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1392 asyncResp->res.jsonValue["Description"] =
1393 "Collection of System Event Log Entries";
1394
1395 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1396 logEntryArray = nlohmann::json::array();
1397 // Go through the log files and create a unique ID for each
1398 // entry
1399 std::vector<std::filesystem::path> redfishLogFiles;
1400 getRedfishLogFiles(redfishLogFiles);
1401 uint64_t entryCount = 0;
1402 std::string logEntry;
1403
1404 // Oldest logs are in the last file, so start there and loop
1405 // backwards
1406 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1407 it++)
1408 {
1409 std::ifstream logStream(*it);
1410 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001411 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001413 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001414
Ed Tanous002d39b2022-05-31 08:59:27 -07001415 // Reset the unique ID on the first entry
1416 bool firstEntry = true;
1417 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001418 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001419 std::string idStr;
1420 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001421 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001422 continue;
1423 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001424 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001425
Jason M. Billsde703c52022-06-23 14:19:04 -07001426 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001427 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1428 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001429 if (status == LogParseError::messageIdNotInRegistry)
1430 {
1431 continue;
1432 }
1433 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001434 {
1435 messages::internalError(asyncResp->res);
1436 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001437 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001438
Jason M. Billsde703c52022-06-23 14:19:04 -07001439 entryCount++;
1440 // Handle paging using skip (number of entries to skip from the
1441 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001442 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001443 {
1444 continue;
1445 }
1446
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001447 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001448 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001449 }
1450 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001451 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001452 {
1453 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1454 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001455 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001456 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001457 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001458}
Chicago Duan336e96c2019-07-15 14:22:08 +08001459
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001460inline void requestRoutesJournalEventLogEntry(App& app)
1461{
1462 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001463 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001464 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001465 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001466 [&app](const crow::Request& req,
1467 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001468 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001469 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001470 {
1471 return;
1472 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001473
1474 if (systemName != "system")
1475 {
1476 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1477 systemName);
1478 return;
1479 }
1480
Ed Tanous002d39b2022-05-31 08:59:27 -07001481 const std::string& targetID = param;
1482
1483 // Go through the log files and check the unique ID for each
1484 // entry to find the target entry
1485 std::vector<std::filesystem::path> redfishLogFiles;
1486 getRedfishLogFiles(redfishLogFiles);
1487 std::string logEntry;
1488
1489 // Oldest logs are in the last file, so start there and loop
1490 // backwards
1491 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1492 it++)
1493 {
1494 std::ifstream logStream(*it);
1495 if (!logStream.is_open())
1496 {
1497 continue;
1498 }
1499
1500 // Reset the unique ID on the first entry
1501 bool firstEntry = true;
1502 while (std::getline(logStream, logEntry))
1503 {
1504 std::string idStr;
1505 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001506 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001507 continue;
1508 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001509 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001510
1511 if (idStr == targetID)
1512 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001513 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001514 LogParseError status =
1515 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1516 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001517 {
1518 messages::internalError(asyncResp->res);
1519 return;
1520 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001521 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001522 return;
1523 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001524 }
1525 }
1526 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001527 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001528 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001529}
1530
1531inline void requestRoutesDBusEventLogEntryCollection(App& app)
1532{
Ed Tanous22d268c2022-05-19 09:39:07 -07001533 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001534 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001535 .methods(boost::beast::http::verb::get)(
1536 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001537 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1538 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001539 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001540 {
1541 return;
1542 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001543 if (systemName != "system")
1544 {
1545 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1546 systemName);
1547 return;
1548 }
1549
Ed Tanous002d39b2022-05-31 08:59:27 -07001550 // Collections don't include the static data added by SubRoute
1551 // because it has a duplicate entry for members
1552 asyncResp->res.jsonValue["@odata.type"] =
1553 "#LogEntryCollection.LogEntryCollection";
1554 asyncResp->res.jsonValue["@odata.id"] =
1555 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1556 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1557 asyncResp->res.jsonValue["Description"] =
1558 "Collection of System Event Log Entries";
1559
1560 // DBus implementation of EventLog/Entries
1561 // Make call to Logging Service to find all log entry objects
1562 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001563 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001564 const dbus::utility::ManagedObjectType& resp) {
1565 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001566 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001567 // TODO Handle for specific error code
1568 BMCWEB_LOG_ERROR
1569 << "getLogEntriesIfaceData resp_handler got error " << ec;
1570 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001571 return;
1572 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001573 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1574 entriesArray = nlohmann::json::array();
1575 for (const auto& objectPath : resp)
1576 {
1577 const uint32_t* id = nullptr;
1578 const uint64_t* timestamp = nullptr;
1579 const uint64_t* updateTimestamp = nullptr;
1580 const std::string* severity = nullptr;
1581 const std::string* message = nullptr;
1582 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001583 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001584 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001585 const std::string* notify = nullptr;
1586
Ed Tanous002d39b2022-05-31 08:59:27 -07001587 for (const auto& interfaceMap : objectPath.second)
1588 {
1589 if (interfaceMap.first ==
1590 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001591 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001592 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001593 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001594 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001595 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001596 id = std::get_if<uint32_t>(&propertyMap.second);
1597 }
1598 else if (propertyMap.first == "Timestamp")
1599 {
1600 timestamp =
1601 std::get_if<uint64_t>(&propertyMap.second);
1602 }
1603 else if (propertyMap.first == "UpdateTimestamp")
1604 {
1605 updateTimestamp =
1606 std::get_if<uint64_t>(&propertyMap.second);
1607 }
1608 else if (propertyMap.first == "Severity")
1609 {
1610 severity = std::get_if<std::string>(
1611 &propertyMap.second);
1612 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001613 else if (propertyMap.first == "Resolution")
1614 {
1615 resolution = std::get_if<std::string>(
1616 &propertyMap.second);
1617 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001618 else if (propertyMap.first == "Message")
1619 {
1620 message = std::get_if<std::string>(
1621 &propertyMap.second);
1622 }
1623 else if (propertyMap.first == "Resolved")
1624 {
1625 const bool* resolveptr =
1626 std::get_if<bool>(&propertyMap.second);
1627 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001628 {
1629 messages::internalError(asyncResp->res);
1630 return;
1631 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001632 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001633 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001634 else if (propertyMap.first ==
1635 "ServiceProviderNotify")
1636 {
1637 notify = std::get_if<std::string>(
1638 &propertyMap.second);
1639 if (notify == nullptr)
1640 {
1641 messages::internalError(asyncResp->res);
1642 return;
1643 }
1644 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001645 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001646 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001647 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001648 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001649 messages::internalError(asyncResp->res);
1650 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001651 }
1652 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001653 else if (interfaceMap.first ==
1654 "xyz.openbmc_project.Common.FilePath")
1655 {
1656 for (const auto& propertyMap : interfaceMap.second)
1657 {
1658 if (propertyMap.first == "Path")
1659 {
1660 filePath = std::get_if<std::string>(
1661 &propertyMap.second);
1662 }
1663 }
1664 }
1665 }
1666 // Object path without the
1667 // xyz.openbmc_project.Logging.Entry interface, ignore
1668 // and continue.
1669 if (id == nullptr || message == nullptr ||
1670 severity == nullptr || timestamp == nullptr ||
1671 updateTimestamp == nullptr)
1672 {
1673 continue;
1674 }
1675 entriesArray.push_back({});
1676 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001677 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001678 thisEntry["@odata.id"] = boost::urls::format(
1679 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1680 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001681 thisEntry["Name"] = "System Event Log Entry";
1682 thisEntry["Id"] = std::to_string(*id);
1683 thisEntry["Message"] = *message;
1684 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001685 if ((resolution != nullptr) && (!(*resolution).empty()))
1686 {
1687 thisEntry["Resolution"] = *resolution;
1688 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001689 std::optional<bool> notifyAction =
1690 getProviderNotifyAction(*notify);
1691 if (notifyAction)
1692 {
1693 thisEntry["ServiceProviderNotified"] = *notifyAction;
1694 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001695 thisEntry["EntryType"] = "Event";
1696 thisEntry["Severity"] =
1697 translateSeverityDbusToRedfish(*severity);
1698 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001699 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001700 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001701 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001702 if (filePath != nullptr)
1703 {
1704 thisEntry["AdditionalDataURI"] =
1705 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1706 std::to_string(*id) + "/attachment";
1707 }
1708 }
1709 std::sort(
1710 entriesArray.begin(), entriesArray.end(),
1711 [](const nlohmann::json& left, const nlohmann::json& right) {
1712 return (left["Id"] <= right["Id"]);
1713 });
1714 asyncResp->res.jsonValue["Members@odata.count"] =
1715 entriesArray.size();
1716 },
1717 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1718 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001719 });
1720}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001721
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001722inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001723{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001724 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001725 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001726 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001727 .methods(boost::beast::http::verb::get)(
1728 [&app](const crow::Request& req,
1729 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001730 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001731 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001732 {
1733 return;
1734 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001735 if (systemName != "system")
1736 {
1737 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1738 systemName);
1739 return;
1740 }
1741
Ed Tanous002d39b2022-05-31 08:59:27 -07001742 std::string entryID = param;
1743 dbus::utility::escapePathForDbus(entryID);
1744
1745 // DBus implementation of EventLog/Entries
1746 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001747 sdbusplus::asio::getAllProperties(
1748 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1749 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001750 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001751 const dbus::utility::DBusPropertiesMap& resp) {
1752 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001753 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001754 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1755 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001756 return;
1757 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001758 if (ec)
1759 {
1760 BMCWEB_LOG_ERROR
1761 << "EventLogEntry (DBus) resp_handler got error " << ec;
1762 messages::internalError(asyncResp->res);
1763 return;
1764 }
1765 const uint32_t* id = nullptr;
1766 const uint64_t* timestamp = nullptr;
1767 const uint64_t* updateTimestamp = nullptr;
1768 const std::string* severity = nullptr;
1769 const std::string* message = nullptr;
1770 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001771 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001772 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001773 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001774
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001775 const bool success = sdbusplus::unpackPropertiesNoThrow(
1776 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1777 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001778 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001779 "Resolution", resolution, "Path", filePath,
1780 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001781
1782 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001783 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001784 messages::internalError(asyncResp->res);
1785 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001786 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001787
Ed Tanous002d39b2022-05-31 08:59:27 -07001788 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001789 timestamp == nullptr || updateTimestamp == nullptr ||
1790 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001791 {
1792 messages::internalError(asyncResp->res);
1793 return;
1794 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001795
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001797 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001798 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1799 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1800 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001801 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1802 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1803 asyncResp->res.jsonValue["Message"] = *message;
1804 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001805 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1806 if (notifyAction)
1807 {
1808 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1809 *notifyAction;
1810 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001811 if ((resolution != nullptr) && (!(*resolution).empty()))
1812 {
1813 asyncResp->res.jsonValue["Resolution"] = *resolution;
1814 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001815 asyncResp->res.jsonValue["EntryType"] = "Event";
1816 asyncResp->res.jsonValue["Severity"] =
1817 translateSeverityDbusToRedfish(*severity);
1818 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001819 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001820 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001821 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001822 if (filePath != nullptr)
1823 {
1824 asyncResp->res.jsonValue["AdditionalDataURI"] =
1825 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1826 std::to_string(*id) + "/attachment";
1827 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001828 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001829 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001830
1831 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001832 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001833 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001834 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001835 [&app](const crow::Request& req,
1836 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001837 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001838 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001839 {
1840 return;
1841 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001842 if (systemName != "system")
1843 {
1844 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1845 systemName);
1846 return;
1847 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001848 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001849
Ed Tanous002d39b2022-05-31 08:59:27 -07001850 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1851 resolved))
1852 {
1853 return;
1854 }
1855 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001856
Ed Tanous002d39b2022-05-31 08:59:27 -07001857 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001858 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 if (ec)
1860 {
1861 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1862 messages::internalError(asyncResp->res);
1863 return;
1864 }
1865 },
1866 "xyz.openbmc_project.Logging",
1867 "/xyz/openbmc_project/logging/entry/" + entryId,
1868 "org.freedesktop.DBus.Properties", "Set",
1869 "xyz.openbmc_project.Logging.Entry", "Resolved",
1870 dbus::utility::DbusVariantType(*resolved));
1871 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001872
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001873 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001874 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001875 .privileges(redfish::privileges::deleteLogEntry)
1876
Ed Tanous002d39b2022-05-31 08:59:27 -07001877 .methods(boost::beast::http::verb::delete_)(
1878 [&app](const crow::Request& req,
1879 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001880 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001881 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001882 {
1883 return;
1884 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001885 if (systemName != "system")
1886 {
1887 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1888 systemName);
1889 return;
1890 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001891 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1892
1893 std::string entryID = param;
1894
1895 dbus::utility::escapePathForDbus(entryID);
1896
1897 // Process response from Logging service.
1898 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001899 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001900 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1901 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001902 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001904 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001905 messages::resourceNotFound(asyncResp->res, "LogEntry",
1906 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001907 return;
1908 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 // TODO Handle for specific error code
1910 BMCWEB_LOG_ERROR
1911 << "EventLogEntry (DBus) doDelete respHandler got error "
1912 << ec;
1913 asyncResp->res.result(
1914 boost::beast::http::status::internal_server_error);
1915 return;
1916 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001917
Ed Tanous002d39b2022-05-31 08:59:27 -07001918 asyncResp->res.result(boost::beast::http::status::ok);
1919 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001920
Ed Tanous002d39b2022-05-31 08:59:27 -07001921 // Make call to Logging service to request Delete Log
1922 crow::connections::systemBus->async_method_call(
1923 respHandler, "xyz.openbmc_project.Logging",
1924 "/xyz/openbmc_project/logging/entry/" + entryID,
1925 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001926 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001927}
1928
1929inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001930{
George Liu0fda0f12021-11-16 10:06:17 +08001931 BMCWEB_ROUTE(
1932 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001933 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001934 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001935 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001936 [&app](const crow::Request& req,
1937 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001938 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001939 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001940 {
1941 return;
1942 }
Matt Spinler72e21372023-04-19 12:53:33 -05001943 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07001944 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001945 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001946 {
1947 asyncResp->res.result(boost::beast::http::status::bad_request);
1948 return;
1949 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001950 if (systemName != "system")
1951 {
1952 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1953 systemName);
1954 return;
1955 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001956
Ed Tanous002d39b2022-05-31 08:59:27 -07001957 std::string entryID = param;
1958 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001959
Ed Tanous002d39b2022-05-31 08:59:27 -07001960 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001961 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 const sdbusplus::message::unix_fd& unixfd) {
1963 if (ec.value() == EBADR)
1964 {
1965 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1966 entryID);
1967 return;
1968 }
1969 if (ec)
1970 {
1971 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1972 messages::internalError(asyncResp->res);
1973 return;
1974 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001975
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 int fd = -1;
1977 fd = dup(unixfd);
1978 if (fd == -1)
1979 {
1980 messages::internalError(asyncResp->res);
1981 return;
1982 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001983
Ed Tanous002d39b2022-05-31 08:59:27 -07001984 long long int size = lseek(fd, 0, SEEK_END);
1985 if (size == -1)
1986 {
1987 messages::internalError(asyncResp->res);
1988 return;
1989 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001990
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 // Arbitrary max size of 64kb
1992 constexpr int maxFileSize = 65536;
1993 if (size > maxFileSize)
1994 {
1995 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1996 << maxFileSize;
1997 messages::internalError(asyncResp->res);
1998 return;
1999 }
2000 std::vector<char> data(static_cast<size_t>(size));
2001 long long int rc = lseek(fd, 0, SEEK_SET);
2002 if (rc == -1)
2003 {
2004 messages::internalError(asyncResp->res);
2005 return;
2006 }
2007 rc = read(fd, data.data(), data.size());
2008 if ((rc == -1) || (rc != size))
2009 {
2010 messages::internalError(asyncResp->res);
2011 return;
2012 }
2013 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002014
Ed Tanous002d39b2022-05-31 08:59:27 -07002015 std::string_view strData(data.data(), data.size());
2016 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002017
Ed Tanousd9f6c622022-03-17 09:12:17 -07002018 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07002019 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07002020 asyncResp->res.addHeader(
2021 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07002022 asyncResp->res.body() = std::move(output);
2023 },
2024 "xyz.openbmc_project.Logging",
2025 "/xyz/openbmc_project/logging/entry/" + entryID,
2026 "xyz.openbmc_project.Logging.Entry", "GetEntry");
2027 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002028}
2029
Spencer Kub7028eb2021-10-26 15:27:35 +08002030constexpr const char* hostLoggerFolderPath = "/var/log/console";
2031
2032inline bool
2033 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2034 std::vector<std::filesystem::path>& hostLoggerFiles)
2035{
2036 std::error_code ec;
2037 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2038 if (ec)
2039 {
2040 BMCWEB_LOG_ERROR << ec.message();
2041 return false;
2042 }
2043 for (const std::filesystem::directory_entry& it : logPath)
2044 {
2045 std::string filename = it.path().filename();
2046 // Prefix of each log files is "log". Find the file and save the
2047 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002048 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002049 {
2050 hostLoggerFiles.emplace_back(it.path());
2051 }
2052 }
2053 // As the log files rotate, they are appended with a ".#" that is higher for
2054 // the older logs. Since we start from oldest logs, sort the name in
2055 // descending order.
2056 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2057 AlphanumLess<std::string>());
2058
2059 return true;
2060}
2061
Ed Tanous02cad962022-06-30 16:50:15 -07002062inline bool getHostLoggerEntries(
2063 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2064 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002065{
2066 GzFileReader logFile;
2067
2068 // Go though all log files and expose host logs.
2069 for (const std::filesystem::path& it : hostLoggerFiles)
2070 {
2071 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2072 {
2073 BMCWEB_LOG_ERROR << "fail to expose host logs";
2074 return false;
2075 }
2076 }
2077 // Get lastMessage from constructor by getter
2078 std::string lastMessage = logFile.getLastMessage();
2079 if (!lastMessage.empty())
2080 {
2081 logCount++;
2082 if (logCount > skip && logCount <= (skip + top))
2083 {
2084 logEntries.push_back(lastMessage);
2085 }
2086 }
2087 return true;
2088}
2089
2090inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2091 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002092 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002093{
2094 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002095 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002096 logEntryJson["@odata.id"] = boost::urls::format(
2097 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2098 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002099 logEntryJson["Name"] = "Host Logger Entry";
2100 logEntryJson["Id"] = logEntryID;
2101 logEntryJson["Message"] = msg;
2102 logEntryJson["EntryType"] = "Oem";
2103 logEntryJson["Severity"] = "OK";
2104 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002105}
2106
2107inline void requestRoutesSystemHostLogger(App& app)
2108{
Ed Tanous22d268c2022-05-19 09:39:07 -07002109 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002110 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002111 .methods(boost::beast::http::verb::get)(
2112 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002113 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2114 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002115 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002116 {
2117 return;
2118 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002119 if (systemName != "system")
2120 {
2121 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2122 systemName);
2123 return;
2124 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002125 asyncResp->res.jsonValue["@odata.id"] =
2126 "/redfish/v1/Systems/system/LogServices/HostLogger";
2127 asyncResp->res.jsonValue["@odata.type"] =
2128 "#LogService.v1_1_0.LogService";
2129 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2130 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2131 asyncResp->res.jsonValue["Id"] = "HostLogger";
2132 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2133 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2134 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002135}
2136
2137inline void requestRoutesSystemHostLoggerCollection(App& app)
2138{
2139 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002140 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002141 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002142 .methods(boost::beast::http::verb::get)(
2143 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002144 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2145 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 query_param::QueryCapabilities capabilities = {
2147 .canDelegateTop = true,
2148 .canDelegateSkip = true,
2149 };
2150 query_param::Query delegatedQuery;
2151 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002152 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 {
2154 return;
2155 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002156 if (systemName != "system")
2157 {
2158 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2159 systemName);
2160 return;
2161 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002162 asyncResp->res.jsonValue["@odata.id"] =
2163 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2164 asyncResp->res.jsonValue["@odata.type"] =
2165 "#LogEntryCollection.LogEntryCollection";
2166 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2167 asyncResp->res.jsonValue["Description"] =
2168 "Collection of HostLogger Entries";
2169 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2170 logEntryArray = nlohmann::json::array();
2171 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002172
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 std::vector<std::filesystem::path> hostLoggerFiles;
2174 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2175 {
2176 BMCWEB_LOG_ERROR << "fail to get host log file path";
2177 return;
2178 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002179 // If we weren't provided top and skip limits, use the defaults.
2180 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002181 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002182 size_t logCount = 0;
2183 // This vector only store the entries we want to expose that
2184 // control by skip and top.
2185 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002186 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2187 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002188 {
2189 messages::internalError(asyncResp->res);
2190 return;
2191 }
2192 // If vector is empty, that means skip value larger than total
2193 // log count
2194 if (logEntries.empty())
2195 {
2196 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2197 return;
2198 }
2199 if (!logEntries.empty())
2200 {
2201 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002202 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002203 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002204 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2205 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002206 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002207 }
2208
Ed Tanous002d39b2022-05-31 08:59:27 -07002209 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002210 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002211 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002212 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2213 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002214 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002215 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002216 }
George Liu0fda0f12021-11-16 10:06:17 +08002217 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002218}
2219
2220inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2221{
2222 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002223 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002224 .privileges(redfish::privileges::getLogEntry)
2225 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002226 [&app](const crow::Request& req,
2227 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002228 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002229 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002230 {
2231 return;
2232 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002233 if (systemName != "system")
2234 {
2235 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2236 systemName);
2237 return;
2238 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002240
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002242
Patrick Williams84396af2023-05-11 11:47:45 -05002243 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2244 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002245 if (ec == std::errc::invalid_argument ||
2246 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002247 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002248 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 return;
2250 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002251
Ed Tanous002d39b2022-05-31 08:59:27 -07002252 std::vector<std::filesystem::path> hostLoggerFiles;
2253 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2254 {
2255 BMCWEB_LOG_ERROR << "fail to get host log file path";
2256 return;
2257 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002258
Ed Tanous002d39b2022-05-31 08:59:27 -07002259 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002260 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002261 std::vector<std::string> logEntries;
2262 // We can get specific entry by skip and top. For example, if we
2263 // want to get nth entry, we can set skip = n-1 and top = 1 to
2264 // get that entry
2265 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2266 logCount))
2267 {
2268 messages::internalError(asyncResp->res);
2269 return;
2270 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002271
Ed Tanous002d39b2022-05-31 08:59:27 -07002272 if (!logEntries.empty())
2273 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002274 nlohmann::json::object_t hostLogEntry;
2275 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2276 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002277 return;
2278 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002279
Ed Tanous002d39b2022-05-31 08:59:27 -07002280 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002281 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002282 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002283}
2284
Claire Weinandd72e872022-08-15 14:20:06 -07002285inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002286 crow::App& app, const crow::Request& req,
2287 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2288{
2289 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2290 {
2291 return;
2292 }
2293 // Collections don't include the static data added by SubRoute
2294 // because it has a duplicate entry for members
2295 asyncResp->res.jsonValue["@odata.type"] =
2296 "#LogServiceCollection.LogServiceCollection";
2297 asyncResp->res.jsonValue["@odata.id"] =
2298 "/redfish/v1/Managers/bmc/LogServices";
2299 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2300 asyncResp->res.jsonValue["Description"] =
2301 "Collection of LogServices for this Manager";
2302 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2303 logServiceArray = nlohmann::json::array();
2304
2305#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002306 nlohmann::json::object_t journal;
2307 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002308 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002309#endif
2310
2311 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2312
2313#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002314 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002315 "xyz.openbmc_project.Collection.DeleteAll"};
2316 dbus::utility::getSubTreePaths(
2317 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002318 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002319 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002320 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2321 if (ec)
2322 {
2323 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002324 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002325 << ec;
2326 // Assume that getting an error simply means there are no dump
2327 // LogServices. Return without adding any error response.
2328 return;
2329 }
2330
2331 nlohmann::json& logServiceArrayLocal =
2332 asyncResp->res.jsonValue["Members"];
2333
2334 for (const std::string& path : subTreePaths)
2335 {
2336 if (path == "/xyz/openbmc_project/dump/bmc")
2337 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002338 nlohmann::json::object_t member;
2339 member["@odata.id"] =
2340 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002341 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002342 }
2343 else if (path == "/xyz/openbmc_project/dump/faultlog")
2344 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002345 nlohmann::json::object_t member;
2346 member["@odata.id"] =
2347 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002348 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002349 }
2350 }
2351
2352 asyncResp->res.jsonValue["Members@odata.count"] =
2353 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002354 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002355#endif
2356}
2357
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002358inline void requestRoutesBMCLogServiceCollection(App& app)
2359{
2360 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002361 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002362 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002363 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002364}
Ed Tanous1da66f72018-07-27 16:13:37 -07002365
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002366inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002367{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002368 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002369 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002370 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002371 [&app](const crow::Request& req,
2372 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002373 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002374 {
2375 return;
2376 }
2377 asyncResp->res.jsonValue["@odata.type"] =
2378 "#LogService.v1_1_0.LogService";
2379 asyncResp->res.jsonValue["@odata.id"] =
2380 "/redfish/v1/Managers/bmc/LogServices/Journal";
2381 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2382 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002383 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002384 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302385
Ed Tanous002d39b2022-05-31 08:59:27 -07002386 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002387 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002388 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2389 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2390 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302391
Ed Tanous002d39b2022-05-31 08:59:27 -07002392 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2393 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2394 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002395}
Jason M. Billse1f26342018-07-18 12:12:00 -07002396
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002397static int
2398 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2399 sd_journal* journal,
2400 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002401{
2402 // Get the Log Entry contents
2403 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002404
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002405 std::string message;
2406 std::string_view syslogID;
2407 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2408 if (ret < 0)
2409 {
2410 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2411 << strerror(-ret);
2412 }
2413 if (!syslogID.empty())
2414 {
2415 message += std::string(syslogID) + ": ";
2416 }
2417
Ed Tanous39e77502019-03-04 17:35:53 -08002418 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002419 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002420 if (ret < 0)
2421 {
2422 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2423 return 1;
2424 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002425 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002426
2427 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002428 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002429 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002430 if (ret < 0)
2431 {
2432 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002433 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002434
2435 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002436 std::string entryTimeStr;
2437 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002438 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002439 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002440 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002441
2442 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002443 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002444 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2445 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002446 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002447 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2448 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2449 bmcJournalLogEntryJson["Message"] = std::move(message);
2450 bmcJournalLogEntryJson["EntryType"] = "Oem";
2451 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2452 : severity <= 4 ? "Warning"
2453 : "OK";
2454 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2455 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002456 return 0;
2457}
2458
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002459inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002460{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002461 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002462 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002463 .methods(boost::beast::http::verb::get)(
2464 [&app](const crow::Request& req,
2465 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2466 query_param::QueryCapabilities capabilities = {
2467 .canDelegateTop = true,
2468 .canDelegateSkip = true,
2469 };
2470 query_param::Query delegatedQuery;
2471 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002472 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002473 {
2474 return;
2475 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002476
2477 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002478 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002479
Ed Tanous002d39b2022-05-31 08:59:27 -07002480 // Collections don't include the static data added by SubRoute
2481 // because it has a duplicate entry for members
2482 asyncResp->res.jsonValue["@odata.type"] =
2483 "#LogEntryCollection.LogEntryCollection";
2484 asyncResp->res.jsonValue["@odata.id"] =
2485 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2486 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2487 asyncResp->res.jsonValue["Description"] =
2488 "Collection of BMC Journal Entries";
2489 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2490 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002491
Ed Tanous002d39b2022-05-31 08:59:27 -07002492 // Go through the journal and use the timestamp to create a
2493 // unique ID for each entry
2494 sd_journal* journalTmp = nullptr;
2495 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2496 if (ret < 0)
2497 {
2498 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2499 messages::internalError(asyncResp->res);
2500 return;
2501 }
2502 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2503 journalTmp, sd_journal_close);
2504 journalTmp = nullptr;
2505 uint64_t entryCount = 0;
2506 // Reset the unique ID on the first entry
2507 bool firstEntry = true;
2508 SD_JOURNAL_FOREACH(journal.get())
2509 {
2510 entryCount++;
2511 // Handle paging using skip (number of entries to skip from
2512 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002513 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002514 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002515 continue;
2516 }
2517
2518 std::string idStr;
2519 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2520 {
2521 continue;
2522 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002523 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002524
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002525 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002526 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2527 bmcJournalLogEntry) != 0)
2528 {
George Liu0fda0f12021-11-16 10:06:17 +08002529 messages::internalError(asyncResp->res);
2530 return;
2531 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002532 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002533 }
2534 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002535 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002536 {
2537 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2538 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002539 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002540 }
George Liu0fda0f12021-11-16 10:06:17 +08002541 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002542}
Jason M. Billse1f26342018-07-18 12:12:00 -07002543
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002544inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002545{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002546 BMCWEB_ROUTE(app,
2547 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002548 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002549 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002550 [&app](const crow::Request& req,
2551 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2552 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002553 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002554 {
2555 return;
2556 }
2557 // Convert the unique ID back to a timestamp to find the entry
2558 uint64_t ts = 0;
2559 uint64_t index = 0;
2560 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2561 {
2562 return;
2563 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002564
Ed Tanous002d39b2022-05-31 08:59:27 -07002565 sd_journal* journalTmp = nullptr;
2566 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2567 if (ret < 0)
2568 {
2569 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2570 messages::internalError(asyncResp->res);
2571 return;
2572 }
2573 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2574 journalTmp, sd_journal_close);
2575 journalTmp = nullptr;
2576 // Go to the timestamp in the log and move to the entry at the
2577 // index tracking the unique ID
2578 std::string idStr;
2579 bool firstEntry = true;
2580 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2581 if (ret < 0)
2582 {
2583 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2584 << strerror(-ret);
2585 messages::internalError(asyncResp->res);
2586 return;
2587 }
2588 for (uint64_t i = 0; i <= index; i++)
2589 {
2590 sd_journal_next(journal.get());
2591 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2592 {
2593 messages::internalError(asyncResp->res);
2594 return;
2595 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002596 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002597 }
2598 // Confirm that the entry ID matches what was requested
2599 if (idStr != entryID)
2600 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002601 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002602 return;
2603 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002604
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002605 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002606 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002607 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002608 {
2609 messages::internalError(asyncResp->res);
2610 return;
2611 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002612 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002613 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002614}
2615
Claire Weinanfdd26902022-03-01 14:18:25 -08002616inline void
2617 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2618 const std::string& dumpType)
2619{
2620 std::string dumpPath;
2621 std::string overWritePolicy;
2622 bool collectDiagnosticDataSupported = false;
2623
2624 if (dumpType == "BMC")
2625 {
2626 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2627 overWritePolicy = "WrapsWhenFull";
2628 collectDiagnosticDataSupported = true;
2629 }
2630 else if (dumpType == "FaultLog")
2631 {
2632 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2633 overWritePolicy = "Unknown";
2634 collectDiagnosticDataSupported = false;
2635 }
2636 else if (dumpType == "System")
2637 {
2638 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2639 overWritePolicy = "WrapsWhenFull";
2640 collectDiagnosticDataSupported = true;
2641 }
2642 else
2643 {
2644 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2645 << dumpType;
2646 messages::internalError(asyncResp->res);
2647 return;
2648 }
2649
2650 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2651 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2652 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2653 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2654 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2655 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2656
2657 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002658 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002659 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2660 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2661 redfishDateTimeOffset.second;
2662
2663 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002664
2665 if (collectDiagnosticDataSupported)
2666 {
2667 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2668 ["target"] =
2669 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2670 }
Claire Weinan0d946212022-07-13 19:40:19 -07002671
2672 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2673 dbus::utility::getSubTreePaths(
2674 "/xyz/openbmc_project/dump", 0, interfaces,
2675 [asyncResp, dumpType, dumpPath](
2676 const boost::system::error_code& ec,
2677 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2678 if (ec)
2679 {
2680 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2681 << ec;
2682 // Assume that getting an error simply means there are no dump
2683 // LogServices. Return without adding any error response.
2684 return;
2685 }
2686
2687 const std::string dbusDumpPath =
2688 "/xyz/openbmc_project/dump/" +
2689 boost::algorithm::to_lower_copy(dumpType);
2690
2691 for (const std::string& path : subTreePaths)
2692 {
2693 if (path == dbusDumpPath)
2694 {
2695 asyncResp->res
2696 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2697 dumpPath + "/Actions/LogService.ClearLog";
2698 break;
2699 }
2700 }
2701 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002702}
2703
2704inline void handleLogServicesDumpServiceGet(
2705 crow::App& app, const std::string& dumpType, const crow::Request& req,
2706 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2707{
2708 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2709 {
2710 return;
2711 }
2712 getDumpServiceInfo(asyncResp, dumpType);
2713}
2714
Ed Tanous22d268c2022-05-19 09:39:07 -07002715inline void handleLogServicesDumpServiceComputerSystemGet(
2716 crow::App& app, const crow::Request& req,
2717 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2718 const std::string& chassisId)
2719{
2720 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2721 {
2722 return;
2723 }
2724 if (chassisId != "system")
2725 {
2726 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2727 return;
2728 }
2729 getDumpServiceInfo(asyncResp, "System");
2730}
2731
Claire Weinanfdd26902022-03-01 14:18:25 -08002732inline void handleLogServicesDumpEntriesCollectionGet(
2733 crow::App& app, const std::string& dumpType, const crow::Request& req,
2734 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2735{
2736 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2737 {
2738 return;
2739 }
2740 getDumpEntryCollection(asyncResp, dumpType);
2741}
2742
Ed Tanous22d268c2022-05-19 09:39:07 -07002743inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2744 crow::App& app, const crow::Request& req,
2745 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2746 const std::string& chassisId)
2747{
2748 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2749 {
2750 return;
2751 }
2752 if (chassisId != "system")
2753 {
2754 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2755 return;
2756 }
2757 getDumpEntryCollection(asyncResp, "System");
2758}
2759
Claire Weinanfdd26902022-03-01 14:18:25 -08002760inline void handleLogServicesDumpEntryGet(
2761 crow::App& app, const std::string& dumpType, const crow::Request& req,
2762 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2763 const std::string& dumpId)
2764{
2765 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2766 {
2767 return;
2768 }
2769 getDumpEntryById(asyncResp, dumpId, dumpType);
2770}
Ed Tanous22d268c2022-05-19 09:39:07 -07002771inline void handleLogServicesDumpEntryComputerSystemGet(
2772 crow::App& app, const crow::Request& req,
2773 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2774 const std::string& chassisId, const std::string& dumpId)
2775{
2776 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2777 {
2778 return;
2779 }
2780 if (chassisId != "system")
2781 {
2782 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2783 return;
2784 }
2785 getDumpEntryById(asyncResp, dumpId, "System");
2786}
Claire Weinanfdd26902022-03-01 14:18:25 -08002787
2788inline void handleLogServicesDumpEntryDelete(
2789 crow::App& app, const std::string& dumpType, const crow::Request& req,
2790 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2791 const std::string& dumpId)
2792{
2793 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2794 {
2795 return;
2796 }
2797 deleteDumpEntry(asyncResp, dumpId, dumpType);
2798}
2799
Ed Tanous22d268c2022-05-19 09:39:07 -07002800inline void handleLogServicesDumpEntryComputerSystemDelete(
2801 crow::App& app, const crow::Request& req,
2802 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2803 const std::string& chassisId, const std::string& dumpId)
2804{
2805 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2806 {
2807 return;
2808 }
2809 if (chassisId != "system")
2810 {
2811 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2812 return;
2813 }
2814 deleteDumpEntry(asyncResp, dumpId, "System");
2815}
2816
Claire Weinanfdd26902022-03-01 14:18:25 -08002817inline void handleLogServicesDumpCollectDiagnosticDataPost(
2818 crow::App& app, const std::string& dumpType, const crow::Request& req,
2819 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2820{
2821 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2822 {
2823 return;
2824 }
2825 createDump(asyncResp, req, dumpType);
2826}
2827
Ed Tanous22d268c2022-05-19 09:39:07 -07002828inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2829 crow::App& app, const crow::Request& req,
2830 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2831 const std::string& chassisId)
2832{
2833 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2834 {
2835 return;
2836 }
2837 if (chassisId != "system")
2838 {
2839 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2840 return;
2841 }
2842 createDump(asyncResp, req, "System");
2843}
2844
Claire Weinanfdd26902022-03-01 14:18:25 -08002845inline void handleLogServicesDumpClearLogPost(
2846 crow::App& app, const std::string& dumpType, const crow::Request& req,
2847 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2848{
2849 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2850 {
2851 return;
2852 }
2853 clearDump(asyncResp, dumpType);
2854}
2855
Ed Tanous22d268c2022-05-19 09:39:07 -07002856inline void handleLogServicesDumpClearLogComputerSystemPost(
2857 crow::App& app, const crow::Request& req,
2858 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2859 const std::string& chassisId)
2860{
2861 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2862 {
2863 return;
2864 }
2865 if (chassisId != "system")
2866 {
2867 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2868 return;
2869 }
2870 clearDump(asyncResp, "System");
2871}
2872
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002873inline void requestRoutesBMCDumpService(App& app)
2874{
2875 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002876 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002877 .methods(boost::beast::http::verb::get)(std::bind_front(
2878 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002879}
2880
2881inline void requestRoutesBMCDumpEntryCollection(App& app)
2882{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002883 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002884 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002885 .methods(boost::beast::http::verb::get)(std::bind_front(
2886 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002887}
2888
2889inline void requestRoutesBMCDumpEntry(App& app)
2890{
2891 BMCWEB_ROUTE(app,
2892 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002893 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002894 .methods(boost::beast::http::verb::get)(std::bind_front(
2895 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2896
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002897 BMCWEB_ROUTE(app,
2898 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002899 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002900 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2901 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002902}
2903
2904inline void requestRoutesBMCDumpCreate(App& app)
2905{
George Liu0fda0f12021-11-16 10:06:17 +08002906 BMCWEB_ROUTE(
2907 app,
2908 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002909 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002910 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002911 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2912 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002913}
2914
2915inline void requestRoutesBMCDumpClear(App& app)
2916{
George Liu0fda0f12021-11-16 10:06:17 +08002917 BMCWEB_ROUTE(
2918 app,
2919 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002920 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002921 .methods(boost::beast::http::verb::post)(std::bind_front(
2922 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2923}
2924
2925inline void requestRoutesFaultLogDumpService(App& app)
2926{
2927 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2928 .privileges(redfish::privileges::getLogService)
2929 .methods(boost::beast::http::verb::get)(std::bind_front(
2930 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2931}
2932
2933inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2934{
2935 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2936 .privileges(redfish::privileges::getLogEntryCollection)
2937 .methods(boost::beast::http::verb::get)(
2938 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2939 std::ref(app), "FaultLog"));
2940}
2941
2942inline void requestRoutesFaultLogDumpEntry(App& app)
2943{
2944 BMCWEB_ROUTE(app,
2945 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2946 .privileges(redfish::privileges::getLogEntry)
2947 .methods(boost::beast::http::verb::get)(std::bind_front(
2948 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2949
2950 BMCWEB_ROUTE(app,
2951 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2952 .privileges(redfish::privileges::deleteLogEntry)
2953 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2954 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2955}
2956
2957inline void requestRoutesFaultLogDumpClear(App& app)
2958{
2959 BMCWEB_ROUTE(
2960 app,
2961 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2962 .privileges(redfish::privileges::postLogService)
2963 .methods(boost::beast::http::verb::post)(std::bind_front(
2964 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002965}
2966
2967inline void requestRoutesSystemDumpService(App& app)
2968{
Ed Tanous22d268c2022-05-19 09:39:07 -07002969 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002970 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002971 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002972 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002973}
2974
2975inline void requestRoutesSystemDumpEntryCollection(App& app)
2976{
Ed Tanous22d268c2022-05-19 09:39:07 -07002977 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002978 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002979 .methods(boost::beast::http::verb::get)(std::bind_front(
2980 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2981 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002982}
2983
2984inline void requestRoutesSystemDumpEntry(App& app)
2985{
2986 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002987 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002988 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002989 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002990 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002991
2992 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002993 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002994 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002995 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002996 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002997}
2998
2999inline void requestRoutesSystemDumpCreate(App& app)
3000{
George Liu0fda0f12021-11-16 10:06:17 +08003001 BMCWEB_ROUTE(
3002 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003003 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003004 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003005 .methods(boost::beast::http::verb::post)(std::bind_front(
3006 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3007 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003008}
3009
3010inline void requestRoutesSystemDumpClear(App& app)
3011{
George Liu0fda0f12021-11-16 10:06:17 +08003012 BMCWEB_ROUTE(
3013 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003014 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003015 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003016 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003017 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003018}
3019
3020inline void requestRoutesCrashdumpService(App& app)
3021{
3022 // Note: Deviated from redfish privilege registry for GET & HEAD
3023 // method for security reasons.
3024 /**
3025 * Functions triggers appropriate requests on DBus
3026 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003027 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003028 // This is incorrect, should be:
3029 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003030 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003031 .methods(boost::beast::http::verb::get)(
3032 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003033 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3034 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003035 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003036 {
3037 return;
3038 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003039 if (systemName != "system")
3040 {
3041 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3042 systemName);
3043 return;
3044 }
3045
Ed Tanous002d39b2022-05-31 08:59:27 -07003046 // Copy over the static data to include the entries added by
3047 // SubRoute
3048 asyncResp->res.jsonValue["@odata.id"] =
3049 "/redfish/v1/Systems/system/LogServices/Crashdump";
3050 asyncResp->res.jsonValue["@odata.type"] =
3051 "#LogService.v1_2_0.LogService";
3052 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3053 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303054 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003055 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3056 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303057
Ed Tanous002d39b2022-05-31 08:59:27 -07003058 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003059 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003060 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3061 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3062 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303063
Ed Tanous002d39b2022-05-31 08:59:27 -07003064 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003065 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003066 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3067 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3068 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3069 ["target"] =
3070 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003071 });
3072}
3073
3074void inline requestRoutesCrashdumpClear(App& app)
3075{
George Liu0fda0f12021-11-16 10:06:17 +08003076 BMCWEB_ROUTE(
3077 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003078 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003079 // This is incorrect, should be:
3080 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003081 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003082 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003083 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003084 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3085 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003086 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003087 {
3088 return;
3089 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003090 if (systemName != "system")
3091 {
3092 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3093 systemName);
3094 return;
3095 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003096 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003097 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003098 const std::string&) {
3099 if (ec)
3100 {
3101 messages::internalError(asyncResp->res);
3102 return;
3103 }
3104 messages::success(asyncResp->res);
3105 },
3106 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3107 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003108}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003109
zhanghch058d1b46d2021-04-01 11:18:24 +08003110static void
3111 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3112 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003113{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003114 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003115 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003116 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003117 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003118 if (ec)
3119 {
3120 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3121 if (ec.value() ==
3122 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003123 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003124 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003125 }
3126 else
3127 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003128 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003129 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003130 return;
3131 }
3132
3133 std::string timestamp{};
3134 std::string filename{};
3135 std::string logfile{};
3136 parseCrashdumpParameters(params, filename, timestamp, logfile);
3137
3138 if (filename.empty() || timestamp.empty())
3139 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003140 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003141 return;
3142 }
3143
3144 std::string crashdumpURI =
3145 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3146 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003147 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003148 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003149 logEntry["@odata.id"] = boost::urls::format(
3150 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3151 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003152 logEntry["Name"] = "CPU Crashdump";
3153 logEntry["Id"] = logID;
3154 logEntry["EntryType"] = "Oem";
3155 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3156 logEntry["DiagnosticDataType"] = "OEM";
3157 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3158 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003159
3160 // If logEntryJson references an array of LogEntry resources
3161 // ('Members' list), then push this as a new entry, otherwise set it
3162 // directly
3163 if (logEntryJson.is_array())
3164 {
3165 logEntryJson.push_back(logEntry);
3166 asyncResp->res.jsonValue["Members@odata.count"] =
3167 logEntryJson.size();
3168 }
3169 else
3170 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003171 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003172 }
3173 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003174 sdbusplus::asio::getAllProperties(
3175 *crow::connections::systemBus, crashdumpObject,
3176 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3177 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003178}
3179
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003180inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003181{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003182 // Note: Deviated from redfish privilege registry for GET & HEAD
3183 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003184 /**
3185 * Functions triggers appropriate requests on DBus
3186 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003187 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003188 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003189 // This is incorrect, should be.
3190 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003191 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003192 .methods(boost::beast::http::verb::get)(
3193 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003194 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3195 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003196 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003197 {
3198 return;
3199 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003200 if (systemName != "system")
3201 {
3202 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3203 systemName);
3204 return;
3205 }
3206
George Liu7a1dbc42022-12-07 16:03:22 +08003207 constexpr std::array<std::string_view, 1> interfaces = {
3208 crashdumpInterface};
3209 dbus::utility::getSubTreePaths(
3210 "/", 0, interfaces,
3211 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003212 const std::vector<std::string>& resp) {
3213 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003214 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003215 if (ec.value() !=
3216 boost::system::errc::no_such_file_or_directory)
3217 {
3218 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3219 << ec.message();
3220 messages::internalError(asyncResp->res);
3221 return;
3222 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003223 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003224 asyncResp->res.jsonValue["@odata.type"] =
3225 "#LogEntryCollection.LogEntryCollection";
3226 asyncResp->res.jsonValue["@odata.id"] =
3227 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3228 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3229 asyncResp->res.jsonValue["Description"] =
3230 "Collection of Crashdump Entries";
3231 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3232 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003233
Ed Tanous002d39b2022-05-31 08:59:27 -07003234 for (const std::string& path : resp)
3235 {
3236 const sdbusplus::message::object_path objPath(path);
3237 // Get the log ID
3238 std::string logID = objPath.filename();
3239 if (logID.empty())
3240 {
3241 continue;
3242 }
3243 // Add the log entry to the array
3244 logCrashdumpEntry(asyncResp, logID,
3245 asyncResp->res.jsonValue["Members"]);
3246 }
George Liu7a1dbc42022-12-07 16:03:22 +08003247 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003248 });
3249}
Ed Tanous1da66f72018-07-27 16:13:37 -07003250
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003251inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003252{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003253 // Note: Deviated from redfish privilege registry for GET & HEAD
3254 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003255
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003256 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003257 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003258 // this is incorrect, should be
3259 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003260 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003261 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003262 [&app](const crow::Request& req,
3263 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003264 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003265 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003266 {
3267 return;
3268 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003269 if (systemName != "system")
3270 {
3271 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3272 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003273 return;
3274 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003275 const std::string& logID = param;
3276 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3277 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003278}
Ed Tanous1da66f72018-07-27 16:13:37 -07003279
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003280inline void requestRoutesCrashdumpFile(App& app)
3281{
3282 // Note: Deviated from redfish privilege registry for GET & HEAD
3283 // method for security reasons.
3284 BMCWEB_ROUTE(
3285 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003286 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003287 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003288 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003289 [](const crow::Request& req,
3290 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003291 const std::string& systemName, const std::string& logID,
3292 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003293 // Do not call getRedfishRoute here since the crashdump file is not a
3294 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003295
3296 if (systemName != "system")
3297 {
3298 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3299 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003300 return;
3301 }
3302
Ed Tanous002d39b2022-05-31 08:59:27 -07003303 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003304 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003305 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003306 const std::vector<
3307 std::pair<std::string, dbus::utility::DbusVariantType>>&
3308 resp) {
3309 if (ec)
3310 {
3311 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3312 messages::internalError(asyncResp->res);
3313 return;
3314 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003315
Ed Tanous002d39b2022-05-31 08:59:27 -07003316 std::string dbusFilename{};
3317 std::string dbusTimestamp{};
3318 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003319
Ed Tanous002d39b2022-05-31 08:59:27 -07003320 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3321 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003322
Ed Tanous002d39b2022-05-31 08:59:27 -07003323 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3324 dbusFilepath.empty())
3325 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003326 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003327 return;
3328 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003329
Ed Tanous002d39b2022-05-31 08:59:27 -07003330 // Verify the file name parameter is correct
3331 if (fileName != dbusFilename)
3332 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003333 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003334 return;
3335 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003336
Ed Tanous002d39b2022-05-31 08:59:27 -07003337 if (!std::filesystem::exists(dbusFilepath))
3338 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003339 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003340 return;
3341 }
3342 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3343 asyncResp->res.body() =
3344 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003345
Ed Tanous002d39b2022-05-31 08:59:27 -07003346 // Configure this to be a file download when accessed
3347 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003348 asyncResp->res.addHeader(
3349 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003350 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003351 sdbusplus::asio::getAllProperties(
3352 *crow::connections::systemBus, crashdumpObject,
3353 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3354 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003355 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003356}
3357
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003358enum class OEMDiagnosticType
3359{
3360 onDemand,
3361 telemetry,
3362 invalid,
3363};
3364
Ed Tanous26ccae32023-02-16 10:28:44 -08003365inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003366{
3367 if (oemDiagStr == "OnDemand")
3368 {
3369 return OEMDiagnosticType::onDemand;
3370 }
3371 if (oemDiagStr == "Telemetry")
3372 {
3373 return OEMDiagnosticType::telemetry;
3374 }
3375
3376 return OEMDiagnosticType::invalid;
3377}
3378
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003379inline void requestRoutesCrashdumpCollect(App& app)
3380{
3381 // Note: Deviated from redfish privilege registry for GET & HEAD
3382 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003383 BMCWEB_ROUTE(
3384 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003385 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003386 // The below is incorrect; Should be ConfigureManager
3387 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003388 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003389 .methods(boost::beast::http::verb::post)(
3390 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003391 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3392 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003393 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003394 {
3395 return;
3396 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003397
3398 if (systemName != "system")
3399 {
3400 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3401 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003402 return;
3403 }
3404
Ed Tanous002d39b2022-05-31 08:59:27 -07003405 std::string diagnosticDataType;
3406 std::string oemDiagnosticDataType;
3407 if (!redfish::json_util::readJsonAction(
3408 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3409 "OEMDiagnosticDataType", oemDiagnosticDataType))
3410 {
3411 return;
3412 }
3413
3414 if (diagnosticDataType != "OEM")
3415 {
3416 BMCWEB_LOG_ERROR
3417 << "Only OEM DiagnosticDataType supported for Crashdump";
3418 messages::actionParameterValueFormatError(
3419 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3420 "CollectDiagnosticData");
3421 return;
3422 }
3423
3424 OEMDiagnosticType oemDiagType =
3425 getOEMDiagnosticType(oemDiagnosticDataType);
3426
3427 std::string iface;
3428 std::string method;
3429 std::string taskMatchStr;
3430 if (oemDiagType == OEMDiagnosticType::onDemand)
3431 {
3432 iface = crashdumpOnDemandInterface;
3433 method = "GenerateOnDemandLog";
3434 taskMatchStr = "type='signal',"
3435 "interface='org.freedesktop.DBus.Properties',"
3436 "member='PropertiesChanged',"
3437 "arg0namespace='com.intel.crashdump'";
3438 }
3439 else if (oemDiagType == OEMDiagnosticType::telemetry)
3440 {
3441 iface = crashdumpTelemetryInterface;
3442 method = "GenerateTelemetryLog";
3443 taskMatchStr = "type='signal',"
3444 "interface='org.freedesktop.DBus.Properties',"
3445 "member='PropertiesChanged',"
3446 "arg0namespace='com.intel.crashdump'";
3447 }
3448 else
3449 {
3450 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3451 << oemDiagnosticDataType;
3452 messages::actionParameterValueFormatError(
3453 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3454 "CollectDiagnosticData");
3455 return;
3456 }
3457
3458 auto collectCrashdumpCallback =
3459 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003460 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003461 const std::string&) mutable {
3462 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003463 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003464 if (ec.value() == boost::system::errc::operation_not_supported)
3465 {
3466 messages::resourceInStandby(asyncResp->res);
3467 }
3468 else if (ec.value() ==
3469 boost::system::errc::device_or_resource_busy)
3470 {
3471 messages::serviceTemporarilyUnavailable(asyncResp->res,
3472 "60");
3473 }
3474 else
3475 {
3476 messages::internalError(asyncResp->res);
3477 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003478 return;
3479 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003480 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003481 [](const boost::system::error_code& err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003482 const std::shared_ptr<task::TaskData>& taskData) {
3483 if (!err)
3484 {
3485 taskData->messages.emplace_back(messages::taskCompletedOK(
3486 std::to_string(taskData->index)));
3487 taskData->state = "Completed";
3488 }
3489 return task::completed;
3490 },
3491 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003492
Ed Tanous002d39b2022-05-31 08:59:27 -07003493 task->startTimer(std::chrono::minutes(5));
3494 task->populateResp(asyncResp->res);
3495 task->payload.emplace(std::move(payload));
3496 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003497
Ed Tanous002d39b2022-05-31 08:59:27 -07003498 crow::connections::systemBus->async_method_call(
3499 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3500 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003501 });
3502}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003503
Andrew Geisslercb92c032018-08-17 07:56:14 -07003504/**
3505 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3506 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003507inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003508{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003509 /**
3510 * Function handles POST method request.
3511 * The Clear Log actions does not require any parameter.The action deletes
3512 * all entries found in the Entries collection for this Log Service.
3513 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003514
George Liu0fda0f12021-11-16 10:06:17 +08003515 BMCWEB_ROUTE(
3516 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003517 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003518 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003519 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003520 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003521 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3522 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003523 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003524 {
3525 return;
3526 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003527 if (systemName != "system")
3528 {
3529 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3530 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003531 return;
3532 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003533 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003534
Ed Tanous002d39b2022-05-31 08:59:27 -07003535 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003536 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003537 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3538 if (ec)
3539 {
3540 // TODO Handle for specific error code
3541 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3542 asyncResp->res.result(
3543 boost::beast::http::status::internal_server_error);
3544 return;
3545 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003546
Ed Tanous002d39b2022-05-31 08:59:27 -07003547 asyncResp->res.result(boost::beast::http::status::no_content);
3548 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003549
Ed Tanous002d39b2022-05-31 08:59:27 -07003550 // Make call to Logging service to request Clear Log
3551 crow::connections::systemBus->async_method_call(
3552 respHandler, "xyz.openbmc_project.Logging",
3553 "/xyz/openbmc_project/logging",
3554 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3555 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003556}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003557
3558/****************************************************
3559 * Redfish PostCode interfaces
3560 * using DBUS interface: getPostCodesTS
3561 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003562inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003563{
Ed Tanous22d268c2022-05-19 09:39:07 -07003564 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003565 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003566 .methods(boost::beast::http::verb::get)(
3567 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003568 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3569 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003570 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003571 {
3572 return;
3573 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003574 if (systemName != "system")
3575 {
3576 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3577 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003578 return;
3579 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003580 asyncResp->res.jsonValue["@odata.id"] =
3581 "/redfish/v1/Systems/system/LogServices/PostCodes";
3582 asyncResp->res.jsonValue["@odata.type"] =
3583 "#LogService.v1_1_0.LogService";
3584 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3585 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003586 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003587 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3588 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3589 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303590
Ed Tanous002d39b2022-05-31 08:59:27 -07003591 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003592 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003593 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3594 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3595 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303596
Ed Tanous002d39b2022-05-31 08:59:27 -07003597 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3598 {"target",
3599 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003600 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003601}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003602
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003603inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003604{
George Liu0fda0f12021-11-16 10:06:17 +08003605 BMCWEB_ROUTE(
3606 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003607 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003608 // The following privilege is incorrect; It should be ConfigureManager
3609 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003610 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003611 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003612 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003613 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3614 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003615 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003616 {
3617 return;
3618 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003619 if (systemName != "system")
3620 {
3621 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3622 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003623 return;
3624 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003625 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003626
Ed Tanous002d39b2022-05-31 08:59:27 -07003627 // Make call to post-code service to request clear all
3628 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003629 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003630 if (ec)
3631 {
3632 // TODO Handle for specific error code
3633 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3634 << ec;
3635 asyncResp->res.result(
3636 boost::beast::http::status::internal_server_error);
3637 messages::internalError(asyncResp->res);
3638 return;
3639 }
3640 },
3641 "xyz.openbmc_project.State.Boot.PostCode0",
3642 "/xyz/openbmc_project/State/Boot/PostCode0",
3643 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3644 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003645}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003646
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003647/**
3648 * @brief Parse post code ID and get the current value and index value
3649 * eg: postCodeID=B1-2, currentValue=1, index=2
3650 *
3651 * @param[in] postCodeID Post Code ID
3652 * @param[out] currentValue Current value
3653 * @param[out] index Index value
3654 *
3655 * @return bool true if the parsing is successful, false the parsing fails
3656 */
3657inline static bool parsePostCode(const std::string& postCodeID,
3658 uint64_t& currentValue, uint16_t& index)
3659{
3660 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003661 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003662 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3663 {
3664 return false;
3665 }
3666
Patrick Williams84396af2023-05-11 11:47:45 -05003667 auto start = std::next(split[0].begin());
3668 auto end = split[0].end();
3669 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003670
Patrick Williams84396af2023-05-11 11:47:45 -05003671 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003672 {
3673 return false;
3674 }
3675
Patrick Williams84396af2023-05-11 11:47:45 -05003676 start = split[1].begin();
3677 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003678
Patrick Williams84396af2023-05-11 11:47:45 -05003679 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003680
Patrick Williams84396af2023-05-11 11:47:45 -05003681 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003682}
3683
3684static bool fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003685 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303686 const boost::container::flat_map<
3687 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003688 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3689 const uint64_t skip = 0, const uint64_t top = 0)
3690{
3691 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003692 const registries::Message* message =
3693 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003694
3695 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003696 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303697 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3698 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003699 {
3700 currentCodeIndex++;
3701 std::string postcodeEntryID =
3702 "B" + std::to_string(bootIndex) + "-" +
3703 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3704
3705 uint64_t usecSinceEpoch = code.first;
3706 uint64_t usTimeOffset = 0;
3707
3708 if (1 == currentCodeIndex)
3709 { // already incremented
3710 firstCodeTimeUs = code.first;
3711 }
3712 else
3713 {
3714 usTimeOffset = code.first - firstCodeTimeUs;
3715 }
3716
3717 // skip if no specific codeIndex is specified and currentCodeIndex does
3718 // not fall between top and skip
3719 if ((codeIndex == 0) &&
3720 (currentCodeIndex <= skip || currentCodeIndex > top))
3721 {
3722 continue;
3723 }
3724
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003725 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003726 // currentIndex
3727 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3728 {
3729 // This is done for simplicity. 1st entry is needed to calculate
3730 // time offset. To improve efficiency, one can get to the entry
3731 // directly (possibly with flatmap's nth method)
3732 continue;
3733 }
3734
3735 // currentCodeIndex is within top and skip or equal to specified code
3736 // index
3737
3738 // Get the Created time from the timestamp
3739 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003740 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003741
3742 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3743 std::ostringstream hexCode;
3744 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303745 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003746 std::ostringstream timeOffsetStr;
3747 // Set Fixed -Point Notation
3748 timeOffsetStr << std::fixed;
3749 // Set precision to 4 digits
3750 timeOffsetStr << std::setprecision(4);
3751 // Add double to stream
3752 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3753 std::vector<std::string> messageArgs = {
3754 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3755
3756 // Get MessageArgs template from message registry
3757 std::string msg;
3758 if (message != nullptr)
3759 {
3760 msg = message->message;
3761
3762 // fill in this post code value
3763 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003764 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003765 {
3766 std::string argStr = "%" + std::to_string(++i);
3767 size_t argPos = msg.find(argStr);
3768 if (argPos != std::string::npos)
3769 {
3770 msg.replace(argPos, argStr.length(), messageArg);
3771 }
3772 }
3773 }
3774
Tim Leed4342a92020-04-27 11:47:58 +08003775 // Get Severity template from message registry
3776 std::string severity;
3777 if (message != nullptr)
3778 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003779 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003780 }
3781
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003782 // Format entry
3783 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003784 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003785 bmcLogEntry["@odata.id"] = boost::urls::format(
3786 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
3787 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003788 bmcLogEntry["Name"] = "POST Code Log Entry";
3789 bmcLogEntry["Id"] = postcodeEntryID;
3790 bmcLogEntry["Message"] = std::move(msg);
3791 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3792 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3793 bmcLogEntry["EntryType"] = "Event";
3794 bmcLogEntry["Severity"] = std::move(severity);
3795 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003796 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3797 {
3798 bmcLogEntry["AdditionalDataURI"] =
3799 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3800 postcodeEntryID + "/attachment";
3801 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003802
3803 // codeIndex is only specified when querying single entry, return only
3804 // that entry in this case
3805 if (codeIndex != 0)
3806 {
3807 aResp->res.jsonValue.update(bmcLogEntry);
3808 return true;
3809 }
3810
3811 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05003812 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003813 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003814
3815 // Return value is always false when querying multiple entries
3816 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003817}
3818
zhanghch058d1b46d2021-04-01 11:18:24 +08003819static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003820 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003821{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003822 uint16_t bootIndex = 0;
3823 uint64_t codeIndex = 0;
3824 if (!parsePostCode(entryId, codeIndex, bootIndex))
3825 {
3826 // Requested ID was not found
3827 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3828 return;
3829 }
3830
3831 if (bootIndex == 0 || codeIndex == 0)
3832 {
3833 // 0 is an invalid index
3834 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3835 return;
3836 }
3837
ZhikuiRena3316fc2020-01-29 14:58:08 -08003838 crow::connections::systemBus->async_method_call(
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003839 [aResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003840 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303841 const boost::container::flat_map<
3842 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3843 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003844 if (ec)
3845 {
3846 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3847 messages::internalError(aResp->res);
3848 return;
3849 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003850
Ed Tanous002d39b2022-05-31 08:59:27 -07003851 if (postcode.empty())
3852 {
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003853 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003854 return;
3855 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003856
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003857 if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
3858 {
3859 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3860 return;
3861 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003862 },
Jonathan Doman15124762021-01-07 17:54:17 -08003863 "xyz.openbmc_project.State.Boot.PostCode0",
3864 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003865 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3866 bootIndex);
3867}
3868
zhanghch058d1b46d2021-04-01 11:18:24 +08003869static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003870 const uint16_t bootIndex,
3871 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003872 const uint64_t entryCount, size_t skip,
3873 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003874{
3875 crow::connections::systemBus->async_method_call(
3876 [aResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003877 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303878 const boost::container::flat_map<
3879 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3880 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003881 if (ec)
3882 {
3883 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3884 messages::internalError(aResp->res);
3885 return;
3886 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003887
Ed Tanous002d39b2022-05-31 08:59:27 -07003888 uint64_t endCount = entryCount;
3889 if (!postcode.empty())
3890 {
3891 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003892 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003893 {
Patrick Williams89492a12023-05-10 07:51:34 -05003894 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
3895 entryCount) -
3896 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003897 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003898 std::min(static_cast<uint64_t>(top + skip), endCount) -
3899 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003900
3901 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3902 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003903 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003904 aResp->res.jsonValue["Members@odata.count"] = endCount;
3905 }
3906
3907 // continue to previous bootIndex
3908 if (bootIndex < bootCount)
3909 {
3910 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3911 bootCount, endCount, skip, top);
3912 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003913 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003914 {
3915 aResp->res.jsonValue["Members@odata.nextLink"] =
3916 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3917 std::to_string(skip + top);
3918 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003919 },
Jonathan Doman15124762021-01-07 17:54:17 -08003920 "xyz.openbmc_project.State.Boot.PostCode0",
3921 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003922 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3923 bootIndex);
3924}
3925
zhanghch058d1b46d2021-04-01 11:18:24 +08003926static void
3927 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003928 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003929{
3930 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003931 sdbusplus::asio::getProperty<uint16_t>(
3932 *crow::connections::systemBus,
3933 "xyz.openbmc_project.State.Boot.PostCode0",
3934 "/xyz/openbmc_project/State/Boot/PostCode0",
3935 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003936 [aResp, entryCount, skip, top](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003937 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003938 if (ec)
3939 {
3940 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3941 messages::internalError(aResp->res);
3942 return;
3943 }
3944 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003945 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003946}
3947
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003948inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003949{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003950 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003951 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003952 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003953 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003954 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003955 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3956 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003957 query_param::QueryCapabilities capabilities = {
3958 .canDelegateTop = true,
3959 .canDelegateSkip = true,
3960 };
3961 query_param::Query delegatedQuery;
3962 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003963 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003964 {
3965 return;
3966 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003967
3968 if (systemName != "system")
3969 {
3970 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3971 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003972 return;
3973 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003974 asyncResp->res.jsonValue["@odata.type"] =
3975 "#LogEntryCollection.LogEntryCollection";
3976 asyncResp->res.jsonValue["@odata.id"] =
3977 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3978 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3979 asyncResp->res.jsonValue["Description"] =
3980 "Collection of POST Code Log Entries";
3981 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3982 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003983 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003984 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003985 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003986 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003987}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003988
George Liu647b3cd2021-07-05 12:43:56 +08003989inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3990{
George Liu0fda0f12021-11-16 10:06:17 +08003991 BMCWEB_ROUTE(
3992 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003993 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003994 .privileges(redfish::privileges::getLogEntry)
3995 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003996 [&app](const crow::Request& req,
3997 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003998 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07003999 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004000 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004001 {
4002 return;
4003 }
Matt Spinler72e21372023-04-19 12:53:33 -05004004 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004005 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004006 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004007 {
4008 asyncResp->res.result(boost::beast::http::status::bad_request);
4009 return;
4010 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004011 if (systemName != "system")
4012 {
4013 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4014 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004015 return;
4016 }
George Liu647b3cd2021-07-05 12:43:56 +08004017
Ed Tanous002d39b2022-05-31 08:59:27 -07004018 uint64_t currentValue = 0;
4019 uint16_t index = 0;
4020 if (!parsePostCode(postCodeID, currentValue, index))
4021 {
4022 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4023 return;
4024 }
George Liu647b3cd2021-07-05 12:43:56 +08004025
Ed Tanous002d39b2022-05-31 08:59:27 -07004026 crow::connections::systemBus->async_method_call(
4027 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004028 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004029 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4030 postcodes) {
4031 if (ec.value() == EBADR)
4032 {
4033 messages::resourceNotFound(asyncResp->res, "LogEntry",
4034 postCodeID);
4035 return;
4036 }
4037 if (ec)
4038 {
4039 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4040 messages::internalError(asyncResp->res);
4041 return;
4042 }
George Liu647b3cd2021-07-05 12:43:56 +08004043
Ed Tanous002d39b2022-05-31 08:59:27 -07004044 size_t value = static_cast<size_t>(currentValue) - 1;
4045 if (value == std::string::npos || postcodes.size() < currentValue)
4046 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004047 BMCWEB_LOG_WARNING << "Wrong currentValue value";
Ed Tanous002d39b2022-05-31 08:59:27 -07004048 messages::resourceNotFound(asyncResp->res, "LogEntry",
4049 postCodeID);
4050 return;
4051 }
George Liu647b3cd2021-07-05 12:43:56 +08004052
Ed Tanous002d39b2022-05-31 08:59:27 -07004053 const auto& [tID, c] = postcodes[value];
4054 if (c.empty())
4055 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004056 BMCWEB_LOG_WARNING << "No found post code data";
Ed Tanous002d39b2022-05-31 08:59:27 -07004057 messages::resourceNotFound(asyncResp->res, "LogEntry",
4058 postCodeID);
4059 return;
4060 }
4061 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4062 const char* d = reinterpret_cast<const char*>(c.data());
4063 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004064
Ed Tanousd9f6c622022-03-17 09:12:17 -07004065 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004066 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004067 asyncResp->res.addHeader(
4068 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004069 asyncResp->res.body() = crow::utility::base64encode(strData);
4070 },
4071 "xyz.openbmc_project.State.Boot.PostCode0",
4072 "/xyz/openbmc_project/State/Boot/PostCode0",
4073 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4074 });
George Liu647b3cd2021-07-05 12:43:56 +08004075}
4076
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004077inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004078{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004079 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004080 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004081 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004082 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004083 [&app](const crow::Request& req,
4084 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004085 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004086 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004087 {
4088 return;
4089 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004090 if (systemName != "system")
4091 {
4092 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4093 systemName);
4094 return;
4095 }
4096
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004097 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004098 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004099}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004100
Ed Tanous1da66f72018-07-27 16:13:37 -07004101} // namespace redfish