blob: 5e732fb83f91222a21414654949b4f90b31a0e77 [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
Spencer Kub7028eb2021-10-26 15:27:35 +080018#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080019#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080020#include "human_sort.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070021#include "registries.hpp"
22#include "registries/base_message_registry.hpp"
23#include "registries/openbmc_message_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080024#include "task.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070025
Jason M. Billse1f26342018-07-18 12:12:00 -070026#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060027#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060028#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070029
John Edward Broadbent7e860f12021-04-08 15:57:16 -070030#include <app.hpp>
Ed Tanous9896eae2022-07-23 15:07:33 -070031#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070032#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060033#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070034#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070035#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070036#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080037#include <boost/system/linux_error.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080038#include <dbus_utility.hpp>
Andrew Geisslercb92c032018-08-17 07:56:14 -070039#include <error_messages.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070040#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070041#include <registries/privilege_registry.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020042#include <sdbusplus/asio/property.hpp>
43#include <sdbusplus/unpack_properties.hpp>
44#include <utils/dbus_utils.hpp>
Ed Tanous2b829372022-08-03 14:22:34 -070045#include <utils/time_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050046
George Liu647b3cd2021-07-05 12:43:56 +080047#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070048#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080049#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070050#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070051#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080052#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070053
54namespace redfish
55{
56
Gunnar Mills1214b7e2020-06-04 10:11:30 -050057constexpr char const* crashdumpObject = "com.intel.crashdump";
58constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050059constexpr char const* crashdumpInterface = "com.intel.crashdump";
60constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070061 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050062constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070063 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070064constexpr char const* crashdumpTelemetryInterface =
65 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070066
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060067enum class DumpCreationProgress
68{
69 DUMP_CREATE_SUCCESS,
70 DUMP_CREATE_FAILED,
71 DUMP_CREATE_INPROGRESS
72};
73
Ed Tanousfffb8c12022-02-07 23:53:03 -080074namespace registries
Jason M. Bills4851d452019-03-28 11:27:48 -070075{
Ed Tanous26702d02021-11-03 15:02:33 -070076static const Message*
77 getMessageFromRegistry(const std::string& messageKey,
78 const std::span<const MessageEntry> registry)
Jason M. Bills4851d452019-03-28 11:27:48 -070079{
Ed Tanous002d39b2022-05-31 08:59:27 -070080 std::span<const MessageEntry>::iterator messageIt =
81 std::find_if(registry.begin(), registry.end(),
82 [&messageKey](const MessageEntry& messageEntry) {
83 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
Ed Tanous26702d02021-11-03 15:02:33 -070084 });
85 if (messageIt != registry.end())
Jason M. Bills4851d452019-03-28 11:27:48 -070086 {
87 return &messageIt->second;
88 }
89
90 return nullptr;
91}
92
Gunnar Mills1214b7e2020-06-04 10:11:30 -050093static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070094{
95 // Redfish MessageIds are in the form
96 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
97 // the right Message
98 std::vector<std::string> fields;
99 fields.reserve(4);
100 boost::split(fields, messageID, boost::is_any_of("."));
Ed Tanous02cad962022-06-30 16:50:15 -0700101 const std::string& registryName = fields[0];
102 const std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -0700103
104 // Find the right registry and check it for the MessageKey
105 if (std::string(base::header.registryPrefix) == registryName)
106 {
107 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -0700108 messageKey, std::span<const MessageEntry>(base::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -0700109 }
110 if (std::string(openbmc::header.registryPrefix) == registryName)
111 {
112 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -0700113 messageKey, std::span<const MessageEntry>(openbmc::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -0700114 }
115 return nullptr;
116}
Ed Tanousfffb8c12022-02-07 23:53:03 -0800117} // namespace registries
Jason M. Bills4851d452019-03-28 11:27:48 -0700118
James Feistf6150402019-01-08 10:36:20 -0800119namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700120
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500121inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700122{
Ed Tanousd4d25792020-09-29 15:15:03 -0700123 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
124 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
125 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
126 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700127 {
128 return "Critical";
129 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700130 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
131 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
132 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700133 {
134 return "OK";
135 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700136 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700137 {
138 return "Warning";
139 }
140 return "";
141}
142
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700143inline static int getJournalMetadata(sd_journal* journal,
144 const std::string_view& field,
145 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700146{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500147 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700148 size_t length = 0;
149 int ret = 0;
150 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800151 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
152 const void** dataVoid = reinterpret_cast<const void**>(&data);
153
154 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700155 if (ret < 0)
156 {
157 return ret;
158 }
Ed Tanous39e77502019-03-04 17:35:53 -0800159 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700160 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000161 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700162 return ret;
163}
164
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165inline static int getJournalMetadata(sd_journal* journal,
166 const std::string_view& field,
167 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700168{
169 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800170 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700171 // Get the metadata from the requested field of the journal entry
172 ret = getJournalMetadata(journal, field, metadata);
173 if (ret < 0)
174 {
175 return ret;
176 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000177 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700178 return ret;
179}
180
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700181inline static bool getEntryTimestamp(sd_journal* journal,
182 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800183{
184 int ret = 0;
185 uint64_t timestamp = 0;
186 ret = sd_journal_get_realtime_usec(journal, &timestamp);
187 if (ret < 0)
188 {
189 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
190 << strerror(-ret);
191 return false;
192 }
Ed Tanous2b829372022-08-03 14:22:34 -0700193 entryTimestamp =
194 redfish::time_utils::getDateTimeUint(timestamp / 1000 / 1000);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500195 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800196}
Ed Tanous50b8a432022-02-03 16:29:50 -0800197
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700198inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
199 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700200{
201 int ret = 0;
202 static uint64_t prevTs = 0;
203 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700204 if (firstEntry)
205 {
206 prevTs = 0;
207 }
208
Jason M. Bills16428a12018-11-02 12:42:29 -0700209 // Get the entry timestamp
210 uint64_t curTs = 0;
211 ret = sd_journal_get_realtime_usec(journal, &curTs);
212 if (ret < 0)
213 {
214 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
215 << strerror(-ret);
216 return false;
217 }
218 // If the timestamp isn't unique, increment the index
219 if (curTs == prevTs)
220 {
221 index++;
222 }
223 else
224 {
225 // Otherwise, reset it
226 index = 0;
227 }
228 // Save the timestamp
229 prevTs = curTs;
230
231 entryID = std::to_string(curTs);
232 if (index > 0)
233 {
234 entryID += "_" + std::to_string(index);
235 }
236 return true;
237}
238
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500239static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700240 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700241{
Ed Tanous271584a2019-07-09 16:24:22 -0700242 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700243 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700244 if (firstEntry)
245 {
246 prevTs = 0;
247 }
248
Jason M. Bills95820182019-04-22 16:25:34 -0700249 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700250 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700251 std::tm timeStruct = {};
252 std::istringstream entryStream(logEntry);
253 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
254 {
255 curTs = std::mktime(&timeStruct);
256 }
257 // If the timestamp isn't unique, increment the index
258 if (curTs == prevTs)
259 {
260 index++;
261 }
262 else
263 {
264 // Otherwise, reset it
265 index = 0;
266 }
267 // Save the timestamp
268 prevTs = curTs;
269
270 entryID = std::to_string(curTs);
271 if (index > 0)
272 {
273 entryID += "_" + std::to_string(index);
274 }
275 return true;
276}
277
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700278inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800279 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
280 const std::string& entryID, uint64_t& timestamp,
281 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700282{
283 if (entryID.empty())
284 {
285 return false;
286 }
287 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800288 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700289
Ed Tanous81ce6092020-12-17 16:54:55 +0000290 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800291 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700292 {
293 // Timestamp has an index
294 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800295 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700296 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700297 auto [ptr, ec] = std::from_chars(
298 indexStr.data(), indexStr.data() + indexStr.size(), index);
299 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700300 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800301 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700302 return false;
303 }
304 }
305 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700306 auto [ptr, ec] =
307 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
308 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700309 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800310 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700311 return false;
312 }
313 return true;
314}
315
Jason M. Bills95820182019-04-22 16:25:34 -0700316static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500317 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700318{
319 static const std::filesystem::path redfishLogDir = "/var/log";
320 static const std::string redfishLogFilename = "redfish";
321
322 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500323 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700324 std::filesystem::directory_iterator(redfishLogDir))
325 {
326 // If we find a redfish log file, save the path
327 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700328 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700329 {
330 redfishLogFiles.emplace_back(redfishLogDir / filename);
331 }
332 }
333 // As the log files rotate, they are appended with a ".#" that is higher for
334 // the older logs. Since we don't expect more than 10 log files, we
335 // can just sort the list to get them in order from newest to oldest
336 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
337
338 return !redfishLogFiles.empty();
339}
340
Claire Weinanaefe3782022-07-15 19:17:19 -0700341inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800342 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700343 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700344 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
345{
346 for (const auto& interfaceMap : object.second)
347 {
348 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
349 {
350 for (const auto& propertyMap : interfaceMap.second)
351 {
352 if (propertyMap.first == "Status")
353 {
354 const auto* status =
355 std::get_if<std::string>(&propertyMap.second);
356 if (status == nullptr)
357 {
358 messages::internalError(asyncResp->res);
359 break;
360 }
361 dumpStatus = *status;
362 }
363 }
364 }
365 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
366 {
367 for (const auto& propertyMap : interfaceMap.second)
368 {
369 if (propertyMap.first == "Size")
370 {
371 const auto* sizePtr =
372 std::get_if<uint64_t>(&propertyMap.second);
373 if (sizePtr == nullptr)
374 {
375 messages::internalError(asyncResp->res);
376 break;
377 }
378 size = *sizePtr;
379 break;
380 }
381 }
382 }
383 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
384 {
385 for (const auto& propertyMap : interfaceMap.second)
386 {
387 if (propertyMap.first == "Elapsed")
388 {
389 const uint64_t* usecsTimeStamp =
390 std::get_if<uint64_t>(&propertyMap.second);
391 if (usecsTimeStamp == nullptr)
392 {
393 messages::internalError(asyncResp->res);
394 break;
395 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700396 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700397 break;
398 }
399 }
400 }
401 }
402}
403
Nan Zhou21ab4042022-06-26 23:07:40 +0000404static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800405{
406 std::string entriesPath;
407
408 if (dumpType == "BMC")
409 {
410 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
411 }
412 else if (dumpType == "FaultLog")
413 {
414 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
415 }
416 else if (dumpType == "System")
417 {
418 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
419 }
420 else
421 {
422 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
423 << dumpType;
424 }
425
426 // Returns empty string on error
427 return entriesPath;
428}
429
zhanghch058d1b46d2021-04-01 11:18:24 +0800430inline void
431 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
432 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500433{
Claire Weinanfdd26902022-03-01 14:18:25 -0800434 std::string entriesPath = getDumpEntriesPath(dumpType);
435 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500436 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500437 messages::internalError(asyncResp->res);
438 return;
439 }
440
441 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800442 [asyncResp, entriesPath,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800443 dumpType](const boost::system::error_code ec,
444 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700445 if (ec)
446 {
447 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
448 messages::internalError(asyncResp->res);
449 return;
450 }
451
Claire Weinanfdd26902022-03-01 14:18:25 -0800452 // Remove ending slash
453 std::string odataIdStr = entriesPath;
454 if (!odataIdStr.empty())
455 {
456 odataIdStr.pop_back();
457 }
458
459 asyncResp->res.jsonValue["@odata.type"] =
460 "#LogEntryCollection.LogEntryCollection";
461 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
462 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
463 asyncResp->res.jsonValue["Description"] =
464 "Collection of " + dumpType + " Dump Entries";
465
Ed Tanous002d39b2022-05-31 08:59:27 -0700466 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
467 entriesArray = nlohmann::json::array();
468 std::string dumpEntryPath =
469 "/xyz/openbmc_project/dump/" +
470 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
471
472 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
473 return AlphanumLess<std::string>()(l.first.filename(),
474 r.first.filename());
475 });
476
477 for (auto& object : resp)
478 {
479 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500480 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 continue;
482 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700483 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700484 uint64_t size = 0;
485 std::string dumpStatus;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700486 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700487
488 std::string entryID = object.first.filename();
489 if (entryID.empty())
490 {
491 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500492 }
493
Claire Weinanc6fecda2022-07-15 10:43:25 -0700494 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700495 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700496
497 if (dumpStatus !=
498 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
499 !dumpStatus.empty())
500 {
501 // Dump status is not Complete, no need to enumerate
502 continue;
503 }
504
Vijay Lobo9c11a172021-10-07 16:53:16 -0500505 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800506 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700507 thisEntry["Id"] = entryID;
508 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700509 thisEntry["Name"] = dumpType + " Dump Entry";
510
Ed Tanous002d39b2022-05-31 08:59:27 -0700511 if (dumpType == "BMC")
512 {
Claire Weinanc6fecda2022-07-15 10:43:25 -0700513 thisEntry["Created"] = redfish::time_utils::getDateTimeUint(
514 timestampUs / 1000 / 1000);
Ed Tanous002d39b2022-05-31 08:59:27 -0700515 thisEntry["DiagnosticDataType"] = "Manager";
516 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800517 entriesPath + entryID + "/attachment";
518 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700519 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700520 else if (dumpType == "FaultLog")
521 {
522 thisEntry["Created"] =
523 redfish::time_utils::getDateTimeUintUs(timestampUs);
524 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700525 else if (dumpType == "System")
526 {
Claire Weinanc6fecda2022-07-15 10:43:25 -0700527 thisEntry["Created"] = redfish::time_utils::getDateTimeUint(
528 timestampUs / 1000 / 1000);
Ed Tanous002d39b2022-05-31 08:59:27 -0700529 thisEntry["DiagnosticDataType"] = "OEM";
530 thisEntry["OEMDiagnosticDataType"] = "System";
531 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800532 entriesPath + entryID + "/attachment";
533 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700534 }
535 entriesArray.push_back(std::move(thisEntry));
536 }
537 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500538 },
539 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
540 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
541}
542
zhanghch058d1b46d2021-04-01 11:18:24 +0800543inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700544 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800545 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500546{
Claire Weinanfdd26902022-03-01 14:18:25 -0800547 std::string entriesPath = getDumpEntriesPath(dumpType);
548 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500549 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500550 messages::internalError(asyncResp->res);
551 return;
552 }
553
554 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800555 [asyncResp, entryID, dumpType,
556 entriesPath](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700557 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700558 if (ec)
559 {
560 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
561 messages::internalError(asyncResp->res);
562 return;
563 }
564
565 bool foundDumpEntry = false;
566 std::string dumpEntryPath =
567 "/xyz/openbmc_project/dump/" +
568 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
569
570 for (const auto& objectPath : resp)
571 {
572 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500573 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700574 continue;
575 }
576
577 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700578 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700579 uint64_t size = 0;
580 std::string dumpStatus;
581
Claire Weinanaefe3782022-07-15 19:17:19 -0700582 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700583 timestampUs, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700584
585 if (dumpStatus !=
586 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
587 !dumpStatus.empty())
588 {
589 // Dump status is not Complete
590 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200591 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
592 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500593 return;
594 }
595
Ed Tanous002d39b2022-05-31 08:59:27 -0700596 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -0500597 "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800598 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700599 asyncResp->res.jsonValue["Id"] = entryID;
600 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700601 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500602
Ed Tanous002d39b2022-05-31 08:59:27 -0700603 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500604 {
Claire Weinanc6fecda2022-07-15 10:43:25 -0700605 asyncResp->res.jsonValue["Created"] =
606 redfish::time_utils::getDateTimeUint(timestampUs / 1000 /
607 1000);
Ed Tanous002d39b2022-05-31 08:59:27 -0700608 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
609 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800610 entriesPath + entryID + "/attachment";
611 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500612 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700613 else if (dumpType == "FaultLog")
614 {
615 asyncResp->res.jsonValue["Created"] =
616 redfish::time_utils::getDateTimeUintUs(timestampUs);
617 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700618 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500619 {
Claire Weinanc6fecda2022-07-15 10:43:25 -0700620 asyncResp->res.jsonValue["Created"] =
621 redfish::time_utils::getDateTimeUint(timestampUs / 1000 /
622 1000);
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](
786 boost::system::error_code err, sdbusplus::message::message& msg,
787 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(
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600917 [asyncResp, payload(task::Payload(req)),
918 dumpPath](const boost::system::error_code ec,
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500919 const sdbusplus::message::message& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600920 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700921 if (ec)
922 {
923 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500924 const sd_bus_error* dbusError = msg.get_error();
925 if (dbusError == nullptr)
926 {
927 messages::internalError(asyncResp->res);
928 return;
929 }
930
931 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
932 << " and error msg: " << dbusError->message;
933 if (std::string_view(
934 "xyz.openbmc_project.Common.Error.NotAllowed") ==
935 dbusError->name)
936 {
937 messages::resourceInStandby(asyncResp->res);
938 return;
939 }
940 if (std::string_view(
941 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
942 dbusError->name)
943 {
944 messages::serviceDisabled(asyncResp->res, dumpPath);
945 return;
946 }
947 if (std::string_view(
948 "xyz.openbmc_project.Common.Error.Unavailable") ==
949 dbusError->name)
950 {
951 messages::resourceInUse(asyncResp->res);
952 return;
953 }
954 // Other Dbus errors such as:
955 // xyz.openbmc_project.Common.Error.InvalidArgument &
956 // org.freedesktop.DBus.Error.InvalidArgs are all related to
957 // the dbus call that is made here in the bmcweb
958 // implementation and has nothing to do with the client's
959 // input in the request. Hence, returning internal error
960 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -0700961 messages::internalError(asyncResp->res);
962 return;
963 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600964 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
965 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500966 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500967 "xyz.openbmc_project.Dump.Manager",
968 "/xyz/openbmc_project/dump/" +
969 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600970 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500971}
972
zhanghch058d1b46d2021-04-01 11:18:24 +0800973inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
974 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500975{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500976 std::string dumpTypeLowerCopy =
977 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800978
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500979 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -0800980 [asyncResp, dumpType](
981 const boost::system::error_code ec,
982 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700983 if (ec)
984 {
985 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
986 messages::internalError(asyncResp->res);
987 return;
988 }
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500989
Ed Tanous002d39b2022-05-31 08:59:27 -0700990 for (const std::string& path : subTreePaths)
991 {
992 sdbusplus::message::object_path objPath(path);
993 std::string logID = objPath.filename();
994 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500995 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700996 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500997 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700998 deleteDumpEntry(asyncResp, logID, dumpType);
999 }
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001000 },
1001 "xyz.openbmc_project.ObjectMapper",
1002 "/xyz/openbmc_project/object_mapper",
1003 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001004 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
1005 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
1006 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001007}
1008
Ed Tanousb9d36b42022-02-26 21:42:46 -08001009inline static void
1010 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1011 std::string& filename, std::string& timestamp,
1012 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001013{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001014 const std::string* filenamePtr = nullptr;
1015 const std::string* timestampPtr = nullptr;
1016 const std::string* logfilePtr = nullptr;
1017
1018 const bool success = sdbusplus::unpackPropertiesNoThrow(
1019 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1020 "Filename", filenamePtr, "Log", logfilePtr);
1021
1022 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001023 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001024 return;
1025 }
1026
1027 if (filenamePtr != nullptr)
1028 {
1029 filename = *filenamePtr;
1030 }
1031
1032 if (timestampPtr != nullptr)
1033 {
1034 timestamp = *timestampPtr;
1035 }
1036
1037 if (logfilePtr != nullptr)
1038 {
1039 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001040 }
1041}
1042
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001043constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001044inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001045{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001046 /**
1047 * Functions triggers appropriate requests on DBus
1048 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001049 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001050 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001051 .methods(boost::beast::http::verb::get)(
1052 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001053 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1054 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001055 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001056 {
1057 return;
1058 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001059 if (systemName != "system")
1060 {
1061 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1062 systemName);
1063 return;
1064 }
1065
Ed Tanous002d39b2022-05-31 08:59:27 -07001066 // Collections don't include the static data added by SubRoute
1067 // because it has a duplicate entry for members
1068 asyncResp->res.jsonValue["@odata.type"] =
1069 "#LogServiceCollection.LogServiceCollection";
1070 asyncResp->res.jsonValue["@odata.id"] =
1071 "/redfish/v1/Systems/system/LogServices";
1072 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1073 asyncResp->res.jsonValue["Description"] =
1074 "Collection of LogServices for this Computer System";
1075 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1076 logServiceArray = nlohmann::json::array();
1077 nlohmann::json::object_t eventLog;
1078 eventLog["@odata.id"] =
1079 "/redfish/v1/Systems/system/LogServices/EventLog";
1080 logServiceArray.push_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001081#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001082 nlohmann::json::object_t dumpLog;
1083 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
1084 logServiceArray.push_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001085#endif
1086
Jason M. Billsd53dd412019-02-12 17:16:22 -08001087#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001088 nlohmann::json::object_t crashdump;
1089 crashdump["@odata.id"] =
1090 "/redfish/v1/Systems/system/LogServices/Crashdump";
1091 logServiceArray.push_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001092#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001093
1094#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001095 nlohmann::json::object_t hostlogger;
1096 hostlogger["@odata.id"] =
1097 "/redfish/v1/Systems/system/LogServices/HostLogger";
1098 logServiceArray.push_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001099#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001100 asyncResp->res.jsonValue["Members@odata.count"] =
1101 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001102
Ed Tanous002d39b2022-05-31 08:59:27 -07001103 crow::connections::systemBus->async_method_call(
1104 [asyncResp](const boost::system::error_code ec,
1105 const dbus::utility::MapperGetSubTreePathsResponse&
1106 subtreePath) {
1107 if (ec)
1108 {
1109 BMCWEB_LOG_ERROR << ec;
1110 return;
1111 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001112
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 for (const auto& pathStr : subtreePath)
1114 {
1115 if (pathStr.find("PostCode") != std::string::npos)
1116 {
1117 nlohmann::json& logServiceArrayLocal =
1118 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001119 nlohmann::json::object_t member;
1120 member["@odata.id"] =
1121 "/redfish/v1/Systems/system/LogServices/PostCodes";
1122
1123 logServiceArrayLocal.push_back(std::move(member));
1124
Ed Tanous002d39b2022-05-31 08:59:27 -07001125 asyncResp->res.jsonValue["Members@odata.count"] =
1126 logServiceArrayLocal.size();
1127 return;
1128 }
1129 }
1130 },
1131 "xyz.openbmc_project.ObjectMapper",
1132 "/xyz/openbmc_project/object_mapper",
1133 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
1134 std::array<const char*, 1>{postCodeIface});
Ed Tanous45ca1b82022-03-25 13:07:27 -07001135 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001136}
1137
1138inline void requestRoutesEventLogService(App& app)
1139{
Ed Tanous22d268c2022-05-19 09:39:07 -07001140 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001141 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001142 .methods(boost::beast::http::verb::get)(
1143 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001144 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1145 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001146 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 {
1148 return;
1149 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001150 if (systemName != "system")
1151 {
1152 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1153 systemName);
1154 return;
1155 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001156 asyncResp->res.jsonValue["@odata.id"] =
1157 "/redfish/v1/Systems/system/LogServices/EventLog";
1158 asyncResp->res.jsonValue["@odata.type"] =
1159 "#LogService.v1_1_0.LogService";
1160 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1161 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1162 asyncResp->res.jsonValue["Id"] = "EventLog";
1163 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301164
Ed Tanous002d39b2022-05-31 08:59:27 -07001165 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001166 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301167
Ed Tanous002d39b2022-05-31 08:59:27 -07001168 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1169 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1170 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301171
Ed Tanous002d39b2022-05-31 08:59:27 -07001172 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1173 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1174 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001175
Ed Tanous002d39b2022-05-31 08:59:27 -07001176 {"target",
1177 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001178 });
1179}
1180
1181inline void requestRoutesJournalEventLogClear(App& app)
1182{
Jason M. Bills4978b632022-02-22 14:17:43 -08001183 BMCWEB_ROUTE(
1184 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001185 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001186 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001187 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001188 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001189 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1190 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001191 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001192 {
1193 return;
1194 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001195 if (systemName != "system")
1196 {
1197 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1198 systemName);
1199 return;
1200 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001201 // Clear the EventLog by deleting the log files
1202 std::vector<std::filesystem::path> redfishLogFiles;
1203 if (getRedfishLogFiles(redfishLogFiles))
1204 {
1205 for (const std::filesystem::path& file : redfishLogFiles)
1206 {
1207 std::error_code ec;
1208 std::filesystem::remove(file, ec);
1209 }
1210 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001211
Ed Tanous002d39b2022-05-31 08:59:27 -07001212 // Reload rsyslog so it knows to start new log files
1213 crow::connections::systemBus->async_method_call(
1214 [asyncResp](const boost::system::error_code ec) {
1215 if (ec)
1216 {
1217 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1218 messages::internalError(asyncResp->res);
1219 return;
1220 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001221
Ed Tanous002d39b2022-05-31 08:59:27 -07001222 messages::success(asyncResp->res);
1223 },
1224 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1225 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1226 "replace");
1227 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001228}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001229
Jason M. Billsac992cd2022-06-24 13:31:46 -07001230enum class LogParseError
1231{
1232 success,
1233 parseFailed,
1234 messageIdNotInRegistry,
1235};
1236
1237static LogParseError
1238 fillEventLogEntryJson(const std::string& logEntryID,
1239 const std::string& logEntry,
1240 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001241{
Jason M. Bills95820182019-04-22 16:25:34 -07001242 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001243 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001244 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001245 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001246 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001247 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001248 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001249 std::string timestamp = logEntry.substr(0, space);
1250 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001251 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001252 if (entryStart == std::string::npos)
1253 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001254 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001255 }
1256 std::string_view entry(logEntry);
1257 entry.remove_prefix(entryStart);
1258 // Use split to separate the entry into its fields
1259 std::vector<std::string> logEntryFields;
1260 boost::split(logEntryFields, entry, boost::is_any_of(","),
1261 boost::token_compress_on);
1262 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001263 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001264 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001265 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001266 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001267 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001268
Jason M. Bills4851d452019-03-28 11:27:48 -07001269 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001270 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001271
Sui Chen54417b02022-03-24 14:59:52 -07001272 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001273 {
Sui Chen54417b02022-03-24 14:59:52 -07001274 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001275 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001276 }
1277
Sui Chen54417b02022-03-24 14:59:52 -07001278 std::string msg = message->message;
1279
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001280 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001281 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001282 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001283 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001284 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001285 // If the first string is empty, assume there are no MessageArgs
1286 std::size_t messageArgsSize = 0;
1287 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001288 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001289 messageArgsSize = logEntryFields.size() - 1;
1290 }
1291
Ed Tanous23a21a12020-07-25 04:45:05 +00001292 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001293
1294 // Fill the MessageArgs into the Message
1295 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001296 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001297 {
1298 std::string argStr = "%" + std::to_string(++i);
1299 size_t argPos = msg.find(argStr);
1300 if (argPos != std::string::npos)
1301 {
1302 msg.replace(argPos, argStr.length(), messageArg);
1303 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001304 }
1305 }
1306
Jason M. Bills95820182019-04-22 16:25:34 -07001307 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1308 // format which matches the Redfish format except for the fractional seconds
1309 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001310 std::size_t dot = timestamp.find_first_of('.');
1311 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001312 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001313 {
Jason M. Bills95820182019-04-22 16:25:34 -07001314 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001315 }
1316
1317 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001318 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07001319 logEntryJson["@odata.id"] =
1320 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
1321 logEntryJson["Name"] = "System Event Log Entry";
1322 logEntryJson["Id"] = logEntryID;
1323 logEntryJson["Message"] = std::move(msg);
1324 logEntryJson["MessageId"] = std::move(messageID);
1325 logEntryJson["MessageArgs"] = messageArgs;
1326 logEntryJson["EntryType"] = "Event";
1327 logEntryJson["Severity"] = message->messageSeverity;
1328 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001329 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001330}
1331
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001332inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001333{
Ed Tanous22d268c2022-05-19 09:39:07 -07001334 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001335 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001336 .methods(boost::beast::http::verb::get)(
1337 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001338 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1339 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001340 query_param::QueryCapabilities capabilities = {
1341 .canDelegateTop = true,
1342 .canDelegateSkip = true,
1343 };
1344 query_param::Query delegatedQuery;
1345 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001346 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001347 {
1348 return;
1349 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001350 if (systemName != "system")
1351 {
1352 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1353 systemName);
1354 return;
1355 }
1356
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001357 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001358 size_t skip = delegatedQuery.skip.value_or(0);
1359
Ed Tanous002d39b2022-05-31 08:59:27 -07001360 // Collections don't include the static data added by SubRoute
1361 // because it has a duplicate entry for members
1362 asyncResp->res.jsonValue["@odata.type"] =
1363 "#LogEntryCollection.LogEntryCollection";
1364 asyncResp->res.jsonValue["@odata.id"] =
1365 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1366 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1367 asyncResp->res.jsonValue["Description"] =
1368 "Collection of System Event Log Entries";
1369
1370 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1371 logEntryArray = nlohmann::json::array();
1372 // Go through the log files and create a unique ID for each
1373 // entry
1374 std::vector<std::filesystem::path> redfishLogFiles;
1375 getRedfishLogFiles(redfishLogFiles);
1376 uint64_t entryCount = 0;
1377 std::string logEntry;
1378
1379 // Oldest logs are in the last file, so start there and loop
1380 // backwards
1381 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1382 it++)
1383 {
1384 std::ifstream logStream(*it);
1385 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001386 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001387 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001388 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001389
Ed Tanous002d39b2022-05-31 08:59:27 -07001390 // Reset the unique ID on the first entry
1391 bool firstEntry = true;
1392 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001393 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001394 std::string idStr;
1395 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001396 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001397 continue;
1398 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001399 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001400
Jason M. Billsde703c52022-06-23 14:19:04 -07001401 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001402 LogParseError status =
1403 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1404 if (status == LogParseError::messageIdNotInRegistry)
1405 {
1406 continue;
1407 }
1408 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001409 {
1410 messages::internalError(asyncResp->res);
1411 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001412 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001413
Jason M. Billsde703c52022-06-23 14:19:04 -07001414 entryCount++;
1415 // Handle paging using skip (number of entries to skip from the
1416 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001417 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001418 {
1419 continue;
1420 }
1421
1422 logEntryArray.push_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001423 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001424 }
1425 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001426 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001427 {
1428 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1429 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001430 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001431 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001432 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001433}
Chicago Duan336e96c2019-07-15 14:22:08 +08001434
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001435inline void requestRoutesJournalEventLogEntry(App& app)
1436{
1437 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001438 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001439 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001440 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001441 [&app](const crow::Request& req,
1442 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001443 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001444 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001445 {
1446 return;
1447 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001448
1449 if (systemName != "system")
1450 {
1451 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1452 systemName);
1453 return;
1454 }
1455
Ed Tanous002d39b2022-05-31 08:59:27 -07001456 const std::string& targetID = param;
1457
1458 // Go through the log files and check the unique ID for each
1459 // entry to find the target entry
1460 std::vector<std::filesystem::path> redfishLogFiles;
1461 getRedfishLogFiles(redfishLogFiles);
1462 std::string logEntry;
1463
1464 // Oldest logs are in the last file, so start there and loop
1465 // backwards
1466 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1467 it++)
1468 {
1469 std::ifstream logStream(*it);
1470 if (!logStream.is_open())
1471 {
1472 continue;
1473 }
1474
1475 // Reset the unique ID on the first entry
1476 bool firstEntry = true;
1477 while (std::getline(logStream, logEntry))
1478 {
1479 std::string idStr;
1480 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001481 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001482 continue;
1483 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001484 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001485
1486 if (idStr == targetID)
1487 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001488 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001489 LogParseError status =
1490 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1491 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001492 {
1493 messages::internalError(asyncResp->res);
1494 return;
1495 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001496 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001497 return;
1498 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001499 }
1500 }
1501 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001502 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001503 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001504}
1505
1506inline void requestRoutesDBusEventLogEntryCollection(App& app)
1507{
Ed Tanous22d268c2022-05-19 09:39:07 -07001508 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001509 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001510 .methods(boost::beast::http::verb::get)(
1511 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001512 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1513 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001514 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001515 {
1516 return;
1517 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001518 if (systemName != "system")
1519 {
1520 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1521 systemName);
1522 return;
1523 }
1524
Ed Tanous002d39b2022-05-31 08:59:27 -07001525 // Collections don't include the static data added by SubRoute
1526 // because it has a duplicate entry for members
1527 asyncResp->res.jsonValue["@odata.type"] =
1528 "#LogEntryCollection.LogEntryCollection";
1529 asyncResp->res.jsonValue["@odata.id"] =
1530 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1531 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1532 asyncResp->res.jsonValue["Description"] =
1533 "Collection of System Event Log Entries";
1534
1535 // DBus implementation of EventLog/Entries
1536 // Make call to Logging Service to find all log entry objects
1537 crow::connections::systemBus->async_method_call(
1538 [asyncResp](const boost::system::error_code ec,
1539 const dbus::utility::ManagedObjectType& resp) {
1540 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001541 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001542 // TODO Handle for specific error code
1543 BMCWEB_LOG_ERROR
1544 << "getLogEntriesIfaceData resp_handler got error " << ec;
1545 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001546 return;
1547 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001548 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1549 entriesArray = nlohmann::json::array();
1550 for (const auto& objectPath : resp)
1551 {
1552 const uint32_t* id = nullptr;
1553 const uint64_t* timestamp = nullptr;
1554 const uint64_t* updateTimestamp = nullptr;
1555 const std::string* severity = nullptr;
1556 const std::string* message = nullptr;
1557 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001558 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001559 bool resolved = false;
1560 for (const auto& interfaceMap : objectPath.second)
1561 {
1562 if (interfaceMap.first ==
1563 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001564 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001565 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001566 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001567 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001568 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001569 id = std::get_if<uint32_t>(&propertyMap.second);
1570 }
1571 else if (propertyMap.first == "Timestamp")
1572 {
1573 timestamp =
1574 std::get_if<uint64_t>(&propertyMap.second);
1575 }
1576 else if (propertyMap.first == "UpdateTimestamp")
1577 {
1578 updateTimestamp =
1579 std::get_if<uint64_t>(&propertyMap.second);
1580 }
1581 else if (propertyMap.first == "Severity")
1582 {
1583 severity = std::get_if<std::string>(
1584 &propertyMap.second);
1585 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001586 else if (propertyMap.first == "Resolution")
1587 {
1588 resolution = std::get_if<std::string>(
1589 &propertyMap.second);
1590 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001591 else if (propertyMap.first == "Message")
1592 {
1593 message = std::get_if<std::string>(
1594 &propertyMap.second);
1595 }
1596 else if (propertyMap.first == "Resolved")
1597 {
1598 const bool* resolveptr =
1599 std::get_if<bool>(&propertyMap.second);
1600 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001601 {
1602 messages::internalError(asyncResp->res);
1603 return;
1604 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001605 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001606 }
1607 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001608 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001609 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001610 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001611 messages::internalError(asyncResp->res);
1612 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001613 }
1614 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001615 else if (interfaceMap.first ==
1616 "xyz.openbmc_project.Common.FilePath")
1617 {
1618 for (const auto& propertyMap : interfaceMap.second)
1619 {
1620 if (propertyMap.first == "Path")
1621 {
1622 filePath = std::get_if<std::string>(
1623 &propertyMap.second);
1624 }
1625 }
1626 }
1627 }
1628 // Object path without the
1629 // xyz.openbmc_project.Logging.Entry interface, ignore
1630 // and continue.
1631 if (id == nullptr || message == nullptr ||
1632 severity == nullptr || timestamp == nullptr ||
1633 updateTimestamp == nullptr)
1634 {
1635 continue;
1636 }
1637 entriesArray.push_back({});
1638 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001639 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001640 thisEntry["@odata.id"] =
1641 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1642 std::to_string(*id);
1643 thisEntry["Name"] = "System Event Log Entry";
1644 thisEntry["Id"] = std::to_string(*id);
1645 thisEntry["Message"] = *message;
1646 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001647 if ((resolution != nullptr) && (!(*resolution).empty()))
1648 {
1649 thisEntry["Resolution"] = *resolution;
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;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001729
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001730 const bool success = sdbusplus::unpackPropertiesNoThrow(
1731 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1732 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001733 severity, "Message", message, "Resolved", resolved,
1734 "Resolution", resolution, "Path", filePath);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001735
1736 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001737 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001738 messages::internalError(asyncResp->res);
1739 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001740 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001741
Ed Tanous002d39b2022-05-31 08:59:27 -07001742 if (id == nullptr || message == nullptr || severity == nullptr ||
1743 timestamp == nullptr || updateTimestamp == nullptr)
1744 {
1745 messages::internalError(asyncResp->res);
1746 return;
1747 }
1748 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001749 "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001750 asyncResp->res.jsonValue["@odata.id"] =
1751 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1752 std::to_string(*id);
1753 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1754 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1755 asyncResp->res.jsonValue["Message"] = *message;
1756 asyncResp->res.jsonValue["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001757 if ((resolution != nullptr) && (!(*resolution).empty()))
1758 {
1759 asyncResp->res.jsonValue["Resolution"] = *resolution;
1760 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001761 asyncResp->res.jsonValue["EntryType"] = "Event";
1762 asyncResp->res.jsonValue["Severity"] =
1763 translateSeverityDbusToRedfish(*severity);
1764 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001765 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001766 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001767 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001768 if (filePath != nullptr)
1769 {
1770 asyncResp->res.jsonValue["AdditionalDataURI"] =
1771 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1772 std::to_string(*id) + "/attachment";
1773 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001774 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001775 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001776
1777 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001778 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001779 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001780 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001781 [&app](const crow::Request& req,
1782 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001783 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001784 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 {
1786 return;
1787 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001788 if (systemName != "system")
1789 {
1790 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1791 systemName);
1792 return;
1793 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001794 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001795
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1797 resolved))
1798 {
1799 return;
1800 }
1801 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001802
Ed Tanous002d39b2022-05-31 08:59:27 -07001803 crow::connections::systemBus->async_method_call(
1804 [asyncResp, entryId](const boost::system::error_code ec) {
1805 if (ec)
1806 {
1807 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1808 messages::internalError(asyncResp->res);
1809 return;
1810 }
1811 },
1812 "xyz.openbmc_project.Logging",
1813 "/xyz/openbmc_project/logging/entry/" + entryId,
1814 "org.freedesktop.DBus.Properties", "Set",
1815 "xyz.openbmc_project.Logging.Entry", "Resolved",
1816 dbus::utility::DbusVariantType(*resolved));
1817 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001818
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001819 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001820 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001821 .privileges(redfish::privileges::deleteLogEntry)
1822
Ed Tanous002d39b2022-05-31 08:59:27 -07001823 .methods(boost::beast::http::verb::delete_)(
1824 [&app](const crow::Request& req,
1825 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001826 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001827 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001828 {
1829 return;
1830 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001831 if (systemName != "system")
1832 {
1833 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1834 systemName);
1835 return;
1836 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001837 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1838
1839 std::string entryID = param;
1840
1841 dbus::utility::escapePathForDbus(entryID);
1842
1843 // Process response from Logging service.
1844 auto respHandler =
1845 [asyncResp, entryID](const boost::system::error_code ec) {
1846 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1847 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001848 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001849 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001850 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001851 messages::resourceNotFound(asyncResp->res, "LogEntry",
1852 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001853 return;
1854 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001855 // TODO Handle for specific error code
1856 BMCWEB_LOG_ERROR
1857 << "EventLogEntry (DBus) doDelete respHandler got error "
1858 << ec;
1859 asyncResp->res.result(
1860 boost::beast::http::status::internal_server_error);
1861 return;
1862 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001863
Ed Tanous002d39b2022-05-31 08:59:27 -07001864 asyncResp->res.result(boost::beast::http::status::ok);
1865 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001866
Ed Tanous002d39b2022-05-31 08:59:27 -07001867 // Make call to Logging service to request Delete Log
1868 crow::connections::systemBus->async_method_call(
1869 respHandler, "xyz.openbmc_project.Logging",
1870 "/xyz/openbmc_project/logging/entry/" + entryID,
1871 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001872 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001873}
1874
1875inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001876{
George Liu0fda0f12021-11-16 10:06:17 +08001877 BMCWEB_ROUTE(
1878 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001879 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001880 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001881 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001882 [&app](const crow::Request& req,
1883 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001884 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001885 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001886 {
1887 return;
1888 }
Ed Tanous99351cd2022-08-07 16:42:51 -07001889 if (http_helpers::isContentTypeAllowed(
1890 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001891 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001892 {
1893 asyncResp->res.result(boost::beast::http::status::bad_request);
1894 return;
1895 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001896 if (systemName != "system")
1897 {
1898 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1899 systemName);
1900 return;
1901 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001902
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 std::string entryID = param;
1904 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001905
Ed Tanous002d39b2022-05-31 08:59:27 -07001906 crow::connections::systemBus->async_method_call(
1907 [asyncResp, entryID](const boost::system::error_code ec,
1908 const sdbusplus::message::unix_fd& unixfd) {
1909 if (ec.value() == EBADR)
1910 {
1911 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1912 entryID);
1913 return;
1914 }
1915 if (ec)
1916 {
1917 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1918 messages::internalError(asyncResp->res);
1919 return;
1920 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001921
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 int fd = -1;
1923 fd = dup(unixfd);
1924 if (fd == -1)
1925 {
1926 messages::internalError(asyncResp->res);
1927 return;
1928 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001929
Ed Tanous002d39b2022-05-31 08:59:27 -07001930 long long int size = lseek(fd, 0, SEEK_END);
1931 if (size == -1)
1932 {
1933 messages::internalError(asyncResp->res);
1934 return;
1935 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001936
Ed Tanous002d39b2022-05-31 08:59:27 -07001937 // Arbitrary max size of 64kb
1938 constexpr int maxFileSize = 65536;
1939 if (size > maxFileSize)
1940 {
1941 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1942 << maxFileSize;
1943 messages::internalError(asyncResp->res);
1944 return;
1945 }
1946 std::vector<char> data(static_cast<size_t>(size));
1947 long long int rc = lseek(fd, 0, SEEK_SET);
1948 if (rc == -1)
1949 {
1950 messages::internalError(asyncResp->res);
1951 return;
1952 }
1953 rc = read(fd, data.data(), data.size());
1954 if ((rc == -1) || (rc != size))
1955 {
1956 messages::internalError(asyncResp->res);
1957 return;
1958 }
1959 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001960
Ed Tanous002d39b2022-05-31 08:59:27 -07001961 std::string_view strData(data.data(), data.size());
1962 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001963
Ed Tanousd9f6c622022-03-17 09:12:17 -07001964 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07001965 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07001966 asyncResp->res.addHeader(
1967 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07001968 asyncResp->res.body() = std::move(output);
1969 },
1970 "xyz.openbmc_project.Logging",
1971 "/xyz/openbmc_project/logging/entry/" + entryID,
1972 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1973 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001974}
1975
Spencer Kub7028eb2021-10-26 15:27:35 +08001976constexpr const char* hostLoggerFolderPath = "/var/log/console";
1977
1978inline bool
1979 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1980 std::vector<std::filesystem::path>& hostLoggerFiles)
1981{
1982 std::error_code ec;
1983 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1984 if (ec)
1985 {
1986 BMCWEB_LOG_ERROR << ec.message();
1987 return false;
1988 }
1989 for (const std::filesystem::directory_entry& it : logPath)
1990 {
1991 std::string filename = it.path().filename();
1992 // Prefix of each log files is "log". Find the file and save the
1993 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07001994 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08001995 {
1996 hostLoggerFiles.emplace_back(it.path());
1997 }
1998 }
1999 // As the log files rotate, they are appended with a ".#" that is higher for
2000 // the older logs. Since we start from oldest logs, sort the name in
2001 // descending order.
2002 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2003 AlphanumLess<std::string>());
2004
2005 return true;
2006}
2007
Ed Tanous02cad962022-06-30 16:50:15 -07002008inline bool getHostLoggerEntries(
2009 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2010 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002011{
2012 GzFileReader logFile;
2013
2014 // Go though all log files and expose host logs.
2015 for (const std::filesystem::path& it : hostLoggerFiles)
2016 {
2017 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2018 {
2019 BMCWEB_LOG_ERROR << "fail to expose host logs";
2020 return false;
2021 }
2022 }
2023 // Get lastMessage from constructor by getter
2024 std::string lastMessage = logFile.getLastMessage();
2025 if (!lastMessage.empty())
2026 {
2027 logCount++;
2028 if (logCount > skip && logCount <= (skip + top))
2029 {
2030 logEntries.push_back(lastMessage);
2031 }
2032 }
2033 return true;
2034}
2035
2036inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2037 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002038 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002039{
2040 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002041 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002042 logEntryJson["@odata.id"] =
2043 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
2044 logEntryID;
2045 logEntryJson["Name"] = "Host Logger Entry";
2046 logEntryJson["Id"] = logEntryID;
2047 logEntryJson["Message"] = msg;
2048 logEntryJson["EntryType"] = "Oem";
2049 logEntryJson["Severity"] = "OK";
2050 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002051}
2052
2053inline void requestRoutesSystemHostLogger(App& app)
2054{
Ed Tanous22d268c2022-05-19 09:39:07 -07002055 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002056 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002057 .methods(boost::beast::http::verb::get)(
2058 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002059 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2060 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002061 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002062 {
2063 return;
2064 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002065 if (systemName != "system")
2066 {
2067 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2068 systemName);
2069 return;
2070 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002071 asyncResp->res.jsonValue["@odata.id"] =
2072 "/redfish/v1/Systems/system/LogServices/HostLogger";
2073 asyncResp->res.jsonValue["@odata.type"] =
2074 "#LogService.v1_1_0.LogService";
2075 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2076 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2077 asyncResp->res.jsonValue["Id"] = "HostLogger";
2078 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2079 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2080 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002081}
2082
2083inline void requestRoutesSystemHostLoggerCollection(App& app)
2084{
2085 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002086 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002087 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002088 .methods(boost::beast::http::verb::get)(
2089 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002090 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2091 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002092 query_param::QueryCapabilities capabilities = {
2093 .canDelegateTop = true,
2094 .canDelegateSkip = true,
2095 };
2096 query_param::Query delegatedQuery;
2097 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002098 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002099 {
2100 return;
2101 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002102 if (systemName != "system")
2103 {
2104 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2105 systemName);
2106 return;
2107 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 asyncResp->res.jsonValue["@odata.id"] =
2109 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2110 asyncResp->res.jsonValue["@odata.type"] =
2111 "#LogEntryCollection.LogEntryCollection";
2112 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2113 asyncResp->res.jsonValue["Description"] =
2114 "Collection of HostLogger Entries";
2115 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2116 logEntryArray = nlohmann::json::array();
2117 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002118
Ed Tanous002d39b2022-05-31 08:59:27 -07002119 std::vector<std::filesystem::path> hostLoggerFiles;
2120 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2121 {
2122 BMCWEB_LOG_ERROR << "fail to get host log file path";
2123 return;
2124 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002125 // If we weren't provided top and skip limits, use the defaults.
2126 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002127 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002128 size_t logCount = 0;
2129 // This vector only store the entries we want to expose that
2130 // control by skip and top.
2131 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002132 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2133 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002134 {
2135 messages::internalError(asyncResp->res);
2136 return;
2137 }
2138 // If vector is empty, that means skip value larger than total
2139 // log count
2140 if (logEntries.empty())
2141 {
2142 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2143 return;
2144 }
2145 if (!logEntries.empty())
2146 {
2147 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002148 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002149 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002150 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2151 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002152 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002153 }
2154
Ed Tanous002d39b2022-05-31 08:59:27 -07002155 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002156 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002157 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2159 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002160 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002161 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002162 }
George Liu0fda0f12021-11-16 10:06:17 +08002163 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002164}
2165
2166inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2167{
2168 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002169 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002170 .privileges(redfish::privileges::getLogEntry)
2171 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002172 [&app](const crow::Request& req,
2173 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002174 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002175 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002176 {
2177 return;
2178 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002179 if (systemName != "system")
2180 {
2181 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2182 systemName);
2183 return;
2184 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002185 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002186
Ed Tanous002d39b2022-05-31 08:59:27 -07002187 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002188
Ed Tanous002d39b2022-05-31 08:59:27 -07002189 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2190 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08002191
Ed Tanous002d39b2022-05-31 08:59:27 -07002192 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002193 if (ec == std::errc::invalid_argument ||
2194 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002195 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002196 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002197 return;
2198 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002199
Ed Tanous002d39b2022-05-31 08:59:27 -07002200 std::vector<std::filesystem::path> hostLoggerFiles;
2201 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2202 {
2203 BMCWEB_LOG_ERROR << "fail to get host log file path";
2204 return;
2205 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002206
Ed Tanous002d39b2022-05-31 08:59:27 -07002207 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002208 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002209 std::vector<std::string> logEntries;
2210 // We can get specific entry by skip and top. For example, if we
2211 // want to get nth entry, we can set skip = n-1 and top = 1 to
2212 // get that entry
2213 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2214 logCount))
2215 {
2216 messages::internalError(asyncResp->res);
2217 return;
2218 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002219
Ed Tanous002d39b2022-05-31 08:59:27 -07002220 if (!logEntries.empty())
2221 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002222 nlohmann::json::object_t hostLogEntry;
2223 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2224 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002225 return;
2226 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002227
Ed Tanous002d39b2022-05-31 08:59:27 -07002228 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002229 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002230 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002231}
2232
Claire Weinanfdd26902022-03-01 14:18:25 -08002233constexpr char const* dumpManagerIface =
2234 "xyz.openbmc_project.Collection.DeleteAll";
Claire Weinandd72e872022-08-15 14:20:06 -07002235inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002236 crow::App& app, const crow::Request& req,
2237 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2238{
2239 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2240 {
2241 return;
2242 }
2243 // Collections don't include the static data added by SubRoute
2244 // because it has a duplicate entry for members
2245 asyncResp->res.jsonValue["@odata.type"] =
2246 "#LogServiceCollection.LogServiceCollection";
2247 asyncResp->res.jsonValue["@odata.id"] =
2248 "/redfish/v1/Managers/bmc/LogServices";
2249 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2250 asyncResp->res.jsonValue["Description"] =
2251 "Collection of LogServices for this Manager";
2252 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2253 logServiceArray = nlohmann::json::array();
2254
2255#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002256 nlohmann::json::object_t journal;
2257 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2258 logServiceArray.push_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002259#endif
2260
2261 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2262
2263#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2264 auto respHandler =
2265 [asyncResp](
2266 const boost::system::error_code ec,
2267 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2268 if (ec)
2269 {
2270 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002271 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002272 << ec;
2273 // Assume that getting an error simply means there are no dump
2274 // LogServices. Return without adding any error response.
2275 return;
2276 }
2277
2278 nlohmann::json& logServiceArrayLocal =
2279 asyncResp->res.jsonValue["Members"];
2280
2281 for (const std::string& path : subTreePaths)
2282 {
2283 if (path == "/xyz/openbmc_project/dump/bmc")
2284 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002285 nlohmann::json::object_t member;
2286 member["@odata.id"] =
2287 "/redfish/v1/Managers/bmc/LogServices/Dump";
2288 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002289 }
2290 else if (path == "/xyz/openbmc_project/dump/faultlog")
2291 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002292 nlohmann::json::object_t member;
2293 member["@odata.id"] =
2294 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2295 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002296 }
2297 }
2298
2299 asyncResp->res.jsonValue["Members@odata.count"] =
2300 logServiceArrayLocal.size();
2301 };
2302
2303 crow::connections::systemBus->async_method_call(
2304 respHandler, "xyz.openbmc_project.ObjectMapper",
2305 "/xyz/openbmc_project/object_mapper",
2306 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2307 "/xyz/openbmc_project/dump", 0,
2308 std::array<const char*, 1>{dumpManagerIface});
2309#endif
2310}
2311
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002312inline void requestRoutesBMCLogServiceCollection(App& app)
2313{
2314 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002315 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002316 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002317 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002318}
Ed Tanous1da66f72018-07-27 16:13:37 -07002319
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002320inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002321{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002322 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002323 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002324 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002325 [&app](const crow::Request& req,
2326 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002327 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002328 {
2329 return;
2330 }
2331 asyncResp->res.jsonValue["@odata.type"] =
2332 "#LogService.v1_1_0.LogService";
2333 asyncResp->res.jsonValue["@odata.id"] =
2334 "/redfish/v1/Managers/bmc/LogServices/Journal";
2335 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2336 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2337 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2338 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302339
Ed Tanous002d39b2022-05-31 08:59:27 -07002340 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002341 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002342 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2343 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2344 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302345
Ed Tanous002d39b2022-05-31 08:59:27 -07002346 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2347 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2348 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002349}
Jason M. Billse1f26342018-07-18 12:12:00 -07002350
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002351static int
2352 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2353 sd_journal* journal,
2354 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002355{
2356 // Get the Log Entry contents
2357 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002358
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002359 std::string message;
2360 std::string_view syslogID;
2361 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2362 if (ret < 0)
2363 {
2364 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2365 << strerror(-ret);
2366 }
2367 if (!syslogID.empty())
2368 {
2369 message += std::string(syslogID) + ": ";
2370 }
2371
Ed Tanous39e77502019-03-04 17:35:53 -08002372 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002373 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002374 if (ret < 0)
2375 {
2376 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2377 return 1;
2378 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002379 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002380
2381 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002382 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002383 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002384 if (ret < 0)
2385 {
2386 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002387 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002388
2389 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002390 std::string entryTimeStr;
2391 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002392 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002393 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002394 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002395
2396 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002397 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07002398 bmcJournalLogEntryJson["@odata.id"] =
2399 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2400 bmcJournalLogEntryID;
2401 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2402 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2403 bmcJournalLogEntryJson["Message"] = std::move(message);
2404 bmcJournalLogEntryJson["EntryType"] = "Oem";
2405 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2406 : severity <= 4 ? "Warning"
2407 : "OK";
2408 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2409 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002410 return 0;
2411}
2412
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002413inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002414{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002415 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002416 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002417 .methods(boost::beast::http::verb::get)(
2418 [&app](const crow::Request& req,
2419 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2420 query_param::QueryCapabilities capabilities = {
2421 .canDelegateTop = true,
2422 .canDelegateSkip = true,
2423 };
2424 query_param::Query delegatedQuery;
2425 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002426 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002427 {
2428 return;
2429 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002430
2431 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002432 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002433
Ed Tanous002d39b2022-05-31 08:59:27 -07002434 // Collections don't include the static data added by SubRoute
2435 // because it has a duplicate entry for members
2436 asyncResp->res.jsonValue["@odata.type"] =
2437 "#LogEntryCollection.LogEntryCollection";
2438 asyncResp->res.jsonValue["@odata.id"] =
2439 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2440 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2441 asyncResp->res.jsonValue["Description"] =
2442 "Collection of BMC Journal Entries";
2443 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2444 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002445
Ed Tanous002d39b2022-05-31 08:59:27 -07002446 // Go through the journal and use the timestamp to create a
2447 // unique ID for each entry
2448 sd_journal* journalTmp = nullptr;
2449 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2450 if (ret < 0)
2451 {
2452 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2453 messages::internalError(asyncResp->res);
2454 return;
2455 }
2456 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2457 journalTmp, sd_journal_close);
2458 journalTmp = nullptr;
2459 uint64_t entryCount = 0;
2460 // Reset the unique ID on the first entry
2461 bool firstEntry = true;
2462 SD_JOURNAL_FOREACH(journal.get())
2463 {
2464 entryCount++;
2465 // Handle paging using skip (number of entries to skip from
2466 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002467 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002468 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002469 continue;
2470 }
2471
2472 std::string idStr;
2473 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2474 {
2475 continue;
2476 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002477 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002478
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002479 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002480 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2481 bmcJournalLogEntry) != 0)
2482 {
George Liu0fda0f12021-11-16 10:06:17 +08002483 messages::internalError(asyncResp->res);
2484 return;
2485 }
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002486 logEntryArray.push_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002487 }
2488 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002489 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002490 {
2491 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2492 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002493 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002494 }
George Liu0fda0f12021-11-16 10:06:17 +08002495 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002496}
Jason M. Billse1f26342018-07-18 12:12:00 -07002497
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002498inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002499{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002500 BMCWEB_ROUTE(app,
2501 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002502 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002503 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002504 [&app](const crow::Request& req,
2505 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2506 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002507 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002508 {
2509 return;
2510 }
2511 // Convert the unique ID back to a timestamp to find the entry
2512 uint64_t ts = 0;
2513 uint64_t index = 0;
2514 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2515 {
2516 return;
2517 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002518
Ed Tanous002d39b2022-05-31 08:59:27 -07002519 sd_journal* journalTmp = nullptr;
2520 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2521 if (ret < 0)
2522 {
2523 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2524 messages::internalError(asyncResp->res);
2525 return;
2526 }
2527 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2528 journalTmp, sd_journal_close);
2529 journalTmp = nullptr;
2530 // Go to the timestamp in the log and move to the entry at the
2531 // index tracking the unique ID
2532 std::string idStr;
2533 bool firstEntry = true;
2534 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2535 if (ret < 0)
2536 {
2537 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2538 << strerror(-ret);
2539 messages::internalError(asyncResp->res);
2540 return;
2541 }
2542 for (uint64_t i = 0; i <= index; i++)
2543 {
2544 sd_journal_next(journal.get());
2545 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2546 {
2547 messages::internalError(asyncResp->res);
2548 return;
2549 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002550 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002551 }
2552 // Confirm that the entry ID matches what was requested
2553 if (idStr != entryID)
2554 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002555 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002556 return;
2557 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002558
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002559 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002560 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002561 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002562 {
2563 messages::internalError(asyncResp->res);
2564 return;
2565 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002566 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002567 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002568}
2569
Claire Weinanfdd26902022-03-01 14:18:25 -08002570inline void
2571 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2572 const std::string& dumpType)
2573{
2574 std::string dumpPath;
2575 std::string overWritePolicy;
2576 bool collectDiagnosticDataSupported = false;
2577
2578 if (dumpType == "BMC")
2579 {
2580 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2581 overWritePolicy = "WrapsWhenFull";
2582 collectDiagnosticDataSupported = true;
2583 }
2584 else if (dumpType == "FaultLog")
2585 {
2586 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2587 overWritePolicy = "Unknown";
2588 collectDiagnosticDataSupported = false;
2589 }
2590 else if (dumpType == "System")
2591 {
2592 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2593 overWritePolicy = "WrapsWhenFull";
2594 collectDiagnosticDataSupported = true;
2595 }
2596 else
2597 {
2598 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2599 << dumpType;
2600 messages::internalError(asyncResp->res);
2601 return;
2602 }
2603
2604 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2605 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2606 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2607 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2608 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2609 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2610
2611 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002612 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002613 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2614 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2615 redfishDateTimeOffset.second;
2616
2617 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2618 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2619 dumpPath + "/Actions/LogService.ClearLog";
2620
2621 if (collectDiagnosticDataSupported)
2622 {
2623 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2624 ["target"] =
2625 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2626 }
2627}
2628
2629inline void handleLogServicesDumpServiceGet(
2630 crow::App& app, const std::string& dumpType, const crow::Request& req,
2631 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2632{
2633 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2634 {
2635 return;
2636 }
2637 getDumpServiceInfo(asyncResp, dumpType);
2638}
2639
Ed Tanous22d268c2022-05-19 09:39:07 -07002640inline void handleLogServicesDumpServiceComputerSystemGet(
2641 crow::App& app, const crow::Request& req,
2642 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2643 const std::string& chassisId)
2644{
2645 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2646 {
2647 return;
2648 }
2649 if (chassisId != "system")
2650 {
2651 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2652 return;
2653 }
2654 getDumpServiceInfo(asyncResp, "System");
2655}
2656
Claire Weinanfdd26902022-03-01 14:18:25 -08002657inline void handleLogServicesDumpEntriesCollectionGet(
2658 crow::App& app, const std::string& dumpType, const crow::Request& req,
2659 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2660{
2661 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2662 {
2663 return;
2664 }
2665 getDumpEntryCollection(asyncResp, dumpType);
2666}
2667
Ed Tanous22d268c2022-05-19 09:39:07 -07002668inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2669 crow::App& app, const crow::Request& req,
2670 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2671 const std::string& chassisId)
2672{
2673 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2674 {
2675 return;
2676 }
2677 if (chassisId != "system")
2678 {
2679 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2680 return;
2681 }
2682 getDumpEntryCollection(asyncResp, "System");
2683}
2684
Claire Weinanfdd26902022-03-01 14:18:25 -08002685inline void handleLogServicesDumpEntryGet(
2686 crow::App& app, const std::string& dumpType, const crow::Request& req,
2687 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2688 const std::string& dumpId)
2689{
2690 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2691 {
2692 return;
2693 }
2694 getDumpEntryById(asyncResp, dumpId, dumpType);
2695}
Ed Tanous22d268c2022-05-19 09:39:07 -07002696inline void handleLogServicesDumpEntryComputerSystemGet(
2697 crow::App& app, const crow::Request& req,
2698 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2699 const std::string& chassisId, const std::string& dumpId)
2700{
2701 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2702 {
2703 return;
2704 }
2705 if (chassisId != "system")
2706 {
2707 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2708 return;
2709 }
2710 getDumpEntryById(asyncResp, dumpId, "System");
2711}
Claire Weinanfdd26902022-03-01 14:18:25 -08002712
2713inline void handleLogServicesDumpEntryDelete(
2714 crow::App& app, const std::string& dumpType, const crow::Request& req,
2715 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2716 const std::string& dumpId)
2717{
2718 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2719 {
2720 return;
2721 }
2722 deleteDumpEntry(asyncResp, dumpId, dumpType);
2723}
2724
Ed Tanous22d268c2022-05-19 09:39:07 -07002725inline void handleLogServicesDumpEntryComputerSystemDelete(
2726 crow::App& app, const crow::Request& req,
2727 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2728 const std::string& chassisId, const std::string& dumpId)
2729{
2730 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2731 {
2732 return;
2733 }
2734 if (chassisId != "system")
2735 {
2736 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2737 return;
2738 }
2739 deleteDumpEntry(asyncResp, dumpId, "System");
2740}
2741
Claire Weinanfdd26902022-03-01 14:18:25 -08002742inline void handleLogServicesDumpCollectDiagnosticDataPost(
2743 crow::App& app, const std::string& dumpType, const crow::Request& req,
2744 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2745{
2746 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2747 {
2748 return;
2749 }
2750 createDump(asyncResp, req, dumpType);
2751}
2752
Ed Tanous22d268c2022-05-19 09:39:07 -07002753inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2754 crow::App& app, const crow::Request& req,
2755 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2756 const std::string& chassisId)
2757{
2758 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2759 {
2760 return;
2761 }
2762 if (chassisId != "system")
2763 {
2764 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2765 return;
2766 }
2767 createDump(asyncResp, req, "System");
2768}
2769
Claire Weinanfdd26902022-03-01 14:18:25 -08002770inline void handleLogServicesDumpClearLogPost(
2771 crow::App& app, const std::string& dumpType, const crow::Request& req,
2772 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2773{
2774 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2775 {
2776 return;
2777 }
2778 clearDump(asyncResp, dumpType);
2779}
2780
Ed Tanous22d268c2022-05-19 09:39:07 -07002781inline void handleLogServicesDumpClearLogComputerSystemPost(
2782 crow::App& app, const crow::Request& req,
2783 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2784 const std::string& chassisId)
2785{
2786 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2787 {
2788 return;
2789 }
2790 if (chassisId != "system")
2791 {
2792 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2793 return;
2794 }
2795 clearDump(asyncResp, "System");
2796}
2797
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002798inline void requestRoutesBMCDumpService(App& app)
2799{
2800 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002801 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002802 .methods(boost::beast::http::verb::get)(std::bind_front(
2803 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002804}
2805
2806inline void requestRoutesBMCDumpEntryCollection(App& app)
2807{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002808 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002809 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002810 .methods(boost::beast::http::verb::get)(std::bind_front(
2811 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002812}
2813
2814inline void requestRoutesBMCDumpEntry(App& app)
2815{
2816 BMCWEB_ROUTE(app,
2817 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002818 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002819 .methods(boost::beast::http::verb::get)(std::bind_front(
2820 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2821
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002822 BMCWEB_ROUTE(app,
2823 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002824 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002825 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2826 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002827}
2828
2829inline void requestRoutesBMCDumpCreate(App& app)
2830{
George Liu0fda0f12021-11-16 10:06:17 +08002831 BMCWEB_ROUTE(
2832 app,
2833 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002834 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002835 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002836 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2837 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002838}
2839
2840inline void requestRoutesBMCDumpClear(App& app)
2841{
George Liu0fda0f12021-11-16 10:06:17 +08002842 BMCWEB_ROUTE(
2843 app,
2844 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002845 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002846 .methods(boost::beast::http::verb::post)(std::bind_front(
2847 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2848}
2849
2850inline void requestRoutesFaultLogDumpService(App& app)
2851{
2852 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2853 .privileges(redfish::privileges::getLogService)
2854 .methods(boost::beast::http::verb::get)(std::bind_front(
2855 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2856}
2857
2858inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2859{
2860 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2861 .privileges(redfish::privileges::getLogEntryCollection)
2862 .methods(boost::beast::http::verb::get)(
2863 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2864 std::ref(app), "FaultLog"));
2865}
2866
2867inline void requestRoutesFaultLogDumpEntry(App& app)
2868{
2869 BMCWEB_ROUTE(app,
2870 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2871 .privileges(redfish::privileges::getLogEntry)
2872 .methods(boost::beast::http::verb::get)(std::bind_front(
2873 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2874
2875 BMCWEB_ROUTE(app,
2876 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2877 .privileges(redfish::privileges::deleteLogEntry)
2878 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2879 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2880}
2881
2882inline void requestRoutesFaultLogDumpClear(App& app)
2883{
2884 BMCWEB_ROUTE(
2885 app,
2886 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2887 .privileges(redfish::privileges::postLogService)
2888 .methods(boost::beast::http::verb::post)(std::bind_front(
2889 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002890}
2891
2892inline void requestRoutesSystemDumpService(App& app)
2893{
Ed Tanous22d268c2022-05-19 09:39:07 -07002894 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002895 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002896 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002897 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002898}
2899
2900inline void requestRoutesSystemDumpEntryCollection(App& app)
2901{
Ed Tanous22d268c2022-05-19 09:39:07 -07002902 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002903 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002904 .methods(boost::beast::http::verb::get)(std::bind_front(
2905 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2906 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002907}
2908
2909inline void requestRoutesSystemDumpEntry(App& app)
2910{
2911 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002912 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002913 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002914 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002915 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002916
2917 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002918 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002919 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002920 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002921 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002922}
2923
2924inline void requestRoutesSystemDumpCreate(App& app)
2925{
George Liu0fda0f12021-11-16 10:06:17 +08002926 BMCWEB_ROUTE(
2927 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002928 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002929 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07002930 .methods(boost::beast::http::verb::post)(std::bind_front(
2931 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
2932 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002933}
2934
2935inline void requestRoutesSystemDumpClear(App& app)
2936{
George Liu0fda0f12021-11-16 10:06:17 +08002937 BMCWEB_ROUTE(
2938 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002939 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002940 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002941 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002942 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002943}
2944
2945inline void requestRoutesCrashdumpService(App& app)
2946{
2947 // Note: Deviated from redfish privilege registry for GET & HEAD
2948 // method for security reasons.
2949 /**
2950 * Functions triggers appropriate requests on DBus
2951 */
Ed Tanous22d268c2022-05-19 09:39:07 -07002952 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002953 // This is incorrect, should be:
2954 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002955 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002956 .methods(boost::beast::http::verb::get)(
2957 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002958 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2959 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002960 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002961 {
2962 return;
2963 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002964 if (systemName != "system")
2965 {
2966 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2967 systemName);
2968 return;
2969 }
2970
Ed Tanous002d39b2022-05-31 08:59:27 -07002971 // Copy over the static data to include the entries added by
2972 // SubRoute
2973 asyncResp->res.jsonValue["@odata.id"] =
2974 "/redfish/v1/Systems/system/LogServices/Crashdump";
2975 asyncResp->res.jsonValue["@odata.type"] =
2976 "#LogService.v1_2_0.LogService";
2977 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2978 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2979 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2980 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2981 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302982
Ed Tanous002d39b2022-05-31 08:59:27 -07002983 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002984 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002985 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2986 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2987 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302988
Ed Tanous002d39b2022-05-31 08:59:27 -07002989 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2990 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2991 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2992 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2993 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2994 ["target"] =
2995 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002996 });
2997}
2998
2999void inline requestRoutesCrashdumpClear(App& app)
3000{
George Liu0fda0f12021-11-16 10:06:17 +08003001 BMCWEB_ROUTE(
3002 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003003 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003004 // This is incorrect, should be:
3005 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003006 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003007 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003008 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003009 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3010 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003011 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003012 {
3013 return;
3014 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003015 if (systemName != "system")
3016 {
3017 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3018 systemName);
3019 return;
3020 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003021 crow::connections::systemBus->async_method_call(
3022 [asyncResp](const boost::system::error_code ec,
3023 const std::string&) {
3024 if (ec)
3025 {
3026 messages::internalError(asyncResp->res);
3027 return;
3028 }
3029 messages::success(asyncResp->res);
3030 },
3031 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3032 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003033}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003034
zhanghch058d1b46d2021-04-01 11:18:24 +08003035static void
3036 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3037 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003038{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003039 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003040 [asyncResp, logID,
3041 &logEntryJson](const boost::system::error_code ec,
3042 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003043 if (ec)
3044 {
3045 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3046 if (ec.value() ==
3047 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003048 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003049 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003050 }
3051 else
3052 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003053 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003054 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003055 return;
3056 }
3057
3058 std::string timestamp{};
3059 std::string filename{};
3060 std::string logfile{};
3061 parseCrashdumpParameters(params, filename, timestamp, logfile);
3062
3063 if (filename.empty() || timestamp.empty())
3064 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003065 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003066 return;
3067 }
3068
3069 std::string crashdumpURI =
3070 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3071 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003072 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003073 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07003074 logEntry["@odata.id"] =
3075 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
3076 logEntry["Name"] = "CPU Crashdump";
3077 logEntry["Id"] = logID;
3078 logEntry["EntryType"] = "Oem";
3079 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3080 logEntry["DiagnosticDataType"] = "OEM";
3081 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3082 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003083
3084 // If logEntryJson references an array of LogEntry resources
3085 // ('Members' list), then push this as a new entry, otherwise set it
3086 // directly
3087 if (logEntryJson.is_array())
3088 {
3089 logEntryJson.push_back(logEntry);
3090 asyncResp->res.jsonValue["Members@odata.count"] =
3091 logEntryJson.size();
3092 }
3093 else
3094 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003095 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003096 }
3097 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003098 sdbusplus::asio::getAllProperties(
3099 *crow::connections::systemBus, crashdumpObject,
3100 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3101 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003102}
3103
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003104inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003105{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003106 // Note: Deviated from redfish privilege registry for GET & HEAD
3107 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003108 /**
3109 * Functions triggers appropriate requests on DBus
3110 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003111 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003112 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003113 // This is incorrect, should be.
3114 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003115 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003116 .methods(boost::beast::http::verb::get)(
3117 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003118 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3119 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003120 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003121 {
3122 return;
3123 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003124 if (systemName != "system")
3125 {
3126 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3127 systemName);
3128 return;
3129 }
3130
Ed Tanous002d39b2022-05-31 08:59:27 -07003131 crow::connections::systemBus->async_method_call(
3132 [asyncResp](const boost::system::error_code ec,
3133 const std::vector<std::string>& resp) {
3134 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003135 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003136 if (ec.value() !=
3137 boost::system::errc::no_such_file_or_directory)
3138 {
3139 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3140 << ec.message();
3141 messages::internalError(asyncResp->res);
3142 return;
3143 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003144 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003145 asyncResp->res.jsonValue["@odata.type"] =
3146 "#LogEntryCollection.LogEntryCollection";
3147 asyncResp->res.jsonValue["@odata.id"] =
3148 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3149 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3150 asyncResp->res.jsonValue["Description"] =
3151 "Collection of Crashdump Entries";
3152 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3153 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003154
Ed Tanous002d39b2022-05-31 08:59:27 -07003155 for (const std::string& path : resp)
3156 {
3157 const sdbusplus::message::object_path objPath(path);
3158 // Get the log ID
3159 std::string logID = objPath.filename();
3160 if (logID.empty())
3161 {
3162 continue;
3163 }
3164 // Add the log entry to the array
3165 logCrashdumpEntry(asyncResp, logID,
3166 asyncResp->res.jsonValue["Members"]);
3167 }
3168 },
3169 "xyz.openbmc_project.ObjectMapper",
3170 "/xyz/openbmc_project/object_mapper",
3171 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
3172 std::array<const char*, 1>{crashdumpInterface});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003173 });
3174}
Ed Tanous1da66f72018-07-27 16:13:37 -07003175
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003176inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003177{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003178 // Note: Deviated from redfish privilege registry for GET & HEAD
3179 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003180
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003181 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003182 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003183 // this is incorrect, should be
3184 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003185 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003186 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003187 [&app](const crow::Request& req,
3188 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003189 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003190 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003191 {
3192 return;
3193 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003194 if (systemName != "system")
3195 {
3196 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3197 systemName);
3198 ;
3199 return;
3200 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003201 const std::string& logID = param;
3202 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3203 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003204}
Ed Tanous1da66f72018-07-27 16:13:37 -07003205
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003206inline void requestRoutesCrashdumpFile(App& app)
3207{
3208 // Note: Deviated from redfish privilege registry for GET & HEAD
3209 // method for security reasons.
3210 BMCWEB_ROUTE(
3211 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003212 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003213 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003214 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003215 [](const crow::Request& req,
3216 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003217 const std::string& systemName, const std::string& logID,
3218 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003219 // Do not call getRedfishRoute here since the crashdump file is not a
3220 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003221
3222 if (systemName != "system")
3223 {
3224 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3225 systemName);
3226 ;
3227 return;
3228 }
3229
Ed Tanous002d39b2022-05-31 08:59:27 -07003230 auto getStoredLogCallback =
3231 [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3232 const boost::system::error_code ec,
3233 const std::vector<
3234 std::pair<std::string, dbus::utility::DbusVariantType>>&
3235 resp) {
3236 if (ec)
3237 {
3238 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3239 messages::internalError(asyncResp->res);
3240 return;
3241 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003242
Ed Tanous002d39b2022-05-31 08:59:27 -07003243 std::string dbusFilename{};
3244 std::string dbusTimestamp{};
3245 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003246
Ed Tanous002d39b2022-05-31 08:59:27 -07003247 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3248 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003249
Ed Tanous002d39b2022-05-31 08:59:27 -07003250 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3251 dbusFilepath.empty())
3252 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003253 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003254 return;
3255 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003256
Ed Tanous002d39b2022-05-31 08:59:27 -07003257 // Verify the file name parameter is correct
3258 if (fileName != dbusFilename)
3259 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003260 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003261 return;
3262 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003263
Ed Tanous002d39b2022-05-31 08:59:27 -07003264 if (!std::filesystem::exists(dbusFilepath))
3265 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003266 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003267 return;
3268 }
3269 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3270 asyncResp->res.body() =
3271 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003272
Ed Tanous002d39b2022-05-31 08:59:27 -07003273 // Configure this to be a file download when accessed
3274 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003275 asyncResp->res.addHeader(
3276 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003277 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003278 sdbusplus::asio::getAllProperties(
3279 *crow::connections::systemBus, crashdumpObject,
3280 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3281 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003282 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003283}
3284
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003285enum class OEMDiagnosticType
3286{
3287 onDemand,
3288 telemetry,
3289 invalid,
3290};
3291
Ed Tanousf7725d72022-03-07 12:46:00 -08003292inline OEMDiagnosticType
3293 getOEMDiagnosticType(const std::string_view& oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003294{
3295 if (oemDiagStr == "OnDemand")
3296 {
3297 return OEMDiagnosticType::onDemand;
3298 }
3299 if (oemDiagStr == "Telemetry")
3300 {
3301 return OEMDiagnosticType::telemetry;
3302 }
3303
3304 return OEMDiagnosticType::invalid;
3305}
3306
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003307inline void requestRoutesCrashdumpCollect(App& app)
3308{
3309 // Note: Deviated from redfish privilege registry for GET & HEAD
3310 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003311 BMCWEB_ROUTE(
3312 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003313 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003314 // The below is incorrect; Should be ConfigureManager
3315 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003316 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003317 .methods(boost::beast::http::verb::post)(
3318 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003319 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3320 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003321 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003322 {
3323 return;
3324 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003325
3326 if (systemName != "system")
3327 {
3328 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3329 systemName);
3330 ;
3331 return;
3332 }
3333
Ed Tanous002d39b2022-05-31 08:59:27 -07003334 std::string diagnosticDataType;
3335 std::string oemDiagnosticDataType;
3336 if (!redfish::json_util::readJsonAction(
3337 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3338 "OEMDiagnosticDataType", oemDiagnosticDataType))
3339 {
3340 return;
3341 }
3342
3343 if (diagnosticDataType != "OEM")
3344 {
3345 BMCWEB_LOG_ERROR
3346 << "Only OEM DiagnosticDataType supported for Crashdump";
3347 messages::actionParameterValueFormatError(
3348 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3349 "CollectDiagnosticData");
3350 return;
3351 }
3352
3353 OEMDiagnosticType oemDiagType =
3354 getOEMDiagnosticType(oemDiagnosticDataType);
3355
3356 std::string iface;
3357 std::string method;
3358 std::string taskMatchStr;
3359 if (oemDiagType == OEMDiagnosticType::onDemand)
3360 {
3361 iface = crashdumpOnDemandInterface;
3362 method = "GenerateOnDemandLog";
3363 taskMatchStr = "type='signal',"
3364 "interface='org.freedesktop.DBus.Properties',"
3365 "member='PropertiesChanged',"
3366 "arg0namespace='com.intel.crashdump'";
3367 }
3368 else if (oemDiagType == OEMDiagnosticType::telemetry)
3369 {
3370 iface = crashdumpTelemetryInterface;
3371 method = "GenerateTelemetryLog";
3372 taskMatchStr = "type='signal',"
3373 "interface='org.freedesktop.DBus.Properties',"
3374 "member='PropertiesChanged',"
3375 "arg0namespace='com.intel.crashdump'";
3376 }
3377 else
3378 {
3379 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3380 << oemDiagnosticDataType;
3381 messages::actionParameterValueFormatError(
3382 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3383 "CollectDiagnosticData");
3384 return;
3385 }
3386
3387 auto collectCrashdumpCallback =
3388 [asyncResp, payload(task::Payload(req)),
3389 taskMatchStr](const boost::system::error_code ec,
3390 const std::string&) mutable {
3391 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003392 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003393 if (ec.value() == boost::system::errc::operation_not_supported)
3394 {
3395 messages::resourceInStandby(asyncResp->res);
3396 }
3397 else if (ec.value() ==
3398 boost::system::errc::device_or_resource_busy)
3399 {
3400 messages::serviceTemporarilyUnavailable(asyncResp->res,
3401 "60");
3402 }
3403 else
3404 {
3405 messages::internalError(asyncResp->res);
3406 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003407 return;
3408 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003409 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -05003410 [](boost::system::error_code err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003411 const std::shared_ptr<task::TaskData>& taskData) {
3412 if (!err)
3413 {
3414 taskData->messages.emplace_back(messages::taskCompletedOK(
3415 std::to_string(taskData->index)));
3416 taskData->state = "Completed";
3417 }
3418 return task::completed;
3419 },
3420 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003421
Ed Tanous002d39b2022-05-31 08:59:27 -07003422 task->startTimer(std::chrono::minutes(5));
3423 task->populateResp(asyncResp->res);
3424 task->payload.emplace(std::move(payload));
3425 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003426
Ed Tanous002d39b2022-05-31 08:59:27 -07003427 crow::connections::systemBus->async_method_call(
3428 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3429 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003430 });
3431}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003432
Andrew Geisslercb92c032018-08-17 07:56:14 -07003433/**
3434 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3435 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003436inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003437{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003438 /**
3439 * Function handles POST method request.
3440 * The Clear Log actions does not require any parameter.The action deletes
3441 * all entries found in the Entries collection for this Log Service.
3442 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003443
George Liu0fda0f12021-11-16 10:06:17 +08003444 BMCWEB_ROUTE(
3445 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003446 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003447 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003448 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003449 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003450 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3451 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003452 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003453 {
3454 return;
3455 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003456 if (systemName != "system")
3457 {
3458 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3459 systemName);
3460 ;
3461 return;
3462 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003463 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003464
Ed Tanous002d39b2022-05-31 08:59:27 -07003465 // Process response from Logging service.
3466 auto respHandler = [asyncResp](const boost::system::error_code ec) {
3467 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3468 if (ec)
3469 {
3470 // TODO Handle for specific error code
3471 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3472 asyncResp->res.result(
3473 boost::beast::http::status::internal_server_error);
3474 return;
3475 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003476
Ed Tanous002d39b2022-05-31 08:59:27 -07003477 asyncResp->res.result(boost::beast::http::status::no_content);
3478 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003479
Ed Tanous002d39b2022-05-31 08:59:27 -07003480 // Make call to Logging service to request Clear Log
3481 crow::connections::systemBus->async_method_call(
3482 respHandler, "xyz.openbmc_project.Logging",
3483 "/xyz/openbmc_project/logging",
3484 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3485 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003486}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003487
3488/****************************************************
3489 * Redfish PostCode interfaces
3490 * using DBUS interface: getPostCodesTS
3491 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003492inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003493{
Ed Tanous22d268c2022-05-19 09:39:07 -07003494 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003495 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003496 .methods(boost::beast::http::verb::get)(
3497 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003498 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3499 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003500 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003501 {
3502 return;
3503 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003504 if (systemName != "system")
3505 {
3506 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3507 systemName);
3508 ;
3509 return;
3510 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003511 asyncResp->res.jsonValue["@odata.id"] =
3512 "/redfish/v1/Systems/system/LogServices/PostCodes";
3513 asyncResp->res.jsonValue["@odata.type"] =
3514 "#LogService.v1_1_0.LogService";
3515 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3516 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3517 asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
3518 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3519 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3520 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303521
Ed Tanous002d39b2022-05-31 08:59:27 -07003522 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003523 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003524 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3525 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3526 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303527
Ed Tanous002d39b2022-05-31 08:59:27 -07003528 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3529 {"target",
3530 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003531 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003532}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003533
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003534inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003535{
George Liu0fda0f12021-11-16 10:06:17 +08003536 BMCWEB_ROUTE(
3537 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003538 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003539 // The following privilege is incorrect; It should be ConfigureManager
3540 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003541 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003542 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003543 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003544 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3545 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003546 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003547 {
3548 return;
3549 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003550 if (systemName != "system")
3551 {
3552 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3553 systemName);
3554 ;
3555 return;
3556 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003557 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003558
Ed Tanous002d39b2022-05-31 08:59:27 -07003559 // Make call to post-code service to request clear all
3560 crow::connections::systemBus->async_method_call(
3561 [asyncResp](const boost::system::error_code ec) {
3562 if (ec)
3563 {
3564 // TODO Handle for specific error code
3565 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3566 << ec;
3567 asyncResp->res.result(
3568 boost::beast::http::status::internal_server_error);
3569 messages::internalError(asyncResp->res);
3570 return;
3571 }
3572 },
3573 "xyz.openbmc_project.State.Boot.PostCode0",
3574 "/xyz/openbmc_project/State/Boot/PostCode0",
3575 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3576 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003577}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003578
3579static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003580 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303581 const boost::container::flat_map<
3582 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003583 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3584 const uint64_t skip = 0, const uint64_t top = 0)
3585{
3586 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003587 const registries::Message* message =
3588 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003589
3590 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003591 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003592
3593 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303594 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3595 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003596 {
3597 currentCodeIndex++;
3598 std::string postcodeEntryID =
3599 "B" + std::to_string(bootIndex) + "-" +
3600 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3601
3602 uint64_t usecSinceEpoch = code.first;
3603 uint64_t usTimeOffset = 0;
3604
3605 if (1 == currentCodeIndex)
3606 { // already incremented
3607 firstCodeTimeUs = code.first;
3608 }
3609 else
3610 {
3611 usTimeOffset = code.first - firstCodeTimeUs;
3612 }
3613
3614 // skip if no specific codeIndex is specified and currentCodeIndex does
3615 // not fall between top and skip
3616 if ((codeIndex == 0) &&
3617 (currentCodeIndex <= skip || currentCodeIndex > top))
3618 {
3619 continue;
3620 }
3621
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003622 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003623 // currentIndex
3624 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3625 {
3626 // This is done for simplicity. 1st entry is needed to calculate
3627 // time offset. To improve efficiency, one can get to the entry
3628 // directly (possibly with flatmap's nth method)
3629 continue;
3630 }
3631
3632 // currentCodeIndex is within top and skip or equal to specified code
3633 // index
3634
3635 // Get the Created time from the timestamp
3636 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003637 entryTimeStr =
Ed Tanous2b829372022-08-03 14:22:34 -07003638 redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003639
3640 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3641 std::ostringstream hexCode;
3642 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303643 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003644 std::ostringstream timeOffsetStr;
3645 // Set Fixed -Point Notation
3646 timeOffsetStr << std::fixed;
3647 // Set precision to 4 digits
3648 timeOffsetStr << std::setprecision(4);
3649 // Add double to stream
3650 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3651 std::vector<std::string> messageArgs = {
3652 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3653
3654 // Get MessageArgs template from message registry
3655 std::string msg;
3656 if (message != nullptr)
3657 {
3658 msg = message->message;
3659
3660 // fill in this post code value
3661 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003662 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003663 {
3664 std::string argStr = "%" + std::to_string(++i);
3665 size_t argPos = msg.find(argStr);
3666 if (argPos != std::string::npos)
3667 {
3668 msg.replace(argPos, argStr.length(), messageArg);
3669 }
3670 }
3671 }
3672
Tim Leed4342a92020-04-27 11:47:58 +08003673 // Get Severity template from message registry
3674 std::string severity;
3675 if (message != nullptr)
3676 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003677 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003678 }
3679
ZhikuiRena3316fc2020-01-29 14:58:08 -08003680 // add to AsyncResp
3681 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003682 nlohmann::json& bmcLogEntry = logEntryArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05003683 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07003684 bmcLogEntry["@odata.id"] =
3685 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3686 postcodeEntryID;
3687 bmcLogEntry["Name"] = "POST Code Log Entry";
3688 bmcLogEntry["Id"] = postcodeEntryID;
3689 bmcLogEntry["Message"] = std::move(msg);
3690 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3691 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3692 bmcLogEntry["EntryType"] = "Event";
3693 bmcLogEntry["Severity"] = std::move(severity);
3694 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003695 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3696 {
3697 bmcLogEntry["AdditionalDataURI"] =
3698 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3699 postcodeEntryID + "/attachment";
3700 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003701 }
3702}
3703
zhanghch058d1b46d2021-04-01 11:18:24 +08003704static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003705 const uint16_t bootIndex,
3706 const uint64_t codeIndex)
3707{
3708 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303709 [aResp, bootIndex,
3710 codeIndex](const boost::system::error_code ec,
3711 const boost::container::flat_map<
3712 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3713 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003714 if (ec)
3715 {
3716 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3717 messages::internalError(aResp->res);
3718 return;
3719 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003720
Ed Tanous002d39b2022-05-31 08:59:27 -07003721 // skip the empty postcode boots
3722 if (postcode.empty())
3723 {
3724 return;
3725 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003726
Ed Tanous002d39b2022-05-31 08:59:27 -07003727 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003728
Ed Tanous002d39b2022-05-31 08:59:27 -07003729 aResp->res.jsonValue["Members@odata.count"] =
3730 aResp->res.jsonValue["Members"].size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08003731 },
Jonathan Doman15124762021-01-07 17:54:17 -08003732 "xyz.openbmc_project.State.Boot.PostCode0",
3733 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003734 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3735 bootIndex);
3736}
3737
zhanghch058d1b46d2021-04-01 11:18:24 +08003738static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003739 const uint16_t bootIndex,
3740 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003741 const uint64_t entryCount, size_t skip,
3742 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003743{
3744 crow::connections::systemBus->async_method_call(
3745 [aResp, bootIndex, bootCount, entryCount, skip,
3746 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303747 const boost::container::flat_map<
3748 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3749 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003750 if (ec)
3751 {
3752 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3753 messages::internalError(aResp->res);
3754 return;
3755 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003756
Ed Tanous002d39b2022-05-31 08:59:27 -07003757 uint64_t endCount = entryCount;
3758 if (!postcode.empty())
3759 {
3760 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003761 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003762 {
Ed Tanous3648c8b2022-07-25 13:39:59 -07003763 uint64_t thisBootSkip =
3764 std::max(static_cast<uint64_t>(skip), entryCount) -
3765 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003766 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003767 std::min(static_cast<uint64_t>(top + skip), endCount) -
3768 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003769
3770 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3771 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003772 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003773 aResp->res.jsonValue["Members@odata.count"] = endCount;
3774 }
3775
3776 // continue to previous bootIndex
3777 if (bootIndex < bootCount)
3778 {
3779 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3780 bootCount, endCount, skip, top);
3781 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003782 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003783 {
3784 aResp->res.jsonValue["Members@odata.nextLink"] =
3785 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3786 std::to_string(skip + top);
3787 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003788 },
Jonathan Doman15124762021-01-07 17:54:17 -08003789 "xyz.openbmc_project.State.Boot.PostCode0",
3790 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003791 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3792 bootIndex);
3793}
3794
zhanghch058d1b46d2021-04-01 11:18:24 +08003795static void
3796 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003797 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003798{
3799 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003800 sdbusplus::asio::getProperty<uint16_t>(
3801 *crow::connections::systemBus,
3802 "xyz.openbmc_project.State.Boot.PostCode0",
3803 "/xyz/openbmc_project/State/Boot/PostCode0",
3804 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3805 [aResp, entryCount, skip, top](const boost::system::error_code ec,
3806 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003807 if (ec)
3808 {
3809 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3810 messages::internalError(aResp->res);
3811 return;
3812 }
3813 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003814 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003815}
3816
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003817inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003818{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003819 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003820 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003821 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003822 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003823 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003824 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3825 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003826 query_param::QueryCapabilities capabilities = {
3827 .canDelegateTop = true,
3828 .canDelegateSkip = true,
3829 };
3830 query_param::Query delegatedQuery;
3831 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003832 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003833 {
3834 return;
3835 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003836
3837 if (systemName != "system")
3838 {
3839 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3840 systemName);
3841 ;
3842 return;
3843 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003844 asyncResp->res.jsonValue["@odata.type"] =
3845 "#LogEntryCollection.LogEntryCollection";
3846 asyncResp->res.jsonValue["@odata.id"] =
3847 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3848 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3849 asyncResp->res.jsonValue["Description"] =
3850 "Collection of POST Code Log Entries";
3851 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3852 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003853 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003854 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003855 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003856 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003857}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003858
George Liu647b3cd2021-07-05 12:43:56 +08003859/**
3860 * @brief Parse post code ID and get the current value and index value
3861 * eg: postCodeID=B1-2, currentValue=1, index=2
3862 *
3863 * @param[in] postCodeID Post Code ID
3864 * @param[out] currentValue Current value
3865 * @param[out] index Index value
3866 *
3867 * @return bool true if the parsing is successful, false the parsing fails
3868 */
3869inline static bool parsePostCode(const std::string& postCodeID,
3870 uint64_t& currentValue, uint16_t& index)
3871{
3872 std::vector<std::string> split;
3873 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3874 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3875 {
3876 return false;
3877 }
3878
Ed Tanousca45aa32022-01-07 09:28:45 -08003879 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003880 const char* start = split[0].data() + 1;
Ed Tanousca45aa32022-01-07 09:28:45 -08003881 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003882 const char* end = split[0].data() + split[0].size();
3883 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3884
3885 if (ptrIndex != end || ecIndex != std::errc())
3886 {
3887 return false;
3888 }
3889
3890 start = split[1].data();
Ed Tanousca45aa32022-01-07 09:28:45 -08003891
3892 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003893 end = split[1].data() + split[1].size();
3894 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
George Liu647b3cd2021-07-05 12:43:56 +08003895
Tony Lee517d9a52022-06-28 15:41:23 +08003896 return ptrValue == end && ecValue == std::errc();
George Liu647b3cd2021-07-05 12:43:56 +08003897}
3898
3899inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3900{
George Liu0fda0f12021-11-16 10:06:17 +08003901 BMCWEB_ROUTE(
3902 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003903 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003904 .privileges(redfish::privileges::getLogEntry)
3905 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003906 [&app](const crow::Request& req,
3907 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003908 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07003909 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003910 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003911 {
3912 return;
3913 }
Ed Tanous99351cd2022-08-07 16:42:51 -07003914 if (http_helpers::isContentTypeAllowed(
3915 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07003916 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07003917 {
3918 asyncResp->res.result(boost::beast::http::status::bad_request);
3919 return;
3920 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003921 if (systemName != "system")
3922 {
3923 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3924 systemName);
3925 ;
3926 return;
3927 }
George Liu647b3cd2021-07-05 12:43:56 +08003928
Ed Tanous002d39b2022-05-31 08:59:27 -07003929 uint64_t currentValue = 0;
3930 uint16_t index = 0;
3931 if (!parsePostCode(postCodeID, currentValue, index))
3932 {
3933 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3934 return;
3935 }
George Liu647b3cd2021-07-05 12:43:56 +08003936
Ed Tanous002d39b2022-05-31 08:59:27 -07003937 crow::connections::systemBus->async_method_call(
3938 [asyncResp, postCodeID, currentValue](
3939 const boost::system::error_code ec,
3940 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3941 postcodes) {
3942 if (ec.value() == EBADR)
3943 {
3944 messages::resourceNotFound(asyncResp->res, "LogEntry",
3945 postCodeID);
3946 return;
3947 }
3948 if (ec)
3949 {
3950 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3951 messages::internalError(asyncResp->res);
3952 return;
3953 }
George Liu647b3cd2021-07-05 12:43:56 +08003954
Ed Tanous002d39b2022-05-31 08:59:27 -07003955 size_t value = static_cast<size_t>(currentValue) - 1;
3956 if (value == std::string::npos || postcodes.size() < currentValue)
3957 {
3958 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3959 messages::resourceNotFound(asyncResp->res, "LogEntry",
3960 postCodeID);
3961 return;
3962 }
George Liu647b3cd2021-07-05 12:43:56 +08003963
Ed Tanous002d39b2022-05-31 08:59:27 -07003964 const auto& [tID, c] = postcodes[value];
3965 if (c.empty())
3966 {
3967 BMCWEB_LOG_INFO << "No found post code data";
3968 messages::resourceNotFound(asyncResp->res, "LogEntry",
3969 postCodeID);
3970 return;
3971 }
3972 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3973 const char* d = reinterpret_cast<const char*>(c.data());
3974 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08003975
Ed Tanousd9f6c622022-03-17 09:12:17 -07003976 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07003977 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07003978 asyncResp->res.addHeader(
3979 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07003980 asyncResp->res.body() = crow::utility::base64encode(strData);
3981 },
3982 "xyz.openbmc_project.State.Boot.PostCode0",
3983 "/xyz/openbmc_project/State/Boot/PostCode0",
3984 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3985 });
George Liu647b3cd2021-07-05 12:43:56 +08003986}
3987
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003988inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003989{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003990 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003991 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003992 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003993 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003994 [&app](const crow::Request& req,
3995 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003996 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003997 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003998 {
3999 return;
4000 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004001 if (systemName != "system")
4002 {
4003 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4004 systemName);
4005 return;
4006 }
4007
Ed Tanous002d39b2022-05-31 08:59:27 -07004008 uint16_t bootIndex = 0;
4009 uint64_t codeIndex = 0;
4010 if (!parsePostCode(targetID, codeIndex, bootIndex))
4011 {
4012 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08004013 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004014 return;
4015 }
4016 if (bootIndex == 0 || codeIndex == 0)
4017 {
4018 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
4019 << targetID;
4020 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004021
Vijay Lobo9c11a172021-10-07 16:53:16 -05004022 asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07004023 asyncResp->res.jsonValue["@odata.id"] =
4024 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4025 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4026 asyncResp->res.jsonValue["Description"] =
4027 "Collection of POST Code Log Entries";
4028 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4029 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004030
Ed Tanous002d39b2022-05-31 08:59:27 -07004031 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
4032 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004033}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004034
Ed Tanous1da66f72018-07-27 16:13:37 -07004035} // namespace redfish