blob: 943e08e7d353a139c36b7d76a0eb37706bbc808b [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;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500424 std::string dumpStatus;
425 nlohmann::json thisEntry;
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000426
427 std::string entryID = object.first.filename();
428 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500429 {
430 continue;
431 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500432
433 for (auto& interfaceMap : object.second)
434 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500435 if (interfaceMap.first ==
436 "xyz.openbmc_project.Common.Progress")
437 {
438 for (auto& propertyMap : interfaceMap.second)
439 {
440 if (propertyMap.first == "Status")
441 {
442 auto status = std::get_if<std::string>(
443 &propertyMap.second);
444 if (status == nullptr)
445 {
446 messages::internalError(asyncResp->res);
447 break;
448 }
449 dumpStatus = *status;
450 }
451 }
452 }
453 else if (interfaceMap.first ==
454 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500455 {
456
457 for (auto& propertyMap : interfaceMap.second)
458 {
459 if (propertyMap.first == "Size")
460 {
461 auto sizePtr =
462 std::get_if<uint64_t>(&propertyMap.second);
463 if (sizePtr == nullptr)
464 {
465 messages::internalError(asyncResp->res);
466 break;
467 }
468 size = *sizePtr;
469 break;
470 }
471 }
472 }
473 else if (interfaceMap.first ==
474 "xyz.openbmc_project.Time.EpochTime")
475 {
476
477 for (auto& propertyMap : interfaceMap.second)
478 {
479 if (propertyMap.first == "Elapsed")
480 {
481 const uint64_t* usecsTimeStamp =
482 std::get_if<uint64_t>(&propertyMap.second);
483 if (usecsTimeStamp == nullptr)
484 {
485 messages::internalError(asyncResp->res);
486 break;
487 }
488 timestamp =
489 static_cast<std::time_t>(*usecsTimeStamp);
490 break;
491 }
492 }
493 }
494 }
495
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500496 if (dumpStatus != "xyz.openbmc_project.Common.Progress."
497 "OperationStatus.Completed" &&
498 !dumpStatus.empty())
499 {
500 // Dump status is not Complete, no need to enumerate
501 continue;
502 }
503
Ed Tanousd0dbeef2021-07-01 08:46:46 -0700504 thisEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500505 thisEntry["@odata.id"] = dumpPath + entryID;
506 thisEntry["Id"] = entryID;
507 thisEntry["EntryType"] = "Event";
508 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
509 thisEntry["Name"] = dumpType + " Dump Entry";
510
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500511 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512
513 if (dumpType == "BMC")
514 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500515 thisEntry["DiagnosticDataType"] = "Manager";
516 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500517 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
518 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500519 }
520 else if (dumpType == "System")
521 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500522 thisEntry["DiagnosticDataType"] = "OEM";
523 thisEntry["OEMDiagnosticDataType"] = "System";
524 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500525 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
526 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500527 }
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500528 entriesArray.push_back(std::move(thisEntry));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500529 }
530 asyncResp->res.jsonValue["Members@odata.count"] =
531 entriesArray.size();
532 },
533 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
534 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
535}
536
zhanghch058d1b46d2021-04-01 11:18:24 +0800537inline void
538 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
539 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500540{
541 std::string dumpPath;
542 if (dumpType == "BMC")
543 {
544 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
545 }
546 else if (dumpType == "System")
547 {
548 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
549 }
550 else
551 {
552 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
553 messages::internalError(asyncResp->res);
554 return;
555 }
556
557 crow::connections::systemBus->async_method_call(
558 [asyncResp, entryID, dumpPath, dumpType](
559 const boost::system::error_code ec, GetManagedObjectsType& resp) {
560 if (ec)
561 {
562 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
563 messages::internalError(asyncResp->res);
564 return;
565 }
566
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500567 bool foundDumpEntry = false;
568 std::string dumpEntryPath =
569 "/xyz/openbmc_project/dump/" +
570 std::string(boost::algorithm::to_lower_copy(dumpType)) +
571 "/entry/";
572
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500573 for (auto& objectPath : resp)
574 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500575 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500576 {
577 continue;
578 }
579
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500580 foundDumpEntry = true;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 std::time_t timestamp;
582 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500583 std::string dumpStatus;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500584
585 for (auto& interfaceMap : objectPath.second)
586 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500587 if (interfaceMap.first ==
588 "xyz.openbmc_project.Common.Progress")
589 {
590 for (auto& propertyMap : interfaceMap.second)
591 {
592 if (propertyMap.first == "Status")
593 {
594 auto status = std::get_if<std::string>(
595 &propertyMap.second);
596 if (status == nullptr)
597 {
598 messages::internalError(asyncResp->res);
599 break;
600 }
601 dumpStatus = *status;
602 }
603 }
604 }
605 else if (interfaceMap.first ==
606 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500607 {
608 for (auto& propertyMap : interfaceMap.second)
609 {
610 if (propertyMap.first == "Size")
611 {
612 auto sizePtr =
613 std::get_if<uint64_t>(&propertyMap.second);
614 if (sizePtr == nullptr)
615 {
616 messages::internalError(asyncResp->res);
617 break;
618 }
619 size = *sizePtr;
620 break;
621 }
622 }
623 }
624 else if (interfaceMap.first ==
625 "xyz.openbmc_project.Time.EpochTime")
626 {
627 for (auto& propertyMap : interfaceMap.second)
628 {
629 if (propertyMap.first == "Elapsed")
630 {
631 const uint64_t* usecsTimeStamp =
632 std::get_if<uint64_t>(&propertyMap.second);
633 if (usecsTimeStamp == nullptr)
634 {
635 messages::internalError(asyncResp->res);
636 break;
637 }
638 timestamp =
639 static_cast<std::time_t>(*usecsTimeStamp);
640 break;
641 }
642 }
643 }
644 }
645
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500646 if (dumpStatus != "xyz.openbmc_project.Common.Progress."
647 "OperationStatus.Completed" &&
648 !dumpStatus.empty())
649 {
650 // Dump status is not Complete
651 // return not found until status is changed to Completed
652 messages::resourceNotFound(asyncResp->res,
653 dumpType + " dump", entryID);
654 return;
655 }
656
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500657 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousd0dbeef2021-07-01 08:46:46 -0700658 "#LogEntry.v1_7_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500659 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
660 asyncResp->res.jsonValue["Id"] = entryID;
661 asyncResp->res.jsonValue["EntryType"] = "Event";
662 asyncResp->res.jsonValue["Created"] =
663 crow::utility::getDateTime(timestamp);
664 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
665
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500666 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500667
668 if (dumpType == "BMC")
669 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500670 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
671 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500672 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
673 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500674 }
675 else if (dumpType == "System")
676 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500677 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
678 asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500679 "System";
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500680 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500681 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
682 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500683 }
684 }
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500685 if (foundDumpEntry == false)
686 {
687 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
688 messages::internalError(asyncResp->res);
689 return;
690 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500691 },
692 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
693 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
694}
695
zhanghch058d1b46d2021-04-01 11:18:24 +0800696inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800697 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500698 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500699{
George Liu3de8d8b2021-03-22 17:49:39 +0800700 auto respHandler = [asyncResp,
701 entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500702 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
703 if (ec)
704 {
George Liu3de8d8b2021-03-22 17:49:39 +0800705 if (ec.value() == EBADR)
706 {
707 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
708 return;
709 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500710 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
711 << ec;
712 messages::internalError(asyncResp->res);
713 return;
714 }
715 };
716 crow::connections::systemBus->async_method_call(
717 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500718 "/xyz/openbmc_project/dump/" +
719 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
720 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500721 "xyz.openbmc_project.Object.Delete", "Delete");
722}
723
zhanghch058d1b46d2021-04-01 11:18:24 +0800724inline void
725 createDumpTaskCallback(const crow::Request& req,
726 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
727 const uint32_t& dumpId, const std::string& dumpPath,
728 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500729{
730 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500731 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500732 boost::system::error_code err, sdbusplus::message::message& m,
733 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000734 if (err)
735 {
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500736 BMCWEB_LOG_ERROR << "Error in creating a dump";
737 taskData->state = "Cancelled";
738 return task::completed;
Ed Tanouscb13a392020-07-25 19:02:03 +0000739 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500740 std::vector<std::pair<
741 std::string,
742 std::vector<std::pair<std::string, std::variant<std::string>>>>>
743 interfacesList;
744
745 sdbusplus::message::object_path objPath;
746
747 m.read(objPath, interfacesList);
748
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500749 if (objPath.str ==
750 "/xyz/openbmc_project/dump/" +
751 std::string(boost::algorithm::to_lower_copy(dumpType)) +
752 "/entry/" + std::to_string(dumpId))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500753 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500754 nlohmann::json retMessage = messages::success();
755 taskData->messages.emplace_back(retMessage);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500756
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500757 std::string headerLoc =
758 "Location: " + dumpPath + std::to_string(dumpId);
759 taskData->payload->httpHeaders.emplace_back(
760 std::move(headerLoc));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500761
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500762 taskData->state = "Completed";
763 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500764 }
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500765 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500766 },
767 "type='signal',interface='org.freedesktop.DBus."
768 "ObjectManager',"
769 "member='InterfacesAdded', "
770 "path='/xyz/openbmc_project/dump'");
771
772 task->startTimer(std::chrono::minutes(3));
773 task->populateResp(asyncResp->res);
774 task->payload.emplace(req);
775}
776
zhanghch058d1b46d2021-04-01 11:18:24 +0800777inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
778 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500779{
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500780
781 std::string dumpPath;
782 if (dumpType == "BMC")
783 {
784 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
785 }
786 else if (dumpType == "System")
787 {
788 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
789 }
790 else
791 {
792 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
793 messages::internalError(asyncResp->res);
794 return;
795 }
796
797 std::optional<std::string> diagnosticDataType;
798 std::optional<std::string> oemDiagnosticDataType;
799
800 if (!redfish::json_util::readJson(
801 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
802 "OEMDiagnosticDataType", oemDiagnosticDataType))
803 {
804 return;
805 }
806
807 if (dumpType == "System")
808 {
809 if (!oemDiagnosticDataType || !diagnosticDataType)
810 {
811 BMCWEB_LOG_ERROR << "CreateDump action parameter "
812 "'DiagnosticDataType'/"
813 "'OEMDiagnosticDataType' value not found!";
814 messages::actionParameterMissing(
815 asyncResp->res, "CollectDiagnosticData",
816 "DiagnosticDataType & OEMDiagnosticDataType");
817 return;
818 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700819 if ((*oemDiagnosticDataType != "System") ||
820 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500821 {
822 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
823 messages::invalidObject(asyncResp->res,
824 "System Dump creation parameters");
825 return;
826 }
827 }
828 else if (dumpType == "BMC")
829 {
830 if (!diagnosticDataType)
831 {
832 BMCWEB_LOG_ERROR << "CreateDump action parameter "
833 "'DiagnosticDataType' not found!";
834 messages::actionParameterMissing(
835 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
836 return;
837 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700838 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500839 {
840 BMCWEB_LOG_ERROR
841 << "Wrong parameter value passed for 'DiagnosticDataType'";
842 messages::invalidObject(asyncResp->res,
843 "BMC Dump creation parameters");
844 return;
845 }
846 }
847
848 crow::connections::systemBus->async_method_call(
849 [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
850 const uint32_t& dumpId) {
851 if (ec)
852 {
853 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
854 messages::internalError(asyncResp->res);
855 return;
856 }
857 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
858
859 createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
860 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500861 "xyz.openbmc_project.Dump.Manager",
862 "/xyz/openbmc_project/dump/" +
863 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500864 "xyz.openbmc_project.Dump.Create", "CreateDump");
865}
866
zhanghch058d1b46d2021-04-01 11:18:24 +0800867inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
868 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500869{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500870 std::string dumpTypeLowerCopy =
871 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800872
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500873 crow::connections::systemBus->async_method_call(
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500874 [asyncResp, dumpType](const boost::system::error_code ec,
875 const std::vector<std::string>& subTreePaths) {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500876 if (ec)
877 {
878 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
879 messages::internalError(asyncResp->res);
880 return;
881 }
882
883 for (const std::string& path : subTreePaths)
884 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000885 sdbusplus::message::object_path objPath(path);
886 std::string logID = objPath.filename();
887 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500888 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000889 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500890 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000891 deleteDumpEntry(asyncResp, logID, dumpType);
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500892 }
893 },
894 "xyz.openbmc_project.ObjectMapper",
895 "/xyz/openbmc_project/object_mapper",
896 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500897 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
898 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
899 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500900}
901
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700902inline static void parseCrashdumpParameters(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500903 const std::vector<std::pair<std::string, VariantType>>& params,
904 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700905{
906 for (auto property : params)
907 {
908 if (property.first == "Timestamp")
909 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500910 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500911 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700912 if (value != nullptr)
913 {
914 timestamp = *value;
915 }
916 }
917 else if (property.first == "Filename")
918 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500919 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500920 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700921 if (value != nullptr)
922 {
923 filename = *value;
924 }
925 }
926 else if (property.first == "Log")
927 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500928 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500929 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700930 if (value != nullptr)
931 {
932 logfile = *value;
933 }
934 }
935 }
936}
937
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500938constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700939inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700940{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800941 /**
942 * Functions triggers appropriate requests on DBus
943 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700944 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700945 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700946 .methods(boost::beast::http::verb::get)(
947 [](const crow::Request&,
948 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
949
950 {
951 // Collections don't include the static data added by SubRoute
952 // because it has a duplicate entry for members
953 asyncResp->res.jsonValue["@odata.type"] =
954 "#LogServiceCollection.LogServiceCollection";
955 asyncResp->res.jsonValue["@odata.id"] =
956 "/redfish/v1/Systems/system/LogServices";
957 asyncResp->res.jsonValue["Name"] =
958 "System Log Services Collection";
959 asyncResp->res.jsonValue["Description"] =
960 "Collection of LogServices for this Computer System";
961 nlohmann::json& logServiceArray =
962 asyncResp->res.jsonValue["Members"];
963 logServiceArray = nlohmann::json::array();
964 logServiceArray.push_back(
965 {{"@odata.id",
966 "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500967#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700968 logServiceArray.push_back(
969 {{"@odata.id",
970 "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600971#endif
972
Jason M. Billsd53dd412019-02-12 17:16:22 -0800973#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700974 logServiceArray.push_back(
975 {{"@odata.id",
976 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800977#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700978 asyncResp->res.jsonValue["Members@odata.count"] =
979 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800980
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700981 crow::connections::systemBus->async_method_call(
982 [asyncResp](const boost::system::error_code ec,
983 const std::vector<std::string>& subtreePath) {
984 if (ec)
985 {
986 BMCWEB_LOG_ERROR << ec;
987 return;
988 }
ZhikuiRena3316fc2020-01-29 14:58:08 -0800989
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700990 for (auto& pathStr : subtreePath)
991 {
992 if (pathStr.find("PostCode") != std::string::npos)
993 {
994 nlohmann::json& logServiceArrayLocal =
995 asyncResp->res.jsonValue["Members"];
996 logServiceArrayLocal.push_back(
997 {{"@odata.id", "/redfish/v1/Systems/system/"
998 "LogServices/PostCodes"}});
999 asyncResp->res
1000 .jsonValue["Members@odata.count"] =
1001 logServiceArrayLocal.size();
1002 return;
1003 }
1004 }
1005 },
1006 "xyz.openbmc_project.ObjectMapper",
1007 "/xyz/openbmc_project/object_mapper",
1008 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1009 0, std::array<const char*, 1>{postCodeIface});
1010 });
1011}
1012
1013inline void requestRoutesEventLogService(App& app)
1014{
1015 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001016 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001017 .methods(
1018 boost::beast::http::verb::
1019 get)([](const crow::Request&,
1020 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1021 asyncResp->res.jsonValue["@odata.id"] =
1022 "/redfish/v1/Systems/system/LogServices/EventLog";
1023 asyncResp->res.jsonValue["@odata.type"] =
1024 "#LogService.v1_1_0.LogService";
1025 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1026 asyncResp->res.jsonValue["Description"] =
1027 "System Event Log Service";
1028 asyncResp->res.jsonValue["Id"] = "EventLog";
1029 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301030
1031 std::pair<std::string, std::string> redfishDateTimeOffset =
1032 crow::utility::getDateTimeOffsetNow();
1033
1034 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1035 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1036 redfishDateTimeOffset.second;
1037
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001038 asyncResp->res.jsonValue["Entries"] = {
1039 {"@odata.id",
1040 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1041 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1042
1043 {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
1044 "Actions/LogService.ClearLog"}};
1045 });
1046}
1047
1048inline void requestRoutesJournalEventLogClear(App& app)
1049{
1050 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1051 "LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001052 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001053 .methods(boost::beast::http::verb::post)(
1054 [](const crow::Request&,
1055 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1056 // Clear the EventLog by deleting the log files
1057 std::vector<std::filesystem::path> redfishLogFiles;
1058 if (getRedfishLogFiles(redfishLogFiles))
ZhikuiRena3316fc2020-01-29 14:58:08 -08001059 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001060 for (const std::filesystem::path& file : redfishLogFiles)
ZhikuiRena3316fc2020-01-29 14:58:08 -08001061 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001062 std::error_code ec;
1063 std::filesystem::remove(file, ec);
ZhikuiRena3316fc2020-01-29 14:58:08 -08001064 }
1065 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001066
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001067 // Reload rsyslog so it knows to start new log files
1068 crow::connections::systemBus->async_method_call(
1069 [asyncResp](const boost::system::error_code ec) {
1070 if (ec)
1071 {
1072 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1073 << ec;
1074 messages::internalError(asyncResp->res);
1075 return;
1076 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001077
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001078 messages::success(asyncResp->res);
1079 },
1080 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1081 "org.freedesktop.systemd1.Manager", "ReloadUnit",
1082 "rsyslog.service", "replace");
1083 });
1084}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001085
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001086static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001087 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001088 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001089{
Jason M. Bills95820182019-04-22 16:25:34 -07001090 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001091 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001092 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001093 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001094 {
1095 return 1;
1096 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001097 std::string timestamp = logEntry.substr(0, space);
1098 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001099 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001100 if (entryStart == std::string::npos)
1101 {
1102 return 1;
1103 }
1104 std::string_view entry(logEntry);
1105 entry.remove_prefix(entryStart);
1106 // Use split to separate the entry into its fields
1107 std::vector<std::string> logEntryFields;
1108 boost::split(logEntryFields, entry, boost::is_any_of(","),
1109 boost::token_compress_on);
1110 // We need at least a MessageId to be valid
1111 if (logEntryFields.size() < 1)
1112 {
1113 return 1;
1114 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001115 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001116
Jason M. Bills4851d452019-03-28 11:27:48 -07001117 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001118 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001119 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001120
Jason M. Bills4851d452019-03-28 11:27:48 -07001121 std::string msg;
1122 std::string severity;
1123 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001124 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001125 msg = message->message;
1126 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001127 }
1128
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001129 // Get the MessageArgs from the log if there are any
1130 boost::beast::span<std::string> messageArgs;
1131 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001132 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001133 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001134 // If the first string is empty, assume there are no MessageArgs
1135 std::size_t messageArgsSize = 0;
1136 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001137 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001138 messageArgsSize = logEntryFields.size() - 1;
1139 }
1140
Ed Tanous23a21a12020-07-25 04:45:05 +00001141 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001142
1143 // Fill the MessageArgs into the Message
1144 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001145 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001146 {
1147 std::string argStr = "%" + std::to_string(++i);
1148 size_t argPos = msg.find(argStr);
1149 if (argPos != std::string::npos)
1150 {
1151 msg.replace(argPos, argStr.length(), messageArg);
1152 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001153 }
1154 }
1155
Jason M. Bills95820182019-04-22 16:25:34 -07001156 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1157 // format which matches the Redfish format except for the fractional seconds
1158 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001159 std::size_t dot = timestamp.find_first_of('.');
1160 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001161 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001162 {
Jason M. Bills95820182019-04-22 16:25:34 -07001163 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001164 }
1165
1166 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001167 logEntryJson = {
Ed Tanousd0dbeef2021-07-01 08:46:46 -07001168 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001169 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001170 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001171 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001172 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001173 {"Id", logEntryID},
1174 {"Message", std::move(msg)},
1175 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001176 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001177 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001178 {"Severity", std::move(severity)},
1179 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001180 return 0;
1181}
1182
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001183inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001184{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001185 BMCWEB_ROUTE(app,
1186 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001187 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001188 .methods(boost::beast::http::verb::get)(
1189 [](const crow::Request& req,
1190 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1191 uint64_t skip = 0;
1192 uint64_t top = maxEntriesPerPage; // Show max entries by default
1193 if (!getSkipParam(asyncResp, req, skip))
Jason M. Bills95820182019-04-22 16:25:34 -07001194 {
Jason M. Bills95820182019-04-22 16:25:34 -07001195 return;
1196 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001197 if (!getTopParam(asyncResp, req, top))
Jason M. Bills897967d2019-07-29 17:05:30 -07001198 {
Jason M. Bills897967d2019-07-29 17:05:30 -07001199 return;
1200 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001201 // Collections don't include the static data added by SubRoute
1202 // because it has a duplicate entry for members
1203 asyncResp->res.jsonValue["@odata.type"] =
1204 "#LogEntryCollection.LogEntryCollection";
1205 asyncResp->res.jsonValue["@odata.id"] =
1206 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1207 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1208 asyncResp->res.jsonValue["Description"] =
1209 "Collection of System Event Log Entries";
Jason M. Bills897967d2019-07-29 17:05:30 -07001210
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001211 nlohmann::json& logEntryArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001212 asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001213 logEntryArray = nlohmann::json::array();
1214 // Go through the log files and create a unique ID for each
1215 // entry
1216 std::vector<std::filesystem::path> redfishLogFiles;
1217 getRedfishLogFiles(redfishLogFiles);
1218 uint64_t entryCount = 0;
1219 std::string logEntry;
1220
1221 // Oldest logs are in the last file, so start there and loop
1222 // backwards
1223 for (auto it = redfishLogFiles.rbegin();
1224 it < redfishLogFiles.rend(); it++)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001225 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001226 std::ifstream logStream(*it);
1227 if (!logStream.is_open())
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001228 {
1229 continue;
1230 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001231
1232 // Reset the unique ID on the first entry
1233 bool firstEntry = true;
1234 while (std::getline(logStream, logEntry))
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001235 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001236 entryCount++;
1237 // Handle paging using skip (number of entries to skip
1238 // from the start) and top (number of entries to
1239 // display)
1240 if (entryCount <= skip || entryCount > skip + top)
George Liuebd45902020-08-26 14:21:10 +08001241 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001242 continue;
George Liuebd45902020-08-26 14:21:10 +08001243 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001244
1245 std::string idStr;
1246 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
George Liuebd45902020-08-26 14:21:10 +08001247 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001248 continue;
George Liuebd45902020-08-26 14:21:10 +08001249 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001250
1251 if (firstEntry)
1252 {
1253 firstEntry = false;
1254 }
1255
1256 logEntryArray.push_back({});
1257 nlohmann::json& bmcLogEntry = logEntryArray.back();
1258 if (fillEventLogEntryJson(idStr, logEntry,
1259 bmcLogEntry) != 0)
Xiaochao Ma75710de2021-01-21 17:56:02 +08001260 {
1261 messages::internalError(asyncResp->res);
1262 return;
1263 }
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001264 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001265 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001266 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1267 if (skip + top < entryCount)
Ed Tanous271584a2019-07-09 16:24:22 -07001268 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001269 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001270 "/redfish/v1/Systems/system/LogServices/EventLog/"
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001271 "Entries?$skip=" +
1272 std::to_string(skip + top);
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001273 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001274 });
1275}
Chicago Duan336e96c2019-07-15 14:22:08 +08001276
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001277inline void requestRoutesJournalEventLogEntry(App& app)
1278{
1279 BMCWEB_ROUTE(
1280 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001281 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001282 .methods(boost::beast::http::verb::get)(
1283 [](const crow::Request&,
1284 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1285 const std::string& param) {
1286 const std::string& targetID = param;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001287
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001288 // Go through the log files and check the unique ID for each
1289 // entry to find the target entry
1290 std::vector<std::filesystem::path> redfishLogFiles;
1291 getRedfishLogFiles(redfishLogFiles);
1292 std::string logEntry;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001293
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001294 // Oldest logs are in the last file, so start there and loop
1295 // backwards
1296 for (auto it = redfishLogFiles.rbegin();
1297 it < redfishLogFiles.rend(); it++)
1298 {
1299 std::ifstream logStream(*it);
1300 if (!logStream.is_open())
1301 {
1302 continue;
1303 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001304
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001305 // Reset the unique ID on the first entry
1306 bool firstEntry = true;
1307 while (std::getline(logStream, logEntry))
1308 {
1309 std::string idStr;
1310 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1311 {
1312 continue;
1313 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001314
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001315 if (firstEntry)
1316 {
1317 firstEntry = false;
1318 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001319
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001320 if (idStr == targetID)
1321 {
1322 if (fillEventLogEntryJson(
1323 idStr, logEntry,
1324 asyncResp->res.jsonValue) != 0)
1325 {
1326 messages::internalError(asyncResp->res);
1327 return;
1328 }
1329 return;
1330 }
1331 }
1332 }
1333 // Requested ID was not found
1334 messages::resourceMissingAtURI(asyncResp->res, targetID);
1335 });
1336}
1337
1338inline void requestRoutesDBusEventLogEntryCollection(App& app)
1339{
1340 BMCWEB_ROUTE(app,
1341 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001342 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001343 .methods(
1344 boost::beast::http::verb::
1345 get)([](const crow::Request&,
1346 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1347 // Collections don't include the static data added by SubRoute
1348 // because it has a duplicate entry for members
1349 asyncResp->res.jsonValue["@odata.type"] =
1350 "#LogEntryCollection.LogEntryCollection";
1351 asyncResp->res.jsonValue["@odata.id"] =
1352 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1353 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1354 asyncResp->res.jsonValue["Description"] =
1355 "Collection of System Event Log Entries";
1356
1357 // DBus implementation of EventLog/Entries
1358 // Make call to Logging Service to find all log entry objects
Xiaochao Ma75710de2021-01-21 17:56:02 +08001359 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001360 [asyncResp](const boost::system::error_code ec,
1361 GetManagedObjectsType& resp) {
Xiaochao Ma75710de2021-01-21 17:56:02 +08001362 if (ec)
1363 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001364 // TODO Handle for specific error code
1365 BMCWEB_LOG_ERROR
1366 << "getLogEntriesIfaceData resp_handler got error "
1367 << ec;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001368 messages::internalError(asyncResp->res);
1369 return;
1370 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001371 nlohmann::json& entriesArray =
1372 asyncResp->res.jsonValue["Members"];
1373 entriesArray = nlohmann::json::array();
1374 for (auto& objectPath : resp)
1375 {
1376 uint32_t* id = nullptr;
1377 std::time_t timestamp{};
1378 std::time_t updateTimestamp{};
1379 std::string* severity = nullptr;
1380 std::string* message = nullptr;
1381 std::string* filePath = nullptr;
1382 bool resolved = false;
1383 for (auto& interfaceMap : objectPath.second)
1384 {
1385 if (interfaceMap.first ==
1386 "xyz.openbmc_project.Logging.Entry")
1387 {
1388 for (auto& propertyMap : interfaceMap.second)
1389 {
1390 if (propertyMap.first == "Id")
1391 {
1392 id = std::get_if<uint32_t>(
1393 &propertyMap.second);
1394 }
1395 else if (propertyMap.first == "Timestamp")
1396 {
1397 const uint64_t* millisTimeStamp =
1398 std::get_if<uint64_t>(
1399 &propertyMap.second);
1400 if (millisTimeStamp != nullptr)
1401 {
1402 timestamp =
1403 crow::utility::getTimestamp(
1404 *millisTimeStamp);
1405 }
1406 }
1407 else if (propertyMap.first ==
1408 "UpdateTimestamp")
1409 {
1410 const uint64_t* millisTimeStamp =
1411 std::get_if<uint64_t>(
1412 &propertyMap.second);
1413 if (millisTimeStamp != nullptr)
1414 {
1415 updateTimestamp =
1416 crow::utility::getTimestamp(
1417 *millisTimeStamp);
1418 }
1419 }
1420 else if (propertyMap.first == "Severity")
1421 {
1422 severity = std::get_if<std::string>(
1423 &propertyMap.second);
1424 }
1425 else if (propertyMap.first == "Message")
1426 {
1427 message = std::get_if<std::string>(
1428 &propertyMap.second);
1429 }
1430 else if (propertyMap.first == "Resolved")
1431 {
1432 bool* resolveptr = std::get_if<bool>(
1433 &propertyMap.second);
1434 if (resolveptr == nullptr)
1435 {
1436 messages::internalError(
1437 asyncResp->res);
1438 return;
1439 }
1440 resolved = *resolveptr;
1441 }
1442 }
1443 if (id == nullptr || message == nullptr ||
1444 severity == nullptr)
1445 {
1446 messages::internalError(asyncResp->res);
1447 return;
1448 }
1449 }
1450 else if (interfaceMap.first ==
1451 "xyz.openbmc_project.Common.FilePath")
1452 {
1453 for (auto& propertyMap : interfaceMap.second)
1454 {
1455 if (propertyMap.first == "Path")
1456 {
1457 filePath = std::get_if<std::string>(
1458 &propertyMap.second);
1459 }
1460 }
1461 }
1462 }
1463 // Object path without the
1464 // xyz.openbmc_project.Logging.Entry interface, ignore
1465 // and continue.
1466 if (id == nullptr || message == nullptr ||
1467 severity == nullptr)
1468 {
1469 continue;
1470 }
1471 entriesArray.push_back({});
1472 nlohmann::json& thisEntry = entriesArray.back();
1473 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1474 thisEntry["@odata.id"] =
1475 "/redfish/v1/Systems/system/"
1476 "LogServices/EventLog/Entries/" +
1477 std::to_string(*id);
1478 thisEntry["Name"] = "System Event Log Entry";
1479 thisEntry["Id"] = std::to_string(*id);
1480 thisEntry["Message"] = *message;
1481 thisEntry["Resolved"] = resolved;
1482 thisEntry["EntryType"] = "Event";
1483 thisEntry["Severity"] =
1484 translateSeverityDbusToRedfish(*severity);
1485 thisEntry["Created"] =
1486 crow::utility::getDateTime(timestamp);
1487 thisEntry["Modified"] =
1488 crow::utility::getDateTime(updateTimestamp);
1489 if (filePath != nullptr)
1490 {
1491 thisEntry["AdditionalDataURI"] =
1492 "/redfish/v1/Systems/system/LogServices/"
1493 "EventLog/"
1494 "Entries/" +
1495 std::to_string(*id) + "/attachment";
1496 }
1497 }
1498 std::sort(entriesArray.begin(), entriesArray.end(),
1499 [](const nlohmann::json& left,
1500 const nlohmann::json& right) {
1501 return (left["Id"] <= right["Id"]);
1502 });
1503 asyncResp->res.jsonValue["Members@odata.count"] =
1504 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001505 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001506 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1507 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1508 });
1509}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001510
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001511inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001512{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001513 BMCWEB_ROUTE(
1514 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001515 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001516 .methods(boost::beast::http::verb::get)(
1517 [](const crow::Request&,
1518 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1519 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001520
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001521 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001522 std::string entryID = param;
1523 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001524
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001525 // DBus implementation of EventLog/Entries
1526 // Make call to Logging Service to find all log entry objects
1527 crow::connections::systemBus->async_method_call(
1528 [asyncResp, entryID](const boost::system::error_code ec,
1529 GetManagedPropertyType& resp) {
1530 if (ec.value() == EBADR)
1531 {
1532 messages::resourceNotFound(
1533 asyncResp->res, "EventLogEntry", entryID);
1534 return;
1535 }
1536 if (ec)
1537 {
1538 BMCWEB_LOG_ERROR << "EventLogEntry (DBus) "
1539 "resp_handler got error "
1540 << ec;
1541 messages::internalError(asyncResp->res);
1542 return;
1543 }
1544 uint32_t* id = nullptr;
1545 std::time_t timestamp{};
1546 std::time_t updateTimestamp{};
1547 std::string* severity = nullptr;
1548 std::string* message = nullptr;
1549 std::string* filePath = nullptr;
1550 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001551
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001552 for (auto& propertyMap : resp)
1553 {
1554 if (propertyMap.first == "Id")
1555 {
1556 id = std::get_if<uint32_t>(&propertyMap.second);
1557 }
1558 else if (propertyMap.first == "Timestamp")
1559 {
1560 const uint64_t* millisTimeStamp =
1561 std::get_if<uint64_t>(&propertyMap.second);
1562 if (millisTimeStamp != nullptr)
1563 {
1564 timestamp = crow::utility::getTimestamp(
1565 *millisTimeStamp);
1566 }
1567 }
1568 else if (propertyMap.first == "UpdateTimestamp")
1569 {
1570 const uint64_t* millisTimeStamp =
1571 std::get_if<uint64_t>(&propertyMap.second);
1572 if (millisTimeStamp != nullptr)
1573 {
1574 updateTimestamp =
1575 crow::utility::getTimestamp(
1576 *millisTimeStamp);
1577 }
1578 }
1579 else if (propertyMap.first == "Severity")
1580 {
1581 severity = std::get_if<std::string>(
1582 &propertyMap.second);
1583 }
1584 else if (propertyMap.first == "Message")
1585 {
1586 message = std::get_if<std::string>(
1587 &propertyMap.second);
1588 }
1589 else if (propertyMap.first == "Resolved")
1590 {
1591 bool* resolveptr =
1592 std::get_if<bool>(&propertyMap.second);
1593 if (resolveptr == nullptr)
1594 {
1595 messages::internalError(asyncResp->res);
1596 return;
1597 }
1598 resolved = *resolveptr;
1599 }
1600 else if (propertyMap.first == "Path")
1601 {
1602 filePath = std::get_if<std::string>(
1603 &propertyMap.second);
1604 }
1605 }
1606 if (id == nullptr || message == nullptr ||
1607 severity == nullptr)
1608 {
1609 messages::internalError(asyncResp->res);
1610 return;
1611 }
1612 asyncResp->res.jsonValue["@odata.type"] =
1613 "#LogEntry.v1_8_0.LogEntry";
1614 asyncResp->res.jsonValue["@odata.id"] =
1615 "/redfish/v1/Systems/system/LogServices/EventLog/"
1616 "Entries/" +
1617 std::to_string(*id);
1618 asyncResp->res.jsonValue["Name"] =
1619 "System Event Log Entry";
1620 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1621 asyncResp->res.jsonValue["Message"] = *message;
1622 asyncResp->res.jsonValue["Resolved"] = resolved;
1623 asyncResp->res.jsonValue["EntryType"] = "Event";
1624 asyncResp->res.jsonValue["Severity"] =
1625 translateSeverityDbusToRedfish(*severity);
1626 asyncResp->res.jsonValue["Created"] =
1627 crow::utility::getDateTime(timestamp);
1628 asyncResp->res.jsonValue["Modified"] =
1629 crow::utility::getDateTime(updateTimestamp);
1630 if (filePath != nullptr)
1631 {
1632 asyncResp->res.jsonValue["AdditionalDataURI"] =
1633 "/redfish/v1/Systems/system/LogServices/"
1634 "EventLog/"
1635 "attachment/" +
1636 std::to_string(*id);
1637 }
1638 },
1639 "xyz.openbmc_project.Logging",
1640 "/xyz/openbmc_project/logging/entry/" + entryID,
1641 "org.freedesktop.DBus.Properties", "GetAll", "");
1642 });
1643
1644 BMCWEB_ROUTE(
1645 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001646 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001647 .methods(boost::beast::http::verb::patch)(
1648 [](const crow::Request& req,
1649 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1650 const std::string& entryId) {
1651 std::optional<bool> resolved;
1652
1653 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1654 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001655 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001656 return;
1657 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001658 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001659
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001660 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001661 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001662 if (ec)
1663 {
1664 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1665 messages::internalError(asyncResp->res);
1666 return;
1667 }
1668 },
1669 "xyz.openbmc_project.Logging",
1670 "/xyz/openbmc_project/logging/entry/" + entryId,
1671 "org.freedesktop.DBus.Properties", "Set",
1672 "xyz.openbmc_project.Logging.Entry", "Resolved",
1673 std::variant<bool>(*resolved));
1674 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001675
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001676 BMCWEB_ROUTE(
1677 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001678 .privileges(redfish::privileges::deleteLogEntry)
1679
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001680 .methods(boost::beast::http::verb::delete_)(
1681 [](const crow::Request&,
1682 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1683 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001684
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001685 {
1686 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001687
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001688 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001689
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001690 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001691
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001692 // Process response from Logging service.
1693 auto respHandler = [asyncResp, entryID](
1694 const boost::system::error_code ec) {
1695 BMCWEB_LOG_DEBUG
1696 << "EventLogEntry (DBus) doDelete callback: Done";
1697 if (ec)
1698 {
1699 if (ec.value() == EBADR)
1700 {
1701 messages::resourceNotFound(asyncResp->res,
1702 "LogEntry", entryID);
1703 return;
1704 }
1705 // TODO Handle for specific error code
1706 BMCWEB_LOG_ERROR << "EventLogEntry (DBus) doDelete "
1707 "respHandler got error "
1708 << ec;
1709 asyncResp->res.result(
1710 boost::beast::http::status::internal_server_error);
1711 return;
1712 }
1713
1714 asyncResp->res.result(boost::beast::http::status::ok);
1715 };
1716
1717 // Make call to Logging service to request Delete Log
1718 crow::connections::systemBus->async_method_call(
1719 respHandler, "xyz.openbmc_project.Logging",
1720 "/xyz/openbmc_project/logging/entry/" + entryID,
1721 "xyz.openbmc_project.Object.Delete", "Delete");
1722 });
1723}
1724
1725inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001726{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001727 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/"
1728 "<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001729 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001730 .methods(boost::beast::http::verb::get)(
1731 [](const crow::Request& req,
1732 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1733 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001734
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001735 {
Ed Tanous753d0342021-07-01 07:57:30 -07001736 std::string_view acceptHeader = req.getHeaderValue("Accept");
1737 // The iterators in boost/http/rfc7230.hpp end the string if '/'
1738 // is found, so replace it with arbitrary character '|' which is
1739 // not part of the Accept header syntax.
1740 std::string acceptStr = boost::replace_all_copy(
1741 std::string(acceptHeader), "/", "|");
1742 boost::beast::http::ext_list acceptTypes{acceptStr};
1743 bool supported = false;
1744 for (const auto& type : acceptTypes)
1745 {
1746 if ((type.first == "*|*") ||
1747 (type.first == "application|octet-stream"))
1748 {
1749 supported = true;
1750 break;
1751 }
1752 }
1753 if (!supported)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001754 {
1755 asyncResp->res.result(
1756 boost::beast::http::status::bad_request);
1757 return;
1758 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001759
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001760 std::string entryID = param;
1761 dbus::utility::escapePathForDbus(entryID);
1762
1763 crow::connections::systemBus->async_method_call(
1764 [asyncResp,
1765 entryID](const boost::system::error_code ec,
1766 const sdbusplus::message::unix_fd& unixfd) {
1767 if (ec.value() == EBADR)
1768 {
1769 messages::resourceNotFound(
1770 asyncResp->res, "EventLogAttachment", entryID);
1771 return;
1772 }
1773 if (ec)
1774 {
1775 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1776 messages::internalError(asyncResp->res);
1777 return;
1778 }
1779
1780 int fd = -1;
1781 fd = dup(unixfd);
1782 if (fd == -1)
1783 {
1784 messages::internalError(asyncResp->res);
1785 return;
1786 }
1787
1788 long long int size = lseek(fd, 0, SEEK_END);
1789 if (size == -1)
1790 {
1791 messages::internalError(asyncResp->res);
1792 return;
1793 }
1794
1795 // Arbitrary max size of 64kb
1796 constexpr int maxFileSize = 65536;
1797 if (size > maxFileSize)
1798 {
1799 BMCWEB_LOG_ERROR
1800 << "File size exceeds maximum allowed size of "
1801 << maxFileSize;
1802 messages::internalError(asyncResp->res);
1803 return;
1804 }
1805 std::vector<char> data(static_cast<size_t>(size));
1806 long long int rc = lseek(fd, 0, SEEK_SET);
1807 if (rc == -1)
1808 {
1809 messages::internalError(asyncResp->res);
1810 return;
1811 }
1812 rc = read(fd, data.data(), data.size());
1813 if ((rc == -1) || (rc != size))
1814 {
1815 messages::internalError(asyncResp->res);
1816 return;
1817 }
1818 close(fd);
1819
1820 std::string_view strData(data.data(), data.size());
1821 std::string output =
1822 crow::utility::base64encode(strData);
1823
1824 asyncResp->res.addHeader("Content-Type",
1825 "application/octet-stream");
1826 asyncResp->res.addHeader("Content-Transfer-Encoding",
1827 "Base64");
1828 asyncResp->res.body() = std::move(output);
1829 },
1830 "xyz.openbmc_project.Logging",
1831 "/xyz/openbmc_project/logging/entry/" + entryID,
1832 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1833 });
1834}
1835
1836inline void requestRoutesBMCLogServiceCollection(App& app)
1837{
1838 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05001839 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001840 .methods(boost::beast::http::verb::get)(
1841 [](const crow::Request&,
1842 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1843 // Collections don't include the static data added by SubRoute
1844 // because it has a duplicate entry for members
1845 asyncResp->res.jsonValue["@odata.type"] =
1846 "#LogServiceCollection.LogServiceCollection";
1847 asyncResp->res.jsonValue["@odata.id"] =
1848 "/redfish/v1/Managers/bmc/LogServices";
1849 asyncResp->res.jsonValue["Name"] =
1850 "Open BMC Log Services Collection";
1851 asyncResp->res.jsonValue["Description"] =
1852 "Collection of LogServices for this Manager";
1853 nlohmann::json& logServiceArray =
1854 asyncResp->res.jsonValue["Members"];
1855 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001856#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001857 logServiceArray.push_back(
1858 {{"@odata.id",
1859 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001860#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001861#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001862 logServiceArray.push_back(
1863 {{"@odata.id",
1864 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001865#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001866 asyncResp->res.jsonValue["Members@odata.count"] =
1867 logServiceArray.size();
1868 });
1869}
Ed Tanous1da66f72018-07-27 16:13:37 -07001870
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001871inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001872{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001873 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07001874 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001875 .methods(boost::beast::http::verb::get)(
1876 [](const crow::Request&,
1877 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07001878
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001879 {
1880 asyncResp->res.jsonValue["@odata.type"] =
1881 "#LogService.v1_1_0.LogService";
1882 asyncResp->res.jsonValue["@odata.id"] =
1883 "/redfish/v1/Managers/bmc/LogServices/Journal";
1884 asyncResp->res.jsonValue["Name"] =
1885 "Open BMC Journal Log Service";
1886 asyncResp->res.jsonValue["Description"] =
1887 "BMC Journal Log Service";
1888 asyncResp->res.jsonValue["Id"] = "BMC Journal";
1889 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301890
1891 std::pair<std::string, std::string> redfishDateTimeOffset =
1892 crow::utility::getDateTimeOffsetNow();
1893 asyncResp->res.jsonValue["DateTime"] =
1894 redfishDateTimeOffset.first;
1895 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1896 redfishDateTimeOffset.second;
1897
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001898 asyncResp->res.jsonValue["Entries"] = {
1899 {"@odata.id",
1900 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1901 });
1902}
Jason M. Billse1f26342018-07-18 12:12:00 -07001903
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001904static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1905 sd_journal* journal,
1906 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07001907{
1908 // Get the Log Entry contents
1909 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07001910
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001911 std::string message;
1912 std::string_view syslogID;
1913 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
1914 if (ret < 0)
1915 {
1916 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
1917 << strerror(-ret);
1918 }
1919 if (!syslogID.empty())
1920 {
1921 message += std::string(syslogID) + ": ";
1922 }
1923
Ed Tanous39e77502019-03-04 17:35:53 -08001924 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07001925 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07001926 if (ret < 0)
1927 {
1928 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1929 return 1;
1930 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001931 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07001932
1933 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07001934 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07001935 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07001936 if (ret < 0)
1937 {
1938 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07001939 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001940
1941 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07001942 std::string entryTimeStr;
1943 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07001944 {
Jason M. Bills16428a12018-11-02 12:42:29 -07001945 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07001946 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001947
1948 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001949 bmcJournalLogEntryJson = {
Ed Tanousd0dbeef2021-07-01 08:46:46 -07001950 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001951 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1952 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07001953 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001954 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001955 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07001956 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06001957 {"Severity", severity <= 2 ? "Critical"
1958 : severity <= 4 ? "Warning"
1959 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07001960 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07001961 {"Created", std::move(entryTimeStr)}};
1962 return 0;
1963}
1964
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001965inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07001966{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001967 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001968 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001969 .methods(boost::beast::http::verb::get)(
1970 [](const crow::Request& req,
1971 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1972 static constexpr const long maxEntriesPerPage = 1000;
1973 uint64_t skip = 0;
1974 uint64_t top = maxEntriesPerPage; // Show max entries by default
1975 if (!getSkipParam(asyncResp, req, skip))
1976 {
1977 return;
1978 }
1979 if (!getTopParam(asyncResp, req, top))
1980 {
1981 return;
1982 }
1983 // Collections don't include the static data added by SubRoute
1984 // because it has a duplicate entry for members
1985 asyncResp->res.jsonValue["@odata.type"] =
1986 "#LogEntryCollection.LogEntryCollection";
1987 asyncResp->res.jsonValue["@odata.id"] =
1988 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1989 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1990 asyncResp->res.jsonValue["Description"] =
1991 "Collection of BMC Journal Entries";
1992 nlohmann::json& logEntryArray =
1993 asyncResp->res.jsonValue["Members"];
1994 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07001995
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001996 // Go through the journal and use the timestamp to create a
1997 // unique ID for each entry
1998 sd_journal* journalTmp = nullptr;
1999 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2000 if (ret < 0)
2001 {
2002 BMCWEB_LOG_ERROR << "failed to open journal: "
2003 << strerror(-ret);
2004 messages::internalError(asyncResp->res);
2005 return;
2006 }
2007 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2008 journal(journalTmp, sd_journal_close);
2009 journalTmp = nullptr;
2010 uint64_t entryCount = 0;
2011 // Reset the unique ID on the first entry
2012 bool firstEntry = true;
2013 SD_JOURNAL_FOREACH(journal.get())
2014 {
2015 entryCount++;
2016 // Handle paging using skip (number of entries to skip from
2017 // the start) and top (number of entries to display)
2018 if (entryCount <= skip || entryCount > skip + top)
2019 {
2020 continue;
2021 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002022
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002023 std::string idStr;
2024 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2025 {
2026 continue;
2027 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002028
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002029 if (firstEntry)
2030 {
2031 firstEntry = false;
2032 }
Jason M. Bills193ad2f2018-09-26 15:08:52 -07002033
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002034 logEntryArray.push_back({});
2035 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2036 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2037 bmcJournalLogEntry) != 0)
2038 {
2039 messages::internalError(asyncResp->res);
2040 return;
2041 }
2042 }
2043 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2044 if (skip + top < entryCount)
2045 {
2046 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2047 "/redfish/v1/Managers/bmc/LogServices/Journal/"
2048 "Entries?$skip=" +
2049 std::to_string(skip + top);
2050 }
2051 });
2052}
Jason M. Billse1f26342018-07-18 12:12:00 -07002053
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002054inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002055{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002056 BMCWEB_ROUTE(app,
2057 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002058 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002059 .methods(boost::beast::http::verb::get)(
2060 [](const crow::Request&,
2061 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2062 const std::string& entryID) {
2063 // Convert the unique ID back to a timestamp to find the entry
2064 uint64_t ts = 0;
2065 uint64_t index = 0;
2066 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2067 {
2068 return;
2069 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002070
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002071 sd_journal* journalTmp = nullptr;
2072 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2073 if (ret < 0)
2074 {
2075 BMCWEB_LOG_ERROR << "failed to open journal: "
2076 << strerror(-ret);
2077 messages::internalError(asyncResp->res);
2078 return;
2079 }
2080 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2081 journal(journalTmp, sd_journal_close);
2082 journalTmp = nullptr;
2083 // Go to the timestamp in the log and move to the entry at the
2084 // index tracking the unique ID
2085 std::string idStr;
2086 bool firstEntry = true;
2087 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2088 if (ret < 0)
2089 {
2090 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2091 << strerror(-ret);
2092 messages::internalError(asyncResp->res);
2093 return;
2094 }
2095 for (uint64_t i = 0; i <= index; i++)
2096 {
2097 sd_journal_next(journal.get());
2098 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2099 {
2100 messages::internalError(asyncResp->res);
2101 return;
2102 }
2103 if (firstEntry)
2104 {
2105 firstEntry = false;
2106 }
2107 }
2108 // Confirm that the entry ID matches what was requested
2109 if (idStr != entryID)
2110 {
2111 messages::resourceMissingAtURI(asyncResp->res, entryID);
2112 return;
2113 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002114
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002115 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2116 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002117 {
2118 messages::internalError(asyncResp->res);
2119 return;
2120 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002121 });
2122}
2123
2124inline void requestRoutesBMCDumpService(App& app)
2125{
2126 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002127 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002128 .methods(boost::beast::http::verb::get)(
2129 [](const crow::Request&,
2130 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2131 asyncResp->res.jsonValue["@odata.id"] =
2132 "/redfish/v1/Managers/bmc/LogServices/Dump";
2133 asyncResp->res.jsonValue["@odata.type"] =
2134 "#LogService.v1_2_0.LogService";
2135 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2136 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2137 asyncResp->res.jsonValue["Id"] = "Dump";
2138 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302139
2140 std::pair<std::string, std::string> redfishDateTimeOffset =
2141 crow::utility::getDateTimeOffsetNow();
2142 asyncResp->res.jsonValue["DateTime"] =
2143 redfishDateTimeOffset.first;
2144 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2145 redfishDateTimeOffset.second;
2146
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002147 asyncResp->res.jsonValue["Entries"] = {
2148 {"@odata.id",
2149 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2150 asyncResp->res.jsonValue["Actions"] = {
2151 {"#LogService.ClearLog",
2152 {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2153 "Actions/LogService.ClearLog"}}},
2154 {"#LogService.CollectDiagnosticData",
2155 {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2156 "Actions/LogService.CollectDiagnosticData"}}}};
2157 });
2158}
2159
2160inline void requestRoutesBMCDumpEntryCollection(App& app)
2161{
2162
2163 /**
2164 * Functions triggers appropriate requests on DBus
2165 */
2166 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002167 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002168 .methods(boost::beast::http::verb::get)(
2169 [](const crow::Request&,
2170 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2171 asyncResp->res.jsonValue["@odata.type"] =
2172 "#LogEntryCollection.LogEntryCollection";
2173 asyncResp->res.jsonValue["@odata.id"] =
2174 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2175 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2176 asyncResp->res.jsonValue["Description"] =
2177 "Collection of BMC Dump Entries";
2178
2179 getDumpEntryCollection(asyncResp, "BMC");
2180 });
2181}
2182
2183inline void requestRoutesBMCDumpEntry(App& app)
2184{
2185 BMCWEB_ROUTE(app,
2186 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002187 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002188 .methods(boost::beast::http::verb::get)(
2189 [](const crow::Request&,
2190 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2191 const std::string& param) {
2192 getDumpEntryById(asyncResp, param, "BMC");
2193 });
2194 BMCWEB_ROUTE(app,
2195 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002196 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002197 .methods(boost::beast::http::verb::delete_)(
2198 [](const crow::Request&,
2199 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2200 const std::string& param) {
2201 deleteDumpEntry(asyncResp, param, "bmc");
2202 });
2203}
2204
2205inline void requestRoutesBMCDumpCreate(App& app)
2206{
2207
2208 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2209 "Actions/"
2210 "LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002211 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002212 .methods(boost::beast::http::verb::post)(
2213 [](const crow::Request& req,
2214 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2215 createDump(asyncResp, req, "BMC");
2216 });
2217}
2218
2219inline void requestRoutesBMCDumpClear(App& app)
2220{
2221 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2222 "Actions/"
2223 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002224 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002225 .methods(boost::beast::http::verb::post)(
2226 [](const crow::Request&,
2227 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2228 clearDump(asyncResp, "BMC");
2229 });
2230}
2231
2232inline void requestRoutesSystemDumpService(App& app)
2233{
2234 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002235 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002236 .methods(boost::beast::http::verb::get)(
2237 [](const crow::Request&,
2238 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2239
2240 {
2241 asyncResp->res.jsonValue["@odata.id"] =
2242 "/redfish/v1/Systems/system/LogServices/Dump";
2243 asyncResp->res.jsonValue["@odata.type"] =
2244 "#LogService.v1_2_0.LogService";
2245 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2246 asyncResp->res.jsonValue["Description"] =
2247 "System Dump LogService";
2248 asyncResp->res.jsonValue["Id"] = "Dump";
2249 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302250
2251 std::pair<std::string, std::string> redfishDateTimeOffset =
2252 crow::utility::getDateTimeOffsetNow();
2253 asyncResp->res.jsonValue["DateTime"] =
2254 redfishDateTimeOffset.first;
2255 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2256 redfishDateTimeOffset.second;
2257
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002258 asyncResp->res.jsonValue["Entries"] = {
2259 {"@odata.id",
2260 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2261 asyncResp->res.jsonValue["Actions"] = {
2262 {"#LogService.ClearLog",
2263 {{"target",
2264 "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2265 "LogService.ClearLog"}}},
2266 {"#LogService.CollectDiagnosticData",
2267 {{"target",
2268 "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2269 "LogService.CollectDiagnosticData"}}}};
2270 });
2271}
2272
2273inline void requestRoutesSystemDumpEntryCollection(App& app)
2274{
2275
2276 /**
2277 * Functions triggers appropriate requests on DBus
2278 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002279 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002280 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002281 .methods(boost::beast::http::verb::get)(
2282 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002283 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002284 asyncResp->res.jsonValue["@odata.type"] =
2285 "#LogEntryCollection.LogEntryCollection";
2286 asyncResp->res.jsonValue["@odata.id"] =
2287 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2288 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2289 asyncResp->res.jsonValue["Description"] =
2290 "Collection of System Dump Entries";
2291
2292 getDumpEntryCollection(asyncResp, "System");
2293 });
2294}
2295
2296inline void requestRoutesSystemDumpEntry(App& app)
2297{
2298 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002299 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002300 .privileges(redfish::privileges::getLogEntry)
2301
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002302 .methods(boost::beast::http::verb::get)(
2303 [](const crow::Request&,
2304 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2305 const std::string& param) {
2306 getDumpEntryById(asyncResp, param, "System");
2307 });
2308
2309 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002310 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002311 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002312 .methods(boost::beast::http::verb::delete_)(
2313 [](const crow::Request&,
2314 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2315 const std::string& param) {
2316 deleteDumpEntry(asyncResp, param, "system");
2317 });
2318}
2319
2320inline void requestRoutesSystemDumpCreate(App& app)
2321{
2322 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2323 "Actions/"
2324 "LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002325 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002326 .methods(boost::beast::http::verb::post)(
2327 [](const crow::Request& req,
2328 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2329
2330 { createDump(asyncResp, req, "System"); });
2331}
2332
2333inline void requestRoutesSystemDumpClear(App& app)
2334{
2335 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2336 "Actions/"
2337 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002338 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002339 .methods(boost::beast::http::verb::post)(
2340 [](const crow::Request&,
2341 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2342
2343 { clearDump(asyncResp, "System"); });
2344}
2345
2346inline void requestRoutesCrashdumpService(App& app)
2347{
2348 // Note: Deviated from redfish privilege registry for GET & HEAD
2349 // method for security reasons.
2350 /**
2351 * Functions triggers appropriate requests on DBus
2352 */
2353 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002354 // This is incorrect, should be:
2355 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002356 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002357 .methods(
2358 boost::beast::http::verb::
2359 get)([](const crow::Request&,
2360 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2361 // Copy over the static data to include the entries added by
2362 // SubRoute
2363 asyncResp->res.jsonValue["@odata.id"] =
2364 "/redfish/v1/Systems/system/LogServices/Crashdump";
2365 asyncResp->res.jsonValue["@odata.type"] =
2366 "#LogService.v1_2_0.LogService";
2367 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2368 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2369 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2370 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2371 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302372
2373 std::pair<std::string, std::string> redfishDateTimeOffset =
2374 crow::utility::getDateTimeOffsetNow();
2375 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2376 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2377 redfishDateTimeOffset.second;
2378
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002379 asyncResp->res.jsonValue["Entries"] = {
2380 {"@odata.id",
2381 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2382 asyncResp->res.jsonValue["Actions"] = {
2383 {"#LogService.ClearLog",
2384 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2385 "Actions/LogService.ClearLog"}}},
2386 {"#LogService.CollectDiagnosticData",
2387 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2388 "Actions/LogService.CollectDiagnosticData"}}}};
2389 });
2390}
2391
2392void inline requestRoutesCrashdumpClear(App& app)
2393{
2394 BMCWEB_ROUTE(app,
2395 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
2396 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002397 // This is incorrect, should be:
2398 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002399 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002400 .methods(boost::beast::http::verb::post)(
2401 [](const crow::Request&,
2402 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2403 crow::connections::systemBus->async_method_call(
2404 [asyncResp](const boost::system::error_code ec,
2405 const std::string&) {
2406 if (ec)
2407 {
2408 messages::internalError(asyncResp->res);
2409 return;
2410 }
2411 messages::success(asyncResp->res);
2412 },
2413 crashdumpObject, crashdumpPath, deleteAllInterface,
2414 "DeleteAll");
2415 });
2416}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002417
zhanghch058d1b46d2021-04-01 11:18:24 +08002418static void
2419 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2420 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002421{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002422 auto getStoredLogCallback =
2423 [asyncResp, logID, &logEntryJson](
2424 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002425 const std::vector<std::pair<std::string, VariantType>>& params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002426 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002427 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002428 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2429 if (ec.value() ==
2430 boost::system::linux_error::bad_request_descriptor)
2431 {
2432 messages::resourceNotFound(asyncResp->res, "LogEntry",
2433 logID);
2434 }
2435 else
2436 {
2437 messages::internalError(asyncResp->res);
2438 }
2439 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002440 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002441
Johnathan Mantey043a0532020-03-10 17:15:28 -07002442 std::string timestamp{};
2443 std::string filename{};
2444 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002445 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002446
2447 if (filename.empty() || timestamp.empty())
2448 {
2449 messages::resourceMissingAtURI(asyncResp->res, logID);
2450 return;
2451 }
2452
2453 std::string crashdumpURI =
2454 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2455 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002456 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002457 {"@odata.id", "/redfish/v1/Systems/system/"
2458 "LogServices/Crashdump/Entries/" +
2459 logID},
2460 {"Name", "CPU Crashdump"},
2461 {"Id", logID},
2462 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002463 {"AdditionalDataURI", std::move(crashdumpURI)},
2464 {"DiagnosticDataType", "OEM"},
2465 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002466 {"Created", std::move(timestamp)}};
2467 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002468 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002469 std::move(getStoredLogCallback), crashdumpObject,
2470 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002471 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002472}
2473
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002474inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002475{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002476 // Note: Deviated from redfish privilege registry for GET & HEAD
2477 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002478 /**
2479 * Functions triggers appropriate requests on DBus
2480 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002481 BMCWEB_ROUTE(app,
2482 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002483 // This is incorrect, should be.
2484 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002485 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002486 .methods(
2487 boost::beast::http::verb::
2488 get)([](const crow::Request&,
2489 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2490 // Collections don't include the static data added by SubRoute
2491 // because it has a duplicate entry for members
2492 auto getLogEntriesCallback = [asyncResp](
2493 const boost::system::error_code ec,
2494 const std::vector<std::string>&
2495 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002496 if (ec)
2497 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002498 if (ec.value() !=
2499 boost::system::errc::no_such_file_or_directory)
2500 {
2501 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2502 << ec.message();
2503 messages::internalError(asyncResp->res);
2504 return;
2505 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002506 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002507 asyncResp->res.jsonValue["@odata.type"] =
2508 "#LogEntryCollection.LogEntryCollection";
2509 asyncResp->res.jsonValue["@odata.id"] =
2510 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2511 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2512 asyncResp->res.jsonValue["Description"] =
2513 "Collection of Crashdump Entries";
2514 nlohmann::json& logEntryArray =
2515 asyncResp->res.jsonValue["Members"];
2516 logEntryArray = nlohmann::json::array();
2517 std::vector<std::string> logIDs;
2518 // Get the list of log entries and build up an empty array big
2519 // enough to hold them
2520 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002521 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002522 // Get the log ID
2523 std::size_t lastPos = objpath.rfind('/');
2524 if (lastPos == std::string::npos)
2525 {
2526 continue;
2527 }
2528 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002529
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002530 // Add a space for the log entry to the array
2531 logEntryArray.push_back({});
2532 }
2533 // Now go through and set up async calls to fill in the entries
2534 size_t index = 0;
2535 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002536 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002537 // Add the log entry to the array
2538 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002539 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002540 asyncResp->res.jsonValue["Members@odata.count"] =
2541 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002542 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002543 crow::connections::systemBus->async_method_call(
2544 std::move(getLogEntriesCallback),
2545 "xyz.openbmc_project.ObjectMapper",
2546 "/xyz/openbmc_project/object_mapper",
2547 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2548 std::array<const char*, 1>{crashdumpInterface});
2549 });
2550}
Ed Tanous1da66f72018-07-27 16:13:37 -07002551
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002552inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002553{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002554 // Note: Deviated from redfish privilege registry for GET & HEAD
2555 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002556
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002557 BMCWEB_ROUTE(
2558 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002559 // this is incorrect, should be
2560 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002561 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002562 .methods(boost::beast::http::verb::get)(
2563 [](const crow::Request&,
2564 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2565 const std::string& param) {
2566 const std::string& logID = param;
2567 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2568 });
2569}
Ed Tanous1da66f72018-07-27 16:13:37 -07002570
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002571inline void requestRoutesCrashdumpFile(App& app)
2572{
2573 // Note: Deviated from redfish privilege registry for GET & HEAD
2574 // method for security reasons.
2575 BMCWEB_ROUTE(
2576 app,
2577 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002578 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002579 .methods(boost::beast::http::verb::get)(
2580 [](const crow::Request&,
2581 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2582 const std::string& logID, const std::string& fileName) {
2583 auto getStoredLogCallback =
2584 [asyncResp, logID, fileName](
2585 const boost::system::error_code ec,
2586 const std::vector<std::pair<std::string, VariantType>>&
2587 resp) {
2588 if (ec)
2589 {
2590 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2591 << ec.message();
2592 messages::internalError(asyncResp->res);
2593 return;
2594 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002595
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002596 std::string dbusFilename{};
2597 std::string dbusTimestamp{};
2598 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002599
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002600 parseCrashdumpParameters(resp, dbusFilename,
2601 dbusTimestamp, dbusFilepath);
2602
2603 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2604 dbusFilepath.empty())
2605 {
2606 messages::resourceMissingAtURI(asyncResp->res,
2607 fileName);
2608 return;
2609 }
2610
2611 // Verify the file name parameter is correct
2612 if (fileName != dbusFilename)
2613 {
2614 messages::resourceMissingAtURI(asyncResp->res,
2615 fileName);
2616 return;
2617 }
2618
2619 if (!std::filesystem::exists(dbusFilepath))
2620 {
2621 messages::resourceMissingAtURI(asyncResp->res,
2622 fileName);
2623 return;
2624 }
2625 std::ifstream ifs(dbusFilepath, std::ios::in |
2626 std::ios::binary |
2627 std::ios::ate);
2628 std::ifstream::pos_type fileSize = ifs.tellg();
2629 if (fileSize < 0)
2630 {
2631 messages::generalError(asyncResp->res);
2632 return;
2633 }
2634 ifs.seekg(0, std::ios::beg);
2635
2636 auto crashData = std::make_unique<char[]>(
2637 static_cast<unsigned int>(fileSize));
2638
2639 ifs.read(crashData.get(), static_cast<int>(fileSize));
2640
2641 // The cast to std::string is intentional in order to
2642 // use the assign() that applies move mechanics
2643 asyncResp->res.body().assign(
2644 static_cast<std::string>(crashData.get()));
2645
2646 // Configure this to be a file download when accessed
2647 // from a browser
2648 asyncResp->res.addHeader("Content-Disposition",
2649 "attachment");
2650 };
2651 crow::connections::systemBus->async_method_call(
2652 std::move(getStoredLogCallback), crashdumpObject,
2653 crashdumpPath + std::string("/") + logID,
2654 "org.freedesktop.DBus.Properties", "GetAll",
2655 crashdumpInterface);
2656 });
2657}
2658
2659inline void requestRoutesCrashdumpCollect(App& app)
2660{
2661 // Note: Deviated from redfish privilege registry for GET & HEAD
2662 // method for security reasons.
2663 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/"
2664 "Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002665 // The below is incorrect; Should be ConfigureManager
2666 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002667 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002668 .methods(
2669 boost::beast::http::verb::
2670 post)([](const crow::Request& req,
2671 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2672 std::string diagnosticDataType;
2673 std::string oemDiagnosticDataType;
2674 if (!redfish::json_util::readJson(
2675 req, asyncResp->res, "DiagnosticDataType",
2676 diagnosticDataType, "OEMDiagnosticDataType",
2677 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002678 {
James Feist46229572020-02-19 15:11:58 -08002679 return;
2680 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002681
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002682 if (diagnosticDataType != "OEM")
2683 {
2684 BMCWEB_LOG_ERROR
2685 << "Only OEM DiagnosticDataType supported for Crashdump";
2686 messages::actionParameterValueFormatError(
2687 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2688 "CollectDiagnosticData");
2689 return;
2690 }
2691
2692 auto collectCrashdumpCallback = [asyncResp, req](
2693 const boost::system::error_code
2694 ec,
2695 const std::string&) {
2696 if (ec)
2697 {
2698 if (ec.value() ==
2699 boost::system::errc::operation_not_supported)
2700 {
2701 messages::resourceInStandby(asyncResp->res);
2702 }
2703 else if (ec.value() ==
2704 boost::system::errc::device_or_resource_busy)
2705 {
2706 messages::serviceTemporarilyUnavailable(asyncResp->res,
2707 "60");
2708 }
2709 else
2710 {
2711 messages::internalError(asyncResp->res);
2712 }
2713 return;
2714 }
2715 std::shared_ptr<task::TaskData> task =
2716 task::TaskData::createTask(
2717 [](boost::system::error_code err,
2718 sdbusplus::message::message&,
2719 const std::shared_ptr<task::TaskData>& taskData) {
2720 if (!err)
2721 {
2722 taskData->messages.emplace_back(
2723 messages::taskCompletedOK(
2724 std::to_string(taskData->index)));
2725 taskData->state = "Completed";
2726 }
2727 return task::completed;
2728 },
2729 "type='signal',interface='org.freedesktop.DBus."
2730 "Properties',"
2731 "member='PropertiesChanged',arg0namespace='com.intel."
2732 "crashdump'");
2733 task->startTimer(std::chrono::minutes(5));
2734 task->populateResp(asyncResp->res);
2735 task->payload.emplace(req);
2736 };
2737
2738 if (oemDiagnosticDataType == "OnDemand")
2739 {
2740 crow::connections::systemBus->async_method_call(
2741 std::move(collectCrashdumpCallback), crashdumpObject,
2742 crashdumpPath, crashdumpOnDemandInterface,
2743 "GenerateOnDemandLog");
2744 }
2745 else if (oemDiagnosticDataType == "Telemetry")
2746 {
2747 crow::connections::systemBus->async_method_call(
2748 std::move(collectCrashdumpCallback), crashdumpObject,
2749 crashdumpPath, crashdumpTelemetryInterface,
2750 "GenerateTelemetryLog");
2751 }
2752 else
2753 {
2754 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2755 << oemDiagnosticDataType;
2756 messages::actionParameterValueFormatError(
2757 asyncResp->res, oemDiagnosticDataType,
2758 "OEMDiagnosticDataType", "CollectDiagnosticData");
2759 return;
2760 }
2761 });
2762}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002763
Andrew Geisslercb92c032018-08-17 07:56:14 -07002764/**
2765 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2766 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002767inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002768{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002769 /**
2770 * Function handles POST method request.
2771 * The Clear Log actions does not require any parameter.The action deletes
2772 * all entries found in the Entries collection for this Log Service.
2773 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002774
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002775 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
2776 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002777 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002778 .methods(boost::beast::http::verb::post)(
2779 [](const crow::Request&,
2780 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2781 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07002782
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002783 // Process response from Logging service.
2784 auto respHandler = [asyncResp](
2785 const boost::system::error_code ec) {
2786 BMCWEB_LOG_DEBUG
2787 << "doClearLog resp_handler callback: Done";
2788 if (ec)
2789 {
2790 // TODO Handle for specific error code
2791 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
2792 << ec;
2793 asyncResp->res.result(
2794 boost::beast::http::status::internal_server_error);
2795 return;
2796 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07002797
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002798 asyncResp->res.result(
2799 boost::beast::http::status::no_content);
2800 };
2801
2802 // Make call to Logging service to request Clear Log
2803 crow::connections::systemBus->async_method_call(
2804 respHandler, "xyz.openbmc_project.Logging",
2805 "/xyz/openbmc_project/logging",
2806 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2807 });
2808}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002809
2810/****************************************************
2811 * Redfish PostCode interfaces
2812 * using DBUS interface: getPostCodesTS
2813 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002814inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002815{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002816 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07002817 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002818 .methods(boost::beast::http::verb::get)(
2819 [](const crow::Request&,
2820 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2821 asyncResp->res.jsonValue = {
2822 {"@odata.id",
2823 "/redfish/v1/Systems/system/LogServices/PostCodes"},
2824 {"@odata.type", "#LogService.v1_1_0.LogService"},
2825 {"Name", "POST Code Log Service"},
2826 {"Description", "POST Code Log Service"},
2827 {"Id", "BIOS POST Code Log"},
2828 {"OverWritePolicy", "WrapsWhenFull"},
2829 {"Entries",
2830 {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2831 "PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05302832
2833 std::pair<std::string, std::string> redfishDateTimeOffset =
2834 crow::utility::getDateTimeOffsetNow();
2835 asyncResp->res.jsonValue["DateTime"] =
2836 redfishDateTimeOffset.first;
2837 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2838 redfishDateTimeOffset.second;
2839
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002840 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
2841 {"target",
2842 "/redfish/v1/Systems/system/LogServices/PostCodes/"
2843 "Actions/LogService.ClearLog"}};
2844 });
2845}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002846
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002847inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002848{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002849 BMCWEB_ROUTE(app,
2850 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
2851 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002852 // The following privilege is incorrect; It should be ConfigureManager
2853 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002854 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002855 .methods(boost::beast::http::verb::post)(
2856 [](const crow::Request&,
2857 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2858 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08002859
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002860 // Make call to post-code service to request clear all
2861 crow::connections::systemBus->async_method_call(
2862 [asyncResp](const boost::system::error_code ec) {
2863 if (ec)
2864 {
2865 // TODO Handle for specific error code
2866 BMCWEB_LOG_ERROR
2867 << "doClearPostCodes resp_handler got error "
2868 << ec;
2869 asyncResp->res.result(boost::beast::http::status::
2870 internal_server_error);
2871 messages::internalError(asyncResp->res);
2872 return;
2873 }
2874 },
2875 "xyz.openbmc_project.State.Boot.PostCode0",
2876 "/xyz/openbmc_project/State/Boot/PostCode0",
2877 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2878 });
2879}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002880
2881static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08002882 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302883 const boost::container::flat_map<
2884 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08002885 const uint16_t bootIndex, const uint64_t codeIndex = 0,
2886 const uint64_t skip = 0, const uint64_t top = 0)
2887{
2888 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002889 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05302890 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08002891
2892 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002893 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08002894
2895 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302896 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
2897 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002898 {
2899 currentCodeIndex++;
2900 std::string postcodeEntryID =
2901 "B" + std::to_string(bootIndex) + "-" +
2902 std::to_string(currentCodeIndex); // 1 based index in EntryID string
2903
2904 uint64_t usecSinceEpoch = code.first;
2905 uint64_t usTimeOffset = 0;
2906
2907 if (1 == currentCodeIndex)
2908 { // already incremented
2909 firstCodeTimeUs = code.first;
2910 }
2911 else
2912 {
2913 usTimeOffset = code.first - firstCodeTimeUs;
2914 }
2915
2916 // skip if no specific codeIndex is specified and currentCodeIndex does
2917 // not fall between top and skip
2918 if ((codeIndex == 0) &&
2919 (currentCodeIndex <= skip || currentCodeIndex > top))
2920 {
2921 continue;
2922 }
2923
Gunnar Mills4e0453b2020-07-08 14:00:30 -05002924 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08002925 // currentIndex
2926 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
2927 {
2928 // This is done for simplicity. 1st entry is needed to calculate
2929 // time offset. To improve efficiency, one can get to the entry
2930 // directly (possibly with flatmap's nth method)
2931 continue;
2932 }
2933
2934 // currentCodeIndex is within top and skip or equal to specified code
2935 // index
2936
2937 // Get the Created time from the timestamp
2938 std::string entryTimeStr;
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -05002939 entryTimeStr = crow::utility::getDateTime(
2940 static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
ZhikuiRena3316fc2020-01-29 14:58:08 -08002941
2942 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
2943 std::ostringstream hexCode;
2944 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302945 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08002946 std::ostringstream timeOffsetStr;
2947 // Set Fixed -Point Notation
2948 timeOffsetStr << std::fixed;
2949 // Set precision to 4 digits
2950 timeOffsetStr << std::setprecision(4);
2951 // Add double to stream
2952 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
2953 std::vector<std::string> messageArgs = {
2954 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
2955
2956 // Get MessageArgs template from message registry
2957 std::string msg;
2958 if (message != nullptr)
2959 {
2960 msg = message->message;
2961
2962 // fill in this post code value
2963 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002964 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002965 {
2966 std::string argStr = "%" + std::to_string(++i);
2967 size_t argPos = msg.find(argStr);
2968 if (argPos != std::string::npos)
2969 {
2970 msg.replace(argPos, argStr.length(), messageArg);
2971 }
2972 }
2973 }
2974
Tim Leed4342a92020-04-27 11:47:58 +08002975 // Get Severity template from message registry
2976 std::string severity;
2977 if (message != nullptr)
2978 {
2979 severity = message->severity;
2980 }
2981
ZhikuiRena3316fc2020-01-29 14:58:08 -08002982 // add to AsyncResp
2983 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002984 nlohmann::json& bmcLogEntry = logEntryArray.back();
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002985 bmcLogEntry = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Gunnar Mills743e9a12020-10-26 12:44:53 -05002986 {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2987 "PostCodes/Entries/" +
2988 postcodeEntryID},
2989 {"Name", "POST Code Log Entry"},
2990 {"Id", postcodeEntryID},
2991 {"Message", std::move(msg)},
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05302992 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
Gunnar Mills743e9a12020-10-26 12:44:53 -05002993 {"MessageArgs", std::move(messageArgs)},
2994 {"EntryType", "Event"},
2995 {"Severity", std::move(severity)},
2996 {"Created", entryTimeStr}};
ZhikuiRena3316fc2020-01-29 14:58:08 -08002997 }
2998}
2999
zhanghch058d1b46d2021-04-01 11:18:24 +08003000static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003001 const uint16_t bootIndex,
3002 const uint64_t codeIndex)
3003{
3004 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303005 [aResp, bootIndex,
3006 codeIndex](const boost::system::error_code ec,
3007 const boost::container::flat_map<
3008 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3009 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003010 if (ec)
3011 {
3012 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3013 messages::internalError(aResp->res);
3014 return;
3015 }
3016
3017 // skip the empty postcode boots
3018 if (postcode.empty())
3019 {
3020 return;
3021 }
3022
3023 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3024
3025 aResp->res.jsonValue["Members@odata.count"] =
3026 aResp->res.jsonValue["Members"].size();
3027 },
Jonathan Doman15124762021-01-07 17:54:17 -08003028 "xyz.openbmc_project.State.Boot.PostCode0",
3029 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003030 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3031 bootIndex);
3032}
3033
zhanghch058d1b46d2021-04-01 11:18:24 +08003034static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003035 const uint16_t bootIndex,
3036 const uint16_t bootCount,
3037 const uint64_t entryCount, const uint64_t skip,
3038 const uint64_t top)
3039{
3040 crow::connections::systemBus->async_method_call(
3041 [aResp, bootIndex, bootCount, entryCount, skip,
3042 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303043 const boost::container::flat_map<
3044 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3045 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003046 if (ec)
3047 {
3048 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3049 messages::internalError(aResp->res);
3050 return;
3051 }
3052
3053 uint64_t endCount = entryCount;
3054 if (!postcode.empty())
3055 {
3056 endCount = entryCount + postcode.size();
3057
3058 if ((skip < endCount) && ((top + skip) > entryCount))
3059 {
3060 uint64_t thisBootSkip =
3061 std::max(skip, entryCount) - entryCount;
3062 uint64_t thisBootTop =
3063 std::min(top + skip, endCount) - entryCount;
3064
3065 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3066 thisBootSkip, thisBootTop);
3067 }
3068 aResp->res.jsonValue["Members@odata.count"] = endCount;
3069 }
3070
3071 // continue to previous bootIndex
3072 if (bootIndex < bootCount)
3073 {
3074 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3075 bootCount, endCount, skip, top);
3076 }
3077 else
3078 {
3079 aResp->res.jsonValue["Members@odata.nextLink"] =
3080 "/redfish/v1/Systems/system/LogServices/PostCodes/"
3081 "Entries?$skip=" +
3082 std::to_string(skip + top);
3083 }
3084 },
Jonathan Doman15124762021-01-07 17:54:17 -08003085 "xyz.openbmc_project.State.Boot.PostCode0",
3086 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003087 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3088 bootIndex);
3089}
3090
zhanghch058d1b46d2021-04-01 11:18:24 +08003091static void
3092 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3093 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003094{
3095 uint64_t entryCount = 0;
3096 crow::connections::systemBus->async_method_call(
3097 [aResp, entryCount, skip,
3098 top](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003099 const std::variant<uint16_t>& bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003100 if (ec)
3101 {
3102 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3103 messages::internalError(aResp->res);
3104 return;
3105 }
3106 auto pVal = std::get_if<uint16_t>(&bootCount);
3107 if (pVal)
3108 {
3109 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3110 }
3111 else
3112 {
3113 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3114 }
3115 },
Jonathan Doman15124762021-01-07 17:54:17 -08003116 "xyz.openbmc_project.State.Boot.PostCode0",
3117 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003118 "org.freedesktop.DBus.Properties", "Get",
3119 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3120}
3121
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003122inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003123{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003124 BMCWEB_ROUTE(app,
3125 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003126 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003127 .methods(boost::beast::http::verb::get)(
3128 [](const crow::Request& req,
3129 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3130 asyncResp->res.jsonValue["@odata.type"] =
3131 "#LogEntryCollection.LogEntryCollection";
3132 asyncResp->res.jsonValue["@odata.id"] =
3133 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3134 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3135 asyncResp->res.jsonValue["Description"] =
3136 "Collection of POST Code Log Entries";
3137 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3138 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003139
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003140 uint64_t skip = 0;
3141 uint64_t top = maxEntriesPerPage; // Show max entries by default
3142 if (!getSkipParam(asyncResp, req, skip))
3143 {
3144 return;
3145 }
3146 if (!getTopParam(asyncResp, req, top))
3147 {
3148 return;
3149 }
3150 getCurrentBootNumber(asyncResp, skip, top);
3151 });
3152}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003153
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003154inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003155{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003156 BMCWEB_ROUTE(
3157 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003158 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003159 .methods(boost::beast::http::verb::get)(
3160 [](const crow::Request&,
3161 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3162 const std::string& targetID) {
3163 size_t bootPos = targetID.find('B');
3164 if (bootPos == std::string::npos)
3165 {
3166 // Requested ID was not found
3167 messages::resourceMissingAtURI(asyncResp->res, targetID);
3168 return;
3169 }
3170 std::string_view bootIndexStr(targetID);
3171 bootIndexStr.remove_prefix(bootPos + 1);
3172 uint16_t bootIndex = 0;
3173 uint64_t codeIndex = 0;
3174 size_t dashPos = bootIndexStr.find('-');
ZhikuiRena3316fc2020-01-29 14:58:08 -08003175
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003176 if (dashPos == std::string::npos)
3177 {
3178 return;
3179 }
3180 std::string_view codeIndexStr(bootIndexStr);
3181 bootIndexStr.remove_suffix(dashPos);
3182 codeIndexStr.remove_prefix(dashPos + 1);
zhanghch058d1b46d2021-04-01 11:18:24 +08003183
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003184 bootIndex = static_cast<uint16_t>(
3185 strtoul(std::string(bootIndexStr).c_str(), nullptr, 0));
3186 codeIndex =
3187 strtoul(std::string(codeIndexStr).c_str(), nullptr, 0);
3188 if (bootIndex == 0 || codeIndex == 0)
3189 {
3190 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3191 << targetID;
3192 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003193
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003194 asyncResp->res.jsonValue["@odata.type"] =
3195 "#LogEntry.v1_4_0.LogEntry";
3196 asyncResp->res.jsonValue["@odata.id"] =
3197 "/redfish/v1/Systems/system/LogServices/PostCodes/"
3198 "Entries";
3199 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3200 asyncResp->res.jsonValue["Description"] =
3201 "Collection of POST Code Log Entries";
3202 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3203 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003204
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003205 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3206 });
3207}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003208
Ed Tanous1da66f72018-07-27 16:13:37 -07003209} // namespace redfish