blob: 24a065721cb792c0fae3caf083a5a2650ff0a807 [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"
Spencer Kub7028eb2021-10-26 15:27:35 +080021#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080022#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080023#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "query.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070025#include "registries.hpp"
26#include "registries/base_message_registry.hpp"
27#include "registries/openbmc_message_registry.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080028#include "registries/privilege_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080029#include "task.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080030#include "utils/dbus_utils.hpp"
31#include "utils/time_utils.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070032
Jason M. Billse1f26342018-07-18 12:12:00 -070033#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060034#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060035#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070036
Ed Tanous9896eae2022-07-23 15:07:33 -070037#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070038#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060039#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070040#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070041#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070042#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080043#include <boost/system/linux_error.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020044#include <sdbusplus/asio/property.hpp>
45#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050046
George Liu7a1dbc42022-12-07 16:03:22 +080047#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080048#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070049#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080050#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070051#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070052#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080053#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070054
55namespace redfish
56{
57
Gunnar Mills1214b7e2020-06-04 10:11:30 -050058constexpr char const* crashdumpObject = "com.intel.crashdump";
59constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050060constexpr char const* crashdumpInterface = "com.intel.crashdump";
61constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070062 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050063constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070064 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070065constexpr char const* crashdumpTelemetryInterface =
66 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070067
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060068enum class DumpCreationProgress
69{
70 DUMP_CREATE_SUCCESS,
71 DUMP_CREATE_FAILED,
72 DUMP_CREATE_INPROGRESS
73};
74
Ed Tanousfffb8c12022-02-07 23:53:03 -080075namespace registries
Jason M. Bills4851d452019-03-28 11:27:48 -070076{
Ed Tanous26702d02021-11-03 15:02:33 -070077static const Message*
78 getMessageFromRegistry(const std::string& messageKey,
79 const std::span<const MessageEntry> registry)
Jason M. Bills4851d452019-03-28 11:27:48 -070080{
Ed Tanous002d39b2022-05-31 08:59:27 -070081 std::span<const MessageEntry>::iterator messageIt =
82 std::find_if(registry.begin(), registry.end(),
83 [&messageKey](const MessageEntry& messageEntry) {
84 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
Ed Tanous26702d02021-11-03 15:02:33 -070085 });
86 if (messageIt != registry.end())
Jason M. Bills4851d452019-03-28 11:27:48 -070087 {
88 return &messageIt->second;
89 }
90
91 return nullptr;
92}
93
Ed Tanous26ccae32023-02-16 10:28:44 -080094static const Message* getMessage(std::string_view messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070095{
96 // Redfish MessageIds are in the form
97 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
98 // the right Message
99 std::vector<std::string> fields;
100 fields.reserve(4);
Ed Tanous50ebd4a2023-01-19 19:03:17 -0800101 bmcweb::split(fields, messageID, '.');
Ed Tanous02cad962022-06-30 16:50:15 -0700102 const std::string& registryName = fields[0];
103 const std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -0700104
105 // Find the right registry and check it for the MessageKey
106 if (std::string(base::header.registryPrefix) == registryName)
107 {
108 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -0700109 messageKey, std::span<const MessageEntry>(base::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -0700110 }
111 if (std::string(openbmc::header.registryPrefix) == registryName)
112 {
113 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -0700114 messageKey, std::span<const MessageEntry>(openbmc::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -0700115 }
116 return nullptr;
117}
Ed Tanousfffb8c12022-02-07 23:53:03 -0800118} // namespace registries
Jason M. Bills4851d452019-03-28 11:27:48 -0700119
James Feistf6150402019-01-08 10:36:20 -0800120namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700121
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500122inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700123{
Ed Tanousd4d25792020-09-29 15:15:03 -0700124 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
125 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
126 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
127 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700128 {
129 return "Critical";
130 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700131 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
132 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
133 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700134 {
135 return "OK";
136 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700137 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700138 {
139 return "Warning";
140 }
141 return "";
142}
143
Abhishek Patel9017faf2021-09-14 22:48:55 -0500144inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
145{
146 std::optional<bool> notifyAction;
147 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
148 {
149 notifyAction = true;
150 }
151 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
152 {
153 notifyAction = false;
154 }
155
156 return notifyAction;
157}
158
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700159inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800160 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700161 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700162{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500163 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700164 size_t length = 0;
165 int ret = 0;
166 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800167 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
168 const void** dataVoid = reinterpret_cast<const void**>(&data);
169
170 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700171 if (ret < 0)
172 {
173 return ret;
174 }
Ed Tanous39e77502019-03-04 17:35:53 -0800175 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700176 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000177 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700178 return ret;
179}
180
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700181inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800182 std::string_view field, const int& base,
183 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700184{
185 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800186 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700187 // Get the metadata from the requested field of the journal entry
188 ret = getJournalMetadata(journal, field, metadata);
189 if (ret < 0)
190 {
191 return ret;
192 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000193 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700194 return ret;
195}
196
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700197inline static bool getEntryTimestamp(sd_journal* journal,
198 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800199{
200 int ret = 0;
201 uint64_t timestamp = 0;
202 ret = sd_journal_get_realtime_usec(journal, &timestamp);
203 if (ret < 0)
204 {
205 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
206 << strerror(-ret);
207 return false;
208 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300209 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500210 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800211}
Ed Tanous50b8a432022-02-03 16:29:50 -0800212
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700213inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
214 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700215{
216 int ret = 0;
217 static uint64_t prevTs = 0;
218 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700219 if (firstEntry)
220 {
221 prevTs = 0;
222 }
223
Jason M. Bills16428a12018-11-02 12:42:29 -0700224 // Get the entry timestamp
225 uint64_t curTs = 0;
226 ret = sd_journal_get_realtime_usec(journal, &curTs);
227 if (ret < 0)
228 {
229 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
230 << strerror(-ret);
231 return false;
232 }
233 // If the timestamp isn't unique, increment the index
234 if (curTs == prevTs)
235 {
236 index++;
237 }
238 else
239 {
240 // Otherwise, reset it
241 index = 0;
242 }
243 // Save the timestamp
244 prevTs = curTs;
245
246 entryID = std::to_string(curTs);
247 if (index > 0)
248 {
249 entryID += "_" + std::to_string(index);
250 }
251 return true;
252}
253
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500254static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700255 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700256{
Ed Tanous271584a2019-07-09 16:24:22 -0700257 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700258 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700259 if (firstEntry)
260 {
261 prevTs = 0;
262 }
263
Jason M. Bills95820182019-04-22 16:25:34 -0700264 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700265 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700266 std::tm timeStruct = {};
267 std::istringstream entryStream(logEntry);
268 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
269 {
270 curTs = std::mktime(&timeStruct);
271 }
272 // If the timestamp isn't unique, increment the index
273 if (curTs == prevTs)
274 {
275 index++;
276 }
277 else
278 {
279 // Otherwise, reset it
280 index = 0;
281 }
282 // Save the timestamp
283 prevTs = curTs;
284
285 entryID = std::to_string(curTs);
286 if (index > 0)
287 {
288 entryID += "_" + std::to_string(index);
289 }
290 return true;
291}
292
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700293inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800294 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
295 const std::string& entryID, uint64_t& timestamp,
296 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700297{
298 if (entryID.empty())
299 {
300 return false;
301 }
302 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800303 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700304
Ed Tanous81ce6092020-12-17 16:54:55 +0000305 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800306 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700307 {
308 // Timestamp has an index
309 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800310 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700311 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700312 auto [ptr, ec] = std::from_chars(
313 indexStr.data(), indexStr.data() + indexStr.size(), index);
314 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700315 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800316 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700317 return false;
318 }
319 }
320 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700321 auto [ptr, ec] =
322 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
323 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700324 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800325 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700326 return false;
327 }
328 return true;
329}
330
Jason M. Bills95820182019-04-22 16:25:34 -0700331static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500332 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700333{
334 static const std::filesystem::path redfishLogDir = "/var/log";
335 static const std::string redfishLogFilename = "redfish";
336
337 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500338 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700339 std::filesystem::directory_iterator(redfishLogDir))
340 {
341 // If we find a redfish log file, save the path
342 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700343 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700344 {
345 redfishLogFiles.emplace_back(redfishLogDir / filename);
346 }
347 }
348 // As the log files rotate, they are appended with a ".#" that is higher for
349 // the older logs. Since we don't expect more than 10 log files, we
350 // can just sort the list to get them in order from newest to oldest
351 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
352
353 return !redfishLogFiles.empty();
354}
355
Claire Weinanaefe3782022-07-15 19:17:19 -0700356inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800357 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700358 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700359 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
360{
361 for (const auto& interfaceMap : object.second)
362 {
363 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
364 {
365 for (const auto& propertyMap : interfaceMap.second)
366 {
367 if (propertyMap.first == "Status")
368 {
369 const auto* status =
370 std::get_if<std::string>(&propertyMap.second);
371 if (status == nullptr)
372 {
373 messages::internalError(asyncResp->res);
374 break;
375 }
376 dumpStatus = *status;
377 }
378 }
379 }
380 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
381 {
382 for (const auto& propertyMap : interfaceMap.second)
383 {
384 if (propertyMap.first == "Size")
385 {
386 const auto* sizePtr =
387 std::get_if<uint64_t>(&propertyMap.second);
388 if (sizePtr == nullptr)
389 {
390 messages::internalError(asyncResp->res);
391 break;
392 }
393 size = *sizePtr;
394 break;
395 }
396 }
397 }
398 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
399 {
400 for (const auto& propertyMap : interfaceMap.second)
401 {
402 if (propertyMap.first == "Elapsed")
403 {
404 const uint64_t* usecsTimeStamp =
405 std::get_if<uint64_t>(&propertyMap.second);
406 if (usecsTimeStamp == nullptr)
407 {
408 messages::internalError(asyncResp->res);
409 break;
410 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700411 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700412 break;
413 }
414 }
415 }
416 }
417}
418
Nan Zhou21ab4042022-06-26 23:07:40 +0000419static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800420{
421 std::string entriesPath;
422
423 if (dumpType == "BMC")
424 {
425 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
426 }
427 else if (dumpType == "FaultLog")
428 {
429 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
430 }
431 else if (dumpType == "System")
432 {
433 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
434 }
435 else
436 {
437 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
438 << dumpType;
439 }
440
441 // Returns empty string on error
442 return entriesPath;
443}
444
zhanghch058d1b46d2021-04-01 11:18:24 +0800445inline void
446 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
447 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500448{
Claire Weinanfdd26902022-03-01 14:18:25 -0800449 std::string entriesPath = getDumpEntriesPath(dumpType);
450 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500451 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500452 messages::internalError(asyncResp->res);
453 return;
454 }
455
456 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800457 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800458 dumpType](const boost::system::error_code& ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800459 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700460 if (ec)
461 {
462 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
463 messages::internalError(asyncResp->res);
464 return;
465 }
466
Claire Weinanfdd26902022-03-01 14:18:25 -0800467 // Remove ending slash
468 std::string odataIdStr = entriesPath;
469 if (!odataIdStr.empty())
470 {
471 odataIdStr.pop_back();
472 }
473
474 asyncResp->res.jsonValue["@odata.type"] =
475 "#LogEntryCollection.LogEntryCollection";
476 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
477 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
478 asyncResp->res.jsonValue["Description"] =
479 "Collection of " + dumpType + " Dump Entries";
480
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
482 entriesArray = nlohmann::json::array();
483 std::string dumpEntryPath =
484 "/xyz/openbmc_project/dump/" +
485 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
486
487 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
488 return AlphanumLess<std::string>()(l.first.filename(),
489 r.first.filename());
490 });
491
492 for (auto& object : resp)
493 {
494 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500495 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700496 continue;
497 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700498 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700499 uint64_t size = 0;
500 std::string dumpStatus;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700501 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700502
503 std::string entryID = object.first.filename();
504 if (entryID.empty())
505 {
506 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500507 }
508
Claire Weinanc6fecda2022-07-15 10:43:25 -0700509 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700510 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700511
512 if (dumpStatus !=
513 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
514 !dumpStatus.empty())
515 {
516 // Dump status is not Complete, no need to enumerate
517 continue;
518 }
519
Vijay Lobo9c11a172021-10-07 16:53:16 -0500520 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800521 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700522 thisEntry["Id"] = entryID;
523 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700524 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700525 thisEntry["Created"] =
526 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700527
Ed Tanous002d39b2022-05-31 08:59:27 -0700528 if (dumpType == "BMC")
529 {
530 thisEntry["DiagnosticDataType"] = "Manager";
531 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800532 entriesPath + entryID + "/attachment";
533 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700534 }
535 else if (dumpType == "System")
536 {
537 thisEntry["DiagnosticDataType"] = "OEM";
538 thisEntry["OEMDiagnosticDataType"] = "System";
539 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800540 entriesPath + entryID + "/attachment";
541 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700542 }
543 entriesArray.push_back(std::move(thisEntry));
544 }
545 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500546 },
547 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
548 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
549}
550
zhanghch058d1b46d2021-04-01 11:18:24 +0800551inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700552 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800553 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500554{
Claire Weinanfdd26902022-03-01 14:18:25 -0800555 std::string entriesPath = getDumpEntriesPath(dumpType);
556 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500557 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500558 messages::internalError(asyncResp->res);
559 return;
560 }
561
562 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800563 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800564 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700565 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700566 if (ec)
567 {
568 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
569 messages::internalError(asyncResp->res);
570 return;
571 }
572
573 bool foundDumpEntry = false;
574 std::string dumpEntryPath =
575 "/xyz/openbmc_project/dump/" +
576 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
577
578 for (const auto& objectPath : resp)
579 {
580 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700582 continue;
583 }
584
585 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700586 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700587 uint64_t size = 0;
588 std::string dumpStatus;
589
Claire Weinanaefe3782022-07-15 19:17:19 -0700590 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700591 timestampUs, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700592
593 if (dumpStatus !=
594 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
595 !dumpStatus.empty())
596 {
597 // Dump status is not Complete
598 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200599 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
600 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500601 return;
602 }
603
Ed Tanous002d39b2022-05-31 08:59:27 -0700604 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -0500605 "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800606 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700607 asyncResp->res.jsonValue["Id"] = entryID;
608 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700609 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700610 asyncResp->res.jsonValue["Created"] =
611 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500612
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500614 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700615 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
616 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800617 entriesPath + entryID + "/attachment";
618 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500619 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700620 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500621 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700622 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
623 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
624 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800625 entriesPath + entryID + "/attachment";
626 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500627 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700628 }
629 if (!foundDumpEntry)
630 {
631 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
632 messages::internalError(asyncResp->res);
633 return;
634 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500635 },
636 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
637 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
638}
639
zhanghch058d1b46d2021-04-01 11:18:24 +0800640inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800641 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500642 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500643{
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800645 [asyncResp, entryID](const boost::system::error_code& ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500646 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
647 if (ec)
648 {
George Liu3de8d8b2021-03-22 17:49:39 +0800649 if (ec.value() == EBADR)
650 {
651 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
652 return;
653 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500654 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800655 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500656 messages::internalError(asyncResp->res);
657 return;
658 }
659 };
660 crow::connections::systemBus->async_method_call(
661 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500662 "/xyz/openbmc_project/dump/" +
663 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
664 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500665 "xyz.openbmc_project.Object.Delete", "Delete");
666}
667
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600668inline DumpCreationProgress
669 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500670{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600671 if (status ==
672 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
673 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
674 {
675 return DumpCreationProgress::DUMP_CREATE_FAILED;
676 }
677 if (status ==
678 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
679 {
680 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
681 }
682 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
683}
684
685inline DumpCreationProgress
686 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
687{
688 for (const auto& [key, val] : values)
689 {
690 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700691 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600692 const std::string* value = std::get_if<std::string>(&val);
693 if (value == nullptr)
694 {
695 BMCWEB_LOG_ERROR << "Status property value is null";
696 return DumpCreationProgress::DUMP_CREATE_FAILED;
697 }
698 return mapDbusStatusToDumpProgress(*value);
699 }
700 }
701 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
702}
703
704inline std::string getDumpEntryPath(const std::string& dumpPath)
705{
706 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
707 {
708 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
709 }
710 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
711 {
712 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
713 }
714 return "";
715}
716
717inline void createDumpTaskCallback(
718 task::Payload&& payload,
719 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
720 const sdbusplus::message::object_path& createdObjPath)
721{
722 const std::string dumpPath = createdObjPath.parent_path().str;
723 const std::string dumpId = createdObjPath.filename();
724
725 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
726
727 if (dumpEntryPath.empty())
728 {
729 BMCWEB_LOG_ERROR << "Invalid dump type received";
730 messages::internalError(asyncResp->res);
731 return;
732 }
733
734 crow::connections::systemBus->async_method_call(
735 [asyncResp, payload, createdObjPath,
736 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800737 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600738 const std::string& introspectXml) {
739 if (ec)
740 {
741 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
742 << ec.message();
743 messages::internalError(asyncResp->res);
744 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700745 }
746
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600747 // Check if the created dump object has implemented Progress
748 // interface to track dump completion. If yes, fetch the "Status"
749 // property of the interface, modify the task state accordingly.
750 // Else, return task completed.
751 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700752
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600753 doc.Parse(introspectXml.data(), introspectXml.size());
754 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
755 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700756 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600757 BMCWEB_LOG_ERROR << "XML document failed to parse";
758 messages::internalError(asyncResp->res);
759 return;
760 }
761 tinyxml2::XMLElement* interfaceNode =
762 pRoot->FirstChildElement("interface");
763
764 bool isProgressIntfPresent = false;
765 while (interfaceNode != nullptr)
766 {
767 const char* thisInterfaceName = interfaceNode->Attribute("name");
768 if (thisInterfaceName != nullptr)
769 {
770 if (thisInterfaceName ==
771 std::string_view("xyz.openbmc_project.Common.Progress"))
772 {
773 interfaceNode =
774 interfaceNode->NextSiblingElement("interface");
775 continue;
776 }
777 isProgressIntfPresent = true;
778 break;
779 }
780 interfaceNode = interfaceNode->NextSiblingElement("interface");
781 }
782
783 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
784 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800785 const boost::system::error_code& err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600786 const std::shared_ptr<task::TaskData>& taskData) {
787 if (err)
788 {
789 BMCWEB_LOG_ERROR << createdObjPath.str
790 << ": Error in creating dump";
791 taskData->messages.emplace_back(messages::internalError());
792 taskData->state = "Cancelled";
793 return task::completed;
794 }
795
796 if (isProgressIntfPresent)
797 {
798 dbus::utility::DBusPropertiesMap values;
799 std::string prop;
800 msg.read(prop, values);
801
802 DumpCreationProgress dumpStatus =
803 getDumpCompletionStatus(values);
804 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
805 {
806 BMCWEB_LOG_ERROR << createdObjPath.str
807 << ": Error in creating dump";
808 taskData->state = "Cancelled";
809 return task::completed;
810 }
811
812 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
813 {
814 BMCWEB_LOG_DEBUG << createdObjPath.str
815 << ": Dump creation task is in progress";
816 return !task::completed;
817 }
818 }
819
Ed Tanous002d39b2022-05-31 08:59:27 -0700820 nlohmann::json retMessage = messages::success();
821 taskData->messages.emplace_back(retMessage);
822
823 std::string headerLoc =
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600824 "Location: " + dumpEntryPath + http_helpers::urlEncode(dumpId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700825 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
826
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600827 BMCWEB_LOG_DEBUG << createdObjPath.str
828 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700829 taskData->state = "Completed";
830 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600831 },
832 "type='signal',interface='org.freedesktop.DBus.Properties',"
833 "member='PropertiesChanged',path='" +
834 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500835
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600836 // The task timer is set to max time limit within which the
837 // requested dump will be collected.
838 task->startTimer(std::chrono::minutes(6));
839 task->populateResp(asyncResp->res);
840 task->payload.emplace(payload);
841 },
842 "xyz.openbmc_project.Dump.Manager", createdObjPath,
843 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500844}
845
zhanghch058d1b46d2021-04-01 11:18:24 +0800846inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
847 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500848{
Claire Weinanfdd26902022-03-01 14:18:25 -0800849 std::string dumpPath = getDumpEntriesPath(dumpType);
850 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500851 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500852 messages::internalError(asyncResp->res);
853 return;
854 }
855
856 std::optional<std::string> diagnosticDataType;
857 std::optional<std::string> oemDiagnosticDataType;
858
Willy Tu15ed6782021-12-14 11:03:16 -0800859 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500860 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
861 "OEMDiagnosticDataType", oemDiagnosticDataType))
862 {
863 return;
864 }
865
866 if (dumpType == "System")
867 {
868 if (!oemDiagnosticDataType || !diagnosticDataType)
869 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800870 BMCWEB_LOG_ERROR
871 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500872 messages::actionParameterMissing(
873 asyncResp->res, "CollectDiagnosticData",
874 "DiagnosticDataType & OEMDiagnosticDataType");
875 return;
876 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700877 if ((*oemDiagnosticDataType != "System") ||
878 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500879 {
880 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700881 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500882 return;
883 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500884 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500885 }
886 else if (dumpType == "BMC")
887 {
888 if (!diagnosticDataType)
889 {
George Liu0fda0f12021-11-16 10:06:17 +0800890 BMCWEB_LOG_ERROR
891 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500892 messages::actionParameterMissing(
893 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
894 return;
895 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700896 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500897 {
898 BMCWEB_LOG_ERROR
899 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700900 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500901 return;
902 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500903 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
904 }
905 else
906 {
907 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
908 messages::internalError(asyncResp->res);
909 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500910 }
911
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600912 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
913 createDumpParamVec;
914
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500915 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800916 [asyncResp, payload(task::Payload(req)),
917 dumpPath](const boost::system::error_code& ec,
918 const sdbusplus::message_t& msg,
919 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700920 if (ec)
921 {
922 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500923 const sd_bus_error* dbusError = msg.get_error();
924 if (dbusError == nullptr)
925 {
926 messages::internalError(asyncResp->res);
927 return;
928 }
929
930 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
931 << " and error msg: " << dbusError->message;
932 if (std::string_view(
933 "xyz.openbmc_project.Common.Error.NotAllowed") ==
934 dbusError->name)
935 {
936 messages::resourceInStandby(asyncResp->res);
937 return;
938 }
939 if (std::string_view(
940 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
941 dbusError->name)
942 {
943 messages::serviceDisabled(asyncResp->res, dumpPath);
944 return;
945 }
946 if (std::string_view(
947 "xyz.openbmc_project.Common.Error.Unavailable") ==
948 dbusError->name)
949 {
950 messages::resourceInUse(asyncResp->res);
951 return;
952 }
953 // Other Dbus errors such as:
954 // xyz.openbmc_project.Common.Error.InvalidArgument &
955 // org.freedesktop.DBus.Error.InvalidArgs are all related to
956 // the dbus call that is made here in the bmcweb
957 // implementation and has nothing to do with the client's
958 // input in the request. Hence, returning internal error
959 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -0700960 messages::internalError(asyncResp->res);
961 return;
962 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600963 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
964 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500965 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500966 "xyz.openbmc_project.Dump.Manager",
967 "/xyz/openbmc_project/dump/" +
968 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600969 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500970}
971
zhanghch058d1b46d2021-04-01 11:18:24 +0800972inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
973 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500974{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500975 std::string dumpTypeLowerCopy =
976 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800977
Claire Weinan0d946212022-07-13 19:40:19 -0700978 crow::connections::systemBus->async_method_call(
979 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700980 if (ec)
981 {
Claire Weinan0d946212022-07-13 19:40:19 -0700982 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -0700983 messages::internalError(asyncResp->res);
984 return;
985 }
Claire Weinan0d946212022-07-13 19:40:19 -0700986 },
987 "xyz.openbmc_project.Dump.Manager",
988 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
989 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500990}
991
Ed Tanousb9d36b42022-02-26 21:42:46 -0800992inline static void
993 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
994 std::string& filename, std::string& timestamp,
995 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700996{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200997 const std::string* filenamePtr = nullptr;
998 const std::string* timestampPtr = nullptr;
999 const std::string* logfilePtr = nullptr;
1000
1001 const bool success = sdbusplus::unpackPropertiesNoThrow(
1002 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1003 "Filename", filenamePtr, "Log", logfilePtr);
1004
1005 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001006 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001007 return;
1008 }
1009
1010 if (filenamePtr != nullptr)
1011 {
1012 filename = *filenamePtr;
1013 }
1014
1015 if (timestampPtr != nullptr)
1016 {
1017 timestamp = *timestampPtr;
1018 }
1019
1020 if (logfilePtr != nullptr)
1021 {
1022 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001023 }
1024}
1025
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001026inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001027{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001028 /**
1029 * Functions triggers appropriate requests on DBus
1030 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001031 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001032 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001033 .methods(boost::beast::http::verb::get)(
1034 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001035 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1036 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001037 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001038 {
1039 return;
1040 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001041 if (systemName != "system")
1042 {
1043 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1044 systemName);
1045 return;
1046 }
1047
Ed Tanous002d39b2022-05-31 08:59:27 -07001048 // Collections don't include the static data added by SubRoute
1049 // because it has a duplicate entry for members
1050 asyncResp->res.jsonValue["@odata.type"] =
1051 "#LogServiceCollection.LogServiceCollection";
1052 asyncResp->res.jsonValue["@odata.id"] =
1053 "/redfish/v1/Systems/system/LogServices";
1054 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1055 asyncResp->res.jsonValue["Description"] =
1056 "Collection of LogServices for this Computer System";
1057 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1058 logServiceArray = nlohmann::json::array();
1059 nlohmann::json::object_t eventLog;
1060 eventLog["@odata.id"] =
1061 "/redfish/v1/Systems/system/LogServices/EventLog";
1062 logServiceArray.push_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001063#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001064 nlohmann::json::object_t dumpLog;
1065 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
1066 logServiceArray.push_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001067#endif
1068
Jason M. Billsd53dd412019-02-12 17:16:22 -08001069#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001070 nlohmann::json::object_t crashdump;
1071 crashdump["@odata.id"] =
1072 "/redfish/v1/Systems/system/LogServices/Crashdump";
1073 logServiceArray.push_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001074#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001075
1076#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001077 nlohmann::json::object_t hostlogger;
1078 hostlogger["@odata.id"] =
1079 "/redfish/v1/Systems/system/LogServices/HostLogger";
1080 logServiceArray.push_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001081#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001082 asyncResp->res.jsonValue["Members@odata.count"] =
1083 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001084
George Liu7a1dbc42022-12-07 16:03:22 +08001085 constexpr std::array<std::string_view, 1> interfaces = {
1086 "xyz.openbmc_project.State.Boot.PostCode"};
1087 dbus::utility::getSubTreePaths(
1088 "/", 0, interfaces,
1089 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001090 const dbus::utility::MapperGetSubTreePathsResponse&
1091 subtreePath) {
1092 if (ec)
1093 {
1094 BMCWEB_LOG_ERROR << ec;
1095 return;
1096 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001097
Ed Tanous002d39b2022-05-31 08:59:27 -07001098 for (const auto& pathStr : subtreePath)
1099 {
1100 if (pathStr.find("PostCode") != std::string::npos)
1101 {
1102 nlohmann::json& logServiceArrayLocal =
1103 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001104 nlohmann::json::object_t member;
1105 member["@odata.id"] =
1106 "/redfish/v1/Systems/system/LogServices/PostCodes";
1107
1108 logServiceArrayLocal.push_back(std::move(member));
1109
Ed Tanous002d39b2022-05-31 08:59:27 -07001110 asyncResp->res.jsonValue["Members@odata.count"] =
1111 logServiceArrayLocal.size();
1112 return;
1113 }
1114 }
George Liu7a1dbc42022-12-07 16:03:22 +08001115 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001116 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001117}
1118
1119inline void requestRoutesEventLogService(App& app)
1120{
Ed Tanous22d268c2022-05-19 09:39:07 -07001121 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001122 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 .methods(boost::beast::http::verb::get)(
1124 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001125 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1126 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001127 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001128 {
1129 return;
1130 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001131 if (systemName != "system")
1132 {
1133 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1134 systemName);
1135 return;
1136 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001137 asyncResp->res.jsonValue["@odata.id"] =
1138 "/redfish/v1/Systems/system/LogServices/EventLog";
1139 asyncResp->res.jsonValue["@odata.type"] =
1140 "#LogService.v1_1_0.LogService";
1141 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1142 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1143 asyncResp->res.jsonValue["Id"] = "EventLog";
1144 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301145
Ed Tanous002d39b2022-05-31 08:59:27 -07001146 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001147 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301148
Ed Tanous002d39b2022-05-31 08:59:27 -07001149 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1150 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1151 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301152
Ed Tanous002d39b2022-05-31 08:59:27 -07001153 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1154 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1155 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001156
Ed Tanous002d39b2022-05-31 08:59:27 -07001157 {"target",
1158 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001159 });
1160}
1161
1162inline void requestRoutesJournalEventLogClear(App& app)
1163{
Jason M. Bills4978b632022-02-22 14:17:43 -08001164 BMCWEB_ROUTE(
1165 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001166 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001167 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001168 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001169 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001170 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1171 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001172 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001173 {
1174 return;
1175 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001176 if (systemName != "system")
1177 {
1178 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1179 systemName);
1180 return;
1181 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001182 // Clear the EventLog by deleting the log files
1183 std::vector<std::filesystem::path> redfishLogFiles;
1184 if (getRedfishLogFiles(redfishLogFiles))
1185 {
1186 for (const std::filesystem::path& file : redfishLogFiles)
1187 {
1188 std::error_code ec;
1189 std::filesystem::remove(file, ec);
1190 }
1191 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001192
Ed Tanous002d39b2022-05-31 08:59:27 -07001193 // Reload rsyslog so it knows to start new log files
1194 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001195 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001196 if (ec)
1197 {
1198 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1199 messages::internalError(asyncResp->res);
1200 return;
1201 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001202
Ed Tanous002d39b2022-05-31 08:59:27 -07001203 messages::success(asyncResp->res);
1204 },
1205 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1206 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1207 "replace");
1208 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001209}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001210
Jason M. Billsac992cd2022-06-24 13:31:46 -07001211enum class LogParseError
1212{
1213 success,
1214 parseFailed,
1215 messageIdNotInRegistry,
1216};
1217
1218static LogParseError
1219 fillEventLogEntryJson(const std::string& logEntryID,
1220 const std::string& logEntry,
1221 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001222{
Jason M. Bills95820182019-04-22 16:25:34 -07001223 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001224 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001225 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001226 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001227 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001228 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001229 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001230 std::string timestamp = logEntry.substr(0, space);
1231 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001232 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001233 if (entryStart == std::string::npos)
1234 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001235 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001236 }
1237 std::string_view entry(logEntry);
1238 entry.remove_prefix(entryStart);
1239 // Use split to separate the entry into its fields
1240 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001241 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001242 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001243 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001244 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001245 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001246 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001247 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001248
Jason M. Bills4851d452019-03-28 11:27:48 -07001249 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001250 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001251
Sui Chen54417b02022-03-24 14:59:52 -07001252 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001253 {
Sui Chen54417b02022-03-24 14:59:52 -07001254 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001255 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001256 }
1257
Sui Chen54417b02022-03-24 14:59:52 -07001258 std::string msg = message->message;
1259
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001260 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001261 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001262 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001263 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001264 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001265 // If the first string is empty, assume there are no MessageArgs
1266 std::size_t messageArgsSize = 0;
1267 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001268 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001269 messageArgsSize = logEntryFields.size() - 1;
1270 }
1271
Ed Tanous23a21a12020-07-25 04:45:05 +00001272 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001273
1274 // Fill the MessageArgs into the Message
1275 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001276 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001277 {
1278 std::string argStr = "%" + std::to_string(++i);
1279 size_t argPos = msg.find(argStr);
1280 if (argPos != std::string::npos)
1281 {
1282 msg.replace(argPos, argStr.length(), messageArg);
1283 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001284 }
1285 }
1286
Jason M. Bills95820182019-04-22 16:25:34 -07001287 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1288 // format which matches the Redfish format except for the fractional seconds
1289 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001290 std::size_t dot = timestamp.find_first_of('.');
1291 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001292 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001293 {
Jason M. Bills95820182019-04-22 16:25:34 -07001294 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001295 }
1296
1297 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001298 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001299 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
1300 "redfish", "v1", "Systems", "system", "LogServices", "EventLog",
1301 "Entries", logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001302 logEntryJson["Name"] = "System Event Log Entry";
1303 logEntryJson["Id"] = logEntryID;
1304 logEntryJson["Message"] = std::move(msg);
1305 logEntryJson["MessageId"] = std::move(messageID);
1306 logEntryJson["MessageArgs"] = messageArgs;
1307 logEntryJson["EntryType"] = "Event";
1308 logEntryJson["Severity"] = message->messageSeverity;
1309 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001310 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001311}
1312
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001313inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001314{
Ed Tanous22d268c2022-05-19 09:39:07 -07001315 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001316 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001317 .methods(boost::beast::http::verb::get)(
1318 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001319 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1320 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001321 query_param::QueryCapabilities capabilities = {
1322 .canDelegateTop = true,
1323 .canDelegateSkip = true,
1324 };
1325 query_param::Query delegatedQuery;
1326 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001327 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001328 {
1329 return;
1330 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001331 if (systemName != "system")
1332 {
1333 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1334 systemName);
1335 return;
1336 }
1337
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001338 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001339 size_t skip = delegatedQuery.skip.value_or(0);
1340
Ed Tanous002d39b2022-05-31 08:59:27 -07001341 // Collections don't include the static data added by SubRoute
1342 // because it has a duplicate entry for members
1343 asyncResp->res.jsonValue["@odata.type"] =
1344 "#LogEntryCollection.LogEntryCollection";
1345 asyncResp->res.jsonValue["@odata.id"] =
1346 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1347 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1348 asyncResp->res.jsonValue["Description"] =
1349 "Collection of System Event Log Entries";
1350
1351 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1352 logEntryArray = nlohmann::json::array();
1353 // Go through the log files and create a unique ID for each
1354 // entry
1355 std::vector<std::filesystem::path> redfishLogFiles;
1356 getRedfishLogFiles(redfishLogFiles);
1357 uint64_t entryCount = 0;
1358 std::string logEntry;
1359
1360 // Oldest logs are in the last file, so start there and loop
1361 // backwards
1362 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1363 it++)
1364 {
1365 std::ifstream logStream(*it);
1366 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001367 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001368 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001369 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001370
Ed Tanous002d39b2022-05-31 08:59:27 -07001371 // Reset the unique ID on the first entry
1372 bool firstEntry = true;
1373 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001374 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001375 std::string idStr;
1376 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001377 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001378 continue;
1379 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001380 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001381
Jason M. Billsde703c52022-06-23 14:19:04 -07001382 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001383 LogParseError status =
1384 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1385 if (status == LogParseError::messageIdNotInRegistry)
1386 {
1387 continue;
1388 }
1389 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001390 {
1391 messages::internalError(asyncResp->res);
1392 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001393 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001394
Jason M. Billsde703c52022-06-23 14:19:04 -07001395 entryCount++;
1396 // Handle paging using skip (number of entries to skip from the
1397 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001398 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001399 {
1400 continue;
1401 }
1402
1403 logEntryArray.push_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001404 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001405 }
1406 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001407 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001408 {
1409 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1410 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001411 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001413 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001414}
Chicago Duan336e96c2019-07-15 14:22:08 +08001415
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001416inline void requestRoutesJournalEventLogEntry(App& app)
1417{
1418 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001419 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001420 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001421 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001422 [&app](const crow::Request& req,
1423 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001424 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001425 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001426 {
1427 return;
1428 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001429
1430 if (systemName != "system")
1431 {
1432 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1433 systemName);
1434 return;
1435 }
1436
Ed Tanous002d39b2022-05-31 08:59:27 -07001437 const std::string& targetID = param;
1438
1439 // Go through the log files and check the unique ID for each
1440 // entry to find the target entry
1441 std::vector<std::filesystem::path> redfishLogFiles;
1442 getRedfishLogFiles(redfishLogFiles);
1443 std::string logEntry;
1444
1445 // Oldest logs are in the last file, so start there and loop
1446 // backwards
1447 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1448 it++)
1449 {
1450 std::ifstream logStream(*it);
1451 if (!logStream.is_open())
1452 {
1453 continue;
1454 }
1455
1456 // Reset the unique ID on the first entry
1457 bool firstEntry = true;
1458 while (std::getline(logStream, logEntry))
1459 {
1460 std::string idStr;
1461 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001462 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001463 continue;
1464 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001465 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001466
1467 if (idStr == targetID)
1468 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001469 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001470 LogParseError status =
1471 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1472 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001473 {
1474 messages::internalError(asyncResp->res);
1475 return;
1476 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001477 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001478 return;
1479 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001480 }
1481 }
1482 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001483 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001484 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001485}
1486
1487inline void requestRoutesDBusEventLogEntryCollection(App& app)
1488{
Ed Tanous22d268c2022-05-19 09:39:07 -07001489 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001490 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001491 .methods(boost::beast::http::verb::get)(
1492 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001493 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1494 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001495 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001496 {
1497 return;
1498 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001499 if (systemName != "system")
1500 {
1501 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1502 systemName);
1503 return;
1504 }
1505
Ed Tanous002d39b2022-05-31 08:59:27 -07001506 // Collections don't include the static data added by SubRoute
1507 // because it has a duplicate entry for members
1508 asyncResp->res.jsonValue["@odata.type"] =
1509 "#LogEntryCollection.LogEntryCollection";
1510 asyncResp->res.jsonValue["@odata.id"] =
1511 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1512 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1513 asyncResp->res.jsonValue["Description"] =
1514 "Collection of System Event Log Entries";
1515
1516 // DBus implementation of EventLog/Entries
1517 // Make call to Logging Service to find all log entry objects
1518 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001519 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001520 const dbus::utility::ManagedObjectType& resp) {
1521 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001522 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001523 // TODO Handle for specific error code
1524 BMCWEB_LOG_ERROR
1525 << "getLogEntriesIfaceData resp_handler got error " << ec;
1526 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001527 return;
1528 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001529 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1530 entriesArray = nlohmann::json::array();
1531 for (const auto& objectPath : resp)
1532 {
1533 const uint32_t* id = nullptr;
1534 const uint64_t* timestamp = nullptr;
1535 const uint64_t* updateTimestamp = nullptr;
1536 const std::string* severity = nullptr;
1537 const std::string* message = nullptr;
1538 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001539 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001540 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001541 const std::string* notify = nullptr;
1542
Ed Tanous002d39b2022-05-31 08:59:27 -07001543 for (const auto& interfaceMap : objectPath.second)
1544 {
1545 if (interfaceMap.first ==
1546 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001547 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001548 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001549 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001550 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001551 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001552 id = std::get_if<uint32_t>(&propertyMap.second);
1553 }
1554 else if (propertyMap.first == "Timestamp")
1555 {
1556 timestamp =
1557 std::get_if<uint64_t>(&propertyMap.second);
1558 }
1559 else if (propertyMap.first == "UpdateTimestamp")
1560 {
1561 updateTimestamp =
1562 std::get_if<uint64_t>(&propertyMap.second);
1563 }
1564 else if (propertyMap.first == "Severity")
1565 {
1566 severity = std::get_if<std::string>(
1567 &propertyMap.second);
1568 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001569 else if (propertyMap.first == "Resolution")
1570 {
1571 resolution = std::get_if<std::string>(
1572 &propertyMap.second);
1573 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001574 else if (propertyMap.first == "Message")
1575 {
1576 message = std::get_if<std::string>(
1577 &propertyMap.second);
1578 }
1579 else if (propertyMap.first == "Resolved")
1580 {
1581 const bool* resolveptr =
1582 std::get_if<bool>(&propertyMap.second);
1583 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001584 {
1585 messages::internalError(asyncResp->res);
1586 return;
1587 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001588 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001589 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001590 else if (propertyMap.first ==
1591 "ServiceProviderNotify")
1592 {
1593 notify = std::get_if<std::string>(
1594 &propertyMap.second);
1595 if (notify == nullptr)
1596 {
1597 messages::internalError(asyncResp->res);
1598 return;
1599 }
1600 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001601 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001602 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001603 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001604 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001605 messages::internalError(asyncResp->res);
1606 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001607 }
1608 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001609 else if (interfaceMap.first ==
1610 "xyz.openbmc_project.Common.FilePath")
1611 {
1612 for (const auto& propertyMap : interfaceMap.second)
1613 {
1614 if (propertyMap.first == "Path")
1615 {
1616 filePath = std::get_if<std::string>(
1617 &propertyMap.second);
1618 }
1619 }
1620 }
1621 }
1622 // Object path without the
1623 // xyz.openbmc_project.Logging.Entry interface, ignore
1624 // and continue.
1625 if (id == nullptr || message == nullptr ||
1626 severity == nullptr || timestamp == nullptr ||
1627 updateTimestamp == nullptr)
1628 {
1629 continue;
1630 }
1631 entriesArray.push_back({});
1632 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001633 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001634 thisEntry["@odata.id"] = crow::utility::urlFromPieces(
1635 "redfish", "v1", "Systems", "system", "LogServices",
1636 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001637 thisEntry["Name"] = "System Event Log Entry";
1638 thisEntry["Id"] = std::to_string(*id);
1639 thisEntry["Message"] = *message;
1640 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001641 if ((resolution != nullptr) && (!(*resolution).empty()))
1642 {
1643 thisEntry["Resolution"] = *resolution;
1644 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001645 std::optional<bool> notifyAction =
1646 getProviderNotifyAction(*notify);
1647 if (notifyAction)
1648 {
1649 thisEntry["ServiceProviderNotified"] = *notifyAction;
1650 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001651 thisEntry["EntryType"] = "Event";
1652 thisEntry["Severity"] =
1653 translateSeverityDbusToRedfish(*severity);
1654 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001655 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001656 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001657 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001658 if (filePath != nullptr)
1659 {
1660 thisEntry["AdditionalDataURI"] =
1661 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1662 std::to_string(*id) + "/attachment";
1663 }
1664 }
1665 std::sort(
1666 entriesArray.begin(), entriesArray.end(),
1667 [](const nlohmann::json& left, const nlohmann::json& right) {
1668 return (left["Id"] <= right["Id"]);
1669 });
1670 asyncResp->res.jsonValue["Members@odata.count"] =
1671 entriesArray.size();
1672 },
1673 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1674 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001675 });
1676}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001677
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001678inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001679{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001680 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001681 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001682 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001683 .methods(boost::beast::http::verb::get)(
1684 [&app](const crow::Request& req,
1685 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001686 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001687 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001688 {
1689 return;
1690 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001691 if (systemName != "system")
1692 {
1693 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1694 systemName);
1695 return;
1696 }
1697
Ed Tanous002d39b2022-05-31 08:59:27 -07001698 std::string entryID = param;
1699 dbus::utility::escapePathForDbus(entryID);
1700
1701 // DBus implementation of EventLog/Entries
1702 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001703 sdbusplus::asio::getAllProperties(
1704 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1705 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001706 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001707 const dbus::utility::DBusPropertiesMap& resp) {
1708 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001709 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001710 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1711 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001712 return;
1713 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001714 if (ec)
1715 {
1716 BMCWEB_LOG_ERROR
1717 << "EventLogEntry (DBus) resp_handler got error " << ec;
1718 messages::internalError(asyncResp->res);
1719 return;
1720 }
1721 const uint32_t* id = nullptr;
1722 const uint64_t* timestamp = nullptr;
1723 const uint64_t* updateTimestamp = nullptr;
1724 const std::string* severity = nullptr;
1725 const std::string* message = nullptr;
1726 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001727 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001728 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001729 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001730
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001731 const bool success = sdbusplus::unpackPropertiesNoThrow(
1732 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1733 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001734 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001735 "Resolution", resolution, "Path", filePath,
1736 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001737
1738 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001739 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001740 messages::internalError(asyncResp->res);
1741 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001742 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001743
Ed Tanous002d39b2022-05-31 08:59:27 -07001744 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001745 timestamp == nullptr || updateTimestamp == nullptr ||
1746 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001747 {
1748 messages::internalError(asyncResp->res);
1749 return;
1750 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001751
Ed Tanous002d39b2022-05-31 08:59:27 -07001752 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001753 "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001754 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00001755 crow::utility::urlFromPieces(
1756 "redfish", "v1", "Systems", "system", "LogServices",
1757 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001758 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1759 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1760 asyncResp->res.jsonValue["Message"] = *message;
1761 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001762 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1763 if (notifyAction)
1764 {
1765 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1766 *notifyAction;
1767 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001768 if ((resolution != nullptr) && (!(*resolution).empty()))
1769 {
1770 asyncResp->res.jsonValue["Resolution"] = *resolution;
1771 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001772 asyncResp->res.jsonValue["EntryType"] = "Event";
1773 asyncResp->res.jsonValue["Severity"] =
1774 translateSeverityDbusToRedfish(*severity);
1775 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001776 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001777 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001778 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001779 if (filePath != nullptr)
1780 {
1781 asyncResp->res.jsonValue["AdditionalDataURI"] =
1782 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1783 std::to_string(*id) + "/attachment";
1784 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001785 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001786 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001787
1788 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001789 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001790 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001791 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001792 [&app](const crow::Request& req,
1793 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001794 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001795 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 {
1797 return;
1798 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001799 if (systemName != "system")
1800 {
1801 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1802 systemName);
1803 return;
1804 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001806
Ed Tanous002d39b2022-05-31 08:59:27 -07001807 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1808 resolved))
1809 {
1810 return;
1811 }
1812 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001813
Ed Tanous002d39b2022-05-31 08:59:27 -07001814 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001815 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001816 if (ec)
1817 {
1818 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1819 messages::internalError(asyncResp->res);
1820 return;
1821 }
1822 },
1823 "xyz.openbmc_project.Logging",
1824 "/xyz/openbmc_project/logging/entry/" + entryId,
1825 "org.freedesktop.DBus.Properties", "Set",
1826 "xyz.openbmc_project.Logging.Entry", "Resolved",
1827 dbus::utility::DbusVariantType(*resolved));
1828 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001829
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001830 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001831 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001832 .privileges(redfish::privileges::deleteLogEntry)
1833
Ed Tanous002d39b2022-05-31 08:59:27 -07001834 .methods(boost::beast::http::verb::delete_)(
1835 [&app](const crow::Request& req,
1836 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001837 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001838 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001839 {
1840 return;
1841 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001842 if (systemName != "system")
1843 {
1844 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1845 systemName);
1846 return;
1847 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001848 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1849
1850 std::string entryID = param;
1851
1852 dbus::utility::escapePathForDbus(entryID);
1853
1854 // Process response from Logging service.
1855 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001856 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001857 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1858 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001859 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001860 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001861 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001862 messages::resourceNotFound(asyncResp->res, "LogEntry",
1863 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001864 return;
1865 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001866 // TODO Handle for specific error code
1867 BMCWEB_LOG_ERROR
1868 << "EventLogEntry (DBus) doDelete respHandler got error "
1869 << ec;
1870 asyncResp->res.result(
1871 boost::beast::http::status::internal_server_error);
1872 return;
1873 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001874
Ed Tanous002d39b2022-05-31 08:59:27 -07001875 asyncResp->res.result(boost::beast::http::status::ok);
1876 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001877
Ed Tanous002d39b2022-05-31 08:59:27 -07001878 // Make call to Logging service to request Delete Log
1879 crow::connections::systemBus->async_method_call(
1880 respHandler, "xyz.openbmc_project.Logging",
1881 "/xyz/openbmc_project/logging/entry/" + entryID,
1882 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001883 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001884}
1885
1886inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001887{
George Liu0fda0f12021-11-16 10:06:17 +08001888 BMCWEB_ROUTE(
1889 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001890 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001891 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001892 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001893 [&app](const crow::Request& req,
1894 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001895 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001896 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001897 {
1898 return;
1899 }
Ed Tanous99351cd2022-08-07 16:42:51 -07001900 if (http_helpers::isContentTypeAllowed(
1901 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001902 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 {
1904 asyncResp->res.result(boost::beast::http::status::bad_request);
1905 return;
1906 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001907 if (systemName != "system")
1908 {
1909 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1910 systemName);
1911 return;
1912 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001913
Ed Tanous002d39b2022-05-31 08:59:27 -07001914 std::string entryID = param;
1915 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001916
Ed Tanous002d39b2022-05-31 08:59:27 -07001917 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001918 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001919 const sdbusplus::message::unix_fd& unixfd) {
1920 if (ec.value() == EBADR)
1921 {
1922 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1923 entryID);
1924 return;
1925 }
1926 if (ec)
1927 {
1928 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1929 messages::internalError(asyncResp->res);
1930 return;
1931 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001932
Ed Tanous002d39b2022-05-31 08:59:27 -07001933 int fd = -1;
1934 fd = dup(unixfd);
1935 if (fd == -1)
1936 {
1937 messages::internalError(asyncResp->res);
1938 return;
1939 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001940
Ed Tanous002d39b2022-05-31 08:59:27 -07001941 long long int size = lseek(fd, 0, SEEK_END);
1942 if (size == -1)
1943 {
1944 messages::internalError(asyncResp->res);
1945 return;
1946 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001947
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 // Arbitrary max size of 64kb
1949 constexpr int maxFileSize = 65536;
1950 if (size > maxFileSize)
1951 {
1952 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1953 << maxFileSize;
1954 messages::internalError(asyncResp->res);
1955 return;
1956 }
1957 std::vector<char> data(static_cast<size_t>(size));
1958 long long int rc = lseek(fd, 0, SEEK_SET);
1959 if (rc == -1)
1960 {
1961 messages::internalError(asyncResp->res);
1962 return;
1963 }
1964 rc = read(fd, data.data(), data.size());
1965 if ((rc == -1) || (rc != size))
1966 {
1967 messages::internalError(asyncResp->res);
1968 return;
1969 }
1970 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001971
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 std::string_view strData(data.data(), data.size());
1973 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001974
Ed Tanousd9f6c622022-03-17 09:12:17 -07001975 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07001977 asyncResp->res.addHeader(
1978 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07001979 asyncResp->res.body() = std::move(output);
1980 },
1981 "xyz.openbmc_project.Logging",
1982 "/xyz/openbmc_project/logging/entry/" + entryID,
1983 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1984 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001985}
1986
Spencer Kub7028eb2021-10-26 15:27:35 +08001987constexpr const char* hostLoggerFolderPath = "/var/log/console";
1988
1989inline bool
1990 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1991 std::vector<std::filesystem::path>& hostLoggerFiles)
1992{
1993 std::error_code ec;
1994 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1995 if (ec)
1996 {
1997 BMCWEB_LOG_ERROR << ec.message();
1998 return false;
1999 }
2000 for (const std::filesystem::directory_entry& it : logPath)
2001 {
2002 std::string filename = it.path().filename();
2003 // Prefix of each log files is "log". Find the file and save the
2004 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002005 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002006 {
2007 hostLoggerFiles.emplace_back(it.path());
2008 }
2009 }
2010 // As the log files rotate, they are appended with a ".#" that is higher for
2011 // the older logs. Since we start from oldest logs, sort the name in
2012 // descending order.
2013 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2014 AlphanumLess<std::string>());
2015
2016 return true;
2017}
2018
Ed Tanous02cad962022-06-30 16:50:15 -07002019inline bool getHostLoggerEntries(
2020 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2021 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002022{
2023 GzFileReader logFile;
2024
2025 // Go though all log files and expose host logs.
2026 for (const std::filesystem::path& it : hostLoggerFiles)
2027 {
2028 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2029 {
2030 BMCWEB_LOG_ERROR << "fail to expose host logs";
2031 return false;
2032 }
2033 }
2034 // Get lastMessage from constructor by getter
2035 std::string lastMessage = logFile.getLastMessage();
2036 if (!lastMessage.empty())
2037 {
2038 logCount++;
2039 if (logCount > skip && logCount <= (skip + top))
2040 {
2041 logEntries.push_back(lastMessage);
2042 }
2043 }
2044 return true;
2045}
2046
2047inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2048 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002049 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002050{
2051 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002052 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002053 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2054 "redfish", "v1", "Systems", "system", "LogServices", "HostLogger",
2055 "Entries", logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002056 logEntryJson["Name"] = "Host Logger Entry";
2057 logEntryJson["Id"] = logEntryID;
2058 logEntryJson["Message"] = msg;
2059 logEntryJson["EntryType"] = "Oem";
2060 logEntryJson["Severity"] = "OK";
2061 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002062}
2063
2064inline void requestRoutesSystemHostLogger(App& app)
2065{
Ed Tanous22d268c2022-05-19 09:39:07 -07002066 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002067 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002068 .methods(boost::beast::http::verb::get)(
2069 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002070 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2071 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002072 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002073 {
2074 return;
2075 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002076 if (systemName != "system")
2077 {
2078 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2079 systemName);
2080 return;
2081 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002082 asyncResp->res.jsonValue["@odata.id"] =
2083 "/redfish/v1/Systems/system/LogServices/HostLogger";
2084 asyncResp->res.jsonValue["@odata.type"] =
2085 "#LogService.v1_1_0.LogService";
2086 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2087 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2088 asyncResp->res.jsonValue["Id"] = "HostLogger";
2089 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2090 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2091 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002092}
2093
2094inline void requestRoutesSystemHostLoggerCollection(App& app)
2095{
2096 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002097 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002098 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002099 .methods(boost::beast::http::verb::get)(
2100 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002101 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2102 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002103 query_param::QueryCapabilities capabilities = {
2104 .canDelegateTop = true,
2105 .canDelegateSkip = true,
2106 };
2107 query_param::Query delegatedQuery;
2108 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002109 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002110 {
2111 return;
2112 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002113 if (systemName != "system")
2114 {
2115 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2116 systemName);
2117 return;
2118 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002119 asyncResp->res.jsonValue["@odata.id"] =
2120 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2121 asyncResp->res.jsonValue["@odata.type"] =
2122 "#LogEntryCollection.LogEntryCollection";
2123 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2124 asyncResp->res.jsonValue["Description"] =
2125 "Collection of HostLogger Entries";
2126 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2127 logEntryArray = nlohmann::json::array();
2128 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002129
Ed Tanous002d39b2022-05-31 08:59:27 -07002130 std::vector<std::filesystem::path> hostLoggerFiles;
2131 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2132 {
2133 BMCWEB_LOG_ERROR << "fail to get host log file path";
2134 return;
2135 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002136 // If we weren't provided top and skip limits, use the defaults.
2137 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002138 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002139 size_t logCount = 0;
2140 // This vector only store the entries we want to expose that
2141 // control by skip and top.
2142 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002143 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2144 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002145 {
2146 messages::internalError(asyncResp->res);
2147 return;
2148 }
2149 // If vector is empty, that means skip value larger than total
2150 // log count
2151 if (logEntries.empty())
2152 {
2153 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2154 return;
2155 }
2156 if (!logEntries.empty())
2157 {
2158 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002159 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002160 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002161 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2162 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002163 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002164 }
2165
Ed Tanous002d39b2022-05-31 08:59:27 -07002166 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002167 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002168 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002169 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2170 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002171 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002172 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 }
George Liu0fda0f12021-11-16 10:06:17 +08002174 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002175}
2176
2177inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2178{
2179 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002180 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002181 .privileges(redfish::privileges::getLogEntry)
2182 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002183 [&app](const crow::Request& req,
2184 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002185 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002186 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002187 {
2188 return;
2189 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002190 if (systemName != "system")
2191 {
2192 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2193 systemName);
2194 return;
2195 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002196 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002197
Ed Tanous002d39b2022-05-31 08:59:27 -07002198 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002199
Ed Tanous002d39b2022-05-31 08:59:27 -07002200 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2201 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08002202
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002204 if (ec == std::errc::invalid_argument ||
2205 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002206 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002207 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002208 return;
2209 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002210
Ed Tanous002d39b2022-05-31 08:59:27 -07002211 std::vector<std::filesystem::path> hostLoggerFiles;
2212 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2213 {
2214 BMCWEB_LOG_ERROR << "fail to get host log file path";
2215 return;
2216 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002217
Ed Tanous002d39b2022-05-31 08:59:27 -07002218 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002219 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002220 std::vector<std::string> logEntries;
2221 // We can get specific entry by skip and top. For example, if we
2222 // want to get nth entry, we can set skip = n-1 and top = 1 to
2223 // get that entry
2224 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2225 logCount))
2226 {
2227 messages::internalError(asyncResp->res);
2228 return;
2229 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002230
Ed Tanous002d39b2022-05-31 08:59:27 -07002231 if (!logEntries.empty())
2232 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002233 nlohmann::json::object_t hostLogEntry;
2234 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2235 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002236 return;
2237 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002238
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002240 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002242}
2243
Claire Weinandd72e872022-08-15 14:20:06 -07002244inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002245 crow::App& app, const crow::Request& req,
2246 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2247{
2248 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2249 {
2250 return;
2251 }
2252 // Collections don't include the static data added by SubRoute
2253 // because it has a duplicate entry for members
2254 asyncResp->res.jsonValue["@odata.type"] =
2255 "#LogServiceCollection.LogServiceCollection";
2256 asyncResp->res.jsonValue["@odata.id"] =
2257 "/redfish/v1/Managers/bmc/LogServices";
2258 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2259 asyncResp->res.jsonValue["Description"] =
2260 "Collection of LogServices for this Manager";
2261 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2262 logServiceArray = nlohmann::json::array();
2263
2264#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002265 nlohmann::json::object_t journal;
2266 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2267 logServiceArray.push_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002268#endif
2269
2270 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2271
2272#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002273 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002274 "xyz.openbmc_project.Collection.DeleteAll"};
2275 dbus::utility::getSubTreePaths(
2276 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002277 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002278 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002279 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2280 if (ec)
2281 {
2282 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002283 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002284 << ec;
2285 // Assume that getting an error simply means there are no dump
2286 // LogServices. Return without adding any error response.
2287 return;
2288 }
2289
2290 nlohmann::json& logServiceArrayLocal =
2291 asyncResp->res.jsonValue["Members"];
2292
2293 for (const std::string& path : subTreePaths)
2294 {
2295 if (path == "/xyz/openbmc_project/dump/bmc")
2296 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002297 nlohmann::json::object_t member;
2298 member["@odata.id"] =
2299 "/redfish/v1/Managers/bmc/LogServices/Dump";
2300 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002301 }
2302 else if (path == "/xyz/openbmc_project/dump/faultlog")
2303 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002304 nlohmann::json::object_t member;
2305 member["@odata.id"] =
2306 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2307 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002308 }
2309 }
2310
2311 asyncResp->res.jsonValue["Members@odata.count"] =
2312 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002313 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002314#endif
2315}
2316
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002317inline void requestRoutesBMCLogServiceCollection(App& app)
2318{
2319 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002320 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002321 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002322 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002323}
Ed Tanous1da66f72018-07-27 16:13:37 -07002324
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002325inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002326{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002327 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002328 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002329 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002330 [&app](const crow::Request& req,
2331 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002332 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002333 {
2334 return;
2335 }
2336 asyncResp->res.jsonValue["@odata.type"] =
2337 "#LogService.v1_1_0.LogService";
2338 asyncResp->res.jsonValue["@odata.id"] =
2339 "/redfish/v1/Managers/bmc/LogServices/Journal";
2340 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2341 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002342 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002343 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302344
Ed Tanous002d39b2022-05-31 08:59:27 -07002345 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002346 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002347 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2348 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2349 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302350
Ed Tanous002d39b2022-05-31 08:59:27 -07002351 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2352 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2353 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002354}
Jason M. Billse1f26342018-07-18 12:12:00 -07002355
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002356static int
2357 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2358 sd_journal* journal,
2359 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002360{
2361 // Get the Log Entry contents
2362 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002363
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002364 std::string message;
2365 std::string_view syslogID;
2366 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2367 if (ret < 0)
2368 {
2369 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2370 << strerror(-ret);
2371 }
2372 if (!syslogID.empty())
2373 {
2374 message += std::string(syslogID) + ": ";
2375 }
2376
Ed Tanous39e77502019-03-04 17:35:53 -08002377 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002378 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002379 if (ret < 0)
2380 {
2381 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2382 return 1;
2383 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002384 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002385
2386 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002387 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002388 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002389 if (ret < 0)
2390 {
2391 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002392 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002393
2394 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002395 std::string entryTimeStr;
2396 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002397 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002398 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002399 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002400
2401 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002402 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002403 bmcJournalLogEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2404 "redfish", "v1", "Managers", "bmc", "LogServices", "Journal", "Entries",
2405 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002406 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2407 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2408 bmcJournalLogEntryJson["Message"] = std::move(message);
2409 bmcJournalLogEntryJson["EntryType"] = "Oem";
2410 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2411 : severity <= 4 ? "Warning"
2412 : "OK";
2413 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2414 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002415 return 0;
2416}
2417
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002418inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002419{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002420 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002421 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002422 .methods(boost::beast::http::verb::get)(
2423 [&app](const crow::Request& req,
2424 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2425 query_param::QueryCapabilities capabilities = {
2426 .canDelegateTop = true,
2427 .canDelegateSkip = true,
2428 };
2429 query_param::Query delegatedQuery;
2430 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002431 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002432 {
2433 return;
2434 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002435
2436 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002437 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002438
Ed Tanous002d39b2022-05-31 08:59:27 -07002439 // Collections don't include the static data added by SubRoute
2440 // because it has a duplicate entry for members
2441 asyncResp->res.jsonValue["@odata.type"] =
2442 "#LogEntryCollection.LogEntryCollection";
2443 asyncResp->res.jsonValue["@odata.id"] =
2444 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2445 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2446 asyncResp->res.jsonValue["Description"] =
2447 "Collection of BMC Journal Entries";
2448 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2449 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002450
Ed Tanous002d39b2022-05-31 08:59:27 -07002451 // Go through the journal and use the timestamp to create a
2452 // unique ID for each entry
2453 sd_journal* journalTmp = nullptr;
2454 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2455 if (ret < 0)
2456 {
2457 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2458 messages::internalError(asyncResp->res);
2459 return;
2460 }
2461 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2462 journalTmp, sd_journal_close);
2463 journalTmp = nullptr;
2464 uint64_t entryCount = 0;
2465 // Reset the unique ID on the first entry
2466 bool firstEntry = true;
2467 SD_JOURNAL_FOREACH(journal.get())
2468 {
2469 entryCount++;
2470 // Handle paging using skip (number of entries to skip from
2471 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002472 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002473 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002474 continue;
2475 }
2476
2477 std::string idStr;
2478 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2479 {
2480 continue;
2481 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002482 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002483
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002484 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002485 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2486 bmcJournalLogEntry) != 0)
2487 {
George Liu0fda0f12021-11-16 10:06:17 +08002488 messages::internalError(asyncResp->res);
2489 return;
2490 }
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002491 logEntryArray.push_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002492 }
2493 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002494 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002495 {
2496 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2497 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002498 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002499 }
George Liu0fda0f12021-11-16 10:06:17 +08002500 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002501}
Jason M. Billse1f26342018-07-18 12:12:00 -07002502
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002503inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002504{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002505 BMCWEB_ROUTE(app,
2506 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002507 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002508 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002509 [&app](const crow::Request& req,
2510 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2511 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002512 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002513 {
2514 return;
2515 }
2516 // Convert the unique ID back to a timestamp to find the entry
2517 uint64_t ts = 0;
2518 uint64_t index = 0;
2519 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2520 {
2521 return;
2522 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002523
Ed Tanous002d39b2022-05-31 08:59:27 -07002524 sd_journal* journalTmp = nullptr;
2525 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2526 if (ret < 0)
2527 {
2528 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2529 messages::internalError(asyncResp->res);
2530 return;
2531 }
2532 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2533 journalTmp, sd_journal_close);
2534 journalTmp = nullptr;
2535 // Go to the timestamp in the log and move to the entry at the
2536 // index tracking the unique ID
2537 std::string idStr;
2538 bool firstEntry = true;
2539 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2540 if (ret < 0)
2541 {
2542 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2543 << strerror(-ret);
2544 messages::internalError(asyncResp->res);
2545 return;
2546 }
2547 for (uint64_t i = 0; i <= index; i++)
2548 {
2549 sd_journal_next(journal.get());
2550 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2551 {
2552 messages::internalError(asyncResp->res);
2553 return;
2554 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002555 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002556 }
2557 // Confirm that the entry ID matches what was requested
2558 if (idStr != entryID)
2559 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002560 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002561 return;
2562 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002563
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002564 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002565 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002566 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002567 {
2568 messages::internalError(asyncResp->res);
2569 return;
2570 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002571 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002572 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002573}
2574
Claire Weinanfdd26902022-03-01 14:18:25 -08002575inline void
2576 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2577 const std::string& dumpType)
2578{
2579 std::string dumpPath;
2580 std::string overWritePolicy;
2581 bool collectDiagnosticDataSupported = false;
2582
2583 if (dumpType == "BMC")
2584 {
2585 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2586 overWritePolicy = "WrapsWhenFull";
2587 collectDiagnosticDataSupported = true;
2588 }
2589 else if (dumpType == "FaultLog")
2590 {
2591 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2592 overWritePolicy = "Unknown";
2593 collectDiagnosticDataSupported = false;
2594 }
2595 else if (dumpType == "System")
2596 {
2597 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2598 overWritePolicy = "WrapsWhenFull";
2599 collectDiagnosticDataSupported = true;
2600 }
2601 else
2602 {
2603 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2604 << dumpType;
2605 messages::internalError(asyncResp->res);
2606 return;
2607 }
2608
2609 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2610 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2611 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2612 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2613 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2614 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2615
2616 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002617 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002618 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2619 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2620 redfishDateTimeOffset.second;
2621
2622 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002623
2624 if (collectDiagnosticDataSupported)
2625 {
2626 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2627 ["target"] =
2628 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2629 }
Claire Weinan0d946212022-07-13 19:40:19 -07002630
2631 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2632 dbus::utility::getSubTreePaths(
2633 "/xyz/openbmc_project/dump", 0, interfaces,
2634 [asyncResp, dumpType, dumpPath](
2635 const boost::system::error_code& ec,
2636 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2637 if (ec)
2638 {
2639 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2640 << ec;
2641 // Assume that getting an error simply means there are no dump
2642 // LogServices. Return without adding any error response.
2643 return;
2644 }
2645
2646 const std::string dbusDumpPath =
2647 "/xyz/openbmc_project/dump/" +
2648 boost::algorithm::to_lower_copy(dumpType);
2649
2650 for (const std::string& path : subTreePaths)
2651 {
2652 if (path == dbusDumpPath)
2653 {
2654 asyncResp->res
2655 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2656 dumpPath + "/Actions/LogService.ClearLog";
2657 break;
2658 }
2659 }
2660 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002661}
2662
2663inline void handleLogServicesDumpServiceGet(
2664 crow::App& app, const std::string& dumpType, const crow::Request& req,
2665 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2666{
2667 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2668 {
2669 return;
2670 }
2671 getDumpServiceInfo(asyncResp, dumpType);
2672}
2673
Ed Tanous22d268c2022-05-19 09:39:07 -07002674inline void handleLogServicesDumpServiceComputerSystemGet(
2675 crow::App& app, const crow::Request& req,
2676 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2677 const std::string& chassisId)
2678{
2679 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2680 {
2681 return;
2682 }
2683 if (chassisId != "system")
2684 {
2685 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2686 return;
2687 }
2688 getDumpServiceInfo(asyncResp, "System");
2689}
2690
Claire Weinanfdd26902022-03-01 14:18:25 -08002691inline void handleLogServicesDumpEntriesCollectionGet(
2692 crow::App& app, const std::string& dumpType, const crow::Request& req,
2693 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2694{
2695 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2696 {
2697 return;
2698 }
2699 getDumpEntryCollection(asyncResp, dumpType);
2700}
2701
Ed Tanous22d268c2022-05-19 09:39:07 -07002702inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2703 crow::App& app, const crow::Request& req,
2704 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2705 const std::string& chassisId)
2706{
2707 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2708 {
2709 return;
2710 }
2711 if (chassisId != "system")
2712 {
2713 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2714 return;
2715 }
2716 getDumpEntryCollection(asyncResp, "System");
2717}
2718
Claire Weinanfdd26902022-03-01 14:18:25 -08002719inline void handleLogServicesDumpEntryGet(
2720 crow::App& app, const std::string& dumpType, const crow::Request& req,
2721 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2722 const std::string& dumpId)
2723{
2724 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2725 {
2726 return;
2727 }
2728 getDumpEntryById(asyncResp, dumpId, dumpType);
2729}
Ed Tanous22d268c2022-05-19 09:39:07 -07002730inline void handleLogServicesDumpEntryComputerSystemGet(
2731 crow::App& app, const crow::Request& req,
2732 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2733 const std::string& chassisId, const std::string& dumpId)
2734{
2735 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2736 {
2737 return;
2738 }
2739 if (chassisId != "system")
2740 {
2741 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2742 return;
2743 }
2744 getDumpEntryById(asyncResp, dumpId, "System");
2745}
Claire Weinanfdd26902022-03-01 14:18:25 -08002746
2747inline void handleLogServicesDumpEntryDelete(
2748 crow::App& app, const std::string& dumpType, const crow::Request& req,
2749 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2750 const std::string& dumpId)
2751{
2752 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2753 {
2754 return;
2755 }
2756 deleteDumpEntry(asyncResp, dumpId, dumpType);
2757}
2758
Ed Tanous22d268c2022-05-19 09:39:07 -07002759inline void handleLogServicesDumpEntryComputerSystemDelete(
2760 crow::App& app, const crow::Request& req,
2761 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2762 const std::string& chassisId, const std::string& dumpId)
2763{
2764 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2765 {
2766 return;
2767 }
2768 if (chassisId != "system")
2769 {
2770 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2771 return;
2772 }
2773 deleteDumpEntry(asyncResp, dumpId, "System");
2774}
2775
Claire Weinanfdd26902022-03-01 14:18:25 -08002776inline void handleLogServicesDumpCollectDiagnosticDataPost(
2777 crow::App& app, const std::string& dumpType, const crow::Request& req,
2778 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2779{
2780 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2781 {
2782 return;
2783 }
2784 createDump(asyncResp, req, dumpType);
2785}
2786
Ed Tanous22d268c2022-05-19 09:39:07 -07002787inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2788 crow::App& app, const crow::Request& req,
2789 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2790 const std::string& chassisId)
2791{
2792 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2793 {
2794 return;
2795 }
2796 if (chassisId != "system")
2797 {
2798 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2799 return;
2800 }
2801 createDump(asyncResp, req, "System");
2802}
2803
Claire Weinanfdd26902022-03-01 14:18:25 -08002804inline void handleLogServicesDumpClearLogPost(
2805 crow::App& app, const std::string& dumpType, const crow::Request& req,
2806 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2807{
2808 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2809 {
2810 return;
2811 }
2812 clearDump(asyncResp, dumpType);
2813}
2814
Ed Tanous22d268c2022-05-19 09:39:07 -07002815inline void handleLogServicesDumpClearLogComputerSystemPost(
2816 crow::App& app, const crow::Request& req,
2817 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2818 const std::string& chassisId)
2819{
2820 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2821 {
2822 return;
2823 }
2824 if (chassisId != "system")
2825 {
2826 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2827 return;
2828 }
2829 clearDump(asyncResp, "System");
2830}
2831
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002832inline void requestRoutesBMCDumpService(App& app)
2833{
2834 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002835 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002836 .methods(boost::beast::http::verb::get)(std::bind_front(
2837 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002838}
2839
2840inline void requestRoutesBMCDumpEntryCollection(App& app)
2841{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002842 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002843 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002844 .methods(boost::beast::http::verb::get)(std::bind_front(
2845 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002846}
2847
2848inline void requestRoutesBMCDumpEntry(App& app)
2849{
2850 BMCWEB_ROUTE(app,
2851 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002852 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002853 .methods(boost::beast::http::verb::get)(std::bind_front(
2854 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2855
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002856 BMCWEB_ROUTE(app,
2857 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002858 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002859 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2860 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002861}
2862
2863inline void requestRoutesBMCDumpCreate(App& app)
2864{
George Liu0fda0f12021-11-16 10:06:17 +08002865 BMCWEB_ROUTE(
2866 app,
2867 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002868 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002869 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002870 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2871 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002872}
2873
2874inline void requestRoutesBMCDumpClear(App& app)
2875{
George Liu0fda0f12021-11-16 10:06:17 +08002876 BMCWEB_ROUTE(
2877 app,
2878 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002879 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002880 .methods(boost::beast::http::verb::post)(std::bind_front(
2881 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2882}
2883
2884inline void requestRoutesFaultLogDumpService(App& app)
2885{
2886 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2887 .privileges(redfish::privileges::getLogService)
2888 .methods(boost::beast::http::verb::get)(std::bind_front(
2889 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2890}
2891
2892inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2893{
2894 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2895 .privileges(redfish::privileges::getLogEntryCollection)
2896 .methods(boost::beast::http::verb::get)(
2897 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2898 std::ref(app), "FaultLog"));
2899}
2900
2901inline void requestRoutesFaultLogDumpEntry(App& app)
2902{
2903 BMCWEB_ROUTE(app,
2904 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2905 .privileges(redfish::privileges::getLogEntry)
2906 .methods(boost::beast::http::verb::get)(std::bind_front(
2907 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2908
2909 BMCWEB_ROUTE(app,
2910 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2911 .privileges(redfish::privileges::deleteLogEntry)
2912 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2913 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2914}
2915
2916inline void requestRoutesFaultLogDumpClear(App& app)
2917{
2918 BMCWEB_ROUTE(
2919 app,
2920 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2921 .privileges(redfish::privileges::postLogService)
2922 .methods(boost::beast::http::verb::post)(std::bind_front(
2923 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002924}
2925
2926inline void requestRoutesSystemDumpService(App& app)
2927{
Ed Tanous22d268c2022-05-19 09:39:07 -07002928 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002929 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002930 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002931 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002932}
2933
2934inline void requestRoutesSystemDumpEntryCollection(App& app)
2935{
Ed Tanous22d268c2022-05-19 09:39:07 -07002936 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002937 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002938 .methods(boost::beast::http::verb::get)(std::bind_front(
2939 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2940 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002941}
2942
2943inline void requestRoutesSystemDumpEntry(App& app)
2944{
2945 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002946 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002947 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002948 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002949 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002950
2951 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002952 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002953 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002954 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002955 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002956}
2957
2958inline void requestRoutesSystemDumpCreate(App& app)
2959{
George Liu0fda0f12021-11-16 10:06:17 +08002960 BMCWEB_ROUTE(
2961 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002962 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002963 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07002964 .methods(boost::beast::http::verb::post)(std::bind_front(
2965 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
2966 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002967}
2968
2969inline void requestRoutesSystemDumpClear(App& app)
2970{
George Liu0fda0f12021-11-16 10:06:17 +08002971 BMCWEB_ROUTE(
2972 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002973 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002974 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002975 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002976 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002977}
2978
2979inline void requestRoutesCrashdumpService(App& app)
2980{
2981 // Note: Deviated from redfish privilege registry for GET & HEAD
2982 // method for security reasons.
2983 /**
2984 * Functions triggers appropriate requests on DBus
2985 */
Ed Tanous22d268c2022-05-19 09:39:07 -07002986 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002987 // This is incorrect, should be:
2988 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002989 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002990 .methods(boost::beast::http::verb::get)(
2991 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002992 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2993 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002994 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002995 {
2996 return;
2997 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002998 if (systemName != "system")
2999 {
3000 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3001 systemName);
3002 return;
3003 }
3004
Ed Tanous002d39b2022-05-31 08:59:27 -07003005 // Copy over the static data to include the entries added by
3006 // SubRoute
3007 asyncResp->res.jsonValue["@odata.id"] =
3008 "/redfish/v1/Systems/system/LogServices/Crashdump";
3009 asyncResp->res.jsonValue["@odata.type"] =
3010 "#LogService.v1_2_0.LogService";
3011 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3012 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
3013 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
3014 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3015 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303016
Ed Tanous002d39b2022-05-31 08:59:27 -07003017 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003018 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003019 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3020 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3021 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303022
Ed Tanous002d39b2022-05-31 08:59:27 -07003023 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00003024 crow::utility::urlFromPieces("redfish", "v1", "Systems", "system",
3025 "LogServices", "Crashdump", "Entries");
Ed Tanous002d39b2022-05-31 08:59:27 -07003026 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3027 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3028 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3029 ["target"] =
3030 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003031 });
3032}
3033
3034void inline requestRoutesCrashdumpClear(App& app)
3035{
George Liu0fda0f12021-11-16 10:06:17 +08003036 BMCWEB_ROUTE(
3037 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003038 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003039 // This is incorrect, should be:
3040 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003041 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003042 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003043 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003044 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3045 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003046 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003047 {
3048 return;
3049 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003050 if (systemName != "system")
3051 {
3052 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3053 systemName);
3054 return;
3055 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003056 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003057 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003058 const std::string&) {
3059 if (ec)
3060 {
3061 messages::internalError(asyncResp->res);
3062 return;
3063 }
3064 messages::success(asyncResp->res);
3065 },
3066 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3067 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003068}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003069
zhanghch058d1b46d2021-04-01 11:18:24 +08003070static void
3071 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3072 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003073{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003074 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003075 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003076 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003077 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003078 if (ec)
3079 {
3080 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3081 if (ec.value() ==
3082 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003083 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003084 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003085 }
3086 else
3087 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003088 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003089 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003090 return;
3091 }
3092
3093 std::string timestamp{};
3094 std::string filename{};
3095 std::string logfile{};
3096 parseCrashdumpParameters(params, filename, timestamp, logfile);
3097
3098 if (filename.empty() || timestamp.empty())
3099 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003100 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003101 return;
3102 }
3103
3104 std::string crashdumpURI =
3105 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3106 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003107 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003108 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003109 logEntry["@odata.id"] = crow::utility::urlFromPieces(
3110 "redfish", "v1", "Systems", "system", "LogServices", "Crashdump",
3111 "Entries", logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003112 logEntry["Name"] = "CPU Crashdump";
3113 logEntry["Id"] = logID;
3114 logEntry["EntryType"] = "Oem";
3115 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3116 logEntry["DiagnosticDataType"] = "OEM";
3117 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3118 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003119
3120 // If logEntryJson references an array of LogEntry resources
3121 // ('Members' list), then push this as a new entry, otherwise set it
3122 // directly
3123 if (logEntryJson.is_array())
3124 {
3125 logEntryJson.push_back(logEntry);
3126 asyncResp->res.jsonValue["Members@odata.count"] =
3127 logEntryJson.size();
3128 }
3129 else
3130 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003131 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003132 }
3133 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003134 sdbusplus::asio::getAllProperties(
3135 *crow::connections::systemBus, crashdumpObject,
3136 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3137 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003138}
3139
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003140inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003141{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003142 // Note: Deviated from redfish privilege registry for GET & HEAD
3143 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003144 /**
3145 * Functions triggers appropriate requests on DBus
3146 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003147 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003148 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003149 // This is incorrect, should be.
3150 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003151 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003152 .methods(boost::beast::http::verb::get)(
3153 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003154 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3155 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003156 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003157 {
3158 return;
3159 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003160 if (systemName != "system")
3161 {
3162 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3163 systemName);
3164 return;
3165 }
3166
George Liu7a1dbc42022-12-07 16:03:22 +08003167 constexpr std::array<std::string_view, 1> interfaces = {
3168 crashdumpInterface};
3169 dbus::utility::getSubTreePaths(
3170 "/", 0, interfaces,
3171 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003172 const std::vector<std::string>& resp) {
3173 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003174 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003175 if (ec.value() !=
3176 boost::system::errc::no_such_file_or_directory)
3177 {
3178 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3179 << ec.message();
3180 messages::internalError(asyncResp->res);
3181 return;
3182 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003183 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003184 asyncResp->res.jsonValue["@odata.type"] =
3185 "#LogEntryCollection.LogEntryCollection";
3186 asyncResp->res.jsonValue["@odata.id"] =
3187 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3188 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3189 asyncResp->res.jsonValue["Description"] =
3190 "Collection of Crashdump Entries";
3191 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3192 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003193
Ed Tanous002d39b2022-05-31 08:59:27 -07003194 for (const std::string& path : resp)
3195 {
3196 const sdbusplus::message::object_path objPath(path);
3197 // Get the log ID
3198 std::string logID = objPath.filename();
3199 if (logID.empty())
3200 {
3201 continue;
3202 }
3203 // Add the log entry to the array
3204 logCrashdumpEntry(asyncResp, logID,
3205 asyncResp->res.jsonValue["Members"]);
3206 }
George Liu7a1dbc42022-12-07 16:03:22 +08003207 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003208 });
3209}
Ed Tanous1da66f72018-07-27 16:13:37 -07003210
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003211inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003212{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003213 // Note: Deviated from redfish privilege registry for GET & HEAD
3214 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003215
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003216 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003217 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003218 // this is incorrect, should be
3219 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003220 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003221 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003222 [&app](const crow::Request& req,
3223 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003224 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003225 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003226 {
3227 return;
3228 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003229 if (systemName != "system")
3230 {
3231 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3232 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003233 return;
3234 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003235 const std::string& logID = param;
3236 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3237 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003238}
Ed Tanous1da66f72018-07-27 16:13:37 -07003239
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003240inline void requestRoutesCrashdumpFile(App& app)
3241{
3242 // Note: Deviated from redfish privilege registry for GET & HEAD
3243 // method for security reasons.
3244 BMCWEB_ROUTE(
3245 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003246 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003247 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003248 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003249 [](const crow::Request& req,
3250 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003251 const std::string& systemName, const std::string& logID,
3252 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003253 // Do not call getRedfishRoute here since the crashdump file is not a
3254 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003255
3256 if (systemName != "system")
3257 {
3258 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3259 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003260 return;
3261 }
3262
Ed Tanous002d39b2022-05-31 08:59:27 -07003263 auto getStoredLogCallback =
3264 [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003265 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003266 const std::vector<
3267 std::pair<std::string, dbus::utility::DbusVariantType>>&
3268 resp) {
3269 if (ec)
3270 {
3271 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3272 messages::internalError(asyncResp->res);
3273 return;
3274 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003275
Ed Tanous002d39b2022-05-31 08:59:27 -07003276 std::string dbusFilename{};
3277 std::string dbusTimestamp{};
3278 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003279
Ed Tanous002d39b2022-05-31 08:59:27 -07003280 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3281 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003282
Ed Tanous002d39b2022-05-31 08:59:27 -07003283 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3284 dbusFilepath.empty())
3285 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003286 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003287 return;
3288 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003289
Ed Tanous002d39b2022-05-31 08:59:27 -07003290 // Verify the file name parameter is correct
3291 if (fileName != dbusFilename)
3292 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003293 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003294 return;
3295 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003296
Ed Tanous002d39b2022-05-31 08:59:27 -07003297 if (!std::filesystem::exists(dbusFilepath))
3298 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003299 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003300 return;
3301 }
3302 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3303 asyncResp->res.body() =
3304 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003305
Ed Tanous002d39b2022-05-31 08:59:27 -07003306 // Configure this to be a file download when accessed
3307 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003308 asyncResp->res.addHeader(
3309 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003310 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003311 sdbusplus::asio::getAllProperties(
3312 *crow::connections::systemBus, crashdumpObject,
3313 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3314 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003315 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003316}
3317
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003318enum class OEMDiagnosticType
3319{
3320 onDemand,
3321 telemetry,
3322 invalid,
3323};
3324
Ed Tanous26ccae32023-02-16 10:28:44 -08003325inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003326{
3327 if (oemDiagStr == "OnDemand")
3328 {
3329 return OEMDiagnosticType::onDemand;
3330 }
3331 if (oemDiagStr == "Telemetry")
3332 {
3333 return OEMDiagnosticType::telemetry;
3334 }
3335
3336 return OEMDiagnosticType::invalid;
3337}
3338
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003339inline void requestRoutesCrashdumpCollect(App& app)
3340{
3341 // Note: Deviated from redfish privilege registry for GET & HEAD
3342 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003343 BMCWEB_ROUTE(
3344 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003345 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003346 // The below is incorrect; Should be ConfigureManager
3347 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003348 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003349 .methods(boost::beast::http::verb::post)(
3350 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003351 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3352 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003353 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003354 {
3355 return;
3356 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003357
3358 if (systemName != "system")
3359 {
3360 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3361 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003362 return;
3363 }
3364
Ed Tanous002d39b2022-05-31 08:59:27 -07003365 std::string diagnosticDataType;
3366 std::string oemDiagnosticDataType;
3367 if (!redfish::json_util::readJsonAction(
3368 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3369 "OEMDiagnosticDataType", oemDiagnosticDataType))
3370 {
3371 return;
3372 }
3373
3374 if (diagnosticDataType != "OEM")
3375 {
3376 BMCWEB_LOG_ERROR
3377 << "Only OEM DiagnosticDataType supported for Crashdump";
3378 messages::actionParameterValueFormatError(
3379 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3380 "CollectDiagnosticData");
3381 return;
3382 }
3383
3384 OEMDiagnosticType oemDiagType =
3385 getOEMDiagnosticType(oemDiagnosticDataType);
3386
3387 std::string iface;
3388 std::string method;
3389 std::string taskMatchStr;
3390 if (oemDiagType == OEMDiagnosticType::onDemand)
3391 {
3392 iface = crashdumpOnDemandInterface;
3393 method = "GenerateOnDemandLog";
3394 taskMatchStr = "type='signal',"
3395 "interface='org.freedesktop.DBus.Properties',"
3396 "member='PropertiesChanged',"
3397 "arg0namespace='com.intel.crashdump'";
3398 }
3399 else if (oemDiagType == OEMDiagnosticType::telemetry)
3400 {
3401 iface = crashdumpTelemetryInterface;
3402 method = "GenerateTelemetryLog";
3403 taskMatchStr = "type='signal',"
3404 "interface='org.freedesktop.DBus.Properties',"
3405 "member='PropertiesChanged',"
3406 "arg0namespace='com.intel.crashdump'";
3407 }
3408 else
3409 {
3410 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3411 << oemDiagnosticDataType;
3412 messages::actionParameterValueFormatError(
3413 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3414 "CollectDiagnosticData");
3415 return;
3416 }
3417
3418 auto collectCrashdumpCallback =
3419 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003420 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003421 const std::string&) mutable {
3422 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003423 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003424 if (ec.value() == boost::system::errc::operation_not_supported)
3425 {
3426 messages::resourceInStandby(asyncResp->res);
3427 }
3428 else if (ec.value() ==
3429 boost::system::errc::device_or_resource_busy)
3430 {
3431 messages::serviceTemporarilyUnavailable(asyncResp->res,
3432 "60");
3433 }
3434 else
3435 {
3436 messages::internalError(asyncResp->res);
3437 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003438 return;
3439 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003440 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003441 [](const boost::system::error_code& err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003442 const std::shared_ptr<task::TaskData>& taskData) {
3443 if (!err)
3444 {
3445 taskData->messages.emplace_back(messages::taskCompletedOK(
3446 std::to_string(taskData->index)));
3447 taskData->state = "Completed";
3448 }
3449 return task::completed;
3450 },
3451 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003452
Ed Tanous002d39b2022-05-31 08:59:27 -07003453 task->startTimer(std::chrono::minutes(5));
3454 task->populateResp(asyncResp->res);
3455 task->payload.emplace(std::move(payload));
3456 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003457
Ed Tanous002d39b2022-05-31 08:59:27 -07003458 crow::connections::systemBus->async_method_call(
3459 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3460 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003461 });
3462}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003463
Andrew Geisslercb92c032018-08-17 07:56:14 -07003464/**
3465 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3466 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003467inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003468{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003469 /**
3470 * Function handles POST method request.
3471 * The Clear Log actions does not require any parameter.The action deletes
3472 * all entries found in the Entries collection for this Log Service.
3473 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003474
George Liu0fda0f12021-11-16 10:06:17 +08003475 BMCWEB_ROUTE(
3476 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003477 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003478 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003479 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003480 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003481 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3482 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003483 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003484 {
3485 return;
3486 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003487 if (systemName != "system")
3488 {
3489 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3490 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003491 return;
3492 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003493 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003494
Ed Tanous002d39b2022-05-31 08:59:27 -07003495 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003496 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003497 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3498 if (ec)
3499 {
3500 // TODO Handle for specific error code
3501 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3502 asyncResp->res.result(
3503 boost::beast::http::status::internal_server_error);
3504 return;
3505 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003506
Ed Tanous002d39b2022-05-31 08:59:27 -07003507 asyncResp->res.result(boost::beast::http::status::no_content);
3508 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003509
Ed Tanous002d39b2022-05-31 08:59:27 -07003510 // Make call to Logging service to request Clear Log
3511 crow::connections::systemBus->async_method_call(
3512 respHandler, "xyz.openbmc_project.Logging",
3513 "/xyz/openbmc_project/logging",
3514 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3515 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003516}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003517
3518/****************************************************
3519 * Redfish PostCode interfaces
3520 * using DBUS interface: getPostCodesTS
3521 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003522inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003523{
Ed Tanous22d268c2022-05-19 09:39:07 -07003524 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003525 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003526 .methods(boost::beast::http::verb::get)(
3527 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003528 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3529 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003530 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003531 {
3532 return;
3533 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003534 if (systemName != "system")
3535 {
3536 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3537 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003538 return;
3539 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003540 asyncResp->res.jsonValue["@odata.id"] =
3541 "/redfish/v1/Systems/system/LogServices/PostCodes";
3542 asyncResp->res.jsonValue["@odata.type"] =
3543 "#LogService.v1_1_0.LogService";
3544 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3545 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003546 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003547 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3548 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3549 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303550
Ed Tanous002d39b2022-05-31 08:59:27 -07003551 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003552 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003553 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3554 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3555 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303556
Ed Tanous002d39b2022-05-31 08:59:27 -07003557 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3558 {"target",
3559 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003560 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003561}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003562
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003563inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003564{
George Liu0fda0f12021-11-16 10:06:17 +08003565 BMCWEB_ROUTE(
3566 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003567 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003568 // The following privilege is incorrect; It should be ConfigureManager
3569 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003570 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003571 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003572 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003573 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3574 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003575 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003576 {
3577 return;
3578 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003579 if (systemName != "system")
3580 {
3581 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3582 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003583 return;
3584 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003585 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003586
Ed Tanous002d39b2022-05-31 08:59:27 -07003587 // Make call to post-code service to request clear all
3588 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003589 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003590 if (ec)
3591 {
3592 // TODO Handle for specific error code
3593 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3594 << ec;
3595 asyncResp->res.result(
3596 boost::beast::http::status::internal_server_error);
3597 messages::internalError(asyncResp->res);
3598 return;
3599 }
3600 },
3601 "xyz.openbmc_project.State.Boot.PostCode0",
3602 "/xyz/openbmc_project/State/Boot/PostCode0",
3603 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3604 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003605}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003606
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003607/**
3608 * @brief Parse post code ID and get the current value and index value
3609 * eg: postCodeID=B1-2, currentValue=1, index=2
3610 *
3611 * @param[in] postCodeID Post Code ID
3612 * @param[out] currentValue Current value
3613 * @param[out] index Index value
3614 *
3615 * @return bool true if the parsing is successful, false the parsing fails
3616 */
3617inline static bool parsePostCode(const std::string& postCodeID,
3618 uint64_t& currentValue, uint16_t& index)
3619{
3620 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003621 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003622 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3623 {
3624 return false;
3625 }
3626
3627 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3628 const char* start = split[0].data() + 1;
3629 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3630 const char* end = split[0].data() + split[0].size();
3631 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3632
3633 if (ptrIndex != end || ecIndex != std::errc())
3634 {
3635 return false;
3636 }
3637
3638 start = split[1].data();
3639
3640 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3641 end = split[1].data() + split[1].size();
3642 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3643
3644 return ptrValue == end && ecValue == std::errc();
3645}
3646
3647static bool fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003648 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303649 const boost::container::flat_map<
3650 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003651 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3652 const uint64_t skip = 0, const uint64_t top = 0)
3653{
3654 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003655 const registries::Message* message =
3656 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003657
3658 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003659 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303660 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3661 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003662 {
3663 currentCodeIndex++;
3664 std::string postcodeEntryID =
3665 "B" + std::to_string(bootIndex) + "-" +
3666 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3667
3668 uint64_t usecSinceEpoch = code.first;
3669 uint64_t usTimeOffset = 0;
3670
3671 if (1 == currentCodeIndex)
3672 { // already incremented
3673 firstCodeTimeUs = code.first;
3674 }
3675 else
3676 {
3677 usTimeOffset = code.first - firstCodeTimeUs;
3678 }
3679
3680 // skip if no specific codeIndex is specified and currentCodeIndex does
3681 // not fall between top and skip
3682 if ((codeIndex == 0) &&
3683 (currentCodeIndex <= skip || currentCodeIndex > top))
3684 {
3685 continue;
3686 }
3687
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003688 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003689 // currentIndex
3690 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3691 {
3692 // This is done for simplicity. 1st entry is needed to calculate
3693 // time offset. To improve efficiency, one can get to the entry
3694 // directly (possibly with flatmap's nth method)
3695 continue;
3696 }
3697
3698 // currentCodeIndex is within top and skip or equal to specified code
3699 // index
3700
3701 // Get the Created time from the timestamp
3702 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003703 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003704
3705 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3706 std::ostringstream hexCode;
3707 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303708 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003709 std::ostringstream timeOffsetStr;
3710 // Set Fixed -Point Notation
3711 timeOffsetStr << std::fixed;
3712 // Set precision to 4 digits
3713 timeOffsetStr << std::setprecision(4);
3714 // Add double to stream
3715 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3716 std::vector<std::string> messageArgs = {
3717 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3718
3719 // Get MessageArgs template from message registry
3720 std::string msg;
3721 if (message != nullptr)
3722 {
3723 msg = message->message;
3724
3725 // fill in this post code value
3726 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003727 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003728 {
3729 std::string argStr = "%" + std::to_string(++i);
3730 size_t argPos = msg.find(argStr);
3731 if (argPos != std::string::npos)
3732 {
3733 msg.replace(argPos, argStr.length(), messageArg);
3734 }
3735 }
3736 }
3737
Tim Leed4342a92020-04-27 11:47:58 +08003738 // Get Severity template from message registry
3739 std::string severity;
3740 if (message != nullptr)
3741 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003742 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003743 }
3744
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003745 // Format entry
3746 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003747 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003748 bmcLogEntry["@odata.id"] = crow::utility::urlFromPieces(
3749 "redfish", "v1", "Systems", "system", "LogServices", "PostCodes",
3750 "Entries", postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003751 bmcLogEntry["Name"] = "POST Code Log Entry";
3752 bmcLogEntry["Id"] = postcodeEntryID;
3753 bmcLogEntry["Message"] = std::move(msg);
3754 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3755 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3756 bmcLogEntry["EntryType"] = "Event";
3757 bmcLogEntry["Severity"] = std::move(severity);
3758 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003759 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3760 {
3761 bmcLogEntry["AdditionalDataURI"] =
3762 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3763 postcodeEntryID + "/attachment";
3764 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003765
3766 // codeIndex is only specified when querying single entry, return only
3767 // that entry in this case
3768 if (codeIndex != 0)
3769 {
3770 aResp->res.jsonValue.update(bmcLogEntry);
3771 return true;
3772 }
3773
3774 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3775 logEntryArray.push_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003776 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003777
3778 // Return value is always false when querying multiple entries
3779 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003780}
3781
zhanghch058d1b46d2021-04-01 11:18:24 +08003782static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003783 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003784{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003785 uint16_t bootIndex = 0;
3786 uint64_t codeIndex = 0;
3787 if (!parsePostCode(entryId, codeIndex, bootIndex))
3788 {
3789 // Requested ID was not found
3790 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3791 return;
3792 }
3793
3794 if (bootIndex == 0 || codeIndex == 0)
3795 {
3796 // 0 is an invalid index
3797 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3798 return;
3799 }
3800
ZhikuiRena3316fc2020-01-29 14:58:08 -08003801 crow::connections::systemBus->async_method_call(
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003802 [aResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003803 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303804 const boost::container::flat_map<
3805 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3806 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003807 if (ec)
3808 {
3809 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3810 messages::internalError(aResp->res);
3811 return;
3812 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003813
Ed Tanous002d39b2022-05-31 08:59:27 -07003814 if (postcode.empty())
3815 {
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003816 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003817 return;
3818 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003819
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003820 if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
3821 {
3822 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3823 return;
3824 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003825 },
Jonathan Doman15124762021-01-07 17:54:17 -08003826 "xyz.openbmc_project.State.Boot.PostCode0",
3827 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003828 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3829 bootIndex);
3830}
3831
zhanghch058d1b46d2021-04-01 11:18:24 +08003832static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003833 const uint16_t bootIndex,
3834 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003835 const uint64_t entryCount, size_t skip,
3836 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003837{
3838 crow::connections::systemBus->async_method_call(
3839 [aResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003840 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303841 const boost::container::flat_map<
3842 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3843 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003844 if (ec)
3845 {
3846 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3847 messages::internalError(aResp->res);
3848 return;
3849 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003850
Ed Tanous002d39b2022-05-31 08:59:27 -07003851 uint64_t endCount = entryCount;
3852 if (!postcode.empty())
3853 {
3854 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003855 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003856 {
Ed Tanous3648c8b2022-07-25 13:39:59 -07003857 uint64_t thisBootSkip =
3858 std::max(static_cast<uint64_t>(skip), entryCount) -
3859 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003860 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003861 std::min(static_cast<uint64_t>(top + skip), endCount) -
3862 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003863
3864 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3865 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003866 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003867 aResp->res.jsonValue["Members@odata.count"] = endCount;
3868 }
3869
3870 // continue to previous bootIndex
3871 if (bootIndex < bootCount)
3872 {
3873 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3874 bootCount, endCount, skip, top);
3875 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003876 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003877 {
3878 aResp->res.jsonValue["Members@odata.nextLink"] =
3879 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3880 std::to_string(skip + top);
3881 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003882 },
Jonathan Doman15124762021-01-07 17:54:17 -08003883 "xyz.openbmc_project.State.Boot.PostCode0",
3884 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003885 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3886 bootIndex);
3887}
3888
zhanghch058d1b46d2021-04-01 11:18:24 +08003889static void
3890 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003891 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003892{
3893 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003894 sdbusplus::asio::getProperty<uint16_t>(
3895 *crow::connections::systemBus,
3896 "xyz.openbmc_project.State.Boot.PostCode0",
3897 "/xyz/openbmc_project/State/Boot/PostCode0",
3898 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003899 [aResp, entryCount, skip, top](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003900 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003901 if (ec)
3902 {
3903 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3904 messages::internalError(aResp->res);
3905 return;
3906 }
3907 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003908 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003909}
3910
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003911inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003912{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003913 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003914 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003915 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003916 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003917 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003918 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3919 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003920 query_param::QueryCapabilities capabilities = {
3921 .canDelegateTop = true,
3922 .canDelegateSkip = true,
3923 };
3924 query_param::Query delegatedQuery;
3925 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003926 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003927 {
3928 return;
3929 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003930
3931 if (systemName != "system")
3932 {
3933 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3934 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003935 return;
3936 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003937 asyncResp->res.jsonValue["@odata.type"] =
3938 "#LogEntryCollection.LogEntryCollection";
3939 asyncResp->res.jsonValue["@odata.id"] =
3940 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3941 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3942 asyncResp->res.jsonValue["Description"] =
3943 "Collection of POST Code Log Entries";
3944 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3945 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003946 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003947 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003948 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003949 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003950}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003951
George Liu647b3cd2021-07-05 12:43:56 +08003952inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3953{
George Liu0fda0f12021-11-16 10:06:17 +08003954 BMCWEB_ROUTE(
3955 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003956 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003957 .privileges(redfish::privileges::getLogEntry)
3958 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003959 [&app](const crow::Request& req,
3960 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003961 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07003962 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003963 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003964 {
3965 return;
3966 }
Ed Tanous99351cd2022-08-07 16:42:51 -07003967 if (http_helpers::isContentTypeAllowed(
3968 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07003969 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07003970 {
3971 asyncResp->res.result(boost::beast::http::status::bad_request);
3972 return;
3973 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003974 if (systemName != "system")
3975 {
3976 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3977 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003978 return;
3979 }
George Liu647b3cd2021-07-05 12:43:56 +08003980
Ed Tanous002d39b2022-05-31 08:59:27 -07003981 uint64_t currentValue = 0;
3982 uint16_t index = 0;
3983 if (!parsePostCode(postCodeID, currentValue, index))
3984 {
3985 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3986 return;
3987 }
George Liu647b3cd2021-07-05 12:43:56 +08003988
Ed Tanous002d39b2022-05-31 08:59:27 -07003989 crow::connections::systemBus->async_method_call(
3990 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003991 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003992 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3993 postcodes) {
3994 if (ec.value() == EBADR)
3995 {
3996 messages::resourceNotFound(asyncResp->res, "LogEntry",
3997 postCodeID);
3998 return;
3999 }
4000 if (ec)
4001 {
4002 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4003 messages::internalError(asyncResp->res);
4004 return;
4005 }
George Liu647b3cd2021-07-05 12:43:56 +08004006
Ed Tanous002d39b2022-05-31 08:59:27 -07004007 size_t value = static_cast<size_t>(currentValue) - 1;
4008 if (value == std::string::npos || postcodes.size() < currentValue)
4009 {
4010 BMCWEB_LOG_ERROR << "Wrong currentValue value";
4011 messages::resourceNotFound(asyncResp->res, "LogEntry",
4012 postCodeID);
4013 return;
4014 }
George Liu647b3cd2021-07-05 12:43:56 +08004015
Ed Tanous002d39b2022-05-31 08:59:27 -07004016 const auto& [tID, c] = postcodes[value];
4017 if (c.empty())
4018 {
4019 BMCWEB_LOG_INFO << "No found post code data";
4020 messages::resourceNotFound(asyncResp->res, "LogEntry",
4021 postCodeID);
4022 return;
4023 }
4024 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4025 const char* d = reinterpret_cast<const char*>(c.data());
4026 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004027
Ed Tanousd9f6c622022-03-17 09:12:17 -07004028 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004029 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004030 asyncResp->res.addHeader(
4031 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004032 asyncResp->res.body() = crow::utility::base64encode(strData);
4033 },
4034 "xyz.openbmc_project.State.Boot.PostCode0",
4035 "/xyz/openbmc_project/State/Boot/PostCode0",
4036 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4037 });
George Liu647b3cd2021-07-05 12:43:56 +08004038}
4039
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004040inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004041{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004042 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004043 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004044 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004045 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004046 [&app](const crow::Request& req,
4047 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004048 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004049 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004050 {
4051 return;
4052 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004053 if (systemName != "system")
4054 {
4055 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4056 systemName);
4057 return;
4058 }
4059
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004060 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004061 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004062}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004063
Ed Tanous1da66f72018-07-27 16:13:37 -07004064} // namespace redfish