blob: 3877541338600ab37e11f2bb2ac5acdf2aa9c5d5 [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
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500109inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700110{
Ed Tanousd4d25792020-09-29 15:15:03 -0700111 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
112 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
113 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
114 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700115 {
116 return "Critical";
117 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700118 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
119 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
120 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700121 {
122 return "OK";
123 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700124 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700125 {
126 return "Warning";
127 }
128 return "";
129}
130
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700131inline static int getJournalMetadata(sd_journal* journal,
132 const std::string_view& field,
133 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700134{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500135 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700136 size_t length = 0;
137 int ret = 0;
138 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800139 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
140 const void** dataVoid = reinterpret_cast<const void**>(&data);
141
142 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700143 if (ret < 0)
144 {
145 return ret;
146 }
Ed Tanous39e77502019-03-04 17:35:53 -0800147 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700148 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000149 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700150 return ret;
151}
152
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700153inline static int getJournalMetadata(sd_journal* journal,
154 const std::string_view& field,
155 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700156{
157 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800158 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700159 // Get the metadata from the requested field of the journal entry
160 ret = getJournalMetadata(journal, field, metadata);
161 if (ret < 0)
162 {
163 return ret;
164 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000165 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700166 return ret;
167}
168
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700169inline static bool getEntryTimestamp(sd_journal* journal,
170 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800171{
172 int ret = 0;
173 uint64_t timestamp = 0;
174 ret = sd_journal_get_realtime_usec(journal, &timestamp);
175 if (ret < 0)
176 {
177 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
178 << strerror(-ret);
179 return false;
180 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800181 entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500182 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800183}
184
zhanghch058d1b46d2021-04-01 11:18:24 +0800185static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
186 const crow::Request& req, uint64_t& skip)
Jason M. Bills16428a12018-11-02 12:42:29 -0700187{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700188 boost::urls::query_params_view::iterator it = req.urlParams.find("$skip");
James Feist5a7e8772020-07-22 09:08:38 -0700189 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700190 {
James Feist5a7e8772020-07-22 09:08:38 -0700191 std::string skipParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500192 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700193 skip = std::strtoul(skipParam.c_str(), &ptr, 10);
194 if (skipParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700195 {
196
zhanghch058d1b46d2021-04-01 11:18:24 +0800197 messages::queryParameterValueTypeError(
198 asyncResp->res, std::string(skipParam), "$skip");
Jason M. Bills16428a12018-11-02 12:42:29 -0700199 return false;
200 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700201 }
202 return true;
203}
204
Ed Tanous271584a2019-07-09 16:24:22 -0700205static constexpr const uint64_t maxEntriesPerPage = 1000;
zhanghch058d1b46d2021-04-01 11:18:24 +0800206static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
207 const crow::Request& req, uint64_t& top)
Jason M. Bills16428a12018-11-02 12:42:29 -0700208{
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700209 boost::urls::query_params_view::iterator it = req.urlParams.find("$top");
James Feist5a7e8772020-07-22 09:08:38 -0700210 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700211 {
James Feist5a7e8772020-07-22 09:08:38 -0700212 std::string topParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500213 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700214 top = std::strtoul(topParam.c_str(), &ptr, 10);
215 if (topParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700216 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800217 messages::queryParameterValueTypeError(
218 asyncResp->res, std::string(topParam), "$top");
Jason M. Bills16428a12018-11-02 12:42:29 -0700219 return false;
220 }
Ed Tanous271584a2019-07-09 16:24:22 -0700221 if (top < 1U || top > maxEntriesPerPage)
Jason M. Bills16428a12018-11-02 12:42:29 -0700222 {
223
224 messages::queryParameterOutOfRange(
zhanghch058d1b46d2021-04-01 11:18:24 +0800225 asyncResp->res, std::to_string(top), "$top",
Jason M. Bills16428a12018-11-02 12:42:29 -0700226 "1-" + std::to_string(maxEntriesPerPage));
227 return false;
228 }
229 }
230 return true;
231}
232
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700233inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
234 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700235{
236 int ret = 0;
237 static uint64_t prevTs = 0;
238 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700239 if (firstEntry)
240 {
241 prevTs = 0;
242 }
243
Jason M. Bills16428a12018-11-02 12:42:29 -0700244 // Get the entry timestamp
245 uint64_t curTs = 0;
246 ret = sd_journal_get_realtime_usec(journal, &curTs);
247 if (ret < 0)
248 {
249 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
250 << strerror(-ret);
251 return false;
252 }
253 // If the timestamp isn't unique, increment the index
254 if (curTs == prevTs)
255 {
256 index++;
257 }
258 else
259 {
260 // Otherwise, reset it
261 index = 0;
262 }
263 // Save the timestamp
264 prevTs = curTs;
265
266 entryID = std::to_string(curTs);
267 if (index > 0)
268 {
269 entryID += "_" + std::to_string(index);
270 }
271 return true;
272}
273
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500274static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700275 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700276{
Ed Tanous271584a2019-07-09 16:24:22 -0700277 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700278 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700279 if (firstEntry)
280 {
281 prevTs = 0;
282 }
283
Jason M. Bills95820182019-04-22 16:25:34 -0700284 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700285 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700286 std::tm timeStruct = {};
287 std::istringstream entryStream(logEntry);
288 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
289 {
290 curTs = std::mktime(&timeStruct);
291 }
292 // If the timestamp isn't unique, increment the index
293 if (curTs == prevTs)
294 {
295 index++;
296 }
297 else
298 {
299 // Otherwise, reset it
300 index = 0;
301 }
302 // Save the timestamp
303 prevTs = curTs;
304
305 entryID = std::to_string(curTs);
306 if (index > 0)
307 {
308 entryID += "_" + std::to_string(index);
309 }
310 return true;
311}
312
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700313inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800314 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
315 const std::string& entryID, uint64_t& timestamp,
316 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700317{
318 if (entryID.empty())
319 {
320 return false;
321 }
322 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800323 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700324
Ed Tanous81ce6092020-12-17 16:54:55 +0000325 auto underscorePos = tsStr.find('_');
Jason M. Bills16428a12018-11-02 12:42:29 -0700326 if (underscorePos != tsStr.npos)
327 {
328 // Timestamp has an index
329 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800330 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700331 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700332 auto [ptr, ec] = std::from_chars(
333 indexStr.data(), indexStr.data() + indexStr.size(), index);
334 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700335 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800336 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700337 return false;
338 }
339 }
340 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700341 auto [ptr, ec] =
342 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
343 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700344 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800345 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700346 return false;
347 }
348 return true;
349}
350
Jason M. Bills95820182019-04-22 16:25:34 -0700351static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500352 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700353{
354 static const std::filesystem::path redfishLogDir = "/var/log";
355 static const std::string redfishLogFilename = "redfish";
356
357 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500358 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700359 std::filesystem::directory_iterator(redfishLogDir))
360 {
361 // If we find a redfish log file, save the path
362 std::string filename = dirEnt.path().filename();
363 if (boost::starts_with(filename, redfishLogFilename))
364 {
365 redfishLogFiles.emplace_back(redfishLogDir / filename);
366 }
367 }
368 // As the log files rotate, they are appended with a ".#" that is higher for
369 // the older logs. Since we don't expect more than 10 log files, we
370 // can just sort the list to get them in order from newest to oldest
371 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
372
373 return !redfishLogFiles.empty();
374}
375
zhanghch058d1b46d2021-04-01 11:18:24 +0800376inline void
377 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
378 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500379{
380 std::string dumpPath;
381 if (dumpType == "BMC")
382 {
383 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
384 }
385 else if (dumpType == "System")
386 {
387 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
388 }
389 else
390 {
391 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
392 messages::internalError(asyncResp->res);
393 return;
394 }
395
396 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -0800397 [asyncResp, dumpPath,
398 dumpType](const boost::system::error_code ec,
399 dbus::utility::ManagedObjectType& resp) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500400 if (ec)
401 {
402 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
403 messages::internalError(asyncResp->res);
404 return;
405 }
406
407 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
408 entriesArray = nlohmann::json::array();
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500409 std::string dumpEntryPath =
410 "/xyz/openbmc_project/dump/" +
411 std::string(boost::algorithm::to_lower_copy(dumpType)) +
412 "/entry/";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500413
414 for (auto& object : resp)
415 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500416 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500417 {
418 continue;
419 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800420 uint64_t timestamp = 0;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500421 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500422 std::string dumpStatus;
423 nlohmann::json thisEntry;
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000424
425 std::string entryID = object.first.filename();
426 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500427 {
428 continue;
429 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500430
431 for (auto& interfaceMap : object.second)
432 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500433 if (interfaceMap.first ==
434 "xyz.openbmc_project.Common.Progress")
435 {
436 for (auto& propertyMap : interfaceMap.second)
437 {
438 if (propertyMap.first == "Status")
439 {
440 auto status = std::get_if<std::string>(
441 &propertyMap.second);
442 if (status == nullptr)
443 {
444 messages::internalError(asyncResp->res);
445 break;
446 }
447 dumpStatus = *status;
448 }
449 }
450 }
451 else if (interfaceMap.first ==
452 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500453 {
454
455 for (auto& propertyMap : interfaceMap.second)
456 {
457 if (propertyMap.first == "Size")
458 {
459 auto sizePtr =
460 std::get_if<uint64_t>(&propertyMap.second);
461 if (sizePtr == nullptr)
462 {
463 messages::internalError(asyncResp->res);
464 break;
465 }
466 size = *sizePtr;
467 break;
468 }
469 }
470 }
471 else if (interfaceMap.first ==
472 "xyz.openbmc_project.Time.EpochTime")
473 {
474
475 for (auto& propertyMap : interfaceMap.second)
476 {
477 if (propertyMap.first == "Elapsed")
478 {
479 const uint64_t* usecsTimeStamp =
480 std::get_if<uint64_t>(&propertyMap.second);
481 if (usecsTimeStamp == nullptr)
482 {
483 messages::internalError(asyncResp->res);
484 break;
485 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800486 timestamp = (*usecsTimeStamp / 1000 / 1000);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500487 break;
488 }
489 }
490 }
491 }
492
George Liu0fda0f12021-11-16 10:06:17 +0800493 if (dumpStatus !=
494 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500495 !dumpStatus.empty())
496 {
497 // Dump status is not Complete, no need to enumerate
498 continue;
499 }
500
George Liu647b3cd2021-07-05 12:43:56 +0800501 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500502 thisEntry["@odata.id"] = dumpPath + entryID;
503 thisEntry["Id"] = entryID;
504 thisEntry["EntryType"] = "Event";
Nan Zhou1d8782e2021-11-29 22:23:18 -0800505 thisEntry["Created"] =
506 crow::utility::getDateTimeUint(timestamp);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500507 thisEntry["Name"] = dumpType + " Dump Entry";
508
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500509 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500510
511 if (dumpType == "BMC")
512 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500513 thisEntry["DiagnosticDataType"] = "Manager";
514 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500515 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
516 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500517 }
518 else if (dumpType == "System")
519 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500520 thisEntry["DiagnosticDataType"] = "OEM";
521 thisEntry["OEMDiagnosticDataType"] = "System";
522 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500523 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
524 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500525 }
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500526 entriesArray.push_back(std::move(thisEntry));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500527 }
528 asyncResp->res.jsonValue["Members@odata.count"] =
529 entriesArray.size();
530 },
531 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
532 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
533}
534
zhanghch058d1b46d2021-04-01 11:18:24 +0800535inline void
536 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
537 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500538{
539 std::string dumpPath;
540 if (dumpType == "BMC")
541 {
542 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
543 }
544 else if (dumpType == "System")
545 {
546 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
547 }
548 else
549 {
550 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
551 messages::internalError(asyncResp->res);
552 return;
553 }
554
555 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -0800556 [asyncResp, entryID, dumpPath,
557 dumpType](const boost::system::error_code ec,
558 dbus::utility::ManagedObjectType& resp) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500559 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,
Ed Tanous914e2d52022-01-07 11:38:34 -08001368 const dbus::utility::ManagedObjectType& 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 {
Ed Tanous914e2d52022-01-07 11:38:34 -08001383 const uint32_t* id = nullptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001384 std::time_t timestamp{};
1385 std::time_t updateTimestamp{};
Ed Tanous914e2d52022-01-07 11:38:34 -08001386 const std::string* severity = nullptr;
1387 const std::string* message = nullptr;
1388 const std::string* filePath = nullptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001389 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 {
Ed Tanous914e2d52022-01-07 11:38:34 -08001439 const bool* resolveptr =
1440 std::get_if<bool>(
1441 &propertyMap.second);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001442 if (resolveptr == nullptr)
1443 {
1444 messages::internalError(
1445 asyncResp->res);
1446 return;
1447 }
1448 resolved = *resolveptr;
1449 }
1450 }
1451 if (id == nullptr || message == nullptr ||
1452 severity == nullptr)
1453 {
1454 messages::internalError(asyncResp->res);
1455 return;
1456 }
1457 }
1458 else if (interfaceMap.first ==
1459 "xyz.openbmc_project.Common.FilePath")
1460 {
1461 for (auto& propertyMap : interfaceMap.second)
1462 {
1463 if (propertyMap.first == "Path")
1464 {
1465 filePath = std::get_if<std::string>(
1466 &propertyMap.second);
1467 }
1468 }
1469 }
1470 }
1471 // Object path without the
1472 // xyz.openbmc_project.Logging.Entry interface, ignore
1473 // and continue.
1474 if (id == nullptr || message == nullptr ||
1475 severity == nullptr)
1476 {
1477 continue;
1478 }
1479 entriesArray.push_back({});
1480 nlohmann::json& thisEntry = entriesArray.back();
1481 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1482 thisEntry["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001483 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001484 std::to_string(*id);
1485 thisEntry["Name"] = "System Event Log Entry";
1486 thisEntry["Id"] = std::to_string(*id);
1487 thisEntry["Message"] = *message;
1488 thisEntry["Resolved"] = resolved;
1489 thisEntry["EntryType"] = "Event";
1490 thisEntry["Severity"] =
1491 translateSeverityDbusToRedfish(*severity);
1492 thisEntry["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001493 crow::utility::getDateTimeStdtime(timestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001494 thisEntry["Modified"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001495 crow::utility::getDateTimeStdtime(updateTimestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001496 if (filePath != nullptr)
1497 {
1498 thisEntry["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001499 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001500 std::to_string(*id) + "/attachment";
1501 }
1502 }
1503 std::sort(entriesArray.begin(), entriesArray.end(),
1504 [](const nlohmann::json& left,
1505 const nlohmann::json& right) {
1506 return (left["Id"] <= right["Id"]);
1507 });
1508 asyncResp->res.jsonValue["Members@odata.count"] =
1509 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001510 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001511 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1512 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1513 });
1514}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001515
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001516inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001517{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001518 BMCWEB_ROUTE(
1519 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001520 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001521 .methods(boost::beast::http::verb::get)(
1522 [](const crow::Request&,
1523 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1524 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001525
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001526 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001527 std::string entryID = param;
1528 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001529
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001530 // DBus implementation of EventLog/Entries
1531 // Make call to Logging Service to find all log entry objects
1532 crow::connections::systemBus->async_method_call(
1533 [asyncResp, entryID](const boost::system::error_code ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001534 const GetManagedPropertyType& resp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001535 if (ec.value() == EBADR)
1536 {
1537 messages::resourceNotFound(
1538 asyncResp->res, "EventLogEntry", entryID);
1539 return;
1540 }
1541 if (ec)
1542 {
George Liu0fda0f12021-11-16 10:06:17 +08001543 BMCWEB_LOG_ERROR
1544 << "EventLogEntry (DBus) resp_handler got error "
1545 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001546 messages::internalError(asyncResp->res);
1547 return;
1548 }
Ed Tanous914e2d52022-01-07 11:38:34 -08001549 const uint32_t* id = nullptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001550 std::time_t timestamp{};
1551 std::time_t updateTimestamp{};
Ed Tanous914e2d52022-01-07 11:38:34 -08001552 const std::string* severity = nullptr;
1553 const std::string* message = nullptr;
1554 const std::string* filePath = nullptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001555 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001556
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001557 for (auto& propertyMap : resp)
1558 {
1559 if (propertyMap.first == "Id")
1560 {
1561 id = std::get_if<uint32_t>(&propertyMap.second);
1562 }
1563 else if (propertyMap.first == "Timestamp")
1564 {
1565 const uint64_t* millisTimeStamp =
1566 std::get_if<uint64_t>(&propertyMap.second);
1567 if (millisTimeStamp != nullptr)
1568 {
1569 timestamp = crow::utility::getTimestamp(
1570 *millisTimeStamp);
1571 }
1572 }
1573 else if (propertyMap.first == "UpdateTimestamp")
1574 {
1575 const uint64_t* millisTimeStamp =
1576 std::get_if<uint64_t>(&propertyMap.second);
1577 if (millisTimeStamp != nullptr)
1578 {
1579 updateTimestamp =
1580 crow::utility::getTimestamp(
1581 *millisTimeStamp);
1582 }
1583 }
1584 else if (propertyMap.first == "Severity")
1585 {
1586 severity = std::get_if<std::string>(
1587 &propertyMap.second);
1588 }
1589 else if (propertyMap.first == "Message")
1590 {
1591 message = std::get_if<std::string>(
1592 &propertyMap.second);
1593 }
1594 else if (propertyMap.first == "Resolved")
1595 {
Ed Tanous914e2d52022-01-07 11:38:34 -08001596 const bool* resolveptr =
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001597 std::get_if<bool>(&propertyMap.second);
1598 if (resolveptr == nullptr)
1599 {
1600 messages::internalError(asyncResp->res);
1601 return;
1602 }
1603 resolved = *resolveptr;
1604 }
1605 else if (propertyMap.first == "Path")
1606 {
1607 filePath = std::get_if<std::string>(
1608 &propertyMap.second);
1609 }
1610 }
1611 if (id == nullptr || message == nullptr ||
1612 severity == nullptr)
1613 {
1614 messages::internalError(asyncResp->res);
1615 return;
1616 }
1617 asyncResp->res.jsonValue["@odata.type"] =
1618 "#LogEntry.v1_8_0.LogEntry";
1619 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001620 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001621 std::to_string(*id);
1622 asyncResp->res.jsonValue["Name"] =
1623 "System Event Log Entry";
1624 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1625 asyncResp->res.jsonValue["Message"] = *message;
1626 asyncResp->res.jsonValue["Resolved"] = resolved;
1627 asyncResp->res.jsonValue["EntryType"] = "Event";
1628 asyncResp->res.jsonValue["Severity"] =
1629 translateSeverityDbusToRedfish(*severity);
1630 asyncResp->res.jsonValue["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001631 crow::utility::getDateTimeStdtime(timestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001632 asyncResp->res.jsonValue["Modified"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -08001633 crow::utility::getDateTimeStdtime(updateTimestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001634 if (filePath != nullptr)
1635 {
1636 asyncResp->res.jsonValue["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001637 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001638 std::to_string(*id);
1639 }
1640 },
1641 "xyz.openbmc_project.Logging",
1642 "/xyz/openbmc_project/logging/entry/" + entryID,
1643 "org.freedesktop.DBus.Properties", "GetAll", "");
1644 });
1645
1646 BMCWEB_ROUTE(
1647 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001648 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001649 .methods(boost::beast::http::verb::patch)(
1650 [](const crow::Request& req,
1651 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1652 const std::string& entryId) {
1653 std::optional<bool> resolved;
1654
1655 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1656 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001657 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001658 return;
1659 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001660 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001661
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001662 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001663 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001664 if (ec)
1665 {
1666 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1667 messages::internalError(asyncResp->res);
1668 return;
1669 }
1670 },
1671 "xyz.openbmc_project.Logging",
1672 "/xyz/openbmc_project/logging/entry/" + entryId,
1673 "org.freedesktop.DBus.Properties", "Set",
1674 "xyz.openbmc_project.Logging.Entry", "Resolved",
Ed Tanous168e20c2021-12-13 14:39:53 -08001675 dbus::utility::DbusVariantType(*resolved));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001676 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001677
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001678 BMCWEB_ROUTE(
1679 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001680 .privileges(redfish::privileges::deleteLogEntry)
1681
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001682 .methods(boost::beast::http::verb::delete_)(
1683 [](const crow::Request&,
1684 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1685 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001686
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001687 {
1688 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001689
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001690 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001691
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001692 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001693
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001694 // Process response from Logging service.
1695 auto respHandler = [asyncResp, entryID](
1696 const boost::system::error_code ec) {
1697 BMCWEB_LOG_DEBUG
1698 << "EventLogEntry (DBus) doDelete callback: Done";
1699 if (ec)
1700 {
1701 if (ec.value() == EBADR)
1702 {
1703 messages::resourceNotFound(asyncResp->res,
1704 "LogEntry", entryID);
1705 return;
1706 }
1707 // TODO Handle for specific error code
George Liu0fda0f12021-11-16 10:06:17 +08001708 BMCWEB_LOG_ERROR
1709 << "EventLogEntry (DBus) doDelete respHandler got error "
1710 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001711 asyncResp->res.result(
1712 boost::beast::http::status::internal_server_error);
1713 return;
1714 }
1715
1716 asyncResp->res.result(boost::beast::http::status::ok);
1717 };
1718
1719 // Make call to Logging service to request Delete Log
1720 crow::connections::systemBus->async_method_call(
1721 respHandler, "xyz.openbmc_project.Logging",
1722 "/xyz/openbmc_project/logging/entry/" + entryID,
1723 "xyz.openbmc_project.Object.Delete", "Delete");
1724 });
1725}
1726
1727inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001728{
George Liu0fda0f12021-11-16 10:06:17 +08001729 BMCWEB_ROUTE(
1730 app,
1731 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001732 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001733 .methods(boost::beast::http::verb::get)(
1734 [](const crow::Request& req,
1735 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1736 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001737
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001738 {
George Liu647b3cd2021-07-05 12:43:56 +08001739 if (!http_helpers::isOctetAccepted(
1740 req.getHeaderValue("Accept")))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001741 {
1742 asyncResp->res.result(
1743 boost::beast::http::status::bad_request);
1744 return;
1745 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001746
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001747 std::string entryID = param;
1748 dbus::utility::escapePathForDbus(entryID);
1749
1750 crow::connections::systemBus->async_method_call(
1751 [asyncResp,
1752 entryID](const boost::system::error_code ec,
1753 const sdbusplus::message::unix_fd& unixfd) {
1754 if (ec.value() == EBADR)
1755 {
1756 messages::resourceNotFound(
1757 asyncResp->res, "EventLogAttachment", entryID);
1758 return;
1759 }
1760 if (ec)
1761 {
1762 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1763 messages::internalError(asyncResp->res);
1764 return;
1765 }
1766
1767 int fd = -1;
1768 fd = dup(unixfd);
1769 if (fd == -1)
1770 {
1771 messages::internalError(asyncResp->res);
1772 return;
1773 }
1774
1775 long long int size = lseek(fd, 0, SEEK_END);
1776 if (size == -1)
1777 {
1778 messages::internalError(asyncResp->res);
1779 return;
1780 }
1781
1782 // Arbitrary max size of 64kb
1783 constexpr int maxFileSize = 65536;
1784 if (size > maxFileSize)
1785 {
1786 BMCWEB_LOG_ERROR
1787 << "File size exceeds maximum allowed size of "
1788 << maxFileSize;
1789 messages::internalError(asyncResp->res);
1790 return;
1791 }
1792 std::vector<char> data(static_cast<size_t>(size));
1793 long long int rc = lseek(fd, 0, SEEK_SET);
1794 if (rc == -1)
1795 {
1796 messages::internalError(asyncResp->res);
1797 return;
1798 }
1799 rc = read(fd, data.data(), data.size());
1800 if ((rc == -1) || (rc != size))
1801 {
1802 messages::internalError(asyncResp->res);
1803 return;
1804 }
1805 close(fd);
1806
1807 std::string_view strData(data.data(), data.size());
1808 std::string output =
1809 crow::utility::base64encode(strData);
1810
1811 asyncResp->res.addHeader("Content-Type",
1812 "application/octet-stream");
1813 asyncResp->res.addHeader("Content-Transfer-Encoding",
1814 "Base64");
1815 asyncResp->res.body() = std::move(output);
1816 },
1817 "xyz.openbmc_project.Logging",
1818 "/xyz/openbmc_project/logging/entry/" + entryID,
1819 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1820 });
1821}
1822
Spencer Kub7028eb2021-10-26 15:27:35 +08001823constexpr const char* hostLoggerFolderPath = "/var/log/console";
1824
1825inline bool
1826 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1827 std::vector<std::filesystem::path>& hostLoggerFiles)
1828{
1829 std::error_code ec;
1830 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1831 if (ec)
1832 {
1833 BMCWEB_LOG_ERROR << ec.message();
1834 return false;
1835 }
1836 for (const std::filesystem::directory_entry& it : logPath)
1837 {
1838 std::string filename = it.path().filename();
1839 // Prefix of each log files is "log". Find the file and save the
1840 // path
1841 if (boost::starts_with(filename, "log"))
1842 {
1843 hostLoggerFiles.emplace_back(it.path());
1844 }
1845 }
1846 // As the log files rotate, they are appended with a ".#" that is higher for
1847 // the older logs. Since we start from oldest logs, sort the name in
1848 // descending order.
1849 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1850 AlphanumLess<std::string>());
1851
1852 return true;
1853}
1854
1855inline bool
1856 getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1857 uint64_t& skip, uint64_t& top,
1858 std::vector<std::string>& logEntries, size_t& logCount)
1859{
1860 GzFileReader logFile;
1861
1862 // Go though all log files and expose host logs.
1863 for (const std::filesystem::path& it : hostLoggerFiles)
1864 {
1865 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1866 {
1867 BMCWEB_LOG_ERROR << "fail to expose host logs";
1868 return false;
1869 }
1870 }
1871 // Get lastMessage from constructor by getter
1872 std::string lastMessage = logFile.getLastMessage();
1873 if (!lastMessage.empty())
1874 {
1875 logCount++;
1876 if (logCount > skip && logCount <= (skip + top))
1877 {
1878 logEntries.push_back(lastMessage);
1879 }
1880 }
1881 return true;
1882}
1883
1884inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1885 const std::string& msg,
1886 nlohmann::json& logEntryJson)
1887{
1888 // Fill in the log entry with the gathered data.
1889 logEntryJson = {
1890 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1891 {"@odata.id",
1892 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1893 logEntryID},
1894 {"Name", "Host Logger Entry"},
1895 {"Id", logEntryID},
1896 {"Message", msg},
1897 {"EntryType", "Oem"},
1898 {"Severity", "OK"},
1899 {"OemRecordFormat", "Host Logger Entry"}};
1900}
1901
1902inline void requestRoutesSystemHostLogger(App& app)
1903{
1904 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1905 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08001906 .methods(
1907 boost::beast::http::verb::
1908 get)([](const crow::Request&,
1909 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1910 asyncResp->res.jsonValue["@odata.id"] =
1911 "/redfish/v1/Systems/system/LogServices/HostLogger";
1912 asyncResp->res.jsonValue["@odata.type"] =
1913 "#LogService.v1_1_0.LogService";
1914 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1915 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1916 asyncResp->res.jsonValue["Id"] = "HostLogger";
1917 asyncResp->res.jsonValue["Entries"] = {
1918 {"@odata.id",
1919 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1920 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001921}
1922
1923inline void requestRoutesSystemHostLoggerCollection(App& app)
1924{
1925 BMCWEB_ROUTE(app,
1926 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1927 .privileges(redfish::privileges::getLogEntry)
George Liu0fda0f12021-11-16 10:06:17 +08001928 .methods(
1929 boost::beast::http::verb::
1930 get)([](const crow::Request& req,
1931 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1932 uint64_t skip = 0;
1933 uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1934 // default, allow range 1 to
1935 // 1000 entries per page.
1936 if (!getSkipParam(asyncResp, req, skip))
1937 {
1938 return;
1939 }
1940 if (!getTopParam(asyncResp, req, top))
1941 {
1942 return;
1943 }
1944 asyncResp->res.jsonValue["@odata.id"] =
1945 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1946 asyncResp->res.jsonValue["@odata.type"] =
1947 "#LogEntryCollection.LogEntryCollection";
1948 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1949 asyncResp->res.jsonValue["Description"] =
1950 "Collection of HostLogger Entries";
1951 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1952 logEntryArray = nlohmann::json::array();
1953 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001954
George Liu0fda0f12021-11-16 10:06:17 +08001955 std::vector<std::filesystem::path> hostLoggerFiles;
1956 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1957 {
1958 BMCWEB_LOG_ERROR << "fail to get host log file path";
1959 return;
1960 }
1961
1962 size_t logCount = 0;
1963 // This vector only store the entries we want to expose that
1964 // control by skip and top.
1965 std::vector<std::string> logEntries;
1966 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1967 logCount))
1968 {
1969 messages::internalError(asyncResp->res);
1970 return;
1971 }
1972 // If vector is empty, that means skip value larger than total
1973 // log count
1974 if (logEntries.size() == 0)
1975 {
1976 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1977 return;
1978 }
1979 if (logEntries.size() > 0)
1980 {
1981 for (size_t i = 0; i < logEntries.size(); i++)
Spencer Kub7028eb2021-10-26 15:27:35 +08001982 {
George Liu0fda0f12021-11-16 10:06:17 +08001983 logEntryArray.push_back({});
1984 nlohmann::json& hostLogEntry = logEntryArray.back();
1985 fillHostLoggerEntryJson(std::to_string(skip + i),
1986 logEntries[i], hostLogEntry);
Spencer Kub7028eb2021-10-26 15:27:35 +08001987 }
1988
George Liu0fda0f12021-11-16 10:06:17 +08001989 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1990 if (skip + top < logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001991 {
George Liu0fda0f12021-11-16 10:06:17 +08001992 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1993 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1994 std::to_string(skip + top);
Spencer Kub7028eb2021-10-26 15:27:35 +08001995 }
George Liu0fda0f12021-11-16 10:06:17 +08001996 }
1997 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001998}
1999
2000inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2001{
2002 BMCWEB_ROUTE(
2003 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2004 .privileges(redfish::privileges::getLogEntry)
2005 .methods(boost::beast::http::verb::get)(
2006 [](const crow::Request&,
2007 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2008 const std::string& param) {
2009 const std::string& targetID = param;
2010
2011 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002012
2013 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2014 const char* end = targetID.data() + targetID.size();
2015
2016 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
Spencer Kub7028eb2021-10-26 15:27:35 +08002017 if (ec == std::errc::invalid_argument)
2018 {
2019 messages::resourceMissingAtURI(asyncResp->res, targetID);
2020 return;
2021 }
2022 if (ec == std::errc::result_out_of_range)
2023 {
2024 messages::resourceMissingAtURI(asyncResp->res, targetID);
2025 return;
2026 }
2027
2028 std::vector<std::filesystem::path> hostLoggerFiles;
2029 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2030 {
2031 BMCWEB_LOG_ERROR << "fail to get host log file path";
2032 return;
2033 }
2034
2035 size_t logCount = 0;
2036 uint64_t top = 1;
2037 std::vector<std::string> logEntries;
2038 // We can get specific entry by skip and top. For example, if we
2039 // want to get nth entry, we can set skip = n-1 and top = 1 to
2040 // get that entry
2041 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2042 logEntries, logCount))
2043 {
2044 messages::internalError(asyncResp->res);
2045 return;
2046 }
2047
2048 if (!logEntries.empty())
2049 {
2050 fillHostLoggerEntryJson(targetID, logEntries[0],
2051 asyncResp->res.jsonValue);
2052 return;
2053 }
2054
2055 // Requested ID was not found
2056 messages::resourceMissingAtURI(asyncResp->res, targetID);
2057 });
2058}
2059
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002060inline void requestRoutesBMCLogServiceCollection(App& app)
2061{
2062 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002063 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002064 .methods(boost::beast::http::verb::get)(
2065 [](const crow::Request&,
2066 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2067 // Collections don't include the static data added by SubRoute
2068 // because it has a duplicate entry for members
2069 asyncResp->res.jsonValue["@odata.type"] =
2070 "#LogServiceCollection.LogServiceCollection";
2071 asyncResp->res.jsonValue["@odata.id"] =
2072 "/redfish/v1/Managers/bmc/LogServices";
2073 asyncResp->res.jsonValue["Name"] =
2074 "Open BMC Log Services Collection";
2075 asyncResp->res.jsonValue["Description"] =
2076 "Collection of LogServices for this Manager";
2077 nlohmann::json& logServiceArray =
2078 asyncResp->res.jsonValue["Members"];
2079 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002080#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002081 logServiceArray.push_back(
2082 {{"@odata.id",
2083 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002084#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002085#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002086 logServiceArray.push_back(
2087 {{"@odata.id",
2088 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002089#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002090 asyncResp->res.jsonValue["Members@odata.count"] =
2091 logServiceArray.size();
2092 });
2093}
Ed Tanous1da66f72018-07-27 16:13:37 -07002094
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002095inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002096{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002097 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002098 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002099 .methods(boost::beast::http::verb::get)(
2100 [](const crow::Request&,
2101 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07002102
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002103 {
2104 asyncResp->res.jsonValue["@odata.type"] =
2105 "#LogService.v1_1_0.LogService";
2106 asyncResp->res.jsonValue["@odata.id"] =
2107 "/redfish/v1/Managers/bmc/LogServices/Journal";
2108 asyncResp->res.jsonValue["Name"] =
2109 "Open BMC Journal Log Service";
2110 asyncResp->res.jsonValue["Description"] =
2111 "BMC Journal Log Service";
2112 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2113 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302114
2115 std::pair<std::string, std::string> redfishDateTimeOffset =
2116 crow::utility::getDateTimeOffsetNow();
2117 asyncResp->res.jsonValue["DateTime"] =
2118 redfishDateTimeOffset.first;
2119 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2120 redfishDateTimeOffset.second;
2121
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002122 asyncResp->res.jsonValue["Entries"] = {
2123 {"@odata.id",
2124 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
2125 });
2126}
Jason M. Billse1f26342018-07-18 12:12:00 -07002127
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002128static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2129 sd_journal* journal,
2130 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002131{
2132 // Get the Log Entry contents
2133 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002134
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002135 std::string message;
2136 std::string_view syslogID;
2137 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2138 if (ret < 0)
2139 {
2140 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2141 << strerror(-ret);
2142 }
2143 if (!syslogID.empty())
2144 {
2145 message += std::string(syslogID) + ": ";
2146 }
2147
Ed Tanous39e77502019-03-04 17:35:53 -08002148 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002149 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002150 if (ret < 0)
2151 {
2152 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2153 return 1;
2154 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002155 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002156
2157 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002158 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002159 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002160 if (ret < 0)
2161 {
2162 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002163 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002164
2165 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002166 std::string entryTimeStr;
2167 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002168 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002169 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002170 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002171
2172 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002173 bmcJournalLogEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08002174 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002175 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2176 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07002177 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002178 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002179 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07002180 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06002181 {"Severity", severity <= 2 ? "Critical"
2182 : severity <= 4 ? "Warning"
2183 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07002184 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07002185 {"Created", std::move(entryTimeStr)}};
2186 return 0;
2187}
2188
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002189inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002190{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002191 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002192 .privileges(redfish::privileges::getLogEntryCollection)
George Liu0fda0f12021-11-16 10:06:17 +08002193 .methods(
2194 boost::beast::http::verb::
2195 get)([](const crow::Request& req,
2196 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2197 static constexpr const long maxEntriesPerPage = 1000;
2198 uint64_t skip = 0;
2199 uint64_t top = maxEntriesPerPage; // Show max entries by default
2200 if (!getSkipParam(asyncResp, req, skip))
2201 {
2202 return;
2203 }
2204 if (!getTopParam(asyncResp, req, top))
2205 {
2206 return;
2207 }
2208 // Collections don't include the static data added by SubRoute
2209 // because it has a duplicate entry for members
2210 asyncResp->res.jsonValue["@odata.type"] =
2211 "#LogEntryCollection.LogEntryCollection";
2212 asyncResp->res.jsonValue["@odata.id"] =
2213 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2214 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2215 asyncResp->res.jsonValue["Description"] =
2216 "Collection of BMC Journal Entries";
2217 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2218 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002219
George Liu0fda0f12021-11-16 10:06:17 +08002220 // Go through the journal and use the timestamp to create a
2221 // unique ID for each entry
2222 sd_journal* journalTmp = nullptr;
2223 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2224 if (ret < 0)
2225 {
2226 BMCWEB_LOG_ERROR << "failed to open journal: "
2227 << strerror(-ret);
2228 messages::internalError(asyncResp->res);
2229 return;
2230 }
2231 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2232 journalTmp, sd_journal_close);
2233 journalTmp = nullptr;
2234 uint64_t entryCount = 0;
2235 // Reset the unique ID on the first entry
2236 bool firstEntry = true;
2237 SD_JOURNAL_FOREACH(journal.get())
2238 {
2239 entryCount++;
2240 // Handle paging using skip (number of entries to skip from
2241 // the start) and top (number of entries to display)
2242 if (entryCount <= skip || entryCount > skip + top)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002243 {
George Liu0fda0f12021-11-16 10:06:17 +08002244 continue;
2245 }
2246
2247 std::string idStr;
2248 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2249 {
2250 continue;
2251 }
2252
2253 if (firstEntry)
2254 {
2255 firstEntry = false;
2256 }
2257
2258 logEntryArray.push_back({});
2259 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2260 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2261 bmcJournalLogEntry) != 0)
2262 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002263 messages::internalError(asyncResp->res);
2264 return;
2265 }
George Liu0fda0f12021-11-16 10:06:17 +08002266 }
2267 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2268 if (skip + top < entryCount)
2269 {
2270 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2271 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2272 std::to_string(skip + top);
2273 }
2274 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002275}
Jason M. Billse1f26342018-07-18 12:12:00 -07002276
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002277inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002278{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002279 BMCWEB_ROUTE(app,
2280 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002281 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002282 .methods(boost::beast::http::verb::get)(
2283 [](const crow::Request&,
2284 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2285 const std::string& entryID) {
2286 // Convert the unique ID back to a timestamp to find the entry
2287 uint64_t ts = 0;
2288 uint64_t index = 0;
2289 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2290 {
2291 return;
2292 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002293
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002294 sd_journal* journalTmp = nullptr;
2295 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2296 if (ret < 0)
2297 {
2298 BMCWEB_LOG_ERROR << "failed to open journal: "
2299 << strerror(-ret);
2300 messages::internalError(asyncResp->res);
2301 return;
2302 }
2303 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2304 journal(journalTmp, sd_journal_close);
2305 journalTmp = nullptr;
2306 // Go to the timestamp in the log and move to the entry at the
2307 // index tracking the unique ID
2308 std::string idStr;
2309 bool firstEntry = true;
2310 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2311 if (ret < 0)
2312 {
2313 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2314 << strerror(-ret);
2315 messages::internalError(asyncResp->res);
2316 return;
2317 }
2318 for (uint64_t i = 0; i <= index; i++)
2319 {
2320 sd_journal_next(journal.get());
2321 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2322 {
2323 messages::internalError(asyncResp->res);
2324 return;
2325 }
2326 if (firstEntry)
2327 {
2328 firstEntry = false;
2329 }
2330 }
2331 // Confirm that the entry ID matches what was requested
2332 if (idStr != entryID)
2333 {
2334 messages::resourceMissingAtURI(asyncResp->res, entryID);
2335 return;
2336 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002337
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002338 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2339 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002340 {
2341 messages::internalError(asyncResp->res);
2342 return;
2343 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002344 });
2345}
2346
2347inline void requestRoutesBMCDumpService(App& app)
2348{
2349 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002350 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08002351 .methods(
2352 boost::beast::http::verb::
2353 get)([](const crow::Request&,
2354 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2355 asyncResp->res.jsonValue["@odata.id"] =
2356 "/redfish/v1/Managers/bmc/LogServices/Dump";
2357 asyncResp->res.jsonValue["@odata.type"] =
2358 "#LogService.v1_2_0.LogService";
2359 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2360 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2361 asyncResp->res.jsonValue["Id"] = "Dump";
2362 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302363
George Liu0fda0f12021-11-16 10:06:17 +08002364 std::pair<std::string, std::string> redfishDateTimeOffset =
2365 crow::utility::getDateTimeOffsetNow();
2366 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2367 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2368 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302369
George Liu0fda0f12021-11-16 10:06:17 +08002370 asyncResp->res.jsonValue["Entries"] = {
2371 {"@odata.id",
2372 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2373 asyncResp->res.jsonValue["Actions"] = {
2374 {"#LogService.ClearLog",
2375 {{"target",
2376 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2377 {"#LogService.CollectDiagnosticData",
2378 {{"target",
2379 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
2380 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002381}
2382
2383inline void requestRoutesBMCDumpEntryCollection(App& app)
2384{
2385
2386 /**
2387 * Functions triggers appropriate requests on DBus
2388 */
2389 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002390 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002391 .methods(boost::beast::http::verb::get)(
2392 [](const crow::Request&,
2393 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2394 asyncResp->res.jsonValue["@odata.type"] =
2395 "#LogEntryCollection.LogEntryCollection";
2396 asyncResp->res.jsonValue["@odata.id"] =
2397 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2398 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2399 asyncResp->res.jsonValue["Description"] =
2400 "Collection of BMC Dump Entries";
2401
2402 getDumpEntryCollection(asyncResp, "BMC");
2403 });
2404}
2405
2406inline void requestRoutesBMCDumpEntry(App& app)
2407{
2408 BMCWEB_ROUTE(app,
2409 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002410 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002411 .methods(boost::beast::http::verb::get)(
2412 [](const crow::Request&,
2413 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2414 const std::string& param) {
2415 getDumpEntryById(asyncResp, param, "BMC");
2416 });
2417 BMCWEB_ROUTE(app,
2418 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002419 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002420 .methods(boost::beast::http::verb::delete_)(
2421 [](const crow::Request&,
2422 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2423 const std::string& param) {
2424 deleteDumpEntry(asyncResp, param, "bmc");
2425 });
2426}
2427
2428inline void requestRoutesBMCDumpCreate(App& app)
2429{
2430
George Liu0fda0f12021-11-16 10:06:17 +08002431 BMCWEB_ROUTE(
2432 app,
2433 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002434 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002435 .methods(boost::beast::http::verb::post)(
2436 [](const crow::Request& req,
2437 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2438 createDump(asyncResp, req, "BMC");
2439 });
2440}
2441
2442inline void requestRoutesBMCDumpClear(App& app)
2443{
George Liu0fda0f12021-11-16 10:06:17 +08002444 BMCWEB_ROUTE(
2445 app,
2446 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002447 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002448 .methods(boost::beast::http::verb::post)(
2449 [](const crow::Request&,
2450 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2451 clearDump(asyncResp, "BMC");
2452 });
2453}
2454
2455inline void requestRoutesSystemDumpService(App& app)
2456{
2457 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002458 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002459 .methods(boost::beast::http::verb::get)(
2460 [](const crow::Request&,
2461 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2462
2463 {
2464 asyncResp->res.jsonValue["@odata.id"] =
2465 "/redfish/v1/Systems/system/LogServices/Dump";
2466 asyncResp->res.jsonValue["@odata.type"] =
2467 "#LogService.v1_2_0.LogService";
2468 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2469 asyncResp->res.jsonValue["Description"] =
2470 "System Dump LogService";
2471 asyncResp->res.jsonValue["Id"] = "Dump";
2472 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302473
2474 std::pair<std::string, std::string> redfishDateTimeOffset =
2475 crow::utility::getDateTimeOffsetNow();
2476 asyncResp->res.jsonValue["DateTime"] =
2477 redfishDateTimeOffset.first;
2478 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2479 redfishDateTimeOffset.second;
2480
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002481 asyncResp->res.jsonValue["Entries"] = {
2482 {"@odata.id",
2483 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2484 asyncResp->res.jsonValue["Actions"] = {
2485 {"#LogService.ClearLog",
2486 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002487 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002488 {"#LogService.CollectDiagnosticData",
2489 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002490 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002491 });
2492}
2493
2494inline void requestRoutesSystemDumpEntryCollection(App& app)
2495{
2496
2497 /**
2498 * Functions triggers appropriate requests on DBus
2499 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002500 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002501 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002502 .methods(boost::beast::http::verb::get)(
2503 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002504 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002505 asyncResp->res.jsonValue["@odata.type"] =
2506 "#LogEntryCollection.LogEntryCollection";
2507 asyncResp->res.jsonValue["@odata.id"] =
2508 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2509 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2510 asyncResp->res.jsonValue["Description"] =
2511 "Collection of System Dump Entries";
2512
2513 getDumpEntryCollection(asyncResp, "System");
2514 });
2515}
2516
2517inline void requestRoutesSystemDumpEntry(App& app)
2518{
2519 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002520 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002521 .privileges(redfish::privileges::getLogEntry)
2522
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002523 .methods(boost::beast::http::verb::get)(
2524 [](const crow::Request&,
2525 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2526 const std::string& param) {
2527 getDumpEntryById(asyncResp, param, "System");
2528 });
2529
2530 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002531 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002532 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002533 .methods(boost::beast::http::verb::delete_)(
2534 [](const crow::Request&,
2535 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2536 const std::string& param) {
2537 deleteDumpEntry(asyncResp, param, "system");
2538 });
2539}
2540
2541inline void requestRoutesSystemDumpCreate(App& app)
2542{
George Liu0fda0f12021-11-16 10:06:17 +08002543 BMCWEB_ROUTE(
2544 app,
2545 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002546 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002547 .methods(boost::beast::http::verb::post)(
2548 [](const crow::Request& req,
2549 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2550
2551 { createDump(asyncResp, req, "System"); });
2552}
2553
2554inline void requestRoutesSystemDumpClear(App& app)
2555{
George Liu0fda0f12021-11-16 10:06:17 +08002556 BMCWEB_ROUTE(
2557 app,
2558 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002559 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002560 .methods(boost::beast::http::verb::post)(
2561 [](const crow::Request&,
2562 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2563
2564 { clearDump(asyncResp, "System"); });
2565}
2566
2567inline void requestRoutesCrashdumpService(App& app)
2568{
2569 // Note: Deviated from redfish privilege registry for GET & HEAD
2570 // method for security reasons.
2571 /**
2572 * Functions triggers appropriate requests on DBus
2573 */
2574 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002575 // This is incorrect, should be:
2576 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002577 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002578 .methods(
2579 boost::beast::http::verb::
2580 get)([](const crow::Request&,
2581 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2582 // Copy over the static data to include the entries added by
2583 // SubRoute
2584 asyncResp->res.jsonValue["@odata.id"] =
2585 "/redfish/v1/Systems/system/LogServices/Crashdump";
2586 asyncResp->res.jsonValue["@odata.type"] =
2587 "#LogService.v1_2_0.LogService";
2588 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2589 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2590 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2591 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2592 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302593
2594 std::pair<std::string, std::string> redfishDateTimeOffset =
2595 crow::utility::getDateTimeOffsetNow();
2596 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2597 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2598 redfishDateTimeOffset.second;
2599
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002600 asyncResp->res.jsonValue["Entries"] = {
2601 {"@odata.id",
2602 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2603 asyncResp->res.jsonValue["Actions"] = {
2604 {"#LogService.ClearLog",
George Liu0fda0f12021-11-16 10:06:17 +08002605 {{"target",
2606 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002607 {"#LogService.CollectDiagnosticData",
George Liu0fda0f12021-11-16 10:06:17 +08002608 {{"target",
2609 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002610 });
2611}
2612
2613void inline requestRoutesCrashdumpClear(App& app)
2614{
George Liu0fda0f12021-11-16 10:06:17 +08002615 BMCWEB_ROUTE(
2616 app,
2617 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002618 // This is incorrect, should be:
2619 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002620 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002621 .methods(boost::beast::http::verb::post)(
2622 [](const crow::Request&,
2623 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2624 crow::connections::systemBus->async_method_call(
2625 [asyncResp](const boost::system::error_code ec,
2626 const std::string&) {
2627 if (ec)
2628 {
2629 messages::internalError(asyncResp->res);
2630 return;
2631 }
2632 messages::success(asyncResp->res);
2633 },
2634 crashdumpObject, crashdumpPath, deleteAllInterface,
2635 "DeleteAll");
2636 });
2637}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002638
zhanghch058d1b46d2021-04-01 11:18:24 +08002639static void
2640 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2641 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002642{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002643 auto getStoredLogCallback =
2644 [asyncResp, logID, &logEntryJson](
2645 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -08002646 const std::vector<
2647 std::pair<std::string, dbus::utility::DbusVariantType>>&
2648 params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002649 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002650 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002651 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2652 if (ec.value() ==
2653 boost::system::linux_error::bad_request_descriptor)
2654 {
2655 messages::resourceNotFound(asyncResp->res, "LogEntry",
2656 logID);
2657 }
2658 else
2659 {
2660 messages::internalError(asyncResp->res);
2661 }
2662 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002663 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002664
Johnathan Mantey043a0532020-03-10 17:15:28 -07002665 std::string timestamp{};
2666 std::string filename{};
2667 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002668 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002669
2670 if (filename.empty() || timestamp.empty())
2671 {
2672 messages::resourceMissingAtURI(asyncResp->res, logID);
2673 return;
2674 }
2675
2676 std::string crashdumpURI =
2677 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2678 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002679 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002680 {"@odata.id", "/redfish/v1/Systems/system/"
2681 "LogServices/Crashdump/Entries/" +
2682 logID},
2683 {"Name", "CPU Crashdump"},
2684 {"Id", logID},
2685 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002686 {"AdditionalDataURI", std::move(crashdumpURI)},
2687 {"DiagnosticDataType", "OEM"},
2688 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002689 {"Created", std::move(timestamp)}};
2690 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002691 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002692 std::move(getStoredLogCallback), crashdumpObject,
2693 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002694 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002695}
2696
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002697inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002698{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002699 // Note: Deviated from redfish privilege registry for GET & HEAD
2700 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002701 /**
2702 * Functions triggers appropriate requests on DBus
2703 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002704 BMCWEB_ROUTE(app,
2705 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002706 // This is incorrect, should be.
2707 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002708 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002709 .methods(
2710 boost::beast::http::verb::
2711 get)([](const crow::Request&,
2712 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2713 // Collections don't include the static data added by SubRoute
2714 // because it has a duplicate entry for members
2715 auto getLogEntriesCallback = [asyncResp](
2716 const boost::system::error_code ec,
2717 const std::vector<std::string>&
2718 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002719 if (ec)
2720 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002721 if (ec.value() !=
2722 boost::system::errc::no_such_file_or_directory)
2723 {
2724 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2725 << ec.message();
2726 messages::internalError(asyncResp->res);
2727 return;
2728 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002729 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002730 asyncResp->res.jsonValue["@odata.type"] =
2731 "#LogEntryCollection.LogEntryCollection";
2732 asyncResp->res.jsonValue["@odata.id"] =
2733 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2734 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2735 asyncResp->res.jsonValue["Description"] =
2736 "Collection of Crashdump Entries";
2737 nlohmann::json& logEntryArray =
2738 asyncResp->res.jsonValue["Members"];
2739 logEntryArray = nlohmann::json::array();
2740 std::vector<std::string> logIDs;
2741 // Get the list of log entries and build up an empty array big
2742 // enough to hold them
2743 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002744 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002745 // Get the log ID
2746 std::size_t lastPos = objpath.rfind('/');
2747 if (lastPos == std::string::npos)
2748 {
2749 continue;
2750 }
2751 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002752
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002753 // Add a space for the log entry to the array
2754 logEntryArray.push_back({});
2755 }
2756 // Now go through and set up async calls to fill in the entries
2757 size_t index = 0;
2758 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002759 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002760 // Add the log entry to the array
2761 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002762 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002763 asyncResp->res.jsonValue["Members@odata.count"] =
2764 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002765 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002766 crow::connections::systemBus->async_method_call(
2767 std::move(getLogEntriesCallback),
2768 "xyz.openbmc_project.ObjectMapper",
2769 "/xyz/openbmc_project/object_mapper",
2770 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2771 std::array<const char*, 1>{crashdumpInterface});
2772 });
2773}
Ed Tanous1da66f72018-07-27 16:13:37 -07002774
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002775inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002776{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002777 // Note: Deviated from redfish privilege registry for GET & HEAD
2778 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002779
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002780 BMCWEB_ROUTE(
2781 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002782 // this is incorrect, should be
2783 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002784 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002785 .methods(boost::beast::http::verb::get)(
2786 [](const crow::Request&,
2787 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2788 const std::string& param) {
2789 const std::string& logID = param;
2790 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2791 });
2792}
Ed Tanous1da66f72018-07-27 16:13:37 -07002793
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002794inline void requestRoutesCrashdumpFile(App& app)
2795{
2796 // Note: Deviated from redfish privilege registry for GET & HEAD
2797 // method for security reasons.
2798 BMCWEB_ROUTE(
2799 app,
2800 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002801 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002802 .methods(boost::beast::http::verb::get)(
2803 [](const crow::Request&,
2804 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2805 const std::string& logID, const std::string& fileName) {
2806 auto getStoredLogCallback =
2807 [asyncResp, logID, fileName](
2808 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -08002809 const std::vector<std::pair<
2810 std::string, dbus::utility::DbusVariantType>>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002811 resp) {
2812 if (ec)
2813 {
2814 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2815 << ec.message();
2816 messages::internalError(asyncResp->res);
2817 return;
2818 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002819
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002820 std::string dbusFilename{};
2821 std::string dbusTimestamp{};
2822 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002823
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002824 parseCrashdumpParameters(resp, dbusFilename,
2825 dbusTimestamp, dbusFilepath);
2826
2827 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2828 dbusFilepath.empty())
2829 {
2830 messages::resourceMissingAtURI(asyncResp->res,
2831 fileName);
2832 return;
2833 }
2834
2835 // Verify the file name parameter is correct
2836 if (fileName != dbusFilename)
2837 {
2838 messages::resourceMissingAtURI(asyncResp->res,
2839 fileName);
2840 return;
2841 }
2842
2843 if (!std::filesystem::exists(dbusFilepath))
2844 {
2845 messages::resourceMissingAtURI(asyncResp->res,
2846 fileName);
2847 return;
2848 }
Jason M. Bills2d314912022-01-12 13:59:01 -08002849 std::ifstream ifs(dbusFilepath,
2850 std::ios::in | std::ios::binary);
2851 asyncResp->res.body() = std::string(
2852 std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002853
2854 // Configure this to be a file download when accessed
2855 // from a browser
2856 asyncResp->res.addHeader("Content-Disposition",
2857 "attachment");
2858 };
2859 crow::connections::systemBus->async_method_call(
2860 std::move(getStoredLogCallback), crashdumpObject,
2861 crashdumpPath + std::string("/") + logID,
2862 "org.freedesktop.DBus.Properties", "GetAll",
2863 crashdumpInterface);
2864 });
2865}
2866
2867inline void requestRoutesCrashdumpCollect(App& app)
2868{
2869 // Note: Deviated from redfish privilege registry for GET & HEAD
2870 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08002871 BMCWEB_ROUTE(
2872 app,
2873 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002874 // The below is incorrect; Should be ConfigureManager
2875 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002876 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002877 .methods(
2878 boost::beast::http::verb::
2879 post)([](const crow::Request& req,
2880 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2881 std::string diagnosticDataType;
2882 std::string oemDiagnosticDataType;
2883 if (!redfish::json_util::readJson(
2884 req, asyncResp->res, "DiagnosticDataType",
2885 diagnosticDataType, "OEMDiagnosticDataType",
2886 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002887 {
James Feist46229572020-02-19 15:11:58 -08002888 return;
2889 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002890
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002891 if (diagnosticDataType != "OEM")
2892 {
2893 BMCWEB_LOG_ERROR
2894 << "Only OEM DiagnosticDataType supported for Crashdump";
2895 messages::actionParameterValueFormatError(
2896 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2897 "CollectDiagnosticData");
2898 return;
2899 }
2900
Ed Tanous98be3e32021-09-16 15:05:36 -07002901 auto collectCrashdumpCallback = [asyncResp,
2902 payload(task::Payload(req))](
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002903 const boost::system::error_code
2904 ec,
Ed Tanous98be3e32021-09-16 15:05:36 -07002905 const std::string&) mutable {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002906 if (ec)
2907 {
2908 if (ec.value() ==
2909 boost::system::errc::operation_not_supported)
2910 {
2911 messages::resourceInStandby(asyncResp->res);
2912 }
2913 else if (ec.value() ==
2914 boost::system::errc::device_or_resource_busy)
2915 {
2916 messages::serviceTemporarilyUnavailable(asyncResp->res,
2917 "60");
2918 }
2919 else
2920 {
2921 messages::internalError(asyncResp->res);
2922 }
2923 return;
2924 }
George Liu0fda0f12021-11-16 10:06:17 +08002925 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
2926 [](boost::system::error_code err,
2927 sdbusplus::message::message&,
2928 const std::shared_ptr<task::TaskData>& taskData) {
2929 if (!err)
2930 {
2931 taskData->messages.emplace_back(
2932 messages::taskCompletedOK(
2933 std::to_string(taskData->index)));
2934 taskData->state = "Completed";
2935 }
2936 return task::completed;
2937 },
2938 "type='signal',interface='org.freedesktop.DBus."
2939 "Properties',"
2940 "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002941 task->startTimer(std::chrono::minutes(5));
2942 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -07002943 task->payload.emplace(std::move(payload));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002944 };
2945
2946 if (oemDiagnosticDataType == "OnDemand")
2947 {
2948 crow::connections::systemBus->async_method_call(
2949 std::move(collectCrashdumpCallback), crashdumpObject,
2950 crashdumpPath, crashdumpOnDemandInterface,
2951 "GenerateOnDemandLog");
2952 }
2953 else if (oemDiagnosticDataType == "Telemetry")
2954 {
2955 crow::connections::systemBus->async_method_call(
2956 std::move(collectCrashdumpCallback), crashdumpObject,
2957 crashdumpPath, crashdumpTelemetryInterface,
2958 "GenerateTelemetryLog");
2959 }
2960 else
2961 {
2962 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2963 << oemDiagnosticDataType;
2964 messages::actionParameterValueFormatError(
2965 asyncResp->res, oemDiagnosticDataType,
2966 "OEMDiagnosticDataType", "CollectDiagnosticData");
2967 return;
2968 }
2969 });
2970}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002971
Andrew Geisslercb92c032018-08-17 07:56:14 -07002972/**
2973 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2974 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002975inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002976{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002977 /**
2978 * Function handles POST method request.
2979 * The Clear Log actions does not require any parameter.The action deletes
2980 * all entries found in the Entries collection for this Log Service.
2981 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002982
George Liu0fda0f12021-11-16 10:06:17 +08002983 BMCWEB_ROUTE(
2984 app,
2985 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002986 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002987 .methods(boost::beast::http::verb::post)(
2988 [](const crow::Request&,
2989 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2990 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07002991
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002992 // Process response from Logging service.
2993 auto respHandler = [asyncResp](
2994 const boost::system::error_code ec) {
2995 BMCWEB_LOG_DEBUG
2996 << "doClearLog resp_handler callback: Done";
2997 if (ec)
2998 {
2999 // TODO Handle for specific error code
3000 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
3001 << ec;
3002 asyncResp->res.result(
3003 boost::beast::http::status::internal_server_error);
3004 return;
3005 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003006
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003007 asyncResp->res.result(
3008 boost::beast::http::status::no_content);
3009 };
3010
3011 // Make call to Logging service to request Clear Log
3012 crow::connections::systemBus->async_method_call(
3013 respHandler, "xyz.openbmc_project.Logging",
3014 "/xyz/openbmc_project/logging",
3015 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3016 });
3017}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003018
3019/****************************************************
3020 * Redfish PostCode interfaces
3021 * using DBUS interface: getPostCodesTS
3022 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003023inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003024{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003025 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003026 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08003027 .methods(
3028 boost::beast::http::verb::
3029 get)([](const crow::Request&,
3030 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3031 asyncResp->res.jsonValue = {
3032 {"@odata.id",
3033 "/redfish/v1/Systems/system/LogServices/PostCodes"},
3034 {"@odata.type", "#LogService.v1_1_0.LogService"},
3035 {"Name", "POST Code Log Service"},
3036 {"Description", "POST Code Log Service"},
3037 {"Id", "BIOS POST Code Log"},
3038 {"OverWritePolicy", "WrapsWhenFull"},
3039 {"Entries",
3040 {{"@odata.id",
3041 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05303042
George Liu0fda0f12021-11-16 10:06:17 +08003043 std::pair<std::string, std::string> redfishDateTimeOffset =
3044 crow::utility::getDateTimeOffsetNow();
3045 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3046 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3047 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303048
George Liu0fda0f12021-11-16 10:06:17 +08003049 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3050 {"target",
3051 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
3052 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003053}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003054
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003055inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003056{
George Liu0fda0f12021-11-16 10:06:17 +08003057 BMCWEB_ROUTE(
3058 app,
3059 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003060 // The following privilege is incorrect; It should be ConfigureManager
3061 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003062 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003063 .methods(boost::beast::http::verb::post)(
3064 [](const crow::Request&,
3065 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3066 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003067
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003068 // Make call to post-code service to request clear all
3069 crow::connections::systemBus->async_method_call(
3070 [asyncResp](const boost::system::error_code ec) {
3071 if (ec)
3072 {
3073 // TODO Handle for specific error code
3074 BMCWEB_LOG_ERROR
3075 << "doClearPostCodes resp_handler got error "
3076 << ec;
3077 asyncResp->res.result(boost::beast::http::status::
3078 internal_server_error);
3079 messages::internalError(asyncResp->res);
3080 return;
3081 }
3082 },
3083 "xyz.openbmc_project.State.Boot.PostCode0",
3084 "/xyz/openbmc_project/State/Boot/PostCode0",
3085 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3086 });
3087}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003088
3089static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003090 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303091 const boost::container::flat_map<
3092 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003093 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3094 const uint64_t skip = 0, const uint64_t top = 0)
3095{
3096 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003097 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05303098 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003099
3100 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003101 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003102
3103 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303104 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3105 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003106 {
3107 currentCodeIndex++;
3108 std::string postcodeEntryID =
3109 "B" + std::to_string(bootIndex) + "-" +
3110 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3111
3112 uint64_t usecSinceEpoch = code.first;
3113 uint64_t usTimeOffset = 0;
3114
3115 if (1 == currentCodeIndex)
3116 { // already incremented
3117 firstCodeTimeUs = code.first;
3118 }
3119 else
3120 {
3121 usTimeOffset = code.first - firstCodeTimeUs;
3122 }
3123
3124 // skip if no specific codeIndex is specified and currentCodeIndex does
3125 // not fall between top and skip
3126 if ((codeIndex == 0) &&
3127 (currentCodeIndex <= skip || currentCodeIndex > top))
3128 {
3129 continue;
3130 }
3131
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003132 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003133 // currentIndex
3134 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3135 {
3136 // This is done for simplicity. 1st entry is needed to calculate
3137 // time offset. To improve efficiency, one can get to the entry
3138 // directly (possibly with flatmap's nth method)
3139 continue;
3140 }
3141
3142 // currentCodeIndex is within top and skip or equal to specified code
3143 // index
3144
3145 // Get the Created time from the timestamp
3146 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003147 entryTimeStr =
3148 crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003149
3150 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3151 std::ostringstream hexCode;
3152 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303153 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003154 std::ostringstream timeOffsetStr;
3155 // Set Fixed -Point Notation
3156 timeOffsetStr << std::fixed;
3157 // Set precision to 4 digits
3158 timeOffsetStr << std::setprecision(4);
3159 // Add double to stream
3160 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3161 std::vector<std::string> messageArgs = {
3162 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3163
3164 // Get MessageArgs template from message registry
3165 std::string msg;
3166 if (message != nullptr)
3167 {
3168 msg = message->message;
3169
3170 // fill in this post code value
3171 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003172 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003173 {
3174 std::string argStr = "%" + std::to_string(++i);
3175 size_t argPos = msg.find(argStr);
3176 if (argPos != std::string::npos)
3177 {
3178 msg.replace(argPos, argStr.length(), messageArg);
3179 }
3180 }
3181 }
3182
Tim Leed4342a92020-04-27 11:47:58 +08003183 // Get Severity template from message registry
3184 std::string severity;
3185 if (message != nullptr)
3186 {
3187 severity = message->severity;
3188 }
3189
ZhikuiRena3316fc2020-01-29 14:58:08 -08003190 // add to AsyncResp
3191 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003192 nlohmann::json& bmcLogEntry = logEntryArray.back();
George Liu0fda0f12021-11-16 10:06:17 +08003193 bmcLogEntry = {
3194 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
3195 {"@odata.id",
3196 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3197 postcodeEntryID},
3198 {"Name", "POST Code Log Entry"},
3199 {"Id", postcodeEntryID},
3200 {"Message", std::move(msg)},
3201 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3202 {"MessageArgs", std::move(messageArgs)},
3203 {"EntryType", "Event"},
3204 {"Severity", std::move(severity)},
3205 {"Created", entryTimeStr}};
George Liu647b3cd2021-07-05 12:43:56 +08003206 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3207 {
3208 bmcLogEntry["AdditionalDataURI"] =
3209 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3210 postcodeEntryID + "/attachment";
3211 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003212 }
3213}
3214
zhanghch058d1b46d2021-04-01 11:18:24 +08003215static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003216 const uint16_t bootIndex,
3217 const uint64_t codeIndex)
3218{
3219 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303220 [aResp, bootIndex,
3221 codeIndex](const boost::system::error_code ec,
3222 const boost::container::flat_map<
3223 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3224 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003225 if (ec)
3226 {
3227 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3228 messages::internalError(aResp->res);
3229 return;
3230 }
3231
3232 // skip the empty postcode boots
3233 if (postcode.empty())
3234 {
3235 return;
3236 }
3237
3238 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3239
3240 aResp->res.jsonValue["Members@odata.count"] =
3241 aResp->res.jsonValue["Members"].size();
3242 },
Jonathan Doman15124762021-01-07 17:54:17 -08003243 "xyz.openbmc_project.State.Boot.PostCode0",
3244 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003245 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3246 bootIndex);
3247}
3248
zhanghch058d1b46d2021-04-01 11:18:24 +08003249static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003250 const uint16_t bootIndex,
3251 const uint16_t bootCount,
3252 const uint64_t entryCount, const uint64_t skip,
3253 const uint64_t top)
3254{
3255 crow::connections::systemBus->async_method_call(
3256 [aResp, bootIndex, bootCount, entryCount, skip,
3257 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303258 const boost::container::flat_map<
3259 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3260 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003261 if (ec)
3262 {
3263 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3264 messages::internalError(aResp->res);
3265 return;
3266 }
3267
3268 uint64_t endCount = entryCount;
3269 if (!postcode.empty())
3270 {
3271 endCount = entryCount + postcode.size();
3272
3273 if ((skip < endCount) && ((top + skip) > entryCount))
3274 {
3275 uint64_t thisBootSkip =
3276 std::max(skip, entryCount) - entryCount;
3277 uint64_t thisBootTop =
3278 std::min(top + skip, endCount) - entryCount;
3279
3280 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3281 thisBootSkip, thisBootTop);
3282 }
3283 aResp->res.jsonValue["Members@odata.count"] = endCount;
3284 }
3285
3286 // continue to previous bootIndex
3287 if (bootIndex < bootCount)
3288 {
3289 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3290 bootCount, endCount, skip, top);
3291 }
3292 else
3293 {
3294 aResp->res.jsonValue["Members@odata.nextLink"] =
George Liu0fda0f12021-11-16 10:06:17 +08003295 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
ZhikuiRena3316fc2020-01-29 14:58:08 -08003296 std::to_string(skip + top);
3297 }
3298 },
Jonathan Doman15124762021-01-07 17:54:17 -08003299 "xyz.openbmc_project.State.Boot.PostCode0",
3300 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003301 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3302 bootIndex);
3303}
3304
zhanghch058d1b46d2021-04-01 11:18:24 +08003305static void
3306 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3307 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003308{
3309 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003310 sdbusplus::asio::getProperty<uint16_t>(
3311 *crow::connections::systemBus,
3312 "xyz.openbmc_project.State.Boot.PostCode0",
3313 "/xyz/openbmc_project/State/Boot/PostCode0",
3314 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3315 [aResp, entryCount, skip, top](const boost::system::error_code ec,
3316 const uint16_t bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003317 if (ec)
3318 {
3319 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3320 messages::internalError(aResp->res);
3321 return;
3322 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003323 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
3324 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003325}
3326
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003327inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003328{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003329 BMCWEB_ROUTE(app,
3330 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003331 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003332 .methods(boost::beast::http::verb::get)(
3333 [](const crow::Request& req,
3334 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3335 asyncResp->res.jsonValue["@odata.type"] =
3336 "#LogEntryCollection.LogEntryCollection";
3337 asyncResp->res.jsonValue["@odata.id"] =
3338 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3339 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3340 asyncResp->res.jsonValue["Description"] =
3341 "Collection of POST Code Log Entries";
3342 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3343 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003344
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003345 uint64_t skip = 0;
3346 uint64_t top = maxEntriesPerPage; // Show max entries by default
3347 if (!getSkipParam(asyncResp, req, skip))
3348 {
3349 return;
3350 }
3351 if (!getTopParam(asyncResp, req, top))
3352 {
3353 return;
3354 }
3355 getCurrentBootNumber(asyncResp, skip, top);
3356 });
3357}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003358
George Liu647b3cd2021-07-05 12:43:56 +08003359/**
3360 * @brief Parse post code ID and get the current value and index value
3361 * eg: postCodeID=B1-2, currentValue=1, index=2
3362 *
3363 * @param[in] postCodeID Post Code ID
3364 * @param[out] currentValue Current value
3365 * @param[out] index Index value
3366 *
3367 * @return bool true if the parsing is successful, false the parsing fails
3368 */
3369inline static bool parsePostCode(const std::string& postCodeID,
3370 uint64_t& currentValue, uint16_t& index)
3371{
3372 std::vector<std::string> split;
3373 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3374 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3375 {
3376 return false;
3377 }
3378
Ed Tanousca45aa32022-01-07 09:28:45 -08003379 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003380 const char* start = split[0].data() + 1;
Ed Tanousca45aa32022-01-07 09:28:45 -08003381 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003382 const char* end = split[0].data() + split[0].size();
3383 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3384
3385 if (ptrIndex != end || ecIndex != std::errc())
3386 {
3387 return false;
3388 }
3389
3390 start = split[1].data();
Ed Tanousca45aa32022-01-07 09:28:45 -08003391
3392 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003393 end = split[1].data() + split[1].size();
3394 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3395 if (ptrValue != end || ecValue != std::errc())
3396 {
3397 return false;
3398 }
3399
3400 return true;
3401}
3402
3403inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3404{
George Liu0fda0f12021-11-16 10:06:17 +08003405 BMCWEB_ROUTE(
3406 app,
3407 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003408 .privileges(redfish::privileges::getLogEntry)
3409 .methods(boost::beast::http::verb::get)(
3410 [](const crow::Request& req,
3411 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3412 const std::string& postCodeID) {
3413 if (!http_helpers::isOctetAccepted(
3414 req.getHeaderValue("Accept")))
3415 {
3416 asyncResp->res.result(
3417 boost::beast::http::status::bad_request);
3418 return;
3419 }
3420
3421 uint64_t currentValue = 0;
3422 uint16_t index = 0;
3423 if (!parsePostCode(postCodeID, currentValue, index))
3424 {
3425 messages::resourceNotFound(asyncResp->res, "LogEntry",
3426 postCodeID);
3427 return;
3428 }
3429
3430 crow::connections::systemBus->async_method_call(
3431 [asyncResp, postCodeID, currentValue](
3432 const boost::system::error_code ec,
3433 const std::vector<std::tuple<
3434 uint64_t, std::vector<uint8_t>>>& postcodes) {
3435 if (ec.value() == EBADR)
3436 {
3437 messages::resourceNotFound(asyncResp->res,
3438 "LogEntry", postCodeID);
3439 return;
3440 }
3441 if (ec)
3442 {
3443 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3444 messages::internalError(asyncResp->res);
3445 return;
3446 }
3447
3448 size_t value = static_cast<size_t>(currentValue) - 1;
3449 if (value == std::string::npos ||
3450 postcodes.size() < currentValue)
3451 {
3452 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3453 messages::resourceNotFound(asyncResp->res,
3454 "LogEntry", postCodeID);
3455 return;
3456 }
3457
Ed Tanous46ff87b2022-01-07 09:25:51 -08003458 auto& [tID, c] = postcodes[value];
3459 if (c.empty())
George Liu647b3cd2021-07-05 12:43:56 +08003460 {
3461 BMCWEB_LOG_INFO << "No found post code data";
3462 messages::resourceNotFound(asyncResp->res,
3463 "LogEntry", postCodeID);
3464 return;
3465 }
Ed Tanous46ff87b2022-01-07 09:25:51 -08003466 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3467 const char* d = reinterpret_cast<const char*>(c.data());
3468 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08003469
3470 asyncResp->res.addHeader("Content-Type",
3471 "application/octet-stream");
3472 asyncResp->res.addHeader("Content-Transfer-Encoding",
3473 "Base64");
3474 asyncResp->res.body() =
3475 crow::utility::base64encode(strData);
3476 },
3477 "xyz.openbmc_project.State.Boot.PostCode0",
3478 "/xyz/openbmc_project/State/Boot/PostCode0",
3479 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3480 index);
3481 });
3482}
3483
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003484inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003485{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003486 BMCWEB_ROUTE(
3487 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003488 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003489 .methods(boost::beast::http::verb::get)(
3490 [](const crow::Request&,
3491 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3492 const std::string& targetID) {
George Liu647b3cd2021-07-05 12:43:56 +08003493 uint16_t bootIndex = 0;
3494 uint64_t codeIndex = 0;
3495 if (!parsePostCode(targetID, codeIndex, bootIndex))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003496 {
3497 // Requested ID was not found
3498 messages::resourceMissingAtURI(asyncResp->res, targetID);
3499 return;
3500 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003501 if (bootIndex == 0 || codeIndex == 0)
3502 {
3503 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3504 << targetID;
3505 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003506
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003507 asyncResp->res.jsonValue["@odata.type"] =
3508 "#LogEntry.v1_4_0.LogEntry";
3509 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08003510 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003511 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3512 asyncResp->res.jsonValue["Description"] =
3513 "Collection of POST Code Log Entries";
3514 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3515 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003516
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003517 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3518 });
3519}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003520
Ed Tanous1da66f72018-07-27 16:13:37 -07003521} // namespace redfish