blob: 7ad489b14153b54e37890b0c2a387e7bc9419e09 [file] [log] [blame]
Ed Tanous1da66f72018-07-27 16:13:37 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "error_messages.hpp"
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -060021#include "generated/enums/log_entry.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080022#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080023#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080024#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "query.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070026#include "registries.hpp"
27#include "registries/base_message_registry.hpp"
28#include "registries/openbmc_message_registry.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include "registries/privilege_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080030#include "task.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080031#include "utils/dbus_utils.hpp"
32#include "utils/time_utils.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070033
Jason M. Billse1f26342018-07-18 12:12:00 -070034#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060035#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060036#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070037
Ed Tanous9896eae2022-07-23 15:07:33 -070038#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070039#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060040#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070041#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070042#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070043#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080044#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070045#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020046#include <sdbusplus/asio/property.hpp>
47#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050048
George Liu7a1dbc42022-12-07 16:03:22 +080049#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080050#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070051#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080052#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070053#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070054#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080055#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070056
57namespace redfish
58{
59
Patrick Williams89492a12023-05-10 07:51:34 -050060constexpr const char* crashdumpObject = "com.intel.crashdump";
61constexpr const char* crashdumpPath = "/com/intel/crashdump";
62constexpr const char* crashdumpInterface = "com.intel.crashdump";
63constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070064 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050065constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070066 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050067constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070068 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070069
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060070enum class DumpCreationProgress
71{
72 DUMP_CREATE_SUCCESS,
73 DUMP_CREATE_FAILED,
74 DUMP_CREATE_INPROGRESS
75};
76
James Feistf6150402019-01-08 10:36:20 -080077namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070078
Gunnar Mills1214b7e2020-06-04 10:11:30 -050079inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070080{
Ed Tanousd4d25792020-09-29 15:15:03 -070081 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
82 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
83 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
84 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070085 {
86 return "Critical";
87 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070088 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
89 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
90 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070091 {
92 return "OK";
93 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070094 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070095 {
96 return "Warning";
97 }
98 return "";
99}
100
Abhishek Patel9017faf2021-09-14 22:48:55 -0500101inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
102{
103 std::optional<bool> notifyAction;
104 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
105 {
106 notifyAction = true;
107 }
108 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
109 {
110 notifyAction = false;
111 }
112
113 return notifyAction;
114}
115
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700116inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800117 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700118 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700119{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500120 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700121 size_t length = 0;
122 int ret = 0;
123 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800124 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
125 const void** dataVoid = reinterpret_cast<const void**>(&data);
126
127 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700128 if (ret < 0)
129 {
130 return ret;
131 }
Ed Tanous39e77502019-03-04 17:35:53 -0800132 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700133 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000134 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700135 return ret;
136}
137
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700138inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800139 std::string_view field, const int& base,
140 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700141{
142 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800143 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700144 // Get the metadata from the requested field of the journal entry
145 ret = getJournalMetadata(journal, field, metadata);
146 if (ret < 0)
147 {
148 return ret;
149 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000150 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700151 return ret;
152}
153
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700154inline static bool getEntryTimestamp(sd_journal* journal,
155 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800156{
157 int ret = 0;
158 uint64_t timestamp = 0;
159 ret = sd_journal_get_realtime_usec(journal, &timestamp);
160 if (ret < 0)
161 {
162 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
163 << strerror(-ret);
164 return false;
165 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300166 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500167 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800168}
Ed Tanous50b8a432022-02-03 16:29:50 -0800169
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
171 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700172{
173 int ret = 0;
174 static uint64_t prevTs = 0;
175 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700176 if (firstEntry)
177 {
178 prevTs = 0;
179 }
180
Jason M. Bills16428a12018-11-02 12:42:29 -0700181 // Get the entry timestamp
182 uint64_t curTs = 0;
183 ret = sd_journal_get_realtime_usec(journal, &curTs);
184 if (ret < 0)
185 {
186 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
187 << strerror(-ret);
188 return false;
189 }
190 // If the timestamp isn't unique, increment the index
191 if (curTs == prevTs)
192 {
193 index++;
194 }
195 else
196 {
197 // Otherwise, reset it
198 index = 0;
199 }
200 // Save the timestamp
201 prevTs = curTs;
202
203 entryID = std::to_string(curTs);
204 if (index > 0)
205 {
206 entryID += "_" + std::to_string(index);
207 }
208 return true;
209}
210
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500211static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700212 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700213{
Ed Tanous271584a2019-07-09 16:24:22 -0700214 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700215 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700216 if (firstEntry)
217 {
218 prevTs = 0;
219 }
220
Jason M. Bills95820182019-04-22 16:25:34 -0700221 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700222 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700223 std::tm timeStruct = {};
224 std::istringstream entryStream(logEntry);
225 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
226 {
227 curTs = std::mktime(&timeStruct);
228 }
229 // If the timestamp isn't unique, increment the index
230 if (curTs == prevTs)
231 {
232 index++;
233 }
234 else
235 {
236 // Otherwise, reset it
237 index = 0;
238 }
239 // Save the timestamp
240 prevTs = curTs;
241
242 entryID = std::to_string(curTs);
243 if (index > 0)
244 {
245 entryID += "_" + std::to_string(index);
246 }
247 return true;
248}
249
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700250inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800251 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
252 const std::string& entryID, uint64_t& timestamp,
253 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700254{
255 if (entryID.empty())
256 {
257 return false;
258 }
259 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800260 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700261
Ed Tanous81ce6092020-12-17 16:54:55 +0000262 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800263 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700264 {
265 // Timestamp has an index
266 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800267 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700268 indexStr.remove_prefix(underscorePos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500269 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
270 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700271 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700272 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800273 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700274 return false;
275 }
276 }
277 // Timestamp has no index
Patrick Williams84396af2023-05-11 11:47:45 -0500278 auto [ptr, ec] = std::from_chars(tsStr.begin(), tsStr.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700279 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700280 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800281 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700282 return false;
283 }
284 return true;
285}
286
Jason M. Bills95820182019-04-22 16:25:34 -0700287static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500288 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700289{
290 static const std::filesystem::path redfishLogDir = "/var/log";
291 static const std::string redfishLogFilename = "redfish";
292
293 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500294 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700295 std::filesystem::directory_iterator(redfishLogDir))
296 {
297 // If we find a redfish log file, save the path
298 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700299 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700300 {
301 redfishLogFiles.emplace_back(redfishLogDir / filename);
302 }
303 }
304 // As the log files rotate, they are appended with a ".#" that is higher for
305 // the older logs. Since we don't expect more than 10 log files, we
306 // can just sort the list to get them in order from newest to oldest
307 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
308
309 return !redfishLogFiles.empty();
310}
311
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600312inline log_entry::OriginatorTypes
313 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
314{
315 if (originatorType ==
316 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
317 {
318 return log_entry::OriginatorTypes::Client;
319 }
320 if (originatorType ==
321 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
322 {
323 return log_entry::OriginatorTypes::Internal;
324 }
325 if (originatorType ==
326 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
327 {
328 return log_entry::OriginatorTypes::SupportingService;
329 }
330 return log_entry::OriginatorTypes::Invalid;
331}
332
Claire Weinanaefe3782022-07-15 19:17:19 -0700333inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800334 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700335 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600336 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700337 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
338{
339 for (const auto& interfaceMap : object.second)
340 {
341 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
342 {
343 for (const auto& propertyMap : interfaceMap.second)
344 {
345 if (propertyMap.first == "Status")
346 {
347 const auto* status =
348 std::get_if<std::string>(&propertyMap.second);
349 if (status == nullptr)
350 {
351 messages::internalError(asyncResp->res);
352 break;
353 }
354 dumpStatus = *status;
355 }
356 }
357 }
358 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
359 {
360 for (const auto& propertyMap : interfaceMap.second)
361 {
362 if (propertyMap.first == "Size")
363 {
364 const auto* sizePtr =
365 std::get_if<uint64_t>(&propertyMap.second);
366 if (sizePtr == nullptr)
367 {
368 messages::internalError(asyncResp->res);
369 break;
370 }
371 size = *sizePtr;
372 break;
373 }
374 }
375 }
376 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
377 {
378 for (const auto& propertyMap : interfaceMap.second)
379 {
380 if (propertyMap.first == "Elapsed")
381 {
382 const uint64_t* usecsTimeStamp =
383 std::get_if<uint64_t>(&propertyMap.second);
384 if (usecsTimeStamp == nullptr)
385 {
386 messages::internalError(asyncResp->res);
387 break;
388 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700389 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700390 break;
391 }
392 }
393 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600394 else if (interfaceMap.first ==
395 "xyz.openbmc_project.Common.OriginatedBy")
396 {
397 for (const auto& propertyMap : interfaceMap.second)
398 {
399 if (propertyMap.first == "OriginatorId")
400 {
401 const std::string* id =
402 std::get_if<std::string>(&propertyMap.second);
403 if (id == nullptr)
404 {
405 messages::internalError(asyncResp->res);
406 break;
407 }
408 originatorId = *id;
409 }
410
411 if (propertyMap.first == "OriginatorType")
412 {
413 const std::string* type =
414 std::get_if<std::string>(&propertyMap.second);
415 if (type == nullptr)
416 {
417 messages::internalError(asyncResp->res);
418 break;
419 }
420
421 originatorType = mapDbusOriginatorTypeToRedfish(*type);
422 if (originatorType == log_entry::OriginatorTypes::Invalid)
423 {
424 messages::internalError(asyncResp->res);
425 break;
426 }
427 }
428 }
429 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700430 }
431}
432
Nan Zhou21ab4042022-06-26 23:07:40 +0000433static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800434{
435 std::string entriesPath;
436
437 if (dumpType == "BMC")
438 {
439 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
440 }
441 else if (dumpType == "FaultLog")
442 {
443 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
444 }
445 else if (dumpType == "System")
446 {
447 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
448 }
449 else
450 {
451 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
452 << dumpType;
453 }
454
455 // Returns empty string on error
456 return entriesPath;
457}
458
zhanghch058d1b46d2021-04-01 11:18:24 +0800459inline void
460 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
461 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500462{
Claire Weinanfdd26902022-03-01 14:18:25 -0800463 std::string entriesPath = getDumpEntriesPath(dumpType);
464 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500465 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500466 messages::internalError(asyncResp->res);
467 return;
468 }
469
George Liu5eb468d2023-06-20 17:03:24 +0800470 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
471 dbus::utility::getManagedObjects(
472 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800473 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800474 dumpType](const boost::system::error_code& ec,
George Liu5eb468d2023-06-20 17:03:24 +0800475 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700476 if (ec)
477 {
478 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
479 messages::internalError(asyncResp->res);
480 return;
481 }
482
Claire Weinanfdd26902022-03-01 14:18:25 -0800483 // Remove ending slash
484 std::string odataIdStr = entriesPath;
485 if (!odataIdStr.empty())
486 {
487 odataIdStr.pop_back();
488 }
489
490 asyncResp->res.jsonValue["@odata.type"] =
491 "#LogEntryCollection.LogEntryCollection";
492 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
493 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500494 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
495 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800496
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
498 entriesArray = nlohmann::json::array();
499 std::string dumpEntryPath =
500 "/xyz/openbmc_project/dump/" +
501 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
502
George Liu5eb468d2023-06-20 17:03:24 +0800503 dbus::utility::ManagedObjectType resp(objects);
Ed Tanous002d39b2022-05-31 08:59:27 -0700504 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
505 return AlphanumLess<std::string>()(l.first.filename(),
506 r.first.filename());
507 });
508
509 for (auto& object : resp)
510 {
511 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700513 continue;
514 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700515 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700516 uint64_t size = 0;
517 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600518 std::string originatorId;
519 log_entry::OriginatorTypes originatorType =
520 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700521 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700522
523 std::string entryID = object.first.filename();
524 if (entryID.empty())
525 {
526 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500527 }
528
Claire Weinanc6fecda2022-07-15 10:43:25 -0700529 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600530 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700531 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700532
533 if (dumpStatus !=
534 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
535 !dumpStatus.empty())
536 {
537 // Dump status is not Complete, no need to enumerate
538 continue;
539 }
540
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600541 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800542 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 thisEntry["Id"] = entryID;
544 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700545 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700546 thisEntry["Created"] =
547 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700548
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600549 if (!originatorId.empty())
550 {
551 thisEntry["Originator"] = originatorId;
552 thisEntry["OriginatorType"] = originatorType;
553 }
554
Ed Tanous002d39b2022-05-31 08:59:27 -0700555 if (dumpType == "BMC")
556 {
557 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500558 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
559 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800560 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700561 }
562 else if (dumpType == "System")
563 {
564 thisEntry["DiagnosticDataType"] = "OEM";
565 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500566 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
567 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800568 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700569 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500570 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700571 }
572 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
George Liu5eb468d2023-06-20 17:03:24 +0800573 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500574}
575
zhanghch058d1b46d2021-04-01 11:18:24 +0800576inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700577 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800578 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500579{
Claire Weinanfdd26902022-03-01 14:18:25 -0800580 std::string entriesPath = getDumpEntriesPath(dumpType);
581 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500582 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500583 messages::internalError(asyncResp->res);
584 return;
585 }
586
George Liu5eb468d2023-06-20 17:03:24 +0800587 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
588 dbus::utility::getManagedObjects(
589 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800590 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800591 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700592 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700593 if (ec)
594 {
595 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
596 messages::internalError(asyncResp->res);
597 return;
598 }
599
600 bool foundDumpEntry = false;
601 std::string dumpEntryPath =
602 "/xyz/openbmc_project/dump/" +
603 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
604
605 for (const auto& objectPath : resp)
606 {
607 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500608 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700609 continue;
610 }
611
612 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700613 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700614 uint64_t size = 0;
615 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600616 std::string originatorId;
617 log_entry::OriginatorTypes originatorType =
618 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700619
Claire Weinanaefe3782022-07-15 19:17:19 -0700620 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600621 timestampUs, originatorId,
622 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700623
624 if (dumpStatus !=
625 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
626 !dumpStatus.empty())
627 {
628 // Dump status is not Complete
629 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200630 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
631 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500632 return;
633 }
634
Ed Tanous002d39b2022-05-31 08:59:27 -0700635 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600636 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800637 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700638 asyncResp->res.jsonValue["Id"] = entryID;
639 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700640 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700641 asyncResp->res.jsonValue["Created"] =
642 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500643
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600644 if (!originatorId.empty())
645 {
646 asyncResp->res.jsonValue["Originator"] = originatorId;
647 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
648 }
649
Ed Tanous002d39b2022-05-31 08:59:27 -0700650 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500651 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700652 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
653 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800654 entriesPath + entryID + "/attachment";
655 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500656 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700657 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500658 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700659 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
660 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
661 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800662 entriesPath + entryID + "/attachment";
663 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500664 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700665 }
666 if (!foundDumpEntry)
667 {
Myung Baeb90d14f2023-05-31 14:40:39 -0500668 BMCWEB_LOG_WARNING << "Can't find Dump Entry " << entryID;
669 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
670 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700671 return;
672 }
George Liu5eb468d2023-06-20 17:03:24 +0800673 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500674}
675
zhanghch058d1b46d2021-04-01 11:18:24 +0800676inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800677 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500678 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500679{
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800681 [asyncResp, entryID](const boost::system::error_code& ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500682 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
683 if (ec)
684 {
George Liu3de8d8b2021-03-22 17:49:39 +0800685 if (ec.value() == EBADR)
686 {
687 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
688 return;
689 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500690 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800691 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500692 messages::internalError(asyncResp->res);
693 return;
694 }
695 };
696 crow::connections::systemBus->async_method_call(
697 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500698 "/xyz/openbmc_project/dump/" +
699 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
700 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500701 "xyz.openbmc_project.Object.Delete", "Delete");
702}
703
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600704inline DumpCreationProgress
705 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500706{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600707 if (status ==
708 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
709 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
710 {
711 return DumpCreationProgress::DUMP_CREATE_FAILED;
712 }
713 if (status ==
714 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
715 {
716 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
717 }
718 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
719}
720
721inline DumpCreationProgress
722 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
723{
724 for (const auto& [key, val] : values)
725 {
726 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700727 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600728 const std::string* value = std::get_if<std::string>(&val);
729 if (value == nullptr)
730 {
731 BMCWEB_LOG_ERROR << "Status property value is null";
732 return DumpCreationProgress::DUMP_CREATE_FAILED;
733 }
734 return mapDbusStatusToDumpProgress(*value);
735 }
736 }
737 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
738}
739
740inline std::string getDumpEntryPath(const std::string& dumpPath)
741{
742 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
743 {
744 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
745 }
746 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
747 {
748 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
749 }
750 return "";
751}
752
753inline void createDumpTaskCallback(
754 task::Payload&& payload,
755 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
756 const sdbusplus::message::object_path& createdObjPath)
757{
758 const std::string dumpPath = createdObjPath.parent_path().str;
759 const std::string dumpId = createdObjPath.filename();
760
761 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
762
763 if (dumpEntryPath.empty())
764 {
765 BMCWEB_LOG_ERROR << "Invalid dump type received";
766 messages::internalError(asyncResp->res);
767 return;
768 }
769
770 crow::connections::systemBus->async_method_call(
771 [asyncResp, payload, createdObjPath,
772 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800773 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600774 const std::string& introspectXml) {
775 if (ec)
776 {
777 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
778 << ec.message();
779 messages::internalError(asyncResp->res);
780 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700781 }
782
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600783 // Check if the created dump object has implemented Progress
784 // interface to track dump completion. If yes, fetch the "Status"
785 // property of the interface, modify the task state accordingly.
786 // Else, return task completed.
787 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700788
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600789 doc.Parse(introspectXml.data(), introspectXml.size());
790 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
791 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700792 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600793 BMCWEB_LOG_ERROR << "XML document failed to parse";
794 messages::internalError(asyncResp->res);
795 return;
796 }
797 tinyxml2::XMLElement* interfaceNode =
798 pRoot->FirstChildElement("interface");
799
800 bool isProgressIntfPresent = false;
801 while (interfaceNode != nullptr)
802 {
803 const char* thisInterfaceName = interfaceNode->Attribute("name");
804 if (thisInterfaceName != nullptr)
805 {
806 if (thisInterfaceName ==
807 std::string_view("xyz.openbmc_project.Common.Progress"))
808 {
809 interfaceNode =
810 interfaceNode->NextSiblingElement("interface");
811 continue;
812 }
813 isProgressIntfPresent = true;
814 break;
815 }
816 interfaceNode = interfaceNode->NextSiblingElement("interface");
817 }
818
819 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
820 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800821 const boost::system::error_code& err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600822 const std::shared_ptr<task::TaskData>& taskData) {
823 if (err)
824 {
825 BMCWEB_LOG_ERROR << createdObjPath.str
826 << ": Error in creating dump";
827 taskData->messages.emplace_back(messages::internalError());
828 taskData->state = "Cancelled";
829 return task::completed;
830 }
831
832 if (isProgressIntfPresent)
833 {
834 dbus::utility::DBusPropertiesMap values;
835 std::string prop;
836 msg.read(prop, values);
837
838 DumpCreationProgress dumpStatus =
839 getDumpCompletionStatus(values);
840 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
841 {
842 BMCWEB_LOG_ERROR << createdObjPath.str
843 << ": Error in creating dump";
844 taskData->state = "Cancelled";
845 return task::completed;
846 }
847
848 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
849 {
850 BMCWEB_LOG_DEBUG << createdObjPath.str
851 << ": Dump creation task is in progress";
852 return !task::completed;
853 }
854 }
855
Ed Tanous002d39b2022-05-31 08:59:27 -0700856 nlohmann::json retMessage = messages::success();
857 taskData->messages.emplace_back(retMessage);
858
Ed Tanousc51a58e2023-03-27 14:43:19 -0700859 boost::urls::url url = boost::urls::format(
860 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
861
862 std::string headerLoc = "Location: ";
863 headerLoc += url.buffer();
864
Ed Tanous002d39b2022-05-31 08:59:27 -0700865 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
866
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600867 BMCWEB_LOG_DEBUG << createdObjPath.str
868 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700869 taskData->state = "Completed";
870 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600871 },
872 "type='signal',interface='org.freedesktop.DBus.Properties',"
873 "member='PropertiesChanged',path='" +
874 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500875
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600876 // The task timer is set to max time limit within which the
877 // requested dump will be collected.
878 task->startTimer(std::chrono::minutes(6));
879 task->populateResp(asyncResp->res);
880 task->payload.emplace(payload);
881 },
882 "xyz.openbmc_project.Dump.Manager", createdObjPath,
883 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500884}
885
zhanghch058d1b46d2021-04-01 11:18:24 +0800886inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
887 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500888{
Claire Weinanfdd26902022-03-01 14:18:25 -0800889 std::string dumpPath = getDumpEntriesPath(dumpType);
890 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500891 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500892 messages::internalError(asyncResp->res);
893 return;
894 }
895
896 std::optional<std::string> diagnosticDataType;
897 std::optional<std::string> oemDiagnosticDataType;
898
Willy Tu15ed6782021-12-14 11:03:16 -0800899 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500900 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
901 "OEMDiagnosticDataType", oemDiagnosticDataType))
902 {
903 return;
904 }
905
906 if (dumpType == "System")
907 {
908 if (!oemDiagnosticDataType || !diagnosticDataType)
909 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800910 BMCWEB_LOG_ERROR
911 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500912 messages::actionParameterMissing(
913 asyncResp->res, "CollectDiagnosticData",
914 "DiagnosticDataType & OEMDiagnosticDataType");
915 return;
916 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700917 if ((*oemDiagnosticDataType != "System") ||
918 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500919 {
920 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700921 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500922 return;
923 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500924 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500925 }
926 else if (dumpType == "BMC")
927 {
928 if (!diagnosticDataType)
929 {
George Liu0fda0f12021-11-16 10:06:17 +0800930 BMCWEB_LOG_ERROR
931 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500932 messages::actionParameterMissing(
933 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
934 return;
935 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700936 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500937 {
938 BMCWEB_LOG_ERROR
939 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700940 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500941 return;
942 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500943 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
944 }
945 else
946 {
947 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
948 messages::internalError(asyncResp->res);
949 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500950 }
951
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600952 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
953 createDumpParamVec;
954
Carson Labradof574a8e2023-03-22 02:26:00 +0000955 if (req.session != nullptr)
956 {
957 createDumpParamVec.emplace_back(
958 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
959 req.session->clientIp);
960 createDumpParamVec.emplace_back(
961 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
962 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
963 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600964
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500965 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800966 [asyncResp, payload(task::Payload(req)),
967 dumpPath](const boost::system::error_code& ec,
968 const sdbusplus::message_t& msg,
969 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700970 if (ec)
971 {
972 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500973 const sd_bus_error* dbusError = msg.get_error();
974 if (dbusError == nullptr)
975 {
976 messages::internalError(asyncResp->res);
977 return;
978 }
979
980 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
981 << " and error msg: " << dbusError->message;
982 if (std::string_view(
983 "xyz.openbmc_project.Common.Error.NotAllowed") ==
984 dbusError->name)
985 {
986 messages::resourceInStandby(asyncResp->res);
987 return;
988 }
989 if (std::string_view(
990 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
991 dbusError->name)
992 {
993 messages::serviceDisabled(asyncResp->res, dumpPath);
994 return;
995 }
996 if (std::string_view(
997 "xyz.openbmc_project.Common.Error.Unavailable") ==
998 dbusError->name)
999 {
1000 messages::resourceInUse(asyncResp->res);
1001 return;
1002 }
1003 // Other Dbus errors such as:
1004 // xyz.openbmc_project.Common.Error.InvalidArgument &
1005 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1006 // the dbus call that is made here in the bmcweb
1007 // implementation and has nothing to do with the client's
1008 // input in the request. Hence, returning internal error
1009 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001010 messages::internalError(asyncResp->res);
1011 return;
1012 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001013 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
1014 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001015 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001016 "xyz.openbmc_project.Dump.Manager",
1017 "/xyz/openbmc_project/dump/" +
1018 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001019 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001020}
1021
zhanghch058d1b46d2021-04-01 11:18:24 +08001022inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1023 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001024{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001025 std::string dumpTypeLowerCopy =
1026 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001027
Claire Weinan0d946212022-07-13 19:40:19 -07001028 crow::connections::systemBus->async_method_call(
1029 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001030 if (ec)
1031 {
Claire Weinan0d946212022-07-13 19:40:19 -07001032 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -07001033 messages::internalError(asyncResp->res);
1034 return;
1035 }
Claire Weinan0d946212022-07-13 19:40:19 -07001036 },
1037 "xyz.openbmc_project.Dump.Manager",
1038 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1039 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001040}
1041
Ed Tanousb9d36b42022-02-26 21:42:46 -08001042inline static void
1043 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1044 std::string& filename, std::string& timestamp,
1045 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001046{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001047 const std::string* filenamePtr = nullptr;
1048 const std::string* timestampPtr = nullptr;
1049 const std::string* logfilePtr = nullptr;
1050
1051 const bool success = sdbusplus::unpackPropertiesNoThrow(
1052 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1053 "Filename", filenamePtr, "Log", logfilePtr);
1054
1055 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001056 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001057 return;
1058 }
1059
1060 if (filenamePtr != nullptr)
1061 {
1062 filename = *filenamePtr;
1063 }
1064
1065 if (timestampPtr != nullptr)
1066 {
1067 timestamp = *timestampPtr;
1068 }
1069
1070 if (logfilePtr != nullptr)
1071 {
1072 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001073 }
1074}
1075
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001076inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001077{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001078 /**
1079 * Functions triggers appropriate requests on DBus
1080 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001081 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001082 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001083 .methods(boost::beast::http::verb::get)(
1084 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001085 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1086 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001087 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001088 {
1089 return;
1090 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001091 if constexpr (bmcwebEnableMultiHost)
1092 {
1093 // Option currently returns no systems. TBD
1094 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1095 systemName);
1096 return;
1097 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001098 if (systemName != "system")
1099 {
1100 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1101 systemName);
1102 return;
1103 }
1104
Ed Tanous002d39b2022-05-31 08:59:27 -07001105 // Collections don't include the static data added by SubRoute
1106 // because it has a duplicate entry for members
1107 asyncResp->res.jsonValue["@odata.type"] =
1108 "#LogServiceCollection.LogServiceCollection";
1109 asyncResp->res.jsonValue["@odata.id"] =
1110 "/redfish/v1/Systems/system/LogServices";
1111 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1112 asyncResp->res.jsonValue["Description"] =
1113 "Collection of LogServices for this Computer System";
1114 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1115 logServiceArray = nlohmann::json::array();
1116 nlohmann::json::object_t eventLog;
1117 eventLog["@odata.id"] =
1118 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001119 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001120#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001121 nlohmann::json::object_t dumpLog;
1122 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001123 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001124#endif
1125
Jason M. Billsd53dd412019-02-12 17:16:22 -08001126#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001127 nlohmann::json::object_t crashdump;
1128 crashdump["@odata.id"] =
1129 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001130 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001131#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001132
1133#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001134 nlohmann::json::object_t hostlogger;
1135 hostlogger["@odata.id"] =
1136 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001137 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001138#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001139 asyncResp->res.jsonValue["Members@odata.count"] =
1140 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001141
George Liu7a1dbc42022-12-07 16:03:22 +08001142 constexpr std::array<std::string_view, 1> interfaces = {
1143 "xyz.openbmc_project.State.Boot.PostCode"};
1144 dbus::utility::getSubTreePaths(
1145 "/", 0, interfaces,
1146 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001147 const dbus::utility::MapperGetSubTreePathsResponse&
1148 subtreePath) {
1149 if (ec)
1150 {
1151 BMCWEB_LOG_ERROR << ec;
1152 return;
1153 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001154
Ed Tanous002d39b2022-05-31 08:59:27 -07001155 for (const auto& pathStr : subtreePath)
1156 {
1157 if (pathStr.find("PostCode") != std::string::npos)
1158 {
1159 nlohmann::json& logServiceArrayLocal =
1160 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001161 nlohmann::json::object_t member;
1162 member["@odata.id"] =
1163 "/redfish/v1/Systems/system/LogServices/PostCodes";
1164
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001165 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001166
Ed Tanous002d39b2022-05-31 08:59:27 -07001167 asyncResp->res.jsonValue["Members@odata.count"] =
1168 logServiceArrayLocal.size();
1169 return;
1170 }
1171 }
George Liu7a1dbc42022-12-07 16:03:22 +08001172 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001173 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001174}
1175
1176inline void requestRoutesEventLogService(App& app)
1177{
Ed Tanous22d268c2022-05-19 09:39:07 -07001178 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001179 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001180 .methods(boost::beast::http::verb::get)(
1181 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001182 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1183 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001184 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001185 {
1186 return;
1187 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001188 if (systemName != "system")
1189 {
1190 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1191 systemName);
1192 return;
1193 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001194 asyncResp->res.jsonValue["@odata.id"] =
1195 "/redfish/v1/Systems/system/LogServices/EventLog";
1196 asyncResp->res.jsonValue["@odata.type"] =
1197 "#LogService.v1_1_0.LogService";
1198 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1199 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1200 asyncResp->res.jsonValue["Id"] = "EventLog";
1201 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301202
Ed Tanous002d39b2022-05-31 08:59:27 -07001203 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001204 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301205
Ed Tanous002d39b2022-05-31 08:59:27 -07001206 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1207 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1208 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301209
Ed Tanous002d39b2022-05-31 08:59:27 -07001210 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1211 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1212 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001213
Ed Tanous002d39b2022-05-31 08:59:27 -07001214 {"target",
1215 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001216 });
1217}
1218
1219inline void requestRoutesJournalEventLogClear(App& app)
1220{
Jason M. Bills4978b632022-02-22 14:17:43 -08001221 BMCWEB_ROUTE(
1222 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001223 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001224 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001225 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001226 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001227 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1228 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001229 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001230 {
1231 return;
1232 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001233 if (systemName != "system")
1234 {
1235 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1236 systemName);
1237 return;
1238 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001239 // Clear the EventLog by deleting the log files
1240 std::vector<std::filesystem::path> redfishLogFiles;
1241 if (getRedfishLogFiles(redfishLogFiles))
1242 {
1243 for (const std::filesystem::path& file : redfishLogFiles)
1244 {
1245 std::error_code ec;
1246 std::filesystem::remove(file, ec);
1247 }
1248 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001249
Ed Tanous002d39b2022-05-31 08:59:27 -07001250 // Reload rsyslog so it knows to start new log files
1251 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001252 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001253 if (ec)
1254 {
1255 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1256 messages::internalError(asyncResp->res);
1257 return;
1258 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001259
Ed Tanous002d39b2022-05-31 08:59:27 -07001260 messages::success(asyncResp->res);
1261 },
1262 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1263 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1264 "replace");
1265 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001266}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001267
Jason M. Billsac992cd2022-06-24 13:31:46 -07001268enum class LogParseError
1269{
1270 success,
1271 parseFailed,
1272 messageIdNotInRegistry,
1273};
1274
1275static LogParseError
1276 fillEventLogEntryJson(const std::string& logEntryID,
1277 const std::string& logEntry,
1278 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001279{
Jason M. Bills95820182019-04-22 16:25:34 -07001280 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001281 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001282 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001283 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001284 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001285 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001286 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001287 std::string timestamp = logEntry.substr(0, space);
1288 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001289 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001290 if (entryStart == std::string::npos)
1291 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001292 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001293 }
1294 std::string_view entry(logEntry);
1295 entry.remove_prefix(entryStart);
1296 // Use split to separate the entry into its fields
1297 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001298 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001299 // We need at least a MessageId to be valid
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001300 auto logEntryIter = logEntryFields.begin();
1301 if (logEntryIter == logEntryFields.end())
Jason M. Billscd225da2019-05-08 15:31:57 -07001302 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001303 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001304 }
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001305 std::string& messageID = *logEntryIter;
Jason M. Bills4851d452019-03-28 11:27:48 -07001306 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001307 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001308
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001309 logEntryIter++;
Sui Chen54417b02022-03-24 14:59:52 -07001310 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001311 {
Sui Chen54417b02022-03-24 14:59:52 -07001312 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001313 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001314 }
1315
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001316 std::vector<std::string_view> messageArgs(logEntryIter,
1317 logEntryFields.end());
1318 std::string msg = redfish::registries::fillMessageArgs(messageArgs,
1319 message->message);
1320 if (msg.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001321 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001322 return LogParseError::parseFailed;
Jason M. Bills4851d452019-03-28 11:27:48 -07001323 }
1324
Jason M. Bills95820182019-04-22 16:25:34 -07001325 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1326 // format which matches the Redfish format except for the fractional seconds
1327 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001328 std::size_t dot = timestamp.find_first_of('.');
1329 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001330 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001331 {
Jason M. Bills95820182019-04-22 16:25:34 -07001332 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001333 }
1334
1335 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001336 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001337 logEntryJson["@odata.id"] = boost::urls::format(
1338 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1339 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001340 logEntryJson["Name"] = "System Event Log Entry";
1341 logEntryJson["Id"] = logEntryID;
1342 logEntryJson["Message"] = std::move(msg);
1343 logEntryJson["MessageId"] = std::move(messageID);
1344 logEntryJson["MessageArgs"] = messageArgs;
1345 logEntryJson["EntryType"] = "Event";
1346 logEntryJson["Severity"] = message->messageSeverity;
1347 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001348 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001349}
1350
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001351inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001352{
Ed Tanous22d268c2022-05-19 09:39:07 -07001353 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001354 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001355 .methods(boost::beast::http::verb::get)(
1356 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001357 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1358 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001359 query_param::QueryCapabilities capabilities = {
1360 .canDelegateTop = true,
1361 .canDelegateSkip = true,
1362 };
1363 query_param::Query delegatedQuery;
1364 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001365 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001366 {
1367 return;
1368 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001369 if constexpr (bmcwebEnableMultiHost)
1370 {
1371 // Option currently returns no systems. TBD
1372 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1373 systemName);
1374 return;
1375 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001376 if (systemName != "system")
1377 {
1378 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1379 systemName);
1380 return;
1381 }
1382
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001383 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001384 size_t skip = delegatedQuery.skip.value_or(0);
1385
Ed Tanous002d39b2022-05-31 08:59:27 -07001386 // Collections don't include the static data added by SubRoute
1387 // because it has a duplicate entry for members
1388 asyncResp->res.jsonValue["@odata.type"] =
1389 "#LogEntryCollection.LogEntryCollection";
1390 asyncResp->res.jsonValue["@odata.id"] =
1391 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1392 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1393 asyncResp->res.jsonValue["Description"] =
1394 "Collection of System Event Log Entries";
1395
1396 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1397 logEntryArray = nlohmann::json::array();
1398 // Go through the log files and create a unique ID for each
1399 // entry
1400 std::vector<std::filesystem::path> redfishLogFiles;
1401 getRedfishLogFiles(redfishLogFiles);
1402 uint64_t entryCount = 0;
1403 std::string logEntry;
1404
1405 // Oldest logs are in the last file, so start there and loop
1406 // backwards
1407 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1408 it++)
1409 {
1410 std::ifstream logStream(*it);
1411 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001412 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001413 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001414 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001415
Ed Tanous002d39b2022-05-31 08:59:27 -07001416 // Reset the unique ID on the first entry
1417 bool firstEntry = true;
1418 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001419 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001420 std::string idStr;
1421 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001422 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001423 continue;
1424 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001425 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001426
Jason M. Billsde703c52022-06-23 14:19:04 -07001427 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001428 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1429 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001430 if (status == LogParseError::messageIdNotInRegistry)
1431 {
1432 continue;
1433 }
1434 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001435 {
1436 messages::internalError(asyncResp->res);
1437 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001438 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001439
Jason M. Billsde703c52022-06-23 14:19:04 -07001440 entryCount++;
1441 // Handle paging using skip (number of entries to skip from the
1442 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001443 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001444 {
1445 continue;
1446 }
1447
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001448 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001449 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001450 }
1451 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001452 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001453 {
1454 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1455 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001456 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001457 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001458 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001459}
Chicago Duan336e96c2019-07-15 14:22:08 +08001460
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001461inline void requestRoutesJournalEventLogEntry(App& app)
1462{
1463 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001464 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001465 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001466 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001467 [&app](const crow::Request& req,
1468 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001469 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001470 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001471 {
1472 return;
1473 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001474 if constexpr (bmcwebEnableMultiHost)
1475 {
1476 // Option currently returns no systems. TBD
1477 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1478 systemName);
1479 return;
1480 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001481
1482 if (systemName != "system")
1483 {
1484 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1485 systemName);
1486 return;
1487 }
1488
Ed Tanous002d39b2022-05-31 08:59:27 -07001489 const std::string& targetID = param;
1490
1491 // Go through the log files and check the unique ID for each
1492 // entry to find the target entry
1493 std::vector<std::filesystem::path> redfishLogFiles;
1494 getRedfishLogFiles(redfishLogFiles);
1495 std::string logEntry;
1496
1497 // Oldest logs are in the last file, so start there and loop
1498 // backwards
1499 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1500 it++)
1501 {
1502 std::ifstream logStream(*it);
1503 if (!logStream.is_open())
1504 {
1505 continue;
1506 }
1507
1508 // Reset the unique ID on the first entry
1509 bool firstEntry = true;
1510 while (std::getline(logStream, logEntry))
1511 {
1512 std::string idStr;
1513 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001514 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001515 continue;
1516 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001517 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001518
1519 if (idStr == targetID)
1520 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001521 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001522 LogParseError status =
1523 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1524 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001525 {
1526 messages::internalError(asyncResp->res);
1527 return;
1528 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001529 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001530 return;
1531 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001532 }
1533 }
1534 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001535 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001536 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001537}
1538
1539inline void requestRoutesDBusEventLogEntryCollection(App& app)
1540{
Ed Tanous22d268c2022-05-19 09:39:07 -07001541 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001542 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001543 .methods(boost::beast::http::verb::get)(
1544 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001545 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1546 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001547 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001548 {
1549 return;
1550 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001551 if constexpr (bmcwebEnableMultiHost)
1552 {
1553 // Option currently returns no systems. TBD
1554 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1555 systemName);
1556 return;
1557 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001558 if (systemName != "system")
1559 {
1560 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1561 systemName);
1562 return;
1563 }
1564
Ed Tanous002d39b2022-05-31 08:59:27 -07001565 // Collections don't include the static data added by SubRoute
1566 // because it has a duplicate entry for members
1567 asyncResp->res.jsonValue["@odata.type"] =
1568 "#LogEntryCollection.LogEntryCollection";
1569 asyncResp->res.jsonValue["@odata.id"] =
1570 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1571 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1572 asyncResp->res.jsonValue["Description"] =
1573 "Collection of System Event Log Entries";
1574
1575 // DBus implementation of EventLog/Entries
1576 // Make call to Logging Service to find all log entry objects
George Liu5eb468d2023-06-20 17:03:24 +08001577 sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1578 dbus::utility::getManagedObjects(
1579 "xyz.openbmc_project.Logging", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001580 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001581 const dbus::utility::ManagedObjectType& resp) {
1582 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001583 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001584 // TODO Handle for specific error code
1585 BMCWEB_LOG_ERROR
1586 << "getLogEntriesIfaceData resp_handler got error " << ec;
1587 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001588 return;
1589 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001590 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1591 entriesArray = nlohmann::json::array();
1592 for (const auto& objectPath : resp)
1593 {
1594 const uint32_t* id = nullptr;
1595 const uint64_t* timestamp = nullptr;
1596 const uint64_t* updateTimestamp = nullptr;
1597 const std::string* severity = nullptr;
1598 const std::string* message = nullptr;
1599 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001600 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001601 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001602 const std::string* notify = nullptr;
1603
Ed Tanous002d39b2022-05-31 08:59:27 -07001604 for (const auto& interfaceMap : objectPath.second)
1605 {
1606 if (interfaceMap.first ==
1607 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001608 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001609 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001610 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001611 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001612 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001613 id = std::get_if<uint32_t>(&propertyMap.second);
1614 }
1615 else if (propertyMap.first == "Timestamp")
1616 {
1617 timestamp =
1618 std::get_if<uint64_t>(&propertyMap.second);
1619 }
1620 else if (propertyMap.first == "UpdateTimestamp")
1621 {
1622 updateTimestamp =
1623 std::get_if<uint64_t>(&propertyMap.second);
1624 }
1625 else if (propertyMap.first == "Severity")
1626 {
1627 severity = std::get_if<std::string>(
1628 &propertyMap.second);
1629 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001630 else if (propertyMap.first == "Resolution")
1631 {
1632 resolution = std::get_if<std::string>(
1633 &propertyMap.second);
1634 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001635 else if (propertyMap.first == "Message")
1636 {
1637 message = std::get_if<std::string>(
1638 &propertyMap.second);
1639 }
1640 else if (propertyMap.first == "Resolved")
1641 {
1642 const bool* resolveptr =
1643 std::get_if<bool>(&propertyMap.second);
1644 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001645 {
1646 messages::internalError(asyncResp->res);
1647 return;
1648 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001649 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001650 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001651 else if (propertyMap.first ==
1652 "ServiceProviderNotify")
1653 {
1654 notify = std::get_if<std::string>(
1655 &propertyMap.second);
1656 if (notify == nullptr)
1657 {
1658 messages::internalError(asyncResp->res);
1659 return;
1660 }
1661 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001662 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001663 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001664 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001665 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001666 messages::internalError(asyncResp->res);
1667 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001668 }
1669 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001670 else if (interfaceMap.first ==
1671 "xyz.openbmc_project.Common.FilePath")
1672 {
1673 for (const auto& propertyMap : interfaceMap.second)
1674 {
1675 if (propertyMap.first == "Path")
1676 {
1677 filePath = std::get_if<std::string>(
1678 &propertyMap.second);
1679 }
1680 }
1681 }
1682 }
1683 // Object path without the
1684 // xyz.openbmc_project.Logging.Entry interface, ignore
1685 // and continue.
1686 if (id == nullptr || message == nullptr ||
1687 severity == nullptr || timestamp == nullptr ||
1688 updateTimestamp == nullptr)
1689 {
1690 continue;
1691 }
1692 entriesArray.push_back({});
1693 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001694 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001695 thisEntry["@odata.id"] = boost::urls::format(
1696 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1697 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001698 thisEntry["Name"] = "System Event Log Entry";
1699 thisEntry["Id"] = std::to_string(*id);
1700 thisEntry["Message"] = *message;
1701 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001702 if ((resolution != nullptr) && (!(*resolution).empty()))
1703 {
1704 thisEntry["Resolution"] = *resolution;
1705 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001706 std::optional<bool> notifyAction =
1707 getProviderNotifyAction(*notify);
1708 if (notifyAction)
1709 {
1710 thisEntry["ServiceProviderNotified"] = *notifyAction;
1711 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001712 thisEntry["EntryType"] = "Event";
1713 thisEntry["Severity"] =
1714 translateSeverityDbusToRedfish(*severity);
1715 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001716 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001717 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001718 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001719 if (filePath != nullptr)
1720 {
1721 thisEntry["AdditionalDataURI"] =
1722 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1723 std::to_string(*id) + "/attachment";
1724 }
1725 }
1726 std::sort(
1727 entriesArray.begin(), entriesArray.end(),
1728 [](const nlohmann::json& left, const nlohmann::json& right) {
1729 return (left["Id"] <= right["Id"]);
1730 });
1731 asyncResp->res.jsonValue["Members@odata.count"] =
1732 entriesArray.size();
George Liu5eb468d2023-06-20 17:03:24 +08001733 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001734 });
1735}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001736
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001737inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001738{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001739 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001740 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001741 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001742 .methods(boost::beast::http::verb::get)(
1743 [&app](const crow::Request& req,
1744 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001745 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001746 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001747 {
1748 return;
1749 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001750 if constexpr (bmcwebEnableMultiHost)
1751 {
1752 // Option currently returns no systems. TBD
1753 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1754 systemName);
1755 return;
1756 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001757 if (systemName != "system")
1758 {
1759 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1760 systemName);
1761 return;
1762 }
1763
Ed Tanous002d39b2022-05-31 08:59:27 -07001764 std::string entryID = param;
1765 dbus::utility::escapePathForDbus(entryID);
1766
1767 // DBus implementation of EventLog/Entries
1768 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001769 sdbusplus::asio::getAllProperties(
1770 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1771 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001772 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001773 const dbus::utility::DBusPropertiesMap& resp) {
1774 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001775 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001776 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1777 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001778 return;
1779 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001780 if (ec)
1781 {
1782 BMCWEB_LOG_ERROR
1783 << "EventLogEntry (DBus) resp_handler got error " << ec;
1784 messages::internalError(asyncResp->res);
1785 return;
1786 }
1787 const uint32_t* id = nullptr;
1788 const uint64_t* timestamp = nullptr;
1789 const uint64_t* updateTimestamp = nullptr;
1790 const std::string* severity = nullptr;
1791 const std::string* message = nullptr;
1792 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001793 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001794 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001795 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001796
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001797 const bool success = sdbusplus::unpackPropertiesNoThrow(
1798 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1799 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001800 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001801 "Resolution", resolution, "Path", filePath,
1802 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001803
1804 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001806 messages::internalError(asyncResp->res);
1807 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001808 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001809
Ed Tanous002d39b2022-05-31 08:59:27 -07001810 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001811 timestamp == nullptr || updateTimestamp == nullptr ||
1812 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 {
1814 messages::internalError(asyncResp->res);
1815 return;
1816 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001817
Ed Tanous002d39b2022-05-31 08:59:27 -07001818 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001819 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001820 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1821 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1822 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001823 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1824 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1825 asyncResp->res.jsonValue["Message"] = *message;
1826 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001827 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1828 if (notifyAction)
1829 {
1830 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1831 *notifyAction;
1832 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001833 if ((resolution != nullptr) && (!(*resolution).empty()))
1834 {
1835 asyncResp->res.jsonValue["Resolution"] = *resolution;
1836 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001837 asyncResp->res.jsonValue["EntryType"] = "Event";
1838 asyncResp->res.jsonValue["Severity"] =
1839 translateSeverityDbusToRedfish(*severity);
1840 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001841 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001842 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001843 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001844 if (filePath != nullptr)
1845 {
1846 asyncResp->res.jsonValue["AdditionalDataURI"] =
1847 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1848 std::to_string(*id) + "/attachment";
1849 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001850 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001851 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001852
1853 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001854 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001855 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001856 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001857 [&app](const crow::Request& req,
1858 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001859 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001860 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001861 {
1862 return;
1863 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001864 if constexpr (bmcwebEnableMultiHost)
1865 {
1866 // Option currently returns no systems. TBD
1867 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1868 systemName);
1869 return;
1870 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001871 if (systemName != "system")
1872 {
1873 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1874 systemName);
1875 return;
1876 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001877 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001878
Ed Tanous002d39b2022-05-31 08:59:27 -07001879 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1880 resolved))
1881 {
1882 return;
1883 }
1884 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001885
Ed Tanous002d39b2022-05-31 08:59:27 -07001886 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001887 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001888 if (ec)
1889 {
1890 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1891 messages::internalError(asyncResp->res);
1892 return;
1893 }
1894 },
1895 "xyz.openbmc_project.Logging",
1896 "/xyz/openbmc_project/logging/entry/" + entryId,
1897 "org.freedesktop.DBus.Properties", "Set",
1898 "xyz.openbmc_project.Logging.Entry", "Resolved",
1899 dbus::utility::DbusVariantType(*resolved));
1900 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001901
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001902 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001903 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001904 .privileges(redfish::privileges::deleteLogEntry)
1905
Ed Tanous002d39b2022-05-31 08:59:27 -07001906 .methods(boost::beast::http::verb::delete_)(
1907 [&app](const crow::Request& req,
1908 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001909 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001910 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 {
1912 return;
1913 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001914 if constexpr (bmcwebEnableMultiHost)
1915 {
1916 // Option currently returns no systems. TBD
1917 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1918 systemName);
1919 return;
1920 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001921 if (systemName != "system")
1922 {
1923 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1924 systemName);
1925 return;
1926 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001927 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1928
1929 std::string entryID = param;
1930
1931 dbus::utility::escapePathForDbus(entryID);
1932
1933 // Process response from Logging service.
1934 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001935 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001936 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1937 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001938 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001939 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001940 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001941 messages::resourceNotFound(asyncResp->res, "LogEntry",
1942 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001943 return;
1944 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001945 // TODO Handle for specific error code
1946 BMCWEB_LOG_ERROR
1947 << "EventLogEntry (DBus) doDelete respHandler got error "
1948 << ec;
1949 asyncResp->res.result(
1950 boost::beast::http::status::internal_server_error);
1951 return;
1952 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001953
Ed Tanous002d39b2022-05-31 08:59:27 -07001954 asyncResp->res.result(boost::beast::http::status::ok);
1955 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001956
Ed Tanous002d39b2022-05-31 08:59:27 -07001957 // Make call to Logging service to request Delete Log
1958 crow::connections::systemBus->async_method_call(
1959 respHandler, "xyz.openbmc_project.Logging",
1960 "/xyz/openbmc_project/logging/entry/" + entryID,
1961 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001962 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001963}
1964
1965inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001966{
George Liu0fda0f12021-11-16 10:06:17 +08001967 BMCWEB_ROUTE(
1968 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001969 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001970 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001971 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001972 [&app](const crow::Request& req,
1973 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001974 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001975 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 {
1977 return;
1978 }
Matt Spinler72e21372023-04-19 12:53:33 -05001979 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07001980 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001981 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001982 {
1983 asyncResp->res.result(boost::beast::http::status::bad_request);
1984 return;
1985 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001986 if constexpr (bmcwebEnableMultiHost)
1987 {
1988 // Option currently returns no systems. TBD
1989 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1990 systemName);
1991 return;
1992 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001993 if (systemName != "system")
1994 {
1995 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1996 systemName);
1997 return;
1998 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001999
Ed Tanous002d39b2022-05-31 08:59:27 -07002000 std::string entryID = param;
2001 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002002
Ed Tanous002d39b2022-05-31 08:59:27 -07002003 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002004 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07002005 const sdbusplus::message::unix_fd& unixfd) {
2006 if (ec.value() == EBADR)
2007 {
2008 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
2009 entryID);
2010 return;
2011 }
2012 if (ec)
2013 {
2014 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
2015 messages::internalError(asyncResp->res);
2016 return;
2017 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002018
Ed Tanous002d39b2022-05-31 08:59:27 -07002019 int fd = -1;
2020 fd = dup(unixfd);
2021 if (fd == -1)
2022 {
2023 messages::internalError(asyncResp->res);
2024 return;
2025 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002026
Ed Tanous002d39b2022-05-31 08:59:27 -07002027 long long int size = lseek(fd, 0, SEEK_END);
2028 if (size == -1)
2029 {
2030 messages::internalError(asyncResp->res);
2031 return;
2032 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002033
Ed Tanous002d39b2022-05-31 08:59:27 -07002034 // Arbitrary max size of 64kb
2035 constexpr int maxFileSize = 65536;
2036 if (size > maxFileSize)
2037 {
2038 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
2039 << maxFileSize;
2040 messages::internalError(asyncResp->res);
2041 return;
2042 }
2043 std::vector<char> data(static_cast<size_t>(size));
2044 long long int rc = lseek(fd, 0, SEEK_SET);
2045 if (rc == -1)
2046 {
2047 messages::internalError(asyncResp->res);
2048 return;
2049 }
2050 rc = read(fd, data.data(), data.size());
2051 if ((rc == -1) || (rc != size))
2052 {
2053 messages::internalError(asyncResp->res);
2054 return;
2055 }
2056 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002057
Ed Tanous002d39b2022-05-31 08:59:27 -07002058 std::string_view strData(data.data(), data.size());
2059 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002060
Ed Tanousd9f6c622022-03-17 09:12:17 -07002061 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07002062 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07002063 asyncResp->res.addHeader(
2064 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07002065 asyncResp->res.body() = std::move(output);
2066 },
2067 "xyz.openbmc_project.Logging",
2068 "/xyz/openbmc_project/logging/entry/" + entryID,
2069 "xyz.openbmc_project.Logging.Entry", "GetEntry");
2070 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002071}
2072
Spencer Kub7028eb2021-10-26 15:27:35 +08002073constexpr const char* hostLoggerFolderPath = "/var/log/console";
2074
2075inline bool
2076 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2077 std::vector<std::filesystem::path>& hostLoggerFiles)
2078{
2079 std::error_code ec;
2080 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2081 if (ec)
2082 {
2083 BMCWEB_LOG_ERROR << ec.message();
2084 return false;
2085 }
2086 for (const std::filesystem::directory_entry& it : logPath)
2087 {
2088 std::string filename = it.path().filename();
2089 // Prefix of each log files is "log". Find the file and save the
2090 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002091 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002092 {
2093 hostLoggerFiles.emplace_back(it.path());
2094 }
2095 }
2096 // As the log files rotate, they are appended with a ".#" that is higher for
2097 // the older logs. Since we start from oldest logs, sort the name in
2098 // descending order.
2099 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2100 AlphanumLess<std::string>());
2101
2102 return true;
2103}
2104
Ed Tanous02cad962022-06-30 16:50:15 -07002105inline bool getHostLoggerEntries(
2106 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2107 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002108{
2109 GzFileReader logFile;
2110
2111 // Go though all log files and expose host logs.
2112 for (const std::filesystem::path& it : hostLoggerFiles)
2113 {
2114 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2115 {
2116 BMCWEB_LOG_ERROR << "fail to expose host logs";
2117 return false;
2118 }
2119 }
2120 // Get lastMessage from constructor by getter
2121 std::string lastMessage = logFile.getLastMessage();
2122 if (!lastMessage.empty())
2123 {
2124 logCount++;
2125 if (logCount > skip && logCount <= (skip + top))
2126 {
2127 logEntries.push_back(lastMessage);
2128 }
2129 }
2130 return true;
2131}
2132
2133inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2134 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002135 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002136{
2137 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002138 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002139 logEntryJson["@odata.id"] = boost::urls::format(
2140 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2141 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002142 logEntryJson["Name"] = "Host Logger Entry";
2143 logEntryJson["Id"] = logEntryID;
2144 logEntryJson["Message"] = msg;
2145 logEntryJson["EntryType"] = "Oem";
2146 logEntryJson["Severity"] = "OK";
2147 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002148}
2149
2150inline void requestRoutesSystemHostLogger(App& app)
2151{
Ed Tanous22d268c2022-05-19 09:39:07 -07002152 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002153 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002154 .methods(boost::beast::http::verb::get)(
2155 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002156 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2157 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002158 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002159 {
2160 return;
2161 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002162 if constexpr (bmcwebEnableMultiHost)
2163 {
2164 // Option currently returns no systems. TBD
2165 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2166 systemName);
2167 return;
2168 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002169 if (systemName != "system")
2170 {
2171 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2172 systemName);
2173 return;
2174 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002175 asyncResp->res.jsonValue["@odata.id"] =
2176 "/redfish/v1/Systems/system/LogServices/HostLogger";
2177 asyncResp->res.jsonValue["@odata.type"] =
2178 "#LogService.v1_1_0.LogService";
2179 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2180 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2181 asyncResp->res.jsonValue["Id"] = "HostLogger";
2182 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2183 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2184 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002185}
2186
2187inline void requestRoutesSystemHostLoggerCollection(App& app)
2188{
2189 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002190 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002191 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002192 .methods(boost::beast::http::verb::get)(
2193 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002194 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2195 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002196 query_param::QueryCapabilities capabilities = {
2197 .canDelegateTop = true,
2198 .canDelegateSkip = true,
2199 };
2200 query_param::Query delegatedQuery;
2201 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002202 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 {
2204 return;
2205 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002206 if constexpr (bmcwebEnableMultiHost)
2207 {
2208 // Option currently returns no systems. TBD
2209 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2210 systemName);
2211 return;
2212 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002213 if (systemName != "system")
2214 {
2215 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2216 systemName);
2217 return;
2218 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002219 asyncResp->res.jsonValue["@odata.id"] =
2220 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2221 asyncResp->res.jsonValue["@odata.type"] =
2222 "#LogEntryCollection.LogEntryCollection";
2223 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2224 asyncResp->res.jsonValue["Description"] =
2225 "Collection of HostLogger Entries";
2226 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2227 logEntryArray = nlohmann::json::array();
2228 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002229
Ed Tanous002d39b2022-05-31 08:59:27 -07002230 std::vector<std::filesystem::path> hostLoggerFiles;
2231 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2232 {
2233 BMCWEB_LOG_ERROR << "fail to get host log file path";
2234 return;
2235 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002236 // If we weren't provided top and skip limits, use the defaults.
2237 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002238 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 size_t logCount = 0;
2240 // This vector only store the entries we want to expose that
2241 // control by skip and top.
2242 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002243 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2244 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002245 {
2246 messages::internalError(asyncResp->res);
2247 return;
2248 }
2249 // If vector is empty, that means skip value larger than total
2250 // log count
2251 if (logEntries.empty())
2252 {
2253 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2254 return;
2255 }
2256 if (!logEntries.empty())
2257 {
2258 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002259 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002260 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002261 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2262 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002263 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002264 }
2265
Ed Tanous002d39b2022-05-31 08:59:27 -07002266 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002267 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002268 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002269 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2270 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002271 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002272 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002273 }
George Liu0fda0f12021-11-16 10:06:17 +08002274 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002275}
2276
2277inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2278{
2279 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002280 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002281 .privileges(redfish::privileges::getLogEntry)
2282 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002283 [&app](const crow::Request& req,
2284 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002285 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002286 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002287 {
2288 return;
2289 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002290 if constexpr (bmcwebEnableMultiHost)
2291 {
2292 // Option currently returns no systems. TBD
2293 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2294 systemName);
2295 return;
2296 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002297 if (systemName != "system")
2298 {
2299 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2300 systemName);
2301 return;
2302 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002303 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002304
Ed Tanous002d39b2022-05-31 08:59:27 -07002305 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002306
Patrick Williams84396af2023-05-11 11:47:45 -05002307 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2308 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002309 if (ec == std::errc::invalid_argument ||
2310 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002311 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002312 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002313 return;
2314 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002315
Ed Tanous002d39b2022-05-31 08:59:27 -07002316 std::vector<std::filesystem::path> hostLoggerFiles;
2317 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2318 {
2319 BMCWEB_LOG_ERROR << "fail to get host log file path";
2320 return;
2321 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002322
Ed Tanous002d39b2022-05-31 08:59:27 -07002323 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002324 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002325 std::vector<std::string> logEntries;
2326 // We can get specific entry by skip and top. For example, if we
2327 // want to get nth entry, we can set skip = n-1 and top = 1 to
2328 // get that entry
2329 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2330 logCount))
2331 {
2332 messages::internalError(asyncResp->res);
2333 return;
2334 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002335
Ed Tanous002d39b2022-05-31 08:59:27 -07002336 if (!logEntries.empty())
2337 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002338 nlohmann::json::object_t hostLogEntry;
2339 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2340 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002341 return;
2342 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002343
Ed Tanous002d39b2022-05-31 08:59:27 -07002344 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002345 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002346 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002347}
2348
Claire Weinandd72e872022-08-15 14:20:06 -07002349inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002350 crow::App& app, const crow::Request& req,
2351 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2352{
2353 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2354 {
2355 return;
2356 }
2357 // Collections don't include the static data added by SubRoute
2358 // because it has a duplicate entry for members
2359 asyncResp->res.jsonValue["@odata.type"] =
2360 "#LogServiceCollection.LogServiceCollection";
2361 asyncResp->res.jsonValue["@odata.id"] =
2362 "/redfish/v1/Managers/bmc/LogServices";
2363 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2364 asyncResp->res.jsonValue["Description"] =
2365 "Collection of LogServices for this Manager";
2366 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2367 logServiceArray = nlohmann::json::array();
2368
2369#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002370 nlohmann::json::object_t journal;
2371 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002372 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002373#endif
2374
2375 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2376
2377#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002378 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002379 "xyz.openbmc_project.Collection.DeleteAll"};
2380 dbus::utility::getSubTreePaths(
2381 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002382 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002383 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002384 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2385 if (ec)
2386 {
2387 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002388 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002389 << ec;
2390 // Assume that getting an error simply means there are no dump
2391 // LogServices. Return without adding any error response.
2392 return;
2393 }
2394
2395 nlohmann::json& logServiceArrayLocal =
2396 asyncResp->res.jsonValue["Members"];
2397
2398 for (const std::string& path : subTreePaths)
2399 {
2400 if (path == "/xyz/openbmc_project/dump/bmc")
2401 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002402 nlohmann::json::object_t member;
2403 member["@odata.id"] =
2404 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002405 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002406 }
2407 else if (path == "/xyz/openbmc_project/dump/faultlog")
2408 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002409 nlohmann::json::object_t member;
2410 member["@odata.id"] =
2411 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002412 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002413 }
2414 }
2415
2416 asyncResp->res.jsonValue["Members@odata.count"] =
2417 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002418 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002419#endif
2420}
2421
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002422inline void requestRoutesBMCLogServiceCollection(App& app)
2423{
2424 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002425 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002426 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002427 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002428}
Ed Tanous1da66f72018-07-27 16:13:37 -07002429
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002430inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002431{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002432 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002433 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002434 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002435 [&app](const crow::Request& req,
2436 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002437 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002438 {
2439 return;
2440 }
2441 asyncResp->res.jsonValue["@odata.type"] =
2442 "#LogService.v1_1_0.LogService";
2443 asyncResp->res.jsonValue["@odata.id"] =
2444 "/redfish/v1/Managers/bmc/LogServices/Journal";
2445 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2446 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002447 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002448 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302449
Ed Tanous002d39b2022-05-31 08:59:27 -07002450 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002451 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002452 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2453 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2454 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302455
Ed Tanous002d39b2022-05-31 08:59:27 -07002456 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2457 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2458 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002459}
Jason M. Billse1f26342018-07-18 12:12:00 -07002460
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002461static int
2462 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2463 sd_journal* journal,
2464 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002465{
2466 // Get the Log Entry contents
2467 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002468
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002469 std::string message;
2470 std::string_view syslogID;
2471 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2472 if (ret < 0)
2473 {
2474 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2475 << strerror(-ret);
2476 }
2477 if (!syslogID.empty())
2478 {
2479 message += std::string(syslogID) + ": ";
2480 }
2481
Ed Tanous39e77502019-03-04 17:35:53 -08002482 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002483 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002484 if (ret < 0)
2485 {
2486 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2487 return 1;
2488 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002489 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002490
2491 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002492 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002493 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002494 if (ret < 0)
2495 {
2496 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002497 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002498
2499 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002500 std::string entryTimeStr;
2501 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002502 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002503 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002504 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002505
2506 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002507 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002508 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2509 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002510 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002511 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2512 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2513 bmcJournalLogEntryJson["Message"] = std::move(message);
2514 bmcJournalLogEntryJson["EntryType"] = "Oem";
2515 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2516 : severity <= 4 ? "Warning"
2517 : "OK";
2518 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2519 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002520 return 0;
2521}
2522
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002523inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002524{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002525 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002526 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002527 .methods(boost::beast::http::verb::get)(
2528 [&app](const crow::Request& req,
2529 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2530 query_param::QueryCapabilities capabilities = {
2531 .canDelegateTop = true,
2532 .canDelegateSkip = true,
2533 };
2534 query_param::Query delegatedQuery;
2535 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002536 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002537 {
2538 return;
2539 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002540
2541 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002542 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002543
Ed Tanous002d39b2022-05-31 08:59:27 -07002544 // Collections don't include the static data added by SubRoute
2545 // because it has a duplicate entry for members
2546 asyncResp->res.jsonValue["@odata.type"] =
2547 "#LogEntryCollection.LogEntryCollection";
2548 asyncResp->res.jsonValue["@odata.id"] =
2549 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2550 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2551 asyncResp->res.jsonValue["Description"] =
2552 "Collection of BMC Journal Entries";
2553 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2554 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002555
Ed Tanous002d39b2022-05-31 08:59:27 -07002556 // Go through the journal and use the timestamp to create a
2557 // unique ID for each entry
2558 sd_journal* journalTmp = nullptr;
2559 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2560 if (ret < 0)
2561 {
2562 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2563 messages::internalError(asyncResp->res);
2564 return;
2565 }
2566 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2567 journalTmp, sd_journal_close);
2568 journalTmp = nullptr;
2569 uint64_t entryCount = 0;
2570 // Reset the unique ID on the first entry
2571 bool firstEntry = true;
2572 SD_JOURNAL_FOREACH(journal.get())
2573 {
2574 entryCount++;
2575 // Handle paging using skip (number of entries to skip from
2576 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002577 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002578 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002579 continue;
2580 }
2581
2582 std::string idStr;
2583 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2584 {
2585 continue;
2586 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002587 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002588
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002589 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002590 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2591 bmcJournalLogEntry) != 0)
2592 {
George Liu0fda0f12021-11-16 10:06:17 +08002593 messages::internalError(asyncResp->res);
2594 return;
2595 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002596 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002597 }
2598 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002599 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002600 {
2601 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2602 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002603 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002604 }
George Liu0fda0f12021-11-16 10:06:17 +08002605 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002606}
Jason M. Billse1f26342018-07-18 12:12:00 -07002607
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002608inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002609{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002610 BMCWEB_ROUTE(app,
2611 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002612 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002613 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002614 [&app](const crow::Request& req,
2615 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2616 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002617 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002618 {
2619 return;
2620 }
2621 // Convert the unique ID back to a timestamp to find the entry
2622 uint64_t ts = 0;
2623 uint64_t index = 0;
2624 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2625 {
2626 return;
2627 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002628
Ed Tanous002d39b2022-05-31 08:59:27 -07002629 sd_journal* journalTmp = nullptr;
2630 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2631 if (ret < 0)
2632 {
2633 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2634 messages::internalError(asyncResp->res);
2635 return;
2636 }
2637 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2638 journalTmp, sd_journal_close);
2639 journalTmp = nullptr;
2640 // Go to the timestamp in the log and move to the entry at the
2641 // index tracking the unique ID
2642 std::string idStr;
2643 bool firstEntry = true;
2644 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2645 if (ret < 0)
2646 {
2647 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2648 << strerror(-ret);
2649 messages::internalError(asyncResp->res);
2650 return;
2651 }
2652 for (uint64_t i = 0; i <= index; i++)
2653 {
2654 sd_journal_next(journal.get());
2655 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2656 {
2657 messages::internalError(asyncResp->res);
2658 return;
2659 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002660 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002661 }
2662 // Confirm that the entry ID matches what was requested
2663 if (idStr != entryID)
2664 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002665 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002666 return;
2667 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002668
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002669 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002670 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002671 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002672 {
2673 messages::internalError(asyncResp->res);
2674 return;
2675 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002676 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002677 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002678}
2679
Claire Weinanfdd26902022-03-01 14:18:25 -08002680inline void
2681 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2682 const std::string& dumpType)
2683{
2684 std::string dumpPath;
2685 std::string overWritePolicy;
2686 bool collectDiagnosticDataSupported = false;
2687
2688 if (dumpType == "BMC")
2689 {
2690 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2691 overWritePolicy = "WrapsWhenFull";
2692 collectDiagnosticDataSupported = true;
2693 }
2694 else if (dumpType == "FaultLog")
2695 {
2696 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2697 overWritePolicy = "Unknown";
2698 collectDiagnosticDataSupported = false;
2699 }
2700 else if (dumpType == "System")
2701 {
2702 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2703 overWritePolicy = "WrapsWhenFull";
2704 collectDiagnosticDataSupported = true;
2705 }
2706 else
2707 {
2708 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2709 << dumpType;
2710 messages::internalError(asyncResp->res);
2711 return;
2712 }
2713
2714 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2715 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2716 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2717 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2718 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2719 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2720
2721 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002722 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002723 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2724 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2725 redfishDateTimeOffset.second;
2726
2727 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002728
2729 if (collectDiagnosticDataSupported)
2730 {
2731 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2732 ["target"] =
2733 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2734 }
Claire Weinan0d946212022-07-13 19:40:19 -07002735
2736 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2737 dbus::utility::getSubTreePaths(
2738 "/xyz/openbmc_project/dump", 0, interfaces,
2739 [asyncResp, dumpType, dumpPath](
2740 const boost::system::error_code& ec,
2741 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2742 if (ec)
2743 {
2744 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2745 << ec;
2746 // Assume that getting an error simply means there are no dump
2747 // LogServices. Return without adding any error response.
2748 return;
2749 }
2750
2751 const std::string dbusDumpPath =
2752 "/xyz/openbmc_project/dump/" +
2753 boost::algorithm::to_lower_copy(dumpType);
2754
2755 for (const std::string& path : subTreePaths)
2756 {
2757 if (path == dbusDumpPath)
2758 {
2759 asyncResp->res
2760 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2761 dumpPath + "/Actions/LogService.ClearLog";
2762 break;
2763 }
2764 }
2765 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002766}
2767
2768inline void handleLogServicesDumpServiceGet(
2769 crow::App& app, const std::string& dumpType, const crow::Request& req,
2770 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2771{
2772 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2773 {
2774 return;
2775 }
2776 getDumpServiceInfo(asyncResp, dumpType);
2777}
2778
Ed Tanous22d268c2022-05-19 09:39:07 -07002779inline void handleLogServicesDumpServiceComputerSystemGet(
2780 crow::App& app, const crow::Request& req,
2781 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2782 const std::string& chassisId)
2783{
2784 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2785 {
2786 return;
2787 }
2788 if (chassisId != "system")
2789 {
2790 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2791 return;
2792 }
2793 getDumpServiceInfo(asyncResp, "System");
2794}
2795
Claire Weinanfdd26902022-03-01 14:18:25 -08002796inline void handleLogServicesDumpEntriesCollectionGet(
2797 crow::App& app, const std::string& dumpType, const crow::Request& req,
2798 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2799{
2800 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2801 {
2802 return;
2803 }
2804 getDumpEntryCollection(asyncResp, dumpType);
2805}
2806
Ed Tanous22d268c2022-05-19 09:39:07 -07002807inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2808 crow::App& app, const crow::Request& req,
2809 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2810 const std::string& chassisId)
2811{
2812 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2813 {
2814 return;
2815 }
2816 if (chassisId != "system")
2817 {
2818 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2819 return;
2820 }
2821 getDumpEntryCollection(asyncResp, "System");
2822}
2823
Claire Weinanfdd26902022-03-01 14:18:25 -08002824inline void handleLogServicesDumpEntryGet(
2825 crow::App& app, const std::string& dumpType, const crow::Request& req,
2826 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2827 const std::string& dumpId)
2828{
2829 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2830 {
2831 return;
2832 }
2833 getDumpEntryById(asyncResp, dumpId, dumpType);
2834}
Ed Tanous22d268c2022-05-19 09:39:07 -07002835inline void handleLogServicesDumpEntryComputerSystemGet(
2836 crow::App& app, const crow::Request& req,
2837 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2838 const std::string& chassisId, const std::string& dumpId)
2839{
2840 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2841 {
2842 return;
2843 }
2844 if (chassisId != "system")
2845 {
2846 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2847 return;
2848 }
2849 getDumpEntryById(asyncResp, dumpId, "System");
2850}
Claire Weinanfdd26902022-03-01 14:18:25 -08002851
2852inline void handleLogServicesDumpEntryDelete(
2853 crow::App& app, const std::string& dumpType, const crow::Request& req,
2854 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2855 const std::string& dumpId)
2856{
2857 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2858 {
2859 return;
2860 }
2861 deleteDumpEntry(asyncResp, dumpId, dumpType);
2862}
2863
Ed Tanous22d268c2022-05-19 09:39:07 -07002864inline void handleLogServicesDumpEntryComputerSystemDelete(
2865 crow::App& app, const crow::Request& req,
2866 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2867 const std::string& chassisId, const std::string& dumpId)
2868{
2869 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2870 {
2871 return;
2872 }
2873 if (chassisId != "system")
2874 {
2875 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2876 return;
2877 }
2878 deleteDumpEntry(asyncResp, dumpId, "System");
2879}
2880
Claire Weinanfdd26902022-03-01 14:18:25 -08002881inline void handleLogServicesDumpCollectDiagnosticDataPost(
2882 crow::App& app, const std::string& dumpType, const crow::Request& req,
2883 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2884{
2885 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2886 {
2887 return;
2888 }
2889 createDump(asyncResp, req, dumpType);
2890}
2891
Ed Tanous22d268c2022-05-19 09:39:07 -07002892inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2893 crow::App& app, const crow::Request& req,
2894 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002895 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07002896{
2897 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2898 {
2899 return;
2900 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002901
2902 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07002903 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002904 // Option currently returns no systems. TBD
2905 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2906 systemName);
2907 return;
2908 }
2909 if (systemName != "system")
2910 {
2911 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2912 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07002913 return;
2914 }
2915 createDump(asyncResp, req, "System");
2916}
2917
Claire Weinanfdd26902022-03-01 14:18:25 -08002918inline void handleLogServicesDumpClearLogPost(
2919 crow::App& app, const std::string& dumpType, const crow::Request& req,
2920 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2921{
2922 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2923 {
2924 return;
2925 }
2926 clearDump(asyncResp, dumpType);
2927}
2928
Ed Tanous22d268c2022-05-19 09:39:07 -07002929inline void handleLogServicesDumpClearLogComputerSystemPost(
2930 crow::App& app, const crow::Request& req,
2931 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002932 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07002933{
2934 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2935 {
2936 return;
2937 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002938 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07002939 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002940 // Option currently returns no systems. TBD
2941 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2942 systemName);
2943 return;
2944 }
2945 if (systemName != "system")
2946 {
2947 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2948 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07002949 return;
2950 }
2951 clearDump(asyncResp, "System");
2952}
2953
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002954inline void requestRoutesBMCDumpService(App& app)
2955{
2956 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002957 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002958 .methods(boost::beast::http::verb::get)(std::bind_front(
2959 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002960}
2961
2962inline void requestRoutesBMCDumpEntryCollection(App& app)
2963{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002964 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002965 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002966 .methods(boost::beast::http::verb::get)(std::bind_front(
2967 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002968}
2969
2970inline void requestRoutesBMCDumpEntry(App& app)
2971{
2972 BMCWEB_ROUTE(app,
2973 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002974 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002975 .methods(boost::beast::http::verb::get)(std::bind_front(
2976 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2977
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002978 BMCWEB_ROUTE(app,
2979 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002980 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002981 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2982 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002983}
2984
2985inline void requestRoutesBMCDumpCreate(App& app)
2986{
George Liu0fda0f12021-11-16 10:06:17 +08002987 BMCWEB_ROUTE(
2988 app,
2989 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002990 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002991 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002992 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2993 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002994}
2995
2996inline void requestRoutesBMCDumpClear(App& app)
2997{
George Liu0fda0f12021-11-16 10:06:17 +08002998 BMCWEB_ROUTE(
2999 app,
3000 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003001 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003002 .methods(boost::beast::http::verb::post)(std::bind_front(
3003 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
3004}
3005
3006inline void requestRoutesFaultLogDumpService(App& app)
3007{
3008 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3009 .privileges(redfish::privileges::getLogService)
3010 .methods(boost::beast::http::verb::get)(std::bind_front(
3011 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3012}
3013
3014inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3015{
3016 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3017 .privileges(redfish::privileges::getLogEntryCollection)
3018 .methods(boost::beast::http::verb::get)(
3019 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3020 std::ref(app), "FaultLog"));
3021}
3022
3023inline void requestRoutesFaultLogDumpEntry(App& app)
3024{
3025 BMCWEB_ROUTE(app,
3026 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3027 .privileges(redfish::privileges::getLogEntry)
3028 .methods(boost::beast::http::verb::get)(std::bind_front(
3029 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3030
3031 BMCWEB_ROUTE(app,
3032 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3033 .privileges(redfish::privileges::deleteLogEntry)
3034 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3035 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3036}
3037
3038inline void requestRoutesFaultLogDumpClear(App& app)
3039{
3040 BMCWEB_ROUTE(
3041 app,
3042 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3043 .privileges(redfish::privileges::postLogService)
3044 .methods(boost::beast::http::verb::post)(std::bind_front(
3045 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003046}
3047
3048inline void requestRoutesSystemDumpService(App& app)
3049{
Ed Tanous22d268c2022-05-19 09:39:07 -07003050 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003051 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003052 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003053 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003054}
3055
3056inline void requestRoutesSystemDumpEntryCollection(App& app)
3057{
Ed Tanous22d268c2022-05-19 09:39:07 -07003058 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003059 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07003060 .methods(boost::beast::http::verb::get)(std::bind_front(
3061 handleLogServicesDumpEntriesCollectionComputerSystemGet,
3062 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003063}
3064
3065inline void requestRoutesSystemDumpEntry(App& app)
3066{
3067 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003068 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003069 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003070 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003071 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003072
3073 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003074 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003075 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003076 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003077 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003078}
3079
3080inline void requestRoutesSystemDumpCreate(App& app)
3081{
George Liu0fda0f12021-11-16 10:06:17 +08003082 BMCWEB_ROUTE(
3083 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003084 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003085 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003086 .methods(boost::beast::http::verb::post)(std::bind_front(
3087 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3088 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003089}
3090
3091inline void requestRoutesSystemDumpClear(App& app)
3092{
George Liu0fda0f12021-11-16 10:06:17 +08003093 BMCWEB_ROUTE(
3094 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003095 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003096 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003097 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003098 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003099}
3100
3101inline void requestRoutesCrashdumpService(App& app)
3102{
3103 // Note: Deviated from redfish privilege registry for GET & HEAD
3104 // method for security reasons.
3105 /**
3106 * Functions triggers appropriate requests on DBus
3107 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003108 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003109 // This is incorrect, should be:
3110 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003111 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003112 .methods(boost::beast::http::verb::get)(
3113 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003114 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3115 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003116 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003117 {
3118 return;
3119 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003120 if constexpr (bmcwebEnableMultiHost)
3121 {
3122 // Option currently returns no systems. TBD
3123 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3124 systemName);
3125 return;
3126 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003127 if (systemName != "system")
3128 {
3129 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3130 systemName);
3131 return;
3132 }
3133
Ed Tanous002d39b2022-05-31 08:59:27 -07003134 // Copy over the static data to include the entries added by
3135 // SubRoute
3136 asyncResp->res.jsonValue["@odata.id"] =
3137 "/redfish/v1/Systems/system/LogServices/Crashdump";
3138 asyncResp->res.jsonValue["@odata.type"] =
3139 "#LogService.v1_2_0.LogService";
3140 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3141 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303142 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003143 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3144 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303145
Ed Tanous002d39b2022-05-31 08:59:27 -07003146 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003147 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003148 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3149 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3150 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303151
Ed Tanous002d39b2022-05-31 08:59:27 -07003152 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003153 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003154 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3155 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3156 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3157 ["target"] =
3158 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003159 });
3160}
3161
3162void inline requestRoutesCrashdumpClear(App& app)
3163{
George Liu0fda0f12021-11-16 10:06:17 +08003164 BMCWEB_ROUTE(
3165 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003166 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003167 // This is incorrect, should be:
3168 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003169 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003170 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003171 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003172 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3173 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003174 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003175 {
3176 return;
3177 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003178 if constexpr (bmcwebEnableMultiHost)
3179 {
3180 // Option currently returns no systems. TBD
3181 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3182 systemName);
3183 return;
3184 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003185 if (systemName != "system")
3186 {
3187 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3188 systemName);
3189 return;
3190 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003191 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003192 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003193 const std::string&) {
3194 if (ec)
3195 {
3196 messages::internalError(asyncResp->res);
3197 return;
3198 }
3199 messages::success(asyncResp->res);
3200 },
3201 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3202 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003203}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003204
zhanghch058d1b46d2021-04-01 11:18:24 +08003205static void
3206 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3207 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003208{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003209 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003210 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003211 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003212 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003213 if (ec)
3214 {
3215 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3216 if (ec.value() ==
3217 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003218 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003219 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003220 }
3221 else
3222 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003223 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003224 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003225 return;
3226 }
3227
3228 std::string timestamp{};
3229 std::string filename{};
3230 std::string logfile{};
3231 parseCrashdumpParameters(params, filename, timestamp, logfile);
3232
3233 if (filename.empty() || timestamp.empty())
3234 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003235 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003236 return;
3237 }
3238
3239 std::string crashdumpURI =
3240 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3241 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003242 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003243 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003244 logEntry["@odata.id"] = boost::urls::format(
3245 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3246 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003247 logEntry["Name"] = "CPU Crashdump";
3248 logEntry["Id"] = logID;
3249 logEntry["EntryType"] = "Oem";
3250 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3251 logEntry["DiagnosticDataType"] = "OEM";
3252 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3253 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003254
3255 // If logEntryJson references an array of LogEntry resources
3256 // ('Members' list), then push this as a new entry, otherwise set it
3257 // directly
3258 if (logEntryJson.is_array())
3259 {
3260 logEntryJson.push_back(logEntry);
3261 asyncResp->res.jsonValue["Members@odata.count"] =
3262 logEntryJson.size();
3263 }
3264 else
3265 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003266 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003267 }
3268 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003269 sdbusplus::asio::getAllProperties(
3270 *crow::connections::systemBus, crashdumpObject,
3271 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3272 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003273}
3274
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003275inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003276{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003277 // Note: Deviated from redfish privilege registry for GET & HEAD
3278 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003279 /**
3280 * Functions triggers appropriate requests on DBus
3281 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003282 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003283 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003284 // This is incorrect, should be.
3285 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003286 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003287 .methods(boost::beast::http::verb::get)(
3288 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003289 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3290 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003291 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003292 {
3293 return;
3294 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003295 if constexpr (bmcwebEnableMultiHost)
3296 {
3297 // Option currently returns no systems. TBD
3298 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3299 systemName);
3300 return;
3301 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003302 if (systemName != "system")
3303 {
3304 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3305 systemName);
3306 return;
3307 }
3308
George Liu7a1dbc42022-12-07 16:03:22 +08003309 constexpr std::array<std::string_view, 1> interfaces = {
3310 crashdumpInterface};
3311 dbus::utility::getSubTreePaths(
3312 "/", 0, interfaces,
3313 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003314 const std::vector<std::string>& resp) {
3315 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003316 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003317 if (ec.value() !=
3318 boost::system::errc::no_such_file_or_directory)
3319 {
3320 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3321 << ec.message();
3322 messages::internalError(asyncResp->res);
3323 return;
3324 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003325 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003326 asyncResp->res.jsonValue["@odata.type"] =
3327 "#LogEntryCollection.LogEntryCollection";
3328 asyncResp->res.jsonValue["@odata.id"] =
3329 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3330 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3331 asyncResp->res.jsonValue["Description"] =
3332 "Collection of Crashdump Entries";
3333 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3334 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003335
Ed Tanous002d39b2022-05-31 08:59:27 -07003336 for (const std::string& path : resp)
3337 {
3338 const sdbusplus::message::object_path objPath(path);
3339 // Get the log ID
3340 std::string logID = objPath.filename();
3341 if (logID.empty())
3342 {
3343 continue;
3344 }
3345 // Add the log entry to the array
3346 logCrashdumpEntry(asyncResp, logID,
3347 asyncResp->res.jsonValue["Members"]);
3348 }
George Liu7a1dbc42022-12-07 16:03:22 +08003349 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003350 });
3351}
Ed Tanous1da66f72018-07-27 16:13:37 -07003352
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003353inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003354{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003355 // Note: Deviated from redfish privilege registry for GET & HEAD
3356 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003357
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003358 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003359 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003360 // this is incorrect, should be
3361 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003362 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003363 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003364 [&app](const crow::Request& req,
3365 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003366 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003367 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003368 {
3369 return;
3370 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003371 if constexpr (bmcwebEnableMultiHost)
3372 {
3373 // Option currently returns no systems. TBD
3374 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3375 systemName);
3376 return;
3377 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003378 if (systemName != "system")
3379 {
3380 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3381 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003382 return;
3383 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003384 const std::string& logID = param;
3385 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3386 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003387}
Ed Tanous1da66f72018-07-27 16:13:37 -07003388
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003389inline void requestRoutesCrashdumpFile(App& app)
3390{
3391 // Note: Deviated from redfish privilege registry for GET & HEAD
3392 // method for security reasons.
3393 BMCWEB_ROUTE(
3394 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003395 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003396 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003397 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003398 [](const crow::Request& req,
3399 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003400 const std::string& systemName, const std::string& logID,
3401 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003402 // Do not call getRedfishRoute here since the crashdump file is not a
3403 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003404
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003405 if constexpr (bmcwebEnableMultiHost)
3406 {
3407 // Option currently returns no systems. TBD
3408 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3409 systemName);
3410 return;
3411 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003412 if (systemName != "system")
3413 {
3414 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3415 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003416 return;
3417 }
3418
Ed Tanous002d39b2022-05-31 08:59:27 -07003419 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003420 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003421 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003422 const std::vector<
3423 std::pair<std::string, dbus::utility::DbusVariantType>>&
3424 resp) {
3425 if (ec)
3426 {
3427 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3428 messages::internalError(asyncResp->res);
3429 return;
3430 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003431
Ed Tanous002d39b2022-05-31 08:59:27 -07003432 std::string dbusFilename{};
3433 std::string dbusTimestamp{};
3434 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003435
Ed Tanous002d39b2022-05-31 08:59:27 -07003436 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3437 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003438
Ed Tanous002d39b2022-05-31 08:59:27 -07003439 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3440 dbusFilepath.empty())
3441 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003442 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003443 return;
3444 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003445
Ed Tanous002d39b2022-05-31 08:59:27 -07003446 // Verify the file name parameter is correct
3447 if (fileName != dbusFilename)
3448 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003449 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003450 return;
3451 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003452
Ed Tanous002d39b2022-05-31 08:59:27 -07003453 if (!std::filesystem::exists(dbusFilepath))
3454 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003455 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003456 return;
3457 }
3458 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3459 asyncResp->res.body() =
3460 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003461
Ed Tanous002d39b2022-05-31 08:59:27 -07003462 // Configure this to be a file download when accessed
3463 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003464 asyncResp->res.addHeader(
3465 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003466 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003467 sdbusplus::asio::getAllProperties(
3468 *crow::connections::systemBus, crashdumpObject,
3469 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3470 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003471 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003472}
3473
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003474enum class OEMDiagnosticType
3475{
3476 onDemand,
3477 telemetry,
3478 invalid,
3479};
3480
Ed Tanous26ccae32023-02-16 10:28:44 -08003481inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003482{
3483 if (oemDiagStr == "OnDemand")
3484 {
3485 return OEMDiagnosticType::onDemand;
3486 }
3487 if (oemDiagStr == "Telemetry")
3488 {
3489 return OEMDiagnosticType::telemetry;
3490 }
3491
3492 return OEMDiagnosticType::invalid;
3493}
3494
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003495inline void requestRoutesCrashdumpCollect(App& app)
3496{
3497 // Note: Deviated from redfish privilege registry for GET & HEAD
3498 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003499 BMCWEB_ROUTE(
3500 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003501 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003502 // The below is incorrect; Should be ConfigureManager
3503 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003504 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003505 .methods(boost::beast::http::verb::post)(
3506 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003507 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3508 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003509 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003510 {
3511 return;
3512 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003513
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003514 if constexpr (bmcwebEnableMultiHost)
3515 {
3516 // Option currently returns no systems. TBD
3517 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3518 systemName);
3519 return;
3520 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003521 if (systemName != "system")
3522 {
3523 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3524 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003525 return;
3526 }
3527
Ed Tanous002d39b2022-05-31 08:59:27 -07003528 std::string diagnosticDataType;
3529 std::string oemDiagnosticDataType;
3530 if (!redfish::json_util::readJsonAction(
3531 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3532 "OEMDiagnosticDataType", oemDiagnosticDataType))
3533 {
3534 return;
3535 }
3536
3537 if (diagnosticDataType != "OEM")
3538 {
3539 BMCWEB_LOG_ERROR
3540 << "Only OEM DiagnosticDataType supported for Crashdump";
3541 messages::actionParameterValueFormatError(
3542 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3543 "CollectDiagnosticData");
3544 return;
3545 }
3546
3547 OEMDiagnosticType oemDiagType =
3548 getOEMDiagnosticType(oemDiagnosticDataType);
3549
3550 std::string iface;
3551 std::string method;
3552 std::string taskMatchStr;
3553 if (oemDiagType == OEMDiagnosticType::onDemand)
3554 {
3555 iface = crashdumpOnDemandInterface;
3556 method = "GenerateOnDemandLog";
3557 taskMatchStr = "type='signal',"
3558 "interface='org.freedesktop.DBus.Properties',"
3559 "member='PropertiesChanged',"
3560 "arg0namespace='com.intel.crashdump'";
3561 }
3562 else if (oemDiagType == OEMDiagnosticType::telemetry)
3563 {
3564 iface = crashdumpTelemetryInterface;
3565 method = "GenerateTelemetryLog";
3566 taskMatchStr = "type='signal',"
3567 "interface='org.freedesktop.DBus.Properties',"
3568 "member='PropertiesChanged',"
3569 "arg0namespace='com.intel.crashdump'";
3570 }
3571 else
3572 {
3573 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3574 << oemDiagnosticDataType;
3575 messages::actionParameterValueFormatError(
3576 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3577 "CollectDiagnosticData");
3578 return;
3579 }
3580
3581 auto collectCrashdumpCallback =
3582 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003583 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003584 const std::string&) mutable {
3585 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003586 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003587 if (ec.value() == boost::system::errc::operation_not_supported)
3588 {
3589 messages::resourceInStandby(asyncResp->res);
3590 }
3591 else if (ec.value() ==
3592 boost::system::errc::device_or_resource_busy)
3593 {
3594 messages::serviceTemporarilyUnavailable(asyncResp->res,
3595 "60");
3596 }
3597 else
3598 {
3599 messages::internalError(asyncResp->res);
3600 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003601 return;
3602 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003603 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003604 [](const boost::system::error_code& err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003605 const std::shared_ptr<task::TaskData>& taskData) {
3606 if (!err)
3607 {
3608 taskData->messages.emplace_back(messages::taskCompletedOK(
3609 std::to_string(taskData->index)));
3610 taskData->state = "Completed";
3611 }
3612 return task::completed;
3613 },
3614 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003615
Ed Tanous002d39b2022-05-31 08:59:27 -07003616 task->startTimer(std::chrono::minutes(5));
3617 task->populateResp(asyncResp->res);
3618 task->payload.emplace(std::move(payload));
3619 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003620
Ed Tanous002d39b2022-05-31 08:59:27 -07003621 crow::connections::systemBus->async_method_call(
3622 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3623 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003624 });
3625}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003626
Andrew Geisslercb92c032018-08-17 07:56:14 -07003627/**
3628 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3629 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003630inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003631{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003632 /**
3633 * Function handles POST method request.
3634 * The Clear Log actions does not require any parameter.The action deletes
3635 * all entries found in the Entries collection for this Log Service.
3636 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003637
George Liu0fda0f12021-11-16 10:06:17 +08003638 BMCWEB_ROUTE(
3639 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003640 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003641 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003642 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003643 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003644 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3645 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003646 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003647 {
3648 return;
3649 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003650 if constexpr (bmcwebEnableMultiHost)
3651 {
3652 // Option currently returns no systems. TBD
3653 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3654 systemName);
3655 return;
3656 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003657 if (systemName != "system")
3658 {
3659 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3660 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003661 return;
3662 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003663 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003664
Ed Tanous002d39b2022-05-31 08:59:27 -07003665 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003666 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003667 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3668 if (ec)
3669 {
3670 // TODO Handle for specific error code
3671 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3672 asyncResp->res.result(
3673 boost::beast::http::status::internal_server_error);
3674 return;
3675 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003676
Ed Tanous002d39b2022-05-31 08:59:27 -07003677 asyncResp->res.result(boost::beast::http::status::no_content);
3678 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003679
Ed Tanous002d39b2022-05-31 08:59:27 -07003680 // Make call to Logging service to request Clear Log
3681 crow::connections::systemBus->async_method_call(
3682 respHandler, "xyz.openbmc_project.Logging",
3683 "/xyz/openbmc_project/logging",
3684 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3685 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003686}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003687
3688/****************************************************
3689 * Redfish PostCode interfaces
3690 * using DBUS interface: getPostCodesTS
3691 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003692inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003693{
Ed Tanous22d268c2022-05-19 09:39:07 -07003694 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003695 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003696 .methods(boost::beast::http::verb::get)(
3697 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003698 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3699 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003700 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003701 {
3702 return;
3703 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003704 if constexpr (bmcwebEnableMultiHost)
3705 {
3706 // Option currently returns no systems. TBD
3707 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3708 systemName);
3709 return;
3710 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003711 if (systemName != "system")
3712 {
3713 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3714 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003715 return;
3716 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003717 asyncResp->res.jsonValue["@odata.id"] =
3718 "/redfish/v1/Systems/system/LogServices/PostCodes";
3719 asyncResp->res.jsonValue["@odata.type"] =
3720 "#LogService.v1_1_0.LogService";
3721 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3722 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003723 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003724 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3725 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3726 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303727
Ed Tanous002d39b2022-05-31 08:59:27 -07003728 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003729 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003730 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3731 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3732 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303733
Ed Tanous002d39b2022-05-31 08:59:27 -07003734 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3735 {"target",
3736 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003737 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003738}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003739
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003740inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003741{
George Liu0fda0f12021-11-16 10:06:17 +08003742 BMCWEB_ROUTE(
3743 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003744 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003745 // The following privilege is incorrect; It should be ConfigureManager
3746 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003747 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003748 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003749 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003750 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3751 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003752 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003753 {
3754 return;
3755 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003756 if constexpr (bmcwebEnableMultiHost)
3757 {
3758 // Option currently returns no systems. TBD
3759 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3760 systemName);
3761 return;
3762 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003763 if (systemName != "system")
3764 {
3765 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3766 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003767 return;
3768 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003769 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003770
Ed Tanous002d39b2022-05-31 08:59:27 -07003771 // Make call to post-code service to request clear all
3772 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003773 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003774 if (ec)
3775 {
3776 // TODO Handle for specific error code
3777 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3778 << ec;
3779 asyncResp->res.result(
3780 boost::beast::http::status::internal_server_error);
3781 messages::internalError(asyncResp->res);
3782 return;
3783 }
3784 },
3785 "xyz.openbmc_project.State.Boot.PostCode0",
3786 "/xyz/openbmc_project/State/Boot/PostCode0",
3787 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3788 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003789}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003790
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003791/**
3792 * @brief Parse post code ID and get the current value and index value
3793 * eg: postCodeID=B1-2, currentValue=1, index=2
3794 *
3795 * @param[in] postCodeID Post Code ID
3796 * @param[out] currentValue Current value
3797 * @param[out] index Index value
3798 *
3799 * @return bool true if the parsing is successful, false the parsing fails
3800 */
3801inline static bool parsePostCode(const std::string& postCodeID,
3802 uint64_t& currentValue, uint16_t& index)
3803{
3804 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003805 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003806 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3807 {
3808 return false;
3809 }
3810
Patrick Williams84396af2023-05-11 11:47:45 -05003811 auto start = std::next(split[0].begin());
3812 auto end = split[0].end();
3813 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003814
Patrick Williams84396af2023-05-11 11:47:45 -05003815 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003816 {
3817 return false;
3818 }
3819
Patrick Williams84396af2023-05-11 11:47:45 -05003820 start = split[1].begin();
3821 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003822
Patrick Williams84396af2023-05-11 11:47:45 -05003823 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003824
Patrick Williams84396af2023-05-11 11:47:45 -05003825 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003826}
3827
3828static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003829 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303830 const boost::container::flat_map<
3831 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003832 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3833 const uint64_t skip = 0, const uint64_t top = 0)
3834{
3835 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003836 const registries::Message* message =
3837 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003838
3839 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003840 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303841 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3842 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003843 {
3844 currentCodeIndex++;
3845 std::string postcodeEntryID =
3846 "B" + std::to_string(bootIndex) + "-" +
3847 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3848
3849 uint64_t usecSinceEpoch = code.first;
3850 uint64_t usTimeOffset = 0;
3851
3852 if (1 == currentCodeIndex)
3853 { // already incremented
3854 firstCodeTimeUs = code.first;
3855 }
3856 else
3857 {
3858 usTimeOffset = code.first - firstCodeTimeUs;
3859 }
3860
3861 // skip if no specific codeIndex is specified and currentCodeIndex does
3862 // not fall between top and skip
3863 if ((codeIndex == 0) &&
3864 (currentCodeIndex <= skip || currentCodeIndex > top))
3865 {
3866 continue;
3867 }
3868
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003869 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003870 // currentIndex
3871 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3872 {
3873 // This is done for simplicity. 1st entry is needed to calculate
3874 // time offset. To improve efficiency, one can get to the entry
3875 // directly (possibly with flatmap's nth method)
3876 continue;
3877 }
3878
3879 // currentCodeIndex is within top and skip or equal to specified code
3880 // index
3881
3882 // Get the Created time from the timestamp
3883 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003884 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003885
3886 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3887 std::ostringstream hexCode;
3888 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303889 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003890 std::ostringstream timeOffsetStr;
3891 // Set Fixed -Point Notation
3892 timeOffsetStr << std::fixed;
3893 // Set precision to 4 digits
3894 timeOffsetStr << std::setprecision(4);
3895 // Add double to stream
3896 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003897
Ed Tanous1e6deaf2022-02-17 11:32:37 -08003898 std::string bootIndexStr = std::to_string(bootIndex);
3899 std::string timeOffsetString = timeOffsetStr.str();
3900 std::string hexCodeStr = hexCode.str();
3901
3902 std::array<std::string_view, 3> messageArgs = {
3903 bootIndexStr, timeOffsetString, hexCodeStr};
3904
3905 std::string msg =
3906 redfish::registries::fillMessageArgs(messageArgs, message->message);
3907 if (msg.empty())
ZhikuiRena3316fc2020-01-29 14:58:08 -08003908 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08003909 messages::internalError(asyncResp->res);
3910 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003911 }
3912
Tim Leed4342a92020-04-27 11:47:58 +08003913 // Get Severity template from message registry
3914 std::string severity;
3915 if (message != nullptr)
3916 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003917 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003918 }
3919
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003920 // Format entry
3921 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003922 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003923 bmcLogEntry["@odata.id"] = boost::urls::format(
3924 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
3925 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003926 bmcLogEntry["Name"] = "POST Code Log Entry";
3927 bmcLogEntry["Id"] = postcodeEntryID;
3928 bmcLogEntry["Message"] = std::move(msg);
3929 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
Ed Tanous1e6deaf2022-02-17 11:32:37 -08003930 bmcLogEntry["MessageArgs"] = messageArgs;
Jason M. Bills84afc482022-06-24 12:38:23 -07003931 bmcLogEntry["EntryType"] = "Event";
3932 bmcLogEntry["Severity"] = std::move(severity);
3933 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003934 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3935 {
3936 bmcLogEntry["AdditionalDataURI"] =
3937 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3938 postcodeEntryID + "/attachment";
3939 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003940
3941 // codeIndex is only specified when querying single entry, return only
3942 // that entry in this case
3943 if (codeIndex != 0)
3944 {
Ed Tanousac106bf2023-06-07 09:24:59 -07003945 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003946 return true;
3947 }
3948
Ed Tanousac106bf2023-06-07 09:24:59 -07003949 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05003950 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003951 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003952
3953 // Return value is always false when querying multiple entries
3954 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003955}
3956
Ed Tanousac106bf2023-06-07 09:24:59 -07003957static void
3958 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3959 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003960{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003961 uint16_t bootIndex = 0;
3962 uint64_t codeIndex = 0;
3963 if (!parsePostCode(entryId, codeIndex, bootIndex))
3964 {
3965 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07003966 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003967 return;
3968 }
3969
3970 if (bootIndex == 0 || codeIndex == 0)
3971 {
3972 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07003973 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003974 return;
3975 }
3976
ZhikuiRena3316fc2020-01-29 14:58:08 -08003977 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07003978 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003979 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303980 const boost::container::flat_map<
3981 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3982 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003983 if (ec)
3984 {
3985 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
Ed Tanousac106bf2023-06-07 09:24:59 -07003986 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07003987 return;
3988 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003989
Ed Tanous002d39b2022-05-31 08:59:27 -07003990 if (postcode.empty())
3991 {
Ed Tanousac106bf2023-06-07 09:24:59 -07003992 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003993 return;
3994 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003995
Ed Tanousac106bf2023-06-07 09:24:59 -07003996 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003997 {
Ed Tanousac106bf2023-06-07 09:24:59 -07003998 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003999 return;
4000 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004001 },
Jonathan Doman15124762021-01-07 17:54:17 -08004002 "xyz.openbmc_project.State.Boot.PostCode0",
4003 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004004 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4005 bootIndex);
4006}
4007
Ed Tanousac106bf2023-06-07 09:24:59 -07004008static void
4009 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4010 const uint16_t bootIndex, const uint16_t bootCount,
4011 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004012{
4013 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004014 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004015 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304016 const boost::container::flat_map<
4017 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4018 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004019 if (ec)
4020 {
4021 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
Ed Tanousac106bf2023-06-07 09:24:59 -07004022 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004023 return;
4024 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004025
Ed Tanous002d39b2022-05-31 08:59:27 -07004026 uint64_t endCount = entryCount;
4027 if (!postcode.empty())
4028 {
4029 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07004030 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004031 {
Patrick Williams89492a12023-05-10 07:51:34 -05004032 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
4033 entryCount) -
4034 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004035 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07004036 std::min(static_cast<uint64_t>(top + skip), endCount) -
4037 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004038
Ed Tanousac106bf2023-06-07 09:24:59 -07004039 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4040 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004041 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004042 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004043 }
4044
4045 // continue to previous bootIndex
4046 if (bootIndex < bootCount)
4047 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004048 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07004049 bootCount, endCount, skip, top);
4050 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08004051 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07004052 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004053 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07004054 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4055 std::to_string(skip + top);
4056 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004057 },
Jonathan Doman15124762021-01-07 17:54:17 -08004058 "xyz.openbmc_project.State.Boot.PostCode0",
4059 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004060 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4061 bootIndex);
4062}
4063
zhanghch058d1b46d2021-04-01 11:18:24 +08004064static void
Ed Tanousac106bf2023-06-07 09:24:59 -07004065 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07004066 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004067{
4068 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004069 sdbusplus::asio::getProperty<uint16_t>(
4070 *crow::connections::systemBus,
4071 "xyz.openbmc_project.State.Boot.PostCode0",
4072 "/xyz/openbmc_project/State/Boot/PostCode0",
4073 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07004074 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
4075 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004076 if (ec)
4077 {
4078 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
Ed Tanousac106bf2023-06-07 09:24:59 -07004079 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004080 return;
4081 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004082 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004083 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08004084}
4085
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004086inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004087{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004088 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004089 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07004090 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004091 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004092 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07004093 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4094 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004095 query_param::QueryCapabilities capabilities = {
4096 .canDelegateTop = true,
4097 .canDelegateSkip = true,
4098 };
4099 query_param::Query delegatedQuery;
4100 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00004101 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07004102 {
4103 return;
4104 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004105 if constexpr (bmcwebEnableMultiHost)
4106 {
4107 // Option currently returns no systems. TBD
4108 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4109 systemName);
4110 return;
4111 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004112
4113 if (systemName != "system")
4114 {
4115 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4116 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004117 return;
4118 }
Ed Tanous002d39b2022-05-31 08:59:27 -07004119 asyncResp->res.jsonValue["@odata.type"] =
4120 "#LogEntryCollection.LogEntryCollection";
4121 asyncResp->res.jsonValue["@odata.id"] =
4122 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4123 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4124 asyncResp->res.jsonValue["Description"] =
4125 "Collection of POST Code Log Entries";
4126 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4127 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07004128 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08004129 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07004130 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07004131 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004132}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004133
George Liu647b3cd2021-07-05 12:43:56 +08004134inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4135{
George Liu0fda0f12021-11-16 10:06:17 +08004136 BMCWEB_ROUTE(
4137 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004138 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004139 .privileges(redfish::privileges::getLogEntry)
4140 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004141 [&app](const crow::Request& req,
4142 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004143 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004144 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004145 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004146 {
4147 return;
4148 }
Matt Spinler72e21372023-04-19 12:53:33 -05004149 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004150 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004151 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004152 {
4153 asyncResp->res.result(boost::beast::http::status::bad_request);
4154 return;
4155 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004156 if constexpr (bmcwebEnableMultiHost)
4157 {
4158 // Option currently returns no systems. TBD
4159 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4160 systemName);
4161 return;
4162 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004163 if (systemName != "system")
4164 {
4165 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4166 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004167 return;
4168 }
George Liu647b3cd2021-07-05 12:43:56 +08004169
Ed Tanous002d39b2022-05-31 08:59:27 -07004170 uint64_t currentValue = 0;
4171 uint16_t index = 0;
4172 if (!parsePostCode(postCodeID, currentValue, index))
4173 {
4174 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4175 return;
4176 }
George Liu647b3cd2021-07-05 12:43:56 +08004177
Ed Tanous002d39b2022-05-31 08:59:27 -07004178 crow::connections::systemBus->async_method_call(
4179 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004180 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004181 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4182 postcodes) {
4183 if (ec.value() == EBADR)
4184 {
4185 messages::resourceNotFound(asyncResp->res, "LogEntry",
4186 postCodeID);
4187 return;
4188 }
4189 if (ec)
4190 {
4191 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4192 messages::internalError(asyncResp->res);
4193 return;
4194 }
George Liu647b3cd2021-07-05 12:43:56 +08004195
Ed Tanous002d39b2022-05-31 08:59:27 -07004196 size_t value = static_cast<size_t>(currentValue) - 1;
4197 if (value == std::string::npos || postcodes.size() < currentValue)
4198 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004199 BMCWEB_LOG_WARNING << "Wrong currentValue value";
Ed Tanous002d39b2022-05-31 08:59:27 -07004200 messages::resourceNotFound(asyncResp->res, "LogEntry",
4201 postCodeID);
4202 return;
4203 }
George Liu647b3cd2021-07-05 12:43:56 +08004204
Ed Tanous002d39b2022-05-31 08:59:27 -07004205 const auto& [tID, c] = postcodes[value];
4206 if (c.empty())
4207 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004208 BMCWEB_LOG_WARNING << "No found post code data";
Ed Tanous002d39b2022-05-31 08:59:27 -07004209 messages::resourceNotFound(asyncResp->res, "LogEntry",
4210 postCodeID);
4211 return;
4212 }
4213 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4214 const char* d = reinterpret_cast<const char*>(c.data());
4215 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004216
Ed Tanousd9f6c622022-03-17 09:12:17 -07004217 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004218 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004219 asyncResp->res.addHeader(
4220 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004221 asyncResp->res.body() = crow::utility::base64encode(strData);
4222 },
4223 "xyz.openbmc_project.State.Boot.PostCode0",
4224 "/xyz/openbmc_project/State/Boot/PostCode0",
4225 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4226 });
George Liu647b3cd2021-07-05 12:43:56 +08004227}
4228
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004229inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004230{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004231 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004232 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004233 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004234 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004235 [&app](const crow::Request& req,
4236 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004237 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004238 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004239 {
4240 return;
4241 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004242 if constexpr (bmcwebEnableMultiHost)
4243 {
4244 // Option currently returns no systems. TBD
4245 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4246 systemName);
4247 return;
4248 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004249 if (systemName != "system")
4250 {
4251 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4252 systemName);
4253 return;
4254 }
4255
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004256 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004257 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004258}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004259
Ed Tanous1da66f72018-07-27 16:13:37 -07004260} // namespace redfish