blob: b0c711b2bf9a978c3b0b3d0afb6f5a95bdeafaa1 [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
Ed Tanousc51a58e2023-03-27 14:43:19 -0700857 boost::urls::url url = boost::urls::format(
858 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
859
860 std::string headerLoc = "Location: ";
861 headerLoc += url.buffer();
862
Ed Tanous002d39b2022-05-31 08:59:27 -0700863 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
864
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600865 BMCWEB_LOG_DEBUG << createdObjPath.str
866 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700867 taskData->state = "Completed";
868 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600869 },
870 "type='signal',interface='org.freedesktop.DBus.Properties',"
871 "member='PropertiesChanged',path='" +
872 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500873
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600874 // The task timer is set to max time limit within which the
875 // requested dump will be collected.
876 task->startTimer(std::chrono::minutes(6));
877 task->populateResp(asyncResp->res);
878 task->payload.emplace(payload);
879 },
880 "xyz.openbmc_project.Dump.Manager", createdObjPath,
881 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500882}
883
zhanghch058d1b46d2021-04-01 11:18:24 +0800884inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
885 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500886{
Claire Weinanfdd26902022-03-01 14:18:25 -0800887 std::string dumpPath = getDumpEntriesPath(dumpType);
888 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500889 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500890 messages::internalError(asyncResp->res);
891 return;
892 }
893
894 std::optional<std::string> diagnosticDataType;
895 std::optional<std::string> oemDiagnosticDataType;
896
Willy Tu15ed6782021-12-14 11:03:16 -0800897 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500898 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
899 "OEMDiagnosticDataType", oemDiagnosticDataType))
900 {
901 return;
902 }
903
904 if (dumpType == "System")
905 {
906 if (!oemDiagnosticDataType || !diagnosticDataType)
907 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800908 BMCWEB_LOG_ERROR
909 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500910 messages::actionParameterMissing(
911 asyncResp->res, "CollectDiagnosticData",
912 "DiagnosticDataType & OEMDiagnosticDataType");
913 return;
914 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700915 if ((*oemDiagnosticDataType != "System") ||
916 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500917 {
918 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700919 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500920 return;
921 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500922 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500923 }
924 else if (dumpType == "BMC")
925 {
926 if (!diagnosticDataType)
927 {
George Liu0fda0f12021-11-16 10:06:17 +0800928 BMCWEB_LOG_ERROR
929 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500930 messages::actionParameterMissing(
931 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
932 return;
933 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700934 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500935 {
936 BMCWEB_LOG_ERROR
937 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700938 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500939 return;
940 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500941 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
942 }
943 else
944 {
945 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
946 messages::internalError(asyncResp->res);
947 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500948 }
949
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600950 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
951 createDumpParamVec;
952
Carson Labradof574a8e2023-03-22 02:26:00 +0000953 if (req.session != nullptr)
954 {
955 createDumpParamVec.emplace_back(
956 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
957 req.session->clientIp);
958 createDumpParamVec.emplace_back(
959 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
960 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
961 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600962
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500963 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800964 [asyncResp, payload(task::Payload(req)),
965 dumpPath](const boost::system::error_code& ec,
966 const sdbusplus::message_t& msg,
967 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700968 if (ec)
969 {
970 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500971 const sd_bus_error* dbusError = msg.get_error();
972 if (dbusError == nullptr)
973 {
974 messages::internalError(asyncResp->res);
975 return;
976 }
977
978 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
979 << " and error msg: " << dbusError->message;
980 if (std::string_view(
981 "xyz.openbmc_project.Common.Error.NotAllowed") ==
982 dbusError->name)
983 {
984 messages::resourceInStandby(asyncResp->res);
985 return;
986 }
987 if (std::string_view(
988 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
989 dbusError->name)
990 {
991 messages::serviceDisabled(asyncResp->res, dumpPath);
992 return;
993 }
994 if (std::string_view(
995 "xyz.openbmc_project.Common.Error.Unavailable") ==
996 dbusError->name)
997 {
998 messages::resourceInUse(asyncResp->res);
999 return;
1000 }
1001 // Other Dbus errors such as:
1002 // xyz.openbmc_project.Common.Error.InvalidArgument &
1003 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1004 // the dbus call that is made here in the bmcweb
1005 // implementation and has nothing to do with the client's
1006 // input in the request. Hence, returning internal error
1007 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001008 messages::internalError(asyncResp->res);
1009 return;
1010 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001011 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
1012 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001013 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001014 "xyz.openbmc_project.Dump.Manager",
1015 "/xyz/openbmc_project/dump/" +
1016 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001017 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001018}
1019
zhanghch058d1b46d2021-04-01 11:18:24 +08001020inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1021 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001022{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001023 std::string dumpTypeLowerCopy =
1024 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001025
Claire Weinan0d946212022-07-13 19:40:19 -07001026 crow::connections::systemBus->async_method_call(
1027 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001028 if (ec)
1029 {
Claire Weinan0d946212022-07-13 19:40:19 -07001030 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -07001031 messages::internalError(asyncResp->res);
1032 return;
1033 }
Claire Weinan0d946212022-07-13 19:40:19 -07001034 },
1035 "xyz.openbmc_project.Dump.Manager",
1036 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1037 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001038}
1039
Ed Tanousb9d36b42022-02-26 21:42:46 -08001040inline static void
1041 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1042 std::string& filename, std::string& timestamp,
1043 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001044{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001045 const std::string* filenamePtr = nullptr;
1046 const std::string* timestampPtr = nullptr;
1047 const std::string* logfilePtr = nullptr;
1048
1049 const bool success = sdbusplus::unpackPropertiesNoThrow(
1050 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1051 "Filename", filenamePtr, "Log", logfilePtr);
1052
1053 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001054 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001055 return;
1056 }
1057
1058 if (filenamePtr != nullptr)
1059 {
1060 filename = *filenamePtr;
1061 }
1062
1063 if (timestampPtr != nullptr)
1064 {
1065 timestamp = *timestampPtr;
1066 }
1067
1068 if (logfilePtr != nullptr)
1069 {
1070 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001071 }
1072}
1073
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001074inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001075{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001076 /**
1077 * Functions triggers appropriate requests on DBus
1078 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001079 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001080 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001081 .methods(boost::beast::http::verb::get)(
1082 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001083 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1084 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001085 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001086 {
1087 return;
1088 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001089 if (systemName != "system")
1090 {
1091 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1092 systemName);
1093 return;
1094 }
1095
Ed Tanous002d39b2022-05-31 08:59:27 -07001096 // Collections don't include the static data added by SubRoute
1097 // because it has a duplicate entry for members
1098 asyncResp->res.jsonValue["@odata.type"] =
1099 "#LogServiceCollection.LogServiceCollection";
1100 asyncResp->res.jsonValue["@odata.id"] =
1101 "/redfish/v1/Systems/system/LogServices";
1102 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1103 asyncResp->res.jsonValue["Description"] =
1104 "Collection of LogServices for this Computer System";
1105 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1106 logServiceArray = nlohmann::json::array();
1107 nlohmann::json::object_t eventLog;
1108 eventLog["@odata.id"] =
1109 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001110 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001111#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001112 nlohmann::json::object_t dumpLog;
1113 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001114 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001115#endif
1116
Jason M. Billsd53dd412019-02-12 17:16:22 -08001117#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001118 nlohmann::json::object_t crashdump;
1119 crashdump["@odata.id"] =
1120 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001121 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001122#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001123
1124#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001125 nlohmann::json::object_t hostlogger;
1126 hostlogger["@odata.id"] =
1127 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001128 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001129#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001130 asyncResp->res.jsonValue["Members@odata.count"] =
1131 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001132
George Liu7a1dbc42022-12-07 16:03:22 +08001133 constexpr std::array<std::string_view, 1> interfaces = {
1134 "xyz.openbmc_project.State.Boot.PostCode"};
1135 dbus::utility::getSubTreePaths(
1136 "/", 0, interfaces,
1137 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001138 const dbus::utility::MapperGetSubTreePathsResponse&
1139 subtreePath) {
1140 if (ec)
1141 {
1142 BMCWEB_LOG_ERROR << ec;
1143 return;
1144 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001145
Ed Tanous002d39b2022-05-31 08:59:27 -07001146 for (const auto& pathStr : subtreePath)
1147 {
1148 if (pathStr.find("PostCode") != std::string::npos)
1149 {
1150 nlohmann::json& logServiceArrayLocal =
1151 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001152 nlohmann::json::object_t member;
1153 member["@odata.id"] =
1154 "/redfish/v1/Systems/system/LogServices/PostCodes";
1155
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001156 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001157
Ed Tanous002d39b2022-05-31 08:59:27 -07001158 asyncResp->res.jsonValue["Members@odata.count"] =
1159 logServiceArrayLocal.size();
1160 return;
1161 }
1162 }
George Liu7a1dbc42022-12-07 16:03:22 +08001163 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001164 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001165}
1166
1167inline void requestRoutesEventLogService(App& app)
1168{
Ed Tanous22d268c2022-05-19 09:39:07 -07001169 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001170 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001171 .methods(boost::beast::http::verb::get)(
1172 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001173 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1174 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001175 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001176 {
1177 return;
1178 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001179 if (systemName != "system")
1180 {
1181 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1182 systemName);
1183 return;
1184 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001185 asyncResp->res.jsonValue["@odata.id"] =
1186 "/redfish/v1/Systems/system/LogServices/EventLog";
1187 asyncResp->res.jsonValue["@odata.type"] =
1188 "#LogService.v1_1_0.LogService";
1189 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1190 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1191 asyncResp->res.jsonValue["Id"] = "EventLog";
1192 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301193
Ed Tanous002d39b2022-05-31 08:59:27 -07001194 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001195 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301196
Ed Tanous002d39b2022-05-31 08:59:27 -07001197 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1198 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1199 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301200
Ed Tanous002d39b2022-05-31 08:59:27 -07001201 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1202 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1203 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001204
Ed Tanous002d39b2022-05-31 08:59:27 -07001205 {"target",
1206 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001207 });
1208}
1209
1210inline void requestRoutesJournalEventLogClear(App& app)
1211{
Jason M. Bills4978b632022-02-22 14:17:43 -08001212 BMCWEB_ROUTE(
1213 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001214 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001215 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001216 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001217 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001218 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1219 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001220 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001221 {
1222 return;
1223 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001224 if (systemName != "system")
1225 {
1226 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1227 systemName);
1228 return;
1229 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001230 // Clear the EventLog by deleting the log files
1231 std::vector<std::filesystem::path> redfishLogFiles;
1232 if (getRedfishLogFiles(redfishLogFiles))
1233 {
1234 for (const std::filesystem::path& file : redfishLogFiles)
1235 {
1236 std::error_code ec;
1237 std::filesystem::remove(file, ec);
1238 }
1239 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001240
Ed Tanous002d39b2022-05-31 08:59:27 -07001241 // Reload rsyslog so it knows to start new log files
1242 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001243 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001244 if (ec)
1245 {
1246 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1247 messages::internalError(asyncResp->res);
1248 return;
1249 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001250
Ed Tanous002d39b2022-05-31 08:59:27 -07001251 messages::success(asyncResp->res);
1252 },
1253 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1254 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1255 "replace");
1256 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001257}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001258
Jason M. Billsac992cd2022-06-24 13:31:46 -07001259enum class LogParseError
1260{
1261 success,
1262 parseFailed,
1263 messageIdNotInRegistry,
1264};
1265
1266static LogParseError
1267 fillEventLogEntryJson(const std::string& logEntryID,
1268 const std::string& logEntry,
1269 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001270{
Jason M. Bills95820182019-04-22 16:25:34 -07001271 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001272 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001273 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001274 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001275 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001276 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001277 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001278 std::string timestamp = logEntry.substr(0, space);
1279 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001280 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001281 if (entryStart == std::string::npos)
1282 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001283 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001284 }
1285 std::string_view entry(logEntry);
1286 entry.remove_prefix(entryStart);
1287 // Use split to separate the entry into its fields
1288 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001289 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001290 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001291 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001292 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001293 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001294 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001295 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001296
Jason M. Bills4851d452019-03-28 11:27:48 -07001297 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001298 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001299
Sui Chen54417b02022-03-24 14:59:52 -07001300 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001301 {
Sui Chen54417b02022-03-24 14:59:52 -07001302 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001303 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001304 }
1305
Sui Chen54417b02022-03-24 14:59:52 -07001306 std::string msg = message->message;
1307
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001308 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001309 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001310 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001311 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001312 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001313 // If the first string is empty, assume there are no MessageArgs
1314 std::size_t messageArgsSize = 0;
1315 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001316 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001317 messageArgsSize = logEntryFields.size() - 1;
1318 }
1319
Ed Tanous23a21a12020-07-25 04:45:05 +00001320 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001321
1322 // Fill the MessageArgs into the Message
1323 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001324 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001325 {
1326 std::string argStr = "%" + std::to_string(++i);
1327 size_t argPos = msg.find(argStr);
1328 if (argPos != std::string::npos)
1329 {
1330 msg.replace(argPos, argStr.length(), messageArg);
1331 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001332 }
1333 }
1334
Jason M. Bills95820182019-04-22 16:25:34 -07001335 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1336 // format which matches the Redfish format except for the fractional seconds
1337 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001338 std::size_t dot = timestamp.find_first_of('.');
1339 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001340 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001341 {
Jason M. Bills95820182019-04-22 16:25:34 -07001342 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001343 }
1344
1345 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001346 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001347 logEntryJson["@odata.id"] = boost::urls::format(
1348 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1349 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001350 logEntryJson["Name"] = "System Event Log Entry";
1351 logEntryJson["Id"] = logEntryID;
1352 logEntryJson["Message"] = std::move(msg);
1353 logEntryJson["MessageId"] = std::move(messageID);
1354 logEntryJson["MessageArgs"] = messageArgs;
1355 logEntryJson["EntryType"] = "Event";
1356 logEntryJson["Severity"] = message->messageSeverity;
1357 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001358 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001359}
1360
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001361inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001362{
Ed Tanous22d268c2022-05-19 09:39:07 -07001363 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001364 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001365 .methods(boost::beast::http::verb::get)(
1366 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001367 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1368 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001369 query_param::QueryCapabilities capabilities = {
1370 .canDelegateTop = true,
1371 .canDelegateSkip = true,
1372 };
1373 query_param::Query delegatedQuery;
1374 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001375 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001376 {
1377 return;
1378 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001379 if (systemName != "system")
1380 {
1381 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1382 systemName);
1383 return;
1384 }
1385
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001386 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001387 size_t skip = delegatedQuery.skip.value_or(0);
1388
Ed Tanous002d39b2022-05-31 08:59:27 -07001389 // Collections don't include the static data added by SubRoute
1390 // because it has a duplicate entry for members
1391 asyncResp->res.jsonValue["@odata.type"] =
1392 "#LogEntryCollection.LogEntryCollection";
1393 asyncResp->res.jsonValue["@odata.id"] =
1394 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1395 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1396 asyncResp->res.jsonValue["Description"] =
1397 "Collection of System Event Log Entries";
1398
1399 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1400 logEntryArray = nlohmann::json::array();
1401 // Go through the log files and create a unique ID for each
1402 // entry
1403 std::vector<std::filesystem::path> redfishLogFiles;
1404 getRedfishLogFiles(redfishLogFiles);
1405 uint64_t entryCount = 0;
1406 std::string logEntry;
1407
1408 // Oldest logs are in the last file, so start there and loop
1409 // backwards
1410 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1411 it++)
1412 {
1413 std::ifstream logStream(*it);
1414 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001415 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001416 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001417 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001418
Ed Tanous002d39b2022-05-31 08:59:27 -07001419 // Reset the unique ID on the first entry
1420 bool firstEntry = true;
1421 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001422 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001423 std::string idStr;
1424 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001425 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001426 continue;
1427 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001428 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001429
Jason M. Billsde703c52022-06-23 14:19:04 -07001430 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001431 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1432 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001433 if (status == LogParseError::messageIdNotInRegistry)
1434 {
1435 continue;
1436 }
1437 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001438 {
1439 messages::internalError(asyncResp->res);
1440 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001441 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001442
Jason M. Billsde703c52022-06-23 14:19:04 -07001443 entryCount++;
1444 // Handle paging using skip (number of entries to skip from the
1445 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001446 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001447 {
1448 continue;
1449 }
1450
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001451 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001452 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001453 }
1454 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001455 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001456 {
1457 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1458 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001459 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001460 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001461 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001462}
Chicago Duan336e96c2019-07-15 14:22:08 +08001463
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001464inline void requestRoutesJournalEventLogEntry(App& app)
1465{
1466 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001467 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001468 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001469 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001470 [&app](const crow::Request& req,
1471 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001472 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001473 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001474 {
1475 return;
1476 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001477
1478 if (systemName != "system")
1479 {
1480 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1481 systemName);
1482 return;
1483 }
1484
Ed Tanous002d39b2022-05-31 08:59:27 -07001485 const std::string& targetID = param;
1486
1487 // Go through the log files and check the unique ID for each
1488 // entry to find the target entry
1489 std::vector<std::filesystem::path> redfishLogFiles;
1490 getRedfishLogFiles(redfishLogFiles);
1491 std::string logEntry;
1492
1493 // Oldest logs are in the last file, so start there and loop
1494 // backwards
1495 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1496 it++)
1497 {
1498 std::ifstream logStream(*it);
1499 if (!logStream.is_open())
1500 {
1501 continue;
1502 }
1503
1504 // Reset the unique ID on the first entry
1505 bool firstEntry = true;
1506 while (std::getline(logStream, logEntry))
1507 {
1508 std::string idStr;
1509 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001510 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001511 continue;
1512 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001513 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001514
1515 if (idStr == targetID)
1516 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001517 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001518 LogParseError status =
1519 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1520 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001521 {
1522 messages::internalError(asyncResp->res);
1523 return;
1524 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001525 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001526 return;
1527 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001528 }
1529 }
1530 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001531 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001532 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001533}
1534
1535inline void requestRoutesDBusEventLogEntryCollection(App& app)
1536{
Ed Tanous22d268c2022-05-19 09:39:07 -07001537 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001538 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001539 .methods(boost::beast::http::verb::get)(
1540 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001541 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1542 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001543 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001544 {
1545 return;
1546 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001547 if (systemName != "system")
1548 {
1549 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1550 systemName);
1551 return;
1552 }
1553
Ed Tanous002d39b2022-05-31 08:59:27 -07001554 // Collections don't include the static data added by SubRoute
1555 // because it has a duplicate entry for members
1556 asyncResp->res.jsonValue["@odata.type"] =
1557 "#LogEntryCollection.LogEntryCollection";
1558 asyncResp->res.jsonValue["@odata.id"] =
1559 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1560 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1561 asyncResp->res.jsonValue["Description"] =
1562 "Collection of System Event Log Entries";
1563
1564 // DBus implementation of EventLog/Entries
1565 // Make call to Logging Service to find all log entry objects
1566 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001567 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001568 const dbus::utility::ManagedObjectType& resp) {
1569 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001570 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001571 // TODO Handle for specific error code
1572 BMCWEB_LOG_ERROR
1573 << "getLogEntriesIfaceData resp_handler got error " << ec;
1574 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001575 return;
1576 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001577 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1578 entriesArray = nlohmann::json::array();
1579 for (const auto& objectPath : resp)
1580 {
1581 const uint32_t* id = nullptr;
1582 const uint64_t* timestamp = nullptr;
1583 const uint64_t* updateTimestamp = nullptr;
1584 const std::string* severity = nullptr;
1585 const std::string* message = nullptr;
1586 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001587 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001588 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001589 const std::string* notify = nullptr;
1590
Ed Tanous002d39b2022-05-31 08:59:27 -07001591 for (const auto& interfaceMap : objectPath.second)
1592 {
1593 if (interfaceMap.first ==
1594 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001595 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001596 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001597 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001598 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001599 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001600 id = std::get_if<uint32_t>(&propertyMap.second);
1601 }
1602 else if (propertyMap.first == "Timestamp")
1603 {
1604 timestamp =
1605 std::get_if<uint64_t>(&propertyMap.second);
1606 }
1607 else if (propertyMap.first == "UpdateTimestamp")
1608 {
1609 updateTimestamp =
1610 std::get_if<uint64_t>(&propertyMap.second);
1611 }
1612 else if (propertyMap.first == "Severity")
1613 {
1614 severity = std::get_if<std::string>(
1615 &propertyMap.second);
1616 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001617 else if (propertyMap.first == "Resolution")
1618 {
1619 resolution = std::get_if<std::string>(
1620 &propertyMap.second);
1621 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001622 else if (propertyMap.first == "Message")
1623 {
1624 message = std::get_if<std::string>(
1625 &propertyMap.second);
1626 }
1627 else if (propertyMap.first == "Resolved")
1628 {
1629 const bool* resolveptr =
1630 std::get_if<bool>(&propertyMap.second);
1631 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001632 {
1633 messages::internalError(asyncResp->res);
1634 return;
1635 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001636 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001637 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001638 else if (propertyMap.first ==
1639 "ServiceProviderNotify")
1640 {
1641 notify = std::get_if<std::string>(
1642 &propertyMap.second);
1643 if (notify == nullptr)
1644 {
1645 messages::internalError(asyncResp->res);
1646 return;
1647 }
1648 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001649 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001650 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001651 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001652 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001653 messages::internalError(asyncResp->res);
1654 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001655 }
1656 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001657 else if (interfaceMap.first ==
1658 "xyz.openbmc_project.Common.FilePath")
1659 {
1660 for (const auto& propertyMap : interfaceMap.second)
1661 {
1662 if (propertyMap.first == "Path")
1663 {
1664 filePath = std::get_if<std::string>(
1665 &propertyMap.second);
1666 }
1667 }
1668 }
1669 }
1670 // Object path without the
1671 // xyz.openbmc_project.Logging.Entry interface, ignore
1672 // and continue.
1673 if (id == nullptr || message == nullptr ||
1674 severity == nullptr || timestamp == nullptr ||
1675 updateTimestamp == nullptr)
1676 {
1677 continue;
1678 }
1679 entriesArray.push_back({});
1680 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001681 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001682 thisEntry["@odata.id"] = boost::urls::format(
1683 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1684 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001685 thisEntry["Name"] = "System Event Log Entry";
1686 thisEntry["Id"] = std::to_string(*id);
1687 thisEntry["Message"] = *message;
1688 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001689 if ((resolution != nullptr) && (!(*resolution).empty()))
1690 {
1691 thisEntry["Resolution"] = *resolution;
1692 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001693 std::optional<bool> notifyAction =
1694 getProviderNotifyAction(*notify);
1695 if (notifyAction)
1696 {
1697 thisEntry["ServiceProviderNotified"] = *notifyAction;
1698 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001699 thisEntry["EntryType"] = "Event";
1700 thisEntry["Severity"] =
1701 translateSeverityDbusToRedfish(*severity);
1702 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001703 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001704 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001705 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001706 if (filePath != nullptr)
1707 {
1708 thisEntry["AdditionalDataURI"] =
1709 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1710 std::to_string(*id) + "/attachment";
1711 }
1712 }
1713 std::sort(
1714 entriesArray.begin(), entriesArray.end(),
1715 [](const nlohmann::json& left, const nlohmann::json& right) {
1716 return (left["Id"] <= right["Id"]);
1717 });
1718 asyncResp->res.jsonValue["Members@odata.count"] =
1719 entriesArray.size();
1720 },
1721 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1722 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001723 });
1724}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001725
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001726inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001727{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001728 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001729 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001730 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001731 .methods(boost::beast::http::verb::get)(
1732 [&app](const crow::Request& req,
1733 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001734 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001735 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001736 {
1737 return;
1738 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001739 if (systemName != "system")
1740 {
1741 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1742 systemName);
1743 return;
1744 }
1745
Ed Tanous002d39b2022-05-31 08:59:27 -07001746 std::string entryID = param;
1747 dbus::utility::escapePathForDbus(entryID);
1748
1749 // DBus implementation of EventLog/Entries
1750 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001751 sdbusplus::asio::getAllProperties(
1752 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1753 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001754 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001755 const dbus::utility::DBusPropertiesMap& resp) {
1756 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001757 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001758 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1759 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001760 return;
1761 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001762 if (ec)
1763 {
1764 BMCWEB_LOG_ERROR
1765 << "EventLogEntry (DBus) resp_handler got error " << ec;
1766 messages::internalError(asyncResp->res);
1767 return;
1768 }
1769 const uint32_t* id = nullptr;
1770 const uint64_t* timestamp = nullptr;
1771 const uint64_t* updateTimestamp = nullptr;
1772 const std::string* severity = nullptr;
1773 const std::string* message = nullptr;
1774 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001775 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001776 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001777 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001778
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001779 const bool success = sdbusplus::unpackPropertiesNoThrow(
1780 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1781 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001782 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001783 "Resolution", resolution, "Path", filePath,
1784 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001785
1786 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001787 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001788 messages::internalError(asyncResp->res);
1789 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001790 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001791
Ed Tanous002d39b2022-05-31 08:59:27 -07001792 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001793 timestamp == nullptr || updateTimestamp == nullptr ||
1794 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 {
1796 messages::internalError(asyncResp->res);
1797 return;
1798 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001799
Ed Tanous002d39b2022-05-31 08:59:27 -07001800 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001801 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001802 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1803 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1804 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1806 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1807 asyncResp->res.jsonValue["Message"] = *message;
1808 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001809 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1810 if (notifyAction)
1811 {
1812 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1813 *notifyAction;
1814 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001815 if ((resolution != nullptr) && (!(*resolution).empty()))
1816 {
1817 asyncResp->res.jsonValue["Resolution"] = *resolution;
1818 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001819 asyncResp->res.jsonValue["EntryType"] = "Event";
1820 asyncResp->res.jsonValue["Severity"] =
1821 translateSeverityDbusToRedfish(*severity);
1822 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001823 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001824 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001825 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001826 if (filePath != nullptr)
1827 {
1828 asyncResp->res.jsonValue["AdditionalDataURI"] =
1829 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1830 std::to_string(*id) + "/attachment";
1831 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001832 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001833 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001834
1835 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001836 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001837 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001838 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001839 [&app](const crow::Request& req,
1840 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001841 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001842 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001843 {
1844 return;
1845 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001846 if (systemName != "system")
1847 {
1848 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1849 systemName);
1850 return;
1851 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001852 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001853
Ed Tanous002d39b2022-05-31 08:59:27 -07001854 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1855 resolved))
1856 {
1857 return;
1858 }
1859 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001860
Ed Tanous002d39b2022-05-31 08:59:27 -07001861 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001862 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001863 if (ec)
1864 {
1865 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1866 messages::internalError(asyncResp->res);
1867 return;
1868 }
1869 },
1870 "xyz.openbmc_project.Logging",
1871 "/xyz/openbmc_project/logging/entry/" + entryId,
1872 "org.freedesktop.DBus.Properties", "Set",
1873 "xyz.openbmc_project.Logging.Entry", "Resolved",
1874 dbus::utility::DbusVariantType(*resolved));
1875 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001876
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001877 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001878 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001879 .privileges(redfish::privileges::deleteLogEntry)
1880
Ed Tanous002d39b2022-05-31 08:59:27 -07001881 .methods(boost::beast::http::verb::delete_)(
1882 [&app](const crow::Request& req,
1883 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001884 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001885 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001886 {
1887 return;
1888 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001889 if (systemName != "system")
1890 {
1891 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1892 systemName);
1893 return;
1894 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001895 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1896
1897 std::string entryID = param;
1898
1899 dbus::utility::escapePathForDbus(entryID);
1900
1901 // Process response from Logging service.
1902 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001903 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001904 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1905 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001906 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001907 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001908 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 messages::resourceNotFound(asyncResp->res, "LogEntry",
1910 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001911 return;
1912 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001913 // TODO Handle for specific error code
1914 BMCWEB_LOG_ERROR
1915 << "EventLogEntry (DBus) doDelete respHandler got error "
1916 << ec;
1917 asyncResp->res.result(
1918 boost::beast::http::status::internal_server_error);
1919 return;
1920 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001921
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 asyncResp->res.result(boost::beast::http::status::ok);
1923 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001924
Ed Tanous002d39b2022-05-31 08:59:27 -07001925 // Make call to Logging service to request Delete Log
1926 crow::connections::systemBus->async_method_call(
1927 respHandler, "xyz.openbmc_project.Logging",
1928 "/xyz/openbmc_project/logging/entry/" + entryID,
1929 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001930 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001931}
1932
1933inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001934{
George Liu0fda0f12021-11-16 10:06:17 +08001935 BMCWEB_ROUTE(
1936 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001937 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001938 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001939 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001940 [&app](const crow::Request& req,
1941 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001942 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001943 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001944 {
1945 return;
1946 }
Matt Spinler72e21372023-04-19 12:53:33 -05001947 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07001948 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001949 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001950 {
1951 asyncResp->res.result(boost::beast::http::status::bad_request);
1952 return;
1953 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001954 if (systemName != "system")
1955 {
1956 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1957 systemName);
1958 return;
1959 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001960
Ed Tanous002d39b2022-05-31 08:59:27 -07001961 std::string entryID = param;
1962 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001963
Ed Tanous002d39b2022-05-31 08:59:27 -07001964 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001965 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001966 const sdbusplus::message::unix_fd& unixfd) {
1967 if (ec.value() == EBADR)
1968 {
1969 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1970 entryID);
1971 return;
1972 }
1973 if (ec)
1974 {
1975 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1976 messages::internalError(asyncResp->res);
1977 return;
1978 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001979
Ed Tanous002d39b2022-05-31 08:59:27 -07001980 int fd = -1;
1981 fd = dup(unixfd);
1982 if (fd == -1)
1983 {
1984 messages::internalError(asyncResp->res);
1985 return;
1986 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001987
Ed Tanous002d39b2022-05-31 08:59:27 -07001988 long long int size = lseek(fd, 0, SEEK_END);
1989 if (size == -1)
1990 {
1991 messages::internalError(asyncResp->res);
1992 return;
1993 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001994
Ed Tanous002d39b2022-05-31 08:59:27 -07001995 // Arbitrary max size of 64kb
1996 constexpr int maxFileSize = 65536;
1997 if (size > maxFileSize)
1998 {
1999 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
2000 << maxFileSize;
2001 messages::internalError(asyncResp->res);
2002 return;
2003 }
2004 std::vector<char> data(static_cast<size_t>(size));
2005 long long int rc = lseek(fd, 0, SEEK_SET);
2006 if (rc == -1)
2007 {
2008 messages::internalError(asyncResp->res);
2009 return;
2010 }
2011 rc = read(fd, data.data(), data.size());
2012 if ((rc == -1) || (rc != size))
2013 {
2014 messages::internalError(asyncResp->res);
2015 return;
2016 }
2017 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002018
Ed Tanous002d39b2022-05-31 08:59:27 -07002019 std::string_view strData(data.data(), data.size());
2020 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002021
Ed Tanousd9f6c622022-03-17 09:12:17 -07002022 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07002023 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07002024 asyncResp->res.addHeader(
2025 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07002026 asyncResp->res.body() = std::move(output);
2027 },
2028 "xyz.openbmc_project.Logging",
2029 "/xyz/openbmc_project/logging/entry/" + entryID,
2030 "xyz.openbmc_project.Logging.Entry", "GetEntry");
2031 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002032}
2033
Spencer Kub7028eb2021-10-26 15:27:35 +08002034constexpr const char* hostLoggerFolderPath = "/var/log/console";
2035
2036inline bool
2037 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2038 std::vector<std::filesystem::path>& hostLoggerFiles)
2039{
2040 std::error_code ec;
2041 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2042 if (ec)
2043 {
2044 BMCWEB_LOG_ERROR << ec.message();
2045 return false;
2046 }
2047 for (const std::filesystem::directory_entry& it : logPath)
2048 {
2049 std::string filename = it.path().filename();
2050 // Prefix of each log files is "log". Find the file and save the
2051 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002052 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002053 {
2054 hostLoggerFiles.emplace_back(it.path());
2055 }
2056 }
2057 // As the log files rotate, they are appended with a ".#" that is higher for
2058 // the older logs. Since we start from oldest logs, sort the name in
2059 // descending order.
2060 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2061 AlphanumLess<std::string>());
2062
2063 return true;
2064}
2065
Ed Tanous02cad962022-06-30 16:50:15 -07002066inline bool getHostLoggerEntries(
2067 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2068 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002069{
2070 GzFileReader logFile;
2071
2072 // Go though all log files and expose host logs.
2073 for (const std::filesystem::path& it : hostLoggerFiles)
2074 {
2075 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2076 {
2077 BMCWEB_LOG_ERROR << "fail to expose host logs";
2078 return false;
2079 }
2080 }
2081 // Get lastMessage from constructor by getter
2082 std::string lastMessage = logFile.getLastMessage();
2083 if (!lastMessage.empty())
2084 {
2085 logCount++;
2086 if (logCount > skip && logCount <= (skip + top))
2087 {
2088 logEntries.push_back(lastMessage);
2089 }
2090 }
2091 return true;
2092}
2093
2094inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2095 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002096 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002097{
2098 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002099 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002100 logEntryJson["@odata.id"] = boost::urls::format(
2101 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2102 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002103 logEntryJson["Name"] = "Host Logger Entry";
2104 logEntryJson["Id"] = logEntryID;
2105 logEntryJson["Message"] = msg;
2106 logEntryJson["EntryType"] = "Oem";
2107 logEntryJson["Severity"] = "OK";
2108 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002109}
2110
2111inline void requestRoutesSystemHostLogger(App& app)
2112{
Ed Tanous22d268c2022-05-19 09:39:07 -07002113 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002114 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002115 .methods(boost::beast::http::verb::get)(
2116 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002117 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2118 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002119 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002120 {
2121 return;
2122 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002123 if (systemName != "system")
2124 {
2125 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2126 systemName);
2127 return;
2128 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 asyncResp->res.jsonValue["@odata.id"] =
2130 "/redfish/v1/Systems/system/LogServices/HostLogger";
2131 asyncResp->res.jsonValue["@odata.type"] =
2132 "#LogService.v1_1_0.LogService";
2133 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2134 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2135 asyncResp->res.jsonValue["Id"] = "HostLogger";
2136 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2137 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2138 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002139}
2140
2141inline void requestRoutesSystemHostLoggerCollection(App& app)
2142{
2143 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002144 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002145 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 .methods(boost::beast::http::verb::get)(
2147 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002148 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2149 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002150 query_param::QueryCapabilities capabilities = {
2151 .canDelegateTop = true,
2152 .canDelegateSkip = true,
2153 };
2154 query_param::Query delegatedQuery;
2155 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002156 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002157 {
2158 return;
2159 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002160 if (systemName != "system")
2161 {
2162 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2163 systemName);
2164 return;
2165 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002166 asyncResp->res.jsonValue["@odata.id"] =
2167 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2168 asyncResp->res.jsonValue["@odata.type"] =
2169 "#LogEntryCollection.LogEntryCollection";
2170 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2171 asyncResp->res.jsonValue["Description"] =
2172 "Collection of HostLogger Entries";
2173 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2174 logEntryArray = nlohmann::json::array();
2175 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002176
Ed Tanous002d39b2022-05-31 08:59:27 -07002177 std::vector<std::filesystem::path> hostLoggerFiles;
2178 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2179 {
2180 BMCWEB_LOG_ERROR << "fail to get host log file path";
2181 return;
2182 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002183 // If we weren't provided top and skip limits, use the defaults.
2184 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002185 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 size_t logCount = 0;
2187 // This vector only store the entries we want to expose that
2188 // control by skip and top.
2189 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002190 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2191 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002192 {
2193 messages::internalError(asyncResp->res);
2194 return;
2195 }
2196 // If vector is empty, that means skip value larger than total
2197 // log count
2198 if (logEntries.empty())
2199 {
2200 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2201 return;
2202 }
2203 if (!logEntries.empty())
2204 {
2205 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002206 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002207 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002208 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2209 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002210 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002211 }
2212
Ed Tanous002d39b2022-05-31 08:59:27 -07002213 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002214 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002215 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002216 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2217 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002218 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002219 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002220 }
George Liu0fda0f12021-11-16 10:06:17 +08002221 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002222}
2223
2224inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2225{
2226 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002227 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002228 .privileges(redfish::privileges::getLogEntry)
2229 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002230 [&app](const crow::Request& req,
2231 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002232 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002233 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002234 {
2235 return;
2236 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002237 if (systemName != "system")
2238 {
2239 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2240 systemName);
2241 return;
2242 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002243 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002244
Ed Tanous002d39b2022-05-31 08:59:27 -07002245 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002246
Patrick Williams84396af2023-05-11 11:47:45 -05002247 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2248 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002249 if (ec == std::errc::invalid_argument ||
2250 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002251 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002252 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002253 return;
2254 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002255
Ed Tanous002d39b2022-05-31 08:59:27 -07002256 std::vector<std::filesystem::path> hostLoggerFiles;
2257 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2258 {
2259 BMCWEB_LOG_ERROR << "fail to get host log file path";
2260 return;
2261 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002262
Ed Tanous002d39b2022-05-31 08:59:27 -07002263 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002264 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 std::vector<std::string> logEntries;
2266 // We can get specific entry by skip and top. For example, if we
2267 // want to get nth entry, we can set skip = n-1 and top = 1 to
2268 // get that entry
2269 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2270 logCount))
2271 {
2272 messages::internalError(asyncResp->res);
2273 return;
2274 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002275
Ed Tanous002d39b2022-05-31 08:59:27 -07002276 if (!logEntries.empty())
2277 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002278 nlohmann::json::object_t hostLogEntry;
2279 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2280 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002281 return;
2282 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002283
Ed Tanous002d39b2022-05-31 08:59:27 -07002284 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002285 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002286 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002287}
2288
Claire Weinandd72e872022-08-15 14:20:06 -07002289inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002290 crow::App& app, const crow::Request& req,
2291 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2292{
2293 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2294 {
2295 return;
2296 }
2297 // Collections don't include the static data added by SubRoute
2298 // because it has a duplicate entry for members
2299 asyncResp->res.jsonValue["@odata.type"] =
2300 "#LogServiceCollection.LogServiceCollection";
2301 asyncResp->res.jsonValue["@odata.id"] =
2302 "/redfish/v1/Managers/bmc/LogServices";
2303 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2304 asyncResp->res.jsonValue["Description"] =
2305 "Collection of LogServices for this Manager";
2306 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2307 logServiceArray = nlohmann::json::array();
2308
2309#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002310 nlohmann::json::object_t journal;
2311 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002312 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002313#endif
2314
2315 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2316
2317#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002318 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002319 "xyz.openbmc_project.Collection.DeleteAll"};
2320 dbus::utility::getSubTreePaths(
2321 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002322 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002323 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002324 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2325 if (ec)
2326 {
2327 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002328 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002329 << ec;
2330 // Assume that getting an error simply means there are no dump
2331 // LogServices. Return without adding any error response.
2332 return;
2333 }
2334
2335 nlohmann::json& logServiceArrayLocal =
2336 asyncResp->res.jsonValue["Members"];
2337
2338 for (const std::string& path : subTreePaths)
2339 {
2340 if (path == "/xyz/openbmc_project/dump/bmc")
2341 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002342 nlohmann::json::object_t member;
2343 member["@odata.id"] =
2344 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002345 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002346 }
2347 else if (path == "/xyz/openbmc_project/dump/faultlog")
2348 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002349 nlohmann::json::object_t member;
2350 member["@odata.id"] =
2351 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002352 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002353 }
2354 }
2355
2356 asyncResp->res.jsonValue["Members@odata.count"] =
2357 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002358 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002359#endif
2360}
2361
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002362inline void requestRoutesBMCLogServiceCollection(App& app)
2363{
2364 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002365 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002366 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002367 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002368}
Ed Tanous1da66f72018-07-27 16:13:37 -07002369
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002370inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002371{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002372 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002373 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002374 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002375 [&app](const crow::Request& req,
2376 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002377 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002378 {
2379 return;
2380 }
2381 asyncResp->res.jsonValue["@odata.type"] =
2382 "#LogService.v1_1_0.LogService";
2383 asyncResp->res.jsonValue["@odata.id"] =
2384 "/redfish/v1/Managers/bmc/LogServices/Journal";
2385 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2386 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002387 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002388 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302389
Ed Tanous002d39b2022-05-31 08:59:27 -07002390 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002391 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002392 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2393 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2394 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302395
Ed Tanous002d39b2022-05-31 08:59:27 -07002396 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2397 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2398 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002399}
Jason M. Billse1f26342018-07-18 12:12:00 -07002400
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002401static int
2402 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2403 sd_journal* journal,
2404 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002405{
2406 // Get the Log Entry contents
2407 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002408
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002409 std::string message;
2410 std::string_view syslogID;
2411 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2412 if (ret < 0)
2413 {
2414 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2415 << strerror(-ret);
2416 }
2417 if (!syslogID.empty())
2418 {
2419 message += std::string(syslogID) + ": ";
2420 }
2421
Ed Tanous39e77502019-03-04 17:35:53 -08002422 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002423 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002424 if (ret < 0)
2425 {
2426 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2427 return 1;
2428 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002429 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002430
2431 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002432 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002433 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002434 if (ret < 0)
2435 {
2436 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002437 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002438
2439 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002440 std::string entryTimeStr;
2441 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002442 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002443 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002444 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002445
2446 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002447 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002448 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2449 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002450 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002451 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2452 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2453 bmcJournalLogEntryJson["Message"] = std::move(message);
2454 bmcJournalLogEntryJson["EntryType"] = "Oem";
2455 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2456 : severity <= 4 ? "Warning"
2457 : "OK";
2458 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2459 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002460 return 0;
2461}
2462
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002463inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002464{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002465 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002466 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002467 .methods(boost::beast::http::verb::get)(
2468 [&app](const crow::Request& req,
2469 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2470 query_param::QueryCapabilities capabilities = {
2471 .canDelegateTop = true,
2472 .canDelegateSkip = true,
2473 };
2474 query_param::Query delegatedQuery;
2475 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002476 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002477 {
2478 return;
2479 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002480
2481 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002482 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002483
Ed Tanous002d39b2022-05-31 08:59:27 -07002484 // Collections don't include the static data added by SubRoute
2485 // because it has a duplicate entry for members
2486 asyncResp->res.jsonValue["@odata.type"] =
2487 "#LogEntryCollection.LogEntryCollection";
2488 asyncResp->res.jsonValue["@odata.id"] =
2489 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2490 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2491 asyncResp->res.jsonValue["Description"] =
2492 "Collection of BMC Journal Entries";
2493 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2494 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002495
Ed Tanous002d39b2022-05-31 08:59:27 -07002496 // Go through the journal and use the timestamp to create a
2497 // unique ID for each entry
2498 sd_journal* journalTmp = nullptr;
2499 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2500 if (ret < 0)
2501 {
2502 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2503 messages::internalError(asyncResp->res);
2504 return;
2505 }
2506 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2507 journalTmp, sd_journal_close);
2508 journalTmp = nullptr;
2509 uint64_t entryCount = 0;
2510 // Reset the unique ID on the first entry
2511 bool firstEntry = true;
2512 SD_JOURNAL_FOREACH(journal.get())
2513 {
2514 entryCount++;
2515 // Handle paging using skip (number of entries to skip from
2516 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002517 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002518 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002519 continue;
2520 }
2521
2522 std::string idStr;
2523 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2524 {
2525 continue;
2526 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002527 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002528
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002529 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002530 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2531 bmcJournalLogEntry) != 0)
2532 {
George Liu0fda0f12021-11-16 10:06:17 +08002533 messages::internalError(asyncResp->res);
2534 return;
2535 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002536 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002537 }
2538 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002539 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002540 {
2541 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2542 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002543 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002544 }
George Liu0fda0f12021-11-16 10:06:17 +08002545 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002546}
Jason M. Billse1f26342018-07-18 12:12:00 -07002547
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002548inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002549{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002550 BMCWEB_ROUTE(app,
2551 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002552 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002553 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002554 [&app](const crow::Request& req,
2555 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2556 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002557 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002558 {
2559 return;
2560 }
2561 // Convert the unique ID back to a timestamp to find the entry
2562 uint64_t ts = 0;
2563 uint64_t index = 0;
2564 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2565 {
2566 return;
2567 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002568
Ed Tanous002d39b2022-05-31 08:59:27 -07002569 sd_journal* journalTmp = nullptr;
2570 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2571 if (ret < 0)
2572 {
2573 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2574 messages::internalError(asyncResp->res);
2575 return;
2576 }
2577 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2578 journalTmp, sd_journal_close);
2579 journalTmp = nullptr;
2580 // Go to the timestamp in the log and move to the entry at the
2581 // index tracking the unique ID
2582 std::string idStr;
2583 bool firstEntry = true;
2584 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2585 if (ret < 0)
2586 {
2587 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2588 << strerror(-ret);
2589 messages::internalError(asyncResp->res);
2590 return;
2591 }
2592 for (uint64_t i = 0; i <= index; i++)
2593 {
2594 sd_journal_next(journal.get());
2595 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2596 {
2597 messages::internalError(asyncResp->res);
2598 return;
2599 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002600 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002601 }
2602 // Confirm that the entry ID matches what was requested
2603 if (idStr != entryID)
2604 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002605 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002606 return;
2607 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002608
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002609 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002610 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002611 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002612 {
2613 messages::internalError(asyncResp->res);
2614 return;
2615 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002616 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002617 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002618}
2619
Claire Weinanfdd26902022-03-01 14:18:25 -08002620inline void
2621 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2622 const std::string& dumpType)
2623{
2624 std::string dumpPath;
2625 std::string overWritePolicy;
2626 bool collectDiagnosticDataSupported = false;
2627
2628 if (dumpType == "BMC")
2629 {
2630 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2631 overWritePolicy = "WrapsWhenFull";
2632 collectDiagnosticDataSupported = true;
2633 }
2634 else if (dumpType == "FaultLog")
2635 {
2636 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2637 overWritePolicy = "Unknown";
2638 collectDiagnosticDataSupported = false;
2639 }
2640 else if (dumpType == "System")
2641 {
2642 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2643 overWritePolicy = "WrapsWhenFull";
2644 collectDiagnosticDataSupported = true;
2645 }
2646 else
2647 {
2648 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2649 << dumpType;
2650 messages::internalError(asyncResp->res);
2651 return;
2652 }
2653
2654 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2655 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2656 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2657 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2658 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2659 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2660
2661 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002662 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002663 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2664 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2665 redfishDateTimeOffset.second;
2666
2667 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002668
2669 if (collectDiagnosticDataSupported)
2670 {
2671 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2672 ["target"] =
2673 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2674 }
Claire Weinan0d946212022-07-13 19:40:19 -07002675
2676 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2677 dbus::utility::getSubTreePaths(
2678 "/xyz/openbmc_project/dump", 0, interfaces,
2679 [asyncResp, dumpType, dumpPath](
2680 const boost::system::error_code& ec,
2681 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2682 if (ec)
2683 {
2684 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2685 << ec;
2686 // Assume that getting an error simply means there are no dump
2687 // LogServices. Return without adding any error response.
2688 return;
2689 }
2690
2691 const std::string dbusDumpPath =
2692 "/xyz/openbmc_project/dump/" +
2693 boost::algorithm::to_lower_copy(dumpType);
2694
2695 for (const std::string& path : subTreePaths)
2696 {
2697 if (path == dbusDumpPath)
2698 {
2699 asyncResp->res
2700 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2701 dumpPath + "/Actions/LogService.ClearLog";
2702 break;
2703 }
2704 }
2705 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002706}
2707
2708inline void handleLogServicesDumpServiceGet(
2709 crow::App& app, const std::string& dumpType, const crow::Request& req,
2710 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2711{
2712 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2713 {
2714 return;
2715 }
2716 getDumpServiceInfo(asyncResp, dumpType);
2717}
2718
Ed Tanous22d268c2022-05-19 09:39:07 -07002719inline void handleLogServicesDumpServiceComputerSystemGet(
2720 crow::App& app, const crow::Request& req,
2721 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2722 const std::string& chassisId)
2723{
2724 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2725 {
2726 return;
2727 }
2728 if (chassisId != "system")
2729 {
2730 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2731 return;
2732 }
2733 getDumpServiceInfo(asyncResp, "System");
2734}
2735
Claire Weinanfdd26902022-03-01 14:18:25 -08002736inline void handleLogServicesDumpEntriesCollectionGet(
2737 crow::App& app, const std::string& dumpType, const crow::Request& req,
2738 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2739{
2740 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2741 {
2742 return;
2743 }
2744 getDumpEntryCollection(asyncResp, dumpType);
2745}
2746
Ed Tanous22d268c2022-05-19 09:39:07 -07002747inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2748 crow::App& app, const crow::Request& req,
2749 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2750 const std::string& chassisId)
2751{
2752 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2753 {
2754 return;
2755 }
2756 if (chassisId != "system")
2757 {
2758 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2759 return;
2760 }
2761 getDumpEntryCollection(asyncResp, "System");
2762}
2763
Claire Weinanfdd26902022-03-01 14:18:25 -08002764inline void handleLogServicesDumpEntryGet(
2765 crow::App& app, const std::string& dumpType, const crow::Request& req,
2766 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2767 const std::string& dumpId)
2768{
2769 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2770 {
2771 return;
2772 }
2773 getDumpEntryById(asyncResp, dumpId, dumpType);
2774}
Ed Tanous22d268c2022-05-19 09:39:07 -07002775inline void handleLogServicesDumpEntryComputerSystemGet(
2776 crow::App& app, const crow::Request& req,
2777 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2778 const std::string& chassisId, const std::string& dumpId)
2779{
2780 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2781 {
2782 return;
2783 }
2784 if (chassisId != "system")
2785 {
2786 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2787 return;
2788 }
2789 getDumpEntryById(asyncResp, dumpId, "System");
2790}
Claire Weinanfdd26902022-03-01 14:18:25 -08002791
2792inline void handleLogServicesDumpEntryDelete(
2793 crow::App& app, const std::string& dumpType, const crow::Request& req,
2794 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2795 const std::string& dumpId)
2796{
2797 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2798 {
2799 return;
2800 }
2801 deleteDumpEntry(asyncResp, dumpId, dumpType);
2802}
2803
Ed Tanous22d268c2022-05-19 09:39:07 -07002804inline void handleLogServicesDumpEntryComputerSystemDelete(
2805 crow::App& app, const crow::Request& req,
2806 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2807 const std::string& chassisId, const std::string& dumpId)
2808{
2809 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2810 {
2811 return;
2812 }
2813 if (chassisId != "system")
2814 {
2815 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2816 return;
2817 }
2818 deleteDumpEntry(asyncResp, dumpId, "System");
2819}
2820
Claire Weinanfdd26902022-03-01 14:18:25 -08002821inline void handleLogServicesDumpCollectDiagnosticDataPost(
2822 crow::App& app, const std::string& dumpType, const crow::Request& req,
2823 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2824{
2825 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2826 {
2827 return;
2828 }
2829 createDump(asyncResp, req, dumpType);
2830}
2831
Ed Tanous22d268c2022-05-19 09:39:07 -07002832inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2833 crow::App& app, const crow::Request& req,
2834 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2835 const std::string& chassisId)
2836{
2837 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2838 {
2839 return;
2840 }
2841 if (chassisId != "system")
2842 {
2843 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2844 return;
2845 }
2846 createDump(asyncResp, req, "System");
2847}
2848
Claire Weinanfdd26902022-03-01 14:18:25 -08002849inline void handleLogServicesDumpClearLogPost(
2850 crow::App& app, const std::string& dumpType, const crow::Request& req,
2851 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2852{
2853 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2854 {
2855 return;
2856 }
2857 clearDump(asyncResp, dumpType);
2858}
2859
Ed Tanous22d268c2022-05-19 09:39:07 -07002860inline void handleLogServicesDumpClearLogComputerSystemPost(
2861 crow::App& app, const crow::Request& req,
2862 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2863 const std::string& chassisId)
2864{
2865 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2866 {
2867 return;
2868 }
2869 if (chassisId != "system")
2870 {
2871 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2872 return;
2873 }
2874 clearDump(asyncResp, "System");
2875}
2876
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002877inline void requestRoutesBMCDumpService(App& app)
2878{
2879 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002880 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002881 .methods(boost::beast::http::verb::get)(std::bind_front(
2882 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002883}
2884
2885inline void requestRoutesBMCDumpEntryCollection(App& app)
2886{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002887 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002888 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002889 .methods(boost::beast::http::verb::get)(std::bind_front(
2890 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002891}
2892
2893inline void requestRoutesBMCDumpEntry(App& app)
2894{
2895 BMCWEB_ROUTE(app,
2896 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002897 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002898 .methods(boost::beast::http::verb::get)(std::bind_front(
2899 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2900
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002901 BMCWEB_ROUTE(app,
2902 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002903 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002904 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2905 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002906}
2907
2908inline void requestRoutesBMCDumpCreate(App& app)
2909{
George Liu0fda0f12021-11-16 10:06:17 +08002910 BMCWEB_ROUTE(
2911 app,
2912 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002913 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002914 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002915 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2916 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002917}
2918
2919inline void requestRoutesBMCDumpClear(App& app)
2920{
George Liu0fda0f12021-11-16 10:06:17 +08002921 BMCWEB_ROUTE(
2922 app,
2923 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002924 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002925 .methods(boost::beast::http::verb::post)(std::bind_front(
2926 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2927}
2928
2929inline void requestRoutesFaultLogDumpService(App& app)
2930{
2931 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2932 .privileges(redfish::privileges::getLogService)
2933 .methods(boost::beast::http::verb::get)(std::bind_front(
2934 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2935}
2936
2937inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2938{
2939 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2940 .privileges(redfish::privileges::getLogEntryCollection)
2941 .methods(boost::beast::http::verb::get)(
2942 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2943 std::ref(app), "FaultLog"));
2944}
2945
2946inline void requestRoutesFaultLogDumpEntry(App& app)
2947{
2948 BMCWEB_ROUTE(app,
2949 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2950 .privileges(redfish::privileges::getLogEntry)
2951 .methods(boost::beast::http::verb::get)(std::bind_front(
2952 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2953
2954 BMCWEB_ROUTE(app,
2955 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2956 .privileges(redfish::privileges::deleteLogEntry)
2957 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2958 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2959}
2960
2961inline void requestRoutesFaultLogDumpClear(App& app)
2962{
2963 BMCWEB_ROUTE(
2964 app,
2965 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2966 .privileges(redfish::privileges::postLogService)
2967 .methods(boost::beast::http::verb::post)(std::bind_front(
2968 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002969}
2970
2971inline void requestRoutesSystemDumpService(App& app)
2972{
Ed Tanous22d268c2022-05-19 09:39:07 -07002973 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002974 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002975 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002976 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002977}
2978
2979inline void requestRoutesSystemDumpEntryCollection(App& app)
2980{
Ed Tanous22d268c2022-05-19 09:39:07 -07002981 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002982 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002983 .methods(boost::beast::http::verb::get)(std::bind_front(
2984 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2985 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002986}
2987
2988inline void requestRoutesSystemDumpEntry(App& app)
2989{
2990 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002991 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002992 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002993 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002994 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002995
2996 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002997 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002998 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002999 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003000 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003001}
3002
3003inline void requestRoutesSystemDumpCreate(App& app)
3004{
George Liu0fda0f12021-11-16 10:06:17 +08003005 BMCWEB_ROUTE(
3006 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003007 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003008 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003009 .methods(boost::beast::http::verb::post)(std::bind_front(
3010 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3011 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003012}
3013
3014inline void requestRoutesSystemDumpClear(App& app)
3015{
George Liu0fda0f12021-11-16 10:06:17 +08003016 BMCWEB_ROUTE(
3017 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003018 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003019 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003020 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003021 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003022}
3023
3024inline void requestRoutesCrashdumpService(App& app)
3025{
3026 // Note: Deviated from redfish privilege registry for GET & HEAD
3027 // method for security reasons.
3028 /**
3029 * Functions triggers appropriate requests on DBus
3030 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003031 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003032 // This is incorrect, should be:
3033 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003034 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003035 .methods(boost::beast::http::verb::get)(
3036 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003037 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3038 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003039 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003040 {
3041 return;
3042 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003043 if (systemName != "system")
3044 {
3045 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3046 systemName);
3047 return;
3048 }
3049
Ed Tanous002d39b2022-05-31 08:59:27 -07003050 // Copy over the static data to include the entries added by
3051 // SubRoute
3052 asyncResp->res.jsonValue["@odata.id"] =
3053 "/redfish/v1/Systems/system/LogServices/Crashdump";
3054 asyncResp->res.jsonValue["@odata.type"] =
3055 "#LogService.v1_2_0.LogService";
3056 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3057 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303058 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003059 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3060 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303061
Ed Tanous002d39b2022-05-31 08:59:27 -07003062 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003063 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003064 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3065 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3066 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303067
Ed Tanous002d39b2022-05-31 08:59:27 -07003068 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003069 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003070 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3071 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3072 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3073 ["target"] =
3074 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003075 });
3076}
3077
3078void inline requestRoutesCrashdumpClear(App& app)
3079{
George Liu0fda0f12021-11-16 10:06:17 +08003080 BMCWEB_ROUTE(
3081 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003082 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003083 // This is incorrect, should be:
3084 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003085 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003086 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003087 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003088 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3089 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003090 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003091 {
3092 return;
3093 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003094 if (systemName != "system")
3095 {
3096 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3097 systemName);
3098 return;
3099 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003100 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003101 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003102 const std::string&) {
3103 if (ec)
3104 {
3105 messages::internalError(asyncResp->res);
3106 return;
3107 }
3108 messages::success(asyncResp->res);
3109 },
3110 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3111 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003112}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003113
zhanghch058d1b46d2021-04-01 11:18:24 +08003114static void
3115 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3116 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003117{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003118 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003119 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003120 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003121 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003122 if (ec)
3123 {
3124 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3125 if (ec.value() ==
3126 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003127 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003128 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003129 }
3130 else
3131 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003132 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003133 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003134 return;
3135 }
3136
3137 std::string timestamp{};
3138 std::string filename{};
3139 std::string logfile{};
3140 parseCrashdumpParameters(params, filename, timestamp, logfile);
3141
3142 if (filename.empty() || timestamp.empty())
3143 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003144 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003145 return;
3146 }
3147
3148 std::string crashdumpURI =
3149 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3150 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003151 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003152 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003153 logEntry["@odata.id"] = boost::urls::format(
3154 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3155 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003156 logEntry["Name"] = "CPU Crashdump";
3157 logEntry["Id"] = logID;
3158 logEntry["EntryType"] = "Oem";
3159 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3160 logEntry["DiagnosticDataType"] = "OEM";
3161 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3162 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003163
3164 // If logEntryJson references an array of LogEntry resources
3165 // ('Members' list), then push this as a new entry, otherwise set it
3166 // directly
3167 if (logEntryJson.is_array())
3168 {
3169 logEntryJson.push_back(logEntry);
3170 asyncResp->res.jsonValue["Members@odata.count"] =
3171 logEntryJson.size();
3172 }
3173 else
3174 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003175 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003176 }
3177 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003178 sdbusplus::asio::getAllProperties(
3179 *crow::connections::systemBus, crashdumpObject,
3180 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3181 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003182}
3183
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003184inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003185{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003186 // Note: Deviated from redfish privilege registry for GET & HEAD
3187 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003188 /**
3189 * Functions triggers appropriate requests on DBus
3190 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003191 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003192 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003193 // This is incorrect, should be.
3194 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003195 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003196 .methods(boost::beast::http::verb::get)(
3197 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003198 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3199 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003200 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003201 {
3202 return;
3203 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003204 if (systemName != "system")
3205 {
3206 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3207 systemName);
3208 return;
3209 }
3210
George Liu7a1dbc42022-12-07 16:03:22 +08003211 constexpr std::array<std::string_view, 1> interfaces = {
3212 crashdumpInterface};
3213 dbus::utility::getSubTreePaths(
3214 "/", 0, interfaces,
3215 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003216 const std::vector<std::string>& resp) {
3217 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003218 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003219 if (ec.value() !=
3220 boost::system::errc::no_such_file_or_directory)
3221 {
3222 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3223 << ec.message();
3224 messages::internalError(asyncResp->res);
3225 return;
3226 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003227 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003228 asyncResp->res.jsonValue["@odata.type"] =
3229 "#LogEntryCollection.LogEntryCollection";
3230 asyncResp->res.jsonValue["@odata.id"] =
3231 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3232 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3233 asyncResp->res.jsonValue["Description"] =
3234 "Collection of Crashdump Entries";
3235 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3236 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003237
Ed Tanous002d39b2022-05-31 08:59:27 -07003238 for (const std::string& path : resp)
3239 {
3240 const sdbusplus::message::object_path objPath(path);
3241 // Get the log ID
3242 std::string logID = objPath.filename();
3243 if (logID.empty())
3244 {
3245 continue;
3246 }
3247 // Add the log entry to the array
3248 logCrashdumpEntry(asyncResp, logID,
3249 asyncResp->res.jsonValue["Members"]);
3250 }
George Liu7a1dbc42022-12-07 16:03:22 +08003251 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003252 });
3253}
Ed Tanous1da66f72018-07-27 16:13:37 -07003254
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003255inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003256{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003257 // Note: Deviated from redfish privilege registry for GET & HEAD
3258 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003259
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003260 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003261 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003262 // this is incorrect, should be
3263 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003264 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003265 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003266 [&app](const crow::Request& req,
3267 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003268 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003269 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003270 {
3271 return;
3272 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003273 if (systemName != "system")
3274 {
3275 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3276 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003277 return;
3278 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003279 const std::string& logID = param;
3280 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3281 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003282}
Ed Tanous1da66f72018-07-27 16:13:37 -07003283
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003284inline void requestRoutesCrashdumpFile(App& app)
3285{
3286 // Note: Deviated from redfish privilege registry for GET & HEAD
3287 // method for security reasons.
3288 BMCWEB_ROUTE(
3289 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003290 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003291 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003292 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003293 [](const crow::Request& req,
3294 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003295 const std::string& systemName, const std::string& logID,
3296 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003297 // Do not call getRedfishRoute here since the crashdump file is not a
3298 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003299
3300 if (systemName != "system")
3301 {
3302 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3303 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003304 return;
3305 }
3306
Ed Tanous002d39b2022-05-31 08:59:27 -07003307 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003308 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003309 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003310 const std::vector<
3311 std::pair<std::string, dbus::utility::DbusVariantType>>&
3312 resp) {
3313 if (ec)
3314 {
3315 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3316 messages::internalError(asyncResp->res);
3317 return;
3318 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003319
Ed Tanous002d39b2022-05-31 08:59:27 -07003320 std::string dbusFilename{};
3321 std::string dbusTimestamp{};
3322 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003323
Ed Tanous002d39b2022-05-31 08:59:27 -07003324 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3325 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003326
Ed Tanous002d39b2022-05-31 08:59:27 -07003327 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3328 dbusFilepath.empty())
3329 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003330 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003331 return;
3332 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003333
Ed Tanous002d39b2022-05-31 08:59:27 -07003334 // Verify the file name parameter is correct
3335 if (fileName != dbusFilename)
3336 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003337 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003338 return;
3339 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003340
Ed Tanous002d39b2022-05-31 08:59:27 -07003341 if (!std::filesystem::exists(dbusFilepath))
3342 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003343 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003344 return;
3345 }
3346 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3347 asyncResp->res.body() =
3348 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003349
Ed Tanous002d39b2022-05-31 08:59:27 -07003350 // Configure this to be a file download when accessed
3351 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003352 asyncResp->res.addHeader(
3353 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003354 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003355 sdbusplus::asio::getAllProperties(
3356 *crow::connections::systemBus, crashdumpObject,
3357 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3358 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003359 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003360}
3361
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003362enum class OEMDiagnosticType
3363{
3364 onDemand,
3365 telemetry,
3366 invalid,
3367};
3368
Ed Tanous26ccae32023-02-16 10:28:44 -08003369inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003370{
3371 if (oemDiagStr == "OnDemand")
3372 {
3373 return OEMDiagnosticType::onDemand;
3374 }
3375 if (oemDiagStr == "Telemetry")
3376 {
3377 return OEMDiagnosticType::telemetry;
3378 }
3379
3380 return OEMDiagnosticType::invalid;
3381}
3382
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003383inline void requestRoutesCrashdumpCollect(App& app)
3384{
3385 // Note: Deviated from redfish privilege registry for GET & HEAD
3386 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003387 BMCWEB_ROUTE(
3388 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003389 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003390 // The below is incorrect; Should be ConfigureManager
3391 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003392 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003393 .methods(boost::beast::http::verb::post)(
3394 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003395 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3396 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003397 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003398 {
3399 return;
3400 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003401
3402 if (systemName != "system")
3403 {
3404 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3405 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003406 return;
3407 }
3408
Ed Tanous002d39b2022-05-31 08:59:27 -07003409 std::string diagnosticDataType;
3410 std::string oemDiagnosticDataType;
3411 if (!redfish::json_util::readJsonAction(
3412 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3413 "OEMDiagnosticDataType", oemDiagnosticDataType))
3414 {
3415 return;
3416 }
3417
3418 if (diagnosticDataType != "OEM")
3419 {
3420 BMCWEB_LOG_ERROR
3421 << "Only OEM DiagnosticDataType supported for Crashdump";
3422 messages::actionParameterValueFormatError(
3423 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3424 "CollectDiagnosticData");
3425 return;
3426 }
3427
3428 OEMDiagnosticType oemDiagType =
3429 getOEMDiagnosticType(oemDiagnosticDataType);
3430
3431 std::string iface;
3432 std::string method;
3433 std::string taskMatchStr;
3434 if (oemDiagType == OEMDiagnosticType::onDemand)
3435 {
3436 iface = crashdumpOnDemandInterface;
3437 method = "GenerateOnDemandLog";
3438 taskMatchStr = "type='signal',"
3439 "interface='org.freedesktop.DBus.Properties',"
3440 "member='PropertiesChanged',"
3441 "arg0namespace='com.intel.crashdump'";
3442 }
3443 else if (oemDiagType == OEMDiagnosticType::telemetry)
3444 {
3445 iface = crashdumpTelemetryInterface;
3446 method = "GenerateTelemetryLog";
3447 taskMatchStr = "type='signal',"
3448 "interface='org.freedesktop.DBus.Properties',"
3449 "member='PropertiesChanged',"
3450 "arg0namespace='com.intel.crashdump'";
3451 }
3452 else
3453 {
3454 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3455 << oemDiagnosticDataType;
3456 messages::actionParameterValueFormatError(
3457 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3458 "CollectDiagnosticData");
3459 return;
3460 }
3461
3462 auto collectCrashdumpCallback =
3463 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003464 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003465 const std::string&) mutable {
3466 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003467 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003468 if (ec.value() == boost::system::errc::operation_not_supported)
3469 {
3470 messages::resourceInStandby(asyncResp->res);
3471 }
3472 else if (ec.value() ==
3473 boost::system::errc::device_or_resource_busy)
3474 {
3475 messages::serviceTemporarilyUnavailable(asyncResp->res,
3476 "60");
3477 }
3478 else
3479 {
3480 messages::internalError(asyncResp->res);
3481 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003482 return;
3483 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003484 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003485 [](const boost::system::error_code& err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003486 const std::shared_ptr<task::TaskData>& taskData) {
3487 if (!err)
3488 {
3489 taskData->messages.emplace_back(messages::taskCompletedOK(
3490 std::to_string(taskData->index)));
3491 taskData->state = "Completed";
3492 }
3493 return task::completed;
3494 },
3495 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003496
Ed Tanous002d39b2022-05-31 08:59:27 -07003497 task->startTimer(std::chrono::minutes(5));
3498 task->populateResp(asyncResp->res);
3499 task->payload.emplace(std::move(payload));
3500 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003501
Ed Tanous002d39b2022-05-31 08:59:27 -07003502 crow::connections::systemBus->async_method_call(
3503 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3504 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003505 });
3506}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003507
Andrew Geisslercb92c032018-08-17 07:56:14 -07003508/**
3509 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3510 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003511inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003512{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003513 /**
3514 * Function handles POST method request.
3515 * The Clear Log actions does not require any parameter.The action deletes
3516 * all entries found in the Entries collection for this Log Service.
3517 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003518
George Liu0fda0f12021-11-16 10:06:17 +08003519 BMCWEB_ROUTE(
3520 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003521 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003522 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003523 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003524 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003525 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3526 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003527 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003528 {
3529 return;
3530 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003531 if (systemName != "system")
3532 {
3533 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3534 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003535 return;
3536 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003537 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003538
Ed Tanous002d39b2022-05-31 08:59:27 -07003539 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003540 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003541 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3542 if (ec)
3543 {
3544 // TODO Handle for specific error code
3545 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3546 asyncResp->res.result(
3547 boost::beast::http::status::internal_server_error);
3548 return;
3549 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003550
Ed Tanous002d39b2022-05-31 08:59:27 -07003551 asyncResp->res.result(boost::beast::http::status::no_content);
3552 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003553
Ed Tanous002d39b2022-05-31 08:59:27 -07003554 // Make call to Logging service to request Clear Log
3555 crow::connections::systemBus->async_method_call(
3556 respHandler, "xyz.openbmc_project.Logging",
3557 "/xyz/openbmc_project/logging",
3558 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3559 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003560}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003561
3562/****************************************************
3563 * Redfish PostCode interfaces
3564 * using DBUS interface: getPostCodesTS
3565 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003566inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003567{
Ed Tanous22d268c2022-05-19 09:39:07 -07003568 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003569 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003570 .methods(boost::beast::http::verb::get)(
3571 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003572 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3573 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003574 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003575 {
3576 return;
3577 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003578 if (systemName != "system")
3579 {
3580 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3581 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003582 return;
3583 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003584 asyncResp->res.jsonValue["@odata.id"] =
3585 "/redfish/v1/Systems/system/LogServices/PostCodes";
3586 asyncResp->res.jsonValue["@odata.type"] =
3587 "#LogService.v1_1_0.LogService";
3588 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3589 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003590 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003591 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3592 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3593 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303594
Ed Tanous002d39b2022-05-31 08:59:27 -07003595 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003596 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003597 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3598 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3599 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303600
Ed Tanous002d39b2022-05-31 08:59:27 -07003601 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3602 {"target",
3603 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003604 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003605}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003606
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003607inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003608{
George Liu0fda0f12021-11-16 10:06:17 +08003609 BMCWEB_ROUTE(
3610 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003611 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003612 // The following privilege is incorrect; It should be ConfigureManager
3613 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003614 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003615 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003616 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003617 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3618 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003619 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003620 {
3621 return;
3622 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003623 if (systemName != "system")
3624 {
3625 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3626 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003627 return;
3628 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003629 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003630
Ed Tanous002d39b2022-05-31 08:59:27 -07003631 // Make call to post-code service to request clear all
3632 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003633 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003634 if (ec)
3635 {
3636 // TODO Handle for specific error code
3637 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3638 << ec;
3639 asyncResp->res.result(
3640 boost::beast::http::status::internal_server_error);
3641 messages::internalError(asyncResp->res);
3642 return;
3643 }
3644 },
3645 "xyz.openbmc_project.State.Boot.PostCode0",
3646 "/xyz/openbmc_project/State/Boot/PostCode0",
3647 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3648 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003649}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003650
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003651/**
3652 * @brief Parse post code ID and get the current value and index value
3653 * eg: postCodeID=B1-2, currentValue=1, index=2
3654 *
3655 * @param[in] postCodeID Post Code ID
3656 * @param[out] currentValue Current value
3657 * @param[out] index Index value
3658 *
3659 * @return bool true if the parsing is successful, false the parsing fails
3660 */
3661inline static bool parsePostCode(const std::string& postCodeID,
3662 uint64_t& currentValue, uint16_t& index)
3663{
3664 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003665 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003666 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3667 {
3668 return false;
3669 }
3670
Patrick Williams84396af2023-05-11 11:47:45 -05003671 auto start = std::next(split[0].begin());
3672 auto end = split[0].end();
3673 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003674
Patrick Williams84396af2023-05-11 11:47:45 -05003675 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003676 {
3677 return false;
3678 }
3679
Patrick Williams84396af2023-05-11 11:47:45 -05003680 start = split[1].begin();
3681 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003682
Patrick Williams84396af2023-05-11 11:47:45 -05003683 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003684
Patrick Williams84396af2023-05-11 11:47:45 -05003685 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003686}
3687
3688static bool fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003689 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303690 const boost::container::flat_map<
3691 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003692 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3693 const uint64_t skip = 0, const uint64_t top = 0)
3694{
3695 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003696 const registries::Message* message =
3697 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003698
3699 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003700 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303701 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3702 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003703 {
3704 currentCodeIndex++;
3705 std::string postcodeEntryID =
3706 "B" + std::to_string(bootIndex) + "-" +
3707 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3708
3709 uint64_t usecSinceEpoch = code.first;
3710 uint64_t usTimeOffset = 0;
3711
3712 if (1 == currentCodeIndex)
3713 { // already incremented
3714 firstCodeTimeUs = code.first;
3715 }
3716 else
3717 {
3718 usTimeOffset = code.first - firstCodeTimeUs;
3719 }
3720
3721 // skip if no specific codeIndex is specified and currentCodeIndex does
3722 // not fall between top and skip
3723 if ((codeIndex == 0) &&
3724 (currentCodeIndex <= skip || currentCodeIndex > top))
3725 {
3726 continue;
3727 }
3728
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003729 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003730 // currentIndex
3731 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3732 {
3733 // This is done for simplicity. 1st entry is needed to calculate
3734 // time offset. To improve efficiency, one can get to the entry
3735 // directly (possibly with flatmap's nth method)
3736 continue;
3737 }
3738
3739 // currentCodeIndex is within top and skip or equal to specified code
3740 // index
3741
3742 // Get the Created time from the timestamp
3743 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003744 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003745
3746 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3747 std::ostringstream hexCode;
3748 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303749 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003750 std::ostringstream timeOffsetStr;
3751 // Set Fixed -Point Notation
3752 timeOffsetStr << std::fixed;
3753 // Set precision to 4 digits
3754 timeOffsetStr << std::setprecision(4);
3755 // Add double to stream
3756 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3757 std::vector<std::string> messageArgs = {
3758 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3759
3760 // Get MessageArgs template from message registry
3761 std::string msg;
3762 if (message != nullptr)
3763 {
3764 msg = message->message;
3765
3766 // fill in this post code value
3767 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003768 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003769 {
3770 std::string argStr = "%" + std::to_string(++i);
3771 size_t argPos = msg.find(argStr);
3772 if (argPos != std::string::npos)
3773 {
3774 msg.replace(argPos, argStr.length(), messageArg);
3775 }
3776 }
3777 }
3778
Tim Leed4342a92020-04-27 11:47:58 +08003779 // Get Severity template from message registry
3780 std::string severity;
3781 if (message != nullptr)
3782 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003783 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003784 }
3785
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003786 // Format entry
3787 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003788 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003789 bmcLogEntry["@odata.id"] = boost::urls::format(
3790 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
3791 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003792 bmcLogEntry["Name"] = "POST Code Log Entry";
3793 bmcLogEntry["Id"] = postcodeEntryID;
3794 bmcLogEntry["Message"] = std::move(msg);
3795 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3796 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3797 bmcLogEntry["EntryType"] = "Event";
3798 bmcLogEntry["Severity"] = std::move(severity);
3799 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003800 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3801 {
3802 bmcLogEntry["AdditionalDataURI"] =
3803 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3804 postcodeEntryID + "/attachment";
3805 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003806
3807 // codeIndex is only specified when querying single entry, return only
3808 // that entry in this case
3809 if (codeIndex != 0)
3810 {
3811 aResp->res.jsonValue.update(bmcLogEntry);
3812 return true;
3813 }
3814
3815 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05003816 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003817 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003818
3819 // Return value is always false when querying multiple entries
3820 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003821}
3822
zhanghch058d1b46d2021-04-01 11:18:24 +08003823static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003824 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003825{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003826 uint16_t bootIndex = 0;
3827 uint64_t codeIndex = 0;
3828 if (!parsePostCode(entryId, codeIndex, bootIndex))
3829 {
3830 // Requested ID was not found
3831 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3832 return;
3833 }
3834
3835 if (bootIndex == 0 || codeIndex == 0)
3836 {
3837 // 0 is an invalid index
3838 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3839 return;
3840 }
3841
ZhikuiRena3316fc2020-01-29 14:58:08 -08003842 crow::connections::systemBus->async_method_call(
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003843 [aResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003844 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303845 const boost::container::flat_map<
3846 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3847 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003848 if (ec)
3849 {
3850 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3851 messages::internalError(aResp->res);
3852 return;
3853 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003854
Ed Tanous002d39b2022-05-31 08:59:27 -07003855 if (postcode.empty())
3856 {
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003857 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003858 return;
3859 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003860
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003861 if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
3862 {
3863 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3864 return;
3865 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003866 },
Jonathan Doman15124762021-01-07 17:54:17 -08003867 "xyz.openbmc_project.State.Boot.PostCode0",
3868 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003869 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3870 bootIndex);
3871}
3872
zhanghch058d1b46d2021-04-01 11:18:24 +08003873static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003874 const uint16_t bootIndex,
3875 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003876 const uint64_t entryCount, size_t skip,
3877 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003878{
3879 crow::connections::systemBus->async_method_call(
3880 [aResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003881 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303882 const boost::container::flat_map<
3883 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3884 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003885 if (ec)
3886 {
3887 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3888 messages::internalError(aResp->res);
3889 return;
3890 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003891
Ed Tanous002d39b2022-05-31 08:59:27 -07003892 uint64_t endCount = entryCount;
3893 if (!postcode.empty())
3894 {
3895 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003896 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003897 {
Patrick Williams89492a12023-05-10 07:51:34 -05003898 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
3899 entryCount) -
3900 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003901 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003902 std::min(static_cast<uint64_t>(top + skip), endCount) -
3903 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003904
3905 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3906 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003907 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003908 aResp->res.jsonValue["Members@odata.count"] = endCount;
3909 }
3910
3911 // continue to previous bootIndex
3912 if (bootIndex < bootCount)
3913 {
3914 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3915 bootCount, endCount, skip, top);
3916 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003917 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003918 {
3919 aResp->res.jsonValue["Members@odata.nextLink"] =
3920 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3921 std::to_string(skip + top);
3922 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003923 },
Jonathan Doman15124762021-01-07 17:54:17 -08003924 "xyz.openbmc_project.State.Boot.PostCode0",
3925 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003926 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3927 bootIndex);
3928}
3929
zhanghch058d1b46d2021-04-01 11:18:24 +08003930static void
3931 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003932 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003933{
3934 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003935 sdbusplus::asio::getProperty<uint16_t>(
3936 *crow::connections::systemBus,
3937 "xyz.openbmc_project.State.Boot.PostCode0",
3938 "/xyz/openbmc_project/State/Boot/PostCode0",
3939 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003940 [aResp, entryCount, skip, top](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003941 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003942 if (ec)
3943 {
3944 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3945 messages::internalError(aResp->res);
3946 return;
3947 }
3948 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003949 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003950}
3951
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003952inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003953{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003954 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003955 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003956 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003957 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003958 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003959 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3960 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003961 query_param::QueryCapabilities capabilities = {
3962 .canDelegateTop = true,
3963 .canDelegateSkip = true,
3964 };
3965 query_param::Query delegatedQuery;
3966 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003967 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003968 {
3969 return;
3970 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003971
3972 if (systemName != "system")
3973 {
3974 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3975 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003976 return;
3977 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003978 asyncResp->res.jsonValue["@odata.type"] =
3979 "#LogEntryCollection.LogEntryCollection";
3980 asyncResp->res.jsonValue["@odata.id"] =
3981 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3982 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3983 asyncResp->res.jsonValue["Description"] =
3984 "Collection of POST Code Log Entries";
3985 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3986 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003987 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003988 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003989 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003990 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003991}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003992
George Liu647b3cd2021-07-05 12:43:56 +08003993inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3994{
George Liu0fda0f12021-11-16 10:06:17 +08003995 BMCWEB_ROUTE(
3996 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003997 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003998 .privileges(redfish::privileges::getLogEntry)
3999 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004000 [&app](const crow::Request& req,
4001 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004002 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004003 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004004 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004005 {
4006 return;
4007 }
Matt Spinler72e21372023-04-19 12:53:33 -05004008 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004009 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004010 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004011 {
4012 asyncResp->res.result(boost::beast::http::status::bad_request);
4013 return;
4014 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004015 if (systemName != "system")
4016 {
4017 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4018 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004019 return;
4020 }
George Liu647b3cd2021-07-05 12:43:56 +08004021
Ed Tanous002d39b2022-05-31 08:59:27 -07004022 uint64_t currentValue = 0;
4023 uint16_t index = 0;
4024 if (!parsePostCode(postCodeID, currentValue, index))
4025 {
4026 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4027 return;
4028 }
George Liu647b3cd2021-07-05 12:43:56 +08004029
Ed Tanous002d39b2022-05-31 08:59:27 -07004030 crow::connections::systemBus->async_method_call(
4031 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004032 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004033 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4034 postcodes) {
4035 if (ec.value() == EBADR)
4036 {
4037 messages::resourceNotFound(asyncResp->res, "LogEntry",
4038 postCodeID);
4039 return;
4040 }
4041 if (ec)
4042 {
4043 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4044 messages::internalError(asyncResp->res);
4045 return;
4046 }
George Liu647b3cd2021-07-05 12:43:56 +08004047
Ed Tanous002d39b2022-05-31 08:59:27 -07004048 size_t value = static_cast<size_t>(currentValue) - 1;
4049 if (value == std::string::npos || postcodes.size() < currentValue)
4050 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004051 BMCWEB_LOG_WARNING << "Wrong currentValue value";
Ed Tanous002d39b2022-05-31 08:59:27 -07004052 messages::resourceNotFound(asyncResp->res, "LogEntry",
4053 postCodeID);
4054 return;
4055 }
George Liu647b3cd2021-07-05 12:43:56 +08004056
Ed Tanous002d39b2022-05-31 08:59:27 -07004057 const auto& [tID, c] = postcodes[value];
4058 if (c.empty())
4059 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004060 BMCWEB_LOG_WARNING << "No found post code data";
Ed Tanous002d39b2022-05-31 08:59:27 -07004061 messages::resourceNotFound(asyncResp->res, "LogEntry",
4062 postCodeID);
4063 return;
4064 }
4065 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4066 const char* d = reinterpret_cast<const char*>(c.data());
4067 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004068
Ed Tanousd9f6c622022-03-17 09:12:17 -07004069 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004070 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004071 asyncResp->res.addHeader(
4072 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004073 asyncResp->res.body() = crow::utility::base64encode(strData);
4074 },
4075 "xyz.openbmc_project.State.Boot.PostCode0",
4076 "/xyz/openbmc_project/State/Boot/PostCode0",
4077 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4078 });
George Liu647b3cd2021-07-05 12:43:56 +08004079}
4080
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004081inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004082{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004083 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004084 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004085 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004086 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004087 [&app](const crow::Request& req,
4088 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004089 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004090 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004091 {
4092 return;
4093 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004094 if (systemName != "system")
4095 {
4096 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4097 systemName);
4098 return;
4099 }
4100
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004101 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004102 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004103}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004104
Ed Tanous1da66f72018-07-27 16:13:37 -07004105} // namespace redfish