blob: b908da5dedaab56fda56a80f99c4f8eb69fee332 [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>
Ed Tanous168e20c2021-12-13 14:39:53 -080035#include <dbus_utility.hpp>
Andrew Geisslercb92c032018-08-17 07:56:14 -070036#include <error_messages.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070037#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038
George Liu647b3cd2021-07-05 12:43:56 +080039#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070040#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080041#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070042#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070043#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080044#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070045
46namespace redfish
47{
48
Gunnar Mills1214b7e2020-06-04 10:11:30 -050049constexpr char const* crashdumpObject = "com.intel.crashdump";
50constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050051constexpr char const* crashdumpInterface = "com.intel.crashdump";
52constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070053 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050054constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070055 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070056constexpr char const* crashdumpTelemetryInterface =
57 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070058
Jason M. Bills4851d452019-03-28 11:27:48 -070059namespace message_registries
60{
Ed Tanous26702d02021-11-03 15:02:33 -070061static const Message*
62 getMessageFromRegistry(const std::string& messageKey,
63 const std::span<const MessageEntry> registry)
Jason M. Bills4851d452019-03-28 11:27:48 -070064{
Ed Tanous26702d02021-11-03 15:02:33 -070065 std::span<const MessageEntry>::iterator messageIt = std::find_if(
66 registry.begin(), registry.end(),
67 [&messageKey](const MessageEntry& messageEntry) {
68 return !std::strcmp(messageEntry.first, messageKey.c_str());
69 });
70 if (messageIt != registry.end())
Jason M. Bills4851d452019-03-28 11:27:48 -070071 {
72 return &messageIt->second;
73 }
74
75 return nullptr;
76}
77
Gunnar Mills1214b7e2020-06-04 10:11:30 -050078static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070079{
80 // Redfish MessageIds are in the form
81 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
82 // the right Message
83 std::vector<std::string> fields;
84 fields.reserve(4);
85 boost::split(fields, messageID, boost::is_any_of("."));
Gunnar Mills1214b7e2020-06-04 10:11:30 -050086 std::string& registryName = fields[0];
87 std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -070088
89 // Find the right registry and check it for the MessageKey
90 if (std::string(base::header.registryPrefix) == registryName)
91 {
92 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070093 messageKey, std::span<const MessageEntry>(base::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070094 }
95 if (std::string(openbmc::header.registryPrefix) == registryName)
96 {
97 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070098 messageKey, std::span<const MessageEntry>(openbmc::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070099 }
100 return nullptr;
101}
102} // namespace message_registries
103
James Feistf6150402019-01-08 10:36:20 -0800104namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700105
Ed Tanous168e20c2021-12-13 14:39:53 -0800106using GetManagedPropertyType =
107 boost::container::flat_map<std::string, dbus::utility::DbusVariantType>;
Andrew Geisslercb92c032018-08-17 07:56:14 -0700108
109using GetManagedObjectsType = boost::container::flat_map<
110 sdbusplus::message::object_path,
111 boost::container::flat_map<std::string, GetManagedPropertyType>>;
112
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500113inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700114{
Ed Tanousd4d25792020-09-29 15:15:03 -0700115 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
116 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
117 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
118 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700119 {
120 return "Critical";
121 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700122 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
123 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
124 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700125 {
126 return "OK";
127 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700128 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700129 {
130 return "Warning";
131 }
132 return "";
133}
134
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700135inline static int getJournalMetadata(sd_journal* journal,
136 const std::string_view& field,
137 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700138{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500139 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700140 size_t length = 0;
141 int ret = 0;
142 // Get the metadata from the requested field of the journal entry
Ed Tanous271584a2019-07-09 16:24:22 -0700143 ret = sd_journal_get_data(journal, field.data(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500144 reinterpret_cast<const void**>(&data), &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700145 if (ret < 0)
146 {
147 return ret;
148 }
Ed Tanous39e77502019-03-04 17:35:53 -0800149 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700150 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000151 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700152 return ret;
153}
154
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700155inline static int getJournalMetadata(sd_journal* journal,
156 const std::string_view& field,
157 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700158{
159 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800160 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700161 // Get the metadata from the requested field of the journal entry
162 ret = getJournalMetadata(journal, field, metadata);
163 if (ret < 0)
164 {
165 return ret;
166 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000167 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700168 return ret;
169}
170
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700171inline static bool getEntryTimestamp(sd_journal* journal,
172 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800173{
174 int ret = 0;
175 uint64_t timestamp = 0;
176 ret = sd_journal_get_realtime_usec(journal, &timestamp);
177 if (ret < 0)
178 {
179 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
180 << strerror(-ret);
181 return false;
182 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800183 entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500184 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800185}
186
zhanghch058d1b46d2021-04-01 11:18:24 +0800187static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
188 const crow::Request& req, uint64_t& skip)
Jason M. Bills16428a12018-11-02 12:42:29 -0700189{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700190 boost::urls::query_params_view::iterator it = req.urlParams.find("$skip");
James Feist5a7e8772020-07-22 09:08:38 -0700191 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700192 {
James Feist5a7e8772020-07-22 09:08:38 -0700193 std::string skipParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500194 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700195 skip = std::strtoul(skipParam.c_str(), &ptr, 10);
196 if (skipParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700197 {
198
zhanghch058d1b46d2021-04-01 11:18:24 +0800199 messages::queryParameterValueTypeError(
200 asyncResp->res, std::string(skipParam), "$skip");
Jason M. Bills16428a12018-11-02 12:42:29 -0700201 return false;
202 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700203 }
204 return true;
205}
206
Ed Tanous271584a2019-07-09 16:24:22 -0700207static constexpr const uint64_t maxEntriesPerPage = 1000;
zhanghch058d1b46d2021-04-01 11:18:24 +0800208static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
209 const crow::Request& req, uint64_t& top)
Jason M. Bills16428a12018-11-02 12:42:29 -0700210{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700211 boost::urls::query_params_view::iterator it = req.urlParams.find("$top");
James Feist5a7e8772020-07-22 09:08:38 -0700212 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700213 {
James Feist5a7e8772020-07-22 09:08:38 -0700214 std::string topParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500215 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700216 top = std::strtoul(topParam.c_str(), &ptr, 10);
217 if (topParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700218 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800219 messages::queryParameterValueTypeError(
220 asyncResp->res, std::string(topParam), "$top");
Jason M. Bills16428a12018-11-02 12:42:29 -0700221 return false;
222 }
Ed Tanous271584a2019-07-09 16:24:22 -0700223 if (top < 1U || top > maxEntriesPerPage)
Jason M. Bills16428a12018-11-02 12:42:29 -0700224 {
225
226 messages::queryParameterOutOfRange(
zhanghch058d1b46d2021-04-01 11:18:24 +0800227 asyncResp->res, std::to_string(top), "$top",
Jason M. Bills16428a12018-11-02 12:42:29 -0700228 "1-" + std::to_string(maxEntriesPerPage));
229 return false;
230 }
231 }
232 return true;
233}
234
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700235inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
236 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700237{
238 int ret = 0;
239 static uint64_t prevTs = 0;
240 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700241 if (firstEntry)
242 {
243 prevTs = 0;
244 }
245
Jason M. Bills16428a12018-11-02 12:42:29 -0700246 // Get the entry timestamp
247 uint64_t curTs = 0;
248 ret = sd_journal_get_realtime_usec(journal, &curTs);
249 if (ret < 0)
250 {
251 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
252 << strerror(-ret);
253 return false;
254 }
255 // If the timestamp isn't unique, increment the index
256 if (curTs == prevTs)
257 {
258 index++;
259 }
260 else
261 {
262 // Otherwise, reset it
263 index = 0;
264 }
265 // Save the timestamp
266 prevTs = curTs;
267
268 entryID = std::to_string(curTs);
269 if (index > 0)
270 {
271 entryID += "_" + std::to_string(index);
272 }
273 return true;
274}
275
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500276static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700277 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700278{
Ed Tanous271584a2019-07-09 16:24:22 -0700279 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700280 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700281 if (firstEntry)
282 {
283 prevTs = 0;
284 }
285
Jason M. Bills95820182019-04-22 16:25:34 -0700286 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700287 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700288 std::tm timeStruct = {};
289 std::istringstream entryStream(logEntry);
290 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
291 {
292 curTs = std::mktime(&timeStruct);
293 }
294 // If the timestamp isn't unique, increment the index
295 if (curTs == prevTs)
296 {
297 index++;
298 }
299 else
300 {
301 // Otherwise, reset it
302 index = 0;
303 }
304 // Save the timestamp
305 prevTs = curTs;
306
307 entryID = std::to_string(curTs);
308 if (index > 0)
309 {
310 entryID += "_" + std::to_string(index);
311 }
312 return true;
313}
314
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700315inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800316 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
317 const std::string& entryID, uint64_t& timestamp,
318 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700319{
320 if (entryID.empty())
321 {
322 return false;
323 }
324 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800325 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700326
Ed Tanous81ce6092020-12-17 16:54:55 +0000327 auto underscorePos = tsStr.find('_');
Jason M. Bills16428a12018-11-02 12:42:29 -0700328 if (underscorePos != tsStr.npos)
329 {
330 // Timestamp has an index
331 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800332 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700333 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700334 auto [ptr, ec] = std::from_chars(
335 indexStr.data(), indexStr.data() + indexStr.size(), index);
336 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700337 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800338 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700339 return false;
340 }
341 }
342 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700343 auto [ptr, ec] =
344 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
345 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700346 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800347 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700348 return false;
349 }
350 return true;
351}
352
Jason M. Bills95820182019-04-22 16:25:34 -0700353static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500354 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700355{
356 static const std::filesystem::path redfishLogDir = "/var/log";
357 static const std::string redfishLogFilename = "redfish";
358
359 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500360 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700361 std::filesystem::directory_iterator(redfishLogDir))
362 {
363 // If we find a redfish log file, save the path
364 std::string filename = dirEnt.path().filename();
365 if (boost::starts_with(filename, redfishLogFilename))
366 {
367 redfishLogFiles.emplace_back(redfishLogDir / filename);
368 }
369 }
370 // As the log files rotate, they are appended with a ".#" that is higher for
371 // the older logs. Since we don't expect more than 10 log files, we
372 // can just sort the list to get them in order from newest to oldest
373 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
374
375 return !redfishLogFiles.empty();
376}
377
zhanghch058d1b46d2021-04-01 11:18:24 +0800378inline void
379 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
380 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500381{
382 std::string dumpPath;
383 if (dumpType == "BMC")
384 {
385 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
386 }
387 else if (dumpType == "System")
388 {
389 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
390 }
391 else
392 {
393 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
394 messages::internalError(asyncResp->res);
395 return;
396 }
397
398 crow::connections::systemBus->async_method_call(
399 [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
400 GetManagedObjectsType& resp) {
401 if (ec)
402 {
403 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
404 messages::internalError(asyncResp->res);
405 return;
406 }
407
408 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
409 entriesArray = nlohmann::json::array();
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500410 std::string dumpEntryPath =
411 "/xyz/openbmc_project/dump/" +
412 std::string(boost::algorithm::to_lower_copy(dumpType)) +
413 "/entry/";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500414
415 for (auto& object : resp)
416 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500417 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500418 {
419 continue;
420 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800421 uint64_t timestamp = 0;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500422 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500423 std::string dumpStatus;
424 nlohmann::json thisEntry;
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000425
426 std::string entryID = object.first.filename();
427 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500428 {
429 continue;
430 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500431
432 for (auto& interfaceMap : object.second)
433 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500434 if (interfaceMap.first ==
435 "xyz.openbmc_project.Common.Progress")
436 {
437 for (auto& propertyMap : interfaceMap.second)
438 {
439 if (propertyMap.first == "Status")
440 {
441 auto status = std::get_if<std::string>(
442 &propertyMap.second);
443 if (status == nullptr)
444 {
445 messages::internalError(asyncResp->res);
446 break;
447 }
448 dumpStatus = *status;
449 }
450 }
451 }
452 else if (interfaceMap.first ==
453 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500454 {
455
456 for (auto& propertyMap : interfaceMap.second)
457 {
458 if (propertyMap.first == "Size")
459 {
460 auto sizePtr =
461 std::get_if<uint64_t>(&propertyMap.second);
462 if (sizePtr == nullptr)
463 {
464 messages::internalError(asyncResp->res);
465 break;
466 }
467 size = *sizePtr;
468 break;
469 }
470 }
471 }
472 else if (interfaceMap.first ==
473 "xyz.openbmc_project.Time.EpochTime")
474 {
475
476 for (auto& propertyMap : interfaceMap.second)
477 {
478 if (propertyMap.first == "Elapsed")
479 {
480 const uint64_t* usecsTimeStamp =
481 std::get_if<uint64_t>(&propertyMap.second);
482 if (usecsTimeStamp == nullptr)
483 {
484 messages::internalError(asyncResp->res);
485 break;
486 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800487 timestamp = (*usecsTimeStamp / 1000 / 1000);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500488 break;
489 }
490 }
491 }
492 }
493
George Liu0fda0f12021-11-16 10:06:17 +0800494 if (dumpStatus !=
495 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500496 !dumpStatus.empty())
497 {
498 // Dump status is not Complete, no need to enumerate
499 continue;
500 }
501
George Liu647b3cd2021-07-05 12:43:56 +0800502 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500503 thisEntry["@odata.id"] = dumpPath + entryID;
504 thisEntry["Id"] = entryID;
505 thisEntry["EntryType"] = "Event";
Nan Zhou1d8782e2021-11-29 22:23:18 -0800506 thisEntry["Created"] =
507 crow::utility::getDateTimeUint(timestamp);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500508 thisEntry["Name"] = dumpType + " Dump Entry";
509
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500510 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500511
512 if (dumpType == "BMC")
513 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500514 thisEntry["DiagnosticDataType"] = "Manager";
515 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500516 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
517 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500518 }
519 else if (dumpType == "System")
520 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500521 thisEntry["DiagnosticDataType"] = "OEM";
522 thisEntry["OEMDiagnosticDataType"] = "System";
523 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500524 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
525 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500526 }
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500527 entriesArray.push_back(std::move(thisEntry));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500528 }
529 asyncResp->res.jsonValue["Members@odata.count"] =
530 entriesArray.size();
531 },
532 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
533 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
534}
535
zhanghch058d1b46d2021-04-01 11:18:24 +0800536inline void
537 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
538 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500539{
540 std::string dumpPath;
541 if (dumpType == "BMC")
542 {
543 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
544 }
545 else if (dumpType == "System")
546 {
547 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
548 }
549 else
550 {
551 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
552 messages::internalError(asyncResp->res);
553 return;
554 }
555
556 crow::connections::systemBus->async_method_call(
557 [asyncResp, entryID, dumpPath, dumpType](
558 const boost::system::error_code ec, GetManagedObjectsType& resp) {
559 if (ec)
560 {
561 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
562 messages::internalError(asyncResp->res);
563 return;
564 }
565
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500566 bool foundDumpEntry = false;
567 std::string dumpEntryPath =
568 "/xyz/openbmc_project/dump/" +
569 std::string(boost::algorithm::to_lower_copy(dumpType)) +
570 "/entry/";
571
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500572 for (auto& objectPath : resp)
573 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500574 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500575 {
576 continue;
577 }
578
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500579 foundDumpEntry = true;
Nan Zhou1d8782e2021-11-29 22:23:18 -0800580 uint64_t timestamp = 0;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500582 std::string dumpStatus;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500583
584 for (auto& interfaceMap : objectPath.second)
585 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500586 if (interfaceMap.first ==
587 "xyz.openbmc_project.Common.Progress")
588 {
589 for (auto& propertyMap : interfaceMap.second)
590 {
591 if (propertyMap.first == "Status")
592 {
593 auto status = std::get_if<std::string>(
594 &propertyMap.second);
595 if (status == nullptr)
596 {
597 messages::internalError(asyncResp->res);
598 break;
599 }
600 dumpStatus = *status;
601 }
602 }
603 }
604 else if (interfaceMap.first ==
605 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500606 {
607 for (auto& propertyMap : interfaceMap.second)
608 {
609 if (propertyMap.first == "Size")
610 {
611 auto sizePtr =
612 std::get_if<uint64_t>(&propertyMap.second);
613 if (sizePtr == nullptr)
614 {
615 messages::internalError(asyncResp->res);
616 break;
617 }
618 size = *sizePtr;
619 break;
620 }
621 }
622 }
623 else if (interfaceMap.first ==
624 "xyz.openbmc_project.Time.EpochTime")
625 {
626 for (auto& propertyMap : interfaceMap.second)
627 {
628 if (propertyMap.first == "Elapsed")
629 {
630 const uint64_t* usecsTimeStamp =
631 std::get_if<uint64_t>(&propertyMap.second);
632 if (usecsTimeStamp == nullptr)
633 {
634 messages::internalError(asyncResp->res);
635 break;
636 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800637 timestamp = *usecsTimeStamp / 1000 / 1000;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500638 break;
639 }
640 }
641 }
642 }
643
George Liu0fda0f12021-11-16 10:06:17 +0800644 if (dumpStatus !=
645 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500646 !dumpStatus.empty())
647 {
648 // Dump status is not Complete
649 // return not found until status is changed to Completed
650 messages::resourceNotFound(asyncResp->res,
651 dumpType + " dump", entryID);
652 return;
653 }
654
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500655 asyncResp->res.jsonValue["@odata.type"] =
George Liu647b3cd2021-07-05 12:43:56 +0800656 "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500657 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
658 asyncResp->res.jsonValue["Id"] = entryID;
659 asyncResp->res.jsonValue["EntryType"] = "Event";
660 asyncResp->res.jsonValue["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -0800661 crow::utility::getDateTimeUint(timestamp);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500662 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
663
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500664 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500665
666 if (dumpType == "BMC")
667 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500668 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
669 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500670 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
671 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500672 }
673 else if (dumpType == "System")
674 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500675 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
676 asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500677 "System";
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500678 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500679 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
680 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500681 }
682 }
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500683 if (foundDumpEntry == false)
684 {
685 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
686 messages::internalError(asyncResp->res);
687 return;
688 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500689 },
690 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
691 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
692}
693
zhanghch058d1b46d2021-04-01 11:18:24 +0800694inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800695 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500696 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500697{
George Liu3de8d8b2021-03-22 17:49:39 +0800698 auto respHandler = [asyncResp,
699 entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500700 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
701 if (ec)
702 {
George Liu3de8d8b2021-03-22 17:49:39 +0800703 if (ec.value() == EBADR)
704 {
705 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
706 return;
707 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500708 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
709 << ec;
710 messages::internalError(asyncResp->res);
711 return;
712 }
713 };
714 crow::connections::systemBus->async_method_call(
715 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500716 "/xyz/openbmc_project/dump/" +
717 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
718 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500719 "xyz.openbmc_project.Object.Delete", "Delete");
720}
721
zhanghch058d1b46d2021-04-01 11:18:24 +0800722inline void
Ed Tanous98be3e32021-09-16 15:05:36 -0700723 createDumpTaskCallback(task::Payload&& payload,
zhanghch058d1b46d2021-04-01 11:18:24 +0800724 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
725 const uint32_t& dumpId, const std::string& dumpPath,
726 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500727{
728 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500729 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500730 boost::system::error_code err, sdbusplus::message::message& m,
731 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000732 if (err)
733 {
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500734 BMCWEB_LOG_ERROR << "Error in creating a dump";
735 taskData->state = "Cancelled";
736 return task::completed;
Ed Tanouscb13a392020-07-25 19:02:03 +0000737 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500738 std::vector<std::pair<
Ed Tanous168e20c2021-12-13 14:39:53 -0800739 std::string, std::vector<std::pair<
740 std::string, dbus::utility::DbusVariantType>>>>
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500741 interfacesList;
742
743 sdbusplus::message::object_path objPath;
744
745 m.read(objPath, interfacesList);
746
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500747 if (objPath.str ==
748 "/xyz/openbmc_project/dump/" +
749 std::string(boost::algorithm::to_lower_copy(dumpType)) +
750 "/entry/" + std::to_string(dumpId))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500751 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500752 nlohmann::json retMessage = messages::success();
753 taskData->messages.emplace_back(retMessage);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500754
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500755 std::string headerLoc =
756 "Location: " + dumpPath + std::to_string(dumpId);
757 taskData->payload->httpHeaders.emplace_back(
758 std::move(headerLoc));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500759
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500760 taskData->state = "Completed";
761 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500762 }
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500763 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500764 },
765 "type='signal',interface='org.freedesktop.DBus."
766 "ObjectManager',"
767 "member='InterfacesAdded', "
768 "path='/xyz/openbmc_project/dump'");
769
770 task->startTimer(std::chrono::minutes(3));
771 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -0700772 task->payload.emplace(std::move(payload));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500773}
774
zhanghch058d1b46d2021-04-01 11:18:24 +0800775inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
776 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500777{
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500778
779 std::string dumpPath;
780 if (dumpType == "BMC")
781 {
782 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
783 }
784 else if (dumpType == "System")
785 {
786 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
787 }
788 else
789 {
790 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
791 messages::internalError(asyncResp->res);
792 return;
793 }
794
795 std::optional<std::string> diagnosticDataType;
796 std::optional<std::string> oemDiagnosticDataType;
797
798 if (!redfish::json_util::readJson(
799 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
800 "OEMDiagnosticDataType", oemDiagnosticDataType))
801 {
802 return;
803 }
804
805 if (dumpType == "System")
806 {
807 if (!oemDiagnosticDataType || !diagnosticDataType)
808 {
809 BMCWEB_LOG_ERROR << "CreateDump action parameter "
810 "'DiagnosticDataType'/"
811 "'OEMDiagnosticDataType' value not found!";
812 messages::actionParameterMissing(
813 asyncResp->res, "CollectDiagnosticData",
814 "DiagnosticDataType & OEMDiagnosticDataType");
815 return;
816 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700817 if ((*oemDiagnosticDataType != "System") ||
818 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500819 {
820 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
821 messages::invalidObject(asyncResp->res,
822 "System Dump creation parameters");
823 return;
824 }
825 }
826 else if (dumpType == "BMC")
827 {
828 if (!diagnosticDataType)
829 {
George Liu0fda0f12021-11-16 10:06:17 +0800830 BMCWEB_LOG_ERROR
831 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500832 messages::actionParameterMissing(
833 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
834 return;
835 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700836 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500837 {
838 BMCWEB_LOG_ERROR
839 << "Wrong parameter value passed for 'DiagnosticDataType'";
840 messages::invalidObject(asyncResp->res,
841 "BMC Dump creation parameters");
842 return;
843 }
844 }
845
846 crow::connections::systemBus->async_method_call(
Ed Tanous98be3e32021-09-16 15:05:36 -0700847 [asyncResp, payload(task::Payload(req)), dumpPath,
848 dumpType](const boost::system::error_code ec,
849 const uint32_t& dumpId) mutable {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500850 if (ec)
851 {
852 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
853 messages::internalError(asyncResp->res);
854 return;
855 }
856 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
857
Ed Tanous98be3e32021-09-16 15:05:36 -0700858 createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
859 dumpPath, dumpType);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500860 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500861 "xyz.openbmc_project.Dump.Manager",
862 "/xyz/openbmc_project/dump/" +
863 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500864 "xyz.openbmc_project.Dump.Create", "CreateDump");
865}
866
zhanghch058d1b46d2021-04-01 11:18:24 +0800867inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
868 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500869{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500870 std::string dumpTypeLowerCopy =
871 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800872
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500873 crow::connections::systemBus->async_method_call(
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500874 [asyncResp, dumpType](const boost::system::error_code ec,
875 const std::vector<std::string>& subTreePaths) {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500876 if (ec)
877 {
878 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
879 messages::internalError(asyncResp->res);
880 return;
881 }
882
883 for (const std::string& path : subTreePaths)
884 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000885 sdbusplus::message::object_path objPath(path);
886 std::string logID = objPath.filename();
887 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500888 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000889 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500890 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000891 deleteDumpEntry(asyncResp, logID, dumpType);
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500892 }
893 },
894 "xyz.openbmc_project.ObjectMapper",
895 "/xyz/openbmc_project/object_mapper",
896 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500897 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
898 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
899 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500900}
901
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700902inline static void parseCrashdumpParameters(
Ed Tanous168e20c2021-12-13 14:39:53 -0800903 const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
904 params,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500905 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700906{
907 for (auto property : params)
908 {
909 if (property.first == "Timestamp")
910 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500911 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500912 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700913 if (value != nullptr)
914 {
915 timestamp = *value;
916 }
917 }
918 else if (property.first == "Filename")
919 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500920 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500921 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700922 if (value != nullptr)
923 {
924 filename = *value;
925 }
926 }
927 else if (property.first == "Log")
928 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500929 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500930 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700931 if (value != nullptr)
932 {
933 logfile = *value;
934 }
935 }
936 }
937}
938
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500939constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700940inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700941{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800942 /**
943 * Functions triggers appropriate requests on DBus
944 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700945 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700946 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700947 .methods(boost::beast::http::verb::get)(
948 [](const crow::Request&,
949 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
950
951 {
952 // Collections don't include the static data added by SubRoute
953 // because it has a duplicate entry for members
954 asyncResp->res.jsonValue["@odata.type"] =
955 "#LogServiceCollection.LogServiceCollection";
956 asyncResp->res.jsonValue["@odata.id"] =
957 "/redfish/v1/Systems/system/LogServices";
958 asyncResp->res.jsonValue["Name"] =
959 "System Log Services Collection";
960 asyncResp->res.jsonValue["Description"] =
961 "Collection of LogServices for this Computer System";
962 nlohmann::json& logServiceArray =
963 asyncResp->res.jsonValue["Members"];
964 logServiceArray = nlohmann::json::array();
965 logServiceArray.push_back(
966 {{"@odata.id",
967 "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500968#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700969 logServiceArray.push_back(
970 {{"@odata.id",
971 "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600972#endif
973
Jason M. Billsd53dd412019-02-12 17:16:22 -0800974#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700975 logServiceArray.push_back(
976 {{"@odata.id",
977 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800978#endif
Spencer Kub7028eb2021-10-26 15:27:35 +0800979
980#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
981 logServiceArray.push_back(
982 {{"@odata.id",
983 "/redfish/v1/Systems/system/LogServices/HostLogger"}});
984#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700985 asyncResp->res.jsonValue["Members@odata.count"] =
986 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800987
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700988 crow::connections::systemBus->async_method_call(
989 [asyncResp](const boost::system::error_code ec,
990 const std::vector<std::string>& subtreePath) {
991 if (ec)
992 {
993 BMCWEB_LOG_ERROR << ec;
994 return;
995 }
ZhikuiRena3316fc2020-01-29 14:58:08 -0800996
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700997 for (auto& pathStr : subtreePath)
998 {
999 if (pathStr.find("PostCode") != std::string::npos)
1000 {
1001 nlohmann::json& logServiceArrayLocal =
1002 asyncResp->res.jsonValue["Members"];
1003 logServiceArrayLocal.push_back(
George Liu0fda0f12021-11-16 10:06:17 +08001004 {{"@odata.id",
1005 "/redfish/v1/Systems/system/LogServices/PostCodes"}});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001006 asyncResp->res
1007 .jsonValue["Members@odata.count"] =
1008 logServiceArrayLocal.size();
1009 return;
1010 }
1011 }
1012 },
1013 "xyz.openbmc_project.ObjectMapper",
1014 "/xyz/openbmc_project/object_mapper",
1015 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1016 0, std::array<const char*, 1>{postCodeIface});
1017 });
1018}
1019
1020inline void requestRoutesEventLogService(App& app)
1021{
1022 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001023 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001024 .methods(
1025 boost::beast::http::verb::
1026 get)([](const crow::Request&,
1027 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1028 asyncResp->res.jsonValue["@odata.id"] =
1029 "/redfish/v1/Systems/system/LogServices/EventLog";
1030 asyncResp->res.jsonValue["@odata.type"] =
1031 "#LogService.v1_1_0.LogService";
1032 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1033 asyncResp->res.jsonValue["Description"] =
1034 "System Event Log Service";
1035 asyncResp->res.jsonValue["Id"] = "EventLog";
1036 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301037
1038 std::pair<std::string, std::string> redfishDateTimeOffset =
1039 crow::utility::getDateTimeOffsetNow();
1040
1041 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1042 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1043 redfishDateTimeOffset.second;
1044
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001045 asyncResp->res.jsonValue["Entries"] = {
1046 {"@odata.id",
1047 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1048 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1049
George Liu0fda0f12021-11-16 10:06:17 +08001050 {"target",
1051 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001052 });
1053}
1054
1055inline void requestRoutesJournalEventLogClear(App& app)
1056{
1057 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1058 "LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001059 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001060 .methods(boost::beast::http::verb::post)(
1061 [](const crow::Request&,
1062 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1063 // Clear the EventLog by deleting the log files
1064 std::vector<std::filesystem::path> redfishLogFiles;
1065 if (getRedfishLogFiles(redfishLogFiles))
ZhikuiRena3316fc2020-01-29 14:58:08 -08001066 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001067 for (const std::filesystem::path& file : redfishLogFiles)
ZhikuiRena3316fc2020-01-29 14:58:08 -08001068 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001069 std::error_code ec;
1070 std::filesystem::remove(file, ec);
ZhikuiRena3316fc2020-01-29 14:58:08 -08001071 }
1072 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001073
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001074 // Reload rsyslog so it knows to start new log files
1075 crow::connections::systemBus->async_method_call(
1076 [asyncResp](const boost::system::error_code ec) {
1077 if (ec)
1078 {
1079 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1080 << ec;
1081 messages::internalError(asyncResp->res);
1082 return;
1083 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001084
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001085 messages::success(asyncResp->res);
1086 },
1087 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1088 "org.freedesktop.systemd1.Manager", "ReloadUnit",
1089 "rsyslog.service", "replace");
1090 });
1091}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001092
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001093static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001094 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001095 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001096{
Jason M. Bills95820182019-04-22 16:25:34 -07001097 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001098 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001099 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001100 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001101 {
1102 return 1;
1103 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001104 std::string timestamp = logEntry.substr(0, space);
1105 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001106 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001107 if (entryStart == std::string::npos)
1108 {
1109 return 1;
1110 }
1111 std::string_view entry(logEntry);
1112 entry.remove_prefix(entryStart);
1113 // Use split to separate the entry into its fields
1114 std::vector<std::string> logEntryFields;
1115 boost::split(logEntryFields, entry, boost::is_any_of(","),
1116 boost::token_compress_on);
1117 // We need at least a MessageId to be valid
1118 if (logEntryFields.size() < 1)
1119 {
1120 return 1;
1121 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001122 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001123
Jason M. Bills4851d452019-03-28 11:27:48 -07001124 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001125 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001126 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001127
Jason M. Bills4851d452019-03-28 11:27:48 -07001128 std::string msg;
1129 std::string severity;
1130 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001131 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001132 msg = message->message;
1133 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001134 }
1135
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001136 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001137 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001138 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001139 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001140 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001141 // If the first string is empty, assume there are no MessageArgs
1142 std::size_t messageArgsSize = 0;
1143 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001144 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001145 messageArgsSize = logEntryFields.size() - 1;
1146 }
1147
Ed Tanous23a21a12020-07-25 04:45:05 +00001148 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001149
1150 // Fill the MessageArgs into the Message
1151 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001152 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001153 {
1154 std::string argStr = "%" + std::to_string(++i);
1155 size_t argPos = msg.find(argStr);
1156 if (argPos != std::string::npos)
1157 {
1158 msg.replace(argPos, argStr.length(), messageArg);
1159 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001160 }
1161 }
1162
Jason M. Bills95820182019-04-22 16:25:34 -07001163 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1164 // format which matches the Redfish format except for the fractional seconds
1165 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001166 std::size_t dot = timestamp.find_first_of('.');
1167 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001168 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001169 {
Jason M. Bills95820182019-04-22 16:25:34 -07001170 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001171 }
1172
1173 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001174 logEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08001175 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001176 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001177 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001178 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001179 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001180 {"Id", logEntryID},
1181 {"Message", std::move(msg)},
1182 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001183 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001184 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001185 {"Severity", std::move(severity)},
1186 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001187 return 0;
1188}
1189
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001190inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001191{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001192 BMCWEB_ROUTE(app,
1193 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001194 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001195 .methods(boost::beast::http::verb::get)(
1196 [](const crow::Request& req,
1197 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1198 uint64_t skip = 0;
1199 uint64_t top = maxEntriesPerPage; // Show max entries by default
1200 if (!getSkipParam(asyncResp, req, skip))
Jason M. Bills95820182019-04-22 16:25:34 -07001201 {
Jason M. Bills95820182019-04-22 16:25:34 -07001202 return;
1203 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001204 if (!getTopParam(asyncResp, req, top))
Jason M. Bills897967d2019-07-29 17:05:30 -07001205 {
Jason M. Bills897967d2019-07-29 17:05:30 -07001206 return;
1207 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001208 // Collections don't include the static data added by SubRoute
1209 // because it has a duplicate entry for members
1210 asyncResp->res.jsonValue["@odata.type"] =
1211 "#LogEntryCollection.LogEntryCollection";
1212 asyncResp->res.jsonValue["@odata.id"] =
1213 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1214 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1215 asyncResp->res.jsonValue["Description"] =
1216 "Collection of System Event Log Entries";
Jason M. Bills897967d2019-07-29 17:05:30 -07001217
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001218 nlohmann::json& logEntryArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001219 asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001220 logEntryArray = nlohmann::json::array();
1221 // Go through the log files and create a unique ID for each
1222 // entry
1223 std::vector<std::filesystem::path> redfishLogFiles;
1224 getRedfishLogFiles(redfishLogFiles);
1225 uint64_t entryCount = 0;
1226 std::string logEntry;
1227
1228 // Oldest logs are in the last file, so start there and loop
1229 // backwards
1230 for (auto it = redfishLogFiles.rbegin();
1231 it < redfishLogFiles.rend(); it++)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001232 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001233 std::ifstream logStream(*it);
1234 if (!logStream.is_open())
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001235 {
1236 continue;
1237 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001238
1239 // Reset the unique ID on the first entry
1240 bool firstEntry = true;
1241 while (std::getline(logStream, logEntry))
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001242 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001243 entryCount++;
1244 // Handle paging using skip (number of entries to skip
1245 // from the start) and top (number of entries to
1246 // display)
1247 if (entryCount <= skip || entryCount > skip + top)
George Liuebd45902020-08-26 14:21:10 +08001248 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001249 continue;
George Liuebd45902020-08-26 14:21:10 +08001250 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001251
1252 std::string idStr;
1253 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
George Liuebd45902020-08-26 14:21:10 +08001254 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001255 continue;
George Liuebd45902020-08-26 14:21:10 +08001256 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001257
1258 if (firstEntry)
1259 {
1260 firstEntry = false;
1261 }
1262
1263 logEntryArray.push_back({});
1264 nlohmann::json& bmcLogEntry = logEntryArray.back();
1265 if (fillEventLogEntryJson(idStr, logEntry,
1266 bmcLogEntry) != 0)
Xiaochao Ma75710de2021-01-21 17:56:02 +08001267 {
1268 messages::internalError(asyncResp->res);
1269 return;
1270 }
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001271 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001272 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001273 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1274 if (skip + top < entryCount)
Ed Tanous271584a2019-07-09 16:24:22 -07001275 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001276 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001277 "/redfish/v1/Systems/system/LogServices/EventLog/"
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001278 "Entries?$skip=" +
1279 std::to_string(skip + top);
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001280 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001281 });
1282}
Chicago Duan336e96c2019-07-15 14:22:08 +08001283
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001284inline void requestRoutesJournalEventLogEntry(App& app)
1285{
1286 BMCWEB_ROUTE(
1287 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001288 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001289 .methods(boost::beast::http::verb::get)(
1290 [](const crow::Request&,
1291 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1292 const std::string& param) {
1293 const std::string& targetID = param;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001294
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001295 // Go through the log files and check the unique ID for each
1296 // entry to find the target entry
1297 std::vector<std::filesystem::path> redfishLogFiles;
1298 getRedfishLogFiles(redfishLogFiles);
1299 std::string logEntry;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001300
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001301 // Oldest logs are in the last file, so start there and loop
1302 // backwards
1303 for (auto it = redfishLogFiles.rbegin();
1304 it < redfishLogFiles.rend(); it++)
1305 {
1306 std::ifstream logStream(*it);
1307 if (!logStream.is_open())
1308 {
1309 continue;
1310 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001311
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001312 // Reset the unique ID on the first entry
1313 bool firstEntry = true;
1314 while (std::getline(logStream, logEntry))
1315 {
1316 std::string idStr;
1317 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1318 {
1319 continue;
1320 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001321
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001322 if (firstEntry)
1323 {
1324 firstEntry = false;
1325 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001326
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001327 if (idStr == targetID)
1328 {
1329 if (fillEventLogEntryJson(
1330 idStr, logEntry,
1331 asyncResp->res.jsonValue) != 0)
1332 {
1333 messages::internalError(asyncResp->res);
1334 return;
1335 }
1336 return;
1337 }
1338 }
1339 }
1340 // Requested ID was not found
1341 messages::resourceMissingAtURI(asyncResp->res, targetID);
1342 });
1343}
1344
1345inline void requestRoutesDBusEventLogEntryCollection(App& app)
1346{
1347 BMCWEB_ROUTE(app,
1348 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001349 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001350 .methods(
1351 boost::beast::http::verb::
1352 get)([](const crow::Request&,
1353 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1354 // Collections don't include the static data added by SubRoute
1355 // because it has a duplicate entry for members
1356 asyncResp->res.jsonValue["@odata.type"] =
1357 "#LogEntryCollection.LogEntryCollection";
1358 asyncResp->res.jsonValue["@odata.id"] =
1359 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1360 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1361 asyncResp->res.jsonValue["Description"] =
1362 "Collection of System Event Log Entries";
1363
1364 // DBus implementation of EventLog/Entries
1365 // Make call to Logging Service to find all log entry objects
Xiaochao Ma75710de2021-01-21 17:56:02 +08001366 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001367 [asyncResp](const boost::system::error_code ec,
1368 GetManagedObjectsType& resp) {
Xiaochao Ma75710de2021-01-21 17:56:02 +08001369 if (ec)
1370 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001371 // TODO Handle for specific error code
1372 BMCWEB_LOG_ERROR
1373 << "getLogEntriesIfaceData resp_handler got error "
1374 << ec;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001375 messages::internalError(asyncResp->res);
1376 return;
1377 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001378 nlohmann::json& entriesArray =
1379 asyncResp->res.jsonValue["Members"];
1380 entriesArray = nlohmann::json::array();
1381 for (auto& objectPath : resp)
1382 {
1383 uint32_t* id = nullptr;
1384 std::time_t timestamp{};
1385 std::time_t updateTimestamp{};
1386 std::string* severity = nullptr;
1387 std::string* message = nullptr;
1388 std::string* filePath = nullptr;
1389 bool resolved = false;
1390 for (auto& interfaceMap : objectPath.second)
1391 {
1392 if (interfaceMap.first ==
1393 "xyz.openbmc_project.Logging.Entry")
1394 {
1395 for (auto& propertyMap : interfaceMap.second)
1396 {
1397 if (propertyMap.first == "Id")
1398 {
1399 id = std::get_if<uint32_t>(
1400 &propertyMap.second);
1401 }
1402 else if (propertyMap.first == "Timestamp")
1403 {
1404 const uint64_t* millisTimeStamp =
1405 std::get_if<uint64_t>(
1406 &propertyMap.second);
1407 if (millisTimeStamp != nullptr)
1408 {
1409 timestamp =
1410 crow::utility::getTimestamp(
1411 *millisTimeStamp);
1412 }
1413 }
1414 else if (propertyMap.first ==
1415 "UpdateTimestamp")
1416 {
1417 const uint64_t* millisTimeStamp =
1418 std::get_if<uint64_t>(
1419 &propertyMap.second);
1420 if (millisTimeStamp != nullptr)
1421 {
1422 updateTimestamp =
1423 crow::utility::getTimestamp(
1424 *millisTimeStamp);
1425 }
1426 }
1427 else if (propertyMap.first == "Severity")
1428 {
1429 severity = std::get_if<std::string>(
1430 &propertyMap.second);
1431 }
1432 else if (propertyMap.first == "Message")
1433 {
1434 message = std::get_if<std::string>(
1435 &propertyMap.second);
1436 }
1437 else if (propertyMap.first == "Resolved")
1438 {
1439 bool* resolveptr = std::get_if<bool>(
1440 &propertyMap.second);
1441 if (resolveptr == nullptr)
1442 {
1443 messages::internalError(
1444 asyncResp->res);
1445 return;
1446 }
1447 resolved = *resolveptr;
1448 }
1449 }
1450 if (id == nullptr || message == nullptr ||
1451 severity == nullptr)
1452 {
1453 messages::internalError(asyncResp->res);
1454 return;
1455 }
1456 }
1457 else if (interfaceMap.first ==
1458 "xyz.openbmc_project.Common.FilePath")
1459 {
1460 for (auto& propertyMap : interfaceMap.second)
1461 {
1462 if (propertyMap.first == "Path")
1463 {
1464 filePath = std::get_if<std::string>(
1465 &propertyMap.second);
1466 }
1467 }
1468 }
1469 }
1470 // Object path without the
1471 // xyz.openbmc_project.Logging.Entry interface, ignore
1472 // and continue.
1473 if (id == nullptr || message == nullptr ||
1474 severity == nullptr)
1475 {
1476 continue;
1477 }
1478 entriesArray.push_back({});
1479 nlohmann::json& thisEntry = entriesArray.back();
1480 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1481 thisEntry["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001482 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001483 std::to_string(*id);
1484 thisEntry["Name"] = "System Event Log Entry";
1485 thisEntry["Id"] = std::to_string(*id);
1486 thisEntry["Message"] = *message;
1487 thisEntry["Resolved"] = resolved;
1488 thisEntry["EntryType"] = "Event";
1489 thisEntry["Severity"] =
1490 translateSeverityDbusToRedfish(*severity);
1491 thisEntry["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001492 crow::utility::getDateTimeStdtime(timestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001493 thisEntry["Modified"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001494 crow::utility::getDateTimeStdtime(updateTimestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001495 if (filePath != nullptr)
1496 {
1497 thisEntry["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001498 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001499 std::to_string(*id) + "/attachment";
1500 }
1501 }
1502 std::sort(entriesArray.begin(), entriesArray.end(),
1503 [](const nlohmann::json& left,
1504 const nlohmann::json& right) {
1505 return (left["Id"] <= right["Id"]);
1506 });
1507 asyncResp->res.jsonValue["Members@odata.count"] =
1508 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001509 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001510 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1511 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1512 });
1513}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001514
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001515inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001516{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001517 BMCWEB_ROUTE(
1518 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001519 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001520 .methods(boost::beast::http::verb::get)(
1521 [](const crow::Request&,
1522 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1523 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001524
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001525 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001526 std::string entryID = param;
1527 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001528
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001529 // DBus implementation of EventLog/Entries
1530 // Make call to Logging Service to find all log entry objects
1531 crow::connections::systemBus->async_method_call(
1532 [asyncResp, entryID](const boost::system::error_code ec,
1533 GetManagedPropertyType& resp) {
1534 if (ec.value() == EBADR)
1535 {
1536 messages::resourceNotFound(
1537 asyncResp->res, "EventLogEntry", entryID);
1538 return;
1539 }
1540 if (ec)
1541 {
George Liu0fda0f12021-11-16 10:06:17 +08001542 BMCWEB_LOG_ERROR
1543 << "EventLogEntry (DBus) resp_handler got error "
1544 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001545 messages::internalError(asyncResp->res);
1546 return;
1547 }
1548 uint32_t* id = nullptr;
1549 std::time_t timestamp{};
1550 std::time_t updateTimestamp{};
1551 std::string* severity = nullptr;
1552 std::string* message = nullptr;
1553 std::string* filePath = nullptr;
1554 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001555
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001556 for (auto& propertyMap : resp)
1557 {
1558 if (propertyMap.first == "Id")
1559 {
1560 id = std::get_if<uint32_t>(&propertyMap.second);
1561 }
1562 else if (propertyMap.first == "Timestamp")
1563 {
1564 const uint64_t* millisTimeStamp =
1565 std::get_if<uint64_t>(&propertyMap.second);
1566 if (millisTimeStamp != nullptr)
1567 {
1568 timestamp = crow::utility::getTimestamp(
1569 *millisTimeStamp);
1570 }
1571 }
1572 else if (propertyMap.first == "UpdateTimestamp")
1573 {
1574 const uint64_t* millisTimeStamp =
1575 std::get_if<uint64_t>(&propertyMap.second);
1576 if (millisTimeStamp != nullptr)
1577 {
1578 updateTimestamp =
1579 crow::utility::getTimestamp(
1580 *millisTimeStamp);
1581 }
1582 }
1583 else if (propertyMap.first == "Severity")
1584 {
1585 severity = std::get_if<std::string>(
1586 &propertyMap.second);
1587 }
1588 else if (propertyMap.first == "Message")
1589 {
1590 message = std::get_if<std::string>(
1591 &propertyMap.second);
1592 }
1593 else if (propertyMap.first == "Resolved")
1594 {
1595 bool* resolveptr =
1596 std::get_if<bool>(&propertyMap.second);
1597 if (resolveptr == nullptr)
1598 {
1599 messages::internalError(asyncResp->res);
1600 return;
1601 }
1602 resolved = *resolveptr;
1603 }
1604 else if (propertyMap.first == "Path")
1605 {
1606 filePath = std::get_if<std::string>(
1607 &propertyMap.second);
1608 }
1609 }
1610 if (id == nullptr || message == nullptr ||
1611 severity == nullptr)
1612 {
1613 messages::internalError(asyncResp->res);
1614 return;
1615 }
1616 asyncResp->res.jsonValue["@odata.type"] =
1617 "#LogEntry.v1_8_0.LogEntry";
1618 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001619 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001620 std::to_string(*id);
1621 asyncResp->res.jsonValue["Name"] =
1622 "System Event Log Entry";
1623 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1624 asyncResp->res.jsonValue["Message"] = *message;
1625 asyncResp->res.jsonValue["Resolved"] = resolved;
1626 asyncResp->res.jsonValue["EntryType"] = "Event";
1627 asyncResp->res.jsonValue["Severity"] =
1628 translateSeverityDbusToRedfish(*severity);
1629 asyncResp->res.jsonValue["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001630 crow::utility::getDateTimeStdtime(timestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001631 asyncResp->res.jsonValue["Modified"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001632 crow::utility::getDateTimeStdtime(updateTimestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001633 if (filePath != nullptr)
1634 {
1635 asyncResp->res.jsonValue["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001636 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001637 std::to_string(*id);
1638 }
1639 },
1640 "xyz.openbmc_project.Logging",
1641 "/xyz/openbmc_project/logging/entry/" + entryID,
1642 "org.freedesktop.DBus.Properties", "GetAll", "");
1643 });
1644
1645 BMCWEB_ROUTE(
1646 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001647 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001648 .methods(boost::beast::http::verb::patch)(
1649 [](const crow::Request& req,
1650 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1651 const std::string& entryId) {
1652 std::optional<bool> resolved;
1653
1654 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1655 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001656 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001657 return;
1658 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001659 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001660
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001661 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001662 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001663 if (ec)
1664 {
1665 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1666 messages::internalError(asyncResp->res);
1667 return;
1668 }
1669 },
1670 "xyz.openbmc_project.Logging",
1671 "/xyz/openbmc_project/logging/entry/" + entryId,
1672 "org.freedesktop.DBus.Properties", "Set",
1673 "xyz.openbmc_project.Logging.Entry", "Resolved",
Ed Tanous168e20c2021-12-13 14:39:53 -08001674 dbus::utility::DbusVariantType(*resolved));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001675 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001676
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001677 BMCWEB_ROUTE(
1678 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001679 .privileges(redfish::privileges::deleteLogEntry)
1680
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001681 .methods(boost::beast::http::verb::delete_)(
1682 [](const crow::Request&,
1683 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1684 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001685
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001686 {
1687 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001688
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001689 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001690
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001691 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001692
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001693 // Process response from Logging service.
1694 auto respHandler = [asyncResp, entryID](
1695 const boost::system::error_code ec) {
1696 BMCWEB_LOG_DEBUG
1697 << "EventLogEntry (DBus) doDelete callback: Done";
1698 if (ec)
1699 {
1700 if (ec.value() == EBADR)
1701 {
1702 messages::resourceNotFound(asyncResp->res,
1703 "LogEntry", entryID);
1704 return;
1705 }
1706 // TODO Handle for specific error code
George Liu0fda0f12021-11-16 10:06:17 +08001707 BMCWEB_LOG_ERROR
1708 << "EventLogEntry (DBus) doDelete respHandler got error "
1709 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001710 asyncResp->res.result(
1711 boost::beast::http::status::internal_server_error);
1712 return;
1713 }
1714
1715 asyncResp->res.result(boost::beast::http::status::ok);
1716 };
1717
1718 // Make call to Logging service to request Delete Log
1719 crow::connections::systemBus->async_method_call(
1720 respHandler, "xyz.openbmc_project.Logging",
1721 "/xyz/openbmc_project/logging/entry/" + entryID,
1722 "xyz.openbmc_project.Object.Delete", "Delete");
1723 });
1724}
1725
1726inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001727{
George Liu0fda0f12021-11-16 10:06:17 +08001728 BMCWEB_ROUTE(
1729 app,
1730 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001731 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001732 .methods(boost::beast::http::verb::get)(
1733 [](const crow::Request& req,
1734 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1735 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001736
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001737 {
George Liu647b3cd2021-07-05 12:43:56 +08001738 if (!http_helpers::isOctetAccepted(
1739 req.getHeaderValue("Accept")))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001740 {
1741 asyncResp->res.result(
1742 boost::beast::http::status::bad_request);
1743 return;
1744 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001745
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001746 std::string entryID = param;
1747 dbus::utility::escapePathForDbus(entryID);
1748
1749 crow::connections::systemBus->async_method_call(
1750 [asyncResp,
1751 entryID](const boost::system::error_code ec,
1752 const sdbusplus::message::unix_fd& unixfd) {
1753 if (ec.value() == EBADR)
1754 {
1755 messages::resourceNotFound(
1756 asyncResp->res, "EventLogAttachment", entryID);
1757 return;
1758 }
1759 if (ec)
1760 {
1761 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1762 messages::internalError(asyncResp->res);
1763 return;
1764 }
1765
1766 int fd = -1;
1767 fd = dup(unixfd);
1768 if (fd == -1)
1769 {
1770 messages::internalError(asyncResp->res);
1771 return;
1772 }
1773
1774 long long int size = lseek(fd, 0, SEEK_END);
1775 if (size == -1)
1776 {
1777 messages::internalError(asyncResp->res);
1778 return;
1779 }
1780
1781 // Arbitrary max size of 64kb
1782 constexpr int maxFileSize = 65536;
1783 if (size > maxFileSize)
1784 {
1785 BMCWEB_LOG_ERROR
1786 << "File size exceeds maximum allowed size of "
1787 << maxFileSize;
1788 messages::internalError(asyncResp->res);
1789 return;
1790 }
1791 std::vector<char> data(static_cast<size_t>(size));
1792 long long int rc = lseek(fd, 0, SEEK_SET);
1793 if (rc == -1)
1794 {
1795 messages::internalError(asyncResp->res);
1796 return;
1797 }
1798 rc = read(fd, data.data(), data.size());
1799 if ((rc == -1) || (rc != size))
1800 {
1801 messages::internalError(asyncResp->res);
1802 return;
1803 }
1804 close(fd);
1805
1806 std::string_view strData(data.data(), data.size());
1807 std::string output =
1808 crow::utility::base64encode(strData);
1809
1810 asyncResp->res.addHeader("Content-Type",
1811 "application/octet-stream");
1812 asyncResp->res.addHeader("Content-Transfer-Encoding",
1813 "Base64");
1814 asyncResp->res.body() = std::move(output);
1815 },
1816 "xyz.openbmc_project.Logging",
1817 "/xyz/openbmc_project/logging/entry/" + entryID,
1818 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1819 });
1820}
1821
Spencer Kub7028eb2021-10-26 15:27:35 +08001822constexpr const char* hostLoggerFolderPath = "/var/log/console";
1823
1824inline bool
1825 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1826 std::vector<std::filesystem::path>& hostLoggerFiles)
1827{
1828 std::error_code ec;
1829 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1830 if (ec)
1831 {
1832 BMCWEB_LOG_ERROR << ec.message();
1833 return false;
1834 }
1835 for (const std::filesystem::directory_entry& it : logPath)
1836 {
1837 std::string filename = it.path().filename();
1838 // Prefix of each log files is "log". Find the file and save the
1839 // path
1840 if (boost::starts_with(filename, "log"))
1841 {
1842 hostLoggerFiles.emplace_back(it.path());
1843 }
1844 }
1845 // As the log files rotate, they are appended with a ".#" that is higher for
1846 // the older logs. Since we start from oldest logs, sort the name in
1847 // descending order.
1848 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1849 AlphanumLess<std::string>());
1850
1851 return true;
1852}
1853
1854inline bool
1855 getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1856 uint64_t& skip, uint64_t& top,
1857 std::vector<std::string>& logEntries, size_t& logCount)
1858{
1859 GzFileReader logFile;
1860
1861 // Go though all log files and expose host logs.
1862 for (const std::filesystem::path& it : hostLoggerFiles)
1863 {
1864 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1865 {
1866 BMCWEB_LOG_ERROR << "fail to expose host logs";
1867 return false;
1868 }
1869 }
1870 // Get lastMessage from constructor by getter
1871 std::string lastMessage = logFile.getLastMessage();
1872 if (!lastMessage.empty())
1873 {
1874 logCount++;
1875 if (logCount > skip && logCount <= (skip + top))
1876 {
1877 logEntries.push_back(lastMessage);
1878 }
1879 }
1880 return true;
1881}
1882
1883inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1884 const std::string& msg,
1885 nlohmann::json& logEntryJson)
1886{
1887 // Fill in the log entry with the gathered data.
1888 logEntryJson = {
1889 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1890 {"@odata.id",
1891 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1892 logEntryID},
1893 {"Name", "Host Logger Entry"},
1894 {"Id", logEntryID},
1895 {"Message", msg},
1896 {"EntryType", "Oem"},
1897 {"Severity", "OK"},
1898 {"OemRecordFormat", "Host Logger Entry"}};
1899}
1900
1901inline void requestRoutesSystemHostLogger(App& app)
1902{
1903 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1904 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08001905 .methods(
1906 boost::beast::http::verb::
1907 get)([](const crow::Request&,
1908 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1909 asyncResp->res.jsonValue["@odata.id"] =
1910 "/redfish/v1/Systems/system/LogServices/HostLogger";
1911 asyncResp->res.jsonValue["@odata.type"] =
1912 "#LogService.v1_1_0.LogService";
1913 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1914 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1915 asyncResp->res.jsonValue["Id"] = "HostLogger";
1916 asyncResp->res.jsonValue["Entries"] = {
1917 {"@odata.id",
1918 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1919 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001920}
1921
1922inline void requestRoutesSystemHostLoggerCollection(App& app)
1923{
1924 BMCWEB_ROUTE(app,
1925 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1926 .privileges(redfish::privileges::getLogEntry)
George Liu0fda0f12021-11-16 10:06:17 +08001927 .methods(
1928 boost::beast::http::verb::
1929 get)([](const crow::Request& req,
1930 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1931 uint64_t skip = 0;
1932 uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1933 // default, allow range 1 to
1934 // 1000 entries per page.
1935 if (!getSkipParam(asyncResp, req, skip))
1936 {
1937 return;
1938 }
1939 if (!getTopParam(asyncResp, req, top))
1940 {
1941 return;
1942 }
1943 asyncResp->res.jsonValue["@odata.id"] =
1944 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1945 asyncResp->res.jsonValue["@odata.type"] =
1946 "#LogEntryCollection.LogEntryCollection";
1947 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1948 asyncResp->res.jsonValue["Description"] =
1949 "Collection of HostLogger Entries";
1950 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1951 logEntryArray = nlohmann::json::array();
1952 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001953
George Liu0fda0f12021-11-16 10:06:17 +08001954 std::vector<std::filesystem::path> hostLoggerFiles;
1955 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1956 {
1957 BMCWEB_LOG_ERROR << "fail to get host log file path";
1958 return;
1959 }
1960
1961 size_t logCount = 0;
1962 // This vector only store the entries we want to expose that
1963 // control by skip and top.
1964 std::vector<std::string> logEntries;
1965 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1966 logCount))
1967 {
1968 messages::internalError(asyncResp->res);
1969 return;
1970 }
1971 // If vector is empty, that means skip value larger than total
1972 // log count
1973 if (logEntries.size() == 0)
1974 {
1975 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1976 return;
1977 }
1978 if (logEntries.size() > 0)
1979 {
1980 for (size_t i = 0; i < logEntries.size(); i++)
Spencer Kub7028eb2021-10-26 15:27:35 +08001981 {
George Liu0fda0f12021-11-16 10:06:17 +08001982 logEntryArray.push_back({});
1983 nlohmann::json& hostLogEntry = logEntryArray.back();
1984 fillHostLoggerEntryJson(std::to_string(skip + i),
1985 logEntries[i], hostLogEntry);
Spencer Kub7028eb2021-10-26 15:27:35 +08001986 }
1987
George Liu0fda0f12021-11-16 10:06:17 +08001988 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1989 if (skip + top < logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001990 {
George Liu0fda0f12021-11-16 10:06:17 +08001991 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1992 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1993 std::to_string(skip + top);
Spencer Kub7028eb2021-10-26 15:27:35 +08001994 }
George Liu0fda0f12021-11-16 10:06:17 +08001995 }
1996 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001997}
1998
1999inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2000{
2001 BMCWEB_ROUTE(
2002 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2003 .privileges(redfish::privileges::getLogEntry)
2004 .methods(boost::beast::http::verb::get)(
2005 [](const crow::Request&,
2006 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2007 const std::string& param) {
2008 const std::string& targetID = param;
2009
2010 uint64_t idInt = 0;
2011 auto [ptr, ec] = std::from_chars(
2012 targetID.data(), targetID.data() + targetID.size(), idInt);
2013 if (ec == std::errc::invalid_argument)
2014 {
2015 messages::resourceMissingAtURI(asyncResp->res, targetID);
2016 return;
2017 }
2018 if (ec == std::errc::result_out_of_range)
2019 {
2020 messages::resourceMissingAtURI(asyncResp->res, targetID);
2021 return;
2022 }
2023
2024 std::vector<std::filesystem::path> hostLoggerFiles;
2025 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2026 {
2027 BMCWEB_LOG_ERROR << "fail to get host log file path";
2028 return;
2029 }
2030
2031 size_t logCount = 0;
2032 uint64_t top = 1;
2033 std::vector<std::string> logEntries;
2034 // We can get specific entry by skip and top. For example, if we
2035 // want to get nth entry, we can set skip = n-1 and top = 1 to
2036 // get that entry
2037 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2038 logEntries, logCount))
2039 {
2040 messages::internalError(asyncResp->res);
2041 return;
2042 }
2043
2044 if (!logEntries.empty())
2045 {
2046 fillHostLoggerEntryJson(targetID, logEntries[0],
2047 asyncResp->res.jsonValue);
2048 return;
2049 }
2050
2051 // Requested ID was not found
2052 messages::resourceMissingAtURI(asyncResp->res, targetID);
2053 });
2054}
2055
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002056inline void requestRoutesBMCLogServiceCollection(App& app)
2057{
2058 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002059 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002060 .methods(boost::beast::http::verb::get)(
2061 [](const crow::Request&,
2062 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2063 // Collections don't include the static data added by SubRoute
2064 // because it has a duplicate entry for members
2065 asyncResp->res.jsonValue["@odata.type"] =
2066 "#LogServiceCollection.LogServiceCollection";
2067 asyncResp->res.jsonValue["@odata.id"] =
2068 "/redfish/v1/Managers/bmc/LogServices";
2069 asyncResp->res.jsonValue["Name"] =
2070 "Open BMC Log Services Collection";
2071 asyncResp->res.jsonValue["Description"] =
2072 "Collection of LogServices for this Manager";
2073 nlohmann::json& logServiceArray =
2074 asyncResp->res.jsonValue["Members"];
2075 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002076#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002077 logServiceArray.push_back(
2078 {{"@odata.id",
2079 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002080#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002081#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002082 logServiceArray.push_back(
2083 {{"@odata.id",
2084 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002085#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002086 asyncResp->res.jsonValue["Members@odata.count"] =
2087 logServiceArray.size();
2088 });
2089}
Ed Tanous1da66f72018-07-27 16:13:37 -07002090
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002091inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002092{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002093 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002094 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002095 .methods(boost::beast::http::verb::get)(
2096 [](const crow::Request&,
2097 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07002098
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002099 {
2100 asyncResp->res.jsonValue["@odata.type"] =
2101 "#LogService.v1_1_0.LogService";
2102 asyncResp->res.jsonValue["@odata.id"] =
2103 "/redfish/v1/Managers/bmc/LogServices/Journal";
2104 asyncResp->res.jsonValue["Name"] =
2105 "Open BMC Journal Log Service";
2106 asyncResp->res.jsonValue["Description"] =
2107 "BMC Journal Log Service";
2108 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2109 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302110
2111 std::pair<std::string, std::string> redfishDateTimeOffset =
2112 crow::utility::getDateTimeOffsetNow();
2113 asyncResp->res.jsonValue["DateTime"] =
2114 redfishDateTimeOffset.first;
2115 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2116 redfishDateTimeOffset.second;
2117
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002118 asyncResp->res.jsonValue["Entries"] = {
2119 {"@odata.id",
2120 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
2121 });
2122}
Jason M. Billse1f26342018-07-18 12:12:00 -07002123
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002124static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2125 sd_journal* journal,
2126 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002127{
2128 // Get the Log Entry contents
2129 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002130
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002131 std::string message;
2132 std::string_view syslogID;
2133 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2134 if (ret < 0)
2135 {
2136 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2137 << strerror(-ret);
2138 }
2139 if (!syslogID.empty())
2140 {
2141 message += std::string(syslogID) + ": ";
2142 }
2143
Ed Tanous39e77502019-03-04 17:35:53 -08002144 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002145 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002146 if (ret < 0)
2147 {
2148 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2149 return 1;
2150 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002151 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002152
2153 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002154 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002155 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002156 if (ret < 0)
2157 {
2158 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002159 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002160
2161 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002162 std::string entryTimeStr;
2163 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002164 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002165 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002166 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002167
2168 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002169 bmcJournalLogEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08002170 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002171 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2172 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07002173 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002174 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002175 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07002176 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06002177 {"Severity", severity <= 2 ? "Critical"
2178 : severity <= 4 ? "Warning"
2179 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07002180 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07002181 {"Created", std::move(entryTimeStr)}};
2182 return 0;
2183}
2184
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002185inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002186{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002187 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002188 .privileges(redfish::privileges::getLogEntryCollection)
George Liu0fda0f12021-11-16 10:06:17 +08002189 .methods(
2190 boost::beast::http::verb::
2191 get)([](const crow::Request& req,
2192 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2193 static constexpr const long maxEntriesPerPage = 1000;
2194 uint64_t skip = 0;
2195 uint64_t top = maxEntriesPerPage; // Show max entries by default
2196 if (!getSkipParam(asyncResp, req, skip))
2197 {
2198 return;
2199 }
2200 if (!getTopParam(asyncResp, req, top))
2201 {
2202 return;
2203 }
2204 // Collections don't include the static data added by SubRoute
2205 // because it has a duplicate entry for members
2206 asyncResp->res.jsonValue["@odata.type"] =
2207 "#LogEntryCollection.LogEntryCollection";
2208 asyncResp->res.jsonValue["@odata.id"] =
2209 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2210 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2211 asyncResp->res.jsonValue["Description"] =
2212 "Collection of BMC Journal Entries";
2213 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2214 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002215
George Liu0fda0f12021-11-16 10:06:17 +08002216 // Go through the journal and use the timestamp to create a
2217 // unique ID for each entry
2218 sd_journal* journalTmp = nullptr;
2219 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2220 if (ret < 0)
2221 {
2222 BMCWEB_LOG_ERROR << "failed to open journal: "
2223 << strerror(-ret);
2224 messages::internalError(asyncResp->res);
2225 return;
2226 }
2227 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2228 journalTmp, sd_journal_close);
2229 journalTmp = nullptr;
2230 uint64_t entryCount = 0;
2231 // Reset the unique ID on the first entry
2232 bool firstEntry = true;
2233 SD_JOURNAL_FOREACH(journal.get())
2234 {
2235 entryCount++;
2236 // Handle paging using skip (number of entries to skip from
2237 // the start) and top (number of entries to display)
2238 if (entryCount <= skip || entryCount > skip + top)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002239 {
George Liu0fda0f12021-11-16 10:06:17 +08002240 continue;
2241 }
2242
2243 std::string idStr;
2244 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2245 {
2246 continue;
2247 }
2248
2249 if (firstEntry)
2250 {
2251 firstEntry = false;
2252 }
2253
2254 logEntryArray.push_back({});
2255 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2256 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2257 bmcJournalLogEntry) != 0)
2258 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002259 messages::internalError(asyncResp->res);
2260 return;
2261 }
George Liu0fda0f12021-11-16 10:06:17 +08002262 }
2263 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2264 if (skip + top < entryCount)
2265 {
2266 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2267 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2268 std::to_string(skip + top);
2269 }
2270 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002271}
Jason M. Billse1f26342018-07-18 12:12:00 -07002272
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002273inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002274{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002275 BMCWEB_ROUTE(app,
2276 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002277 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002278 .methods(boost::beast::http::verb::get)(
2279 [](const crow::Request&,
2280 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2281 const std::string& entryID) {
2282 // Convert the unique ID back to a timestamp to find the entry
2283 uint64_t ts = 0;
2284 uint64_t index = 0;
2285 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2286 {
2287 return;
2288 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002289
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002290 sd_journal* journalTmp = nullptr;
2291 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2292 if (ret < 0)
2293 {
2294 BMCWEB_LOG_ERROR << "failed to open journal: "
2295 << strerror(-ret);
2296 messages::internalError(asyncResp->res);
2297 return;
2298 }
2299 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2300 journal(journalTmp, sd_journal_close);
2301 journalTmp = nullptr;
2302 // Go to the timestamp in the log and move to the entry at the
2303 // index tracking the unique ID
2304 std::string idStr;
2305 bool firstEntry = true;
2306 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2307 if (ret < 0)
2308 {
2309 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2310 << strerror(-ret);
2311 messages::internalError(asyncResp->res);
2312 return;
2313 }
2314 for (uint64_t i = 0; i <= index; i++)
2315 {
2316 sd_journal_next(journal.get());
2317 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2318 {
2319 messages::internalError(asyncResp->res);
2320 return;
2321 }
2322 if (firstEntry)
2323 {
2324 firstEntry = false;
2325 }
2326 }
2327 // Confirm that the entry ID matches what was requested
2328 if (idStr != entryID)
2329 {
2330 messages::resourceMissingAtURI(asyncResp->res, entryID);
2331 return;
2332 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002333
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002334 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2335 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002336 {
2337 messages::internalError(asyncResp->res);
2338 return;
2339 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002340 });
2341}
2342
2343inline void requestRoutesBMCDumpService(App& app)
2344{
2345 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002346 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08002347 .methods(
2348 boost::beast::http::verb::
2349 get)([](const crow::Request&,
2350 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2351 asyncResp->res.jsonValue["@odata.id"] =
2352 "/redfish/v1/Managers/bmc/LogServices/Dump";
2353 asyncResp->res.jsonValue["@odata.type"] =
2354 "#LogService.v1_2_0.LogService";
2355 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2356 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2357 asyncResp->res.jsonValue["Id"] = "Dump";
2358 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302359
George Liu0fda0f12021-11-16 10:06:17 +08002360 std::pair<std::string, std::string> redfishDateTimeOffset =
2361 crow::utility::getDateTimeOffsetNow();
2362 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2363 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2364 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302365
George Liu0fda0f12021-11-16 10:06:17 +08002366 asyncResp->res.jsonValue["Entries"] = {
2367 {"@odata.id",
2368 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2369 asyncResp->res.jsonValue["Actions"] = {
2370 {"#LogService.ClearLog",
2371 {{"target",
2372 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2373 {"#LogService.CollectDiagnosticData",
2374 {{"target",
2375 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
2376 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002377}
2378
2379inline void requestRoutesBMCDumpEntryCollection(App& app)
2380{
2381
2382 /**
2383 * Functions triggers appropriate requests on DBus
2384 */
2385 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002386 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002387 .methods(boost::beast::http::verb::get)(
2388 [](const crow::Request&,
2389 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2390 asyncResp->res.jsonValue["@odata.type"] =
2391 "#LogEntryCollection.LogEntryCollection";
2392 asyncResp->res.jsonValue["@odata.id"] =
2393 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2394 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2395 asyncResp->res.jsonValue["Description"] =
2396 "Collection of BMC Dump Entries";
2397
2398 getDumpEntryCollection(asyncResp, "BMC");
2399 });
2400}
2401
2402inline void requestRoutesBMCDumpEntry(App& app)
2403{
2404 BMCWEB_ROUTE(app,
2405 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002406 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002407 .methods(boost::beast::http::verb::get)(
2408 [](const crow::Request&,
2409 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2410 const std::string& param) {
2411 getDumpEntryById(asyncResp, param, "BMC");
2412 });
2413 BMCWEB_ROUTE(app,
2414 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002415 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002416 .methods(boost::beast::http::verb::delete_)(
2417 [](const crow::Request&,
2418 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2419 const std::string& param) {
2420 deleteDumpEntry(asyncResp, param, "bmc");
2421 });
2422}
2423
2424inline void requestRoutesBMCDumpCreate(App& app)
2425{
2426
George Liu0fda0f12021-11-16 10:06:17 +08002427 BMCWEB_ROUTE(
2428 app,
2429 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002430 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002431 .methods(boost::beast::http::verb::post)(
2432 [](const crow::Request& req,
2433 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2434 createDump(asyncResp, req, "BMC");
2435 });
2436}
2437
2438inline void requestRoutesBMCDumpClear(App& app)
2439{
George Liu0fda0f12021-11-16 10:06:17 +08002440 BMCWEB_ROUTE(
2441 app,
2442 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002443 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002444 .methods(boost::beast::http::verb::post)(
2445 [](const crow::Request&,
2446 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2447 clearDump(asyncResp, "BMC");
2448 });
2449}
2450
2451inline void requestRoutesSystemDumpService(App& app)
2452{
2453 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002454 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002455 .methods(boost::beast::http::verb::get)(
2456 [](const crow::Request&,
2457 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2458
2459 {
2460 asyncResp->res.jsonValue["@odata.id"] =
2461 "/redfish/v1/Systems/system/LogServices/Dump";
2462 asyncResp->res.jsonValue["@odata.type"] =
2463 "#LogService.v1_2_0.LogService";
2464 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2465 asyncResp->res.jsonValue["Description"] =
2466 "System Dump LogService";
2467 asyncResp->res.jsonValue["Id"] = "Dump";
2468 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302469
2470 std::pair<std::string, std::string> redfishDateTimeOffset =
2471 crow::utility::getDateTimeOffsetNow();
2472 asyncResp->res.jsonValue["DateTime"] =
2473 redfishDateTimeOffset.first;
2474 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2475 redfishDateTimeOffset.second;
2476
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002477 asyncResp->res.jsonValue["Entries"] = {
2478 {"@odata.id",
2479 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2480 asyncResp->res.jsonValue["Actions"] = {
2481 {"#LogService.ClearLog",
2482 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002483 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002484 {"#LogService.CollectDiagnosticData",
2485 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002486 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002487 });
2488}
2489
2490inline void requestRoutesSystemDumpEntryCollection(App& app)
2491{
2492
2493 /**
2494 * Functions triggers appropriate requests on DBus
2495 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002496 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002497 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002498 .methods(boost::beast::http::verb::get)(
2499 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002500 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002501 asyncResp->res.jsonValue["@odata.type"] =
2502 "#LogEntryCollection.LogEntryCollection";
2503 asyncResp->res.jsonValue["@odata.id"] =
2504 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2505 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2506 asyncResp->res.jsonValue["Description"] =
2507 "Collection of System Dump Entries";
2508
2509 getDumpEntryCollection(asyncResp, "System");
2510 });
2511}
2512
2513inline void requestRoutesSystemDumpEntry(App& app)
2514{
2515 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002516 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002517 .privileges(redfish::privileges::getLogEntry)
2518
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002519 .methods(boost::beast::http::verb::get)(
2520 [](const crow::Request&,
2521 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2522 const std::string& param) {
2523 getDumpEntryById(asyncResp, param, "System");
2524 });
2525
2526 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002527 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002528 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002529 .methods(boost::beast::http::verb::delete_)(
2530 [](const crow::Request&,
2531 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2532 const std::string& param) {
2533 deleteDumpEntry(asyncResp, param, "system");
2534 });
2535}
2536
2537inline void requestRoutesSystemDumpCreate(App& app)
2538{
George Liu0fda0f12021-11-16 10:06:17 +08002539 BMCWEB_ROUTE(
2540 app,
2541 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002542 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002543 .methods(boost::beast::http::verb::post)(
2544 [](const crow::Request& req,
2545 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2546
2547 { createDump(asyncResp, req, "System"); });
2548}
2549
2550inline void requestRoutesSystemDumpClear(App& app)
2551{
George Liu0fda0f12021-11-16 10:06:17 +08002552 BMCWEB_ROUTE(
2553 app,
2554 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002555 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002556 .methods(boost::beast::http::verb::post)(
2557 [](const crow::Request&,
2558 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2559
2560 { clearDump(asyncResp, "System"); });
2561}
2562
2563inline void requestRoutesCrashdumpService(App& app)
2564{
2565 // Note: Deviated from redfish privilege registry for GET & HEAD
2566 // method for security reasons.
2567 /**
2568 * Functions triggers appropriate requests on DBus
2569 */
2570 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002571 // This is incorrect, should be:
2572 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002573 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002574 .methods(
2575 boost::beast::http::verb::
2576 get)([](const crow::Request&,
2577 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2578 // Copy over the static data to include the entries added by
2579 // SubRoute
2580 asyncResp->res.jsonValue["@odata.id"] =
2581 "/redfish/v1/Systems/system/LogServices/Crashdump";
2582 asyncResp->res.jsonValue["@odata.type"] =
2583 "#LogService.v1_2_0.LogService";
2584 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2585 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2586 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2587 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2588 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302589
2590 std::pair<std::string, std::string> redfishDateTimeOffset =
2591 crow::utility::getDateTimeOffsetNow();
2592 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2593 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2594 redfishDateTimeOffset.second;
2595
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002596 asyncResp->res.jsonValue["Entries"] = {
2597 {"@odata.id",
2598 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2599 asyncResp->res.jsonValue["Actions"] = {
2600 {"#LogService.ClearLog",
George Liu0fda0f12021-11-16 10:06:17 +08002601 {{"target",
2602 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002603 {"#LogService.CollectDiagnosticData",
George Liu0fda0f12021-11-16 10:06:17 +08002604 {{"target",
2605 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002606 });
2607}
2608
2609void inline requestRoutesCrashdumpClear(App& app)
2610{
George Liu0fda0f12021-11-16 10:06:17 +08002611 BMCWEB_ROUTE(
2612 app,
2613 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002614 // This is incorrect, should be:
2615 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002616 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002617 .methods(boost::beast::http::verb::post)(
2618 [](const crow::Request&,
2619 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2620 crow::connections::systemBus->async_method_call(
2621 [asyncResp](const boost::system::error_code ec,
2622 const std::string&) {
2623 if (ec)
2624 {
2625 messages::internalError(asyncResp->res);
2626 return;
2627 }
2628 messages::success(asyncResp->res);
2629 },
2630 crashdumpObject, crashdumpPath, deleteAllInterface,
2631 "DeleteAll");
2632 });
2633}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002634
zhanghch058d1b46d2021-04-01 11:18:24 +08002635static void
2636 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2637 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002638{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002639 auto getStoredLogCallback =
2640 [asyncResp, logID, &logEntryJson](
2641 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -08002642 const std::vector<
2643 std::pair<std::string, dbus::utility::DbusVariantType>>&
2644 params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002645 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002646 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002647 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2648 if (ec.value() ==
2649 boost::system::linux_error::bad_request_descriptor)
2650 {
2651 messages::resourceNotFound(asyncResp->res, "LogEntry",
2652 logID);
2653 }
2654 else
2655 {
2656 messages::internalError(asyncResp->res);
2657 }
2658 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002659 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002660
Johnathan Mantey043a0532020-03-10 17:15:28 -07002661 std::string timestamp{};
2662 std::string filename{};
2663 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002664 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002665
2666 if (filename.empty() || timestamp.empty())
2667 {
2668 messages::resourceMissingAtURI(asyncResp->res, logID);
2669 return;
2670 }
2671
2672 std::string crashdumpURI =
2673 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2674 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002675 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002676 {"@odata.id", "/redfish/v1/Systems/system/"
2677 "LogServices/Crashdump/Entries/" +
2678 logID},
2679 {"Name", "CPU Crashdump"},
2680 {"Id", logID},
2681 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002682 {"AdditionalDataURI", std::move(crashdumpURI)},
2683 {"DiagnosticDataType", "OEM"},
2684 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002685 {"Created", std::move(timestamp)}};
2686 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002687 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002688 std::move(getStoredLogCallback), crashdumpObject,
2689 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002690 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002691}
2692
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002693inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002694{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002695 // Note: Deviated from redfish privilege registry for GET & HEAD
2696 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002697 /**
2698 * Functions triggers appropriate requests on DBus
2699 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002700 BMCWEB_ROUTE(app,
2701 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002702 // This is incorrect, should be.
2703 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002704 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002705 .methods(
2706 boost::beast::http::verb::
2707 get)([](const crow::Request&,
2708 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2709 // Collections don't include the static data added by SubRoute
2710 // because it has a duplicate entry for members
2711 auto getLogEntriesCallback = [asyncResp](
2712 const boost::system::error_code ec,
2713 const std::vector<std::string>&
2714 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002715 if (ec)
2716 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002717 if (ec.value() !=
2718 boost::system::errc::no_such_file_or_directory)
2719 {
2720 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2721 << ec.message();
2722 messages::internalError(asyncResp->res);
2723 return;
2724 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002725 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002726 asyncResp->res.jsonValue["@odata.type"] =
2727 "#LogEntryCollection.LogEntryCollection";
2728 asyncResp->res.jsonValue["@odata.id"] =
2729 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2730 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2731 asyncResp->res.jsonValue["Description"] =
2732 "Collection of Crashdump Entries";
2733 nlohmann::json& logEntryArray =
2734 asyncResp->res.jsonValue["Members"];
2735 logEntryArray = nlohmann::json::array();
2736 std::vector<std::string> logIDs;
2737 // Get the list of log entries and build up an empty array big
2738 // enough to hold them
2739 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002740 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002741 // Get the log ID
2742 std::size_t lastPos = objpath.rfind('/');
2743 if (lastPos == std::string::npos)
2744 {
2745 continue;
2746 }
2747 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002748
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002749 // Add a space for the log entry to the array
2750 logEntryArray.push_back({});
2751 }
2752 // Now go through and set up async calls to fill in the entries
2753 size_t index = 0;
2754 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002755 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002756 // Add the log entry to the array
2757 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002758 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002759 asyncResp->res.jsonValue["Members@odata.count"] =
2760 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002761 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002762 crow::connections::systemBus->async_method_call(
2763 std::move(getLogEntriesCallback),
2764 "xyz.openbmc_project.ObjectMapper",
2765 "/xyz/openbmc_project/object_mapper",
2766 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2767 std::array<const char*, 1>{crashdumpInterface});
2768 });
2769}
Ed Tanous1da66f72018-07-27 16:13:37 -07002770
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002771inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002772{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002773 // Note: Deviated from redfish privilege registry for GET & HEAD
2774 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002775
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002776 BMCWEB_ROUTE(
2777 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002778 // this is incorrect, should be
2779 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002780 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002781 .methods(boost::beast::http::verb::get)(
2782 [](const crow::Request&,
2783 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2784 const std::string& param) {
2785 const std::string& logID = param;
2786 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2787 });
2788}
Ed Tanous1da66f72018-07-27 16:13:37 -07002789
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002790inline void requestRoutesCrashdumpFile(App& app)
2791{
2792 // Note: Deviated from redfish privilege registry for GET & HEAD
2793 // method for security reasons.
2794 BMCWEB_ROUTE(
2795 app,
2796 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002797 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002798 .methods(boost::beast::http::verb::get)(
2799 [](const crow::Request&,
2800 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2801 const std::string& logID, const std::string& fileName) {
2802 auto getStoredLogCallback =
2803 [asyncResp, logID, fileName](
2804 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -08002805 const std::vector<std::pair<
2806 std::string, dbus::utility::DbusVariantType>>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002807 resp) {
2808 if (ec)
2809 {
2810 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2811 << ec.message();
2812 messages::internalError(asyncResp->res);
2813 return;
2814 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002815
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002816 std::string dbusFilename{};
2817 std::string dbusTimestamp{};
2818 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002819
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002820 parseCrashdumpParameters(resp, dbusFilename,
2821 dbusTimestamp, dbusFilepath);
2822
2823 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2824 dbusFilepath.empty())
2825 {
2826 messages::resourceMissingAtURI(asyncResp->res,
2827 fileName);
2828 return;
2829 }
2830
2831 // Verify the file name parameter is correct
2832 if (fileName != dbusFilename)
2833 {
2834 messages::resourceMissingAtURI(asyncResp->res,
2835 fileName);
2836 return;
2837 }
2838
2839 if (!std::filesystem::exists(dbusFilepath))
2840 {
2841 messages::resourceMissingAtURI(asyncResp->res,
2842 fileName);
2843 return;
2844 }
2845 std::ifstream ifs(dbusFilepath, std::ios::in |
2846 std::ios::binary |
2847 std::ios::ate);
2848 std::ifstream::pos_type fileSize = ifs.tellg();
2849 if (fileSize < 0)
2850 {
2851 messages::generalError(asyncResp->res);
2852 return;
2853 }
2854 ifs.seekg(0, std::ios::beg);
2855
2856 auto crashData = std::make_unique<char[]>(
2857 static_cast<unsigned int>(fileSize));
2858
2859 ifs.read(crashData.get(), static_cast<int>(fileSize));
2860
2861 // The cast to std::string is intentional in order to
2862 // use the assign() that applies move mechanics
2863 asyncResp->res.body().assign(
2864 static_cast<std::string>(crashData.get()));
2865
2866 // Configure this to be a file download when accessed
2867 // from a browser
2868 asyncResp->res.addHeader("Content-Disposition",
2869 "attachment");
2870 };
2871 crow::connections::systemBus->async_method_call(
2872 std::move(getStoredLogCallback), crashdumpObject,
2873 crashdumpPath + std::string("/") + logID,
2874 "org.freedesktop.DBus.Properties", "GetAll",
2875 crashdumpInterface);
2876 });
2877}
2878
2879inline void requestRoutesCrashdumpCollect(App& app)
2880{
2881 // Note: Deviated from redfish privilege registry for GET & HEAD
2882 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08002883 BMCWEB_ROUTE(
2884 app,
2885 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002886 // The below is incorrect; Should be ConfigureManager
2887 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002888 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002889 .methods(
2890 boost::beast::http::verb::
2891 post)([](const crow::Request& req,
2892 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2893 std::string diagnosticDataType;
2894 std::string oemDiagnosticDataType;
2895 if (!redfish::json_util::readJson(
2896 req, asyncResp->res, "DiagnosticDataType",
2897 diagnosticDataType, "OEMDiagnosticDataType",
2898 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002899 {
James Feist46229572020-02-19 15:11:58 -08002900 return;
2901 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002902
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002903 if (diagnosticDataType != "OEM")
2904 {
2905 BMCWEB_LOG_ERROR
2906 << "Only OEM DiagnosticDataType supported for Crashdump";
2907 messages::actionParameterValueFormatError(
2908 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2909 "CollectDiagnosticData");
2910 return;
2911 }
2912
Ed Tanous98be3e32021-09-16 15:05:36 -07002913 auto collectCrashdumpCallback = [asyncResp,
2914 payload(task::Payload(req))](
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002915 const boost::system::error_code
2916 ec,
Ed Tanous98be3e32021-09-16 15:05:36 -07002917 const std::string&) mutable {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002918 if (ec)
2919 {
2920 if (ec.value() ==
2921 boost::system::errc::operation_not_supported)
2922 {
2923 messages::resourceInStandby(asyncResp->res);
2924 }
2925 else if (ec.value() ==
2926 boost::system::errc::device_or_resource_busy)
2927 {
2928 messages::serviceTemporarilyUnavailable(asyncResp->res,
2929 "60");
2930 }
2931 else
2932 {
2933 messages::internalError(asyncResp->res);
2934 }
2935 return;
2936 }
George Liu0fda0f12021-11-16 10:06:17 +08002937 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
2938 [](boost::system::error_code err,
2939 sdbusplus::message::message&,
2940 const std::shared_ptr<task::TaskData>& taskData) {
2941 if (!err)
2942 {
2943 taskData->messages.emplace_back(
2944 messages::taskCompletedOK(
2945 std::to_string(taskData->index)));
2946 taskData->state = "Completed";
2947 }
2948 return task::completed;
2949 },
2950 "type='signal',interface='org.freedesktop.DBus."
2951 "Properties',"
2952 "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002953 task->startTimer(std::chrono::minutes(5));
2954 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -07002955 task->payload.emplace(std::move(payload));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002956 };
2957
2958 if (oemDiagnosticDataType == "OnDemand")
2959 {
2960 crow::connections::systemBus->async_method_call(
2961 std::move(collectCrashdumpCallback), crashdumpObject,
2962 crashdumpPath, crashdumpOnDemandInterface,
2963 "GenerateOnDemandLog");
2964 }
2965 else if (oemDiagnosticDataType == "Telemetry")
2966 {
2967 crow::connections::systemBus->async_method_call(
2968 std::move(collectCrashdumpCallback), crashdumpObject,
2969 crashdumpPath, crashdumpTelemetryInterface,
2970 "GenerateTelemetryLog");
2971 }
2972 else
2973 {
2974 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2975 << oemDiagnosticDataType;
2976 messages::actionParameterValueFormatError(
2977 asyncResp->res, oemDiagnosticDataType,
2978 "OEMDiagnosticDataType", "CollectDiagnosticData");
2979 return;
2980 }
2981 });
2982}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002983
Andrew Geisslercb92c032018-08-17 07:56:14 -07002984/**
2985 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2986 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002987inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002988{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002989 /**
2990 * Function handles POST method request.
2991 * The Clear Log actions does not require any parameter.The action deletes
2992 * all entries found in the Entries collection for this Log Service.
2993 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002994
George Liu0fda0f12021-11-16 10:06:17 +08002995 BMCWEB_ROUTE(
2996 app,
2997 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002998 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002999 .methods(boost::beast::http::verb::post)(
3000 [](const crow::Request&,
3001 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3002 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003003
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003004 // Process response from Logging service.
3005 auto respHandler = [asyncResp](
3006 const boost::system::error_code ec) {
3007 BMCWEB_LOG_DEBUG
3008 << "doClearLog resp_handler callback: Done";
3009 if (ec)
3010 {
3011 // TODO Handle for specific error code
3012 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
3013 << ec;
3014 asyncResp->res.result(
3015 boost::beast::http::status::internal_server_error);
3016 return;
3017 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003018
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003019 asyncResp->res.result(
3020 boost::beast::http::status::no_content);
3021 };
3022
3023 // Make call to Logging service to request Clear Log
3024 crow::connections::systemBus->async_method_call(
3025 respHandler, "xyz.openbmc_project.Logging",
3026 "/xyz/openbmc_project/logging",
3027 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3028 });
3029}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003030
3031/****************************************************
3032 * Redfish PostCode interfaces
3033 * using DBUS interface: getPostCodesTS
3034 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003035inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003036{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003037 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003038 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08003039 .methods(
3040 boost::beast::http::verb::
3041 get)([](const crow::Request&,
3042 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3043 asyncResp->res.jsonValue = {
3044 {"@odata.id",
3045 "/redfish/v1/Systems/system/LogServices/PostCodes"},
3046 {"@odata.type", "#LogService.v1_1_0.LogService"},
3047 {"Name", "POST Code Log Service"},
3048 {"Description", "POST Code Log Service"},
3049 {"Id", "BIOS POST Code Log"},
3050 {"OverWritePolicy", "WrapsWhenFull"},
3051 {"Entries",
3052 {{"@odata.id",
3053 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05303054
George Liu0fda0f12021-11-16 10:06:17 +08003055 std::pair<std::string, std::string> redfishDateTimeOffset =
3056 crow::utility::getDateTimeOffsetNow();
3057 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3058 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3059 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303060
George Liu0fda0f12021-11-16 10:06:17 +08003061 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3062 {"target",
3063 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
3064 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003065}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003066
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003067inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003068{
George Liu0fda0f12021-11-16 10:06:17 +08003069 BMCWEB_ROUTE(
3070 app,
3071 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003072 // The following privilege is incorrect; It should be ConfigureManager
3073 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003074 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003075 .methods(boost::beast::http::verb::post)(
3076 [](const crow::Request&,
3077 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3078 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003079
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003080 // Make call to post-code service to request clear all
3081 crow::connections::systemBus->async_method_call(
3082 [asyncResp](const boost::system::error_code ec) {
3083 if (ec)
3084 {
3085 // TODO Handle for specific error code
3086 BMCWEB_LOG_ERROR
3087 << "doClearPostCodes resp_handler got error "
3088 << ec;
3089 asyncResp->res.result(boost::beast::http::status::
3090 internal_server_error);
3091 messages::internalError(asyncResp->res);
3092 return;
3093 }
3094 },
3095 "xyz.openbmc_project.State.Boot.PostCode0",
3096 "/xyz/openbmc_project/State/Boot/PostCode0",
3097 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3098 });
3099}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003100
3101static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003102 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303103 const boost::container::flat_map<
3104 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003105 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3106 const uint64_t skip = 0, const uint64_t top = 0)
3107{
3108 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003109 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05303110 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003111
3112 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003113 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003114
3115 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303116 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3117 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003118 {
3119 currentCodeIndex++;
3120 std::string postcodeEntryID =
3121 "B" + std::to_string(bootIndex) + "-" +
3122 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3123
3124 uint64_t usecSinceEpoch = code.first;
3125 uint64_t usTimeOffset = 0;
3126
3127 if (1 == currentCodeIndex)
3128 { // already incremented
3129 firstCodeTimeUs = code.first;
3130 }
3131 else
3132 {
3133 usTimeOffset = code.first - firstCodeTimeUs;
3134 }
3135
3136 // skip if no specific codeIndex is specified and currentCodeIndex does
3137 // not fall between top and skip
3138 if ((codeIndex == 0) &&
3139 (currentCodeIndex <= skip || currentCodeIndex > top))
3140 {
3141 continue;
3142 }
3143
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003144 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003145 // currentIndex
3146 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3147 {
3148 // This is done for simplicity. 1st entry is needed to calculate
3149 // time offset. To improve efficiency, one can get to the entry
3150 // directly (possibly with flatmap's nth method)
3151 continue;
3152 }
3153
3154 // currentCodeIndex is within top and skip or equal to specified code
3155 // index
3156
3157 // Get the Created time from the timestamp
3158 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003159 entryTimeStr =
3160 crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003161
3162 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3163 std::ostringstream hexCode;
3164 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303165 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003166 std::ostringstream timeOffsetStr;
3167 // Set Fixed -Point Notation
3168 timeOffsetStr << std::fixed;
3169 // Set precision to 4 digits
3170 timeOffsetStr << std::setprecision(4);
3171 // Add double to stream
3172 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3173 std::vector<std::string> messageArgs = {
3174 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3175
3176 // Get MessageArgs template from message registry
3177 std::string msg;
3178 if (message != nullptr)
3179 {
3180 msg = message->message;
3181
3182 // fill in this post code value
3183 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003184 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003185 {
3186 std::string argStr = "%" + std::to_string(++i);
3187 size_t argPos = msg.find(argStr);
3188 if (argPos != std::string::npos)
3189 {
3190 msg.replace(argPos, argStr.length(), messageArg);
3191 }
3192 }
3193 }
3194
Tim Leed4342a92020-04-27 11:47:58 +08003195 // Get Severity template from message registry
3196 std::string severity;
3197 if (message != nullptr)
3198 {
3199 severity = message->severity;
3200 }
3201
ZhikuiRena3316fc2020-01-29 14:58:08 -08003202 // add to AsyncResp
3203 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003204 nlohmann::json& bmcLogEntry = logEntryArray.back();
George Liu0fda0f12021-11-16 10:06:17 +08003205 bmcLogEntry = {
3206 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
3207 {"@odata.id",
3208 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3209 postcodeEntryID},
3210 {"Name", "POST Code Log Entry"},
3211 {"Id", postcodeEntryID},
3212 {"Message", std::move(msg)},
3213 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3214 {"MessageArgs", std::move(messageArgs)},
3215 {"EntryType", "Event"},
3216 {"Severity", std::move(severity)},
3217 {"Created", entryTimeStr}};
George Liu647b3cd2021-07-05 12:43:56 +08003218 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3219 {
3220 bmcLogEntry["AdditionalDataURI"] =
3221 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3222 postcodeEntryID + "/attachment";
3223 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003224 }
3225}
3226
zhanghch058d1b46d2021-04-01 11:18:24 +08003227static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003228 const uint16_t bootIndex,
3229 const uint64_t codeIndex)
3230{
3231 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303232 [aResp, bootIndex,
3233 codeIndex](const boost::system::error_code ec,
3234 const boost::container::flat_map<
3235 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3236 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003237 if (ec)
3238 {
3239 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3240 messages::internalError(aResp->res);
3241 return;
3242 }
3243
3244 // skip the empty postcode boots
3245 if (postcode.empty())
3246 {
3247 return;
3248 }
3249
3250 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3251
3252 aResp->res.jsonValue["Members@odata.count"] =
3253 aResp->res.jsonValue["Members"].size();
3254 },
Jonathan Doman15124762021-01-07 17:54:17 -08003255 "xyz.openbmc_project.State.Boot.PostCode0",
3256 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003257 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3258 bootIndex);
3259}
3260
zhanghch058d1b46d2021-04-01 11:18:24 +08003261static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003262 const uint16_t bootIndex,
3263 const uint16_t bootCount,
3264 const uint64_t entryCount, const uint64_t skip,
3265 const uint64_t top)
3266{
3267 crow::connections::systemBus->async_method_call(
3268 [aResp, bootIndex, bootCount, entryCount, skip,
3269 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303270 const boost::container::flat_map<
3271 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3272 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003273 if (ec)
3274 {
3275 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3276 messages::internalError(aResp->res);
3277 return;
3278 }
3279
3280 uint64_t endCount = entryCount;
3281 if (!postcode.empty())
3282 {
3283 endCount = entryCount + postcode.size();
3284
3285 if ((skip < endCount) && ((top + skip) > entryCount))
3286 {
3287 uint64_t thisBootSkip =
3288 std::max(skip, entryCount) - entryCount;
3289 uint64_t thisBootTop =
3290 std::min(top + skip, endCount) - entryCount;
3291
3292 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3293 thisBootSkip, thisBootTop);
3294 }
3295 aResp->res.jsonValue["Members@odata.count"] = endCount;
3296 }
3297
3298 // continue to previous bootIndex
3299 if (bootIndex < bootCount)
3300 {
3301 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3302 bootCount, endCount, skip, top);
3303 }
3304 else
3305 {
3306 aResp->res.jsonValue["Members@odata.nextLink"] =
George Liu0fda0f12021-11-16 10:06:17 +08003307 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
ZhikuiRena3316fc2020-01-29 14:58:08 -08003308 std::to_string(skip + top);
3309 }
3310 },
Jonathan Doman15124762021-01-07 17:54:17 -08003311 "xyz.openbmc_project.State.Boot.PostCode0",
3312 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003313 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3314 bootIndex);
3315}
3316
zhanghch058d1b46d2021-04-01 11:18:24 +08003317static void
3318 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3319 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003320{
3321 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003322 sdbusplus::asio::getProperty<uint16_t>(
3323 *crow::connections::systemBus,
3324 "xyz.openbmc_project.State.Boot.PostCode0",
3325 "/xyz/openbmc_project/State/Boot/PostCode0",
3326 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3327 [aResp, entryCount, skip, top](const boost::system::error_code ec,
3328 const uint16_t bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003329 if (ec)
3330 {
3331 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3332 messages::internalError(aResp->res);
3333 return;
3334 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003335 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
3336 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003337}
3338
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003339inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003340{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003341 BMCWEB_ROUTE(app,
3342 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003343 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003344 .methods(boost::beast::http::verb::get)(
3345 [](const crow::Request& req,
3346 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3347 asyncResp->res.jsonValue["@odata.type"] =
3348 "#LogEntryCollection.LogEntryCollection";
3349 asyncResp->res.jsonValue["@odata.id"] =
3350 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3351 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3352 asyncResp->res.jsonValue["Description"] =
3353 "Collection of POST Code Log Entries";
3354 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3355 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003356
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003357 uint64_t skip = 0;
3358 uint64_t top = maxEntriesPerPage; // Show max entries by default
3359 if (!getSkipParam(asyncResp, req, skip))
3360 {
3361 return;
3362 }
3363 if (!getTopParam(asyncResp, req, top))
3364 {
3365 return;
3366 }
3367 getCurrentBootNumber(asyncResp, skip, top);
3368 });
3369}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003370
George Liu647b3cd2021-07-05 12:43:56 +08003371/**
3372 * @brief Parse post code ID and get the current value and index value
3373 * eg: postCodeID=B1-2, currentValue=1, index=2
3374 *
3375 * @param[in] postCodeID Post Code ID
3376 * @param[out] currentValue Current value
3377 * @param[out] index Index value
3378 *
3379 * @return bool true if the parsing is successful, false the parsing fails
3380 */
3381inline static bool parsePostCode(const std::string& postCodeID,
3382 uint64_t& currentValue, uint16_t& index)
3383{
3384 std::vector<std::string> split;
3385 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3386 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3387 {
3388 return false;
3389 }
3390
3391 const char* start = split[0].data() + 1;
3392 const char* end = split[0].data() + split[0].size();
3393 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3394
3395 if (ptrIndex != end || ecIndex != std::errc())
3396 {
3397 return false;
3398 }
3399
3400 start = split[1].data();
3401 end = split[1].data() + split[1].size();
3402 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3403 if (ptrValue != end || ecValue != std::errc())
3404 {
3405 return false;
3406 }
3407
3408 return true;
3409}
3410
3411inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3412{
George Liu0fda0f12021-11-16 10:06:17 +08003413 BMCWEB_ROUTE(
3414 app,
3415 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003416 .privileges(redfish::privileges::getLogEntry)
3417 .methods(boost::beast::http::verb::get)(
3418 [](const crow::Request& req,
3419 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3420 const std::string& postCodeID) {
3421 if (!http_helpers::isOctetAccepted(
3422 req.getHeaderValue("Accept")))
3423 {
3424 asyncResp->res.result(
3425 boost::beast::http::status::bad_request);
3426 return;
3427 }
3428
3429 uint64_t currentValue = 0;
3430 uint16_t index = 0;
3431 if (!parsePostCode(postCodeID, currentValue, index))
3432 {
3433 messages::resourceNotFound(asyncResp->res, "LogEntry",
3434 postCodeID);
3435 return;
3436 }
3437
3438 crow::connections::systemBus->async_method_call(
3439 [asyncResp, postCodeID, currentValue](
3440 const boost::system::error_code ec,
3441 const std::vector<std::tuple<
3442 uint64_t, std::vector<uint8_t>>>& postcodes) {
3443 if (ec.value() == EBADR)
3444 {
3445 messages::resourceNotFound(asyncResp->res,
3446 "LogEntry", postCodeID);
3447 return;
3448 }
3449 if (ec)
3450 {
3451 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3452 messages::internalError(asyncResp->res);
3453 return;
3454 }
3455
3456 size_t value = static_cast<size_t>(currentValue) - 1;
3457 if (value == std::string::npos ||
3458 postcodes.size() < currentValue)
3459 {
3460 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3461 messages::resourceNotFound(asyncResp->res,
3462 "LogEntry", postCodeID);
3463 return;
3464 }
3465
3466 auto& [tID, code] = postcodes[value];
3467 if (code.empty())
3468 {
3469 BMCWEB_LOG_INFO << "No found post code data";
3470 messages::resourceNotFound(asyncResp->res,
3471 "LogEntry", postCodeID);
3472 return;
3473 }
3474
3475 std::string_view strData(
3476 reinterpret_cast<const char*>(code.data()),
3477 code.size());
3478
3479 asyncResp->res.addHeader("Content-Type",
3480 "application/octet-stream");
3481 asyncResp->res.addHeader("Content-Transfer-Encoding",
3482 "Base64");
3483 asyncResp->res.body() =
3484 crow::utility::base64encode(strData);
3485 },
3486 "xyz.openbmc_project.State.Boot.PostCode0",
3487 "/xyz/openbmc_project/State/Boot/PostCode0",
3488 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3489 index);
3490 });
3491}
3492
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003493inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003494{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003495 BMCWEB_ROUTE(
3496 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003497 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003498 .methods(boost::beast::http::verb::get)(
3499 [](const crow::Request&,
3500 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3501 const std::string& targetID) {
George Liu647b3cd2021-07-05 12:43:56 +08003502 uint16_t bootIndex = 0;
3503 uint64_t codeIndex = 0;
3504 if (!parsePostCode(targetID, codeIndex, bootIndex))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003505 {
3506 // Requested ID was not found
3507 messages::resourceMissingAtURI(asyncResp->res, targetID);
3508 return;
3509 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003510 if (bootIndex == 0 || codeIndex == 0)
3511 {
3512 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3513 << targetID;
3514 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003515
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003516 asyncResp->res.jsonValue["@odata.type"] =
3517 "#LogEntry.v1_4_0.LogEntry";
3518 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08003519 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003520 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3521 asyncResp->res.jsonValue["Description"] =
3522 "Collection of POST Code Log Entries";
3523 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3524 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003525
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003526 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3527 });
3528}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003529
Ed Tanous1da66f72018-07-27 16:13:37 -07003530} // namespace redfish