blob: 3b9069f1796607deabea81a5c06c3c342b72bd17 [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
Jason M. Bills4851d452019-03-28 11:27:48 -070018#include "registries.hpp"
19#include "registries/base_message_registry.hpp"
20#include "registries/openbmc_message_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080021#include "task.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070022
Jason M. Billse1f26342018-07-18 12:12:00 -070023#include <systemd/sd-journal.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060024#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070025
John Edward Broadbent7e860f12021-04-08 15:57:16 -070026#include <app.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060027#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070028#include <boost/algorithm/string/split.hpp>
29#include <boost/beast/core/span.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060030#include <boost/beast/http.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070031#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080032#include <boost/system/linux_error.hpp>
Andrew Geisslercb92c032018-08-17 07:56:14 -070033#include <error_messages.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070034#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050035
James Feist4418c7f2019-04-15 11:09:15 -070036#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080037#include <optional>
Jason M. Billscd225da2019-05-08 15:31:57 -070038#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080039#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070040
41namespace redfish
42{
43
Gunnar Mills1214b7e2020-06-04 10:11:30 -050044constexpr char const* crashdumpObject = "com.intel.crashdump";
45constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050046constexpr char const* crashdumpInterface = "com.intel.crashdump";
47constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070048 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050049constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070050 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070051constexpr char const* crashdumpTelemetryInterface =
52 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070053
Jason M. Bills4851d452019-03-28 11:27:48 -070054namespace message_registries
55{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050056static const Message* getMessageFromRegistry(
57 const std::string& messageKey,
Jason M. Bills4851d452019-03-28 11:27:48 -070058 const boost::beast::span<const MessageEntry> registry)
59{
60 boost::beast::span<const MessageEntry>::const_iterator messageIt =
61 std::find_if(registry.cbegin(), registry.cend(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -050062 [&messageKey](const MessageEntry& messageEntry) {
Jason M. Bills4851d452019-03-28 11:27:48 -070063 return !std::strcmp(messageEntry.first,
64 messageKey.c_str());
65 });
66 if (messageIt != registry.cend())
67 {
68 return &messageIt->second;
69 }
70
71 return nullptr;
72}
73
Gunnar Mills1214b7e2020-06-04 10:11:30 -050074static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070075{
76 // Redfish MessageIds are in the form
77 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
78 // the right Message
79 std::vector<std::string> fields;
80 fields.reserve(4);
81 boost::split(fields, messageID, boost::is_any_of("."));
Gunnar Mills1214b7e2020-06-04 10:11:30 -050082 std::string& registryName = fields[0];
83 std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -070084
85 // Find the right registry and check it for the MessageKey
86 if (std::string(base::header.registryPrefix) == registryName)
87 {
88 return getMessageFromRegistry(
89 messageKey, boost::beast::span<const MessageEntry>(base::registry));
90 }
91 if (std::string(openbmc::header.registryPrefix) == registryName)
92 {
93 return getMessageFromRegistry(
94 messageKey,
95 boost::beast::span<const MessageEntry>(openbmc::registry));
96 }
97 return nullptr;
98}
99} // namespace message_registries
100
James Feistf6150402019-01-08 10:36:20 -0800101namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700102
Andrew Geisslercb92c032018-08-17 07:56:14 -0700103using GetManagedPropertyType = boost::container::flat_map<
Patrick Williams19bd78d2020-05-13 17:38:24 -0500104 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
105 int32_t, uint32_t, int64_t, uint64_t, double>>;
Andrew Geisslercb92c032018-08-17 07:56:14 -0700106
107using GetManagedObjectsType = boost::container::flat_map<
108 sdbusplus::message::object_path,
109 boost::container::flat_map<std::string, GetManagedPropertyType>>;
110
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500111inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700112{
Ed Tanousd4d25792020-09-29 15:15:03 -0700113 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
114 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
115 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
116 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700117 {
118 return "Critical";
119 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700120 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
121 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
122 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700123 {
124 return "OK";
125 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700126 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700127 {
128 return "Warning";
129 }
130 return "";
131}
132
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700133inline static int getJournalMetadata(sd_journal* journal,
134 const std::string_view& field,
135 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700136{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500137 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700138 size_t length = 0;
139 int ret = 0;
140 // Get the metadata from the requested field of the journal entry
Ed Tanous271584a2019-07-09 16:24:22 -0700141 ret = sd_journal_get_data(journal, field.data(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500142 reinterpret_cast<const void**>(&data), &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 }
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500181 entryTimestamp = crow::utility::getDateTime(
182 static_cast<std::time_t>(timestamp / 1000 / 1000));
183 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800184}
185
zhanghch058d1b46d2021-04-01 11:18:24 +0800186static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
187 const crow::Request& req, uint64_t& skip)
Jason M. Bills16428a12018-11-02 12:42:29 -0700188{
James Feist5a7e8772020-07-22 09:08:38 -0700189 boost::urls::url_view::params_type::iterator it =
190 req.urlParams.find("$skip");
191 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700192 {
James Feist5a7e8772020-07-22 09:08:38 -0700193 std::string skipParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500194 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700195 skip = std::strtoul(skipParam.c_str(), &ptr, 10);
196 if (skipParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700197 {
198
zhanghch058d1b46d2021-04-01 11:18:24 +0800199 messages::queryParameterValueTypeError(
200 asyncResp->res, std::string(skipParam), "$skip");
Jason M. Bills16428a12018-11-02 12:42:29 -0700201 return false;
202 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700203 }
204 return true;
205}
206
Ed Tanous271584a2019-07-09 16:24:22 -0700207static constexpr const uint64_t maxEntriesPerPage = 1000;
zhanghch058d1b46d2021-04-01 11:18:24 +0800208static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
209 const crow::Request& req, uint64_t& top)
Jason M. Bills16428a12018-11-02 12:42:29 -0700210{
James Feist5a7e8772020-07-22 09:08:38 -0700211 boost::urls::url_view::params_type::iterator it =
212 req.urlParams.find("$top");
213 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700214 {
James Feist5a7e8772020-07-22 09:08:38 -0700215 std::string topParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500216 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700217 top = std::strtoul(topParam.c_str(), &ptr, 10);
218 if (topParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700219 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800220 messages::queryParameterValueTypeError(
221 asyncResp->res, std::string(topParam), "$top");
Jason M. Bills16428a12018-11-02 12:42:29 -0700222 return false;
223 }
Ed Tanous271584a2019-07-09 16:24:22 -0700224 if (top < 1U || top > maxEntriesPerPage)
Jason M. Bills16428a12018-11-02 12:42:29 -0700225 {
226
227 messages::queryParameterOutOfRange(
zhanghch058d1b46d2021-04-01 11:18:24 +0800228 asyncResp->res, std::to_string(top), "$top",
Jason M. Bills16428a12018-11-02 12:42:29 -0700229 "1-" + std::to_string(maxEntriesPerPage));
230 return false;
231 }
232 }
233 return true;
234}
235
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700236inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
237 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700238{
239 int ret = 0;
240 static uint64_t prevTs = 0;
241 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700242 if (firstEntry)
243 {
244 prevTs = 0;
245 }
246
Jason M. Bills16428a12018-11-02 12:42:29 -0700247 // Get the entry timestamp
248 uint64_t curTs = 0;
249 ret = sd_journal_get_realtime_usec(journal, &curTs);
250 if (ret < 0)
251 {
252 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
253 << strerror(-ret);
254 return false;
255 }
256 // If the timestamp isn't unique, increment the index
257 if (curTs == prevTs)
258 {
259 index++;
260 }
261 else
262 {
263 // Otherwise, reset it
264 index = 0;
265 }
266 // Save the timestamp
267 prevTs = curTs;
268
269 entryID = std::to_string(curTs);
270 if (index > 0)
271 {
272 entryID += "_" + std::to_string(index);
273 }
274 return true;
275}
276
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500277static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700278 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700279{
Ed Tanous271584a2019-07-09 16:24:22 -0700280 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700281 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700282 if (firstEntry)
283 {
284 prevTs = 0;
285 }
286
Jason M. Bills95820182019-04-22 16:25:34 -0700287 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700288 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700289 std::tm timeStruct = {};
290 std::istringstream entryStream(logEntry);
291 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
292 {
293 curTs = std::mktime(&timeStruct);
294 }
295 // If the timestamp isn't unique, increment the index
296 if (curTs == prevTs)
297 {
298 index++;
299 }
300 else
301 {
302 // Otherwise, reset it
303 index = 0;
304 }
305 // Save the timestamp
306 prevTs = curTs;
307
308 entryID = std::to_string(curTs);
309 if (index > 0)
310 {
311 entryID += "_" + std::to_string(index);
312 }
313 return true;
314}
315
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700316inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800317 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
318 const std::string& entryID, uint64_t& timestamp,
319 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700320{
321 if (entryID.empty())
322 {
323 return false;
324 }
325 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800326 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700327
Ed Tanous81ce6092020-12-17 16:54:55 +0000328 auto underscorePos = tsStr.find('_');
Jason M. Bills16428a12018-11-02 12:42:29 -0700329 if (underscorePos != tsStr.npos)
330 {
331 // Timestamp has an index
332 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800333 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700334 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700335 auto [ptr, ec] = std::from_chars(
336 indexStr.data(), indexStr.data() + indexStr.size(), index);
337 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700338 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800339 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700340 return false;
341 }
342 }
343 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700344 auto [ptr, ec] =
345 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
346 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700347 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800348 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700349 return false;
350 }
351 return true;
352}
353
Jason M. Bills95820182019-04-22 16:25:34 -0700354static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500355 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700356{
357 static const std::filesystem::path redfishLogDir = "/var/log";
358 static const std::string redfishLogFilename = "redfish";
359
360 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500361 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700362 std::filesystem::directory_iterator(redfishLogDir))
363 {
364 // If we find a redfish log file, save the path
365 std::string filename = dirEnt.path().filename();
366 if (boost::starts_with(filename, redfishLogFilename))
367 {
368 redfishLogFiles.emplace_back(redfishLogDir / filename);
369 }
370 }
371 // As the log files rotate, they are appended with a ".#" that is higher for
372 // the older logs. Since we don't expect more than 10 log files, we
373 // can just sort the list to get them in order from newest to oldest
374 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
375
376 return !redfishLogFiles.empty();
377}
378
zhanghch058d1b46d2021-04-01 11:18:24 +0800379inline void
380 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
381 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500382{
383 std::string dumpPath;
384 if (dumpType == "BMC")
385 {
386 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
387 }
388 else if (dumpType == "System")
389 {
390 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
391 }
392 else
393 {
394 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
395 messages::internalError(asyncResp->res);
396 return;
397 }
398
399 crow::connections::systemBus->async_method_call(
400 [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
401 GetManagedObjectsType& resp) {
402 if (ec)
403 {
404 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
405 messages::internalError(asyncResp->res);
406 return;
407 }
408
409 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
410 entriesArray = nlohmann::json::array();
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500411 std::string dumpEntryPath =
412 "/xyz/openbmc_project/dump/" +
413 std::string(boost::algorithm::to_lower_copy(dumpType)) +
414 "/entry/";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500415
416 for (auto& object : resp)
417 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500418 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500419 {
420 continue;
421 }
422 std::time_t timestamp;
423 uint64_t size = 0;
424 entriesArray.push_back({});
425 nlohmann::json& thisEntry = entriesArray.back();
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000426
427 std::string entryID = object.first.filename();
428 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500429 {
430 continue;
431 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500432
433 for (auto& interfaceMap : object.second)
434 {
435 if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
436 {
437
438 for (auto& propertyMap : interfaceMap.second)
439 {
440 if (propertyMap.first == "Size")
441 {
442 auto sizePtr =
443 std::get_if<uint64_t>(&propertyMap.second);
444 if (sizePtr == nullptr)
445 {
446 messages::internalError(asyncResp->res);
447 break;
448 }
449 size = *sizePtr;
450 break;
451 }
452 }
453 }
454 else if (interfaceMap.first ==
455 "xyz.openbmc_project.Time.EpochTime")
456 {
457
458 for (auto& propertyMap : interfaceMap.second)
459 {
460 if (propertyMap.first == "Elapsed")
461 {
462 const uint64_t* usecsTimeStamp =
463 std::get_if<uint64_t>(&propertyMap.second);
464 if (usecsTimeStamp == nullptr)
465 {
466 messages::internalError(asyncResp->res);
467 break;
468 }
469 timestamp =
470 static_cast<std::time_t>(*usecsTimeStamp);
471 break;
472 }
473 }
474 }
475 }
476
Ed Tanousd0dbeef2021-07-01 08:46:46 -0700477 thisEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500478 thisEntry["@odata.id"] = dumpPath + entryID;
479 thisEntry["Id"] = entryID;
480 thisEntry["EntryType"] = "Event";
481 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
482 thisEntry["Name"] = dumpType + " Dump Entry";
483
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500484 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500485
486 if (dumpType == "BMC")
487 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500488 thisEntry["DiagnosticDataType"] = "Manager";
489 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500490 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
491 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500492 }
493 else if (dumpType == "System")
494 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500495 thisEntry["DiagnosticDataType"] = "OEM";
496 thisEntry["OEMDiagnosticDataType"] = "System";
497 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500498 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
499 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500500 }
501 }
502 asyncResp->res.jsonValue["Members@odata.count"] =
503 entriesArray.size();
504 },
505 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
506 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
507}
508
zhanghch058d1b46d2021-04-01 11:18:24 +0800509inline void
510 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
511 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512{
513 std::string dumpPath;
514 if (dumpType == "BMC")
515 {
516 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
517 }
518 else if (dumpType == "System")
519 {
520 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
521 }
522 else
523 {
524 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
525 messages::internalError(asyncResp->res);
526 return;
527 }
528
529 crow::connections::systemBus->async_method_call(
530 [asyncResp, entryID, dumpPath, dumpType](
531 const boost::system::error_code ec, GetManagedObjectsType& resp) {
532 if (ec)
533 {
534 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
535 messages::internalError(asyncResp->res);
536 return;
537 }
538
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500539 bool foundDumpEntry = false;
540 std::string dumpEntryPath =
541 "/xyz/openbmc_project/dump/" +
542 std::string(boost::algorithm::to_lower_copy(dumpType)) +
543 "/entry/";
544
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500545 for (auto& objectPath : resp)
546 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500547 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500548 {
549 continue;
550 }
551
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500552 foundDumpEntry = true;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500553 std::time_t timestamp;
554 uint64_t size = 0;
555
556 for (auto& interfaceMap : objectPath.second)
557 {
558 if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
559 {
560 for (auto& propertyMap : interfaceMap.second)
561 {
562 if (propertyMap.first == "Size")
563 {
564 auto sizePtr =
565 std::get_if<uint64_t>(&propertyMap.second);
566 if (sizePtr == nullptr)
567 {
568 messages::internalError(asyncResp->res);
569 break;
570 }
571 size = *sizePtr;
572 break;
573 }
574 }
575 }
576 else if (interfaceMap.first ==
577 "xyz.openbmc_project.Time.EpochTime")
578 {
579 for (auto& propertyMap : interfaceMap.second)
580 {
581 if (propertyMap.first == "Elapsed")
582 {
583 const uint64_t* usecsTimeStamp =
584 std::get_if<uint64_t>(&propertyMap.second);
585 if (usecsTimeStamp == nullptr)
586 {
587 messages::internalError(asyncResp->res);
588 break;
589 }
590 timestamp =
591 static_cast<std::time_t>(*usecsTimeStamp);
592 break;
593 }
594 }
595 }
596 }
597
598 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousd0dbeef2021-07-01 08:46:46 -0700599 "#LogEntry.v1_7_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500600 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
601 asyncResp->res.jsonValue["Id"] = entryID;
602 asyncResp->res.jsonValue["EntryType"] = "Event";
603 asyncResp->res.jsonValue["Created"] =
604 crow::utility::getDateTime(timestamp);
605 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
606
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500607 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500608
609 if (dumpType == "BMC")
610 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500611 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
612 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500613 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
614 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500615 }
616 else if (dumpType == "System")
617 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500618 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
619 asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500620 "System";
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500621 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500622 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
623 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500624 }
625 }
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500626 if (foundDumpEntry == false)
627 {
628 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
629 messages::internalError(asyncResp->res);
630 return;
631 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500632 },
633 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
634 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
635}
636
zhanghch058d1b46d2021-04-01 11:18:24 +0800637inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800638 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500639 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500640{
George Liu3de8d8b2021-03-22 17:49:39 +0800641 auto respHandler = [asyncResp,
642 entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500643 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
644 if (ec)
645 {
George Liu3de8d8b2021-03-22 17:49:39 +0800646 if (ec.value() == EBADR)
647 {
648 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
649 return;
650 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500651 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
652 << ec;
653 messages::internalError(asyncResp->res);
654 return;
655 }
656 };
657 crow::connections::systemBus->async_method_call(
658 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500659 "/xyz/openbmc_project/dump/" +
660 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
661 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500662 "xyz.openbmc_project.Object.Delete", "Delete");
663}
664
zhanghch058d1b46d2021-04-01 11:18:24 +0800665inline void
666 createDumpTaskCallback(const crow::Request& req,
667 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
668 const uint32_t& dumpId, const std::string& dumpPath,
669 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500670{
671 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500672 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500673 boost::system::error_code err, sdbusplus::message::message& m,
674 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000675 if (err)
676 {
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500677 BMCWEB_LOG_ERROR << "Error in creating a dump";
678 taskData->state = "Cancelled";
679 return task::completed;
Ed Tanouscb13a392020-07-25 19:02:03 +0000680 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500681 std::vector<std::pair<
682 std::string,
683 std::vector<std::pair<std::string, std::variant<std::string>>>>>
684 interfacesList;
685
686 sdbusplus::message::object_path objPath;
687
688 m.read(objPath, interfacesList);
689
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500690 if (objPath.str ==
691 "/xyz/openbmc_project/dump/" +
692 std::string(boost::algorithm::to_lower_copy(dumpType)) +
693 "/entry/" + std::to_string(dumpId))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500694 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500695 nlohmann::json retMessage = messages::success();
696 taskData->messages.emplace_back(retMessage);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500697
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500698 std::string headerLoc =
699 "Location: " + dumpPath + std::to_string(dumpId);
700 taskData->payload->httpHeaders.emplace_back(
701 std::move(headerLoc));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500702
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500703 taskData->state = "Completed";
704 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500705 }
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500706 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500707 },
708 "type='signal',interface='org.freedesktop.DBus."
709 "ObjectManager',"
710 "member='InterfacesAdded', "
711 "path='/xyz/openbmc_project/dump'");
712
713 task->startTimer(std::chrono::minutes(3));
714 task->populateResp(asyncResp->res);
715 task->payload.emplace(req);
716}
717
zhanghch058d1b46d2021-04-01 11:18:24 +0800718inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
719 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500720{
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500721
722 std::string dumpPath;
723 if (dumpType == "BMC")
724 {
725 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
726 }
727 else if (dumpType == "System")
728 {
729 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
730 }
731 else
732 {
733 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
734 messages::internalError(asyncResp->res);
735 return;
736 }
737
738 std::optional<std::string> diagnosticDataType;
739 std::optional<std::string> oemDiagnosticDataType;
740
741 if (!redfish::json_util::readJson(
742 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
743 "OEMDiagnosticDataType", oemDiagnosticDataType))
744 {
745 return;
746 }
747
748 if (dumpType == "System")
749 {
750 if (!oemDiagnosticDataType || !diagnosticDataType)
751 {
752 BMCWEB_LOG_ERROR << "CreateDump action parameter "
753 "'DiagnosticDataType'/"
754 "'OEMDiagnosticDataType' value not found!";
755 messages::actionParameterMissing(
756 asyncResp->res, "CollectDiagnosticData",
757 "DiagnosticDataType & OEMDiagnosticDataType");
758 return;
759 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700760 if ((*oemDiagnosticDataType != "System") ||
761 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500762 {
763 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
764 messages::invalidObject(asyncResp->res,
765 "System Dump creation parameters");
766 return;
767 }
768 }
769 else if (dumpType == "BMC")
770 {
771 if (!diagnosticDataType)
772 {
773 BMCWEB_LOG_ERROR << "CreateDump action parameter "
774 "'DiagnosticDataType' not found!";
775 messages::actionParameterMissing(
776 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
777 return;
778 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700779 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500780 {
781 BMCWEB_LOG_ERROR
782 << "Wrong parameter value passed for 'DiagnosticDataType'";
783 messages::invalidObject(asyncResp->res,
784 "BMC Dump creation parameters");
785 return;
786 }
787 }
788
789 crow::connections::systemBus->async_method_call(
790 [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
791 const uint32_t& dumpId) {
792 if (ec)
793 {
794 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
795 messages::internalError(asyncResp->res);
796 return;
797 }
798 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
799
800 createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
801 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500802 "xyz.openbmc_project.Dump.Manager",
803 "/xyz/openbmc_project/dump/" +
804 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500805 "xyz.openbmc_project.Dump.Create", "CreateDump");
806}
807
zhanghch058d1b46d2021-04-01 11:18:24 +0800808inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
809 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500810{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500811 std::string dumpTypeLowerCopy =
812 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800813
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500814 crow::connections::systemBus->async_method_call(
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500815 [asyncResp, dumpType](const boost::system::error_code ec,
816 const std::vector<std::string>& subTreePaths) {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500817 if (ec)
818 {
819 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
820 messages::internalError(asyncResp->res);
821 return;
822 }
823
824 for (const std::string& path : subTreePaths)
825 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000826 sdbusplus::message::object_path objPath(path);
827 std::string logID = objPath.filename();
828 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500829 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000830 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500831 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000832 deleteDumpEntry(asyncResp, logID, dumpType);
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500833 }
834 },
835 "xyz.openbmc_project.ObjectMapper",
836 "/xyz/openbmc_project/object_mapper",
837 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500838 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
839 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
840 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500841}
842
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700843inline static void parseCrashdumpParameters(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500844 const std::vector<std::pair<std::string, VariantType>>& params,
845 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700846{
847 for (auto property : params)
848 {
849 if (property.first == "Timestamp")
850 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500851 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500852 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700853 if (value != nullptr)
854 {
855 timestamp = *value;
856 }
857 }
858 else if (property.first == "Filename")
859 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500860 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500861 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700862 if (value != nullptr)
863 {
864 filename = *value;
865 }
866 }
867 else if (property.first == "Log")
868 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500869 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500870 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700871 if (value != nullptr)
872 {
873 logfile = *value;
874 }
875 }
876 }
877}
878
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500879constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700880inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700881{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800882 /**
883 * Functions triggers appropriate requests on DBus
884 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700885 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700886 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700887 .methods(boost::beast::http::verb::get)(
888 [](const crow::Request&,
889 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
890
891 {
892 // Collections don't include the static data added by SubRoute
893 // because it has a duplicate entry for members
894 asyncResp->res.jsonValue["@odata.type"] =
895 "#LogServiceCollection.LogServiceCollection";
896 asyncResp->res.jsonValue["@odata.id"] =
897 "/redfish/v1/Systems/system/LogServices";
898 asyncResp->res.jsonValue["Name"] =
899 "System Log Services Collection";
900 asyncResp->res.jsonValue["Description"] =
901 "Collection of LogServices for this Computer System";
902 nlohmann::json& logServiceArray =
903 asyncResp->res.jsonValue["Members"];
904 logServiceArray = nlohmann::json::array();
905 logServiceArray.push_back(
906 {{"@odata.id",
907 "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500908#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700909 logServiceArray.push_back(
910 {{"@odata.id",
911 "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600912#endif
913
Jason M. Billsd53dd412019-02-12 17:16:22 -0800914#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700915 logServiceArray.push_back(
916 {{"@odata.id",
917 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800918#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700919 asyncResp->res.jsonValue["Members@odata.count"] =
920 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800921
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700922 crow::connections::systemBus->async_method_call(
923 [asyncResp](const boost::system::error_code ec,
924 const std::vector<std::string>& subtreePath) {
925 if (ec)
926 {
927 BMCWEB_LOG_ERROR << ec;
928 return;
929 }
ZhikuiRena3316fc2020-01-29 14:58:08 -0800930
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700931 for (auto& pathStr : subtreePath)
932 {
933 if (pathStr.find("PostCode") != std::string::npos)
934 {
935 nlohmann::json& logServiceArrayLocal =
936 asyncResp->res.jsonValue["Members"];
937 logServiceArrayLocal.push_back(
938 {{"@odata.id", "/redfish/v1/Systems/system/"
939 "LogServices/PostCodes"}});
940 asyncResp->res
941 .jsonValue["Members@odata.count"] =
942 logServiceArrayLocal.size();
943 return;
944 }
945 }
946 },
947 "xyz.openbmc_project.ObjectMapper",
948 "/xyz/openbmc_project/object_mapper",
949 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
950 0, std::array<const char*, 1>{postCodeIface});
951 });
952}
953
954inline void requestRoutesEventLogService(App& app)
955{
956 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -0700957 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700958 .methods(
959 boost::beast::http::verb::
960 get)([](const crow::Request&,
961 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
962 asyncResp->res.jsonValue["@odata.id"] =
963 "/redfish/v1/Systems/system/LogServices/EventLog";
964 asyncResp->res.jsonValue["@odata.type"] =
965 "#LogService.v1_1_0.LogService";
966 asyncResp->res.jsonValue["Name"] = "Event Log Service";
967 asyncResp->res.jsonValue["Description"] =
968 "System Event Log Service";
969 asyncResp->res.jsonValue["Id"] = "EventLog";
970 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +0530971
972 std::pair<std::string, std::string> redfishDateTimeOffset =
973 crow::utility::getDateTimeOffsetNow();
974
975 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
976 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
977 redfishDateTimeOffset.second;
978
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700979 asyncResp->res.jsonValue["Entries"] = {
980 {"@odata.id",
981 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
982 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
983
984 {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
985 "Actions/LogService.ClearLog"}};
986 });
987}
988
989inline void requestRoutesJournalEventLogClear(App& app)
990{
991 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
992 "LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -0700993 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700994 .methods(boost::beast::http::verb::post)(
995 [](const crow::Request&,
996 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
997 // Clear the EventLog by deleting the log files
998 std::vector<std::filesystem::path> redfishLogFiles;
999 if (getRedfishLogFiles(redfishLogFiles))
ZhikuiRena3316fc2020-01-29 14:58:08 -08001000 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001001 for (const std::filesystem::path& file : redfishLogFiles)
ZhikuiRena3316fc2020-01-29 14:58:08 -08001002 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001003 std::error_code ec;
1004 std::filesystem::remove(file, ec);
ZhikuiRena3316fc2020-01-29 14:58:08 -08001005 }
1006 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001007
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001008 // Reload rsyslog so it knows to start new log files
1009 crow::connections::systemBus->async_method_call(
1010 [asyncResp](const boost::system::error_code ec) {
1011 if (ec)
1012 {
1013 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1014 << ec;
1015 messages::internalError(asyncResp->res);
1016 return;
1017 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001018
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001019 messages::success(asyncResp->res);
1020 },
1021 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1022 "org.freedesktop.systemd1.Manager", "ReloadUnit",
1023 "rsyslog.service", "replace");
1024 });
1025}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001026
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001027static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001028 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001029 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001030{
Jason M. Bills95820182019-04-22 16:25:34 -07001031 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001032 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001033 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001034 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001035 {
1036 return 1;
1037 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001038 std::string timestamp = logEntry.substr(0, space);
1039 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001040 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001041 if (entryStart == std::string::npos)
1042 {
1043 return 1;
1044 }
1045 std::string_view entry(logEntry);
1046 entry.remove_prefix(entryStart);
1047 // Use split to separate the entry into its fields
1048 std::vector<std::string> logEntryFields;
1049 boost::split(logEntryFields, entry, boost::is_any_of(","),
1050 boost::token_compress_on);
1051 // We need at least a MessageId to be valid
1052 if (logEntryFields.size() < 1)
1053 {
1054 return 1;
1055 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001056 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001057
Jason M. Bills4851d452019-03-28 11:27:48 -07001058 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001059 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001060 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001061
Jason M. Bills4851d452019-03-28 11:27:48 -07001062 std::string msg;
1063 std::string severity;
1064 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001065 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001066 msg = message->message;
1067 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001068 }
1069
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001070 // Get the MessageArgs from the log if there are any
1071 boost::beast::span<std::string> messageArgs;
1072 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001073 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001074 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001075 // If the first string is empty, assume there are no MessageArgs
1076 std::size_t messageArgsSize = 0;
1077 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001078 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001079 messageArgsSize = logEntryFields.size() - 1;
1080 }
1081
Ed Tanous23a21a12020-07-25 04:45:05 +00001082 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001083
1084 // Fill the MessageArgs into the Message
1085 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001086 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001087 {
1088 std::string argStr = "%" + std::to_string(++i);
1089 size_t argPos = msg.find(argStr);
1090 if (argPos != std::string::npos)
1091 {
1092 msg.replace(argPos, argStr.length(), messageArg);
1093 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001094 }
1095 }
1096
Jason M. Bills95820182019-04-22 16:25:34 -07001097 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1098 // format which matches the Redfish format except for the fractional seconds
1099 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001100 std::size_t dot = timestamp.find_first_of('.');
1101 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001102 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001103 {
Jason M. Bills95820182019-04-22 16:25:34 -07001104 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001105 }
1106
1107 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001108 logEntryJson = {
Ed Tanousd0dbeef2021-07-01 08:46:46 -07001109 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001110 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001111 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001112 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001113 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001114 {"Id", logEntryID},
1115 {"Message", std::move(msg)},
1116 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001117 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001118 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001119 {"Severity", std::move(severity)},
1120 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001121 return 0;
1122}
1123
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001124inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001125{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001126 BMCWEB_ROUTE(app,
1127 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001128 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001129 .methods(boost::beast::http::verb::get)(
1130 [](const crow::Request& req,
1131 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1132 uint64_t skip = 0;
1133 uint64_t top = maxEntriesPerPage; // Show max entries by default
1134 if (!getSkipParam(asyncResp, req, skip))
Jason M. Bills95820182019-04-22 16:25:34 -07001135 {
Jason M. Bills95820182019-04-22 16:25:34 -07001136 return;
1137 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001138 if (!getTopParam(asyncResp, req, top))
Jason M. Bills897967d2019-07-29 17:05:30 -07001139 {
Jason M. Bills897967d2019-07-29 17:05:30 -07001140 return;
1141 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001142 // Collections don't include the static data added by SubRoute
1143 // because it has a duplicate entry for members
1144 asyncResp->res.jsonValue["@odata.type"] =
1145 "#LogEntryCollection.LogEntryCollection";
1146 asyncResp->res.jsonValue["@odata.id"] =
1147 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1148 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1149 asyncResp->res.jsonValue["Description"] =
1150 "Collection of System Event Log Entries";
Jason M. Bills897967d2019-07-29 17:05:30 -07001151
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001152 nlohmann::json& logEntryArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001153 asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001154 logEntryArray = nlohmann::json::array();
1155 // Go through the log files and create a unique ID for each
1156 // entry
1157 std::vector<std::filesystem::path> redfishLogFiles;
1158 getRedfishLogFiles(redfishLogFiles);
1159 uint64_t entryCount = 0;
1160 std::string logEntry;
1161
1162 // Oldest logs are in the last file, so start there and loop
1163 // backwards
1164 for (auto it = redfishLogFiles.rbegin();
1165 it < redfishLogFiles.rend(); it++)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001166 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001167 std::ifstream logStream(*it);
1168 if (!logStream.is_open())
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001169 {
1170 continue;
1171 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001172
1173 // Reset the unique ID on the first entry
1174 bool firstEntry = true;
1175 while (std::getline(logStream, logEntry))
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001176 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001177 entryCount++;
1178 // Handle paging using skip (number of entries to skip
1179 // from the start) and top (number of entries to
1180 // display)
1181 if (entryCount <= skip || entryCount > skip + top)
George Liuebd45902020-08-26 14:21:10 +08001182 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001183 continue;
George Liuebd45902020-08-26 14:21:10 +08001184 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001185
1186 std::string idStr;
1187 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
George Liuebd45902020-08-26 14:21:10 +08001188 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001189 continue;
George Liuebd45902020-08-26 14:21:10 +08001190 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001191
1192 if (firstEntry)
1193 {
1194 firstEntry = false;
1195 }
1196
1197 logEntryArray.push_back({});
1198 nlohmann::json& bmcLogEntry = logEntryArray.back();
1199 if (fillEventLogEntryJson(idStr, logEntry,
1200 bmcLogEntry) != 0)
Xiaochao Ma75710de2021-01-21 17:56:02 +08001201 {
1202 messages::internalError(asyncResp->res);
1203 return;
1204 }
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001205 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001206 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001207 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1208 if (skip + top < entryCount)
Ed Tanous271584a2019-07-09 16:24:22 -07001209 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001210 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001211 "/redfish/v1/Systems/system/LogServices/EventLog/"
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001212 "Entries?$skip=" +
1213 std::to_string(skip + top);
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001214 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001215 });
1216}
Chicago Duan336e96c2019-07-15 14:22:08 +08001217
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001218inline void requestRoutesJournalEventLogEntry(App& app)
1219{
1220 BMCWEB_ROUTE(
1221 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001222 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001223 .methods(boost::beast::http::verb::get)(
1224 [](const crow::Request&,
1225 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1226 const std::string& param) {
1227 const std::string& targetID = param;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001228
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001229 // Go through the log files and check the unique ID for each
1230 // entry to find the target entry
1231 std::vector<std::filesystem::path> redfishLogFiles;
1232 getRedfishLogFiles(redfishLogFiles);
1233 std::string logEntry;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001234
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001235 // Oldest logs are in the last file, so start there and loop
1236 // backwards
1237 for (auto it = redfishLogFiles.rbegin();
1238 it < redfishLogFiles.rend(); it++)
1239 {
1240 std::ifstream logStream(*it);
1241 if (!logStream.is_open())
1242 {
1243 continue;
1244 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001245
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001246 // Reset the unique ID on the first entry
1247 bool firstEntry = true;
1248 while (std::getline(logStream, logEntry))
1249 {
1250 std::string idStr;
1251 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1252 {
1253 continue;
1254 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001255
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001256 if (firstEntry)
1257 {
1258 firstEntry = false;
1259 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001260
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001261 if (idStr == targetID)
1262 {
1263 if (fillEventLogEntryJson(
1264 idStr, logEntry,
1265 asyncResp->res.jsonValue) != 0)
1266 {
1267 messages::internalError(asyncResp->res);
1268 return;
1269 }
1270 return;
1271 }
1272 }
1273 }
1274 // Requested ID was not found
1275 messages::resourceMissingAtURI(asyncResp->res, targetID);
1276 });
1277}
1278
1279inline void requestRoutesDBusEventLogEntryCollection(App& app)
1280{
1281 BMCWEB_ROUTE(app,
1282 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001283 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001284 .methods(
1285 boost::beast::http::verb::
1286 get)([](const crow::Request&,
1287 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1288 // Collections don't include the static data added by SubRoute
1289 // because it has a duplicate entry for members
1290 asyncResp->res.jsonValue["@odata.type"] =
1291 "#LogEntryCollection.LogEntryCollection";
1292 asyncResp->res.jsonValue["@odata.id"] =
1293 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1294 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1295 asyncResp->res.jsonValue["Description"] =
1296 "Collection of System Event Log Entries";
1297
1298 // DBus implementation of EventLog/Entries
1299 // Make call to Logging Service to find all log entry objects
Xiaochao Ma75710de2021-01-21 17:56:02 +08001300 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001301 [asyncResp](const boost::system::error_code ec,
1302 GetManagedObjectsType& resp) {
Xiaochao Ma75710de2021-01-21 17:56:02 +08001303 if (ec)
1304 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001305 // TODO Handle for specific error code
1306 BMCWEB_LOG_ERROR
1307 << "getLogEntriesIfaceData resp_handler got error "
1308 << ec;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001309 messages::internalError(asyncResp->res);
1310 return;
1311 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001312 nlohmann::json& entriesArray =
1313 asyncResp->res.jsonValue["Members"];
1314 entriesArray = nlohmann::json::array();
1315 for (auto& objectPath : resp)
1316 {
1317 uint32_t* id = nullptr;
1318 std::time_t timestamp{};
1319 std::time_t updateTimestamp{};
1320 std::string* severity = nullptr;
1321 std::string* message = nullptr;
1322 std::string* filePath = nullptr;
1323 bool resolved = false;
1324 for (auto& interfaceMap : objectPath.second)
1325 {
1326 if (interfaceMap.first ==
1327 "xyz.openbmc_project.Logging.Entry")
1328 {
1329 for (auto& propertyMap : interfaceMap.second)
1330 {
1331 if (propertyMap.first == "Id")
1332 {
1333 id = std::get_if<uint32_t>(
1334 &propertyMap.second);
1335 }
1336 else if (propertyMap.first == "Timestamp")
1337 {
1338 const uint64_t* millisTimeStamp =
1339 std::get_if<uint64_t>(
1340 &propertyMap.second);
1341 if (millisTimeStamp != nullptr)
1342 {
1343 timestamp =
1344 crow::utility::getTimestamp(
1345 *millisTimeStamp);
1346 }
1347 }
1348 else if (propertyMap.first ==
1349 "UpdateTimestamp")
1350 {
1351 const uint64_t* millisTimeStamp =
1352 std::get_if<uint64_t>(
1353 &propertyMap.second);
1354 if (millisTimeStamp != nullptr)
1355 {
1356 updateTimestamp =
1357 crow::utility::getTimestamp(
1358 *millisTimeStamp);
1359 }
1360 }
1361 else if (propertyMap.first == "Severity")
1362 {
1363 severity = std::get_if<std::string>(
1364 &propertyMap.second);
1365 }
1366 else if (propertyMap.first == "Message")
1367 {
1368 message = std::get_if<std::string>(
1369 &propertyMap.second);
1370 }
1371 else if (propertyMap.first == "Resolved")
1372 {
1373 bool* resolveptr = std::get_if<bool>(
1374 &propertyMap.second);
1375 if (resolveptr == nullptr)
1376 {
1377 messages::internalError(
1378 asyncResp->res);
1379 return;
1380 }
1381 resolved = *resolveptr;
1382 }
1383 }
1384 if (id == nullptr || message == nullptr ||
1385 severity == nullptr)
1386 {
1387 messages::internalError(asyncResp->res);
1388 return;
1389 }
1390 }
1391 else if (interfaceMap.first ==
1392 "xyz.openbmc_project.Common.FilePath")
1393 {
1394 for (auto& propertyMap : interfaceMap.second)
1395 {
1396 if (propertyMap.first == "Path")
1397 {
1398 filePath = std::get_if<std::string>(
1399 &propertyMap.second);
1400 }
1401 }
1402 }
1403 }
1404 // Object path without the
1405 // xyz.openbmc_project.Logging.Entry interface, ignore
1406 // and continue.
1407 if (id == nullptr || message == nullptr ||
1408 severity == nullptr)
1409 {
1410 continue;
1411 }
1412 entriesArray.push_back({});
1413 nlohmann::json& thisEntry = entriesArray.back();
1414 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1415 thisEntry["@odata.id"] =
1416 "/redfish/v1/Systems/system/"
1417 "LogServices/EventLog/Entries/" +
1418 std::to_string(*id);
1419 thisEntry["Name"] = "System Event Log Entry";
1420 thisEntry["Id"] = std::to_string(*id);
1421 thisEntry["Message"] = *message;
1422 thisEntry["Resolved"] = resolved;
1423 thisEntry["EntryType"] = "Event";
1424 thisEntry["Severity"] =
1425 translateSeverityDbusToRedfish(*severity);
1426 thisEntry["Created"] =
1427 crow::utility::getDateTime(timestamp);
1428 thisEntry["Modified"] =
1429 crow::utility::getDateTime(updateTimestamp);
1430 if (filePath != nullptr)
1431 {
1432 thisEntry["AdditionalDataURI"] =
1433 "/redfish/v1/Systems/system/LogServices/"
1434 "EventLog/"
1435 "Entries/" +
1436 std::to_string(*id) + "/attachment";
1437 }
1438 }
1439 std::sort(entriesArray.begin(), entriesArray.end(),
1440 [](const nlohmann::json& left,
1441 const nlohmann::json& right) {
1442 return (left["Id"] <= right["Id"]);
1443 });
1444 asyncResp->res.jsonValue["Members@odata.count"] =
1445 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001446 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001447 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1448 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1449 });
1450}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001451
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001452inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001453{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001454 BMCWEB_ROUTE(
1455 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001456 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001457 .methods(boost::beast::http::verb::get)(
1458 [](const crow::Request&,
1459 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1460 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001461
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001462 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001463 std::string entryID = param;
1464 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001465
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001466 // DBus implementation of EventLog/Entries
1467 // Make call to Logging Service to find all log entry objects
1468 crow::connections::systemBus->async_method_call(
1469 [asyncResp, entryID](const boost::system::error_code ec,
1470 GetManagedPropertyType& resp) {
1471 if (ec.value() == EBADR)
1472 {
1473 messages::resourceNotFound(
1474 asyncResp->res, "EventLogEntry", entryID);
1475 return;
1476 }
1477 if (ec)
1478 {
1479 BMCWEB_LOG_ERROR << "EventLogEntry (DBus) "
1480 "resp_handler got error "
1481 << ec;
1482 messages::internalError(asyncResp->res);
1483 return;
1484 }
1485 uint32_t* id = nullptr;
1486 std::time_t timestamp{};
1487 std::time_t updateTimestamp{};
1488 std::string* severity = nullptr;
1489 std::string* message = nullptr;
1490 std::string* filePath = nullptr;
1491 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001492
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001493 for (auto& propertyMap : resp)
1494 {
1495 if (propertyMap.first == "Id")
1496 {
1497 id = std::get_if<uint32_t>(&propertyMap.second);
1498 }
1499 else if (propertyMap.first == "Timestamp")
1500 {
1501 const uint64_t* millisTimeStamp =
1502 std::get_if<uint64_t>(&propertyMap.second);
1503 if (millisTimeStamp != nullptr)
1504 {
1505 timestamp = crow::utility::getTimestamp(
1506 *millisTimeStamp);
1507 }
1508 }
1509 else if (propertyMap.first == "UpdateTimestamp")
1510 {
1511 const uint64_t* millisTimeStamp =
1512 std::get_if<uint64_t>(&propertyMap.second);
1513 if (millisTimeStamp != nullptr)
1514 {
1515 updateTimestamp =
1516 crow::utility::getTimestamp(
1517 *millisTimeStamp);
1518 }
1519 }
1520 else if (propertyMap.first == "Severity")
1521 {
1522 severity = std::get_if<std::string>(
1523 &propertyMap.second);
1524 }
1525 else if (propertyMap.first == "Message")
1526 {
1527 message = std::get_if<std::string>(
1528 &propertyMap.second);
1529 }
1530 else if (propertyMap.first == "Resolved")
1531 {
1532 bool* resolveptr =
1533 std::get_if<bool>(&propertyMap.second);
1534 if (resolveptr == nullptr)
1535 {
1536 messages::internalError(asyncResp->res);
1537 return;
1538 }
1539 resolved = *resolveptr;
1540 }
1541 else if (propertyMap.first == "Path")
1542 {
1543 filePath = std::get_if<std::string>(
1544 &propertyMap.second);
1545 }
1546 }
1547 if (id == nullptr || message == nullptr ||
1548 severity == nullptr)
1549 {
1550 messages::internalError(asyncResp->res);
1551 return;
1552 }
1553 asyncResp->res.jsonValue["@odata.type"] =
1554 "#LogEntry.v1_8_0.LogEntry";
1555 asyncResp->res.jsonValue["@odata.id"] =
1556 "/redfish/v1/Systems/system/LogServices/EventLog/"
1557 "Entries/" +
1558 std::to_string(*id);
1559 asyncResp->res.jsonValue["Name"] =
1560 "System Event Log Entry";
1561 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1562 asyncResp->res.jsonValue["Message"] = *message;
1563 asyncResp->res.jsonValue["Resolved"] = resolved;
1564 asyncResp->res.jsonValue["EntryType"] = "Event";
1565 asyncResp->res.jsonValue["Severity"] =
1566 translateSeverityDbusToRedfish(*severity);
1567 asyncResp->res.jsonValue["Created"] =
1568 crow::utility::getDateTime(timestamp);
1569 asyncResp->res.jsonValue["Modified"] =
1570 crow::utility::getDateTime(updateTimestamp);
1571 if (filePath != nullptr)
1572 {
1573 asyncResp->res.jsonValue["AdditionalDataURI"] =
1574 "/redfish/v1/Systems/system/LogServices/"
1575 "EventLog/"
1576 "attachment/" +
1577 std::to_string(*id);
1578 }
1579 },
1580 "xyz.openbmc_project.Logging",
1581 "/xyz/openbmc_project/logging/entry/" + entryID,
1582 "org.freedesktop.DBus.Properties", "GetAll", "");
1583 });
1584
1585 BMCWEB_ROUTE(
1586 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001587 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001588 .methods(boost::beast::http::verb::patch)(
1589 [](const crow::Request& req,
1590 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1591 const std::string& entryId) {
1592 std::optional<bool> resolved;
1593
1594 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1595 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001596 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001597 return;
1598 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001599 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001600
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001601 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001602 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001603 if (ec)
1604 {
1605 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1606 messages::internalError(asyncResp->res);
1607 return;
1608 }
1609 },
1610 "xyz.openbmc_project.Logging",
1611 "/xyz/openbmc_project/logging/entry/" + entryId,
1612 "org.freedesktop.DBus.Properties", "Set",
1613 "xyz.openbmc_project.Logging.Entry", "Resolved",
1614 std::variant<bool>(*resolved));
1615 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001616
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001617 BMCWEB_ROUTE(
1618 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001619 .privileges(redfish::privileges::deleteLogEntry)
1620
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001621 .methods(boost::beast::http::verb::delete_)(
1622 [](const crow::Request&,
1623 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1624 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001625
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001626 {
1627 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001628
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001629 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001630
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001631 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001632
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001633 // Process response from Logging service.
1634 auto respHandler = [asyncResp, entryID](
1635 const boost::system::error_code ec) {
1636 BMCWEB_LOG_DEBUG
1637 << "EventLogEntry (DBus) doDelete callback: Done";
1638 if (ec)
1639 {
1640 if (ec.value() == EBADR)
1641 {
1642 messages::resourceNotFound(asyncResp->res,
1643 "LogEntry", entryID);
1644 return;
1645 }
1646 // TODO Handle for specific error code
1647 BMCWEB_LOG_ERROR << "EventLogEntry (DBus) doDelete "
1648 "respHandler got error "
1649 << ec;
1650 asyncResp->res.result(
1651 boost::beast::http::status::internal_server_error);
1652 return;
1653 }
1654
1655 asyncResp->res.result(boost::beast::http::status::ok);
1656 };
1657
1658 // Make call to Logging service to request Delete Log
1659 crow::connections::systemBus->async_method_call(
1660 respHandler, "xyz.openbmc_project.Logging",
1661 "/xyz/openbmc_project/logging/entry/" + entryID,
1662 "xyz.openbmc_project.Object.Delete", "Delete");
1663 });
1664}
1665
1666inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001667{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001668 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/"
1669 "<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001670 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001671 .methods(boost::beast::http::verb::get)(
1672 [](const crow::Request& req,
1673 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1674 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001675
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001676 {
Ed Tanous753d0342021-07-01 07:57:30 -07001677 std::string_view acceptHeader = req.getHeaderValue("Accept");
1678 // The iterators in boost/http/rfc7230.hpp end the string if '/'
1679 // is found, so replace it with arbitrary character '|' which is
1680 // not part of the Accept header syntax.
1681 std::string acceptStr = boost::replace_all_copy(
1682 std::string(acceptHeader), "/", "|");
1683 boost::beast::http::ext_list acceptTypes{acceptStr};
1684 bool supported = false;
1685 for (const auto& type : acceptTypes)
1686 {
1687 if ((type.first == "*|*") ||
1688 (type.first == "application|octet-stream"))
1689 {
1690 supported = true;
1691 break;
1692 }
1693 }
1694 if (!supported)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001695 {
1696 asyncResp->res.result(
1697 boost::beast::http::status::bad_request);
1698 return;
1699 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001700
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001701 std::string entryID = param;
1702 dbus::utility::escapePathForDbus(entryID);
1703
1704 crow::connections::systemBus->async_method_call(
1705 [asyncResp,
1706 entryID](const boost::system::error_code ec,
1707 const sdbusplus::message::unix_fd& unixfd) {
1708 if (ec.value() == EBADR)
1709 {
1710 messages::resourceNotFound(
1711 asyncResp->res, "EventLogAttachment", entryID);
1712 return;
1713 }
1714 if (ec)
1715 {
1716 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1717 messages::internalError(asyncResp->res);
1718 return;
1719 }
1720
1721 int fd = -1;
1722 fd = dup(unixfd);
1723 if (fd == -1)
1724 {
1725 messages::internalError(asyncResp->res);
1726 return;
1727 }
1728
1729 long long int size = lseek(fd, 0, SEEK_END);
1730 if (size == -1)
1731 {
1732 messages::internalError(asyncResp->res);
1733 return;
1734 }
1735
1736 // Arbitrary max size of 64kb
1737 constexpr int maxFileSize = 65536;
1738 if (size > maxFileSize)
1739 {
1740 BMCWEB_LOG_ERROR
1741 << "File size exceeds maximum allowed size of "
1742 << maxFileSize;
1743 messages::internalError(asyncResp->res);
1744 return;
1745 }
1746 std::vector<char> data(static_cast<size_t>(size));
1747 long long int rc = lseek(fd, 0, SEEK_SET);
1748 if (rc == -1)
1749 {
1750 messages::internalError(asyncResp->res);
1751 return;
1752 }
1753 rc = read(fd, data.data(), data.size());
1754 if ((rc == -1) || (rc != size))
1755 {
1756 messages::internalError(asyncResp->res);
1757 return;
1758 }
1759 close(fd);
1760
1761 std::string_view strData(data.data(), data.size());
1762 std::string output =
1763 crow::utility::base64encode(strData);
1764
1765 asyncResp->res.addHeader("Content-Type",
1766 "application/octet-stream");
1767 asyncResp->res.addHeader("Content-Transfer-Encoding",
1768 "Base64");
1769 asyncResp->res.body() = std::move(output);
1770 },
1771 "xyz.openbmc_project.Logging",
1772 "/xyz/openbmc_project/logging/entry/" + entryID,
1773 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1774 });
1775}
1776
1777inline void requestRoutesBMCLogServiceCollection(App& app)
1778{
1779 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05001780 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001781 .methods(boost::beast::http::verb::get)(
1782 [](const crow::Request&,
1783 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1784 // Collections don't include the static data added by SubRoute
1785 // because it has a duplicate entry for members
1786 asyncResp->res.jsonValue["@odata.type"] =
1787 "#LogServiceCollection.LogServiceCollection";
1788 asyncResp->res.jsonValue["@odata.id"] =
1789 "/redfish/v1/Managers/bmc/LogServices";
1790 asyncResp->res.jsonValue["Name"] =
1791 "Open BMC Log Services Collection";
1792 asyncResp->res.jsonValue["Description"] =
1793 "Collection of LogServices for this Manager";
1794 nlohmann::json& logServiceArray =
1795 asyncResp->res.jsonValue["Members"];
1796 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001797#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001798 logServiceArray.push_back(
1799 {{"@odata.id",
1800 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001801#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001802#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001803 logServiceArray.push_back(
1804 {{"@odata.id",
1805 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001806#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001807 asyncResp->res.jsonValue["Members@odata.count"] =
1808 logServiceArray.size();
1809 });
1810}
Ed Tanous1da66f72018-07-27 16:13:37 -07001811
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001812inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001813{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001814 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07001815 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001816 .methods(boost::beast::http::verb::get)(
1817 [](const crow::Request&,
1818 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07001819
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001820 {
1821 asyncResp->res.jsonValue["@odata.type"] =
1822 "#LogService.v1_1_0.LogService";
1823 asyncResp->res.jsonValue["@odata.id"] =
1824 "/redfish/v1/Managers/bmc/LogServices/Journal";
1825 asyncResp->res.jsonValue["Name"] =
1826 "Open BMC Journal Log Service";
1827 asyncResp->res.jsonValue["Description"] =
1828 "BMC Journal Log Service";
1829 asyncResp->res.jsonValue["Id"] = "BMC Journal";
1830 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301831
1832 std::pair<std::string, std::string> redfishDateTimeOffset =
1833 crow::utility::getDateTimeOffsetNow();
1834 asyncResp->res.jsonValue["DateTime"] =
1835 redfishDateTimeOffset.first;
1836 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1837 redfishDateTimeOffset.second;
1838
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001839 asyncResp->res.jsonValue["Entries"] = {
1840 {"@odata.id",
1841 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1842 });
1843}
Jason M. Billse1f26342018-07-18 12:12:00 -07001844
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001845static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1846 sd_journal* journal,
1847 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07001848{
1849 // Get the Log Entry contents
1850 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07001851
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001852 std::string message;
1853 std::string_view syslogID;
1854 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
1855 if (ret < 0)
1856 {
1857 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
1858 << strerror(-ret);
1859 }
1860 if (!syslogID.empty())
1861 {
1862 message += std::string(syslogID) + ": ";
1863 }
1864
Ed Tanous39e77502019-03-04 17:35:53 -08001865 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07001866 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07001867 if (ret < 0)
1868 {
1869 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1870 return 1;
1871 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001872 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07001873
1874 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07001875 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07001876 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07001877 if (ret < 0)
1878 {
1879 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07001880 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001881
1882 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07001883 std::string entryTimeStr;
1884 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07001885 {
Jason M. Bills16428a12018-11-02 12:42:29 -07001886 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07001887 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001888
1889 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001890 bmcJournalLogEntryJson = {
Ed Tanousd0dbeef2021-07-01 08:46:46 -07001891 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001892 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1893 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07001894 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001895 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001896 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07001897 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06001898 {"Severity", severity <= 2 ? "Critical"
1899 : severity <= 4 ? "Warning"
1900 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07001901 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07001902 {"Created", std::move(entryTimeStr)}};
1903 return 0;
1904}
1905
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001906inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07001907{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001908 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001909 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001910 .methods(boost::beast::http::verb::get)(
1911 [](const crow::Request& req,
1912 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1913 static constexpr const long maxEntriesPerPage = 1000;
1914 uint64_t skip = 0;
1915 uint64_t top = maxEntriesPerPage; // Show max entries by default
1916 if (!getSkipParam(asyncResp, req, skip))
1917 {
1918 return;
1919 }
1920 if (!getTopParam(asyncResp, req, top))
1921 {
1922 return;
1923 }
1924 // Collections don't include the static data added by SubRoute
1925 // because it has a duplicate entry for members
1926 asyncResp->res.jsonValue["@odata.type"] =
1927 "#LogEntryCollection.LogEntryCollection";
1928 asyncResp->res.jsonValue["@odata.id"] =
1929 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1930 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1931 asyncResp->res.jsonValue["Description"] =
1932 "Collection of BMC Journal Entries";
1933 nlohmann::json& logEntryArray =
1934 asyncResp->res.jsonValue["Members"];
1935 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07001936
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001937 // Go through the journal and use the timestamp to create a
1938 // unique ID for each entry
1939 sd_journal* journalTmp = nullptr;
1940 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1941 if (ret < 0)
1942 {
1943 BMCWEB_LOG_ERROR << "failed to open journal: "
1944 << strerror(-ret);
1945 messages::internalError(asyncResp->res);
1946 return;
1947 }
1948 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
1949 journal(journalTmp, sd_journal_close);
1950 journalTmp = nullptr;
1951 uint64_t entryCount = 0;
1952 // Reset the unique ID on the first entry
1953 bool firstEntry = true;
1954 SD_JOURNAL_FOREACH(journal.get())
1955 {
1956 entryCount++;
1957 // Handle paging using skip (number of entries to skip from
1958 // the start) and top (number of entries to display)
1959 if (entryCount <= skip || entryCount > skip + top)
1960 {
1961 continue;
1962 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001963
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001964 std::string idStr;
1965 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1966 {
1967 continue;
1968 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001969
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001970 if (firstEntry)
1971 {
1972 firstEntry = false;
1973 }
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001974
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001975 logEntryArray.push_back({});
1976 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
1977 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
1978 bmcJournalLogEntry) != 0)
1979 {
1980 messages::internalError(asyncResp->res);
1981 return;
1982 }
1983 }
1984 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1985 if (skip + top < entryCount)
1986 {
1987 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1988 "/redfish/v1/Managers/bmc/LogServices/Journal/"
1989 "Entries?$skip=" +
1990 std::to_string(skip + top);
1991 }
1992 });
1993}
Jason M. Billse1f26342018-07-18 12:12:00 -07001994
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001995inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07001996{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001997 BMCWEB_ROUTE(app,
1998 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001999 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002000 .methods(boost::beast::http::verb::get)(
2001 [](const crow::Request&,
2002 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2003 const std::string& entryID) {
2004 // Convert the unique ID back to a timestamp to find the entry
2005 uint64_t ts = 0;
2006 uint64_t index = 0;
2007 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2008 {
2009 return;
2010 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002011
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002012 sd_journal* journalTmp = nullptr;
2013 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2014 if (ret < 0)
2015 {
2016 BMCWEB_LOG_ERROR << "failed to open journal: "
2017 << strerror(-ret);
2018 messages::internalError(asyncResp->res);
2019 return;
2020 }
2021 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2022 journal(journalTmp, sd_journal_close);
2023 journalTmp = nullptr;
2024 // Go to the timestamp in the log and move to the entry at the
2025 // index tracking the unique ID
2026 std::string idStr;
2027 bool firstEntry = true;
2028 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2029 if (ret < 0)
2030 {
2031 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2032 << strerror(-ret);
2033 messages::internalError(asyncResp->res);
2034 return;
2035 }
2036 for (uint64_t i = 0; i <= index; i++)
2037 {
2038 sd_journal_next(journal.get());
2039 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2040 {
2041 messages::internalError(asyncResp->res);
2042 return;
2043 }
2044 if (firstEntry)
2045 {
2046 firstEntry = false;
2047 }
2048 }
2049 // Confirm that the entry ID matches what was requested
2050 if (idStr != entryID)
2051 {
2052 messages::resourceMissingAtURI(asyncResp->res, entryID);
2053 return;
2054 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002055
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002056 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2057 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002058 {
2059 messages::internalError(asyncResp->res);
2060 return;
2061 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002062 });
2063}
2064
2065inline void requestRoutesBMCDumpService(App& app)
2066{
2067 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002068 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002069 .methods(boost::beast::http::verb::get)(
2070 [](const crow::Request&,
2071 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2072 asyncResp->res.jsonValue["@odata.id"] =
2073 "/redfish/v1/Managers/bmc/LogServices/Dump";
2074 asyncResp->res.jsonValue["@odata.type"] =
2075 "#LogService.v1_2_0.LogService";
2076 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2077 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2078 asyncResp->res.jsonValue["Id"] = "Dump";
2079 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302080
2081 std::pair<std::string, std::string> redfishDateTimeOffset =
2082 crow::utility::getDateTimeOffsetNow();
2083 asyncResp->res.jsonValue["DateTime"] =
2084 redfishDateTimeOffset.first;
2085 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2086 redfishDateTimeOffset.second;
2087
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002088 asyncResp->res.jsonValue["Entries"] = {
2089 {"@odata.id",
2090 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2091 asyncResp->res.jsonValue["Actions"] = {
2092 {"#LogService.ClearLog",
2093 {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2094 "Actions/LogService.ClearLog"}}},
2095 {"#LogService.CollectDiagnosticData",
2096 {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2097 "Actions/LogService.CollectDiagnosticData"}}}};
2098 });
2099}
2100
2101inline void requestRoutesBMCDumpEntryCollection(App& app)
2102{
2103
2104 /**
2105 * Functions triggers appropriate requests on DBus
2106 */
2107 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002108 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002109 .methods(boost::beast::http::verb::get)(
2110 [](const crow::Request&,
2111 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2112 asyncResp->res.jsonValue["@odata.type"] =
2113 "#LogEntryCollection.LogEntryCollection";
2114 asyncResp->res.jsonValue["@odata.id"] =
2115 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2116 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2117 asyncResp->res.jsonValue["Description"] =
2118 "Collection of BMC Dump Entries";
2119
2120 getDumpEntryCollection(asyncResp, "BMC");
2121 });
2122}
2123
2124inline void requestRoutesBMCDumpEntry(App& app)
2125{
2126 BMCWEB_ROUTE(app,
2127 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002128 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002129 .methods(boost::beast::http::verb::get)(
2130 [](const crow::Request&,
2131 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2132 const std::string& param) {
2133 getDumpEntryById(asyncResp, param, "BMC");
2134 });
2135 BMCWEB_ROUTE(app,
2136 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002137 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002138 .methods(boost::beast::http::verb::delete_)(
2139 [](const crow::Request&,
2140 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2141 const std::string& param) {
2142 deleteDumpEntry(asyncResp, param, "bmc");
2143 });
2144}
2145
2146inline void requestRoutesBMCDumpCreate(App& app)
2147{
2148
2149 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2150 "Actions/"
2151 "LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002152 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002153 .methods(boost::beast::http::verb::post)(
2154 [](const crow::Request& req,
2155 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2156 createDump(asyncResp, req, "BMC");
2157 });
2158}
2159
2160inline void requestRoutesBMCDumpClear(App& app)
2161{
2162 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2163 "Actions/"
2164 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002165 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002166 .methods(boost::beast::http::verb::post)(
2167 [](const crow::Request&,
2168 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2169 clearDump(asyncResp, "BMC");
2170 });
2171}
2172
2173inline void requestRoutesSystemDumpService(App& app)
2174{
2175 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002176 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002177 .methods(boost::beast::http::verb::get)(
2178 [](const crow::Request&,
2179 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2180
2181 {
2182 asyncResp->res.jsonValue["@odata.id"] =
2183 "/redfish/v1/Systems/system/LogServices/Dump";
2184 asyncResp->res.jsonValue["@odata.type"] =
2185 "#LogService.v1_2_0.LogService";
2186 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2187 asyncResp->res.jsonValue["Description"] =
2188 "System Dump LogService";
2189 asyncResp->res.jsonValue["Id"] = "Dump";
2190 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302191
2192 std::pair<std::string, std::string> redfishDateTimeOffset =
2193 crow::utility::getDateTimeOffsetNow();
2194 asyncResp->res.jsonValue["DateTime"] =
2195 redfishDateTimeOffset.first;
2196 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2197 redfishDateTimeOffset.second;
2198
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002199 asyncResp->res.jsonValue["Entries"] = {
2200 {"@odata.id",
2201 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2202 asyncResp->res.jsonValue["Actions"] = {
2203 {"#LogService.ClearLog",
2204 {{"target",
2205 "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2206 "LogService.ClearLog"}}},
2207 {"#LogService.CollectDiagnosticData",
2208 {{"target",
2209 "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2210 "LogService.CollectDiagnosticData"}}}};
2211 });
2212}
2213
2214inline void requestRoutesSystemDumpEntryCollection(App& app)
2215{
2216
2217 /**
2218 * Functions triggers appropriate requests on DBus
2219 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002220 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002221 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002222 .methods(boost::beast::http::verb::get)(
2223 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002224 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002225 asyncResp->res.jsonValue["@odata.type"] =
2226 "#LogEntryCollection.LogEntryCollection";
2227 asyncResp->res.jsonValue["@odata.id"] =
2228 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2229 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2230 asyncResp->res.jsonValue["Description"] =
2231 "Collection of System Dump Entries";
2232
2233 getDumpEntryCollection(asyncResp, "System");
2234 });
2235}
2236
2237inline void requestRoutesSystemDumpEntry(App& app)
2238{
2239 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002240 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002241 .privileges(redfish::privileges::getLogEntry)
2242
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002243 .methods(boost::beast::http::verb::get)(
2244 [](const crow::Request&,
2245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2246 const std::string& param) {
2247 getDumpEntryById(asyncResp, param, "System");
2248 });
2249
2250 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002251 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002252 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002253 .methods(boost::beast::http::verb::delete_)(
2254 [](const crow::Request&,
2255 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2256 const std::string& param) {
2257 deleteDumpEntry(asyncResp, param, "system");
2258 });
2259}
2260
2261inline void requestRoutesSystemDumpCreate(App& app)
2262{
2263 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2264 "Actions/"
2265 "LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002266 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002267 .methods(boost::beast::http::verb::post)(
2268 [](const crow::Request& req,
2269 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2270
2271 { createDump(asyncResp, req, "System"); });
2272}
2273
2274inline void requestRoutesSystemDumpClear(App& app)
2275{
2276 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2277 "Actions/"
2278 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002279 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002280 .methods(boost::beast::http::verb::post)(
2281 [](const crow::Request&,
2282 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2283
2284 { clearDump(asyncResp, "System"); });
2285}
2286
2287inline void requestRoutesCrashdumpService(App& app)
2288{
2289 // Note: Deviated from redfish privilege registry for GET & HEAD
2290 // method for security reasons.
2291 /**
2292 * Functions triggers appropriate requests on DBus
2293 */
2294 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002295 // This is incorrect, should be:
2296 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002297 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002298 .methods(
2299 boost::beast::http::verb::
2300 get)([](const crow::Request&,
2301 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2302 // Copy over the static data to include the entries added by
2303 // SubRoute
2304 asyncResp->res.jsonValue["@odata.id"] =
2305 "/redfish/v1/Systems/system/LogServices/Crashdump";
2306 asyncResp->res.jsonValue["@odata.type"] =
2307 "#LogService.v1_2_0.LogService";
2308 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2309 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2310 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2311 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2312 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302313
2314 std::pair<std::string, std::string> redfishDateTimeOffset =
2315 crow::utility::getDateTimeOffsetNow();
2316 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2317 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2318 redfishDateTimeOffset.second;
2319
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002320 asyncResp->res.jsonValue["Entries"] = {
2321 {"@odata.id",
2322 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2323 asyncResp->res.jsonValue["Actions"] = {
2324 {"#LogService.ClearLog",
2325 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2326 "Actions/LogService.ClearLog"}}},
2327 {"#LogService.CollectDiagnosticData",
2328 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2329 "Actions/LogService.CollectDiagnosticData"}}}};
2330 });
2331}
2332
2333void inline requestRoutesCrashdumpClear(App& app)
2334{
2335 BMCWEB_ROUTE(app,
2336 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
2337 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002338 // This is incorrect, should be:
2339 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002340 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002341 .methods(boost::beast::http::verb::post)(
2342 [](const crow::Request&,
2343 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2344 crow::connections::systemBus->async_method_call(
2345 [asyncResp](const boost::system::error_code ec,
2346 const std::string&) {
2347 if (ec)
2348 {
2349 messages::internalError(asyncResp->res);
2350 return;
2351 }
2352 messages::success(asyncResp->res);
2353 },
2354 crashdumpObject, crashdumpPath, deleteAllInterface,
2355 "DeleteAll");
2356 });
2357}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002358
zhanghch058d1b46d2021-04-01 11:18:24 +08002359static void
2360 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2361 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002362{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002363 auto getStoredLogCallback =
2364 [asyncResp, logID, &logEntryJson](
2365 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002366 const std::vector<std::pair<std::string, VariantType>>& params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002367 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002368 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002369 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2370 if (ec.value() ==
2371 boost::system::linux_error::bad_request_descriptor)
2372 {
2373 messages::resourceNotFound(asyncResp->res, "LogEntry",
2374 logID);
2375 }
2376 else
2377 {
2378 messages::internalError(asyncResp->res);
2379 }
2380 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002381 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002382
Johnathan Mantey043a0532020-03-10 17:15:28 -07002383 std::string timestamp{};
2384 std::string filename{};
2385 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002386 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002387
2388 if (filename.empty() || timestamp.empty())
2389 {
2390 messages::resourceMissingAtURI(asyncResp->res, logID);
2391 return;
2392 }
2393
2394 std::string crashdumpURI =
2395 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2396 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002397 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002398 {"@odata.id", "/redfish/v1/Systems/system/"
2399 "LogServices/Crashdump/Entries/" +
2400 logID},
2401 {"Name", "CPU Crashdump"},
2402 {"Id", logID},
2403 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002404 {"AdditionalDataURI", std::move(crashdumpURI)},
2405 {"DiagnosticDataType", "OEM"},
2406 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002407 {"Created", std::move(timestamp)}};
2408 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002409 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002410 std::move(getStoredLogCallback), crashdumpObject,
2411 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002412 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002413}
2414
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002415inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002416{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002417 // Note: Deviated from redfish privilege registry for GET & HEAD
2418 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002419 /**
2420 * Functions triggers appropriate requests on DBus
2421 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002422 BMCWEB_ROUTE(app,
2423 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002424 // This is incorrect, should be.
2425 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002426 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002427 .methods(
2428 boost::beast::http::verb::
2429 get)([](const crow::Request&,
2430 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2431 // Collections don't include the static data added by SubRoute
2432 // because it has a duplicate entry for members
2433 auto getLogEntriesCallback = [asyncResp](
2434 const boost::system::error_code ec,
2435 const std::vector<std::string>&
2436 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002437 if (ec)
2438 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002439 if (ec.value() !=
2440 boost::system::errc::no_such_file_or_directory)
2441 {
2442 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2443 << ec.message();
2444 messages::internalError(asyncResp->res);
2445 return;
2446 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002447 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002448 asyncResp->res.jsonValue["@odata.type"] =
2449 "#LogEntryCollection.LogEntryCollection";
2450 asyncResp->res.jsonValue["@odata.id"] =
2451 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2452 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2453 asyncResp->res.jsonValue["Description"] =
2454 "Collection of Crashdump Entries";
2455 nlohmann::json& logEntryArray =
2456 asyncResp->res.jsonValue["Members"];
2457 logEntryArray = nlohmann::json::array();
2458 std::vector<std::string> logIDs;
2459 // Get the list of log entries and build up an empty array big
2460 // enough to hold them
2461 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002462 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002463 // Get the log ID
2464 std::size_t lastPos = objpath.rfind('/');
2465 if (lastPos == std::string::npos)
2466 {
2467 continue;
2468 }
2469 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002470
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002471 // Add a space for the log entry to the array
2472 logEntryArray.push_back({});
2473 }
2474 // Now go through and set up async calls to fill in the entries
2475 size_t index = 0;
2476 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002477 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002478 // Add the log entry to the array
2479 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002480 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002481 asyncResp->res.jsonValue["Members@odata.count"] =
2482 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002483 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002484 crow::connections::systemBus->async_method_call(
2485 std::move(getLogEntriesCallback),
2486 "xyz.openbmc_project.ObjectMapper",
2487 "/xyz/openbmc_project/object_mapper",
2488 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2489 std::array<const char*, 1>{crashdumpInterface});
2490 });
2491}
Ed Tanous1da66f72018-07-27 16:13:37 -07002492
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002493inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002494{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002495 // Note: Deviated from redfish privilege registry for GET & HEAD
2496 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002497
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002498 BMCWEB_ROUTE(
2499 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002500 // this is incorrect, should be
2501 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002502 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002503 .methods(boost::beast::http::verb::get)(
2504 [](const crow::Request&,
2505 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2506 const std::string& param) {
2507 const std::string& logID = param;
2508 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2509 });
2510}
Ed Tanous1da66f72018-07-27 16:13:37 -07002511
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002512inline void requestRoutesCrashdumpFile(App& app)
2513{
2514 // Note: Deviated from redfish privilege registry for GET & HEAD
2515 // method for security reasons.
2516 BMCWEB_ROUTE(
2517 app,
2518 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002519 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002520 .methods(boost::beast::http::verb::get)(
2521 [](const crow::Request&,
2522 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2523 const std::string& logID, const std::string& fileName) {
2524 auto getStoredLogCallback =
2525 [asyncResp, logID, fileName](
2526 const boost::system::error_code ec,
2527 const std::vector<std::pair<std::string, VariantType>>&
2528 resp) {
2529 if (ec)
2530 {
2531 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2532 << ec.message();
2533 messages::internalError(asyncResp->res);
2534 return;
2535 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002536
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002537 std::string dbusFilename{};
2538 std::string dbusTimestamp{};
2539 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002540
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002541 parseCrashdumpParameters(resp, dbusFilename,
2542 dbusTimestamp, dbusFilepath);
2543
2544 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2545 dbusFilepath.empty())
2546 {
2547 messages::resourceMissingAtURI(asyncResp->res,
2548 fileName);
2549 return;
2550 }
2551
2552 // Verify the file name parameter is correct
2553 if (fileName != dbusFilename)
2554 {
2555 messages::resourceMissingAtURI(asyncResp->res,
2556 fileName);
2557 return;
2558 }
2559
2560 if (!std::filesystem::exists(dbusFilepath))
2561 {
2562 messages::resourceMissingAtURI(asyncResp->res,
2563 fileName);
2564 return;
2565 }
2566 std::ifstream ifs(dbusFilepath, std::ios::in |
2567 std::ios::binary |
2568 std::ios::ate);
2569 std::ifstream::pos_type fileSize = ifs.tellg();
2570 if (fileSize < 0)
2571 {
2572 messages::generalError(asyncResp->res);
2573 return;
2574 }
2575 ifs.seekg(0, std::ios::beg);
2576
2577 auto crashData = std::make_unique<char[]>(
2578 static_cast<unsigned int>(fileSize));
2579
2580 ifs.read(crashData.get(), static_cast<int>(fileSize));
2581
2582 // The cast to std::string is intentional in order to
2583 // use the assign() that applies move mechanics
2584 asyncResp->res.body().assign(
2585 static_cast<std::string>(crashData.get()));
2586
2587 // Configure this to be a file download when accessed
2588 // from a browser
2589 asyncResp->res.addHeader("Content-Disposition",
2590 "attachment");
2591 };
2592 crow::connections::systemBus->async_method_call(
2593 std::move(getStoredLogCallback), crashdumpObject,
2594 crashdumpPath + std::string("/") + logID,
2595 "org.freedesktop.DBus.Properties", "GetAll",
2596 crashdumpInterface);
2597 });
2598}
2599
2600inline void requestRoutesCrashdumpCollect(App& app)
2601{
2602 // Note: Deviated from redfish privilege registry for GET & HEAD
2603 // method for security reasons.
2604 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/"
2605 "Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002606 // The below is incorrect; Should be ConfigureManager
2607 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002608 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002609 .methods(
2610 boost::beast::http::verb::
2611 post)([](const crow::Request& req,
2612 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2613 std::string diagnosticDataType;
2614 std::string oemDiagnosticDataType;
2615 if (!redfish::json_util::readJson(
2616 req, asyncResp->res, "DiagnosticDataType",
2617 diagnosticDataType, "OEMDiagnosticDataType",
2618 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002619 {
James Feist46229572020-02-19 15:11:58 -08002620 return;
2621 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002622
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002623 if (diagnosticDataType != "OEM")
2624 {
2625 BMCWEB_LOG_ERROR
2626 << "Only OEM DiagnosticDataType supported for Crashdump";
2627 messages::actionParameterValueFormatError(
2628 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2629 "CollectDiagnosticData");
2630 return;
2631 }
2632
2633 auto collectCrashdumpCallback = [asyncResp, req](
2634 const boost::system::error_code
2635 ec,
2636 const std::string&) {
2637 if (ec)
2638 {
2639 if (ec.value() ==
2640 boost::system::errc::operation_not_supported)
2641 {
2642 messages::resourceInStandby(asyncResp->res);
2643 }
2644 else if (ec.value() ==
2645 boost::system::errc::device_or_resource_busy)
2646 {
2647 messages::serviceTemporarilyUnavailable(asyncResp->res,
2648 "60");
2649 }
2650 else
2651 {
2652 messages::internalError(asyncResp->res);
2653 }
2654 return;
2655 }
2656 std::shared_ptr<task::TaskData> task =
2657 task::TaskData::createTask(
2658 [](boost::system::error_code err,
2659 sdbusplus::message::message&,
2660 const std::shared_ptr<task::TaskData>& taskData) {
2661 if (!err)
2662 {
2663 taskData->messages.emplace_back(
2664 messages::taskCompletedOK(
2665 std::to_string(taskData->index)));
2666 taskData->state = "Completed";
2667 }
2668 return task::completed;
2669 },
2670 "type='signal',interface='org.freedesktop.DBus."
2671 "Properties',"
2672 "member='PropertiesChanged',arg0namespace='com.intel."
2673 "crashdump'");
2674 task->startTimer(std::chrono::minutes(5));
2675 task->populateResp(asyncResp->res);
2676 task->payload.emplace(req);
2677 };
2678
2679 if (oemDiagnosticDataType == "OnDemand")
2680 {
2681 crow::connections::systemBus->async_method_call(
2682 std::move(collectCrashdumpCallback), crashdumpObject,
2683 crashdumpPath, crashdumpOnDemandInterface,
2684 "GenerateOnDemandLog");
2685 }
2686 else if (oemDiagnosticDataType == "Telemetry")
2687 {
2688 crow::connections::systemBus->async_method_call(
2689 std::move(collectCrashdumpCallback), crashdumpObject,
2690 crashdumpPath, crashdumpTelemetryInterface,
2691 "GenerateTelemetryLog");
2692 }
2693 else
2694 {
2695 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2696 << oemDiagnosticDataType;
2697 messages::actionParameterValueFormatError(
2698 asyncResp->res, oemDiagnosticDataType,
2699 "OEMDiagnosticDataType", "CollectDiagnosticData");
2700 return;
2701 }
2702 });
2703}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002704
Andrew Geisslercb92c032018-08-17 07:56:14 -07002705/**
2706 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2707 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002708inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002709{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002710 /**
2711 * Function handles POST method request.
2712 * The Clear Log actions does not require any parameter.The action deletes
2713 * all entries found in the Entries collection for this Log Service.
2714 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002715
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002716 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
2717 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002718 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002719 .methods(boost::beast::http::verb::post)(
2720 [](const crow::Request&,
2721 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2722 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07002723
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002724 // Process response from Logging service.
2725 auto respHandler = [asyncResp](
2726 const boost::system::error_code ec) {
2727 BMCWEB_LOG_DEBUG
2728 << "doClearLog resp_handler callback: Done";
2729 if (ec)
2730 {
2731 // TODO Handle for specific error code
2732 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
2733 << ec;
2734 asyncResp->res.result(
2735 boost::beast::http::status::internal_server_error);
2736 return;
2737 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07002738
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002739 asyncResp->res.result(
2740 boost::beast::http::status::no_content);
2741 };
2742
2743 // Make call to Logging service to request Clear Log
2744 crow::connections::systemBus->async_method_call(
2745 respHandler, "xyz.openbmc_project.Logging",
2746 "/xyz/openbmc_project/logging",
2747 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2748 });
2749}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002750
2751/****************************************************
2752 * Redfish PostCode interfaces
2753 * using DBUS interface: getPostCodesTS
2754 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002755inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002756{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002757 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07002758 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002759 .methods(boost::beast::http::verb::get)(
2760 [](const crow::Request&,
2761 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2762 asyncResp->res.jsonValue = {
2763 {"@odata.id",
2764 "/redfish/v1/Systems/system/LogServices/PostCodes"},
2765 {"@odata.type", "#LogService.v1_1_0.LogService"},
2766 {"Name", "POST Code Log Service"},
2767 {"Description", "POST Code Log Service"},
2768 {"Id", "BIOS POST Code Log"},
2769 {"OverWritePolicy", "WrapsWhenFull"},
2770 {"Entries",
2771 {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2772 "PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05302773
2774 std::pair<std::string, std::string> redfishDateTimeOffset =
2775 crow::utility::getDateTimeOffsetNow();
2776 asyncResp->res.jsonValue["DateTime"] =
2777 redfishDateTimeOffset.first;
2778 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2779 redfishDateTimeOffset.second;
2780
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002781 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
2782 {"target",
2783 "/redfish/v1/Systems/system/LogServices/PostCodes/"
2784 "Actions/LogService.ClearLog"}};
2785 });
2786}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002787
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002788inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002789{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002790 BMCWEB_ROUTE(app,
2791 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
2792 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002793 // The following privilege is incorrect; It should be ConfigureManager
2794 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002795 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002796 .methods(boost::beast::http::verb::post)(
2797 [](const crow::Request&,
2798 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2799 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08002800
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002801 // Make call to post-code service to request clear all
2802 crow::connections::systemBus->async_method_call(
2803 [asyncResp](const boost::system::error_code ec) {
2804 if (ec)
2805 {
2806 // TODO Handle for specific error code
2807 BMCWEB_LOG_ERROR
2808 << "doClearPostCodes resp_handler got error "
2809 << ec;
2810 asyncResp->res.result(boost::beast::http::status::
2811 internal_server_error);
2812 messages::internalError(asyncResp->res);
2813 return;
2814 }
2815 },
2816 "xyz.openbmc_project.State.Boot.PostCode0",
2817 "/xyz/openbmc_project/State/Boot/PostCode0",
2818 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2819 });
2820}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002821
2822static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08002823 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302824 const boost::container::flat_map<
2825 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08002826 const uint16_t bootIndex, const uint64_t codeIndex = 0,
2827 const uint64_t skip = 0, const uint64_t top = 0)
2828{
2829 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002830 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05302831 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08002832
2833 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002834 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08002835
2836 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302837 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
2838 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002839 {
2840 currentCodeIndex++;
2841 std::string postcodeEntryID =
2842 "B" + std::to_string(bootIndex) + "-" +
2843 std::to_string(currentCodeIndex); // 1 based index in EntryID string
2844
2845 uint64_t usecSinceEpoch = code.first;
2846 uint64_t usTimeOffset = 0;
2847
2848 if (1 == currentCodeIndex)
2849 { // already incremented
2850 firstCodeTimeUs = code.first;
2851 }
2852 else
2853 {
2854 usTimeOffset = code.first - firstCodeTimeUs;
2855 }
2856
2857 // skip if no specific codeIndex is specified and currentCodeIndex does
2858 // not fall between top and skip
2859 if ((codeIndex == 0) &&
2860 (currentCodeIndex <= skip || currentCodeIndex > top))
2861 {
2862 continue;
2863 }
2864
Gunnar Mills4e0453b2020-07-08 14:00:30 -05002865 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08002866 // currentIndex
2867 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
2868 {
2869 // This is done for simplicity. 1st entry is needed to calculate
2870 // time offset. To improve efficiency, one can get to the entry
2871 // directly (possibly with flatmap's nth method)
2872 continue;
2873 }
2874
2875 // currentCodeIndex is within top and skip or equal to specified code
2876 // index
2877
2878 // Get the Created time from the timestamp
2879 std::string entryTimeStr;
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -05002880 entryTimeStr = crow::utility::getDateTime(
2881 static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
ZhikuiRena3316fc2020-01-29 14:58:08 -08002882
2883 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
2884 std::ostringstream hexCode;
2885 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302886 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08002887 std::ostringstream timeOffsetStr;
2888 // Set Fixed -Point Notation
2889 timeOffsetStr << std::fixed;
2890 // Set precision to 4 digits
2891 timeOffsetStr << std::setprecision(4);
2892 // Add double to stream
2893 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
2894 std::vector<std::string> messageArgs = {
2895 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
2896
2897 // Get MessageArgs template from message registry
2898 std::string msg;
2899 if (message != nullptr)
2900 {
2901 msg = message->message;
2902
2903 // fill in this post code value
2904 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002905 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002906 {
2907 std::string argStr = "%" + std::to_string(++i);
2908 size_t argPos = msg.find(argStr);
2909 if (argPos != std::string::npos)
2910 {
2911 msg.replace(argPos, argStr.length(), messageArg);
2912 }
2913 }
2914 }
2915
Tim Leed4342a92020-04-27 11:47:58 +08002916 // Get Severity template from message registry
2917 std::string severity;
2918 if (message != nullptr)
2919 {
2920 severity = message->severity;
2921 }
2922
ZhikuiRena3316fc2020-01-29 14:58:08 -08002923 // add to AsyncResp
2924 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002925 nlohmann::json& bmcLogEntry = logEntryArray.back();
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002926 bmcLogEntry = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Gunnar Mills743e9a12020-10-26 12:44:53 -05002927 {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2928 "PostCodes/Entries/" +
2929 postcodeEntryID},
2930 {"Name", "POST Code Log Entry"},
2931 {"Id", postcodeEntryID},
2932 {"Message", std::move(msg)},
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05302933 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
Gunnar Mills743e9a12020-10-26 12:44:53 -05002934 {"MessageArgs", std::move(messageArgs)},
2935 {"EntryType", "Event"},
2936 {"Severity", std::move(severity)},
2937 {"Created", entryTimeStr}};
ZhikuiRena3316fc2020-01-29 14:58:08 -08002938 }
2939}
2940
zhanghch058d1b46d2021-04-01 11:18:24 +08002941static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08002942 const uint16_t bootIndex,
2943 const uint64_t codeIndex)
2944{
2945 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302946 [aResp, bootIndex,
2947 codeIndex](const boost::system::error_code ec,
2948 const boost::container::flat_map<
2949 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
2950 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08002951 if (ec)
2952 {
2953 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
2954 messages::internalError(aResp->res);
2955 return;
2956 }
2957
2958 // skip the empty postcode boots
2959 if (postcode.empty())
2960 {
2961 return;
2962 }
2963
2964 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
2965
2966 aResp->res.jsonValue["Members@odata.count"] =
2967 aResp->res.jsonValue["Members"].size();
2968 },
Jonathan Doman15124762021-01-07 17:54:17 -08002969 "xyz.openbmc_project.State.Boot.PostCode0",
2970 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08002971 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
2972 bootIndex);
2973}
2974
zhanghch058d1b46d2021-04-01 11:18:24 +08002975static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08002976 const uint16_t bootIndex,
2977 const uint16_t bootCount,
2978 const uint64_t entryCount, const uint64_t skip,
2979 const uint64_t top)
2980{
2981 crow::connections::systemBus->async_method_call(
2982 [aResp, bootIndex, bootCount, entryCount, skip,
2983 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302984 const boost::container::flat_map<
2985 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
2986 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08002987 if (ec)
2988 {
2989 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
2990 messages::internalError(aResp->res);
2991 return;
2992 }
2993
2994 uint64_t endCount = entryCount;
2995 if (!postcode.empty())
2996 {
2997 endCount = entryCount + postcode.size();
2998
2999 if ((skip < endCount) && ((top + skip) > entryCount))
3000 {
3001 uint64_t thisBootSkip =
3002 std::max(skip, entryCount) - entryCount;
3003 uint64_t thisBootTop =
3004 std::min(top + skip, endCount) - entryCount;
3005
3006 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3007 thisBootSkip, thisBootTop);
3008 }
3009 aResp->res.jsonValue["Members@odata.count"] = endCount;
3010 }
3011
3012 // continue to previous bootIndex
3013 if (bootIndex < bootCount)
3014 {
3015 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3016 bootCount, endCount, skip, top);
3017 }
3018 else
3019 {
3020 aResp->res.jsonValue["Members@odata.nextLink"] =
3021 "/redfish/v1/Systems/system/LogServices/PostCodes/"
3022 "Entries?$skip=" +
3023 std::to_string(skip + top);
3024 }
3025 },
Jonathan Doman15124762021-01-07 17:54:17 -08003026 "xyz.openbmc_project.State.Boot.PostCode0",
3027 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003028 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3029 bootIndex);
3030}
3031
zhanghch058d1b46d2021-04-01 11:18:24 +08003032static void
3033 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3034 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003035{
3036 uint64_t entryCount = 0;
3037 crow::connections::systemBus->async_method_call(
3038 [aResp, entryCount, skip,
3039 top](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003040 const std::variant<uint16_t>& bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003041 if (ec)
3042 {
3043 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3044 messages::internalError(aResp->res);
3045 return;
3046 }
3047 auto pVal = std::get_if<uint16_t>(&bootCount);
3048 if (pVal)
3049 {
3050 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3051 }
3052 else
3053 {
3054 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3055 }
3056 },
Jonathan Doman15124762021-01-07 17:54:17 -08003057 "xyz.openbmc_project.State.Boot.PostCode0",
3058 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003059 "org.freedesktop.DBus.Properties", "Get",
3060 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3061}
3062
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003063inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003064{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003065 BMCWEB_ROUTE(app,
3066 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003067 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003068 .methods(boost::beast::http::verb::get)(
3069 [](const crow::Request& req,
3070 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3071 asyncResp->res.jsonValue["@odata.type"] =
3072 "#LogEntryCollection.LogEntryCollection";
3073 asyncResp->res.jsonValue["@odata.id"] =
3074 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3075 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3076 asyncResp->res.jsonValue["Description"] =
3077 "Collection of POST Code Log Entries";
3078 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3079 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003080
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003081 uint64_t skip = 0;
3082 uint64_t top = maxEntriesPerPage; // Show max entries by default
3083 if (!getSkipParam(asyncResp, req, skip))
3084 {
3085 return;
3086 }
3087 if (!getTopParam(asyncResp, req, top))
3088 {
3089 return;
3090 }
3091 getCurrentBootNumber(asyncResp, skip, top);
3092 });
3093}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003094
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003095inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003096{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003097 BMCWEB_ROUTE(
3098 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003099 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003100 .methods(boost::beast::http::verb::get)(
3101 [](const crow::Request&,
3102 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3103 const std::string& targetID) {
3104 size_t bootPos = targetID.find('B');
3105 if (bootPos == std::string::npos)
3106 {
3107 // Requested ID was not found
3108 messages::resourceMissingAtURI(asyncResp->res, targetID);
3109 return;
3110 }
3111 std::string_view bootIndexStr(targetID);
3112 bootIndexStr.remove_prefix(bootPos + 1);
3113 uint16_t bootIndex = 0;
3114 uint64_t codeIndex = 0;
3115 size_t dashPos = bootIndexStr.find('-');
ZhikuiRena3316fc2020-01-29 14:58:08 -08003116
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003117 if (dashPos == std::string::npos)
3118 {
3119 return;
3120 }
3121 std::string_view codeIndexStr(bootIndexStr);
3122 bootIndexStr.remove_suffix(dashPos);
3123 codeIndexStr.remove_prefix(dashPos + 1);
zhanghch058d1b46d2021-04-01 11:18:24 +08003124
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003125 bootIndex = static_cast<uint16_t>(
3126 strtoul(std::string(bootIndexStr).c_str(), nullptr, 0));
3127 codeIndex =
3128 strtoul(std::string(codeIndexStr).c_str(), nullptr, 0);
3129 if (bootIndex == 0 || codeIndex == 0)
3130 {
3131 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3132 << targetID;
3133 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003134
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003135 asyncResp->res.jsonValue["@odata.type"] =
3136 "#LogEntry.v1_4_0.LogEntry";
3137 asyncResp->res.jsonValue["@odata.id"] =
3138 "/redfish/v1/Systems/system/LogServices/PostCodes/"
3139 "Entries";
3140 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3141 asyncResp->res.jsonValue["Description"] =
3142 "Collection of POST Code Log Entries";
3143 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3144 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003145
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003146 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3147 });
3148}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003149
Ed Tanous1da66f72018-07-27 16:13:37 -07003150} // namespace redfish