blob: ef1652bd728b1ed80d2f8e90350a7acbd32ce0b8 [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
Gunnar Mills1214b7e2020-06-04 10:11:30 -050094static const Message* getMessage(const 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);
101 boost::split(fields, messageID, boost::is_any_of("."));
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,
160 const std::string_view& field,
161 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,
182 const std::string_view& field,
183 const int& base, 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 }
Ed Tanous2b829372022-08-03 14:22:34 -0700209 entryTimestamp =
210 redfish::time_utils::getDateTimeUint(timestamp / 1000 / 1000);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500211 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800212}
Ed Tanous50b8a432022-02-03 16:29:50 -0800213
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700214inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
215 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700216{
217 int ret = 0;
218 static uint64_t prevTs = 0;
219 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700220 if (firstEntry)
221 {
222 prevTs = 0;
223 }
224
Jason M. Bills16428a12018-11-02 12:42:29 -0700225 // Get the entry timestamp
226 uint64_t curTs = 0;
227 ret = sd_journal_get_realtime_usec(journal, &curTs);
228 if (ret < 0)
229 {
230 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
231 << strerror(-ret);
232 return false;
233 }
234 // If the timestamp isn't unique, increment the index
235 if (curTs == prevTs)
236 {
237 index++;
238 }
239 else
240 {
241 // Otherwise, reset it
242 index = 0;
243 }
244 // Save the timestamp
245 prevTs = curTs;
246
247 entryID = std::to_string(curTs);
248 if (index > 0)
249 {
250 entryID += "_" + std::to_string(index);
251 }
252 return true;
253}
254
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500255static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700256 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700257{
Ed Tanous271584a2019-07-09 16:24:22 -0700258 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700259 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700260 if (firstEntry)
261 {
262 prevTs = 0;
263 }
264
Jason M. Bills95820182019-04-22 16:25:34 -0700265 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700266 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700267 std::tm timeStruct = {};
268 std::istringstream entryStream(logEntry);
269 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
270 {
271 curTs = std::mktime(&timeStruct);
272 }
273 // If the timestamp isn't unique, increment the index
274 if (curTs == prevTs)
275 {
276 index++;
277 }
278 else
279 {
280 // Otherwise, reset it
281 index = 0;
282 }
283 // Save the timestamp
284 prevTs = curTs;
285
286 entryID = std::to_string(curTs);
287 if (index > 0)
288 {
289 entryID += "_" + std::to_string(index);
290 }
291 return true;
292}
293
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700294inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800295 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
296 const std::string& entryID, uint64_t& timestamp,
297 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700298{
299 if (entryID.empty())
300 {
301 return false;
302 }
303 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800304 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700305
Ed Tanous81ce6092020-12-17 16:54:55 +0000306 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800307 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700308 {
309 // Timestamp has an index
310 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800311 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700312 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700313 auto [ptr, ec] = std::from_chars(
314 indexStr.data(), indexStr.data() + indexStr.size(), index);
315 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700316 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800317 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700318 return false;
319 }
320 }
321 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700322 auto [ptr, ec] =
323 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
324 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700325 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800326 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700327 return false;
328 }
329 return true;
330}
331
Jason M. Bills95820182019-04-22 16:25:34 -0700332static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500333 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700334{
335 static const std::filesystem::path redfishLogDir = "/var/log";
336 static const std::string redfishLogFilename = "redfish";
337
338 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500339 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700340 std::filesystem::directory_iterator(redfishLogDir))
341 {
342 // If we find a redfish log file, save the path
343 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700344 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700345 {
346 redfishLogFiles.emplace_back(redfishLogDir / filename);
347 }
348 }
349 // As the log files rotate, they are appended with a ".#" that is higher for
350 // the older logs. Since we don't expect more than 10 log files, we
351 // can just sort the list to get them in order from newest to oldest
352 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
353
354 return !redfishLogFiles.empty();
355}
356
Claire Weinanaefe3782022-07-15 19:17:19 -0700357inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800358 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700359 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700360 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
361{
362 for (const auto& interfaceMap : object.second)
363 {
364 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
365 {
366 for (const auto& propertyMap : interfaceMap.second)
367 {
368 if (propertyMap.first == "Status")
369 {
370 const auto* status =
371 std::get_if<std::string>(&propertyMap.second);
372 if (status == nullptr)
373 {
374 messages::internalError(asyncResp->res);
375 break;
376 }
377 dumpStatus = *status;
378 }
379 }
380 }
381 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
382 {
383 for (const auto& propertyMap : interfaceMap.second)
384 {
385 if (propertyMap.first == "Size")
386 {
387 const auto* sizePtr =
388 std::get_if<uint64_t>(&propertyMap.second);
389 if (sizePtr == nullptr)
390 {
391 messages::internalError(asyncResp->res);
392 break;
393 }
394 size = *sizePtr;
395 break;
396 }
397 }
398 }
399 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
400 {
401 for (const auto& propertyMap : interfaceMap.second)
402 {
403 if (propertyMap.first == "Elapsed")
404 {
405 const uint64_t* usecsTimeStamp =
406 std::get_if<uint64_t>(&propertyMap.second);
407 if (usecsTimeStamp == nullptr)
408 {
409 messages::internalError(asyncResp->res);
410 break;
411 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700412 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700413 break;
414 }
415 }
416 }
417 }
418}
419
Nan Zhou21ab4042022-06-26 23:07:40 +0000420static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800421{
422 std::string entriesPath;
423
424 if (dumpType == "BMC")
425 {
426 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
427 }
428 else if (dumpType == "FaultLog")
429 {
430 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
431 }
432 else if (dumpType == "System")
433 {
434 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
435 }
436 else
437 {
438 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
439 << dumpType;
440 }
441
442 // Returns empty string on error
443 return entriesPath;
444}
445
zhanghch058d1b46d2021-04-01 11:18:24 +0800446inline void
447 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
448 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500449{
Claire Weinanfdd26902022-03-01 14:18:25 -0800450 std::string entriesPath = getDumpEntriesPath(dumpType);
451 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500452 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500453 messages::internalError(asyncResp->res);
454 return;
455 }
456
457 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800458 [asyncResp, entriesPath,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800459 dumpType](const boost::system::error_code ec,
460 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700461 if (ec)
462 {
463 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
464 messages::internalError(asyncResp->res);
465 return;
466 }
467
Claire Weinanfdd26902022-03-01 14:18:25 -0800468 // Remove ending slash
469 std::string odataIdStr = entriesPath;
470 if (!odataIdStr.empty())
471 {
472 odataIdStr.pop_back();
473 }
474
475 asyncResp->res.jsonValue["@odata.type"] =
476 "#LogEntryCollection.LogEntryCollection";
477 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
478 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
479 asyncResp->res.jsonValue["Description"] =
480 "Collection of " + dumpType + " Dump Entries";
481
Ed Tanous002d39b2022-05-31 08:59:27 -0700482 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
483 entriesArray = nlohmann::json::array();
484 std::string dumpEntryPath =
485 "/xyz/openbmc_project/dump/" +
486 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
487
488 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
489 return AlphanumLess<std::string>()(l.first.filename(),
490 r.first.filename());
491 });
492
493 for (auto& object : resp)
494 {
495 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500496 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 continue;
498 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700499 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700500 uint64_t size = 0;
501 std::string dumpStatus;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700502 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700503
504 std::string entryID = object.first.filename();
505 if (entryID.empty())
506 {
507 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500508 }
509
Claire Weinanc6fecda2022-07-15 10:43:25 -0700510 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700511 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700512
513 if (dumpStatus !=
514 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
515 !dumpStatus.empty())
516 {
517 // Dump status is not Complete, no need to enumerate
518 continue;
519 }
520
Vijay Lobo9c11a172021-10-07 16:53:16 -0500521 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800522 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700523 thisEntry["Id"] = entryID;
524 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700525 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700526 thisEntry["Created"] =
527 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700528
Ed Tanous002d39b2022-05-31 08:59:27 -0700529 if (dumpType == "BMC")
530 {
531 thisEntry["DiagnosticDataType"] = "Manager";
532 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800533 entriesPath + entryID + "/attachment";
534 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700535 }
536 else if (dumpType == "System")
537 {
538 thisEntry["DiagnosticDataType"] = "OEM";
539 thisEntry["OEMDiagnosticDataType"] = "System";
540 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800541 entriesPath + entryID + "/attachment";
542 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 }
544 entriesArray.push_back(std::move(thisEntry));
545 }
546 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500547 },
548 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
549 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
550}
551
zhanghch058d1b46d2021-04-01 11:18:24 +0800552inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700553 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800554 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500555{
Claire Weinanfdd26902022-03-01 14:18:25 -0800556 std::string entriesPath = getDumpEntriesPath(dumpType);
557 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500558 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500559 messages::internalError(asyncResp->res);
560 return;
561 }
562
563 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800564 [asyncResp, entryID, dumpType,
565 entriesPath](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700566 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700567 if (ec)
568 {
569 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
570 messages::internalError(asyncResp->res);
571 return;
572 }
573
574 bool foundDumpEntry = false;
575 std::string dumpEntryPath =
576 "/xyz/openbmc_project/dump/" +
577 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
578
579 for (const auto& objectPath : resp)
580 {
581 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500582 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700583 continue;
584 }
585
586 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700587 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700588 uint64_t size = 0;
589 std::string dumpStatus;
590
Claire Weinanaefe3782022-07-15 19:17:19 -0700591 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700592 timestampUs, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700593
594 if (dumpStatus !=
595 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
596 !dumpStatus.empty())
597 {
598 // Dump status is not Complete
599 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200600 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
601 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500602 return;
603 }
604
Ed Tanous002d39b2022-05-31 08:59:27 -0700605 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -0500606 "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800607 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700608 asyncResp->res.jsonValue["Id"] = entryID;
609 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700610 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700611 asyncResp->res.jsonValue["Created"] =
612 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500613
Ed Tanous002d39b2022-05-31 08:59:27 -0700614 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500615 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700616 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
617 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800618 entriesPath + entryID + "/attachment";
619 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500620 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700621 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500622 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700623 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
624 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
625 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800626 entriesPath + entryID + "/attachment";
627 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500628 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700629 }
630 if (!foundDumpEntry)
631 {
632 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
633 messages::internalError(asyncResp->res);
634 return;
635 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500636 },
637 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
638 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
639}
640
zhanghch058d1b46d2021-04-01 11:18:24 +0800641inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800642 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500643 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500644{
Ed Tanous002d39b2022-05-31 08:59:27 -0700645 auto respHandler =
646 [asyncResp, entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500647 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
648 if (ec)
649 {
George Liu3de8d8b2021-03-22 17:49:39 +0800650 if (ec.value() == EBADR)
651 {
652 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
653 return;
654 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500655 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800656 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500657 messages::internalError(asyncResp->res);
658 return;
659 }
660 };
661 crow::connections::systemBus->async_method_call(
662 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500663 "/xyz/openbmc_project/dump/" +
664 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
665 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500666 "xyz.openbmc_project.Object.Delete", "Delete");
667}
668
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600669inline DumpCreationProgress
670 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500671{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600672 if (status ==
673 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
674 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
675 {
676 return DumpCreationProgress::DUMP_CREATE_FAILED;
677 }
678 if (status ==
679 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
680 {
681 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
682 }
683 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
684}
685
686inline DumpCreationProgress
687 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
688{
689 for (const auto& [key, val] : values)
690 {
691 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700692 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600693 const std::string* value = std::get_if<std::string>(&val);
694 if (value == nullptr)
695 {
696 BMCWEB_LOG_ERROR << "Status property value is null";
697 return DumpCreationProgress::DUMP_CREATE_FAILED;
698 }
699 return mapDbusStatusToDumpProgress(*value);
700 }
701 }
702 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
703}
704
705inline std::string getDumpEntryPath(const std::string& dumpPath)
706{
707 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
708 {
709 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
710 }
711 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
712 {
713 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
714 }
715 return "";
716}
717
718inline void createDumpTaskCallback(
719 task::Payload&& payload,
720 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
721 const sdbusplus::message::object_path& createdObjPath)
722{
723 const std::string dumpPath = createdObjPath.parent_path().str;
724 const std::string dumpId = createdObjPath.filename();
725
726 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
727
728 if (dumpEntryPath.empty())
729 {
730 BMCWEB_LOG_ERROR << "Invalid dump type received";
731 messages::internalError(asyncResp->res);
732 return;
733 }
734
735 crow::connections::systemBus->async_method_call(
736 [asyncResp, payload, createdObjPath,
737 dumpEntryPath{std::move(dumpEntryPath)},
738 dumpId](const boost::system::error_code ec,
739 const std::string& introspectXml) {
740 if (ec)
741 {
742 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
743 << ec.message();
744 messages::internalError(asyncResp->res);
745 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700746 }
747
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600748 // Check if the created dump object has implemented Progress
749 // interface to track dump completion. If yes, fetch the "Status"
750 // property of the interface, modify the task state accordingly.
751 // Else, return task completed.
752 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700753
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600754 doc.Parse(introspectXml.data(), introspectXml.size());
755 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
756 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700757 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600758 BMCWEB_LOG_ERROR << "XML document failed to parse";
759 messages::internalError(asyncResp->res);
760 return;
761 }
762 tinyxml2::XMLElement* interfaceNode =
763 pRoot->FirstChildElement("interface");
764
765 bool isProgressIntfPresent = false;
766 while (interfaceNode != nullptr)
767 {
768 const char* thisInterfaceName = interfaceNode->Attribute("name");
769 if (thisInterfaceName != nullptr)
770 {
771 if (thisInterfaceName ==
772 std::string_view("xyz.openbmc_project.Common.Progress"))
773 {
774 interfaceNode =
775 interfaceNode->NextSiblingElement("interface");
776 continue;
777 }
778 isProgressIntfPresent = true;
779 break;
780 }
781 interfaceNode = interfaceNode->NextSiblingElement("interface");
782 }
783
784 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
785 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Patrick Williams5b378542022-11-26 09:41:59 -0600786 boost::system::error_code err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600787 const std::shared_ptr<task::TaskData>& taskData) {
788 if (err)
789 {
790 BMCWEB_LOG_ERROR << createdObjPath.str
791 << ": Error in creating dump";
792 taskData->messages.emplace_back(messages::internalError());
793 taskData->state = "Cancelled";
794 return task::completed;
795 }
796
797 if (isProgressIntfPresent)
798 {
799 dbus::utility::DBusPropertiesMap values;
800 std::string prop;
801 msg.read(prop, values);
802
803 DumpCreationProgress dumpStatus =
804 getDumpCompletionStatus(values);
805 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
806 {
807 BMCWEB_LOG_ERROR << createdObjPath.str
808 << ": Error in creating dump";
809 taskData->state = "Cancelled";
810 return task::completed;
811 }
812
813 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
814 {
815 BMCWEB_LOG_DEBUG << createdObjPath.str
816 << ": Dump creation task is in progress";
817 return !task::completed;
818 }
819 }
820
Ed Tanous002d39b2022-05-31 08:59:27 -0700821 nlohmann::json retMessage = messages::success();
822 taskData->messages.emplace_back(retMessage);
823
824 std::string headerLoc =
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600825 "Location: " + dumpEntryPath + http_helpers::urlEncode(dumpId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700826 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
827
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600828 BMCWEB_LOG_DEBUG << createdObjPath.str
829 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700830 taskData->state = "Completed";
831 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600832 },
833 "type='signal',interface='org.freedesktop.DBus.Properties',"
834 "member='PropertiesChanged',path='" +
835 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500836
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600837 // The task timer is set to max time limit within which the
838 // requested dump will be collected.
839 task->startTimer(std::chrono::minutes(6));
840 task->populateResp(asyncResp->res);
841 task->payload.emplace(payload);
842 },
843 "xyz.openbmc_project.Dump.Manager", createdObjPath,
844 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500845}
846
zhanghch058d1b46d2021-04-01 11:18:24 +0800847inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
848 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500849{
Claire Weinanfdd26902022-03-01 14:18:25 -0800850 std::string dumpPath = getDumpEntriesPath(dumpType);
851 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500852 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500853 messages::internalError(asyncResp->res);
854 return;
855 }
856
857 std::optional<std::string> diagnosticDataType;
858 std::optional<std::string> oemDiagnosticDataType;
859
Willy Tu15ed6782021-12-14 11:03:16 -0800860 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500861 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
862 "OEMDiagnosticDataType", oemDiagnosticDataType))
863 {
864 return;
865 }
866
867 if (dumpType == "System")
868 {
869 if (!oemDiagnosticDataType || !diagnosticDataType)
870 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800871 BMCWEB_LOG_ERROR
872 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500873 messages::actionParameterMissing(
874 asyncResp->res, "CollectDiagnosticData",
875 "DiagnosticDataType & OEMDiagnosticDataType");
876 return;
877 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700878 if ((*oemDiagnosticDataType != "System") ||
879 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500880 {
881 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700882 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500883 return;
884 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500885 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500886 }
887 else if (dumpType == "BMC")
888 {
889 if (!diagnosticDataType)
890 {
George Liu0fda0f12021-11-16 10:06:17 +0800891 BMCWEB_LOG_ERROR
892 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500893 messages::actionParameterMissing(
894 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
895 return;
896 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700897 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500898 {
899 BMCWEB_LOG_ERROR
900 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700901 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500902 return;
903 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500904 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
905 }
906 else
907 {
908 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
909 messages::internalError(asyncResp->res);
910 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500911 }
912
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600913 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
914 createDumpParamVec;
915
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500916 crow::connections::systemBus->async_method_call(
Patrick Williams5b378542022-11-26 09:41:59 -0600917 [asyncResp, payload(task::Payload(req)), dumpPath](
918 const boost::system::error_code ec, 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(
1195 [asyncResp](const boost::system::error_code ec) {
1196 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;
1241 boost::split(logEntryFields, entry, boost::is_any_of(","),
1242 boost::token_compress_on);
1243 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001244 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001245 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001246 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001247 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001248 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001249
Jason M. Bills4851d452019-03-28 11:27:48 -07001250 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001251 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001252
Sui Chen54417b02022-03-24 14:59:52 -07001253 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001254 {
Sui Chen54417b02022-03-24 14:59:52 -07001255 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001256 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001257 }
1258
Sui Chen54417b02022-03-24 14:59:52 -07001259 std::string msg = message->message;
1260
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001261 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001262 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001263 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001264 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001265 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001266 // If the first string is empty, assume there are no MessageArgs
1267 std::size_t messageArgsSize = 0;
1268 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001269 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001270 messageArgsSize = logEntryFields.size() - 1;
1271 }
1272
Ed Tanous23a21a12020-07-25 04:45:05 +00001273 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001274
1275 // Fill the MessageArgs into the Message
1276 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001277 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001278 {
1279 std::string argStr = "%" + std::to_string(++i);
1280 size_t argPos = msg.find(argStr);
1281 if (argPos != std::string::npos)
1282 {
1283 msg.replace(argPos, argStr.length(), messageArg);
1284 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001285 }
1286 }
1287
Jason M. Bills95820182019-04-22 16:25:34 -07001288 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1289 // format which matches the Redfish format except for the fractional seconds
1290 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001291 std::size_t dot = timestamp.find_first_of('.');
1292 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001293 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001294 {
Jason M. Bills95820182019-04-22 16:25:34 -07001295 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001296 }
1297
1298 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001299 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07001300 logEntryJson["@odata.id"] =
1301 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
1302 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(
1519 [asyncResp](const boost::system::error_code ec,
1520 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";
Ed Tanous002d39b2022-05-31 08:59:27 -07001634 thisEntry["@odata.id"] =
1635 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1636 std::to_string(*id);
1637 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 Tanous002d39b2022-05-31 08:59:27 -07001706 [asyncResp, entryID](const boost::system::error_code ec,
1707 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"] =
1755 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1756 std::to_string(*id);
1757 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1758 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1759 asyncResp->res.jsonValue["Message"] = *message;
1760 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001761 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1762 if (notifyAction)
1763 {
1764 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1765 *notifyAction;
1766 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001767 if ((resolution != nullptr) && (!(*resolution).empty()))
1768 {
1769 asyncResp->res.jsonValue["Resolution"] = *resolution;
1770 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001771 asyncResp->res.jsonValue["EntryType"] = "Event";
1772 asyncResp->res.jsonValue["Severity"] =
1773 translateSeverityDbusToRedfish(*severity);
1774 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001775 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001776 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001777 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001778 if (filePath != nullptr)
1779 {
1780 asyncResp->res.jsonValue["AdditionalDataURI"] =
1781 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1782 std::to_string(*id) + "/attachment";
1783 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001784 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001785 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001786
1787 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001788 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001789 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001790 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001791 [&app](const crow::Request& req,
1792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001793 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001794 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 {
1796 return;
1797 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001798 if (systemName != "system")
1799 {
1800 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1801 systemName);
1802 return;
1803 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001804 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001805
Ed Tanous002d39b2022-05-31 08:59:27 -07001806 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1807 resolved))
1808 {
1809 return;
1810 }
1811 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001812
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 crow::connections::systemBus->async_method_call(
1814 [asyncResp, entryId](const boost::system::error_code ec) {
1815 if (ec)
1816 {
1817 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1818 messages::internalError(asyncResp->res);
1819 return;
1820 }
1821 },
1822 "xyz.openbmc_project.Logging",
1823 "/xyz/openbmc_project/logging/entry/" + entryId,
1824 "org.freedesktop.DBus.Properties", "Set",
1825 "xyz.openbmc_project.Logging.Entry", "Resolved",
1826 dbus::utility::DbusVariantType(*resolved));
1827 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001828
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001829 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001830 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001831 .privileges(redfish::privileges::deleteLogEntry)
1832
Ed Tanous002d39b2022-05-31 08:59:27 -07001833 .methods(boost::beast::http::verb::delete_)(
1834 [&app](const crow::Request& req,
1835 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001836 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001837 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001838 {
1839 return;
1840 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001841 if (systemName != "system")
1842 {
1843 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1844 systemName);
1845 return;
1846 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001847 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1848
1849 std::string entryID = param;
1850
1851 dbus::utility::escapePathForDbus(entryID);
1852
1853 // Process response from Logging service.
1854 auto respHandler =
1855 [asyncResp, entryID](const boost::system::error_code ec) {
1856 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1857 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001858 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001860 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001861 messages::resourceNotFound(asyncResp->res, "LogEntry",
1862 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001863 return;
1864 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001865 // TODO Handle for specific error code
1866 BMCWEB_LOG_ERROR
1867 << "EventLogEntry (DBus) doDelete respHandler got error "
1868 << ec;
1869 asyncResp->res.result(
1870 boost::beast::http::status::internal_server_error);
1871 return;
1872 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001873
Ed Tanous002d39b2022-05-31 08:59:27 -07001874 asyncResp->res.result(boost::beast::http::status::ok);
1875 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001876
Ed Tanous002d39b2022-05-31 08:59:27 -07001877 // Make call to Logging service to request Delete Log
1878 crow::connections::systemBus->async_method_call(
1879 respHandler, "xyz.openbmc_project.Logging",
1880 "/xyz/openbmc_project/logging/entry/" + entryID,
1881 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001882 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001883}
1884
1885inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001886{
George Liu0fda0f12021-11-16 10:06:17 +08001887 BMCWEB_ROUTE(
1888 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001889 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001890 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001891 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001892 [&app](const crow::Request& req,
1893 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001894 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001895 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001896 {
1897 return;
1898 }
Ed Tanous99351cd2022-08-07 16:42:51 -07001899 if (http_helpers::isContentTypeAllowed(
1900 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001901 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001902 {
1903 asyncResp->res.result(boost::beast::http::status::bad_request);
1904 return;
1905 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001906 if (systemName != "system")
1907 {
1908 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1909 systemName);
1910 return;
1911 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001912
Ed Tanous002d39b2022-05-31 08:59:27 -07001913 std::string entryID = param;
1914 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001915
Ed Tanous002d39b2022-05-31 08:59:27 -07001916 crow::connections::systemBus->async_method_call(
1917 [asyncResp, entryID](const boost::system::error_code ec,
1918 const sdbusplus::message::unix_fd& unixfd) {
1919 if (ec.value() == EBADR)
1920 {
1921 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1922 entryID);
1923 return;
1924 }
1925 if (ec)
1926 {
1927 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1928 messages::internalError(asyncResp->res);
1929 return;
1930 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001931
Ed Tanous002d39b2022-05-31 08:59:27 -07001932 int fd = -1;
1933 fd = dup(unixfd);
1934 if (fd == -1)
1935 {
1936 messages::internalError(asyncResp->res);
1937 return;
1938 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001939
Ed Tanous002d39b2022-05-31 08:59:27 -07001940 long long int size = lseek(fd, 0, SEEK_END);
1941 if (size == -1)
1942 {
1943 messages::internalError(asyncResp->res);
1944 return;
1945 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001946
Ed Tanous002d39b2022-05-31 08:59:27 -07001947 // Arbitrary max size of 64kb
1948 constexpr int maxFileSize = 65536;
1949 if (size > maxFileSize)
1950 {
1951 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1952 << maxFileSize;
1953 messages::internalError(asyncResp->res);
1954 return;
1955 }
1956 std::vector<char> data(static_cast<size_t>(size));
1957 long long int rc = lseek(fd, 0, SEEK_SET);
1958 if (rc == -1)
1959 {
1960 messages::internalError(asyncResp->res);
1961 return;
1962 }
1963 rc = read(fd, data.data(), data.size());
1964 if ((rc == -1) || (rc != size))
1965 {
1966 messages::internalError(asyncResp->res);
1967 return;
1968 }
1969 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001970
Ed Tanous002d39b2022-05-31 08:59:27 -07001971 std::string_view strData(data.data(), data.size());
1972 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001973
Ed Tanousd9f6c622022-03-17 09:12:17 -07001974 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07001975 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07001976 asyncResp->res.addHeader(
1977 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07001978 asyncResp->res.body() = std::move(output);
1979 },
1980 "xyz.openbmc_project.Logging",
1981 "/xyz/openbmc_project/logging/entry/" + entryID,
1982 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1983 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001984}
1985
Spencer Kub7028eb2021-10-26 15:27:35 +08001986constexpr const char* hostLoggerFolderPath = "/var/log/console";
1987
1988inline bool
1989 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1990 std::vector<std::filesystem::path>& hostLoggerFiles)
1991{
1992 std::error_code ec;
1993 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1994 if (ec)
1995 {
1996 BMCWEB_LOG_ERROR << ec.message();
1997 return false;
1998 }
1999 for (const std::filesystem::directory_entry& it : logPath)
2000 {
2001 std::string filename = it.path().filename();
2002 // Prefix of each log files is "log". Find the file and save the
2003 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002004 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002005 {
2006 hostLoggerFiles.emplace_back(it.path());
2007 }
2008 }
2009 // As the log files rotate, they are appended with a ".#" that is higher for
2010 // the older logs. Since we start from oldest logs, sort the name in
2011 // descending order.
2012 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2013 AlphanumLess<std::string>());
2014
2015 return true;
2016}
2017
Ed Tanous02cad962022-06-30 16:50:15 -07002018inline bool getHostLoggerEntries(
2019 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2020 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002021{
2022 GzFileReader logFile;
2023
2024 // Go though all log files and expose host logs.
2025 for (const std::filesystem::path& it : hostLoggerFiles)
2026 {
2027 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2028 {
2029 BMCWEB_LOG_ERROR << "fail to expose host logs";
2030 return false;
2031 }
2032 }
2033 // Get lastMessage from constructor by getter
2034 std::string lastMessage = logFile.getLastMessage();
2035 if (!lastMessage.empty())
2036 {
2037 logCount++;
2038 if (logCount > skip && logCount <= (skip + top))
2039 {
2040 logEntries.push_back(lastMessage);
2041 }
2042 }
2043 return true;
2044}
2045
2046inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2047 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002048 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002049{
2050 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002051 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002052 logEntryJson["@odata.id"] =
2053 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
2054 logEntryID;
2055 logEntryJson["Name"] = "Host Logger Entry";
2056 logEntryJson["Id"] = logEntryID;
2057 logEntryJson["Message"] = msg;
2058 logEntryJson["EntryType"] = "Oem";
2059 logEntryJson["Severity"] = "OK";
2060 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002061}
2062
2063inline void requestRoutesSystemHostLogger(App& app)
2064{
Ed Tanous22d268c2022-05-19 09:39:07 -07002065 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002066 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002067 .methods(boost::beast::http::verb::get)(
2068 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002069 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2070 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002071 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002072 {
2073 return;
2074 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002075 if (systemName != "system")
2076 {
2077 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2078 systemName);
2079 return;
2080 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002081 asyncResp->res.jsonValue["@odata.id"] =
2082 "/redfish/v1/Systems/system/LogServices/HostLogger";
2083 asyncResp->res.jsonValue["@odata.type"] =
2084 "#LogService.v1_1_0.LogService";
2085 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2086 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2087 asyncResp->res.jsonValue["Id"] = "HostLogger";
2088 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2089 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2090 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002091}
2092
2093inline void requestRoutesSystemHostLoggerCollection(App& app)
2094{
2095 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002096 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002097 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002098 .methods(boost::beast::http::verb::get)(
2099 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002100 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2101 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002102 query_param::QueryCapabilities capabilities = {
2103 .canDelegateTop = true,
2104 .canDelegateSkip = true,
2105 };
2106 query_param::Query delegatedQuery;
2107 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002108 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002109 {
2110 return;
2111 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002112 if (systemName != "system")
2113 {
2114 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2115 systemName);
2116 return;
2117 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002118 asyncResp->res.jsonValue["@odata.id"] =
2119 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2120 asyncResp->res.jsonValue["@odata.type"] =
2121 "#LogEntryCollection.LogEntryCollection";
2122 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2123 asyncResp->res.jsonValue["Description"] =
2124 "Collection of HostLogger Entries";
2125 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2126 logEntryArray = nlohmann::json::array();
2127 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002128
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 std::vector<std::filesystem::path> hostLoggerFiles;
2130 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2131 {
2132 BMCWEB_LOG_ERROR << "fail to get host log file path";
2133 return;
2134 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002135 // If we weren't provided top and skip limits, use the defaults.
2136 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002137 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002138 size_t logCount = 0;
2139 // This vector only store the entries we want to expose that
2140 // control by skip and top.
2141 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002142 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2143 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 {
2145 messages::internalError(asyncResp->res);
2146 return;
2147 }
2148 // If vector is empty, that means skip value larger than total
2149 // log count
2150 if (logEntries.empty())
2151 {
2152 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2153 return;
2154 }
2155 if (!logEntries.empty())
2156 {
2157 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002158 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002159 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002160 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2161 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002162 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002163 }
2164
Ed Tanous002d39b2022-05-31 08:59:27 -07002165 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002166 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002167 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002168 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2169 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002170 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002171 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002172 }
George Liu0fda0f12021-11-16 10:06:17 +08002173 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002174}
2175
2176inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2177{
2178 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002179 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002180 .privileges(redfish::privileges::getLogEntry)
2181 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002182 [&app](const crow::Request& req,
2183 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002184 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002185 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 {
2187 return;
2188 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002189 if (systemName != "system")
2190 {
2191 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2192 systemName);
2193 return;
2194 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002195 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002196
Ed Tanous002d39b2022-05-31 08:59:27 -07002197 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002198
Ed Tanous002d39b2022-05-31 08:59:27 -07002199 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2200 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08002201
Ed Tanous002d39b2022-05-31 08:59:27 -07002202 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002203 if (ec == std::errc::invalid_argument ||
2204 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002205 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002206 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002207 return;
2208 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002209
Ed Tanous002d39b2022-05-31 08:59:27 -07002210 std::vector<std::filesystem::path> hostLoggerFiles;
2211 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2212 {
2213 BMCWEB_LOG_ERROR << "fail to get host log file path";
2214 return;
2215 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002216
Ed Tanous002d39b2022-05-31 08:59:27 -07002217 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002218 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002219 std::vector<std::string> logEntries;
2220 // We can get specific entry by skip and top. For example, if we
2221 // want to get nth entry, we can set skip = n-1 and top = 1 to
2222 // get that entry
2223 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2224 logCount))
2225 {
2226 messages::internalError(asyncResp->res);
2227 return;
2228 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002229
Ed Tanous002d39b2022-05-31 08:59:27 -07002230 if (!logEntries.empty())
2231 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002232 nlohmann::json::object_t hostLogEntry;
2233 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2234 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002235 return;
2236 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002237
Ed Tanous002d39b2022-05-31 08:59:27 -07002238 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002239 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002240 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002241}
2242
Claire Weinandd72e872022-08-15 14:20:06 -07002243inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002244 crow::App& app, const crow::Request& req,
2245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2246{
2247 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2248 {
2249 return;
2250 }
2251 // Collections don't include the static data added by SubRoute
2252 // because it has a duplicate entry for members
2253 asyncResp->res.jsonValue["@odata.type"] =
2254 "#LogServiceCollection.LogServiceCollection";
2255 asyncResp->res.jsonValue["@odata.id"] =
2256 "/redfish/v1/Managers/bmc/LogServices";
2257 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2258 asyncResp->res.jsonValue["Description"] =
2259 "Collection of LogServices for this Manager";
2260 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2261 logServiceArray = nlohmann::json::array();
2262
2263#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002264 nlohmann::json::object_t journal;
2265 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2266 logServiceArray.push_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002267#endif
2268
2269 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2270
2271#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002272 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002273 "xyz.openbmc_project.Collection.DeleteAll"};
2274 dbus::utility::getSubTreePaths(
2275 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002276 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002277 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002278 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2279 if (ec)
2280 {
2281 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002282 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002283 << ec;
2284 // Assume that getting an error simply means there are no dump
2285 // LogServices. Return without adding any error response.
2286 return;
2287 }
2288
2289 nlohmann::json& logServiceArrayLocal =
2290 asyncResp->res.jsonValue["Members"];
2291
2292 for (const std::string& path : subTreePaths)
2293 {
2294 if (path == "/xyz/openbmc_project/dump/bmc")
2295 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002296 nlohmann::json::object_t member;
2297 member["@odata.id"] =
2298 "/redfish/v1/Managers/bmc/LogServices/Dump";
2299 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002300 }
2301 else if (path == "/xyz/openbmc_project/dump/faultlog")
2302 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002303 nlohmann::json::object_t member;
2304 member["@odata.id"] =
2305 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2306 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002307 }
2308 }
2309
2310 asyncResp->res.jsonValue["Members@odata.count"] =
2311 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002312 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002313#endif
2314}
2315
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002316inline void requestRoutesBMCLogServiceCollection(App& app)
2317{
2318 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002319 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002320 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002321 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002322}
Ed Tanous1da66f72018-07-27 16:13:37 -07002323
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002324inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002325{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002326 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002327 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002328 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002329 [&app](const crow::Request& req,
2330 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002331 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002332 {
2333 return;
2334 }
2335 asyncResp->res.jsonValue["@odata.type"] =
2336 "#LogService.v1_1_0.LogService";
2337 asyncResp->res.jsonValue["@odata.id"] =
2338 "/redfish/v1/Managers/bmc/LogServices/Journal";
2339 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2340 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002341 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002342 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302343
Ed Tanous002d39b2022-05-31 08:59:27 -07002344 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002345 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002346 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2347 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2348 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302349
Ed Tanous002d39b2022-05-31 08:59:27 -07002350 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2351 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2352 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002353}
Jason M. Billse1f26342018-07-18 12:12:00 -07002354
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002355static int
2356 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2357 sd_journal* journal,
2358 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002359{
2360 // Get the Log Entry contents
2361 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002362
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002363 std::string message;
2364 std::string_view syslogID;
2365 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2366 if (ret < 0)
2367 {
2368 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2369 << strerror(-ret);
2370 }
2371 if (!syslogID.empty())
2372 {
2373 message += std::string(syslogID) + ": ";
2374 }
2375
Ed Tanous39e77502019-03-04 17:35:53 -08002376 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002377 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002378 if (ret < 0)
2379 {
2380 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2381 return 1;
2382 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002383 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002384
2385 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002386 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002387 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002388 if (ret < 0)
2389 {
2390 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002391 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002392
2393 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002394 std::string entryTimeStr;
2395 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002396 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002397 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002398 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002399
2400 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002401 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07002402 bmcJournalLogEntryJson["@odata.id"] =
2403 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2404 bmcJournalLogEntryID;
2405 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2406 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2407 bmcJournalLogEntryJson["Message"] = std::move(message);
2408 bmcJournalLogEntryJson["EntryType"] = "Oem";
2409 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2410 : severity <= 4 ? "Warning"
2411 : "OK";
2412 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2413 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002414 return 0;
2415}
2416
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002417inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002418{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002419 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002420 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002421 .methods(boost::beast::http::verb::get)(
2422 [&app](const crow::Request& req,
2423 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2424 query_param::QueryCapabilities capabilities = {
2425 .canDelegateTop = true,
2426 .canDelegateSkip = true,
2427 };
2428 query_param::Query delegatedQuery;
2429 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002430 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002431 {
2432 return;
2433 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002434
2435 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002436 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002437
Ed Tanous002d39b2022-05-31 08:59:27 -07002438 // Collections don't include the static data added by SubRoute
2439 // because it has a duplicate entry for members
2440 asyncResp->res.jsonValue["@odata.type"] =
2441 "#LogEntryCollection.LogEntryCollection";
2442 asyncResp->res.jsonValue["@odata.id"] =
2443 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2444 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2445 asyncResp->res.jsonValue["Description"] =
2446 "Collection of BMC Journal Entries";
2447 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2448 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002449
Ed Tanous002d39b2022-05-31 08:59:27 -07002450 // Go through the journal and use the timestamp to create a
2451 // unique ID for each entry
2452 sd_journal* journalTmp = nullptr;
2453 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2454 if (ret < 0)
2455 {
2456 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2457 messages::internalError(asyncResp->res);
2458 return;
2459 }
2460 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2461 journalTmp, sd_journal_close);
2462 journalTmp = nullptr;
2463 uint64_t entryCount = 0;
2464 // Reset the unique ID on the first entry
2465 bool firstEntry = true;
2466 SD_JOURNAL_FOREACH(journal.get())
2467 {
2468 entryCount++;
2469 // Handle paging using skip (number of entries to skip from
2470 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002471 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002472 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002473 continue;
2474 }
2475
2476 std::string idStr;
2477 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2478 {
2479 continue;
2480 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002481 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002482
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002483 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002484 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2485 bmcJournalLogEntry) != 0)
2486 {
George Liu0fda0f12021-11-16 10:06:17 +08002487 messages::internalError(asyncResp->res);
2488 return;
2489 }
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002490 logEntryArray.push_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002491 }
2492 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002493 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002494 {
2495 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2496 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002497 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002498 }
George Liu0fda0f12021-11-16 10:06:17 +08002499 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002500}
Jason M. Billse1f26342018-07-18 12:12:00 -07002501
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002502inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002503{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002504 BMCWEB_ROUTE(app,
2505 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002506 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002507 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002508 [&app](const crow::Request& req,
2509 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2510 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002511 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002512 {
2513 return;
2514 }
2515 // Convert the unique ID back to a timestamp to find the entry
2516 uint64_t ts = 0;
2517 uint64_t index = 0;
2518 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2519 {
2520 return;
2521 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002522
Ed Tanous002d39b2022-05-31 08:59:27 -07002523 sd_journal* journalTmp = nullptr;
2524 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2525 if (ret < 0)
2526 {
2527 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2528 messages::internalError(asyncResp->res);
2529 return;
2530 }
2531 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2532 journalTmp, sd_journal_close);
2533 journalTmp = nullptr;
2534 // Go to the timestamp in the log and move to the entry at the
2535 // index tracking the unique ID
2536 std::string idStr;
2537 bool firstEntry = true;
2538 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2539 if (ret < 0)
2540 {
2541 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2542 << strerror(-ret);
2543 messages::internalError(asyncResp->res);
2544 return;
2545 }
2546 for (uint64_t i = 0; i <= index; i++)
2547 {
2548 sd_journal_next(journal.get());
2549 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2550 {
2551 messages::internalError(asyncResp->res);
2552 return;
2553 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002554 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002555 }
2556 // Confirm that the entry ID matches what was requested
2557 if (idStr != entryID)
2558 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002559 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002560 return;
2561 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002562
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002563 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002564 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002565 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002566 {
2567 messages::internalError(asyncResp->res);
2568 return;
2569 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002570 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002571 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002572}
2573
Claire Weinanfdd26902022-03-01 14:18:25 -08002574inline void
2575 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2576 const std::string& dumpType)
2577{
2578 std::string dumpPath;
2579 std::string overWritePolicy;
2580 bool collectDiagnosticDataSupported = false;
2581
2582 if (dumpType == "BMC")
2583 {
2584 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2585 overWritePolicy = "WrapsWhenFull";
2586 collectDiagnosticDataSupported = true;
2587 }
2588 else if (dumpType == "FaultLog")
2589 {
2590 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2591 overWritePolicy = "Unknown";
2592 collectDiagnosticDataSupported = false;
2593 }
2594 else if (dumpType == "System")
2595 {
2596 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2597 overWritePolicy = "WrapsWhenFull";
2598 collectDiagnosticDataSupported = true;
2599 }
2600 else
2601 {
2602 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2603 << dumpType;
2604 messages::internalError(asyncResp->res);
2605 return;
2606 }
2607
2608 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2609 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2610 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2611 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2612 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2613 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2614
2615 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002616 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002617 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2618 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2619 redfishDateTimeOffset.second;
2620
2621 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002622
2623 if (collectDiagnosticDataSupported)
2624 {
2625 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2626 ["target"] =
2627 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2628 }
Claire Weinan0d946212022-07-13 19:40:19 -07002629
2630 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2631 dbus::utility::getSubTreePaths(
2632 "/xyz/openbmc_project/dump", 0, interfaces,
2633 [asyncResp, dumpType, dumpPath](
2634 const boost::system::error_code& ec,
2635 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2636 if (ec)
2637 {
2638 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2639 << ec;
2640 // Assume that getting an error simply means there are no dump
2641 // LogServices. Return without adding any error response.
2642 return;
2643 }
2644
2645 const std::string dbusDumpPath =
2646 "/xyz/openbmc_project/dump/" +
2647 boost::algorithm::to_lower_copy(dumpType);
2648
2649 for (const std::string& path : subTreePaths)
2650 {
2651 if (path == dbusDumpPath)
2652 {
2653 asyncResp->res
2654 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2655 dumpPath + "/Actions/LogService.ClearLog";
2656 break;
2657 }
2658 }
2659 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002660}
2661
2662inline void handleLogServicesDumpServiceGet(
2663 crow::App& app, const std::string& dumpType, const crow::Request& req,
2664 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2665{
2666 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2667 {
2668 return;
2669 }
2670 getDumpServiceInfo(asyncResp, dumpType);
2671}
2672
Ed Tanous22d268c2022-05-19 09:39:07 -07002673inline void handleLogServicesDumpServiceComputerSystemGet(
2674 crow::App& app, const crow::Request& req,
2675 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2676 const std::string& chassisId)
2677{
2678 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2679 {
2680 return;
2681 }
2682 if (chassisId != "system")
2683 {
2684 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2685 return;
2686 }
2687 getDumpServiceInfo(asyncResp, "System");
2688}
2689
Claire Weinanfdd26902022-03-01 14:18:25 -08002690inline void handleLogServicesDumpEntriesCollectionGet(
2691 crow::App& app, const std::string& dumpType, const crow::Request& req,
2692 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2693{
2694 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2695 {
2696 return;
2697 }
2698 getDumpEntryCollection(asyncResp, dumpType);
2699}
2700
Ed Tanous22d268c2022-05-19 09:39:07 -07002701inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2702 crow::App& app, const crow::Request& req,
2703 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2704 const std::string& chassisId)
2705{
2706 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2707 {
2708 return;
2709 }
2710 if (chassisId != "system")
2711 {
2712 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2713 return;
2714 }
2715 getDumpEntryCollection(asyncResp, "System");
2716}
2717
Claire Weinanfdd26902022-03-01 14:18:25 -08002718inline void handleLogServicesDumpEntryGet(
2719 crow::App& app, const std::string& dumpType, const crow::Request& req,
2720 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2721 const std::string& dumpId)
2722{
2723 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2724 {
2725 return;
2726 }
2727 getDumpEntryById(asyncResp, dumpId, dumpType);
2728}
Ed Tanous22d268c2022-05-19 09:39:07 -07002729inline void handleLogServicesDumpEntryComputerSystemGet(
2730 crow::App& app, const crow::Request& req,
2731 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2732 const std::string& chassisId, const std::string& dumpId)
2733{
2734 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2735 {
2736 return;
2737 }
2738 if (chassisId != "system")
2739 {
2740 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2741 return;
2742 }
2743 getDumpEntryById(asyncResp, dumpId, "System");
2744}
Claire Weinanfdd26902022-03-01 14:18:25 -08002745
2746inline void handleLogServicesDumpEntryDelete(
2747 crow::App& app, const std::string& dumpType, const crow::Request& req,
2748 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2749 const std::string& dumpId)
2750{
2751 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2752 {
2753 return;
2754 }
2755 deleteDumpEntry(asyncResp, dumpId, dumpType);
2756}
2757
Ed Tanous22d268c2022-05-19 09:39:07 -07002758inline void handleLogServicesDumpEntryComputerSystemDelete(
2759 crow::App& app, const crow::Request& req,
2760 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2761 const std::string& chassisId, const std::string& dumpId)
2762{
2763 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2764 {
2765 return;
2766 }
2767 if (chassisId != "system")
2768 {
2769 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2770 return;
2771 }
2772 deleteDumpEntry(asyncResp, dumpId, "System");
2773}
2774
Claire Weinanfdd26902022-03-01 14:18:25 -08002775inline void handleLogServicesDumpCollectDiagnosticDataPost(
2776 crow::App& app, const std::string& dumpType, const crow::Request& req,
2777 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2778{
2779 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2780 {
2781 return;
2782 }
2783 createDump(asyncResp, req, dumpType);
2784}
2785
Ed Tanous22d268c2022-05-19 09:39:07 -07002786inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2787 crow::App& app, const crow::Request& req,
2788 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2789 const std::string& chassisId)
2790{
2791 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2792 {
2793 return;
2794 }
2795 if (chassisId != "system")
2796 {
2797 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2798 return;
2799 }
2800 createDump(asyncResp, req, "System");
2801}
2802
Claire Weinanfdd26902022-03-01 14:18:25 -08002803inline void handleLogServicesDumpClearLogPost(
2804 crow::App& app, const std::string& dumpType, const crow::Request& req,
2805 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2806{
2807 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2808 {
2809 return;
2810 }
2811 clearDump(asyncResp, dumpType);
2812}
2813
Ed Tanous22d268c2022-05-19 09:39:07 -07002814inline void handleLogServicesDumpClearLogComputerSystemPost(
2815 crow::App& app, const crow::Request& req,
2816 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2817 const std::string& chassisId)
2818{
2819 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2820 {
2821 return;
2822 }
2823 if (chassisId != "system")
2824 {
2825 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2826 return;
2827 }
2828 clearDump(asyncResp, "System");
2829}
2830
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002831inline void requestRoutesBMCDumpService(App& app)
2832{
2833 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002834 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002835 .methods(boost::beast::http::verb::get)(std::bind_front(
2836 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002837}
2838
2839inline void requestRoutesBMCDumpEntryCollection(App& app)
2840{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002841 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002842 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002843 .methods(boost::beast::http::verb::get)(std::bind_front(
2844 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002845}
2846
2847inline void requestRoutesBMCDumpEntry(App& app)
2848{
2849 BMCWEB_ROUTE(app,
2850 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002851 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002852 .methods(boost::beast::http::verb::get)(std::bind_front(
2853 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2854
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002855 BMCWEB_ROUTE(app,
2856 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002857 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002858 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2859 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002860}
2861
2862inline void requestRoutesBMCDumpCreate(App& app)
2863{
George Liu0fda0f12021-11-16 10:06:17 +08002864 BMCWEB_ROUTE(
2865 app,
2866 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002867 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002868 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002869 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2870 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002871}
2872
2873inline void requestRoutesBMCDumpClear(App& app)
2874{
George Liu0fda0f12021-11-16 10:06:17 +08002875 BMCWEB_ROUTE(
2876 app,
2877 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002878 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002879 .methods(boost::beast::http::verb::post)(std::bind_front(
2880 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2881}
2882
2883inline void requestRoutesFaultLogDumpService(App& app)
2884{
2885 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2886 .privileges(redfish::privileges::getLogService)
2887 .methods(boost::beast::http::verb::get)(std::bind_front(
2888 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2889}
2890
2891inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2892{
2893 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2894 .privileges(redfish::privileges::getLogEntryCollection)
2895 .methods(boost::beast::http::verb::get)(
2896 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2897 std::ref(app), "FaultLog"));
2898}
2899
2900inline void requestRoutesFaultLogDumpEntry(App& app)
2901{
2902 BMCWEB_ROUTE(app,
2903 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2904 .privileges(redfish::privileges::getLogEntry)
2905 .methods(boost::beast::http::verb::get)(std::bind_front(
2906 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2907
2908 BMCWEB_ROUTE(app,
2909 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2910 .privileges(redfish::privileges::deleteLogEntry)
2911 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2912 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2913}
2914
2915inline void requestRoutesFaultLogDumpClear(App& app)
2916{
2917 BMCWEB_ROUTE(
2918 app,
2919 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2920 .privileges(redfish::privileges::postLogService)
2921 .methods(boost::beast::http::verb::post)(std::bind_front(
2922 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002923}
2924
2925inline void requestRoutesSystemDumpService(App& app)
2926{
Ed Tanous22d268c2022-05-19 09:39:07 -07002927 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002928 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002929 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002930 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002931}
2932
2933inline void requestRoutesSystemDumpEntryCollection(App& app)
2934{
Ed Tanous22d268c2022-05-19 09:39:07 -07002935 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002936 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002937 .methods(boost::beast::http::verb::get)(std::bind_front(
2938 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2939 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002940}
2941
2942inline void requestRoutesSystemDumpEntry(App& app)
2943{
2944 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002945 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002946 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002947 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002948 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002949
2950 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002951 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002952 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002953 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002954 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002955}
2956
2957inline void requestRoutesSystemDumpCreate(App& app)
2958{
George Liu0fda0f12021-11-16 10:06:17 +08002959 BMCWEB_ROUTE(
2960 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002961 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002962 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07002963 .methods(boost::beast::http::verb::post)(std::bind_front(
2964 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
2965 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002966}
2967
2968inline void requestRoutesSystemDumpClear(App& app)
2969{
George Liu0fda0f12021-11-16 10:06:17 +08002970 BMCWEB_ROUTE(
2971 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002972 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002973 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002974 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002975 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002976}
2977
2978inline void requestRoutesCrashdumpService(App& app)
2979{
2980 // Note: Deviated from redfish privilege registry for GET & HEAD
2981 // method for security reasons.
2982 /**
2983 * Functions triggers appropriate requests on DBus
2984 */
Ed Tanous22d268c2022-05-19 09:39:07 -07002985 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002986 // This is incorrect, should be:
2987 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002988 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002989 .methods(boost::beast::http::verb::get)(
2990 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002991 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2992 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002993 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002994 {
2995 return;
2996 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002997 if (systemName != "system")
2998 {
2999 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3000 systemName);
3001 return;
3002 }
3003
Ed Tanous002d39b2022-05-31 08:59:27 -07003004 // Copy over the static data to include the entries added by
3005 // SubRoute
3006 asyncResp->res.jsonValue["@odata.id"] =
3007 "/redfish/v1/Systems/system/LogServices/Crashdump";
3008 asyncResp->res.jsonValue["@odata.type"] =
3009 "#LogService.v1_2_0.LogService";
3010 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3011 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
3012 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
3013 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3014 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303015
Ed Tanous002d39b2022-05-31 08:59:27 -07003016 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003017 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003018 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3019 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3020 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303021
Ed Tanous002d39b2022-05-31 08:59:27 -07003022 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3023 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3024 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3025 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3026 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3027 ["target"] =
3028 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003029 });
3030}
3031
3032void inline requestRoutesCrashdumpClear(App& app)
3033{
George Liu0fda0f12021-11-16 10:06:17 +08003034 BMCWEB_ROUTE(
3035 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003036 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003037 // This is incorrect, should be:
3038 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003039 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003040 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003041 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003042 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3043 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003044 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003045 {
3046 return;
3047 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003048 if (systemName != "system")
3049 {
3050 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3051 systemName);
3052 return;
3053 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003054 crow::connections::systemBus->async_method_call(
3055 [asyncResp](const boost::system::error_code ec,
3056 const std::string&) {
3057 if (ec)
3058 {
3059 messages::internalError(asyncResp->res);
3060 return;
3061 }
3062 messages::success(asyncResp->res);
3063 },
3064 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3065 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003066}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003067
zhanghch058d1b46d2021-04-01 11:18:24 +08003068static void
3069 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3070 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003071{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003072 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003073 [asyncResp, logID,
3074 &logEntryJson](const boost::system::error_code ec,
3075 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003076 if (ec)
3077 {
3078 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3079 if (ec.value() ==
3080 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003081 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003082 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003083 }
3084 else
3085 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003086 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003087 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003088 return;
3089 }
3090
3091 std::string timestamp{};
3092 std::string filename{};
3093 std::string logfile{};
3094 parseCrashdumpParameters(params, filename, timestamp, logfile);
3095
3096 if (filename.empty() || timestamp.empty())
3097 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003098 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003099 return;
3100 }
3101
3102 std::string crashdumpURI =
3103 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3104 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003105 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003106 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07003107 logEntry["@odata.id"] =
3108 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
3109 logEntry["Name"] = "CPU Crashdump";
3110 logEntry["Id"] = logID;
3111 logEntry["EntryType"] = "Oem";
3112 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3113 logEntry["DiagnosticDataType"] = "OEM";
3114 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3115 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003116
3117 // If logEntryJson references an array of LogEntry resources
3118 // ('Members' list), then push this as a new entry, otherwise set it
3119 // directly
3120 if (logEntryJson.is_array())
3121 {
3122 logEntryJson.push_back(logEntry);
3123 asyncResp->res.jsonValue["Members@odata.count"] =
3124 logEntryJson.size();
3125 }
3126 else
3127 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003128 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003129 }
3130 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003131 sdbusplus::asio::getAllProperties(
3132 *crow::connections::systemBus, crashdumpObject,
3133 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3134 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003135}
3136
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003137inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003138{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003139 // Note: Deviated from redfish privilege registry for GET & HEAD
3140 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003141 /**
3142 * Functions triggers appropriate requests on DBus
3143 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003144 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003145 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003146 // This is incorrect, should be.
3147 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003148 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003149 .methods(boost::beast::http::verb::get)(
3150 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003151 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3152 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003153 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003154 {
3155 return;
3156 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003157 if (systemName != "system")
3158 {
3159 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3160 systemName);
3161 return;
3162 }
3163
George Liu7a1dbc42022-12-07 16:03:22 +08003164 constexpr std::array<std::string_view, 1> interfaces = {
3165 crashdumpInterface};
3166 dbus::utility::getSubTreePaths(
3167 "/", 0, interfaces,
3168 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003169 const std::vector<std::string>& resp) {
3170 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003171 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003172 if (ec.value() !=
3173 boost::system::errc::no_such_file_or_directory)
3174 {
3175 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3176 << ec.message();
3177 messages::internalError(asyncResp->res);
3178 return;
3179 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003180 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003181 asyncResp->res.jsonValue["@odata.type"] =
3182 "#LogEntryCollection.LogEntryCollection";
3183 asyncResp->res.jsonValue["@odata.id"] =
3184 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3185 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3186 asyncResp->res.jsonValue["Description"] =
3187 "Collection of Crashdump Entries";
3188 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3189 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003190
Ed Tanous002d39b2022-05-31 08:59:27 -07003191 for (const std::string& path : resp)
3192 {
3193 const sdbusplus::message::object_path objPath(path);
3194 // Get the log ID
3195 std::string logID = objPath.filename();
3196 if (logID.empty())
3197 {
3198 continue;
3199 }
3200 // Add the log entry to the array
3201 logCrashdumpEntry(asyncResp, logID,
3202 asyncResp->res.jsonValue["Members"]);
3203 }
George Liu7a1dbc42022-12-07 16:03:22 +08003204 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003205 });
3206}
Ed Tanous1da66f72018-07-27 16:13:37 -07003207
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003208inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003209{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003210 // Note: Deviated from redfish privilege registry for GET & HEAD
3211 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003212
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003213 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003214 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003215 // this is incorrect, should be
3216 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003217 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003218 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003219 [&app](const crow::Request& req,
3220 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003221 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003222 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003223 {
3224 return;
3225 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003226 if (systemName != "system")
3227 {
3228 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3229 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003230 return;
3231 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003232 const std::string& logID = param;
3233 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3234 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003235}
Ed Tanous1da66f72018-07-27 16:13:37 -07003236
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003237inline void requestRoutesCrashdumpFile(App& app)
3238{
3239 // Note: Deviated from redfish privilege registry for GET & HEAD
3240 // method for security reasons.
3241 BMCWEB_ROUTE(
3242 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003243 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003244 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003245 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003246 [](const crow::Request& req,
3247 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003248 const std::string& systemName, const std::string& logID,
3249 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003250 // Do not call getRedfishRoute here since the crashdump file is not a
3251 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003252
3253 if (systemName != "system")
3254 {
3255 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3256 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003257 return;
3258 }
3259
Ed Tanous002d39b2022-05-31 08:59:27 -07003260 auto getStoredLogCallback =
3261 [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3262 const boost::system::error_code ec,
3263 const std::vector<
3264 std::pair<std::string, dbus::utility::DbusVariantType>>&
3265 resp) {
3266 if (ec)
3267 {
3268 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3269 messages::internalError(asyncResp->res);
3270 return;
3271 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003272
Ed Tanous002d39b2022-05-31 08:59:27 -07003273 std::string dbusFilename{};
3274 std::string dbusTimestamp{};
3275 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003276
Ed Tanous002d39b2022-05-31 08:59:27 -07003277 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3278 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003279
Ed Tanous002d39b2022-05-31 08:59:27 -07003280 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3281 dbusFilepath.empty())
3282 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003283 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003284 return;
3285 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003286
Ed Tanous002d39b2022-05-31 08:59:27 -07003287 // Verify the file name parameter is correct
3288 if (fileName != dbusFilename)
3289 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003290 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003291 return;
3292 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003293
Ed Tanous002d39b2022-05-31 08:59:27 -07003294 if (!std::filesystem::exists(dbusFilepath))
3295 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003296 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003297 return;
3298 }
3299 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3300 asyncResp->res.body() =
3301 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003302
Ed Tanous002d39b2022-05-31 08:59:27 -07003303 // Configure this to be a file download when accessed
3304 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003305 asyncResp->res.addHeader(
3306 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003307 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003308 sdbusplus::asio::getAllProperties(
3309 *crow::connections::systemBus, crashdumpObject,
3310 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3311 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003312 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003313}
3314
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003315enum class OEMDiagnosticType
3316{
3317 onDemand,
3318 telemetry,
3319 invalid,
3320};
3321
Ed Tanousf7725d72022-03-07 12:46:00 -08003322inline OEMDiagnosticType
3323 getOEMDiagnosticType(const std::string_view& oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003324{
3325 if (oemDiagStr == "OnDemand")
3326 {
3327 return OEMDiagnosticType::onDemand;
3328 }
3329 if (oemDiagStr == "Telemetry")
3330 {
3331 return OEMDiagnosticType::telemetry;
3332 }
3333
3334 return OEMDiagnosticType::invalid;
3335}
3336
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003337inline void requestRoutesCrashdumpCollect(App& app)
3338{
3339 // Note: Deviated from redfish privilege registry for GET & HEAD
3340 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003341 BMCWEB_ROUTE(
3342 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003343 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003344 // The below is incorrect; Should be ConfigureManager
3345 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003346 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003347 .methods(boost::beast::http::verb::post)(
3348 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003349 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3350 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003351 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003352 {
3353 return;
3354 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003355
3356 if (systemName != "system")
3357 {
3358 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3359 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003360 return;
3361 }
3362
Ed Tanous002d39b2022-05-31 08:59:27 -07003363 std::string diagnosticDataType;
3364 std::string oemDiagnosticDataType;
3365 if (!redfish::json_util::readJsonAction(
3366 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3367 "OEMDiagnosticDataType", oemDiagnosticDataType))
3368 {
3369 return;
3370 }
3371
3372 if (diagnosticDataType != "OEM")
3373 {
3374 BMCWEB_LOG_ERROR
3375 << "Only OEM DiagnosticDataType supported for Crashdump";
3376 messages::actionParameterValueFormatError(
3377 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3378 "CollectDiagnosticData");
3379 return;
3380 }
3381
3382 OEMDiagnosticType oemDiagType =
3383 getOEMDiagnosticType(oemDiagnosticDataType);
3384
3385 std::string iface;
3386 std::string method;
3387 std::string taskMatchStr;
3388 if (oemDiagType == OEMDiagnosticType::onDemand)
3389 {
3390 iface = crashdumpOnDemandInterface;
3391 method = "GenerateOnDemandLog";
3392 taskMatchStr = "type='signal',"
3393 "interface='org.freedesktop.DBus.Properties',"
3394 "member='PropertiesChanged',"
3395 "arg0namespace='com.intel.crashdump'";
3396 }
3397 else if (oemDiagType == OEMDiagnosticType::telemetry)
3398 {
3399 iface = crashdumpTelemetryInterface;
3400 method = "GenerateTelemetryLog";
3401 taskMatchStr = "type='signal',"
3402 "interface='org.freedesktop.DBus.Properties',"
3403 "member='PropertiesChanged',"
3404 "arg0namespace='com.intel.crashdump'";
3405 }
3406 else
3407 {
3408 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3409 << oemDiagnosticDataType;
3410 messages::actionParameterValueFormatError(
3411 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3412 "CollectDiagnosticData");
3413 return;
3414 }
3415
3416 auto collectCrashdumpCallback =
3417 [asyncResp, payload(task::Payload(req)),
3418 taskMatchStr](const boost::system::error_code ec,
3419 const std::string&) mutable {
3420 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003421 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003422 if (ec.value() == boost::system::errc::operation_not_supported)
3423 {
3424 messages::resourceInStandby(asyncResp->res);
3425 }
3426 else if (ec.value() ==
3427 boost::system::errc::device_or_resource_busy)
3428 {
3429 messages::serviceTemporarilyUnavailable(asyncResp->res,
3430 "60");
3431 }
3432 else
3433 {
3434 messages::internalError(asyncResp->res);
3435 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003436 return;
3437 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003438 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -05003439 [](boost::system::error_code err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003440 const std::shared_ptr<task::TaskData>& taskData) {
3441 if (!err)
3442 {
3443 taskData->messages.emplace_back(messages::taskCompletedOK(
3444 std::to_string(taskData->index)));
3445 taskData->state = "Completed";
3446 }
3447 return task::completed;
3448 },
3449 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003450
Ed Tanous002d39b2022-05-31 08:59:27 -07003451 task->startTimer(std::chrono::minutes(5));
3452 task->populateResp(asyncResp->res);
3453 task->payload.emplace(std::move(payload));
3454 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003455
Ed Tanous002d39b2022-05-31 08:59:27 -07003456 crow::connections::systemBus->async_method_call(
3457 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3458 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003459 });
3460}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003461
Andrew Geisslercb92c032018-08-17 07:56:14 -07003462/**
3463 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3464 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003465inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003466{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003467 /**
3468 * Function handles POST method request.
3469 * The Clear Log actions does not require any parameter.The action deletes
3470 * all entries found in the Entries collection for this Log Service.
3471 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003472
George Liu0fda0f12021-11-16 10:06:17 +08003473 BMCWEB_ROUTE(
3474 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003475 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003476 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003477 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003478 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003479 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3480 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003481 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003482 {
3483 return;
3484 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003485 if (systemName != "system")
3486 {
3487 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3488 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003489 return;
3490 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003491 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003492
Ed Tanous002d39b2022-05-31 08:59:27 -07003493 // Process response from Logging service.
3494 auto respHandler = [asyncResp](const boost::system::error_code ec) {
3495 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3496 if (ec)
3497 {
3498 // TODO Handle for specific error code
3499 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3500 asyncResp->res.result(
3501 boost::beast::http::status::internal_server_error);
3502 return;
3503 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003504
Ed Tanous002d39b2022-05-31 08:59:27 -07003505 asyncResp->res.result(boost::beast::http::status::no_content);
3506 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003507
Ed Tanous002d39b2022-05-31 08:59:27 -07003508 // Make call to Logging service to request Clear Log
3509 crow::connections::systemBus->async_method_call(
3510 respHandler, "xyz.openbmc_project.Logging",
3511 "/xyz/openbmc_project/logging",
3512 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3513 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003514}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003515
3516/****************************************************
3517 * Redfish PostCode interfaces
3518 * using DBUS interface: getPostCodesTS
3519 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003520inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003521{
Ed Tanous22d268c2022-05-19 09:39:07 -07003522 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003523 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003524 .methods(boost::beast::http::verb::get)(
3525 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003526 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3527 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003528 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003529 {
3530 return;
3531 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003532 if (systemName != "system")
3533 {
3534 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3535 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003536 return;
3537 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003538 asyncResp->res.jsonValue["@odata.id"] =
3539 "/redfish/v1/Systems/system/LogServices/PostCodes";
3540 asyncResp->res.jsonValue["@odata.type"] =
3541 "#LogService.v1_1_0.LogService";
3542 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3543 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003544 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003545 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3546 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3547 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303548
Ed Tanous002d39b2022-05-31 08:59:27 -07003549 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003550 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003551 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3552 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3553 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303554
Ed Tanous002d39b2022-05-31 08:59:27 -07003555 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3556 {"target",
3557 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003558 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003559}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003560
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003561inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003562{
George Liu0fda0f12021-11-16 10:06:17 +08003563 BMCWEB_ROUTE(
3564 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003565 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003566 // The following privilege is incorrect; It should be ConfigureManager
3567 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003568 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003569 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003570 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003571 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3572 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003573 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003574 {
3575 return;
3576 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003577 if (systemName != "system")
3578 {
3579 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3580 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003581 return;
3582 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003583 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003584
Ed Tanous002d39b2022-05-31 08:59:27 -07003585 // Make call to post-code service to request clear all
3586 crow::connections::systemBus->async_method_call(
3587 [asyncResp](const boost::system::error_code ec) {
3588 if (ec)
3589 {
3590 // TODO Handle for specific error code
3591 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3592 << ec;
3593 asyncResp->res.result(
3594 boost::beast::http::status::internal_server_error);
3595 messages::internalError(asyncResp->res);
3596 return;
3597 }
3598 },
3599 "xyz.openbmc_project.State.Boot.PostCode0",
3600 "/xyz/openbmc_project/State/Boot/PostCode0",
3601 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3602 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003603}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003604
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003605/**
3606 * @brief Parse post code ID and get the current value and index value
3607 * eg: postCodeID=B1-2, currentValue=1, index=2
3608 *
3609 * @param[in] postCodeID Post Code ID
3610 * @param[out] currentValue Current value
3611 * @param[out] index Index value
3612 *
3613 * @return bool true if the parsing is successful, false the parsing fails
3614 */
3615inline static bool parsePostCode(const std::string& postCodeID,
3616 uint64_t& currentValue, uint16_t& index)
3617{
3618 std::vector<std::string> split;
3619 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3620 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3621 {
3622 return false;
3623 }
3624
3625 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3626 const char* start = split[0].data() + 1;
3627 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3628 const char* end = split[0].data() + split[0].size();
3629 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3630
3631 if (ptrIndex != end || ecIndex != std::errc())
3632 {
3633 return false;
3634 }
3635
3636 start = split[1].data();
3637
3638 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3639 end = split[1].data() + split[1].size();
3640 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3641
3642 return ptrValue == end && ecValue == std::errc();
3643}
3644
3645static bool fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003646 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303647 const boost::container::flat_map<
3648 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003649 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3650 const uint64_t skip = 0, const uint64_t top = 0)
3651{
3652 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003653 const registries::Message* message =
3654 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003655
3656 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003657 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303658 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3659 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003660 {
3661 currentCodeIndex++;
3662 std::string postcodeEntryID =
3663 "B" + std::to_string(bootIndex) + "-" +
3664 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3665
3666 uint64_t usecSinceEpoch = code.first;
3667 uint64_t usTimeOffset = 0;
3668
3669 if (1 == currentCodeIndex)
3670 { // already incremented
3671 firstCodeTimeUs = code.first;
3672 }
3673 else
3674 {
3675 usTimeOffset = code.first - firstCodeTimeUs;
3676 }
3677
3678 // skip if no specific codeIndex is specified and currentCodeIndex does
3679 // not fall between top and skip
3680 if ((codeIndex == 0) &&
3681 (currentCodeIndex <= skip || currentCodeIndex > top))
3682 {
3683 continue;
3684 }
3685
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003686 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003687 // currentIndex
3688 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3689 {
3690 // This is done for simplicity. 1st entry is needed to calculate
3691 // time offset. To improve efficiency, one can get to the entry
3692 // directly (possibly with flatmap's nth method)
3693 continue;
3694 }
3695
3696 // currentCodeIndex is within top and skip or equal to specified code
3697 // index
3698
3699 // Get the Created time from the timestamp
3700 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003701 entryTimeStr =
Ed Tanous2b829372022-08-03 14:22:34 -07003702 redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003703
3704 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3705 std::ostringstream hexCode;
3706 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303707 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003708 std::ostringstream timeOffsetStr;
3709 // Set Fixed -Point Notation
3710 timeOffsetStr << std::fixed;
3711 // Set precision to 4 digits
3712 timeOffsetStr << std::setprecision(4);
3713 // Add double to stream
3714 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3715 std::vector<std::string> messageArgs = {
3716 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3717
3718 // Get MessageArgs template from message registry
3719 std::string msg;
3720 if (message != nullptr)
3721 {
3722 msg = message->message;
3723
3724 // fill in this post code value
3725 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003726 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003727 {
3728 std::string argStr = "%" + std::to_string(++i);
3729 size_t argPos = msg.find(argStr);
3730 if (argPos != std::string::npos)
3731 {
3732 msg.replace(argPos, argStr.length(), messageArg);
3733 }
3734 }
3735 }
3736
Tim Leed4342a92020-04-27 11:47:58 +08003737 // Get Severity template from message registry
3738 std::string severity;
3739 if (message != nullptr)
3740 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003741 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003742 }
3743
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003744 // Format entry
3745 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003746 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07003747 bmcLogEntry["@odata.id"] =
3748 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3749 postcodeEntryID;
3750 bmcLogEntry["Name"] = "POST Code Log Entry";
3751 bmcLogEntry["Id"] = postcodeEntryID;
3752 bmcLogEntry["Message"] = std::move(msg);
3753 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3754 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3755 bmcLogEntry["EntryType"] = "Event";
3756 bmcLogEntry["Severity"] = std::move(severity);
3757 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003758 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3759 {
3760 bmcLogEntry["AdditionalDataURI"] =
3761 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3762 postcodeEntryID + "/attachment";
3763 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003764
3765 // codeIndex is only specified when querying single entry, return only
3766 // that entry in this case
3767 if (codeIndex != 0)
3768 {
3769 aResp->res.jsonValue.update(bmcLogEntry);
3770 return true;
3771 }
3772
3773 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3774 logEntryArray.push_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003775 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003776
3777 // Return value is always false when querying multiple entries
3778 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003779}
3780
zhanghch058d1b46d2021-04-01 11:18:24 +08003781static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003782 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003783{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003784 uint16_t bootIndex = 0;
3785 uint64_t codeIndex = 0;
3786 if (!parsePostCode(entryId, codeIndex, bootIndex))
3787 {
3788 // Requested ID was not found
3789 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3790 return;
3791 }
3792
3793 if (bootIndex == 0 || codeIndex == 0)
3794 {
3795 // 0 is an invalid index
3796 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3797 return;
3798 }
3799
ZhikuiRena3316fc2020-01-29 14:58:08 -08003800 crow::connections::systemBus->async_method_call(
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003801 [aResp, entryId, bootIndex,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303802 codeIndex](const boost::system::error_code ec,
3803 const boost::container::flat_map<
3804 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3805 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003806 if (ec)
3807 {
3808 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3809 messages::internalError(aResp->res);
3810 return;
3811 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003812
Ed Tanous002d39b2022-05-31 08:59:27 -07003813 if (postcode.empty())
3814 {
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003815 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003816 return;
3817 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003818
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003819 if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
3820 {
3821 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3822 return;
3823 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003824 },
Jonathan Doman15124762021-01-07 17:54:17 -08003825 "xyz.openbmc_project.State.Boot.PostCode0",
3826 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003827 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3828 bootIndex);
3829}
3830
zhanghch058d1b46d2021-04-01 11:18:24 +08003831static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003832 const uint16_t bootIndex,
3833 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003834 const uint64_t entryCount, size_t skip,
3835 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003836{
3837 crow::connections::systemBus->async_method_call(
3838 [aResp, bootIndex, bootCount, entryCount, skip,
3839 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303840 const boost::container::flat_map<
3841 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3842 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003843 if (ec)
3844 {
3845 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3846 messages::internalError(aResp->res);
3847 return;
3848 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003849
Ed Tanous002d39b2022-05-31 08:59:27 -07003850 uint64_t endCount = entryCount;
3851 if (!postcode.empty())
3852 {
3853 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003854 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003855 {
Ed Tanous3648c8b2022-07-25 13:39:59 -07003856 uint64_t thisBootSkip =
3857 std::max(static_cast<uint64_t>(skip), entryCount) -
3858 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003859 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003860 std::min(static_cast<uint64_t>(top + skip), endCount) -
3861 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003862
3863 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3864 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003865 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003866 aResp->res.jsonValue["Members@odata.count"] = endCount;
3867 }
3868
3869 // continue to previous bootIndex
3870 if (bootIndex < bootCount)
3871 {
3872 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3873 bootCount, endCount, skip, top);
3874 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003875 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003876 {
3877 aResp->res.jsonValue["Members@odata.nextLink"] =
3878 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3879 std::to_string(skip + top);
3880 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003881 },
Jonathan Doman15124762021-01-07 17:54:17 -08003882 "xyz.openbmc_project.State.Boot.PostCode0",
3883 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003884 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3885 bootIndex);
3886}
3887
zhanghch058d1b46d2021-04-01 11:18:24 +08003888static void
3889 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003890 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003891{
3892 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003893 sdbusplus::asio::getProperty<uint16_t>(
3894 *crow::connections::systemBus,
3895 "xyz.openbmc_project.State.Boot.PostCode0",
3896 "/xyz/openbmc_project/State/Boot/PostCode0",
3897 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3898 [aResp, entryCount, skip, top](const boost::system::error_code ec,
3899 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003900 if (ec)
3901 {
3902 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3903 messages::internalError(aResp->res);
3904 return;
3905 }
3906 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003907 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003908}
3909
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003910inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003911{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003912 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003913 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003914 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003915 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003916 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003917 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3918 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003919 query_param::QueryCapabilities capabilities = {
3920 .canDelegateTop = true,
3921 .canDelegateSkip = true,
3922 };
3923 query_param::Query delegatedQuery;
3924 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003925 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003926 {
3927 return;
3928 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003929
3930 if (systemName != "system")
3931 {
3932 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3933 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003934 return;
3935 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003936 asyncResp->res.jsonValue["@odata.type"] =
3937 "#LogEntryCollection.LogEntryCollection";
3938 asyncResp->res.jsonValue["@odata.id"] =
3939 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3940 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3941 asyncResp->res.jsonValue["Description"] =
3942 "Collection of POST Code Log Entries";
3943 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3944 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003945 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003946 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003947 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003948 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003949}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003950
George Liu647b3cd2021-07-05 12:43:56 +08003951inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3952{
George Liu0fda0f12021-11-16 10:06:17 +08003953 BMCWEB_ROUTE(
3954 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003955 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003956 .privileges(redfish::privileges::getLogEntry)
3957 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003958 [&app](const crow::Request& req,
3959 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003960 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07003961 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003962 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003963 {
3964 return;
3965 }
Ed Tanous99351cd2022-08-07 16:42:51 -07003966 if (http_helpers::isContentTypeAllowed(
3967 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07003968 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07003969 {
3970 asyncResp->res.result(boost::beast::http::status::bad_request);
3971 return;
3972 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003973 if (systemName != "system")
3974 {
3975 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3976 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003977 return;
3978 }
George Liu647b3cd2021-07-05 12:43:56 +08003979
Ed Tanous002d39b2022-05-31 08:59:27 -07003980 uint64_t currentValue = 0;
3981 uint16_t index = 0;
3982 if (!parsePostCode(postCodeID, currentValue, index))
3983 {
3984 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3985 return;
3986 }
George Liu647b3cd2021-07-05 12:43:56 +08003987
Ed Tanous002d39b2022-05-31 08:59:27 -07003988 crow::connections::systemBus->async_method_call(
3989 [asyncResp, postCodeID, currentValue](
3990 const boost::system::error_code ec,
3991 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3992 postcodes) {
3993 if (ec.value() == EBADR)
3994 {
3995 messages::resourceNotFound(asyncResp->res, "LogEntry",
3996 postCodeID);
3997 return;
3998 }
3999 if (ec)
4000 {
4001 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4002 messages::internalError(asyncResp->res);
4003 return;
4004 }
George Liu647b3cd2021-07-05 12:43:56 +08004005
Ed Tanous002d39b2022-05-31 08:59:27 -07004006 size_t value = static_cast<size_t>(currentValue) - 1;
4007 if (value == std::string::npos || postcodes.size() < currentValue)
4008 {
4009 BMCWEB_LOG_ERROR << "Wrong currentValue value";
4010 messages::resourceNotFound(asyncResp->res, "LogEntry",
4011 postCodeID);
4012 return;
4013 }
George Liu647b3cd2021-07-05 12:43:56 +08004014
Ed Tanous002d39b2022-05-31 08:59:27 -07004015 const auto& [tID, c] = postcodes[value];
4016 if (c.empty())
4017 {
4018 BMCWEB_LOG_INFO << "No found post code data";
4019 messages::resourceNotFound(asyncResp->res, "LogEntry",
4020 postCodeID);
4021 return;
4022 }
4023 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4024 const char* d = reinterpret_cast<const char*>(c.data());
4025 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004026
Ed Tanousd9f6c622022-03-17 09:12:17 -07004027 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004028 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004029 asyncResp->res.addHeader(
4030 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004031 asyncResp->res.body() = crow::utility::base64encode(strData);
4032 },
4033 "xyz.openbmc_project.State.Boot.PostCode0",
4034 "/xyz/openbmc_project/State/Boot/PostCode0",
4035 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4036 });
George Liu647b3cd2021-07-05 12:43:56 +08004037}
4038
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004039inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004040{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004041 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004042 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004043 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004044 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004045 [&app](const crow::Request& req,
4046 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004047 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004048 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004049 {
4050 return;
4051 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004052 if (systemName != "system")
4053 {
4054 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4055 systemName);
4056 return;
4057 }
4058
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004059 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004060 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004061}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004062
Ed Tanous1da66f72018-07-27 16:13:37 -07004063} // namespace redfish