blob: 074c927d4a1f9f50d4c2fa035e8949cd4a42bf9d [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>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060030#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070031#include <boost/algorithm/string/split.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060032#include <boost/beast/http.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070033#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080034#include <boost/system/linux_error.hpp>
Andrew Geisslercb92c032018-08-17 07:56:14 -070035#include <error_messages.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070036#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050037
George Liu647b3cd2021-07-05 12:43:56 +080038#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070039#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080040#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070041#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070042#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080043#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070044
45namespace redfish
46{
47
Gunnar Mills1214b7e2020-06-04 10:11:30 -050048constexpr char const* crashdumpObject = "com.intel.crashdump";
49constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050050constexpr char const* crashdumpInterface = "com.intel.crashdump";
51constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070052 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050053constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070054 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070055constexpr char const* crashdumpTelemetryInterface =
56 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070057
Jason M. Bills4851d452019-03-28 11:27:48 -070058namespace message_registries
59{
Ed Tanous26702d02021-11-03 15:02:33 -070060static const Message*
61 getMessageFromRegistry(const std::string& messageKey,
62 const std::span<const MessageEntry> registry)
Jason M. Bills4851d452019-03-28 11:27:48 -070063{
Ed Tanous26702d02021-11-03 15:02:33 -070064 std::span<const MessageEntry>::iterator messageIt = std::find_if(
65 registry.begin(), registry.end(),
66 [&messageKey](const MessageEntry& messageEntry) {
67 return !std::strcmp(messageEntry.first, messageKey.c_str());
68 });
69 if (messageIt != registry.end())
Jason M. Bills4851d452019-03-28 11:27:48 -070070 {
71 return &messageIt->second;
72 }
73
74 return nullptr;
75}
76
Gunnar Mills1214b7e2020-06-04 10:11:30 -050077static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070078{
79 // Redfish MessageIds are in the form
80 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
81 // the right Message
82 std::vector<std::string> fields;
83 fields.reserve(4);
84 boost::split(fields, messageID, boost::is_any_of("."));
Gunnar Mills1214b7e2020-06-04 10:11:30 -050085 std::string& registryName = fields[0];
86 std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -070087
88 // Find the right registry and check it for the MessageKey
89 if (std::string(base::header.registryPrefix) == registryName)
90 {
91 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070092 messageKey, std::span<const MessageEntry>(base::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070093 }
94 if (std::string(openbmc::header.registryPrefix) == registryName)
95 {
96 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070097 messageKey, std::span<const MessageEntry>(openbmc::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070098 }
99 return nullptr;
100}
101} // namespace message_registries
102
James Feistf6150402019-01-08 10:36:20 -0800103namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700104
Andrew Geisslercb92c032018-08-17 07:56:14 -0700105using GetManagedPropertyType = boost::container::flat_map<
Patrick Williams19bd78d2020-05-13 17:38:24 -0500106 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
107 int32_t, uint32_t, int64_t, uint64_t, double>>;
Andrew Geisslercb92c032018-08-17 07:56:14 -0700108
109using GetManagedObjectsType = boost::container::flat_map<
110 sdbusplus::message::object_path,
111 boost::container::flat_map<std::string, GetManagedPropertyType>>;
112
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 Tanous271584a2019-07-09 16:24:22 -0700143 ret = sd_journal_get_data(journal, field.data(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500144 reinterpret_cast<const void**>(&data), &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700145 if (ret < 0)
146 {
147 return ret;
148 }
Ed Tanous39e77502019-03-04 17:35:53 -0800149 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700150 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000151 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700152 return ret;
153}
154
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700155inline static int getJournalMetadata(sd_journal* journal,
156 const std::string_view& field,
157 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700158{
159 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800160 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700161 // Get the metadata from the requested field of the journal entry
162 ret = getJournalMetadata(journal, field, metadata);
163 if (ret < 0)
164 {
165 return ret;
166 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000167 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700168 return ret;
169}
170
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700171inline static bool getEntryTimestamp(sd_journal* journal,
172 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800173{
174 int ret = 0;
175 uint64_t timestamp = 0;
176 ret = sd_journal_get_realtime_usec(journal, &timestamp);
177 if (ret < 0)
178 {
179 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
180 << strerror(-ret);
181 return false;
182 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800183 entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500184 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800185}
186
zhanghch058d1b46d2021-04-01 11:18:24 +0800187static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
188 const crow::Request& req, uint64_t& skip)
Jason M. Bills16428a12018-11-02 12:42:29 -0700189{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700190 boost::urls::query_params_view::iterator it = req.urlParams.find("$skip");
James Feist5a7e8772020-07-22 09:08:38 -0700191 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700192 {
James Feist5a7e8772020-07-22 09:08:38 -0700193 std::string skipParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500194 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700195 skip = std::strtoul(skipParam.c_str(), &ptr, 10);
196 if (skipParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700197 {
198
zhanghch058d1b46d2021-04-01 11:18:24 +0800199 messages::queryParameterValueTypeError(
200 asyncResp->res, std::string(skipParam), "$skip");
Jason M. Bills16428a12018-11-02 12:42:29 -0700201 return false;
202 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700203 }
204 return true;
205}
206
Ed Tanous271584a2019-07-09 16:24:22 -0700207static constexpr const uint64_t maxEntriesPerPage = 1000;
zhanghch058d1b46d2021-04-01 11:18:24 +0800208static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
209 const crow::Request& req, uint64_t& top)
Jason M. Bills16428a12018-11-02 12:42:29 -0700210{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700211 boost::urls::query_params_view::iterator it = req.urlParams.find("$top");
James Feist5a7e8772020-07-22 09:08:38 -0700212 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700213 {
James Feist5a7e8772020-07-22 09:08:38 -0700214 std::string topParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500215 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700216 top = std::strtoul(topParam.c_str(), &ptr, 10);
217 if (topParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700218 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800219 messages::queryParameterValueTypeError(
220 asyncResp->res, std::string(topParam), "$top");
Jason M. Bills16428a12018-11-02 12:42:29 -0700221 return false;
222 }
Ed Tanous271584a2019-07-09 16:24:22 -0700223 if (top < 1U || top > maxEntriesPerPage)
Jason M. Bills16428a12018-11-02 12:42:29 -0700224 {
225
226 messages::queryParameterOutOfRange(
zhanghch058d1b46d2021-04-01 11:18:24 +0800227 asyncResp->res, std::to_string(top), "$top",
Jason M. Bills16428a12018-11-02 12:42:29 -0700228 "1-" + std::to_string(maxEntriesPerPage));
229 return false;
230 }
231 }
232 return true;
233}
234
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700235inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
236 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700237{
238 int ret = 0;
239 static uint64_t prevTs = 0;
240 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700241 if (firstEntry)
242 {
243 prevTs = 0;
244 }
245
Jason M. Bills16428a12018-11-02 12:42:29 -0700246 // Get the entry timestamp
247 uint64_t curTs = 0;
248 ret = sd_journal_get_realtime_usec(journal, &curTs);
249 if (ret < 0)
250 {
251 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
252 << strerror(-ret);
253 return false;
254 }
255 // If the timestamp isn't unique, increment the index
256 if (curTs == prevTs)
257 {
258 index++;
259 }
260 else
261 {
262 // Otherwise, reset it
263 index = 0;
264 }
265 // Save the timestamp
266 prevTs = curTs;
267
268 entryID = std::to_string(curTs);
269 if (index > 0)
270 {
271 entryID += "_" + std::to_string(index);
272 }
273 return true;
274}
275
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500276static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700277 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700278{
Ed Tanous271584a2019-07-09 16:24:22 -0700279 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700280 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700281 if (firstEntry)
282 {
283 prevTs = 0;
284 }
285
Jason M. Bills95820182019-04-22 16:25:34 -0700286 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700287 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700288 std::tm timeStruct = {};
289 std::istringstream entryStream(logEntry);
290 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
291 {
292 curTs = std::mktime(&timeStruct);
293 }
294 // If the timestamp isn't unique, increment the index
295 if (curTs == prevTs)
296 {
297 index++;
298 }
299 else
300 {
301 // Otherwise, reset it
302 index = 0;
303 }
304 // Save the timestamp
305 prevTs = curTs;
306
307 entryID = std::to_string(curTs);
308 if (index > 0)
309 {
310 entryID += "_" + std::to_string(index);
311 }
312 return true;
313}
314
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700315inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800316 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
317 const std::string& entryID, uint64_t& timestamp,
318 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700319{
320 if (entryID.empty())
321 {
322 return false;
323 }
324 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800325 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700326
Ed Tanous81ce6092020-12-17 16:54:55 +0000327 auto underscorePos = tsStr.find('_');
Jason M. Bills16428a12018-11-02 12:42:29 -0700328 if (underscorePos != tsStr.npos)
329 {
330 // Timestamp has an index
331 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800332 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700333 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700334 auto [ptr, ec] = std::from_chars(
335 indexStr.data(), indexStr.data() + indexStr.size(), index);
336 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700337 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800338 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700339 return false;
340 }
341 }
342 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700343 auto [ptr, ec] =
344 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
345 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700346 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800347 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700348 return false;
349 }
350 return true;
351}
352
Jason M. Bills95820182019-04-22 16:25:34 -0700353static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500354 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700355{
356 static const std::filesystem::path redfishLogDir = "/var/log";
357 static const std::string redfishLogFilename = "redfish";
358
359 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500360 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700361 std::filesystem::directory_iterator(redfishLogDir))
362 {
363 // If we find a redfish log file, save the path
364 std::string filename = dirEnt.path().filename();
365 if (boost::starts_with(filename, redfishLogFilename))
366 {
367 redfishLogFiles.emplace_back(redfishLogDir / filename);
368 }
369 }
370 // As the log files rotate, they are appended with a ".#" that is higher for
371 // the older logs. Since we don't expect more than 10 log files, we
372 // can just sort the list to get them in order from newest to oldest
373 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
374
375 return !redfishLogFiles.empty();
376}
377
zhanghch058d1b46d2021-04-01 11:18:24 +0800378inline void
379 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
380 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500381{
382 std::string dumpPath;
383 if (dumpType == "BMC")
384 {
385 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
386 }
387 else if (dumpType == "System")
388 {
389 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
390 }
391 else
392 {
393 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
394 messages::internalError(asyncResp->res);
395 return;
396 }
397
398 crow::connections::systemBus->async_method_call(
399 [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
400 GetManagedObjectsType& resp) {
401 if (ec)
402 {
403 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
404 messages::internalError(asyncResp->res);
405 return;
406 }
407
408 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
409 entriesArray = nlohmann::json::array();
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500410 std::string dumpEntryPath =
411 "/xyz/openbmc_project/dump/" +
412 std::string(boost::algorithm::to_lower_copy(dumpType)) +
413 "/entry/";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500414
415 for (auto& object : resp)
416 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500417 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500418 {
419 continue;
420 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800421 uint64_t timestamp = 0;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500422 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500423 std::string dumpStatus;
424 nlohmann::json thisEntry;
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000425
426 std::string entryID = object.first.filename();
427 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500428 {
429 continue;
430 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500431
432 for (auto& interfaceMap : object.second)
433 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500434 if (interfaceMap.first ==
435 "xyz.openbmc_project.Common.Progress")
436 {
437 for (auto& propertyMap : interfaceMap.second)
438 {
439 if (propertyMap.first == "Status")
440 {
441 auto status = std::get_if<std::string>(
442 &propertyMap.second);
443 if (status == nullptr)
444 {
445 messages::internalError(asyncResp->res);
446 break;
447 }
448 dumpStatus = *status;
449 }
450 }
451 }
452 else if (interfaceMap.first ==
453 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500454 {
455
456 for (auto& propertyMap : interfaceMap.second)
457 {
458 if (propertyMap.first == "Size")
459 {
460 auto sizePtr =
461 std::get_if<uint64_t>(&propertyMap.second);
462 if (sizePtr == nullptr)
463 {
464 messages::internalError(asyncResp->res);
465 break;
466 }
467 size = *sizePtr;
468 break;
469 }
470 }
471 }
472 else if (interfaceMap.first ==
473 "xyz.openbmc_project.Time.EpochTime")
474 {
475
476 for (auto& propertyMap : interfaceMap.second)
477 {
478 if (propertyMap.first == "Elapsed")
479 {
480 const uint64_t* usecsTimeStamp =
481 std::get_if<uint64_t>(&propertyMap.second);
482 if (usecsTimeStamp == nullptr)
483 {
484 messages::internalError(asyncResp->res);
485 break;
486 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800487 timestamp = (*usecsTimeStamp / 1000 / 1000);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500488 break;
489 }
490 }
491 }
492 }
493
George Liu0fda0f12021-11-16 10:06:17 +0800494 if (dumpStatus !=
495 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500496 !dumpStatus.empty())
497 {
498 // Dump status is not Complete, no need to enumerate
499 continue;
500 }
501
George Liu647b3cd2021-07-05 12:43:56 +0800502 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500503 thisEntry["@odata.id"] = dumpPath + entryID;
504 thisEntry["Id"] = entryID;
505 thisEntry["EntryType"] = "Event";
Nan Zhou1d8782e2021-11-29 22:23:18 -0800506 thisEntry["Created"] =
507 crow::utility::getDateTimeUint(timestamp);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500508 thisEntry["Name"] = dumpType + " Dump Entry";
509
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500510 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500511
512 if (dumpType == "BMC")
513 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500514 thisEntry["DiagnosticDataType"] = "Manager";
515 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500516 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
517 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500518 }
519 else if (dumpType == "System")
520 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500521 thisEntry["DiagnosticDataType"] = "OEM";
522 thisEntry["OEMDiagnosticDataType"] = "System";
523 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500524 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
525 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500526 }
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500527 entriesArray.push_back(std::move(thisEntry));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500528 }
529 asyncResp->res.jsonValue["Members@odata.count"] =
530 entriesArray.size();
531 },
532 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
533 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
534}
535
zhanghch058d1b46d2021-04-01 11:18:24 +0800536inline void
537 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
538 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500539{
540 std::string dumpPath;
541 if (dumpType == "BMC")
542 {
543 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
544 }
545 else if (dumpType == "System")
546 {
547 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
548 }
549 else
550 {
551 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
552 messages::internalError(asyncResp->res);
553 return;
554 }
555
556 crow::connections::systemBus->async_method_call(
557 [asyncResp, entryID, dumpPath, dumpType](
558 const boost::system::error_code ec, GetManagedObjectsType& resp) {
559 if (ec)
560 {
561 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
562 messages::internalError(asyncResp->res);
563 return;
564 }
565
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500566 bool foundDumpEntry = false;
567 std::string dumpEntryPath =
568 "/xyz/openbmc_project/dump/" +
569 std::string(boost::algorithm::to_lower_copy(dumpType)) +
570 "/entry/";
571
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500572 for (auto& objectPath : resp)
573 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500574 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500575 {
576 continue;
577 }
578
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500579 foundDumpEntry = true;
Nan Zhou1d8782e2021-11-29 22:23:18 -0800580 uint64_t timestamp = 0;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500582 std::string dumpStatus;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500583
584 for (auto& interfaceMap : objectPath.second)
585 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500586 if (interfaceMap.first ==
587 "xyz.openbmc_project.Common.Progress")
588 {
589 for (auto& propertyMap : interfaceMap.second)
590 {
591 if (propertyMap.first == "Status")
592 {
593 auto status = std::get_if<std::string>(
594 &propertyMap.second);
595 if (status == nullptr)
596 {
597 messages::internalError(asyncResp->res);
598 break;
599 }
600 dumpStatus = *status;
601 }
602 }
603 }
604 else if (interfaceMap.first ==
605 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500606 {
607 for (auto& propertyMap : interfaceMap.second)
608 {
609 if (propertyMap.first == "Size")
610 {
611 auto sizePtr =
612 std::get_if<uint64_t>(&propertyMap.second);
613 if (sizePtr == nullptr)
614 {
615 messages::internalError(asyncResp->res);
616 break;
617 }
618 size = *sizePtr;
619 break;
620 }
621 }
622 }
623 else if (interfaceMap.first ==
624 "xyz.openbmc_project.Time.EpochTime")
625 {
626 for (auto& propertyMap : interfaceMap.second)
627 {
628 if (propertyMap.first == "Elapsed")
629 {
630 const uint64_t* usecsTimeStamp =
631 std::get_if<uint64_t>(&propertyMap.second);
632 if (usecsTimeStamp == nullptr)
633 {
634 messages::internalError(asyncResp->res);
635 break;
636 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800637 timestamp = *usecsTimeStamp / 1000 / 1000;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500638 break;
639 }
640 }
641 }
642 }
643
George Liu0fda0f12021-11-16 10:06:17 +0800644 if (dumpStatus !=
645 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500646 !dumpStatus.empty())
647 {
648 // Dump status is not Complete
649 // return not found until status is changed to Completed
650 messages::resourceNotFound(asyncResp->res,
651 dumpType + " dump", entryID);
652 return;
653 }
654
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500655 asyncResp->res.jsonValue["@odata.type"] =
George Liu647b3cd2021-07-05 12:43:56 +0800656 "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500657 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
658 asyncResp->res.jsonValue["Id"] = entryID;
659 asyncResp->res.jsonValue["EntryType"] = "Event";
660 asyncResp->res.jsonValue["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -0800661 crow::utility::getDateTimeUint(timestamp);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500662 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
663
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500664 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500665
666 if (dumpType == "BMC")
667 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500668 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
669 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500670 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
671 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500672 }
673 else if (dumpType == "System")
674 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500675 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
676 asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500677 "System";
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500678 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500679 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
680 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500681 }
682 }
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500683 if (foundDumpEntry == false)
684 {
685 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
686 messages::internalError(asyncResp->res);
687 return;
688 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500689 },
690 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
691 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
692}
693
zhanghch058d1b46d2021-04-01 11:18:24 +0800694inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800695 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500696 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500697{
George Liu3de8d8b2021-03-22 17:49:39 +0800698 auto respHandler = [asyncResp,
699 entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500700 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
701 if (ec)
702 {
George Liu3de8d8b2021-03-22 17:49:39 +0800703 if (ec.value() == EBADR)
704 {
705 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
706 return;
707 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500708 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
709 << ec;
710 messages::internalError(asyncResp->res);
711 return;
712 }
713 };
714 crow::connections::systemBus->async_method_call(
715 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500716 "/xyz/openbmc_project/dump/" +
717 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
718 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500719 "xyz.openbmc_project.Object.Delete", "Delete");
720}
721
zhanghch058d1b46d2021-04-01 11:18:24 +0800722inline void
Ed Tanous98be3e32021-09-16 15:05:36 -0700723 createDumpTaskCallback(task::Payload&& payload,
zhanghch058d1b46d2021-04-01 11:18:24 +0800724 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
725 const uint32_t& dumpId, const std::string& dumpPath,
726 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500727{
728 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500729 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500730 boost::system::error_code err, sdbusplus::message::message& m,
731 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000732 if (err)
733 {
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500734 BMCWEB_LOG_ERROR << "Error in creating a dump";
735 taskData->state = "Cancelled";
736 return task::completed;
Ed Tanouscb13a392020-07-25 19:02:03 +0000737 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500738 std::vector<std::pair<
739 std::string,
740 std::vector<std::pair<std::string, std::variant<std::string>>>>>
741 interfacesList;
742
743 sdbusplus::message::object_path objPath;
744
745 m.read(objPath, interfacesList);
746
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500747 if (objPath.str ==
748 "/xyz/openbmc_project/dump/" +
749 std::string(boost::algorithm::to_lower_copy(dumpType)) +
750 "/entry/" + std::to_string(dumpId))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500751 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500752 nlohmann::json retMessage = messages::success();
753 taskData->messages.emplace_back(retMessage);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500754
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500755 std::string headerLoc =
756 "Location: " + dumpPath + std::to_string(dumpId);
757 taskData->payload->httpHeaders.emplace_back(
758 std::move(headerLoc));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500759
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500760 taskData->state = "Completed";
761 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500762 }
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500763 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500764 },
765 "type='signal',interface='org.freedesktop.DBus."
766 "ObjectManager',"
767 "member='InterfacesAdded', "
768 "path='/xyz/openbmc_project/dump'");
769
770 task->startTimer(std::chrono::minutes(3));
771 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -0700772 task->payload.emplace(std::move(payload));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500773}
774
zhanghch058d1b46d2021-04-01 11:18:24 +0800775inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
776 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500777{
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500778
779 std::string dumpPath;
780 if (dumpType == "BMC")
781 {
782 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
783 }
784 else if (dumpType == "System")
785 {
786 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
787 }
788 else
789 {
790 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
791 messages::internalError(asyncResp->res);
792 return;
793 }
794
795 std::optional<std::string> diagnosticDataType;
796 std::optional<std::string> oemDiagnosticDataType;
797
798 if (!redfish::json_util::readJson(
799 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
800 "OEMDiagnosticDataType", oemDiagnosticDataType))
801 {
802 return;
803 }
804
805 if (dumpType == "System")
806 {
807 if (!oemDiagnosticDataType || !diagnosticDataType)
808 {
809 BMCWEB_LOG_ERROR << "CreateDump action parameter "
810 "'DiagnosticDataType'/"
811 "'OEMDiagnosticDataType' value not found!";
812 messages::actionParameterMissing(
813 asyncResp->res, "CollectDiagnosticData",
814 "DiagnosticDataType & OEMDiagnosticDataType");
815 return;
816 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700817 if ((*oemDiagnosticDataType != "System") ||
818 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500819 {
820 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
821 messages::invalidObject(asyncResp->res,
822 "System Dump creation parameters");
823 return;
824 }
825 }
826 else if (dumpType == "BMC")
827 {
828 if (!diagnosticDataType)
829 {
George Liu0fda0f12021-11-16 10:06:17 +0800830 BMCWEB_LOG_ERROR
831 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500832 messages::actionParameterMissing(
833 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
834 return;
835 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700836 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500837 {
838 BMCWEB_LOG_ERROR
839 << "Wrong parameter value passed for 'DiagnosticDataType'";
840 messages::invalidObject(asyncResp->res,
841 "BMC Dump creation parameters");
842 return;
843 }
844 }
845
846 crow::connections::systemBus->async_method_call(
Ed Tanous98be3e32021-09-16 15:05:36 -0700847 [asyncResp, payload(task::Payload(req)), dumpPath,
848 dumpType](const boost::system::error_code ec,
849 const uint32_t& dumpId) mutable {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500850 if (ec)
851 {
852 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
853 messages::internalError(asyncResp->res);
854 return;
855 }
856 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
857
Ed Tanous98be3e32021-09-16 15:05:36 -0700858 createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
859 dumpPath, dumpType);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500860 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500861 "xyz.openbmc_project.Dump.Manager",
862 "/xyz/openbmc_project/dump/" +
863 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500864 "xyz.openbmc_project.Dump.Create", "CreateDump");
865}
866
zhanghch058d1b46d2021-04-01 11:18:24 +0800867inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
868 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500869{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500870 std::string dumpTypeLowerCopy =
871 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800872
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500873 crow::connections::systemBus->async_method_call(
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500874 [asyncResp, dumpType](const boost::system::error_code ec,
875 const std::vector<std::string>& subTreePaths) {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500876 if (ec)
877 {
878 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
879 messages::internalError(asyncResp->res);
880 return;
881 }
882
883 for (const std::string& path : subTreePaths)
884 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000885 sdbusplus::message::object_path objPath(path);
886 std::string logID = objPath.filename();
887 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500888 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000889 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500890 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000891 deleteDumpEntry(asyncResp, logID, dumpType);
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500892 }
893 },
894 "xyz.openbmc_project.ObjectMapper",
895 "/xyz/openbmc_project/object_mapper",
896 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500897 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
898 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
899 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500900}
901
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700902inline static void parseCrashdumpParameters(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500903 const std::vector<std::pair<std::string, VariantType>>& params,
904 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700905{
906 for (auto property : params)
907 {
908 if (property.first == "Timestamp")
909 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500910 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500911 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700912 if (value != nullptr)
913 {
914 timestamp = *value;
915 }
916 }
917 else if (property.first == "Filename")
918 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500919 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500920 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700921 if (value != nullptr)
922 {
923 filename = *value;
924 }
925 }
926 else if (property.first == "Log")
927 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500928 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500929 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700930 if (value != nullptr)
931 {
932 logfile = *value;
933 }
934 }
935 }
936}
937
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500938constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700939inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700940{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800941 /**
942 * Functions triggers appropriate requests on DBus
943 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700944 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700945 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700946 .methods(boost::beast::http::verb::get)(
947 [](const crow::Request&,
948 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
949
950 {
951 // Collections don't include the static data added by SubRoute
952 // because it has a duplicate entry for members
953 asyncResp->res.jsonValue["@odata.type"] =
954 "#LogServiceCollection.LogServiceCollection";
955 asyncResp->res.jsonValue["@odata.id"] =
956 "/redfish/v1/Systems/system/LogServices";
957 asyncResp->res.jsonValue["Name"] =
958 "System Log Services Collection";
959 asyncResp->res.jsonValue["Description"] =
960 "Collection of LogServices for this Computer System";
961 nlohmann::json& logServiceArray =
962 asyncResp->res.jsonValue["Members"];
963 logServiceArray = nlohmann::json::array();
964 logServiceArray.push_back(
965 {{"@odata.id",
966 "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500967#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700968 logServiceArray.push_back(
969 {{"@odata.id",
970 "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600971#endif
972
Jason M. Billsd53dd412019-02-12 17:16:22 -0800973#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700974 logServiceArray.push_back(
975 {{"@odata.id",
976 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800977#endif
Spencer Kub7028eb2021-10-26 15:27:35 +0800978
979#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
980 logServiceArray.push_back(
981 {{"@odata.id",
982 "/redfish/v1/Systems/system/LogServices/HostLogger"}});
983#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700984 asyncResp->res.jsonValue["Members@odata.count"] =
985 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800986
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700987 crow::connections::systemBus->async_method_call(
988 [asyncResp](const boost::system::error_code ec,
989 const std::vector<std::string>& subtreePath) {
990 if (ec)
991 {
992 BMCWEB_LOG_ERROR << ec;
993 return;
994 }
ZhikuiRena3316fc2020-01-29 14:58:08 -0800995
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700996 for (auto& pathStr : subtreePath)
997 {
998 if (pathStr.find("PostCode") != std::string::npos)
999 {
1000 nlohmann::json& logServiceArrayLocal =
1001 asyncResp->res.jsonValue["Members"];
1002 logServiceArrayLocal.push_back(
George Liu0fda0f12021-11-16 10:06:17 +08001003 {{"@odata.id",
1004 "/redfish/v1/Systems/system/LogServices/PostCodes"}});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001005 asyncResp->res
1006 .jsonValue["Members@odata.count"] =
1007 logServiceArrayLocal.size();
1008 return;
1009 }
1010 }
1011 },
1012 "xyz.openbmc_project.ObjectMapper",
1013 "/xyz/openbmc_project/object_mapper",
1014 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1015 0, std::array<const char*, 1>{postCodeIface});
1016 });
1017}
1018
1019inline void requestRoutesEventLogService(App& app)
1020{
1021 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001022 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001023 .methods(
1024 boost::beast::http::verb::
1025 get)([](const crow::Request&,
1026 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1027 asyncResp->res.jsonValue["@odata.id"] =
1028 "/redfish/v1/Systems/system/LogServices/EventLog";
1029 asyncResp->res.jsonValue["@odata.type"] =
1030 "#LogService.v1_1_0.LogService";
1031 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1032 asyncResp->res.jsonValue["Description"] =
1033 "System Event Log Service";
1034 asyncResp->res.jsonValue["Id"] = "EventLog";
1035 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301036
1037 std::pair<std::string, std::string> redfishDateTimeOffset =
1038 crow::utility::getDateTimeOffsetNow();
1039
1040 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1041 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1042 redfishDateTimeOffset.second;
1043
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001044 asyncResp->res.jsonValue["Entries"] = {
1045 {"@odata.id",
1046 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1047 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1048
George Liu0fda0f12021-11-16 10:06:17 +08001049 {"target",
1050 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001051 });
1052}
1053
1054inline void requestRoutesJournalEventLogClear(App& app)
1055{
1056 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1057 "LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001058 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001059 .methods(boost::beast::http::verb::post)(
1060 [](const crow::Request&,
1061 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1062 // Clear the EventLog by deleting the log files
1063 std::vector<std::filesystem::path> redfishLogFiles;
1064 if (getRedfishLogFiles(redfishLogFiles))
ZhikuiRena3316fc2020-01-29 14:58:08 -08001065 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001066 for (const std::filesystem::path& file : redfishLogFiles)
ZhikuiRena3316fc2020-01-29 14:58:08 -08001067 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001068 std::error_code ec;
1069 std::filesystem::remove(file, ec);
ZhikuiRena3316fc2020-01-29 14:58:08 -08001070 }
1071 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001072
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001073 // Reload rsyslog so it knows to start new log files
1074 crow::connections::systemBus->async_method_call(
1075 [asyncResp](const boost::system::error_code ec) {
1076 if (ec)
1077 {
1078 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1079 << ec;
1080 messages::internalError(asyncResp->res);
1081 return;
1082 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001083
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001084 messages::success(asyncResp->res);
1085 },
1086 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1087 "org.freedesktop.systemd1.Manager", "ReloadUnit",
1088 "rsyslog.service", "replace");
1089 });
1090}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001091
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001092static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001093 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001094 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001095{
Jason M. Bills95820182019-04-22 16:25:34 -07001096 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001097 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001098 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001099 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001100 {
1101 return 1;
1102 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001103 std::string timestamp = logEntry.substr(0, space);
1104 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001105 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001106 if (entryStart == std::string::npos)
1107 {
1108 return 1;
1109 }
1110 std::string_view entry(logEntry);
1111 entry.remove_prefix(entryStart);
1112 // Use split to separate the entry into its fields
1113 std::vector<std::string> logEntryFields;
1114 boost::split(logEntryFields, entry, boost::is_any_of(","),
1115 boost::token_compress_on);
1116 // We need at least a MessageId to be valid
1117 if (logEntryFields.size() < 1)
1118 {
1119 return 1;
1120 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001121 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001122
Jason M. Bills4851d452019-03-28 11:27:48 -07001123 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001124 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001125 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001126
Jason M. Bills4851d452019-03-28 11:27:48 -07001127 std::string msg;
1128 std::string severity;
1129 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001130 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001131 msg = message->message;
1132 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001133 }
1134
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001135 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001136 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001137 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001138 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001139 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001140 // If the first string is empty, assume there are no MessageArgs
1141 std::size_t messageArgsSize = 0;
1142 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001143 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001144 messageArgsSize = logEntryFields.size() - 1;
1145 }
1146
Ed Tanous23a21a12020-07-25 04:45:05 +00001147 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001148
1149 // Fill the MessageArgs into the Message
1150 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001151 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001152 {
1153 std::string argStr = "%" + std::to_string(++i);
1154 size_t argPos = msg.find(argStr);
1155 if (argPos != std::string::npos)
1156 {
1157 msg.replace(argPos, argStr.length(), messageArg);
1158 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001159 }
1160 }
1161
Jason M. Bills95820182019-04-22 16:25:34 -07001162 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1163 // format which matches the Redfish format except for the fractional seconds
1164 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001165 std::size_t dot = timestamp.find_first_of('.');
1166 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001167 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001168 {
Jason M. Bills95820182019-04-22 16:25:34 -07001169 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001170 }
1171
1172 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001173 logEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08001174 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001175 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001176 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001177 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001178 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001179 {"Id", logEntryID},
1180 {"Message", std::move(msg)},
1181 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001182 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001183 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001184 {"Severity", std::move(severity)},
1185 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001186 return 0;
1187}
1188
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001189inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001190{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001191 BMCWEB_ROUTE(app,
1192 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001193 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001194 .methods(boost::beast::http::verb::get)(
1195 [](const crow::Request& req,
1196 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1197 uint64_t skip = 0;
1198 uint64_t top = maxEntriesPerPage; // Show max entries by default
1199 if (!getSkipParam(asyncResp, req, skip))
Jason M. Bills95820182019-04-22 16:25:34 -07001200 {
Jason M. Bills95820182019-04-22 16:25:34 -07001201 return;
1202 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001203 if (!getTopParam(asyncResp, req, top))
Jason M. Bills897967d2019-07-29 17:05:30 -07001204 {
Jason M. Bills897967d2019-07-29 17:05:30 -07001205 return;
1206 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001207 // Collections don't include the static data added by SubRoute
1208 // because it has a duplicate entry for members
1209 asyncResp->res.jsonValue["@odata.type"] =
1210 "#LogEntryCollection.LogEntryCollection";
1211 asyncResp->res.jsonValue["@odata.id"] =
1212 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1213 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1214 asyncResp->res.jsonValue["Description"] =
1215 "Collection of System Event Log Entries";
Jason M. Bills897967d2019-07-29 17:05:30 -07001216
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001217 nlohmann::json& logEntryArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001218 asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001219 logEntryArray = nlohmann::json::array();
1220 // Go through the log files and create a unique ID for each
1221 // entry
1222 std::vector<std::filesystem::path> redfishLogFiles;
1223 getRedfishLogFiles(redfishLogFiles);
1224 uint64_t entryCount = 0;
1225 std::string logEntry;
1226
1227 // Oldest logs are in the last file, so start there and loop
1228 // backwards
1229 for (auto it = redfishLogFiles.rbegin();
1230 it < redfishLogFiles.rend(); it++)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001231 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001232 std::ifstream logStream(*it);
1233 if (!logStream.is_open())
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001234 {
1235 continue;
1236 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001237
1238 // Reset the unique ID on the first entry
1239 bool firstEntry = true;
1240 while (std::getline(logStream, logEntry))
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001241 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001242 entryCount++;
1243 // Handle paging using skip (number of entries to skip
1244 // from the start) and top (number of entries to
1245 // display)
1246 if (entryCount <= skip || entryCount > skip + top)
George Liuebd45902020-08-26 14:21:10 +08001247 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001248 continue;
George Liuebd45902020-08-26 14:21:10 +08001249 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001250
1251 std::string idStr;
1252 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
George Liuebd45902020-08-26 14:21:10 +08001253 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001254 continue;
George Liuebd45902020-08-26 14:21:10 +08001255 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001256
1257 if (firstEntry)
1258 {
1259 firstEntry = false;
1260 }
1261
1262 logEntryArray.push_back({});
1263 nlohmann::json& bmcLogEntry = logEntryArray.back();
1264 if (fillEventLogEntryJson(idStr, logEntry,
1265 bmcLogEntry) != 0)
Xiaochao Ma75710de2021-01-21 17:56:02 +08001266 {
1267 messages::internalError(asyncResp->res);
1268 return;
1269 }
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001270 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001271 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001272 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1273 if (skip + top < entryCount)
Ed Tanous271584a2019-07-09 16:24:22 -07001274 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001275 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001276 "/redfish/v1/Systems/system/LogServices/EventLog/"
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001277 "Entries?$skip=" +
1278 std::to_string(skip + top);
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001279 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001280 });
1281}
Chicago Duan336e96c2019-07-15 14:22:08 +08001282
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001283inline void requestRoutesJournalEventLogEntry(App& app)
1284{
1285 BMCWEB_ROUTE(
1286 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001287 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001288 .methods(boost::beast::http::verb::get)(
1289 [](const crow::Request&,
1290 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1291 const std::string& param) {
1292 const std::string& targetID = param;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001293
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001294 // Go through the log files and check the unique ID for each
1295 // entry to find the target entry
1296 std::vector<std::filesystem::path> redfishLogFiles;
1297 getRedfishLogFiles(redfishLogFiles);
1298 std::string logEntry;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001299
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001300 // Oldest logs are in the last file, so start there and loop
1301 // backwards
1302 for (auto it = redfishLogFiles.rbegin();
1303 it < redfishLogFiles.rend(); it++)
1304 {
1305 std::ifstream logStream(*it);
1306 if (!logStream.is_open())
1307 {
1308 continue;
1309 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001310
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001311 // Reset the unique ID on the first entry
1312 bool firstEntry = true;
1313 while (std::getline(logStream, logEntry))
1314 {
1315 std::string idStr;
1316 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1317 {
1318 continue;
1319 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001320
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001321 if (firstEntry)
1322 {
1323 firstEntry = false;
1324 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001325
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001326 if (idStr == targetID)
1327 {
1328 if (fillEventLogEntryJson(
1329 idStr, logEntry,
1330 asyncResp->res.jsonValue) != 0)
1331 {
1332 messages::internalError(asyncResp->res);
1333 return;
1334 }
1335 return;
1336 }
1337 }
1338 }
1339 // Requested ID was not found
1340 messages::resourceMissingAtURI(asyncResp->res, targetID);
1341 });
1342}
1343
1344inline void requestRoutesDBusEventLogEntryCollection(App& app)
1345{
1346 BMCWEB_ROUTE(app,
1347 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001348 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001349 .methods(
1350 boost::beast::http::verb::
1351 get)([](const crow::Request&,
1352 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1353 // Collections don't include the static data added by SubRoute
1354 // because it has a duplicate entry for members
1355 asyncResp->res.jsonValue["@odata.type"] =
1356 "#LogEntryCollection.LogEntryCollection";
1357 asyncResp->res.jsonValue["@odata.id"] =
1358 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1359 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1360 asyncResp->res.jsonValue["Description"] =
1361 "Collection of System Event Log Entries";
1362
1363 // DBus implementation of EventLog/Entries
1364 // Make call to Logging Service to find all log entry objects
Xiaochao Ma75710de2021-01-21 17:56:02 +08001365 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001366 [asyncResp](const boost::system::error_code ec,
1367 GetManagedObjectsType& resp) {
Xiaochao Ma75710de2021-01-21 17:56:02 +08001368 if (ec)
1369 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001370 // TODO Handle for specific error code
1371 BMCWEB_LOG_ERROR
1372 << "getLogEntriesIfaceData resp_handler got error "
1373 << ec;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001374 messages::internalError(asyncResp->res);
1375 return;
1376 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001377 nlohmann::json& entriesArray =
1378 asyncResp->res.jsonValue["Members"];
1379 entriesArray = nlohmann::json::array();
1380 for (auto& objectPath : resp)
1381 {
1382 uint32_t* id = nullptr;
1383 std::time_t timestamp{};
1384 std::time_t updateTimestamp{};
1385 std::string* severity = nullptr;
1386 std::string* message = nullptr;
1387 std::string* filePath = nullptr;
1388 bool resolved = false;
1389 for (auto& interfaceMap : objectPath.second)
1390 {
1391 if (interfaceMap.first ==
1392 "xyz.openbmc_project.Logging.Entry")
1393 {
1394 for (auto& propertyMap : interfaceMap.second)
1395 {
1396 if (propertyMap.first == "Id")
1397 {
1398 id = std::get_if<uint32_t>(
1399 &propertyMap.second);
1400 }
1401 else if (propertyMap.first == "Timestamp")
1402 {
1403 const uint64_t* millisTimeStamp =
1404 std::get_if<uint64_t>(
1405 &propertyMap.second);
1406 if (millisTimeStamp != nullptr)
1407 {
1408 timestamp =
1409 crow::utility::getTimestamp(
1410 *millisTimeStamp);
1411 }
1412 }
1413 else if (propertyMap.first ==
1414 "UpdateTimestamp")
1415 {
1416 const uint64_t* millisTimeStamp =
1417 std::get_if<uint64_t>(
1418 &propertyMap.second);
1419 if (millisTimeStamp != nullptr)
1420 {
1421 updateTimestamp =
1422 crow::utility::getTimestamp(
1423 *millisTimeStamp);
1424 }
1425 }
1426 else if (propertyMap.first == "Severity")
1427 {
1428 severity = std::get_if<std::string>(
1429 &propertyMap.second);
1430 }
1431 else if (propertyMap.first == "Message")
1432 {
1433 message = std::get_if<std::string>(
1434 &propertyMap.second);
1435 }
1436 else if (propertyMap.first == "Resolved")
1437 {
1438 bool* resolveptr = std::get_if<bool>(
1439 &propertyMap.second);
1440 if (resolveptr == nullptr)
1441 {
1442 messages::internalError(
1443 asyncResp->res);
1444 return;
1445 }
1446 resolved = *resolveptr;
1447 }
1448 }
1449 if (id == nullptr || message == nullptr ||
1450 severity == nullptr)
1451 {
1452 messages::internalError(asyncResp->res);
1453 return;
1454 }
1455 }
1456 else if (interfaceMap.first ==
1457 "xyz.openbmc_project.Common.FilePath")
1458 {
1459 for (auto& propertyMap : interfaceMap.second)
1460 {
1461 if (propertyMap.first == "Path")
1462 {
1463 filePath = std::get_if<std::string>(
1464 &propertyMap.second);
1465 }
1466 }
1467 }
1468 }
1469 // Object path without the
1470 // xyz.openbmc_project.Logging.Entry interface, ignore
1471 // and continue.
1472 if (id == nullptr || message == nullptr ||
1473 severity == nullptr)
1474 {
1475 continue;
1476 }
1477 entriesArray.push_back({});
1478 nlohmann::json& thisEntry = entriesArray.back();
1479 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1480 thisEntry["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001481 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001482 std::to_string(*id);
1483 thisEntry["Name"] = "System Event Log Entry";
1484 thisEntry["Id"] = std::to_string(*id);
1485 thisEntry["Message"] = *message;
1486 thisEntry["Resolved"] = resolved;
1487 thisEntry["EntryType"] = "Event";
1488 thisEntry["Severity"] =
1489 translateSeverityDbusToRedfish(*severity);
1490 thisEntry["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001491 crow::utility::getDateTimeStdtime(timestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001492 thisEntry["Modified"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001493 crow::utility::getDateTimeStdtime(updateTimestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001494 if (filePath != nullptr)
1495 {
1496 thisEntry["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001497 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001498 std::to_string(*id) + "/attachment";
1499 }
1500 }
1501 std::sort(entriesArray.begin(), entriesArray.end(),
1502 [](const nlohmann::json& left,
1503 const nlohmann::json& right) {
1504 return (left["Id"] <= right["Id"]);
1505 });
1506 asyncResp->res.jsonValue["Members@odata.count"] =
1507 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001508 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001509 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1510 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1511 });
1512}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001513
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001514inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001515{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001516 BMCWEB_ROUTE(
1517 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001518 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001519 .methods(boost::beast::http::verb::get)(
1520 [](const crow::Request&,
1521 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1522 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001523
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001524 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001525 std::string entryID = param;
1526 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001527
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001528 // DBus implementation of EventLog/Entries
1529 // Make call to Logging Service to find all log entry objects
1530 crow::connections::systemBus->async_method_call(
1531 [asyncResp, entryID](const boost::system::error_code ec,
1532 GetManagedPropertyType& resp) {
1533 if (ec.value() == EBADR)
1534 {
1535 messages::resourceNotFound(
1536 asyncResp->res, "EventLogEntry", entryID);
1537 return;
1538 }
1539 if (ec)
1540 {
George Liu0fda0f12021-11-16 10:06:17 +08001541 BMCWEB_LOG_ERROR
1542 << "EventLogEntry (DBus) resp_handler got error "
1543 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001544 messages::internalError(asyncResp->res);
1545 return;
1546 }
1547 uint32_t* id = nullptr;
1548 std::time_t timestamp{};
1549 std::time_t updateTimestamp{};
1550 std::string* severity = nullptr;
1551 std::string* message = nullptr;
1552 std::string* filePath = nullptr;
1553 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001554
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001555 for (auto& propertyMap : resp)
1556 {
1557 if (propertyMap.first == "Id")
1558 {
1559 id = std::get_if<uint32_t>(&propertyMap.second);
1560 }
1561 else if (propertyMap.first == "Timestamp")
1562 {
1563 const uint64_t* millisTimeStamp =
1564 std::get_if<uint64_t>(&propertyMap.second);
1565 if (millisTimeStamp != nullptr)
1566 {
1567 timestamp = crow::utility::getTimestamp(
1568 *millisTimeStamp);
1569 }
1570 }
1571 else if (propertyMap.first == "UpdateTimestamp")
1572 {
1573 const uint64_t* millisTimeStamp =
1574 std::get_if<uint64_t>(&propertyMap.second);
1575 if (millisTimeStamp != nullptr)
1576 {
1577 updateTimestamp =
1578 crow::utility::getTimestamp(
1579 *millisTimeStamp);
1580 }
1581 }
1582 else if (propertyMap.first == "Severity")
1583 {
1584 severity = std::get_if<std::string>(
1585 &propertyMap.second);
1586 }
1587 else if (propertyMap.first == "Message")
1588 {
1589 message = std::get_if<std::string>(
1590 &propertyMap.second);
1591 }
1592 else if (propertyMap.first == "Resolved")
1593 {
1594 bool* resolveptr =
1595 std::get_if<bool>(&propertyMap.second);
1596 if (resolveptr == nullptr)
1597 {
1598 messages::internalError(asyncResp->res);
1599 return;
1600 }
1601 resolved = *resolveptr;
1602 }
1603 else if (propertyMap.first == "Path")
1604 {
1605 filePath = std::get_if<std::string>(
1606 &propertyMap.second);
1607 }
1608 }
1609 if (id == nullptr || message == nullptr ||
1610 severity == nullptr)
1611 {
1612 messages::internalError(asyncResp->res);
1613 return;
1614 }
1615 asyncResp->res.jsonValue["@odata.type"] =
1616 "#LogEntry.v1_8_0.LogEntry";
1617 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001618 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001619 std::to_string(*id);
1620 asyncResp->res.jsonValue["Name"] =
1621 "System Event Log Entry";
1622 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1623 asyncResp->res.jsonValue["Message"] = *message;
1624 asyncResp->res.jsonValue["Resolved"] = resolved;
1625 asyncResp->res.jsonValue["EntryType"] = "Event";
1626 asyncResp->res.jsonValue["Severity"] =
1627 translateSeverityDbusToRedfish(*severity);
1628 asyncResp->res.jsonValue["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001629 crow::utility::getDateTimeStdtime(timestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001630 asyncResp->res.jsonValue["Modified"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001631 crow::utility::getDateTimeStdtime(updateTimestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001632 if (filePath != nullptr)
1633 {
1634 asyncResp->res.jsonValue["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001635 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001636 std::to_string(*id);
1637 }
1638 },
1639 "xyz.openbmc_project.Logging",
1640 "/xyz/openbmc_project/logging/entry/" + entryID,
1641 "org.freedesktop.DBus.Properties", "GetAll", "");
1642 });
1643
1644 BMCWEB_ROUTE(
1645 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001646 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001647 .methods(boost::beast::http::verb::patch)(
1648 [](const crow::Request& req,
1649 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1650 const std::string& entryId) {
1651 std::optional<bool> resolved;
1652
1653 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1654 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001655 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001656 return;
1657 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001658 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001659
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001660 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001661 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001662 if (ec)
1663 {
1664 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1665 messages::internalError(asyncResp->res);
1666 return;
1667 }
1668 },
1669 "xyz.openbmc_project.Logging",
1670 "/xyz/openbmc_project/logging/entry/" + entryId,
1671 "org.freedesktop.DBus.Properties", "Set",
1672 "xyz.openbmc_project.Logging.Entry", "Resolved",
1673 std::variant<bool>(*resolved));
1674 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001675
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001676 BMCWEB_ROUTE(
1677 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001678 .privileges(redfish::privileges::deleteLogEntry)
1679
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001680 .methods(boost::beast::http::verb::delete_)(
1681 [](const crow::Request&,
1682 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1683 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001684
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001685 {
1686 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001687
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001688 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001689
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001690 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001691
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001692 // Process response from Logging service.
1693 auto respHandler = [asyncResp, entryID](
1694 const boost::system::error_code ec) {
1695 BMCWEB_LOG_DEBUG
1696 << "EventLogEntry (DBus) doDelete callback: Done";
1697 if (ec)
1698 {
1699 if (ec.value() == EBADR)
1700 {
1701 messages::resourceNotFound(asyncResp->res,
1702 "LogEntry", entryID);
1703 return;
1704 }
1705 // TODO Handle for specific error code
George Liu0fda0f12021-11-16 10:06:17 +08001706 BMCWEB_LOG_ERROR
1707 << "EventLogEntry (DBus) doDelete respHandler got error "
1708 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001709 asyncResp->res.result(
1710 boost::beast::http::status::internal_server_error);
1711 return;
1712 }
1713
1714 asyncResp->res.result(boost::beast::http::status::ok);
1715 };
1716
1717 // Make call to Logging service to request Delete Log
1718 crow::connections::systemBus->async_method_call(
1719 respHandler, "xyz.openbmc_project.Logging",
1720 "/xyz/openbmc_project/logging/entry/" + entryID,
1721 "xyz.openbmc_project.Object.Delete", "Delete");
1722 });
1723}
1724
1725inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001726{
George Liu0fda0f12021-11-16 10:06:17 +08001727 BMCWEB_ROUTE(
1728 app,
1729 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001730 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001731 .methods(boost::beast::http::verb::get)(
1732 [](const crow::Request& req,
1733 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1734 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001735
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001736 {
George Liu647b3cd2021-07-05 12:43:56 +08001737 if (!http_helpers::isOctetAccepted(
1738 req.getHeaderValue("Accept")))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001739 {
1740 asyncResp->res.result(
1741 boost::beast::http::status::bad_request);
1742 return;
1743 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001744
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001745 std::string entryID = param;
1746 dbus::utility::escapePathForDbus(entryID);
1747
1748 crow::connections::systemBus->async_method_call(
1749 [asyncResp,
1750 entryID](const boost::system::error_code ec,
1751 const sdbusplus::message::unix_fd& unixfd) {
1752 if (ec.value() == EBADR)
1753 {
1754 messages::resourceNotFound(
1755 asyncResp->res, "EventLogAttachment", entryID);
1756 return;
1757 }
1758 if (ec)
1759 {
1760 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1761 messages::internalError(asyncResp->res);
1762 return;
1763 }
1764
1765 int fd = -1;
1766 fd = dup(unixfd);
1767 if (fd == -1)
1768 {
1769 messages::internalError(asyncResp->res);
1770 return;
1771 }
1772
1773 long long int size = lseek(fd, 0, SEEK_END);
1774 if (size == -1)
1775 {
1776 messages::internalError(asyncResp->res);
1777 return;
1778 }
1779
1780 // Arbitrary max size of 64kb
1781 constexpr int maxFileSize = 65536;
1782 if (size > maxFileSize)
1783 {
1784 BMCWEB_LOG_ERROR
1785 << "File size exceeds maximum allowed size of "
1786 << maxFileSize;
1787 messages::internalError(asyncResp->res);
1788 return;
1789 }
1790 std::vector<char> data(static_cast<size_t>(size));
1791 long long int rc = lseek(fd, 0, SEEK_SET);
1792 if (rc == -1)
1793 {
1794 messages::internalError(asyncResp->res);
1795 return;
1796 }
1797 rc = read(fd, data.data(), data.size());
1798 if ((rc == -1) || (rc != size))
1799 {
1800 messages::internalError(asyncResp->res);
1801 return;
1802 }
1803 close(fd);
1804
1805 std::string_view strData(data.data(), data.size());
1806 std::string output =
1807 crow::utility::base64encode(strData);
1808
1809 asyncResp->res.addHeader("Content-Type",
1810 "application/octet-stream");
1811 asyncResp->res.addHeader("Content-Transfer-Encoding",
1812 "Base64");
1813 asyncResp->res.body() = std::move(output);
1814 },
1815 "xyz.openbmc_project.Logging",
1816 "/xyz/openbmc_project/logging/entry/" + entryID,
1817 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1818 });
1819}
1820
Spencer Kub7028eb2021-10-26 15:27:35 +08001821constexpr const char* hostLoggerFolderPath = "/var/log/console";
1822
1823inline bool
1824 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1825 std::vector<std::filesystem::path>& hostLoggerFiles)
1826{
1827 std::error_code ec;
1828 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1829 if (ec)
1830 {
1831 BMCWEB_LOG_ERROR << ec.message();
1832 return false;
1833 }
1834 for (const std::filesystem::directory_entry& it : logPath)
1835 {
1836 std::string filename = it.path().filename();
1837 // Prefix of each log files is "log". Find the file and save the
1838 // path
1839 if (boost::starts_with(filename, "log"))
1840 {
1841 hostLoggerFiles.emplace_back(it.path());
1842 }
1843 }
1844 // As the log files rotate, they are appended with a ".#" that is higher for
1845 // the older logs. Since we start from oldest logs, sort the name in
1846 // descending order.
1847 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1848 AlphanumLess<std::string>());
1849
1850 return true;
1851}
1852
1853inline bool
1854 getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1855 uint64_t& skip, uint64_t& top,
1856 std::vector<std::string>& logEntries, size_t& logCount)
1857{
1858 GzFileReader logFile;
1859
1860 // Go though all log files and expose host logs.
1861 for (const std::filesystem::path& it : hostLoggerFiles)
1862 {
1863 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1864 {
1865 BMCWEB_LOG_ERROR << "fail to expose host logs";
1866 return false;
1867 }
1868 }
1869 // Get lastMessage from constructor by getter
1870 std::string lastMessage = logFile.getLastMessage();
1871 if (!lastMessage.empty())
1872 {
1873 logCount++;
1874 if (logCount > skip && logCount <= (skip + top))
1875 {
1876 logEntries.push_back(lastMessage);
1877 }
1878 }
1879 return true;
1880}
1881
1882inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1883 const std::string& msg,
1884 nlohmann::json& logEntryJson)
1885{
1886 // Fill in the log entry with the gathered data.
1887 logEntryJson = {
1888 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1889 {"@odata.id",
1890 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1891 logEntryID},
1892 {"Name", "Host Logger Entry"},
1893 {"Id", logEntryID},
1894 {"Message", msg},
1895 {"EntryType", "Oem"},
1896 {"Severity", "OK"},
1897 {"OemRecordFormat", "Host Logger Entry"}};
1898}
1899
1900inline void requestRoutesSystemHostLogger(App& app)
1901{
1902 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1903 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08001904 .methods(
1905 boost::beast::http::verb::
1906 get)([](const crow::Request&,
1907 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1908 asyncResp->res.jsonValue["@odata.id"] =
1909 "/redfish/v1/Systems/system/LogServices/HostLogger";
1910 asyncResp->res.jsonValue["@odata.type"] =
1911 "#LogService.v1_1_0.LogService";
1912 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1913 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1914 asyncResp->res.jsonValue["Id"] = "HostLogger";
1915 asyncResp->res.jsonValue["Entries"] = {
1916 {"@odata.id",
1917 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1918 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001919}
1920
1921inline void requestRoutesSystemHostLoggerCollection(App& app)
1922{
1923 BMCWEB_ROUTE(app,
1924 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1925 .privileges(redfish::privileges::getLogEntry)
George Liu0fda0f12021-11-16 10:06:17 +08001926 .methods(
1927 boost::beast::http::verb::
1928 get)([](const crow::Request& req,
1929 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1930 uint64_t skip = 0;
1931 uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1932 // default, allow range 1 to
1933 // 1000 entries per page.
1934 if (!getSkipParam(asyncResp, req, skip))
1935 {
1936 return;
1937 }
1938 if (!getTopParam(asyncResp, req, top))
1939 {
1940 return;
1941 }
1942 asyncResp->res.jsonValue["@odata.id"] =
1943 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1944 asyncResp->res.jsonValue["@odata.type"] =
1945 "#LogEntryCollection.LogEntryCollection";
1946 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1947 asyncResp->res.jsonValue["Description"] =
1948 "Collection of HostLogger Entries";
1949 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1950 logEntryArray = nlohmann::json::array();
1951 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001952
George Liu0fda0f12021-11-16 10:06:17 +08001953 std::vector<std::filesystem::path> hostLoggerFiles;
1954 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1955 {
1956 BMCWEB_LOG_ERROR << "fail to get host log file path";
1957 return;
1958 }
1959
1960 size_t logCount = 0;
1961 // This vector only store the entries we want to expose that
1962 // control by skip and top.
1963 std::vector<std::string> logEntries;
1964 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1965 logCount))
1966 {
1967 messages::internalError(asyncResp->res);
1968 return;
1969 }
1970 // If vector is empty, that means skip value larger than total
1971 // log count
1972 if (logEntries.size() == 0)
1973 {
1974 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1975 return;
1976 }
1977 if (logEntries.size() > 0)
1978 {
1979 for (size_t i = 0; i < logEntries.size(); i++)
Spencer Kub7028eb2021-10-26 15:27:35 +08001980 {
George Liu0fda0f12021-11-16 10:06:17 +08001981 logEntryArray.push_back({});
1982 nlohmann::json& hostLogEntry = logEntryArray.back();
1983 fillHostLoggerEntryJson(std::to_string(skip + i),
1984 logEntries[i], hostLogEntry);
Spencer Kub7028eb2021-10-26 15:27:35 +08001985 }
1986
George Liu0fda0f12021-11-16 10:06:17 +08001987 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1988 if (skip + top < logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001989 {
George Liu0fda0f12021-11-16 10:06:17 +08001990 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1991 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1992 std::to_string(skip + top);
Spencer Kub7028eb2021-10-26 15:27:35 +08001993 }
George Liu0fda0f12021-11-16 10:06:17 +08001994 }
1995 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001996}
1997
1998inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1999{
2000 BMCWEB_ROUTE(
2001 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2002 .privileges(redfish::privileges::getLogEntry)
2003 .methods(boost::beast::http::verb::get)(
2004 [](const crow::Request&,
2005 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2006 const std::string& param) {
2007 const std::string& targetID = param;
2008
2009 uint64_t idInt = 0;
2010 auto [ptr, ec] = std::from_chars(
2011 targetID.data(), targetID.data() + targetID.size(), idInt);
2012 if (ec == std::errc::invalid_argument)
2013 {
2014 messages::resourceMissingAtURI(asyncResp->res, targetID);
2015 return;
2016 }
2017 if (ec == std::errc::result_out_of_range)
2018 {
2019 messages::resourceMissingAtURI(asyncResp->res, targetID);
2020 return;
2021 }
2022
2023 std::vector<std::filesystem::path> hostLoggerFiles;
2024 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2025 {
2026 BMCWEB_LOG_ERROR << "fail to get host log file path";
2027 return;
2028 }
2029
2030 size_t logCount = 0;
2031 uint64_t top = 1;
2032 std::vector<std::string> logEntries;
2033 // We can get specific entry by skip and top. For example, if we
2034 // want to get nth entry, we can set skip = n-1 and top = 1 to
2035 // get that entry
2036 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2037 logEntries, logCount))
2038 {
2039 messages::internalError(asyncResp->res);
2040 return;
2041 }
2042
2043 if (!logEntries.empty())
2044 {
2045 fillHostLoggerEntryJson(targetID, logEntries[0],
2046 asyncResp->res.jsonValue);
2047 return;
2048 }
2049
2050 // Requested ID was not found
2051 messages::resourceMissingAtURI(asyncResp->res, targetID);
2052 });
2053}
2054
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002055inline void requestRoutesBMCLogServiceCollection(App& app)
2056{
2057 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002058 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002059 .methods(boost::beast::http::verb::get)(
2060 [](const crow::Request&,
2061 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2062 // Collections don't include the static data added by SubRoute
2063 // because it has a duplicate entry for members
2064 asyncResp->res.jsonValue["@odata.type"] =
2065 "#LogServiceCollection.LogServiceCollection";
2066 asyncResp->res.jsonValue["@odata.id"] =
2067 "/redfish/v1/Managers/bmc/LogServices";
2068 asyncResp->res.jsonValue["Name"] =
2069 "Open BMC Log Services Collection";
2070 asyncResp->res.jsonValue["Description"] =
2071 "Collection of LogServices for this Manager";
2072 nlohmann::json& logServiceArray =
2073 asyncResp->res.jsonValue["Members"];
2074 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002075#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002076 logServiceArray.push_back(
2077 {{"@odata.id",
2078 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002079#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002080#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002081 logServiceArray.push_back(
2082 {{"@odata.id",
2083 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002084#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002085 asyncResp->res.jsonValue["Members@odata.count"] =
2086 logServiceArray.size();
2087 });
2088}
Ed Tanous1da66f72018-07-27 16:13:37 -07002089
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002090inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002091{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002092 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002093 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002094 .methods(boost::beast::http::verb::get)(
2095 [](const crow::Request&,
2096 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07002097
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002098 {
2099 asyncResp->res.jsonValue["@odata.type"] =
2100 "#LogService.v1_1_0.LogService";
2101 asyncResp->res.jsonValue["@odata.id"] =
2102 "/redfish/v1/Managers/bmc/LogServices/Journal";
2103 asyncResp->res.jsonValue["Name"] =
2104 "Open BMC Journal Log Service";
2105 asyncResp->res.jsonValue["Description"] =
2106 "BMC Journal Log Service";
2107 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2108 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302109
2110 std::pair<std::string, std::string> redfishDateTimeOffset =
2111 crow::utility::getDateTimeOffsetNow();
2112 asyncResp->res.jsonValue["DateTime"] =
2113 redfishDateTimeOffset.first;
2114 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2115 redfishDateTimeOffset.second;
2116
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002117 asyncResp->res.jsonValue["Entries"] = {
2118 {"@odata.id",
2119 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
2120 });
2121}
Jason M. Billse1f26342018-07-18 12:12:00 -07002122
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002123static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2124 sd_journal* journal,
2125 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002126{
2127 // Get the Log Entry contents
2128 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002129
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002130 std::string message;
2131 std::string_view syslogID;
2132 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2133 if (ret < 0)
2134 {
2135 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2136 << strerror(-ret);
2137 }
2138 if (!syslogID.empty())
2139 {
2140 message += std::string(syslogID) + ": ";
2141 }
2142
Ed Tanous39e77502019-03-04 17:35:53 -08002143 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002144 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002145 if (ret < 0)
2146 {
2147 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2148 return 1;
2149 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002150 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002151
2152 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002153 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002154 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002155 if (ret < 0)
2156 {
2157 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002158 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002159
2160 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002161 std::string entryTimeStr;
2162 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002163 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002164 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002165 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002166
2167 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002168 bmcJournalLogEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08002169 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002170 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2171 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07002172 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002173 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002174 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07002175 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06002176 {"Severity", severity <= 2 ? "Critical"
2177 : severity <= 4 ? "Warning"
2178 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07002179 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07002180 {"Created", std::move(entryTimeStr)}};
2181 return 0;
2182}
2183
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002184inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002185{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002186 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002187 .privileges(redfish::privileges::getLogEntryCollection)
George Liu0fda0f12021-11-16 10:06:17 +08002188 .methods(
2189 boost::beast::http::verb::
2190 get)([](const crow::Request& req,
2191 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2192 static constexpr const long maxEntriesPerPage = 1000;
2193 uint64_t skip = 0;
2194 uint64_t top = maxEntriesPerPage; // Show max entries by default
2195 if (!getSkipParam(asyncResp, req, skip))
2196 {
2197 return;
2198 }
2199 if (!getTopParam(asyncResp, req, top))
2200 {
2201 return;
2202 }
2203 // Collections don't include the static data added by SubRoute
2204 // because it has a duplicate entry for members
2205 asyncResp->res.jsonValue["@odata.type"] =
2206 "#LogEntryCollection.LogEntryCollection";
2207 asyncResp->res.jsonValue["@odata.id"] =
2208 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2209 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2210 asyncResp->res.jsonValue["Description"] =
2211 "Collection of BMC Journal Entries";
2212 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2213 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002214
George Liu0fda0f12021-11-16 10:06:17 +08002215 // Go through the journal and use the timestamp to create a
2216 // unique ID for each entry
2217 sd_journal* journalTmp = nullptr;
2218 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2219 if (ret < 0)
2220 {
2221 BMCWEB_LOG_ERROR << "failed to open journal: "
2222 << strerror(-ret);
2223 messages::internalError(asyncResp->res);
2224 return;
2225 }
2226 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2227 journalTmp, sd_journal_close);
2228 journalTmp = nullptr;
2229 uint64_t entryCount = 0;
2230 // Reset the unique ID on the first entry
2231 bool firstEntry = true;
2232 SD_JOURNAL_FOREACH(journal.get())
2233 {
2234 entryCount++;
2235 // Handle paging using skip (number of entries to skip from
2236 // the start) and top (number of entries to display)
2237 if (entryCount <= skip || entryCount > skip + top)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002238 {
George Liu0fda0f12021-11-16 10:06:17 +08002239 continue;
2240 }
2241
2242 std::string idStr;
2243 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2244 {
2245 continue;
2246 }
2247
2248 if (firstEntry)
2249 {
2250 firstEntry = false;
2251 }
2252
2253 logEntryArray.push_back({});
2254 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2255 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2256 bmcJournalLogEntry) != 0)
2257 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002258 messages::internalError(asyncResp->res);
2259 return;
2260 }
George Liu0fda0f12021-11-16 10:06:17 +08002261 }
2262 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2263 if (skip + top < entryCount)
2264 {
2265 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2266 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2267 std::to_string(skip + top);
2268 }
2269 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002270}
Jason M. Billse1f26342018-07-18 12:12:00 -07002271
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002272inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002273{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002274 BMCWEB_ROUTE(app,
2275 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002276 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002277 .methods(boost::beast::http::verb::get)(
2278 [](const crow::Request&,
2279 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2280 const std::string& entryID) {
2281 // Convert the unique ID back to a timestamp to find the entry
2282 uint64_t ts = 0;
2283 uint64_t index = 0;
2284 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2285 {
2286 return;
2287 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002288
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002289 sd_journal* journalTmp = nullptr;
2290 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2291 if (ret < 0)
2292 {
2293 BMCWEB_LOG_ERROR << "failed to open journal: "
2294 << strerror(-ret);
2295 messages::internalError(asyncResp->res);
2296 return;
2297 }
2298 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2299 journal(journalTmp, sd_journal_close);
2300 journalTmp = nullptr;
2301 // Go to the timestamp in the log and move to the entry at the
2302 // index tracking the unique ID
2303 std::string idStr;
2304 bool firstEntry = true;
2305 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2306 if (ret < 0)
2307 {
2308 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2309 << strerror(-ret);
2310 messages::internalError(asyncResp->res);
2311 return;
2312 }
2313 for (uint64_t i = 0; i <= index; i++)
2314 {
2315 sd_journal_next(journal.get());
2316 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2317 {
2318 messages::internalError(asyncResp->res);
2319 return;
2320 }
2321 if (firstEntry)
2322 {
2323 firstEntry = false;
2324 }
2325 }
2326 // Confirm that the entry ID matches what was requested
2327 if (idStr != entryID)
2328 {
2329 messages::resourceMissingAtURI(asyncResp->res, entryID);
2330 return;
2331 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002332
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002333 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2334 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002335 {
2336 messages::internalError(asyncResp->res);
2337 return;
2338 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002339 });
2340}
2341
2342inline void requestRoutesBMCDumpService(App& app)
2343{
2344 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002345 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08002346 .methods(
2347 boost::beast::http::verb::
2348 get)([](const crow::Request&,
2349 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2350 asyncResp->res.jsonValue["@odata.id"] =
2351 "/redfish/v1/Managers/bmc/LogServices/Dump";
2352 asyncResp->res.jsonValue["@odata.type"] =
2353 "#LogService.v1_2_0.LogService";
2354 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2355 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2356 asyncResp->res.jsonValue["Id"] = "Dump";
2357 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302358
George Liu0fda0f12021-11-16 10:06:17 +08002359 std::pair<std::string, std::string> redfishDateTimeOffset =
2360 crow::utility::getDateTimeOffsetNow();
2361 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2362 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2363 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302364
George Liu0fda0f12021-11-16 10:06:17 +08002365 asyncResp->res.jsonValue["Entries"] = {
2366 {"@odata.id",
2367 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2368 asyncResp->res.jsonValue["Actions"] = {
2369 {"#LogService.ClearLog",
2370 {{"target",
2371 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2372 {"#LogService.CollectDiagnosticData",
2373 {{"target",
2374 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
2375 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002376}
2377
2378inline void requestRoutesBMCDumpEntryCollection(App& app)
2379{
2380
2381 /**
2382 * Functions triggers appropriate requests on DBus
2383 */
2384 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002385 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002386 .methods(boost::beast::http::verb::get)(
2387 [](const crow::Request&,
2388 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2389 asyncResp->res.jsonValue["@odata.type"] =
2390 "#LogEntryCollection.LogEntryCollection";
2391 asyncResp->res.jsonValue["@odata.id"] =
2392 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2393 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2394 asyncResp->res.jsonValue["Description"] =
2395 "Collection of BMC Dump Entries";
2396
2397 getDumpEntryCollection(asyncResp, "BMC");
2398 });
2399}
2400
2401inline void requestRoutesBMCDumpEntry(App& app)
2402{
2403 BMCWEB_ROUTE(app,
2404 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002405 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002406 .methods(boost::beast::http::verb::get)(
2407 [](const crow::Request&,
2408 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2409 const std::string& param) {
2410 getDumpEntryById(asyncResp, param, "BMC");
2411 });
2412 BMCWEB_ROUTE(app,
2413 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002414 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002415 .methods(boost::beast::http::verb::delete_)(
2416 [](const crow::Request&,
2417 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2418 const std::string& param) {
2419 deleteDumpEntry(asyncResp, param, "bmc");
2420 });
2421}
2422
2423inline void requestRoutesBMCDumpCreate(App& app)
2424{
2425
George Liu0fda0f12021-11-16 10:06:17 +08002426 BMCWEB_ROUTE(
2427 app,
2428 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002429 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002430 .methods(boost::beast::http::verb::post)(
2431 [](const crow::Request& req,
2432 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2433 createDump(asyncResp, req, "BMC");
2434 });
2435}
2436
2437inline void requestRoutesBMCDumpClear(App& app)
2438{
George Liu0fda0f12021-11-16 10:06:17 +08002439 BMCWEB_ROUTE(
2440 app,
2441 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002442 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002443 .methods(boost::beast::http::verb::post)(
2444 [](const crow::Request&,
2445 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2446 clearDump(asyncResp, "BMC");
2447 });
2448}
2449
2450inline void requestRoutesSystemDumpService(App& app)
2451{
2452 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002453 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002454 .methods(boost::beast::http::verb::get)(
2455 [](const crow::Request&,
2456 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2457
2458 {
2459 asyncResp->res.jsonValue["@odata.id"] =
2460 "/redfish/v1/Systems/system/LogServices/Dump";
2461 asyncResp->res.jsonValue["@odata.type"] =
2462 "#LogService.v1_2_0.LogService";
2463 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2464 asyncResp->res.jsonValue["Description"] =
2465 "System Dump LogService";
2466 asyncResp->res.jsonValue["Id"] = "Dump";
2467 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302468
2469 std::pair<std::string, std::string> redfishDateTimeOffset =
2470 crow::utility::getDateTimeOffsetNow();
2471 asyncResp->res.jsonValue["DateTime"] =
2472 redfishDateTimeOffset.first;
2473 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2474 redfishDateTimeOffset.second;
2475
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002476 asyncResp->res.jsonValue["Entries"] = {
2477 {"@odata.id",
2478 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2479 asyncResp->res.jsonValue["Actions"] = {
2480 {"#LogService.ClearLog",
2481 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002482 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002483 {"#LogService.CollectDiagnosticData",
2484 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002485 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002486 });
2487}
2488
2489inline void requestRoutesSystemDumpEntryCollection(App& app)
2490{
2491
2492 /**
2493 * Functions triggers appropriate requests on DBus
2494 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002495 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002496 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002497 .methods(boost::beast::http::verb::get)(
2498 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002499 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002500 asyncResp->res.jsonValue["@odata.type"] =
2501 "#LogEntryCollection.LogEntryCollection";
2502 asyncResp->res.jsonValue["@odata.id"] =
2503 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2504 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2505 asyncResp->res.jsonValue["Description"] =
2506 "Collection of System Dump Entries";
2507
2508 getDumpEntryCollection(asyncResp, "System");
2509 });
2510}
2511
2512inline void requestRoutesSystemDumpEntry(App& app)
2513{
2514 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002515 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002516 .privileges(redfish::privileges::getLogEntry)
2517
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002518 .methods(boost::beast::http::verb::get)(
2519 [](const crow::Request&,
2520 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2521 const std::string& param) {
2522 getDumpEntryById(asyncResp, param, "System");
2523 });
2524
2525 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002526 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002527 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002528 .methods(boost::beast::http::verb::delete_)(
2529 [](const crow::Request&,
2530 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2531 const std::string& param) {
2532 deleteDumpEntry(asyncResp, param, "system");
2533 });
2534}
2535
2536inline void requestRoutesSystemDumpCreate(App& app)
2537{
George Liu0fda0f12021-11-16 10:06:17 +08002538 BMCWEB_ROUTE(
2539 app,
2540 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002541 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002542 .methods(boost::beast::http::verb::post)(
2543 [](const crow::Request& req,
2544 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2545
2546 { createDump(asyncResp, req, "System"); });
2547}
2548
2549inline void requestRoutesSystemDumpClear(App& app)
2550{
George Liu0fda0f12021-11-16 10:06:17 +08002551 BMCWEB_ROUTE(
2552 app,
2553 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002554 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002555 .methods(boost::beast::http::verb::post)(
2556 [](const crow::Request&,
2557 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2558
2559 { clearDump(asyncResp, "System"); });
2560}
2561
2562inline void requestRoutesCrashdumpService(App& app)
2563{
2564 // Note: Deviated from redfish privilege registry for GET & HEAD
2565 // method for security reasons.
2566 /**
2567 * Functions triggers appropriate requests on DBus
2568 */
2569 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002570 // This is incorrect, should be:
2571 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002572 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002573 .methods(
2574 boost::beast::http::verb::
2575 get)([](const crow::Request&,
2576 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2577 // Copy over the static data to include the entries added by
2578 // SubRoute
2579 asyncResp->res.jsonValue["@odata.id"] =
2580 "/redfish/v1/Systems/system/LogServices/Crashdump";
2581 asyncResp->res.jsonValue["@odata.type"] =
2582 "#LogService.v1_2_0.LogService";
2583 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2584 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2585 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2586 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2587 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302588
2589 std::pair<std::string, std::string> redfishDateTimeOffset =
2590 crow::utility::getDateTimeOffsetNow();
2591 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2592 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2593 redfishDateTimeOffset.second;
2594
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002595 asyncResp->res.jsonValue["Entries"] = {
2596 {"@odata.id",
2597 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2598 asyncResp->res.jsonValue["Actions"] = {
2599 {"#LogService.ClearLog",
George Liu0fda0f12021-11-16 10:06:17 +08002600 {{"target",
2601 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002602 {"#LogService.CollectDiagnosticData",
George Liu0fda0f12021-11-16 10:06:17 +08002603 {{"target",
2604 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002605 });
2606}
2607
2608void inline requestRoutesCrashdumpClear(App& app)
2609{
George Liu0fda0f12021-11-16 10:06:17 +08002610 BMCWEB_ROUTE(
2611 app,
2612 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002613 // This is incorrect, should be:
2614 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002615 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002616 .methods(boost::beast::http::verb::post)(
2617 [](const crow::Request&,
2618 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2619 crow::connections::systemBus->async_method_call(
2620 [asyncResp](const boost::system::error_code ec,
2621 const std::string&) {
2622 if (ec)
2623 {
2624 messages::internalError(asyncResp->res);
2625 return;
2626 }
2627 messages::success(asyncResp->res);
2628 },
2629 crashdumpObject, crashdumpPath, deleteAllInterface,
2630 "DeleteAll");
2631 });
2632}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002633
zhanghch058d1b46d2021-04-01 11:18:24 +08002634static void
2635 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2636 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002637{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002638 auto getStoredLogCallback =
2639 [asyncResp, logID, &logEntryJson](
2640 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002641 const std::vector<std::pair<std::string, VariantType>>& params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002642 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002643 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002644 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2645 if (ec.value() ==
2646 boost::system::linux_error::bad_request_descriptor)
2647 {
2648 messages::resourceNotFound(asyncResp->res, "LogEntry",
2649 logID);
2650 }
2651 else
2652 {
2653 messages::internalError(asyncResp->res);
2654 }
2655 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002656 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002657
Johnathan Mantey043a0532020-03-10 17:15:28 -07002658 std::string timestamp{};
2659 std::string filename{};
2660 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002661 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002662
2663 if (filename.empty() || timestamp.empty())
2664 {
2665 messages::resourceMissingAtURI(asyncResp->res, logID);
2666 return;
2667 }
2668
2669 std::string crashdumpURI =
2670 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2671 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002672 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002673 {"@odata.id", "/redfish/v1/Systems/system/"
2674 "LogServices/Crashdump/Entries/" +
2675 logID},
2676 {"Name", "CPU Crashdump"},
2677 {"Id", logID},
2678 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002679 {"AdditionalDataURI", std::move(crashdumpURI)},
2680 {"DiagnosticDataType", "OEM"},
2681 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002682 {"Created", std::move(timestamp)}};
2683 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002684 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002685 std::move(getStoredLogCallback), crashdumpObject,
2686 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002687 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002688}
2689
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002690inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002691{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002692 // Note: Deviated from redfish privilege registry for GET & HEAD
2693 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002694 /**
2695 * Functions triggers appropriate requests on DBus
2696 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002697 BMCWEB_ROUTE(app,
2698 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002699 // This is incorrect, should be.
2700 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002701 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002702 .methods(
2703 boost::beast::http::verb::
2704 get)([](const crow::Request&,
2705 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2706 // Collections don't include the static data added by SubRoute
2707 // because it has a duplicate entry for members
2708 auto getLogEntriesCallback = [asyncResp](
2709 const boost::system::error_code ec,
2710 const std::vector<std::string>&
2711 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002712 if (ec)
2713 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002714 if (ec.value() !=
2715 boost::system::errc::no_such_file_or_directory)
2716 {
2717 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2718 << ec.message();
2719 messages::internalError(asyncResp->res);
2720 return;
2721 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002722 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002723 asyncResp->res.jsonValue["@odata.type"] =
2724 "#LogEntryCollection.LogEntryCollection";
2725 asyncResp->res.jsonValue["@odata.id"] =
2726 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2727 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2728 asyncResp->res.jsonValue["Description"] =
2729 "Collection of Crashdump Entries";
2730 nlohmann::json& logEntryArray =
2731 asyncResp->res.jsonValue["Members"];
2732 logEntryArray = nlohmann::json::array();
2733 std::vector<std::string> logIDs;
2734 // Get the list of log entries and build up an empty array big
2735 // enough to hold them
2736 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002737 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002738 // Get the log ID
2739 std::size_t lastPos = objpath.rfind('/');
2740 if (lastPos == std::string::npos)
2741 {
2742 continue;
2743 }
2744 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002745
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002746 // Add a space for the log entry to the array
2747 logEntryArray.push_back({});
2748 }
2749 // Now go through and set up async calls to fill in the entries
2750 size_t index = 0;
2751 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002752 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002753 // Add the log entry to the array
2754 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002755 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002756 asyncResp->res.jsonValue["Members@odata.count"] =
2757 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002758 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002759 crow::connections::systemBus->async_method_call(
2760 std::move(getLogEntriesCallback),
2761 "xyz.openbmc_project.ObjectMapper",
2762 "/xyz/openbmc_project/object_mapper",
2763 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2764 std::array<const char*, 1>{crashdumpInterface});
2765 });
2766}
Ed Tanous1da66f72018-07-27 16:13:37 -07002767
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002768inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002769{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002770 // Note: Deviated from redfish privilege registry for GET & HEAD
2771 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002772
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002773 BMCWEB_ROUTE(
2774 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002775 // this is incorrect, should be
2776 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002777 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002778 .methods(boost::beast::http::verb::get)(
2779 [](const crow::Request&,
2780 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2781 const std::string& param) {
2782 const std::string& logID = param;
2783 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2784 });
2785}
Ed Tanous1da66f72018-07-27 16:13:37 -07002786
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002787inline void requestRoutesCrashdumpFile(App& app)
2788{
2789 // Note: Deviated from redfish privilege registry for GET & HEAD
2790 // method for security reasons.
2791 BMCWEB_ROUTE(
2792 app,
2793 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002794 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002795 .methods(boost::beast::http::verb::get)(
2796 [](const crow::Request&,
2797 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2798 const std::string& logID, const std::string& fileName) {
2799 auto getStoredLogCallback =
2800 [asyncResp, logID, fileName](
2801 const boost::system::error_code ec,
2802 const std::vector<std::pair<std::string, VariantType>>&
2803 resp) {
2804 if (ec)
2805 {
2806 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2807 << ec.message();
2808 messages::internalError(asyncResp->res);
2809 return;
2810 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002811
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002812 std::string dbusFilename{};
2813 std::string dbusTimestamp{};
2814 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002815
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002816 parseCrashdumpParameters(resp, dbusFilename,
2817 dbusTimestamp, dbusFilepath);
2818
2819 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2820 dbusFilepath.empty())
2821 {
2822 messages::resourceMissingAtURI(asyncResp->res,
2823 fileName);
2824 return;
2825 }
2826
2827 // Verify the file name parameter is correct
2828 if (fileName != dbusFilename)
2829 {
2830 messages::resourceMissingAtURI(asyncResp->res,
2831 fileName);
2832 return;
2833 }
2834
2835 if (!std::filesystem::exists(dbusFilepath))
2836 {
2837 messages::resourceMissingAtURI(asyncResp->res,
2838 fileName);
2839 return;
2840 }
2841 std::ifstream ifs(dbusFilepath, std::ios::in |
2842 std::ios::binary |
2843 std::ios::ate);
2844 std::ifstream::pos_type fileSize = ifs.tellg();
2845 if (fileSize < 0)
2846 {
2847 messages::generalError(asyncResp->res);
2848 return;
2849 }
2850 ifs.seekg(0, std::ios::beg);
2851
2852 auto crashData = std::make_unique<char[]>(
2853 static_cast<unsigned int>(fileSize));
2854
2855 ifs.read(crashData.get(), static_cast<int>(fileSize));
2856
2857 // The cast to std::string is intentional in order to
2858 // use the assign() that applies move mechanics
2859 asyncResp->res.body().assign(
2860 static_cast<std::string>(crashData.get()));
2861
2862 // Configure this to be a file download when accessed
2863 // from a browser
2864 asyncResp->res.addHeader("Content-Disposition",
2865 "attachment");
2866 };
2867 crow::connections::systemBus->async_method_call(
2868 std::move(getStoredLogCallback), crashdumpObject,
2869 crashdumpPath + std::string("/") + logID,
2870 "org.freedesktop.DBus.Properties", "GetAll",
2871 crashdumpInterface);
2872 });
2873}
2874
2875inline void requestRoutesCrashdumpCollect(App& app)
2876{
2877 // Note: Deviated from redfish privilege registry for GET & HEAD
2878 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08002879 BMCWEB_ROUTE(
2880 app,
2881 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002882 // The below is incorrect; Should be ConfigureManager
2883 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002884 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002885 .methods(
2886 boost::beast::http::verb::
2887 post)([](const crow::Request& req,
2888 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2889 std::string diagnosticDataType;
2890 std::string oemDiagnosticDataType;
2891 if (!redfish::json_util::readJson(
2892 req, asyncResp->res, "DiagnosticDataType",
2893 diagnosticDataType, "OEMDiagnosticDataType",
2894 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002895 {
James Feist46229572020-02-19 15:11:58 -08002896 return;
2897 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002898
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002899 if (diagnosticDataType != "OEM")
2900 {
2901 BMCWEB_LOG_ERROR
2902 << "Only OEM DiagnosticDataType supported for Crashdump";
2903 messages::actionParameterValueFormatError(
2904 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2905 "CollectDiagnosticData");
2906 return;
2907 }
2908
Ed Tanous98be3e32021-09-16 15:05:36 -07002909 auto collectCrashdumpCallback = [asyncResp,
2910 payload(task::Payload(req))](
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002911 const boost::system::error_code
2912 ec,
Ed Tanous98be3e32021-09-16 15:05:36 -07002913 const std::string&) mutable {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002914 if (ec)
2915 {
2916 if (ec.value() ==
2917 boost::system::errc::operation_not_supported)
2918 {
2919 messages::resourceInStandby(asyncResp->res);
2920 }
2921 else if (ec.value() ==
2922 boost::system::errc::device_or_resource_busy)
2923 {
2924 messages::serviceTemporarilyUnavailable(asyncResp->res,
2925 "60");
2926 }
2927 else
2928 {
2929 messages::internalError(asyncResp->res);
2930 }
2931 return;
2932 }
George Liu0fda0f12021-11-16 10:06:17 +08002933 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
2934 [](boost::system::error_code err,
2935 sdbusplus::message::message&,
2936 const std::shared_ptr<task::TaskData>& taskData) {
2937 if (!err)
2938 {
2939 taskData->messages.emplace_back(
2940 messages::taskCompletedOK(
2941 std::to_string(taskData->index)));
2942 taskData->state = "Completed";
2943 }
2944 return task::completed;
2945 },
2946 "type='signal',interface='org.freedesktop.DBus."
2947 "Properties',"
2948 "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002949 task->startTimer(std::chrono::minutes(5));
2950 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -07002951 task->payload.emplace(std::move(payload));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002952 };
2953
2954 if (oemDiagnosticDataType == "OnDemand")
2955 {
2956 crow::connections::systemBus->async_method_call(
2957 std::move(collectCrashdumpCallback), crashdumpObject,
2958 crashdumpPath, crashdumpOnDemandInterface,
2959 "GenerateOnDemandLog");
2960 }
2961 else if (oemDiagnosticDataType == "Telemetry")
2962 {
2963 crow::connections::systemBus->async_method_call(
2964 std::move(collectCrashdumpCallback), crashdumpObject,
2965 crashdumpPath, crashdumpTelemetryInterface,
2966 "GenerateTelemetryLog");
2967 }
2968 else
2969 {
2970 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2971 << oemDiagnosticDataType;
2972 messages::actionParameterValueFormatError(
2973 asyncResp->res, oemDiagnosticDataType,
2974 "OEMDiagnosticDataType", "CollectDiagnosticData");
2975 return;
2976 }
2977 });
2978}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002979
Andrew Geisslercb92c032018-08-17 07:56:14 -07002980/**
2981 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2982 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002983inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002984{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002985 /**
2986 * Function handles POST method request.
2987 * The Clear Log actions does not require any parameter.The action deletes
2988 * all entries found in the Entries collection for this Log Service.
2989 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002990
George Liu0fda0f12021-11-16 10:06:17 +08002991 BMCWEB_ROUTE(
2992 app,
2993 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002994 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002995 .methods(boost::beast::http::verb::post)(
2996 [](const crow::Request&,
2997 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2998 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07002999
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003000 // Process response from Logging service.
3001 auto respHandler = [asyncResp](
3002 const boost::system::error_code ec) {
3003 BMCWEB_LOG_DEBUG
3004 << "doClearLog resp_handler callback: Done";
3005 if (ec)
3006 {
3007 // TODO Handle for specific error code
3008 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
3009 << ec;
3010 asyncResp->res.result(
3011 boost::beast::http::status::internal_server_error);
3012 return;
3013 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003014
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003015 asyncResp->res.result(
3016 boost::beast::http::status::no_content);
3017 };
3018
3019 // Make call to Logging service to request Clear Log
3020 crow::connections::systemBus->async_method_call(
3021 respHandler, "xyz.openbmc_project.Logging",
3022 "/xyz/openbmc_project/logging",
3023 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3024 });
3025}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003026
3027/****************************************************
3028 * Redfish PostCode interfaces
3029 * using DBUS interface: getPostCodesTS
3030 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003031inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003032{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003033 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003034 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08003035 .methods(
3036 boost::beast::http::verb::
3037 get)([](const crow::Request&,
3038 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3039 asyncResp->res.jsonValue = {
3040 {"@odata.id",
3041 "/redfish/v1/Systems/system/LogServices/PostCodes"},
3042 {"@odata.type", "#LogService.v1_1_0.LogService"},
3043 {"Name", "POST Code Log Service"},
3044 {"Description", "POST Code Log Service"},
3045 {"Id", "BIOS POST Code Log"},
3046 {"OverWritePolicy", "WrapsWhenFull"},
3047 {"Entries",
3048 {{"@odata.id",
3049 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05303050
George Liu0fda0f12021-11-16 10:06:17 +08003051 std::pair<std::string, std::string> redfishDateTimeOffset =
3052 crow::utility::getDateTimeOffsetNow();
3053 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3054 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3055 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303056
George Liu0fda0f12021-11-16 10:06:17 +08003057 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3058 {"target",
3059 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
3060 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003061}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003062
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003063inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003064{
George Liu0fda0f12021-11-16 10:06:17 +08003065 BMCWEB_ROUTE(
3066 app,
3067 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003068 // The following privilege is incorrect; It should be ConfigureManager
3069 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003070 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003071 .methods(boost::beast::http::verb::post)(
3072 [](const crow::Request&,
3073 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3074 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003075
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003076 // Make call to post-code service to request clear all
3077 crow::connections::systemBus->async_method_call(
3078 [asyncResp](const boost::system::error_code ec) {
3079 if (ec)
3080 {
3081 // TODO Handle for specific error code
3082 BMCWEB_LOG_ERROR
3083 << "doClearPostCodes resp_handler got error "
3084 << ec;
3085 asyncResp->res.result(boost::beast::http::status::
3086 internal_server_error);
3087 messages::internalError(asyncResp->res);
3088 return;
3089 }
3090 },
3091 "xyz.openbmc_project.State.Boot.PostCode0",
3092 "/xyz/openbmc_project/State/Boot/PostCode0",
3093 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3094 });
3095}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003096
3097static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003098 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303099 const boost::container::flat_map<
3100 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003101 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3102 const uint64_t skip = 0, const uint64_t top = 0)
3103{
3104 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003105 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05303106 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003107
3108 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003109 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003110
3111 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303112 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3113 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003114 {
3115 currentCodeIndex++;
3116 std::string postcodeEntryID =
3117 "B" + std::to_string(bootIndex) + "-" +
3118 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3119
3120 uint64_t usecSinceEpoch = code.first;
3121 uint64_t usTimeOffset = 0;
3122
3123 if (1 == currentCodeIndex)
3124 { // already incremented
3125 firstCodeTimeUs = code.first;
3126 }
3127 else
3128 {
3129 usTimeOffset = code.first - firstCodeTimeUs;
3130 }
3131
3132 // skip if no specific codeIndex is specified and currentCodeIndex does
3133 // not fall between top and skip
3134 if ((codeIndex == 0) &&
3135 (currentCodeIndex <= skip || currentCodeIndex > top))
3136 {
3137 continue;
3138 }
3139
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003140 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003141 // currentIndex
3142 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3143 {
3144 // This is done for simplicity. 1st entry is needed to calculate
3145 // time offset. To improve efficiency, one can get to the entry
3146 // directly (possibly with flatmap's nth method)
3147 continue;
3148 }
3149
3150 // currentCodeIndex is within top and skip or equal to specified code
3151 // index
3152
3153 // Get the Created time from the timestamp
3154 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003155 entryTimeStr =
3156 crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003157
3158 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3159 std::ostringstream hexCode;
3160 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303161 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003162 std::ostringstream timeOffsetStr;
3163 // Set Fixed -Point Notation
3164 timeOffsetStr << std::fixed;
3165 // Set precision to 4 digits
3166 timeOffsetStr << std::setprecision(4);
3167 // Add double to stream
3168 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3169 std::vector<std::string> messageArgs = {
3170 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3171
3172 // Get MessageArgs template from message registry
3173 std::string msg;
3174 if (message != nullptr)
3175 {
3176 msg = message->message;
3177
3178 // fill in this post code value
3179 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003180 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003181 {
3182 std::string argStr = "%" + std::to_string(++i);
3183 size_t argPos = msg.find(argStr);
3184 if (argPos != std::string::npos)
3185 {
3186 msg.replace(argPos, argStr.length(), messageArg);
3187 }
3188 }
3189 }
3190
Tim Leed4342a92020-04-27 11:47:58 +08003191 // Get Severity template from message registry
3192 std::string severity;
3193 if (message != nullptr)
3194 {
3195 severity = message->severity;
3196 }
3197
ZhikuiRena3316fc2020-01-29 14:58:08 -08003198 // add to AsyncResp
3199 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003200 nlohmann::json& bmcLogEntry = logEntryArray.back();
George Liu0fda0f12021-11-16 10:06:17 +08003201 bmcLogEntry = {
3202 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
3203 {"@odata.id",
3204 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3205 postcodeEntryID},
3206 {"Name", "POST Code Log Entry"},
3207 {"Id", postcodeEntryID},
3208 {"Message", std::move(msg)},
3209 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3210 {"MessageArgs", std::move(messageArgs)},
3211 {"EntryType", "Event"},
3212 {"Severity", std::move(severity)},
3213 {"Created", entryTimeStr}};
George Liu647b3cd2021-07-05 12:43:56 +08003214 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3215 {
3216 bmcLogEntry["AdditionalDataURI"] =
3217 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3218 postcodeEntryID + "/attachment";
3219 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003220 }
3221}
3222
zhanghch058d1b46d2021-04-01 11:18:24 +08003223static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003224 const uint16_t bootIndex,
3225 const uint64_t codeIndex)
3226{
3227 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303228 [aResp, bootIndex,
3229 codeIndex](const boost::system::error_code ec,
3230 const boost::container::flat_map<
3231 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3232 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003233 if (ec)
3234 {
3235 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3236 messages::internalError(aResp->res);
3237 return;
3238 }
3239
3240 // skip the empty postcode boots
3241 if (postcode.empty())
3242 {
3243 return;
3244 }
3245
3246 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3247
3248 aResp->res.jsonValue["Members@odata.count"] =
3249 aResp->res.jsonValue["Members"].size();
3250 },
Jonathan Doman15124762021-01-07 17:54:17 -08003251 "xyz.openbmc_project.State.Boot.PostCode0",
3252 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003253 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3254 bootIndex);
3255}
3256
zhanghch058d1b46d2021-04-01 11:18:24 +08003257static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003258 const uint16_t bootIndex,
3259 const uint16_t bootCount,
3260 const uint64_t entryCount, const uint64_t skip,
3261 const uint64_t top)
3262{
3263 crow::connections::systemBus->async_method_call(
3264 [aResp, bootIndex, bootCount, entryCount, skip,
3265 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303266 const boost::container::flat_map<
3267 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3268 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003269 if (ec)
3270 {
3271 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3272 messages::internalError(aResp->res);
3273 return;
3274 }
3275
3276 uint64_t endCount = entryCount;
3277 if (!postcode.empty())
3278 {
3279 endCount = entryCount + postcode.size();
3280
3281 if ((skip < endCount) && ((top + skip) > entryCount))
3282 {
3283 uint64_t thisBootSkip =
3284 std::max(skip, entryCount) - entryCount;
3285 uint64_t thisBootTop =
3286 std::min(top + skip, endCount) - entryCount;
3287
3288 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3289 thisBootSkip, thisBootTop);
3290 }
3291 aResp->res.jsonValue["Members@odata.count"] = endCount;
3292 }
3293
3294 // continue to previous bootIndex
3295 if (bootIndex < bootCount)
3296 {
3297 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3298 bootCount, endCount, skip, top);
3299 }
3300 else
3301 {
3302 aResp->res.jsonValue["Members@odata.nextLink"] =
George Liu0fda0f12021-11-16 10:06:17 +08003303 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
ZhikuiRena3316fc2020-01-29 14:58:08 -08003304 std::to_string(skip + top);
3305 }
3306 },
Jonathan Doman15124762021-01-07 17:54:17 -08003307 "xyz.openbmc_project.State.Boot.PostCode0",
3308 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003309 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3310 bootIndex);
3311}
3312
zhanghch058d1b46d2021-04-01 11:18:24 +08003313static void
3314 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3315 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003316{
3317 uint64_t entryCount = 0;
3318 crow::connections::systemBus->async_method_call(
3319 [aResp, entryCount, skip,
3320 top](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003321 const std::variant<uint16_t>& bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003322 if (ec)
3323 {
3324 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3325 messages::internalError(aResp->res);
3326 return;
3327 }
3328 auto pVal = std::get_if<uint16_t>(&bootCount);
3329 if (pVal)
3330 {
3331 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3332 }
3333 else
3334 {
3335 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3336 }
3337 },
Jonathan Doman15124762021-01-07 17:54:17 -08003338 "xyz.openbmc_project.State.Boot.PostCode0",
3339 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003340 "org.freedesktop.DBus.Properties", "Get",
3341 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3342}
3343
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003344inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003345{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003346 BMCWEB_ROUTE(app,
3347 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003348 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003349 .methods(boost::beast::http::verb::get)(
3350 [](const crow::Request& req,
3351 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3352 asyncResp->res.jsonValue["@odata.type"] =
3353 "#LogEntryCollection.LogEntryCollection";
3354 asyncResp->res.jsonValue["@odata.id"] =
3355 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3356 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3357 asyncResp->res.jsonValue["Description"] =
3358 "Collection of POST Code Log Entries";
3359 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3360 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003361
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003362 uint64_t skip = 0;
3363 uint64_t top = maxEntriesPerPage; // Show max entries by default
3364 if (!getSkipParam(asyncResp, req, skip))
3365 {
3366 return;
3367 }
3368 if (!getTopParam(asyncResp, req, top))
3369 {
3370 return;
3371 }
3372 getCurrentBootNumber(asyncResp, skip, top);
3373 });
3374}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003375
George Liu647b3cd2021-07-05 12:43:56 +08003376/**
3377 * @brief Parse post code ID and get the current value and index value
3378 * eg: postCodeID=B1-2, currentValue=1, index=2
3379 *
3380 * @param[in] postCodeID Post Code ID
3381 * @param[out] currentValue Current value
3382 * @param[out] index Index value
3383 *
3384 * @return bool true if the parsing is successful, false the parsing fails
3385 */
3386inline static bool parsePostCode(const std::string& postCodeID,
3387 uint64_t& currentValue, uint16_t& index)
3388{
3389 std::vector<std::string> split;
3390 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3391 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3392 {
3393 return false;
3394 }
3395
3396 const char* start = split[0].data() + 1;
3397 const char* end = split[0].data() + split[0].size();
3398 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3399
3400 if (ptrIndex != end || ecIndex != std::errc())
3401 {
3402 return false;
3403 }
3404
3405 start = split[1].data();
3406 end = split[1].data() + split[1].size();
3407 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3408 if (ptrValue != end || ecValue != std::errc())
3409 {
3410 return false;
3411 }
3412
3413 return true;
3414}
3415
3416inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3417{
George Liu0fda0f12021-11-16 10:06:17 +08003418 BMCWEB_ROUTE(
3419 app,
3420 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003421 .privileges(redfish::privileges::getLogEntry)
3422 .methods(boost::beast::http::verb::get)(
3423 [](const crow::Request& req,
3424 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3425 const std::string& postCodeID) {
3426 if (!http_helpers::isOctetAccepted(
3427 req.getHeaderValue("Accept")))
3428 {
3429 asyncResp->res.result(
3430 boost::beast::http::status::bad_request);
3431 return;
3432 }
3433
3434 uint64_t currentValue = 0;
3435 uint16_t index = 0;
3436 if (!parsePostCode(postCodeID, currentValue, index))
3437 {
3438 messages::resourceNotFound(asyncResp->res, "LogEntry",
3439 postCodeID);
3440 return;
3441 }
3442
3443 crow::connections::systemBus->async_method_call(
3444 [asyncResp, postCodeID, currentValue](
3445 const boost::system::error_code ec,
3446 const std::vector<std::tuple<
3447 uint64_t, std::vector<uint8_t>>>& postcodes) {
3448 if (ec.value() == EBADR)
3449 {
3450 messages::resourceNotFound(asyncResp->res,
3451 "LogEntry", postCodeID);
3452 return;
3453 }
3454 if (ec)
3455 {
3456 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3457 messages::internalError(asyncResp->res);
3458 return;
3459 }
3460
3461 size_t value = static_cast<size_t>(currentValue) - 1;
3462 if (value == std::string::npos ||
3463 postcodes.size() < currentValue)
3464 {
3465 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3466 messages::resourceNotFound(asyncResp->res,
3467 "LogEntry", postCodeID);
3468 return;
3469 }
3470
3471 auto& [tID, code] = postcodes[value];
3472 if (code.empty())
3473 {
3474 BMCWEB_LOG_INFO << "No found post code data";
3475 messages::resourceNotFound(asyncResp->res,
3476 "LogEntry", postCodeID);
3477 return;
3478 }
3479
3480 std::string_view strData(
3481 reinterpret_cast<const char*>(code.data()),
3482 code.size());
3483
3484 asyncResp->res.addHeader("Content-Type",
3485 "application/octet-stream");
3486 asyncResp->res.addHeader("Content-Transfer-Encoding",
3487 "Base64");
3488 asyncResp->res.body() =
3489 crow::utility::base64encode(strData);
3490 },
3491 "xyz.openbmc_project.State.Boot.PostCode0",
3492 "/xyz/openbmc_project/State/Boot/PostCode0",
3493 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3494 index);
3495 });
3496}
3497
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003498inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003499{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003500 BMCWEB_ROUTE(
3501 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003502 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003503 .methods(boost::beast::http::verb::get)(
3504 [](const crow::Request&,
3505 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3506 const std::string& targetID) {
George Liu647b3cd2021-07-05 12:43:56 +08003507 uint16_t bootIndex = 0;
3508 uint64_t codeIndex = 0;
3509 if (!parsePostCode(targetID, codeIndex, bootIndex))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003510 {
3511 // Requested ID was not found
3512 messages::resourceMissingAtURI(asyncResp->res, targetID);
3513 return;
3514 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003515 if (bootIndex == 0 || codeIndex == 0)
3516 {
3517 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3518 << targetID;
3519 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003520
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003521 asyncResp->res.jsonValue["@odata.type"] =
3522 "#LogEntry.v1_4_0.LogEntry";
3523 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08003524 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003525 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3526 asyncResp->res.jsonValue["Description"] =
3527 "Collection of POST Code Log Entries";
3528 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3529 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003530
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003531 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3532 });
3533}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003534
Ed Tanous1da66f72018-07-27 16:13:37 -07003535} // namespace redfish