blob: d4ef1f19dff8c98e4a5a23ba7a3021768eb87e5c [file] [log] [blame]
Ed Tanous1da66f72018-07-27 16:13:37 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Spencer Kub7028eb2021-10-26 15:27:35 +080018#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080019#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080020#include "human_sort.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070021#include "registries.hpp"
22#include "registries/base_message_registry.hpp"
23#include "registries/openbmc_message_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080024#include "task.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070025
Jason M. Billse1f26342018-07-18 12:12:00 -070026#include <systemd/sd-journal.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060027#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070028
John Edward Broadbent7e860f12021-04-08 15:57:16 -070029#include <app.hpp>
Ed Tanous9896eae2022-07-23 15:07:33 -070030#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070031#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060032#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070033#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070034#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070035#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080036#include <boost/system/linux_error.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080037#include <dbus_utility.hpp>
Andrew Geisslercb92c032018-08-17 07:56:14 -070038#include <error_messages.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070039#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070040#include <registries/privilege_registry.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020041#include <sdbusplus/asio/property.hpp>
42#include <sdbusplus/unpack_properties.hpp>
43#include <utils/dbus_utils.hpp>
Ed Tanous2b829372022-08-03 14:22:34 -070044#include <utils/time_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045
George Liu647b3cd2021-07-05 12:43:56 +080046#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070047#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080048#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070049#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070050#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080051#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070052
53namespace redfish
54{
55
Gunnar Mills1214b7e2020-06-04 10:11:30 -050056constexpr char const* crashdumpObject = "com.intel.crashdump";
57constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050058constexpr char const* crashdumpInterface = "com.intel.crashdump";
59constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070060 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050061constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070062 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070063constexpr char const* crashdumpTelemetryInterface =
64 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070065
Ed Tanousfffb8c12022-02-07 23:53:03 -080066namespace registries
Jason M. Bills4851d452019-03-28 11:27:48 -070067{
Ed Tanous26702d02021-11-03 15:02:33 -070068static const Message*
69 getMessageFromRegistry(const std::string& messageKey,
70 const std::span<const MessageEntry> registry)
Jason M. Bills4851d452019-03-28 11:27:48 -070071{
Ed Tanous002d39b2022-05-31 08:59:27 -070072 std::span<const MessageEntry>::iterator messageIt =
73 std::find_if(registry.begin(), registry.end(),
74 [&messageKey](const MessageEntry& messageEntry) {
75 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
Ed Tanous26702d02021-11-03 15:02:33 -070076 });
77 if (messageIt != registry.end())
Jason M. Bills4851d452019-03-28 11:27:48 -070078 {
79 return &messageIt->second;
80 }
81
82 return nullptr;
83}
84
Gunnar Mills1214b7e2020-06-04 10:11:30 -050085static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070086{
87 // Redfish MessageIds are in the form
88 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
89 // the right Message
90 std::vector<std::string> fields;
91 fields.reserve(4);
92 boost::split(fields, messageID, boost::is_any_of("."));
Ed Tanous02cad962022-06-30 16:50:15 -070093 const std::string& registryName = fields[0];
94 const std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -070095
96 // Find the right registry and check it for the MessageKey
97 if (std::string(base::header.registryPrefix) == registryName)
98 {
99 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -0700100 messageKey, std::span<const MessageEntry>(base::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -0700101 }
102 if (std::string(openbmc::header.registryPrefix) == registryName)
103 {
104 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -0700105 messageKey, std::span<const MessageEntry>(openbmc::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -0700106 }
107 return nullptr;
108}
Ed Tanousfffb8c12022-02-07 23:53:03 -0800109} // namespace registries
Jason M. Bills4851d452019-03-28 11:27:48 -0700110
James Feistf6150402019-01-08 10:36:20 -0800111namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700112
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500113inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700114{
Ed Tanousd4d25792020-09-29 15:15:03 -0700115 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
116 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
117 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
118 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700119 {
120 return "Critical";
121 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700122 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
123 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
124 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700125 {
126 return "OK";
127 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700128 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700129 {
130 return "Warning";
131 }
132 return "";
133}
134
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700135inline static int getJournalMetadata(sd_journal* journal,
136 const std::string_view& field,
137 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700138{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500139 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700140 size_t length = 0;
141 int ret = 0;
142 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800143 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
144 const void** dataVoid = reinterpret_cast<const void**>(&data);
145
146 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700147 if (ret < 0)
148 {
149 return ret;
150 }
Ed Tanous39e77502019-03-04 17:35:53 -0800151 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700152 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000153 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700154 return ret;
155}
156
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700157inline static int getJournalMetadata(sd_journal* journal,
158 const std::string_view& field,
159 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700160{
161 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800162 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700163 // Get the metadata from the requested field of the journal entry
164 ret = getJournalMetadata(journal, field, metadata);
165 if (ret < 0)
166 {
167 return ret;
168 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000169 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700170 return ret;
171}
172
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700173inline static bool getEntryTimestamp(sd_journal* journal,
174 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800175{
176 int ret = 0;
177 uint64_t timestamp = 0;
178 ret = sd_journal_get_realtime_usec(journal, &timestamp);
179 if (ret < 0)
180 {
181 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
182 << strerror(-ret);
183 return false;
184 }
Ed Tanous2b829372022-08-03 14:22:34 -0700185 entryTimestamp =
186 redfish::time_utils::getDateTimeUint(timestamp / 1000 / 1000);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500187 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800188}
Ed Tanous50b8a432022-02-03 16:29:50 -0800189
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700190inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
191 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700192{
193 int ret = 0;
194 static uint64_t prevTs = 0;
195 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700196 if (firstEntry)
197 {
198 prevTs = 0;
199 }
200
Jason M. Bills16428a12018-11-02 12:42:29 -0700201 // Get the entry timestamp
202 uint64_t curTs = 0;
203 ret = sd_journal_get_realtime_usec(journal, &curTs);
204 if (ret < 0)
205 {
206 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
207 << strerror(-ret);
208 return false;
209 }
210 // If the timestamp isn't unique, increment the index
211 if (curTs == prevTs)
212 {
213 index++;
214 }
215 else
216 {
217 // Otherwise, reset it
218 index = 0;
219 }
220 // Save the timestamp
221 prevTs = curTs;
222
223 entryID = std::to_string(curTs);
224 if (index > 0)
225 {
226 entryID += "_" + std::to_string(index);
227 }
228 return true;
229}
230
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500231static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700232 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700233{
Ed Tanous271584a2019-07-09 16:24:22 -0700234 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700235 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700236 if (firstEntry)
237 {
238 prevTs = 0;
239 }
240
Jason M. Bills95820182019-04-22 16:25:34 -0700241 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700242 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700243 std::tm timeStruct = {};
244 std::istringstream entryStream(logEntry);
245 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
246 {
247 curTs = std::mktime(&timeStruct);
248 }
249 // If the timestamp isn't unique, increment the index
250 if (curTs == prevTs)
251 {
252 index++;
253 }
254 else
255 {
256 // Otherwise, reset it
257 index = 0;
258 }
259 // Save the timestamp
260 prevTs = curTs;
261
262 entryID = std::to_string(curTs);
263 if (index > 0)
264 {
265 entryID += "_" + std::to_string(index);
266 }
267 return true;
268}
269
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700270inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800271 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
272 const std::string& entryID, uint64_t& timestamp,
273 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700274{
275 if (entryID.empty())
276 {
277 return false;
278 }
279 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800280 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700281
Ed Tanous81ce6092020-12-17 16:54:55 +0000282 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800283 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700284 {
285 // Timestamp has an index
286 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800287 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700288 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700289 auto [ptr, ec] = std::from_chars(
290 indexStr.data(), indexStr.data() + indexStr.size(), index);
291 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700292 {
Ed Tanousace85d62021-10-26 12:45:59 -0700293 messages::resourceMissingAtURI(
294 asyncResp->res, crow::utility::urlFromPieces(entryID));
Jason M. Bills16428a12018-11-02 12:42:29 -0700295 return false;
296 }
297 }
298 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700299 auto [ptr, ec] =
300 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
301 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700302 {
Ed Tanousace85d62021-10-26 12:45:59 -0700303 messages::resourceMissingAtURI(asyncResp->res,
304 crow::utility::urlFromPieces(entryID));
Jason M. Bills16428a12018-11-02 12:42:29 -0700305 return false;
306 }
307 return true;
308}
309
Jason M. Bills95820182019-04-22 16:25:34 -0700310static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500311 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700312{
313 static const std::filesystem::path redfishLogDir = "/var/log";
314 static const std::string redfishLogFilename = "redfish";
315
316 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500317 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700318 std::filesystem::directory_iterator(redfishLogDir))
319 {
320 // If we find a redfish log file, save the path
321 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700322 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700323 {
324 redfishLogFiles.emplace_back(redfishLogDir / filename);
325 }
326 }
327 // As the log files rotate, they are appended with a ".#" that is higher for
328 // the older logs. Since we don't expect more than 10 log files, we
329 // can just sort the list to get them in order from newest to oldest
330 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
331
332 return !redfishLogFiles.empty();
333}
334
Claire Weinanaefe3782022-07-15 19:17:19 -0700335inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800336 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700337 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700338 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
339{
340 for (const auto& interfaceMap : object.second)
341 {
342 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
343 {
344 for (const auto& propertyMap : interfaceMap.second)
345 {
346 if (propertyMap.first == "Status")
347 {
348 const auto* status =
349 std::get_if<std::string>(&propertyMap.second);
350 if (status == nullptr)
351 {
352 messages::internalError(asyncResp->res);
353 break;
354 }
355 dumpStatus = *status;
356 }
357 }
358 }
359 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
360 {
361 for (const auto& propertyMap : interfaceMap.second)
362 {
363 if (propertyMap.first == "Size")
364 {
365 const auto* sizePtr =
366 std::get_if<uint64_t>(&propertyMap.second);
367 if (sizePtr == nullptr)
368 {
369 messages::internalError(asyncResp->res);
370 break;
371 }
372 size = *sizePtr;
373 break;
374 }
375 }
376 }
377 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
378 {
379 for (const auto& propertyMap : interfaceMap.second)
380 {
381 if (propertyMap.first == "Elapsed")
382 {
383 const uint64_t* usecsTimeStamp =
384 std::get_if<uint64_t>(&propertyMap.second);
385 if (usecsTimeStamp == nullptr)
386 {
387 messages::internalError(asyncResp->res);
388 break;
389 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700390 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700391 break;
392 }
393 }
394 }
395 }
396}
397
Nan Zhou21ab4042022-06-26 23:07:40 +0000398static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800399{
400 std::string entriesPath;
401
402 if (dumpType == "BMC")
403 {
404 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
405 }
406 else if (dumpType == "FaultLog")
407 {
408 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
409 }
410 else if (dumpType == "System")
411 {
412 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
413 }
414 else
415 {
416 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
417 << dumpType;
418 }
419
420 // Returns empty string on error
421 return entriesPath;
422}
423
zhanghch058d1b46d2021-04-01 11:18:24 +0800424inline void
425 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
426 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500427{
Claire Weinanfdd26902022-03-01 14:18:25 -0800428 std::string entriesPath = getDumpEntriesPath(dumpType);
429 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500430 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500431 messages::internalError(asyncResp->res);
432 return;
433 }
434
435 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800436 [asyncResp, entriesPath,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800437 dumpType](const boost::system::error_code ec,
438 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700439 if (ec)
440 {
441 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
442 messages::internalError(asyncResp->res);
443 return;
444 }
445
Claire Weinanfdd26902022-03-01 14:18:25 -0800446 // Remove ending slash
447 std::string odataIdStr = entriesPath;
448 if (!odataIdStr.empty())
449 {
450 odataIdStr.pop_back();
451 }
452
453 asyncResp->res.jsonValue["@odata.type"] =
454 "#LogEntryCollection.LogEntryCollection";
455 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
456 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
457 asyncResp->res.jsonValue["Description"] =
458 "Collection of " + dumpType + " Dump Entries";
459
Ed Tanous002d39b2022-05-31 08:59:27 -0700460 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
461 entriesArray = nlohmann::json::array();
462 std::string dumpEntryPath =
463 "/xyz/openbmc_project/dump/" +
464 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
465
466 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
467 return AlphanumLess<std::string>()(l.first.filename(),
468 r.first.filename());
469 });
470
471 for (auto& object : resp)
472 {
473 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500474 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700475 continue;
476 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700477 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700478 uint64_t size = 0;
479 std::string dumpStatus;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700480 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700481
482 std::string entryID = object.first.filename();
483 if (entryID.empty())
484 {
485 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500486 }
487
Claire Weinanc6fecda2022-07-15 10:43:25 -0700488 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Claire Weinanaefe3782022-07-15 19:17:19 -0700489 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700490
491 if (dumpStatus !=
492 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
493 !dumpStatus.empty())
494 {
495 // Dump status is not Complete, no need to enumerate
496 continue;
497 }
498
Vijay Lobo9c11a172021-10-07 16:53:16 -0500499 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800500 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700501 thisEntry["Id"] = entryID;
502 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700503 thisEntry["Name"] = dumpType + " Dump Entry";
504
Ed Tanous002d39b2022-05-31 08:59:27 -0700505 if (dumpType == "BMC")
506 {
Claire Weinanc6fecda2022-07-15 10:43:25 -0700507 thisEntry["Created"] = redfish::time_utils::getDateTimeUint(
508 timestampUs / 1000 / 1000);
Ed Tanous002d39b2022-05-31 08:59:27 -0700509 thisEntry["DiagnosticDataType"] = "Manager";
510 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800511 entriesPath + entryID + "/attachment";
512 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700513 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700514 else if (dumpType == "FaultLog")
515 {
516 thisEntry["Created"] =
517 redfish::time_utils::getDateTimeUintUs(timestampUs);
518 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700519 else if (dumpType == "System")
520 {
Claire Weinanc6fecda2022-07-15 10:43:25 -0700521 thisEntry["Created"] = redfish::time_utils::getDateTimeUint(
522 timestampUs / 1000 / 1000);
Ed Tanous002d39b2022-05-31 08:59:27 -0700523 thisEntry["DiagnosticDataType"] = "OEM";
524 thisEntry["OEMDiagnosticDataType"] = "System";
525 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800526 entriesPath + entryID + "/attachment";
527 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700528 }
529 entriesArray.push_back(std::move(thisEntry));
530 }
531 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500532 },
533 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
534 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
535}
536
zhanghch058d1b46d2021-04-01 11:18:24 +0800537inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700538 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800539 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500540{
Claire Weinanfdd26902022-03-01 14:18:25 -0800541 std::string entriesPath = getDumpEntriesPath(dumpType);
542 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500543 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500544 messages::internalError(asyncResp->res);
545 return;
546 }
547
548 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800549 [asyncResp, entryID, dumpType,
550 entriesPath](const boost::system::error_code ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700551 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700552 if (ec)
553 {
554 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
555 messages::internalError(asyncResp->res);
556 return;
557 }
558
559 bool foundDumpEntry = false;
560 std::string dumpEntryPath =
561 "/xyz/openbmc_project/dump/" +
562 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
563
564 for (const auto& objectPath : resp)
565 {
566 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500567 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 continue;
569 }
570
571 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700572 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700573 uint64_t size = 0;
574 std::string dumpStatus;
575
Claire Weinanaefe3782022-07-15 19:17:19 -0700576 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700577 timestampUs, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700578
579 if (dumpStatus !=
580 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
581 !dumpStatus.empty())
582 {
583 // Dump status is not Complete
584 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200585 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
586 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500587 return;
588 }
589
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -0500591 "#LogEntry.v1_9_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800592 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700593 asyncResp->res.jsonValue["Id"] = entryID;
594 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500596
Ed Tanous002d39b2022-05-31 08:59:27 -0700597 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500598 {
Claire Weinanc6fecda2022-07-15 10:43:25 -0700599 asyncResp->res.jsonValue["Created"] =
600 redfish::time_utils::getDateTimeUint(timestampUs / 1000 /
601 1000);
Ed Tanous002d39b2022-05-31 08:59:27 -0700602 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
603 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800604 entriesPath + entryID + "/attachment";
605 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500606 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700607 else if (dumpType == "FaultLog")
608 {
609 asyncResp->res.jsonValue["Created"] =
610 redfish::time_utils::getDateTimeUintUs(timestampUs);
611 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500613 {
Claire Weinanc6fecda2022-07-15 10:43:25 -0700614 asyncResp->res.jsonValue["Created"] =
615 redfish::time_utils::getDateTimeUint(timestampUs / 1000 /
616 1000);
Ed Tanous002d39b2022-05-31 08:59:27 -0700617 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
618 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
619 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800620 entriesPath + entryID + "/attachment";
621 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500622 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700623 }
624 if (!foundDumpEntry)
625 {
626 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
627 messages::internalError(asyncResp->res);
628 return;
629 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500630 },
631 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
632 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
633}
634
zhanghch058d1b46d2021-04-01 11:18:24 +0800635inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800636 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500637 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500638{
Ed Tanous002d39b2022-05-31 08:59:27 -0700639 auto respHandler =
640 [asyncResp, entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500641 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
642 if (ec)
643 {
George Liu3de8d8b2021-03-22 17:49:39 +0800644 if (ec.value() == EBADR)
645 {
646 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
647 return;
648 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500649 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800650 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500651 messages::internalError(asyncResp->res);
652 return;
653 }
654 };
655 crow::connections::systemBus->async_method_call(
656 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500657 "/xyz/openbmc_project/dump/" +
658 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
659 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500660 "xyz.openbmc_project.Object.Delete", "Delete");
661}
662
zhanghch058d1b46d2021-04-01 11:18:24 +0800663inline void
Ed Tanous98be3e32021-09-16 15:05:36 -0700664 createDumpTaskCallback(task::Payload&& payload,
zhanghch058d1b46d2021-04-01 11:18:24 +0800665 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
666 const uint32_t& dumpId, const std::string& dumpPath,
667 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500668{
669 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -0500670 [dumpId, dumpPath,
671 dumpType](boost::system::error_code err, sdbusplus::message_t& m,
672 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700673 if (err)
674 {
675 BMCWEB_LOG_ERROR << "Error in creating a dump";
676 taskData->state = "Cancelled";
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500677 return task::completed;
Ed Tanous002d39b2022-05-31 08:59:27 -0700678 }
679
680 dbus::utility::DBusInteracesMap interfacesList;
681
682 sdbusplus::message::object_path objPath;
683
684 m.read(objPath, interfacesList);
685
686 if (objPath.str ==
687 "/xyz/openbmc_project/dump/" +
688 std::string(boost::algorithm::to_lower_copy(dumpType)) +
689 "/entry/" + std::to_string(dumpId))
690 {
691 nlohmann::json retMessage = messages::success();
692 taskData->messages.emplace_back(retMessage);
693
694 std::string headerLoc =
695 "Location: " + dumpPath + std::to_string(dumpId);
696 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
697
698 taskData->state = "Completed";
699 return task::completed;
700 }
701 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500702 },
Jason M. Bills4978b632022-02-22 14:17:43 -0800703 "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500704 "member='InterfacesAdded', "
705 "path='/xyz/openbmc_project/dump'");
706
707 task->startTimer(std::chrono::minutes(3));
708 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -0700709 task->payload.emplace(std::move(payload));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500710}
711
zhanghch058d1b46d2021-04-01 11:18:24 +0800712inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
713 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500714{
Claire Weinanfdd26902022-03-01 14:18:25 -0800715 std::string dumpPath = getDumpEntriesPath(dumpType);
716 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500717 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500718 messages::internalError(asyncResp->res);
719 return;
720 }
721
722 std::optional<std::string> diagnosticDataType;
723 std::optional<std::string> oemDiagnosticDataType;
724
Willy Tu15ed6782021-12-14 11:03:16 -0800725 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500726 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
727 "OEMDiagnosticDataType", oemDiagnosticDataType))
728 {
729 return;
730 }
731
732 if (dumpType == "System")
733 {
734 if (!oemDiagnosticDataType || !diagnosticDataType)
735 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800736 BMCWEB_LOG_ERROR
737 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500738 messages::actionParameterMissing(
739 asyncResp->res, "CollectDiagnosticData",
740 "DiagnosticDataType & OEMDiagnosticDataType");
741 return;
742 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700743 if ((*oemDiagnosticDataType != "System") ||
744 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500745 {
746 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700747 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500748 return;
749 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500750 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500751 }
752 else if (dumpType == "BMC")
753 {
754 if (!diagnosticDataType)
755 {
George Liu0fda0f12021-11-16 10:06:17 +0800756 BMCWEB_LOG_ERROR
757 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500758 messages::actionParameterMissing(
759 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
760 return;
761 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700762 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500763 {
764 BMCWEB_LOG_ERROR
765 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700766 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500767 return;
768 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500769 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
770 }
771 else
772 {
773 BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
774 messages::internalError(asyncResp->res);
775 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500776 }
777
778 crow::connections::systemBus->async_method_call(
Ed Tanous98be3e32021-09-16 15:05:36 -0700779 [asyncResp, payload(task::Payload(req)), dumpPath,
780 dumpType](const boost::system::error_code ec,
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500781 const sdbusplus::message::message& msg,
Ed Tanous98be3e32021-09-16 15:05:36 -0700782 const uint32_t& dumpId) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700783 if (ec)
784 {
785 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
Asmitha Karunanithi59075712021-10-22 01:17:41 -0500786 const sd_bus_error* dbusError = msg.get_error();
787 if (dbusError == nullptr)
788 {
789 messages::internalError(asyncResp->res);
790 return;
791 }
792
793 BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
794 << " and error msg: " << dbusError->message;
795 if (std::string_view(
796 "xyz.openbmc_project.Common.Error.NotAllowed") ==
797 dbusError->name)
798 {
799 messages::resourceInStandby(asyncResp->res);
800 return;
801 }
802 if (std::string_view(
803 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
804 dbusError->name)
805 {
806 messages::serviceDisabled(asyncResp->res, dumpPath);
807 return;
808 }
809 if (std::string_view(
810 "xyz.openbmc_project.Common.Error.Unavailable") ==
811 dbusError->name)
812 {
813 messages::resourceInUse(asyncResp->res);
814 return;
815 }
816 // Other Dbus errors such as:
817 // xyz.openbmc_project.Common.Error.InvalidArgument &
818 // org.freedesktop.DBus.Error.InvalidArgs are all related to
819 // the dbus call that is made here in the bmcweb
820 // implementation and has nothing to do with the client's
821 // input in the request. Hence, returning internal error
822 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -0700823 messages::internalError(asyncResp->res);
824 return;
825 }
826 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500827
Ed Tanous002d39b2022-05-31 08:59:27 -0700828 createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath,
829 dumpType);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500830 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500831 "xyz.openbmc_project.Dump.Manager",
832 "/xyz/openbmc_project/dump/" +
833 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500834 "xyz.openbmc_project.Dump.Create", "CreateDump");
835}
836
zhanghch058d1b46d2021-04-01 11:18:24 +0800837inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
838 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500839{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500840 std::string dumpTypeLowerCopy =
841 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800842
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500843 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -0800844 [asyncResp, dumpType](
845 const boost::system::error_code ec,
846 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700847 if (ec)
848 {
849 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
850 messages::internalError(asyncResp->res);
851 return;
852 }
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500853
Ed Tanous002d39b2022-05-31 08:59:27 -0700854 for (const std::string& path : subTreePaths)
855 {
856 sdbusplus::message::object_path objPath(path);
857 std::string logID = objPath.filename();
858 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500859 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700860 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500861 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700862 deleteDumpEntry(asyncResp, logID, dumpType);
863 }
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500864 },
865 "xyz.openbmc_project.ObjectMapper",
866 "/xyz/openbmc_project/object_mapper",
867 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500868 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
869 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
870 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500871}
872
Ed Tanousb9d36b42022-02-26 21:42:46 -0800873inline static void
874 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
875 std::string& filename, std::string& timestamp,
876 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700877{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200878 const std::string* filenamePtr = nullptr;
879 const std::string* timestampPtr = nullptr;
880 const std::string* logfilePtr = nullptr;
881
882 const bool success = sdbusplus::unpackPropertiesNoThrow(
883 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
884 "Filename", filenamePtr, "Log", logfilePtr);
885
886 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700887 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200888 return;
889 }
890
891 if (filenamePtr != nullptr)
892 {
893 filename = *filenamePtr;
894 }
895
896 if (timestampPtr != nullptr)
897 {
898 timestamp = *timestampPtr;
899 }
900
901 if (logfilePtr != nullptr)
902 {
903 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -0700904 }
905}
906
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500907constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700908inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700909{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800910 /**
911 * Functions triggers appropriate requests on DBus
912 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700913 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700914 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -0700915 .methods(boost::beast::http::verb::get)(
916 [&app](const crow::Request& req,
917 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000918 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700919 {
920 return;
921 }
922 // Collections don't include the static data added by SubRoute
923 // because it has a duplicate entry for members
924 asyncResp->res.jsonValue["@odata.type"] =
925 "#LogServiceCollection.LogServiceCollection";
926 asyncResp->res.jsonValue["@odata.id"] =
927 "/redfish/v1/Systems/system/LogServices";
928 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
929 asyncResp->res.jsonValue["Description"] =
930 "Collection of LogServices for this Computer System";
931 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
932 logServiceArray = nlohmann::json::array();
933 nlohmann::json::object_t eventLog;
934 eventLog["@odata.id"] =
935 "/redfish/v1/Systems/system/LogServices/EventLog";
936 logServiceArray.push_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500937#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -0700938 nlohmann::json::object_t dumpLog;
939 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
940 logServiceArray.push_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -0600941#endif
942
Jason M. Billsd53dd412019-02-12 17:16:22 -0800943#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -0700944 nlohmann::json::object_t crashdump;
945 crashdump["@odata.id"] =
946 "/redfish/v1/Systems/system/LogServices/Crashdump";
947 logServiceArray.push_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -0800948#endif
Spencer Kub7028eb2021-10-26 15:27:35 +0800949
950#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -0700951 nlohmann::json::object_t hostlogger;
952 hostlogger["@odata.id"] =
953 "/redfish/v1/Systems/system/LogServices/HostLogger";
954 logServiceArray.push_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +0800955#endif
Ed Tanous002d39b2022-05-31 08:59:27 -0700956 asyncResp->res.jsonValue["Members@odata.count"] =
957 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800958
Ed Tanous002d39b2022-05-31 08:59:27 -0700959 crow::connections::systemBus->async_method_call(
960 [asyncResp](const boost::system::error_code ec,
961 const dbus::utility::MapperGetSubTreePathsResponse&
962 subtreePath) {
963 if (ec)
964 {
965 BMCWEB_LOG_ERROR << ec;
966 return;
967 }
Ed Tanous45ca1b82022-03-25 13:07:27 -0700968
Ed Tanous002d39b2022-05-31 08:59:27 -0700969 for (const auto& pathStr : subtreePath)
970 {
971 if (pathStr.find("PostCode") != std::string::npos)
972 {
973 nlohmann::json& logServiceArrayLocal =
974 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -0700975 nlohmann::json::object_t member;
976 member["@odata.id"] =
977 "/redfish/v1/Systems/system/LogServices/PostCodes";
978
979 logServiceArrayLocal.push_back(std::move(member));
980
Ed Tanous002d39b2022-05-31 08:59:27 -0700981 asyncResp->res.jsonValue["Members@odata.count"] =
982 logServiceArrayLocal.size();
983 return;
984 }
985 }
986 },
987 "xyz.openbmc_project.ObjectMapper",
988 "/xyz/openbmc_project/object_mapper",
989 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
990 std::array<const char*, 1>{postCodeIface});
Ed Tanous45ca1b82022-03-25 13:07:27 -0700991 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700992}
993
994inline void requestRoutesEventLogService(App& app)
995{
996 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -0700997 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700998 .methods(boost::beast::http::verb::get)(
999 [&app](const crow::Request& req,
1000 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001001 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001002 {
1003 return;
1004 }
1005 asyncResp->res.jsonValue["@odata.id"] =
1006 "/redfish/v1/Systems/system/LogServices/EventLog";
1007 asyncResp->res.jsonValue["@odata.type"] =
1008 "#LogService.v1_1_0.LogService";
1009 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1010 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1011 asyncResp->res.jsonValue["Id"] = "EventLog";
1012 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301013
Ed Tanous002d39b2022-05-31 08:59:27 -07001014 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001015 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301016
Ed Tanous002d39b2022-05-31 08:59:27 -07001017 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1018 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1019 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301020
Ed Tanous002d39b2022-05-31 08:59:27 -07001021 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1022 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1023 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001024
Ed Tanous002d39b2022-05-31 08:59:27 -07001025 {"target",
1026 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001027 });
1028}
1029
1030inline void requestRoutesJournalEventLogClear(App& app)
1031{
Jason M. Bills4978b632022-02-22 14:17:43 -08001032 BMCWEB_ROUTE(
1033 app,
1034 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001035 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001036 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001037 [&app](const crow::Request& req,
1038 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001039 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001040 {
1041 return;
1042 }
1043 // Clear the EventLog by deleting the log files
1044 std::vector<std::filesystem::path> redfishLogFiles;
1045 if (getRedfishLogFiles(redfishLogFiles))
1046 {
1047 for (const std::filesystem::path& file : redfishLogFiles)
1048 {
1049 std::error_code ec;
1050 std::filesystem::remove(file, ec);
1051 }
1052 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001053
Ed Tanous002d39b2022-05-31 08:59:27 -07001054 // Reload rsyslog so it knows to start new log files
1055 crow::connections::systemBus->async_method_call(
1056 [asyncResp](const boost::system::error_code ec) {
1057 if (ec)
1058 {
1059 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1060 messages::internalError(asyncResp->res);
1061 return;
1062 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001063
Ed Tanous002d39b2022-05-31 08:59:27 -07001064 messages::success(asyncResp->res);
1065 },
1066 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1067 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1068 "replace");
1069 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001070}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001071
Jason M. Billsac992cd2022-06-24 13:31:46 -07001072enum class LogParseError
1073{
1074 success,
1075 parseFailed,
1076 messageIdNotInRegistry,
1077};
1078
1079static LogParseError
1080 fillEventLogEntryJson(const std::string& logEntryID,
1081 const std::string& logEntry,
1082 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001083{
Jason M. Bills95820182019-04-22 16:25:34 -07001084 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001085 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001086 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001087 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001088 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001089 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001090 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001091 std::string timestamp = logEntry.substr(0, space);
1092 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001093 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001094 if (entryStart == std::string::npos)
1095 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001096 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001097 }
1098 std::string_view entry(logEntry);
1099 entry.remove_prefix(entryStart);
1100 // Use split to separate the entry into its fields
1101 std::vector<std::string> logEntryFields;
1102 boost::split(logEntryFields, entry, boost::is_any_of(","),
1103 boost::token_compress_on);
1104 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001105 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001106 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001107 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001108 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001109 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001110
Jason M. Bills4851d452019-03-28 11:27:48 -07001111 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001112 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001113
Sui Chen54417b02022-03-24 14:59:52 -07001114 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001115 {
Sui Chen54417b02022-03-24 14:59:52 -07001116 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001117 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001118 }
1119
Sui Chen54417b02022-03-24 14:59:52 -07001120 std::string msg = message->message;
1121
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001122 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001123 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001124 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001125 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001126 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001127 // If the first string is empty, assume there are no MessageArgs
1128 std::size_t messageArgsSize = 0;
1129 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001130 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001131 messageArgsSize = logEntryFields.size() - 1;
1132 }
1133
Ed Tanous23a21a12020-07-25 04:45:05 +00001134 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001135
1136 // Fill the MessageArgs into the Message
1137 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001138 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001139 {
1140 std::string argStr = "%" + std::to_string(++i);
1141 size_t argPos = msg.find(argStr);
1142 if (argPos != std::string::npos)
1143 {
1144 msg.replace(argPos, argStr.length(), messageArg);
1145 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001146 }
1147 }
1148
Jason M. Bills95820182019-04-22 16:25:34 -07001149 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1150 // format which matches the Redfish format except for the fractional seconds
1151 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001152 std::size_t dot = timestamp.find_first_of('.');
1153 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001154 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001155 {
Jason M. Bills95820182019-04-22 16:25:34 -07001156 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001157 }
1158
1159 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001160 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07001161 logEntryJson["@odata.id"] =
1162 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
1163 logEntryJson["Name"] = "System Event Log Entry";
1164 logEntryJson["Id"] = logEntryID;
1165 logEntryJson["Message"] = std::move(msg);
1166 logEntryJson["MessageId"] = std::move(messageID);
1167 logEntryJson["MessageArgs"] = messageArgs;
1168 logEntryJson["EntryType"] = "Event";
1169 logEntryJson["Severity"] = message->messageSeverity;
1170 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001171 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001172}
1173
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001174inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001175{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001176 BMCWEB_ROUTE(app,
1177 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001178 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001179 .methods(boost::beast::http::verb::get)(
1180 [&app](const crow::Request& req,
1181 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1182 query_param::QueryCapabilities capabilities = {
1183 .canDelegateTop = true,
1184 .canDelegateSkip = true,
1185 };
1186 query_param::Query delegatedQuery;
1187 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001188 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001189 {
1190 return;
1191 }
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001192 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001193 size_t skip = delegatedQuery.skip.value_or(0);
1194
Ed Tanous002d39b2022-05-31 08:59:27 -07001195 // Collections don't include the static data added by SubRoute
1196 // because it has a duplicate entry for members
1197 asyncResp->res.jsonValue["@odata.type"] =
1198 "#LogEntryCollection.LogEntryCollection";
1199 asyncResp->res.jsonValue["@odata.id"] =
1200 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1201 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1202 asyncResp->res.jsonValue["Description"] =
1203 "Collection of System Event Log Entries";
1204
1205 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1206 logEntryArray = nlohmann::json::array();
1207 // Go through the log files and create a unique ID for each
1208 // entry
1209 std::vector<std::filesystem::path> redfishLogFiles;
1210 getRedfishLogFiles(redfishLogFiles);
1211 uint64_t entryCount = 0;
1212 std::string logEntry;
1213
1214 // Oldest logs are in the last file, so start there and loop
1215 // backwards
1216 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1217 it++)
1218 {
1219 std::ifstream logStream(*it);
1220 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001221 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001222 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001223 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001224
Ed Tanous002d39b2022-05-31 08:59:27 -07001225 // Reset the unique ID on the first entry
1226 bool firstEntry = true;
1227 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001228 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001229 std::string idStr;
1230 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001231 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001232 continue;
1233 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001234 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001235
Jason M. Billsde703c52022-06-23 14:19:04 -07001236 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001237 LogParseError status =
1238 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1239 if (status == LogParseError::messageIdNotInRegistry)
1240 {
1241 continue;
1242 }
1243 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001244 {
1245 messages::internalError(asyncResp->res);
1246 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001247 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001248
Jason M. Billsde703c52022-06-23 14:19:04 -07001249 entryCount++;
1250 // Handle paging using skip (number of entries to skip from the
1251 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001252 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001253 {
1254 continue;
1255 }
1256
1257 logEntryArray.push_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001258 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001259 }
1260 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001261 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001262 {
1263 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1264 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001265 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001266 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001267 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001268}
Chicago Duan336e96c2019-07-15 14:22:08 +08001269
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001270inline void requestRoutesJournalEventLogEntry(App& app)
1271{
1272 BMCWEB_ROUTE(
1273 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001274 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001275 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001276 [&app](const crow::Request& req,
1277 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1278 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001279 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001280 {
1281 return;
1282 }
1283 const std::string& targetID = param;
1284
1285 // Go through the log files and check the unique ID for each
1286 // entry to find the target entry
1287 std::vector<std::filesystem::path> redfishLogFiles;
1288 getRedfishLogFiles(redfishLogFiles);
1289 std::string logEntry;
1290
1291 // Oldest logs are in the last file, so start there and loop
1292 // backwards
1293 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1294 it++)
1295 {
1296 std::ifstream logStream(*it);
1297 if (!logStream.is_open())
1298 {
1299 continue;
1300 }
1301
1302 // Reset the unique ID on the first entry
1303 bool firstEntry = true;
1304 while (std::getline(logStream, logEntry))
1305 {
1306 std::string idStr;
1307 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001308 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001309 continue;
1310 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001311 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001312
1313 if (idStr == targetID)
1314 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001315 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001316 LogParseError status =
1317 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1318 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001319 {
1320 messages::internalError(asyncResp->res);
1321 return;
1322 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001323 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001324 return;
1325 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001326 }
1327 }
1328 // Requested ID was not found
1329 messages::resourceMissingAtURI(asyncResp->res,
1330 crow::utility::urlFromPieces(targetID));
1331 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001332}
1333
1334inline void requestRoutesDBusEventLogEntryCollection(App& app)
1335{
1336 BMCWEB_ROUTE(app,
1337 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001338 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001339 .methods(boost::beast::http::verb::get)(
1340 [&app](const crow::Request& req,
1341 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001342 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001343 {
1344 return;
1345 }
1346 // Collections don't include the static data added by SubRoute
1347 // because it has a duplicate entry for members
1348 asyncResp->res.jsonValue["@odata.type"] =
1349 "#LogEntryCollection.LogEntryCollection";
1350 asyncResp->res.jsonValue["@odata.id"] =
1351 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1352 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1353 asyncResp->res.jsonValue["Description"] =
1354 "Collection of System Event Log Entries";
1355
1356 // DBus implementation of EventLog/Entries
1357 // Make call to Logging Service to find all log entry objects
1358 crow::connections::systemBus->async_method_call(
1359 [asyncResp](const boost::system::error_code ec,
1360 const dbus::utility::ManagedObjectType& resp) {
1361 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001362 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001363 // TODO Handle for specific error code
1364 BMCWEB_LOG_ERROR
1365 << "getLogEntriesIfaceData resp_handler got error " << ec;
1366 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001367 return;
1368 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001369 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1370 entriesArray = nlohmann::json::array();
1371 for (const auto& objectPath : resp)
1372 {
1373 const uint32_t* id = nullptr;
1374 const uint64_t* timestamp = nullptr;
1375 const uint64_t* updateTimestamp = nullptr;
1376 const std::string* severity = nullptr;
1377 const std::string* message = nullptr;
1378 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001379 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001380 bool resolved = false;
1381 for (const auto& interfaceMap : objectPath.second)
1382 {
1383 if (interfaceMap.first ==
1384 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001385 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001386 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001387 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001388 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001389 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001390 id = std::get_if<uint32_t>(&propertyMap.second);
1391 }
1392 else if (propertyMap.first == "Timestamp")
1393 {
1394 timestamp =
1395 std::get_if<uint64_t>(&propertyMap.second);
1396 }
1397 else if (propertyMap.first == "UpdateTimestamp")
1398 {
1399 updateTimestamp =
1400 std::get_if<uint64_t>(&propertyMap.second);
1401 }
1402 else if (propertyMap.first == "Severity")
1403 {
1404 severity = std::get_if<std::string>(
1405 &propertyMap.second);
1406 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001407 else if (propertyMap.first == "Resolution")
1408 {
1409 resolution = std::get_if<std::string>(
1410 &propertyMap.second);
1411 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 else if (propertyMap.first == "Message")
1413 {
1414 message = std::get_if<std::string>(
1415 &propertyMap.second);
1416 }
1417 else if (propertyMap.first == "Resolved")
1418 {
1419 const bool* resolveptr =
1420 std::get_if<bool>(&propertyMap.second);
1421 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001422 {
1423 messages::internalError(asyncResp->res);
1424 return;
1425 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001426 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001427 }
1428 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001429 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001430 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001431 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001432 messages::internalError(asyncResp->res);
1433 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001434 }
1435 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001436 else if (interfaceMap.first ==
1437 "xyz.openbmc_project.Common.FilePath")
1438 {
1439 for (const auto& propertyMap : interfaceMap.second)
1440 {
1441 if (propertyMap.first == "Path")
1442 {
1443 filePath = std::get_if<std::string>(
1444 &propertyMap.second);
1445 }
1446 }
1447 }
1448 }
1449 // Object path without the
1450 // xyz.openbmc_project.Logging.Entry interface, ignore
1451 // and continue.
1452 if (id == nullptr || message == nullptr ||
1453 severity == nullptr || timestamp == nullptr ||
1454 updateTimestamp == nullptr)
1455 {
1456 continue;
1457 }
1458 entriesArray.push_back({});
1459 nlohmann::json& thisEntry = entriesArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001460 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001461 thisEntry["@odata.id"] =
1462 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1463 std::to_string(*id);
1464 thisEntry["Name"] = "System Event Log Entry";
1465 thisEntry["Id"] = std::to_string(*id);
1466 thisEntry["Message"] = *message;
1467 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001468 if ((resolution != nullptr) && (!(*resolution).empty()))
1469 {
1470 thisEntry["Resolution"] = *resolution;
1471 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001472 thisEntry["EntryType"] = "Event";
1473 thisEntry["Severity"] =
1474 translateSeverityDbusToRedfish(*severity);
1475 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001476 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001477 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001478 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001479 if (filePath != nullptr)
1480 {
1481 thisEntry["AdditionalDataURI"] =
1482 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1483 std::to_string(*id) + "/attachment";
1484 }
1485 }
1486 std::sort(
1487 entriesArray.begin(), entriesArray.end(),
1488 [](const nlohmann::json& left, const nlohmann::json& right) {
1489 return (left["Id"] <= right["Id"]);
1490 });
1491 asyncResp->res.jsonValue["Members@odata.count"] =
1492 entriesArray.size();
1493 },
1494 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1495 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001496 });
1497}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001498
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001499inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001500{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001501 BMCWEB_ROUTE(
1502 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001503 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001504 .methods(boost::beast::http::verb::get)(
1505 [&app](const crow::Request& req,
1506 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1507 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001508 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001509 {
1510 return;
1511 }
1512 std::string entryID = param;
1513 dbus::utility::escapePathForDbus(entryID);
1514
1515 // DBus implementation of EventLog/Entries
1516 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001517 sdbusplus::asio::getAllProperties(
1518 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1519 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous002d39b2022-05-31 08:59:27 -07001520 [asyncResp, entryID](const boost::system::error_code ec,
1521 const dbus::utility::DBusPropertiesMap& resp) {
1522 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001523 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001524 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1525 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001526 return;
1527 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001528 if (ec)
1529 {
1530 BMCWEB_LOG_ERROR
1531 << "EventLogEntry (DBus) resp_handler got error " << ec;
1532 messages::internalError(asyncResp->res);
1533 return;
1534 }
1535 const uint32_t* id = nullptr;
1536 const uint64_t* timestamp = nullptr;
1537 const uint64_t* updateTimestamp = nullptr;
1538 const std::string* severity = nullptr;
1539 const std::string* message = nullptr;
1540 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001541 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001542 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001543
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001544 const bool success = sdbusplus::unpackPropertiesNoThrow(
1545 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1546 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001547 severity, "Message", message, "Resolved", resolved,
1548 "Resolution", resolution, "Path", filePath);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001549
1550 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001551 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001552 messages::internalError(asyncResp->res);
1553 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001554 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001555
Ed Tanous002d39b2022-05-31 08:59:27 -07001556 if (id == nullptr || message == nullptr || severity == nullptr ||
1557 timestamp == nullptr || updateTimestamp == nullptr)
1558 {
1559 messages::internalError(asyncResp->res);
1560 return;
1561 }
1562 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001563 "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07001564 asyncResp->res.jsonValue["@odata.id"] =
1565 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1566 std::to_string(*id);
1567 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1568 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1569 asyncResp->res.jsonValue["Message"] = *message;
1570 asyncResp->res.jsonValue["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001571 if ((resolution != nullptr) && (!(*resolution).empty()))
1572 {
1573 asyncResp->res.jsonValue["Resolution"] = *resolution;
1574 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001575 asyncResp->res.jsonValue["EntryType"] = "Event";
1576 asyncResp->res.jsonValue["Severity"] =
1577 translateSeverityDbusToRedfish(*severity);
1578 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001579 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001580 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001581 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001582 if (filePath != nullptr)
1583 {
1584 asyncResp->res.jsonValue["AdditionalDataURI"] =
1585 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1586 std::to_string(*id) + "/attachment";
1587 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001588 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001589 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001590
1591 BMCWEB_ROUTE(
1592 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001593 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001594 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001595 [&app](const crow::Request& req,
1596 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1597 const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001598 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001599 {
1600 return;
1601 }
1602 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001603
Ed Tanous002d39b2022-05-31 08:59:27 -07001604 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1605 resolved))
1606 {
1607 return;
1608 }
1609 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001610
Ed Tanous002d39b2022-05-31 08:59:27 -07001611 crow::connections::systemBus->async_method_call(
1612 [asyncResp, entryId](const boost::system::error_code ec) {
1613 if (ec)
1614 {
1615 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1616 messages::internalError(asyncResp->res);
1617 return;
1618 }
1619 },
1620 "xyz.openbmc_project.Logging",
1621 "/xyz/openbmc_project/logging/entry/" + entryId,
1622 "org.freedesktop.DBus.Properties", "Set",
1623 "xyz.openbmc_project.Logging.Entry", "Resolved",
1624 dbus::utility::DbusVariantType(*resolved));
1625 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001626
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001627 BMCWEB_ROUTE(
1628 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001629 .privileges(redfish::privileges::deleteLogEntry)
1630
Ed Tanous002d39b2022-05-31 08:59:27 -07001631 .methods(boost::beast::http::verb::delete_)(
1632 [&app](const crow::Request& req,
1633 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1634 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001635 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001636 {
1637 return;
1638 }
1639 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1640
1641 std::string entryID = param;
1642
1643 dbus::utility::escapePathForDbus(entryID);
1644
1645 // Process response from Logging service.
1646 auto respHandler =
1647 [asyncResp, entryID](const boost::system::error_code ec) {
1648 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1649 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001650 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001651 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001652 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001653 messages::resourceNotFound(asyncResp->res, "LogEntry",
1654 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001655 return;
1656 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001657 // TODO Handle for specific error code
1658 BMCWEB_LOG_ERROR
1659 << "EventLogEntry (DBus) doDelete respHandler got error "
1660 << ec;
1661 asyncResp->res.result(
1662 boost::beast::http::status::internal_server_error);
1663 return;
1664 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001665
Ed Tanous002d39b2022-05-31 08:59:27 -07001666 asyncResp->res.result(boost::beast::http::status::ok);
1667 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001668
Ed Tanous002d39b2022-05-31 08:59:27 -07001669 // Make call to Logging service to request Delete Log
1670 crow::connections::systemBus->async_method_call(
1671 respHandler, "xyz.openbmc_project.Logging",
1672 "/xyz/openbmc_project/logging/entry/" + entryID,
1673 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001674 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001675}
1676
1677inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001678{
George Liu0fda0f12021-11-16 10:06:17 +08001679 BMCWEB_ROUTE(
1680 app,
1681 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001682 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001683 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001684 [&app](const crow::Request& req,
1685 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1686 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001687 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001688 {
1689 return;
1690 }
Ed Tanous99351cd2022-08-07 16:42:51 -07001691 if (http_helpers::isContentTypeAllowed(
1692 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07001693 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07001694 {
1695 asyncResp->res.result(boost::beast::http::status::bad_request);
1696 return;
1697 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001698
Ed Tanous002d39b2022-05-31 08:59:27 -07001699 std::string entryID = param;
1700 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001701
Ed Tanous002d39b2022-05-31 08:59:27 -07001702 crow::connections::systemBus->async_method_call(
1703 [asyncResp, entryID](const boost::system::error_code ec,
1704 const sdbusplus::message::unix_fd& unixfd) {
1705 if (ec.value() == EBADR)
1706 {
1707 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1708 entryID);
1709 return;
1710 }
1711 if (ec)
1712 {
1713 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1714 messages::internalError(asyncResp->res);
1715 return;
1716 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001717
Ed Tanous002d39b2022-05-31 08:59:27 -07001718 int fd = -1;
1719 fd = dup(unixfd);
1720 if (fd == -1)
1721 {
1722 messages::internalError(asyncResp->res);
1723 return;
1724 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001725
Ed Tanous002d39b2022-05-31 08:59:27 -07001726 long long int size = lseek(fd, 0, SEEK_END);
1727 if (size == -1)
1728 {
1729 messages::internalError(asyncResp->res);
1730 return;
1731 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001732
Ed Tanous002d39b2022-05-31 08:59:27 -07001733 // Arbitrary max size of 64kb
1734 constexpr int maxFileSize = 65536;
1735 if (size > maxFileSize)
1736 {
1737 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1738 << maxFileSize;
1739 messages::internalError(asyncResp->res);
1740 return;
1741 }
1742 std::vector<char> data(static_cast<size_t>(size));
1743 long long int rc = lseek(fd, 0, SEEK_SET);
1744 if (rc == -1)
1745 {
1746 messages::internalError(asyncResp->res);
1747 return;
1748 }
1749 rc = read(fd, data.data(), data.size());
1750 if ((rc == -1) || (rc != size))
1751 {
1752 messages::internalError(asyncResp->res);
1753 return;
1754 }
1755 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001756
Ed Tanous002d39b2022-05-31 08:59:27 -07001757 std::string_view strData(data.data(), data.size());
1758 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001759
Ed Tanousd9f6c622022-03-17 09:12:17 -07001760 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07001761 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07001762 asyncResp->res.addHeader(
1763 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07001764 asyncResp->res.body() = std::move(output);
1765 },
1766 "xyz.openbmc_project.Logging",
1767 "/xyz/openbmc_project/logging/entry/" + entryID,
1768 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1769 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001770}
1771
Spencer Kub7028eb2021-10-26 15:27:35 +08001772constexpr const char* hostLoggerFolderPath = "/var/log/console";
1773
1774inline bool
1775 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1776 std::vector<std::filesystem::path>& hostLoggerFiles)
1777{
1778 std::error_code ec;
1779 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1780 if (ec)
1781 {
1782 BMCWEB_LOG_ERROR << ec.message();
1783 return false;
1784 }
1785 for (const std::filesystem::directory_entry& it : logPath)
1786 {
1787 std::string filename = it.path().filename();
1788 // Prefix of each log files is "log". Find the file and save the
1789 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07001790 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08001791 {
1792 hostLoggerFiles.emplace_back(it.path());
1793 }
1794 }
1795 // As the log files rotate, they are appended with a ".#" that is higher for
1796 // the older logs. Since we start from oldest logs, sort the name in
1797 // descending order.
1798 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1799 AlphanumLess<std::string>());
1800
1801 return true;
1802}
1803
Ed Tanous02cad962022-06-30 16:50:15 -07001804inline bool getHostLoggerEntries(
1805 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
1806 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001807{
1808 GzFileReader logFile;
1809
1810 // Go though all log files and expose host logs.
1811 for (const std::filesystem::path& it : hostLoggerFiles)
1812 {
1813 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1814 {
1815 BMCWEB_LOG_ERROR << "fail to expose host logs";
1816 return false;
1817 }
1818 }
1819 // Get lastMessage from constructor by getter
1820 std::string lastMessage = logFile.getLastMessage();
1821 if (!lastMessage.empty())
1822 {
1823 logCount++;
1824 if (logCount > skip && logCount <= (skip + top))
1825 {
1826 logEntries.push_back(lastMessage);
1827 }
1828 }
1829 return true;
1830}
1831
1832inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1833 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001834 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08001835{
1836 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05001837 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001838 logEntryJson["@odata.id"] =
1839 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1840 logEntryID;
1841 logEntryJson["Name"] = "Host Logger Entry";
1842 logEntryJson["Id"] = logEntryID;
1843 logEntryJson["Message"] = msg;
1844 logEntryJson["EntryType"] = "Oem";
1845 logEntryJson["Severity"] = "OK";
1846 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08001847}
1848
1849inline void requestRoutesSystemHostLogger(App& app)
1850{
1851 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1852 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07001853 .methods(boost::beast::http::verb::get)(
1854 [&app](const crow::Request& req,
1855 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001856 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001857 {
1858 return;
1859 }
1860 asyncResp->res.jsonValue["@odata.id"] =
1861 "/redfish/v1/Systems/system/LogServices/HostLogger";
1862 asyncResp->res.jsonValue["@odata.type"] =
1863 "#LogService.v1_1_0.LogService";
1864 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1865 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1866 asyncResp->res.jsonValue["Id"] = "HostLogger";
1867 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1868 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1869 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001870}
1871
1872inline void requestRoutesSystemHostLoggerCollection(App& app)
1873{
1874 BMCWEB_ROUTE(app,
1875 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1876 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001877 .methods(boost::beast::http::verb::get)(
1878 [&app](const crow::Request& req,
1879 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1880 query_param::QueryCapabilities capabilities = {
1881 .canDelegateTop = true,
1882 .canDelegateSkip = true,
1883 };
1884 query_param::Query delegatedQuery;
1885 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001886 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001887 {
1888 return;
1889 }
1890 asyncResp->res.jsonValue["@odata.id"] =
1891 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1892 asyncResp->res.jsonValue["@odata.type"] =
1893 "#LogEntryCollection.LogEntryCollection";
1894 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1895 asyncResp->res.jsonValue["Description"] =
1896 "Collection of HostLogger Entries";
1897 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1898 logEntryArray = nlohmann::json::array();
1899 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001900
Ed Tanous002d39b2022-05-31 08:59:27 -07001901 std::vector<std::filesystem::path> hostLoggerFiles;
1902 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1903 {
1904 BMCWEB_LOG_ERROR << "fail to get host log file path";
1905 return;
1906 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07001907 // If we weren't provided top and skip limits, use the defaults.
1908 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001909 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07001910 size_t logCount = 0;
1911 // This vector only store the entries we want to expose that
1912 // control by skip and top.
1913 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001914 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1915 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07001916 {
1917 messages::internalError(asyncResp->res);
1918 return;
1919 }
1920 // If vector is empty, that means skip value larger than total
1921 // log count
1922 if (logEntries.empty())
1923 {
1924 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1925 return;
1926 }
1927 if (!logEntries.empty())
1928 {
1929 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08001930 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001931 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001932 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
1933 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001934 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08001935 }
1936
Ed Tanous002d39b2022-05-31 08:59:27 -07001937 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001938 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08001939 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001940 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1941 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001942 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08001943 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001944 }
George Liu0fda0f12021-11-16 10:06:17 +08001945 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001946}
1947
1948inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1949{
1950 BMCWEB_ROUTE(
1951 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1952 .privileges(redfish::privileges::getLogEntry)
1953 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001954 [&app](const crow::Request& req,
1955 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1956 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001957 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001958 {
1959 return;
1960 }
1961 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08001962
Ed Tanous002d39b2022-05-31 08:59:27 -07001963 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08001964
Ed Tanous002d39b2022-05-31 08:59:27 -07001965 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1966 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08001967
Ed Tanous002d39b2022-05-31 08:59:27 -07001968 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1969 if (ec == std::errc::invalid_argument)
1970 {
1971 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1972 return;
1973 }
1974 if (ec == std::errc::result_out_of_range)
1975 {
1976 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1977 return;
1978 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001979
Ed Tanous002d39b2022-05-31 08:59:27 -07001980 std::vector<std::filesystem::path> hostLoggerFiles;
1981 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1982 {
1983 BMCWEB_LOG_ERROR << "fail to get host log file path";
1984 return;
1985 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001986
Ed Tanous002d39b2022-05-31 08:59:27 -07001987 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001988 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 std::vector<std::string> logEntries;
1990 // We can get specific entry by skip and top. For example, if we
1991 // want to get nth entry, we can set skip = n-1 and top = 1 to
1992 // get that entry
1993 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
1994 logCount))
1995 {
1996 messages::internalError(asyncResp->res);
1997 return;
1998 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001999
Ed Tanous002d39b2022-05-31 08:59:27 -07002000 if (!logEntries.empty())
2001 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002002 nlohmann::json::object_t hostLogEntry;
2003 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2004 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002005 return;
2006 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002007
Ed Tanous002d39b2022-05-31 08:59:27 -07002008 // Requested ID was not found
2009 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2010 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002011}
2012
Claire Weinanfdd26902022-03-01 14:18:25 -08002013constexpr char const* dumpManagerIface =
2014 "xyz.openbmc_project.Collection.DeleteAll";
Claire Weinandd72e872022-08-15 14:20:06 -07002015inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002016 crow::App& app, const crow::Request& req,
2017 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2018{
2019 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2020 {
2021 return;
2022 }
2023 // Collections don't include the static data added by SubRoute
2024 // because it has a duplicate entry for members
2025 asyncResp->res.jsonValue["@odata.type"] =
2026 "#LogServiceCollection.LogServiceCollection";
2027 asyncResp->res.jsonValue["@odata.id"] =
2028 "/redfish/v1/Managers/bmc/LogServices";
2029 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2030 asyncResp->res.jsonValue["Description"] =
2031 "Collection of LogServices for this Manager";
2032 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2033 logServiceArray = nlohmann::json::array();
2034
2035#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002036 nlohmann::json::object_t journal;
2037 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2038 logServiceArray.push_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002039#endif
2040
2041 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2042
2043#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2044 auto respHandler =
2045 [asyncResp](
2046 const boost::system::error_code ec,
2047 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2048 if (ec)
2049 {
2050 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002051 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002052 << ec;
2053 // Assume that getting an error simply means there are no dump
2054 // LogServices. Return without adding any error response.
2055 return;
2056 }
2057
2058 nlohmann::json& logServiceArrayLocal =
2059 asyncResp->res.jsonValue["Members"];
2060
2061 for (const std::string& path : subTreePaths)
2062 {
2063 if (path == "/xyz/openbmc_project/dump/bmc")
2064 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002065 nlohmann::json::object_t member;
2066 member["@odata.id"] =
2067 "/redfish/v1/Managers/bmc/LogServices/Dump";
2068 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002069 }
2070 else if (path == "/xyz/openbmc_project/dump/faultlog")
2071 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002072 nlohmann::json::object_t member;
2073 member["@odata.id"] =
2074 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2075 logServiceArrayLocal.push_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002076 }
2077 }
2078
2079 asyncResp->res.jsonValue["Members@odata.count"] =
2080 logServiceArrayLocal.size();
2081 };
2082
2083 crow::connections::systemBus->async_method_call(
2084 respHandler, "xyz.openbmc_project.ObjectMapper",
2085 "/xyz/openbmc_project/object_mapper",
2086 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2087 "/xyz/openbmc_project/dump", 0,
2088 std::array<const char*, 1>{dumpManagerIface});
2089#endif
2090}
2091
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002092inline void requestRoutesBMCLogServiceCollection(App& app)
2093{
2094 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002095 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002096 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002097 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002098}
Ed Tanous1da66f72018-07-27 16:13:37 -07002099
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002100inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002101{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002102 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002103 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002104 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002105 [&app](const crow::Request& req,
2106 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002107 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 {
2109 return;
2110 }
2111 asyncResp->res.jsonValue["@odata.type"] =
2112 "#LogService.v1_1_0.LogService";
2113 asyncResp->res.jsonValue["@odata.id"] =
2114 "/redfish/v1/Managers/bmc/LogServices/Journal";
2115 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2116 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2117 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2118 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302119
Ed Tanous002d39b2022-05-31 08:59:27 -07002120 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002121 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002122 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2123 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2124 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302125
Ed Tanous002d39b2022-05-31 08:59:27 -07002126 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2127 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2128 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002129}
Jason M. Billse1f26342018-07-18 12:12:00 -07002130
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002131static int
2132 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2133 sd_journal* journal,
2134 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002135{
2136 // Get the Log Entry contents
2137 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002138
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002139 std::string message;
2140 std::string_view syslogID;
2141 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2142 if (ret < 0)
2143 {
2144 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2145 << strerror(-ret);
2146 }
2147 if (!syslogID.empty())
2148 {
2149 message += std::string(syslogID) + ": ";
2150 }
2151
Ed Tanous39e77502019-03-04 17:35:53 -08002152 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002153 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002154 if (ret < 0)
2155 {
2156 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2157 return 1;
2158 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002159 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002160
2161 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002162 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002163 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002164 if (ret < 0)
2165 {
2166 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002167 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002168
2169 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002170 std::string entryTimeStr;
2171 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002172 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002173 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002174 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002175
2176 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002177 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07002178 bmcJournalLogEntryJson["@odata.id"] =
2179 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2180 bmcJournalLogEntryID;
2181 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2182 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2183 bmcJournalLogEntryJson["Message"] = std::move(message);
2184 bmcJournalLogEntryJson["EntryType"] = "Oem";
2185 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2186 : severity <= 4 ? "Warning"
2187 : "OK";
2188 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2189 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002190 return 0;
2191}
2192
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002193inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002194{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002195 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002196 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002197 .methods(boost::beast::http::verb::get)(
2198 [&app](const crow::Request& req,
2199 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2200 query_param::QueryCapabilities capabilities = {
2201 .canDelegateTop = true,
2202 .canDelegateSkip = true,
2203 };
2204 query_param::Query delegatedQuery;
2205 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002206 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002207 {
2208 return;
2209 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002210
2211 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002212 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002213
Ed Tanous002d39b2022-05-31 08:59:27 -07002214 // Collections don't include the static data added by SubRoute
2215 // because it has a duplicate entry for members
2216 asyncResp->res.jsonValue["@odata.type"] =
2217 "#LogEntryCollection.LogEntryCollection";
2218 asyncResp->res.jsonValue["@odata.id"] =
2219 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2220 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2221 asyncResp->res.jsonValue["Description"] =
2222 "Collection of BMC Journal Entries";
2223 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2224 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002225
Ed Tanous002d39b2022-05-31 08:59:27 -07002226 // Go through the journal and use the timestamp to create a
2227 // unique ID for each entry
2228 sd_journal* journalTmp = nullptr;
2229 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2230 if (ret < 0)
2231 {
2232 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2233 messages::internalError(asyncResp->res);
2234 return;
2235 }
2236 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2237 journalTmp, sd_journal_close);
2238 journalTmp = nullptr;
2239 uint64_t entryCount = 0;
2240 // Reset the unique ID on the first entry
2241 bool firstEntry = true;
2242 SD_JOURNAL_FOREACH(journal.get())
2243 {
2244 entryCount++;
2245 // Handle paging using skip (number of entries to skip from
2246 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002247 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002248 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 continue;
2250 }
2251
2252 std::string idStr;
2253 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2254 {
2255 continue;
2256 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002257 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002258
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002259 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002260 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2261 bmcJournalLogEntry) != 0)
2262 {
George Liu0fda0f12021-11-16 10:06:17 +08002263 messages::internalError(asyncResp->res);
2264 return;
2265 }
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002266 logEntryArray.push_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002267 }
2268 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002269 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002270 {
2271 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2272 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002273 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002274 }
George Liu0fda0f12021-11-16 10:06:17 +08002275 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002276}
Jason M. Billse1f26342018-07-18 12:12:00 -07002277
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002278inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002279{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002280 BMCWEB_ROUTE(app,
2281 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002282 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002283 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002284 [&app](const crow::Request& req,
2285 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2286 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002287 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002288 {
2289 return;
2290 }
2291 // Convert the unique ID back to a timestamp to find the entry
2292 uint64_t ts = 0;
2293 uint64_t index = 0;
2294 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2295 {
2296 return;
2297 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002298
Ed Tanous002d39b2022-05-31 08:59:27 -07002299 sd_journal* journalTmp = nullptr;
2300 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2301 if (ret < 0)
2302 {
2303 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2304 messages::internalError(asyncResp->res);
2305 return;
2306 }
2307 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2308 journalTmp, sd_journal_close);
2309 journalTmp = nullptr;
2310 // Go to the timestamp in the log and move to the entry at the
2311 // index tracking the unique ID
2312 std::string idStr;
2313 bool firstEntry = true;
2314 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2315 if (ret < 0)
2316 {
2317 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2318 << strerror(-ret);
2319 messages::internalError(asyncResp->res);
2320 return;
2321 }
2322 for (uint64_t i = 0; i <= index; i++)
2323 {
2324 sd_journal_next(journal.get());
2325 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2326 {
2327 messages::internalError(asyncResp->res);
2328 return;
2329 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002330 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002331 }
2332 // Confirm that the entry ID matches what was requested
2333 if (idStr != entryID)
2334 {
2335 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2336 return;
2337 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002338
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002339 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002340 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002341 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002342 {
2343 messages::internalError(asyncResp->res);
2344 return;
2345 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002346 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002347 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002348}
2349
Claire Weinanfdd26902022-03-01 14:18:25 -08002350inline void
2351 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2352 const std::string& dumpType)
2353{
2354 std::string dumpPath;
2355 std::string overWritePolicy;
2356 bool collectDiagnosticDataSupported = false;
2357
2358 if (dumpType == "BMC")
2359 {
2360 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2361 overWritePolicy = "WrapsWhenFull";
2362 collectDiagnosticDataSupported = true;
2363 }
2364 else if (dumpType == "FaultLog")
2365 {
2366 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2367 overWritePolicy = "Unknown";
2368 collectDiagnosticDataSupported = false;
2369 }
2370 else if (dumpType == "System")
2371 {
2372 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2373 overWritePolicy = "WrapsWhenFull";
2374 collectDiagnosticDataSupported = true;
2375 }
2376 else
2377 {
2378 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2379 << dumpType;
2380 messages::internalError(asyncResp->res);
2381 return;
2382 }
2383
2384 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2385 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2386 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2387 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2388 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2389 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2390
2391 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002392 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002393 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2394 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2395 redfishDateTimeOffset.second;
2396
2397 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2398 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2399 dumpPath + "/Actions/LogService.ClearLog";
2400
2401 if (collectDiagnosticDataSupported)
2402 {
2403 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2404 ["target"] =
2405 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2406 }
2407}
2408
2409inline void handleLogServicesDumpServiceGet(
2410 crow::App& app, const std::string& dumpType, const crow::Request& req,
2411 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2412{
2413 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2414 {
2415 return;
2416 }
2417 getDumpServiceInfo(asyncResp, dumpType);
2418}
2419
2420inline void handleLogServicesDumpEntriesCollectionGet(
2421 crow::App& app, const std::string& dumpType, const crow::Request& req,
2422 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2423{
2424 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2425 {
2426 return;
2427 }
2428 getDumpEntryCollection(asyncResp, dumpType);
2429}
2430
2431inline void handleLogServicesDumpEntryGet(
2432 crow::App& app, const std::string& dumpType, const crow::Request& req,
2433 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2434 const std::string& dumpId)
2435{
2436 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2437 {
2438 return;
2439 }
2440 getDumpEntryById(asyncResp, dumpId, dumpType);
2441}
2442
2443inline void handleLogServicesDumpEntryDelete(
2444 crow::App& app, const std::string& dumpType, const crow::Request& req,
2445 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2446 const std::string& dumpId)
2447{
2448 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2449 {
2450 return;
2451 }
2452 deleteDumpEntry(asyncResp, dumpId, dumpType);
2453}
2454
2455inline void handleLogServicesDumpCollectDiagnosticDataPost(
2456 crow::App& app, const std::string& dumpType, const crow::Request& req,
2457 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2458{
2459 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2460 {
2461 return;
2462 }
2463 createDump(asyncResp, req, dumpType);
2464}
2465
2466inline void handleLogServicesDumpClearLogPost(
2467 crow::App& app, const std::string& dumpType, const crow::Request& req,
2468 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2469{
2470 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2471 {
2472 return;
2473 }
2474 clearDump(asyncResp, dumpType);
2475}
2476
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002477inline void requestRoutesBMCDumpService(App& app)
2478{
2479 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002480 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002481 .methods(boost::beast::http::verb::get)(std::bind_front(
2482 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002483}
2484
2485inline void requestRoutesBMCDumpEntryCollection(App& app)
2486{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002487 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002488 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002489 .methods(boost::beast::http::verb::get)(std::bind_front(
2490 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002491}
2492
2493inline void requestRoutesBMCDumpEntry(App& app)
2494{
2495 BMCWEB_ROUTE(app,
2496 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002497 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002498 .methods(boost::beast::http::verb::get)(std::bind_front(
2499 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2500
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002501 BMCWEB_ROUTE(app,
2502 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002503 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002504 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2505 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002506}
2507
2508inline void requestRoutesBMCDumpCreate(App& app)
2509{
George Liu0fda0f12021-11-16 10:06:17 +08002510 BMCWEB_ROUTE(
2511 app,
2512 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002513 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002514 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002515 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2516 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002517}
2518
2519inline void requestRoutesBMCDumpClear(App& app)
2520{
George Liu0fda0f12021-11-16 10:06:17 +08002521 BMCWEB_ROUTE(
2522 app,
2523 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002524 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002525 .methods(boost::beast::http::verb::post)(std::bind_front(
2526 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2527}
2528
2529inline void requestRoutesFaultLogDumpService(App& app)
2530{
2531 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2532 .privileges(redfish::privileges::getLogService)
2533 .methods(boost::beast::http::verb::get)(std::bind_front(
2534 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2535}
2536
2537inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2538{
2539 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2540 .privileges(redfish::privileges::getLogEntryCollection)
2541 .methods(boost::beast::http::verb::get)(
2542 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2543 std::ref(app), "FaultLog"));
2544}
2545
2546inline void requestRoutesFaultLogDumpEntry(App& app)
2547{
2548 BMCWEB_ROUTE(app,
2549 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2550 .privileges(redfish::privileges::getLogEntry)
2551 .methods(boost::beast::http::verb::get)(std::bind_front(
2552 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2553
2554 BMCWEB_ROUTE(app,
2555 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2556 .privileges(redfish::privileges::deleteLogEntry)
2557 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2558 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2559}
2560
2561inline void requestRoutesFaultLogDumpClear(App& app)
2562{
2563 BMCWEB_ROUTE(
2564 app,
2565 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2566 .privileges(redfish::privileges::postLogService)
2567 .methods(boost::beast::http::verb::post)(std::bind_front(
2568 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002569}
2570
2571inline void requestRoutesSystemDumpService(App& app)
2572{
2573 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002574 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002575 .methods(boost::beast::http::verb::get)(std::bind_front(
2576 handleLogServicesDumpServiceGet, std::ref(app), "System"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002577}
2578
2579inline void requestRoutesSystemDumpEntryCollection(App& app)
2580{
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002581 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002582 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002583 .methods(boost::beast::http::verb::get)(
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002584 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2585 std::ref(app), "System"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002586}
2587
2588inline void requestRoutesSystemDumpEntry(App& app)
2589{
2590 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002591 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002592 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002593 .methods(boost::beast::http::verb::get)(std::bind_front(
2594 handleLogServicesDumpEntryGet, std::ref(app), "System"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002595
2596 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002597 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002598 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002599 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2600 handleLogServicesDumpEntryDelete, std::ref(app), "System"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002601}
2602
2603inline void requestRoutesSystemDumpCreate(App& app)
2604{
George Liu0fda0f12021-11-16 10:06:17 +08002605 BMCWEB_ROUTE(
2606 app,
2607 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002608 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002609 .methods(boost::beast::http::verb::post)(
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002610 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2611 std::ref(app), "System"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002612}
2613
2614inline void requestRoutesSystemDumpClear(App& app)
2615{
George Liu0fda0f12021-11-16 10:06:17 +08002616 BMCWEB_ROUTE(
2617 app,
2618 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002619 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07002620 .methods(boost::beast::http::verb::post)(std::bind_front(
2621 handleLogServicesDumpClearLogPost, std::ref(app), "System"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002622}
2623
2624inline void requestRoutesCrashdumpService(App& app)
2625{
2626 // Note: Deviated from redfish privilege registry for GET & HEAD
2627 // method for security reasons.
2628 /**
2629 * Functions triggers appropriate requests on DBus
2630 */
2631 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002632 // This is incorrect, should be:
2633 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002634 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002635 .methods(boost::beast::http::verb::get)(
2636 [&app](const crow::Request& req,
2637 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002638 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002639 {
2640 return;
2641 }
2642 // Copy over the static data to include the entries added by
2643 // SubRoute
2644 asyncResp->res.jsonValue["@odata.id"] =
2645 "/redfish/v1/Systems/system/LogServices/Crashdump";
2646 asyncResp->res.jsonValue["@odata.type"] =
2647 "#LogService.v1_2_0.LogService";
2648 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2649 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2650 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2651 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2652 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302653
Ed Tanous002d39b2022-05-31 08:59:27 -07002654 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002655 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002656 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2657 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2658 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302659
Ed Tanous002d39b2022-05-31 08:59:27 -07002660 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2661 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2662 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2663 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2664 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2665 ["target"] =
2666 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002667 });
2668}
2669
2670void inline requestRoutesCrashdumpClear(App& app)
2671{
George Liu0fda0f12021-11-16 10:06:17 +08002672 BMCWEB_ROUTE(
2673 app,
2674 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002675 // This is incorrect, should be:
2676 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002677 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002678 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002679 [&app](const crow::Request& req,
2680 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002681 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002682 {
2683 return;
2684 }
2685 crow::connections::systemBus->async_method_call(
2686 [asyncResp](const boost::system::error_code ec,
2687 const std::string&) {
2688 if (ec)
2689 {
2690 messages::internalError(asyncResp->res);
2691 return;
2692 }
2693 messages::success(asyncResp->res);
2694 },
2695 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
2696 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002697}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002698
zhanghch058d1b46d2021-04-01 11:18:24 +08002699static void
2700 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2701 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002702{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002703 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08002704 [asyncResp, logID,
2705 &logEntryJson](const boost::system::error_code ec,
2706 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002707 if (ec)
2708 {
2709 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2710 if (ec.value() ==
2711 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002712 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002713 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002714 }
2715 else
2716 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002717 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002718 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002719 return;
2720 }
2721
2722 std::string timestamp{};
2723 std::string filename{};
2724 std::string logfile{};
2725 parseCrashdumpParameters(params, filename, timestamp, logfile);
2726
2727 if (filename.empty() || timestamp.empty())
2728 {
2729 messages::resourceMissingAtURI(asyncResp->res,
2730 crow::utility::urlFromPieces(logID));
2731 return;
2732 }
2733
2734 std::string crashdumpURI =
2735 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2736 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07002737 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05002738 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07002739 logEntry["@odata.id"] =
2740 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
2741 logEntry["Name"] = "CPU Crashdump";
2742 logEntry["Id"] = logID;
2743 logEntry["EntryType"] = "Oem";
2744 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
2745 logEntry["DiagnosticDataType"] = "OEM";
2746 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
2747 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002748
2749 // If logEntryJson references an array of LogEntry resources
2750 // ('Members' list), then push this as a new entry, otherwise set it
2751 // directly
2752 if (logEntryJson.is_array())
2753 {
2754 logEntryJson.push_back(logEntry);
2755 asyncResp->res.jsonValue["Members@odata.count"] =
2756 logEntryJson.size();
2757 }
2758 else
2759 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07002760 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002761 }
2762 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002763 sdbusplus::asio::getAllProperties(
2764 *crow::connections::systemBus, crashdumpObject,
2765 crashdumpPath + std::string("/") + logID, crashdumpInterface,
2766 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07002767}
2768
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002769inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002770{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002771 // Note: Deviated from redfish privilege registry for GET & HEAD
2772 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002773 /**
2774 * Functions triggers appropriate requests on DBus
2775 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002776 BMCWEB_ROUTE(app,
2777 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002778 // This is incorrect, should be.
2779 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002780 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002781 .methods(boost::beast::http::verb::get)(
2782 [&app](const crow::Request& req,
2783 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002784 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002785 {
2786 return;
2787 }
2788 crow::connections::systemBus->async_method_call(
2789 [asyncResp](const boost::system::error_code ec,
2790 const std::vector<std::string>& resp) {
2791 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002792 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002793 if (ec.value() !=
2794 boost::system::errc::no_such_file_or_directory)
2795 {
2796 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2797 << ec.message();
2798 messages::internalError(asyncResp->res);
2799 return;
2800 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002801 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002802 asyncResp->res.jsonValue["@odata.type"] =
2803 "#LogEntryCollection.LogEntryCollection";
2804 asyncResp->res.jsonValue["@odata.id"] =
2805 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2806 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2807 asyncResp->res.jsonValue["Description"] =
2808 "Collection of Crashdump Entries";
2809 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
2810 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002811
Ed Tanous002d39b2022-05-31 08:59:27 -07002812 for (const std::string& path : resp)
2813 {
2814 const sdbusplus::message::object_path objPath(path);
2815 // Get the log ID
2816 std::string logID = objPath.filename();
2817 if (logID.empty())
2818 {
2819 continue;
2820 }
2821 // Add the log entry to the array
2822 logCrashdumpEntry(asyncResp, logID,
2823 asyncResp->res.jsonValue["Members"]);
2824 }
2825 },
2826 "xyz.openbmc_project.ObjectMapper",
2827 "/xyz/openbmc_project/object_mapper",
2828 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2829 std::array<const char*, 1>{crashdumpInterface});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002830 });
2831}
Ed Tanous1da66f72018-07-27 16:13:37 -07002832
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002833inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002834{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002835 // Note: Deviated from redfish privilege registry for GET & HEAD
2836 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002837
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002838 BMCWEB_ROUTE(
2839 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002840 // this is incorrect, should be
2841 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002842 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002843 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002844 [&app](const crow::Request& req,
2845 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2846 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002847 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002848 {
2849 return;
2850 }
2851 const std::string& logID = param;
2852 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2853 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002854}
Ed Tanous1da66f72018-07-27 16:13:37 -07002855
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002856inline void requestRoutesCrashdumpFile(App& app)
2857{
2858 // Note: Deviated from redfish privilege registry for GET & HEAD
2859 // method for security reasons.
2860 BMCWEB_ROUTE(
2861 app,
2862 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002863 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002864 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00002865 [](const crow::Request& req,
2866 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2867 const std::string& logID, const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00002868 // Do not call getRedfishRoute here since the crashdump file is not a
2869 // Redfish resource.
Ed Tanous002d39b2022-05-31 08:59:27 -07002870 auto getStoredLogCallback =
2871 [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
2872 const boost::system::error_code ec,
2873 const std::vector<
2874 std::pair<std::string, dbus::utility::DbusVariantType>>&
2875 resp) {
2876 if (ec)
2877 {
2878 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2879 messages::internalError(asyncResp->res);
2880 return;
2881 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002882
Ed Tanous002d39b2022-05-31 08:59:27 -07002883 std::string dbusFilename{};
2884 std::string dbusTimestamp{};
2885 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002886
Ed Tanous002d39b2022-05-31 08:59:27 -07002887 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2888 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002889
Ed Tanous002d39b2022-05-31 08:59:27 -07002890 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2891 dbusFilepath.empty())
2892 {
2893 messages::resourceMissingAtURI(asyncResp->res, url);
2894 return;
2895 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002896
Ed Tanous002d39b2022-05-31 08:59:27 -07002897 // Verify the file name parameter is correct
2898 if (fileName != dbusFilename)
2899 {
2900 messages::resourceMissingAtURI(asyncResp->res, url);
2901 return;
2902 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002903
Ed Tanous002d39b2022-05-31 08:59:27 -07002904 if (!std::filesystem::exists(dbusFilepath))
2905 {
2906 messages::resourceMissingAtURI(asyncResp->res, url);
2907 return;
2908 }
2909 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
2910 asyncResp->res.body() =
2911 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002912
Ed Tanous002d39b2022-05-31 08:59:27 -07002913 // Configure this to be a file download when accessed
2914 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07002915 asyncResp->res.addHeader(
2916 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07002917 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002918 sdbusplus::asio::getAllProperties(
2919 *crow::connections::systemBus, crashdumpObject,
2920 crashdumpPath + std::string("/") + logID, crashdumpInterface,
2921 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07002922 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002923}
2924
Jason M. Billsc5a4c822022-01-06 15:51:23 -08002925enum class OEMDiagnosticType
2926{
2927 onDemand,
2928 telemetry,
2929 invalid,
2930};
2931
Ed Tanousf7725d72022-03-07 12:46:00 -08002932inline OEMDiagnosticType
2933 getOEMDiagnosticType(const std::string_view& oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08002934{
2935 if (oemDiagStr == "OnDemand")
2936 {
2937 return OEMDiagnosticType::onDemand;
2938 }
2939 if (oemDiagStr == "Telemetry")
2940 {
2941 return OEMDiagnosticType::telemetry;
2942 }
2943
2944 return OEMDiagnosticType::invalid;
2945}
2946
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002947inline void requestRoutesCrashdumpCollect(App& app)
2948{
2949 // Note: Deviated from redfish privilege registry for GET & HEAD
2950 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08002951 BMCWEB_ROUTE(
2952 app,
2953 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002954 // The below is incorrect; Should be ConfigureManager
2955 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002956 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002957 .methods(boost::beast::http::verb::post)(
2958 [&app](const crow::Request& req,
2959 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002960 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002961 {
2962 return;
2963 }
2964 std::string diagnosticDataType;
2965 std::string oemDiagnosticDataType;
2966 if (!redfish::json_util::readJsonAction(
2967 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
2968 "OEMDiagnosticDataType", oemDiagnosticDataType))
2969 {
2970 return;
2971 }
2972
2973 if (diagnosticDataType != "OEM")
2974 {
2975 BMCWEB_LOG_ERROR
2976 << "Only OEM DiagnosticDataType supported for Crashdump";
2977 messages::actionParameterValueFormatError(
2978 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2979 "CollectDiagnosticData");
2980 return;
2981 }
2982
2983 OEMDiagnosticType oemDiagType =
2984 getOEMDiagnosticType(oemDiagnosticDataType);
2985
2986 std::string iface;
2987 std::string method;
2988 std::string taskMatchStr;
2989 if (oemDiagType == OEMDiagnosticType::onDemand)
2990 {
2991 iface = crashdumpOnDemandInterface;
2992 method = "GenerateOnDemandLog";
2993 taskMatchStr = "type='signal',"
2994 "interface='org.freedesktop.DBus.Properties',"
2995 "member='PropertiesChanged',"
2996 "arg0namespace='com.intel.crashdump'";
2997 }
2998 else if (oemDiagType == OEMDiagnosticType::telemetry)
2999 {
3000 iface = crashdumpTelemetryInterface;
3001 method = "GenerateTelemetryLog";
3002 taskMatchStr = "type='signal',"
3003 "interface='org.freedesktop.DBus.Properties',"
3004 "member='PropertiesChanged',"
3005 "arg0namespace='com.intel.crashdump'";
3006 }
3007 else
3008 {
3009 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3010 << oemDiagnosticDataType;
3011 messages::actionParameterValueFormatError(
3012 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3013 "CollectDiagnosticData");
3014 return;
3015 }
3016
3017 auto collectCrashdumpCallback =
3018 [asyncResp, payload(task::Payload(req)),
3019 taskMatchStr](const boost::system::error_code ec,
3020 const std::string&) mutable {
3021 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003022 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003023 if (ec.value() == boost::system::errc::operation_not_supported)
3024 {
3025 messages::resourceInStandby(asyncResp->res);
3026 }
3027 else if (ec.value() ==
3028 boost::system::errc::device_or_resource_busy)
3029 {
3030 messages::serviceTemporarilyUnavailable(asyncResp->res,
3031 "60");
3032 }
3033 else
3034 {
3035 messages::internalError(asyncResp->res);
3036 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003037 return;
3038 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003039 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -05003040 [](boost::system::error_code err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003041 const std::shared_ptr<task::TaskData>& taskData) {
3042 if (!err)
3043 {
3044 taskData->messages.emplace_back(messages::taskCompletedOK(
3045 std::to_string(taskData->index)));
3046 taskData->state = "Completed";
3047 }
3048 return task::completed;
3049 },
3050 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003051
Ed Tanous002d39b2022-05-31 08:59:27 -07003052 task->startTimer(std::chrono::minutes(5));
3053 task->populateResp(asyncResp->res);
3054 task->payload.emplace(std::move(payload));
3055 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003056
Ed Tanous002d39b2022-05-31 08:59:27 -07003057 crow::connections::systemBus->async_method_call(
3058 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3059 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003060 });
3061}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003062
Andrew Geisslercb92c032018-08-17 07:56:14 -07003063/**
3064 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3065 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003066inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003067{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003068 /**
3069 * Function handles POST method request.
3070 * The Clear Log actions does not require any parameter.The action deletes
3071 * all entries found in the Entries collection for this Log Service.
3072 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003073
George Liu0fda0f12021-11-16 10:06:17 +08003074 BMCWEB_ROUTE(
3075 app,
3076 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003077 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003078 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003079 [&app](const crow::Request& req,
3080 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003081 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003082 {
3083 return;
3084 }
3085 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003086
Ed Tanous002d39b2022-05-31 08:59:27 -07003087 // Process response from Logging service.
3088 auto respHandler = [asyncResp](const boost::system::error_code ec) {
3089 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3090 if (ec)
3091 {
3092 // TODO Handle for specific error code
3093 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3094 asyncResp->res.result(
3095 boost::beast::http::status::internal_server_error);
3096 return;
3097 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003098
Ed Tanous002d39b2022-05-31 08:59:27 -07003099 asyncResp->res.result(boost::beast::http::status::no_content);
3100 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003101
Ed Tanous002d39b2022-05-31 08:59:27 -07003102 // Make call to Logging service to request Clear Log
3103 crow::connections::systemBus->async_method_call(
3104 respHandler, "xyz.openbmc_project.Logging",
3105 "/xyz/openbmc_project/logging",
3106 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3107 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003108}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003109
3110/****************************************************
3111 * Redfish PostCode interfaces
3112 * using DBUS interface: getPostCodesTS
3113 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003114inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003115{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003116 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003117 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003118 .methods(boost::beast::http::verb::get)(
3119 [&app](const crow::Request& req,
3120 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003121 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003122 {
3123 return;
3124 }
Ed Tanous14766872022-03-15 10:44:42 -07003125
Ed Tanous002d39b2022-05-31 08:59:27 -07003126 asyncResp->res.jsonValue["@odata.id"] =
3127 "/redfish/v1/Systems/system/LogServices/PostCodes";
3128 asyncResp->res.jsonValue["@odata.type"] =
3129 "#LogService.v1_1_0.LogService";
3130 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3131 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3132 asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
3133 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3134 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3135 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303136
Ed Tanous002d39b2022-05-31 08:59:27 -07003137 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003138 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003139 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3140 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3141 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303142
Ed Tanous002d39b2022-05-31 08:59:27 -07003143 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3144 {"target",
3145 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003146 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003147}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003148
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003149inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003150{
George Liu0fda0f12021-11-16 10:06:17 +08003151 BMCWEB_ROUTE(
3152 app,
3153 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003154 // The following privilege is incorrect; It should be ConfigureManager
3155 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003156 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003157 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003158 [&app](const crow::Request& req,
3159 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003160 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003161 {
3162 return;
3163 }
3164 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003165
Ed Tanous002d39b2022-05-31 08:59:27 -07003166 // Make call to post-code service to request clear all
3167 crow::connections::systemBus->async_method_call(
3168 [asyncResp](const boost::system::error_code ec) {
3169 if (ec)
3170 {
3171 // TODO Handle for specific error code
3172 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3173 << ec;
3174 asyncResp->res.result(
3175 boost::beast::http::status::internal_server_error);
3176 messages::internalError(asyncResp->res);
3177 return;
3178 }
3179 },
3180 "xyz.openbmc_project.State.Boot.PostCode0",
3181 "/xyz/openbmc_project/State/Boot/PostCode0",
3182 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3183 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003184}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003185
3186static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003187 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303188 const boost::container::flat_map<
3189 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003190 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3191 const uint64_t skip = 0, const uint64_t top = 0)
3192{
3193 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003194 const registries::Message* message =
3195 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003196
3197 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003198 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003199
3200 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303201 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3202 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003203 {
3204 currentCodeIndex++;
3205 std::string postcodeEntryID =
3206 "B" + std::to_string(bootIndex) + "-" +
3207 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3208
3209 uint64_t usecSinceEpoch = code.first;
3210 uint64_t usTimeOffset = 0;
3211
3212 if (1 == currentCodeIndex)
3213 { // already incremented
3214 firstCodeTimeUs = code.first;
3215 }
3216 else
3217 {
3218 usTimeOffset = code.first - firstCodeTimeUs;
3219 }
3220
3221 // skip if no specific codeIndex is specified and currentCodeIndex does
3222 // not fall between top and skip
3223 if ((codeIndex == 0) &&
3224 (currentCodeIndex <= skip || currentCodeIndex > top))
3225 {
3226 continue;
3227 }
3228
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003229 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003230 // currentIndex
3231 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3232 {
3233 // This is done for simplicity. 1st entry is needed to calculate
3234 // time offset. To improve efficiency, one can get to the entry
3235 // directly (possibly with flatmap's nth method)
3236 continue;
3237 }
3238
3239 // currentCodeIndex is within top and skip or equal to specified code
3240 // index
3241
3242 // Get the Created time from the timestamp
3243 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003244 entryTimeStr =
Ed Tanous2b829372022-08-03 14:22:34 -07003245 redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003246
3247 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3248 std::ostringstream hexCode;
3249 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303250 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003251 std::ostringstream timeOffsetStr;
3252 // Set Fixed -Point Notation
3253 timeOffsetStr << std::fixed;
3254 // Set precision to 4 digits
3255 timeOffsetStr << std::setprecision(4);
3256 // Add double to stream
3257 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3258 std::vector<std::string> messageArgs = {
3259 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3260
3261 // Get MessageArgs template from message registry
3262 std::string msg;
3263 if (message != nullptr)
3264 {
3265 msg = message->message;
3266
3267 // fill in this post code value
3268 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003269 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003270 {
3271 std::string argStr = "%" + std::to_string(++i);
3272 size_t argPos = msg.find(argStr);
3273 if (argPos != std::string::npos)
3274 {
3275 msg.replace(argPos, argStr.length(), messageArg);
3276 }
3277 }
3278 }
3279
Tim Leed4342a92020-04-27 11:47:58 +08003280 // Get Severity template from message registry
3281 std::string severity;
3282 if (message != nullptr)
3283 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003284 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003285 }
3286
ZhikuiRena3316fc2020-01-29 14:58:08 -08003287 // add to AsyncResp
3288 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003289 nlohmann::json& bmcLogEntry = logEntryArray.back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05003290 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Jason M. Bills84afc482022-06-24 12:38:23 -07003291 bmcLogEntry["@odata.id"] =
3292 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3293 postcodeEntryID;
3294 bmcLogEntry["Name"] = "POST Code Log Entry";
3295 bmcLogEntry["Id"] = postcodeEntryID;
3296 bmcLogEntry["Message"] = std::move(msg);
3297 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3298 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3299 bmcLogEntry["EntryType"] = "Event";
3300 bmcLogEntry["Severity"] = std::move(severity);
3301 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003302 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3303 {
3304 bmcLogEntry["AdditionalDataURI"] =
3305 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3306 postcodeEntryID + "/attachment";
3307 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003308 }
3309}
3310
zhanghch058d1b46d2021-04-01 11:18:24 +08003311static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003312 const uint16_t bootIndex,
3313 const uint64_t codeIndex)
3314{
3315 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303316 [aResp, bootIndex,
3317 codeIndex](const boost::system::error_code ec,
3318 const boost::container::flat_map<
3319 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3320 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003321 if (ec)
3322 {
3323 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3324 messages::internalError(aResp->res);
3325 return;
3326 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003327
Ed Tanous002d39b2022-05-31 08:59:27 -07003328 // skip the empty postcode boots
3329 if (postcode.empty())
3330 {
3331 return;
3332 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003333
Ed Tanous002d39b2022-05-31 08:59:27 -07003334 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003335
Ed Tanous002d39b2022-05-31 08:59:27 -07003336 aResp->res.jsonValue["Members@odata.count"] =
3337 aResp->res.jsonValue["Members"].size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08003338 },
Jonathan Doman15124762021-01-07 17:54:17 -08003339 "xyz.openbmc_project.State.Boot.PostCode0",
3340 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003341 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3342 bootIndex);
3343}
3344
zhanghch058d1b46d2021-04-01 11:18:24 +08003345static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003346 const uint16_t bootIndex,
3347 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003348 const uint64_t entryCount, size_t skip,
3349 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003350{
3351 crow::connections::systemBus->async_method_call(
3352 [aResp, bootIndex, bootCount, entryCount, skip,
3353 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303354 const boost::container::flat_map<
3355 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3356 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003357 if (ec)
3358 {
3359 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3360 messages::internalError(aResp->res);
3361 return;
3362 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003363
Ed Tanous002d39b2022-05-31 08:59:27 -07003364 uint64_t endCount = entryCount;
3365 if (!postcode.empty())
3366 {
3367 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003368 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003369 {
Ed Tanous3648c8b2022-07-25 13:39:59 -07003370 uint64_t thisBootSkip =
3371 std::max(static_cast<uint64_t>(skip), entryCount) -
3372 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003373 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003374 std::min(static_cast<uint64_t>(top + skip), endCount) -
3375 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003376
3377 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3378 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003379 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003380 aResp->res.jsonValue["Members@odata.count"] = endCount;
3381 }
3382
3383 // continue to previous bootIndex
3384 if (bootIndex < bootCount)
3385 {
3386 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3387 bootCount, endCount, skip, top);
3388 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003389 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003390 {
3391 aResp->res.jsonValue["Members@odata.nextLink"] =
3392 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3393 std::to_string(skip + top);
3394 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003395 },
Jonathan Doman15124762021-01-07 17:54:17 -08003396 "xyz.openbmc_project.State.Boot.PostCode0",
3397 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003398 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3399 bootIndex);
3400}
3401
zhanghch058d1b46d2021-04-01 11:18:24 +08003402static void
3403 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003404 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003405{
3406 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003407 sdbusplus::asio::getProperty<uint16_t>(
3408 *crow::connections::systemBus,
3409 "xyz.openbmc_project.State.Boot.PostCode0",
3410 "/xyz/openbmc_project/State/Boot/PostCode0",
3411 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3412 [aResp, entryCount, skip, top](const boost::system::error_code ec,
3413 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003414 if (ec)
3415 {
3416 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3417 messages::internalError(aResp->res);
3418 return;
3419 }
3420 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003421 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003422}
3423
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003424inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003425{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003426 BMCWEB_ROUTE(app,
3427 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003428 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003429 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003430 [&app](const crow::Request& req,
3431 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003432 query_param::QueryCapabilities capabilities = {
3433 .canDelegateTop = true,
3434 .canDelegateSkip = true,
3435 };
3436 query_param::Query delegatedQuery;
3437 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003438 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003439 {
3440 return;
3441 }
3442 asyncResp->res.jsonValue["@odata.type"] =
3443 "#LogEntryCollection.LogEntryCollection";
3444 asyncResp->res.jsonValue["@odata.id"] =
3445 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3446 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3447 asyncResp->res.jsonValue["Description"] =
3448 "Collection of POST Code Log Entries";
3449 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3450 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003451 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003452 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003453 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003454 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003455}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003456
George Liu647b3cd2021-07-05 12:43:56 +08003457/**
3458 * @brief Parse post code ID and get the current value and index value
3459 * eg: postCodeID=B1-2, currentValue=1, index=2
3460 *
3461 * @param[in] postCodeID Post Code ID
3462 * @param[out] currentValue Current value
3463 * @param[out] index Index value
3464 *
3465 * @return bool true if the parsing is successful, false the parsing fails
3466 */
3467inline static bool parsePostCode(const std::string& postCodeID,
3468 uint64_t& currentValue, uint16_t& index)
3469{
3470 std::vector<std::string> split;
3471 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3472 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3473 {
3474 return false;
3475 }
3476
Ed Tanousca45aa32022-01-07 09:28:45 -08003477 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003478 const char* start = split[0].data() + 1;
Ed Tanousca45aa32022-01-07 09:28:45 -08003479 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003480 const char* end = split[0].data() + split[0].size();
3481 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3482
3483 if (ptrIndex != end || ecIndex != std::errc())
3484 {
3485 return false;
3486 }
3487
3488 start = split[1].data();
Ed Tanousca45aa32022-01-07 09:28:45 -08003489
3490 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003491 end = split[1].data() + split[1].size();
3492 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
George Liu647b3cd2021-07-05 12:43:56 +08003493
Tony Lee517d9a52022-06-28 15:41:23 +08003494 return ptrValue == end && ecValue == std::errc();
George Liu647b3cd2021-07-05 12:43:56 +08003495}
3496
3497inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3498{
George Liu0fda0f12021-11-16 10:06:17 +08003499 BMCWEB_ROUTE(
3500 app,
3501 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003502 .privileges(redfish::privileges::getLogEntry)
3503 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003504 [&app](const crow::Request& req,
3505 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3506 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003507 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003508 {
3509 return;
3510 }
Ed Tanous99351cd2022-08-07 16:42:51 -07003511 if (http_helpers::isContentTypeAllowed(
3512 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07003513 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07003514 {
3515 asyncResp->res.result(boost::beast::http::status::bad_request);
3516 return;
3517 }
George Liu647b3cd2021-07-05 12:43:56 +08003518
Ed Tanous002d39b2022-05-31 08:59:27 -07003519 uint64_t currentValue = 0;
3520 uint16_t index = 0;
3521 if (!parsePostCode(postCodeID, currentValue, index))
3522 {
3523 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3524 return;
3525 }
George Liu647b3cd2021-07-05 12:43:56 +08003526
Ed Tanous002d39b2022-05-31 08:59:27 -07003527 crow::connections::systemBus->async_method_call(
3528 [asyncResp, postCodeID, currentValue](
3529 const boost::system::error_code ec,
3530 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3531 postcodes) {
3532 if (ec.value() == EBADR)
3533 {
3534 messages::resourceNotFound(asyncResp->res, "LogEntry",
3535 postCodeID);
3536 return;
3537 }
3538 if (ec)
3539 {
3540 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3541 messages::internalError(asyncResp->res);
3542 return;
3543 }
George Liu647b3cd2021-07-05 12:43:56 +08003544
Ed Tanous002d39b2022-05-31 08:59:27 -07003545 size_t value = static_cast<size_t>(currentValue) - 1;
3546 if (value == std::string::npos || postcodes.size() < currentValue)
3547 {
3548 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3549 messages::resourceNotFound(asyncResp->res, "LogEntry",
3550 postCodeID);
3551 return;
3552 }
George Liu647b3cd2021-07-05 12:43:56 +08003553
Ed Tanous002d39b2022-05-31 08:59:27 -07003554 const auto& [tID, c] = postcodes[value];
3555 if (c.empty())
3556 {
3557 BMCWEB_LOG_INFO << "No found post code data";
3558 messages::resourceNotFound(asyncResp->res, "LogEntry",
3559 postCodeID);
3560 return;
3561 }
3562 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3563 const char* d = reinterpret_cast<const char*>(c.data());
3564 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08003565
Ed Tanousd9f6c622022-03-17 09:12:17 -07003566 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07003567 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07003568 asyncResp->res.addHeader(
3569 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07003570 asyncResp->res.body() = crow::utility::base64encode(strData);
3571 },
3572 "xyz.openbmc_project.State.Boot.PostCode0",
3573 "/xyz/openbmc_project/State/Boot/PostCode0",
3574 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3575 });
George Liu647b3cd2021-07-05 12:43:56 +08003576}
3577
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003578inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003579{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003580 BMCWEB_ROUTE(
3581 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003582 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003583 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003584 [&app](const crow::Request& req,
3585 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3586 const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003587 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003588 {
3589 return;
3590 }
3591 uint16_t bootIndex = 0;
3592 uint64_t codeIndex = 0;
3593 if (!parsePostCode(targetID, codeIndex, bootIndex))
3594 {
3595 // Requested ID was not found
3596 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3597 return;
3598 }
3599 if (bootIndex == 0 || codeIndex == 0)
3600 {
3601 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3602 << targetID;
3603 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003604
Vijay Lobo9c11a172021-10-07 16:53:16 -05003605 asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanous002d39b2022-05-31 08:59:27 -07003606 asyncResp->res.jsonValue["@odata.id"] =
3607 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3608 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3609 asyncResp->res.jsonValue["Description"] =
3610 "Collection of POST Code Log Entries";
3611 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3612 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003613
Ed Tanous002d39b2022-05-31 08:59:27 -07003614 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3615 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003616}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003617
Ed Tanous1da66f72018-07-27 16:13:37 -07003618} // namespace redfish