blob: fb515fe98c108feaf56ea924fcc30076f82f2f26 [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 }
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500183 entryTimestamp = crow::utility::getDateTime(
184 static_cast<std::time_t>(timestamp / 1000 / 1000));
185 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800186}
187
zhanghch058d1b46d2021-04-01 11:18:24 +0800188static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
189 const crow::Request& req, uint64_t& skip)
Jason M. Bills16428a12018-11-02 12:42:29 -0700190{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700191 boost::urls::query_params_view::iterator it = req.urlParams.find("$skip");
James Feist5a7e8772020-07-22 09:08:38 -0700192 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700193 {
James Feist5a7e8772020-07-22 09:08:38 -0700194 std::string skipParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500195 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700196 skip = std::strtoul(skipParam.c_str(), &ptr, 10);
197 if (skipParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700198 {
199
zhanghch058d1b46d2021-04-01 11:18:24 +0800200 messages::queryParameterValueTypeError(
201 asyncResp->res, std::string(skipParam), "$skip");
Jason M. Bills16428a12018-11-02 12:42:29 -0700202 return false;
203 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700204 }
205 return true;
206}
207
Ed Tanous271584a2019-07-09 16:24:22 -0700208static constexpr const uint64_t maxEntriesPerPage = 1000;
zhanghch058d1b46d2021-04-01 11:18:24 +0800209static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
210 const crow::Request& req, uint64_t& top)
Jason M. Bills16428a12018-11-02 12:42:29 -0700211{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700212 boost::urls::query_params_view::iterator it = req.urlParams.find("$top");
James Feist5a7e8772020-07-22 09:08:38 -0700213 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700214 {
James Feist5a7e8772020-07-22 09:08:38 -0700215 std::string topParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500216 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700217 top = std::strtoul(topParam.c_str(), &ptr, 10);
218 if (topParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700219 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800220 messages::queryParameterValueTypeError(
221 asyncResp->res, std::string(topParam), "$top");
Jason M. Bills16428a12018-11-02 12:42:29 -0700222 return false;
223 }
Ed Tanous271584a2019-07-09 16:24:22 -0700224 if (top < 1U || top > maxEntriesPerPage)
Jason M. Bills16428a12018-11-02 12:42:29 -0700225 {
226
227 messages::queryParameterOutOfRange(
zhanghch058d1b46d2021-04-01 11:18:24 +0800228 asyncResp->res, std::to_string(top), "$top",
Jason M. Bills16428a12018-11-02 12:42:29 -0700229 "1-" + std::to_string(maxEntriesPerPage));
230 return false;
231 }
232 }
233 return true;
234}
235
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700236inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
237 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700238{
239 int ret = 0;
240 static uint64_t prevTs = 0;
241 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700242 if (firstEntry)
243 {
244 prevTs = 0;
245 }
246
Jason M. Bills16428a12018-11-02 12:42:29 -0700247 // Get the entry timestamp
248 uint64_t curTs = 0;
249 ret = sd_journal_get_realtime_usec(journal, &curTs);
250 if (ret < 0)
251 {
252 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
253 << strerror(-ret);
254 return false;
255 }
256 // If the timestamp isn't unique, increment the index
257 if (curTs == prevTs)
258 {
259 index++;
260 }
261 else
262 {
263 // Otherwise, reset it
264 index = 0;
265 }
266 // Save the timestamp
267 prevTs = curTs;
268
269 entryID = std::to_string(curTs);
270 if (index > 0)
271 {
272 entryID += "_" + std::to_string(index);
273 }
274 return true;
275}
276
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500277static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700278 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700279{
Ed Tanous271584a2019-07-09 16:24:22 -0700280 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700281 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700282 if (firstEntry)
283 {
284 prevTs = 0;
285 }
286
Jason M. Bills95820182019-04-22 16:25:34 -0700287 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700288 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700289 std::tm timeStruct = {};
290 std::istringstream entryStream(logEntry);
291 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
292 {
293 curTs = std::mktime(&timeStruct);
294 }
295 // If the timestamp isn't unique, increment the index
296 if (curTs == prevTs)
297 {
298 index++;
299 }
300 else
301 {
302 // Otherwise, reset it
303 index = 0;
304 }
305 // Save the timestamp
306 prevTs = curTs;
307
308 entryID = std::to_string(curTs);
309 if (index > 0)
310 {
311 entryID += "_" + std::to_string(index);
312 }
313 return true;
314}
315
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700316inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800317 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
318 const std::string& entryID, uint64_t& timestamp,
319 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700320{
321 if (entryID.empty())
322 {
323 return false;
324 }
325 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800326 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700327
Ed Tanous81ce6092020-12-17 16:54:55 +0000328 auto underscorePos = tsStr.find('_');
Jason M. Bills16428a12018-11-02 12:42:29 -0700329 if (underscorePos != tsStr.npos)
330 {
331 // Timestamp has an index
332 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800333 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700334 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700335 auto [ptr, ec] = std::from_chars(
336 indexStr.data(), indexStr.data() + indexStr.size(), index);
337 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700338 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800339 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700340 return false;
341 }
342 }
343 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700344 auto [ptr, ec] =
345 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
346 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700347 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800348 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700349 return false;
350 }
351 return true;
352}
353
Jason M. Bills95820182019-04-22 16:25:34 -0700354static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500355 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700356{
357 static const std::filesystem::path redfishLogDir = "/var/log";
358 static const std::string redfishLogFilename = "redfish";
359
360 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500361 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700362 std::filesystem::directory_iterator(redfishLogDir))
363 {
364 // If we find a redfish log file, save the path
365 std::string filename = dirEnt.path().filename();
366 if (boost::starts_with(filename, redfishLogFilename))
367 {
368 redfishLogFiles.emplace_back(redfishLogDir / filename);
369 }
370 }
371 // As the log files rotate, they are appended with a ".#" that is higher for
372 // the older logs. Since we don't expect more than 10 log files, we
373 // can just sort the list to get them in order from newest to oldest
374 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
375
376 return !redfishLogFiles.empty();
377}
378
zhanghch058d1b46d2021-04-01 11:18:24 +0800379inline void
380 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
381 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500382{
383 std::string dumpPath;
384 if (dumpType == "BMC")
385 {
386 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
387 }
388 else if (dumpType == "System")
389 {
390 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
391 }
392 else
393 {
394 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
395 messages::internalError(asyncResp->res);
396 return;
397 }
398
399 crow::connections::systemBus->async_method_call(
400 [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
401 GetManagedObjectsType& resp) {
402 if (ec)
403 {
404 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
405 messages::internalError(asyncResp->res);
406 return;
407 }
408
409 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
410 entriesArray = nlohmann::json::array();
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500411 std::string dumpEntryPath =
412 "/xyz/openbmc_project/dump/" +
413 std::string(boost::algorithm::to_lower_copy(dumpType)) +
414 "/entry/";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500415
416 for (auto& object : resp)
417 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500418 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500419 {
420 continue;
421 }
422 std::time_t timestamp;
423 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500424 std::string dumpStatus;
425 nlohmann::json thisEntry;
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000426
427 std::string entryID = object.first.filename();
428 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500429 {
430 continue;
431 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500432
433 for (auto& interfaceMap : object.second)
434 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500435 if (interfaceMap.first ==
436 "xyz.openbmc_project.Common.Progress")
437 {
438 for (auto& propertyMap : interfaceMap.second)
439 {
440 if (propertyMap.first == "Status")
441 {
442 auto status = std::get_if<std::string>(
443 &propertyMap.second);
444 if (status == nullptr)
445 {
446 messages::internalError(asyncResp->res);
447 break;
448 }
449 dumpStatus = *status;
450 }
451 }
452 }
453 else if (interfaceMap.first ==
454 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500455 {
456
457 for (auto& propertyMap : interfaceMap.second)
458 {
459 if (propertyMap.first == "Size")
460 {
461 auto sizePtr =
462 std::get_if<uint64_t>(&propertyMap.second);
463 if (sizePtr == nullptr)
464 {
465 messages::internalError(asyncResp->res);
466 break;
467 }
468 size = *sizePtr;
469 break;
470 }
471 }
472 }
473 else if (interfaceMap.first ==
474 "xyz.openbmc_project.Time.EpochTime")
475 {
476
477 for (auto& propertyMap : interfaceMap.second)
478 {
479 if (propertyMap.first == "Elapsed")
480 {
481 const uint64_t* usecsTimeStamp =
482 std::get_if<uint64_t>(&propertyMap.second);
483 if (usecsTimeStamp == nullptr)
484 {
485 messages::internalError(asyncResp->res);
486 break;
487 }
488 timestamp =
489 static_cast<std::time_t>(*usecsTimeStamp);
490 break;
491 }
492 }
493 }
494 }
495
George Liu0fda0f12021-11-16 10:06:17 +0800496 if (dumpStatus !=
497 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500498 !dumpStatus.empty())
499 {
500 // Dump status is not Complete, no need to enumerate
501 continue;
502 }
503
George Liu647b3cd2021-07-05 12:43:56 +0800504 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500505 thisEntry["@odata.id"] = dumpPath + entryID;
506 thisEntry["Id"] = entryID;
507 thisEntry["EntryType"] = "Event";
508 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
509 thisEntry["Name"] = dumpType + " Dump Entry";
510
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500511 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512
513 if (dumpType == "BMC")
514 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500515 thisEntry["DiagnosticDataType"] = "Manager";
516 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500517 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
518 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500519 }
520 else if (dumpType == "System")
521 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500522 thisEntry["DiagnosticDataType"] = "OEM";
523 thisEntry["OEMDiagnosticDataType"] = "System";
524 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500525 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
526 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500527 }
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500528 entriesArray.push_back(std::move(thisEntry));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500529 }
530 asyncResp->res.jsonValue["Members@odata.count"] =
531 entriesArray.size();
532 },
533 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
534 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
535}
536
zhanghch058d1b46d2021-04-01 11:18:24 +0800537inline void
538 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
539 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500540{
541 std::string dumpPath;
542 if (dumpType == "BMC")
543 {
544 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
545 }
546 else if (dumpType == "System")
547 {
548 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
549 }
550 else
551 {
552 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
553 messages::internalError(asyncResp->res);
554 return;
555 }
556
557 crow::connections::systemBus->async_method_call(
558 [asyncResp, entryID, dumpPath, dumpType](
559 const boost::system::error_code ec, GetManagedObjectsType& resp) {
560 if (ec)
561 {
562 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
563 messages::internalError(asyncResp->res);
564 return;
565 }
566
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500567 bool foundDumpEntry = false;
568 std::string dumpEntryPath =
569 "/xyz/openbmc_project/dump/" +
570 std::string(boost::algorithm::to_lower_copy(dumpType)) +
571 "/entry/";
572
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500573 for (auto& objectPath : resp)
574 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500575 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500576 {
577 continue;
578 }
579
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500580 foundDumpEntry = true;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 std::time_t timestamp;
582 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500583 std::string dumpStatus;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500584
585 for (auto& interfaceMap : objectPath.second)
586 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500587 if (interfaceMap.first ==
588 "xyz.openbmc_project.Common.Progress")
589 {
590 for (auto& propertyMap : interfaceMap.second)
591 {
592 if (propertyMap.first == "Status")
593 {
594 auto status = std::get_if<std::string>(
595 &propertyMap.second);
596 if (status == nullptr)
597 {
598 messages::internalError(asyncResp->res);
599 break;
600 }
601 dumpStatus = *status;
602 }
603 }
604 }
605 else if (interfaceMap.first ==
606 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500607 {
608 for (auto& propertyMap : interfaceMap.second)
609 {
610 if (propertyMap.first == "Size")
611 {
612 auto sizePtr =
613 std::get_if<uint64_t>(&propertyMap.second);
614 if (sizePtr == nullptr)
615 {
616 messages::internalError(asyncResp->res);
617 break;
618 }
619 size = *sizePtr;
620 break;
621 }
622 }
623 }
624 else if (interfaceMap.first ==
625 "xyz.openbmc_project.Time.EpochTime")
626 {
627 for (auto& propertyMap : interfaceMap.second)
628 {
629 if (propertyMap.first == "Elapsed")
630 {
631 const uint64_t* usecsTimeStamp =
632 std::get_if<uint64_t>(&propertyMap.second);
633 if (usecsTimeStamp == nullptr)
634 {
635 messages::internalError(asyncResp->res);
636 break;
637 }
638 timestamp =
639 static_cast<std::time_t>(*usecsTimeStamp);
640 break;
641 }
642 }
643 }
644 }
645
George Liu0fda0f12021-11-16 10:06:17 +0800646 if (dumpStatus !=
647 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500648 !dumpStatus.empty())
649 {
650 // Dump status is not Complete
651 // return not found until status is changed to Completed
652 messages::resourceNotFound(asyncResp->res,
653 dumpType + " dump", entryID);
654 return;
655 }
656
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500657 asyncResp->res.jsonValue["@odata.type"] =
George Liu647b3cd2021-07-05 12:43:56 +0800658 "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500659 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
660 asyncResp->res.jsonValue["Id"] = entryID;
661 asyncResp->res.jsonValue["EntryType"] = "Event";
662 asyncResp->res.jsonValue["Created"] =
663 crow::utility::getDateTime(timestamp);
664 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
665
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500666 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500667
668 if (dumpType == "BMC")
669 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500670 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
671 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500672 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
673 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500674 }
675 else if (dumpType == "System")
676 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500677 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
678 asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500679 "System";
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500680 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500681 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
682 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500683 }
684 }
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500685 if (foundDumpEntry == false)
686 {
687 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
688 messages::internalError(asyncResp->res);
689 return;
690 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500691 },
692 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
693 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
694}
695
zhanghch058d1b46d2021-04-01 11:18:24 +0800696inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800697 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500698 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500699{
George Liu3de8d8b2021-03-22 17:49:39 +0800700 auto respHandler = [asyncResp,
701 entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500702 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
703 if (ec)
704 {
George Liu3de8d8b2021-03-22 17:49:39 +0800705 if (ec.value() == EBADR)
706 {
707 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
708 return;
709 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500710 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
711 << ec;
712 messages::internalError(asyncResp->res);
713 return;
714 }
715 };
716 crow::connections::systemBus->async_method_call(
717 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500718 "/xyz/openbmc_project/dump/" +
719 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
720 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500721 "xyz.openbmc_project.Object.Delete", "Delete");
722}
723
zhanghch058d1b46d2021-04-01 11:18:24 +0800724inline void
Ed Tanous98be3e32021-09-16 15:05:36 -0700725 createDumpTaskCallback(task::Payload&& payload,
zhanghch058d1b46d2021-04-01 11:18:24 +0800726 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
727 const uint32_t& dumpId, const std::string& dumpPath,
728 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500729{
730 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500731 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500732 boost::system::error_code err, sdbusplus::message::message& m,
733 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000734 if (err)
735 {
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500736 BMCWEB_LOG_ERROR << "Error in creating a dump";
737 taskData->state = "Cancelled";
738 return task::completed;
Ed Tanouscb13a392020-07-25 19:02:03 +0000739 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500740 std::vector<std::pair<
741 std::string,
742 std::vector<std::pair<std::string, std::variant<std::string>>>>>
743 interfacesList;
744
745 sdbusplus::message::object_path objPath;
746
747 m.read(objPath, interfacesList);
748
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500749 if (objPath.str ==
750 "/xyz/openbmc_project/dump/" +
751 std::string(boost::algorithm::to_lower_copy(dumpType)) +
752 "/entry/" + std::to_string(dumpId))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500753 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500754 nlohmann::json retMessage = messages::success();
755 taskData->messages.emplace_back(retMessage);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500756
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500757 std::string headerLoc =
758 "Location: " + dumpPath + std::to_string(dumpId);
759 taskData->payload->httpHeaders.emplace_back(
760 std::move(headerLoc));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500761
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500762 taskData->state = "Completed";
763 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500764 }
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500765 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500766 },
767 "type='signal',interface='org.freedesktop.DBus."
768 "ObjectManager',"
769 "member='InterfacesAdded', "
770 "path='/xyz/openbmc_project/dump'");
771
772 task->startTimer(std::chrono::minutes(3));
773 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -0700774 task->payload.emplace(std::move(payload));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500775}
776
zhanghch058d1b46d2021-04-01 11:18:24 +0800777inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
778 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500779{
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500780
781 std::string dumpPath;
782 if (dumpType == "BMC")
783 {
784 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
785 }
786 else if (dumpType == "System")
787 {
788 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
789 }
790 else
791 {
792 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
793 messages::internalError(asyncResp->res);
794 return;
795 }
796
797 std::optional<std::string> diagnosticDataType;
798 std::optional<std::string> oemDiagnosticDataType;
799
800 if (!redfish::json_util::readJson(
801 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
802 "OEMDiagnosticDataType", oemDiagnosticDataType))
803 {
804 return;
805 }
806
807 if (dumpType == "System")
808 {
809 if (!oemDiagnosticDataType || !diagnosticDataType)
810 {
811 BMCWEB_LOG_ERROR << "CreateDump action parameter "
812 "'DiagnosticDataType'/"
813 "'OEMDiagnosticDataType' value not found!";
814 messages::actionParameterMissing(
815 asyncResp->res, "CollectDiagnosticData",
816 "DiagnosticDataType & OEMDiagnosticDataType");
817 return;
818 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700819 if ((*oemDiagnosticDataType != "System") ||
820 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500821 {
822 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
823 messages::invalidObject(asyncResp->res,
824 "System Dump creation parameters");
825 return;
826 }
827 }
828 else if (dumpType == "BMC")
829 {
830 if (!diagnosticDataType)
831 {
George Liu0fda0f12021-11-16 10:06:17 +0800832 BMCWEB_LOG_ERROR
833 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500834 messages::actionParameterMissing(
835 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
836 return;
837 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700838 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500839 {
840 BMCWEB_LOG_ERROR
841 << "Wrong parameter value passed for 'DiagnosticDataType'";
842 messages::invalidObject(asyncResp->res,
843 "BMC Dump creation parameters");
844 return;
845 }
846 }
847
848 crow::connections::systemBus->async_method_call(
Ed Tanous98be3e32021-09-16 15:05:36 -0700849 [asyncResp, payload(task::Payload(req)), dumpPath,
850 dumpType](const boost::system::error_code ec,
851 const uint32_t& dumpId) mutable {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500852 if (ec)
853 {
854 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
855 messages::internalError(asyncResp->res);
856 return;
857 }
858 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
859
Ed Tanous98be3e32021-09-16 15:05:36 -0700860 createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
861 dumpPath, dumpType);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500862 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500863 "xyz.openbmc_project.Dump.Manager",
864 "/xyz/openbmc_project/dump/" +
865 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500866 "xyz.openbmc_project.Dump.Create", "CreateDump");
867}
868
zhanghch058d1b46d2021-04-01 11:18:24 +0800869inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
870 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500871{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500872 std::string dumpTypeLowerCopy =
873 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800874
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500875 crow::connections::systemBus->async_method_call(
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500876 [asyncResp, dumpType](const boost::system::error_code ec,
877 const std::vector<std::string>& subTreePaths) {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500878 if (ec)
879 {
880 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
881 messages::internalError(asyncResp->res);
882 return;
883 }
884
885 for (const std::string& path : subTreePaths)
886 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000887 sdbusplus::message::object_path objPath(path);
888 std::string logID = objPath.filename();
889 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500890 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000891 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500892 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000893 deleteDumpEntry(asyncResp, logID, dumpType);
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500894 }
895 },
896 "xyz.openbmc_project.ObjectMapper",
897 "/xyz/openbmc_project/object_mapper",
898 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500899 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
900 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
901 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500902}
903
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700904inline static void parseCrashdumpParameters(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500905 const std::vector<std::pair<std::string, VariantType>>& params,
906 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700907{
908 for (auto property : params)
909 {
910 if (property.first == "Timestamp")
911 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500912 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500913 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700914 if (value != nullptr)
915 {
916 timestamp = *value;
917 }
918 }
919 else if (property.first == "Filename")
920 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500921 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500922 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700923 if (value != nullptr)
924 {
925 filename = *value;
926 }
927 }
928 else if (property.first == "Log")
929 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500930 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500931 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700932 if (value != nullptr)
933 {
934 logfile = *value;
935 }
936 }
937 }
938}
939
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500940constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700941inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700942{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800943 /**
944 * Functions triggers appropriate requests on DBus
945 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700946 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700947 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700948 .methods(boost::beast::http::verb::get)(
949 [](const crow::Request&,
950 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
951
952 {
953 // Collections don't include the static data added by SubRoute
954 // because it has a duplicate entry for members
955 asyncResp->res.jsonValue["@odata.type"] =
956 "#LogServiceCollection.LogServiceCollection";
957 asyncResp->res.jsonValue["@odata.id"] =
958 "/redfish/v1/Systems/system/LogServices";
959 asyncResp->res.jsonValue["Name"] =
960 "System Log Services Collection";
961 asyncResp->res.jsonValue["Description"] =
962 "Collection of LogServices for this Computer System";
963 nlohmann::json& logServiceArray =
964 asyncResp->res.jsonValue["Members"];
965 logServiceArray = nlohmann::json::array();
966 logServiceArray.push_back(
967 {{"@odata.id",
968 "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500969#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700970 logServiceArray.push_back(
971 {{"@odata.id",
972 "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600973#endif
974
Jason M. Billsd53dd412019-02-12 17:16:22 -0800975#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700976 logServiceArray.push_back(
977 {{"@odata.id",
978 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800979#endif
Spencer Kub7028eb2021-10-26 15:27:35 +0800980
981#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
982 logServiceArray.push_back(
983 {{"@odata.id",
984 "/redfish/v1/Systems/system/LogServices/HostLogger"}});
985#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700986 asyncResp->res.jsonValue["Members@odata.count"] =
987 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800988
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700989 crow::connections::systemBus->async_method_call(
990 [asyncResp](const boost::system::error_code ec,
991 const std::vector<std::string>& subtreePath) {
992 if (ec)
993 {
994 BMCWEB_LOG_ERROR << ec;
995 return;
996 }
ZhikuiRena3316fc2020-01-29 14:58:08 -0800997
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700998 for (auto& pathStr : subtreePath)
999 {
1000 if (pathStr.find("PostCode") != std::string::npos)
1001 {
1002 nlohmann::json& logServiceArrayLocal =
1003 asyncResp->res.jsonValue["Members"];
1004 logServiceArrayLocal.push_back(
George Liu0fda0f12021-11-16 10:06:17 +08001005 {{"@odata.id",
1006 "/redfish/v1/Systems/system/LogServices/PostCodes"}});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001007 asyncResp->res
1008 .jsonValue["Members@odata.count"] =
1009 logServiceArrayLocal.size();
1010 return;
1011 }
1012 }
1013 },
1014 "xyz.openbmc_project.ObjectMapper",
1015 "/xyz/openbmc_project/object_mapper",
1016 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1017 0, std::array<const char*, 1>{postCodeIface});
1018 });
1019}
1020
1021inline void requestRoutesEventLogService(App& app)
1022{
1023 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001024 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001025 .methods(
1026 boost::beast::http::verb::
1027 get)([](const crow::Request&,
1028 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1029 asyncResp->res.jsonValue["@odata.id"] =
1030 "/redfish/v1/Systems/system/LogServices/EventLog";
1031 asyncResp->res.jsonValue["@odata.type"] =
1032 "#LogService.v1_1_0.LogService";
1033 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1034 asyncResp->res.jsonValue["Description"] =
1035 "System Event Log Service";
1036 asyncResp->res.jsonValue["Id"] = "EventLog";
1037 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301038
1039 std::pair<std::string, std::string> redfishDateTimeOffset =
1040 crow::utility::getDateTimeOffsetNow();
1041
1042 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1043 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1044 redfishDateTimeOffset.second;
1045
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001046 asyncResp->res.jsonValue["Entries"] = {
1047 {"@odata.id",
1048 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1049 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1050
George Liu0fda0f12021-11-16 10:06:17 +08001051 {"target",
1052 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001053 });
1054}
1055
1056inline void requestRoutesJournalEventLogClear(App& app)
1057{
1058 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1059 "LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001060 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001061 .methods(boost::beast::http::verb::post)(
1062 [](const crow::Request&,
1063 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1064 // Clear the EventLog by deleting the log files
1065 std::vector<std::filesystem::path> redfishLogFiles;
1066 if (getRedfishLogFiles(redfishLogFiles))
ZhikuiRena3316fc2020-01-29 14:58:08 -08001067 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001068 for (const std::filesystem::path& file : redfishLogFiles)
ZhikuiRena3316fc2020-01-29 14:58:08 -08001069 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001070 std::error_code ec;
1071 std::filesystem::remove(file, ec);
ZhikuiRena3316fc2020-01-29 14:58:08 -08001072 }
1073 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001074
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001075 // Reload rsyslog so it knows to start new log files
1076 crow::connections::systemBus->async_method_call(
1077 [asyncResp](const boost::system::error_code ec) {
1078 if (ec)
1079 {
1080 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1081 << ec;
1082 messages::internalError(asyncResp->res);
1083 return;
1084 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001085
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001086 messages::success(asyncResp->res);
1087 },
1088 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1089 "org.freedesktop.systemd1.Manager", "ReloadUnit",
1090 "rsyslog.service", "replace");
1091 });
1092}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001093
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001094static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001095 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001096 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001097{
Jason M. Bills95820182019-04-22 16:25:34 -07001098 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001099 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001100 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001101 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001102 {
1103 return 1;
1104 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001105 std::string timestamp = logEntry.substr(0, space);
1106 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001107 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001108 if (entryStart == std::string::npos)
1109 {
1110 return 1;
1111 }
1112 std::string_view entry(logEntry);
1113 entry.remove_prefix(entryStart);
1114 // Use split to separate the entry into its fields
1115 std::vector<std::string> logEntryFields;
1116 boost::split(logEntryFields, entry, boost::is_any_of(","),
1117 boost::token_compress_on);
1118 // We need at least a MessageId to be valid
1119 if (logEntryFields.size() < 1)
1120 {
1121 return 1;
1122 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001123 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001124
Jason M. Bills4851d452019-03-28 11:27:48 -07001125 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001126 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001127 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001128
Jason M. Bills4851d452019-03-28 11:27:48 -07001129 std::string msg;
1130 std::string severity;
1131 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001132 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001133 msg = message->message;
1134 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001135 }
1136
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001137 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001138 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001139 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001140 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001141 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001142 // If the first string is empty, assume there are no MessageArgs
1143 std::size_t messageArgsSize = 0;
1144 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001145 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001146 messageArgsSize = logEntryFields.size() - 1;
1147 }
1148
Ed Tanous23a21a12020-07-25 04:45:05 +00001149 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001150
1151 // Fill the MessageArgs into the Message
1152 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001153 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001154 {
1155 std::string argStr = "%" + std::to_string(++i);
1156 size_t argPos = msg.find(argStr);
1157 if (argPos != std::string::npos)
1158 {
1159 msg.replace(argPos, argStr.length(), messageArg);
1160 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001161 }
1162 }
1163
Jason M. Bills95820182019-04-22 16:25:34 -07001164 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1165 // format which matches the Redfish format except for the fractional seconds
1166 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001167 std::size_t dot = timestamp.find_first_of('.');
1168 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001169 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001170 {
Jason M. Bills95820182019-04-22 16:25:34 -07001171 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001172 }
1173
1174 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001175 logEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08001176 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001177 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001178 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001179 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001180 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001181 {"Id", logEntryID},
1182 {"Message", std::move(msg)},
1183 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001184 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001185 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001186 {"Severity", std::move(severity)},
1187 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001188 return 0;
1189}
1190
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001191inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001192{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001193 BMCWEB_ROUTE(app,
1194 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001195 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001196 .methods(boost::beast::http::verb::get)(
1197 [](const crow::Request& req,
1198 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1199 uint64_t skip = 0;
1200 uint64_t top = maxEntriesPerPage; // Show max entries by default
1201 if (!getSkipParam(asyncResp, req, skip))
Jason M. Bills95820182019-04-22 16:25:34 -07001202 {
Jason M. Bills95820182019-04-22 16:25:34 -07001203 return;
1204 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001205 if (!getTopParam(asyncResp, req, top))
Jason M. Bills897967d2019-07-29 17:05:30 -07001206 {
Jason M. Bills897967d2019-07-29 17:05:30 -07001207 return;
1208 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001209 // Collections don't include the static data added by SubRoute
1210 // because it has a duplicate entry for members
1211 asyncResp->res.jsonValue["@odata.type"] =
1212 "#LogEntryCollection.LogEntryCollection";
1213 asyncResp->res.jsonValue["@odata.id"] =
1214 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1215 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1216 asyncResp->res.jsonValue["Description"] =
1217 "Collection of System Event Log Entries";
Jason M. Bills897967d2019-07-29 17:05:30 -07001218
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001219 nlohmann::json& logEntryArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001220 asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001221 logEntryArray = nlohmann::json::array();
1222 // Go through the log files and create a unique ID for each
1223 // entry
1224 std::vector<std::filesystem::path> redfishLogFiles;
1225 getRedfishLogFiles(redfishLogFiles);
1226 uint64_t entryCount = 0;
1227 std::string logEntry;
1228
1229 // Oldest logs are in the last file, so start there and loop
1230 // backwards
1231 for (auto it = redfishLogFiles.rbegin();
1232 it < redfishLogFiles.rend(); it++)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001233 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001234 std::ifstream logStream(*it);
1235 if (!logStream.is_open())
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001236 {
1237 continue;
1238 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001239
1240 // Reset the unique ID on the first entry
1241 bool firstEntry = true;
1242 while (std::getline(logStream, logEntry))
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001243 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001244 entryCount++;
1245 // Handle paging using skip (number of entries to skip
1246 // from the start) and top (number of entries to
1247 // display)
1248 if (entryCount <= skip || entryCount > skip + top)
George Liuebd45902020-08-26 14:21:10 +08001249 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001250 continue;
George Liuebd45902020-08-26 14:21:10 +08001251 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001252
1253 std::string idStr;
1254 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
George Liuebd45902020-08-26 14:21:10 +08001255 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001256 continue;
George Liuebd45902020-08-26 14:21:10 +08001257 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001258
1259 if (firstEntry)
1260 {
1261 firstEntry = false;
1262 }
1263
1264 logEntryArray.push_back({});
1265 nlohmann::json& bmcLogEntry = logEntryArray.back();
1266 if (fillEventLogEntryJson(idStr, logEntry,
1267 bmcLogEntry) != 0)
Xiaochao Ma75710de2021-01-21 17:56:02 +08001268 {
1269 messages::internalError(asyncResp->res);
1270 return;
1271 }
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001272 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001273 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001274 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1275 if (skip + top < entryCount)
Ed Tanous271584a2019-07-09 16:24:22 -07001276 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001277 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001278 "/redfish/v1/Systems/system/LogServices/EventLog/"
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001279 "Entries?$skip=" +
1280 std::to_string(skip + top);
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001281 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001282 });
1283}
Chicago Duan336e96c2019-07-15 14:22:08 +08001284
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001285inline void requestRoutesJournalEventLogEntry(App& app)
1286{
1287 BMCWEB_ROUTE(
1288 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001289 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001290 .methods(boost::beast::http::verb::get)(
1291 [](const crow::Request&,
1292 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1293 const std::string& param) {
1294 const std::string& targetID = param;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001295
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001296 // Go through the log files and check the unique ID for each
1297 // entry to find the target entry
1298 std::vector<std::filesystem::path> redfishLogFiles;
1299 getRedfishLogFiles(redfishLogFiles);
1300 std::string logEntry;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001301
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001302 // Oldest logs are in the last file, so start there and loop
1303 // backwards
1304 for (auto it = redfishLogFiles.rbegin();
1305 it < redfishLogFiles.rend(); it++)
1306 {
1307 std::ifstream logStream(*it);
1308 if (!logStream.is_open())
1309 {
1310 continue;
1311 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001312
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001313 // Reset the unique ID on the first entry
1314 bool firstEntry = true;
1315 while (std::getline(logStream, logEntry))
1316 {
1317 std::string idStr;
1318 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1319 {
1320 continue;
1321 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001322
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001323 if (firstEntry)
1324 {
1325 firstEntry = false;
1326 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001327
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001328 if (idStr == targetID)
1329 {
1330 if (fillEventLogEntryJson(
1331 idStr, logEntry,
1332 asyncResp->res.jsonValue) != 0)
1333 {
1334 messages::internalError(asyncResp->res);
1335 return;
1336 }
1337 return;
1338 }
1339 }
1340 }
1341 // Requested ID was not found
1342 messages::resourceMissingAtURI(asyncResp->res, targetID);
1343 });
1344}
1345
1346inline void requestRoutesDBusEventLogEntryCollection(App& app)
1347{
1348 BMCWEB_ROUTE(app,
1349 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001350 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001351 .methods(
1352 boost::beast::http::verb::
1353 get)([](const crow::Request&,
1354 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1355 // Collections don't include the static data added by SubRoute
1356 // because it has a duplicate entry for members
1357 asyncResp->res.jsonValue["@odata.type"] =
1358 "#LogEntryCollection.LogEntryCollection";
1359 asyncResp->res.jsonValue["@odata.id"] =
1360 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1361 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1362 asyncResp->res.jsonValue["Description"] =
1363 "Collection of System Event Log Entries";
1364
1365 // DBus implementation of EventLog/Entries
1366 // Make call to Logging Service to find all log entry objects
Xiaochao Ma75710de2021-01-21 17:56:02 +08001367 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001368 [asyncResp](const boost::system::error_code ec,
1369 GetManagedObjectsType& resp) {
Xiaochao Ma75710de2021-01-21 17:56:02 +08001370 if (ec)
1371 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001372 // TODO Handle for specific error code
1373 BMCWEB_LOG_ERROR
1374 << "getLogEntriesIfaceData resp_handler got error "
1375 << ec;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001376 messages::internalError(asyncResp->res);
1377 return;
1378 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001379 nlohmann::json& entriesArray =
1380 asyncResp->res.jsonValue["Members"];
1381 entriesArray = nlohmann::json::array();
1382 for (auto& objectPath : resp)
1383 {
1384 uint32_t* id = nullptr;
1385 std::time_t timestamp{};
1386 std::time_t updateTimestamp{};
1387 std::string* severity = nullptr;
1388 std::string* message = nullptr;
1389 std::string* filePath = nullptr;
1390 bool resolved = false;
1391 for (auto& interfaceMap : objectPath.second)
1392 {
1393 if (interfaceMap.first ==
1394 "xyz.openbmc_project.Logging.Entry")
1395 {
1396 for (auto& propertyMap : interfaceMap.second)
1397 {
1398 if (propertyMap.first == "Id")
1399 {
1400 id = std::get_if<uint32_t>(
1401 &propertyMap.second);
1402 }
1403 else if (propertyMap.first == "Timestamp")
1404 {
1405 const uint64_t* millisTimeStamp =
1406 std::get_if<uint64_t>(
1407 &propertyMap.second);
1408 if (millisTimeStamp != nullptr)
1409 {
1410 timestamp =
1411 crow::utility::getTimestamp(
1412 *millisTimeStamp);
1413 }
1414 }
1415 else if (propertyMap.first ==
1416 "UpdateTimestamp")
1417 {
1418 const uint64_t* millisTimeStamp =
1419 std::get_if<uint64_t>(
1420 &propertyMap.second);
1421 if (millisTimeStamp != nullptr)
1422 {
1423 updateTimestamp =
1424 crow::utility::getTimestamp(
1425 *millisTimeStamp);
1426 }
1427 }
1428 else if (propertyMap.first == "Severity")
1429 {
1430 severity = std::get_if<std::string>(
1431 &propertyMap.second);
1432 }
1433 else if (propertyMap.first == "Message")
1434 {
1435 message = std::get_if<std::string>(
1436 &propertyMap.second);
1437 }
1438 else if (propertyMap.first == "Resolved")
1439 {
1440 bool* resolveptr = std::get_if<bool>(
1441 &propertyMap.second);
1442 if (resolveptr == nullptr)
1443 {
1444 messages::internalError(
1445 asyncResp->res);
1446 return;
1447 }
1448 resolved = *resolveptr;
1449 }
1450 }
1451 if (id == nullptr || message == nullptr ||
1452 severity == nullptr)
1453 {
1454 messages::internalError(asyncResp->res);
1455 return;
1456 }
1457 }
1458 else if (interfaceMap.first ==
1459 "xyz.openbmc_project.Common.FilePath")
1460 {
1461 for (auto& propertyMap : interfaceMap.second)
1462 {
1463 if (propertyMap.first == "Path")
1464 {
1465 filePath = std::get_if<std::string>(
1466 &propertyMap.second);
1467 }
1468 }
1469 }
1470 }
1471 // Object path without the
1472 // xyz.openbmc_project.Logging.Entry interface, ignore
1473 // and continue.
1474 if (id == nullptr || message == nullptr ||
1475 severity == nullptr)
1476 {
1477 continue;
1478 }
1479 entriesArray.push_back({});
1480 nlohmann::json& thisEntry = entriesArray.back();
1481 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1482 thisEntry["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001483 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001484 std::to_string(*id);
1485 thisEntry["Name"] = "System Event Log Entry";
1486 thisEntry["Id"] = std::to_string(*id);
1487 thisEntry["Message"] = *message;
1488 thisEntry["Resolved"] = resolved;
1489 thisEntry["EntryType"] = "Event";
1490 thisEntry["Severity"] =
1491 translateSeverityDbusToRedfish(*severity);
1492 thisEntry["Created"] =
1493 crow::utility::getDateTime(timestamp);
1494 thisEntry["Modified"] =
1495 crow::utility::getDateTime(updateTimestamp);
1496 if (filePath != nullptr)
1497 {
1498 thisEntry["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001499 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001500 std::to_string(*id) + "/attachment";
1501 }
1502 }
1503 std::sort(entriesArray.begin(), entriesArray.end(),
1504 [](const nlohmann::json& left,
1505 const nlohmann::json& right) {
1506 return (left["Id"] <= right["Id"]);
1507 });
1508 asyncResp->res.jsonValue["Members@odata.count"] =
1509 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001510 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001511 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1512 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1513 });
1514}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001515
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001516inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001517{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001518 BMCWEB_ROUTE(
1519 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001520 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001521 .methods(boost::beast::http::verb::get)(
1522 [](const crow::Request&,
1523 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1524 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001525
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001526 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001527 std::string entryID = param;
1528 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001529
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001530 // DBus implementation of EventLog/Entries
1531 // Make call to Logging Service to find all log entry objects
1532 crow::connections::systemBus->async_method_call(
1533 [asyncResp, entryID](const boost::system::error_code ec,
1534 GetManagedPropertyType& resp) {
1535 if (ec.value() == EBADR)
1536 {
1537 messages::resourceNotFound(
1538 asyncResp->res, "EventLogEntry", entryID);
1539 return;
1540 }
1541 if (ec)
1542 {
George Liu0fda0f12021-11-16 10:06:17 +08001543 BMCWEB_LOG_ERROR
1544 << "EventLogEntry (DBus) resp_handler got error "
1545 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001546 messages::internalError(asyncResp->res);
1547 return;
1548 }
1549 uint32_t* id = nullptr;
1550 std::time_t timestamp{};
1551 std::time_t updateTimestamp{};
1552 std::string* severity = nullptr;
1553 std::string* message = nullptr;
1554 std::string* filePath = nullptr;
1555 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001556
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001557 for (auto& propertyMap : resp)
1558 {
1559 if (propertyMap.first == "Id")
1560 {
1561 id = std::get_if<uint32_t>(&propertyMap.second);
1562 }
1563 else if (propertyMap.first == "Timestamp")
1564 {
1565 const uint64_t* millisTimeStamp =
1566 std::get_if<uint64_t>(&propertyMap.second);
1567 if (millisTimeStamp != nullptr)
1568 {
1569 timestamp = crow::utility::getTimestamp(
1570 *millisTimeStamp);
1571 }
1572 }
1573 else if (propertyMap.first == "UpdateTimestamp")
1574 {
1575 const uint64_t* millisTimeStamp =
1576 std::get_if<uint64_t>(&propertyMap.second);
1577 if (millisTimeStamp != nullptr)
1578 {
1579 updateTimestamp =
1580 crow::utility::getTimestamp(
1581 *millisTimeStamp);
1582 }
1583 }
1584 else if (propertyMap.first == "Severity")
1585 {
1586 severity = std::get_if<std::string>(
1587 &propertyMap.second);
1588 }
1589 else if (propertyMap.first == "Message")
1590 {
1591 message = std::get_if<std::string>(
1592 &propertyMap.second);
1593 }
1594 else if (propertyMap.first == "Resolved")
1595 {
1596 bool* resolveptr =
1597 std::get_if<bool>(&propertyMap.second);
1598 if (resolveptr == nullptr)
1599 {
1600 messages::internalError(asyncResp->res);
1601 return;
1602 }
1603 resolved = *resolveptr;
1604 }
1605 else if (propertyMap.first == "Path")
1606 {
1607 filePath = std::get_if<std::string>(
1608 &propertyMap.second);
1609 }
1610 }
1611 if (id == nullptr || message == nullptr ||
1612 severity == nullptr)
1613 {
1614 messages::internalError(asyncResp->res);
1615 return;
1616 }
1617 asyncResp->res.jsonValue["@odata.type"] =
1618 "#LogEntry.v1_8_0.LogEntry";
1619 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001620 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001621 std::to_string(*id);
1622 asyncResp->res.jsonValue["Name"] =
1623 "System Event Log Entry";
1624 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1625 asyncResp->res.jsonValue["Message"] = *message;
1626 asyncResp->res.jsonValue["Resolved"] = resolved;
1627 asyncResp->res.jsonValue["EntryType"] = "Event";
1628 asyncResp->res.jsonValue["Severity"] =
1629 translateSeverityDbusToRedfish(*severity);
1630 asyncResp->res.jsonValue["Created"] =
1631 crow::utility::getDateTime(timestamp);
1632 asyncResp->res.jsonValue["Modified"] =
1633 crow::utility::getDateTime(updateTimestamp);
1634 if (filePath != nullptr)
1635 {
1636 asyncResp->res.jsonValue["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001637 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001638 std::to_string(*id);
1639 }
1640 },
1641 "xyz.openbmc_project.Logging",
1642 "/xyz/openbmc_project/logging/entry/" + entryID,
1643 "org.freedesktop.DBus.Properties", "GetAll", "");
1644 });
1645
1646 BMCWEB_ROUTE(
1647 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001648 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001649 .methods(boost::beast::http::verb::patch)(
1650 [](const crow::Request& req,
1651 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1652 const std::string& entryId) {
1653 std::optional<bool> resolved;
1654
1655 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1656 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001657 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001658 return;
1659 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001660 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001661
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001662 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001663 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001664 if (ec)
1665 {
1666 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1667 messages::internalError(asyncResp->res);
1668 return;
1669 }
1670 },
1671 "xyz.openbmc_project.Logging",
1672 "/xyz/openbmc_project/logging/entry/" + entryId,
1673 "org.freedesktop.DBus.Properties", "Set",
1674 "xyz.openbmc_project.Logging.Entry", "Resolved",
1675 std::variant<bool>(*resolved));
1676 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001677
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001678 BMCWEB_ROUTE(
1679 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001680 .privileges(redfish::privileges::deleteLogEntry)
1681
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001682 .methods(boost::beast::http::verb::delete_)(
1683 [](const crow::Request&,
1684 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1685 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001686
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001687 {
1688 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001689
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001690 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001691
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001692 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001693
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001694 // Process response from Logging service.
1695 auto respHandler = [asyncResp, entryID](
1696 const boost::system::error_code ec) {
1697 BMCWEB_LOG_DEBUG
1698 << "EventLogEntry (DBus) doDelete callback: Done";
1699 if (ec)
1700 {
1701 if (ec.value() == EBADR)
1702 {
1703 messages::resourceNotFound(asyncResp->res,
1704 "LogEntry", entryID);
1705 return;
1706 }
1707 // TODO Handle for specific error code
George Liu0fda0f12021-11-16 10:06:17 +08001708 BMCWEB_LOG_ERROR
1709 << "EventLogEntry (DBus) doDelete respHandler got error "
1710 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001711 asyncResp->res.result(
1712 boost::beast::http::status::internal_server_error);
1713 return;
1714 }
1715
1716 asyncResp->res.result(boost::beast::http::status::ok);
1717 };
1718
1719 // Make call to Logging service to request Delete Log
1720 crow::connections::systemBus->async_method_call(
1721 respHandler, "xyz.openbmc_project.Logging",
1722 "/xyz/openbmc_project/logging/entry/" + entryID,
1723 "xyz.openbmc_project.Object.Delete", "Delete");
1724 });
1725}
1726
1727inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001728{
George Liu0fda0f12021-11-16 10:06:17 +08001729 BMCWEB_ROUTE(
1730 app,
1731 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001732 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001733 .methods(boost::beast::http::verb::get)(
1734 [](const crow::Request& req,
1735 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1736 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001737
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001738 {
George Liu647b3cd2021-07-05 12:43:56 +08001739 if (!http_helpers::isOctetAccepted(
1740 req.getHeaderValue("Accept")))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001741 {
1742 asyncResp->res.result(
1743 boost::beast::http::status::bad_request);
1744 return;
1745 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001746
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001747 std::string entryID = param;
1748 dbus::utility::escapePathForDbus(entryID);
1749
1750 crow::connections::systemBus->async_method_call(
1751 [asyncResp,
1752 entryID](const boost::system::error_code ec,
1753 const sdbusplus::message::unix_fd& unixfd) {
1754 if (ec.value() == EBADR)
1755 {
1756 messages::resourceNotFound(
1757 asyncResp->res, "EventLogAttachment", entryID);
1758 return;
1759 }
1760 if (ec)
1761 {
1762 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1763 messages::internalError(asyncResp->res);
1764 return;
1765 }
1766
1767 int fd = -1;
1768 fd = dup(unixfd);
1769 if (fd == -1)
1770 {
1771 messages::internalError(asyncResp->res);
1772 return;
1773 }
1774
1775 long long int size = lseek(fd, 0, SEEK_END);
1776 if (size == -1)
1777 {
1778 messages::internalError(asyncResp->res);
1779 return;
1780 }
1781
1782 // Arbitrary max size of 64kb
1783 constexpr int maxFileSize = 65536;
1784 if (size > maxFileSize)
1785 {
1786 BMCWEB_LOG_ERROR
1787 << "File size exceeds maximum allowed size of "
1788 << maxFileSize;
1789 messages::internalError(asyncResp->res);
1790 return;
1791 }
1792 std::vector<char> data(static_cast<size_t>(size));
1793 long long int rc = lseek(fd, 0, SEEK_SET);
1794 if (rc == -1)
1795 {
1796 messages::internalError(asyncResp->res);
1797 return;
1798 }
1799 rc = read(fd, data.data(), data.size());
1800 if ((rc == -1) || (rc != size))
1801 {
1802 messages::internalError(asyncResp->res);
1803 return;
1804 }
1805 close(fd);
1806
1807 std::string_view strData(data.data(), data.size());
1808 std::string output =
1809 crow::utility::base64encode(strData);
1810
1811 asyncResp->res.addHeader("Content-Type",
1812 "application/octet-stream");
1813 asyncResp->res.addHeader("Content-Transfer-Encoding",
1814 "Base64");
1815 asyncResp->res.body() = std::move(output);
1816 },
1817 "xyz.openbmc_project.Logging",
1818 "/xyz/openbmc_project/logging/entry/" + entryID,
1819 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1820 });
1821}
1822
Spencer Kub7028eb2021-10-26 15:27:35 +08001823constexpr const char* hostLoggerFolderPath = "/var/log/console";
1824
1825inline bool
1826 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1827 std::vector<std::filesystem::path>& hostLoggerFiles)
1828{
1829 std::error_code ec;
1830 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1831 if (ec)
1832 {
1833 BMCWEB_LOG_ERROR << ec.message();
1834 return false;
1835 }
1836 for (const std::filesystem::directory_entry& it : logPath)
1837 {
1838 std::string filename = it.path().filename();
1839 // Prefix of each log files is "log". Find the file and save the
1840 // path
1841 if (boost::starts_with(filename, "log"))
1842 {
1843 hostLoggerFiles.emplace_back(it.path());
1844 }
1845 }
1846 // As the log files rotate, they are appended with a ".#" that is higher for
1847 // the older logs. Since we start from oldest logs, sort the name in
1848 // descending order.
1849 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1850 AlphanumLess<std::string>());
1851
1852 return true;
1853}
1854
1855inline bool
1856 getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1857 uint64_t& skip, uint64_t& top,
1858 std::vector<std::string>& logEntries, size_t& logCount)
1859{
1860 GzFileReader logFile;
1861
1862 // Go though all log files and expose host logs.
1863 for (const std::filesystem::path& it : hostLoggerFiles)
1864 {
1865 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1866 {
1867 BMCWEB_LOG_ERROR << "fail to expose host logs";
1868 return false;
1869 }
1870 }
1871 // Get lastMessage from constructor by getter
1872 std::string lastMessage = logFile.getLastMessage();
1873 if (!lastMessage.empty())
1874 {
1875 logCount++;
1876 if (logCount > skip && logCount <= (skip + top))
1877 {
1878 logEntries.push_back(lastMessage);
1879 }
1880 }
1881 return true;
1882}
1883
1884inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1885 const std::string& msg,
1886 nlohmann::json& logEntryJson)
1887{
1888 // Fill in the log entry with the gathered data.
1889 logEntryJson = {
1890 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1891 {"@odata.id",
1892 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1893 logEntryID},
1894 {"Name", "Host Logger Entry"},
1895 {"Id", logEntryID},
1896 {"Message", msg},
1897 {"EntryType", "Oem"},
1898 {"Severity", "OK"},
1899 {"OemRecordFormat", "Host Logger Entry"}};
1900}
1901
1902inline void requestRoutesSystemHostLogger(App& app)
1903{
1904 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1905 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08001906 .methods(
1907 boost::beast::http::verb::
1908 get)([](const crow::Request&,
1909 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1910 asyncResp->res.jsonValue["@odata.id"] =
1911 "/redfish/v1/Systems/system/LogServices/HostLogger";
1912 asyncResp->res.jsonValue["@odata.type"] =
1913 "#LogService.v1_1_0.LogService";
1914 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1915 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1916 asyncResp->res.jsonValue["Id"] = "HostLogger";
1917 asyncResp->res.jsonValue["Entries"] = {
1918 {"@odata.id",
1919 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1920 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001921}
1922
1923inline void requestRoutesSystemHostLoggerCollection(App& app)
1924{
1925 BMCWEB_ROUTE(app,
1926 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1927 .privileges(redfish::privileges::getLogEntry)
George Liu0fda0f12021-11-16 10:06:17 +08001928 .methods(
1929 boost::beast::http::verb::
1930 get)([](const crow::Request& req,
1931 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1932 uint64_t skip = 0;
1933 uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1934 // default, allow range 1 to
1935 // 1000 entries per page.
1936 if (!getSkipParam(asyncResp, req, skip))
1937 {
1938 return;
1939 }
1940 if (!getTopParam(asyncResp, req, top))
1941 {
1942 return;
1943 }
1944 asyncResp->res.jsonValue["@odata.id"] =
1945 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1946 asyncResp->res.jsonValue["@odata.type"] =
1947 "#LogEntryCollection.LogEntryCollection";
1948 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1949 asyncResp->res.jsonValue["Description"] =
1950 "Collection of HostLogger Entries";
1951 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1952 logEntryArray = nlohmann::json::array();
1953 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001954
George Liu0fda0f12021-11-16 10:06:17 +08001955 std::vector<std::filesystem::path> hostLoggerFiles;
1956 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1957 {
1958 BMCWEB_LOG_ERROR << "fail to get host log file path";
1959 return;
1960 }
1961
1962 size_t logCount = 0;
1963 // This vector only store the entries we want to expose that
1964 // control by skip and top.
1965 std::vector<std::string> logEntries;
1966 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1967 logCount))
1968 {
1969 messages::internalError(asyncResp->res);
1970 return;
1971 }
1972 // If vector is empty, that means skip value larger than total
1973 // log count
1974 if (logEntries.size() == 0)
1975 {
1976 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1977 return;
1978 }
1979 if (logEntries.size() > 0)
1980 {
1981 for (size_t i = 0; i < logEntries.size(); i++)
Spencer Kub7028eb2021-10-26 15:27:35 +08001982 {
George Liu0fda0f12021-11-16 10:06:17 +08001983 logEntryArray.push_back({});
1984 nlohmann::json& hostLogEntry = logEntryArray.back();
1985 fillHostLoggerEntryJson(std::to_string(skip + i),
1986 logEntries[i], hostLogEntry);
Spencer Kub7028eb2021-10-26 15:27:35 +08001987 }
1988
George Liu0fda0f12021-11-16 10:06:17 +08001989 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1990 if (skip + top < logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001991 {
George Liu0fda0f12021-11-16 10:06:17 +08001992 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1993 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1994 std::to_string(skip + top);
Spencer Kub7028eb2021-10-26 15:27:35 +08001995 }
George Liu0fda0f12021-11-16 10:06:17 +08001996 }
1997 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001998}
1999
2000inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2001{
2002 BMCWEB_ROUTE(
2003 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2004 .privileges(redfish::privileges::getLogEntry)
2005 .methods(boost::beast::http::verb::get)(
2006 [](const crow::Request&,
2007 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2008 const std::string& param) {
2009 const std::string& targetID = param;
2010
2011 uint64_t idInt = 0;
2012 auto [ptr, ec] = std::from_chars(
2013 targetID.data(), targetID.data() + targetID.size(), idInt);
2014 if (ec == std::errc::invalid_argument)
2015 {
2016 messages::resourceMissingAtURI(asyncResp->res, targetID);
2017 return;
2018 }
2019 if (ec == std::errc::result_out_of_range)
2020 {
2021 messages::resourceMissingAtURI(asyncResp->res, targetID);
2022 return;
2023 }
2024
2025 std::vector<std::filesystem::path> hostLoggerFiles;
2026 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2027 {
2028 BMCWEB_LOG_ERROR << "fail to get host log file path";
2029 return;
2030 }
2031
2032 size_t logCount = 0;
2033 uint64_t top = 1;
2034 std::vector<std::string> logEntries;
2035 // We can get specific entry by skip and top. For example, if we
2036 // want to get nth entry, we can set skip = n-1 and top = 1 to
2037 // get that entry
2038 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2039 logEntries, logCount))
2040 {
2041 messages::internalError(asyncResp->res);
2042 return;
2043 }
2044
2045 if (!logEntries.empty())
2046 {
2047 fillHostLoggerEntryJson(targetID, logEntries[0],
2048 asyncResp->res.jsonValue);
2049 return;
2050 }
2051
2052 // Requested ID was not found
2053 messages::resourceMissingAtURI(asyncResp->res, targetID);
2054 });
2055}
2056
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002057inline void requestRoutesBMCLogServiceCollection(App& app)
2058{
2059 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002060 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002061 .methods(boost::beast::http::verb::get)(
2062 [](const crow::Request&,
2063 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2064 // Collections don't include the static data added by SubRoute
2065 // because it has a duplicate entry for members
2066 asyncResp->res.jsonValue["@odata.type"] =
2067 "#LogServiceCollection.LogServiceCollection";
2068 asyncResp->res.jsonValue["@odata.id"] =
2069 "/redfish/v1/Managers/bmc/LogServices";
2070 asyncResp->res.jsonValue["Name"] =
2071 "Open BMC Log Services Collection";
2072 asyncResp->res.jsonValue["Description"] =
2073 "Collection of LogServices for this Manager";
2074 nlohmann::json& logServiceArray =
2075 asyncResp->res.jsonValue["Members"];
2076 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002077#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002078 logServiceArray.push_back(
2079 {{"@odata.id",
2080 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002081#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002082#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002083 logServiceArray.push_back(
2084 {{"@odata.id",
2085 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002086#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002087 asyncResp->res.jsonValue["Members@odata.count"] =
2088 logServiceArray.size();
2089 });
2090}
Ed Tanous1da66f72018-07-27 16:13:37 -07002091
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002092inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002093{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002094 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002095 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002096 .methods(boost::beast::http::verb::get)(
2097 [](const crow::Request&,
2098 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07002099
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002100 {
2101 asyncResp->res.jsonValue["@odata.type"] =
2102 "#LogService.v1_1_0.LogService";
2103 asyncResp->res.jsonValue["@odata.id"] =
2104 "/redfish/v1/Managers/bmc/LogServices/Journal";
2105 asyncResp->res.jsonValue["Name"] =
2106 "Open BMC Journal Log Service";
2107 asyncResp->res.jsonValue["Description"] =
2108 "BMC Journal Log Service";
2109 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2110 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302111
2112 std::pair<std::string, std::string> redfishDateTimeOffset =
2113 crow::utility::getDateTimeOffsetNow();
2114 asyncResp->res.jsonValue["DateTime"] =
2115 redfishDateTimeOffset.first;
2116 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2117 redfishDateTimeOffset.second;
2118
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002119 asyncResp->res.jsonValue["Entries"] = {
2120 {"@odata.id",
2121 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
2122 });
2123}
Jason M. Billse1f26342018-07-18 12:12:00 -07002124
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002125static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2126 sd_journal* journal,
2127 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002128{
2129 // Get the Log Entry contents
2130 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002131
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002132 std::string message;
2133 std::string_view syslogID;
2134 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2135 if (ret < 0)
2136 {
2137 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2138 << strerror(-ret);
2139 }
2140 if (!syslogID.empty())
2141 {
2142 message += std::string(syslogID) + ": ";
2143 }
2144
Ed Tanous39e77502019-03-04 17:35:53 -08002145 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002146 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002147 if (ret < 0)
2148 {
2149 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2150 return 1;
2151 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002152 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002153
2154 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002155 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002156 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002157 if (ret < 0)
2158 {
2159 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002160 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002161
2162 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002163 std::string entryTimeStr;
2164 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002165 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002166 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002167 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002168
2169 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002170 bmcJournalLogEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08002171 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002172 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2173 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07002174 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002175 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002176 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07002177 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06002178 {"Severity", severity <= 2 ? "Critical"
2179 : severity <= 4 ? "Warning"
2180 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07002181 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07002182 {"Created", std::move(entryTimeStr)}};
2183 return 0;
2184}
2185
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002186inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002187{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002188 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002189 .privileges(redfish::privileges::getLogEntryCollection)
George Liu0fda0f12021-11-16 10:06:17 +08002190 .methods(
2191 boost::beast::http::verb::
2192 get)([](const crow::Request& req,
2193 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2194 static constexpr const long maxEntriesPerPage = 1000;
2195 uint64_t skip = 0;
2196 uint64_t top = maxEntriesPerPage; // Show max entries by default
2197 if (!getSkipParam(asyncResp, req, skip))
2198 {
2199 return;
2200 }
2201 if (!getTopParam(asyncResp, req, top))
2202 {
2203 return;
2204 }
2205 // Collections don't include the static data added by SubRoute
2206 // because it has a duplicate entry for members
2207 asyncResp->res.jsonValue["@odata.type"] =
2208 "#LogEntryCollection.LogEntryCollection";
2209 asyncResp->res.jsonValue["@odata.id"] =
2210 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2211 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2212 asyncResp->res.jsonValue["Description"] =
2213 "Collection of BMC Journal Entries";
2214 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2215 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002216
George Liu0fda0f12021-11-16 10:06:17 +08002217 // Go through the journal and use the timestamp to create a
2218 // unique ID for each entry
2219 sd_journal* journalTmp = nullptr;
2220 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2221 if (ret < 0)
2222 {
2223 BMCWEB_LOG_ERROR << "failed to open journal: "
2224 << strerror(-ret);
2225 messages::internalError(asyncResp->res);
2226 return;
2227 }
2228 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2229 journalTmp, sd_journal_close);
2230 journalTmp = nullptr;
2231 uint64_t entryCount = 0;
2232 // Reset the unique ID on the first entry
2233 bool firstEntry = true;
2234 SD_JOURNAL_FOREACH(journal.get())
2235 {
2236 entryCount++;
2237 // Handle paging using skip (number of entries to skip from
2238 // the start) and top (number of entries to display)
2239 if (entryCount <= skip || entryCount > skip + top)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002240 {
George Liu0fda0f12021-11-16 10:06:17 +08002241 continue;
2242 }
2243
2244 std::string idStr;
2245 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2246 {
2247 continue;
2248 }
2249
2250 if (firstEntry)
2251 {
2252 firstEntry = false;
2253 }
2254
2255 logEntryArray.push_back({});
2256 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2257 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2258 bmcJournalLogEntry) != 0)
2259 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002260 messages::internalError(asyncResp->res);
2261 return;
2262 }
George Liu0fda0f12021-11-16 10:06:17 +08002263 }
2264 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2265 if (skip + top < entryCount)
2266 {
2267 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2268 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2269 std::to_string(skip + top);
2270 }
2271 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002272}
Jason M. Billse1f26342018-07-18 12:12:00 -07002273
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002274inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002275{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002276 BMCWEB_ROUTE(app,
2277 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002278 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002279 .methods(boost::beast::http::verb::get)(
2280 [](const crow::Request&,
2281 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2282 const std::string& entryID) {
2283 // Convert the unique ID back to a timestamp to find the entry
2284 uint64_t ts = 0;
2285 uint64_t index = 0;
2286 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2287 {
2288 return;
2289 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002290
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002291 sd_journal* journalTmp = nullptr;
2292 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2293 if (ret < 0)
2294 {
2295 BMCWEB_LOG_ERROR << "failed to open journal: "
2296 << strerror(-ret);
2297 messages::internalError(asyncResp->res);
2298 return;
2299 }
2300 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2301 journal(journalTmp, sd_journal_close);
2302 journalTmp = nullptr;
2303 // Go to the timestamp in the log and move to the entry at the
2304 // index tracking the unique ID
2305 std::string idStr;
2306 bool firstEntry = true;
2307 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2308 if (ret < 0)
2309 {
2310 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2311 << strerror(-ret);
2312 messages::internalError(asyncResp->res);
2313 return;
2314 }
2315 for (uint64_t i = 0; i <= index; i++)
2316 {
2317 sd_journal_next(journal.get());
2318 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2319 {
2320 messages::internalError(asyncResp->res);
2321 return;
2322 }
2323 if (firstEntry)
2324 {
2325 firstEntry = false;
2326 }
2327 }
2328 // Confirm that the entry ID matches what was requested
2329 if (idStr != entryID)
2330 {
2331 messages::resourceMissingAtURI(asyncResp->res, entryID);
2332 return;
2333 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002334
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002335 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2336 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002337 {
2338 messages::internalError(asyncResp->res);
2339 return;
2340 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002341 });
2342}
2343
2344inline void requestRoutesBMCDumpService(App& app)
2345{
2346 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002347 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08002348 .methods(
2349 boost::beast::http::verb::
2350 get)([](const crow::Request&,
2351 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2352 asyncResp->res.jsonValue["@odata.id"] =
2353 "/redfish/v1/Managers/bmc/LogServices/Dump";
2354 asyncResp->res.jsonValue["@odata.type"] =
2355 "#LogService.v1_2_0.LogService";
2356 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2357 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2358 asyncResp->res.jsonValue["Id"] = "Dump";
2359 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302360
George Liu0fda0f12021-11-16 10:06:17 +08002361 std::pair<std::string, std::string> redfishDateTimeOffset =
2362 crow::utility::getDateTimeOffsetNow();
2363 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2364 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2365 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302366
George Liu0fda0f12021-11-16 10:06:17 +08002367 asyncResp->res.jsonValue["Entries"] = {
2368 {"@odata.id",
2369 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2370 asyncResp->res.jsonValue["Actions"] = {
2371 {"#LogService.ClearLog",
2372 {{"target",
2373 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2374 {"#LogService.CollectDiagnosticData",
2375 {{"target",
2376 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
2377 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002378}
2379
2380inline void requestRoutesBMCDumpEntryCollection(App& app)
2381{
2382
2383 /**
2384 * Functions triggers appropriate requests on DBus
2385 */
2386 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002387 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002388 .methods(boost::beast::http::verb::get)(
2389 [](const crow::Request&,
2390 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2391 asyncResp->res.jsonValue["@odata.type"] =
2392 "#LogEntryCollection.LogEntryCollection";
2393 asyncResp->res.jsonValue["@odata.id"] =
2394 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2395 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2396 asyncResp->res.jsonValue["Description"] =
2397 "Collection of BMC Dump Entries";
2398
2399 getDumpEntryCollection(asyncResp, "BMC");
2400 });
2401}
2402
2403inline void requestRoutesBMCDumpEntry(App& app)
2404{
2405 BMCWEB_ROUTE(app,
2406 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002407 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002408 .methods(boost::beast::http::verb::get)(
2409 [](const crow::Request&,
2410 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2411 const std::string& param) {
2412 getDumpEntryById(asyncResp, param, "BMC");
2413 });
2414 BMCWEB_ROUTE(app,
2415 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002416 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002417 .methods(boost::beast::http::verb::delete_)(
2418 [](const crow::Request&,
2419 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2420 const std::string& param) {
2421 deleteDumpEntry(asyncResp, param, "bmc");
2422 });
2423}
2424
2425inline void requestRoutesBMCDumpCreate(App& app)
2426{
2427
George Liu0fda0f12021-11-16 10:06:17 +08002428 BMCWEB_ROUTE(
2429 app,
2430 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002431 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002432 .methods(boost::beast::http::verb::post)(
2433 [](const crow::Request& req,
2434 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2435 createDump(asyncResp, req, "BMC");
2436 });
2437}
2438
2439inline void requestRoutesBMCDumpClear(App& app)
2440{
George Liu0fda0f12021-11-16 10:06:17 +08002441 BMCWEB_ROUTE(
2442 app,
2443 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002444 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002445 .methods(boost::beast::http::verb::post)(
2446 [](const crow::Request&,
2447 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2448 clearDump(asyncResp, "BMC");
2449 });
2450}
2451
2452inline void requestRoutesSystemDumpService(App& app)
2453{
2454 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002455 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002456 .methods(boost::beast::http::verb::get)(
2457 [](const crow::Request&,
2458 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2459
2460 {
2461 asyncResp->res.jsonValue["@odata.id"] =
2462 "/redfish/v1/Systems/system/LogServices/Dump";
2463 asyncResp->res.jsonValue["@odata.type"] =
2464 "#LogService.v1_2_0.LogService";
2465 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2466 asyncResp->res.jsonValue["Description"] =
2467 "System Dump LogService";
2468 asyncResp->res.jsonValue["Id"] = "Dump";
2469 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302470
2471 std::pair<std::string, std::string> redfishDateTimeOffset =
2472 crow::utility::getDateTimeOffsetNow();
2473 asyncResp->res.jsonValue["DateTime"] =
2474 redfishDateTimeOffset.first;
2475 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2476 redfishDateTimeOffset.second;
2477
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002478 asyncResp->res.jsonValue["Entries"] = {
2479 {"@odata.id",
2480 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2481 asyncResp->res.jsonValue["Actions"] = {
2482 {"#LogService.ClearLog",
2483 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002484 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002485 {"#LogService.CollectDiagnosticData",
2486 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002487 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002488 });
2489}
2490
2491inline void requestRoutesSystemDumpEntryCollection(App& app)
2492{
2493
2494 /**
2495 * Functions triggers appropriate requests on DBus
2496 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002497 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002498 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002499 .methods(boost::beast::http::verb::get)(
2500 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002501 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002502 asyncResp->res.jsonValue["@odata.type"] =
2503 "#LogEntryCollection.LogEntryCollection";
2504 asyncResp->res.jsonValue["@odata.id"] =
2505 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2506 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2507 asyncResp->res.jsonValue["Description"] =
2508 "Collection of System Dump Entries";
2509
2510 getDumpEntryCollection(asyncResp, "System");
2511 });
2512}
2513
2514inline void requestRoutesSystemDumpEntry(App& app)
2515{
2516 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002517 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002518 .privileges(redfish::privileges::getLogEntry)
2519
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002520 .methods(boost::beast::http::verb::get)(
2521 [](const crow::Request&,
2522 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2523 const std::string& param) {
2524 getDumpEntryById(asyncResp, param, "System");
2525 });
2526
2527 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002528 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002529 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002530 .methods(boost::beast::http::verb::delete_)(
2531 [](const crow::Request&,
2532 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2533 const std::string& param) {
2534 deleteDumpEntry(asyncResp, param, "system");
2535 });
2536}
2537
2538inline void requestRoutesSystemDumpCreate(App& app)
2539{
George Liu0fda0f12021-11-16 10:06:17 +08002540 BMCWEB_ROUTE(
2541 app,
2542 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002543 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002544 .methods(boost::beast::http::verb::post)(
2545 [](const crow::Request& req,
2546 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2547
2548 { createDump(asyncResp, req, "System"); });
2549}
2550
2551inline void requestRoutesSystemDumpClear(App& app)
2552{
George Liu0fda0f12021-11-16 10:06:17 +08002553 BMCWEB_ROUTE(
2554 app,
2555 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002556 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002557 .methods(boost::beast::http::verb::post)(
2558 [](const crow::Request&,
2559 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2560
2561 { clearDump(asyncResp, "System"); });
2562}
2563
2564inline void requestRoutesCrashdumpService(App& app)
2565{
2566 // Note: Deviated from redfish privilege registry for GET & HEAD
2567 // method for security reasons.
2568 /**
2569 * Functions triggers appropriate requests on DBus
2570 */
2571 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002572 // This is incorrect, should be:
2573 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002574 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002575 .methods(
2576 boost::beast::http::verb::
2577 get)([](const crow::Request&,
2578 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2579 // Copy over the static data to include the entries added by
2580 // SubRoute
2581 asyncResp->res.jsonValue["@odata.id"] =
2582 "/redfish/v1/Systems/system/LogServices/Crashdump";
2583 asyncResp->res.jsonValue["@odata.type"] =
2584 "#LogService.v1_2_0.LogService";
2585 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2586 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2587 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2588 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2589 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302590
2591 std::pair<std::string, std::string> redfishDateTimeOffset =
2592 crow::utility::getDateTimeOffsetNow();
2593 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2594 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2595 redfishDateTimeOffset.second;
2596
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002597 asyncResp->res.jsonValue["Entries"] = {
2598 {"@odata.id",
2599 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2600 asyncResp->res.jsonValue["Actions"] = {
2601 {"#LogService.ClearLog",
George Liu0fda0f12021-11-16 10:06:17 +08002602 {{"target",
2603 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002604 {"#LogService.CollectDiagnosticData",
George Liu0fda0f12021-11-16 10:06:17 +08002605 {{"target",
2606 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002607 });
2608}
2609
2610void inline requestRoutesCrashdumpClear(App& app)
2611{
George Liu0fda0f12021-11-16 10:06:17 +08002612 BMCWEB_ROUTE(
2613 app,
2614 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002615 // This is incorrect, should be:
2616 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002617 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002618 .methods(boost::beast::http::verb::post)(
2619 [](const crow::Request&,
2620 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2621 crow::connections::systemBus->async_method_call(
2622 [asyncResp](const boost::system::error_code ec,
2623 const std::string&) {
2624 if (ec)
2625 {
2626 messages::internalError(asyncResp->res);
2627 return;
2628 }
2629 messages::success(asyncResp->res);
2630 },
2631 crashdumpObject, crashdumpPath, deleteAllInterface,
2632 "DeleteAll");
2633 });
2634}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002635
zhanghch058d1b46d2021-04-01 11:18:24 +08002636static void
2637 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2638 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002639{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002640 auto getStoredLogCallback =
2641 [asyncResp, logID, &logEntryJson](
2642 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002643 const std::vector<std::pair<std::string, VariantType>>& params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002644 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002645 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002646 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2647 if (ec.value() ==
2648 boost::system::linux_error::bad_request_descriptor)
2649 {
2650 messages::resourceNotFound(asyncResp->res, "LogEntry",
2651 logID);
2652 }
2653 else
2654 {
2655 messages::internalError(asyncResp->res);
2656 }
2657 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002658 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002659
Johnathan Mantey043a0532020-03-10 17:15:28 -07002660 std::string timestamp{};
2661 std::string filename{};
2662 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002663 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002664
2665 if (filename.empty() || timestamp.empty())
2666 {
2667 messages::resourceMissingAtURI(asyncResp->res, logID);
2668 return;
2669 }
2670
2671 std::string crashdumpURI =
2672 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2673 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002674 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002675 {"@odata.id", "/redfish/v1/Systems/system/"
2676 "LogServices/Crashdump/Entries/" +
2677 logID},
2678 {"Name", "CPU Crashdump"},
2679 {"Id", logID},
2680 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002681 {"AdditionalDataURI", std::move(crashdumpURI)},
2682 {"DiagnosticDataType", "OEM"},
2683 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002684 {"Created", std::move(timestamp)}};
2685 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002686 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002687 std::move(getStoredLogCallback), crashdumpObject,
2688 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002689 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002690}
2691
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002692inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002693{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002694 // Note: Deviated from redfish privilege registry for GET & HEAD
2695 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002696 /**
2697 * Functions triggers appropriate requests on DBus
2698 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002699 BMCWEB_ROUTE(app,
2700 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002701 // This is incorrect, should be.
2702 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002703 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002704 .methods(
2705 boost::beast::http::verb::
2706 get)([](const crow::Request&,
2707 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2708 // Collections don't include the static data added by SubRoute
2709 // because it has a duplicate entry for members
2710 auto getLogEntriesCallback = [asyncResp](
2711 const boost::system::error_code ec,
2712 const std::vector<std::string>&
2713 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002714 if (ec)
2715 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002716 if (ec.value() !=
2717 boost::system::errc::no_such_file_or_directory)
2718 {
2719 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2720 << ec.message();
2721 messages::internalError(asyncResp->res);
2722 return;
2723 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002724 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002725 asyncResp->res.jsonValue["@odata.type"] =
2726 "#LogEntryCollection.LogEntryCollection";
2727 asyncResp->res.jsonValue["@odata.id"] =
2728 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2729 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2730 asyncResp->res.jsonValue["Description"] =
2731 "Collection of Crashdump Entries";
2732 nlohmann::json& logEntryArray =
2733 asyncResp->res.jsonValue["Members"];
2734 logEntryArray = nlohmann::json::array();
2735 std::vector<std::string> logIDs;
2736 // Get the list of log entries and build up an empty array big
2737 // enough to hold them
2738 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002739 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002740 // Get the log ID
2741 std::size_t lastPos = objpath.rfind('/');
2742 if (lastPos == std::string::npos)
2743 {
2744 continue;
2745 }
2746 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002747
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002748 // Add a space for the log entry to the array
2749 logEntryArray.push_back({});
2750 }
2751 // Now go through and set up async calls to fill in the entries
2752 size_t index = 0;
2753 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002754 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002755 // Add the log entry to the array
2756 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002757 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002758 asyncResp->res.jsonValue["Members@odata.count"] =
2759 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002760 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002761 crow::connections::systemBus->async_method_call(
2762 std::move(getLogEntriesCallback),
2763 "xyz.openbmc_project.ObjectMapper",
2764 "/xyz/openbmc_project/object_mapper",
2765 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2766 std::array<const char*, 1>{crashdumpInterface});
2767 });
2768}
Ed Tanous1da66f72018-07-27 16:13:37 -07002769
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002770inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002771{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002772 // Note: Deviated from redfish privilege registry for GET & HEAD
2773 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002774
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002775 BMCWEB_ROUTE(
2776 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002777 // this is incorrect, should be
2778 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002779 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002780 .methods(boost::beast::http::verb::get)(
2781 [](const crow::Request&,
2782 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2783 const std::string& param) {
2784 const std::string& logID = param;
2785 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2786 });
2787}
Ed Tanous1da66f72018-07-27 16:13:37 -07002788
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002789inline void requestRoutesCrashdumpFile(App& app)
2790{
2791 // Note: Deviated from redfish privilege registry for GET & HEAD
2792 // method for security reasons.
2793 BMCWEB_ROUTE(
2794 app,
2795 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002796 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002797 .methods(boost::beast::http::verb::get)(
2798 [](const crow::Request&,
2799 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2800 const std::string& logID, const std::string& fileName) {
2801 auto getStoredLogCallback =
2802 [asyncResp, logID, fileName](
2803 const boost::system::error_code ec,
2804 const std::vector<std::pair<std::string, VariantType>>&
2805 resp) {
2806 if (ec)
2807 {
2808 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2809 << ec.message();
2810 messages::internalError(asyncResp->res);
2811 return;
2812 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002813
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002814 std::string dbusFilename{};
2815 std::string dbusTimestamp{};
2816 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002817
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002818 parseCrashdumpParameters(resp, dbusFilename,
2819 dbusTimestamp, dbusFilepath);
2820
2821 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2822 dbusFilepath.empty())
2823 {
2824 messages::resourceMissingAtURI(asyncResp->res,
2825 fileName);
2826 return;
2827 }
2828
2829 // Verify the file name parameter is correct
2830 if (fileName != dbusFilename)
2831 {
2832 messages::resourceMissingAtURI(asyncResp->res,
2833 fileName);
2834 return;
2835 }
2836
2837 if (!std::filesystem::exists(dbusFilepath))
2838 {
2839 messages::resourceMissingAtURI(asyncResp->res,
2840 fileName);
2841 return;
2842 }
2843 std::ifstream ifs(dbusFilepath, std::ios::in |
2844 std::ios::binary |
2845 std::ios::ate);
2846 std::ifstream::pos_type fileSize = ifs.tellg();
2847 if (fileSize < 0)
2848 {
2849 messages::generalError(asyncResp->res);
2850 return;
2851 }
2852 ifs.seekg(0, std::ios::beg);
2853
2854 auto crashData = std::make_unique<char[]>(
2855 static_cast<unsigned int>(fileSize));
2856
2857 ifs.read(crashData.get(), static_cast<int>(fileSize));
2858
2859 // The cast to std::string is intentional in order to
2860 // use the assign() that applies move mechanics
2861 asyncResp->res.body().assign(
2862 static_cast<std::string>(crashData.get()));
2863
2864 // Configure this to be a file download when accessed
2865 // from a browser
2866 asyncResp->res.addHeader("Content-Disposition",
2867 "attachment");
2868 };
2869 crow::connections::systemBus->async_method_call(
2870 std::move(getStoredLogCallback), crashdumpObject,
2871 crashdumpPath + std::string("/") + logID,
2872 "org.freedesktop.DBus.Properties", "GetAll",
2873 crashdumpInterface);
2874 });
2875}
2876
2877inline void requestRoutesCrashdumpCollect(App& app)
2878{
2879 // Note: Deviated from redfish privilege registry for GET & HEAD
2880 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08002881 BMCWEB_ROUTE(
2882 app,
2883 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002884 // The below is incorrect; Should be ConfigureManager
2885 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002886 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002887 .methods(
2888 boost::beast::http::verb::
2889 post)([](const crow::Request& req,
2890 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2891 std::string diagnosticDataType;
2892 std::string oemDiagnosticDataType;
2893 if (!redfish::json_util::readJson(
2894 req, asyncResp->res, "DiagnosticDataType",
2895 diagnosticDataType, "OEMDiagnosticDataType",
2896 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002897 {
James Feist46229572020-02-19 15:11:58 -08002898 return;
2899 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002900
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002901 if (diagnosticDataType != "OEM")
2902 {
2903 BMCWEB_LOG_ERROR
2904 << "Only OEM DiagnosticDataType supported for Crashdump";
2905 messages::actionParameterValueFormatError(
2906 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2907 "CollectDiagnosticData");
2908 return;
2909 }
2910
Ed Tanous98be3e32021-09-16 15:05:36 -07002911 auto collectCrashdumpCallback = [asyncResp,
2912 payload(task::Payload(req))](
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002913 const boost::system::error_code
2914 ec,
Ed Tanous98be3e32021-09-16 15:05:36 -07002915 const std::string&) mutable {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002916 if (ec)
2917 {
2918 if (ec.value() ==
2919 boost::system::errc::operation_not_supported)
2920 {
2921 messages::resourceInStandby(asyncResp->res);
2922 }
2923 else if (ec.value() ==
2924 boost::system::errc::device_or_resource_busy)
2925 {
2926 messages::serviceTemporarilyUnavailable(asyncResp->res,
2927 "60");
2928 }
2929 else
2930 {
2931 messages::internalError(asyncResp->res);
2932 }
2933 return;
2934 }
George Liu0fda0f12021-11-16 10:06:17 +08002935 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
2936 [](boost::system::error_code err,
2937 sdbusplus::message::message&,
2938 const std::shared_ptr<task::TaskData>& taskData) {
2939 if (!err)
2940 {
2941 taskData->messages.emplace_back(
2942 messages::taskCompletedOK(
2943 std::to_string(taskData->index)));
2944 taskData->state = "Completed";
2945 }
2946 return task::completed;
2947 },
2948 "type='signal',interface='org.freedesktop.DBus."
2949 "Properties',"
2950 "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002951 task->startTimer(std::chrono::minutes(5));
2952 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -07002953 task->payload.emplace(std::move(payload));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002954 };
2955
2956 if (oemDiagnosticDataType == "OnDemand")
2957 {
2958 crow::connections::systemBus->async_method_call(
2959 std::move(collectCrashdumpCallback), crashdumpObject,
2960 crashdumpPath, crashdumpOnDemandInterface,
2961 "GenerateOnDemandLog");
2962 }
2963 else if (oemDiagnosticDataType == "Telemetry")
2964 {
2965 crow::connections::systemBus->async_method_call(
2966 std::move(collectCrashdumpCallback), crashdumpObject,
2967 crashdumpPath, crashdumpTelemetryInterface,
2968 "GenerateTelemetryLog");
2969 }
2970 else
2971 {
2972 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2973 << oemDiagnosticDataType;
2974 messages::actionParameterValueFormatError(
2975 asyncResp->res, oemDiagnosticDataType,
2976 "OEMDiagnosticDataType", "CollectDiagnosticData");
2977 return;
2978 }
2979 });
2980}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002981
Andrew Geisslercb92c032018-08-17 07:56:14 -07002982/**
2983 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2984 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002985inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002986{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002987 /**
2988 * Function handles POST method request.
2989 * The Clear Log actions does not require any parameter.The action deletes
2990 * all entries found in the Entries collection for this Log Service.
2991 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002992
George Liu0fda0f12021-11-16 10:06:17 +08002993 BMCWEB_ROUTE(
2994 app,
2995 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002996 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002997 .methods(boost::beast::http::verb::post)(
2998 [](const crow::Request&,
2999 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3000 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003001
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003002 // Process response from Logging service.
3003 auto respHandler = [asyncResp](
3004 const boost::system::error_code ec) {
3005 BMCWEB_LOG_DEBUG
3006 << "doClearLog resp_handler callback: Done";
3007 if (ec)
3008 {
3009 // TODO Handle for specific error code
3010 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
3011 << ec;
3012 asyncResp->res.result(
3013 boost::beast::http::status::internal_server_error);
3014 return;
3015 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003016
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003017 asyncResp->res.result(
3018 boost::beast::http::status::no_content);
3019 };
3020
3021 // Make call to Logging service to request Clear Log
3022 crow::connections::systemBus->async_method_call(
3023 respHandler, "xyz.openbmc_project.Logging",
3024 "/xyz/openbmc_project/logging",
3025 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3026 });
3027}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003028
3029/****************************************************
3030 * Redfish PostCode interfaces
3031 * using DBUS interface: getPostCodesTS
3032 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003033inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003034{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003035 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003036 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08003037 .methods(
3038 boost::beast::http::verb::
3039 get)([](const crow::Request&,
3040 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3041 asyncResp->res.jsonValue = {
3042 {"@odata.id",
3043 "/redfish/v1/Systems/system/LogServices/PostCodes"},
3044 {"@odata.type", "#LogService.v1_1_0.LogService"},
3045 {"Name", "POST Code Log Service"},
3046 {"Description", "POST Code Log Service"},
3047 {"Id", "BIOS POST Code Log"},
3048 {"OverWritePolicy", "WrapsWhenFull"},
3049 {"Entries",
3050 {{"@odata.id",
3051 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05303052
George Liu0fda0f12021-11-16 10:06:17 +08003053 std::pair<std::string, std::string> redfishDateTimeOffset =
3054 crow::utility::getDateTimeOffsetNow();
3055 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3056 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3057 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303058
George Liu0fda0f12021-11-16 10:06:17 +08003059 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3060 {"target",
3061 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
3062 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003063}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003064
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003065inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003066{
George Liu0fda0f12021-11-16 10:06:17 +08003067 BMCWEB_ROUTE(
3068 app,
3069 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003070 // The following privilege is incorrect; It should be ConfigureManager
3071 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003072 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003073 .methods(boost::beast::http::verb::post)(
3074 [](const crow::Request&,
3075 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3076 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003077
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003078 // Make call to post-code service to request clear all
3079 crow::connections::systemBus->async_method_call(
3080 [asyncResp](const boost::system::error_code ec) {
3081 if (ec)
3082 {
3083 // TODO Handle for specific error code
3084 BMCWEB_LOG_ERROR
3085 << "doClearPostCodes resp_handler got error "
3086 << ec;
3087 asyncResp->res.result(boost::beast::http::status::
3088 internal_server_error);
3089 messages::internalError(asyncResp->res);
3090 return;
3091 }
3092 },
3093 "xyz.openbmc_project.State.Boot.PostCode0",
3094 "/xyz/openbmc_project/State/Boot/PostCode0",
3095 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3096 });
3097}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003098
3099static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003100 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303101 const boost::container::flat_map<
3102 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003103 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3104 const uint64_t skip = 0, const uint64_t top = 0)
3105{
3106 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003107 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05303108 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003109
3110 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003111 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003112
3113 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303114 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3115 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003116 {
3117 currentCodeIndex++;
3118 std::string postcodeEntryID =
3119 "B" + std::to_string(bootIndex) + "-" +
3120 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3121
3122 uint64_t usecSinceEpoch = code.first;
3123 uint64_t usTimeOffset = 0;
3124
3125 if (1 == currentCodeIndex)
3126 { // already incremented
3127 firstCodeTimeUs = code.first;
3128 }
3129 else
3130 {
3131 usTimeOffset = code.first - firstCodeTimeUs;
3132 }
3133
3134 // skip if no specific codeIndex is specified and currentCodeIndex does
3135 // not fall between top and skip
3136 if ((codeIndex == 0) &&
3137 (currentCodeIndex <= skip || currentCodeIndex > top))
3138 {
3139 continue;
3140 }
3141
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003142 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003143 // currentIndex
3144 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3145 {
3146 // This is done for simplicity. 1st entry is needed to calculate
3147 // time offset. To improve efficiency, one can get to the entry
3148 // directly (possibly with flatmap's nth method)
3149 continue;
3150 }
3151
3152 // currentCodeIndex is within top and skip or equal to specified code
3153 // index
3154
3155 // Get the Created time from the timestamp
3156 std::string entryTimeStr;
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -05003157 entryTimeStr = crow::utility::getDateTime(
3158 static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003159
3160 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3161 std::ostringstream hexCode;
3162 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303163 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003164 std::ostringstream timeOffsetStr;
3165 // Set Fixed -Point Notation
3166 timeOffsetStr << std::fixed;
3167 // Set precision to 4 digits
3168 timeOffsetStr << std::setprecision(4);
3169 // Add double to stream
3170 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3171 std::vector<std::string> messageArgs = {
3172 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3173
3174 // Get MessageArgs template from message registry
3175 std::string msg;
3176 if (message != nullptr)
3177 {
3178 msg = message->message;
3179
3180 // fill in this post code value
3181 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003182 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003183 {
3184 std::string argStr = "%" + std::to_string(++i);
3185 size_t argPos = msg.find(argStr);
3186 if (argPos != std::string::npos)
3187 {
3188 msg.replace(argPos, argStr.length(), messageArg);
3189 }
3190 }
3191 }
3192
Tim Leed4342a92020-04-27 11:47:58 +08003193 // Get Severity template from message registry
3194 std::string severity;
3195 if (message != nullptr)
3196 {
3197 severity = message->severity;
3198 }
3199
ZhikuiRena3316fc2020-01-29 14:58:08 -08003200 // add to AsyncResp
3201 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003202 nlohmann::json& bmcLogEntry = logEntryArray.back();
George Liu0fda0f12021-11-16 10:06:17 +08003203 bmcLogEntry = {
3204 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
3205 {"@odata.id",
3206 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3207 postcodeEntryID},
3208 {"Name", "POST Code Log Entry"},
3209 {"Id", postcodeEntryID},
3210 {"Message", std::move(msg)},
3211 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3212 {"MessageArgs", std::move(messageArgs)},
3213 {"EntryType", "Event"},
3214 {"Severity", std::move(severity)},
3215 {"Created", entryTimeStr}};
George Liu647b3cd2021-07-05 12:43:56 +08003216 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3217 {
3218 bmcLogEntry["AdditionalDataURI"] =
3219 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3220 postcodeEntryID + "/attachment";
3221 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003222 }
3223}
3224
zhanghch058d1b46d2021-04-01 11:18:24 +08003225static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003226 const uint16_t bootIndex,
3227 const uint64_t codeIndex)
3228{
3229 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303230 [aResp, bootIndex,
3231 codeIndex](const boost::system::error_code ec,
3232 const boost::container::flat_map<
3233 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3234 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003235 if (ec)
3236 {
3237 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3238 messages::internalError(aResp->res);
3239 return;
3240 }
3241
3242 // skip the empty postcode boots
3243 if (postcode.empty())
3244 {
3245 return;
3246 }
3247
3248 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3249
3250 aResp->res.jsonValue["Members@odata.count"] =
3251 aResp->res.jsonValue["Members"].size();
3252 },
Jonathan Doman15124762021-01-07 17:54:17 -08003253 "xyz.openbmc_project.State.Boot.PostCode0",
3254 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003255 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3256 bootIndex);
3257}
3258
zhanghch058d1b46d2021-04-01 11:18:24 +08003259static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003260 const uint16_t bootIndex,
3261 const uint16_t bootCount,
3262 const uint64_t entryCount, const uint64_t skip,
3263 const uint64_t top)
3264{
3265 crow::connections::systemBus->async_method_call(
3266 [aResp, bootIndex, bootCount, entryCount, skip,
3267 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303268 const boost::container::flat_map<
3269 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3270 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003271 if (ec)
3272 {
3273 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3274 messages::internalError(aResp->res);
3275 return;
3276 }
3277
3278 uint64_t endCount = entryCount;
3279 if (!postcode.empty())
3280 {
3281 endCount = entryCount + postcode.size();
3282
3283 if ((skip < endCount) && ((top + skip) > entryCount))
3284 {
3285 uint64_t thisBootSkip =
3286 std::max(skip, entryCount) - entryCount;
3287 uint64_t thisBootTop =
3288 std::min(top + skip, endCount) - entryCount;
3289
3290 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3291 thisBootSkip, thisBootTop);
3292 }
3293 aResp->res.jsonValue["Members@odata.count"] = endCount;
3294 }
3295
3296 // continue to previous bootIndex
3297 if (bootIndex < bootCount)
3298 {
3299 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3300 bootCount, endCount, skip, top);
3301 }
3302 else
3303 {
3304 aResp->res.jsonValue["Members@odata.nextLink"] =
George Liu0fda0f12021-11-16 10:06:17 +08003305 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
ZhikuiRena3316fc2020-01-29 14:58:08 -08003306 std::to_string(skip + top);
3307 }
3308 },
Jonathan Doman15124762021-01-07 17:54:17 -08003309 "xyz.openbmc_project.State.Boot.PostCode0",
3310 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003311 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3312 bootIndex);
3313}
3314
zhanghch058d1b46d2021-04-01 11:18:24 +08003315static void
3316 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3317 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003318{
3319 uint64_t entryCount = 0;
3320 crow::connections::systemBus->async_method_call(
3321 [aResp, entryCount, skip,
3322 top](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003323 const std::variant<uint16_t>& bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003324 if (ec)
3325 {
3326 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3327 messages::internalError(aResp->res);
3328 return;
3329 }
3330 auto pVal = std::get_if<uint16_t>(&bootCount);
3331 if (pVal)
3332 {
3333 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3334 }
3335 else
3336 {
3337 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3338 }
3339 },
Jonathan Doman15124762021-01-07 17:54:17 -08003340 "xyz.openbmc_project.State.Boot.PostCode0",
3341 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003342 "org.freedesktop.DBus.Properties", "Get",
3343 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3344}
3345
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003346inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003347{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003348 BMCWEB_ROUTE(app,
3349 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003350 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003351 .methods(boost::beast::http::verb::get)(
3352 [](const crow::Request& req,
3353 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3354 asyncResp->res.jsonValue["@odata.type"] =
3355 "#LogEntryCollection.LogEntryCollection";
3356 asyncResp->res.jsonValue["@odata.id"] =
3357 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3358 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3359 asyncResp->res.jsonValue["Description"] =
3360 "Collection of POST Code Log Entries";
3361 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3362 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003363
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003364 uint64_t skip = 0;
3365 uint64_t top = maxEntriesPerPage; // Show max entries by default
3366 if (!getSkipParam(asyncResp, req, skip))
3367 {
3368 return;
3369 }
3370 if (!getTopParam(asyncResp, req, top))
3371 {
3372 return;
3373 }
3374 getCurrentBootNumber(asyncResp, skip, top);
3375 });
3376}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003377
George Liu647b3cd2021-07-05 12:43:56 +08003378/**
3379 * @brief Parse post code ID and get the current value and index value
3380 * eg: postCodeID=B1-2, currentValue=1, index=2
3381 *
3382 * @param[in] postCodeID Post Code ID
3383 * @param[out] currentValue Current value
3384 * @param[out] index Index value
3385 *
3386 * @return bool true if the parsing is successful, false the parsing fails
3387 */
3388inline static bool parsePostCode(const std::string& postCodeID,
3389 uint64_t& currentValue, uint16_t& index)
3390{
3391 std::vector<std::string> split;
3392 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3393 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3394 {
3395 return false;
3396 }
3397
3398 const char* start = split[0].data() + 1;
3399 const char* end = split[0].data() + split[0].size();
3400 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3401
3402 if (ptrIndex != end || ecIndex != std::errc())
3403 {
3404 return false;
3405 }
3406
3407 start = split[1].data();
3408 end = split[1].data() + split[1].size();
3409 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3410 if (ptrValue != end || ecValue != std::errc())
3411 {
3412 return false;
3413 }
3414
3415 return true;
3416}
3417
3418inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3419{
George Liu0fda0f12021-11-16 10:06:17 +08003420 BMCWEB_ROUTE(
3421 app,
3422 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003423 .privileges(redfish::privileges::getLogEntry)
3424 .methods(boost::beast::http::verb::get)(
3425 [](const crow::Request& req,
3426 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3427 const std::string& postCodeID) {
3428 if (!http_helpers::isOctetAccepted(
3429 req.getHeaderValue("Accept")))
3430 {
3431 asyncResp->res.result(
3432 boost::beast::http::status::bad_request);
3433 return;
3434 }
3435
3436 uint64_t currentValue = 0;
3437 uint16_t index = 0;
3438 if (!parsePostCode(postCodeID, currentValue, index))
3439 {
3440 messages::resourceNotFound(asyncResp->res, "LogEntry",
3441 postCodeID);
3442 return;
3443 }
3444
3445 crow::connections::systemBus->async_method_call(
3446 [asyncResp, postCodeID, currentValue](
3447 const boost::system::error_code ec,
3448 const std::vector<std::tuple<
3449 uint64_t, std::vector<uint8_t>>>& postcodes) {
3450 if (ec.value() == EBADR)
3451 {
3452 messages::resourceNotFound(asyncResp->res,
3453 "LogEntry", postCodeID);
3454 return;
3455 }
3456 if (ec)
3457 {
3458 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3459 messages::internalError(asyncResp->res);
3460 return;
3461 }
3462
3463 size_t value = static_cast<size_t>(currentValue) - 1;
3464 if (value == std::string::npos ||
3465 postcodes.size() < currentValue)
3466 {
3467 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3468 messages::resourceNotFound(asyncResp->res,
3469 "LogEntry", postCodeID);
3470 return;
3471 }
3472
3473 auto& [tID, code] = postcodes[value];
3474 if (code.empty())
3475 {
3476 BMCWEB_LOG_INFO << "No found post code data";
3477 messages::resourceNotFound(asyncResp->res,
3478 "LogEntry", postCodeID);
3479 return;
3480 }
3481
3482 std::string_view strData(
3483 reinterpret_cast<const char*>(code.data()),
3484 code.size());
3485
3486 asyncResp->res.addHeader("Content-Type",
3487 "application/octet-stream");
3488 asyncResp->res.addHeader("Content-Transfer-Encoding",
3489 "Base64");
3490 asyncResp->res.body() =
3491 crow::utility::base64encode(strData);
3492 },
3493 "xyz.openbmc_project.State.Boot.PostCode0",
3494 "/xyz/openbmc_project/State/Boot/PostCode0",
3495 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3496 index);
3497 });
3498}
3499
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003500inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003501{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003502 BMCWEB_ROUTE(
3503 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003504 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003505 .methods(boost::beast::http::verb::get)(
3506 [](const crow::Request&,
3507 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3508 const std::string& targetID) {
George Liu647b3cd2021-07-05 12:43:56 +08003509 uint16_t bootIndex = 0;
3510 uint64_t codeIndex = 0;
3511 if (!parsePostCode(targetID, codeIndex, bootIndex))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003512 {
3513 // Requested ID was not found
3514 messages::resourceMissingAtURI(asyncResp->res, targetID);
3515 return;
3516 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003517 if (bootIndex == 0 || codeIndex == 0)
3518 {
3519 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3520 << targetID;
3521 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003522
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003523 asyncResp->res.jsonValue["@odata.type"] =
3524 "#LogEntry.v1_4_0.LogEntry";
3525 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08003526 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003527 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3528 asyncResp->res.jsonValue["Description"] =
3529 "Collection of POST Code Log Entries";
3530 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3531 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003532
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003533 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3534 });
3535}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003536
Ed Tanous1da66f72018-07-27 16:13:37 -07003537} // namespace redfish