blob: aeb1d191bf4658eae8f3714eac2b63db310c4298 [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>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020045#include <sdbusplus/asio/property.hpp>
46#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050047
George Liu7a1dbc42022-12-07 16:03:22 +080048#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080049#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070050#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080051#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070052#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070053#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080054#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070055
56namespace redfish
57{
58
Patrick Williams89492a12023-05-10 07:51:34 -050059constexpr const char* crashdumpObject = "com.intel.crashdump";
60constexpr const char* crashdumpPath = "/com/intel/crashdump";
61constexpr const char* crashdumpInterface = "com.intel.crashdump";
62constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070063 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050064constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070065 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050066constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070067 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070068
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060069enum class DumpCreationProgress
70{
71 DUMP_CREATE_SUCCESS,
72 DUMP_CREATE_FAILED,
73 DUMP_CREATE_INPROGRESS
74};
75
James Feistf6150402019-01-08 10:36:20 -080076namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070077
Gunnar Mills1214b7e2020-06-04 10:11:30 -050078inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070079{
Ed Tanousd4d25792020-09-29 15:15:03 -070080 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
81 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
82 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
83 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070084 {
85 return "Critical";
86 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070087 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
88 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
89 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070090 {
91 return "OK";
92 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070093 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070094 {
95 return "Warning";
96 }
97 return "";
98}
99
Abhishek Patel9017faf2021-09-14 22:48:55 -0500100inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
101{
102 std::optional<bool> notifyAction;
103 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
104 {
105 notifyAction = true;
106 }
107 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
108 {
109 notifyAction = false;
110 }
111
112 return notifyAction;
113}
114
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700115inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800116 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700117 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700118{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500119 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700120 size_t length = 0;
121 int ret = 0;
122 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800123 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
124 const void** dataVoid = reinterpret_cast<const void**>(&data);
125
126 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700127 if (ret < 0)
128 {
129 return ret;
130 }
Ed Tanous39e77502019-03-04 17:35:53 -0800131 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700132 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000133 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700134 return ret;
135}
136
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700137inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800138 std::string_view field, const int& base,
139 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700140{
141 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800142 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700143 // Get the metadata from the requested field of the journal entry
144 ret = getJournalMetadata(journal, field, metadata);
145 if (ret < 0)
146 {
147 return ret;
148 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000149 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700150 return ret;
151}
152
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700153inline static bool getEntryTimestamp(sd_journal* journal,
154 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800155{
156 int ret = 0;
157 uint64_t timestamp = 0;
158 ret = sd_journal_get_realtime_usec(journal, &timestamp);
159 if (ret < 0)
160 {
161 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
162 << strerror(-ret);
163 return false;
164 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300165 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500166 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800167}
Ed Tanous50b8a432022-02-03 16:29:50 -0800168
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700169inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
170 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700171{
172 int ret = 0;
173 static uint64_t prevTs = 0;
174 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700175 if (firstEntry)
176 {
177 prevTs = 0;
178 }
179
Jason M. Bills16428a12018-11-02 12:42:29 -0700180 // Get the entry timestamp
181 uint64_t curTs = 0;
182 ret = sd_journal_get_realtime_usec(journal, &curTs);
183 if (ret < 0)
184 {
185 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
186 << strerror(-ret);
187 return false;
188 }
189 // If the timestamp isn't unique, increment the index
190 if (curTs == prevTs)
191 {
192 index++;
193 }
194 else
195 {
196 // Otherwise, reset it
197 index = 0;
198 }
199 // Save the timestamp
200 prevTs = curTs;
201
202 entryID = std::to_string(curTs);
203 if (index > 0)
204 {
205 entryID += "_" + std::to_string(index);
206 }
207 return true;
208}
209
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500210static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700211 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700212{
Ed Tanous271584a2019-07-09 16:24:22 -0700213 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700214 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700215 if (firstEntry)
216 {
217 prevTs = 0;
218 }
219
Jason M. Bills95820182019-04-22 16:25:34 -0700220 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700221 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700222 std::tm timeStruct = {};
223 std::istringstream entryStream(logEntry);
224 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
225 {
226 curTs = std::mktime(&timeStruct);
227 }
228 // If the timestamp isn't unique, increment the index
229 if (curTs == prevTs)
230 {
231 index++;
232 }
233 else
234 {
235 // Otherwise, reset it
236 index = 0;
237 }
238 // Save the timestamp
239 prevTs = curTs;
240
241 entryID = std::to_string(curTs);
242 if (index > 0)
243 {
244 entryID += "_" + std::to_string(index);
245 }
246 return true;
247}
248
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700249inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
251 const std::string& entryID, uint64_t& timestamp,
252 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700253{
254 if (entryID.empty())
255 {
256 return false;
257 }
258 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800259 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700260
Ed Tanous81ce6092020-12-17 16:54:55 +0000261 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800262 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700263 {
264 // Timestamp has an index
265 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800266 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700267 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700268 auto [ptr, ec] = std::from_chars(
269 indexStr.data(), indexStr.data() + indexStr.size(), index);
270 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700271 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800272 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700273 return false;
274 }
275 }
276 // Timestamp has no index
Patrick Williams89492a12023-05-10 07:51:34 -0500277 auto [ptr, ec] = std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(),
278 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 }
567 entriesArray.push_back(std::move(thisEntry));
568 }
569 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500570 },
571 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
572 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
573}
574
zhanghch058d1b46d2021-04-01 11:18:24 +0800575inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700576 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500578{
Claire Weinanfdd26902022-03-01 14:18:25 -0800579 std::string entriesPath = getDumpEntriesPath(dumpType);
580 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500582 messages::internalError(asyncResp->res);
583 return;
584 }
585
586 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800587 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800588 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700589 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 if (ec)
591 {
592 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
593 messages::internalError(asyncResp->res);
594 return;
595 }
596
597 bool foundDumpEntry = false;
598 std::string dumpEntryPath =
599 "/xyz/openbmc_project/dump/" +
600 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
601
602 for (const auto& objectPath : resp)
603 {
604 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500605 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700606 continue;
607 }
608
609 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700610 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700611 uint64_t size = 0;
612 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600613 std::string originatorId;
614 log_entry::OriginatorTypes originatorType =
615 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700616
Claire Weinanaefe3782022-07-15 19:17:19 -0700617 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600618 timestampUs, originatorId,
619 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700620
621 if (dumpStatus !=
622 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
623 !dumpStatus.empty())
624 {
625 // Dump status is not Complete
626 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200627 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
628 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500629 return;
630 }
631
Ed Tanous002d39b2022-05-31 08:59:27 -0700632 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600633 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800634 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700635 asyncResp->res.jsonValue["Id"] = entryID;
636 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700638 asyncResp->res.jsonValue["Created"] =
639 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500640
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600641 if (!originatorId.empty())
642 {
643 asyncResp->res.jsonValue["Originator"] = originatorId;
644 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
645 }
646
Ed Tanous002d39b2022-05-31 08:59:27 -0700647 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500648 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700649 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
650 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800651 entriesPath + entryID + "/attachment";
652 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500653 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700654 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500655 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
657 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
658 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800659 entriesPath + entryID + "/attachment";
660 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500661 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700662 }
663 if (!foundDumpEntry)
664 {
665 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
666 messages::internalError(asyncResp->res);
667 return;
668 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500669 },
670 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
671 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
672}
673
zhanghch058d1b46d2021-04-01 11:18:24 +0800674inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800675 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500676 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500677{
Ed Tanous002d39b2022-05-31 08:59:27 -0700678 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800679 [asyncResp, entryID](const boost::system::error_code& ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500680 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
681 if (ec)
682 {
George Liu3de8d8b2021-03-22 17:49:39 +0800683 if (ec.value() == EBADR)
684 {
685 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
686 return;
687 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500688 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800689 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500690 messages::internalError(asyncResp->res);
691 return;
692 }
693 };
694 crow::connections::systemBus->async_method_call(
695 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500696 "/xyz/openbmc_project/dump/" +
697 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
698 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500699 "xyz.openbmc_project.Object.Delete", "Delete");
700}
701
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600702inline DumpCreationProgress
703 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500704{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600705 if (status ==
706 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
707 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
708 {
709 return DumpCreationProgress::DUMP_CREATE_FAILED;
710 }
711 if (status ==
712 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
713 {
714 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
715 }
716 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
717}
718
719inline DumpCreationProgress
720 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
721{
722 for (const auto& [key, val] : values)
723 {
724 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700725 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600726 const std::string* value = std::get_if<std::string>(&val);
727 if (value == nullptr)
728 {
729 BMCWEB_LOG_ERROR << "Status property value is null";
730 return DumpCreationProgress::DUMP_CREATE_FAILED;
731 }
732 return mapDbusStatusToDumpProgress(*value);
733 }
734 }
735 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
736}
737
738inline std::string getDumpEntryPath(const std::string& dumpPath)
739{
740 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
741 {
742 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
743 }
744 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
745 {
746 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
747 }
748 return "";
749}
750
751inline void createDumpTaskCallback(
752 task::Payload&& payload,
753 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
754 const sdbusplus::message::object_path& createdObjPath)
755{
756 const std::string dumpPath = createdObjPath.parent_path().str;
757 const std::string dumpId = createdObjPath.filename();
758
759 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
760
761 if (dumpEntryPath.empty())
762 {
763 BMCWEB_LOG_ERROR << "Invalid dump type received";
764 messages::internalError(asyncResp->res);
765 return;
766 }
767
768 crow::connections::systemBus->async_method_call(
769 [asyncResp, payload, createdObjPath,
770 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800771 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600772 const std::string& introspectXml) {
773 if (ec)
774 {
775 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
776 << ec.message();
777 messages::internalError(asyncResp->res);
778 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700779 }
780
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600781 // Check if the created dump object has implemented Progress
782 // interface to track dump completion. If yes, fetch the "Status"
783 // property of the interface, modify the task state accordingly.
784 // Else, return task completed.
785 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700786
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600787 doc.Parse(introspectXml.data(), introspectXml.size());
788 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
789 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700790 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600791 BMCWEB_LOG_ERROR << "XML document failed to parse";
792 messages::internalError(asyncResp->res);
793 return;
794 }
795 tinyxml2::XMLElement* interfaceNode =
796 pRoot->FirstChildElement("interface");
797
798 bool isProgressIntfPresent = false;
799 while (interfaceNode != nullptr)
800 {
801 const char* thisInterfaceName = interfaceNode->Attribute("name");
802 if (thisInterfaceName != nullptr)
803 {
804 if (thisInterfaceName ==
805 std::string_view("xyz.openbmc_project.Common.Progress"))
806 {
807 interfaceNode =
808 interfaceNode->NextSiblingElement("interface");
809 continue;
810 }
811 isProgressIntfPresent = true;
812 break;
813 }
814 interfaceNode = interfaceNode->NextSiblingElement("interface");
815 }
816
817 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
818 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800819 const boost::system::error_code& err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600820 const std::shared_ptr<task::TaskData>& taskData) {
821 if (err)
822 {
823 BMCWEB_LOG_ERROR << createdObjPath.str
824 << ": Error in creating dump";
825 taskData->messages.emplace_back(messages::internalError());
826 taskData->state = "Cancelled";
827 return task::completed;
828 }
829
830 if (isProgressIntfPresent)
831 {
832 dbus::utility::DBusPropertiesMap values;
833 std::string prop;
834 msg.read(prop, values);
835
836 DumpCreationProgress dumpStatus =
837 getDumpCompletionStatus(values);
838 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
839 {
840 BMCWEB_LOG_ERROR << createdObjPath.str
841 << ": Error in creating dump";
842 taskData->state = "Cancelled";
843 return task::completed;
844 }
845
846 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
847 {
848 BMCWEB_LOG_DEBUG << createdObjPath.str
849 << ": Dump creation task is in progress";
850 return !task::completed;
851 }
852 }
853
Ed Tanous002d39b2022-05-31 08:59:27 -0700854 nlohmann::json retMessage = messages::success();
855 taskData->messages.emplace_back(retMessage);
856
Patrick Williams89492a12023-05-10 07:51:34 -0500857 std::string headerLoc = "Location: " + dumpEntryPath +
858 http_helpers::urlEncode(dumpId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700859 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
860
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600861 BMCWEB_LOG_DEBUG << createdObjPath.str
862 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700863 taskData->state = "Completed";
864 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600865 },
866 "type='signal',interface='org.freedesktop.DBus.Properties',"
867 "member='PropertiesChanged',path='" +
868 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500869
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600870 // The task timer is set to max time limit within which the
871 // requested dump will be collected.
872 task->startTimer(std::chrono::minutes(6));
873 task->populateResp(asyncResp->res);
874 task->payload.emplace(payload);
875 },
876 "xyz.openbmc_project.Dump.Manager", createdObjPath,
877 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500878}
879
zhanghch058d1b46d2021-04-01 11:18:24 +0800880inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
881 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500882{
Claire Weinanfdd26902022-03-01 14:18:25 -0800883 std::string dumpPath = getDumpEntriesPath(dumpType);
884 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500885 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500886 messages::internalError(asyncResp->res);
887 return;
888 }
889
890 std::optional<std::string> diagnosticDataType;
891 std::optional<std::string> oemDiagnosticDataType;
892
Willy Tu15ed6782021-12-14 11:03:16 -0800893 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500894 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
895 "OEMDiagnosticDataType", oemDiagnosticDataType))
896 {
897 return;
898 }
899
900 if (dumpType == "System")
901 {
902 if (!oemDiagnosticDataType || !diagnosticDataType)
903 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800904 BMCWEB_LOG_ERROR
905 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500906 messages::actionParameterMissing(
907 asyncResp->res, "CollectDiagnosticData",
908 "DiagnosticDataType & OEMDiagnosticDataType");
909 return;
910 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700911 if ((*oemDiagnosticDataType != "System") ||
912 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500913 {
914 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700915 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500916 return;
917 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500918 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500919 }
920 else if (dumpType == "BMC")
921 {
922 if (!diagnosticDataType)
923 {
George Liu0fda0f12021-11-16 10:06:17 +0800924 BMCWEB_LOG_ERROR
925 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500926 messages::actionParameterMissing(
927 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
928 return;
929 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700930 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500931 {
932 BMCWEB_LOG_ERROR
933 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700934 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500935 return;
936 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500937 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
938 }
939 else
940 {
941 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
942 messages::internalError(asyncResp->res);
943 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500944 }
945
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600946 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
947 createDumpParamVec;
948
Carson Labradof574a8e2023-03-22 02:26:00 +0000949 if (req.session != nullptr)
950 {
951 createDumpParamVec.emplace_back(
952 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
953 req.session->clientIp);
954 createDumpParamVec.emplace_back(
955 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
956 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
957 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600958
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500959 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800960 [asyncResp, payload(task::Payload(req)),
961 dumpPath](const boost::system::error_code& ec,
962 const sdbusplus::message_t& msg,
963 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700964 if (ec)
965 {
966 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500967 const sd_bus_error* dbusError = msg.get_error();
968 if (dbusError == nullptr)
969 {
970 messages::internalError(asyncResp->res);
971 return;
972 }
973
974 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
975 << " and error msg: " << dbusError->message;
976 if (std::string_view(
977 "xyz.openbmc_project.Common.Error.NotAllowed") ==
978 dbusError->name)
979 {
980 messages::resourceInStandby(asyncResp->res);
981 return;
982 }
983 if (std::string_view(
984 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
985 dbusError->name)
986 {
987 messages::serviceDisabled(asyncResp->res, dumpPath);
988 return;
989 }
990 if (std::string_view(
991 "xyz.openbmc_project.Common.Error.Unavailable") ==
992 dbusError->name)
993 {
994 messages::resourceInUse(asyncResp->res);
995 return;
996 }
997 // Other Dbus errors such as:
998 // xyz.openbmc_project.Common.Error.InvalidArgument &
999 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1000 // the dbus call that is made here in the bmcweb
1001 // implementation and has nothing to do with the client's
1002 // input in the request. Hence, returning internal error
1003 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001004 messages::internalError(asyncResp->res);
1005 return;
1006 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001007 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
1008 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001009 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001010 "xyz.openbmc_project.Dump.Manager",
1011 "/xyz/openbmc_project/dump/" +
1012 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001013 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001014}
1015
zhanghch058d1b46d2021-04-01 11:18:24 +08001016inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1017 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001018{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001019 std::string dumpTypeLowerCopy =
1020 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001021
Claire Weinan0d946212022-07-13 19:40:19 -07001022 crow::connections::systemBus->async_method_call(
1023 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001024 if (ec)
1025 {
Claire Weinan0d946212022-07-13 19:40:19 -07001026 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -07001027 messages::internalError(asyncResp->res);
1028 return;
1029 }
Claire Weinan0d946212022-07-13 19:40:19 -07001030 },
1031 "xyz.openbmc_project.Dump.Manager",
1032 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1033 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001034}
1035
Ed Tanousb9d36b42022-02-26 21:42:46 -08001036inline static void
1037 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1038 std::string& filename, std::string& timestamp,
1039 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001040{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001041 const std::string* filenamePtr = nullptr;
1042 const std::string* timestampPtr = nullptr;
1043 const std::string* logfilePtr = nullptr;
1044
1045 const bool success = sdbusplus::unpackPropertiesNoThrow(
1046 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1047 "Filename", filenamePtr, "Log", logfilePtr);
1048
1049 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001050 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001051 return;
1052 }
1053
1054 if (filenamePtr != nullptr)
1055 {
1056 filename = *filenamePtr;
1057 }
1058
1059 if (timestampPtr != nullptr)
1060 {
1061 timestamp = *timestampPtr;
1062 }
1063
1064 if (logfilePtr != nullptr)
1065 {
1066 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001067 }
1068}
1069
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001070inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001071{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001072 /**
1073 * Functions triggers appropriate requests on DBus
1074 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001075 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001076 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001077 .methods(boost::beast::http::verb::get)(
1078 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001079 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1080 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001081 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001082 {
1083 return;
1084 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001085 if (systemName != "system")
1086 {
1087 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1088 systemName);
1089 return;
1090 }
1091
Ed Tanous002d39b2022-05-31 08:59:27 -07001092 // Collections don't include the static data added by SubRoute
1093 // because it has a duplicate entry for members
1094 asyncResp->res.jsonValue["@odata.type"] =
1095 "#LogServiceCollection.LogServiceCollection";
1096 asyncResp->res.jsonValue["@odata.id"] =
1097 "/redfish/v1/Systems/system/LogServices";
1098 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1099 asyncResp->res.jsonValue["Description"] =
1100 "Collection of LogServices for this Computer System";
1101 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1102 logServiceArray = nlohmann::json::array();
1103 nlohmann::json::object_t eventLog;
1104 eventLog["@odata.id"] =
1105 "/redfish/v1/Systems/system/LogServices/EventLog";
1106 logServiceArray.push_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001107#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001108 nlohmann::json::object_t dumpLog;
1109 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
1110 logServiceArray.push_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001111#endif
1112
Jason M. Billsd53dd412019-02-12 17:16:22 -08001113#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001114 nlohmann::json::object_t crashdump;
1115 crashdump["@odata.id"] =
1116 "/redfish/v1/Systems/system/LogServices/Crashdump";
1117 logServiceArray.push_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001118#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001119
1120#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 nlohmann::json::object_t hostlogger;
1122 hostlogger["@odata.id"] =
1123 "/redfish/v1/Systems/system/LogServices/HostLogger";
1124 logServiceArray.push_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001125#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001126 asyncResp->res.jsonValue["Members@odata.count"] =
1127 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001128
George Liu7a1dbc42022-12-07 16:03:22 +08001129 constexpr std::array<std::string_view, 1> interfaces = {
1130 "xyz.openbmc_project.State.Boot.PostCode"};
1131 dbus::utility::getSubTreePaths(
1132 "/", 0, interfaces,
1133 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001134 const dbus::utility::MapperGetSubTreePathsResponse&
1135 subtreePath) {
1136 if (ec)
1137 {
1138 BMCWEB_LOG_ERROR << ec;
1139 return;
1140 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001141
Ed Tanous002d39b2022-05-31 08:59:27 -07001142 for (const auto& pathStr : subtreePath)
1143 {
1144 if (pathStr.find("PostCode") != std::string::npos)
1145 {
1146 nlohmann::json& logServiceArrayLocal =
1147 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001148 nlohmann::json::object_t member;
1149 member["@odata.id"] =
1150 "/redfish/v1/Systems/system/LogServices/PostCodes";
1151
1152 logServiceArrayLocal.push_back(std::move(member));
1153
Ed Tanous002d39b2022-05-31 08:59:27 -07001154 asyncResp->res.jsonValue["Members@odata.count"] =
1155 logServiceArrayLocal.size();
1156 return;
1157 }
1158 }
George Liu7a1dbc42022-12-07 16:03:22 +08001159 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001160 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001161}
1162
1163inline void requestRoutesEventLogService(App& app)
1164{
Ed Tanous22d268c2022-05-19 09:39:07 -07001165 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001166 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001167 .methods(boost::beast::http::verb::get)(
1168 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001169 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1170 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001171 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 {
1173 return;
1174 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001175 if (systemName != "system")
1176 {
1177 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1178 systemName);
1179 return;
1180 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001181 asyncResp->res.jsonValue["@odata.id"] =
1182 "/redfish/v1/Systems/system/LogServices/EventLog";
1183 asyncResp->res.jsonValue["@odata.type"] =
1184 "#LogService.v1_1_0.LogService";
1185 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1186 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1187 asyncResp->res.jsonValue["Id"] = "EventLog";
1188 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301189
Ed Tanous002d39b2022-05-31 08:59:27 -07001190 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001191 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301192
Ed Tanous002d39b2022-05-31 08:59:27 -07001193 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1194 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1195 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301196
Ed Tanous002d39b2022-05-31 08:59:27 -07001197 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1198 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1199 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001200
Ed Tanous002d39b2022-05-31 08:59:27 -07001201 {"target",
1202 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001203 });
1204}
1205
1206inline void requestRoutesJournalEventLogClear(App& app)
1207{
Jason M. Bills4978b632022-02-22 14:17:43 -08001208 BMCWEB_ROUTE(
1209 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001210 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001211 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001212 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001213 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001214 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1215 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001216 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001217 {
1218 return;
1219 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001220 if (systemName != "system")
1221 {
1222 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1223 systemName);
1224 return;
1225 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 // Clear the EventLog by deleting the log files
1227 std::vector<std::filesystem::path> redfishLogFiles;
1228 if (getRedfishLogFiles(redfishLogFiles))
1229 {
1230 for (const std::filesystem::path& file : redfishLogFiles)
1231 {
1232 std::error_code ec;
1233 std::filesystem::remove(file, ec);
1234 }
1235 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001236
Ed Tanous002d39b2022-05-31 08:59:27 -07001237 // Reload rsyslog so it knows to start new log files
1238 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001239 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001240 if (ec)
1241 {
1242 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1243 messages::internalError(asyncResp->res);
1244 return;
1245 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001246
Ed Tanous002d39b2022-05-31 08:59:27 -07001247 messages::success(asyncResp->res);
1248 },
1249 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1250 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1251 "replace");
1252 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001253}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001254
Jason M. Billsac992cd2022-06-24 13:31:46 -07001255enum class LogParseError
1256{
1257 success,
1258 parseFailed,
1259 messageIdNotInRegistry,
1260};
1261
1262static LogParseError
1263 fillEventLogEntryJson(const std::string& logEntryID,
1264 const std::string& logEntry,
1265 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001266{
Jason M. Bills95820182019-04-22 16:25:34 -07001267 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001268 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001269 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001270 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001271 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001272 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001273 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001274 std::string timestamp = logEntry.substr(0, space);
1275 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001276 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001277 if (entryStart == std::string::npos)
1278 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001279 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001280 }
1281 std::string_view entry(logEntry);
1282 entry.remove_prefix(entryStart);
1283 // Use split to separate the entry into its fields
1284 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001285 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001286 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001287 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001288 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001289 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001290 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001291 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001292
Jason M. Bills4851d452019-03-28 11:27:48 -07001293 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001294 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001295
Sui Chen54417b02022-03-24 14:59:52 -07001296 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001297 {
Sui Chen54417b02022-03-24 14:59:52 -07001298 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001299 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001300 }
1301
Sui Chen54417b02022-03-24 14:59:52 -07001302 std::string msg = message->message;
1303
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001304 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001305 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001306 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001307 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001308 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001309 // If the first string is empty, assume there are no MessageArgs
1310 std::size_t messageArgsSize = 0;
1311 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001312 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001313 messageArgsSize = logEntryFields.size() - 1;
1314 }
1315
Ed Tanous23a21a12020-07-25 04:45:05 +00001316 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001317
1318 // Fill the MessageArgs into the Message
1319 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001320 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001321 {
1322 std::string argStr = "%" + std::to_string(++i);
1323 size_t argPos = msg.find(argStr);
1324 if (argPos != std::string::npos)
1325 {
1326 msg.replace(argPos, argStr.length(), messageArg);
1327 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001328 }
1329 }
1330
Jason M. Bills95820182019-04-22 16:25:34 -07001331 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1332 // format which matches the Redfish format except for the fractional seconds
1333 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001334 std::size_t dot = timestamp.find_first_of('.');
1335 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001336 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001337 {
Jason M. Bills95820182019-04-22 16:25:34 -07001338 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001339 }
1340
1341 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001342 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001343 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
1344 "redfish", "v1", "Systems", "system", "LogServices", "EventLog",
1345 "Entries", logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001346 logEntryJson["Name"] = "System Event Log Entry";
1347 logEntryJson["Id"] = logEntryID;
1348 logEntryJson["Message"] = std::move(msg);
1349 logEntryJson["MessageId"] = std::move(messageID);
1350 logEntryJson["MessageArgs"] = messageArgs;
1351 logEntryJson["EntryType"] = "Event";
1352 logEntryJson["Severity"] = message->messageSeverity;
1353 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001354 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001355}
1356
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001357inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001358{
Ed Tanous22d268c2022-05-19 09:39:07 -07001359 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001360 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001361 .methods(boost::beast::http::verb::get)(
1362 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001363 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1364 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001365 query_param::QueryCapabilities capabilities = {
1366 .canDelegateTop = true,
1367 .canDelegateSkip = true,
1368 };
1369 query_param::Query delegatedQuery;
1370 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001371 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001372 {
1373 return;
1374 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001375 if (systemName != "system")
1376 {
1377 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1378 systemName);
1379 return;
1380 }
1381
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001382 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001383 size_t skip = delegatedQuery.skip.value_or(0);
1384
Ed Tanous002d39b2022-05-31 08:59:27 -07001385 // Collections don't include the static data added by SubRoute
1386 // because it has a duplicate entry for members
1387 asyncResp->res.jsonValue["@odata.type"] =
1388 "#LogEntryCollection.LogEntryCollection";
1389 asyncResp->res.jsonValue["@odata.id"] =
1390 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1391 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1392 asyncResp->res.jsonValue["Description"] =
1393 "Collection of System Event Log Entries";
1394
1395 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1396 logEntryArray = nlohmann::json::array();
1397 // Go through the log files and create a unique ID for each
1398 // entry
1399 std::vector<std::filesystem::path> redfishLogFiles;
1400 getRedfishLogFiles(redfishLogFiles);
1401 uint64_t entryCount = 0;
1402 std::string logEntry;
1403
1404 // Oldest logs are in the last file, so start there and loop
1405 // backwards
1406 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1407 it++)
1408 {
1409 std::ifstream logStream(*it);
1410 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001411 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001413 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001414
Ed Tanous002d39b2022-05-31 08:59:27 -07001415 // Reset the unique ID on the first entry
1416 bool firstEntry = true;
1417 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001418 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001419 std::string idStr;
1420 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001421 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001422 continue;
1423 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001424 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001425
Jason M. Billsde703c52022-06-23 14:19:04 -07001426 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001427 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1428 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001429 if (status == LogParseError::messageIdNotInRegistry)
1430 {
1431 continue;
1432 }
1433 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001434 {
1435 messages::internalError(asyncResp->res);
1436 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001437 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001438
Jason M. Billsde703c52022-06-23 14:19:04 -07001439 entryCount++;
1440 // Handle paging using skip (number of entries to skip from the
1441 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001442 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001443 {
1444 continue;
1445 }
1446
1447 logEntryArray.push_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001448 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001449 }
1450 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001451 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001452 {
1453 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1454 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001455 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001456 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001457 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001458}
Chicago Duan336e96c2019-07-15 14:22:08 +08001459
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001460inline void requestRoutesJournalEventLogEntry(App& app)
1461{
1462 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001463 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001464 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001465 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001466 [&app](const crow::Request& req,
1467 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001468 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001469 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001470 {
1471 return;
1472 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001473
1474 if (systemName != "system")
1475 {
1476 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1477 systemName);
1478 return;
1479 }
1480
Ed Tanous002d39b2022-05-31 08:59:27 -07001481 const std::string& targetID = param;
1482
1483 // Go through the log files and check the unique ID for each
1484 // entry to find the target entry
1485 std::vector<std::filesystem::path> redfishLogFiles;
1486 getRedfishLogFiles(redfishLogFiles);
1487 std::string logEntry;
1488
1489 // Oldest logs are in the last file, so start there and loop
1490 // backwards
1491 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1492 it++)
1493 {
1494 std::ifstream logStream(*it);
1495 if (!logStream.is_open())
1496 {
1497 continue;
1498 }
1499
1500 // Reset the unique ID on the first entry
1501 bool firstEntry = true;
1502 while (std::getline(logStream, logEntry))
1503 {
1504 std::string idStr;
1505 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001506 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001507 continue;
1508 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001509 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001510
1511 if (idStr == targetID)
1512 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001513 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001514 LogParseError status =
1515 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1516 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001517 {
1518 messages::internalError(asyncResp->res);
1519 return;
1520 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001521 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001522 return;
1523 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001524 }
1525 }
1526 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001527 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001528 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001529}
1530
1531inline void requestRoutesDBusEventLogEntryCollection(App& app)
1532{
Ed Tanous22d268c2022-05-19 09:39:07 -07001533 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001534 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001535 .methods(boost::beast::http::verb::get)(
1536 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001537 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1538 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001539 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001540 {
1541 return;
1542 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001543 if (systemName != "system")
1544 {
1545 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1546 systemName);
1547 return;
1548 }
1549
Ed Tanous002d39b2022-05-31 08:59:27 -07001550 // Collections don't include the static data added by SubRoute
1551 // because it has a duplicate entry for members
1552 asyncResp->res.jsonValue["@odata.type"] =
1553 "#LogEntryCollection.LogEntryCollection";
1554 asyncResp->res.jsonValue["@odata.id"] =
1555 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1556 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1557 asyncResp->res.jsonValue["Description"] =
1558 "Collection of System Event Log Entries";
1559
1560 // DBus implementation of EventLog/Entries
1561 // Make call to Logging Service to find all log entry objects
1562 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001563 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001564 const dbus::utility::ManagedObjectType& resp) {
1565 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001566 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001567 // TODO Handle for specific error code
1568 BMCWEB_LOG_ERROR
1569 << "getLogEntriesIfaceData resp_handler got error " << ec;
1570 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001571 return;
1572 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001573 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1574 entriesArray = nlohmann::json::array();
1575 for (const auto& objectPath : resp)
1576 {
1577 const uint32_t* id = nullptr;
1578 const uint64_t* timestamp = nullptr;
1579 const uint64_t* updateTimestamp = nullptr;
1580 const std::string* severity = nullptr;
1581 const std::string* message = nullptr;
1582 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001583 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001584 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001585 const std::string* notify = nullptr;
1586
Ed Tanous002d39b2022-05-31 08:59:27 -07001587 for (const auto& interfaceMap : objectPath.second)
1588 {
1589 if (interfaceMap.first ==
1590 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001591 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001592 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001593 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001594 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001595 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001596 id = std::get_if<uint32_t>(&propertyMap.second);
1597 }
1598 else if (propertyMap.first == "Timestamp")
1599 {
1600 timestamp =
1601 std::get_if<uint64_t>(&propertyMap.second);
1602 }
1603 else if (propertyMap.first == "UpdateTimestamp")
1604 {
1605 updateTimestamp =
1606 std::get_if<uint64_t>(&propertyMap.second);
1607 }
1608 else if (propertyMap.first == "Severity")
1609 {
1610 severity = std::get_if<std::string>(
1611 &propertyMap.second);
1612 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001613 else if (propertyMap.first == "Resolution")
1614 {
1615 resolution = std::get_if<std::string>(
1616 &propertyMap.second);
1617 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001618 else if (propertyMap.first == "Message")
1619 {
1620 message = std::get_if<std::string>(
1621 &propertyMap.second);
1622 }
1623 else if (propertyMap.first == "Resolved")
1624 {
1625 const bool* resolveptr =
1626 std::get_if<bool>(&propertyMap.second);
1627 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001628 {
1629 messages::internalError(asyncResp->res);
1630 return;
1631 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001632 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001633 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001634 else if (propertyMap.first ==
1635 "ServiceProviderNotify")
1636 {
1637 notify = std::get_if<std::string>(
1638 &propertyMap.second);
1639 if (notify == nullptr)
1640 {
1641 messages::internalError(asyncResp->res);
1642 return;
1643 }
1644 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001645 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001646 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001647 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001648 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001649 messages::internalError(asyncResp->res);
1650 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001651 }
1652 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001653 else if (interfaceMap.first ==
1654 "xyz.openbmc_project.Common.FilePath")
1655 {
1656 for (const auto& propertyMap : interfaceMap.second)
1657 {
1658 if (propertyMap.first == "Path")
1659 {
1660 filePath = std::get_if<std::string>(
1661 &propertyMap.second);
1662 }
1663 }
1664 }
1665 }
1666 // Object path without the
1667 // xyz.openbmc_project.Logging.Entry interface, ignore
1668 // and continue.
1669 if (id == nullptr || message == nullptr ||
1670 severity == nullptr || timestamp == nullptr ||
1671 updateTimestamp == nullptr)
1672 {
1673 continue;
1674 }
1675 entriesArray.push_back({});
1676 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001677 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001678 thisEntry["@odata.id"] = crow::utility::urlFromPieces(
1679 "redfish", "v1", "Systems", "system", "LogServices",
1680 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001681 thisEntry["Name"] = "System Event Log Entry";
1682 thisEntry["Id"] = std::to_string(*id);
1683 thisEntry["Message"] = *message;
1684 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001685 if ((resolution != nullptr) && (!(*resolution).empty()))
1686 {
1687 thisEntry["Resolution"] = *resolution;
1688 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001689 std::optional<bool> notifyAction =
1690 getProviderNotifyAction(*notify);
1691 if (notifyAction)
1692 {
1693 thisEntry["ServiceProviderNotified"] = *notifyAction;
1694 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001695 thisEntry["EntryType"] = "Event";
1696 thisEntry["Severity"] =
1697 translateSeverityDbusToRedfish(*severity);
1698 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001699 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001700 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001701 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001702 if (filePath != nullptr)
1703 {
1704 thisEntry["AdditionalDataURI"] =
1705 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1706 std::to_string(*id) + "/attachment";
1707 }
1708 }
1709 std::sort(
1710 entriesArray.begin(), entriesArray.end(),
1711 [](const nlohmann::json& left, const nlohmann::json& right) {
1712 return (left["Id"] <= right["Id"]);
1713 });
1714 asyncResp->res.jsonValue["Members@odata.count"] =
1715 entriesArray.size();
1716 },
1717 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1718 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001719 });
1720}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001721
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001722inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001723{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001724 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001725 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001726 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001727 .methods(boost::beast::http::verb::get)(
1728 [&app](const crow::Request& req,
1729 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001730 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001731 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001732 {
1733 return;
1734 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001735 if (systemName != "system")
1736 {
1737 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1738 systemName);
1739 return;
1740 }
1741
Ed Tanous002d39b2022-05-31 08:59:27 -07001742 std::string entryID = param;
1743 dbus::utility::escapePathForDbus(entryID);
1744
1745 // DBus implementation of EventLog/Entries
1746 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001747 sdbusplus::asio::getAllProperties(
1748 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1749 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001750 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001751 const dbus::utility::DBusPropertiesMap& resp) {
1752 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001753 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001754 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1755 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001756 return;
1757 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001758 if (ec)
1759 {
1760 BMCWEB_LOG_ERROR
1761 << "EventLogEntry (DBus) resp_handler got error " << ec;
1762 messages::internalError(asyncResp->res);
1763 return;
1764 }
1765 const uint32_t* id = nullptr;
1766 const uint64_t* timestamp = nullptr;
1767 const uint64_t* updateTimestamp = nullptr;
1768 const std::string* severity = nullptr;
1769 const std::string* message = nullptr;
1770 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001771 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001772 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001773 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001774
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001775 const bool success = sdbusplus::unpackPropertiesNoThrow(
1776 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1777 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001778 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001779 "Resolution", resolution, "Path", filePath,
1780 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001781
1782 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001783 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001784 messages::internalError(asyncResp->res);
1785 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001786 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001787
Ed Tanous002d39b2022-05-31 08:59:27 -07001788 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001789 timestamp == nullptr || updateTimestamp == nullptr ||
1790 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001791 {
1792 messages::internalError(asyncResp->res);
1793 return;
1794 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001795
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001797 "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001798 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00001799 crow::utility::urlFromPieces(
1800 "redfish", "v1", "Systems", "system", "LogServices",
1801 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1803 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1804 asyncResp->res.jsonValue["Message"] = *message;
1805 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001806 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1807 if (notifyAction)
1808 {
1809 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1810 *notifyAction;
1811 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001812 if ((resolution != nullptr) && (!(*resolution).empty()))
1813 {
1814 asyncResp->res.jsonValue["Resolution"] = *resolution;
1815 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001816 asyncResp->res.jsonValue["EntryType"] = "Event";
1817 asyncResp->res.jsonValue["Severity"] =
1818 translateSeverityDbusToRedfish(*severity);
1819 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001820 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001821 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001822 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001823 if (filePath != nullptr)
1824 {
1825 asyncResp->res.jsonValue["AdditionalDataURI"] =
1826 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1827 std::to_string(*id) + "/attachment";
1828 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001829 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001830 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001831
1832 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001833 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001834 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001835 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001836 [&app](const crow::Request& req,
1837 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001838 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001839 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001840 {
1841 return;
1842 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001843 if (systemName != "system")
1844 {
1845 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1846 systemName);
1847 return;
1848 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001849 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001850
Ed Tanous002d39b2022-05-31 08:59:27 -07001851 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1852 resolved))
1853 {
1854 return;
1855 }
1856 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001857
Ed Tanous002d39b2022-05-31 08:59:27 -07001858 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001859 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001860 if (ec)
1861 {
1862 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1863 messages::internalError(asyncResp->res);
1864 return;
1865 }
1866 },
1867 "xyz.openbmc_project.Logging",
1868 "/xyz/openbmc_project/logging/entry/" + entryId,
1869 "org.freedesktop.DBus.Properties", "Set",
1870 "xyz.openbmc_project.Logging.Entry", "Resolved",
1871 dbus::utility::DbusVariantType(*resolved));
1872 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001873
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001874 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001875 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001876 .privileges(redfish::privileges::deleteLogEntry)
1877
Ed Tanous002d39b2022-05-31 08:59:27 -07001878 .methods(boost::beast::http::verb::delete_)(
1879 [&app](const crow::Request& req,
1880 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001881 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001882 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001883 {
1884 return;
1885 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001886 if (systemName != "system")
1887 {
1888 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1889 systemName);
1890 return;
1891 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001892 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1893
1894 std::string entryID = param;
1895
1896 dbus::utility::escapePathForDbus(entryID);
1897
1898 // Process response from Logging service.
1899 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001900 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001901 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1902 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001903 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001904 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001905 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001906 messages::resourceNotFound(asyncResp->res, "LogEntry",
1907 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001908 return;
1909 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001910 // TODO Handle for specific error code
1911 BMCWEB_LOG_ERROR
1912 << "EventLogEntry (DBus) doDelete respHandler got error "
1913 << ec;
1914 asyncResp->res.result(
1915 boost::beast::http::status::internal_server_error);
1916 return;
1917 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001918
Ed Tanous002d39b2022-05-31 08:59:27 -07001919 asyncResp->res.result(boost::beast::http::status::ok);
1920 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001921
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 // Make call to Logging service to request Delete Log
1923 crow::connections::systemBus->async_method_call(
1924 respHandler, "xyz.openbmc_project.Logging",
1925 "/xyz/openbmc_project/logging/entry/" + entryID,
1926 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001927 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001928}
1929
1930inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001931{
George Liu0fda0f12021-11-16 10:06:17 +08001932 BMCWEB_ROUTE(
1933 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001934 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001935 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001936 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001937 [&app](const crow::Request& req,
1938 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001939 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001940 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001941 {
1942 return;
1943 }
Matt Spinler72e21372023-04-19 12:53:33 -05001944 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07001945 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001946 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001947 {
1948 asyncResp->res.result(boost::beast::http::status::bad_request);
1949 return;
1950 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001951 if (systemName != "system")
1952 {
1953 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1954 systemName);
1955 return;
1956 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001957
Ed Tanous002d39b2022-05-31 08:59:27 -07001958 std::string entryID = param;
1959 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001960
Ed Tanous002d39b2022-05-31 08:59:27 -07001961 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001962 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001963 const sdbusplus::message::unix_fd& unixfd) {
1964 if (ec.value() == EBADR)
1965 {
1966 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1967 entryID);
1968 return;
1969 }
1970 if (ec)
1971 {
1972 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1973 messages::internalError(asyncResp->res);
1974 return;
1975 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001976
Ed Tanous002d39b2022-05-31 08:59:27 -07001977 int fd = -1;
1978 fd = dup(unixfd);
1979 if (fd == -1)
1980 {
1981 messages::internalError(asyncResp->res);
1982 return;
1983 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001984
Ed Tanous002d39b2022-05-31 08:59:27 -07001985 long long int size = lseek(fd, 0, SEEK_END);
1986 if (size == -1)
1987 {
1988 messages::internalError(asyncResp->res);
1989 return;
1990 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001991
Ed Tanous002d39b2022-05-31 08:59:27 -07001992 // Arbitrary max size of 64kb
1993 constexpr int maxFileSize = 65536;
1994 if (size > maxFileSize)
1995 {
1996 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1997 << maxFileSize;
1998 messages::internalError(asyncResp->res);
1999 return;
2000 }
2001 std::vector<char> data(static_cast<size_t>(size));
2002 long long int rc = lseek(fd, 0, SEEK_SET);
2003 if (rc == -1)
2004 {
2005 messages::internalError(asyncResp->res);
2006 return;
2007 }
2008 rc = read(fd, data.data(), data.size());
2009 if ((rc == -1) || (rc != size))
2010 {
2011 messages::internalError(asyncResp->res);
2012 return;
2013 }
2014 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002015
Ed Tanous002d39b2022-05-31 08:59:27 -07002016 std::string_view strData(data.data(), data.size());
2017 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002018
Ed Tanousd9f6c622022-03-17 09:12:17 -07002019 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07002020 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07002021 asyncResp->res.addHeader(
2022 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07002023 asyncResp->res.body() = std::move(output);
2024 },
2025 "xyz.openbmc_project.Logging",
2026 "/xyz/openbmc_project/logging/entry/" + entryID,
2027 "xyz.openbmc_project.Logging.Entry", "GetEntry");
2028 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002029}
2030
Spencer Kub7028eb2021-10-26 15:27:35 +08002031constexpr const char* hostLoggerFolderPath = "/var/log/console";
2032
2033inline bool
2034 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2035 std::vector<std::filesystem::path>& hostLoggerFiles)
2036{
2037 std::error_code ec;
2038 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2039 if (ec)
2040 {
2041 BMCWEB_LOG_ERROR << ec.message();
2042 return false;
2043 }
2044 for (const std::filesystem::directory_entry& it : logPath)
2045 {
2046 std::string filename = it.path().filename();
2047 // Prefix of each log files is "log". Find the file and save the
2048 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002049 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002050 {
2051 hostLoggerFiles.emplace_back(it.path());
2052 }
2053 }
2054 // As the log files rotate, they are appended with a ".#" that is higher for
2055 // the older logs. Since we start from oldest logs, sort the name in
2056 // descending order.
2057 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2058 AlphanumLess<std::string>());
2059
2060 return true;
2061}
2062
Ed Tanous02cad962022-06-30 16:50:15 -07002063inline bool getHostLoggerEntries(
2064 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2065 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002066{
2067 GzFileReader logFile;
2068
2069 // Go though all log files and expose host logs.
2070 for (const std::filesystem::path& it : hostLoggerFiles)
2071 {
2072 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2073 {
2074 BMCWEB_LOG_ERROR << "fail to expose host logs";
2075 return false;
2076 }
2077 }
2078 // Get lastMessage from constructor by getter
2079 std::string lastMessage = logFile.getLastMessage();
2080 if (!lastMessage.empty())
2081 {
2082 logCount++;
2083 if (logCount > skip && logCount <= (skip + top))
2084 {
2085 logEntries.push_back(lastMessage);
2086 }
2087 }
2088 return true;
2089}
2090
2091inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2092 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002093 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002094{
2095 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002096 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002097 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2098 "redfish", "v1", "Systems", "system", "LogServices", "HostLogger",
2099 "Entries", logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002100 logEntryJson["Name"] = "Host Logger Entry";
2101 logEntryJson["Id"] = logEntryID;
2102 logEntryJson["Message"] = msg;
2103 logEntryJson["EntryType"] = "Oem";
2104 logEntryJson["Severity"] = "OK";
2105 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002106}
2107
2108inline void requestRoutesSystemHostLogger(App& app)
2109{
Ed Tanous22d268c2022-05-19 09:39:07 -07002110 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002111 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002112 .methods(boost::beast::http::verb::get)(
2113 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002114 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2115 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002116 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002117 {
2118 return;
2119 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002120 if (systemName != "system")
2121 {
2122 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2123 systemName);
2124 return;
2125 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002126 asyncResp->res.jsonValue["@odata.id"] =
2127 "/redfish/v1/Systems/system/LogServices/HostLogger";
2128 asyncResp->res.jsonValue["@odata.type"] =
2129 "#LogService.v1_1_0.LogService";
2130 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2131 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2132 asyncResp->res.jsonValue["Id"] = "HostLogger";
2133 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2134 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2135 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002136}
2137
2138inline void requestRoutesSystemHostLoggerCollection(App& app)
2139{
2140 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002141 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002142 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002143 .methods(boost::beast::http::verb::get)(
2144 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002145 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2146 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 query_param::QueryCapabilities capabilities = {
2148 .canDelegateTop = true,
2149 .canDelegateSkip = true,
2150 };
2151 query_param::Query delegatedQuery;
2152 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002153 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002154 {
2155 return;
2156 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002157 if (systemName != "system")
2158 {
2159 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2160 systemName);
2161 return;
2162 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002163 asyncResp->res.jsonValue["@odata.id"] =
2164 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2165 asyncResp->res.jsonValue["@odata.type"] =
2166 "#LogEntryCollection.LogEntryCollection";
2167 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2168 asyncResp->res.jsonValue["Description"] =
2169 "Collection of HostLogger Entries";
2170 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2171 logEntryArray = nlohmann::json::array();
2172 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002173
Ed Tanous002d39b2022-05-31 08:59:27 -07002174 std::vector<std::filesystem::path> hostLoggerFiles;
2175 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2176 {
2177 BMCWEB_LOG_ERROR << "fail to get host log file path";
2178 return;
2179 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002180 // If we weren't provided top and skip limits, use the defaults.
2181 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002182 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002183 size_t logCount = 0;
2184 // This vector only store the entries we want to expose that
2185 // control by skip and top.
2186 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002187 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2188 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002189 {
2190 messages::internalError(asyncResp->res);
2191 return;
2192 }
2193 // If vector is empty, that means skip value larger than total
2194 // log count
2195 if (logEntries.empty())
2196 {
2197 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2198 return;
2199 }
2200 if (!logEntries.empty())
2201 {
2202 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002203 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002204 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002205 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2206 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002207 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002208 }
2209
Ed Tanous002d39b2022-05-31 08:59:27 -07002210 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002211 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002212 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002213 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2214 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002215 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002216 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002217 }
George Liu0fda0f12021-11-16 10:06:17 +08002218 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002219}
2220
2221inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2222{
2223 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002224 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002225 .privileges(redfish::privileges::getLogEntry)
2226 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002227 [&app](const crow::Request& req,
2228 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002229 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002230 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002231 {
2232 return;
2233 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002234 if (systemName != "system")
2235 {
2236 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2237 systemName);
2238 return;
2239 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002240 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002241
Ed Tanous002d39b2022-05-31 08:59:27 -07002242 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002243
Ed Tanous002d39b2022-05-31 08:59:27 -07002244 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2245 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08002246
Ed Tanous002d39b2022-05-31 08:59:27 -07002247 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002248 if (ec == std::errc::invalid_argument ||
2249 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002250 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002251 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002252 return;
2253 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002254
Ed Tanous002d39b2022-05-31 08:59:27 -07002255 std::vector<std::filesystem::path> hostLoggerFiles;
2256 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2257 {
2258 BMCWEB_LOG_ERROR << "fail to get host log file path";
2259 return;
2260 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002261
Ed Tanous002d39b2022-05-31 08:59:27 -07002262 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002263 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002264 std::vector<std::string> logEntries;
2265 // We can get specific entry by skip and top. For example, if we
2266 // want to get nth entry, we can set skip = n-1 and top = 1 to
2267 // get that entry
2268 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2269 logCount))
2270 {
2271 messages::internalError(asyncResp->res);
2272 return;
2273 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002274
Ed Tanous002d39b2022-05-31 08:59:27 -07002275 if (!logEntries.empty())
2276 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002277 nlohmann::json::object_t hostLogEntry;
2278 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2279 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002280 return;
2281 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002282
Ed Tanous002d39b2022-05-31 08:59:27 -07002283 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002284 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002285 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002286}
2287
Claire Weinandd72e872022-08-15 14:20:06 -07002288inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002289 crow::App& app, const crow::Request& req,
2290 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2291{
2292 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2293 {
2294 return;
2295 }
2296 // Collections don't include the static data added by SubRoute
2297 // because it has a duplicate entry for members
2298 asyncResp->res.jsonValue["@odata.type"] =
2299 "#LogServiceCollection.LogServiceCollection";
2300 asyncResp->res.jsonValue["@odata.id"] =
2301 "/redfish/v1/Managers/bmc/LogServices";
2302 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2303 asyncResp->res.jsonValue["Description"] =
2304 "Collection of LogServices for this Manager";
2305 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2306 logServiceArray = nlohmann::json::array();
2307
2308#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002309 nlohmann::json::object_t journal;
2310 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2311 logServiceArray.push_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002312#endif
2313
2314 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2315
2316#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002317 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002318 "xyz.openbmc_project.Collection.DeleteAll"};
2319 dbus::utility::getSubTreePaths(
2320 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002321 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002322 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002323 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2324 if (ec)
2325 {
2326 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002327 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002328 << ec;
2329 // Assume that getting an error simply means there are no dump
2330 // LogServices. Return without adding any error response.
2331 return;
2332 }
2333
2334 nlohmann::json& logServiceArrayLocal =
2335 asyncResp->res.jsonValue["Members"];
2336
2337 for (const std::string& path : subTreePaths)
2338 {
2339 if (path == "/xyz/openbmc_project/dump/bmc")
2340 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002341 nlohmann::json::object_t member;
2342 member["@odata.id"] =
2343 "/redfish/v1/Managers/bmc/LogServices/Dump";
2344 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002345 }
2346 else if (path == "/xyz/openbmc_project/dump/faultlog")
2347 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002348 nlohmann::json::object_t member;
2349 member["@odata.id"] =
2350 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2351 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002352 }
2353 }
2354
2355 asyncResp->res.jsonValue["Members@odata.count"] =
2356 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002357 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002358#endif
2359}
2360
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002361inline void requestRoutesBMCLogServiceCollection(App& app)
2362{
2363 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002364 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002365 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002366 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002367}
Ed Tanous1da66f72018-07-27 16:13:37 -07002368
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002369inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002370{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002371 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002372 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002373 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002374 [&app](const crow::Request& req,
2375 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002376 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002377 {
2378 return;
2379 }
2380 asyncResp->res.jsonValue["@odata.type"] =
2381 "#LogService.v1_1_0.LogService";
2382 asyncResp->res.jsonValue["@odata.id"] =
2383 "/redfish/v1/Managers/bmc/LogServices/Journal";
2384 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2385 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002386 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002387 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302388
Ed Tanous002d39b2022-05-31 08:59:27 -07002389 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002390 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002391 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2392 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2393 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302394
Ed Tanous002d39b2022-05-31 08:59:27 -07002395 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2396 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2397 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002398}
Jason M. Billse1f26342018-07-18 12:12:00 -07002399
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002400static int
2401 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2402 sd_journal* journal,
2403 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002404{
2405 // Get the Log Entry contents
2406 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002407
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002408 std::string message;
2409 std::string_view syslogID;
2410 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2411 if (ret < 0)
2412 {
2413 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2414 << strerror(-ret);
2415 }
2416 if (!syslogID.empty())
2417 {
2418 message += std::string(syslogID) + ": ";
2419 }
2420
Ed Tanous39e77502019-03-04 17:35:53 -08002421 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002422 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002423 if (ret < 0)
2424 {
2425 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2426 return 1;
2427 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002428 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002429
2430 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002431 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002432 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002433 if (ret < 0)
2434 {
2435 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002436 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002437
2438 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002439 std::string entryTimeStr;
2440 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002441 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002442 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002443 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002444
2445 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002446 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002447 bmcJournalLogEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2448 "redfish", "v1", "Managers", "bmc", "LogServices", "Journal", "Entries",
2449 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002450 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2451 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2452 bmcJournalLogEntryJson["Message"] = std::move(message);
2453 bmcJournalLogEntryJson["EntryType"] = "Oem";
2454 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2455 : severity <= 4 ? "Warning"
2456 : "OK";
2457 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2458 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002459 return 0;
2460}
2461
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002462inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002463{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002464 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002465 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002466 .methods(boost::beast::http::verb::get)(
2467 [&app](const crow::Request& req,
2468 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2469 query_param::QueryCapabilities capabilities = {
2470 .canDelegateTop = true,
2471 .canDelegateSkip = true,
2472 };
2473 query_param::Query delegatedQuery;
2474 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002475 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002476 {
2477 return;
2478 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002479
2480 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002481 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002482
Ed Tanous002d39b2022-05-31 08:59:27 -07002483 // Collections don't include the static data added by SubRoute
2484 // because it has a duplicate entry for members
2485 asyncResp->res.jsonValue["@odata.type"] =
2486 "#LogEntryCollection.LogEntryCollection";
2487 asyncResp->res.jsonValue["@odata.id"] =
2488 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2489 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2490 asyncResp->res.jsonValue["Description"] =
2491 "Collection of BMC Journal Entries";
2492 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2493 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002494
Ed Tanous002d39b2022-05-31 08:59:27 -07002495 // Go through the journal and use the timestamp to create a
2496 // unique ID for each entry
2497 sd_journal* journalTmp = nullptr;
2498 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2499 if (ret < 0)
2500 {
2501 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2502 messages::internalError(asyncResp->res);
2503 return;
2504 }
2505 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2506 journalTmp, sd_journal_close);
2507 journalTmp = nullptr;
2508 uint64_t entryCount = 0;
2509 // Reset the unique ID on the first entry
2510 bool firstEntry = true;
2511 SD_JOURNAL_FOREACH(journal.get())
2512 {
2513 entryCount++;
2514 // Handle paging using skip (number of entries to skip from
2515 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002516 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002517 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002518 continue;
2519 }
2520
2521 std::string idStr;
2522 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2523 {
2524 continue;
2525 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002526 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002527
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002528 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002529 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2530 bmcJournalLogEntry) != 0)
2531 {
George Liu0fda0f12021-11-16 10:06:17 +08002532 messages::internalError(asyncResp->res);
2533 return;
2534 }
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002535 logEntryArray.push_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002536 }
2537 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002538 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002539 {
2540 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2541 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002542 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002543 }
George Liu0fda0f12021-11-16 10:06:17 +08002544 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002545}
Jason M. Billse1f26342018-07-18 12:12:00 -07002546
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002547inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002548{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002549 BMCWEB_ROUTE(app,
2550 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002551 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002552 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002553 [&app](const crow::Request& req,
2554 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2555 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002556 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002557 {
2558 return;
2559 }
2560 // Convert the unique ID back to a timestamp to find the entry
2561 uint64_t ts = 0;
2562 uint64_t index = 0;
2563 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2564 {
2565 return;
2566 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002567
Ed Tanous002d39b2022-05-31 08:59:27 -07002568 sd_journal* journalTmp = nullptr;
2569 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2570 if (ret < 0)
2571 {
2572 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2573 messages::internalError(asyncResp->res);
2574 return;
2575 }
2576 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2577 journalTmp, sd_journal_close);
2578 journalTmp = nullptr;
2579 // Go to the timestamp in the log and move to the entry at the
2580 // index tracking the unique ID
2581 std::string idStr;
2582 bool firstEntry = true;
2583 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2584 if (ret < 0)
2585 {
2586 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2587 << strerror(-ret);
2588 messages::internalError(asyncResp->res);
2589 return;
2590 }
2591 for (uint64_t i = 0; i <= index; i++)
2592 {
2593 sd_journal_next(journal.get());
2594 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2595 {
2596 messages::internalError(asyncResp->res);
2597 return;
2598 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002599 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002600 }
2601 // Confirm that the entry ID matches what was requested
2602 if (idStr != entryID)
2603 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002604 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002605 return;
2606 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002607
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002608 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002609 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002610 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002611 {
2612 messages::internalError(asyncResp->res);
2613 return;
2614 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002615 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002616 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002617}
2618
Claire Weinanfdd26902022-03-01 14:18:25 -08002619inline void
2620 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2621 const std::string& dumpType)
2622{
2623 std::string dumpPath;
2624 std::string overWritePolicy;
2625 bool collectDiagnosticDataSupported = false;
2626
2627 if (dumpType == "BMC")
2628 {
2629 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2630 overWritePolicy = "WrapsWhenFull";
2631 collectDiagnosticDataSupported = true;
2632 }
2633 else if (dumpType == "FaultLog")
2634 {
2635 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2636 overWritePolicy = "Unknown";
2637 collectDiagnosticDataSupported = false;
2638 }
2639 else if (dumpType == "System")
2640 {
2641 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2642 overWritePolicy = "WrapsWhenFull";
2643 collectDiagnosticDataSupported = true;
2644 }
2645 else
2646 {
2647 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2648 << dumpType;
2649 messages::internalError(asyncResp->res);
2650 return;
2651 }
2652
2653 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2654 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2655 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2656 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2657 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2658 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2659
2660 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002661 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002662 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2663 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2664 redfishDateTimeOffset.second;
2665
2666 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002667
2668 if (collectDiagnosticDataSupported)
2669 {
2670 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2671 ["target"] =
2672 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2673 }
Claire Weinan0d946212022-07-13 19:40:19 -07002674
2675 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2676 dbus::utility::getSubTreePaths(
2677 "/xyz/openbmc_project/dump", 0, interfaces,
2678 [asyncResp, dumpType, dumpPath](
2679 const boost::system::error_code& ec,
2680 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2681 if (ec)
2682 {
2683 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2684 << ec;
2685 // Assume that getting an error simply means there are no dump
2686 // LogServices. Return without adding any error response.
2687 return;
2688 }
2689
2690 const std::string dbusDumpPath =
2691 "/xyz/openbmc_project/dump/" +
2692 boost::algorithm::to_lower_copy(dumpType);
2693
2694 for (const std::string& path : subTreePaths)
2695 {
2696 if (path == dbusDumpPath)
2697 {
2698 asyncResp->res
2699 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2700 dumpPath + "/Actions/LogService.ClearLog";
2701 break;
2702 }
2703 }
2704 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002705}
2706
2707inline void handleLogServicesDumpServiceGet(
2708 crow::App& app, const std::string& dumpType, const crow::Request& req,
2709 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2710{
2711 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2712 {
2713 return;
2714 }
2715 getDumpServiceInfo(asyncResp, dumpType);
2716}
2717
Ed Tanous22d268c2022-05-19 09:39:07 -07002718inline void handleLogServicesDumpServiceComputerSystemGet(
2719 crow::App& app, const crow::Request& req,
2720 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2721 const std::string& chassisId)
2722{
2723 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2724 {
2725 return;
2726 }
2727 if (chassisId != "system")
2728 {
2729 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2730 return;
2731 }
2732 getDumpServiceInfo(asyncResp, "System");
2733}
2734
Claire Weinanfdd26902022-03-01 14:18:25 -08002735inline void handleLogServicesDumpEntriesCollectionGet(
2736 crow::App& app, const std::string& dumpType, const crow::Request& req,
2737 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2738{
2739 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2740 {
2741 return;
2742 }
2743 getDumpEntryCollection(asyncResp, dumpType);
2744}
2745
Ed Tanous22d268c2022-05-19 09:39:07 -07002746inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2747 crow::App& app, const crow::Request& req,
2748 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2749 const std::string& chassisId)
2750{
2751 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2752 {
2753 return;
2754 }
2755 if (chassisId != "system")
2756 {
2757 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2758 return;
2759 }
2760 getDumpEntryCollection(asyncResp, "System");
2761}
2762
Claire Weinanfdd26902022-03-01 14:18:25 -08002763inline void handleLogServicesDumpEntryGet(
2764 crow::App& app, const std::string& dumpType, const crow::Request& req,
2765 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2766 const std::string& dumpId)
2767{
2768 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2769 {
2770 return;
2771 }
2772 getDumpEntryById(asyncResp, dumpId, dumpType);
2773}
Ed Tanous22d268c2022-05-19 09:39:07 -07002774inline void handleLogServicesDumpEntryComputerSystemGet(
2775 crow::App& app, const crow::Request& req,
2776 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2777 const std::string& chassisId, const std::string& dumpId)
2778{
2779 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2780 {
2781 return;
2782 }
2783 if (chassisId != "system")
2784 {
2785 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2786 return;
2787 }
2788 getDumpEntryById(asyncResp, dumpId, "System");
2789}
Claire Weinanfdd26902022-03-01 14:18:25 -08002790
2791inline void handleLogServicesDumpEntryDelete(
2792 crow::App& app, const std::string& dumpType, const crow::Request& req,
2793 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2794 const std::string& dumpId)
2795{
2796 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2797 {
2798 return;
2799 }
2800 deleteDumpEntry(asyncResp, dumpId, dumpType);
2801}
2802
Ed Tanous22d268c2022-05-19 09:39:07 -07002803inline void handleLogServicesDumpEntryComputerSystemDelete(
2804 crow::App& app, const crow::Request& req,
2805 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2806 const std::string& chassisId, const std::string& dumpId)
2807{
2808 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2809 {
2810 return;
2811 }
2812 if (chassisId != "system")
2813 {
2814 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2815 return;
2816 }
2817 deleteDumpEntry(asyncResp, dumpId, "System");
2818}
2819
Claire Weinanfdd26902022-03-01 14:18:25 -08002820inline void handleLogServicesDumpCollectDiagnosticDataPost(
2821 crow::App& app, const std::string& dumpType, const crow::Request& req,
2822 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2823{
2824 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2825 {
2826 return;
2827 }
2828 createDump(asyncResp, req, dumpType);
2829}
2830
Ed Tanous22d268c2022-05-19 09:39:07 -07002831inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2832 crow::App& app, const crow::Request& req,
2833 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2834 const std::string& chassisId)
2835{
2836 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2837 {
2838 return;
2839 }
2840 if (chassisId != "system")
2841 {
2842 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2843 return;
2844 }
2845 createDump(asyncResp, req, "System");
2846}
2847
Claire Weinanfdd26902022-03-01 14:18:25 -08002848inline void handleLogServicesDumpClearLogPost(
2849 crow::App& app, const std::string& dumpType, const crow::Request& req,
2850 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2851{
2852 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2853 {
2854 return;
2855 }
2856 clearDump(asyncResp, dumpType);
2857}
2858
Ed Tanous22d268c2022-05-19 09:39:07 -07002859inline void handleLogServicesDumpClearLogComputerSystemPost(
2860 crow::App& app, const crow::Request& req,
2861 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2862 const std::string& chassisId)
2863{
2864 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2865 {
2866 return;
2867 }
2868 if (chassisId != "system")
2869 {
2870 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2871 return;
2872 }
2873 clearDump(asyncResp, "System");
2874}
2875
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002876inline void requestRoutesBMCDumpService(App& app)
2877{
2878 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002879 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002880 .methods(boost::beast::http::verb::get)(std::bind_front(
2881 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002882}
2883
2884inline void requestRoutesBMCDumpEntryCollection(App& app)
2885{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002886 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002887 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002888 .methods(boost::beast::http::verb::get)(std::bind_front(
2889 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002890}
2891
2892inline void requestRoutesBMCDumpEntry(App& app)
2893{
2894 BMCWEB_ROUTE(app,
2895 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002896 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002897 .methods(boost::beast::http::verb::get)(std::bind_front(
2898 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2899
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002900 BMCWEB_ROUTE(app,
2901 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002902 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002903 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2904 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002905}
2906
2907inline void requestRoutesBMCDumpCreate(App& app)
2908{
George Liu0fda0f12021-11-16 10:06:17 +08002909 BMCWEB_ROUTE(
2910 app,
2911 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002912 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002913 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002914 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2915 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002916}
2917
2918inline void requestRoutesBMCDumpClear(App& app)
2919{
George Liu0fda0f12021-11-16 10:06:17 +08002920 BMCWEB_ROUTE(
2921 app,
2922 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002923 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002924 .methods(boost::beast::http::verb::post)(std::bind_front(
2925 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2926}
2927
2928inline void requestRoutesFaultLogDumpService(App& app)
2929{
2930 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2931 .privileges(redfish::privileges::getLogService)
2932 .methods(boost::beast::http::verb::get)(std::bind_front(
2933 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2934}
2935
2936inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2937{
2938 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2939 .privileges(redfish::privileges::getLogEntryCollection)
2940 .methods(boost::beast::http::verb::get)(
2941 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2942 std::ref(app), "FaultLog"));
2943}
2944
2945inline void requestRoutesFaultLogDumpEntry(App& app)
2946{
2947 BMCWEB_ROUTE(app,
2948 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2949 .privileges(redfish::privileges::getLogEntry)
2950 .methods(boost::beast::http::verb::get)(std::bind_front(
2951 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2952
2953 BMCWEB_ROUTE(app,
2954 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2955 .privileges(redfish::privileges::deleteLogEntry)
2956 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2957 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2958}
2959
2960inline void requestRoutesFaultLogDumpClear(App& app)
2961{
2962 BMCWEB_ROUTE(
2963 app,
2964 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2965 .privileges(redfish::privileges::postLogService)
2966 .methods(boost::beast::http::verb::post)(std::bind_front(
2967 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002968}
2969
2970inline void requestRoutesSystemDumpService(App& app)
2971{
Ed Tanous22d268c2022-05-19 09:39:07 -07002972 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002973 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002974 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002975 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002976}
2977
2978inline void requestRoutesSystemDumpEntryCollection(App& app)
2979{
Ed Tanous22d268c2022-05-19 09:39:07 -07002980 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002981 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002982 .methods(boost::beast::http::verb::get)(std::bind_front(
2983 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2984 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002985}
2986
2987inline void requestRoutesSystemDumpEntry(App& app)
2988{
2989 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002990 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002991 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002992 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002993 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002994
2995 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002996 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002997 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002998 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002999 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003000}
3001
3002inline void requestRoutesSystemDumpCreate(App& app)
3003{
George Liu0fda0f12021-11-16 10:06:17 +08003004 BMCWEB_ROUTE(
3005 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003006 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003007 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003008 .methods(boost::beast::http::verb::post)(std::bind_front(
3009 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3010 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003011}
3012
3013inline void requestRoutesSystemDumpClear(App& app)
3014{
George Liu0fda0f12021-11-16 10:06:17 +08003015 BMCWEB_ROUTE(
3016 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003017 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003018 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003019 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003020 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003021}
3022
3023inline void requestRoutesCrashdumpService(App& app)
3024{
3025 // Note: Deviated from redfish privilege registry for GET & HEAD
3026 // method for security reasons.
3027 /**
3028 * Functions triggers appropriate requests on DBus
3029 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003030 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003031 // This is incorrect, should be:
3032 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003033 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003034 .methods(boost::beast::http::verb::get)(
3035 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003036 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3037 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003038 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003039 {
3040 return;
3041 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003042 if (systemName != "system")
3043 {
3044 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3045 systemName);
3046 return;
3047 }
3048
Ed Tanous002d39b2022-05-31 08:59:27 -07003049 // Copy over the static data to include the entries added by
3050 // SubRoute
3051 asyncResp->res.jsonValue["@odata.id"] =
3052 "/redfish/v1/Systems/system/LogServices/Crashdump";
3053 asyncResp->res.jsonValue["@odata.type"] =
3054 "#LogService.v1_2_0.LogService";
3055 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3056 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
3057 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
3058 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3059 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303060
Ed Tanous002d39b2022-05-31 08:59:27 -07003061 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003062 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003063 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3064 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3065 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303066
Ed Tanous002d39b2022-05-31 08:59:27 -07003067 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00003068 crow::utility::urlFromPieces("redfish", "v1", "Systems", "system",
3069 "LogServices", "Crashdump", "Entries");
Ed Tanous002d39b2022-05-31 08:59:27 -07003070 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3071 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3072 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3073 ["target"] =
3074 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003075 });
3076}
3077
3078void inline requestRoutesCrashdumpClear(App& app)
3079{
George Liu0fda0f12021-11-16 10:06:17 +08003080 BMCWEB_ROUTE(
3081 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003082 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003083 // This is incorrect, should be:
3084 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003085 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003086 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003087 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003088 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3089 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003090 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003091 {
3092 return;
3093 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003094 if (systemName != "system")
3095 {
3096 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3097 systemName);
3098 return;
3099 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003100 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003101 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003102 const std::string&) {
3103 if (ec)
3104 {
3105 messages::internalError(asyncResp->res);
3106 return;
3107 }
3108 messages::success(asyncResp->res);
3109 },
3110 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3111 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003112}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003113
zhanghch058d1b46d2021-04-01 11:18:24 +08003114static void
3115 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3116 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003117{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003118 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003119 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003120 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003121 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003122 if (ec)
3123 {
3124 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3125 if (ec.value() ==
3126 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003127 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003128 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003129 }
3130 else
3131 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003132 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003133 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003134 return;
3135 }
3136
3137 std::string timestamp{};
3138 std::string filename{};
3139 std::string logfile{};
3140 parseCrashdumpParameters(params, filename, timestamp, logfile);
3141
3142 if (filename.empty() || timestamp.empty())
3143 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003144 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003145 return;
3146 }
3147
3148 std::string crashdumpURI =
3149 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3150 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003151 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003152 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003153 logEntry["@odata.id"] = crow::utility::urlFromPieces(
3154 "redfish", "v1", "Systems", "system", "LogServices", "Crashdump",
3155 "Entries", logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003156 logEntry["Name"] = "CPU Crashdump";
3157 logEntry["Id"] = logID;
3158 logEntry["EntryType"] = "Oem";
3159 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3160 logEntry["DiagnosticDataType"] = "OEM";
3161 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3162 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003163
3164 // If logEntryJson references an array of LogEntry resources
3165 // ('Members' list), then push this as a new entry, otherwise set it
3166 // directly
3167 if (logEntryJson.is_array())
3168 {
3169 logEntryJson.push_back(logEntry);
3170 asyncResp->res.jsonValue["Members@odata.count"] =
3171 logEntryJson.size();
3172 }
3173 else
3174 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003175 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003176 }
3177 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003178 sdbusplus::asio::getAllProperties(
3179 *crow::connections::systemBus, crashdumpObject,
3180 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3181 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003182}
3183
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003184inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003185{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003186 // Note: Deviated from redfish privilege registry for GET & HEAD
3187 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003188 /**
3189 * Functions triggers appropriate requests on DBus
3190 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003191 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003192 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003193 // This is incorrect, should be.
3194 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003195 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003196 .methods(boost::beast::http::verb::get)(
3197 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003198 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3199 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003200 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003201 {
3202 return;
3203 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003204 if (systemName != "system")
3205 {
3206 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3207 systemName);
3208 return;
3209 }
3210
George Liu7a1dbc42022-12-07 16:03:22 +08003211 constexpr std::array<std::string_view, 1> interfaces = {
3212 crashdumpInterface};
3213 dbus::utility::getSubTreePaths(
3214 "/", 0, interfaces,
3215 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003216 const std::vector<std::string>& resp) {
3217 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003218 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003219 if (ec.value() !=
3220 boost::system::errc::no_such_file_or_directory)
3221 {
3222 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3223 << ec.message();
3224 messages::internalError(asyncResp->res);
3225 return;
3226 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003227 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003228 asyncResp->res.jsonValue["@odata.type"] =
3229 "#LogEntryCollection.LogEntryCollection";
3230 asyncResp->res.jsonValue["@odata.id"] =
3231 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3232 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3233 asyncResp->res.jsonValue["Description"] =
3234 "Collection of Crashdump Entries";
3235 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3236 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003237
Ed Tanous002d39b2022-05-31 08:59:27 -07003238 for (const std::string& path : resp)
3239 {
3240 const sdbusplus::message::object_path objPath(path);
3241 // Get the log ID
3242 std::string logID = objPath.filename();
3243 if (logID.empty())
3244 {
3245 continue;
3246 }
3247 // Add the log entry to the array
3248 logCrashdumpEntry(asyncResp, logID,
3249 asyncResp->res.jsonValue["Members"]);
3250 }
George Liu7a1dbc42022-12-07 16:03:22 +08003251 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003252 });
3253}
Ed Tanous1da66f72018-07-27 16:13:37 -07003254
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003255inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003256{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003257 // Note: Deviated from redfish privilege registry for GET & HEAD
3258 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003259
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003260 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003261 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003262 // this is incorrect, should be
3263 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003264 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003265 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003266 [&app](const crow::Request& req,
3267 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003268 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003269 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003270 {
3271 return;
3272 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003273 if (systemName != "system")
3274 {
3275 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3276 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003277 return;
3278 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003279 const std::string& logID = param;
3280 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3281 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003282}
Ed Tanous1da66f72018-07-27 16:13:37 -07003283
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003284inline void requestRoutesCrashdumpFile(App& app)
3285{
3286 // Note: Deviated from redfish privilege registry for GET & HEAD
3287 // method for security reasons.
3288 BMCWEB_ROUTE(
3289 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003290 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003291 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003292 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003293 [](const crow::Request& req,
3294 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003295 const std::string& systemName, const std::string& logID,
3296 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003297 // Do not call getRedfishRoute here since the crashdump file is not a
3298 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003299
3300 if (systemName != "system")
3301 {
3302 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3303 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003304 return;
3305 }
3306
Ed Tanous002d39b2022-05-31 08:59:27 -07003307 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003308 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003309 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003310 const std::vector<
3311 std::pair<std::string, dbus::utility::DbusVariantType>>&
3312 resp) {
3313 if (ec)
3314 {
3315 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3316 messages::internalError(asyncResp->res);
3317 return;
3318 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003319
Ed Tanous002d39b2022-05-31 08:59:27 -07003320 std::string dbusFilename{};
3321 std::string dbusTimestamp{};
3322 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003323
Ed Tanous002d39b2022-05-31 08:59:27 -07003324 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3325 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003326
Ed Tanous002d39b2022-05-31 08:59:27 -07003327 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3328 dbusFilepath.empty())
3329 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003330 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003331 return;
3332 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003333
Ed Tanous002d39b2022-05-31 08:59:27 -07003334 // Verify the file name parameter is correct
3335 if (fileName != dbusFilename)
3336 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003337 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003338 return;
3339 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003340
Ed Tanous002d39b2022-05-31 08:59:27 -07003341 if (!std::filesystem::exists(dbusFilepath))
3342 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003343 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003344 return;
3345 }
3346 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3347 asyncResp->res.body() =
3348 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003349
Ed Tanous002d39b2022-05-31 08:59:27 -07003350 // Configure this to be a file download when accessed
3351 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003352 asyncResp->res.addHeader(
3353 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003354 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003355 sdbusplus::asio::getAllProperties(
3356 *crow::connections::systemBus, crashdumpObject,
3357 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3358 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003359 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003360}
3361
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003362enum class OEMDiagnosticType
3363{
3364 onDemand,
3365 telemetry,
3366 invalid,
3367};
3368
Ed Tanous26ccae32023-02-16 10:28:44 -08003369inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003370{
3371 if (oemDiagStr == "OnDemand")
3372 {
3373 return OEMDiagnosticType::onDemand;
3374 }
3375 if (oemDiagStr == "Telemetry")
3376 {
3377 return OEMDiagnosticType::telemetry;
3378 }
3379
3380 return OEMDiagnosticType::invalid;
3381}
3382
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003383inline void requestRoutesCrashdumpCollect(App& app)
3384{
3385 // Note: Deviated from redfish privilege registry for GET & HEAD
3386 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003387 BMCWEB_ROUTE(
3388 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003389 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003390 // The below is incorrect; Should be ConfigureManager
3391 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003392 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003393 .methods(boost::beast::http::verb::post)(
3394 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003395 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3396 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003397 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003398 {
3399 return;
3400 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003401
3402 if (systemName != "system")
3403 {
3404 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3405 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003406 return;
3407 }
3408
Ed Tanous002d39b2022-05-31 08:59:27 -07003409 std::string diagnosticDataType;
3410 std::string oemDiagnosticDataType;
3411 if (!redfish::json_util::readJsonAction(
3412 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3413 "OEMDiagnosticDataType", oemDiagnosticDataType))
3414 {
3415 return;
3416 }
3417
3418 if (diagnosticDataType != "OEM")
3419 {
3420 BMCWEB_LOG_ERROR
3421 << "Only OEM DiagnosticDataType supported for Crashdump";
3422 messages::actionParameterValueFormatError(
3423 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3424 "CollectDiagnosticData");
3425 return;
3426 }
3427
3428 OEMDiagnosticType oemDiagType =
3429 getOEMDiagnosticType(oemDiagnosticDataType);
3430
3431 std::string iface;
3432 std::string method;
3433 std::string taskMatchStr;
3434 if (oemDiagType == OEMDiagnosticType::onDemand)
3435 {
3436 iface = crashdumpOnDemandInterface;
3437 method = "GenerateOnDemandLog";
3438 taskMatchStr = "type='signal',"
3439 "interface='org.freedesktop.DBus.Properties',"
3440 "member='PropertiesChanged',"
3441 "arg0namespace='com.intel.crashdump'";
3442 }
3443 else if (oemDiagType == OEMDiagnosticType::telemetry)
3444 {
3445 iface = crashdumpTelemetryInterface;
3446 method = "GenerateTelemetryLog";
3447 taskMatchStr = "type='signal',"
3448 "interface='org.freedesktop.DBus.Properties',"
3449 "member='PropertiesChanged',"
3450 "arg0namespace='com.intel.crashdump'";
3451 }
3452 else
3453 {
3454 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3455 << oemDiagnosticDataType;
3456 messages::actionParameterValueFormatError(
3457 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3458 "CollectDiagnosticData");
3459 return;
3460 }
3461
3462 auto collectCrashdumpCallback =
3463 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003464 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003465 const std::string&) mutable {
3466 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003467 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003468 if (ec.value() == boost::system::errc::operation_not_supported)
3469 {
3470 messages::resourceInStandby(asyncResp->res);
3471 }
3472 else if (ec.value() ==
3473 boost::system::errc::device_or_resource_busy)
3474 {
3475 messages::serviceTemporarilyUnavailable(asyncResp->res,
3476 "60");
3477 }
3478 else
3479 {
3480 messages::internalError(asyncResp->res);
3481 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003482 return;
3483 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003484 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003485 [](const boost::system::error_code& err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003486 const std::shared_ptr<task::TaskData>& taskData) {
3487 if (!err)
3488 {
3489 taskData->messages.emplace_back(messages::taskCompletedOK(
3490 std::to_string(taskData->index)));
3491 taskData->state = "Completed";
3492 }
3493 return task::completed;
3494 },
3495 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003496
Ed Tanous002d39b2022-05-31 08:59:27 -07003497 task->startTimer(std::chrono::minutes(5));
3498 task->populateResp(asyncResp->res);
3499 task->payload.emplace(std::move(payload));
3500 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003501
Ed Tanous002d39b2022-05-31 08:59:27 -07003502 crow::connections::systemBus->async_method_call(
3503 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3504 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003505 });
3506}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003507
Andrew Geisslercb92c032018-08-17 07:56:14 -07003508/**
3509 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3510 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003511inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003512{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003513 /**
3514 * Function handles POST method request.
3515 * The Clear Log actions does not require any parameter.The action deletes
3516 * all entries found in the Entries collection for this Log Service.
3517 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003518
George Liu0fda0f12021-11-16 10:06:17 +08003519 BMCWEB_ROUTE(
3520 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003521 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003522 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003523 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003524 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003525 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3526 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003527 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003528 {
3529 return;
3530 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003531 if (systemName != "system")
3532 {
3533 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3534 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003535 return;
3536 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003537 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003538
Ed Tanous002d39b2022-05-31 08:59:27 -07003539 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003540 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003541 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3542 if (ec)
3543 {
3544 // TODO Handle for specific error code
3545 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3546 asyncResp->res.result(
3547 boost::beast::http::status::internal_server_error);
3548 return;
3549 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003550
Ed Tanous002d39b2022-05-31 08:59:27 -07003551 asyncResp->res.result(boost::beast::http::status::no_content);
3552 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003553
Ed Tanous002d39b2022-05-31 08:59:27 -07003554 // Make call to Logging service to request Clear Log
3555 crow::connections::systemBus->async_method_call(
3556 respHandler, "xyz.openbmc_project.Logging",
3557 "/xyz/openbmc_project/logging",
3558 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3559 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003560}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003561
3562/****************************************************
3563 * Redfish PostCode interfaces
3564 * using DBUS interface: getPostCodesTS
3565 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003566inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003567{
Ed Tanous22d268c2022-05-19 09:39:07 -07003568 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003569 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003570 .methods(boost::beast::http::verb::get)(
3571 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003572 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3573 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003574 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003575 {
3576 return;
3577 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003578 if (systemName != "system")
3579 {
3580 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3581 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003582 return;
3583 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003584 asyncResp->res.jsonValue["@odata.id"] =
3585 "/redfish/v1/Systems/system/LogServices/PostCodes";
3586 asyncResp->res.jsonValue["@odata.type"] =
3587 "#LogService.v1_1_0.LogService";
3588 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3589 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003590 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003591 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3592 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3593 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303594
Ed Tanous002d39b2022-05-31 08:59:27 -07003595 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003596 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003597 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3598 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3599 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303600
Ed Tanous002d39b2022-05-31 08:59:27 -07003601 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3602 {"target",
3603 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003604 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003605}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003606
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003607inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003608{
George Liu0fda0f12021-11-16 10:06:17 +08003609 BMCWEB_ROUTE(
3610 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003611 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003612 // The following privilege is incorrect; It should be ConfigureManager
3613 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003614 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003615 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003616 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003617 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3618 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003619 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003620 {
3621 return;
3622 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003623 if (systemName != "system")
3624 {
3625 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3626 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003627 return;
3628 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003629 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003630
Ed Tanous002d39b2022-05-31 08:59:27 -07003631 // Make call to post-code service to request clear all
3632 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003633 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003634 if (ec)
3635 {
3636 // TODO Handle for specific error code
3637 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3638 << ec;
3639 asyncResp->res.result(
3640 boost::beast::http::status::internal_server_error);
3641 messages::internalError(asyncResp->res);
3642 return;
3643 }
3644 },
3645 "xyz.openbmc_project.State.Boot.PostCode0",
3646 "/xyz/openbmc_project/State/Boot/PostCode0",
3647 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3648 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003649}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003650
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003651/**
3652 * @brief Parse post code ID and get the current value and index value
3653 * eg: postCodeID=B1-2, currentValue=1, index=2
3654 *
3655 * @param[in] postCodeID Post Code ID
3656 * @param[out] currentValue Current value
3657 * @param[out] index Index value
3658 *
3659 * @return bool true if the parsing is successful, false the parsing fails
3660 */
3661inline static bool parsePostCode(const std::string& postCodeID,
3662 uint64_t& currentValue, uint16_t& index)
3663{
3664 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003665 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003666 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3667 {
3668 return false;
3669 }
3670
3671 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3672 const char* start = split[0].data() + 1;
3673 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3674 const char* end = split[0].data() + split[0].size();
3675 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3676
3677 if (ptrIndex != end || ecIndex != std::errc())
3678 {
3679 return false;
3680 }
3681
3682 start = split[1].data();
3683
3684 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3685 end = split[1].data() + split[1].size();
3686 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3687
3688 return ptrValue == end && ecValue == std::errc();
3689}
3690
3691static bool fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003692 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303693 const boost::container::flat_map<
3694 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003695 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3696 const uint64_t skip = 0, const uint64_t top = 0)
3697{
3698 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003699 const registries::Message* message =
3700 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003701
3702 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003703 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303704 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3705 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003706 {
3707 currentCodeIndex++;
3708 std::string postcodeEntryID =
3709 "B" + std::to_string(bootIndex) + "-" +
3710 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3711
3712 uint64_t usecSinceEpoch = code.first;
3713 uint64_t usTimeOffset = 0;
3714
3715 if (1 == currentCodeIndex)
3716 { // already incremented
3717 firstCodeTimeUs = code.first;
3718 }
3719 else
3720 {
3721 usTimeOffset = code.first - firstCodeTimeUs;
3722 }
3723
3724 // skip if no specific codeIndex is specified and currentCodeIndex does
3725 // not fall between top and skip
3726 if ((codeIndex == 0) &&
3727 (currentCodeIndex <= skip || currentCodeIndex > top))
3728 {
3729 continue;
3730 }
3731
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003732 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003733 // currentIndex
3734 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3735 {
3736 // This is done for simplicity. 1st entry is needed to calculate
3737 // time offset. To improve efficiency, one can get to the entry
3738 // directly (possibly with flatmap's nth method)
3739 continue;
3740 }
3741
3742 // currentCodeIndex is within top and skip or equal to specified code
3743 // index
3744
3745 // Get the Created time from the timestamp
3746 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003747 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003748
3749 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3750 std::ostringstream hexCode;
3751 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303752 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003753 std::ostringstream timeOffsetStr;
3754 // Set Fixed -Point Notation
3755 timeOffsetStr << std::fixed;
3756 // Set precision to 4 digits
3757 timeOffsetStr << std::setprecision(4);
3758 // Add double to stream
3759 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3760 std::vector<std::string> messageArgs = {
3761 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3762
3763 // Get MessageArgs template from message registry
3764 std::string msg;
3765 if (message != nullptr)
3766 {
3767 msg = message->message;
3768
3769 // fill in this post code value
3770 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003771 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003772 {
3773 std::string argStr = "%" + std::to_string(++i);
3774 size_t argPos = msg.find(argStr);
3775 if (argPos != std::string::npos)
3776 {
3777 msg.replace(argPos, argStr.length(), messageArg);
3778 }
3779 }
3780 }
3781
Tim Leed4342a92020-04-27 11:47:58 +08003782 // Get Severity template from message registry
3783 std::string severity;
3784 if (message != nullptr)
3785 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003786 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003787 }
3788
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003789 // Format entry
3790 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003791 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003792 bmcLogEntry["@odata.id"] = crow::utility::urlFromPieces(
3793 "redfish", "v1", "Systems", "system", "LogServices", "PostCodes",
3794 "Entries", postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003795 bmcLogEntry["Name"] = "POST Code Log Entry";
3796 bmcLogEntry["Id"] = postcodeEntryID;
3797 bmcLogEntry["Message"] = std::move(msg);
3798 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3799 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3800 bmcLogEntry["EntryType"] = "Event";
3801 bmcLogEntry["Severity"] = std::move(severity);
3802 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003803 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3804 {
3805 bmcLogEntry["AdditionalDataURI"] =
3806 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3807 postcodeEntryID + "/attachment";
3808 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003809
3810 // codeIndex is only specified when querying single entry, return only
3811 // that entry in this case
3812 if (codeIndex != 0)
3813 {
3814 aResp->res.jsonValue.update(bmcLogEntry);
3815 return true;
3816 }
3817
3818 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3819 logEntryArray.push_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003820 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003821
3822 // Return value is always false when querying multiple entries
3823 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003824}
3825
zhanghch058d1b46d2021-04-01 11:18:24 +08003826static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003827 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003828{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003829 uint16_t bootIndex = 0;
3830 uint64_t codeIndex = 0;
3831 if (!parsePostCode(entryId, codeIndex, bootIndex))
3832 {
3833 // Requested ID was not found
3834 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3835 return;
3836 }
3837
3838 if (bootIndex == 0 || codeIndex == 0)
3839 {
3840 // 0 is an invalid index
3841 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3842 return;
3843 }
3844
ZhikuiRena3316fc2020-01-29 14:58:08 -08003845 crow::connections::systemBus->async_method_call(
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003846 [aResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003847 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303848 const boost::container::flat_map<
3849 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3850 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003851 if (ec)
3852 {
3853 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3854 messages::internalError(aResp->res);
3855 return;
3856 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003857
Ed Tanous002d39b2022-05-31 08:59:27 -07003858 if (postcode.empty())
3859 {
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003860 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003861 return;
3862 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003863
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003864 if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
3865 {
3866 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3867 return;
3868 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003869 },
Jonathan Doman15124762021-01-07 17:54:17 -08003870 "xyz.openbmc_project.State.Boot.PostCode0",
3871 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003872 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3873 bootIndex);
3874}
3875
zhanghch058d1b46d2021-04-01 11:18:24 +08003876static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003877 const uint16_t bootIndex,
3878 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003879 const uint64_t entryCount, size_t skip,
3880 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003881{
3882 crow::connections::systemBus->async_method_call(
3883 [aResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003884 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303885 const boost::container::flat_map<
3886 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3887 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003888 if (ec)
3889 {
3890 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3891 messages::internalError(aResp->res);
3892 return;
3893 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003894
Ed Tanous002d39b2022-05-31 08:59:27 -07003895 uint64_t endCount = entryCount;
3896 if (!postcode.empty())
3897 {
3898 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003899 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003900 {
Patrick Williams89492a12023-05-10 07:51:34 -05003901 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
3902 entryCount) -
3903 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003904 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003905 std::min(static_cast<uint64_t>(top + skip), endCount) -
3906 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003907
3908 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3909 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003910 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003911 aResp->res.jsonValue["Members@odata.count"] = endCount;
3912 }
3913
3914 // continue to previous bootIndex
3915 if (bootIndex < bootCount)
3916 {
3917 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3918 bootCount, endCount, skip, top);
3919 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003920 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003921 {
3922 aResp->res.jsonValue["Members@odata.nextLink"] =
3923 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3924 std::to_string(skip + top);
3925 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003926 },
Jonathan Doman15124762021-01-07 17:54:17 -08003927 "xyz.openbmc_project.State.Boot.PostCode0",
3928 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003929 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3930 bootIndex);
3931}
3932
zhanghch058d1b46d2021-04-01 11:18:24 +08003933static void
3934 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003935 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003936{
3937 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003938 sdbusplus::asio::getProperty<uint16_t>(
3939 *crow::connections::systemBus,
3940 "xyz.openbmc_project.State.Boot.PostCode0",
3941 "/xyz/openbmc_project/State/Boot/PostCode0",
3942 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003943 [aResp, entryCount, skip, top](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003944 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003945 if (ec)
3946 {
3947 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3948 messages::internalError(aResp->res);
3949 return;
3950 }
3951 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003952 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003953}
3954
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003955inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003956{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003957 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003958 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003959 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003960 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003961 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003962 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3963 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003964 query_param::QueryCapabilities capabilities = {
3965 .canDelegateTop = true,
3966 .canDelegateSkip = true,
3967 };
3968 query_param::Query delegatedQuery;
3969 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003970 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003971 {
3972 return;
3973 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003974
3975 if (systemName != "system")
3976 {
3977 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3978 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003979 return;
3980 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003981 asyncResp->res.jsonValue["@odata.type"] =
3982 "#LogEntryCollection.LogEntryCollection";
3983 asyncResp->res.jsonValue["@odata.id"] =
3984 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3985 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3986 asyncResp->res.jsonValue["Description"] =
3987 "Collection of POST Code Log Entries";
3988 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3989 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003990 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003991 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003992 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003993 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003994}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003995
George Liu647b3cd2021-07-05 12:43:56 +08003996inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3997{
George Liu0fda0f12021-11-16 10:06:17 +08003998 BMCWEB_ROUTE(
3999 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004000 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004001 .privileges(redfish::privileges::getLogEntry)
4002 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004003 [&app](const crow::Request& req,
4004 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004005 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004006 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004007 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004008 {
4009 return;
4010 }
Matt Spinler72e21372023-04-19 12:53:33 -05004011 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004012 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004013 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004014 {
4015 asyncResp->res.result(boost::beast::http::status::bad_request);
4016 return;
4017 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004018 if (systemName != "system")
4019 {
4020 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4021 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004022 return;
4023 }
George Liu647b3cd2021-07-05 12:43:56 +08004024
Ed Tanous002d39b2022-05-31 08:59:27 -07004025 uint64_t currentValue = 0;
4026 uint16_t index = 0;
4027 if (!parsePostCode(postCodeID, currentValue, index))
4028 {
4029 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4030 return;
4031 }
George Liu647b3cd2021-07-05 12:43:56 +08004032
Ed Tanous002d39b2022-05-31 08:59:27 -07004033 crow::connections::systemBus->async_method_call(
4034 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004035 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004036 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4037 postcodes) {
4038 if (ec.value() == EBADR)
4039 {
4040 messages::resourceNotFound(asyncResp->res, "LogEntry",
4041 postCodeID);
4042 return;
4043 }
4044 if (ec)
4045 {
4046 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4047 messages::internalError(asyncResp->res);
4048 return;
4049 }
George Liu647b3cd2021-07-05 12:43:56 +08004050
Ed Tanous002d39b2022-05-31 08:59:27 -07004051 size_t value = static_cast<size_t>(currentValue) - 1;
4052 if (value == std::string::npos || postcodes.size() < currentValue)
4053 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004054 BMCWEB_LOG_WARNING << "Wrong currentValue value";
Ed Tanous002d39b2022-05-31 08:59:27 -07004055 messages::resourceNotFound(asyncResp->res, "LogEntry",
4056 postCodeID);
4057 return;
4058 }
George Liu647b3cd2021-07-05 12:43:56 +08004059
Ed Tanous002d39b2022-05-31 08:59:27 -07004060 const auto& [tID, c] = postcodes[value];
4061 if (c.empty())
4062 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004063 BMCWEB_LOG_WARNING << "No found post code data";
Ed Tanous002d39b2022-05-31 08:59:27 -07004064 messages::resourceNotFound(asyncResp->res, "LogEntry",
4065 postCodeID);
4066 return;
4067 }
4068 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4069 const char* d = reinterpret_cast<const char*>(c.data());
4070 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004071
Ed Tanousd9f6c622022-03-17 09:12:17 -07004072 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004073 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004074 asyncResp->res.addHeader(
4075 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004076 asyncResp->res.body() = crow::utility::base64encode(strData);
4077 },
4078 "xyz.openbmc_project.State.Boot.PostCode0",
4079 "/xyz/openbmc_project/State/Boot/PostCode0",
4080 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4081 });
George Liu647b3cd2021-07-05 12:43:56 +08004082}
4083
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004084inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004085{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004086 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004087 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004088 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004089 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004090 [&app](const crow::Request& req,
4091 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004092 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004093 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004094 {
4095 return;
4096 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004097 if (systemName != "system")
4098 {
4099 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4100 systemName);
4101 return;
4102 }
4103
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004104 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004105 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004106}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004107
Ed Tanous1da66f72018-07-27 16:13:37 -07004108} // namespace redfish