blob: c52c99ba55afc8e1ceda8c1389b6a7e54433f72e [file] [log] [blame]
Ed Tanous1da66f72018-07-27 16:13:37 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "error_messages.hpp"
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -060021#include "generated/enums/log_entry.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080022#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080023#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080024#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "query.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070026#include "registries.hpp"
27#include "registries/base_message_registry.hpp"
28#include "registries/openbmc_message_registry.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include "registries/privilege_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080030#include "task.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080031#include "utils/dbus_utils.hpp"
32#include "utils/time_utils.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070033
Jason M. Billse1f26342018-07-18 12:12:00 -070034#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060035#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060036#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070037
Ed Tanous9896eae2022-07-23 15:07:33 -070038#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070039#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060040#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070041#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070042#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070043#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080044#include <boost/system/linux_error.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020045#include <sdbusplus/asio/property.hpp>
46#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050047
George Liu7a1dbc42022-12-07 16:03:22 +080048#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080049#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070050#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080051#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070052#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070053#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080054#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070055
56namespace redfish
57{
58
Patrick Williams89492a12023-05-10 07:51:34 -050059constexpr const char* crashdumpObject = "com.intel.crashdump";
60constexpr const char* crashdumpPath = "/com/intel/crashdump";
61constexpr const char* crashdumpInterface = "com.intel.crashdump";
62constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070063 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050064constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070065 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050066constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070067 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070068
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060069enum class DumpCreationProgress
70{
71 DUMP_CREATE_SUCCESS,
72 DUMP_CREATE_FAILED,
73 DUMP_CREATE_INPROGRESS
74};
75
James Feistf6150402019-01-08 10:36:20 -080076namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070077
Gunnar Mills1214b7e2020-06-04 10:11:30 -050078inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070079{
Ed Tanousd4d25792020-09-29 15:15:03 -070080 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
81 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
82 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
83 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070084 {
85 return "Critical";
86 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070087 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
88 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
89 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070090 {
91 return "OK";
92 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070093 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070094 {
95 return "Warning";
96 }
97 return "";
98}
99
Abhishek Patel9017faf2021-09-14 22:48:55 -0500100inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
101{
102 std::optional<bool> notifyAction;
103 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
104 {
105 notifyAction = true;
106 }
107 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
108 {
109 notifyAction = false;
110 }
111
112 return notifyAction;
113}
114
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700115inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800116 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700117 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700118{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500119 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700120 size_t length = 0;
121 int ret = 0;
122 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800123 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
124 const void** dataVoid = reinterpret_cast<const void**>(&data);
125
126 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700127 if (ret < 0)
128 {
129 return ret;
130 }
Ed Tanous39e77502019-03-04 17:35:53 -0800131 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700132 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000133 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700134 return ret;
135}
136
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700137inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800138 std::string_view field, const int& base,
139 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700140{
141 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800142 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700143 // Get the metadata from the requested field of the journal entry
144 ret = getJournalMetadata(journal, field, metadata);
145 if (ret < 0)
146 {
147 return ret;
148 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000149 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700150 return ret;
151}
152
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700153inline static bool getEntryTimestamp(sd_journal* journal,
154 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800155{
156 int ret = 0;
157 uint64_t timestamp = 0;
158 ret = sd_journal_get_realtime_usec(journal, &timestamp);
159 if (ret < 0)
160 {
161 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
162 << strerror(-ret);
163 return false;
164 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300165 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500166 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800167}
Ed Tanous50b8a432022-02-03 16:29:50 -0800168
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700169inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
170 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700171{
172 int ret = 0;
173 static uint64_t prevTs = 0;
174 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700175 if (firstEntry)
176 {
177 prevTs = 0;
178 }
179
Jason M. Bills16428a12018-11-02 12:42:29 -0700180 // Get the entry timestamp
181 uint64_t curTs = 0;
182 ret = sd_journal_get_realtime_usec(journal, &curTs);
183 if (ret < 0)
184 {
185 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
186 << strerror(-ret);
187 return false;
188 }
189 // If the timestamp isn't unique, increment the index
190 if (curTs == prevTs)
191 {
192 index++;
193 }
194 else
195 {
196 // Otherwise, reset it
197 index = 0;
198 }
199 // Save the timestamp
200 prevTs = curTs;
201
202 entryID = std::to_string(curTs);
203 if (index > 0)
204 {
205 entryID += "_" + std::to_string(index);
206 }
207 return true;
208}
209
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500210static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700211 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700212{
Ed Tanous271584a2019-07-09 16:24:22 -0700213 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700214 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700215 if (firstEntry)
216 {
217 prevTs = 0;
218 }
219
Jason M. Bills95820182019-04-22 16:25:34 -0700220 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700221 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700222 std::tm timeStruct = {};
223 std::istringstream entryStream(logEntry);
224 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
225 {
226 curTs = std::mktime(&timeStruct);
227 }
228 // If the timestamp isn't unique, increment the index
229 if (curTs == prevTs)
230 {
231 index++;
232 }
233 else
234 {
235 // Otherwise, reset it
236 index = 0;
237 }
238 // Save the timestamp
239 prevTs = curTs;
240
241 entryID = std::to_string(curTs);
242 if (index > 0)
243 {
244 entryID += "_" + std::to_string(index);
245 }
246 return true;
247}
248
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700249inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
251 const std::string& entryID, uint64_t& timestamp,
252 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700253{
254 if (entryID.empty())
255 {
256 return false;
257 }
258 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800259 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700260
Ed Tanous81ce6092020-12-17 16:54:55 +0000261 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800262 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700263 {
264 // Timestamp has an index
265 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800266 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700267 indexStr.remove_prefix(underscorePos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500268 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
269 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700270 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700271 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800272 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700273 return false;
274 }
275 }
276 // Timestamp has no index
Patrick Williams84396af2023-05-11 11:47:45 -0500277 auto [ptr, ec] = std::from_chars(tsStr.begin(), tsStr.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700278 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700279 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800280 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700281 return false;
282 }
283 return true;
284}
285
Jason M. Bills95820182019-04-22 16:25:34 -0700286static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500287 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700288{
289 static const std::filesystem::path redfishLogDir = "/var/log";
290 static const std::string redfishLogFilename = "redfish";
291
292 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500293 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700294 std::filesystem::directory_iterator(redfishLogDir))
295 {
296 // If we find a redfish log file, save the path
297 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700298 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700299 {
300 redfishLogFiles.emplace_back(redfishLogDir / filename);
301 }
302 }
303 // As the log files rotate, they are appended with a ".#" that is higher for
304 // the older logs. Since we don't expect more than 10 log files, we
305 // can just sort the list to get them in order from newest to oldest
306 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
307
308 return !redfishLogFiles.empty();
309}
310
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600311inline log_entry::OriginatorTypes
312 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
313{
314 if (originatorType ==
315 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
316 {
317 return log_entry::OriginatorTypes::Client;
318 }
319 if (originatorType ==
320 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
321 {
322 return log_entry::OriginatorTypes::Internal;
323 }
324 if (originatorType ==
325 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
326 {
327 return log_entry::OriginatorTypes::SupportingService;
328 }
329 return log_entry::OriginatorTypes::Invalid;
330}
331
Claire Weinanaefe3782022-07-15 19:17:19 -0700332inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800333 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700334 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600335 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700336 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
337{
338 for (const auto& interfaceMap : object.second)
339 {
340 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
341 {
342 for (const auto& propertyMap : interfaceMap.second)
343 {
344 if (propertyMap.first == "Status")
345 {
346 const auto* status =
347 std::get_if<std::string>(&propertyMap.second);
348 if (status == nullptr)
349 {
350 messages::internalError(asyncResp->res);
351 break;
352 }
353 dumpStatus = *status;
354 }
355 }
356 }
357 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
358 {
359 for (const auto& propertyMap : interfaceMap.second)
360 {
361 if (propertyMap.first == "Size")
362 {
363 const auto* sizePtr =
364 std::get_if<uint64_t>(&propertyMap.second);
365 if (sizePtr == nullptr)
366 {
367 messages::internalError(asyncResp->res);
368 break;
369 }
370 size = *sizePtr;
371 break;
372 }
373 }
374 }
375 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
376 {
377 for (const auto& propertyMap : interfaceMap.second)
378 {
379 if (propertyMap.first == "Elapsed")
380 {
381 const uint64_t* usecsTimeStamp =
382 std::get_if<uint64_t>(&propertyMap.second);
383 if (usecsTimeStamp == nullptr)
384 {
385 messages::internalError(asyncResp->res);
386 break;
387 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700388 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700389 break;
390 }
391 }
392 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600393 else if (interfaceMap.first ==
394 "xyz.openbmc_project.Common.OriginatedBy")
395 {
396 for (const auto& propertyMap : interfaceMap.second)
397 {
398 if (propertyMap.first == "OriginatorId")
399 {
400 const std::string* id =
401 std::get_if<std::string>(&propertyMap.second);
402 if (id == nullptr)
403 {
404 messages::internalError(asyncResp->res);
405 break;
406 }
407 originatorId = *id;
408 }
409
410 if (propertyMap.first == "OriginatorType")
411 {
412 const std::string* type =
413 std::get_if<std::string>(&propertyMap.second);
414 if (type == nullptr)
415 {
416 messages::internalError(asyncResp->res);
417 break;
418 }
419
420 originatorType = mapDbusOriginatorTypeToRedfish(*type);
421 if (originatorType == log_entry::OriginatorTypes::Invalid)
422 {
423 messages::internalError(asyncResp->res);
424 break;
425 }
426 }
427 }
428 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700429 }
430}
431
Nan Zhou21ab4042022-06-26 23:07:40 +0000432static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800433{
434 std::string entriesPath;
435
436 if (dumpType == "BMC")
437 {
438 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
439 }
440 else if (dumpType == "FaultLog")
441 {
442 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
443 }
444 else if (dumpType == "System")
445 {
446 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
447 }
448 else
449 {
450 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
451 << dumpType;
452 }
453
454 // Returns empty string on error
455 return entriesPath;
456}
457
zhanghch058d1b46d2021-04-01 11:18:24 +0800458inline void
459 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
460 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500461{
Claire Weinanfdd26902022-03-01 14:18:25 -0800462 std::string entriesPath = getDumpEntriesPath(dumpType);
463 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500464 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500465 messages::internalError(asyncResp->res);
466 return;
467 }
468
469 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800470 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800471 dumpType](const boost::system::error_code& ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800472 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700473 if (ec)
474 {
475 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
476 messages::internalError(asyncResp->res);
477 return;
478 }
479
Claire Weinanfdd26902022-03-01 14:18:25 -0800480 // Remove ending slash
481 std::string odataIdStr = entriesPath;
482 if (!odataIdStr.empty())
483 {
484 odataIdStr.pop_back();
485 }
486
487 asyncResp->res.jsonValue["@odata.type"] =
488 "#LogEntryCollection.LogEntryCollection";
489 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
490 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500491 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
492 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800493
Ed Tanous002d39b2022-05-31 08:59:27 -0700494 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
495 entriesArray = nlohmann::json::array();
496 std::string dumpEntryPath =
497 "/xyz/openbmc_project/dump/" +
498 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
499
500 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
501 return AlphanumLess<std::string>()(l.first.filename(),
502 r.first.filename());
503 });
504
505 for (auto& object : resp)
506 {
507 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500508 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700509 continue;
510 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700511 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700512 uint64_t size = 0;
513 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600514 std::string originatorId;
515 log_entry::OriginatorTypes originatorType =
516 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700517 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700518
519 std::string entryID = object.first.filename();
520 if (entryID.empty())
521 {
522 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500523 }
524
Claire Weinanc6fecda2022-07-15 10:43:25 -0700525 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600526 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700527 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700528
529 if (dumpStatus !=
530 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
531 !dumpStatus.empty())
532 {
533 // Dump status is not Complete, no need to enumerate
534 continue;
535 }
536
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600537 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800538 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700539 thisEntry["Id"] = entryID;
540 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700541 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700542 thisEntry["Created"] =
543 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700544
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600545 if (!originatorId.empty())
546 {
547 thisEntry["Originator"] = originatorId;
548 thisEntry["OriginatorType"] = originatorType;
549 }
550
Ed Tanous002d39b2022-05-31 08:59:27 -0700551 if (dumpType == "BMC")
552 {
553 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500554 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
555 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800556 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700557 }
558 else if (dumpType == "System")
559 {
560 thisEntry["DiagnosticDataType"] = "OEM";
561 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500562 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
563 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800564 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700565 }
566 entriesArray.push_back(std::move(thisEntry));
567 }
568 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500569 },
570 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
571 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
572}
573
zhanghch058d1b46d2021-04-01 11:18:24 +0800574inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700575 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800576 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500577{
Claire Weinanfdd26902022-03-01 14:18:25 -0800578 std::string entriesPath = getDumpEntriesPath(dumpType);
579 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500580 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 messages::internalError(asyncResp->res);
582 return;
583 }
584
585 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800586 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800587 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700588 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 if (ec)
590 {
591 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
592 messages::internalError(asyncResp->res);
593 return;
594 }
595
596 bool foundDumpEntry = false;
597 std::string dumpEntryPath =
598 "/xyz/openbmc_project/dump/" +
599 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
600
601 for (const auto& objectPath : resp)
602 {
603 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500604 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700605 continue;
606 }
607
608 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700609 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700610 uint64_t size = 0;
611 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600612 std::string originatorId;
613 log_entry::OriginatorTypes originatorType =
614 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700615
Claire Weinanaefe3782022-07-15 19:17:19 -0700616 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600617 timestampUs, originatorId,
618 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700619
620 if (dumpStatus !=
621 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
622 !dumpStatus.empty())
623 {
624 // Dump status is not Complete
625 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200626 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
627 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500628 return;
629 }
630
Ed Tanous002d39b2022-05-31 08:59:27 -0700631 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600632 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800633 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 asyncResp->res.jsonValue["Id"] = entryID;
635 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700636 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700637 asyncResp->res.jsonValue["Created"] =
638 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500639
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600640 if (!originatorId.empty())
641 {
642 asyncResp->res.jsonValue["Originator"] = originatorId;
643 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
644 }
645
Ed Tanous002d39b2022-05-31 08:59:27 -0700646 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500647 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700648 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
649 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800650 entriesPath + entryID + "/attachment";
651 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500652 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700653 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500654 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700655 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
656 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
657 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800658 entriesPath + entryID + "/attachment";
659 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500660 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700661 }
662 if (!foundDumpEntry)
663 {
664 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
665 messages::internalError(asyncResp->res);
666 return;
667 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500668 },
669 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
670 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
671}
672
zhanghch058d1b46d2021-04-01 11:18:24 +0800673inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800674 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500675 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500676{
Ed Tanous002d39b2022-05-31 08:59:27 -0700677 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800678 [asyncResp, entryID](const boost::system::error_code& ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500679 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
680 if (ec)
681 {
George Liu3de8d8b2021-03-22 17:49:39 +0800682 if (ec.value() == EBADR)
683 {
684 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
685 return;
686 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500687 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800688 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500689 messages::internalError(asyncResp->res);
690 return;
691 }
692 };
693 crow::connections::systemBus->async_method_call(
694 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500695 "/xyz/openbmc_project/dump/" +
696 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
697 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500698 "xyz.openbmc_project.Object.Delete", "Delete");
699}
700
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600701inline DumpCreationProgress
702 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500703{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600704 if (status ==
705 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
706 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
707 {
708 return DumpCreationProgress::DUMP_CREATE_FAILED;
709 }
710 if (status ==
711 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
712 {
713 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
714 }
715 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
716}
717
718inline DumpCreationProgress
719 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
720{
721 for (const auto& [key, val] : values)
722 {
723 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700724 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600725 const std::string* value = std::get_if<std::string>(&val);
726 if (value == nullptr)
727 {
728 BMCWEB_LOG_ERROR << "Status property value is null";
729 return DumpCreationProgress::DUMP_CREATE_FAILED;
730 }
731 return mapDbusStatusToDumpProgress(*value);
732 }
733 }
734 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
735}
736
737inline std::string getDumpEntryPath(const std::string& dumpPath)
738{
739 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
740 {
741 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
742 }
743 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
744 {
745 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
746 }
747 return "";
748}
749
750inline void createDumpTaskCallback(
751 task::Payload&& payload,
752 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
753 const sdbusplus::message::object_path& createdObjPath)
754{
755 const std::string dumpPath = createdObjPath.parent_path().str;
756 const std::string dumpId = createdObjPath.filename();
757
758 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
759
760 if (dumpEntryPath.empty())
761 {
762 BMCWEB_LOG_ERROR << "Invalid dump type received";
763 messages::internalError(asyncResp->res);
764 return;
765 }
766
767 crow::connections::systemBus->async_method_call(
768 [asyncResp, payload, createdObjPath,
769 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800770 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600771 const std::string& introspectXml) {
772 if (ec)
773 {
774 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
775 << ec.message();
776 messages::internalError(asyncResp->res);
777 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700778 }
779
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600780 // Check if the created dump object has implemented Progress
781 // interface to track dump completion. If yes, fetch the "Status"
782 // property of the interface, modify the task state accordingly.
783 // Else, return task completed.
784 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700785
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600786 doc.Parse(introspectXml.data(), introspectXml.size());
787 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
788 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700789 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600790 BMCWEB_LOG_ERROR << "XML document failed to parse";
791 messages::internalError(asyncResp->res);
792 return;
793 }
794 tinyxml2::XMLElement* interfaceNode =
795 pRoot->FirstChildElement("interface");
796
797 bool isProgressIntfPresent = false;
798 while (interfaceNode != nullptr)
799 {
800 const char* thisInterfaceName = interfaceNode->Attribute("name");
801 if (thisInterfaceName != nullptr)
802 {
803 if (thisInterfaceName ==
804 std::string_view("xyz.openbmc_project.Common.Progress"))
805 {
806 interfaceNode =
807 interfaceNode->NextSiblingElement("interface");
808 continue;
809 }
810 isProgressIntfPresent = true;
811 break;
812 }
813 interfaceNode = interfaceNode->NextSiblingElement("interface");
814 }
815
816 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
817 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800818 const boost::system::error_code& err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600819 const std::shared_ptr<task::TaskData>& taskData) {
820 if (err)
821 {
822 BMCWEB_LOG_ERROR << createdObjPath.str
823 << ": Error in creating dump";
824 taskData->messages.emplace_back(messages::internalError());
825 taskData->state = "Cancelled";
826 return task::completed;
827 }
828
829 if (isProgressIntfPresent)
830 {
831 dbus::utility::DBusPropertiesMap values;
832 std::string prop;
833 msg.read(prop, values);
834
835 DumpCreationProgress dumpStatus =
836 getDumpCompletionStatus(values);
837 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
838 {
839 BMCWEB_LOG_ERROR << createdObjPath.str
840 << ": Error in creating dump";
841 taskData->state = "Cancelled";
842 return task::completed;
843 }
844
845 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
846 {
847 BMCWEB_LOG_DEBUG << createdObjPath.str
848 << ": Dump creation task is in progress";
849 return !task::completed;
850 }
851 }
852
Ed Tanous002d39b2022-05-31 08:59:27 -0700853 nlohmann::json retMessage = messages::success();
854 taskData->messages.emplace_back(retMessage);
855
Patrick Williams89492a12023-05-10 07:51:34 -0500856 std::string headerLoc = "Location: " + dumpEntryPath +
857 http_helpers::urlEncode(dumpId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700858 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
859
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600860 BMCWEB_LOG_DEBUG << createdObjPath.str
861 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700862 taskData->state = "Completed";
863 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600864 },
865 "type='signal',interface='org.freedesktop.DBus.Properties',"
866 "member='PropertiesChanged',path='" +
867 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500868
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600869 // The task timer is set to max time limit within which the
870 // requested dump will be collected.
871 task->startTimer(std::chrono::minutes(6));
872 task->populateResp(asyncResp->res);
873 task->payload.emplace(payload);
874 },
875 "xyz.openbmc_project.Dump.Manager", createdObjPath,
876 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500877}
878
zhanghch058d1b46d2021-04-01 11:18:24 +0800879inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
880 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500881{
Claire Weinanfdd26902022-03-01 14:18:25 -0800882 std::string dumpPath = getDumpEntriesPath(dumpType);
883 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500884 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500885 messages::internalError(asyncResp->res);
886 return;
887 }
888
889 std::optional<std::string> diagnosticDataType;
890 std::optional<std::string> oemDiagnosticDataType;
891
Willy Tu15ed6782021-12-14 11:03:16 -0800892 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500893 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
894 "OEMDiagnosticDataType", oemDiagnosticDataType))
895 {
896 return;
897 }
898
899 if (dumpType == "System")
900 {
901 if (!oemDiagnosticDataType || !diagnosticDataType)
902 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800903 BMCWEB_LOG_ERROR
904 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500905 messages::actionParameterMissing(
906 asyncResp->res, "CollectDiagnosticData",
907 "DiagnosticDataType & OEMDiagnosticDataType");
908 return;
909 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700910 if ((*oemDiagnosticDataType != "System") ||
911 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500912 {
913 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700914 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500915 return;
916 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500917 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500918 }
919 else if (dumpType == "BMC")
920 {
921 if (!diagnosticDataType)
922 {
George Liu0fda0f12021-11-16 10:06:17 +0800923 BMCWEB_LOG_ERROR
924 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500925 messages::actionParameterMissing(
926 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
927 return;
928 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700929 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500930 {
931 BMCWEB_LOG_ERROR
932 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700933 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500934 return;
935 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500936 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
937 }
938 else
939 {
940 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
941 messages::internalError(asyncResp->res);
942 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500943 }
944
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600945 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
946 createDumpParamVec;
947
Carson Labradof574a8e2023-03-22 02:26:00 +0000948 if (req.session != nullptr)
949 {
950 createDumpParamVec.emplace_back(
951 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
952 req.session->clientIp);
953 createDumpParamVec.emplace_back(
954 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
955 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
956 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600957
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500958 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800959 [asyncResp, payload(task::Payload(req)),
960 dumpPath](const boost::system::error_code& ec,
961 const sdbusplus::message_t& msg,
962 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700963 if (ec)
964 {
965 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500966 const sd_bus_error* dbusError = msg.get_error();
967 if (dbusError == nullptr)
968 {
969 messages::internalError(asyncResp->res);
970 return;
971 }
972
973 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
974 << " and error msg: " << dbusError->message;
975 if (std::string_view(
976 "xyz.openbmc_project.Common.Error.NotAllowed") ==
977 dbusError->name)
978 {
979 messages::resourceInStandby(asyncResp->res);
980 return;
981 }
982 if (std::string_view(
983 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
984 dbusError->name)
985 {
986 messages::serviceDisabled(asyncResp->res, dumpPath);
987 return;
988 }
989 if (std::string_view(
990 "xyz.openbmc_project.Common.Error.Unavailable") ==
991 dbusError->name)
992 {
993 messages::resourceInUse(asyncResp->res);
994 return;
995 }
996 // Other Dbus errors such as:
997 // xyz.openbmc_project.Common.Error.InvalidArgument &
998 // org.freedesktop.DBus.Error.InvalidArgs are all related to
999 // the dbus call that is made here in the bmcweb
1000 // implementation and has nothing to do with the client's
1001 // input in the request. Hence, returning internal error
1002 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001003 messages::internalError(asyncResp->res);
1004 return;
1005 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001006 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
1007 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001008 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001009 "xyz.openbmc_project.Dump.Manager",
1010 "/xyz/openbmc_project/dump/" +
1011 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001012 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001013}
1014
zhanghch058d1b46d2021-04-01 11:18:24 +08001015inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1016 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001017{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001018 std::string dumpTypeLowerCopy =
1019 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001020
Claire Weinan0d946212022-07-13 19:40:19 -07001021 crow::connections::systemBus->async_method_call(
1022 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001023 if (ec)
1024 {
Claire Weinan0d946212022-07-13 19:40:19 -07001025 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -07001026 messages::internalError(asyncResp->res);
1027 return;
1028 }
Claire Weinan0d946212022-07-13 19:40:19 -07001029 },
1030 "xyz.openbmc_project.Dump.Manager",
1031 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1032 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001033}
1034
Ed Tanousb9d36b42022-02-26 21:42:46 -08001035inline static void
1036 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1037 std::string& filename, std::string& timestamp,
1038 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001039{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001040 const std::string* filenamePtr = nullptr;
1041 const std::string* timestampPtr = nullptr;
1042 const std::string* logfilePtr = nullptr;
1043
1044 const bool success = sdbusplus::unpackPropertiesNoThrow(
1045 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1046 "Filename", filenamePtr, "Log", logfilePtr);
1047
1048 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001049 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001050 return;
1051 }
1052
1053 if (filenamePtr != nullptr)
1054 {
1055 filename = *filenamePtr;
1056 }
1057
1058 if (timestampPtr != nullptr)
1059 {
1060 timestamp = *timestampPtr;
1061 }
1062
1063 if (logfilePtr != nullptr)
1064 {
1065 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001066 }
1067}
1068
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001069inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001070{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001071 /**
1072 * Functions triggers appropriate requests on DBus
1073 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001074 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001075 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001076 .methods(boost::beast::http::verb::get)(
1077 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001078 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1079 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001080 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001081 {
1082 return;
1083 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001084 if (systemName != "system")
1085 {
1086 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1087 systemName);
1088 return;
1089 }
1090
Ed Tanous002d39b2022-05-31 08:59:27 -07001091 // Collections don't include the static data added by SubRoute
1092 // because it has a duplicate entry for members
1093 asyncResp->res.jsonValue["@odata.type"] =
1094 "#LogServiceCollection.LogServiceCollection";
1095 asyncResp->res.jsonValue["@odata.id"] =
1096 "/redfish/v1/Systems/system/LogServices";
1097 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1098 asyncResp->res.jsonValue["Description"] =
1099 "Collection of LogServices for this Computer System";
1100 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1101 logServiceArray = nlohmann::json::array();
1102 nlohmann::json::object_t eventLog;
1103 eventLog["@odata.id"] =
1104 "/redfish/v1/Systems/system/LogServices/EventLog";
1105 logServiceArray.push_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001106#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001107 nlohmann::json::object_t dumpLog;
1108 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
1109 logServiceArray.push_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001110#endif
1111
Jason M. Billsd53dd412019-02-12 17:16:22 -08001112#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001113 nlohmann::json::object_t crashdump;
1114 crashdump["@odata.id"] =
1115 "/redfish/v1/Systems/system/LogServices/Crashdump";
1116 logServiceArray.push_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001117#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001118
1119#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 nlohmann::json::object_t hostlogger;
1121 hostlogger["@odata.id"] =
1122 "/redfish/v1/Systems/system/LogServices/HostLogger";
1123 logServiceArray.push_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001124#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001125 asyncResp->res.jsonValue["Members@odata.count"] =
1126 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001127
George Liu7a1dbc42022-12-07 16:03:22 +08001128 constexpr std::array<std::string_view, 1> interfaces = {
1129 "xyz.openbmc_project.State.Boot.PostCode"};
1130 dbus::utility::getSubTreePaths(
1131 "/", 0, interfaces,
1132 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001133 const dbus::utility::MapperGetSubTreePathsResponse&
1134 subtreePath) {
1135 if (ec)
1136 {
1137 BMCWEB_LOG_ERROR << ec;
1138 return;
1139 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001140
Ed Tanous002d39b2022-05-31 08:59:27 -07001141 for (const auto& pathStr : subtreePath)
1142 {
1143 if (pathStr.find("PostCode") != std::string::npos)
1144 {
1145 nlohmann::json& logServiceArrayLocal =
1146 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001147 nlohmann::json::object_t member;
1148 member["@odata.id"] =
1149 "/redfish/v1/Systems/system/LogServices/PostCodes";
1150
1151 logServiceArrayLocal.push_back(std::move(member));
1152
Ed Tanous002d39b2022-05-31 08:59:27 -07001153 asyncResp->res.jsonValue["Members@odata.count"] =
1154 logServiceArrayLocal.size();
1155 return;
1156 }
1157 }
George Liu7a1dbc42022-12-07 16:03:22 +08001158 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001159 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001160}
1161
1162inline void requestRoutesEventLogService(App& app)
1163{
Ed Tanous22d268c2022-05-19 09:39:07 -07001164 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001165 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001166 .methods(boost::beast::http::verb::get)(
1167 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001168 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1169 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001170 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001171 {
1172 return;
1173 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001174 if (systemName != "system")
1175 {
1176 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1177 systemName);
1178 return;
1179 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001180 asyncResp->res.jsonValue["@odata.id"] =
1181 "/redfish/v1/Systems/system/LogServices/EventLog";
1182 asyncResp->res.jsonValue["@odata.type"] =
1183 "#LogService.v1_1_0.LogService";
1184 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1185 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1186 asyncResp->res.jsonValue["Id"] = "EventLog";
1187 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301188
Ed Tanous002d39b2022-05-31 08:59:27 -07001189 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001190 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301191
Ed Tanous002d39b2022-05-31 08:59:27 -07001192 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1193 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1194 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301195
Ed Tanous002d39b2022-05-31 08:59:27 -07001196 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1197 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1198 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001199
Ed Tanous002d39b2022-05-31 08:59:27 -07001200 {"target",
1201 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001202 });
1203}
1204
1205inline void requestRoutesJournalEventLogClear(App& app)
1206{
Jason M. Bills4978b632022-02-22 14:17:43 -08001207 BMCWEB_ROUTE(
1208 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001209 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001210 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001211 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001212 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001213 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1214 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001215 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001216 {
1217 return;
1218 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001219 if (systemName != "system")
1220 {
1221 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1222 systemName);
1223 return;
1224 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001225 // Clear the EventLog by deleting the log files
1226 std::vector<std::filesystem::path> redfishLogFiles;
1227 if (getRedfishLogFiles(redfishLogFiles))
1228 {
1229 for (const std::filesystem::path& file : redfishLogFiles)
1230 {
1231 std::error_code ec;
1232 std::filesystem::remove(file, ec);
1233 }
1234 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001235
Ed Tanous002d39b2022-05-31 08:59:27 -07001236 // Reload rsyslog so it knows to start new log files
1237 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001238 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001239 if (ec)
1240 {
1241 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1242 messages::internalError(asyncResp->res);
1243 return;
1244 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001245
Ed Tanous002d39b2022-05-31 08:59:27 -07001246 messages::success(asyncResp->res);
1247 },
1248 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1249 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1250 "replace");
1251 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001252}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001253
Jason M. Billsac992cd2022-06-24 13:31:46 -07001254enum class LogParseError
1255{
1256 success,
1257 parseFailed,
1258 messageIdNotInRegistry,
1259};
1260
1261static LogParseError
1262 fillEventLogEntryJson(const std::string& logEntryID,
1263 const std::string& logEntry,
1264 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001265{
Jason M. Bills95820182019-04-22 16:25:34 -07001266 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001267 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001268 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001269 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001270 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001271 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001272 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001273 std::string timestamp = logEntry.substr(0, space);
1274 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001275 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001276 if (entryStart == std::string::npos)
1277 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001278 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001279 }
1280 std::string_view entry(logEntry);
1281 entry.remove_prefix(entryStart);
1282 // Use split to separate the entry into its fields
1283 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001284 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001285 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001286 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001287 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001288 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001289 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001290 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001291
Jason M. Bills4851d452019-03-28 11:27:48 -07001292 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001293 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001294
Sui Chen54417b02022-03-24 14:59:52 -07001295 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001296 {
Sui Chen54417b02022-03-24 14:59:52 -07001297 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001298 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001299 }
1300
Sui Chen54417b02022-03-24 14:59:52 -07001301 std::string msg = message->message;
1302
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001303 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001304 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001305 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001306 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001307 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001308 // If the first string is empty, assume there are no MessageArgs
1309 std::size_t messageArgsSize = 0;
1310 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001311 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001312 messageArgsSize = logEntryFields.size() - 1;
1313 }
1314
Ed Tanous23a21a12020-07-25 04:45:05 +00001315 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001316
1317 // Fill the MessageArgs into the Message
1318 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001319 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001320 {
1321 std::string argStr = "%" + std::to_string(++i);
1322 size_t argPos = msg.find(argStr);
1323 if (argPos != std::string::npos)
1324 {
1325 msg.replace(argPos, argStr.length(), messageArg);
1326 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001327 }
1328 }
1329
Jason M. Bills95820182019-04-22 16:25:34 -07001330 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1331 // format which matches the Redfish format except for the fractional seconds
1332 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001333 std::size_t dot = timestamp.find_first_of('.');
1334 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001335 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001336 {
Jason M. Bills95820182019-04-22 16:25:34 -07001337 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001338 }
1339
1340 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001341 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001342 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
1343 "redfish", "v1", "Systems", "system", "LogServices", "EventLog",
1344 "Entries", logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001345 logEntryJson["Name"] = "System Event Log Entry";
1346 logEntryJson["Id"] = logEntryID;
1347 logEntryJson["Message"] = std::move(msg);
1348 logEntryJson["MessageId"] = std::move(messageID);
1349 logEntryJson["MessageArgs"] = messageArgs;
1350 logEntryJson["EntryType"] = "Event";
1351 logEntryJson["Severity"] = message->messageSeverity;
1352 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001353 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001354}
1355
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001356inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001357{
Ed Tanous22d268c2022-05-19 09:39:07 -07001358 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001359 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001360 .methods(boost::beast::http::verb::get)(
1361 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001362 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1363 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001364 query_param::QueryCapabilities capabilities = {
1365 .canDelegateTop = true,
1366 .canDelegateSkip = true,
1367 };
1368 query_param::Query delegatedQuery;
1369 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001370 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001371 {
1372 return;
1373 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001374 if (systemName != "system")
1375 {
1376 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1377 systemName);
1378 return;
1379 }
1380
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001381 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001382 size_t skip = delegatedQuery.skip.value_or(0);
1383
Ed Tanous002d39b2022-05-31 08:59:27 -07001384 // Collections don't include the static data added by SubRoute
1385 // because it has a duplicate entry for members
1386 asyncResp->res.jsonValue["@odata.type"] =
1387 "#LogEntryCollection.LogEntryCollection";
1388 asyncResp->res.jsonValue["@odata.id"] =
1389 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1390 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1391 asyncResp->res.jsonValue["Description"] =
1392 "Collection of System Event Log Entries";
1393
1394 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1395 logEntryArray = nlohmann::json::array();
1396 // Go through the log files and create a unique ID for each
1397 // entry
1398 std::vector<std::filesystem::path> redfishLogFiles;
1399 getRedfishLogFiles(redfishLogFiles);
1400 uint64_t entryCount = 0;
1401 std::string logEntry;
1402
1403 // Oldest logs are in the last file, so start there and loop
1404 // backwards
1405 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1406 it++)
1407 {
1408 std::ifstream logStream(*it);
1409 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001410 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001411 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001412 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001413
Ed Tanous002d39b2022-05-31 08:59:27 -07001414 // Reset the unique ID on the first entry
1415 bool firstEntry = true;
1416 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001417 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001418 std::string idStr;
1419 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001420 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001421 continue;
1422 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001423 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001424
Jason M. Billsde703c52022-06-23 14:19:04 -07001425 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001426 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1427 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001428 if (status == LogParseError::messageIdNotInRegistry)
1429 {
1430 continue;
1431 }
1432 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001433 {
1434 messages::internalError(asyncResp->res);
1435 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001436 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001437
Jason M. Billsde703c52022-06-23 14:19:04 -07001438 entryCount++;
1439 // Handle paging using skip (number of entries to skip from the
1440 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001441 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001442 {
1443 continue;
1444 }
1445
1446 logEntryArray.push_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001447 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001448 }
1449 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001450 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001451 {
1452 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1453 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001454 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001455 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001456 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001457}
Chicago Duan336e96c2019-07-15 14:22:08 +08001458
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001459inline void requestRoutesJournalEventLogEntry(App& app)
1460{
1461 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001462 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001463 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001464 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001465 [&app](const crow::Request& req,
1466 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001467 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001468 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001469 {
1470 return;
1471 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001472
1473 if (systemName != "system")
1474 {
1475 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1476 systemName);
1477 return;
1478 }
1479
Ed Tanous002d39b2022-05-31 08:59:27 -07001480 const std::string& targetID = param;
1481
1482 // Go through the log files and check the unique ID for each
1483 // entry to find the target entry
1484 std::vector<std::filesystem::path> redfishLogFiles;
1485 getRedfishLogFiles(redfishLogFiles);
1486 std::string logEntry;
1487
1488 // Oldest logs are in the last file, so start there and loop
1489 // backwards
1490 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1491 it++)
1492 {
1493 std::ifstream logStream(*it);
1494 if (!logStream.is_open())
1495 {
1496 continue;
1497 }
1498
1499 // Reset the unique ID on the first entry
1500 bool firstEntry = true;
1501 while (std::getline(logStream, logEntry))
1502 {
1503 std::string idStr;
1504 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001505 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001506 continue;
1507 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001508 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001509
1510 if (idStr == targetID)
1511 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001512 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001513 LogParseError status =
1514 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1515 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001516 {
1517 messages::internalError(asyncResp->res);
1518 return;
1519 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001520 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001521 return;
1522 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001523 }
1524 }
1525 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001526 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001527 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001528}
1529
1530inline void requestRoutesDBusEventLogEntryCollection(App& app)
1531{
Ed Tanous22d268c2022-05-19 09:39:07 -07001532 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001533 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001534 .methods(boost::beast::http::verb::get)(
1535 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001536 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1537 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001538 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001539 {
1540 return;
1541 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001542 if (systemName != "system")
1543 {
1544 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1545 systemName);
1546 return;
1547 }
1548
Ed Tanous002d39b2022-05-31 08:59:27 -07001549 // Collections don't include the static data added by SubRoute
1550 // because it has a duplicate entry for members
1551 asyncResp->res.jsonValue["@odata.type"] =
1552 "#LogEntryCollection.LogEntryCollection";
1553 asyncResp->res.jsonValue["@odata.id"] =
1554 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1555 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1556 asyncResp->res.jsonValue["Description"] =
1557 "Collection of System Event Log Entries";
1558
1559 // DBus implementation of EventLog/Entries
1560 // Make call to Logging Service to find all log entry objects
1561 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001562 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001563 const dbus::utility::ManagedObjectType& resp) {
1564 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001565 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001566 // TODO Handle for specific error code
1567 BMCWEB_LOG_ERROR
1568 << "getLogEntriesIfaceData resp_handler got error " << ec;
1569 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001570 return;
1571 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001572 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1573 entriesArray = nlohmann::json::array();
1574 for (const auto& objectPath : resp)
1575 {
1576 const uint32_t* id = nullptr;
1577 const uint64_t* timestamp = nullptr;
1578 const uint64_t* updateTimestamp = nullptr;
1579 const std::string* severity = nullptr;
1580 const std::string* message = nullptr;
1581 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001582 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001583 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001584 const std::string* notify = nullptr;
1585
Ed Tanous002d39b2022-05-31 08:59:27 -07001586 for (const auto& interfaceMap : objectPath.second)
1587 {
1588 if (interfaceMap.first ==
1589 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001590 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001591 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001592 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001593 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001594 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001595 id = std::get_if<uint32_t>(&propertyMap.second);
1596 }
1597 else if (propertyMap.first == "Timestamp")
1598 {
1599 timestamp =
1600 std::get_if<uint64_t>(&propertyMap.second);
1601 }
1602 else if (propertyMap.first == "UpdateTimestamp")
1603 {
1604 updateTimestamp =
1605 std::get_if<uint64_t>(&propertyMap.second);
1606 }
1607 else if (propertyMap.first == "Severity")
1608 {
1609 severity = std::get_if<std::string>(
1610 &propertyMap.second);
1611 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001612 else if (propertyMap.first == "Resolution")
1613 {
1614 resolution = std::get_if<std::string>(
1615 &propertyMap.second);
1616 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001617 else if (propertyMap.first == "Message")
1618 {
1619 message = std::get_if<std::string>(
1620 &propertyMap.second);
1621 }
1622 else if (propertyMap.first == "Resolved")
1623 {
1624 const bool* resolveptr =
1625 std::get_if<bool>(&propertyMap.second);
1626 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001627 {
1628 messages::internalError(asyncResp->res);
1629 return;
1630 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001631 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001632 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001633 else if (propertyMap.first ==
1634 "ServiceProviderNotify")
1635 {
1636 notify = std::get_if<std::string>(
1637 &propertyMap.second);
1638 if (notify == nullptr)
1639 {
1640 messages::internalError(asyncResp->res);
1641 return;
1642 }
1643 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001644 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001645 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001646 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001647 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001648 messages::internalError(asyncResp->res);
1649 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001650 }
1651 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001652 else if (interfaceMap.first ==
1653 "xyz.openbmc_project.Common.FilePath")
1654 {
1655 for (const auto& propertyMap : interfaceMap.second)
1656 {
1657 if (propertyMap.first == "Path")
1658 {
1659 filePath = std::get_if<std::string>(
1660 &propertyMap.second);
1661 }
1662 }
1663 }
1664 }
1665 // Object path without the
1666 // xyz.openbmc_project.Logging.Entry interface, ignore
1667 // and continue.
1668 if (id == nullptr || message == nullptr ||
1669 severity == nullptr || timestamp == nullptr ||
1670 updateTimestamp == nullptr)
1671 {
1672 continue;
1673 }
1674 entriesArray.push_back({});
1675 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001676 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001677 thisEntry["@odata.id"] = crow::utility::urlFromPieces(
1678 "redfish", "v1", "Systems", "system", "LogServices",
1679 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001680 thisEntry["Name"] = "System Event Log Entry";
1681 thisEntry["Id"] = std::to_string(*id);
1682 thisEntry["Message"] = *message;
1683 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001684 if ((resolution != nullptr) && (!(*resolution).empty()))
1685 {
1686 thisEntry["Resolution"] = *resolution;
1687 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001688 std::optional<bool> notifyAction =
1689 getProviderNotifyAction(*notify);
1690 if (notifyAction)
1691 {
1692 thisEntry["ServiceProviderNotified"] = *notifyAction;
1693 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001694 thisEntry["EntryType"] = "Event";
1695 thisEntry["Severity"] =
1696 translateSeverityDbusToRedfish(*severity);
1697 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001698 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001699 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001700 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001701 if (filePath != nullptr)
1702 {
1703 thisEntry["AdditionalDataURI"] =
1704 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1705 std::to_string(*id) + "/attachment";
1706 }
1707 }
1708 std::sort(
1709 entriesArray.begin(), entriesArray.end(),
1710 [](const nlohmann::json& left, const nlohmann::json& right) {
1711 return (left["Id"] <= right["Id"]);
1712 });
1713 asyncResp->res.jsonValue["Members@odata.count"] =
1714 entriesArray.size();
1715 },
1716 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1717 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001718 });
1719}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001720
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001721inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001722{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001723 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001724 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001725 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001726 .methods(boost::beast::http::verb::get)(
1727 [&app](const crow::Request& req,
1728 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001729 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001730 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001731 {
1732 return;
1733 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001734 if (systemName != "system")
1735 {
1736 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1737 systemName);
1738 return;
1739 }
1740
Ed Tanous002d39b2022-05-31 08:59:27 -07001741 std::string entryID = param;
1742 dbus::utility::escapePathForDbus(entryID);
1743
1744 // DBus implementation of EventLog/Entries
1745 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001746 sdbusplus::asio::getAllProperties(
1747 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1748 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001749 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001750 const dbus::utility::DBusPropertiesMap& resp) {
1751 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001752 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001753 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1754 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001755 return;
1756 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001757 if (ec)
1758 {
1759 BMCWEB_LOG_ERROR
1760 << "EventLogEntry (DBus) resp_handler got error " << ec;
1761 messages::internalError(asyncResp->res);
1762 return;
1763 }
1764 const uint32_t* id = nullptr;
1765 const uint64_t* timestamp = nullptr;
1766 const uint64_t* updateTimestamp = nullptr;
1767 const std::string* severity = nullptr;
1768 const std::string* message = nullptr;
1769 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001770 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001771 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001772 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001773
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001774 const bool success = sdbusplus::unpackPropertiesNoThrow(
1775 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1776 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001777 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001778 "Resolution", resolution, "Path", filePath,
1779 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001780
1781 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001782 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001783 messages::internalError(asyncResp->res);
1784 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001786
Ed Tanous002d39b2022-05-31 08:59:27 -07001787 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001788 timestamp == nullptr || updateTimestamp == nullptr ||
1789 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001790 {
1791 messages::internalError(asyncResp->res);
1792 return;
1793 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001794
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001796 "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001797 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00001798 crow::utility::urlFromPieces(
1799 "redfish", "v1", "Systems", "system", "LogServices",
1800 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001801 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1802 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1803 asyncResp->res.jsonValue["Message"] = *message;
1804 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001805 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1806 if (notifyAction)
1807 {
1808 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1809 *notifyAction;
1810 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001811 if ((resolution != nullptr) && (!(*resolution).empty()))
1812 {
1813 asyncResp->res.jsonValue["Resolution"] = *resolution;
1814 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001815 asyncResp->res.jsonValue["EntryType"] = "Event";
1816 asyncResp->res.jsonValue["Severity"] =
1817 translateSeverityDbusToRedfish(*severity);
1818 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001819 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001820 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001821 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001822 if (filePath != nullptr)
1823 {
1824 asyncResp->res.jsonValue["AdditionalDataURI"] =
1825 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1826 std::to_string(*id) + "/attachment";
1827 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001828 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001829 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001830
1831 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001832 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001833 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001834 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001835 [&app](const crow::Request& req,
1836 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001837 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001838 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001839 {
1840 return;
1841 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001842 if (systemName != "system")
1843 {
1844 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1845 systemName);
1846 return;
1847 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001848 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001849
Ed Tanous002d39b2022-05-31 08:59:27 -07001850 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1851 resolved))
1852 {
1853 return;
1854 }
1855 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001856
Ed Tanous002d39b2022-05-31 08:59:27 -07001857 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001858 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 if (ec)
1860 {
1861 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1862 messages::internalError(asyncResp->res);
1863 return;
1864 }
1865 },
1866 "xyz.openbmc_project.Logging",
1867 "/xyz/openbmc_project/logging/entry/" + entryId,
1868 "org.freedesktop.DBus.Properties", "Set",
1869 "xyz.openbmc_project.Logging.Entry", "Resolved",
1870 dbus::utility::DbusVariantType(*resolved));
1871 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001872
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001873 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001874 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001875 .privileges(redfish::privileges::deleteLogEntry)
1876
Ed Tanous002d39b2022-05-31 08:59:27 -07001877 .methods(boost::beast::http::verb::delete_)(
1878 [&app](const crow::Request& req,
1879 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001880 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001881 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001882 {
1883 return;
1884 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001885 if (systemName != "system")
1886 {
1887 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1888 systemName);
1889 return;
1890 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001891 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1892
1893 std::string entryID = param;
1894
1895 dbus::utility::escapePathForDbus(entryID);
1896
1897 // Process response from Logging service.
1898 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001899 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001900 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1901 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001902 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001904 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001905 messages::resourceNotFound(asyncResp->res, "LogEntry",
1906 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001907 return;
1908 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 // TODO Handle for specific error code
1910 BMCWEB_LOG_ERROR
1911 << "EventLogEntry (DBus) doDelete respHandler got error "
1912 << ec;
1913 asyncResp->res.result(
1914 boost::beast::http::status::internal_server_error);
1915 return;
1916 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001917
Ed Tanous002d39b2022-05-31 08:59:27 -07001918 asyncResp->res.result(boost::beast::http::status::ok);
1919 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001920
Ed Tanous002d39b2022-05-31 08:59:27 -07001921 // Make call to Logging service to request Delete Log
1922 crow::connections::systemBus->async_method_call(
1923 respHandler, "xyz.openbmc_project.Logging",
1924 "/xyz/openbmc_project/logging/entry/" + entryID,
1925 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001926 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001927}
1928
1929inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001930{
George Liu0fda0f12021-11-16 10:06:17 +08001931 BMCWEB_ROUTE(
1932 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001933 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001934 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001935 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001936 [&app](const crow::Request& req,
1937 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001938 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001939 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001940 {
1941 return;
1942 }
Matt Spinler72e21372023-04-19 12:53:33 -05001943 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07001944 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001945 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001946 {
1947 asyncResp->res.result(boost::beast::http::status::bad_request);
1948 return;
1949 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001950 if (systemName != "system")
1951 {
1952 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1953 systemName);
1954 return;
1955 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001956
Ed Tanous002d39b2022-05-31 08:59:27 -07001957 std::string entryID = param;
1958 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001959
Ed Tanous002d39b2022-05-31 08:59:27 -07001960 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001961 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 const sdbusplus::message::unix_fd& unixfd) {
1963 if (ec.value() == EBADR)
1964 {
1965 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1966 entryID);
1967 return;
1968 }
1969 if (ec)
1970 {
1971 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1972 messages::internalError(asyncResp->res);
1973 return;
1974 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001975
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 int fd = -1;
1977 fd = dup(unixfd);
1978 if (fd == -1)
1979 {
1980 messages::internalError(asyncResp->res);
1981 return;
1982 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001983
Ed Tanous002d39b2022-05-31 08:59:27 -07001984 long long int size = lseek(fd, 0, SEEK_END);
1985 if (size == -1)
1986 {
1987 messages::internalError(asyncResp->res);
1988 return;
1989 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001990
Ed Tanous002d39b2022-05-31 08:59:27 -07001991 // Arbitrary max size of 64kb
1992 constexpr int maxFileSize = 65536;
1993 if (size > maxFileSize)
1994 {
1995 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1996 << maxFileSize;
1997 messages::internalError(asyncResp->res);
1998 return;
1999 }
2000 std::vector<char> data(static_cast<size_t>(size));
2001 long long int rc = lseek(fd, 0, SEEK_SET);
2002 if (rc == -1)
2003 {
2004 messages::internalError(asyncResp->res);
2005 return;
2006 }
2007 rc = read(fd, data.data(), data.size());
2008 if ((rc == -1) || (rc != size))
2009 {
2010 messages::internalError(asyncResp->res);
2011 return;
2012 }
2013 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002014
Ed Tanous002d39b2022-05-31 08:59:27 -07002015 std::string_view strData(data.data(), data.size());
2016 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002017
Ed Tanousd9f6c622022-03-17 09:12:17 -07002018 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07002019 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07002020 asyncResp->res.addHeader(
2021 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07002022 asyncResp->res.body() = std::move(output);
2023 },
2024 "xyz.openbmc_project.Logging",
2025 "/xyz/openbmc_project/logging/entry/" + entryID,
2026 "xyz.openbmc_project.Logging.Entry", "GetEntry");
2027 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002028}
2029
Spencer Kub7028eb2021-10-26 15:27:35 +08002030constexpr const char* hostLoggerFolderPath = "/var/log/console";
2031
2032inline bool
2033 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2034 std::vector<std::filesystem::path>& hostLoggerFiles)
2035{
2036 std::error_code ec;
2037 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2038 if (ec)
2039 {
2040 BMCWEB_LOG_ERROR << ec.message();
2041 return false;
2042 }
2043 for (const std::filesystem::directory_entry& it : logPath)
2044 {
2045 std::string filename = it.path().filename();
2046 // Prefix of each log files is "log". Find the file and save the
2047 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002048 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002049 {
2050 hostLoggerFiles.emplace_back(it.path());
2051 }
2052 }
2053 // As the log files rotate, they are appended with a ".#" that is higher for
2054 // the older logs. Since we start from oldest logs, sort the name in
2055 // descending order.
2056 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2057 AlphanumLess<std::string>());
2058
2059 return true;
2060}
2061
Ed Tanous02cad962022-06-30 16:50:15 -07002062inline bool getHostLoggerEntries(
2063 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2064 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002065{
2066 GzFileReader logFile;
2067
2068 // Go though all log files and expose host logs.
2069 for (const std::filesystem::path& it : hostLoggerFiles)
2070 {
2071 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2072 {
2073 BMCWEB_LOG_ERROR << "fail to expose host logs";
2074 return false;
2075 }
2076 }
2077 // Get lastMessage from constructor by getter
2078 std::string lastMessage = logFile.getLastMessage();
2079 if (!lastMessage.empty())
2080 {
2081 logCount++;
2082 if (logCount > skip && logCount <= (skip + top))
2083 {
2084 logEntries.push_back(lastMessage);
2085 }
2086 }
2087 return true;
2088}
2089
2090inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2091 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002092 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002093{
2094 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002095 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002096 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2097 "redfish", "v1", "Systems", "system", "LogServices", "HostLogger",
2098 "Entries", logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002099 logEntryJson["Name"] = "Host Logger Entry";
2100 logEntryJson["Id"] = logEntryID;
2101 logEntryJson["Message"] = msg;
2102 logEntryJson["EntryType"] = "Oem";
2103 logEntryJson["Severity"] = "OK";
2104 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002105}
2106
2107inline void requestRoutesSystemHostLogger(App& app)
2108{
Ed Tanous22d268c2022-05-19 09:39:07 -07002109 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002110 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002111 .methods(boost::beast::http::verb::get)(
2112 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002113 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2114 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002115 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002116 {
2117 return;
2118 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002119 if (systemName != "system")
2120 {
2121 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2122 systemName);
2123 return;
2124 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002125 asyncResp->res.jsonValue["@odata.id"] =
2126 "/redfish/v1/Systems/system/LogServices/HostLogger";
2127 asyncResp->res.jsonValue["@odata.type"] =
2128 "#LogService.v1_1_0.LogService";
2129 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2130 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2131 asyncResp->res.jsonValue["Id"] = "HostLogger";
2132 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2133 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2134 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002135}
2136
2137inline void requestRoutesSystemHostLoggerCollection(App& app)
2138{
2139 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002140 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002141 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002142 .methods(boost::beast::http::verb::get)(
2143 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002144 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2145 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 query_param::QueryCapabilities capabilities = {
2147 .canDelegateTop = true,
2148 .canDelegateSkip = true,
2149 };
2150 query_param::Query delegatedQuery;
2151 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002152 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 {
2154 return;
2155 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002156 if (systemName != "system")
2157 {
2158 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2159 systemName);
2160 return;
2161 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002162 asyncResp->res.jsonValue["@odata.id"] =
2163 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2164 asyncResp->res.jsonValue["@odata.type"] =
2165 "#LogEntryCollection.LogEntryCollection";
2166 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2167 asyncResp->res.jsonValue["Description"] =
2168 "Collection of HostLogger Entries";
2169 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2170 logEntryArray = nlohmann::json::array();
2171 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002172
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 std::vector<std::filesystem::path> hostLoggerFiles;
2174 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2175 {
2176 BMCWEB_LOG_ERROR << "fail to get host log file path";
2177 return;
2178 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002179 // If we weren't provided top and skip limits, use the defaults.
2180 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002181 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002182 size_t logCount = 0;
2183 // This vector only store the entries we want to expose that
2184 // control by skip and top.
2185 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002186 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2187 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002188 {
2189 messages::internalError(asyncResp->res);
2190 return;
2191 }
2192 // If vector is empty, that means skip value larger than total
2193 // log count
2194 if (logEntries.empty())
2195 {
2196 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2197 return;
2198 }
2199 if (!logEntries.empty())
2200 {
2201 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002202 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002203 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002204 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2205 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002206 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002207 }
2208
Ed Tanous002d39b2022-05-31 08:59:27 -07002209 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002210 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002211 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002212 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2213 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002214 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002215 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002216 }
George Liu0fda0f12021-11-16 10:06:17 +08002217 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002218}
2219
2220inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2221{
2222 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002223 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002224 .privileges(redfish::privileges::getLogEntry)
2225 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002226 [&app](const crow::Request& req,
2227 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002228 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002229 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002230 {
2231 return;
2232 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002233 if (systemName != "system")
2234 {
2235 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2236 systemName);
2237 return;
2238 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002240
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002242
Patrick Williams84396af2023-05-11 11:47:45 -05002243 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2244 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002245 if (ec == std::errc::invalid_argument ||
2246 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002247 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002248 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 return;
2250 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002251
Ed Tanous002d39b2022-05-31 08:59:27 -07002252 std::vector<std::filesystem::path> hostLoggerFiles;
2253 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2254 {
2255 BMCWEB_LOG_ERROR << "fail to get host log file path";
2256 return;
2257 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002258
Ed Tanous002d39b2022-05-31 08:59:27 -07002259 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002260 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002261 std::vector<std::string> logEntries;
2262 // We can get specific entry by skip and top. For example, if we
2263 // want to get nth entry, we can set skip = n-1 and top = 1 to
2264 // get that entry
2265 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2266 logCount))
2267 {
2268 messages::internalError(asyncResp->res);
2269 return;
2270 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002271
Ed Tanous002d39b2022-05-31 08:59:27 -07002272 if (!logEntries.empty())
2273 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002274 nlohmann::json::object_t hostLogEntry;
2275 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2276 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002277 return;
2278 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002279
Ed Tanous002d39b2022-05-31 08:59:27 -07002280 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002281 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002282 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002283}
2284
Claire Weinandd72e872022-08-15 14:20:06 -07002285inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002286 crow::App& app, const crow::Request& req,
2287 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2288{
2289 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2290 {
2291 return;
2292 }
2293 // Collections don't include the static data added by SubRoute
2294 // because it has a duplicate entry for members
2295 asyncResp->res.jsonValue["@odata.type"] =
2296 "#LogServiceCollection.LogServiceCollection";
2297 asyncResp->res.jsonValue["@odata.id"] =
2298 "/redfish/v1/Managers/bmc/LogServices";
2299 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2300 asyncResp->res.jsonValue["Description"] =
2301 "Collection of LogServices for this Manager";
2302 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2303 logServiceArray = nlohmann::json::array();
2304
2305#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002306 nlohmann::json::object_t journal;
2307 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2308 logServiceArray.push_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002309#endif
2310
2311 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2312
2313#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002314 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002315 "xyz.openbmc_project.Collection.DeleteAll"};
2316 dbus::utility::getSubTreePaths(
2317 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002318 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002319 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002320 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2321 if (ec)
2322 {
2323 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002324 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002325 << ec;
2326 // Assume that getting an error simply means there are no dump
2327 // LogServices. Return without adding any error response.
2328 return;
2329 }
2330
2331 nlohmann::json& logServiceArrayLocal =
2332 asyncResp->res.jsonValue["Members"];
2333
2334 for (const std::string& path : subTreePaths)
2335 {
2336 if (path == "/xyz/openbmc_project/dump/bmc")
2337 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002338 nlohmann::json::object_t member;
2339 member["@odata.id"] =
2340 "/redfish/v1/Managers/bmc/LogServices/Dump";
2341 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002342 }
2343 else if (path == "/xyz/openbmc_project/dump/faultlog")
2344 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002345 nlohmann::json::object_t member;
2346 member["@odata.id"] =
2347 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2348 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002349 }
2350 }
2351
2352 asyncResp->res.jsonValue["Members@odata.count"] =
2353 logServiceArrayLocal.size();
George Liu7a1dbc42022-12-07 16:03:22 +08002354 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002355#endif
2356}
2357
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002358inline void requestRoutesBMCLogServiceCollection(App& app)
2359{
2360 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002361 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002362 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002363 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002364}
Ed Tanous1da66f72018-07-27 16:13:37 -07002365
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002366inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002367{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002368 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002369 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002370 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002371 [&app](const crow::Request& req,
2372 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002373 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002374 {
2375 return;
2376 }
2377 asyncResp->res.jsonValue["@odata.type"] =
2378 "#LogService.v1_1_0.LogService";
2379 asyncResp->res.jsonValue["@odata.id"] =
2380 "/redfish/v1/Managers/bmc/LogServices/Journal";
2381 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2382 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002383 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002384 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302385
Ed Tanous002d39b2022-05-31 08:59:27 -07002386 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002387 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002388 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2389 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2390 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302391
Ed Tanous002d39b2022-05-31 08:59:27 -07002392 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2393 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2394 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002395}
Jason M. Billse1f26342018-07-18 12:12:00 -07002396
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002397static int
2398 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2399 sd_journal* journal,
2400 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002401{
2402 // Get the Log Entry contents
2403 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002404
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002405 std::string message;
2406 std::string_view syslogID;
2407 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2408 if (ret < 0)
2409 {
2410 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2411 << strerror(-ret);
2412 }
2413 if (!syslogID.empty())
2414 {
2415 message += std::string(syslogID) + ": ";
2416 }
2417
Ed Tanous39e77502019-03-04 17:35:53 -08002418 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002419 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002420 if (ret < 0)
2421 {
2422 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2423 return 1;
2424 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002425 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002426
2427 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002428 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002429 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002430 if (ret < 0)
2431 {
2432 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002433 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002434
2435 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002436 std::string entryTimeStr;
2437 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002438 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002439 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002440 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002441
2442 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002443 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002444 bmcJournalLogEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2445 "redfish", "v1", "Managers", "bmc", "LogServices", "Journal", "Entries",
2446 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002447 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2448 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2449 bmcJournalLogEntryJson["Message"] = std::move(message);
2450 bmcJournalLogEntryJson["EntryType"] = "Oem";
2451 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2452 : severity <= 4 ? "Warning"
2453 : "OK";
2454 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2455 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002456 return 0;
2457}
2458
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002459inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002460{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002461 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002462 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002463 .methods(boost::beast::http::verb::get)(
2464 [&app](const crow::Request& req,
2465 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2466 query_param::QueryCapabilities capabilities = {
2467 .canDelegateTop = true,
2468 .canDelegateSkip = true,
2469 };
2470 query_param::Query delegatedQuery;
2471 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002472 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002473 {
2474 return;
2475 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002476
2477 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002478 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002479
Ed Tanous002d39b2022-05-31 08:59:27 -07002480 // Collections don't include the static data added by SubRoute
2481 // because it has a duplicate entry for members
2482 asyncResp->res.jsonValue["@odata.type"] =
2483 "#LogEntryCollection.LogEntryCollection";
2484 asyncResp->res.jsonValue["@odata.id"] =
2485 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2486 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2487 asyncResp->res.jsonValue["Description"] =
2488 "Collection of BMC Journal Entries";
2489 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2490 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002491
Ed Tanous002d39b2022-05-31 08:59:27 -07002492 // Go through the journal and use the timestamp to create a
2493 // unique ID for each entry
2494 sd_journal* journalTmp = nullptr;
2495 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2496 if (ret < 0)
2497 {
2498 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2499 messages::internalError(asyncResp->res);
2500 return;
2501 }
2502 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2503 journalTmp, sd_journal_close);
2504 journalTmp = nullptr;
2505 uint64_t entryCount = 0;
2506 // Reset the unique ID on the first entry
2507 bool firstEntry = true;
2508 SD_JOURNAL_FOREACH(journal.get())
2509 {
2510 entryCount++;
2511 // Handle paging using skip (number of entries to skip from
2512 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002513 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002514 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002515 continue;
2516 }
2517
2518 std::string idStr;
2519 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2520 {
2521 continue;
2522 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002523 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002524
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002525 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002526 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2527 bmcJournalLogEntry) != 0)
2528 {
George Liu0fda0f12021-11-16 10:06:17 +08002529 messages::internalError(asyncResp->res);
2530 return;
2531 }
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002532 logEntryArray.push_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002533 }
2534 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002535 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002536 {
2537 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2538 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002539 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002540 }
George Liu0fda0f12021-11-16 10:06:17 +08002541 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002542}
Jason M. Billse1f26342018-07-18 12:12:00 -07002543
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002544inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002545{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002546 BMCWEB_ROUTE(app,
2547 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002548 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002549 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002550 [&app](const crow::Request& req,
2551 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2552 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002553 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002554 {
2555 return;
2556 }
2557 // Convert the unique ID back to a timestamp to find the entry
2558 uint64_t ts = 0;
2559 uint64_t index = 0;
2560 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2561 {
2562 return;
2563 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002564
Ed Tanous002d39b2022-05-31 08:59:27 -07002565 sd_journal* journalTmp = nullptr;
2566 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2567 if (ret < 0)
2568 {
2569 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2570 messages::internalError(asyncResp->res);
2571 return;
2572 }
2573 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2574 journalTmp, sd_journal_close);
2575 journalTmp = nullptr;
2576 // Go to the timestamp in the log and move to the entry at the
2577 // index tracking the unique ID
2578 std::string idStr;
2579 bool firstEntry = true;
2580 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2581 if (ret < 0)
2582 {
2583 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2584 << strerror(-ret);
2585 messages::internalError(asyncResp->res);
2586 return;
2587 }
2588 for (uint64_t i = 0; i <= index; i++)
2589 {
2590 sd_journal_next(journal.get());
2591 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2592 {
2593 messages::internalError(asyncResp->res);
2594 return;
2595 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002596 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002597 }
2598 // Confirm that the entry ID matches what was requested
2599 if (idStr != entryID)
2600 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002601 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002602 return;
2603 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002604
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002605 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002606 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002607 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002608 {
2609 messages::internalError(asyncResp->res);
2610 return;
2611 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002612 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002613 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002614}
2615
Claire Weinanfdd26902022-03-01 14:18:25 -08002616inline void
2617 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2618 const std::string& dumpType)
2619{
2620 std::string dumpPath;
2621 std::string overWritePolicy;
2622 bool collectDiagnosticDataSupported = false;
2623
2624 if (dumpType == "BMC")
2625 {
2626 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2627 overWritePolicy = "WrapsWhenFull";
2628 collectDiagnosticDataSupported = true;
2629 }
2630 else if (dumpType == "FaultLog")
2631 {
2632 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2633 overWritePolicy = "Unknown";
2634 collectDiagnosticDataSupported = false;
2635 }
2636 else if (dumpType == "System")
2637 {
2638 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2639 overWritePolicy = "WrapsWhenFull";
2640 collectDiagnosticDataSupported = true;
2641 }
2642 else
2643 {
2644 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2645 << dumpType;
2646 messages::internalError(asyncResp->res);
2647 return;
2648 }
2649
2650 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2651 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2652 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2653 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2654 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2655 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2656
2657 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002658 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002659 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2660 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2661 redfishDateTimeOffset.second;
2662
2663 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002664
2665 if (collectDiagnosticDataSupported)
2666 {
2667 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2668 ["target"] =
2669 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2670 }
Claire Weinan0d946212022-07-13 19:40:19 -07002671
2672 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2673 dbus::utility::getSubTreePaths(
2674 "/xyz/openbmc_project/dump", 0, interfaces,
2675 [asyncResp, dumpType, dumpPath](
2676 const boost::system::error_code& ec,
2677 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2678 if (ec)
2679 {
2680 BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
2681 << ec;
2682 // Assume that getting an error simply means there are no dump
2683 // LogServices. Return without adding any error response.
2684 return;
2685 }
2686
2687 const std::string dbusDumpPath =
2688 "/xyz/openbmc_project/dump/" +
2689 boost::algorithm::to_lower_copy(dumpType);
2690
2691 for (const std::string& path : subTreePaths)
2692 {
2693 if (path == dbusDumpPath)
2694 {
2695 asyncResp->res
2696 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2697 dumpPath + "/Actions/LogService.ClearLog";
2698 break;
2699 }
2700 }
2701 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002702}
2703
2704inline void handleLogServicesDumpServiceGet(
2705 crow::App& app, const std::string& dumpType, const crow::Request& req,
2706 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2707{
2708 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2709 {
2710 return;
2711 }
2712 getDumpServiceInfo(asyncResp, dumpType);
2713}
2714
Ed Tanous22d268c2022-05-19 09:39:07 -07002715inline void handleLogServicesDumpServiceComputerSystemGet(
2716 crow::App& app, const crow::Request& req,
2717 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2718 const std::string& chassisId)
2719{
2720 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2721 {
2722 return;
2723 }
2724 if (chassisId != "system")
2725 {
2726 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2727 return;
2728 }
2729 getDumpServiceInfo(asyncResp, "System");
2730}
2731
Claire Weinanfdd26902022-03-01 14:18:25 -08002732inline void handleLogServicesDumpEntriesCollectionGet(
2733 crow::App& app, const std::string& dumpType, const crow::Request& req,
2734 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2735{
2736 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2737 {
2738 return;
2739 }
2740 getDumpEntryCollection(asyncResp, dumpType);
2741}
2742
Ed Tanous22d268c2022-05-19 09:39:07 -07002743inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2744 crow::App& app, const crow::Request& req,
2745 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2746 const std::string& chassisId)
2747{
2748 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2749 {
2750 return;
2751 }
2752 if (chassisId != "system")
2753 {
2754 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2755 return;
2756 }
2757 getDumpEntryCollection(asyncResp, "System");
2758}
2759
Claire Weinanfdd26902022-03-01 14:18:25 -08002760inline void handleLogServicesDumpEntryGet(
2761 crow::App& app, const std::string& dumpType, const crow::Request& req,
2762 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2763 const std::string& dumpId)
2764{
2765 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2766 {
2767 return;
2768 }
2769 getDumpEntryById(asyncResp, dumpId, dumpType);
2770}
Ed Tanous22d268c2022-05-19 09:39:07 -07002771inline void handleLogServicesDumpEntryComputerSystemGet(
2772 crow::App& app, const crow::Request& req,
2773 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2774 const std::string& chassisId, const std::string& dumpId)
2775{
2776 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2777 {
2778 return;
2779 }
2780 if (chassisId != "system")
2781 {
2782 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2783 return;
2784 }
2785 getDumpEntryById(asyncResp, dumpId, "System");
2786}
Claire Weinanfdd26902022-03-01 14:18:25 -08002787
2788inline void handleLogServicesDumpEntryDelete(
2789 crow::App& app, const std::string& dumpType, const crow::Request& req,
2790 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2791 const std::string& dumpId)
2792{
2793 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2794 {
2795 return;
2796 }
2797 deleteDumpEntry(asyncResp, dumpId, dumpType);
2798}
2799
Ed Tanous22d268c2022-05-19 09:39:07 -07002800inline void handleLogServicesDumpEntryComputerSystemDelete(
2801 crow::App& app, const crow::Request& req,
2802 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2803 const std::string& chassisId, const std::string& dumpId)
2804{
2805 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2806 {
2807 return;
2808 }
2809 if (chassisId != "system")
2810 {
2811 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2812 return;
2813 }
2814 deleteDumpEntry(asyncResp, dumpId, "System");
2815}
2816
Claire Weinanfdd26902022-03-01 14:18:25 -08002817inline void handleLogServicesDumpCollectDiagnosticDataPost(
2818 crow::App& app, const std::string& dumpType, const crow::Request& req,
2819 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2820{
2821 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2822 {
2823 return;
2824 }
2825 createDump(asyncResp, req, dumpType);
2826}
2827
Ed Tanous22d268c2022-05-19 09:39:07 -07002828inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2829 crow::App& app, const crow::Request& req,
2830 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2831 const std::string& chassisId)
2832{
2833 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2834 {
2835 return;
2836 }
2837 if (chassisId != "system")
2838 {
2839 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2840 return;
2841 }
2842 createDump(asyncResp, req, "System");
2843}
2844
Claire Weinanfdd26902022-03-01 14:18:25 -08002845inline void handleLogServicesDumpClearLogPost(
2846 crow::App& app, const std::string& dumpType, const crow::Request& req,
2847 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2848{
2849 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2850 {
2851 return;
2852 }
2853 clearDump(asyncResp, dumpType);
2854}
2855
Ed Tanous22d268c2022-05-19 09:39:07 -07002856inline void handleLogServicesDumpClearLogComputerSystemPost(
2857 crow::App& app, const crow::Request& req,
2858 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2859 const std::string& chassisId)
2860{
2861 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2862 {
2863 return;
2864 }
2865 if (chassisId != "system")
2866 {
2867 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2868 return;
2869 }
2870 clearDump(asyncResp, "System");
2871}
2872
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002873inline void requestRoutesBMCDumpService(App& app)
2874{
2875 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002876 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002877 .methods(boost::beast::http::verb::get)(std::bind_front(
2878 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002879}
2880
2881inline void requestRoutesBMCDumpEntryCollection(App& app)
2882{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002883 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002884 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002885 .methods(boost::beast::http::verb::get)(std::bind_front(
2886 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002887}
2888
2889inline void requestRoutesBMCDumpEntry(App& app)
2890{
2891 BMCWEB_ROUTE(app,
2892 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002893 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002894 .methods(boost::beast::http::verb::get)(std::bind_front(
2895 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2896
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002897 BMCWEB_ROUTE(app,
2898 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002899 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002900 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2901 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002902}
2903
2904inline void requestRoutesBMCDumpCreate(App& app)
2905{
George Liu0fda0f12021-11-16 10:06:17 +08002906 BMCWEB_ROUTE(
2907 app,
2908 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002909 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002910 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002911 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2912 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002913}
2914
2915inline void requestRoutesBMCDumpClear(App& app)
2916{
George Liu0fda0f12021-11-16 10:06:17 +08002917 BMCWEB_ROUTE(
2918 app,
2919 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002920 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002921 .methods(boost::beast::http::verb::post)(std::bind_front(
2922 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2923}
2924
2925inline void requestRoutesFaultLogDumpService(App& app)
2926{
2927 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2928 .privileges(redfish::privileges::getLogService)
2929 .methods(boost::beast::http::verb::get)(std::bind_front(
2930 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2931}
2932
2933inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2934{
2935 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2936 .privileges(redfish::privileges::getLogEntryCollection)
2937 .methods(boost::beast::http::verb::get)(
2938 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2939 std::ref(app), "FaultLog"));
2940}
2941
2942inline void requestRoutesFaultLogDumpEntry(App& app)
2943{
2944 BMCWEB_ROUTE(app,
2945 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2946 .privileges(redfish::privileges::getLogEntry)
2947 .methods(boost::beast::http::verb::get)(std::bind_front(
2948 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2949
2950 BMCWEB_ROUTE(app,
2951 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2952 .privileges(redfish::privileges::deleteLogEntry)
2953 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2954 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2955}
2956
2957inline void requestRoutesFaultLogDumpClear(App& app)
2958{
2959 BMCWEB_ROUTE(
2960 app,
2961 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2962 .privileges(redfish::privileges::postLogService)
2963 .methods(boost::beast::http::verb::post)(std::bind_front(
2964 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002965}
2966
2967inline void requestRoutesSystemDumpService(App& app)
2968{
Ed Tanous22d268c2022-05-19 09:39:07 -07002969 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002970 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002971 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002972 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002973}
2974
2975inline void requestRoutesSystemDumpEntryCollection(App& app)
2976{
Ed Tanous22d268c2022-05-19 09:39:07 -07002977 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002978 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07002979 .methods(boost::beast::http::verb::get)(std::bind_front(
2980 handleLogServicesDumpEntriesCollectionComputerSystemGet,
2981 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002982}
2983
2984inline void requestRoutesSystemDumpEntry(App& app)
2985{
2986 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002987 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002988 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002989 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002990 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002991
2992 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002993 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002994 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002995 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07002996 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002997}
2998
2999inline void requestRoutesSystemDumpCreate(App& app)
3000{
George Liu0fda0f12021-11-16 10:06:17 +08003001 BMCWEB_ROUTE(
3002 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003003 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003004 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003005 .methods(boost::beast::http::verb::post)(std::bind_front(
3006 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3007 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003008}
3009
3010inline void requestRoutesSystemDumpClear(App& app)
3011{
George Liu0fda0f12021-11-16 10:06:17 +08003012 BMCWEB_ROUTE(
3013 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003014 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003015 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003016 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003017 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003018}
3019
3020inline void requestRoutesCrashdumpService(App& app)
3021{
3022 // Note: Deviated from redfish privilege registry for GET & HEAD
3023 // method for security reasons.
3024 /**
3025 * Functions triggers appropriate requests on DBus
3026 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003027 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003028 // This is incorrect, should be:
3029 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003030 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003031 .methods(boost::beast::http::verb::get)(
3032 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003033 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3034 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003035 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003036 {
3037 return;
3038 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003039 if (systemName != "system")
3040 {
3041 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3042 systemName);
3043 return;
3044 }
3045
Ed Tanous002d39b2022-05-31 08:59:27 -07003046 // Copy over the static data to include the entries added by
3047 // SubRoute
3048 asyncResp->res.jsonValue["@odata.id"] =
3049 "/redfish/v1/Systems/system/LogServices/Crashdump";
3050 asyncResp->res.jsonValue["@odata.type"] =
3051 "#LogService.v1_2_0.LogService";
3052 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3053 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
3054 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
3055 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3056 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303057
Ed Tanous002d39b2022-05-31 08:59:27 -07003058 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003059 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003060 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3061 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3062 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303063
Ed Tanous002d39b2022-05-31 08:59:27 -07003064 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00003065 crow::utility::urlFromPieces("redfish", "v1", "Systems", "system",
3066 "LogServices", "Crashdump", "Entries");
Ed Tanous002d39b2022-05-31 08:59:27 -07003067 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3068 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3069 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3070 ["target"] =
3071 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003072 });
3073}
3074
3075void inline requestRoutesCrashdumpClear(App& app)
3076{
George Liu0fda0f12021-11-16 10:06:17 +08003077 BMCWEB_ROUTE(
3078 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003079 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003080 // This is incorrect, should be:
3081 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003082 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003083 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003084 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003085 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3086 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003087 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003088 {
3089 return;
3090 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003091 if (systemName != "system")
3092 {
3093 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3094 systemName);
3095 return;
3096 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003097 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003098 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003099 const std::string&) {
3100 if (ec)
3101 {
3102 messages::internalError(asyncResp->res);
3103 return;
3104 }
3105 messages::success(asyncResp->res);
3106 },
3107 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
3108 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003109}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003110
zhanghch058d1b46d2021-04-01 11:18:24 +08003111static void
3112 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3113 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003114{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003115 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003116 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003117 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003118 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003119 if (ec)
3120 {
3121 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3122 if (ec.value() ==
3123 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003124 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003125 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003126 }
3127 else
3128 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003129 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003130 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003131 return;
3132 }
3133
3134 std::string timestamp{};
3135 std::string filename{};
3136 std::string logfile{};
3137 parseCrashdumpParameters(params, filename, timestamp, logfile);
3138
3139 if (filename.empty() || timestamp.empty())
3140 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003141 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003142 return;
3143 }
3144
3145 std::string crashdumpURI =
3146 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3147 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003148 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003149 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003150 logEntry["@odata.id"] = crow::utility::urlFromPieces(
3151 "redfish", "v1", "Systems", "system", "LogServices", "Crashdump",
3152 "Entries", logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003153 logEntry["Name"] = "CPU Crashdump";
3154 logEntry["Id"] = logID;
3155 logEntry["EntryType"] = "Oem";
3156 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3157 logEntry["DiagnosticDataType"] = "OEM";
3158 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3159 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003160
3161 // If logEntryJson references an array of LogEntry resources
3162 // ('Members' list), then push this as a new entry, otherwise set it
3163 // directly
3164 if (logEntryJson.is_array())
3165 {
3166 logEntryJson.push_back(logEntry);
3167 asyncResp->res.jsonValue["Members@odata.count"] =
3168 logEntryJson.size();
3169 }
3170 else
3171 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003172 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003173 }
3174 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003175 sdbusplus::asio::getAllProperties(
3176 *crow::connections::systemBus, crashdumpObject,
3177 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3178 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003179}
3180
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003181inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003182{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003183 // Note: Deviated from redfish privilege registry for GET & HEAD
3184 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003185 /**
3186 * Functions triggers appropriate requests on DBus
3187 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003188 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003189 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003190 // This is incorrect, should be.
3191 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003192 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003193 .methods(boost::beast::http::verb::get)(
3194 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003195 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3196 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003197 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003198 {
3199 return;
3200 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003201 if (systemName != "system")
3202 {
3203 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3204 systemName);
3205 return;
3206 }
3207
George Liu7a1dbc42022-12-07 16:03:22 +08003208 constexpr std::array<std::string_view, 1> interfaces = {
3209 crashdumpInterface};
3210 dbus::utility::getSubTreePaths(
3211 "/", 0, interfaces,
3212 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003213 const std::vector<std::string>& resp) {
3214 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003215 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003216 if (ec.value() !=
3217 boost::system::errc::no_such_file_or_directory)
3218 {
3219 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3220 << ec.message();
3221 messages::internalError(asyncResp->res);
3222 return;
3223 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003224 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003225 asyncResp->res.jsonValue["@odata.type"] =
3226 "#LogEntryCollection.LogEntryCollection";
3227 asyncResp->res.jsonValue["@odata.id"] =
3228 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3229 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3230 asyncResp->res.jsonValue["Description"] =
3231 "Collection of Crashdump Entries";
3232 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3233 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003234
Ed Tanous002d39b2022-05-31 08:59:27 -07003235 for (const std::string& path : resp)
3236 {
3237 const sdbusplus::message::object_path objPath(path);
3238 // Get the log ID
3239 std::string logID = objPath.filename();
3240 if (logID.empty())
3241 {
3242 continue;
3243 }
3244 // Add the log entry to the array
3245 logCrashdumpEntry(asyncResp, logID,
3246 asyncResp->res.jsonValue["Members"]);
3247 }
George Liu7a1dbc42022-12-07 16:03:22 +08003248 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003249 });
3250}
Ed Tanous1da66f72018-07-27 16:13:37 -07003251
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003252inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003253{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003254 // Note: Deviated from redfish privilege registry for GET & HEAD
3255 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003256
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003257 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003258 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003259 // this is incorrect, should be
3260 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003261 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003262 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003263 [&app](const crow::Request& req,
3264 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003265 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003266 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003267 {
3268 return;
3269 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003270 if (systemName != "system")
3271 {
3272 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3273 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003274 return;
3275 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003276 const std::string& logID = param;
3277 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3278 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003279}
Ed Tanous1da66f72018-07-27 16:13:37 -07003280
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003281inline void requestRoutesCrashdumpFile(App& app)
3282{
3283 // Note: Deviated from redfish privilege registry for GET & HEAD
3284 // method for security reasons.
3285 BMCWEB_ROUTE(
3286 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003287 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003288 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003289 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003290 [](const crow::Request& req,
3291 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003292 const std::string& systemName, const std::string& logID,
3293 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003294 // Do not call getRedfishRoute here since the crashdump file is not a
3295 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003296
3297 if (systemName != "system")
3298 {
3299 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3300 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003301 return;
3302 }
3303
Ed Tanous002d39b2022-05-31 08:59:27 -07003304 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003305 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003306 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003307 const std::vector<
3308 std::pair<std::string, dbus::utility::DbusVariantType>>&
3309 resp) {
3310 if (ec)
3311 {
3312 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3313 messages::internalError(asyncResp->res);
3314 return;
3315 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003316
Ed Tanous002d39b2022-05-31 08:59:27 -07003317 std::string dbusFilename{};
3318 std::string dbusTimestamp{};
3319 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003320
Ed Tanous002d39b2022-05-31 08:59:27 -07003321 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3322 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003323
Ed Tanous002d39b2022-05-31 08:59:27 -07003324 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3325 dbusFilepath.empty())
3326 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003327 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003328 return;
3329 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003330
Ed Tanous002d39b2022-05-31 08:59:27 -07003331 // Verify the file name parameter is correct
3332 if (fileName != dbusFilename)
3333 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003334 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003335 return;
3336 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003337
Ed Tanous002d39b2022-05-31 08:59:27 -07003338 if (!std::filesystem::exists(dbusFilepath))
3339 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003340 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003341 return;
3342 }
3343 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3344 asyncResp->res.body() =
3345 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003346
Ed Tanous002d39b2022-05-31 08:59:27 -07003347 // Configure this to be a file download when accessed
3348 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003349 asyncResp->res.addHeader(
3350 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003351 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003352 sdbusplus::asio::getAllProperties(
3353 *crow::connections::systemBus, crashdumpObject,
3354 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3355 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07003356 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003357}
3358
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003359enum class OEMDiagnosticType
3360{
3361 onDemand,
3362 telemetry,
3363 invalid,
3364};
3365
Ed Tanous26ccae32023-02-16 10:28:44 -08003366inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003367{
3368 if (oemDiagStr == "OnDemand")
3369 {
3370 return OEMDiagnosticType::onDemand;
3371 }
3372 if (oemDiagStr == "Telemetry")
3373 {
3374 return OEMDiagnosticType::telemetry;
3375 }
3376
3377 return OEMDiagnosticType::invalid;
3378}
3379
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003380inline void requestRoutesCrashdumpCollect(App& app)
3381{
3382 // Note: Deviated from redfish privilege registry for GET & HEAD
3383 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003384 BMCWEB_ROUTE(
3385 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003386 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003387 // The below is incorrect; Should be ConfigureManager
3388 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003389 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003390 .methods(boost::beast::http::verb::post)(
3391 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003392 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3393 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003394 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003395 {
3396 return;
3397 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003398
3399 if (systemName != "system")
3400 {
3401 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3402 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003403 return;
3404 }
3405
Ed Tanous002d39b2022-05-31 08:59:27 -07003406 std::string diagnosticDataType;
3407 std::string oemDiagnosticDataType;
3408 if (!redfish::json_util::readJsonAction(
3409 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3410 "OEMDiagnosticDataType", oemDiagnosticDataType))
3411 {
3412 return;
3413 }
3414
3415 if (diagnosticDataType != "OEM")
3416 {
3417 BMCWEB_LOG_ERROR
3418 << "Only OEM DiagnosticDataType supported for Crashdump";
3419 messages::actionParameterValueFormatError(
3420 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3421 "CollectDiagnosticData");
3422 return;
3423 }
3424
3425 OEMDiagnosticType oemDiagType =
3426 getOEMDiagnosticType(oemDiagnosticDataType);
3427
3428 std::string iface;
3429 std::string method;
3430 std::string taskMatchStr;
3431 if (oemDiagType == OEMDiagnosticType::onDemand)
3432 {
3433 iface = crashdumpOnDemandInterface;
3434 method = "GenerateOnDemandLog";
3435 taskMatchStr = "type='signal',"
3436 "interface='org.freedesktop.DBus.Properties',"
3437 "member='PropertiesChanged',"
3438 "arg0namespace='com.intel.crashdump'";
3439 }
3440 else if (oemDiagType == OEMDiagnosticType::telemetry)
3441 {
3442 iface = crashdumpTelemetryInterface;
3443 method = "GenerateTelemetryLog";
3444 taskMatchStr = "type='signal',"
3445 "interface='org.freedesktop.DBus.Properties',"
3446 "member='PropertiesChanged',"
3447 "arg0namespace='com.intel.crashdump'";
3448 }
3449 else
3450 {
3451 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3452 << oemDiagnosticDataType;
3453 messages::actionParameterValueFormatError(
3454 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3455 "CollectDiagnosticData");
3456 return;
3457 }
3458
3459 auto collectCrashdumpCallback =
3460 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003461 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003462 const std::string&) mutable {
3463 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003464 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003465 if (ec.value() == boost::system::errc::operation_not_supported)
3466 {
3467 messages::resourceInStandby(asyncResp->res);
3468 }
3469 else if (ec.value() ==
3470 boost::system::errc::device_or_resource_busy)
3471 {
3472 messages::serviceTemporarilyUnavailable(asyncResp->res,
3473 "60");
3474 }
3475 else
3476 {
3477 messages::internalError(asyncResp->res);
3478 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003479 return;
3480 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003481 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003482 [](const boost::system::error_code& err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003483 const std::shared_ptr<task::TaskData>& taskData) {
3484 if (!err)
3485 {
3486 taskData->messages.emplace_back(messages::taskCompletedOK(
3487 std::to_string(taskData->index)));
3488 taskData->state = "Completed";
3489 }
3490 return task::completed;
3491 },
3492 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003493
Ed Tanous002d39b2022-05-31 08:59:27 -07003494 task->startTimer(std::chrono::minutes(5));
3495 task->populateResp(asyncResp->res);
3496 task->payload.emplace(std::move(payload));
3497 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003498
Ed Tanous002d39b2022-05-31 08:59:27 -07003499 crow::connections::systemBus->async_method_call(
3500 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3501 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003502 });
3503}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003504
Andrew Geisslercb92c032018-08-17 07:56:14 -07003505/**
3506 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3507 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003508inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003509{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003510 /**
3511 * Function handles POST method request.
3512 * The Clear Log actions does not require any parameter.The action deletes
3513 * all entries found in the Entries collection for this Log Service.
3514 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003515
George Liu0fda0f12021-11-16 10:06:17 +08003516 BMCWEB_ROUTE(
3517 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003518 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003519 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003520 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003521 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003522 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3523 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003524 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003525 {
3526 return;
3527 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003528 if (systemName != "system")
3529 {
3530 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3531 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003532 return;
3533 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003534 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003535
Ed Tanous002d39b2022-05-31 08:59:27 -07003536 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003537 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003538 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3539 if (ec)
3540 {
3541 // TODO Handle for specific error code
3542 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3543 asyncResp->res.result(
3544 boost::beast::http::status::internal_server_error);
3545 return;
3546 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003547
Ed Tanous002d39b2022-05-31 08:59:27 -07003548 asyncResp->res.result(boost::beast::http::status::no_content);
3549 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003550
Ed Tanous002d39b2022-05-31 08:59:27 -07003551 // Make call to Logging service to request Clear Log
3552 crow::connections::systemBus->async_method_call(
3553 respHandler, "xyz.openbmc_project.Logging",
3554 "/xyz/openbmc_project/logging",
3555 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3556 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003557}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003558
3559/****************************************************
3560 * Redfish PostCode interfaces
3561 * using DBUS interface: getPostCodesTS
3562 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003563inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003564{
Ed Tanous22d268c2022-05-19 09:39:07 -07003565 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003566 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003567 .methods(boost::beast::http::verb::get)(
3568 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003569 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3570 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003571 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003572 {
3573 return;
3574 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003575 if (systemName != "system")
3576 {
3577 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3578 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003579 return;
3580 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003581 asyncResp->res.jsonValue["@odata.id"] =
3582 "/redfish/v1/Systems/system/LogServices/PostCodes";
3583 asyncResp->res.jsonValue["@odata.type"] =
3584 "#LogService.v1_1_0.LogService";
3585 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3586 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003587 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003588 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3589 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3590 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303591
Ed Tanous002d39b2022-05-31 08:59:27 -07003592 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003593 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003594 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3595 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3596 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303597
Ed Tanous002d39b2022-05-31 08:59:27 -07003598 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3599 {"target",
3600 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003601 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003602}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003603
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003604inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003605{
George Liu0fda0f12021-11-16 10:06:17 +08003606 BMCWEB_ROUTE(
3607 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003608 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003609 // The following privilege is incorrect; It should be ConfigureManager
3610 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003611 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003612 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003613 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003614 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3615 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003616 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003617 {
3618 return;
3619 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003620 if (systemName != "system")
3621 {
3622 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3623 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003624 return;
3625 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003626 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003627
Ed Tanous002d39b2022-05-31 08:59:27 -07003628 // Make call to post-code service to request clear all
3629 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003630 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003631 if (ec)
3632 {
3633 // TODO Handle for specific error code
3634 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3635 << ec;
3636 asyncResp->res.result(
3637 boost::beast::http::status::internal_server_error);
3638 messages::internalError(asyncResp->res);
3639 return;
3640 }
3641 },
3642 "xyz.openbmc_project.State.Boot.PostCode0",
3643 "/xyz/openbmc_project/State/Boot/PostCode0",
3644 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3645 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003646}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003647
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003648/**
3649 * @brief Parse post code ID and get the current value and index value
3650 * eg: postCodeID=B1-2, currentValue=1, index=2
3651 *
3652 * @param[in] postCodeID Post Code ID
3653 * @param[out] currentValue Current value
3654 * @param[out] index Index value
3655 *
3656 * @return bool true if the parsing is successful, false the parsing fails
3657 */
3658inline static bool parsePostCode(const std::string& postCodeID,
3659 uint64_t& currentValue, uint16_t& index)
3660{
3661 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003662 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003663 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3664 {
3665 return false;
3666 }
3667
Patrick Williams84396af2023-05-11 11:47:45 -05003668 auto start = std::next(split[0].begin());
3669 auto end = split[0].end();
3670 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003671
Patrick Williams84396af2023-05-11 11:47:45 -05003672 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003673 {
3674 return false;
3675 }
3676
Patrick Williams84396af2023-05-11 11:47:45 -05003677 start = split[1].begin();
3678 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003679
Patrick Williams84396af2023-05-11 11:47:45 -05003680 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003681
Patrick Williams84396af2023-05-11 11:47:45 -05003682 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003683}
3684
3685static bool fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003686 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303687 const boost::container::flat_map<
3688 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003689 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3690 const uint64_t skip = 0, const uint64_t top = 0)
3691{
3692 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003693 const registries::Message* message =
3694 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003695
3696 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003697 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303698 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3699 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003700 {
3701 currentCodeIndex++;
3702 std::string postcodeEntryID =
3703 "B" + std::to_string(bootIndex) + "-" +
3704 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3705
3706 uint64_t usecSinceEpoch = code.first;
3707 uint64_t usTimeOffset = 0;
3708
3709 if (1 == currentCodeIndex)
3710 { // already incremented
3711 firstCodeTimeUs = code.first;
3712 }
3713 else
3714 {
3715 usTimeOffset = code.first - firstCodeTimeUs;
3716 }
3717
3718 // skip if no specific codeIndex is specified and currentCodeIndex does
3719 // not fall between top and skip
3720 if ((codeIndex == 0) &&
3721 (currentCodeIndex <= skip || currentCodeIndex > top))
3722 {
3723 continue;
3724 }
3725
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003726 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003727 // currentIndex
3728 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3729 {
3730 // This is done for simplicity. 1st entry is needed to calculate
3731 // time offset. To improve efficiency, one can get to the entry
3732 // directly (possibly with flatmap's nth method)
3733 continue;
3734 }
3735
3736 // currentCodeIndex is within top and skip or equal to specified code
3737 // index
3738
3739 // Get the Created time from the timestamp
3740 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003741 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003742
3743 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3744 std::ostringstream hexCode;
3745 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303746 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003747 std::ostringstream timeOffsetStr;
3748 // Set Fixed -Point Notation
3749 timeOffsetStr << std::fixed;
3750 // Set precision to 4 digits
3751 timeOffsetStr << std::setprecision(4);
3752 // Add double to stream
3753 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3754 std::vector<std::string> messageArgs = {
3755 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3756
3757 // Get MessageArgs template from message registry
3758 std::string msg;
3759 if (message != nullptr)
3760 {
3761 msg = message->message;
3762
3763 // fill in this post code value
3764 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003765 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003766 {
3767 std::string argStr = "%" + std::to_string(++i);
3768 size_t argPos = msg.find(argStr);
3769 if (argPos != std::string::npos)
3770 {
3771 msg.replace(argPos, argStr.length(), messageArg);
3772 }
3773 }
3774 }
3775
Tim Leed4342a92020-04-27 11:47:58 +08003776 // Get Severity template from message registry
3777 std::string severity;
3778 if (message != nullptr)
3779 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003780 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003781 }
3782
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003783 // Format entry
3784 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003785 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003786 bmcLogEntry["@odata.id"] = crow::utility::urlFromPieces(
3787 "redfish", "v1", "Systems", "system", "LogServices", "PostCodes",
3788 "Entries", postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003789 bmcLogEntry["Name"] = "POST Code Log Entry";
3790 bmcLogEntry["Id"] = postcodeEntryID;
3791 bmcLogEntry["Message"] = std::move(msg);
3792 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3793 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3794 bmcLogEntry["EntryType"] = "Event";
3795 bmcLogEntry["Severity"] = std::move(severity);
3796 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003797 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3798 {
3799 bmcLogEntry["AdditionalDataURI"] =
3800 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3801 postcodeEntryID + "/attachment";
3802 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003803
3804 // codeIndex is only specified when querying single entry, return only
3805 // that entry in this case
3806 if (codeIndex != 0)
3807 {
3808 aResp->res.jsonValue.update(bmcLogEntry);
3809 return true;
3810 }
3811
3812 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3813 logEntryArray.push_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003814 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003815
3816 // Return value is always false when querying multiple entries
3817 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003818}
3819
zhanghch058d1b46d2021-04-01 11:18:24 +08003820static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003821 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003822{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003823 uint16_t bootIndex = 0;
3824 uint64_t codeIndex = 0;
3825 if (!parsePostCode(entryId, codeIndex, bootIndex))
3826 {
3827 // Requested ID was not found
3828 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3829 return;
3830 }
3831
3832 if (bootIndex == 0 || codeIndex == 0)
3833 {
3834 // 0 is an invalid index
3835 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3836 return;
3837 }
3838
ZhikuiRena3316fc2020-01-29 14:58:08 -08003839 crow::connections::systemBus->async_method_call(
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003840 [aResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003841 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303842 const boost::container::flat_map<
3843 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3844 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003845 if (ec)
3846 {
3847 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3848 messages::internalError(aResp->res);
3849 return;
3850 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003851
Ed Tanous002d39b2022-05-31 08:59:27 -07003852 if (postcode.empty())
3853 {
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003854 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003855 return;
3856 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003857
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003858 if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
3859 {
3860 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3861 return;
3862 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003863 },
Jonathan Doman15124762021-01-07 17:54:17 -08003864 "xyz.openbmc_project.State.Boot.PostCode0",
3865 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003866 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3867 bootIndex);
3868}
3869
zhanghch058d1b46d2021-04-01 11:18:24 +08003870static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003871 const uint16_t bootIndex,
3872 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003873 const uint64_t entryCount, size_t skip,
3874 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003875{
3876 crow::connections::systemBus->async_method_call(
3877 [aResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003878 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303879 const boost::container::flat_map<
3880 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3881 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003882 if (ec)
3883 {
3884 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3885 messages::internalError(aResp->res);
3886 return;
3887 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003888
Ed Tanous002d39b2022-05-31 08:59:27 -07003889 uint64_t endCount = entryCount;
3890 if (!postcode.empty())
3891 {
3892 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003893 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003894 {
Patrick Williams89492a12023-05-10 07:51:34 -05003895 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
3896 entryCount) -
3897 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003898 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003899 std::min(static_cast<uint64_t>(top + skip), endCount) -
3900 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003901
3902 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3903 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003904 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003905 aResp->res.jsonValue["Members@odata.count"] = endCount;
3906 }
3907
3908 // continue to previous bootIndex
3909 if (bootIndex < bootCount)
3910 {
3911 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3912 bootCount, endCount, skip, top);
3913 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003914 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003915 {
3916 aResp->res.jsonValue["Members@odata.nextLink"] =
3917 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3918 std::to_string(skip + top);
3919 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003920 },
Jonathan Doman15124762021-01-07 17:54:17 -08003921 "xyz.openbmc_project.State.Boot.PostCode0",
3922 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003923 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3924 bootIndex);
3925}
3926
zhanghch058d1b46d2021-04-01 11:18:24 +08003927static void
3928 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003929 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003930{
3931 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003932 sdbusplus::asio::getProperty<uint16_t>(
3933 *crow::connections::systemBus,
3934 "xyz.openbmc_project.State.Boot.PostCode0",
3935 "/xyz/openbmc_project/State/Boot/PostCode0",
3936 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003937 [aResp, entryCount, skip, top](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003938 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003939 if (ec)
3940 {
3941 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3942 messages::internalError(aResp->res);
3943 return;
3944 }
3945 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003946 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003947}
3948
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003949inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003950{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003951 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003952 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003953 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003954 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003955 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003956 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3957 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003958 query_param::QueryCapabilities capabilities = {
3959 .canDelegateTop = true,
3960 .canDelegateSkip = true,
3961 };
3962 query_param::Query delegatedQuery;
3963 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003964 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003965 {
3966 return;
3967 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003968
3969 if (systemName != "system")
3970 {
3971 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3972 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003973 return;
3974 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003975 asyncResp->res.jsonValue["@odata.type"] =
3976 "#LogEntryCollection.LogEntryCollection";
3977 asyncResp->res.jsonValue["@odata.id"] =
3978 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3979 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3980 asyncResp->res.jsonValue["Description"] =
3981 "Collection of POST Code Log Entries";
3982 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3983 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003984 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003985 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003986 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003987 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003988}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003989
George Liu647b3cd2021-07-05 12:43:56 +08003990inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3991{
George Liu0fda0f12021-11-16 10:06:17 +08003992 BMCWEB_ROUTE(
3993 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003994 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003995 .privileges(redfish::privileges::getLogEntry)
3996 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003997 [&app](const crow::Request& req,
3998 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003999 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004000 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004001 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004002 {
4003 return;
4004 }
Matt Spinler72e21372023-04-19 12:53:33 -05004005 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004006 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004007 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004008 {
4009 asyncResp->res.result(boost::beast::http::status::bad_request);
4010 return;
4011 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004012 if (systemName != "system")
4013 {
4014 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4015 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004016 return;
4017 }
George Liu647b3cd2021-07-05 12:43:56 +08004018
Ed Tanous002d39b2022-05-31 08:59:27 -07004019 uint64_t currentValue = 0;
4020 uint16_t index = 0;
4021 if (!parsePostCode(postCodeID, currentValue, index))
4022 {
4023 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4024 return;
4025 }
George Liu647b3cd2021-07-05 12:43:56 +08004026
Ed Tanous002d39b2022-05-31 08:59:27 -07004027 crow::connections::systemBus->async_method_call(
4028 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004029 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004030 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4031 postcodes) {
4032 if (ec.value() == EBADR)
4033 {
4034 messages::resourceNotFound(asyncResp->res, "LogEntry",
4035 postCodeID);
4036 return;
4037 }
4038 if (ec)
4039 {
4040 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4041 messages::internalError(asyncResp->res);
4042 return;
4043 }
George Liu647b3cd2021-07-05 12:43:56 +08004044
Ed Tanous002d39b2022-05-31 08:59:27 -07004045 size_t value = static_cast<size_t>(currentValue) - 1;
4046 if (value == std::string::npos || postcodes.size() < currentValue)
4047 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004048 BMCWEB_LOG_WARNING << "Wrong currentValue value";
Ed Tanous002d39b2022-05-31 08:59:27 -07004049 messages::resourceNotFound(asyncResp->res, "LogEntry",
4050 postCodeID);
4051 return;
4052 }
George Liu647b3cd2021-07-05 12:43:56 +08004053
Ed Tanous002d39b2022-05-31 08:59:27 -07004054 const auto& [tID, c] = postcodes[value];
4055 if (c.empty())
4056 {
Gunnar Millsa7405d52023-02-22 13:23:23 -06004057 BMCWEB_LOG_WARNING << "No found post code data";
Ed Tanous002d39b2022-05-31 08:59:27 -07004058 messages::resourceNotFound(asyncResp->res, "LogEntry",
4059 postCodeID);
4060 return;
4061 }
4062 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4063 const char* d = reinterpret_cast<const char*>(c.data());
4064 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004065
Ed Tanousd9f6c622022-03-17 09:12:17 -07004066 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004067 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004068 asyncResp->res.addHeader(
4069 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004070 asyncResp->res.body() = crow::utility::base64encode(strData);
4071 },
4072 "xyz.openbmc_project.State.Boot.PostCode0",
4073 "/xyz/openbmc_project/State/Boot/PostCode0",
4074 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4075 });
George Liu647b3cd2021-07-05 12:43:56 +08004076}
4077
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004078inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004079{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004080 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004081 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004082 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004083 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004084 [&app](const crow::Request& req,
4085 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004086 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004087 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004088 {
4089 return;
4090 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004091 if (systemName != "system")
4092 {
4093 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4094 systemName);
4095 return;
4096 }
4097
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004098 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004099 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004100}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004101
Ed Tanous1da66f72018-07-27 16:13:37 -07004102} // namespace redfish