blob: faa295adc14cce41cd53c38ac7f1c372d17ce27f [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
499 thisEntry["@odata.type"] = "#LogEntry.v1_8_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"] =
591 "#LogEntry.v1_8_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"];
975 logServiceArrayLocal.push_back(
976 {{"@odata.id",
977 "/redfish/v1/Systems/system/LogServices/PostCodes"}});
978 asyncResp->res.jsonValue["Members@odata.count"] =
979 logServiceArrayLocal.size();
980 return;
981 }
982 }
983 },
984 "xyz.openbmc_project.ObjectMapper",
985 "/xyz/openbmc_project/object_mapper",
986 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
987 std::array<const char*, 1>{postCodeIface});
Ed Tanous45ca1b82022-03-25 13:07:27 -0700988 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700989}
990
991inline void requestRoutesEventLogService(App& app)
992{
993 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -0700994 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700995 .methods(boost::beast::http::verb::get)(
996 [&app](const crow::Request& req,
997 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000998 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700999 {
1000 return;
1001 }
1002 asyncResp->res.jsonValue["@odata.id"] =
1003 "/redfish/v1/Systems/system/LogServices/EventLog";
1004 asyncResp->res.jsonValue["@odata.type"] =
1005 "#LogService.v1_1_0.LogService";
1006 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1007 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1008 asyncResp->res.jsonValue["Id"] = "EventLog";
1009 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301010
Ed Tanous002d39b2022-05-31 08:59:27 -07001011 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001012 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301013
Ed Tanous002d39b2022-05-31 08:59:27 -07001014 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1015 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1016 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301017
Ed Tanous002d39b2022-05-31 08:59:27 -07001018 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1019 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1020 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001021
Ed Tanous002d39b2022-05-31 08:59:27 -07001022 {"target",
1023 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001024 });
1025}
1026
1027inline void requestRoutesJournalEventLogClear(App& app)
1028{
Jason M. Bills4978b632022-02-22 14:17:43 -08001029 BMCWEB_ROUTE(
1030 app,
1031 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001032 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001033 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001034 [&app](const crow::Request& req,
1035 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001036 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001037 {
1038 return;
1039 }
1040 // Clear the EventLog by deleting the log files
1041 std::vector<std::filesystem::path> redfishLogFiles;
1042 if (getRedfishLogFiles(redfishLogFiles))
1043 {
1044 for (const std::filesystem::path& file : redfishLogFiles)
1045 {
1046 std::error_code ec;
1047 std::filesystem::remove(file, ec);
1048 }
1049 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001050
Ed Tanous002d39b2022-05-31 08:59:27 -07001051 // Reload rsyslog so it knows to start new log files
1052 crow::connections::systemBus->async_method_call(
1053 [asyncResp](const boost::system::error_code ec) {
1054 if (ec)
1055 {
1056 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1057 messages::internalError(asyncResp->res);
1058 return;
1059 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001060
Ed Tanous002d39b2022-05-31 08:59:27 -07001061 messages::success(asyncResp->res);
1062 },
1063 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1064 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1065 "replace");
1066 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001067}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001068
Jason M. Billsac992cd2022-06-24 13:31:46 -07001069enum class LogParseError
1070{
1071 success,
1072 parseFailed,
1073 messageIdNotInRegistry,
1074};
1075
1076static LogParseError
1077 fillEventLogEntryJson(const std::string& logEntryID,
1078 const std::string& logEntry,
1079 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001080{
Jason M. Bills95820182019-04-22 16:25:34 -07001081 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001082 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001083 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001084 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001085 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001086 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001087 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001088 std::string timestamp = logEntry.substr(0, space);
1089 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001090 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001091 if (entryStart == std::string::npos)
1092 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001093 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001094 }
1095 std::string_view entry(logEntry);
1096 entry.remove_prefix(entryStart);
1097 // Use split to separate the entry into its fields
1098 std::vector<std::string> logEntryFields;
1099 boost::split(logEntryFields, entry, boost::is_any_of(","),
1100 boost::token_compress_on);
1101 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001102 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001103 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001104 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001105 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001106 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001107
Jason M. Bills4851d452019-03-28 11:27:48 -07001108 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001109 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001110
Sui Chen54417b02022-03-24 14:59:52 -07001111 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001112 {
Sui Chen54417b02022-03-24 14:59:52 -07001113 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001114 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001115 }
1116
Sui Chen54417b02022-03-24 14:59:52 -07001117 std::string msg = message->message;
1118
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001119 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001120 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001121 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001122 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001123 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001124 // If the first string is empty, assume there are no MessageArgs
1125 std::size_t messageArgsSize = 0;
1126 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001127 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001128 messageArgsSize = logEntryFields.size() - 1;
1129 }
1130
Ed Tanous23a21a12020-07-25 04:45:05 +00001131 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001132
1133 // Fill the MessageArgs into the Message
1134 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001135 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001136 {
1137 std::string argStr = "%" + std::to_string(++i);
1138 size_t argPos = msg.find(argStr);
1139 if (argPos != std::string::npos)
1140 {
1141 msg.replace(argPos, argStr.length(), messageArg);
1142 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001143 }
1144 }
1145
Jason M. Bills95820182019-04-22 16:25:34 -07001146 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1147 // format which matches the Redfish format except for the fractional seconds
1148 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001149 std::size_t dot = timestamp.find_first_of('.');
1150 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001151 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001152 {
Jason M. Bills95820182019-04-22 16:25:34 -07001153 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001154 }
1155
1156 // Fill in the log entry with the gathered data
Jason M. Bills84afc482022-06-24 12:38:23 -07001157 logEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1158 logEntryJson["@odata.id"] =
1159 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
1160 logEntryJson["Name"] = "System Event Log Entry";
1161 logEntryJson["Id"] = logEntryID;
1162 logEntryJson["Message"] = std::move(msg);
1163 logEntryJson["MessageId"] = std::move(messageID);
1164 logEntryJson["MessageArgs"] = messageArgs;
1165 logEntryJson["EntryType"] = "Event";
1166 logEntryJson["Severity"] = message->messageSeverity;
1167 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001168 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001169}
1170
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001171inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001172{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001173 BMCWEB_ROUTE(app,
1174 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001175 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001176 .methods(boost::beast::http::verb::get)(
1177 [&app](const crow::Request& req,
1178 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1179 query_param::QueryCapabilities capabilities = {
1180 .canDelegateTop = true,
1181 .canDelegateSkip = true,
1182 };
1183 query_param::Query delegatedQuery;
1184 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001185 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001186 {
1187 return;
1188 }
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001189 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001190 size_t skip = delegatedQuery.skip.value_or(0);
1191
Ed Tanous002d39b2022-05-31 08:59:27 -07001192 // Collections don't include the static data added by SubRoute
1193 // because it has a duplicate entry for members
1194 asyncResp->res.jsonValue["@odata.type"] =
1195 "#LogEntryCollection.LogEntryCollection";
1196 asyncResp->res.jsonValue["@odata.id"] =
1197 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1198 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1199 asyncResp->res.jsonValue["Description"] =
1200 "Collection of System Event Log Entries";
1201
1202 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1203 logEntryArray = nlohmann::json::array();
1204 // Go through the log files and create a unique ID for each
1205 // entry
1206 std::vector<std::filesystem::path> redfishLogFiles;
1207 getRedfishLogFiles(redfishLogFiles);
1208 uint64_t entryCount = 0;
1209 std::string logEntry;
1210
1211 // Oldest logs are in the last file, so start there and loop
1212 // backwards
1213 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1214 it++)
1215 {
1216 std::ifstream logStream(*it);
1217 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001218 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001219 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001220 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001221
Ed Tanous002d39b2022-05-31 08:59:27 -07001222 // Reset the unique ID on the first entry
1223 bool firstEntry = true;
1224 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001225 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 std::string idStr;
1227 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001228 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001229 continue;
1230 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001231 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001232
Jason M. Billsde703c52022-06-23 14:19:04 -07001233 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001234 LogParseError status =
1235 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1236 if (status == LogParseError::messageIdNotInRegistry)
1237 {
1238 continue;
1239 }
1240 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001241 {
1242 messages::internalError(asyncResp->res);
1243 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001244 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001245
Jason M. Billsde703c52022-06-23 14:19:04 -07001246 entryCount++;
1247 // Handle paging using skip (number of entries to skip from the
1248 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001249 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001250 {
1251 continue;
1252 }
1253
1254 logEntryArray.push_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001255 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001256 }
1257 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001258 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001259 {
1260 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1261 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001262 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001263 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001264 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001265}
Chicago Duan336e96c2019-07-15 14:22:08 +08001266
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001267inline void requestRoutesJournalEventLogEntry(App& app)
1268{
1269 BMCWEB_ROUTE(
1270 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001271 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001272 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001273 [&app](const crow::Request& req,
1274 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1275 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001276 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001277 {
1278 return;
1279 }
1280 const std::string& targetID = param;
1281
1282 // Go through the log files and check the unique ID for each
1283 // entry to find the target entry
1284 std::vector<std::filesystem::path> redfishLogFiles;
1285 getRedfishLogFiles(redfishLogFiles);
1286 std::string logEntry;
1287
1288 // Oldest logs are in the last file, so start there and loop
1289 // backwards
1290 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1291 it++)
1292 {
1293 std::ifstream logStream(*it);
1294 if (!logStream.is_open())
1295 {
1296 continue;
1297 }
1298
1299 // Reset the unique ID on the first entry
1300 bool firstEntry = true;
1301 while (std::getline(logStream, logEntry))
1302 {
1303 std::string idStr;
1304 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001305 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001306 continue;
1307 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001308 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001309
1310 if (idStr == targetID)
1311 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001312 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001313 LogParseError status =
1314 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1315 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001316 {
1317 messages::internalError(asyncResp->res);
1318 return;
1319 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001320 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001321 return;
1322 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001323 }
1324 }
1325 // Requested ID was not found
1326 messages::resourceMissingAtURI(asyncResp->res,
1327 crow::utility::urlFromPieces(targetID));
1328 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001329}
1330
1331inline void requestRoutesDBusEventLogEntryCollection(App& app)
1332{
1333 BMCWEB_ROUTE(app,
1334 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001335 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001336 .methods(boost::beast::http::verb::get)(
1337 [&app](const crow::Request& req,
1338 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001339 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001340 {
1341 return;
1342 }
1343 // Collections don't include the static data added by SubRoute
1344 // because it has a duplicate entry for members
1345 asyncResp->res.jsonValue["@odata.type"] =
1346 "#LogEntryCollection.LogEntryCollection";
1347 asyncResp->res.jsonValue["@odata.id"] =
1348 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1349 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1350 asyncResp->res.jsonValue["Description"] =
1351 "Collection of System Event Log Entries";
1352
1353 // DBus implementation of EventLog/Entries
1354 // Make call to Logging Service to find all log entry objects
1355 crow::connections::systemBus->async_method_call(
1356 [asyncResp](const boost::system::error_code ec,
1357 const dbus::utility::ManagedObjectType& resp) {
1358 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001359 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001360 // TODO Handle for specific error code
1361 BMCWEB_LOG_ERROR
1362 << "getLogEntriesIfaceData resp_handler got error " << ec;
1363 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001364 return;
1365 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001366 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1367 entriesArray = nlohmann::json::array();
1368 for (const auto& objectPath : resp)
1369 {
1370 const uint32_t* id = nullptr;
1371 const uint64_t* timestamp = nullptr;
1372 const uint64_t* updateTimestamp = nullptr;
1373 const std::string* severity = nullptr;
1374 const std::string* message = nullptr;
1375 const std::string* filePath = nullptr;
1376 bool resolved = false;
1377 for (const auto& interfaceMap : objectPath.second)
1378 {
1379 if (interfaceMap.first ==
1380 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001381 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001382 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001383 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001384 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001385 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001386 id = std::get_if<uint32_t>(&propertyMap.second);
1387 }
1388 else if (propertyMap.first == "Timestamp")
1389 {
1390 timestamp =
1391 std::get_if<uint64_t>(&propertyMap.second);
1392 }
1393 else if (propertyMap.first == "UpdateTimestamp")
1394 {
1395 updateTimestamp =
1396 std::get_if<uint64_t>(&propertyMap.second);
1397 }
1398 else if (propertyMap.first == "Severity")
1399 {
1400 severity = std::get_if<std::string>(
1401 &propertyMap.second);
1402 }
1403 else if (propertyMap.first == "Message")
1404 {
1405 message = std::get_if<std::string>(
1406 &propertyMap.second);
1407 }
1408 else if (propertyMap.first == "Resolved")
1409 {
1410 const bool* resolveptr =
1411 std::get_if<bool>(&propertyMap.second);
1412 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001413 {
1414 messages::internalError(asyncResp->res);
1415 return;
1416 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001417 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001418 }
1419 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001420 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001421 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001422 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001423 messages::internalError(asyncResp->res);
1424 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001425 }
1426 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001427 else if (interfaceMap.first ==
1428 "xyz.openbmc_project.Common.FilePath")
1429 {
1430 for (const auto& propertyMap : interfaceMap.second)
1431 {
1432 if (propertyMap.first == "Path")
1433 {
1434 filePath = std::get_if<std::string>(
1435 &propertyMap.second);
1436 }
1437 }
1438 }
1439 }
1440 // Object path without the
1441 // xyz.openbmc_project.Logging.Entry interface, ignore
1442 // and continue.
1443 if (id == nullptr || message == nullptr ||
1444 severity == nullptr || timestamp == nullptr ||
1445 updateTimestamp == nullptr)
1446 {
1447 continue;
1448 }
1449 entriesArray.push_back({});
1450 nlohmann::json& thisEntry = entriesArray.back();
1451 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1452 thisEntry["@odata.id"] =
1453 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1454 std::to_string(*id);
1455 thisEntry["Name"] = "System Event Log Entry";
1456 thisEntry["Id"] = std::to_string(*id);
1457 thisEntry["Message"] = *message;
1458 thisEntry["Resolved"] = resolved;
1459 thisEntry["EntryType"] = "Event";
1460 thisEntry["Severity"] =
1461 translateSeverityDbusToRedfish(*severity);
1462 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001463 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001464 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001465 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001466 if (filePath != nullptr)
1467 {
1468 thisEntry["AdditionalDataURI"] =
1469 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1470 std::to_string(*id) + "/attachment";
1471 }
1472 }
1473 std::sort(
1474 entriesArray.begin(), entriesArray.end(),
1475 [](const nlohmann::json& left, const nlohmann::json& right) {
1476 return (left["Id"] <= right["Id"]);
1477 });
1478 asyncResp->res.jsonValue["Members@odata.count"] =
1479 entriesArray.size();
1480 },
1481 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1482 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001483 });
1484}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001485
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001486inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001487{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001488 BMCWEB_ROUTE(
1489 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001490 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001491 .methods(boost::beast::http::verb::get)(
1492 [&app](const crow::Request& req,
1493 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1494 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001495 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001496 {
1497 return;
1498 }
1499 std::string entryID = param;
1500 dbus::utility::escapePathForDbus(entryID);
1501
1502 // DBus implementation of EventLog/Entries
1503 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001504 sdbusplus::asio::getAllProperties(
1505 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1506 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous002d39b2022-05-31 08:59:27 -07001507 [asyncResp, entryID](const boost::system::error_code ec,
1508 const dbus::utility::DBusPropertiesMap& resp) {
1509 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001510 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001511 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1512 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001513 return;
1514 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001515 if (ec)
1516 {
1517 BMCWEB_LOG_ERROR
1518 << "EventLogEntry (DBus) resp_handler got error " << ec;
1519 messages::internalError(asyncResp->res);
1520 return;
1521 }
1522 const uint32_t* id = nullptr;
1523 const uint64_t* timestamp = nullptr;
1524 const uint64_t* updateTimestamp = nullptr;
1525 const std::string* severity = nullptr;
1526 const std::string* message = nullptr;
1527 const std::string* filePath = nullptr;
1528 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001529
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001530 const bool success = sdbusplus::unpackPropertiesNoThrow(
1531 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1532 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
1533 severity, "Message", message, "Resolved", resolved, "Path",
1534 filePath);
1535
1536 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001537 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001538 messages::internalError(asyncResp->res);
1539 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001540 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001541
Ed Tanous002d39b2022-05-31 08:59:27 -07001542 if (id == nullptr || message == nullptr || severity == nullptr ||
1543 timestamp == nullptr || updateTimestamp == nullptr)
1544 {
1545 messages::internalError(asyncResp->res);
1546 return;
1547 }
1548 asyncResp->res.jsonValue["@odata.type"] =
1549 "#LogEntry.v1_8_0.LogEntry";
1550 asyncResp->res.jsonValue["@odata.id"] =
1551 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1552 std::to_string(*id);
1553 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1554 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1555 asyncResp->res.jsonValue["Message"] = *message;
1556 asyncResp->res.jsonValue["Resolved"] = resolved;
1557 asyncResp->res.jsonValue["EntryType"] = "Event";
1558 asyncResp->res.jsonValue["Severity"] =
1559 translateSeverityDbusToRedfish(*severity);
1560 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001561 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001562 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001563 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001564 if (filePath != nullptr)
1565 {
1566 asyncResp->res.jsonValue["AdditionalDataURI"] =
1567 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1568 std::to_string(*id) + "/attachment";
1569 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001570 });
Ed Tanous45ca1b82022-03-25 13:07:27 -07001571 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001572
1573 BMCWEB_ROUTE(
1574 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001575 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001576 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001577 [&app](const crow::Request& req,
1578 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1579 const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001580 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001581 {
1582 return;
1583 }
1584 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001585
Ed Tanous002d39b2022-05-31 08:59:27 -07001586 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1587 resolved))
1588 {
1589 return;
1590 }
1591 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001592
Ed Tanous002d39b2022-05-31 08:59:27 -07001593 crow::connections::systemBus->async_method_call(
1594 [asyncResp, entryId](const boost::system::error_code ec) {
1595 if (ec)
1596 {
1597 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1598 messages::internalError(asyncResp->res);
1599 return;
1600 }
1601 },
1602 "xyz.openbmc_project.Logging",
1603 "/xyz/openbmc_project/logging/entry/" + entryId,
1604 "org.freedesktop.DBus.Properties", "Set",
1605 "xyz.openbmc_project.Logging.Entry", "Resolved",
1606 dbus::utility::DbusVariantType(*resolved));
1607 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001608
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001609 BMCWEB_ROUTE(
1610 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001611 .privileges(redfish::privileges::deleteLogEntry)
1612
Ed Tanous002d39b2022-05-31 08:59:27 -07001613 .methods(boost::beast::http::verb::delete_)(
1614 [&app](const crow::Request& req,
1615 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1616 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001617 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001618 {
1619 return;
1620 }
1621 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1622
1623 std::string entryID = param;
1624
1625 dbus::utility::escapePathForDbus(entryID);
1626
1627 // Process response from Logging service.
1628 auto respHandler =
1629 [asyncResp, entryID](const boost::system::error_code ec) {
1630 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1631 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001632 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001633 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001634 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001635 messages::resourceNotFound(asyncResp->res, "LogEntry",
1636 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001637 return;
1638 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001639 // TODO Handle for specific error code
1640 BMCWEB_LOG_ERROR
1641 << "EventLogEntry (DBus) doDelete respHandler got error "
1642 << ec;
1643 asyncResp->res.result(
1644 boost::beast::http::status::internal_server_error);
1645 return;
1646 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001647
Ed Tanous002d39b2022-05-31 08:59:27 -07001648 asyncResp->res.result(boost::beast::http::status::ok);
1649 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001650
Ed Tanous002d39b2022-05-31 08:59:27 -07001651 // Make call to Logging service to request Delete Log
1652 crow::connections::systemBus->async_method_call(
1653 respHandler, "xyz.openbmc_project.Logging",
1654 "/xyz/openbmc_project/logging/entry/" + entryID,
1655 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001656 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001657}
1658
1659inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001660{
George Liu0fda0f12021-11-16 10:06:17 +08001661 BMCWEB_ROUTE(
1662 app,
1663 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001664 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001665 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001666 [&app](const crow::Request& req,
1667 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1668 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001669 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001670 {
1671 return;
1672 }
1673 if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
1674 {
1675 asyncResp->res.result(boost::beast::http::status::bad_request);
1676 return;
1677 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001678
Ed Tanous002d39b2022-05-31 08:59:27 -07001679 std::string entryID = param;
1680 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001681
Ed Tanous002d39b2022-05-31 08:59:27 -07001682 crow::connections::systemBus->async_method_call(
1683 [asyncResp, entryID](const boost::system::error_code ec,
1684 const sdbusplus::message::unix_fd& unixfd) {
1685 if (ec.value() == EBADR)
1686 {
1687 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1688 entryID);
1689 return;
1690 }
1691 if (ec)
1692 {
1693 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1694 messages::internalError(asyncResp->res);
1695 return;
1696 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001697
Ed Tanous002d39b2022-05-31 08:59:27 -07001698 int fd = -1;
1699 fd = dup(unixfd);
1700 if (fd == -1)
1701 {
1702 messages::internalError(asyncResp->res);
1703 return;
1704 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001705
Ed Tanous002d39b2022-05-31 08:59:27 -07001706 long long int size = lseek(fd, 0, SEEK_END);
1707 if (size == -1)
1708 {
1709 messages::internalError(asyncResp->res);
1710 return;
1711 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001712
Ed Tanous002d39b2022-05-31 08:59:27 -07001713 // Arbitrary max size of 64kb
1714 constexpr int maxFileSize = 65536;
1715 if (size > maxFileSize)
1716 {
1717 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1718 << maxFileSize;
1719 messages::internalError(asyncResp->res);
1720 return;
1721 }
1722 std::vector<char> data(static_cast<size_t>(size));
1723 long long int rc = lseek(fd, 0, SEEK_SET);
1724 if (rc == -1)
1725 {
1726 messages::internalError(asyncResp->res);
1727 return;
1728 }
1729 rc = read(fd, data.data(), data.size());
1730 if ((rc == -1) || (rc != size))
1731 {
1732 messages::internalError(asyncResp->res);
1733 return;
1734 }
1735 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001736
Ed Tanous002d39b2022-05-31 08:59:27 -07001737 std::string_view strData(data.data(), data.size());
1738 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001739
Ed Tanousd9f6c622022-03-17 09:12:17 -07001740 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07001741 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07001742 asyncResp->res.addHeader(
1743 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07001744 asyncResp->res.body() = std::move(output);
1745 },
1746 "xyz.openbmc_project.Logging",
1747 "/xyz/openbmc_project/logging/entry/" + entryID,
1748 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1749 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001750}
1751
Spencer Kub7028eb2021-10-26 15:27:35 +08001752constexpr const char* hostLoggerFolderPath = "/var/log/console";
1753
1754inline bool
1755 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1756 std::vector<std::filesystem::path>& hostLoggerFiles)
1757{
1758 std::error_code ec;
1759 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1760 if (ec)
1761 {
1762 BMCWEB_LOG_ERROR << ec.message();
1763 return false;
1764 }
1765 for (const std::filesystem::directory_entry& it : logPath)
1766 {
1767 std::string filename = it.path().filename();
1768 // Prefix of each log files is "log". Find the file and save the
1769 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07001770 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08001771 {
1772 hostLoggerFiles.emplace_back(it.path());
1773 }
1774 }
1775 // As the log files rotate, they are appended with a ".#" that is higher for
1776 // the older logs. Since we start from oldest logs, sort the name in
1777 // descending order.
1778 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1779 AlphanumLess<std::string>());
1780
1781 return true;
1782}
1783
Ed Tanous02cad962022-06-30 16:50:15 -07001784inline bool getHostLoggerEntries(
1785 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
1786 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001787{
1788 GzFileReader logFile;
1789
1790 // Go though all log files and expose host logs.
1791 for (const std::filesystem::path& it : hostLoggerFiles)
1792 {
1793 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1794 {
1795 BMCWEB_LOG_ERROR << "fail to expose host logs";
1796 return false;
1797 }
1798 }
1799 // Get lastMessage from constructor by getter
1800 std::string lastMessage = logFile.getLastMessage();
1801 if (!lastMessage.empty())
1802 {
1803 logCount++;
1804 if (logCount > skip && logCount <= (skip + top))
1805 {
1806 logEntries.push_back(lastMessage);
1807 }
1808 }
1809 return true;
1810}
1811
1812inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1813 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001814 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08001815{
1816 // Fill in the log entry with the gathered data.
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001817 logEntryJson["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
1818 logEntryJson["@odata.id"] =
1819 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1820 logEntryID;
1821 logEntryJson["Name"] = "Host Logger Entry";
1822 logEntryJson["Id"] = logEntryID;
1823 logEntryJson["Message"] = msg;
1824 logEntryJson["EntryType"] = "Oem";
1825 logEntryJson["Severity"] = "OK";
1826 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08001827}
1828
1829inline void requestRoutesSystemHostLogger(App& app)
1830{
1831 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1832 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07001833 .methods(boost::beast::http::verb::get)(
1834 [&app](const crow::Request& req,
1835 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001836 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001837 {
1838 return;
1839 }
1840 asyncResp->res.jsonValue["@odata.id"] =
1841 "/redfish/v1/Systems/system/LogServices/HostLogger";
1842 asyncResp->res.jsonValue["@odata.type"] =
1843 "#LogService.v1_1_0.LogService";
1844 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1845 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1846 asyncResp->res.jsonValue["Id"] = "HostLogger";
1847 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1848 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1849 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001850}
1851
1852inline void requestRoutesSystemHostLoggerCollection(App& app)
1853{
1854 BMCWEB_ROUTE(app,
1855 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1856 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001857 .methods(boost::beast::http::verb::get)(
1858 [&app](const crow::Request& req,
1859 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1860 query_param::QueryCapabilities capabilities = {
1861 .canDelegateTop = true,
1862 .canDelegateSkip = true,
1863 };
1864 query_param::Query delegatedQuery;
1865 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001866 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001867 {
1868 return;
1869 }
1870 asyncResp->res.jsonValue["@odata.id"] =
1871 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1872 asyncResp->res.jsonValue["@odata.type"] =
1873 "#LogEntryCollection.LogEntryCollection";
1874 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1875 asyncResp->res.jsonValue["Description"] =
1876 "Collection of HostLogger Entries";
1877 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1878 logEntryArray = nlohmann::json::array();
1879 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001880
Ed Tanous002d39b2022-05-31 08:59:27 -07001881 std::vector<std::filesystem::path> hostLoggerFiles;
1882 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1883 {
1884 BMCWEB_LOG_ERROR << "fail to get host log file path";
1885 return;
1886 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07001887 // If we weren't provided top and skip limits, use the defaults.
1888 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001889 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07001890 size_t logCount = 0;
1891 // This vector only store the entries we want to expose that
1892 // control by skip and top.
1893 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001894 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1895 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07001896 {
1897 messages::internalError(asyncResp->res);
1898 return;
1899 }
1900 // If vector is empty, that means skip value larger than total
1901 // log count
1902 if (logEntries.empty())
1903 {
1904 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1905 return;
1906 }
1907 if (!logEntries.empty())
1908 {
1909 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08001910 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001911 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001912 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
1913 hostLogEntry);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001914 logEntryArray.push_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08001915 }
1916
Ed Tanous002d39b2022-05-31 08:59:27 -07001917 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001918 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08001919 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001920 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1921 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001922 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08001923 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001924 }
George Liu0fda0f12021-11-16 10:06:17 +08001925 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001926}
1927
1928inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1929{
1930 BMCWEB_ROUTE(
1931 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1932 .privileges(redfish::privileges::getLogEntry)
1933 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001934 [&app](const crow::Request& req,
1935 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1936 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001937 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001938 {
1939 return;
1940 }
1941 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08001942
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08001944
Ed Tanous002d39b2022-05-31 08:59:27 -07001945 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1946 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08001947
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1949 if (ec == std::errc::invalid_argument)
1950 {
1951 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1952 return;
1953 }
1954 if (ec == std::errc::result_out_of_range)
1955 {
1956 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1957 return;
1958 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001959
Ed Tanous002d39b2022-05-31 08:59:27 -07001960 std::vector<std::filesystem::path> hostLoggerFiles;
1961 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1962 {
1963 BMCWEB_LOG_ERROR << "fail to get host log file path";
1964 return;
1965 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001966
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001968 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07001969 std::vector<std::string> logEntries;
1970 // We can get specific entry by skip and top. For example, if we
1971 // want to get nth entry, we can set skip = n-1 and top = 1 to
1972 // get that entry
1973 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
1974 logCount))
1975 {
1976 messages::internalError(asyncResp->res);
1977 return;
1978 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001979
Ed Tanous002d39b2022-05-31 08:59:27 -07001980 if (!logEntries.empty())
1981 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07001982 nlohmann::json::object_t hostLogEntry;
1983 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
1984 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07001985 return;
1986 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001987
Ed Tanous002d39b2022-05-31 08:59:27 -07001988 // Requested ID was not found
1989 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1990 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001991}
1992
Claire Weinanfdd26902022-03-01 14:18:25 -08001993constexpr char const* dumpManagerIface =
1994 "xyz.openbmc_project.Collection.DeleteAll";
Claire Weinandd72e872022-08-15 14:20:06 -07001995inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08001996 crow::App& app, const crow::Request& req,
1997 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1998{
1999 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2000 {
2001 return;
2002 }
2003 // Collections don't include the static data added by SubRoute
2004 // because it has a duplicate entry for members
2005 asyncResp->res.jsonValue["@odata.type"] =
2006 "#LogServiceCollection.LogServiceCollection";
2007 asyncResp->res.jsonValue["@odata.id"] =
2008 "/redfish/v1/Managers/bmc/LogServices";
2009 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2010 asyncResp->res.jsonValue["Description"] =
2011 "Collection of LogServices for this Manager";
2012 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2013 logServiceArray = nlohmann::json::array();
2014
2015#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2016 logServiceArray.push_back(
2017 {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2018#endif
2019
2020 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2021
2022#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2023 auto respHandler =
2024 [asyncResp](
2025 const boost::system::error_code ec,
2026 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2027 if (ec)
2028 {
2029 BMCWEB_LOG_ERROR
Claire Weinandd72e872022-08-15 14:20:06 -07002030 << "handleBMCLogServicesCollectionGet respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -08002031 << ec;
2032 // Assume that getting an error simply means there are no dump
2033 // LogServices. Return without adding any error response.
2034 return;
2035 }
2036
2037 nlohmann::json& logServiceArrayLocal =
2038 asyncResp->res.jsonValue["Members"];
2039
2040 for (const std::string& path : subTreePaths)
2041 {
2042 if (path == "/xyz/openbmc_project/dump/bmc")
2043 {
2044 logServiceArrayLocal.push_back(
2045 {{"@odata.id",
2046 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
2047 }
2048 else if (path == "/xyz/openbmc_project/dump/faultlog")
2049 {
2050 logServiceArrayLocal.push_back(
2051 {{"@odata.id",
2052 "/redfish/v1/Managers/bmc/LogServices/FaultLog"}});
2053 }
2054 }
2055
2056 asyncResp->res.jsonValue["Members@odata.count"] =
2057 logServiceArrayLocal.size();
2058 };
2059
2060 crow::connections::systemBus->async_method_call(
2061 respHandler, "xyz.openbmc_project.ObjectMapper",
2062 "/xyz/openbmc_project/object_mapper",
2063 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2064 "/xyz/openbmc_project/dump", 0,
2065 std::array<const char*, 1>{dumpManagerIface});
2066#endif
2067}
2068
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002069inline void requestRoutesBMCLogServiceCollection(App& app)
2070{
2071 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002072 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002073 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002074 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002075}
Ed Tanous1da66f72018-07-27 16:13:37 -07002076
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002077inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002078{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002079 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002080 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002081 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002082 [&app](const crow::Request& req,
2083 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002084 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 {
2086 return;
2087 }
2088 asyncResp->res.jsonValue["@odata.type"] =
2089 "#LogService.v1_1_0.LogService";
2090 asyncResp->res.jsonValue["@odata.id"] =
2091 "/redfish/v1/Managers/bmc/LogServices/Journal";
2092 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2093 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2094 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2095 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302096
Ed Tanous002d39b2022-05-31 08:59:27 -07002097 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002098 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002099 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2100 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2101 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302102
Ed Tanous002d39b2022-05-31 08:59:27 -07002103 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2104 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2105 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002106}
Jason M. Billse1f26342018-07-18 12:12:00 -07002107
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002108static int
2109 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2110 sd_journal* journal,
2111 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002112{
2113 // Get the Log Entry contents
2114 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002115
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002116 std::string message;
2117 std::string_view syslogID;
2118 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2119 if (ret < 0)
2120 {
2121 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2122 << strerror(-ret);
2123 }
2124 if (!syslogID.empty())
2125 {
2126 message += std::string(syslogID) + ": ";
2127 }
2128
Ed Tanous39e77502019-03-04 17:35:53 -08002129 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002130 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002131 if (ret < 0)
2132 {
2133 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2134 return 1;
2135 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002136 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002137
2138 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002139 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002140 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002141 if (ret < 0)
2142 {
2143 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002144 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002145
2146 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002147 std::string entryTimeStr;
2148 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002149 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002150 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002151 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002152
2153 // Fill in the log entry with the gathered data
Jason M. Bills84afc482022-06-24 12:38:23 -07002154 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
2155 bmcJournalLogEntryJson["@odata.id"] =
2156 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2157 bmcJournalLogEntryID;
2158 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2159 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2160 bmcJournalLogEntryJson["Message"] = std::move(message);
2161 bmcJournalLogEntryJson["EntryType"] = "Oem";
2162 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2163 : severity <= 4 ? "Warning"
2164 : "OK";
2165 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2166 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002167 return 0;
2168}
2169
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002170inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002171{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002172 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002173 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002174 .methods(boost::beast::http::verb::get)(
2175 [&app](const crow::Request& req,
2176 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2177 query_param::QueryCapabilities capabilities = {
2178 .canDelegateTop = true,
2179 .canDelegateSkip = true,
2180 };
2181 query_param::Query delegatedQuery;
2182 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002183 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002184 {
2185 return;
2186 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002187
2188 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002189 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002190
Ed Tanous002d39b2022-05-31 08:59:27 -07002191 // Collections don't include the static data added by SubRoute
2192 // because it has a duplicate entry for members
2193 asyncResp->res.jsonValue["@odata.type"] =
2194 "#LogEntryCollection.LogEntryCollection";
2195 asyncResp->res.jsonValue["@odata.id"] =
2196 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2197 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2198 asyncResp->res.jsonValue["Description"] =
2199 "Collection of BMC Journal Entries";
2200 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2201 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002202
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 // Go through the journal and use the timestamp to create a
2204 // unique ID for each entry
2205 sd_journal* journalTmp = nullptr;
2206 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2207 if (ret < 0)
2208 {
2209 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2210 messages::internalError(asyncResp->res);
2211 return;
2212 }
2213 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2214 journalTmp, sd_journal_close);
2215 journalTmp = nullptr;
2216 uint64_t entryCount = 0;
2217 // Reset the unique ID on the first entry
2218 bool firstEntry = true;
2219 SD_JOURNAL_FOREACH(journal.get())
2220 {
2221 entryCount++;
2222 // Handle paging using skip (number of entries to skip from
2223 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002224 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002225 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002226 continue;
2227 }
2228
2229 std::string idStr;
2230 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2231 {
2232 continue;
2233 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002234 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002235
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002236 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002237 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2238 bmcJournalLogEntry) != 0)
2239 {
George Liu0fda0f12021-11-16 10:06:17 +08002240 messages::internalError(asyncResp->res);
2241 return;
2242 }
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002243 logEntryArray.push_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002244 }
2245 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002246 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002247 {
2248 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2249 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002250 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002251 }
George Liu0fda0f12021-11-16 10:06:17 +08002252 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002253}
Jason M. Billse1f26342018-07-18 12:12:00 -07002254
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002255inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002256{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002257 BMCWEB_ROUTE(app,
2258 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002259 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002260 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002261 [&app](const crow::Request& req,
2262 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2263 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002264 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 {
2266 return;
2267 }
2268 // Convert the unique ID back to a timestamp to find the entry
2269 uint64_t ts = 0;
2270 uint64_t index = 0;
2271 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2272 {
2273 return;
2274 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002275
Ed Tanous002d39b2022-05-31 08:59:27 -07002276 sd_journal* journalTmp = nullptr;
2277 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2278 if (ret < 0)
2279 {
2280 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2281 messages::internalError(asyncResp->res);
2282 return;
2283 }
2284 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2285 journalTmp, sd_journal_close);
2286 journalTmp = nullptr;
2287 // Go to the timestamp in the log and move to the entry at the
2288 // index tracking the unique ID
2289 std::string idStr;
2290 bool firstEntry = true;
2291 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2292 if (ret < 0)
2293 {
2294 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2295 << strerror(-ret);
2296 messages::internalError(asyncResp->res);
2297 return;
2298 }
2299 for (uint64_t i = 0; i <= index; i++)
2300 {
2301 sd_journal_next(journal.get());
2302 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2303 {
2304 messages::internalError(asyncResp->res);
2305 return;
2306 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002307 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002308 }
2309 // Confirm that the entry ID matches what was requested
2310 if (idStr != entryID)
2311 {
2312 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2313 return;
2314 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002315
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002316 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002317 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002318 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002319 {
2320 messages::internalError(asyncResp->res);
2321 return;
2322 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002323 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002324 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002325}
2326
Claire Weinanfdd26902022-03-01 14:18:25 -08002327inline void
2328 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2329 const std::string& dumpType)
2330{
2331 std::string dumpPath;
2332 std::string overWritePolicy;
2333 bool collectDiagnosticDataSupported = false;
2334
2335 if (dumpType == "BMC")
2336 {
2337 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2338 overWritePolicy = "WrapsWhenFull";
2339 collectDiagnosticDataSupported = true;
2340 }
2341 else if (dumpType == "FaultLog")
2342 {
2343 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2344 overWritePolicy = "Unknown";
2345 collectDiagnosticDataSupported = false;
2346 }
2347 else if (dumpType == "System")
2348 {
2349 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2350 overWritePolicy = "WrapsWhenFull";
2351 collectDiagnosticDataSupported = true;
2352 }
2353 else
2354 {
2355 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2356 << dumpType;
2357 messages::internalError(asyncResp->res);
2358 return;
2359 }
2360
2361 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2362 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2363 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2364 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2365 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2366 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2367
2368 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002369 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002370 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2371 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2372 redfishDateTimeOffset.second;
2373
2374 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2375 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2376 dumpPath + "/Actions/LogService.ClearLog";
2377
2378 if (collectDiagnosticDataSupported)
2379 {
2380 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2381 ["target"] =
2382 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2383 }
2384}
2385
2386inline void handleLogServicesDumpServiceGet(
2387 crow::App& app, const std::string& dumpType, const crow::Request& req,
2388 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2389{
2390 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2391 {
2392 return;
2393 }
2394 getDumpServiceInfo(asyncResp, dumpType);
2395}
2396
2397inline void handleLogServicesDumpEntriesCollectionGet(
2398 crow::App& app, const std::string& dumpType, const crow::Request& req,
2399 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2400{
2401 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2402 {
2403 return;
2404 }
2405 getDumpEntryCollection(asyncResp, dumpType);
2406}
2407
2408inline void handleLogServicesDumpEntryGet(
2409 crow::App& app, const std::string& dumpType, const crow::Request& req,
2410 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2411 const std::string& dumpId)
2412{
2413 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2414 {
2415 return;
2416 }
2417 getDumpEntryById(asyncResp, dumpId, dumpType);
2418}
2419
2420inline void handleLogServicesDumpEntryDelete(
2421 crow::App& app, const std::string& dumpType, const crow::Request& req,
2422 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2423 const std::string& dumpId)
2424{
2425 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2426 {
2427 return;
2428 }
2429 deleteDumpEntry(asyncResp, dumpId, dumpType);
2430}
2431
2432inline void handleLogServicesDumpCollectDiagnosticDataPost(
2433 crow::App& app, const std::string& dumpType, const crow::Request& req,
2434 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2435{
2436 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2437 {
2438 return;
2439 }
2440 createDump(asyncResp, req, dumpType);
2441}
2442
2443inline void handleLogServicesDumpClearLogPost(
2444 crow::App& app, const std::string& dumpType, const crow::Request& req,
2445 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2446{
2447 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2448 {
2449 return;
2450 }
2451 clearDump(asyncResp, dumpType);
2452}
2453
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002454inline void requestRoutesBMCDumpService(App& app)
2455{
2456 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002457 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002458 .methods(boost::beast::http::verb::get)(std::bind_front(
2459 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002460}
2461
2462inline void requestRoutesBMCDumpEntryCollection(App& app)
2463{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002464 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002465 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002466 .methods(boost::beast::http::verb::get)(std::bind_front(
2467 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002468}
2469
2470inline void requestRoutesBMCDumpEntry(App& app)
2471{
2472 BMCWEB_ROUTE(app,
2473 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002474 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002475 .methods(boost::beast::http::verb::get)(std::bind_front(
2476 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2477
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002478 BMCWEB_ROUTE(app,
2479 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002480 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002481 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2482 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002483}
2484
2485inline void requestRoutesBMCDumpCreate(App& app)
2486{
George Liu0fda0f12021-11-16 10:06:17 +08002487 BMCWEB_ROUTE(
2488 app,
2489 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002490 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002491 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002492 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2493 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002494}
2495
2496inline void requestRoutesBMCDumpClear(App& app)
2497{
George Liu0fda0f12021-11-16 10:06:17 +08002498 BMCWEB_ROUTE(
2499 app,
2500 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002501 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002502 .methods(boost::beast::http::verb::post)(std::bind_front(
2503 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2504}
2505
2506inline void requestRoutesFaultLogDumpService(App& app)
2507{
2508 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2509 .privileges(redfish::privileges::getLogService)
2510 .methods(boost::beast::http::verb::get)(std::bind_front(
2511 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2512}
2513
2514inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2515{
2516 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2517 .privileges(redfish::privileges::getLogEntryCollection)
2518 .methods(boost::beast::http::verb::get)(
2519 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2520 std::ref(app), "FaultLog"));
2521}
2522
2523inline void requestRoutesFaultLogDumpEntry(App& app)
2524{
2525 BMCWEB_ROUTE(app,
2526 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2527 .privileges(redfish::privileges::getLogEntry)
2528 .methods(boost::beast::http::verb::get)(std::bind_front(
2529 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2530
2531 BMCWEB_ROUTE(app,
2532 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2533 .privileges(redfish::privileges::deleteLogEntry)
2534 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2535 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2536}
2537
2538inline void requestRoutesFaultLogDumpClear(App& app)
2539{
2540 BMCWEB_ROUTE(
2541 app,
2542 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2543 .privileges(redfish::privileges::postLogService)
2544 .methods(boost::beast::http::verb::post)(std::bind_front(
2545 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002546}
2547
2548inline void requestRoutesSystemDumpService(App& app)
2549{
2550 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002551 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07002552 .methods(boost::beast::http::verb::get)(
2553 [&app](const crow::Request& req,
2554 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002555 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002556 {
2557 return;
2558 }
2559 asyncResp->res.jsonValue["@odata.id"] =
2560 "/redfish/v1/Systems/system/LogServices/Dump";
2561 asyncResp->res.jsonValue["@odata.type"] =
2562 "#LogService.v1_2_0.LogService";
2563 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2564 asyncResp->res.jsonValue["Description"] = "System Dump LogService";
2565 asyncResp->res.jsonValue["Id"] = "Dump";
2566 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302567
Ed Tanous002d39b2022-05-31 08:59:27 -07002568 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002569 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002570 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2571 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2572 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302573
Ed Tanous002d39b2022-05-31 08:59:27 -07002574 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2575 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2576 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2577 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog";
Ed Tanous14766872022-03-15 10:44:42 -07002578
Ed Tanous002d39b2022-05-31 08:59:27 -07002579 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2580 ["target"] =
2581 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData";
Ed Tanous45ca1b82022-03-25 13:07:27 -07002582 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002583}
2584
2585inline void requestRoutesSystemDumpEntryCollection(App& app)
2586{
2587
2588 /**
2589 * Functions triggers appropriate requests on DBus
2590 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002591 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002592 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002593 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002594 [&app](const crow::Request& req,
2595 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002596 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002597 {
2598 return;
2599 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002600 getDumpEntryCollection(asyncResp, "System");
2601 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002602}
2603
2604inline void requestRoutesSystemDumpEntry(App& app)
2605{
2606 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002607 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002608 .privileges(redfish::privileges::getLogEntry)
2609
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002610 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002611 [&app](const crow::Request& req,
2612 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2613 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002614 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Claire Weinanc7a6d662022-06-13 16:36:39 -07002615 {
2616 return;
2617 }
2618 getDumpEntryById(asyncResp, param, "System");
Ed Tanous002d39b2022-05-31 08:59:27 -07002619 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002620
2621 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002622 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002623 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002624 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002625 [&app](const crow::Request& req,
2626 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2627 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002628 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002629 {
2630 return;
2631 }
2632 deleteDumpEntry(asyncResp, param, "system");
2633 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002634}
2635
2636inline void requestRoutesSystemDumpCreate(App& app)
2637{
George Liu0fda0f12021-11-16 10:06:17 +08002638 BMCWEB_ROUTE(
2639 app,
2640 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002641 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002642 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002643 [&app](const crow::Request& req,
2644 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002645 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002646 {
2647 return;
2648 }
2649 createDump(asyncResp, req, "System");
2650 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002651}
2652
2653inline void requestRoutesSystemDumpClear(App& app)
2654{
George Liu0fda0f12021-11-16 10:06:17 +08002655 BMCWEB_ROUTE(
2656 app,
2657 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002658 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002659 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002660 [&app](const crow::Request& req,
2661 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002662
Ed Tanous45ca1b82022-03-25 13:07:27 -07002663 {
Carson Labrado3ba00072022-06-06 19:40:56 +00002664 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002665 {
2666 return;
2667 }
2668 clearDump(asyncResp, "System");
2669 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002670}
2671
2672inline void requestRoutesCrashdumpService(App& app)
2673{
2674 // Note: Deviated from redfish privilege registry for GET & HEAD
2675 // method for security reasons.
2676 /**
2677 * Functions triggers appropriate requests on DBus
2678 */
2679 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002680 // This is incorrect, should be:
2681 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002682 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002683 .methods(boost::beast::http::verb::get)(
2684 [&app](const crow::Request& req,
2685 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002686 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002687 {
2688 return;
2689 }
2690 // Copy over the static data to include the entries added by
2691 // SubRoute
2692 asyncResp->res.jsonValue["@odata.id"] =
2693 "/redfish/v1/Systems/system/LogServices/Crashdump";
2694 asyncResp->res.jsonValue["@odata.type"] =
2695 "#LogService.v1_2_0.LogService";
2696 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2697 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2698 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2699 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2700 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302701
Ed Tanous002d39b2022-05-31 08:59:27 -07002702 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002703 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002704 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2705 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2706 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302707
Ed Tanous002d39b2022-05-31 08:59:27 -07002708 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2709 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2710 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2711 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2712 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2713 ["target"] =
2714 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002715 });
2716}
2717
2718void inline requestRoutesCrashdumpClear(App& app)
2719{
George Liu0fda0f12021-11-16 10:06:17 +08002720 BMCWEB_ROUTE(
2721 app,
2722 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002723 // This is incorrect, should be:
2724 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002725 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002726 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002727 [&app](const crow::Request& req,
2728 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002729 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002730 {
2731 return;
2732 }
2733 crow::connections::systemBus->async_method_call(
2734 [asyncResp](const boost::system::error_code ec,
2735 const std::string&) {
2736 if (ec)
2737 {
2738 messages::internalError(asyncResp->res);
2739 return;
2740 }
2741 messages::success(asyncResp->res);
2742 },
2743 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
2744 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002745}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002746
zhanghch058d1b46d2021-04-01 11:18:24 +08002747static void
2748 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2749 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002750{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002751 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08002752 [asyncResp, logID,
2753 &logEntryJson](const boost::system::error_code ec,
2754 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002755 if (ec)
2756 {
2757 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2758 if (ec.value() ==
2759 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002760 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002761 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002762 }
2763 else
2764 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002765 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002766 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002767 return;
2768 }
2769
2770 std::string timestamp{};
2771 std::string filename{};
2772 std::string logfile{};
2773 parseCrashdumpParameters(params, filename, timestamp, logfile);
2774
2775 if (filename.empty() || timestamp.empty())
2776 {
2777 messages::resourceMissingAtURI(asyncResp->res,
2778 crow::utility::urlFromPieces(logID));
2779 return;
2780 }
2781
2782 std::string crashdumpURI =
2783 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2784 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07002785 nlohmann::json::object_t logEntry;
2786 logEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
2787 logEntry["@odata.id"] =
2788 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
2789 logEntry["Name"] = "CPU Crashdump";
2790 logEntry["Id"] = logID;
2791 logEntry["EntryType"] = "Oem";
2792 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
2793 logEntry["DiagnosticDataType"] = "OEM";
2794 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
2795 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002796
2797 // If logEntryJson references an array of LogEntry resources
2798 // ('Members' list), then push this as a new entry, otherwise set it
2799 // directly
2800 if (logEntryJson.is_array())
2801 {
2802 logEntryJson.push_back(logEntry);
2803 asyncResp->res.jsonValue["Members@odata.count"] =
2804 logEntryJson.size();
2805 }
2806 else
2807 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07002808 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002809 }
2810 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002811 sdbusplus::asio::getAllProperties(
2812 *crow::connections::systemBus, crashdumpObject,
2813 crashdumpPath + std::string("/") + logID, crashdumpInterface,
2814 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07002815}
2816
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002817inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002818{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002819 // Note: Deviated from redfish privilege registry for GET & HEAD
2820 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002821 /**
2822 * Functions triggers appropriate requests on DBus
2823 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002824 BMCWEB_ROUTE(app,
2825 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002826 // This is incorrect, should be.
2827 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002828 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002829 .methods(boost::beast::http::verb::get)(
2830 [&app](const crow::Request& req,
2831 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002832 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002833 {
2834 return;
2835 }
2836 crow::connections::systemBus->async_method_call(
2837 [asyncResp](const boost::system::error_code ec,
2838 const std::vector<std::string>& resp) {
2839 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002840 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002841 if (ec.value() !=
2842 boost::system::errc::no_such_file_or_directory)
2843 {
2844 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2845 << ec.message();
2846 messages::internalError(asyncResp->res);
2847 return;
2848 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002849 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002850 asyncResp->res.jsonValue["@odata.type"] =
2851 "#LogEntryCollection.LogEntryCollection";
2852 asyncResp->res.jsonValue["@odata.id"] =
2853 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2854 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2855 asyncResp->res.jsonValue["Description"] =
2856 "Collection of Crashdump Entries";
2857 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
2858 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002859
Ed Tanous002d39b2022-05-31 08:59:27 -07002860 for (const std::string& path : resp)
2861 {
2862 const sdbusplus::message::object_path objPath(path);
2863 // Get the log ID
2864 std::string logID = objPath.filename();
2865 if (logID.empty())
2866 {
2867 continue;
2868 }
2869 // Add the log entry to the array
2870 logCrashdumpEntry(asyncResp, logID,
2871 asyncResp->res.jsonValue["Members"]);
2872 }
2873 },
2874 "xyz.openbmc_project.ObjectMapper",
2875 "/xyz/openbmc_project/object_mapper",
2876 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2877 std::array<const char*, 1>{crashdumpInterface});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002878 });
2879}
Ed Tanous1da66f72018-07-27 16:13:37 -07002880
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002881inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002882{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002883 // Note: Deviated from redfish privilege registry for GET & HEAD
2884 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002885
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002886 BMCWEB_ROUTE(
2887 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002888 // this is incorrect, should be
2889 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002890 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002891 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002892 [&app](const crow::Request& req,
2893 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2894 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002895 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002896 {
2897 return;
2898 }
2899 const std::string& logID = param;
2900 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2901 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002902}
Ed Tanous1da66f72018-07-27 16:13:37 -07002903
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002904inline void requestRoutesCrashdumpFile(App& app)
2905{
2906 // Note: Deviated from redfish privilege registry for GET & HEAD
2907 // method for security reasons.
2908 BMCWEB_ROUTE(
2909 app,
2910 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002911 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002912 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00002913 [](const crow::Request& req,
2914 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2915 const std::string& logID, const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00002916 // Do not call getRedfishRoute here since the crashdump file is not a
2917 // Redfish resource.
Ed Tanous002d39b2022-05-31 08:59:27 -07002918 auto getStoredLogCallback =
2919 [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
2920 const boost::system::error_code ec,
2921 const std::vector<
2922 std::pair<std::string, dbus::utility::DbusVariantType>>&
2923 resp) {
2924 if (ec)
2925 {
2926 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2927 messages::internalError(asyncResp->res);
2928 return;
2929 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002930
Ed Tanous002d39b2022-05-31 08:59:27 -07002931 std::string dbusFilename{};
2932 std::string dbusTimestamp{};
2933 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002934
Ed Tanous002d39b2022-05-31 08:59:27 -07002935 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2936 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002937
Ed Tanous002d39b2022-05-31 08:59:27 -07002938 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2939 dbusFilepath.empty())
2940 {
2941 messages::resourceMissingAtURI(asyncResp->res, url);
2942 return;
2943 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002944
Ed Tanous002d39b2022-05-31 08:59:27 -07002945 // Verify the file name parameter is correct
2946 if (fileName != dbusFilename)
2947 {
2948 messages::resourceMissingAtURI(asyncResp->res, url);
2949 return;
2950 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002951
Ed Tanous002d39b2022-05-31 08:59:27 -07002952 if (!std::filesystem::exists(dbusFilepath))
2953 {
2954 messages::resourceMissingAtURI(asyncResp->res, url);
2955 return;
2956 }
2957 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
2958 asyncResp->res.body() =
2959 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002960
Ed Tanous002d39b2022-05-31 08:59:27 -07002961 // Configure this to be a file download when accessed
2962 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07002963 asyncResp->res.addHeader(
2964 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07002965 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002966 sdbusplus::asio::getAllProperties(
2967 *crow::connections::systemBus, crashdumpObject,
2968 crashdumpPath + std::string("/") + logID, crashdumpInterface,
2969 std::move(getStoredLogCallback));
Ed Tanous002d39b2022-05-31 08:59:27 -07002970 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002971}
2972
Jason M. Billsc5a4c822022-01-06 15:51:23 -08002973enum class OEMDiagnosticType
2974{
2975 onDemand,
2976 telemetry,
2977 invalid,
2978};
2979
Ed Tanousf7725d72022-03-07 12:46:00 -08002980inline OEMDiagnosticType
2981 getOEMDiagnosticType(const std::string_view& oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08002982{
2983 if (oemDiagStr == "OnDemand")
2984 {
2985 return OEMDiagnosticType::onDemand;
2986 }
2987 if (oemDiagStr == "Telemetry")
2988 {
2989 return OEMDiagnosticType::telemetry;
2990 }
2991
2992 return OEMDiagnosticType::invalid;
2993}
2994
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002995inline void requestRoutesCrashdumpCollect(App& app)
2996{
2997 // Note: Deviated from redfish privilege registry for GET & HEAD
2998 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08002999 BMCWEB_ROUTE(
3000 app,
3001 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003002 // The below is incorrect; Should be ConfigureManager
3003 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003004 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003005 .methods(boost::beast::http::verb::post)(
3006 [&app](const crow::Request& req,
3007 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003008 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003009 {
3010 return;
3011 }
3012 std::string diagnosticDataType;
3013 std::string oemDiagnosticDataType;
3014 if (!redfish::json_util::readJsonAction(
3015 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3016 "OEMDiagnosticDataType", oemDiagnosticDataType))
3017 {
3018 return;
3019 }
3020
3021 if (diagnosticDataType != "OEM")
3022 {
3023 BMCWEB_LOG_ERROR
3024 << "Only OEM DiagnosticDataType supported for Crashdump";
3025 messages::actionParameterValueFormatError(
3026 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3027 "CollectDiagnosticData");
3028 return;
3029 }
3030
3031 OEMDiagnosticType oemDiagType =
3032 getOEMDiagnosticType(oemDiagnosticDataType);
3033
3034 std::string iface;
3035 std::string method;
3036 std::string taskMatchStr;
3037 if (oemDiagType == OEMDiagnosticType::onDemand)
3038 {
3039 iface = crashdumpOnDemandInterface;
3040 method = "GenerateOnDemandLog";
3041 taskMatchStr = "type='signal',"
3042 "interface='org.freedesktop.DBus.Properties',"
3043 "member='PropertiesChanged',"
3044 "arg0namespace='com.intel.crashdump'";
3045 }
3046 else if (oemDiagType == OEMDiagnosticType::telemetry)
3047 {
3048 iface = crashdumpTelemetryInterface;
3049 method = "GenerateTelemetryLog";
3050 taskMatchStr = "type='signal',"
3051 "interface='org.freedesktop.DBus.Properties',"
3052 "member='PropertiesChanged',"
3053 "arg0namespace='com.intel.crashdump'";
3054 }
3055 else
3056 {
3057 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3058 << oemDiagnosticDataType;
3059 messages::actionParameterValueFormatError(
3060 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3061 "CollectDiagnosticData");
3062 return;
3063 }
3064
3065 auto collectCrashdumpCallback =
3066 [asyncResp, payload(task::Payload(req)),
3067 taskMatchStr](const boost::system::error_code ec,
3068 const std::string&) mutable {
3069 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003070 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003071 if (ec.value() == boost::system::errc::operation_not_supported)
3072 {
3073 messages::resourceInStandby(asyncResp->res);
3074 }
3075 else if (ec.value() ==
3076 boost::system::errc::device_or_resource_busy)
3077 {
3078 messages::serviceTemporarilyUnavailable(asyncResp->res,
3079 "60");
3080 }
3081 else
3082 {
3083 messages::internalError(asyncResp->res);
3084 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003085 return;
3086 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003087 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Patrick Williams59d494e2022-07-22 19:26:55 -05003088 [](boost::system::error_code err, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003089 const std::shared_ptr<task::TaskData>& taskData) {
3090 if (!err)
3091 {
3092 taskData->messages.emplace_back(messages::taskCompletedOK(
3093 std::to_string(taskData->index)));
3094 taskData->state = "Completed";
3095 }
3096 return task::completed;
3097 },
3098 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003099
Ed Tanous002d39b2022-05-31 08:59:27 -07003100 task->startTimer(std::chrono::minutes(5));
3101 task->populateResp(asyncResp->res);
3102 task->payload.emplace(std::move(payload));
3103 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003104
Ed Tanous002d39b2022-05-31 08:59:27 -07003105 crow::connections::systemBus->async_method_call(
3106 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3107 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003108 });
3109}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003110
Andrew Geisslercb92c032018-08-17 07:56:14 -07003111/**
3112 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3113 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003114inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003115{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003116 /**
3117 * Function handles POST method request.
3118 * The Clear Log actions does not require any parameter.The action deletes
3119 * all entries found in the Entries collection for this Log Service.
3120 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003121
George Liu0fda0f12021-11-16 10:06:17 +08003122 BMCWEB_ROUTE(
3123 app,
3124 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003125 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003126 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003127 [&app](const crow::Request& req,
3128 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003129 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003130 {
3131 return;
3132 }
3133 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003134
Ed Tanous002d39b2022-05-31 08:59:27 -07003135 // Process response from Logging service.
3136 auto respHandler = [asyncResp](const boost::system::error_code ec) {
3137 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3138 if (ec)
3139 {
3140 // TODO Handle for specific error code
3141 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3142 asyncResp->res.result(
3143 boost::beast::http::status::internal_server_error);
3144 return;
3145 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003146
Ed Tanous002d39b2022-05-31 08:59:27 -07003147 asyncResp->res.result(boost::beast::http::status::no_content);
3148 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003149
Ed Tanous002d39b2022-05-31 08:59:27 -07003150 // Make call to Logging service to request Clear Log
3151 crow::connections::systemBus->async_method_call(
3152 respHandler, "xyz.openbmc_project.Logging",
3153 "/xyz/openbmc_project/logging",
3154 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3155 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003156}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003157
3158/****************************************************
3159 * Redfish PostCode interfaces
3160 * using DBUS interface: getPostCodesTS
3161 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003162inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003163{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003164 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003165 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003166 .methods(boost::beast::http::verb::get)(
3167 [&app](const crow::Request& req,
3168 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003169 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003170 {
3171 return;
3172 }
Ed Tanous14766872022-03-15 10:44:42 -07003173
Ed Tanous002d39b2022-05-31 08:59:27 -07003174 asyncResp->res.jsonValue["@odata.id"] =
3175 "/redfish/v1/Systems/system/LogServices/PostCodes";
3176 asyncResp->res.jsonValue["@odata.type"] =
3177 "#LogService.v1_1_0.LogService";
3178 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3179 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3180 asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
3181 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3182 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3183 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303184
Ed Tanous002d39b2022-05-31 08:59:27 -07003185 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003186 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003187 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3188 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3189 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303190
Ed Tanous002d39b2022-05-31 08:59:27 -07003191 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3192 {"target",
3193 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003194 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003195}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003196
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003197inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003198{
George Liu0fda0f12021-11-16 10:06:17 +08003199 BMCWEB_ROUTE(
3200 app,
3201 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003202 // The following privilege is incorrect; It should be ConfigureManager
3203 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003204 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003205 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003206 [&app](const crow::Request& req,
3207 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003208 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003209 {
3210 return;
3211 }
3212 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003213
Ed Tanous002d39b2022-05-31 08:59:27 -07003214 // Make call to post-code service to request clear all
3215 crow::connections::systemBus->async_method_call(
3216 [asyncResp](const boost::system::error_code ec) {
3217 if (ec)
3218 {
3219 // TODO Handle for specific error code
3220 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3221 << ec;
3222 asyncResp->res.result(
3223 boost::beast::http::status::internal_server_error);
3224 messages::internalError(asyncResp->res);
3225 return;
3226 }
3227 },
3228 "xyz.openbmc_project.State.Boot.PostCode0",
3229 "/xyz/openbmc_project/State/Boot/PostCode0",
3230 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3231 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003232}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003233
3234static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003235 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303236 const boost::container::flat_map<
3237 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003238 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3239 const uint64_t skip = 0, const uint64_t top = 0)
3240{
3241 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003242 const registries::Message* message =
3243 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003244
3245 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003246 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003247
3248 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303249 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3250 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003251 {
3252 currentCodeIndex++;
3253 std::string postcodeEntryID =
3254 "B" + std::to_string(bootIndex) + "-" +
3255 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3256
3257 uint64_t usecSinceEpoch = code.first;
3258 uint64_t usTimeOffset = 0;
3259
3260 if (1 == currentCodeIndex)
3261 { // already incremented
3262 firstCodeTimeUs = code.first;
3263 }
3264 else
3265 {
3266 usTimeOffset = code.first - firstCodeTimeUs;
3267 }
3268
3269 // skip if no specific codeIndex is specified and currentCodeIndex does
3270 // not fall between top and skip
3271 if ((codeIndex == 0) &&
3272 (currentCodeIndex <= skip || currentCodeIndex > top))
3273 {
3274 continue;
3275 }
3276
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003277 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003278 // currentIndex
3279 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3280 {
3281 // This is done for simplicity. 1st entry is needed to calculate
3282 // time offset. To improve efficiency, one can get to the entry
3283 // directly (possibly with flatmap's nth method)
3284 continue;
3285 }
3286
3287 // currentCodeIndex is within top and skip or equal to specified code
3288 // index
3289
3290 // Get the Created time from the timestamp
3291 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003292 entryTimeStr =
Ed Tanous2b829372022-08-03 14:22:34 -07003293 redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003294
3295 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3296 std::ostringstream hexCode;
3297 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303298 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003299 std::ostringstream timeOffsetStr;
3300 // Set Fixed -Point Notation
3301 timeOffsetStr << std::fixed;
3302 // Set precision to 4 digits
3303 timeOffsetStr << std::setprecision(4);
3304 // Add double to stream
3305 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3306 std::vector<std::string> messageArgs = {
3307 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3308
3309 // Get MessageArgs template from message registry
3310 std::string msg;
3311 if (message != nullptr)
3312 {
3313 msg = message->message;
3314
3315 // fill in this post code value
3316 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003317 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003318 {
3319 std::string argStr = "%" + std::to_string(++i);
3320 size_t argPos = msg.find(argStr);
3321 if (argPos != std::string::npos)
3322 {
3323 msg.replace(argPos, argStr.length(), messageArg);
3324 }
3325 }
3326 }
3327
Tim Leed4342a92020-04-27 11:47:58 +08003328 // Get Severity template from message registry
3329 std::string severity;
3330 if (message != nullptr)
3331 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003332 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003333 }
3334
ZhikuiRena3316fc2020-01-29 14:58:08 -08003335 // add to AsyncResp
3336 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003337 nlohmann::json& bmcLogEntry = logEntryArray.back();
Jason M. Bills84afc482022-06-24 12:38:23 -07003338 bmcLogEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
3339 bmcLogEntry["@odata.id"] =
3340 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3341 postcodeEntryID;
3342 bmcLogEntry["Name"] = "POST Code Log Entry";
3343 bmcLogEntry["Id"] = postcodeEntryID;
3344 bmcLogEntry["Message"] = std::move(msg);
3345 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3346 bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3347 bmcLogEntry["EntryType"] = "Event";
3348 bmcLogEntry["Severity"] = std::move(severity);
3349 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08003350 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3351 {
3352 bmcLogEntry["AdditionalDataURI"] =
3353 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3354 postcodeEntryID + "/attachment";
3355 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003356 }
3357}
3358
zhanghch058d1b46d2021-04-01 11:18:24 +08003359static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003360 const uint16_t bootIndex,
3361 const uint64_t codeIndex)
3362{
3363 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303364 [aResp, bootIndex,
3365 codeIndex](const boost::system::error_code ec,
3366 const boost::container::flat_map<
3367 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3368 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003369 if (ec)
3370 {
3371 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3372 messages::internalError(aResp->res);
3373 return;
3374 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003375
Ed Tanous002d39b2022-05-31 08:59:27 -07003376 // skip the empty postcode boots
3377 if (postcode.empty())
3378 {
3379 return;
3380 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003381
Ed Tanous002d39b2022-05-31 08:59:27 -07003382 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003383
Ed Tanous002d39b2022-05-31 08:59:27 -07003384 aResp->res.jsonValue["Members@odata.count"] =
3385 aResp->res.jsonValue["Members"].size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08003386 },
Jonathan Doman15124762021-01-07 17:54:17 -08003387 "xyz.openbmc_project.State.Boot.PostCode0",
3388 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003389 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3390 bootIndex);
3391}
3392
zhanghch058d1b46d2021-04-01 11:18:24 +08003393static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003394 const uint16_t bootIndex,
3395 const uint16_t bootCount,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003396 const uint64_t entryCount, size_t skip,
3397 size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003398{
3399 crow::connections::systemBus->async_method_call(
3400 [aResp, bootIndex, bootCount, entryCount, skip,
3401 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303402 const boost::container::flat_map<
3403 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3404 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003405 if (ec)
3406 {
3407 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3408 messages::internalError(aResp->res);
3409 return;
3410 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003411
Ed Tanous002d39b2022-05-31 08:59:27 -07003412 uint64_t endCount = entryCount;
3413 if (!postcode.empty())
3414 {
3415 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07003416 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003417 {
Ed Tanous3648c8b2022-07-25 13:39:59 -07003418 uint64_t thisBootSkip =
3419 std::max(static_cast<uint64_t>(skip), entryCount) -
3420 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003421 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07003422 std::min(static_cast<uint64_t>(top + skip), endCount) -
3423 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07003424
3425 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3426 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003427 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003428 aResp->res.jsonValue["Members@odata.count"] = endCount;
3429 }
3430
3431 // continue to previous bootIndex
3432 if (bootIndex < bootCount)
3433 {
3434 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3435 bootCount, endCount, skip, top);
3436 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08003437 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07003438 {
3439 aResp->res.jsonValue["Members@odata.nextLink"] =
3440 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3441 std::to_string(skip + top);
3442 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003443 },
Jonathan Doman15124762021-01-07 17:54:17 -08003444 "xyz.openbmc_project.State.Boot.PostCode0",
3445 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003446 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3447 bootIndex);
3448}
3449
zhanghch058d1b46d2021-04-01 11:18:24 +08003450static void
3451 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07003452 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003453{
3454 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003455 sdbusplus::asio::getProperty<uint16_t>(
3456 *crow::connections::systemBus,
3457 "xyz.openbmc_project.State.Boot.PostCode0",
3458 "/xyz/openbmc_project/State/Boot/PostCode0",
3459 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3460 [aResp, entryCount, skip, top](const boost::system::error_code ec,
3461 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003462 if (ec)
3463 {
3464 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3465 messages::internalError(aResp->res);
3466 return;
3467 }
3468 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003469 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003470}
3471
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003472inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003473{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003474 BMCWEB_ROUTE(app,
3475 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003476 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003477 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003478 [&app](const crow::Request& req,
3479 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003480 query_param::QueryCapabilities capabilities = {
3481 .canDelegateTop = true,
3482 .canDelegateSkip = true,
3483 };
3484 query_param::Query delegatedQuery;
3485 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003486 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003487 {
3488 return;
3489 }
3490 asyncResp->res.jsonValue["@odata.type"] =
3491 "#LogEntryCollection.LogEntryCollection";
3492 asyncResp->res.jsonValue["@odata.id"] =
3493 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3494 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3495 asyncResp->res.jsonValue["Description"] =
3496 "Collection of POST Code Log Entries";
3497 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3498 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07003499 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08003500 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07003501 getCurrentBootNumber(asyncResp, skip, top);
Ed Tanous002d39b2022-05-31 08:59:27 -07003502 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003503}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003504
George Liu647b3cd2021-07-05 12:43:56 +08003505/**
3506 * @brief Parse post code ID and get the current value and index value
3507 * eg: postCodeID=B1-2, currentValue=1, index=2
3508 *
3509 * @param[in] postCodeID Post Code ID
3510 * @param[out] currentValue Current value
3511 * @param[out] index Index value
3512 *
3513 * @return bool true if the parsing is successful, false the parsing fails
3514 */
3515inline static bool parsePostCode(const std::string& postCodeID,
3516 uint64_t& currentValue, uint16_t& index)
3517{
3518 std::vector<std::string> split;
3519 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3520 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3521 {
3522 return false;
3523 }
3524
Ed Tanousca45aa32022-01-07 09:28:45 -08003525 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003526 const char* start = split[0].data() + 1;
Ed Tanousca45aa32022-01-07 09:28:45 -08003527 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003528 const char* end = split[0].data() + split[0].size();
3529 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3530
3531 if (ptrIndex != end || ecIndex != std::errc())
3532 {
3533 return false;
3534 }
3535
3536 start = split[1].data();
Ed Tanousca45aa32022-01-07 09:28:45 -08003537
3538 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003539 end = split[1].data() + split[1].size();
3540 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
George Liu647b3cd2021-07-05 12:43:56 +08003541
Tony Lee517d9a52022-06-28 15:41:23 +08003542 return ptrValue == end && ecValue == std::errc();
George Liu647b3cd2021-07-05 12:43:56 +08003543}
3544
3545inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3546{
George Liu0fda0f12021-11-16 10:06:17 +08003547 BMCWEB_ROUTE(
3548 app,
3549 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003550 .privileges(redfish::privileges::getLogEntry)
3551 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003552 [&app](const crow::Request& req,
3553 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3554 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003555 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003556 {
3557 return;
3558 }
3559 if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
3560 {
3561 asyncResp->res.result(boost::beast::http::status::bad_request);
3562 return;
3563 }
George Liu647b3cd2021-07-05 12:43:56 +08003564
Ed Tanous002d39b2022-05-31 08:59:27 -07003565 uint64_t currentValue = 0;
3566 uint16_t index = 0;
3567 if (!parsePostCode(postCodeID, currentValue, index))
3568 {
3569 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3570 return;
3571 }
George Liu647b3cd2021-07-05 12:43:56 +08003572
Ed Tanous002d39b2022-05-31 08:59:27 -07003573 crow::connections::systemBus->async_method_call(
3574 [asyncResp, postCodeID, currentValue](
3575 const boost::system::error_code ec,
3576 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3577 postcodes) {
3578 if (ec.value() == EBADR)
3579 {
3580 messages::resourceNotFound(asyncResp->res, "LogEntry",
3581 postCodeID);
3582 return;
3583 }
3584 if (ec)
3585 {
3586 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3587 messages::internalError(asyncResp->res);
3588 return;
3589 }
George Liu647b3cd2021-07-05 12:43:56 +08003590
Ed Tanous002d39b2022-05-31 08:59:27 -07003591 size_t value = static_cast<size_t>(currentValue) - 1;
3592 if (value == std::string::npos || postcodes.size() < currentValue)
3593 {
3594 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3595 messages::resourceNotFound(asyncResp->res, "LogEntry",
3596 postCodeID);
3597 return;
3598 }
George Liu647b3cd2021-07-05 12:43:56 +08003599
Ed Tanous002d39b2022-05-31 08:59:27 -07003600 const auto& [tID, c] = postcodes[value];
3601 if (c.empty())
3602 {
3603 BMCWEB_LOG_INFO << "No found post code data";
3604 messages::resourceNotFound(asyncResp->res, "LogEntry",
3605 postCodeID);
3606 return;
3607 }
3608 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3609 const char* d = reinterpret_cast<const char*>(c.data());
3610 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08003611
Ed Tanousd9f6c622022-03-17 09:12:17 -07003612 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07003613 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07003614 asyncResp->res.addHeader(
3615 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous002d39b2022-05-31 08:59:27 -07003616 asyncResp->res.body() = crow::utility::base64encode(strData);
3617 },
3618 "xyz.openbmc_project.State.Boot.PostCode0",
3619 "/xyz/openbmc_project/State/Boot/PostCode0",
3620 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3621 });
George Liu647b3cd2021-07-05 12:43:56 +08003622}
3623
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003624inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003625{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003626 BMCWEB_ROUTE(
3627 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003628 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003629 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003630 [&app](const crow::Request& req,
3631 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3632 const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003633 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003634 {
3635 return;
3636 }
3637 uint16_t bootIndex = 0;
3638 uint64_t codeIndex = 0;
3639 if (!parsePostCode(targetID, codeIndex, bootIndex))
3640 {
3641 // Requested ID was not found
3642 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3643 return;
3644 }
3645 if (bootIndex == 0 || codeIndex == 0)
3646 {
3647 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3648 << targetID;
3649 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003650
Ed Tanous002d39b2022-05-31 08:59:27 -07003651 asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3652 asyncResp->res.jsonValue["@odata.id"] =
3653 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3654 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3655 asyncResp->res.jsonValue["Description"] =
3656 "Collection of POST Code Log Entries";
3657 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3658 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003659
Ed Tanous002d39b2022-05-31 08:59:27 -07003660 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3661 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003662}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003663
Ed Tanous1da66f72018-07-27 16:13:37 -07003664} // namespace redfish