blob: 9ff3cdf8eab5f1ef36ee332913e7aee7203b5a4c [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 {
Myung Baeb90d14f2023-05-31 14:40:39 -0500665 BMCWEB_LOG_WARNING << "Can't find Dump Entry " << entryID;
666 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
667 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700668 return;
669 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500670 },
671 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
672 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
673}
674
zhanghch058d1b46d2021-04-01 11:18:24 +0800675inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800676 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500677 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500678{
Ed Tanous002d39b2022-05-31 08:59:27 -0700679 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800680 [asyncResp, entryID](const boost::system::error_code& ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500681 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
682 if (ec)
683 {
George Liu3de8d8b2021-03-22 17:49:39 +0800684 if (ec.value() == EBADR)
685 {
686 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
687 return;
688 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500689 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800690 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500691 messages::internalError(asyncResp->res);
692 return;
693 }
694 };
695 crow::connections::systemBus->async_method_call(
696 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500697 "/xyz/openbmc_project/dump/" +
698 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
699 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500700 "xyz.openbmc_project.Object.Delete", "Delete");
701}
702
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600703inline DumpCreationProgress
704 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500705{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600706 if (status ==
707 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
708 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
709 {
710 return DumpCreationProgress::DUMP_CREATE_FAILED;
711 }
712 if (status ==
713 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
714 {
715 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
716 }
717 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
718}
719
720inline DumpCreationProgress
721 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
722{
723 for (const auto& [key, val] : values)
724 {
725 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700726 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600727 const std::string* value = std::get_if<std::string>(&val);
728 if (value == nullptr)
729 {
730 BMCWEB_LOG_ERROR << "Status property value is null";
731 return DumpCreationProgress::DUMP_CREATE_FAILED;
732 }
733 return mapDbusStatusToDumpProgress(*value);
734 }
735 }
736 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
737}
738
739inline std::string getDumpEntryPath(const std::string& dumpPath)
740{
741 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
742 {
743 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
744 }
745 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
746 {
747 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
748 }
749 return "";
750}
751
752inline void createDumpTaskCallback(
753 task::Payload&& payload,
754 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
755 const sdbusplus::message::object_path& createdObjPath)
756{
757 const std::string dumpPath = createdObjPath.parent_path().str;
758 const std::string dumpId = createdObjPath.filename();
759
760 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
761
762 if (dumpEntryPath.empty())
763 {
764 BMCWEB_LOG_ERROR << "Invalid dump type received";
765 messages::internalError(asyncResp->res);
766 return;
767 }
768
769 crow::connections::systemBus->async_method_call(
770 [asyncResp, payload, createdObjPath,
771 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800772 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600773 const std::string& introspectXml) {
774 if (ec)
775 {
776 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
777 << ec.message();
778 messages::internalError(asyncResp->res);
779 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700780 }
781
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600782 // Check if the created dump object has implemented Progress
783 // interface to track dump completion. If yes, fetch the "Status"
784 // property of the interface, modify the task state accordingly.
785 // Else, return task completed.
786 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700787
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600788 doc.Parse(introspectXml.data(), introspectXml.size());
789 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
790 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700791 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600792 BMCWEB_LOG_ERROR << "XML document failed to parse";
793 messages::internalError(asyncResp->res);
794 return;
795 }
796 tinyxml2::XMLElement* interfaceNode =
797 pRoot->FirstChildElement("interface");
798
799 bool isProgressIntfPresent = false;
800 while (interfaceNode != nullptr)
801 {
802 const char* thisInterfaceName = interfaceNode->Attribute("name");
803 if (thisInterfaceName != nullptr)
804 {
805 if (thisInterfaceName ==
806 std::string_view("xyz.openbmc_project.Common.Progress"))
807 {
808 interfaceNode =
809 interfaceNode->NextSiblingElement("interface");
810 continue;
811 }
812 isProgressIntfPresent = true;
813 break;
814 }
815 interfaceNode = interfaceNode->NextSiblingElement("interface");
816 }
817
818 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
819 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800820 const boost::system::error_code& err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600821 const std::shared_ptr<task::TaskData>& taskData) {
822 if (err)
823 {
824 BMCWEB_LOG_ERROR << createdObjPath.str
825 << ": Error in creating dump";
826 taskData->messages.emplace_back(messages::internalError());
827 taskData->state = "Cancelled";
828 return task::completed;
829 }
830
831 if (isProgressIntfPresent)
832 {
833 dbus::utility::DBusPropertiesMap values;
834 std::string prop;
835 msg.read(prop, values);
836
837 DumpCreationProgress dumpStatus =
838 getDumpCompletionStatus(values);
839 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
840 {
841 BMCWEB_LOG_ERROR << createdObjPath.str
842 << ": Error in creating dump";
843 taskData->state = "Cancelled";
844 return task::completed;
845 }
846
847 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
848 {
849 BMCWEB_LOG_DEBUG << createdObjPath.str
850 << ": Dump creation task is in progress";
851 return !task::completed;
852 }
853 }
854
Ed Tanous002d39b2022-05-31 08:59:27 -0700855 nlohmann::json retMessage = messages::success();
856 taskData->messages.emplace_back(retMessage);
857
Ed Tanousc51a58e2023-03-27 14:43:19 -0700858 boost::urls::url url = boost::urls::format(
859 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
860
861 std::string headerLoc = "Location: ";
862 headerLoc += url.buffer();
863
Ed Tanous002d39b2022-05-31 08:59:27 -0700864 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
865
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600866 BMCWEB_LOG_DEBUG << createdObjPath.str
867 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700868 taskData->state = "Completed";
869 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600870 },
871 "type='signal',interface='org.freedesktop.DBus.Properties',"
872 "member='PropertiesChanged',path='" +
873 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500874
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600875 // The task timer is set to max time limit within which the
876 // requested dump will be collected.
877 task->startTimer(std::chrono::minutes(6));
878 task->populateResp(asyncResp->res);
879 task->payload.emplace(payload);
880 },
881 "xyz.openbmc_project.Dump.Manager", createdObjPath,
882 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500883}
884
zhanghch058d1b46d2021-04-01 11:18:24 +0800885inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
886 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500887{
Claire Weinanfdd26902022-03-01 14:18:25 -0800888 std::string dumpPath = getDumpEntriesPath(dumpType);
889 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500890 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500891 messages::internalError(asyncResp->res);
892 return;
893 }
894
895 std::optional<std::string> diagnosticDataType;
896 std::optional<std::string> oemDiagnosticDataType;
897
Willy Tu15ed6782021-12-14 11:03:16 -0800898 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500899 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
900 "OEMDiagnosticDataType", oemDiagnosticDataType))
901 {
902 return;
903 }
904
905 if (dumpType == "System")
906 {
907 if (!oemDiagnosticDataType || !diagnosticDataType)
908 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800909 BMCWEB_LOG_ERROR
910 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500911 messages::actionParameterMissing(
912 asyncResp->res, "CollectDiagnosticData",
913 "DiagnosticDataType & OEMDiagnosticDataType");
914 return;
915 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700916 if ((*oemDiagnosticDataType != "System") ||
917 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500918 {
919 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700920 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500921 return;
922 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500923 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500924 }
925 else if (dumpType == "BMC")
926 {
927 if (!diagnosticDataType)
928 {
George Liu0fda0f12021-11-16 10:06:17 +0800929 BMCWEB_LOG_ERROR
930 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500931 messages::actionParameterMissing(
932 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
933 return;
934 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700935 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500936 {
937 BMCWEB_LOG_ERROR
938 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700939 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500940 return;
941 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500942 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
943 }
944 else
945 {
946 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
947 messages::internalError(asyncResp->res);
948 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500949 }
950
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600951 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
952 createDumpParamVec;
953
Carson Labradof574a8e2023-03-22 02:26:00 +0000954 if (req.session != nullptr)
955 {
956 createDumpParamVec.emplace_back(
957 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
958 req.session->clientIp);
959 createDumpParamVec.emplace_back(
960 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
961 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
962 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600963
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500964 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800965 [asyncResp, payload(task::Payload(req)),
966 dumpPath](const boost::system::error_code& ec,
967 const sdbusplus::message_t& msg,
968 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700969 if (ec)
970 {
971 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500972 const sd_bus_error* dbusError = msg.get_error();
973 if (dbusError == nullptr)
974 {
975 messages::internalError(asyncResp->res);
976 return;
977 }
978
979 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
980 << " and error msg: " << dbusError->message;
981 if (std::string_view(
982 "xyz.openbmc_project.Common.Error.NotAllowed") ==
983 dbusError->name)
984 {
985 messages::resourceInStandby(asyncResp->res);
986 return;
987 }
988 if (std::string_view(
989 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
990 dbusError->name)
991 {
992 messages::serviceDisabled(asyncResp->res, dumpPath);
993 return;
994 }
995 if (std::string_view(
996 "xyz.openbmc_project.Common.Error.Unavailable") ==
997 dbusError->name)
998 {
999 messages::resourceInUse(asyncResp->res);
1000 return;
1001 }
1002 // Other Dbus errors such as:
1003 // xyz.openbmc_project.Common.Error.InvalidArgument &
1004 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1005 // the dbus call that is made here in the bmcweb
1006 // implementation and has nothing to do with the client's
1007 // input in the request. Hence, returning internal error
1008 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001009 messages::internalError(asyncResp->res);
1010 return;
1011 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001012 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
1013 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001014 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001015 "xyz.openbmc_project.Dump.Manager",
1016 "/xyz/openbmc_project/dump/" +
1017 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001018 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001019}
1020
zhanghch058d1b46d2021-04-01 11:18:24 +08001021inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1022 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001023{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001024 std::string dumpTypeLowerCopy =
1025 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001026
Claire Weinan0d946212022-07-13 19:40:19 -07001027 crow::connections::systemBus->async_method_call(
1028 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001029 if (ec)
1030 {
Claire Weinan0d946212022-07-13 19:40:19 -07001031 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -07001032 messages::internalError(asyncResp->res);
1033 return;
1034 }
Claire Weinan0d946212022-07-13 19:40:19 -07001035 },
1036 "xyz.openbmc_project.Dump.Manager",
1037 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1038 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001039}
1040
Ed Tanousb9d36b42022-02-26 21:42:46 -08001041inline static void
1042 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1043 std::string& filename, std::string& timestamp,
1044 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001045{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001046 const std::string* filenamePtr = nullptr;
1047 const std::string* timestampPtr = nullptr;
1048 const std::string* logfilePtr = nullptr;
1049
1050 const bool success = sdbusplus::unpackPropertiesNoThrow(
1051 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1052 "Filename", filenamePtr, "Log", logfilePtr);
1053
1054 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001055 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001056 return;
1057 }
1058
1059 if (filenamePtr != nullptr)
1060 {
1061 filename = *filenamePtr;
1062 }
1063
1064 if (timestampPtr != nullptr)
1065 {
1066 timestamp = *timestampPtr;
1067 }
1068
1069 if (logfilePtr != nullptr)
1070 {
1071 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001072 }
1073}
1074
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001075inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001076{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001077 /**
1078 * Functions triggers appropriate requests on DBus
1079 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001080 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001081 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001082 .methods(boost::beast::http::verb::get)(
1083 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001084 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1085 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001086 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001087 {
1088 return;
1089 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001090 if (systemName != "system")
1091 {
1092 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1093 systemName);
1094 return;
1095 }
1096
Ed Tanous002d39b2022-05-31 08:59:27 -07001097 // Collections don't include the static data added by SubRoute
1098 // because it has a duplicate entry for members
1099 asyncResp->res.jsonValue["@odata.type"] =
1100 "#LogServiceCollection.LogServiceCollection";
1101 asyncResp->res.jsonValue["@odata.id"] =
1102 "/redfish/v1/Systems/system/LogServices";
1103 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1104 asyncResp->res.jsonValue["Description"] =
1105 "Collection of LogServices for this Computer System";
1106 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1107 logServiceArray = nlohmann::json::array();
1108 nlohmann::json::object_t eventLog;
1109 eventLog["@odata.id"] =
1110 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001111 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001112#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 nlohmann::json::object_t dumpLog;
1114 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001115 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001116#endif
1117
Jason M. Billsd53dd412019-02-12 17:16:22 -08001118#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001119 nlohmann::json::object_t crashdump;
1120 crashdump["@odata.id"] =
1121 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001122 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001123#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001124
1125#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001126 nlohmann::json::object_t hostlogger;
1127 hostlogger["@odata.id"] =
1128 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001129 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001130#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001131 asyncResp->res.jsonValue["Members@odata.count"] =
1132 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001133
George Liu7a1dbc42022-12-07 16:03:22 +08001134 constexpr std::array<std::string_view, 1> interfaces = {
1135 "xyz.openbmc_project.State.Boot.PostCode"};
1136 dbus::utility::getSubTreePaths(
1137 "/", 0, interfaces,
1138 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001139 const dbus::utility::MapperGetSubTreePathsResponse&
1140 subtreePath) {
1141 if (ec)
1142 {
1143 BMCWEB_LOG_ERROR << ec;
1144 return;
1145 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001146
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 for (const auto& pathStr : subtreePath)
1148 {
1149 if (pathStr.find("PostCode") != std::string::npos)
1150 {
1151 nlohmann::json& logServiceArrayLocal =
1152 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001153 nlohmann::json::object_t member;
1154 member["@odata.id"] =
1155 "/redfish/v1/Systems/system/LogServices/PostCodes";
1156
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001157 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001158
Ed Tanous002d39b2022-05-31 08:59:27 -07001159 asyncResp->res.jsonValue["Members@odata.count"] =
1160 logServiceArrayLocal.size();
1161 return;
1162 }
1163 }
George Liu7a1dbc42022-12-07 16:03:22 +08001164 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001165 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001166}
1167
1168inline void requestRoutesEventLogService(App& app)
1169{
Ed Tanous22d268c2022-05-19 09:39:07 -07001170 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001171 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 .methods(boost::beast::http::verb::get)(
1173 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001174 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1175 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001176 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001177 {
1178 return;
1179 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001180 if (systemName != "system")
1181 {
1182 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1183 systemName);
1184 return;
1185 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001186 asyncResp->res.jsonValue["@odata.id"] =
1187 "/redfish/v1/Systems/system/LogServices/EventLog";
1188 asyncResp->res.jsonValue["@odata.type"] =
1189 "#LogService.v1_1_0.LogService";
1190 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1191 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1192 asyncResp->res.jsonValue["Id"] = "EventLog";
1193 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301194
Ed Tanous002d39b2022-05-31 08:59:27 -07001195 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001196 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301197
Ed Tanous002d39b2022-05-31 08:59:27 -07001198 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1199 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1200 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301201
Ed Tanous002d39b2022-05-31 08:59:27 -07001202 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1203 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1204 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001205
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 {"target",
1207 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001208 });
1209}
1210
1211inline void requestRoutesJournalEventLogClear(App& app)
1212{
Jason M. Bills4978b632022-02-22 14:17:43 -08001213 BMCWEB_ROUTE(
1214 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001215 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001216 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001217 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001218 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001219 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1220 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001221 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001222 {
1223 return;
1224 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001225 if (systemName != "system")
1226 {
1227 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1228 systemName);
1229 return;
1230 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001231 // Clear the EventLog by deleting the log files
1232 std::vector<std::filesystem::path> redfishLogFiles;
1233 if (getRedfishLogFiles(redfishLogFiles))
1234 {
1235 for (const std::filesystem::path& file : redfishLogFiles)
1236 {
1237 std::error_code ec;
1238 std::filesystem::remove(file, ec);
1239 }
1240 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001241
Ed Tanous002d39b2022-05-31 08:59:27 -07001242 // Reload rsyslog so it knows to start new log files
1243 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001244 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001245 if (ec)
1246 {
1247 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1248 messages::internalError(asyncResp->res);
1249 return;
1250 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001251
Ed Tanous002d39b2022-05-31 08:59:27 -07001252 messages::success(asyncResp->res);
1253 },
1254 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1255 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1256 "replace");
1257 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001258}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001259
Jason M. Billsac992cd2022-06-24 13:31:46 -07001260enum class LogParseError
1261{
1262 success,
1263 parseFailed,
1264 messageIdNotInRegistry,
1265};
1266
1267static LogParseError
1268 fillEventLogEntryJson(const std::string& logEntryID,
1269 const std::string& logEntry,
1270 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001271{
Jason M. Bills95820182019-04-22 16:25:34 -07001272 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001273 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001274 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001275 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001276 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001277 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001278 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001279 std::string timestamp = logEntry.substr(0, space);
1280 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001281 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001282 if (entryStart == std::string::npos)
1283 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001284 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001285 }
1286 std::string_view entry(logEntry);
1287 entry.remove_prefix(entryStart);
1288 // Use split to separate the entry into its fields
1289 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001290 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001291 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001292 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001293 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001294 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001295 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001296 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001297
Jason M. Bills4851d452019-03-28 11:27:48 -07001298 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001299 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001300
Sui Chen54417b02022-03-24 14:59:52 -07001301 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001302 {
Sui Chen54417b02022-03-24 14:59:52 -07001303 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001304 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001305 }
1306
Sui Chen54417b02022-03-24 14:59:52 -07001307 std::string msg = message->message;
1308
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001309 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001310 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001311 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001312 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001313 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001314 // If the first string is empty, assume there are no MessageArgs
1315 std::size_t messageArgsSize = 0;
1316 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001317 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001318 messageArgsSize = logEntryFields.size() - 1;
1319 }
1320
Ed Tanous23a21a12020-07-25 04:45:05 +00001321 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001322
1323 // Fill the MessageArgs into the Message
1324 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001325 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001326 {
1327 std::string argStr = "%" + std::to_string(++i);
1328 size_t argPos = msg.find(argStr);
1329 if (argPos != std::string::npos)
1330 {
1331 msg.replace(argPos, argStr.length(), messageArg);
1332 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001333 }
1334 }
1335
Jason M. Bills95820182019-04-22 16:25:34 -07001336 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1337 // format which matches the Redfish format except for the fractional seconds
1338 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001339 std::size_t dot = timestamp.find_first_of('.');
1340 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001341 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001342 {
Jason M. Bills95820182019-04-22 16:25:34 -07001343 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001344 }
1345
1346 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001347 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001348 logEntryJson["@odata.id"] = boost::urls::format(
1349 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1350 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001351 logEntryJson["Name"] = "System Event Log Entry";
1352 logEntryJson["Id"] = logEntryID;
1353 logEntryJson["Message"] = std::move(msg);
1354 logEntryJson["MessageId"] = std::move(messageID);
1355 logEntryJson["MessageArgs"] = messageArgs;
1356 logEntryJson["EntryType"] = "Event";
1357 logEntryJson["Severity"] = message->messageSeverity;
1358 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001359 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001360}
1361
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001362inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001363{
Ed Tanous22d268c2022-05-19 09:39:07 -07001364 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001365 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001366 .methods(boost::beast::http::verb::get)(
1367 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001368 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1369 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001370 query_param::QueryCapabilities capabilities = {
1371 .canDelegateTop = true,
1372 .canDelegateSkip = true,
1373 };
1374 query_param::Query delegatedQuery;
1375 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001376 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001377 {
1378 return;
1379 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001380 if (systemName != "system")
1381 {
1382 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1383 systemName);
1384 return;
1385 }
1386
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001387 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001388 size_t skip = delegatedQuery.skip.value_or(0);
1389
Ed Tanous002d39b2022-05-31 08:59:27 -07001390 // Collections don't include the static data added by SubRoute
1391 // because it has a duplicate entry for members
1392 asyncResp->res.jsonValue["@odata.type"] =
1393 "#LogEntryCollection.LogEntryCollection";
1394 asyncResp->res.jsonValue["@odata.id"] =
1395 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1396 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1397 asyncResp->res.jsonValue["Description"] =
1398 "Collection of System Event Log Entries";
1399
1400 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1401 logEntryArray = nlohmann::json::array();
1402 // Go through the log files and create a unique ID for each
1403 // entry
1404 std::vector<std::filesystem::path> redfishLogFiles;
1405 getRedfishLogFiles(redfishLogFiles);
1406 uint64_t entryCount = 0;
1407 std::string logEntry;
1408
1409 // Oldest logs are in the last file, so start there and loop
1410 // backwards
1411 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1412 it++)
1413 {
1414 std::ifstream logStream(*it);
1415 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001416 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001417 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001418 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001419
Ed Tanous002d39b2022-05-31 08:59:27 -07001420 // Reset the unique ID on the first entry
1421 bool firstEntry = true;
1422 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001423 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001424 std::string idStr;
1425 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001426 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001427 continue;
1428 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001429 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001430
Jason M. Billsde703c52022-06-23 14:19:04 -07001431 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001432 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1433 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001434 if (status == LogParseError::messageIdNotInRegistry)
1435 {
1436 continue;
1437 }
1438 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001439 {
1440 messages::internalError(asyncResp->res);
1441 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001442 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001443
Jason M. Billsde703c52022-06-23 14:19:04 -07001444 entryCount++;
1445 // Handle paging using skip (number of entries to skip from the
1446 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001447 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001448 {
1449 continue;
1450 }
1451
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001452 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001453 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001454 }
1455 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001456 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001457 {
1458 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1459 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001460 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001461 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001462 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001463}
Chicago Duan336e96c2019-07-15 14:22:08 +08001464
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001465inline void requestRoutesJournalEventLogEntry(App& app)
1466{
1467 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001468 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001469 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001470 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001471 [&app](const crow::Request& req,
1472 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001473 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001474 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001475 {
1476 return;
1477 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001478
1479 if (systemName != "system")
1480 {
1481 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1482 systemName);
1483 return;
1484 }
1485
Ed Tanous002d39b2022-05-31 08:59:27 -07001486 const std::string& targetID = param;
1487
1488 // Go through the log files and check the unique ID for each
1489 // entry to find the target entry
1490 std::vector<std::filesystem::path> redfishLogFiles;
1491 getRedfishLogFiles(redfishLogFiles);
1492 std::string logEntry;
1493
1494 // Oldest logs are in the last file, so start there and loop
1495 // backwards
1496 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1497 it++)
1498 {
1499 std::ifstream logStream(*it);
1500 if (!logStream.is_open())
1501 {
1502 continue;
1503 }
1504
1505 // Reset the unique ID on the first entry
1506 bool firstEntry = true;
1507 while (std::getline(logStream, logEntry))
1508 {
1509 std::string idStr;
1510 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001511 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001512 continue;
1513 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001514 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001515
1516 if (idStr == targetID)
1517 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001518 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001519 LogParseError status =
1520 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1521 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001522 {
1523 messages::internalError(asyncResp->res);
1524 return;
1525 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001526 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001527 return;
1528 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001529 }
1530 }
1531 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001532 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001533 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001534}
1535
1536inline void requestRoutesDBusEventLogEntryCollection(App& app)
1537{
Ed Tanous22d268c2022-05-19 09:39:07 -07001538 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001539 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001540 .methods(boost::beast::http::verb::get)(
1541 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001542 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1543 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001544 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001545 {
1546 return;
1547 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001548 if (systemName != "system")
1549 {
1550 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1551 systemName);
1552 return;
1553 }
1554
Ed Tanous002d39b2022-05-31 08:59:27 -07001555 // Collections don't include the static data added by SubRoute
1556 // because it has a duplicate entry for members
1557 asyncResp->res.jsonValue["@odata.type"] =
1558 "#LogEntryCollection.LogEntryCollection";
1559 asyncResp->res.jsonValue["@odata.id"] =
1560 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1561 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1562 asyncResp->res.jsonValue["Description"] =
1563 "Collection of System Event Log Entries";
1564
1565 // DBus implementation of EventLog/Entries
1566 // Make call to Logging Service to find all log entry objects
1567 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001568 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001569 const dbus::utility::ManagedObjectType& resp) {
1570 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001571 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001572 // TODO Handle for specific error code
1573 BMCWEB_LOG_ERROR
1574 << "getLogEntriesIfaceData resp_handler got error " << ec;
1575 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001576 return;
1577 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001578 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1579 entriesArray = nlohmann::json::array();
1580 for (const auto& objectPath : resp)
1581 {
1582 const uint32_t* id = nullptr;
1583 const uint64_t* timestamp = nullptr;
1584 const uint64_t* updateTimestamp = nullptr;
1585 const std::string* severity = nullptr;
1586 const std::string* message = nullptr;
1587 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001588 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001589 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001590 const std::string* notify = nullptr;
1591
Ed Tanous002d39b2022-05-31 08:59:27 -07001592 for (const auto& interfaceMap : objectPath.second)
1593 {
1594 if (interfaceMap.first ==
1595 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001596 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001597 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001598 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001599 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001600 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001601 id = std::get_if<uint32_t>(&propertyMap.second);
1602 }
1603 else if (propertyMap.first == "Timestamp")
1604 {
1605 timestamp =
1606 std::get_if<uint64_t>(&propertyMap.second);
1607 }
1608 else if (propertyMap.first == "UpdateTimestamp")
1609 {
1610 updateTimestamp =
1611 std::get_if<uint64_t>(&propertyMap.second);
1612 }
1613 else if (propertyMap.first == "Severity")
1614 {
1615 severity = std::get_if<std::string>(
1616 &propertyMap.second);
1617 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001618 else if (propertyMap.first == "Resolution")
1619 {
1620 resolution = std::get_if<std::string>(
1621 &propertyMap.second);
1622 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001623 else if (propertyMap.first == "Message")
1624 {
1625 message = std::get_if<std::string>(
1626 &propertyMap.second);
1627 }
1628 else if (propertyMap.first == "Resolved")
1629 {
1630 const bool* resolveptr =
1631 std::get_if<bool>(&propertyMap.second);
1632 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001633 {
1634 messages::internalError(asyncResp->res);
1635 return;
1636 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001637 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001638 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001639 else if (propertyMap.first ==
1640 "ServiceProviderNotify")
1641 {
1642 notify = std::get_if<std::string>(
1643 &propertyMap.second);
1644 if (notify == nullptr)
1645 {
1646 messages::internalError(asyncResp->res);
1647 return;
1648 }
1649 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001650 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001651 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001652 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001653 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001654 messages::internalError(asyncResp->res);
1655 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001656 }
1657 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001658 else if (interfaceMap.first ==
1659 "xyz.openbmc_project.Common.FilePath")
1660 {
1661 for (const auto& propertyMap : interfaceMap.second)
1662 {
1663 if (propertyMap.first == "Path")
1664 {
1665 filePath = std::get_if<std::string>(
1666 &propertyMap.second);
1667 }
1668 }
1669 }
1670 }
1671 // Object path without the
1672 // xyz.openbmc_project.Logging.Entry interface, ignore
1673 // and continue.
1674 if (id == nullptr || message == nullptr ||
1675 severity == nullptr || timestamp == nullptr ||
1676 updateTimestamp == nullptr)
1677 {
1678 continue;
1679 }
1680 entriesArray.push_back({});
1681 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001682 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001683 thisEntry["@odata.id"] = boost::urls::format(
1684 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1685 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001686 thisEntry["Name"] = "System Event Log Entry";
1687 thisEntry["Id"] = std::to_string(*id);
1688 thisEntry["Message"] = *message;
1689 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001690 if ((resolution != nullptr) && (!(*resolution).empty()))
1691 {
1692 thisEntry["Resolution"] = *resolution;
1693 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001694 std::optional<bool> notifyAction =
1695 getProviderNotifyAction(*notify);
1696 if (notifyAction)
1697 {
1698 thisEntry["ServiceProviderNotified"] = *notifyAction;
1699 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001700 thisEntry["EntryType"] = "Event";
1701 thisEntry["Severity"] =
1702 translateSeverityDbusToRedfish(*severity);
1703 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001704 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001705 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001706 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001707 if (filePath != nullptr)
1708 {
1709 thisEntry["AdditionalDataURI"] =
1710 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1711 std::to_string(*id) + "/attachment";
1712 }
1713 }
1714 std::sort(
1715 entriesArray.begin(), entriesArray.end(),
1716 [](const nlohmann::json& left, const nlohmann::json& right) {
1717 return (left["Id"] <= right["Id"]);
1718 });
1719 asyncResp->res.jsonValue["Members@odata.count"] =
1720 entriesArray.size();
1721 },
1722 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1723 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001724 });
1725}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001726
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001727inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001728{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001729 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001730 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001731 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001732 .methods(boost::beast::http::verb::get)(
1733 [&app](const crow::Request& req,
1734 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001735 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001736 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001737 {
1738 return;
1739 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001740 if (systemName != "system")
1741 {
1742 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1743 systemName);
1744 return;
1745 }
1746
Ed Tanous002d39b2022-05-31 08:59:27 -07001747 std::string entryID = param;
1748 dbus::utility::escapePathForDbus(entryID);
1749
1750 // DBus implementation of EventLog/Entries
1751 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001752 sdbusplus::asio::getAllProperties(
1753 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1754 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001755 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001756 const dbus::utility::DBusPropertiesMap& resp) {
1757 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001758 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001759 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1760 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001761 return;
1762 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001763 if (ec)
1764 {
1765 BMCWEB_LOG_ERROR
1766 << "EventLogEntry (DBus) resp_handler got error " << ec;
1767 messages::internalError(asyncResp->res);
1768 return;
1769 }
1770 const uint32_t* id = nullptr;
1771 const uint64_t* timestamp = nullptr;
1772 const uint64_t* updateTimestamp = nullptr;
1773 const std::string* severity = nullptr;
1774 const std::string* message = nullptr;
1775 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001776 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001777 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001778 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001779
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001780 const bool success = sdbusplus::unpackPropertiesNoThrow(
1781 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1782 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001783 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001784 "Resolution", resolution, "Path", filePath,
1785 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001786
1787 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001788 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001789 messages::internalError(asyncResp->res);
1790 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001791 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001792
Ed Tanous002d39b2022-05-31 08:59:27 -07001793 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001794 timestamp == nullptr || updateTimestamp == nullptr ||
1795 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 {
1797 messages::internalError(asyncResp->res);
1798 return;
1799 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001800
Ed Tanous002d39b2022-05-31 08:59:27 -07001801 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001802 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001803 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1804 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1805 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001806 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1807 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1808 asyncResp->res.jsonValue["Message"] = *message;
1809 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001810 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1811 if (notifyAction)
1812 {
1813 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1814 *notifyAction;
1815 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001816 if ((resolution != nullptr) && (!(*resolution).empty()))
1817 {
1818 asyncResp->res.jsonValue["Resolution"] = *resolution;
1819 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001820 asyncResp->res.jsonValue["EntryType"] = "Event";
1821 asyncResp->res.jsonValue["Severity"] =
1822 translateSeverityDbusToRedfish(*severity);
1823 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001824 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001825 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001826 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001827 if (filePath != nullptr)
1828 {
1829 asyncResp->res.jsonValue["AdditionalDataURI"] =
1830 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1831 std::to_string(*id) + "/attachment";
1832 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001833 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001834 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001835
1836 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001837 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001838 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001839 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001840 [&app](const crow::Request& req,
1841 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001842 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001843 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001844 {
1845 return;
1846 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001847 if (systemName != "system")
1848 {
1849 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1850 systemName);
1851 return;
1852 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001853 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001854
Ed Tanous002d39b2022-05-31 08:59:27 -07001855 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1856 resolved))
1857 {
1858 return;
1859 }
1860 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001861
Ed Tanous002d39b2022-05-31 08:59:27 -07001862 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001863 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001864 if (ec)
1865 {
1866 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1867 messages::internalError(asyncResp->res);
1868 return;
1869 }
1870 },
1871 "xyz.openbmc_project.Logging",
1872 "/xyz/openbmc_project/logging/entry/" + entryId,
1873 "org.freedesktop.DBus.Properties", "Set",
1874 "xyz.openbmc_project.Logging.Entry", "Resolved",
1875 dbus::utility::DbusVariantType(*resolved));
1876 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001877
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001878 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001879 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001880 .privileges(redfish::privileges::deleteLogEntry)
1881
Ed Tanous002d39b2022-05-31 08:59:27 -07001882 .methods(boost::beast::http::verb::delete_)(
1883 [&app](const crow::Request& req,
1884 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001885 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001886 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001887 {
1888 return;
1889 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001890 if (systemName != "system")
1891 {
1892 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1893 systemName);
1894 return;
1895 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001896 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1897
1898 std::string entryID = param;
1899
1900 dbus::utility::escapePathForDbus(entryID);
1901
1902 // Process response from Logging service.
1903 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001904 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001905 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1906 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001907 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001908 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001909 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001910 messages::resourceNotFound(asyncResp->res, "LogEntry",
1911 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001912 return;
1913 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001914 // TODO Handle for specific error code
1915 BMCWEB_LOG_ERROR
1916 << "EventLogEntry (DBus) doDelete respHandler got error "
1917 << ec;
1918 asyncResp->res.result(
1919 boost::beast::http::status::internal_server_error);
1920 return;
1921 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001922
Ed Tanous002d39b2022-05-31 08:59:27 -07001923 asyncResp->res.result(boost::beast::http::status::ok);
1924 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001925
Ed Tanous002d39b2022-05-31 08:59:27 -07001926 // Make call to Logging service to request Delete Log
1927 crow::connections::systemBus->async_method_call(
1928 respHandler, "xyz.openbmc_project.Logging",
1929 "/xyz/openbmc_project/logging/entry/" + entryID,
1930 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001931 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001932}
1933
1934inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001935{
George Liu0fda0f12021-11-16 10:06:17 +08001936 BMCWEB_ROUTE(
1937 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001938 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001939 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001940 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001941 [&app](const crow::Request& req,
1942 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001943 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001944 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001945 {
1946 return;
1947 }
Matt Spinler72e21372023-04-19 12:53:33 -05001948 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07001949 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001950 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001951 {
1952 asyncResp->res.result(boost::beast::http::status::bad_request);
1953 return;
1954 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001955 if (systemName != "system")
1956 {
1957 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1958 systemName);
1959 return;
1960 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001961
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 std::string entryID = param;
1963 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001964
Ed Tanous002d39b2022-05-31 08:59:27 -07001965 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001966 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 const sdbusplus::message::unix_fd& unixfd) {
1968 if (ec.value() == EBADR)
1969 {
1970 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1971 entryID);
1972 return;
1973 }
1974 if (ec)
1975 {
1976 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1977 messages::internalError(asyncResp->res);
1978 return;
1979 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001980
Ed Tanous002d39b2022-05-31 08:59:27 -07001981 int fd = -1;
1982 fd = dup(unixfd);
1983 if (fd == -1)
1984 {
1985 messages::internalError(asyncResp->res);
1986 return;
1987 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001988
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 long long int size = lseek(fd, 0, SEEK_END);
1990 if (size == -1)
1991 {
1992 messages::internalError(asyncResp->res);
1993 return;
1994 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001995
Ed Tanous002d39b2022-05-31 08:59:27 -07001996 // Arbitrary max size of 64kb
1997 constexpr int maxFileSize = 65536;
1998 if (size > maxFileSize)
1999 {
2000 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
2001 << maxFileSize;
2002 messages::internalError(asyncResp->res);
2003 return;
2004 }
2005 std::vector<char> data(static_cast<size_t>(size));
2006 long long int rc = lseek(fd, 0, SEEK_SET);
2007 if (rc == -1)
2008 {
2009 messages::internalError(asyncResp->res);
2010 return;
2011 }
2012 rc = read(fd, data.data(), data.size());
2013 if ((rc == -1) || (rc != size))
2014 {
2015 messages::internalError(asyncResp->res);
2016 return;
2017 }
2018 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002019
Ed Tanous002d39b2022-05-31 08:59:27 -07002020 std::string_view strData(data.data(), data.size());
2021 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002022
Ed Tanousd9f6c622022-03-17 09:12:17 -07002023 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07002024 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07002025 asyncResp->res.addHeader(
2026 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07002027 asyncResp->res.body() = std::move(output);
2028 },
2029 "xyz.openbmc_project.Logging",
2030 "/xyz/openbmc_project/logging/entry/" + entryID,
2031 "xyz.openbmc_project.Logging.Entry", "GetEntry");
2032 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002033}
2034
Spencer Kub7028eb2021-10-26 15:27:35 +08002035constexpr const char* hostLoggerFolderPath = "/var/log/console";
2036
2037inline bool
2038 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2039 std::vector<std::filesystem::path>& hostLoggerFiles)
2040{
2041 std::error_code ec;
2042 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2043 if (ec)
2044 {
2045 BMCWEB_LOG_ERROR << ec.message();
2046 return false;
2047 }
2048 for (const std::filesystem::directory_entry& it : logPath)
2049 {
2050 std::string filename = it.path().filename();
2051 // Prefix of each log files is "log". Find the file and save the
2052 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002053 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002054 {
2055 hostLoggerFiles.emplace_back(it.path());
2056 }
2057 }
2058 // As the log files rotate, they are appended with a ".#" that is higher for
2059 // the older logs. Since we start from oldest logs, sort the name in
2060 // descending order.
2061 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2062 AlphanumLess<std::string>());
2063
2064 return true;
2065}
2066
Ed Tanous02cad962022-06-30 16:50:15 -07002067inline bool getHostLoggerEntries(
2068 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2069 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002070{
2071 GzFileReader logFile;
2072
2073 // Go though all log files and expose host logs.
2074 for (const std::filesystem::path& it : hostLoggerFiles)
2075 {
2076 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2077 {
2078 BMCWEB_LOG_ERROR << "fail to expose host logs";
2079 return false;
2080 }
2081 }
2082 // Get lastMessage from constructor by getter
2083 std::string lastMessage = logFile.getLastMessage();
2084 if (!lastMessage.empty())
2085 {
2086 logCount++;
2087 if (logCount > skip && logCount <= (skip + top))
2088 {
2089 logEntries.push_back(lastMessage);
2090 }
2091 }
2092 return true;
2093}
2094
2095inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2096 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002097 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002098{
2099 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002100 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002101 logEntryJson["@odata.id"] = boost::urls::format(
2102 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2103 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002104 logEntryJson["Name"] = "Host Logger Entry";
2105 logEntryJson["Id"] = logEntryID;
2106 logEntryJson["Message"] = msg;
2107 logEntryJson["EntryType"] = "Oem";
2108 logEntryJson["Severity"] = "OK";
2109 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002110}
2111
2112inline void requestRoutesSystemHostLogger(App& app)
2113{
Ed Tanous22d268c2022-05-19 09:39:07 -07002114 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002115 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002116 .methods(boost::beast::http::verb::get)(
2117 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002118 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2119 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002120 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002121 {
2122 return;
2123 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002124 if (systemName != "system")
2125 {
2126 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2127 systemName);
2128 return;
2129 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002130 asyncResp->res.jsonValue["@odata.id"] =
2131 "/redfish/v1/Systems/system/LogServices/HostLogger";
2132 asyncResp->res.jsonValue["@odata.type"] =
2133 "#LogService.v1_1_0.LogService";
2134 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2135 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2136 asyncResp->res.jsonValue["Id"] = "HostLogger";
2137 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2138 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2139 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002140}
2141
2142inline void requestRoutesSystemHostLoggerCollection(App& app)
2143{
2144 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002145 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002146 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 .methods(boost::beast::http::verb::get)(
2148 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002149 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2150 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002151 query_param::QueryCapabilities capabilities = {
2152 .canDelegateTop = true,
2153 .canDelegateSkip = true,
2154 };
2155 query_param::Query delegatedQuery;
2156 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002157 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 {
2159 return;
2160 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002161 if (systemName != "system")
2162 {
2163 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2164 systemName);
2165 return;
2166 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002167 asyncResp->res.jsonValue["@odata.id"] =
2168 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2169 asyncResp->res.jsonValue["@odata.type"] =
2170 "#LogEntryCollection.LogEntryCollection";
2171 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2172 asyncResp->res.jsonValue["Description"] =
2173 "Collection of HostLogger Entries";
2174 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2175 logEntryArray = nlohmann::json::array();
2176 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002177
Ed Tanous002d39b2022-05-31 08:59:27 -07002178 std::vector<std::filesystem::path> hostLoggerFiles;
2179 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2180 {
2181 BMCWEB_LOG_ERROR << "fail to get host log file path";
2182 return;
2183 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002184 // If we weren't provided top and skip limits, use the defaults.
2185 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002186 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002187 size_t logCount = 0;
2188 // This vector only store the entries we want to expose that
2189 // control by skip and top.
2190 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002191 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2192 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002193 {
2194 messages::internalError(asyncResp->res);
2195 return;
2196 }
2197 // If vector is empty, that means skip value larger than total
2198 // log count
2199 if (logEntries.empty())
2200 {
2201 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2202 return;
2203 }
2204 if (!logEntries.empty())
2205 {
2206 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002207 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002208 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002209 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2210 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002211 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002212 }
2213
Ed Tanous002d39b2022-05-31 08:59:27 -07002214 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002215 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002216 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002217 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2218 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002219 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002220 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002221 }
George Liu0fda0f12021-11-16 10:06:17 +08002222 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002223}
2224
2225inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2226{
2227 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002228 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002229 .privileges(redfish::privileges::getLogEntry)
2230 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002231 [&app](const crow::Request& req,
2232 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002233 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002234 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002235 {
2236 return;
2237 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002238 if (systemName != "system")
2239 {
2240 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2241 systemName);
2242 return;
2243 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002244 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002245
Ed Tanous002d39b2022-05-31 08:59:27 -07002246 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002247
Patrick Williams84396af2023-05-11 11:47:45 -05002248 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2249 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002250 if (ec == std::errc::invalid_argument ||
2251 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002252 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002253 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002254 return;
2255 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002256
Ed Tanous002d39b2022-05-31 08:59:27 -07002257 std::vector<std::filesystem::path> hostLoggerFiles;
2258 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2259 {
2260 BMCWEB_LOG_ERROR << "fail to get host log file path";
2261 return;
2262 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002263
Ed Tanous002d39b2022-05-31 08:59:27 -07002264 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002265 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002266 std::vector<std::string> logEntries;
2267 // We can get specific entry by skip and top. For example, if we
2268 // want to get nth entry, we can set skip = n-1 and top = 1 to
2269 // get that entry
2270 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2271 logCount))
2272 {
2273 messages::internalError(asyncResp->res);
2274 return;
2275 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002276
Ed Tanous002d39b2022-05-31 08:59:27 -07002277 if (!logEntries.empty())
2278 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002279 nlohmann::json::object_t hostLogEntry;
2280 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2281 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002282 return;
2283 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002284
Ed Tanous002d39b2022-05-31 08:59:27 -07002285 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002286 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002287 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002288}
2289
Claire Weinandd72e872022-08-15 14:20:06 -07002290inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002291 crow::App& app, const crow::Request& req,
2292 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2293{
2294 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2295 {
2296 return;
2297 }
2298 // Collections don't include the static data added by SubRoute
2299 // because it has a duplicate entry for members
2300 asyncResp->res.jsonValue["@odata.type"] =
2301 "#LogServiceCollection.LogServiceCollection";
2302 asyncResp->res.jsonValue["@odata.id"] =
2303 "/redfish/v1/Managers/bmc/LogServices";
2304 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2305 asyncResp->res.jsonValue["Description"] =
2306 "Collection of LogServices for this Manager";
2307 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2308 logServiceArray = nlohmann::json::array();
2309
2310#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002311 nlohmann::json::object_t journal;
2312 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002313 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002314#endif
2315
2316 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2317
2318#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002319 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002320 "xyz.openbmc_project.Collection.DeleteAll"};
2321 dbus::utility::getSubTreePaths(
2322 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002323 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002324 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002325 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2326 if (ec)
2327 {
2328 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002329 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002330 << ec;
2331 // Assume that getting an error simply means there are no dump
2332 // LogServices. Return without adding any error response.
2333 return;
2334 }
2335
2336 nlohmann::json& logServiceArrayLocal =
2337 asyncResp->res.jsonValue["Members"];
2338
2339 for (const std::string& path : subTreePaths)
2340 {
2341 if (path == "/xyz/openbmc_project/dump/bmc")
2342 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002343 nlohmann::json::object_t member;
2344 member["@odata.id"] =
2345 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002346 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002347 }
2348 else if (path == "/xyz/openbmc_project/dump/faultlog")
2349 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002350 nlohmann::json::object_t member;
2351 member["@odata.id"] =
2352 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002353 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002354 }
2355 }
2356
2357 asyncResp->res.jsonValue["Members@odata.count"] =
2358 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002359 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002360#endif
2361}
2362
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002363inline void requestRoutesBMCLogServiceCollection(App& app)
2364{
2365 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002366 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002367 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002368 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002369}
Ed Tanous1da66f72018-07-27 16:13:37 -07002370
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002371inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002372{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002373 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002374 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002375 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002376 [&app](const crow::Request& req,
2377 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002378 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002379 {
2380 return;
2381 }
2382 asyncResp->res.jsonValue["@odata.type"] =
2383 "#LogService.v1_1_0.LogService";
2384 asyncResp->res.jsonValue["@odata.id"] =
2385 "/redfish/v1/Managers/bmc/LogServices/Journal";
2386 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2387 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002388 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002389 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302390
Ed Tanous002d39b2022-05-31 08:59:27 -07002391 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002392 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002393 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2394 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2395 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302396
Ed Tanous002d39b2022-05-31 08:59:27 -07002397 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2398 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2399 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002400}
Jason M. Billse1f26342018-07-18 12:12:00 -07002401
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002402static int
2403 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2404 sd_journal* journal,
2405 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002406{
2407 // Get the Log Entry contents
2408 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002409
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002410 std::string message;
2411 std::string_view syslogID;
2412 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2413 if (ret < 0)
2414 {
2415 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2416 << strerror(-ret);
2417 }
2418 if (!syslogID.empty())
2419 {
2420 message += std::string(syslogID) + ": ";
2421 }
2422
Ed Tanous39e77502019-03-04 17:35:53 -08002423 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002424 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002425 if (ret < 0)
2426 {
2427 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2428 return 1;
2429 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002430 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002431
2432 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002433 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002434 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002435 if (ret < 0)
2436 {
2437 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002438 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002439
2440 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002441 std::string entryTimeStr;
2442 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002443 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002444 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002445 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002446
2447 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002448 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002449 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2450 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002451 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002452 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2453 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2454 bmcJournalLogEntryJson["Message"] = std::move(message);
2455 bmcJournalLogEntryJson["EntryType"] = "Oem";
2456 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2457 : severity <= 4 ? "Warning"
2458 : "OK";
2459 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2460 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002461 return 0;
2462}
2463
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002464inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002465{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002466 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002467 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002468 .methods(boost::beast::http::verb::get)(
2469 [&app](const crow::Request& req,
2470 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2471 query_param::QueryCapabilities capabilities = {
2472 .canDelegateTop = true,
2473 .canDelegateSkip = true,
2474 };
2475 query_param::Query delegatedQuery;
2476 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002477 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002478 {
2479 return;
2480 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002481
2482 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002483 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002484
Ed Tanous002d39b2022-05-31 08:59:27 -07002485 // Collections don't include the static data added by SubRoute
2486 // because it has a duplicate entry for members
2487 asyncResp->res.jsonValue["@odata.type"] =
2488 "#LogEntryCollection.LogEntryCollection";
2489 asyncResp->res.jsonValue["@odata.id"] =
2490 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2491 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2492 asyncResp->res.jsonValue["Description"] =
2493 "Collection of BMC Journal Entries";
2494 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2495 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002496
Ed Tanous002d39b2022-05-31 08:59:27 -07002497 // Go through the journal and use the timestamp to create a
2498 // unique ID for each entry
2499 sd_journal* journalTmp = nullptr;
2500 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2501 if (ret < 0)
2502 {
2503 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2504 messages::internalError(asyncResp->res);
2505 return;
2506 }
2507 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2508 journalTmp, sd_journal_close);
2509 journalTmp = nullptr;
2510 uint64_t entryCount = 0;
2511 // Reset the unique ID on the first entry
2512 bool firstEntry = true;
2513 SD_JOURNAL_FOREACH(journal.get())
2514 {
2515 entryCount++;
2516 // Handle paging using skip (number of entries to skip from
2517 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002518 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002519 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002520 continue;
2521 }
2522
2523 std::string idStr;
2524 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2525 {
2526 continue;
2527 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002528 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002529
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002530 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002531 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2532 bmcJournalLogEntry) != 0)
2533 {
George Liu0fda0f12021-11-16 10:06:17 +08002534 messages::internalError(asyncResp->res);
2535 return;
2536 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002537 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002538 }
2539 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002540 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002541 {
2542 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2543 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002544 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002545 }
George Liu0fda0f12021-11-16 10:06:17 +08002546 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002547}
Jason M. Billse1f26342018-07-18 12:12:00 -07002548
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002549inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002550{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002551 BMCWEB_ROUTE(app,
2552 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002553 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002554 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002555 [&app](const crow::Request& req,
2556 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2557 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002558 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002559 {
2560 return;
2561 }
2562 // Convert the unique ID back to a timestamp to find the entry
2563 uint64_t ts = 0;
2564 uint64_t index = 0;
2565 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2566 {
2567 return;
2568 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002569
Ed Tanous002d39b2022-05-31 08:59:27 -07002570 sd_journal* journalTmp = nullptr;
2571 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2572 if (ret < 0)
2573 {
2574 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2575 messages::internalError(asyncResp->res);
2576 return;
2577 }
2578 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2579 journalTmp, sd_journal_close);
2580 journalTmp = nullptr;
2581 // Go to the timestamp in the log and move to the entry at the
2582 // index tracking the unique ID
2583 std::string idStr;
2584 bool firstEntry = true;
2585 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2586 if (ret < 0)
2587 {
2588 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2589 << strerror(-ret);
2590 messages::internalError(asyncResp->res);
2591 return;
2592 }
2593 for (uint64_t i = 0; i <= index; i++)
2594 {
2595 sd_journal_next(journal.get());
2596 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2597 {
2598 messages::internalError(asyncResp->res);
2599 return;
2600 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002601 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002602 }
2603 // Confirm that the entry ID matches what was requested
2604 if (idStr != entryID)
2605 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002606 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002607 return;
2608 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002609
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002610 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002611 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002612 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002613 {
2614 messages::internalError(asyncResp->res);
2615 return;
2616 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002617 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002618 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002619}
2620
Claire Weinanfdd26902022-03-01 14:18:25 -08002621inline void
2622 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2623 const std::string& dumpType)
2624{
2625 std::string dumpPath;
2626 std::string overWritePolicy;
2627 bool collectDiagnosticDataSupported = false;
2628
2629 if (dumpType == "BMC")
2630 {
2631 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2632 overWritePolicy = "WrapsWhenFull";
2633 collectDiagnosticDataSupported = true;
2634 }
2635 else if (dumpType == "FaultLog")
2636 {
2637 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2638 overWritePolicy = "Unknown";
2639 collectDiagnosticDataSupported = false;
2640 }
2641 else if (dumpType == "System")
2642 {
2643 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2644 overWritePolicy = "WrapsWhenFull";
2645 collectDiagnosticDataSupported = true;
2646 }
2647 else
2648 {
2649 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2650 << dumpType;
2651 messages::internalError(asyncResp->res);
2652 return;
2653 }
2654
2655 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2656 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2657 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2658 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2659 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2660 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2661
2662 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002663 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002664 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2665 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2666 redfishDateTimeOffset.second;
2667
2668 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002669
2670 if (collectDiagnosticDataSupported)
2671 {
2672 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2673 ["target"] =
2674 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2675 }
Claire Weinan0d946212022-07-13 19:40:19 -07002676
2677 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2678 dbus::utility::getSubTreePaths(
2679 "/xyz/openbmc_project/dump", 0, interfaces,
2680 [asyncResp, dumpType, dumpPath](
2681 const boost::system::error_code& ec,
2682 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2683 if (ec)
2684 {
2685 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2686 << ec;
2687 // Assume that getting an error simply means there are no dump
2688 // LogServices. Return without adding any error response.
2689 return;
2690 }
2691
2692 const std::string dbusDumpPath =
2693 "/xyz/openbmc_project/dump/" +
2694 boost::algorithm::to_lower_copy(dumpType);
2695
2696 for (const std::string& path : subTreePaths)
2697 {
2698 if (path == dbusDumpPath)
2699 {
2700 asyncResp->res
2701 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2702 dumpPath + "/Actions/LogService.ClearLog";
2703 break;
2704 }
2705 }
2706 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002707}
2708
2709inline void handleLogServicesDumpServiceGet(
2710 crow::App& app, const std::string& dumpType, const crow::Request& req,
2711 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2712{
2713 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2714 {
2715 return;
2716 }
2717 getDumpServiceInfo(asyncResp, dumpType);
2718}
2719
Ed Tanous22d268c2022-05-19 09:39:07 -07002720inline void handleLogServicesDumpServiceComputerSystemGet(
2721 crow::App& app, const crow::Request& req,
2722 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2723 const std::string& chassisId)
2724{
2725 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2726 {
2727 return;
2728 }
2729 if (chassisId != "system")
2730 {
2731 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2732 return;
2733 }
2734 getDumpServiceInfo(asyncResp, "System");
2735}
2736
Claire Weinanfdd26902022-03-01 14:18:25 -08002737inline void handleLogServicesDumpEntriesCollectionGet(
2738 crow::App& app, const std::string& dumpType, const crow::Request& req,
2739 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2740{
2741 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2742 {
2743 return;
2744 }
2745 getDumpEntryCollection(asyncResp, dumpType);
2746}
2747
Ed Tanous22d268c2022-05-19 09:39:07 -07002748inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2749 crow::App& app, const crow::Request& req,
2750 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2751 const std::string& chassisId)
2752{
2753 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2754 {
2755 return;
2756 }
2757 if (chassisId != "system")
2758 {
2759 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2760 return;
2761 }
2762 getDumpEntryCollection(asyncResp, "System");
2763}
2764
Claire Weinanfdd26902022-03-01 14:18:25 -08002765inline void handleLogServicesDumpEntryGet(
2766 crow::App& app, const std::string& dumpType, const crow::Request& req,
2767 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2768 const std::string& dumpId)
2769{
2770 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2771 {
2772 return;
2773 }
2774 getDumpEntryById(asyncResp, dumpId, dumpType);
2775}
Ed Tanous22d268c2022-05-19 09:39:07 -07002776inline void handleLogServicesDumpEntryComputerSystemGet(
2777 crow::App& app, const crow::Request& req,
2778 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2779 const std::string& chassisId, const std::string& dumpId)
2780{
2781 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2782 {
2783 return;
2784 }
2785 if (chassisId != "system")
2786 {
2787 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2788 return;
2789 }
2790 getDumpEntryById(asyncResp, dumpId, "System");
2791}
Claire Weinanfdd26902022-03-01 14:18:25 -08002792
2793inline void handleLogServicesDumpEntryDelete(
2794 crow::App& app, const std::string& dumpType, const crow::Request& req,
2795 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2796 const std::string& dumpId)
2797{
2798 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2799 {
2800 return;
2801 }
2802 deleteDumpEntry(asyncResp, dumpId, dumpType);
2803}
2804
Ed Tanous22d268c2022-05-19 09:39:07 -07002805inline void handleLogServicesDumpEntryComputerSystemDelete(
2806 crow::App& app, const crow::Request& req,
2807 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2808 const std::string& chassisId, const std::string& dumpId)
2809{
2810 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2811 {
2812 return;
2813 }
2814 if (chassisId != "system")
2815 {
2816 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2817 return;
2818 }
2819 deleteDumpEntry(asyncResp, dumpId, "System");
2820}
2821
Claire Weinanfdd26902022-03-01 14:18:25 -08002822inline void handleLogServicesDumpCollectDiagnosticDataPost(
2823 crow::App& app, const std::string& dumpType, const crow::Request& req,
2824 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2825{
2826 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2827 {
2828 return;
2829 }
2830 createDump(asyncResp, req, dumpType);
2831}
2832
Ed Tanous22d268c2022-05-19 09:39:07 -07002833inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2834 crow::App& app, const crow::Request& req,
2835 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2836 const std::string& chassisId)
2837{
2838 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2839 {
2840 return;
2841 }
2842 if (chassisId != "system")
2843 {
2844 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2845 return;
2846 }
2847 createDump(asyncResp, req, "System");
2848}
2849
Claire Weinanfdd26902022-03-01 14:18:25 -08002850inline void handleLogServicesDumpClearLogPost(
2851 crow::App& app, const std::string& dumpType, const crow::Request& req,
2852 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2853{
2854 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2855 {
2856 return;
2857 }
2858 clearDump(asyncResp, dumpType);
2859}
2860
Ed Tanous22d268c2022-05-19 09:39:07 -07002861inline void handleLogServicesDumpClearLogComputerSystemPost(
2862 crow::App& app, const crow::Request& req,
2863 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2864 const std::string& chassisId)
2865{
2866 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2867 {
2868 return;
2869 }
2870 if (chassisId != "system")
2871 {
2872 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2873 return;
2874 }
2875 clearDump(asyncResp, "System");
2876}
2877
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002878inline void requestRoutesBMCDumpService(App& app)
2879{
2880 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002881 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002882 .methods(boost::beast::http::verb::get)(std::bind_front(
2883 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002884}
2885
2886inline void requestRoutesBMCDumpEntryCollection(App& app)
2887{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002888 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002889 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002890 .methods(boost::beast::http::verb::get)(std::bind_front(
2891 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002892}
2893
2894inline void requestRoutesBMCDumpEntry(App& app)
2895{
2896 BMCWEB_ROUTE(app,
2897 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002898 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002899 .methods(boost::beast::http::verb::get)(std::bind_front(
2900 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2901
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002902 BMCWEB_ROUTE(app,
2903 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002904 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002905 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2906 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002907}
2908
2909inline void requestRoutesBMCDumpCreate(App& app)
2910{
George Liu0fda0f12021-11-16 10:06:17 +08002911 BMCWEB_ROUTE(
2912 app,
2913 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002914 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002915 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002916 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2917 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002918}
2919
2920inline void requestRoutesBMCDumpClear(App& app)
2921{
George Liu0fda0f12021-11-16 10:06:17 +08002922 BMCWEB_ROUTE(
2923 app,
2924 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002925 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002926 .methods(boost::beast::http::verb::post)(std::bind_front(
2927 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2928}
2929
2930inline void requestRoutesFaultLogDumpService(App& app)
2931{
2932 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2933 .privileges(redfish::privileges::getLogService)
2934 .methods(boost::beast::http::verb::get)(std::bind_front(
2935 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2936}
2937
2938inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2939{
2940 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2941 .privileges(redfish::privileges::getLogEntryCollection)
2942 .methods(boost::beast::http::verb::get)(
2943 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2944 std::ref(app), "FaultLog"));
2945}
2946
2947inline void requestRoutesFaultLogDumpEntry(App& app)
2948{
2949 BMCWEB_ROUTE(app,
2950 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2951 .privileges(redfish::privileges::getLogEntry)
2952 .methods(boost::beast::http::verb::get)(std::bind_front(
2953 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2954
2955 BMCWEB_ROUTE(app,
2956 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2957 .privileges(redfish::privileges::deleteLogEntry)
2958 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2959 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2960}
2961
2962inline void requestRoutesFaultLogDumpClear(App& app)
2963{
2964 BMCWEB_ROUTE(
2965 app,
2966 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2967 .privileges(redfish::privileges::postLogService)
2968 .methods(boost::beast::http::verb::post)(std::bind_front(
2969 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002970}
2971
2972inline void requestRoutesSystemDumpService(App& app)
2973{
Ed Tanous22d268c2022-05-19 09:39:07 -07002974 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002975 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002976 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002977 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002978}
2979
2980inline void requestRoutesSystemDumpEntryCollection(App& app)
2981{
Ed Tanous22d268c2022-05-19 09:39:07 -07002982 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002983 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002984 .methods(boost::beast::http::verb::get)(std::bind_front(
2985 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2986 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002987}
2988
2989inline void requestRoutesSystemDumpEntry(App& app)
2990{
2991 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002992 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002993 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002994 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002995 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002996
2997 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002998 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002999 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003000 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003001 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003002}
3003
3004inline void requestRoutesSystemDumpCreate(App& app)
3005{
George Liu0fda0f12021-11-16 10:06:17 +08003006 BMCWEB_ROUTE(
3007 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003008 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003009 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003010 .methods(boost::beast::http::verb::post)(std::bind_front(
3011 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3012 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003013}
3014
3015inline void requestRoutesSystemDumpClear(App& app)
3016{
George Liu0fda0f12021-11-16 10:06:17 +08003017 BMCWEB_ROUTE(
3018 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003019 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003020 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003021 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003022 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003023}
3024
3025inline void requestRoutesCrashdumpService(App& app)
3026{
3027 // Note: Deviated from redfish privilege registry for GET & HEAD
3028 // method for security reasons.
3029 /**
3030 * Functions triggers appropriate requests on DBus
3031 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003032 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003033 // This is incorrect, should be:
3034 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003035 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003036 .methods(boost::beast::http::verb::get)(
3037 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003038 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3039 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003040 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003041 {
3042 return;
3043 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003044 if (systemName != "system")
3045 {
3046 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3047 systemName);
3048 return;
3049 }
3050
Ed Tanous002d39b2022-05-31 08:59:27 -07003051 // Copy over the static data to include the entries added by
3052 // SubRoute
3053 asyncResp->res.jsonValue["@odata.id"] =
3054 "/redfish/v1/Systems/system/LogServices/Crashdump";
3055 asyncResp->res.jsonValue["@odata.type"] =
3056 "#LogService.v1_2_0.LogService";
3057 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3058 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303059 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003060 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3061 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303062
Ed Tanous002d39b2022-05-31 08:59:27 -07003063 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003064 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003065 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3066 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3067 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303068
Ed Tanous002d39b2022-05-31 08:59:27 -07003069 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003070 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003071 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3072 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3073 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3074 ["target"] =
3075 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003076 });
3077}
3078
3079void inline requestRoutesCrashdumpClear(App& app)
3080{
George Liu0fda0f12021-11-16 10:06:17 +08003081 BMCWEB_ROUTE(
3082 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003083 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003084 // This is incorrect, should be:
3085 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003086 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003087 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003088 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003089 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3090 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003091 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003092 {
3093 return;
3094 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003095 if (systemName != "system")
3096 {
3097 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3098 systemName);
3099 return;
3100 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003101 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003102 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003103 const std::string&) {
3104 if (ec)
3105 {
3106 messages::internalError(asyncResp->res);
3107 return;
3108 }
3109 messages::success(asyncResp->res);
3110 },
3111 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3112 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003113}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003114
zhanghch058d1b46d2021-04-01 11:18:24 +08003115static void
3116 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3117 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003118{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003119 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003120 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003121 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003122 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003123 if (ec)
3124 {
3125 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3126 if (ec.value() ==
3127 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003128 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003129 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003130 }
3131 else
3132 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003133 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003134 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003135 return;
3136 }
3137
3138 std::string timestamp{};
3139 std::string filename{};
3140 std::string logfile{};
3141 parseCrashdumpParameters(params, filename, timestamp, logfile);
3142
3143 if (filename.empty() || timestamp.empty())
3144 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003145 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003146 return;
3147 }
3148
3149 std::string crashdumpURI =
3150 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3151 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003152 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003153 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003154 logEntry["@odata.id"] = boost::urls::format(
3155 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3156 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003157 logEntry["Name"] = "CPU Crashdump";
3158 logEntry["Id"] = logID;
3159 logEntry["EntryType"] = "Oem";
3160 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3161 logEntry["DiagnosticDataType"] = "OEM";
3162 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3163 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003164
3165 // If logEntryJson references an array of LogEntry resources
3166 // ('Members' list), then push this as a new entry, otherwise set it
3167 // directly
3168 if (logEntryJson.is_array())
3169 {
3170 logEntryJson.push_back(logEntry);
3171 asyncResp->res.jsonValue["Members@odata.count"] =
3172 logEntryJson.size();
3173 }
3174 else
3175 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003176 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003177 }
3178 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003179 sdbusplus::asio::getAllProperties(
3180 *crow::connections::systemBus, crashdumpObject,
3181 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3182 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003183}
3184
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003185inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003186{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003187 // Note: Deviated from redfish privilege registry for GET & HEAD
3188 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003189 /**
3190 * Functions triggers appropriate requests on DBus
3191 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003192 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003193 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003194 // This is incorrect, should be.
3195 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003196 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003197 .methods(boost::beast::http::verb::get)(
3198 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003199 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3200 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003201 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003202 {
3203 return;
3204 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003205 if (systemName != "system")
3206 {
3207 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3208 systemName);
3209 return;
3210 }
3211
George Liu7a1dbc42022-12-07 16:03:22 +08003212 constexpr std::array<std::string_view, 1> interfaces = {
3213 crashdumpInterface};
3214 dbus::utility::getSubTreePaths(
3215 "/", 0, interfaces,
3216 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003217 const std::vector<std::string>& resp) {
3218 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003219 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003220 if (ec.value() !=
3221 boost::system::errc::no_such_file_or_directory)
3222 {
3223 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3224 << ec.message();
3225 messages::internalError(asyncResp->res);
3226 return;
3227 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003228 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003229 asyncResp->res.jsonValue["@odata.type"] =
3230 "#LogEntryCollection.LogEntryCollection";
3231 asyncResp->res.jsonValue["@odata.id"] =
3232 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3233 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3234 asyncResp->res.jsonValue["Description"] =
3235 "Collection of Crashdump Entries";
3236 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3237 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003238
Ed Tanous002d39b2022-05-31 08:59:27 -07003239 for (const std::string& path : resp)
3240 {
3241 const sdbusplus::message::object_path objPath(path);
3242 // Get the log ID
3243 std::string logID = objPath.filename();
3244 if (logID.empty())
3245 {
3246 continue;
3247 }
3248 // Add the log entry to the array
3249 logCrashdumpEntry(asyncResp, logID,
3250 asyncResp->res.jsonValue["Members"]);
3251 }
George Liu7a1dbc42022-12-07 16:03:22 +08003252 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003253 });
3254}
Ed Tanous1da66f72018-07-27 16:13:37 -07003255
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003256inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003257{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003258 // Note: Deviated from redfish privilege registry for GET & HEAD
3259 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003260
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003261 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003262 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003263 // this is incorrect, should be
3264 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003265 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003266 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003267 [&app](const crow::Request& req,
3268 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003269 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003270 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003271 {
3272 return;
3273 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003274 if (systemName != "system")
3275 {
3276 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3277 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003278 return;
3279 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003280 const std::string& logID = param;
3281 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3282 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003283}
Ed Tanous1da66f72018-07-27 16:13:37 -07003284
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003285inline void requestRoutesCrashdumpFile(App& app)
3286{
3287 // Note: Deviated from redfish privilege registry for GET & HEAD
3288 // method for security reasons.
3289 BMCWEB_ROUTE(
3290 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003291 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003292 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003293 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003294 [](const crow::Request& req,
3295 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003296 const std::string& systemName, const std::string& logID,
3297 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003298 // Do not call getRedfishRoute here since the crashdump file is not a
3299 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003300
3301 if (systemName != "system")
3302 {
3303 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3304 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003305 return;
3306 }
3307
Ed Tanous002d39b2022-05-31 08:59:27 -07003308 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003309 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003310 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003311 const std::vector<
3312 std::pair<std::string, dbus::utility::DbusVariantType>>&
3313 resp) {
3314 if (ec)
3315 {
3316 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3317 messages::internalError(asyncResp->res);
3318 return;
3319 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003320
Ed Tanous002d39b2022-05-31 08:59:27 -07003321 std::string dbusFilename{};
3322 std::string dbusTimestamp{};
3323 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003324
Ed Tanous002d39b2022-05-31 08:59:27 -07003325 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3326 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003327
Ed Tanous002d39b2022-05-31 08:59:27 -07003328 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3329 dbusFilepath.empty())
3330 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003331 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003332 return;
3333 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003334
Ed Tanous002d39b2022-05-31 08:59:27 -07003335 // Verify the file name parameter is correct
3336 if (fileName != dbusFilename)
3337 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003338 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003339 return;
3340 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003341
Ed Tanous002d39b2022-05-31 08:59:27 -07003342 if (!std::filesystem::exists(dbusFilepath))
3343 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003344 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003345 return;
3346 }
3347 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3348 asyncResp->res.body() =
3349 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003350
Ed Tanous002d39b2022-05-31 08:59:27 -07003351 // Configure this to be a file download when accessed
3352 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003353 asyncResp->res.addHeader(
3354 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003355 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003356 sdbusplus::asio::getAllProperties(
3357 *crow::connections::systemBus, crashdumpObject,
3358 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3359 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003360 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003361}
3362
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003363enum class OEMDiagnosticType
3364{
3365 onDemand,
3366 telemetry,
3367 invalid,
3368};
3369
Ed Tanous26ccae32023-02-16 10:28:44 -08003370inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003371{
3372 if (oemDiagStr == "OnDemand")
3373 {
3374 return OEMDiagnosticType::onDemand;
3375 }
3376 if (oemDiagStr == "Telemetry")
3377 {
3378 return OEMDiagnosticType::telemetry;
3379 }
3380
3381 return OEMDiagnosticType::invalid;
3382}
3383
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003384inline void requestRoutesCrashdumpCollect(App& app)
3385{
3386 // Note: Deviated from redfish privilege registry for GET & HEAD
3387 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003388 BMCWEB_ROUTE(
3389 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003390 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003391 // The below is incorrect; Should be ConfigureManager
3392 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003393 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003394 .methods(boost::beast::http::verb::post)(
3395 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003396 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3397 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003398 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003399 {
3400 return;
3401 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003402
3403 if (systemName != "system")
3404 {
3405 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3406 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003407 return;
3408 }
3409
Ed Tanous002d39b2022-05-31 08:59:27 -07003410 std::string diagnosticDataType;
3411 std::string oemDiagnosticDataType;
3412 if (!redfish::json_util::readJsonAction(
3413 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3414 "OEMDiagnosticDataType", oemDiagnosticDataType))
3415 {
3416 return;
3417 }
3418
3419 if (diagnosticDataType != "OEM")
3420 {
3421 BMCWEB_LOG_ERROR
3422 << "Only OEM DiagnosticDataType supported for Crashdump";
3423 messages::actionParameterValueFormatError(
3424 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3425 "CollectDiagnosticData");
3426 return;
3427 }
3428
3429 OEMDiagnosticType oemDiagType =
3430 getOEMDiagnosticType(oemDiagnosticDataType);
3431
3432 std::string iface;
3433 std::string method;
3434 std::string taskMatchStr;
3435 if (oemDiagType == OEMDiagnosticType::onDemand)
3436 {
3437 iface = crashdumpOnDemandInterface;
3438 method = "GenerateOnDemandLog";
3439 taskMatchStr = "type='signal',"
3440 "interface='org.freedesktop.DBus.Properties',"
3441 "member='PropertiesChanged',"
3442 "arg0namespace='com.intel.crashdump'";
3443 }
3444 else if (oemDiagType == OEMDiagnosticType::telemetry)
3445 {
3446 iface = crashdumpTelemetryInterface;
3447 method = "GenerateTelemetryLog";
3448 taskMatchStr = "type='signal',"
3449 "interface='org.freedesktop.DBus.Properties',"
3450 "member='PropertiesChanged',"
3451 "arg0namespace='com.intel.crashdump'";
3452 }
3453 else
3454 {
3455 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3456 << oemDiagnosticDataType;
3457 messages::actionParameterValueFormatError(
3458 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3459 "CollectDiagnosticData");
3460 return;
3461 }
3462
3463 auto collectCrashdumpCallback =
3464 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003465 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003466 const std::string&) mutable {
3467 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003468 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003469 if (ec.value() == boost::system::errc::operation_not_supported)
3470 {
3471 messages::resourceInStandby(asyncResp->res);
3472 }
3473 else if (ec.value() ==
3474 boost::system::errc::device_or_resource_busy)
3475 {
3476 messages::serviceTemporarilyUnavailable(asyncResp->res,
3477 "60");
3478 }
3479 else
3480 {
3481 messages::internalError(asyncResp->res);
3482 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003483 return;
3484 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003485 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003486 [](const boost::system::error_code& err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003487 const std::shared_ptr<task::TaskData>& taskData) {
3488 if (!err)
3489 {
3490 taskData->messages.emplace_back(messages::taskCompletedOK(
3491 std::to_string(taskData->index)));
3492 taskData->state = "Completed";
3493 }
3494 return task::completed;
3495 },
3496 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003497
Ed Tanous002d39b2022-05-31 08:59:27 -07003498 task->startTimer(std::chrono::minutes(5));
3499 task->populateResp(asyncResp->res);
3500 task->payload.emplace(std::move(payload));
3501 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003502
Ed Tanous002d39b2022-05-31 08:59:27 -07003503 crow::connections::systemBus->async_method_call(
3504 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3505 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003506 });
3507}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003508
Andrew Geisslercb92c032018-08-17 07:56:14 -07003509/**
3510 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3511 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003512inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003513{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003514 /**
3515 * Function handles POST method request.
3516 * The Clear Log actions does not require any parameter.The action deletes
3517 * all entries found in the Entries collection for this Log Service.
3518 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003519
George Liu0fda0f12021-11-16 10:06:17 +08003520 BMCWEB_ROUTE(
3521 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003522 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003523 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003524 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003525 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003526 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3527 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003528 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003529 {
3530 return;
3531 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003532 if (systemName != "system")
3533 {
3534 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3535 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003536 return;
3537 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003538 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003539
Ed Tanous002d39b2022-05-31 08:59:27 -07003540 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003541 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003542 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3543 if (ec)
3544 {
3545 // TODO Handle for specific error code
3546 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3547 asyncResp->res.result(
3548 boost::beast::http::status::internal_server_error);
3549 return;
3550 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003551
Ed Tanous002d39b2022-05-31 08:59:27 -07003552 asyncResp->res.result(boost::beast::http::status::no_content);
3553 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003554
Ed Tanous002d39b2022-05-31 08:59:27 -07003555 // Make call to Logging service to request Clear Log
3556 crow::connections::systemBus->async_method_call(
3557 respHandler, "xyz.openbmc_project.Logging",
3558 "/xyz/openbmc_project/logging",
3559 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3560 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003561}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003562
3563/****************************************************
3564 * Redfish PostCode interfaces
3565 * using DBUS interface: getPostCodesTS
3566 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003567inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003568{
Ed Tanous22d268c2022-05-19 09:39:07 -07003569 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003570 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003571 .methods(boost::beast::http::verb::get)(
3572 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003573 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3574 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003575 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003576 {
3577 return;
3578 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003579 if (systemName != "system")
3580 {
3581 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3582 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003583 return;
3584 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003585 asyncResp->res.jsonValue["@odata.id"] =
3586 "/redfish/v1/Systems/system/LogServices/PostCodes";
3587 asyncResp->res.jsonValue["@odata.type"] =
3588 "#LogService.v1_1_0.LogService";
3589 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3590 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003591 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003592 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3593 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3594 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303595
Ed Tanous002d39b2022-05-31 08:59:27 -07003596 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003597 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003598 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3599 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3600 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303601
Ed Tanous002d39b2022-05-31 08:59:27 -07003602 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3603 {"target",
3604 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003605 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003606}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003607
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003608inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003609{
George Liu0fda0f12021-11-16 10:06:17 +08003610 BMCWEB_ROUTE(
3611 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003612 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003613 // The following privilege is incorrect; It should be ConfigureManager
3614 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003615 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003616 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003617 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003618 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3619 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003620 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003621 {
3622 return;
3623 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003624 if (systemName != "system")
3625 {
3626 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3627 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003628 return;
3629 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003630 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003631
Ed Tanous002d39b2022-05-31 08:59:27 -07003632 // Make call to post-code service to request clear all
3633 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003634 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003635 if (ec)
3636 {
3637 // TODO Handle for specific error code
3638 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3639 << ec;
3640 asyncResp->res.result(
3641 boost::beast::http::status::internal_server_error);
3642 messages::internalError(asyncResp->res);
3643 return;
3644 }
3645 },
3646 "xyz.openbmc_project.State.Boot.PostCode0",
3647 "/xyz/openbmc_project/State/Boot/PostCode0",
3648 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3649 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003650}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003651
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003652/**
3653 * @brief Parse post code ID and get the current value and index value
3654 * eg: postCodeID=B1-2, currentValue=1, index=2
3655 *
3656 * @param[in] postCodeID Post Code ID
3657 * @param[out] currentValue Current value
3658 * @param[out] index Index value
3659 *
3660 * @return bool true if the parsing is successful, false the parsing fails
3661 */
3662inline static bool parsePostCode(const std::string& postCodeID,
3663 uint64_t& currentValue, uint16_t& index)
3664{
3665 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003666 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003667 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3668 {
3669 return false;
3670 }
3671
Patrick Williams84396af2023-05-11 11:47:45 -05003672 auto start = std::next(split[0].begin());
3673 auto end = split[0].end();
3674 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003675
Patrick Williams84396af2023-05-11 11:47:45 -05003676 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003677 {
3678 return false;
3679 }
3680
Patrick Williams84396af2023-05-11 11:47:45 -05003681 start = split[1].begin();
3682 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003683
Patrick Williams84396af2023-05-11 11:47:45 -05003684 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003685
Patrick Williams84396af2023-05-11 11:47:45 -05003686 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003687}
3688
3689static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003690 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303691 const boost::container::flat_map<
3692 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003693 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3694 const uint64_t skip = 0, const uint64_t top = 0)
3695{
3696 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003697 const registries::Message* message =
3698 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003699
3700 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003701 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303702 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3703 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003704 {
3705 currentCodeIndex++;
3706 std::string postcodeEntryID =
3707 "B" + std::to_string(bootIndex) + "-" +
3708 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3709
3710 uint64_t usecSinceEpoch = code.first;
3711 uint64_t usTimeOffset = 0;
3712
3713 if (1 == currentCodeIndex)
3714 { // already incremented
3715 firstCodeTimeUs = code.first;
3716 }
3717 else
3718 {
3719 usTimeOffset = code.first - firstCodeTimeUs;
3720 }
3721
3722 // skip if no specific codeIndex is specified and currentCodeIndex does
3723 // not fall between top and skip
3724 if ((codeIndex == 0) &&
3725 (currentCodeIndex <= skip || currentCodeIndex > top))
3726 {
3727 continue;
3728 }
3729
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003730 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003731 // currentIndex
3732 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3733 {
3734 // This is done for simplicity. 1st entry is needed to calculate
3735 // time offset. To improve efficiency, one can get to the entry
3736 // directly (possibly with flatmap's nth method)
3737 continue;
3738 }
3739
3740 // currentCodeIndex is within top and skip or equal to specified code
3741 // index
3742
3743 // Get the Created time from the timestamp
3744 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003745 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003746
3747 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3748 std::ostringstream hexCode;
3749 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303750 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003751 std::ostringstream timeOffsetStr;
3752 // Set Fixed -Point Notation
3753 timeOffsetStr << std::fixed;
3754 // Set precision to 4 digits
3755 timeOffsetStr << std::setprecision(4);
3756 // Add double to stream
3757 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3758 std::vector<std::string> messageArgs = {
3759 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3760
3761 // Get MessageArgs template from message registry
3762 std::string msg;
3763 if (message != nullptr)
3764 {
3765 msg = message->message;
3766
3767 // fill in this post code value
3768 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003769 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003770 {
3771 std::string argStr = "%" + std::to_string(++i);
3772 size_t argPos = msg.find(argStr);
3773 if (argPos != std::string::npos)
3774 {
3775 msg.replace(argPos, argStr.length(), messageArg);
3776 }
3777 }
3778 }
3779
Tim Leed4342a92020-04-27 11:47:58 +08003780 // Get Severity template from message registry
3781 std::string severity;
3782 if (message != nullptr)
3783 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003784 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003785 }
3786
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003787 // Format entry
3788 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003789 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003790 bmcLogEntry["@odata.id"] = boost::urls::format(
3791 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
3792 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003793 bmcLogEntry["Name"] = "POST Code Log Entry";
3794 bmcLogEntry["Id"] = postcodeEntryID;
3795 bmcLogEntry["Message"] = std::move(msg);
3796 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3797 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3798 bmcLogEntry["EntryType"] = "Event";
3799 bmcLogEntry["Severity"] = std::move(severity);
3800 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003801 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3802 {
3803 bmcLogEntry["AdditionalDataURI"] =
3804 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3805 postcodeEntryID + "/attachment";
3806 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003807
3808 // codeIndex is only specified when querying single entry, return only
3809 // that entry in this case
3810 if (codeIndex != 0)
3811 {
Ed Tanousac106bf2023-06-07 09:24:59 -07003812 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003813 return true;
3814 }
3815
Ed Tanousac106bf2023-06-07 09:24:59 -07003816 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05003817 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003818 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003819
3820 // Return value is always false when querying multiple entries
3821 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003822}
3823
Ed Tanousac106bf2023-06-07 09:24:59 -07003824static void
3825 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3826 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003827{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003828 uint16_t bootIndex = 0;
3829 uint64_t codeIndex = 0;
3830 if (!parsePostCode(entryId, codeIndex, bootIndex))
3831 {
3832 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07003833 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003834 return;
3835 }
3836
3837 if (bootIndex == 0 || codeIndex == 0)
3838 {
3839 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07003840 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003841 return;
3842 }
3843
ZhikuiRena3316fc2020-01-29 14:58:08 -08003844 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07003845 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003846 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303847 const boost::container::flat_map<
3848 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3849 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003850 if (ec)
3851 {
3852 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
Ed Tanousac106bf2023-06-07 09:24:59 -07003853 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07003854 return;
3855 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003856
Ed Tanous002d39b2022-05-31 08:59:27 -07003857 if (postcode.empty())
3858 {
Ed Tanousac106bf2023-06-07 09:24:59 -07003859 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003860 return;
3861 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003862
Ed Tanousac106bf2023-06-07 09:24:59 -07003863 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003864 {
Ed Tanousac106bf2023-06-07 09:24:59 -07003865 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003866 return;
3867 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003868 },
Jonathan Doman15124762021-01-07 17:54:17 -08003869 "xyz.openbmc_project.State.Boot.PostCode0",
3870 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003871 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3872 bootIndex);
3873}
3874
Ed Tanousac106bf2023-06-07 09:24:59 -07003875static void
3876 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3877 const uint16_t bootIndex, const uint16_t bootCount,
3878 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003879{
3880 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07003881 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003882 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303883 const boost::container::flat_map<
3884 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3885 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003886 if (ec)
3887 {
3888 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
Ed Tanousac106bf2023-06-07 09:24:59 -07003889 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07003890 return;
3891 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003892
Ed Tanous002d39b2022-05-31 08:59:27 -07003893 uint64_t endCount = entryCount;
3894 if (!postcode.empty())
3895 {
3896 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003897 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003898 {
Patrick Williams89492a12023-05-10 07:51:34 -05003899 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
3900 entryCount) -
3901 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003902 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003903 std::min(static_cast<uint64_t>(top + skip), endCount) -
3904 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003905
Ed Tanousac106bf2023-06-07 09:24:59 -07003906 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
3907 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003908 }
Ed Tanousac106bf2023-06-07 09:24:59 -07003909 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003910 }
3911
3912 // continue to previous bootIndex
3913 if (bootIndex < bootCount)
3914 {
Ed Tanousac106bf2023-06-07 09:24:59 -07003915 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07003916 bootCount, endCount, skip, top);
3917 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003918 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003919 {
Ed Tanousac106bf2023-06-07 09:24:59 -07003920 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07003921 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3922 std::to_string(skip + top);
3923 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003924 },
Jonathan Doman15124762021-01-07 17:54:17 -08003925 "xyz.openbmc_project.State.Boot.PostCode0",
3926 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003927 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3928 bootIndex);
3929}
3930
zhanghch058d1b46d2021-04-01 11:18:24 +08003931static void
Ed Tanousac106bf2023-06-07 09:24:59 -07003932 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003933 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003934{
3935 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003936 sdbusplus::asio::getProperty<uint16_t>(
3937 *crow::connections::systemBus,
3938 "xyz.openbmc_project.State.Boot.PostCode0",
3939 "/xyz/openbmc_project/State/Boot/PostCode0",
3940 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07003941 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
3942 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003943 if (ec)
3944 {
3945 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
Ed Tanousac106bf2023-06-07 09:24:59 -07003946 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07003947 return;
3948 }
Ed Tanousac106bf2023-06-07 09:24:59 -07003949 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003950 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003951}
3952
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003953inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003954{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003955 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003956 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003957 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003958 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003959 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003960 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3961 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003962 query_param::QueryCapabilities capabilities = {
3963 .canDelegateTop = true,
3964 .canDelegateSkip = true,
3965 };
3966 query_param::Query delegatedQuery;
3967 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003968 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003969 {
3970 return;
3971 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003972
3973 if (systemName != "system")
3974 {
3975 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3976 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003977 return;
3978 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003979 asyncResp->res.jsonValue["@odata.type"] =
3980 "#LogEntryCollection.LogEntryCollection";
3981 asyncResp->res.jsonValue["@odata.id"] =
3982 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3983 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3984 asyncResp->res.jsonValue["Description"] =
3985 "Collection of POST Code Log Entries";
3986 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3987 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003988 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003989 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003990 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003991 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003992}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003993
George Liu647b3cd2021-07-05 12:43:56 +08003994inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3995{
George Liu0fda0f12021-11-16 10:06:17 +08003996 BMCWEB_ROUTE(
3997 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003998 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003999 .privileges(redfish::privileges::getLogEntry)
4000 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004001 [&app](const crow::Request& req,
4002 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004003 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004004 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004005 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004006 {
4007 return;
4008 }
Matt Spinler72e21372023-04-19 12:53:33 -05004009 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004010 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004011 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004012 {
4013 asyncResp->res.result(boost::beast::http::status::bad_request);
4014 return;
4015 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004016 if (systemName != "system")
4017 {
4018 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4019 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004020 return;
4021 }
George Liu647b3cd2021-07-05 12:43:56 +08004022
Ed Tanous002d39b2022-05-31 08:59:27 -07004023 uint64_t currentValue = 0;
4024 uint16_t index = 0;
4025 if (!parsePostCode(postCodeID, currentValue, index))
4026 {
4027 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4028 return;
4029 }
George Liu647b3cd2021-07-05 12:43:56 +08004030
Ed Tanous002d39b2022-05-31 08:59:27 -07004031 crow::connections::systemBus->async_method_call(
4032 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004033 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004034 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4035 postcodes) {
4036 if (ec.value() == EBADR)
4037 {
4038 messages::resourceNotFound(asyncResp->res, "LogEntry",
4039 postCodeID);
4040 return;
4041 }
4042 if (ec)
4043 {
4044 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4045 messages::internalError(asyncResp->res);
4046 return;
4047 }
George Liu647b3cd2021-07-05 12:43:56 +08004048
Ed Tanous002d39b2022-05-31 08:59:27 -07004049 size_t value = static_cast<size_t>(currentValue) - 1;
4050 if (value == std::string::npos || postcodes.size() < currentValue)
4051 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004052 BMCWEB_LOG_WARNING << "Wrong currentValue value";
Ed Tanous002d39b2022-05-31 08:59:27 -07004053 messages::resourceNotFound(asyncResp->res, "LogEntry",
4054 postCodeID);
4055 return;
4056 }
George Liu647b3cd2021-07-05 12:43:56 +08004057
Ed Tanous002d39b2022-05-31 08:59:27 -07004058 const auto& [tID, c] = postcodes[value];
4059 if (c.empty())
4060 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004061 BMCWEB_LOG_WARNING << "No found post code data";
Ed Tanous002d39b2022-05-31 08:59:27 -07004062 messages::resourceNotFound(asyncResp->res, "LogEntry",
4063 postCodeID);
4064 return;
4065 }
4066 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4067 const char* d = reinterpret_cast<const char*>(c.data());
4068 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004069
Ed Tanousd9f6c622022-03-17 09:12:17 -07004070 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004071 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004072 asyncResp->res.addHeader(
4073 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004074 asyncResp->res.body() = crow::utility::base64encode(strData);
4075 },
4076 "xyz.openbmc_project.State.Boot.PostCode0",
4077 "/xyz/openbmc_project/State/Boot/PostCode0",
4078 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4079 });
George Liu647b3cd2021-07-05 12:43:56 +08004080}
4081
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004082inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004083{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004084 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004085 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004086 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004087 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004088 [&app](const crow::Request& req,
4089 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004090 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004091 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004092 {
4093 return;
4094 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004095 if (systemName != "system")
4096 {
4097 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4098 systemName);
4099 return;
4100 }
4101
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004102 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004103 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004104}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004105
Ed Tanous1da66f72018-07-27 16:13:37 -07004106} // namespace redfish