blob: dad63a523fda6dd81022197221d6f9d9641856b7 [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);
335 std::size_t pos;
336 try
337 {
Ed Tanous39e77502019-03-04 17:35:53 -0800338 index = std::stoul(std::string(indexStr), &pos);
Jason M. Bills16428a12018-11-02 12:42:29 -0700339 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500340 catch (std::invalid_argument&)
Jason M. Bills16428a12018-11-02 12:42:29 -0700341 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800342 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700343 return false;
344 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500345 catch (std::out_of_range&)
Jason M. Bills16428a12018-11-02 12:42:29 -0700346 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800347 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700348 return false;
349 }
350 if (pos != indexStr.size())
351 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800352 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700353 return false;
354 }
355 }
356 // Timestamp has no index
357 std::size_t pos;
358 try
359 {
Ed Tanous39e77502019-03-04 17:35:53 -0800360 timestamp = std::stoull(std::string(tsStr), &pos);
Jason M. Bills16428a12018-11-02 12:42:29 -0700361 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500362 catch (std::invalid_argument&)
Jason M. Bills16428a12018-11-02 12:42:29 -0700363 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800364 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700365 return false;
366 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500367 catch (std::out_of_range&)
Jason M. Bills16428a12018-11-02 12:42:29 -0700368 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800369 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700370 return false;
371 }
372 if (pos != tsStr.size())
373 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800374 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700375 return false;
376 }
377 return true;
378}
379
Jason M. Bills95820182019-04-22 16:25:34 -0700380static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500381 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700382{
383 static const std::filesystem::path redfishLogDir = "/var/log";
384 static const std::string redfishLogFilename = "redfish";
385
386 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500387 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700388 std::filesystem::directory_iterator(redfishLogDir))
389 {
390 // If we find a redfish log file, save the path
391 std::string filename = dirEnt.path().filename();
392 if (boost::starts_with(filename, redfishLogFilename))
393 {
394 redfishLogFiles.emplace_back(redfishLogDir / filename);
395 }
396 }
397 // As the log files rotate, they are appended with a ".#" that is higher for
398 // the older logs. Since we don't expect more than 10 log files, we
399 // can just sort the list to get them in order from newest to oldest
400 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
401
402 return !redfishLogFiles.empty();
403}
404
zhanghch058d1b46d2021-04-01 11:18:24 +0800405inline void
406 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
407 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500408{
409 std::string dumpPath;
410 if (dumpType == "BMC")
411 {
412 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
413 }
414 else if (dumpType == "System")
415 {
416 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
417 }
418 else
419 {
420 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
421 messages::internalError(asyncResp->res);
422 return;
423 }
424
425 crow::connections::systemBus->async_method_call(
426 [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
427 GetManagedObjectsType& resp) {
428 if (ec)
429 {
430 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
431 messages::internalError(asyncResp->res);
432 return;
433 }
434
435 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
436 entriesArray = nlohmann::json::array();
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500437 std::string dumpEntryPath =
438 "/xyz/openbmc_project/dump/" +
439 std::string(boost::algorithm::to_lower_copy(dumpType)) +
440 "/entry/";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500441
442 for (auto& object : resp)
443 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500444 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500445 {
446 continue;
447 }
448 std::time_t timestamp;
449 uint64_t size = 0;
450 entriesArray.push_back({});
451 nlohmann::json& thisEntry = entriesArray.back();
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000452
453 std::string entryID = object.first.filename();
454 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500455 {
456 continue;
457 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500458
459 for (auto& interfaceMap : object.second)
460 {
461 if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
462 {
463
464 for (auto& propertyMap : interfaceMap.second)
465 {
466 if (propertyMap.first == "Size")
467 {
468 auto sizePtr =
469 std::get_if<uint64_t>(&propertyMap.second);
470 if (sizePtr == nullptr)
471 {
472 messages::internalError(asyncResp->res);
473 break;
474 }
475 size = *sizePtr;
476 break;
477 }
478 }
479 }
480 else if (interfaceMap.first ==
481 "xyz.openbmc_project.Time.EpochTime")
482 {
483
484 for (auto& propertyMap : interfaceMap.second)
485 {
486 if (propertyMap.first == "Elapsed")
487 {
488 const uint64_t* usecsTimeStamp =
489 std::get_if<uint64_t>(&propertyMap.second);
490 if (usecsTimeStamp == nullptr)
491 {
492 messages::internalError(asyncResp->res);
493 break;
494 }
495 timestamp =
496 static_cast<std::time_t>(*usecsTimeStamp);
497 break;
498 }
499 }
500 }
501 }
502
Ed Tanousd0dbeef2021-07-01 08:46:46 -0700503 thisEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500504 thisEntry["@odata.id"] = dumpPath + entryID;
505 thisEntry["Id"] = entryID;
506 thisEntry["EntryType"] = "Event";
507 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
508 thisEntry["Name"] = dumpType + " Dump Entry";
509
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500510 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500511
512 if (dumpType == "BMC")
513 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500514 thisEntry["DiagnosticDataType"] = "Manager";
515 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500516 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
517 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500518 }
519 else if (dumpType == "System")
520 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500521 thisEntry["DiagnosticDataType"] = "OEM";
522 thisEntry["OEMDiagnosticDataType"] = "System";
523 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500524 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
525 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500526 }
527 }
528 asyncResp->res.jsonValue["Members@odata.count"] =
529 entriesArray.size();
530 },
531 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
532 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
533}
534
zhanghch058d1b46d2021-04-01 11:18:24 +0800535inline void
536 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
537 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500538{
539 std::string dumpPath;
540 if (dumpType == "BMC")
541 {
542 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
543 }
544 else if (dumpType == "System")
545 {
546 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
547 }
548 else
549 {
550 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
551 messages::internalError(asyncResp->res);
552 return;
553 }
554
555 crow::connections::systemBus->async_method_call(
556 [asyncResp, entryID, dumpPath, dumpType](
557 const boost::system::error_code ec, GetManagedObjectsType& resp) {
558 if (ec)
559 {
560 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
561 messages::internalError(asyncResp->res);
562 return;
563 }
564
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500565 bool foundDumpEntry = false;
566 std::string dumpEntryPath =
567 "/xyz/openbmc_project/dump/" +
568 std::string(boost::algorithm::to_lower_copy(dumpType)) +
569 "/entry/";
570
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500571 for (auto& objectPath : resp)
572 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500573 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500574 {
575 continue;
576 }
577
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500578 foundDumpEntry = true;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500579 std::time_t timestamp;
580 uint64_t size = 0;
581
582 for (auto& interfaceMap : objectPath.second)
583 {
584 if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
585 {
586 for (auto& propertyMap : interfaceMap.second)
587 {
588 if (propertyMap.first == "Size")
589 {
590 auto sizePtr =
591 std::get_if<uint64_t>(&propertyMap.second);
592 if (sizePtr == nullptr)
593 {
594 messages::internalError(asyncResp->res);
595 break;
596 }
597 size = *sizePtr;
598 break;
599 }
600 }
601 }
602 else if (interfaceMap.first ==
603 "xyz.openbmc_project.Time.EpochTime")
604 {
605 for (auto& propertyMap : interfaceMap.second)
606 {
607 if (propertyMap.first == "Elapsed")
608 {
609 const uint64_t* usecsTimeStamp =
610 std::get_if<uint64_t>(&propertyMap.second);
611 if (usecsTimeStamp == nullptr)
612 {
613 messages::internalError(asyncResp->res);
614 break;
615 }
616 timestamp =
617 static_cast<std::time_t>(*usecsTimeStamp);
618 break;
619 }
620 }
621 }
622 }
623
624 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousd0dbeef2021-07-01 08:46:46 -0700625 "#LogEntry.v1_7_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500626 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
627 asyncResp->res.jsonValue["Id"] = entryID;
628 asyncResp->res.jsonValue["EntryType"] = "Event";
629 asyncResp->res.jsonValue["Created"] =
630 crow::utility::getDateTime(timestamp);
631 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
632
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500633 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500634
635 if (dumpType == "BMC")
636 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500637 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
638 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500639 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
640 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500641 }
642 else if (dumpType == "System")
643 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500644 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
645 asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500646 "System";
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500647 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500648 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
649 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500650 }
651 }
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500652 if (foundDumpEntry == false)
653 {
654 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
655 messages::internalError(asyncResp->res);
656 return;
657 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500658 },
659 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
660 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
661}
662
zhanghch058d1b46d2021-04-01 11:18:24 +0800663inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800664 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500665 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500666{
George Liu3de8d8b2021-03-22 17:49:39 +0800667 auto respHandler = [asyncResp,
668 entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500669 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
670 if (ec)
671 {
George Liu3de8d8b2021-03-22 17:49:39 +0800672 if (ec.value() == EBADR)
673 {
674 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
675 return;
676 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500677 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
678 << ec;
679 messages::internalError(asyncResp->res);
680 return;
681 }
682 };
683 crow::connections::systemBus->async_method_call(
684 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500685 "/xyz/openbmc_project/dump/" +
686 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
687 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500688 "xyz.openbmc_project.Object.Delete", "Delete");
689}
690
zhanghch058d1b46d2021-04-01 11:18:24 +0800691inline void
692 createDumpTaskCallback(const crow::Request& req,
693 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
694 const uint32_t& dumpId, const std::string& dumpPath,
695 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500696{
697 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500698 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500699 boost::system::error_code err, sdbusplus::message::message& m,
700 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000701 if (err)
702 {
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500703 BMCWEB_LOG_ERROR << "Error in creating a dump";
704 taskData->state = "Cancelled";
705 return task::completed;
Ed Tanouscb13a392020-07-25 19:02:03 +0000706 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500707 std::vector<std::pair<
708 std::string,
709 std::vector<std::pair<std::string, std::variant<std::string>>>>>
710 interfacesList;
711
712 sdbusplus::message::object_path objPath;
713
714 m.read(objPath, interfacesList);
715
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500716 if (objPath.str ==
717 "/xyz/openbmc_project/dump/" +
718 std::string(boost::algorithm::to_lower_copy(dumpType)) +
719 "/entry/" + std::to_string(dumpId))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500720 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500721 nlohmann::json retMessage = messages::success();
722 taskData->messages.emplace_back(retMessage);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500723
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500724 std::string headerLoc =
725 "Location: " + dumpPath + std::to_string(dumpId);
726 taskData->payload->httpHeaders.emplace_back(
727 std::move(headerLoc));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500728
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500729 taskData->state = "Completed";
730 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500731 }
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500732 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500733 },
734 "type='signal',interface='org.freedesktop.DBus."
735 "ObjectManager',"
736 "member='InterfacesAdded', "
737 "path='/xyz/openbmc_project/dump'");
738
739 task->startTimer(std::chrono::minutes(3));
740 task->populateResp(asyncResp->res);
741 task->payload.emplace(req);
742}
743
zhanghch058d1b46d2021-04-01 11:18:24 +0800744inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
745 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500746{
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500747
748 std::string dumpPath;
749 if (dumpType == "BMC")
750 {
751 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
752 }
753 else if (dumpType == "System")
754 {
755 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
756 }
757 else
758 {
759 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
760 messages::internalError(asyncResp->res);
761 return;
762 }
763
764 std::optional<std::string> diagnosticDataType;
765 std::optional<std::string> oemDiagnosticDataType;
766
767 if (!redfish::json_util::readJson(
768 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
769 "OEMDiagnosticDataType", oemDiagnosticDataType))
770 {
771 return;
772 }
773
774 if (dumpType == "System")
775 {
776 if (!oemDiagnosticDataType || !diagnosticDataType)
777 {
778 BMCWEB_LOG_ERROR << "CreateDump action parameter "
779 "'DiagnosticDataType'/"
780 "'OEMDiagnosticDataType' value not found!";
781 messages::actionParameterMissing(
782 asyncResp->res, "CollectDiagnosticData",
783 "DiagnosticDataType & OEMDiagnosticDataType");
784 return;
785 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700786 if ((*oemDiagnosticDataType != "System") ||
787 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500788 {
789 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
790 messages::invalidObject(asyncResp->res,
791 "System Dump creation parameters");
792 return;
793 }
794 }
795 else if (dumpType == "BMC")
796 {
797 if (!diagnosticDataType)
798 {
799 BMCWEB_LOG_ERROR << "CreateDump action parameter "
800 "'DiagnosticDataType' not found!";
801 messages::actionParameterMissing(
802 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
803 return;
804 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700805 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500806 {
807 BMCWEB_LOG_ERROR
808 << "Wrong parameter value passed for 'DiagnosticDataType'";
809 messages::invalidObject(asyncResp->res,
810 "BMC Dump creation parameters");
811 return;
812 }
813 }
814
815 crow::connections::systemBus->async_method_call(
816 [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
817 const uint32_t& dumpId) {
818 if (ec)
819 {
820 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
821 messages::internalError(asyncResp->res);
822 return;
823 }
824 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
825
826 createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
827 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500828 "xyz.openbmc_project.Dump.Manager",
829 "/xyz/openbmc_project/dump/" +
830 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500831 "xyz.openbmc_project.Dump.Create", "CreateDump");
832}
833
zhanghch058d1b46d2021-04-01 11:18:24 +0800834inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
835 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500836{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500837 std::string dumpTypeLowerCopy =
838 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800839
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500840 crow::connections::systemBus->async_method_call(
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500841 [asyncResp, dumpType](const boost::system::error_code ec,
842 const std::vector<std::string>& subTreePaths) {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500843 if (ec)
844 {
845 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
846 messages::internalError(asyncResp->res);
847 return;
848 }
849
850 for (const std::string& path : subTreePaths)
851 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000852 sdbusplus::message::object_path objPath(path);
853 std::string logID = objPath.filename();
854 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500855 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000856 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500857 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000858 deleteDumpEntry(asyncResp, logID, dumpType);
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500859 }
860 },
861 "xyz.openbmc_project.ObjectMapper",
862 "/xyz/openbmc_project/object_mapper",
863 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500864 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
865 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
866 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500867}
868
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700869inline static void parseCrashdumpParameters(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500870 const std::vector<std::pair<std::string, VariantType>>& params,
871 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700872{
873 for (auto property : params)
874 {
875 if (property.first == "Timestamp")
876 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500877 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500878 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700879 if (value != nullptr)
880 {
881 timestamp = *value;
882 }
883 }
884 else if (property.first == "Filename")
885 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500886 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500887 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700888 if (value != nullptr)
889 {
890 filename = *value;
891 }
892 }
893 else if (property.first == "Log")
894 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500895 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500896 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700897 if (value != nullptr)
898 {
899 logfile = *value;
900 }
901 }
902 }
903}
904
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500905constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700906inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700907{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800908 /**
909 * Functions triggers appropriate requests on DBus
910 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700911 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700912 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700913 .methods(boost::beast::http::verb::get)(
914 [](const crow::Request&,
915 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
916
917 {
918 // Collections don't include the static data added by SubRoute
919 // because it has a duplicate entry for members
920 asyncResp->res.jsonValue["@odata.type"] =
921 "#LogServiceCollection.LogServiceCollection";
922 asyncResp->res.jsonValue["@odata.id"] =
923 "/redfish/v1/Systems/system/LogServices";
924 asyncResp->res.jsonValue["Name"] =
925 "System Log Services Collection";
926 asyncResp->res.jsonValue["Description"] =
927 "Collection of LogServices for this Computer System";
928 nlohmann::json& logServiceArray =
929 asyncResp->res.jsonValue["Members"];
930 logServiceArray = nlohmann::json::array();
931 logServiceArray.push_back(
932 {{"@odata.id",
933 "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500934#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700935 logServiceArray.push_back(
936 {{"@odata.id",
937 "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600938#endif
939
Jason M. Billsd53dd412019-02-12 17:16:22 -0800940#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700941 logServiceArray.push_back(
942 {{"@odata.id",
943 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800944#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700945 asyncResp->res.jsonValue["Members@odata.count"] =
946 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800947
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700948 crow::connections::systemBus->async_method_call(
949 [asyncResp](const boost::system::error_code ec,
950 const std::vector<std::string>& subtreePath) {
951 if (ec)
952 {
953 BMCWEB_LOG_ERROR << ec;
954 return;
955 }
ZhikuiRena3316fc2020-01-29 14:58:08 -0800956
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700957 for (auto& pathStr : subtreePath)
958 {
959 if (pathStr.find("PostCode") != std::string::npos)
960 {
961 nlohmann::json& logServiceArrayLocal =
962 asyncResp->res.jsonValue["Members"];
963 logServiceArrayLocal.push_back(
964 {{"@odata.id", "/redfish/v1/Systems/system/"
965 "LogServices/PostCodes"}});
966 asyncResp->res
967 .jsonValue["Members@odata.count"] =
968 logServiceArrayLocal.size();
969 return;
970 }
971 }
972 },
973 "xyz.openbmc_project.ObjectMapper",
974 "/xyz/openbmc_project/object_mapper",
975 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
976 0, std::array<const char*, 1>{postCodeIface});
977 });
978}
979
980inline void requestRoutesEventLogService(App& app)
981{
982 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -0700983 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700984 .methods(
985 boost::beast::http::verb::
986 get)([](const crow::Request&,
987 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
988 asyncResp->res.jsonValue["@odata.id"] =
989 "/redfish/v1/Systems/system/LogServices/EventLog";
990 asyncResp->res.jsonValue["@odata.type"] =
991 "#LogService.v1_1_0.LogService";
992 asyncResp->res.jsonValue["Name"] = "Event Log Service";
993 asyncResp->res.jsonValue["Description"] =
994 "System Event Log Service";
995 asyncResp->res.jsonValue["Id"] = "EventLog";
996 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +0530997
998 std::pair<std::string, std::string> redfishDateTimeOffset =
999 crow::utility::getDateTimeOffsetNow();
1000
1001 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1002 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1003 redfishDateTimeOffset.second;
1004
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001005 asyncResp->res.jsonValue["Entries"] = {
1006 {"@odata.id",
1007 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1008 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1009
1010 {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
1011 "Actions/LogService.ClearLog"}};
1012 });
1013}
1014
1015inline void requestRoutesJournalEventLogClear(App& app)
1016{
1017 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1018 "LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001019 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001020 .methods(boost::beast::http::verb::post)(
1021 [](const crow::Request&,
1022 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1023 // Clear the EventLog by deleting the log files
1024 std::vector<std::filesystem::path> redfishLogFiles;
1025 if (getRedfishLogFiles(redfishLogFiles))
ZhikuiRena3316fc2020-01-29 14:58:08 -08001026 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001027 for (const std::filesystem::path& file : redfishLogFiles)
ZhikuiRena3316fc2020-01-29 14:58:08 -08001028 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001029 std::error_code ec;
1030 std::filesystem::remove(file, ec);
ZhikuiRena3316fc2020-01-29 14:58:08 -08001031 }
1032 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001033
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001034 // Reload rsyslog so it knows to start new log files
1035 crow::connections::systemBus->async_method_call(
1036 [asyncResp](const boost::system::error_code ec) {
1037 if (ec)
1038 {
1039 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1040 << ec;
1041 messages::internalError(asyncResp->res);
1042 return;
1043 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001044
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001045 messages::success(asyncResp->res);
1046 },
1047 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1048 "org.freedesktop.systemd1.Manager", "ReloadUnit",
1049 "rsyslog.service", "replace");
1050 });
1051}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001052
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001053static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001054 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001055 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001056{
Jason M. Bills95820182019-04-22 16:25:34 -07001057 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001058 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001059 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001060 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001061 {
1062 return 1;
1063 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001064 std::string timestamp = logEntry.substr(0, space);
1065 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001066 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001067 if (entryStart == std::string::npos)
1068 {
1069 return 1;
1070 }
1071 std::string_view entry(logEntry);
1072 entry.remove_prefix(entryStart);
1073 // Use split to separate the entry into its fields
1074 std::vector<std::string> logEntryFields;
1075 boost::split(logEntryFields, entry, boost::is_any_of(","),
1076 boost::token_compress_on);
1077 // We need at least a MessageId to be valid
1078 if (logEntryFields.size() < 1)
1079 {
1080 return 1;
1081 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001082 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001083
Jason M. Bills4851d452019-03-28 11:27:48 -07001084 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001085 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001086 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001087
Jason M. Bills4851d452019-03-28 11:27:48 -07001088 std::string msg;
1089 std::string severity;
1090 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001091 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001092 msg = message->message;
1093 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001094 }
1095
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001096 // Get the MessageArgs from the log if there are any
1097 boost::beast::span<std::string> messageArgs;
1098 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001099 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001100 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001101 // If the first string is empty, assume there are no MessageArgs
1102 std::size_t messageArgsSize = 0;
1103 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001104 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001105 messageArgsSize = logEntryFields.size() - 1;
1106 }
1107
Ed Tanous23a21a12020-07-25 04:45:05 +00001108 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001109
1110 // Fill the MessageArgs into the Message
1111 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001112 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001113 {
1114 std::string argStr = "%" + std::to_string(++i);
1115 size_t argPos = msg.find(argStr);
1116 if (argPos != std::string::npos)
1117 {
1118 msg.replace(argPos, argStr.length(), messageArg);
1119 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001120 }
1121 }
1122
Jason M. Bills95820182019-04-22 16:25:34 -07001123 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1124 // format which matches the Redfish format except for the fractional seconds
1125 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001126 std::size_t dot = timestamp.find_first_of('.');
1127 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001128 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001129 {
Jason M. Bills95820182019-04-22 16:25:34 -07001130 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001131 }
1132
1133 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001134 logEntryJson = {
Ed Tanousd0dbeef2021-07-01 08:46:46 -07001135 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001136 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001137 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001138 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001139 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001140 {"Id", logEntryID},
1141 {"Message", std::move(msg)},
1142 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001143 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001144 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001145 {"Severity", std::move(severity)},
1146 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001147 return 0;
1148}
1149
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001150inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001151{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001152 BMCWEB_ROUTE(app,
1153 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001154 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001155 .methods(boost::beast::http::verb::get)(
1156 [](const crow::Request& req,
1157 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1158 uint64_t skip = 0;
1159 uint64_t top = maxEntriesPerPage; // Show max entries by default
1160 if (!getSkipParam(asyncResp, req, skip))
Jason M. Bills95820182019-04-22 16:25:34 -07001161 {
Jason M. Bills95820182019-04-22 16:25:34 -07001162 return;
1163 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001164 if (!getTopParam(asyncResp, req, top))
Jason M. Bills897967d2019-07-29 17:05:30 -07001165 {
Jason M. Bills897967d2019-07-29 17:05:30 -07001166 return;
1167 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001168 // Collections don't include the static data added by SubRoute
1169 // because it has a duplicate entry for members
1170 asyncResp->res.jsonValue["@odata.type"] =
1171 "#LogEntryCollection.LogEntryCollection";
1172 asyncResp->res.jsonValue["@odata.id"] =
1173 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1174 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1175 asyncResp->res.jsonValue["Description"] =
1176 "Collection of System Event Log Entries";
Jason M. Bills897967d2019-07-29 17:05:30 -07001177
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001178 nlohmann::json& logEntryArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001179 asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001180 logEntryArray = nlohmann::json::array();
1181 // Go through the log files and create a unique ID for each
1182 // entry
1183 std::vector<std::filesystem::path> redfishLogFiles;
1184 getRedfishLogFiles(redfishLogFiles);
1185 uint64_t entryCount = 0;
1186 std::string logEntry;
1187
1188 // Oldest logs are in the last file, so start there and loop
1189 // backwards
1190 for (auto it = redfishLogFiles.rbegin();
1191 it < redfishLogFiles.rend(); it++)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001192 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001193 std::ifstream logStream(*it);
1194 if (!logStream.is_open())
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001195 {
1196 continue;
1197 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001198
1199 // Reset the unique ID on the first entry
1200 bool firstEntry = true;
1201 while (std::getline(logStream, logEntry))
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001202 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001203 entryCount++;
1204 // Handle paging using skip (number of entries to skip
1205 // from the start) and top (number of entries to
1206 // display)
1207 if (entryCount <= skip || entryCount > skip + top)
George Liuebd45902020-08-26 14:21:10 +08001208 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001209 continue;
George Liuebd45902020-08-26 14:21:10 +08001210 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001211
1212 std::string idStr;
1213 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
George Liuebd45902020-08-26 14:21:10 +08001214 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001215 continue;
George Liuebd45902020-08-26 14:21:10 +08001216 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001217
1218 if (firstEntry)
1219 {
1220 firstEntry = false;
1221 }
1222
1223 logEntryArray.push_back({});
1224 nlohmann::json& bmcLogEntry = logEntryArray.back();
1225 if (fillEventLogEntryJson(idStr, logEntry,
1226 bmcLogEntry) != 0)
Xiaochao Ma75710de2021-01-21 17:56:02 +08001227 {
1228 messages::internalError(asyncResp->res);
1229 return;
1230 }
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001231 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001232 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001233 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1234 if (skip + top < entryCount)
Ed Tanous271584a2019-07-09 16:24:22 -07001235 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001236 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001237 "/redfish/v1/Systems/system/LogServices/EventLog/"
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001238 "Entries?$skip=" +
1239 std::to_string(skip + top);
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001240 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001241 });
1242}
Chicago Duan336e96c2019-07-15 14:22:08 +08001243
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001244inline void requestRoutesJournalEventLogEntry(App& app)
1245{
1246 BMCWEB_ROUTE(
1247 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001248 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001249 .methods(boost::beast::http::verb::get)(
1250 [](const crow::Request&,
1251 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1252 const std::string& param) {
1253 const std::string& targetID = param;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001254
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001255 // Go through the log files and check the unique ID for each
1256 // entry to find the target entry
1257 std::vector<std::filesystem::path> redfishLogFiles;
1258 getRedfishLogFiles(redfishLogFiles);
1259 std::string logEntry;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001260
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001261 // Oldest logs are in the last file, so start there and loop
1262 // backwards
1263 for (auto it = redfishLogFiles.rbegin();
1264 it < redfishLogFiles.rend(); it++)
1265 {
1266 std::ifstream logStream(*it);
1267 if (!logStream.is_open())
1268 {
1269 continue;
1270 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001271
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001272 // Reset the unique ID on the first entry
1273 bool firstEntry = true;
1274 while (std::getline(logStream, logEntry))
1275 {
1276 std::string idStr;
1277 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1278 {
1279 continue;
1280 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001281
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001282 if (firstEntry)
1283 {
1284 firstEntry = false;
1285 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001286
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001287 if (idStr == targetID)
1288 {
1289 if (fillEventLogEntryJson(
1290 idStr, logEntry,
1291 asyncResp->res.jsonValue) != 0)
1292 {
1293 messages::internalError(asyncResp->res);
1294 return;
1295 }
1296 return;
1297 }
1298 }
1299 }
1300 // Requested ID was not found
1301 messages::resourceMissingAtURI(asyncResp->res, targetID);
1302 });
1303}
1304
1305inline void requestRoutesDBusEventLogEntryCollection(App& app)
1306{
1307 BMCWEB_ROUTE(app,
1308 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001309 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001310 .methods(
1311 boost::beast::http::verb::
1312 get)([](const crow::Request&,
1313 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1314 // Collections don't include the static data added by SubRoute
1315 // because it has a duplicate entry for members
1316 asyncResp->res.jsonValue["@odata.type"] =
1317 "#LogEntryCollection.LogEntryCollection";
1318 asyncResp->res.jsonValue["@odata.id"] =
1319 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1320 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1321 asyncResp->res.jsonValue["Description"] =
1322 "Collection of System Event Log Entries";
1323
1324 // DBus implementation of EventLog/Entries
1325 // Make call to Logging Service to find all log entry objects
Xiaochao Ma75710de2021-01-21 17:56:02 +08001326 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001327 [asyncResp](const boost::system::error_code ec,
1328 GetManagedObjectsType& resp) {
Xiaochao Ma75710de2021-01-21 17:56:02 +08001329 if (ec)
1330 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001331 // TODO Handle for specific error code
1332 BMCWEB_LOG_ERROR
1333 << "getLogEntriesIfaceData resp_handler got error "
1334 << ec;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001335 messages::internalError(asyncResp->res);
1336 return;
1337 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001338 nlohmann::json& entriesArray =
1339 asyncResp->res.jsonValue["Members"];
1340 entriesArray = nlohmann::json::array();
1341 for (auto& objectPath : resp)
1342 {
1343 uint32_t* id = nullptr;
1344 std::time_t timestamp{};
1345 std::time_t updateTimestamp{};
1346 std::string* severity = nullptr;
1347 std::string* message = nullptr;
1348 std::string* filePath = nullptr;
1349 bool resolved = false;
1350 for (auto& interfaceMap : objectPath.second)
1351 {
1352 if (interfaceMap.first ==
1353 "xyz.openbmc_project.Logging.Entry")
1354 {
1355 for (auto& propertyMap : interfaceMap.second)
1356 {
1357 if (propertyMap.first == "Id")
1358 {
1359 id = std::get_if<uint32_t>(
1360 &propertyMap.second);
1361 }
1362 else if (propertyMap.first == "Timestamp")
1363 {
1364 const uint64_t* millisTimeStamp =
1365 std::get_if<uint64_t>(
1366 &propertyMap.second);
1367 if (millisTimeStamp != nullptr)
1368 {
1369 timestamp =
1370 crow::utility::getTimestamp(
1371 *millisTimeStamp);
1372 }
1373 }
1374 else if (propertyMap.first ==
1375 "UpdateTimestamp")
1376 {
1377 const uint64_t* millisTimeStamp =
1378 std::get_if<uint64_t>(
1379 &propertyMap.second);
1380 if (millisTimeStamp != nullptr)
1381 {
1382 updateTimestamp =
1383 crow::utility::getTimestamp(
1384 *millisTimeStamp);
1385 }
1386 }
1387 else if (propertyMap.first == "Severity")
1388 {
1389 severity = std::get_if<std::string>(
1390 &propertyMap.second);
1391 }
1392 else if (propertyMap.first == "Message")
1393 {
1394 message = std::get_if<std::string>(
1395 &propertyMap.second);
1396 }
1397 else if (propertyMap.first == "Resolved")
1398 {
1399 bool* resolveptr = std::get_if<bool>(
1400 &propertyMap.second);
1401 if (resolveptr == nullptr)
1402 {
1403 messages::internalError(
1404 asyncResp->res);
1405 return;
1406 }
1407 resolved = *resolveptr;
1408 }
1409 }
1410 if (id == nullptr || message == nullptr ||
1411 severity == nullptr)
1412 {
1413 messages::internalError(asyncResp->res);
1414 return;
1415 }
1416 }
1417 else if (interfaceMap.first ==
1418 "xyz.openbmc_project.Common.FilePath")
1419 {
1420 for (auto& propertyMap : interfaceMap.second)
1421 {
1422 if (propertyMap.first == "Path")
1423 {
1424 filePath = std::get_if<std::string>(
1425 &propertyMap.second);
1426 }
1427 }
1428 }
1429 }
1430 // Object path without the
1431 // xyz.openbmc_project.Logging.Entry interface, ignore
1432 // and continue.
1433 if (id == nullptr || message == nullptr ||
1434 severity == nullptr)
1435 {
1436 continue;
1437 }
1438 entriesArray.push_back({});
1439 nlohmann::json& thisEntry = entriesArray.back();
1440 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1441 thisEntry["@odata.id"] =
1442 "/redfish/v1/Systems/system/"
1443 "LogServices/EventLog/Entries/" +
1444 std::to_string(*id);
1445 thisEntry["Name"] = "System Event Log Entry";
1446 thisEntry["Id"] = std::to_string(*id);
1447 thisEntry["Message"] = *message;
1448 thisEntry["Resolved"] = resolved;
1449 thisEntry["EntryType"] = "Event";
1450 thisEntry["Severity"] =
1451 translateSeverityDbusToRedfish(*severity);
1452 thisEntry["Created"] =
1453 crow::utility::getDateTime(timestamp);
1454 thisEntry["Modified"] =
1455 crow::utility::getDateTime(updateTimestamp);
1456 if (filePath != nullptr)
1457 {
1458 thisEntry["AdditionalDataURI"] =
1459 "/redfish/v1/Systems/system/LogServices/"
1460 "EventLog/"
1461 "Entries/" +
1462 std::to_string(*id) + "/attachment";
1463 }
1464 }
1465 std::sort(entriesArray.begin(), entriesArray.end(),
1466 [](const nlohmann::json& left,
1467 const nlohmann::json& right) {
1468 return (left["Id"] <= right["Id"]);
1469 });
1470 asyncResp->res.jsonValue["Members@odata.count"] =
1471 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001472 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001473 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1474 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1475 });
1476}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001477
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001478inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001479{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001480 BMCWEB_ROUTE(
1481 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001482 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001483 .methods(boost::beast::http::verb::get)(
1484 [](const crow::Request&,
1485 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1486 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001487
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001488 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001489 std::string entryID = param;
1490 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001491
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001492 // DBus implementation of EventLog/Entries
1493 // Make call to Logging Service to find all log entry objects
1494 crow::connections::systemBus->async_method_call(
1495 [asyncResp, entryID](const boost::system::error_code ec,
1496 GetManagedPropertyType& resp) {
1497 if (ec.value() == EBADR)
1498 {
1499 messages::resourceNotFound(
1500 asyncResp->res, "EventLogEntry", entryID);
1501 return;
1502 }
1503 if (ec)
1504 {
1505 BMCWEB_LOG_ERROR << "EventLogEntry (DBus) "
1506 "resp_handler got error "
1507 << ec;
1508 messages::internalError(asyncResp->res);
1509 return;
1510 }
1511 uint32_t* id = nullptr;
1512 std::time_t timestamp{};
1513 std::time_t updateTimestamp{};
1514 std::string* severity = nullptr;
1515 std::string* message = nullptr;
1516 std::string* filePath = nullptr;
1517 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001518
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001519 for (auto& propertyMap : resp)
1520 {
1521 if (propertyMap.first == "Id")
1522 {
1523 id = std::get_if<uint32_t>(&propertyMap.second);
1524 }
1525 else if (propertyMap.first == "Timestamp")
1526 {
1527 const uint64_t* millisTimeStamp =
1528 std::get_if<uint64_t>(&propertyMap.second);
1529 if (millisTimeStamp != nullptr)
1530 {
1531 timestamp = crow::utility::getTimestamp(
1532 *millisTimeStamp);
1533 }
1534 }
1535 else if (propertyMap.first == "UpdateTimestamp")
1536 {
1537 const uint64_t* millisTimeStamp =
1538 std::get_if<uint64_t>(&propertyMap.second);
1539 if (millisTimeStamp != nullptr)
1540 {
1541 updateTimestamp =
1542 crow::utility::getTimestamp(
1543 *millisTimeStamp);
1544 }
1545 }
1546 else if (propertyMap.first == "Severity")
1547 {
1548 severity = std::get_if<std::string>(
1549 &propertyMap.second);
1550 }
1551 else if (propertyMap.first == "Message")
1552 {
1553 message = std::get_if<std::string>(
1554 &propertyMap.second);
1555 }
1556 else if (propertyMap.first == "Resolved")
1557 {
1558 bool* resolveptr =
1559 std::get_if<bool>(&propertyMap.second);
1560 if (resolveptr == nullptr)
1561 {
1562 messages::internalError(asyncResp->res);
1563 return;
1564 }
1565 resolved = *resolveptr;
1566 }
1567 else if (propertyMap.first == "Path")
1568 {
1569 filePath = std::get_if<std::string>(
1570 &propertyMap.second);
1571 }
1572 }
1573 if (id == nullptr || message == nullptr ||
1574 severity == nullptr)
1575 {
1576 messages::internalError(asyncResp->res);
1577 return;
1578 }
1579 asyncResp->res.jsonValue["@odata.type"] =
1580 "#LogEntry.v1_8_0.LogEntry";
1581 asyncResp->res.jsonValue["@odata.id"] =
1582 "/redfish/v1/Systems/system/LogServices/EventLog/"
1583 "Entries/" +
1584 std::to_string(*id);
1585 asyncResp->res.jsonValue["Name"] =
1586 "System Event Log Entry";
1587 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1588 asyncResp->res.jsonValue["Message"] = *message;
1589 asyncResp->res.jsonValue["Resolved"] = resolved;
1590 asyncResp->res.jsonValue["EntryType"] = "Event";
1591 asyncResp->res.jsonValue["Severity"] =
1592 translateSeverityDbusToRedfish(*severity);
1593 asyncResp->res.jsonValue["Created"] =
1594 crow::utility::getDateTime(timestamp);
1595 asyncResp->res.jsonValue["Modified"] =
1596 crow::utility::getDateTime(updateTimestamp);
1597 if (filePath != nullptr)
1598 {
1599 asyncResp->res.jsonValue["AdditionalDataURI"] =
1600 "/redfish/v1/Systems/system/LogServices/"
1601 "EventLog/"
1602 "attachment/" +
1603 std::to_string(*id);
1604 }
1605 },
1606 "xyz.openbmc_project.Logging",
1607 "/xyz/openbmc_project/logging/entry/" + entryID,
1608 "org.freedesktop.DBus.Properties", "GetAll", "");
1609 });
1610
1611 BMCWEB_ROUTE(
1612 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001613 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001614 .methods(boost::beast::http::verb::patch)(
1615 [](const crow::Request& req,
1616 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1617 const std::string& entryId) {
1618 std::optional<bool> resolved;
1619
1620 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1621 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001622 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001623 return;
1624 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001625 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001626
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001627 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001628 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001629 if (ec)
1630 {
1631 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1632 messages::internalError(asyncResp->res);
1633 return;
1634 }
1635 },
1636 "xyz.openbmc_project.Logging",
1637 "/xyz/openbmc_project/logging/entry/" + entryId,
1638 "org.freedesktop.DBus.Properties", "Set",
1639 "xyz.openbmc_project.Logging.Entry", "Resolved",
1640 std::variant<bool>(*resolved));
1641 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001642
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001643 BMCWEB_ROUTE(
1644 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001645 .privileges(redfish::privileges::deleteLogEntry)
1646
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001647 .methods(boost::beast::http::verb::delete_)(
1648 [](const crow::Request&,
1649 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1650 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001651
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001652 {
1653 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001654
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001655 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001656
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001657 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001658
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001659 // Process response from Logging service.
1660 auto respHandler = [asyncResp, entryID](
1661 const boost::system::error_code ec) {
1662 BMCWEB_LOG_DEBUG
1663 << "EventLogEntry (DBus) doDelete callback: Done";
1664 if (ec)
1665 {
1666 if (ec.value() == EBADR)
1667 {
1668 messages::resourceNotFound(asyncResp->res,
1669 "LogEntry", entryID);
1670 return;
1671 }
1672 // TODO Handle for specific error code
1673 BMCWEB_LOG_ERROR << "EventLogEntry (DBus) doDelete "
1674 "respHandler got error "
1675 << ec;
1676 asyncResp->res.result(
1677 boost::beast::http::status::internal_server_error);
1678 return;
1679 }
1680
1681 asyncResp->res.result(boost::beast::http::status::ok);
1682 };
1683
1684 // Make call to Logging service to request Delete Log
1685 crow::connections::systemBus->async_method_call(
1686 respHandler, "xyz.openbmc_project.Logging",
1687 "/xyz/openbmc_project/logging/entry/" + entryID,
1688 "xyz.openbmc_project.Object.Delete", "Delete");
1689 });
1690}
1691
1692inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001693{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001694 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/"
1695 "<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001696 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001697 .methods(boost::beast::http::verb::get)(
1698 [](const crow::Request& req,
1699 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1700 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001701
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001702 {
Ed Tanous753d0342021-07-01 07:57:30 -07001703 std::string_view acceptHeader = req.getHeaderValue("Accept");
1704 // The iterators in boost/http/rfc7230.hpp end the string if '/'
1705 // is found, so replace it with arbitrary character '|' which is
1706 // not part of the Accept header syntax.
1707 std::string acceptStr = boost::replace_all_copy(
1708 std::string(acceptHeader), "/", "|");
1709 boost::beast::http::ext_list acceptTypes{acceptStr};
1710 bool supported = false;
1711 for (const auto& type : acceptTypes)
1712 {
1713 if ((type.first == "*|*") ||
1714 (type.first == "application|octet-stream"))
1715 {
1716 supported = true;
1717 break;
1718 }
1719 }
1720 if (!supported)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001721 {
1722 asyncResp->res.result(
1723 boost::beast::http::status::bad_request);
1724 return;
1725 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001726
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001727 std::string entryID = param;
1728 dbus::utility::escapePathForDbus(entryID);
1729
1730 crow::connections::systemBus->async_method_call(
1731 [asyncResp,
1732 entryID](const boost::system::error_code ec,
1733 const sdbusplus::message::unix_fd& unixfd) {
1734 if (ec.value() == EBADR)
1735 {
1736 messages::resourceNotFound(
1737 asyncResp->res, "EventLogAttachment", entryID);
1738 return;
1739 }
1740 if (ec)
1741 {
1742 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1743 messages::internalError(asyncResp->res);
1744 return;
1745 }
1746
1747 int fd = -1;
1748 fd = dup(unixfd);
1749 if (fd == -1)
1750 {
1751 messages::internalError(asyncResp->res);
1752 return;
1753 }
1754
1755 long long int size = lseek(fd, 0, SEEK_END);
1756 if (size == -1)
1757 {
1758 messages::internalError(asyncResp->res);
1759 return;
1760 }
1761
1762 // Arbitrary max size of 64kb
1763 constexpr int maxFileSize = 65536;
1764 if (size > maxFileSize)
1765 {
1766 BMCWEB_LOG_ERROR
1767 << "File size exceeds maximum allowed size of "
1768 << maxFileSize;
1769 messages::internalError(asyncResp->res);
1770 return;
1771 }
1772 std::vector<char> data(static_cast<size_t>(size));
1773 long long int rc = lseek(fd, 0, SEEK_SET);
1774 if (rc == -1)
1775 {
1776 messages::internalError(asyncResp->res);
1777 return;
1778 }
1779 rc = read(fd, data.data(), data.size());
1780 if ((rc == -1) || (rc != size))
1781 {
1782 messages::internalError(asyncResp->res);
1783 return;
1784 }
1785 close(fd);
1786
1787 std::string_view strData(data.data(), data.size());
1788 std::string output =
1789 crow::utility::base64encode(strData);
1790
1791 asyncResp->res.addHeader("Content-Type",
1792 "application/octet-stream");
1793 asyncResp->res.addHeader("Content-Transfer-Encoding",
1794 "Base64");
1795 asyncResp->res.body() = std::move(output);
1796 },
1797 "xyz.openbmc_project.Logging",
1798 "/xyz/openbmc_project/logging/entry/" + entryID,
1799 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1800 });
1801}
1802
1803inline void requestRoutesBMCLogServiceCollection(App& app)
1804{
1805 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05001806 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001807 .methods(boost::beast::http::verb::get)(
1808 [](const crow::Request&,
1809 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1810 // Collections don't include the static data added by SubRoute
1811 // because it has a duplicate entry for members
1812 asyncResp->res.jsonValue["@odata.type"] =
1813 "#LogServiceCollection.LogServiceCollection";
1814 asyncResp->res.jsonValue["@odata.id"] =
1815 "/redfish/v1/Managers/bmc/LogServices";
1816 asyncResp->res.jsonValue["Name"] =
1817 "Open BMC Log Services Collection";
1818 asyncResp->res.jsonValue["Description"] =
1819 "Collection of LogServices for this Manager";
1820 nlohmann::json& logServiceArray =
1821 asyncResp->res.jsonValue["Members"];
1822 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001823#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001824 logServiceArray.push_back(
1825 {{"@odata.id",
1826 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001827#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001828#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001829 logServiceArray.push_back(
1830 {{"@odata.id",
1831 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001832#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001833 asyncResp->res.jsonValue["Members@odata.count"] =
1834 logServiceArray.size();
1835 });
1836}
Ed Tanous1da66f72018-07-27 16:13:37 -07001837
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001838inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001839{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001840 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07001841 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001842 .methods(boost::beast::http::verb::get)(
1843 [](const crow::Request&,
1844 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07001845
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001846 {
1847 asyncResp->res.jsonValue["@odata.type"] =
1848 "#LogService.v1_1_0.LogService";
1849 asyncResp->res.jsonValue["@odata.id"] =
1850 "/redfish/v1/Managers/bmc/LogServices/Journal";
1851 asyncResp->res.jsonValue["Name"] =
1852 "Open BMC Journal Log Service";
1853 asyncResp->res.jsonValue["Description"] =
1854 "BMC Journal Log Service";
1855 asyncResp->res.jsonValue["Id"] = "BMC Journal";
1856 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301857
1858 std::pair<std::string, std::string> redfishDateTimeOffset =
1859 crow::utility::getDateTimeOffsetNow();
1860 asyncResp->res.jsonValue["DateTime"] =
1861 redfishDateTimeOffset.first;
1862 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1863 redfishDateTimeOffset.second;
1864
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001865 asyncResp->res.jsonValue["Entries"] = {
1866 {"@odata.id",
1867 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1868 });
1869}
Jason M. Billse1f26342018-07-18 12:12:00 -07001870
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001871static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1872 sd_journal* journal,
1873 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07001874{
1875 // Get the Log Entry contents
1876 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07001877
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001878 std::string message;
1879 std::string_view syslogID;
1880 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
1881 if (ret < 0)
1882 {
1883 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
1884 << strerror(-ret);
1885 }
1886 if (!syslogID.empty())
1887 {
1888 message += std::string(syslogID) + ": ";
1889 }
1890
Ed Tanous39e77502019-03-04 17:35:53 -08001891 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07001892 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07001893 if (ret < 0)
1894 {
1895 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1896 return 1;
1897 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001898 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07001899
1900 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07001901 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07001902 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07001903 if (ret < 0)
1904 {
1905 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07001906 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001907
1908 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07001909 std::string entryTimeStr;
1910 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07001911 {
Jason M. Bills16428a12018-11-02 12:42:29 -07001912 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07001913 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001914
1915 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001916 bmcJournalLogEntryJson = {
Ed Tanousd0dbeef2021-07-01 08:46:46 -07001917 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001918 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1919 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07001920 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001921 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08001922 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07001923 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06001924 {"Severity", severity <= 2 ? "Critical"
1925 : severity <= 4 ? "Warning"
1926 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07001927 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07001928 {"Created", std::move(entryTimeStr)}};
1929 return 0;
1930}
1931
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001932inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07001933{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001934 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001935 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001936 .methods(boost::beast::http::verb::get)(
1937 [](const crow::Request& req,
1938 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1939 static constexpr const long maxEntriesPerPage = 1000;
1940 uint64_t skip = 0;
1941 uint64_t top = maxEntriesPerPage; // Show max entries by default
1942 if (!getSkipParam(asyncResp, req, skip))
1943 {
1944 return;
1945 }
1946 if (!getTopParam(asyncResp, req, top))
1947 {
1948 return;
1949 }
1950 // Collections don't include the static data added by SubRoute
1951 // because it has a duplicate entry for members
1952 asyncResp->res.jsonValue["@odata.type"] =
1953 "#LogEntryCollection.LogEntryCollection";
1954 asyncResp->res.jsonValue["@odata.id"] =
1955 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1956 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1957 asyncResp->res.jsonValue["Description"] =
1958 "Collection of BMC Journal Entries";
1959 nlohmann::json& logEntryArray =
1960 asyncResp->res.jsonValue["Members"];
1961 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07001962
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001963 // Go through the journal and use the timestamp to create a
1964 // unique ID for each entry
1965 sd_journal* journalTmp = nullptr;
1966 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1967 if (ret < 0)
1968 {
1969 BMCWEB_LOG_ERROR << "failed to open journal: "
1970 << strerror(-ret);
1971 messages::internalError(asyncResp->res);
1972 return;
1973 }
1974 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
1975 journal(journalTmp, sd_journal_close);
1976 journalTmp = nullptr;
1977 uint64_t entryCount = 0;
1978 // Reset the unique ID on the first entry
1979 bool firstEntry = true;
1980 SD_JOURNAL_FOREACH(journal.get())
1981 {
1982 entryCount++;
1983 // Handle paging using skip (number of entries to skip from
1984 // the start) and top (number of entries to display)
1985 if (entryCount <= skip || entryCount > skip + top)
1986 {
1987 continue;
1988 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001989
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001990 std::string idStr;
1991 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1992 {
1993 continue;
1994 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001995
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001996 if (firstEntry)
1997 {
1998 firstEntry = false;
1999 }
Jason M. Bills193ad2f2018-09-26 15:08:52 -07002000
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002001 logEntryArray.push_back({});
2002 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2003 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2004 bmcJournalLogEntry) != 0)
2005 {
2006 messages::internalError(asyncResp->res);
2007 return;
2008 }
2009 }
2010 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2011 if (skip + top < entryCount)
2012 {
2013 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2014 "/redfish/v1/Managers/bmc/LogServices/Journal/"
2015 "Entries?$skip=" +
2016 std::to_string(skip + top);
2017 }
2018 });
2019}
Jason M. Billse1f26342018-07-18 12:12:00 -07002020
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002021inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002022{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002023 BMCWEB_ROUTE(app,
2024 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002025 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002026 .methods(boost::beast::http::verb::get)(
2027 [](const crow::Request&,
2028 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2029 const std::string& entryID) {
2030 // Convert the unique ID back to a timestamp to find the entry
2031 uint64_t ts = 0;
2032 uint64_t index = 0;
2033 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2034 {
2035 return;
2036 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002037
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002038 sd_journal* journalTmp = nullptr;
2039 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2040 if (ret < 0)
2041 {
2042 BMCWEB_LOG_ERROR << "failed to open journal: "
2043 << strerror(-ret);
2044 messages::internalError(asyncResp->res);
2045 return;
2046 }
2047 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2048 journal(journalTmp, sd_journal_close);
2049 journalTmp = nullptr;
2050 // Go to the timestamp in the log and move to the entry at the
2051 // index tracking the unique ID
2052 std::string idStr;
2053 bool firstEntry = true;
2054 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2055 if (ret < 0)
2056 {
2057 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2058 << strerror(-ret);
2059 messages::internalError(asyncResp->res);
2060 return;
2061 }
2062 for (uint64_t i = 0; i <= index; i++)
2063 {
2064 sd_journal_next(journal.get());
2065 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2066 {
2067 messages::internalError(asyncResp->res);
2068 return;
2069 }
2070 if (firstEntry)
2071 {
2072 firstEntry = false;
2073 }
2074 }
2075 // Confirm that the entry ID matches what was requested
2076 if (idStr != entryID)
2077 {
2078 messages::resourceMissingAtURI(asyncResp->res, entryID);
2079 return;
2080 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002081
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002082 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2083 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002084 {
2085 messages::internalError(asyncResp->res);
2086 return;
2087 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002088 });
2089}
2090
2091inline void requestRoutesBMCDumpService(App& app)
2092{
2093 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002094 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002095 .methods(boost::beast::http::verb::get)(
2096 [](const crow::Request&,
2097 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2098 asyncResp->res.jsonValue["@odata.id"] =
2099 "/redfish/v1/Managers/bmc/LogServices/Dump";
2100 asyncResp->res.jsonValue["@odata.type"] =
2101 "#LogService.v1_2_0.LogService";
2102 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2103 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2104 asyncResp->res.jsonValue["Id"] = "Dump";
2105 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302106
2107 std::pair<std::string, std::string> redfishDateTimeOffset =
2108 crow::utility::getDateTimeOffsetNow();
2109 asyncResp->res.jsonValue["DateTime"] =
2110 redfishDateTimeOffset.first;
2111 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2112 redfishDateTimeOffset.second;
2113
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002114 asyncResp->res.jsonValue["Entries"] = {
2115 {"@odata.id",
2116 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2117 asyncResp->res.jsonValue["Actions"] = {
2118 {"#LogService.ClearLog",
2119 {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2120 "Actions/LogService.ClearLog"}}},
2121 {"#LogService.CollectDiagnosticData",
2122 {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2123 "Actions/LogService.CollectDiagnosticData"}}}};
2124 });
2125}
2126
2127inline void requestRoutesBMCDumpEntryCollection(App& app)
2128{
2129
2130 /**
2131 * Functions triggers appropriate requests on DBus
2132 */
2133 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002134 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002135 .methods(boost::beast::http::verb::get)(
2136 [](const crow::Request&,
2137 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2138 asyncResp->res.jsonValue["@odata.type"] =
2139 "#LogEntryCollection.LogEntryCollection";
2140 asyncResp->res.jsonValue["@odata.id"] =
2141 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2142 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2143 asyncResp->res.jsonValue["Description"] =
2144 "Collection of BMC Dump Entries";
2145
2146 getDumpEntryCollection(asyncResp, "BMC");
2147 });
2148}
2149
2150inline void requestRoutesBMCDumpEntry(App& app)
2151{
2152 BMCWEB_ROUTE(app,
2153 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002154 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002155 .methods(boost::beast::http::verb::get)(
2156 [](const crow::Request&,
2157 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2158 const std::string& param) {
2159 getDumpEntryById(asyncResp, param, "BMC");
2160 });
2161 BMCWEB_ROUTE(app,
2162 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002163 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002164 .methods(boost::beast::http::verb::delete_)(
2165 [](const crow::Request&,
2166 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2167 const std::string& param) {
2168 deleteDumpEntry(asyncResp, param, "bmc");
2169 });
2170}
2171
2172inline void requestRoutesBMCDumpCreate(App& app)
2173{
2174
2175 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2176 "Actions/"
2177 "LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002178 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002179 .methods(boost::beast::http::verb::post)(
2180 [](const crow::Request& req,
2181 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2182 createDump(asyncResp, req, "BMC");
2183 });
2184}
2185
2186inline void requestRoutesBMCDumpClear(App& app)
2187{
2188 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2189 "Actions/"
2190 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002191 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002192 .methods(boost::beast::http::verb::post)(
2193 [](const crow::Request&,
2194 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2195 clearDump(asyncResp, "BMC");
2196 });
2197}
2198
2199inline void requestRoutesSystemDumpService(App& app)
2200{
2201 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002202 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002203 .methods(boost::beast::http::verb::get)(
2204 [](const crow::Request&,
2205 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2206
2207 {
2208 asyncResp->res.jsonValue["@odata.id"] =
2209 "/redfish/v1/Systems/system/LogServices/Dump";
2210 asyncResp->res.jsonValue["@odata.type"] =
2211 "#LogService.v1_2_0.LogService";
2212 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2213 asyncResp->res.jsonValue["Description"] =
2214 "System Dump LogService";
2215 asyncResp->res.jsonValue["Id"] = "Dump";
2216 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302217
2218 std::pair<std::string, std::string> redfishDateTimeOffset =
2219 crow::utility::getDateTimeOffsetNow();
2220 asyncResp->res.jsonValue["DateTime"] =
2221 redfishDateTimeOffset.first;
2222 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2223 redfishDateTimeOffset.second;
2224
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002225 asyncResp->res.jsonValue["Entries"] = {
2226 {"@odata.id",
2227 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2228 asyncResp->res.jsonValue["Actions"] = {
2229 {"#LogService.ClearLog",
2230 {{"target",
2231 "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2232 "LogService.ClearLog"}}},
2233 {"#LogService.CollectDiagnosticData",
2234 {{"target",
2235 "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2236 "LogService.CollectDiagnosticData"}}}};
2237 });
2238}
2239
2240inline void requestRoutesSystemDumpEntryCollection(App& app)
2241{
2242
2243 /**
2244 * Functions triggers appropriate requests on DBus
2245 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002246 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002247 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002248 .methods(boost::beast::http::verb::get)(
2249 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002250 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002251 asyncResp->res.jsonValue["@odata.type"] =
2252 "#LogEntryCollection.LogEntryCollection";
2253 asyncResp->res.jsonValue["@odata.id"] =
2254 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2255 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2256 asyncResp->res.jsonValue["Description"] =
2257 "Collection of System Dump Entries";
2258
2259 getDumpEntryCollection(asyncResp, "System");
2260 });
2261}
2262
2263inline void requestRoutesSystemDumpEntry(App& app)
2264{
2265 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002266 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002267 .privileges(redfish::privileges::getLogEntry)
2268
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002269 .methods(boost::beast::http::verb::get)(
2270 [](const crow::Request&,
2271 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2272 const std::string& param) {
2273 getDumpEntryById(asyncResp, param, "System");
2274 });
2275
2276 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002277 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002278 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002279 .methods(boost::beast::http::verb::delete_)(
2280 [](const crow::Request&,
2281 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2282 const std::string& param) {
2283 deleteDumpEntry(asyncResp, param, "system");
2284 });
2285}
2286
2287inline void requestRoutesSystemDumpCreate(App& app)
2288{
2289 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2290 "Actions/"
2291 "LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002292 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002293 .methods(boost::beast::http::verb::post)(
2294 [](const crow::Request& req,
2295 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2296
2297 { createDump(asyncResp, req, "System"); });
2298}
2299
2300inline void requestRoutesSystemDumpClear(App& app)
2301{
2302 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2303 "Actions/"
2304 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002305 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002306 .methods(boost::beast::http::verb::post)(
2307 [](const crow::Request&,
2308 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2309
2310 { clearDump(asyncResp, "System"); });
2311}
2312
2313inline void requestRoutesCrashdumpService(App& app)
2314{
2315 // Note: Deviated from redfish privilege registry for GET & HEAD
2316 // method for security reasons.
2317 /**
2318 * Functions triggers appropriate requests on DBus
2319 */
2320 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002321 // This is incorrect, should be:
2322 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002323 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002324 .methods(
2325 boost::beast::http::verb::
2326 get)([](const crow::Request&,
2327 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2328 // Copy over the static data to include the entries added by
2329 // SubRoute
2330 asyncResp->res.jsonValue["@odata.id"] =
2331 "/redfish/v1/Systems/system/LogServices/Crashdump";
2332 asyncResp->res.jsonValue["@odata.type"] =
2333 "#LogService.v1_2_0.LogService";
2334 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2335 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2336 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2337 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2338 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302339
2340 std::pair<std::string, std::string> redfishDateTimeOffset =
2341 crow::utility::getDateTimeOffsetNow();
2342 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2343 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2344 redfishDateTimeOffset.second;
2345
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002346 asyncResp->res.jsonValue["Entries"] = {
2347 {"@odata.id",
2348 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2349 asyncResp->res.jsonValue["Actions"] = {
2350 {"#LogService.ClearLog",
2351 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2352 "Actions/LogService.ClearLog"}}},
2353 {"#LogService.CollectDiagnosticData",
2354 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2355 "Actions/LogService.CollectDiagnosticData"}}}};
2356 });
2357}
2358
2359void inline requestRoutesCrashdumpClear(App& app)
2360{
2361 BMCWEB_ROUTE(app,
2362 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
2363 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002364 // This is incorrect, should be:
2365 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002366 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002367 .methods(boost::beast::http::verb::post)(
2368 [](const crow::Request&,
2369 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2370 crow::connections::systemBus->async_method_call(
2371 [asyncResp](const boost::system::error_code ec,
2372 const std::string&) {
2373 if (ec)
2374 {
2375 messages::internalError(asyncResp->res);
2376 return;
2377 }
2378 messages::success(asyncResp->res);
2379 },
2380 crashdumpObject, crashdumpPath, deleteAllInterface,
2381 "DeleteAll");
2382 });
2383}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002384
zhanghch058d1b46d2021-04-01 11:18:24 +08002385static void
2386 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2387 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002388{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002389 auto getStoredLogCallback =
2390 [asyncResp, logID, &logEntryJson](
2391 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002392 const std::vector<std::pair<std::string, VariantType>>& params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002393 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002394 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002395 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2396 if (ec.value() ==
2397 boost::system::linux_error::bad_request_descriptor)
2398 {
2399 messages::resourceNotFound(asyncResp->res, "LogEntry",
2400 logID);
2401 }
2402 else
2403 {
2404 messages::internalError(asyncResp->res);
2405 }
2406 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002407 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002408
Johnathan Mantey043a0532020-03-10 17:15:28 -07002409 std::string timestamp{};
2410 std::string filename{};
2411 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002412 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002413
2414 if (filename.empty() || timestamp.empty())
2415 {
2416 messages::resourceMissingAtURI(asyncResp->res, logID);
2417 return;
2418 }
2419
2420 std::string crashdumpURI =
2421 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2422 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002423 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002424 {"@odata.id", "/redfish/v1/Systems/system/"
2425 "LogServices/Crashdump/Entries/" +
2426 logID},
2427 {"Name", "CPU Crashdump"},
2428 {"Id", logID},
2429 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002430 {"AdditionalDataURI", std::move(crashdumpURI)},
2431 {"DiagnosticDataType", "OEM"},
2432 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002433 {"Created", std::move(timestamp)}};
2434 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002435 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002436 std::move(getStoredLogCallback), crashdumpObject,
2437 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002438 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002439}
2440
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002441inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002442{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002443 // Note: Deviated from redfish privilege registry for GET & HEAD
2444 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002445 /**
2446 * Functions triggers appropriate requests on DBus
2447 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002448 BMCWEB_ROUTE(app,
2449 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002450 // This is incorrect, should be.
2451 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002452 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002453 .methods(
2454 boost::beast::http::verb::
2455 get)([](const crow::Request&,
2456 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2457 // Collections don't include the static data added by SubRoute
2458 // because it has a duplicate entry for members
2459 auto getLogEntriesCallback = [asyncResp](
2460 const boost::system::error_code ec,
2461 const std::vector<std::string>&
2462 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002463 if (ec)
2464 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002465 if (ec.value() !=
2466 boost::system::errc::no_such_file_or_directory)
2467 {
2468 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2469 << ec.message();
2470 messages::internalError(asyncResp->res);
2471 return;
2472 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002473 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002474 asyncResp->res.jsonValue["@odata.type"] =
2475 "#LogEntryCollection.LogEntryCollection";
2476 asyncResp->res.jsonValue["@odata.id"] =
2477 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2478 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2479 asyncResp->res.jsonValue["Description"] =
2480 "Collection of Crashdump Entries";
2481 nlohmann::json& logEntryArray =
2482 asyncResp->res.jsonValue["Members"];
2483 logEntryArray = nlohmann::json::array();
2484 std::vector<std::string> logIDs;
2485 // Get the list of log entries and build up an empty array big
2486 // enough to hold them
2487 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002488 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002489 // Get the log ID
2490 std::size_t lastPos = objpath.rfind('/');
2491 if (lastPos == std::string::npos)
2492 {
2493 continue;
2494 }
2495 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002496
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002497 // Add a space for the log entry to the array
2498 logEntryArray.push_back({});
2499 }
2500 // Now go through and set up async calls to fill in the entries
2501 size_t index = 0;
2502 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002503 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002504 // Add the log entry to the array
2505 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002506 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002507 asyncResp->res.jsonValue["Members@odata.count"] =
2508 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002509 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002510 crow::connections::systemBus->async_method_call(
2511 std::move(getLogEntriesCallback),
2512 "xyz.openbmc_project.ObjectMapper",
2513 "/xyz/openbmc_project/object_mapper",
2514 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2515 std::array<const char*, 1>{crashdumpInterface});
2516 });
2517}
Ed Tanous1da66f72018-07-27 16:13:37 -07002518
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002519inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002520{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002521 // Note: Deviated from redfish privilege registry for GET & HEAD
2522 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002523
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002524 BMCWEB_ROUTE(
2525 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002526 // this is incorrect, should be
2527 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002528 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002529 .methods(boost::beast::http::verb::get)(
2530 [](const crow::Request&,
2531 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2532 const std::string& param) {
2533 const std::string& logID = param;
2534 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2535 });
2536}
Ed Tanous1da66f72018-07-27 16:13:37 -07002537
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002538inline void requestRoutesCrashdumpFile(App& app)
2539{
2540 // Note: Deviated from redfish privilege registry for GET & HEAD
2541 // method for security reasons.
2542 BMCWEB_ROUTE(
2543 app,
2544 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002545 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002546 .methods(boost::beast::http::verb::get)(
2547 [](const crow::Request&,
2548 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2549 const std::string& logID, const std::string& fileName) {
2550 auto getStoredLogCallback =
2551 [asyncResp, logID, fileName](
2552 const boost::system::error_code ec,
2553 const std::vector<std::pair<std::string, VariantType>>&
2554 resp) {
2555 if (ec)
2556 {
2557 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2558 << ec.message();
2559 messages::internalError(asyncResp->res);
2560 return;
2561 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002562
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002563 std::string dbusFilename{};
2564 std::string dbusTimestamp{};
2565 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002566
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002567 parseCrashdumpParameters(resp, dbusFilename,
2568 dbusTimestamp, dbusFilepath);
2569
2570 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2571 dbusFilepath.empty())
2572 {
2573 messages::resourceMissingAtURI(asyncResp->res,
2574 fileName);
2575 return;
2576 }
2577
2578 // Verify the file name parameter is correct
2579 if (fileName != dbusFilename)
2580 {
2581 messages::resourceMissingAtURI(asyncResp->res,
2582 fileName);
2583 return;
2584 }
2585
2586 if (!std::filesystem::exists(dbusFilepath))
2587 {
2588 messages::resourceMissingAtURI(asyncResp->res,
2589 fileName);
2590 return;
2591 }
2592 std::ifstream ifs(dbusFilepath, std::ios::in |
2593 std::ios::binary |
2594 std::ios::ate);
2595 std::ifstream::pos_type fileSize = ifs.tellg();
2596 if (fileSize < 0)
2597 {
2598 messages::generalError(asyncResp->res);
2599 return;
2600 }
2601 ifs.seekg(0, std::ios::beg);
2602
2603 auto crashData = std::make_unique<char[]>(
2604 static_cast<unsigned int>(fileSize));
2605
2606 ifs.read(crashData.get(), static_cast<int>(fileSize));
2607
2608 // The cast to std::string is intentional in order to
2609 // use the assign() that applies move mechanics
2610 asyncResp->res.body().assign(
2611 static_cast<std::string>(crashData.get()));
2612
2613 // Configure this to be a file download when accessed
2614 // from a browser
2615 asyncResp->res.addHeader("Content-Disposition",
2616 "attachment");
2617 };
2618 crow::connections::systemBus->async_method_call(
2619 std::move(getStoredLogCallback), crashdumpObject,
2620 crashdumpPath + std::string("/") + logID,
2621 "org.freedesktop.DBus.Properties", "GetAll",
2622 crashdumpInterface);
2623 });
2624}
2625
2626inline void requestRoutesCrashdumpCollect(App& app)
2627{
2628 // Note: Deviated from redfish privilege registry for GET & HEAD
2629 // method for security reasons.
2630 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/"
2631 "Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002632 // The below is incorrect; Should be ConfigureManager
2633 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002634 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002635 .methods(
2636 boost::beast::http::verb::
2637 post)([](const crow::Request& req,
2638 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2639 std::string diagnosticDataType;
2640 std::string oemDiagnosticDataType;
2641 if (!redfish::json_util::readJson(
2642 req, asyncResp->res, "DiagnosticDataType",
2643 diagnosticDataType, "OEMDiagnosticDataType",
2644 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002645 {
James Feist46229572020-02-19 15:11:58 -08002646 return;
2647 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002648
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002649 if (diagnosticDataType != "OEM")
2650 {
2651 BMCWEB_LOG_ERROR
2652 << "Only OEM DiagnosticDataType supported for Crashdump";
2653 messages::actionParameterValueFormatError(
2654 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2655 "CollectDiagnosticData");
2656 return;
2657 }
2658
2659 auto collectCrashdumpCallback = [asyncResp, req](
2660 const boost::system::error_code
2661 ec,
2662 const std::string&) {
2663 if (ec)
2664 {
2665 if (ec.value() ==
2666 boost::system::errc::operation_not_supported)
2667 {
2668 messages::resourceInStandby(asyncResp->res);
2669 }
2670 else if (ec.value() ==
2671 boost::system::errc::device_or_resource_busy)
2672 {
2673 messages::serviceTemporarilyUnavailable(asyncResp->res,
2674 "60");
2675 }
2676 else
2677 {
2678 messages::internalError(asyncResp->res);
2679 }
2680 return;
2681 }
2682 std::shared_ptr<task::TaskData> task =
2683 task::TaskData::createTask(
2684 [](boost::system::error_code err,
2685 sdbusplus::message::message&,
2686 const std::shared_ptr<task::TaskData>& taskData) {
2687 if (!err)
2688 {
2689 taskData->messages.emplace_back(
2690 messages::taskCompletedOK(
2691 std::to_string(taskData->index)));
2692 taskData->state = "Completed";
2693 }
2694 return task::completed;
2695 },
2696 "type='signal',interface='org.freedesktop.DBus."
2697 "Properties',"
2698 "member='PropertiesChanged',arg0namespace='com.intel."
2699 "crashdump'");
2700 task->startTimer(std::chrono::minutes(5));
2701 task->populateResp(asyncResp->res);
2702 task->payload.emplace(req);
2703 };
2704
2705 if (oemDiagnosticDataType == "OnDemand")
2706 {
2707 crow::connections::systemBus->async_method_call(
2708 std::move(collectCrashdumpCallback), crashdumpObject,
2709 crashdumpPath, crashdumpOnDemandInterface,
2710 "GenerateOnDemandLog");
2711 }
2712 else if (oemDiagnosticDataType == "Telemetry")
2713 {
2714 crow::connections::systemBus->async_method_call(
2715 std::move(collectCrashdumpCallback), crashdumpObject,
2716 crashdumpPath, crashdumpTelemetryInterface,
2717 "GenerateTelemetryLog");
2718 }
2719 else
2720 {
2721 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2722 << oemDiagnosticDataType;
2723 messages::actionParameterValueFormatError(
2724 asyncResp->res, oemDiagnosticDataType,
2725 "OEMDiagnosticDataType", "CollectDiagnosticData");
2726 return;
2727 }
2728 });
2729}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002730
Andrew Geisslercb92c032018-08-17 07:56:14 -07002731/**
2732 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2733 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002734inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002735{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002736 /**
2737 * Function handles POST method request.
2738 * The Clear Log actions does not require any parameter.The action deletes
2739 * all entries found in the Entries collection for this Log Service.
2740 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002741
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002742 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
2743 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002744 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002745 .methods(boost::beast::http::verb::post)(
2746 [](const crow::Request&,
2747 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2748 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07002749
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002750 // Process response from Logging service.
2751 auto respHandler = [asyncResp](
2752 const boost::system::error_code ec) {
2753 BMCWEB_LOG_DEBUG
2754 << "doClearLog resp_handler callback: Done";
2755 if (ec)
2756 {
2757 // TODO Handle for specific error code
2758 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
2759 << ec;
2760 asyncResp->res.result(
2761 boost::beast::http::status::internal_server_error);
2762 return;
2763 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07002764
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002765 asyncResp->res.result(
2766 boost::beast::http::status::no_content);
2767 };
2768
2769 // Make call to Logging service to request Clear Log
2770 crow::connections::systemBus->async_method_call(
2771 respHandler, "xyz.openbmc_project.Logging",
2772 "/xyz/openbmc_project/logging",
2773 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2774 });
2775}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002776
2777/****************************************************
2778 * Redfish PostCode interfaces
2779 * using DBUS interface: getPostCodesTS
2780 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002781inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002782{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002783 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07002784 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002785 .methods(boost::beast::http::verb::get)(
2786 [](const crow::Request&,
2787 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2788 asyncResp->res.jsonValue = {
2789 {"@odata.id",
2790 "/redfish/v1/Systems/system/LogServices/PostCodes"},
2791 {"@odata.type", "#LogService.v1_1_0.LogService"},
2792 {"Name", "POST Code Log Service"},
2793 {"Description", "POST Code Log Service"},
2794 {"Id", "BIOS POST Code Log"},
2795 {"OverWritePolicy", "WrapsWhenFull"},
2796 {"Entries",
2797 {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2798 "PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05302799
2800 std::pair<std::string, std::string> redfishDateTimeOffset =
2801 crow::utility::getDateTimeOffsetNow();
2802 asyncResp->res.jsonValue["DateTime"] =
2803 redfishDateTimeOffset.first;
2804 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2805 redfishDateTimeOffset.second;
2806
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002807 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
2808 {"target",
2809 "/redfish/v1/Systems/system/LogServices/PostCodes/"
2810 "Actions/LogService.ClearLog"}};
2811 });
2812}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002813
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002814inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002815{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002816 BMCWEB_ROUTE(app,
2817 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
2818 "LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002819 // The following privilege is incorrect; It should be ConfigureManager
2820 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002821 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002822 .methods(boost::beast::http::verb::post)(
2823 [](const crow::Request&,
2824 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2825 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08002826
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002827 // Make call to post-code service to request clear all
2828 crow::connections::systemBus->async_method_call(
2829 [asyncResp](const boost::system::error_code ec) {
2830 if (ec)
2831 {
2832 // TODO Handle for specific error code
2833 BMCWEB_LOG_ERROR
2834 << "doClearPostCodes resp_handler got error "
2835 << ec;
2836 asyncResp->res.result(boost::beast::http::status::
2837 internal_server_error);
2838 messages::internalError(asyncResp->res);
2839 return;
2840 }
2841 },
2842 "xyz.openbmc_project.State.Boot.PostCode0",
2843 "/xyz/openbmc_project/State/Boot/PostCode0",
2844 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2845 });
2846}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002847
2848static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08002849 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302850 const boost::container::flat_map<
2851 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08002852 const uint16_t bootIndex, const uint64_t codeIndex = 0,
2853 const uint64_t skip = 0, const uint64_t top = 0)
2854{
2855 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002856 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05302857 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08002858
2859 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002860 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08002861
2862 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302863 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
2864 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002865 {
2866 currentCodeIndex++;
2867 std::string postcodeEntryID =
2868 "B" + std::to_string(bootIndex) + "-" +
2869 std::to_string(currentCodeIndex); // 1 based index in EntryID string
2870
2871 uint64_t usecSinceEpoch = code.first;
2872 uint64_t usTimeOffset = 0;
2873
2874 if (1 == currentCodeIndex)
2875 { // already incremented
2876 firstCodeTimeUs = code.first;
2877 }
2878 else
2879 {
2880 usTimeOffset = code.first - firstCodeTimeUs;
2881 }
2882
2883 // skip if no specific codeIndex is specified and currentCodeIndex does
2884 // not fall between top and skip
2885 if ((codeIndex == 0) &&
2886 (currentCodeIndex <= skip || currentCodeIndex > top))
2887 {
2888 continue;
2889 }
2890
Gunnar Mills4e0453b2020-07-08 14:00:30 -05002891 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08002892 // currentIndex
2893 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
2894 {
2895 // This is done for simplicity. 1st entry is needed to calculate
2896 // time offset. To improve efficiency, one can get to the entry
2897 // directly (possibly with flatmap's nth method)
2898 continue;
2899 }
2900
2901 // currentCodeIndex is within top and skip or equal to specified code
2902 // index
2903
2904 // Get the Created time from the timestamp
2905 std::string entryTimeStr;
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -05002906 entryTimeStr = crow::utility::getDateTime(
2907 static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
ZhikuiRena3316fc2020-01-29 14:58:08 -08002908
2909 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
2910 std::ostringstream hexCode;
2911 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302912 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08002913 std::ostringstream timeOffsetStr;
2914 // Set Fixed -Point Notation
2915 timeOffsetStr << std::fixed;
2916 // Set precision to 4 digits
2917 timeOffsetStr << std::setprecision(4);
2918 // Add double to stream
2919 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
2920 std::vector<std::string> messageArgs = {
2921 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
2922
2923 // Get MessageArgs template from message registry
2924 std::string msg;
2925 if (message != nullptr)
2926 {
2927 msg = message->message;
2928
2929 // fill in this post code value
2930 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002931 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002932 {
2933 std::string argStr = "%" + std::to_string(++i);
2934 size_t argPos = msg.find(argStr);
2935 if (argPos != std::string::npos)
2936 {
2937 msg.replace(argPos, argStr.length(), messageArg);
2938 }
2939 }
2940 }
2941
Tim Leed4342a92020-04-27 11:47:58 +08002942 // Get Severity template from message registry
2943 std::string severity;
2944 if (message != nullptr)
2945 {
2946 severity = message->severity;
2947 }
2948
ZhikuiRena3316fc2020-01-29 14:58:08 -08002949 // add to AsyncResp
2950 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002951 nlohmann::json& bmcLogEntry = logEntryArray.back();
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002952 bmcLogEntry = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Gunnar Mills743e9a12020-10-26 12:44:53 -05002953 {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2954 "PostCodes/Entries/" +
2955 postcodeEntryID},
2956 {"Name", "POST Code Log Entry"},
2957 {"Id", postcodeEntryID},
2958 {"Message", std::move(msg)},
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05302959 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
Gunnar Mills743e9a12020-10-26 12:44:53 -05002960 {"MessageArgs", std::move(messageArgs)},
2961 {"EntryType", "Event"},
2962 {"Severity", std::move(severity)},
2963 {"Created", entryTimeStr}};
ZhikuiRena3316fc2020-01-29 14:58:08 -08002964 }
2965}
2966
zhanghch058d1b46d2021-04-01 11:18:24 +08002967static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08002968 const uint16_t bootIndex,
2969 const uint64_t codeIndex)
2970{
2971 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05302972 [aResp, bootIndex,
2973 codeIndex](const boost::system::error_code ec,
2974 const boost::container::flat_map<
2975 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
2976 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08002977 if (ec)
2978 {
2979 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
2980 messages::internalError(aResp->res);
2981 return;
2982 }
2983
2984 // skip the empty postcode boots
2985 if (postcode.empty())
2986 {
2987 return;
2988 }
2989
2990 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
2991
2992 aResp->res.jsonValue["Members@odata.count"] =
2993 aResp->res.jsonValue["Members"].size();
2994 },
Jonathan Doman15124762021-01-07 17:54:17 -08002995 "xyz.openbmc_project.State.Boot.PostCode0",
2996 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08002997 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
2998 bootIndex);
2999}
3000
zhanghch058d1b46d2021-04-01 11:18:24 +08003001static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003002 const uint16_t bootIndex,
3003 const uint16_t bootCount,
3004 const uint64_t entryCount, const uint64_t skip,
3005 const uint64_t top)
3006{
3007 crow::connections::systemBus->async_method_call(
3008 [aResp, bootIndex, bootCount, entryCount, skip,
3009 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303010 const boost::container::flat_map<
3011 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3012 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003013 if (ec)
3014 {
3015 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3016 messages::internalError(aResp->res);
3017 return;
3018 }
3019
3020 uint64_t endCount = entryCount;
3021 if (!postcode.empty())
3022 {
3023 endCount = entryCount + postcode.size();
3024
3025 if ((skip < endCount) && ((top + skip) > entryCount))
3026 {
3027 uint64_t thisBootSkip =
3028 std::max(skip, entryCount) - entryCount;
3029 uint64_t thisBootTop =
3030 std::min(top + skip, endCount) - entryCount;
3031
3032 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3033 thisBootSkip, thisBootTop);
3034 }
3035 aResp->res.jsonValue["Members@odata.count"] = endCount;
3036 }
3037
3038 // continue to previous bootIndex
3039 if (bootIndex < bootCount)
3040 {
3041 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3042 bootCount, endCount, skip, top);
3043 }
3044 else
3045 {
3046 aResp->res.jsonValue["Members@odata.nextLink"] =
3047 "/redfish/v1/Systems/system/LogServices/PostCodes/"
3048 "Entries?$skip=" +
3049 std::to_string(skip + top);
3050 }
3051 },
Jonathan Doman15124762021-01-07 17:54:17 -08003052 "xyz.openbmc_project.State.Boot.PostCode0",
3053 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003054 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3055 bootIndex);
3056}
3057
zhanghch058d1b46d2021-04-01 11:18:24 +08003058static void
3059 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3060 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003061{
3062 uint64_t entryCount = 0;
3063 crow::connections::systemBus->async_method_call(
3064 [aResp, entryCount, skip,
3065 top](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003066 const std::variant<uint16_t>& bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003067 if (ec)
3068 {
3069 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3070 messages::internalError(aResp->res);
3071 return;
3072 }
3073 auto pVal = std::get_if<uint16_t>(&bootCount);
3074 if (pVal)
3075 {
3076 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3077 }
3078 else
3079 {
3080 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3081 }
3082 },
Jonathan Doman15124762021-01-07 17:54:17 -08003083 "xyz.openbmc_project.State.Boot.PostCode0",
3084 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003085 "org.freedesktop.DBus.Properties", "Get",
3086 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3087}
3088
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003089inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003090{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003091 BMCWEB_ROUTE(app,
3092 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003093 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003094 .methods(boost::beast::http::verb::get)(
3095 [](const crow::Request& req,
3096 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3097 asyncResp->res.jsonValue["@odata.type"] =
3098 "#LogEntryCollection.LogEntryCollection";
3099 asyncResp->res.jsonValue["@odata.id"] =
3100 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3101 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3102 asyncResp->res.jsonValue["Description"] =
3103 "Collection of POST Code Log Entries";
3104 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3105 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003106
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003107 uint64_t skip = 0;
3108 uint64_t top = maxEntriesPerPage; // Show max entries by default
3109 if (!getSkipParam(asyncResp, req, skip))
3110 {
3111 return;
3112 }
3113 if (!getTopParam(asyncResp, req, top))
3114 {
3115 return;
3116 }
3117 getCurrentBootNumber(asyncResp, skip, top);
3118 });
3119}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003120
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003121inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003122{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003123 BMCWEB_ROUTE(
3124 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003125 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003126 .methods(boost::beast::http::verb::get)(
3127 [](const crow::Request&,
3128 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3129 const std::string& targetID) {
3130 size_t bootPos = targetID.find('B');
3131 if (bootPos == std::string::npos)
3132 {
3133 // Requested ID was not found
3134 messages::resourceMissingAtURI(asyncResp->res, targetID);
3135 return;
3136 }
3137 std::string_view bootIndexStr(targetID);
3138 bootIndexStr.remove_prefix(bootPos + 1);
3139 uint16_t bootIndex = 0;
3140 uint64_t codeIndex = 0;
3141 size_t dashPos = bootIndexStr.find('-');
ZhikuiRena3316fc2020-01-29 14:58:08 -08003142
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003143 if (dashPos == std::string::npos)
3144 {
3145 return;
3146 }
3147 std::string_view codeIndexStr(bootIndexStr);
3148 bootIndexStr.remove_suffix(dashPos);
3149 codeIndexStr.remove_prefix(dashPos + 1);
zhanghch058d1b46d2021-04-01 11:18:24 +08003150
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003151 bootIndex = static_cast<uint16_t>(
3152 strtoul(std::string(bootIndexStr).c_str(), nullptr, 0));
3153 codeIndex =
3154 strtoul(std::string(codeIndexStr).c_str(), nullptr, 0);
3155 if (bootIndex == 0 || codeIndex == 0)
3156 {
3157 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3158 << targetID;
3159 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003160
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003161 asyncResp->res.jsonValue["@odata.type"] =
3162 "#LogEntry.v1_4_0.LogEntry";
3163 asyncResp->res.jsonValue["@odata.id"] =
3164 "/redfish/v1/Systems/system/LogServices/PostCodes/"
3165 "Entries";
3166 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3167 asyncResp->res.jsonValue["Description"] =
3168 "Collection of POST Code Log Entries";
3169 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3170 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003171
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003172 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3173 });
3174}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003175
Ed Tanous1da66f72018-07-27 16:13:37 -07003176} // namespace redfish