blob: f2a57223cf6e9914771b6e121434faaf1ff3566f [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
Gunnar Mills1214b7e2020-06-04 10:11:30 -050059constexpr char const* crashdumpObject = "com.intel.crashdump";
60constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050061constexpr char const* crashdumpInterface = "com.intel.crashdump";
62constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070063 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050064constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070065 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070066constexpr char const* crashdumpTelemetryInterface =
67 "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);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700268 auto [ptr, ec] = std::from_chars(
269 indexStr.data(), indexStr.data() + indexStr.size(), index);
270 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
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700277 auto [ptr, ec] =
278 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
279 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700280 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800281 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700282 return false;
283 }
284 return true;
285}
286
Jason M. Bills95820182019-04-22 16:25:34 -0700287static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500288 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700289{
290 static const std::filesystem::path redfishLogDir = "/var/log";
291 static const std::string redfishLogFilename = "redfish";
292
293 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500294 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700295 std::filesystem::directory_iterator(redfishLogDir))
296 {
297 // If we find a redfish log file, save the path
298 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700299 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700300 {
301 redfishLogFiles.emplace_back(redfishLogDir / filename);
302 }
303 }
304 // As the log files rotate, they are appended with a ".#" that is higher for
305 // the older logs. Since we don't expect more than 10 log files, we
306 // can just sort the list to get them in order from newest to oldest
307 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
308
309 return !redfishLogFiles.empty();
310}
311
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600312inline log_entry::OriginatorTypes
313 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
314{
315 if (originatorType ==
316 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
317 {
318 return log_entry::OriginatorTypes::Client;
319 }
320 if (originatorType ==
321 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
322 {
323 return log_entry::OriginatorTypes::Internal;
324 }
325 if (originatorType ==
326 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
327 {
328 return log_entry::OriginatorTypes::SupportingService;
329 }
330 return log_entry::OriginatorTypes::Invalid;
331}
332
Claire Weinanaefe3782022-07-15 19:17:19 -0700333inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800334 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700335 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600336 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700337 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
338{
339 for (const auto& interfaceMap : object.second)
340 {
341 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
342 {
343 for (const auto& propertyMap : interfaceMap.second)
344 {
345 if (propertyMap.first == "Status")
346 {
347 const auto* status =
348 std::get_if<std::string>(&propertyMap.second);
349 if (status == nullptr)
350 {
351 messages::internalError(asyncResp->res);
352 break;
353 }
354 dumpStatus = *status;
355 }
356 }
357 }
358 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
359 {
360 for (const auto& propertyMap : interfaceMap.second)
361 {
362 if (propertyMap.first == "Size")
363 {
364 const auto* sizePtr =
365 std::get_if<uint64_t>(&propertyMap.second);
366 if (sizePtr == nullptr)
367 {
368 messages::internalError(asyncResp->res);
369 break;
370 }
371 size = *sizePtr;
372 break;
373 }
374 }
375 }
376 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
377 {
378 for (const auto& propertyMap : interfaceMap.second)
379 {
380 if (propertyMap.first == "Elapsed")
381 {
382 const uint64_t* usecsTimeStamp =
383 std::get_if<uint64_t>(&propertyMap.second);
384 if (usecsTimeStamp == nullptr)
385 {
386 messages::internalError(asyncResp->res);
387 break;
388 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700389 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700390 break;
391 }
392 }
393 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600394 else if (interfaceMap.first ==
395 "xyz.openbmc_project.Common.OriginatedBy")
396 {
397 for (const auto& propertyMap : interfaceMap.second)
398 {
399 if (propertyMap.first == "OriginatorId")
400 {
401 const std::string* id =
402 std::get_if<std::string>(&propertyMap.second);
403 if (id == nullptr)
404 {
405 messages::internalError(asyncResp->res);
406 break;
407 }
408 originatorId = *id;
409 }
410
411 if (propertyMap.first == "OriginatorType")
412 {
413 const std::string* type =
414 std::get_if<std::string>(&propertyMap.second);
415 if (type == nullptr)
416 {
417 messages::internalError(asyncResp->res);
418 break;
419 }
420
421 originatorType = mapDbusOriginatorTypeToRedfish(*type);
422 if (originatorType == log_entry::OriginatorTypes::Invalid)
423 {
424 messages::internalError(asyncResp->res);
425 break;
426 }
427 }
428 }
429 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700430 }
431}
432
Nan Zhou21ab4042022-06-26 23:07:40 +0000433static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800434{
435 std::string entriesPath;
436
437 if (dumpType == "BMC")
438 {
439 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
440 }
441 else if (dumpType == "FaultLog")
442 {
443 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
444 }
445 else if (dumpType == "System")
446 {
447 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
448 }
449 else
450 {
451 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
452 << dumpType;
453 }
454
455 // Returns empty string on error
456 return entriesPath;
457}
458
zhanghch058d1b46d2021-04-01 11:18:24 +0800459inline void
460 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
461 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500462{
Claire Weinanfdd26902022-03-01 14:18:25 -0800463 std::string entriesPath = getDumpEntriesPath(dumpType);
464 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500465 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500466 messages::internalError(asyncResp->res);
467 return;
468 }
469
470 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800471 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800472 dumpType](const boost::system::error_code& ec,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800473 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700474 if (ec)
475 {
476 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
477 messages::internalError(asyncResp->res);
478 return;
479 }
480
Claire Weinanfdd26902022-03-01 14:18:25 -0800481 // Remove ending slash
482 std::string odataIdStr = entriesPath;
483 if (!odataIdStr.empty())
484 {
485 odataIdStr.pop_back();
486 }
487
488 asyncResp->res.jsonValue["@odata.type"] =
489 "#LogEntryCollection.LogEntryCollection";
490 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
491 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
492 asyncResp->res.jsonValue["Description"] =
493 "Collection of " + dumpType + " Dump Entries";
494
Ed Tanous002d39b2022-05-31 08:59:27 -0700495 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
496 entriesArray = nlohmann::json::array();
497 std::string dumpEntryPath =
498 "/xyz/openbmc_project/dump/" +
499 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
500
501 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
502 return AlphanumLess<std::string>()(l.first.filename(),
503 r.first.filename());
504 });
505
506 for (auto& object : resp)
507 {
508 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500509 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700510 continue;
511 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700512 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700513 uint64_t size = 0;
514 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600515 std::string originatorId;
516 log_entry::OriginatorTypes originatorType =
517 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700518 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700519
520 std::string entryID = object.first.filename();
521 if (entryID.empty())
522 {
523 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500524 }
525
Claire Weinanc6fecda2022-07-15 10:43:25 -0700526 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600527 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700528 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700529
530 if (dumpStatus !=
531 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
532 !dumpStatus.empty())
533 {
534 // Dump status is not Complete, no need to enumerate
535 continue;
536 }
537
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600538 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800539 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700540 thisEntry["Id"] = entryID;
541 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700542 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700543 thisEntry["Created"] =
544 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700545
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600546 if (!originatorId.empty())
547 {
548 thisEntry["Originator"] = originatorId;
549 thisEntry["OriginatorType"] = originatorType;
550 }
551
Ed Tanous002d39b2022-05-31 08:59:27 -0700552 if (dumpType == "BMC")
553 {
554 thisEntry["DiagnosticDataType"] = "Manager";
555 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800556 entriesPath + entryID + "/attachment";
557 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700558 }
559 else if (dumpType == "System")
560 {
561 thisEntry["DiagnosticDataType"] = "OEM";
562 thisEntry["OEMDiagnosticDataType"] = "System";
563 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800564 entriesPath + entryID + "/attachment";
565 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700566 }
567 entriesArray.push_back(std::move(thisEntry));
568 }
569 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500570 },
571 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
572 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
573}
574
zhanghch058d1b46d2021-04-01 11:18:24 +0800575inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700576 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500578{
Claire Weinanfdd26902022-03-01 14:18:25 -0800579 std::string entriesPath = getDumpEntriesPath(dumpType);
580 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500582 messages::internalError(asyncResp->res);
583 return;
584 }
585
586 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800587 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800588 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700589 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 if (ec)
591 {
592 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
593 messages::internalError(asyncResp->res);
594 return;
595 }
596
597 bool foundDumpEntry = false;
598 std::string dumpEntryPath =
599 "/xyz/openbmc_project/dump/" +
600 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
601
602 for (const auto& objectPath : resp)
603 {
604 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500605 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700606 continue;
607 }
608
609 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700610 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700611 uint64_t size = 0;
612 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600613 std::string originatorId;
614 log_entry::OriginatorTypes originatorType =
615 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700616
Claire Weinanaefe3782022-07-15 19:17:19 -0700617 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600618 timestampUs, originatorId,
619 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700620
621 if (dumpStatus !=
622 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
623 !dumpStatus.empty())
624 {
625 // Dump status is not Complete
626 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200627 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
628 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500629 return;
630 }
631
Ed Tanous002d39b2022-05-31 08:59:27 -0700632 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600633 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800634 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700635 asyncResp->res.jsonValue["Id"] = entryID;
636 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700638 asyncResp->res.jsonValue["Created"] =
639 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500640
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600641 if (!originatorId.empty())
642 {
643 asyncResp->res.jsonValue["Originator"] = originatorId;
644 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
645 }
646
Ed Tanous002d39b2022-05-31 08:59:27 -0700647 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500648 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700649 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
650 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800651 entriesPath + entryID + "/attachment";
652 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500653 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700654 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500655 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
657 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
658 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800659 entriesPath + entryID + "/attachment";
660 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500661 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700662 }
663 if (!foundDumpEntry)
664 {
665 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
666 messages::internalError(asyncResp->res);
667 return;
668 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500669 },
670 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
671 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
672}
673
zhanghch058d1b46d2021-04-01 11:18:24 +0800674inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800675 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500676 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500677{
Ed Tanous002d39b2022-05-31 08:59:27 -0700678 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800679 [asyncResp, entryID](const boost::system::error_code& ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500680 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
681 if (ec)
682 {
George Liu3de8d8b2021-03-22 17:49:39 +0800683 if (ec.value() == EBADR)
684 {
685 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
686 return;
687 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500688 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800689 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500690 messages::internalError(asyncResp->res);
691 return;
692 }
693 };
694 crow::connections::systemBus->async_method_call(
695 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500696 "/xyz/openbmc_project/dump/" +
697 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
698 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500699 "xyz.openbmc_project.Object.Delete", "Delete");
700}
701
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600702inline DumpCreationProgress
703 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500704{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600705 if (status ==
706 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
707 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
708 {
709 return DumpCreationProgress::DUMP_CREATE_FAILED;
710 }
711 if (status ==
712 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
713 {
714 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
715 }
716 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
717}
718
719inline DumpCreationProgress
720 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
721{
722 for (const auto& [key, val] : values)
723 {
724 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700725 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600726 const std::string* value = std::get_if<std::string>(&val);
727 if (value == nullptr)
728 {
729 BMCWEB_LOG_ERROR << "Status property value is null";
730 return DumpCreationProgress::DUMP_CREATE_FAILED;
731 }
732 return mapDbusStatusToDumpProgress(*value);
733 }
734 }
735 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
736}
737
738inline std::string getDumpEntryPath(const std::string& dumpPath)
739{
740 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
741 {
742 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
743 }
744 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
745 {
746 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
747 }
748 return "";
749}
750
751inline void createDumpTaskCallback(
752 task::Payload&& payload,
753 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
754 const sdbusplus::message::object_path& createdObjPath)
755{
756 const std::string dumpPath = createdObjPath.parent_path().str;
757 const std::string dumpId = createdObjPath.filename();
758
759 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
760
761 if (dumpEntryPath.empty())
762 {
763 BMCWEB_LOG_ERROR << "Invalid dump type received";
764 messages::internalError(asyncResp->res);
765 return;
766 }
767
768 crow::connections::systemBus->async_method_call(
769 [asyncResp, payload, createdObjPath,
770 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800771 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600772 const std::string& introspectXml) {
773 if (ec)
774 {
775 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
776 << ec.message();
777 messages::internalError(asyncResp->res);
778 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700779 }
780
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600781 // Check if the created dump object has implemented Progress
782 // interface to track dump completion. If yes, fetch the "Status"
783 // property of the interface, modify the task state accordingly.
784 // Else, return task completed.
785 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700786
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600787 doc.Parse(introspectXml.data(), introspectXml.size());
788 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
789 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700790 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600791 BMCWEB_LOG_ERROR << "XML document failed to parse";
792 messages::internalError(asyncResp->res);
793 return;
794 }
795 tinyxml2::XMLElement* interfaceNode =
796 pRoot->FirstChildElement("interface");
797
798 bool isProgressIntfPresent = false;
799 while (interfaceNode != nullptr)
800 {
801 const char* thisInterfaceName = interfaceNode->Attribute("name");
802 if (thisInterfaceName != nullptr)
803 {
804 if (thisInterfaceName ==
805 std::string_view("xyz.openbmc_project.Common.Progress"))
806 {
807 interfaceNode =
808 interfaceNode->NextSiblingElement("interface");
809 continue;
810 }
811 isProgressIntfPresent = true;
812 break;
813 }
814 interfaceNode = interfaceNode->NextSiblingElement("interface");
815 }
816
817 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
818 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800819 const boost::system::error_code& err, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600820 const std::shared_ptr<task::TaskData>& taskData) {
821 if (err)
822 {
823 BMCWEB_LOG_ERROR << createdObjPath.str
824 << ": Error in creating dump";
825 taskData->messages.emplace_back(messages::internalError());
826 taskData->state = "Cancelled";
827 return task::completed;
828 }
829
830 if (isProgressIntfPresent)
831 {
832 dbus::utility::DBusPropertiesMap values;
833 std::string prop;
834 msg.read(prop, values);
835
836 DumpCreationProgress dumpStatus =
837 getDumpCompletionStatus(values);
838 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
839 {
840 BMCWEB_LOG_ERROR << createdObjPath.str
841 << ": Error in creating dump";
842 taskData->state = "Cancelled";
843 return task::completed;
844 }
845
846 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
847 {
848 BMCWEB_LOG_DEBUG << createdObjPath.str
849 << ": Dump creation task is in progress";
850 return !task::completed;
851 }
852 }
853
Ed Tanous002d39b2022-05-31 08:59:27 -0700854 nlohmann::json retMessage = messages::success();
855 taskData->messages.emplace_back(retMessage);
856
857 std::string headerLoc =
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600858 "Location: " + dumpEntryPath + http_helpers::urlEncode(dumpId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700859 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
860
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600861 BMCWEB_LOG_DEBUG << createdObjPath.str
862 << ": Dump creation task completed";
Ed Tanous002d39b2022-05-31 08:59:27 -0700863 taskData->state = "Completed";
864 return task::completed;
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600865 },
866 "type='signal',interface='org.freedesktop.DBus.Properties',"
867 "member='PropertiesChanged',path='" +
868 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500869
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600870 // The task timer is set to max time limit within which the
871 // requested dump will be collected.
872 task->startTimer(std::chrono::minutes(6));
873 task->populateResp(asyncResp->res);
874 task->payload.emplace(payload);
875 },
876 "xyz.openbmc_project.Dump.Manager", createdObjPath,
877 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500878}
879
zhanghch058d1b46d2021-04-01 11:18:24 +0800880inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
881 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500882{
Claire Weinanfdd26902022-03-01 14:18:25 -0800883 std::string dumpPath = getDumpEntriesPath(dumpType);
884 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500885 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500886 messages::internalError(asyncResp->res);
887 return;
888 }
889
890 std::optional<std::string> diagnosticDataType;
891 std::optional<std::string> oemDiagnosticDataType;
892
Willy Tu15ed6782021-12-14 11:03:16 -0800893 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500894 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
895 "OEMDiagnosticDataType", oemDiagnosticDataType))
896 {
897 return;
898 }
899
900 if (dumpType == "System")
901 {
902 if (!oemDiagnosticDataType || !diagnosticDataType)
903 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800904 BMCWEB_LOG_ERROR
905 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500906 messages::actionParameterMissing(
907 asyncResp->res, "CollectDiagnosticData",
908 "DiagnosticDataType & OEMDiagnosticDataType");
909 return;
910 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700911 if ((*oemDiagnosticDataType != "System") ||
912 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500913 {
914 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700915 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500916 return;
917 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500918 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500919 }
920 else if (dumpType == "BMC")
921 {
922 if (!diagnosticDataType)
923 {
George Liu0fda0f12021-11-16 10:06:17 +0800924 BMCWEB_LOG_ERROR
925 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500926 messages::actionParameterMissing(
927 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
928 return;
929 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700930 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500931 {
932 BMCWEB_LOG_ERROR
933 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700934 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500935 return;
936 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500937 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
938 }
939 else
940 {
941 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
942 messages::internalError(asyncResp->res);
943 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500944 }
945
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600946 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
947 createDumpParamVec;
948
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600949 createDumpParamVec.emplace_back(
950 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
951 req.session->clientIp);
952 createDumpParamVec.emplace_back(
953 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
954 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
955
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500956 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800957 [asyncResp, payload(task::Payload(req)),
958 dumpPath](const boost::system::error_code& ec,
959 const sdbusplus::message_t& msg,
960 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700961 if (ec)
962 {
963 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500964 const sd_bus_error* dbusError = msg.get_error();
965 if (dbusError == nullptr)
966 {
967 messages::internalError(asyncResp->res);
968 return;
969 }
970
971 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
972 << " and error msg: " << dbusError->message;
973 if (std::string_view(
974 "xyz.openbmc_project.Common.Error.NotAllowed") ==
975 dbusError->name)
976 {
977 messages::resourceInStandby(asyncResp->res);
978 return;
979 }
980 if (std::string_view(
981 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
982 dbusError->name)
983 {
984 messages::serviceDisabled(asyncResp->res, dumpPath);
985 return;
986 }
987 if (std::string_view(
988 "xyz.openbmc_project.Common.Error.Unavailable") ==
989 dbusError->name)
990 {
991 messages::resourceInUse(asyncResp->res);
992 return;
993 }
994 // Other Dbus errors such as:
995 // xyz.openbmc_project.Common.Error.InvalidArgument &
996 // org.freedesktop.DBus.Error.InvalidArgs are all related to
997 // the dbus call that is made here in the bmcweb
998 // implementation and has nothing to do with the client's
999 // input in the request. Hence, returning internal error
1000 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001001 messages::internalError(asyncResp->res);
1002 return;
1003 }
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001004 BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
1005 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001006 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001007 "xyz.openbmc_project.Dump.Manager",
1008 "/xyz/openbmc_project/dump/" +
1009 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001010 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001011}
1012
zhanghch058d1b46d2021-04-01 11:18:24 +08001013inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1014 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001015{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001016 std::string dumpTypeLowerCopy =
1017 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001018
Claire Weinan0d946212022-07-13 19:40:19 -07001019 crow::connections::systemBus->async_method_call(
1020 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001021 if (ec)
1022 {
Claire Weinan0d946212022-07-13 19:40:19 -07001023 BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -07001024 messages::internalError(asyncResp->res);
1025 return;
1026 }
Claire Weinan0d946212022-07-13 19:40:19 -07001027 },
1028 "xyz.openbmc_project.Dump.Manager",
1029 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1030 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001031}
1032
Ed Tanousb9d36b42022-02-26 21:42:46 -08001033inline static void
1034 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1035 std::string& filename, std::string& timestamp,
1036 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001037{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001038 const std::string* filenamePtr = nullptr;
1039 const std::string* timestampPtr = nullptr;
1040 const std::string* logfilePtr = nullptr;
1041
1042 const bool success = sdbusplus::unpackPropertiesNoThrow(
1043 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1044 "Filename", filenamePtr, "Log", logfilePtr);
1045
1046 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001047 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001048 return;
1049 }
1050
1051 if (filenamePtr != nullptr)
1052 {
1053 filename = *filenamePtr;
1054 }
1055
1056 if (timestampPtr != nullptr)
1057 {
1058 timestamp = *timestampPtr;
1059 }
1060
1061 if (logfilePtr != nullptr)
1062 {
1063 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001064 }
1065}
1066
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001067inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001068{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001069 /**
1070 * Functions triggers appropriate requests on DBus
1071 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001072 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001073 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001074 .methods(boost::beast::http::verb::get)(
1075 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001076 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1077 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001078 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001079 {
1080 return;
1081 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001082 if (systemName != "system")
1083 {
1084 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1085 systemName);
1086 return;
1087 }
1088
Ed Tanous002d39b2022-05-31 08:59:27 -07001089 // Collections don't include the static data added by SubRoute
1090 // because it has a duplicate entry for members
1091 asyncResp->res.jsonValue["@odata.type"] =
1092 "#LogServiceCollection.LogServiceCollection";
1093 asyncResp->res.jsonValue["@odata.id"] =
1094 "/redfish/v1/Systems/system/LogServices";
1095 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1096 asyncResp->res.jsonValue["Description"] =
1097 "Collection of LogServices for this Computer System";
1098 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1099 logServiceArray = nlohmann::json::array();
1100 nlohmann::json::object_t eventLog;
1101 eventLog["@odata.id"] =
1102 "/redfish/v1/Systems/system/LogServices/EventLog";
1103 logServiceArray.push_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001104#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001105 nlohmann::json::object_t dumpLog;
1106 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
1107 logServiceArray.push_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001108#endif
1109
Jason M. Billsd53dd412019-02-12 17:16:22 -08001110#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001111 nlohmann::json::object_t crashdump;
1112 crashdump["@odata.id"] =
1113 "/redfish/v1/Systems/system/LogServices/Crashdump";
1114 logServiceArray.push_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001115#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001116
1117#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001118 nlohmann::json::object_t hostlogger;
1119 hostlogger["@odata.id"] =
1120 "/redfish/v1/Systems/system/LogServices/HostLogger";
1121 logServiceArray.push_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001122#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001123 asyncResp->res.jsonValue["Members@odata.count"] =
1124 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001125
George Liu7a1dbc42022-12-07 16:03:22 +08001126 constexpr std::array<std::string_view, 1> interfaces = {
1127 "xyz.openbmc_project.State.Boot.PostCode"};
1128 dbus::utility::getSubTreePaths(
1129 "/", 0, interfaces,
1130 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001131 const dbus::utility::MapperGetSubTreePathsResponse&
1132 subtreePath) {
1133 if (ec)
1134 {
1135 BMCWEB_LOG_ERROR << ec;
1136 return;
1137 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001138
Ed Tanous002d39b2022-05-31 08:59:27 -07001139 for (const auto& pathStr : subtreePath)
1140 {
1141 if (pathStr.find("PostCode") != std::string::npos)
1142 {
1143 nlohmann::json& logServiceArrayLocal =
1144 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001145 nlohmann::json::object_t member;
1146 member["@odata.id"] =
1147 "/redfish/v1/Systems/system/LogServices/PostCodes";
1148
1149 logServiceArrayLocal.push_back(std::move(member));
1150
Ed Tanous002d39b2022-05-31 08:59:27 -07001151 asyncResp->res.jsonValue["Members@odata.count"] =
1152 logServiceArrayLocal.size();
1153 return;
1154 }
1155 }
George Liu7a1dbc42022-12-07 16:03:22 +08001156 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001157 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001158}
1159
1160inline void requestRoutesEventLogService(App& app)
1161{
Ed Tanous22d268c2022-05-19 09:39:07 -07001162 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001163 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001164 .methods(boost::beast::http::verb::get)(
1165 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001166 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1167 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001168 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001169 {
1170 return;
1171 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001172 if (systemName != "system")
1173 {
1174 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1175 systemName);
1176 return;
1177 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001178 asyncResp->res.jsonValue["@odata.id"] =
1179 "/redfish/v1/Systems/system/LogServices/EventLog";
1180 asyncResp->res.jsonValue["@odata.type"] =
1181 "#LogService.v1_1_0.LogService";
1182 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1183 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1184 asyncResp->res.jsonValue["Id"] = "EventLog";
1185 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301186
Ed Tanous002d39b2022-05-31 08:59:27 -07001187 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001188 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301189
Ed Tanous002d39b2022-05-31 08:59:27 -07001190 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1191 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1192 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301193
Ed Tanous002d39b2022-05-31 08:59:27 -07001194 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1195 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1196 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001197
Ed Tanous002d39b2022-05-31 08:59:27 -07001198 {"target",
1199 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001200 });
1201}
1202
1203inline void requestRoutesJournalEventLogClear(App& app)
1204{
Jason M. Bills4978b632022-02-22 14:17:43 -08001205 BMCWEB_ROUTE(
1206 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001207 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001208 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001209 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001210 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001211 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1212 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001213 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001214 {
1215 return;
1216 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001217 if (systemName != "system")
1218 {
1219 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1220 systemName);
1221 return;
1222 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001223 // Clear the EventLog by deleting the log files
1224 std::vector<std::filesystem::path> redfishLogFiles;
1225 if (getRedfishLogFiles(redfishLogFiles))
1226 {
1227 for (const std::filesystem::path& file : redfishLogFiles)
1228 {
1229 std::error_code ec;
1230 std::filesystem::remove(file, ec);
1231 }
1232 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001233
Ed Tanous002d39b2022-05-31 08:59:27 -07001234 // Reload rsyslog so it knows to start new log files
1235 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001236 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001237 if (ec)
1238 {
1239 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1240 messages::internalError(asyncResp->res);
1241 return;
1242 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001243
Ed Tanous002d39b2022-05-31 08:59:27 -07001244 messages::success(asyncResp->res);
1245 },
1246 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1247 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1248 "replace");
1249 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001250}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001251
Jason M. Billsac992cd2022-06-24 13:31:46 -07001252enum class LogParseError
1253{
1254 success,
1255 parseFailed,
1256 messageIdNotInRegistry,
1257};
1258
1259static LogParseError
1260 fillEventLogEntryJson(const std::string& logEntryID,
1261 const std::string& logEntry,
1262 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001263{
Jason M. Bills95820182019-04-22 16:25:34 -07001264 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001265 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001266 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001267 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001268 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001269 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001270 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001271 std::string timestamp = logEntry.substr(0, space);
1272 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001273 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001274 if (entryStart == std::string::npos)
1275 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001276 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001277 }
1278 std::string_view entry(logEntry);
1279 entry.remove_prefix(entryStart);
1280 // Use split to separate the entry into its fields
1281 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001282 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001283 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001284 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001285 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001286 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001287 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001288 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001289
Jason M. Bills4851d452019-03-28 11:27:48 -07001290 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001291 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001292
Sui Chen54417b02022-03-24 14:59:52 -07001293 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001294 {
Sui Chen54417b02022-03-24 14:59:52 -07001295 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001296 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001297 }
1298
Sui Chen54417b02022-03-24 14:59:52 -07001299 std::string msg = message->message;
1300
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001301 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001302 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001303 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001304 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001305 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001306 // If the first string is empty, assume there are no MessageArgs
1307 std::size_t messageArgsSize = 0;
1308 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001309 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001310 messageArgsSize = logEntryFields.size() - 1;
1311 }
1312
Ed Tanous23a21a12020-07-25 04:45:05 +00001313 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001314
1315 // Fill the MessageArgs into the Message
1316 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001317 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001318 {
1319 std::string argStr = "%" + std::to_string(++i);
1320 size_t argPos = msg.find(argStr);
1321 if (argPos != std::string::npos)
1322 {
1323 msg.replace(argPos, argStr.length(), messageArg);
1324 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001325 }
1326 }
1327
Jason M. Bills95820182019-04-22 16:25:34 -07001328 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1329 // format which matches the Redfish format except for the fractional seconds
1330 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001331 std::size_t dot = timestamp.find_first_of('.');
1332 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001333 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001334 {
Jason M. Bills95820182019-04-22 16:25:34 -07001335 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001336 }
1337
1338 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001339 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001340 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
1341 "redfish", "v1", "Systems", "system", "LogServices", "EventLog",
1342 "Entries", logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001343 logEntryJson["Name"] = "System Event Log Entry";
1344 logEntryJson["Id"] = logEntryID;
1345 logEntryJson["Message"] = std::move(msg);
1346 logEntryJson["MessageId"] = std::move(messageID);
1347 logEntryJson["MessageArgs"] = messageArgs;
1348 logEntryJson["EntryType"] = "Event";
1349 logEntryJson["Severity"] = message->messageSeverity;
1350 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001351 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001352}
1353
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001354inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001355{
Ed Tanous22d268c2022-05-19 09:39:07 -07001356 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001357 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001358 .methods(boost::beast::http::verb::get)(
1359 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001360 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1361 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001362 query_param::QueryCapabilities capabilities = {
1363 .canDelegateTop = true,
1364 .canDelegateSkip = true,
1365 };
1366 query_param::Query delegatedQuery;
1367 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001368 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001369 {
1370 return;
1371 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001372 if (systemName != "system")
1373 {
1374 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1375 systemName);
1376 return;
1377 }
1378
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001379 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001380 size_t skip = delegatedQuery.skip.value_or(0);
1381
Ed Tanous002d39b2022-05-31 08:59:27 -07001382 // Collections don't include the static data added by SubRoute
1383 // because it has a duplicate entry for members
1384 asyncResp->res.jsonValue["@odata.type"] =
1385 "#LogEntryCollection.LogEntryCollection";
1386 asyncResp->res.jsonValue["@odata.id"] =
1387 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1388 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1389 asyncResp->res.jsonValue["Description"] =
1390 "Collection of System Event Log Entries";
1391
1392 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1393 logEntryArray = nlohmann::json::array();
1394 // Go through the log files and create a unique ID for each
1395 // entry
1396 std::vector<std::filesystem::path> redfishLogFiles;
1397 getRedfishLogFiles(redfishLogFiles);
1398 uint64_t entryCount = 0;
1399 std::string logEntry;
1400
1401 // Oldest logs are in the last file, so start there and loop
1402 // backwards
1403 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1404 it++)
1405 {
1406 std::ifstream logStream(*it);
1407 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001408 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001409 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001410 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001411
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 // Reset the unique ID on the first entry
1413 bool firstEntry = true;
1414 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001415 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001416 std::string idStr;
1417 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001418 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001419 continue;
1420 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001421 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001422
Jason M. Billsde703c52022-06-23 14:19:04 -07001423 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001424 LogParseError status =
1425 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1426 if (status == LogParseError::messageIdNotInRegistry)
1427 {
1428 continue;
1429 }
1430 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001431 {
1432 messages::internalError(asyncResp->res);
1433 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001434 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001435
Jason M. Billsde703c52022-06-23 14:19:04 -07001436 entryCount++;
1437 // Handle paging using skip (number of entries to skip from the
1438 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001439 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001440 {
1441 continue;
1442 }
1443
1444 logEntryArray.push_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001445 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001446 }
1447 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001448 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001449 {
1450 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1451 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001452 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001453 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001454 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001455}
Chicago Duan336e96c2019-07-15 14:22:08 +08001456
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001457inline void requestRoutesJournalEventLogEntry(App& app)
1458{
1459 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001460 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001461 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001462 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001463 [&app](const crow::Request& req,
1464 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001465 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001466 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001467 {
1468 return;
1469 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001470
1471 if (systemName != "system")
1472 {
1473 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1474 systemName);
1475 return;
1476 }
1477
Ed Tanous002d39b2022-05-31 08:59:27 -07001478 const std::string& targetID = param;
1479
1480 // Go through the log files and check the unique ID for each
1481 // entry to find the target entry
1482 std::vector<std::filesystem::path> redfishLogFiles;
1483 getRedfishLogFiles(redfishLogFiles);
1484 std::string logEntry;
1485
1486 // Oldest logs are in the last file, so start there and loop
1487 // backwards
1488 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1489 it++)
1490 {
1491 std::ifstream logStream(*it);
1492 if (!logStream.is_open())
1493 {
1494 continue;
1495 }
1496
1497 // Reset the unique ID on the first entry
1498 bool firstEntry = true;
1499 while (std::getline(logStream, logEntry))
1500 {
1501 std::string idStr;
1502 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001503 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001504 continue;
1505 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001506 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001507
1508 if (idStr == targetID)
1509 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001510 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001511 LogParseError status =
1512 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1513 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001514 {
1515 messages::internalError(asyncResp->res);
1516 return;
1517 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001518 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001519 return;
1520 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001521 }
1522 }
1523 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001524 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07001525 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001526}
1527
1528inline void requestRoutesDBusEventLogEntryCollection(App& app)
1529{
Ed Tanous22d268c2022-05-19 09:39:07 -07001530 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001531 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001532 .methods(boost::beast::http::verb::get)(
1533 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001534 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1535 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001536 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001537 {
1538 return;
1539 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001540 if (systemName != "system")
1541 {
1542 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1543 systemName);
1544 return;
1545 }
1546
Ed Tanous002d39b2022-05-31 08:59:27 -07001547 // Collections don't include the static data added by SubRoute
1548 // because it has a duplicate entry for members
1549 asyncResp->res.jsonValue["@odata.type"] =
1550 "#LogEntryCollection.LogEntryCollection";
1551 asyncResp->res.jsonValue["@odata.id"] =
1552 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1553 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1554 asyncResp->res.jsonValue["Description"] =
1555 "Collection of System Event Log Entries";
1556
1557 // DBus implementation of EventLog/Entries
1558 // Make call to Logging Service to find all log entry objects
1559 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001560 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001561 const dbus::utility::ManagedObjectType& resp) {
1562 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001563 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001564 // TODO Handle for specific error code
1565 BMCWEB_LOG_ERROR
1566 << "getLogEntriesIfaceData resp_handler got error " << ec;
1567 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001568 return;
1569 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001570 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1571 entriesArray = nlohmann::json::array();
1572 for (const auto& objectPath : resp)
1573 {
1574 const uint32_t* id = nullptr;
1575 const uint64_t* timestamp = nullptr;
1576 const uint64_t* updateTimestamp = nullptr;
1577 const std::string* severity = nullptr;
1578 const std::string* message = nullptr;
1579 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001580 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001581 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001582 const std::string* notify = nullptr;
1583
Ed Tanous002d39b2022-05-31 08:59:27 -07001584 for (const auto& interfaceMap : objectPath.second)
1585 {
1586 if (interfaceMap.first ==
1587 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001588 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001589 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001590 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001591 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001592 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001593 id = std::get_if<uint32_t>(&propertyMap.second);
1594 }
1595 else if (propertyMap.first == "Timestamp")
1596 {
1597 timestamp =
1598 std::get_if<uint64_t>(&propertyMap.second);
1599 }
1600 else if (propertyMap.first == "UpdateTimestamp")
1601 {
1602 updateTimestamp =
1603 std::get_if<uint64_t>(&propertyMap.second);
1604 }
1605 else if (propertyMap.first == "Severity")
1606 {
1607 severity = std::get_if<std::string>(
1608 &propertyMap.second);
1609 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001610 else if (propertyMap.first == "Resolution")
1611 {
1612 resolution = std::get_if<std::string>(
1613 &propertyMap.second);
1614 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001615 else if (propertyMap.first == "Message")
1616 {
1617 message = std::get_if<std::string>(
1618 &propertyMap.second);
1619 }
1620 else if (propertyMap.first == "Resolved")
1621 {
1622 const bool* resolveptr =
1623 std::get_if<bool>(&propertyMap.second);
1624 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001625 {
1626 messages::internalError(asyncResp->res);
1627 return;
1628 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001629 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001630 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001631 else if (propertyMap.first ==
1632 "ServiceProviderNotify")
1633 {
1634 notify = std::get_if<std::string>(
1635 &propertyMap.second);
1636 if (notify == nullptr)
1637 {
1638 messages::internalError(asyncResp->res);
1639 return;
1640 }
1641 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001642 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001643 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001644 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001645 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001646 messages::internalError(asyncResp->res);
1647 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001648 }
1649 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001650 else if (interfaceMap.first ==
1651 "xyz.openbmc_project.Common.FilePath")
1652 {
1653 for (const auto& propertyMap : interfaceMap.second)
1654 {
1655 if (propertyMap.first == "Path")
1656 {
1657 filePath = std::get_if<std::string>(
1658 &propertyMap.second);
1659 }
1660 }
1661 }
1662 }
1663 // Object path without the
1664 // xyz.openbmc_project.Logging.Entry interface, ignore
1665 // and continue.
1666 if (id == nullptr || message == nullptr ||
1667 severity == nullptr || timestamp == nullptr ||
1668 updateTimestamp == nullptr)
1669 {
1670 continue;
1671 }
1672 entriesArray.push_back({});
1673 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001674 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00001675 thisEntry["@odata.id"] = crow::utility::urlFromPieces(
1676 "redfish", "v1", "Systems", "system", "LogServices",
1677 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001678 thisEntry["Name"] = "System Event Log Entry";
1679 thisEntry["Id"] = std::to_string(*id);
1680 thisEntry["Message"] = *message;
1681 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001682 if ((resolution != nullptr) && (!(*resolution).empty()))
1683 {
1684 thisEntry["Resolution"] = *resolution;
1685 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001686 std::optional<bool> notifyAction =
1687 getProviderNotifyAction(*notify);
1688 if (notifyAction)
1689 {
1690 thisEntry["ServiceProviderNotified"] = *notifyAction;
1691 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001692 thisEntry["EntryType"] = "Event";
1693 thisEntry["Severity"] =
1694 translateSeverityDbusToRedfish(*severity);
1695 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001696 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001697 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001698 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001699 if (filePath != nullptr)
1700 {
1701 thisEntry["AdditionalDataURI"] =
1702 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1703 std::to_string(*id) + "/attachment";
1704 }
1705 }
1706 std::sort(
1707 entriesArray.begin(), entriesArray.end(),
1708 [](const nlohmann::json& left, const nlohmann::json& right) {
1709 return (left["Id"] <= right["Id"]);
1710 });
1711 asyncResp->res.jsonValue["Members@odata.count"] =
1712 entriesArray.size();
1713 },
1714 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1715 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001716 });
1717}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001718
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001719inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001720{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001721 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001722 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001723 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001724 .methods(boost::beast::http::verb::get)(
1725 [&app](const crow::Request& req,
1726 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001727 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001728 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001729 {
1730 return;
1731 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001732 if (systemName != "system")
1733 {
1734 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1735 systemName);
1736 return;
1737 }
1738
Ed Tanous002d39b2022-05-31 08:59:27 -07001739 std::string entryID = param;
1740 dbus::utility::escapePathForDbus(entryID);
1741
1742 // DBus implementation of EventLog/Entries
1743 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001744 sdbusplus::asio::getAllProperties(
1745 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1746 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001747 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001748 const dbus::utility::DBusPropertiesMap& resp) {
1749 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001750 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001751 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1752 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001753 return;
1754 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001755 if (ec)
1756 {
1757 BMCWEB_LOG_ERROR
1758 << "EventLogEntry (DBus) resp_handler got error " << ec;
1759 messages::internalError(asyncResp->res);
1760 return;
1761 }
1762 const uint32_t* id = nullptr;
1763 const uint64_t* timestamp = nullptr;
1764 const uint64_t* updateTimestamp = nullptr;
1765 const std::string* severity = nullptr;
1766 const std::string* message = nullptr;
1767 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001768 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001769 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001770 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001771
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001772 const bool success = sdbusplus::unpackPropertiesNoThrow(
1773 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1774 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001775 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001776 "Resolution", resolution, "Path", filePath,
1777 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001778
1779 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001780 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001781 messages::internalError(asyncResp->res);
1782 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001783 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001784
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001786 timestamp == nullptr || updateTimestamp == nullptr ||
1787 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001788 {
1789 messages::internalError(asyncResp->res);
1790 return;
1791 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001792
Ed Tanous002d39b2022-05-31 08:59:27 -07001793 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001794 "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00001796 crow::utility::urlFromPieces(
1797 "redfish", "v1", "Systems", "system", "LogServices",
1798 "EventLog", "Entries", std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001799 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1800 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1801 asyncResp->res.jsonValue["Message"] = *message;
1802 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001803 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1804 if (notifyAction)
1805 {
1806 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1807 *notifyAction;
1808 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001809 if ((resolution != nullptr) && (!(*resolution).empty()))
1810 {
1811 asyncResp->res.jsonValue["Resolution"] = *resolution;
1812 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 asyncResp->res.jsonValue["EntryType"] = "Event";
1814 asyncResp->res.jsonValue["Severity"] =
1815 translateSeverityDbusToRedfish(*severity);
1816 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001817 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001818 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001819 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001820 if (filePath != nullptr)
1821 {
1822 asyncResp->res.jsonValue["AdditionalDataURI"] =
1823 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1824 std::to_string(*id) + "/attachment";
1825 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001826 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001827 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001828
1829 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001830 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001831 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001832 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001833 [&app](const crow::Request& req,
1834 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001835 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001836 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001837 {
1838 return;
1839 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001840 if (systemName != "system")
1841 {
1842 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1843 systemName);
1844 return;
1845 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001846 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001847
Ed Tanous002d39b2022-05-31 08:59:27 -07001848 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1849 resolved))
1850 {
1851 return;
1852 }
1853 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001854
Ed Tanous002d39b2022-05-31 08:59:27 -07001855 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001856 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001857 if (ec)
1858 {
1859 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1860 messages::internalError(asyncResp->res);
1861 return;
1862 }
1863 },
1864 "xyz.openbmc_project.Logging",
1865 "/xyz/openbmc_project/logging/entry/" + entryId,
1866 "org.freedesktop.DBus.Properties", "Set",
1867 "xyz.openbmc_project.Logging.Entry", "Resolved",
1868 dbus::utility::DbusVariantType(*resolved));
1869 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001870
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001871 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001872 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001873 .privileges(redfish::privileges::deleteLogEntry)
1874
Ed Tanous002d39b2022-05-31 08:59:27 -07001875 .methods(boost::beast::http::verb::delete_)(
1876 [&app](const crow::Request& req,
1877 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001878 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001879 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001880 {
1881 return;
1882 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001883 if (systemName != "system")
1884 {
1885 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1886 systemName);
1887 return;
1888 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001889 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1890
1891 std::string entryID = param;
1892
1893 dbus::utility::escapePathForDbus(entryID);
1894
1895 // Process response from Logging service.
1896 auto respHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001897 [asyncResp, entryID](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001898 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1899 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001900 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001901 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001902 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 messages::resourceNotFound(asyncResp->res, "LogEntry",
1904 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001905 return;
1906 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001907 // TODO Handle for specific error code
1908 BMCWEB_LOG_ERROR
1909 << "EventLogEntry (DBus) doDelete respHandler got error "
1910 << ec;
1911 asyncResp->res.result(
1912 boost::beast::http::status::internal_server_error);
1913 return;
1914 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001915
Ed Tanous002d39b2022-05-31 08:59:27 -07001916 asyncResp->res.result(boost::beast::http::status::ok);
1917 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001918
Ed Tanous002d39b2022-05-31 08:59:27 -07001919 // Make call to Logging service to request Delete Log
1920 crow::connections::systemBus->async_method_call(
1921 respHandler, "xyz.openbmc_project.Logging",
1922 "/xyz/openbmc_project/logging/entry/" + entryID,
1923 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001924 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001925}
1926
1927inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001928{
George Liu0fda0f12021-11-16 10:06:17 +08001929 BMCWEB_ROUTE(
1930 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001931 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001932 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001933 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001934 [&app](const crow::Request& req,
1935 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001936 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001937 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001938 {
1939 return;
1940 }
Ed Tanous99351cd2022-08-07 16:42:51 -07001941 if (http_helpers::isContentTypeAllowed(
1942 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001943 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001944 {
1945 asyncResp->res.result(boost::beast::http::status::bad_request);
1946 return;
1947 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001948 if (systemName != "system")
1949 {
1950 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1951 systemName);
1952 return;
1953 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001954
Ed Tanous002d39b2022-05-31 08:59:27 -07001955 std::string entryID = param;
1956 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001957
Ed Tanous002d39b2022-05-31 08:59:27 -07001958 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001959 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001960 const sdbusplus::message::unix_fd& unixfd) {
1961 if (ec.value() == EBADR)
1962 {
1963 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1964 entryID);
1965 return;
1966 }
1967 if (ec)
1968 {
1969 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1970 messages::internalError(asyncResp->res);
1971 return;
1972 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001973
Ed Tanous002d39b2022-05-31 08:59:27 -07001974 int fd = -1;
1975 fd = dup(unixfd);
1976 if (fd == -1)
1977 {
1978 messages::internalError(asyncResp->res);
1979 return;
1980 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001981
Ed Tanous002d39b2022-05-31 08:59:27 -07001982 long long int size = lseek(fd, 0, SEEK_END);
1983 if (size == -1)
1984 {
1985 messages::internalError(asyncResp->res);
1986 return;
1987 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001988
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 // Arbitrary max size of 64kb
1990 constexpr int maxFileSize = 65536;
1991 if (size > maxFileSize)
1992 {
1993 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1994 << maxFileSize;
1995 messages::internalError(asyncResp->res);
1996 return;
1997 }
1998 std::vector<char> data(static_cast<size_t>(size));
1999 long long int rc = lseek(fd, 0, SEEK_SET);
2000 if (rc == -1)
2001 {
2002 messages::internalError(asyncResp->res);
2003 return;
2004 }
2005 rc = read(fd, data.data(), data.size());
2006 if ((rc == -1) || (rc != size))
2007 {
2008 messages::internalError(asyncResp->res);
2009 return;
2010 }
2011 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002012
Ed Tanous002d39b2022-05-31 08:59:27 -07002013 std::string_view strData(data.data(), data.size());
2014 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002015
Ed Tanousd9f6c622022-03-17 09:12:17 -07002016 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07002017 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07002018 asyncResp->res.addHeader(
2019 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07002020 asyncResp->res.body() = std::move(output);
2021 },
2022 "xyz.openbmc_project.Logging",
2023 "/xyz/openbmc_project/logging/entry/" + entryID,
2024 "xyz.openbmc_project.Logging.Entry", "GetEntry");
2025 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002026}
2027
Spencer Kub7028eb2021-10-26 15:27:35 +08002028constexpr const char* hostLoggerFolderPath = "/var/log/console";
2029
2030inline bool
2031 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2032 std::vector<std::filesystem::path>& hostLoggerFiles)
2033{
2034 std::error_code ec;
2035 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2036 if (ec)
2037 {
2038 BMCWEB_LOG_ERROR << ec.message();
2039 return false;
2040 }
2041 for (const std::filesystem::directory_entry& it : logPath)
2042 {
2043 std::string filename = it.path().filename();
2044 // Prefix of each log files is "log". Find the file and save the
2045 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002046 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002047 {
2048 hostLoggerFiles.emplace_back(it.path());
2049 }
2050 }
2051 // As the log files rotate, they are appended with a ".#" that is higher for
2052 // the older logs. Since we start from oldest logs, sort the name in
2053 // descending order.
2054 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2055 AlphanumLess<std::string>());
2056
2057 return true;
2058}
2059
Ed Tanous02cad962022-06-30 16:50:15 -07002060inline bool getHostLoggerEntries(
2061 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2062 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002063{
2064 GzFileReader logFile;
2065
2066 // Go though all log files and expose host logs.
2067 for (const std::filesystem::path& it : hostLoggerFiles)
2068 {
2069 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2070 {
2071 BMCWEB_LOG_ERROR << "fail to expose host logs";
2072 return false;
2073 }
2074 }
2075 // Get lastMessage from constructor by getter
2076 std::string lastMessage = logFile.getLastMessage();
2077 if (!lastMessage.empty())
2078 {
2079 logCount++;
2080 if (logCount > skip && logCount <= (skip + top))
2081 {
2082 logEntries.push_back(lastMessage);
2083 }
2084 }
2085 return true;
2086}
2087
2088inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2089 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002090 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002091{
2092 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002093 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00002094 logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2095 "redfish", "v1", "Systems", "system", "LogServices", "HostLogger",
2096 "Entries", logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002097 logEntryJson["Name"] = "Host Logger Entry";
2098 logEntryJson["Id"] = logEntryID;
2099 logEntryJson["Message"] = msg;
2100 logEntryJson["EntryType"] = "Oem";
2101 logEntryJson["Severity"] = "OK";
2102 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002103}
2104
2105inline void requestRoutesSystemHostLogger(App& app)
2106{
Ed Tanous22d268c2022-05-19 09:39:07 -07002107 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002108 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002109 .methods(boost::beast::http::verb::get)(
2110 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002111 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2112 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002113 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002114 {
2115 return;
2116 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002117 if (systemName != "system")
2118 {
2119 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2120 systemName);
2121 return;
2122 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002123 asyncResp->res.jsonValue["@odata.id"] =
2124 "/redfish/v1/Systems/system/LogServices/HostLogger";
2125 asyncResp->res.jsonValue["@odata.type"] =
2126 "#LogService.v1_1_0.LogService";
2127 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2128 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2129 asyncResp->res.jsonValue["Id"] = "HostLogger";
2130 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2131 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2132 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002133}
2134
2135inline void requestRoutesSystemHostLoggerCollection(App& app)
2136{
2137 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002138 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002139 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002140 .methods(boost::beast::http::verb::get)(
2141 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002142 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2143 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 query_param::QueryCapabilities capabilities = {
2145 .canDelegateTop = true,
2146 .canDelegateSkip = true,
2147 };
2148 query_param::Query delegatedQuery;
2149 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002150 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002151 {
2152 return;
2153 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002154 if (systemName != "system")
2155 {
2156 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2157 systemName);
2158 return;
2159 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002160 asyncResp->res.jsonValue["@odata.id"] =
2161 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2162 asyncResp->res.jsonValue["@odata.type"] =
2163 "#LogEntryCollection.LogEntryCollection";
2164 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2165 asyncResp->res.jsonValue["Description"] =
2166 "Collection of HostLogger Entries";
2167 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2168 logEntryArray = nlohmann::json::array();
2169 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002170
Ed Tanous002d39b2022-05-31 08:59:27 -07002171 std::vector<std::filesystem::path> hostLoggerFiles;
2172 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2173 {
2174 BMCWEB_LOG_ERROR << "fail to get host log file path";
2175 return;
2176 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002177 // If we weren't provided top and skip limits, use the defaults.
2178 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002179 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002180 size_t logCount = 0;
2181 // This vector only store the entries we want to expose that
2182 // control by skip and top.
2183 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002184 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2185 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 {
2187 messages::internalError(asyncResp->res);
2188 return;
2189 }
2190 // If vector is empty, that means skip value larger than total
2191 // log count
2192 if (logEntries.empty())
2193 {
2194 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2195 return;
2196 }
2197 if (!logEntries.empty())
2198 {
2199 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002200 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002201 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002202 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2203 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002204 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002205 }
2206
Ed Tanous002d39b2022-05-31 08:59:27 -07002207 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002208 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002209 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002210 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2211 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002212 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002213 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002214 }
George Liu0fda0f12021-11-16 10:06:17 +08002215 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002216}
2217
2218inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2219{
2220 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002221 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002222 .privileges(redfish::privileges::getLogEntry)
2223 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002224 [&app](const crow::Request& req,
2225 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002226 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002227 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002228 {
2229 return;
2230 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002231 if (systemName != "system")
2232 {
2233 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2234 systemName);
2235 return;
2236 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002237 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002238
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002240
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2242 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08002243
Ed Tanous002d39b2022-05-31 08:59:27 -07002244 auto [ptr, ec] = std::from_chars(targetID.data(), end, 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 =
3305 [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
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
3668 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3669 const char* start = split[0].data() + 1;
3670 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3671 const char* end = split[0].data() + split[0].size();
3672 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3673
3674 if (ptrIndex != end || ecIndex != std::errc())
3675 {
3676 return false;
3677 }
3678
3679 start = split[1].data();
3680
3681 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3682 end = split[1].data() + split[1].size();
3683 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3684
3685 return ptrValue == end && ecValue == std::errc();
3686}
3687
3688static bool fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003689 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303690 const boost::container::flat_map<
3691 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003692 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3693 const uint64_t skip = 0, const uint64_t top = 0)
3694{
3695 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003696 const registries::Message* message =
3697 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003698
3699 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003700 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303701 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3702 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003703 {
3704 currentCodeIndex++;
3705 std::string postcodeEntryID =
3706 "B" + std::to_string(bootIndex) + "-" +
3707 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3708
3709 uint64_t usecSinceEpoch = code.first;
3710 uint64_t usTimeOffset = 0;
3711
3712 if (1 == currentCodeIndex)
3713 { // already incremented
3714 firstCodeTimeUs = code.first;
3715 }
3716 else
3717 {
3718 usTimeOffset = code.first - firstCodeTimeUs;
3719 }
3720
3721 // skip if no specific codeIndex is specified and currentCodeIndex does
3722 // not fall between top and skip
3723 if ((codeIndex == 0) &&
3724 (currentCodeIndex <= skip || currentCodeIndex > top))
3725 {
3726 continue;
3727 }
3728
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003729 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003730 // currentIndex
3731 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3732 {
3733 // This is done for simplicity. 1st entry is needed to calculate
3734 // time offset. To improve efficiency, one can get to the entry
3735 // directly (possibly with flatmap's nth method)
3736 continue;
3737 }
3738
3739 // currentCodeIndex is within top and skip or equal to specified code
3740 // index
3741
3742 // Get the Created time from the timestamp
3743 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003744 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003745
3746 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3747 std::ostringstream hexCode;
3748 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303749 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003750 std::ostringstream timeOffsetStr;
3751 // Set Fixed -Point Notation
3752 timeOffsetStr << std::fixed;
3753 // Set precision to 4 digits
3754 timeOffsetStr << std::setprecision(4);
3755 // Add double to stream
3756 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3757 std::vector<std::string> messageArgs = {
3758 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3759
3760 // Get MessageArgs template from message registry
3761 std::string msg;
3762 if (message != nullptr)
3763 {
3764 msg = message->message;
3765
3766 // fill in this post code value
3767 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003768 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003769 {
3770 std::string argStr = "%" + std::to_string(++i);
3771 size_t argPos = msg.find(argStr);
3772 if (argPos != std::string::npos)
3773 {
3774 msg.replace(argPos, argStr.length(), messageArg);
3775 }
3776 }
3777 }
3778
Tim Leed4342a92020-04-27 11:47:58 +08003779 // Get Severity template from message registry
3780 std::string severity;
3781 if (message != nullptr)
3782 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003783 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003784 }
3785
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003786 // Format entry
3787 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003788 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Willy Tueddfc432022-09-26 16:46:38 +00003789 bmcLogEntry["@odata.id"] = crow::utility::urlFromPieces(
3790 "redfish", "v1", "Systems", "system", "LogServices", "PostCodes",
3791 "Entries", postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003792 bmcLogEntry["Name"] = "POST Code Log Entry";
3793 bmcLogEntry["Id"] = postcodeEntryID;
3794 bmcLogEntry["Message"] = std::move(msg);
3795 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3796 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3797 bmcLogEntry["EntryType"] = "Event";
3798 bmcLogEntry["Severity"] = std::move(severity);
3799 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003800 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3801 {
3802 bmcLogEntry["AdditionalDataURI"] =
3803 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3804 postcodeEntryID + "/attachment";
3805 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003806
3807 // codeIndex is only specified when querying single entry, return only
3808 // that entry in this case
3809 if (codeIndex != 0)
3810 {
3811 aResp->res.jsonValue.update(bmcLogEntry);
3812 return true;
3813 }
3814
3815 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3816 logEntryArray.push_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003817 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003818
3819 // Return value is always false when querying multiple entries
3820 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003821}
3822
zhanghch058d1b46d2021-04-01 11:18:24 +08003823static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003824 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003825{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003826 uint16_t bootIndex = 0;
3827 uint64_t codeIndex = 0;
3828 if (!parsePostCode(entryId, codeIndex, bootIndex))
3829 {
3830 // Requested ID was not found
3831 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3832 return;
3833 }
3834
3835 if (bootIndex == 0 || codeIndex == 0)
3836 {
3837 // 0 is an invalid index
3838 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3839 return;
3840 }
3841
ZhikuiRena3316fc2020-01-29 14:58:08 -08003842 crow::connections::systemBus->async_method_call(
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003843 [aResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003844 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303845 const boost::container::flat_map<
3846 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3847 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003848 if (ec)
3849 {
3850 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3851 messages::internalError(aResp->res);
3852 return;
3853 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003854
Ed Tanous002d39b2022-05-31 08:59:27 -07003855 if (postcode.empty())
3856 {
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003857 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07003858 return;
3859 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003860
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003861 if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
3862 {
3863 messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3864 return;
3865 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003866 },
Jonathan Doman15124762021-01-07 17:54:17 -08003867 "xyz.openbmc_project.State.Boot.PostCode0",
3868 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003869 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3870 bootIndex);
3871}
3872
zhanghch058d1b46d2021-04-01 11:18:24 +08003873static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003874 const uint16_t bootIndex,
3875 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003876 const uint64_t entryCount, size_t skip,
3877 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003878{
3879 crow::connections::systemBus->async_method_call(
3880 [aResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003881 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303882 const boost::container::flat_map<
3883 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3884 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003885 if (ec)
3886 {
3887 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3888 messages::internalError(aResp->res);
3889 return;
3890 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003891
Ed Tanous002d39b2022-05-31 08:59:27 -07003892 uint64_t endCount = entryCount;
3893 if (!postcode.empty())
3894 {
3895 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003896 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003897 {
Ed Tanous3648c8b2022-07-25 13:39:59 -07003898 uint64_t thisBootSkip =
3899 std::max(static_cast<uint64_t>(skip), entryCount) -
3900 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003901 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003902 std::min(static_cast<uint64_t>(top + skip), endCount) -
3903 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003904
3905 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3906 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003907 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003908 aResp->res.jsonValue["Members@odata.count"] = endCount;
3909 }
3910
3911 // continue to previous bootIndex
3912 if (bootIndex < bootCount)
3913 {
3914 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3915 bootCount, endCount, skip, top);
3916 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003917 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003918 {
3919 aResp->res.jsonValue["Members@odata.nextLink"] =
3920 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3921 std::to_string(skip + top);
3922 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003923 },
Jonathan Doman15124762021-01-07 17:54:17 -08003924 "xyz.openbmc_project.State.Boot.PostCode0",
3925 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003926 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3927 bootIndex);
3928}
3929
zhanghch058d1b46d2021-04-01 11:18:24 +08003930static void
3931 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003932 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003933{
3934 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003935 sdbusplus::asio::getProperty<uint16_t>(
3936 *crow::connections::systemBus,
3937 "xyz.openbmc_project.State.Boot.PostCode0",
3938 "/xyz/openbmc_project/State/Boot/PostCode0",
3939 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003940 [aResp, entryCount, skip, top](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003941 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003942 if (ec)
3943 {
3944 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3945 messages::internalError(aResp->res);
3946 return;
3947 }
3948 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003949 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003950}
3951
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003952inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003953{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003954 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003955 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003956 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003957 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003958 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003959 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3960 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003961 query_param::QueryCapabilities capabilities = {
3962 .canDelegateTop = true,
3963 .canDelegateSkip = true,
3964 };
3965 query_param::Query delegatedQuery;
3966 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003967 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003968 {
3969 return;
3970 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003971
3972 if (systemName != "system")
3973 {
3974 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3975 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003976 return;
3977 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003978 asyncResp->res.jsonValue["@odata.type"] =
3979 "#LogEntryCollection.LogEntryCollection";
3980 asyncResp->res.jsonValue["@odata.id"] =
3981 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3982 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3983 asyncResp->res.jsonValue["Description"] =
3984 "Collection of POST Code Log Entries";
3985 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3986 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003987 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003988 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003989 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003990 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003991}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003992
George Liu647b3cd2021-07-05 12:43:56 +08003993inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3994{
George Liu0fda0f12021-11-16 10:06:17 +08003995 BMCWEB_ROUTE(
3996 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003997 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003998 .privileges(redfish::privileges::getLogEntry)
3999 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004000 [&app](const crow::Request& req,
4001 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004002 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004003 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004004 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004005 {
4006 return;
4007 }
Ed Tanous99351cd2022-08-07 16:42:51 -07004008 if (http_helpers::isContentTypeAllowed(
4009 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004010 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004011 {
4012 asyncResp->res.result(boost::beast::http::status::bad_request);
4013 return;
4014 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004015 if (systemName != "system")
4016 {
4017 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4018 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004019 return;
4020 }
George Liu647b3cd2021-07-05 12:43:56 +08004021
Ed Tanous002d39b2022-05-31 08:59:27 -07004022 uint64_t currentValue = 0;
4023 uint16_t index = 0;
4024 if (!parsePostCode(postCodeID, currentValue, index))
4025 {
4026 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4027 return;
4028 }
George Liu647b3cd2021-07-05 12:43:56 +08004029
Ed Tanous002d39b2022-05-31 08:59:27 -07004030 crow::connections::systemBus->async_method_call(
4031 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004032 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004033 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4034 postcodes) {
4035 if (ec.value() == EBADR)
4036 {
4037 messages::resourceNotFound(asyncResp->res, "LogEntry",
4038 postCodeID);
4039 return;
4040 }
4041 if (ec)
4042 {
4043 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4044 messages::internalError(asyncResp->res);
4045 return;
4046 }
George Liu647b3cd2021-07-05 12:43:56 +08004047
Ed Tanous002d39b2022-05-31 08:59:27 -07004048 size_t value = static_cast<size_t>(currentValue) - 1;
4049 if (value == std::string::npos || postcodes.size() < currentValue)
4050 {
4051 BMCWEB_LOG_ERROR << "Wrong currentValue value";
4052 messages::resourceNotFound(asyncResp->res, "LogEntry",
4053 postCodeID);
4054 return;
4055 }
George Liu647b3cd2021-07-05 12:43:56 +08004056
Ed Tanous002d39b2022-05-31 08:59:27 -07004057 const auto& [tID, c] = postcodes[value];
4058 if (c.empty())
4059 {
4060 BMCWEB_LOG_INFO << "No found post code data";
4061 messages::resourceNotFound(asyncResp->res, "LogEntry",
4062 postCodeID);
4063 return;
4064 }
4065 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4066 const char* d = reinterpret_cast<const char*>(c.data());
4067 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004068
Ed Tanousd9f6c622022-03-17 09:12:17 -07004069 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004070 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004071 asyncResp->res.addHeader(
4072 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07004073 asyncResp->res.body() = crow::utility::base64encode(strData);
4074 },
4075 "xyz.openbmc_project.State.Boot.PostCode0",
4076 "/xyz/openbmc_project/State/Boot/PostCode0",
4077 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4078 });
George Liu647b3cd2021-07-05 12:43:56 +08004079}
4080
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004081inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004082{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004083 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004084 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004085 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004086 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004087 [&app](const crow::Request& req,
4088 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004089 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004090 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004091 {
4092 return;
4093 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004094 if (systemName != "system")
4095 {
4096 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4097 systemName);
4098 return;
4099 }
4100
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004101 getPostCodeForEntry(asyncResp, targetID);
Ed Tanous002d39b2022-05-31 08:59:27 -07004102 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004103}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004104
Ed Tanous1da66f72018-07-27 16:13:37 -07004105} // namespace redfish