blob: 9f40725d612c1c9fcaf0d46d935394b511ddf2bb [file] [log] [blame]
Ed Tanous1da66f72018-07-27 16:13:37 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Spencer Kub7028eb2021-10-26 15:27:35 +080018#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080019#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080020#include "human_sort.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070021#include "registries.hpp"
22#include "registries/base_message_registry.hpp"
23#include "registries/openbmc_message_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080024#include "task.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070025
Jason M. Billse1f26342018-07-18 12:12:00 -070026#include <systemd/sd-journal.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060027#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070028
John Edward Broadbent7e860f12021-04-08 15:57:16 -070029#include <app.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060030#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070031#include <boost/algorithm/string/split.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060032#include <boost/beast/http.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070033#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080034#include <boost/system/linux_error.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080035#include <dbus_utility.hpp>
Andrew Geisslercb92c032018-08-17 07:56:14 -070036#include <error_messages.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070037#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038
George Liu647b3cd2021-07-05 12:43:56 +080039#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070040#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080041#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070042#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070043#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080044#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070045
46namespace redfish
47{
48
Gunnar Mills1214b7e2020-06-04 10:11:30 -050049constexpr char const* crashdumpObject = "com.intel.crashdump";
50constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050051constexpr char const* crashdumpInterface = "com.intel.crashdump";
52constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070053 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050054constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070055 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070056constexpr char const* crashdumpTelemetryInterface =
57 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070058
Jason M. Bills4851d452019-03-28 11:27:48 -070059namespace message_registries
60{
Ed Tanous26702d02021-11-03 15:02:33 -070061static const Message*
62 getMessageFromRegistry(const std::string& messageKey,
63 const std::span<const MessageEntry> registry)
Jason M. Bills4851d452019-03-28 11:27:48 -070064{
Ed Tanous26702d02021-11-03 15:02:33 -070065 std::span<const MessageEntry>::iterator messageIt = std::find_if(
66 registry.begin(), registry.end(),
67 [&messageKey](const MessageEntry& messageEntry) {
68 return !std::strcmp(messageEntry.first, messageKey.c_str());
69 });
70 if (messageIt != registry.end())
Jason M. Bills4851d452019-03-28 11:27:48 -070071 {
72 return &messageIt->second;
73 }
74
75 return nullptr;
76}
77
Gunnar Mills1214b7e2020-06-04 10:11:30 -050078static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070079{
80 // Redfish MessageIds are in the form
81 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
82 // the right Message
83 std::vector<std::string> fields;
84 fields.reserve(4);
85 boost::split(fields, messageID, boost::is_any_of("."));
Gunnar Mills1214b7e2020-06-04 10:11:30 -050086 std::string& registryName = fields[0];
87 std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -070088
89 // Find the right registry and check it for the MessageKey
90 if (std::string(base::header.registryPrefix) == registryName)
91 {
92 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070093 messageKey, std::span<const MessageEntry>(base::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070094 }
95 if (std::string(openbmc::header.registryPrefix) == registryName)
96 {
97 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070098 messageKey, std::span<const MessageEntry>(openbmc::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070099 }
100 return nullptr;
101}
102} // namespace message_registries
103
James Feistf6150402019-01-08 10:36:20 -0800104namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700105
Ed Tanous168e20c2021-12-13 14:39:53 -0800106using GetManagedPropertyType =
107 boost::container::flat_map<std::string, dbus::utility::DbusVariantType>;
Andrew Geisslercb92c032018-08-17 07:56:14 -0700108
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500109inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700110{
Ed Tanousd4d25792020-09-29 15:15:03 -0700111 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
112 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
113 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
114 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700115 {
116 return "Critical";
117 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700118 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
119 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
120 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700121 {
122 return "OK";
123 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700124 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700125 {
126 return "Warning";
127 }
128 return "";
129}
130
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700131inline static int getJournalMetadata(sd_journal* journal,
132 const std::string_view& field,
133 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700134{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500135 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700136 size_t length = 0;
137 int ret = 0;
138 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800139 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
140 const void** dataVoid = reinterpret_cast<const void**>(&data);
141
142 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700143 if (ret < 0)
144 {
145 return ret;
146 }
Ed Tanous39e77502019-03-04 17:35:53 -0800147 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700148 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000149 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700150 return ret;
151}
152
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700153inline static int getJournalMetadata(sd_journal* journal,
154 const std::string_view& field,
155 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700156{
157 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800158 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700159 // Get the metadata from the requested field of the journal entry
160 ret = getJournalMetadata(journal, field, metadata);
161 if (ret < 0)
162 {
163 return ret;
164 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000165 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700166 return ret;
167}
168
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700169inline static bool getEntryTimestamp(sd_journal* journal,
170 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800171{
172 int ret = 0;
173 uint64_t timestamp = 0;
174 ret = sd_journal_get_realtime_usec(journal, &timestamp);
175 if (ret < 0)
176 {
177 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
178 << strerror(-ret);
179 return false;
180 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800181 entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500182 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800183}
Ed Tanous50b8a432022-02-03 16:29:50 -0800184
Ed Tanous67df0732021-10-26 11:23:56 -0700185static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
186 const crow::Request& req, uint64_t& skip)
187{
188 boost::urls::params_view::iterator it = req.urlView.params().find("$skip");
189 if (it != req.urlView.params().end())
190 {
191 std::from_chars_result r = std::from_chars(
192 (*it).value.data(), (*it).value.data() + (*it).value.size(), skip);
193 if (r.ec != std::errc())
194 {
195 messages::queryParameterValueTypeError(asyncResp->res, "", "$skip");
196 return false;
197 }
198 }
199 return true;
200}
201
202static constexpr const uint64_t maxEntriesPerPage = 1000;
203static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
204 const crow::Request& req, uint64_t& top)
205{
206 boost::urls::params_view::iterator it = req.urlView.params().find("$top");
207 if (it != req.urlView.params().end())
208 {
209 std::from_chars_result r = std::from_chars(
210 (*it).value.data(), (*it).value.data() + (*it).value.size(), top);
211 if (r.ec != std::errc())
212 {
213 messages::queryParameterValueTypeError(asyncResp->res, "", "$top");
214 return false;
215 }
216 if (top < 1U || top > maxEntriesPerPage)
217 {
218
219 messages::queryParameterOutOfRange(
220 asyncResp->res, std::to_string(top), "$top",
221 "1-" + std::to_string(maxEntriesPerPage));
222 return false;
223 }
224 }
225 return true;
226}
227
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700228inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
229 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700230{
231 int ret = 0;
232 static uint64_t prevTs = 0;
233 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700234 if (firstEntry)
235 {
236 prevTs = 0;
237 }
238
Jason M. Bills16428a12018-11-02 12:42:29 -0700239 // Get the entry timestamp
240 uint64_t curTs = 0;
241 ret = sd_journal_get_realtime_usec(journal, &curTs);
242 if (ret < 0)
243 {
244 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
245 << strerror(-ret);
246 return false;
247 }
248 // If the timestamp isn't unique, increment the index
249 if (curTs == prevTs)
250 {
251 index++;
252 }
253 else
254 {
255 // Otherwise, reset it
256 index = 0;
257 }
258 // Save the timestamp
259 prevTs = curTs;
260
261 entryID = std::to_string(curTs);
262 if (index > 0)
263 {
264 entryID += "_" + std::to_string(index);
265 }
266 return true;
267}
268
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500269static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700270 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700271{
Ed Tanous271584a2019-07-09 16:24:22 -0700272 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700273 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700274 if (firstEntry)
275 {
276 prevTs = 0;
277 }
278
Jason M. Bills95820182019-04-22 16:25:34 -0700279 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700280 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700281 std::tm timeStruct = {};
282 std::istringstream entryStream(logEntry);
283 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
284 {
285 curTs = std::mktime(&timeStruct);
286 }
287 // If the timestamp isn't unique, increment the index
288 if (curTs == prevTs)
289 {
290 index++;
291 }
292 else
293 {
294 // Otherwise, reset it
295 index = 0;
296 }
297 // Save the timestamp
298 prevTs = curTs;
299
300 entryID = std::to_string(curTs);
301 if (index > 0)
302 {
303 entryID += "_" + std::to_string(index);
304 }
305 return true;
306}
307
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700308inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800309 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
310 const std::string& entryID, uint64_t& timestamp,
311 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700312{
313 if (entryID.empty())
314 {
315 return false;
316 }
317 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800318 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700319
Ed Tanous81ce6092020-12-17 16:54:55 +0000320 auto underscorePos = tsStr.find('_');
Jason M. Bills16428a12018-11-02 12:42:29 -0700321 if (underscorePos != tsStr.npos)
322 {
323 // Timestamp has an index
324 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800325 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700326 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700327 auto [ptr, ec] = std::from_chars(
328 indexStr.data(), indexStr.data() + indexStr.size(), index);
329 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700330 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800331 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700332 return false;
333 }
334 }
335 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700336 auto [ptr, ec] =
337 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
338 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700339 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800340 messages::resourceMissingAtURI(asyncResp->res, entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700341 return false;
342 }
343 return true;
344}
345
Jason M. Bills95820182019-04-22 16:25:34 -0700346static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500347 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700348{
349 static const std::filesystem::path redfishLogDir = "/var/log";
350 static const std::string redfishLogFilename = "redfish";
351
352 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500353 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700354 std::filesystem::directory_iterator(redfishLogDir))
355 {
356 // If we find a redfish log file, save the path
357 std::string filename = dirEnt.path().filename();
358 if (boost::starts_with(filename, redfishLogFilename))
359 {
360 redfishLogFiles.emplace_back(redfishLogDir / filename);
361 }
362 }
363 // As the log files rotate, they are appended with a ".#" that is higher for
364 // the older logs. Since we don't expect more than 10 log files, we
365 // can just sort the list to get them in order from newest to oldest
366 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
367
368 return !redfishLogFiles.empty();
369}
370
zhanghch058d1b46d2021-04-01 11:18:24 +0800371inline void
372 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
373 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500374{
375 std::string dumpPath;
376 if (dumpType == "BMC")
377 {
378 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
379 }
380 else if (dumpType == "System")
381 {
382 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
383 }
384 else
385 {
386 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
387 messages::internalError(asyncResp->res);
388 return;
389 }
390
391 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -0800392 [asyncResp, dumpPath,
393 dumpType](const boost::system::error_code ec,
394 dbus::utility::ManagedObjectType& resp) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500395 if (ec)
396 {
397 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
398 messages::internalError(asyncResp->res);
399 return;
400 }
401
402 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
403 entriesArray = nlohmann::json::array();
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500404 std::string dumpEntryPath =
405 "/xyz/openbmc_project/dump/" +
406 std::string(boost::algorithm::to_lower_copy(dumpType)) +
407 "/entry/";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500408
409 for (auto& object : resp)
410 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500411 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500412 {
413 continue;
414 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800415 uint64_t timestamp = 0;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500416 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500417 std::string dumpStatus;
418 nlohmann::json thisEntry;
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000419
420 std::string entryID = object.first.filename();
421 if (entryID.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500422 {
423 continue;
424 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500425
426 for (auto& interfaceMap : object.second)
427 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500428 if (interfaceMap.first ==
429 "xyz.openbmc_project.Common.Progress")
430 {
431 for (auto& propertyMap : interfaceMap.second)
432 {
433 if (propertyMap.first == "Status")
434 {
435 auto status = std::get_if<std::string>(
436 &propertyMap.second);
437 if (status == nullptr)
438 {
439 messages::internalError(asyncResp->res);
440 break;
441 }
442 dumpStatus = *status;
443 }
444 }
445 }
446 else if (interfaceMap.first ==
447 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500448 {
449
450 for (auto& propertyMap : interfaceMap.second)
451 {
452 if (propertyMap.first == "Size")
453 {
454 auto sizePtr =
455 std::get_if<uint64_t>(&propertyMap.second);
456 if (sizePtr == nullptr)
457 {
458 messages::internalError(asyncResp->res);
459 break;
460 }
461 size = *sizePtr;
462 break;
463 }
464 }
465 }
466 else if (interfaceMap.first ==
467 "xyz.openbmc_project.Time.EpochTime")
468 {
469
470 for (auto& propertyMap : interfaceMap.second)
471 {
472 if (propertyMap.first == "Elapsed")
473 {
474 const uint64_t* usecsTimeStamp =
475 std::get_if<uint64_t>(&propertyMap.second);
476 if (usecsTimeStamp == nullptr)
477 {
478 messages::internalError(asyncResp->res);
479 break;
480 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800481 timestamp = (*usecsTimeStamp / 1000 / 1000);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500482 break;
483 }
484 }
485 }
486 }
487
George Liu0fda0f12021-11-16 10:06:17 +0800488 if (dumpStatus !=
489 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500490 !dumpStatus.empty())
491 {
492 // Dump status is not Complete, no need to enumerate
493 continue;
494 }
495
George Liu647b3cd2021-07-05 12:43:56 +0800496 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500497 thisEntry["@odata.id"] = dumpPath + entryID;
498 thisEntry["Id"] = entryID;
499 thisEntry["EntryType"] = "Event";
Nan Zhou1d8782e2021-11-29 22:23:18 -0800500 thisEntry["Created"] =
501 crow::utility::getDateTimeUint(timestamp);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500502 thisEntry["Name"] = dumpType + " Dump Entry";
503
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500504 thisEntry["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500505
506 if (dumpType == "BMC")
507 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500508 thisEntry["DiagnosticDataType"] = "Manager";
509 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500510 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
511 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512 }
513 else if (dumpType == "System")
514 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500515 thisEntry["DiagnosticDataType"] = "OEM";
516 thisEntry["OEMDiagnosticDataType"] = "System";
517 thisEntry["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500518 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
519 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500520 }
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500521 entriesArray.push_back(std::move(thisEntry));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500522 }
523 asyncResp->res.jsonValue["Members@odata.count"] =
524 entriesArray.size();
525 },
526 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
527 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
528}
529
zhanghch058d1b46d2021-04-01 11:18:24 +0800530inline void
531 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
532 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500533{
534 std::string dumpPath;
535 if (dumpType == "BMC")
536 {
537 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
538 }
539 else if (dumpType == "System")
540 {
541 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
542 }
543 else
544 {
545 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
546 messages::internalError(asyncResp->res);
547 return;
548 }
549
550 crow::connections::systemBus->async_method_call(
Ed Tanous711ac7a2021-12-20 09:34:41 -0800551 [asyncResp, entryID, dumpPath,
552 dumpType](const boost::system::error_code ec,
553 dbus::utility::ManagedObjectType& resp) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500554 if (ec)
555 {
556 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
557 messages::internalError(asyncResp->res);
558 return;
559 }
560
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500561 bool foundDumpEntry = false;
562 std::string dumpEntryPath =
563 "/xyz/openbmc_project/dump/" +
564 std::string(boost::algorithm::to_lower_copy(dumpType)) +
565 "/entry/";
566
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500567 for (auto& objectPath : resp)
568 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500569 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500570 {
571 continue;
572 }
573
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500574 foundDumpEntry = true;
Nan Zhou1d8782e2021-11-29 22:23:18 -0800575 uint64_t timestamp = 0;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500576 uint64_t size = 0;
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500577 std::string dumpStatus;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500578
579 for (auto& interfaceMap : objectPath.second)
580 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500581 if (interfaceMap.first ==
582 "xyz.openbmc_project.Common.Progress")
583 {
584 for (auto& propertyMap : interfaceMap.second)
585 {
586 if (propertyMap.first == "Status")
587 {
588 auto status = std::get_if<std::string>(
589 &propertyMap.second);
590 if (status == nullptr)
591 {
592 messages::internalError(asyncResp->res);
593 break;
594 }
595 dumpStatus = *status;
596 }
597 }
598 }
599 else if (interfaceMap.first ==
600 "xyz.openbmc_project.Dump.Entry")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500601 {
602 for (auto& propertyMap : interfaceMap.second)
603 {
604 if (propertyMap.first == "Size")
605 {
606 auto sizePtr =
607 std::get_if<uint64_t>(&propertyMap.second);
608 if (sizePtr == nullptr)
609 {
610 messages::internalError(asyncResp->res);
611 break;
612 }
613 size = *sizePtr;
614 break;
615 }
616 }
617 }
618 else if (interfaceMap.first ==
619 "xyz.openbmc_project.Time.EpochTime")
620 {
621 for (auto& propertyMap : interfaceMap.second)
622 {
623 if (propertyMap.first == "Elapsed")
624 {
625 const uint64_t* usecsTimeStamp =
626 std::get_if<uint64_t>(&propertyMap.second);
627 if (usecsTimeStamp == nullptr)
628 {
629 messages::internalError(asyncResp->res);
630 break;
631 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800632 timestamp = *usecsTimeStamp / 1000 / 1000;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500633 break;
634 }
635 }
636 }
637 }
638
George Liu0fda0f12021-11-16 10:06:17 +0800639 if (dumpStatus !=
640 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500641 !dumpStatus.empty())
642 {
643 // Dump status is not Complete
644 // return not found until status is changed to Completed
645 messages::resourceNotFound(asyncResp->res,
646 dumpType + " dump", entryID);
647 return;
648 }
649
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500650 asyncResp->res.jsonValue["@odata.type"] =
George Liu647b3cd2021-07-05 12:43:56 +0800651 "#LogEntry.v1_8_0.LogEntry";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500652 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
653 asyncResp->res.jsonValue["Id"] = entryID;
654 asyncResp->res.jsonValue["EntryType"] = "Event";
655 asyncResp->res.jsonValue["Created"] =
Nan Zhou1d8782e2021-11-29 22:23:18 -0800656 crow::utility::getDateTimeUint(timestamp);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500657 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
658
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500659 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500660
661 if (dumpType == "BMC")
662 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500663 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
664 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500665 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
666 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500667 }
668 else if (dumpType == "System")
669 {
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500670 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
671 asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500672 "System";
Asmitha Karunanithid337bb72020-09-21 10:34:02 -0500673 asyncResp->res.jsonValue["AdditionalDataURI"] =
Abhishek Patelde8d94a2021-05-13 22:57:36 -0500674 "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
675 entryID + "/attachment";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500676 }
677 }
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500678 if (foundDumpEntry == false)
679 {
680 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
681 messages::internalError(asyncResp->res);
682 return;
683 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500684 },
685 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
686 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
687}
688
zhanghch058d1b46d2021-04-01 11:18:24 +0800689inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800690 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500691 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500692{
George Liu3de8d8b2021-03-22 17:49:39 +0800693 auto respHandler = [asyncResp,
694 entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500695 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
696 if (ec)
697 {
George Liu3de8d8b2021-03-22 17:49:39 +0800698 if (ec.value() == EBADR)
699 {
700 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
701 return;
702 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500703 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
704 << ec;
705 messages::internalError(asyncResp->res);
706 return;
707 }
708 };
709 crow::connections::systemBus->async_method_call(
710 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500711 "/xyz/openbmc_project/dump/" +
712 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
713 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500714 "xyz.openbmc_project.Object.Delete", "Delete");
715}
716
zhanghch058d1b46d2021-04-01 11:18:24 +0800717inline void
Ed Tanous98be3e32021-09-16 15:05:36 -0700718 createDumpTaskCallback(task::Payload&& payload,
zhanghch058d1b46d2021-04-01 11:18:24 +0800719 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
720 const uint32_t& dumpId, const std::string& dumpPath,
721 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500722{
723 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500724 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500725 boost::system::error_code err, sdbusplus::message::message& m,
726 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000727 if (err)
728 {
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500729 BMCWEB_LOG_ERROR << "Error in creating a dump";
730 taskData->state = "Cancelled";
731 return task::completed;
Ed Tanouscb13a392020-07-25 19:02:03 +0000732 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500733 std::vector<std::pair<
Ed Tanous168e20c2021-12-13 14:39:53 -0800734 std::string, std::vector<std::pair<
735 std::string, dbus::utility::DbusVariantType>>>>
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500736 interfacesList;
737
738 sdbusplus::message::object_path objPath;
739
740 m.read(objPath, interfacesList);
741
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500742 if (objPath.str ==
743 "/xyz/openbmc_project/dump/" +
744 std::string(boost::algorithm::to_lower_copy(dumpType)) +
745 "/entry/" + std::to_string(dumpId))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500746 {
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500747 nlohmann::json retMessage = messages::success();
748 taskData->messages.emplace_back(retMessage);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500749
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500750 std::string headerLoc =
751 "Location: " + dumpPath + std::to_string(dumpId);
752 taskData->payload->httpHeaders.emplace_back(
753 std::move(headerLoc));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500754
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500755 taskData->state = "Completed";
756 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500757 }
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500758 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500759 },
760 "type='signal',interface='org.freedesktop.DBus."
761 "ObjectManager',"
762 "member='InterfacesAdded', "
763 "path='/xyz/openbmc_project/dump'");
764
765 task->startTimer(std::chrono::minutes(3));
766 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -0700767 task->payload.emplace(std::move(payload));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500768}
769
zhanghch058d1b46d2021-04-01 11:18:24 +0800770inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
771 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500772{
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500773
774 std::string dumpPath;
775 if (dumpType == "BMC")
776 {
777 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
778 }
779 else if (dumpType == "System")
780 {
781 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
782 }
783 else
784 {
785 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
786 messages::internalError(asyncResp->res);
787 return;
788 }
789
790 std::optional<std::string> diagnosticDataType;
791 std::optional<std::string> oemDiagnosticDataType;
792
793 if (!redfish::json_util::readJson(
794 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
795 "OEMDiagnosticDataType", oemDiagnosticDataType))
796 {
797 return;
798 }
799
800 if (dumpType == "System")
801 {
802 if (!oemDiagnosticDataType || !diagnosticDataType)
803 {
804 BMCWEB_LOG_ERROR << "CreateDump action parameter "
805 "'DiagnosticDataType'/"
806 "'OEMDiagnosticDataType' value not found!";
807 messages::actionParameterMissing(
808 asyncResp->res, "CollectDiagnosticData",
809 "DiagnosticDataType & OEMDiagnosticDataType");
810 return;
811 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700812 if ((*oemDiagnosticDataType != "System") ||
813 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500814 {
815 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
816 messages::invalidObject(asyncResp->res,
817 "System Dump creation parameters");
818 return;
819 }
820 }
821 else if (dumpType == "BMC")
822 {
823 if (!diagnosticDataType)
824 {
George Liu0fda0f12021-11-16 10:06:17 +0800825 BMCWEB_LOG_ERROR
826 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500827 messages::actionParameterMissing(
828 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
829 return;
830 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700831 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500832 {
833 BMCWEB_LOG_ERROR
834 << "Wrong parameter value passed for 'DiagnosticDataType'";
835 messages::invalidObject(asyncResp->res,
836 "BMC Dump creation parameters");
837 return;
838 }
839 }
840
841 crow::connections::systemBus->async_method_call(
Ed Tanous98be3e32021-09-16 15:05:36 -0700842 [asyncResp, payload(task::Payload(req)), dumpPath,
843 dumpType](const boost::system::error_code ec,
844 const uint32_t& dumpId) mutable {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500845 if (ec)
846 {
847 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
848 messages::internalError(asyncResp->res);
849 return;
850 }
851 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
852
Ed Tanous98be3e32021-09-16 15:05:36 -0700853 createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
854 dumpPath, dumpType);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500855 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500856 "xyz.openbmc_project.Dump.Manager",
857 "/xyz/openbmc_project/dump/" +
858 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500859 "xyz.openbmc_project.Dump.Create", "CreateDump");
860}
861
zhanghch058d1b46d2021-04-01 11:18:24 +0800862inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
863 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500864{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500865 std::string dumpTypeLowerCopy =
866 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800867
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500868 crow::connections::systemBus->async_method_call(
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500869 [asyncResp, dumpType](const boost::system::error_code ec,
870 const std::vector<std::string>& subTreePaths) {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500871 if (ec)
872 {
873 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
874 messages::internalError(asyncResp->res);
875 return;
876 }
877
878 for (const std::string& path : subTreePaths)
879 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000880 sdbusplus::message::object_path objPath(path);
881 std::string logID = objPath.filename();
882 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500883 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000884 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500885 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000886 deleteDumpEntry(asyncResp, logID, dumpType);
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500887 }
888 },
889 "xyz.openbmc_project.ObjectMapper",
890 "/xyz/openbmc_project/object_mapper",
891 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500892 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
893 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
894 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500895}
896
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700897inline static void parseCrashdumpParameters(
Ed Tanous168e20c2021-12-13 14:39:53 -0800898 const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
899 params,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500900 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700901{
902 for (auto property : params)
903 {
904 if (property.first == "Timestamp")
905 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500906 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500907 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700908 if (value != nullptr)
909 {
910 timestamp = *value;
911 }
912 }
913 else if (property.first == "Filename")
914 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500915 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500916 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700917 if (value != nullptr)
918 {
919 filename = *value;
920 }
921 }
922 else if (property.first == "Log")
923 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500924 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500925 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700926 if (value != nullptr)
927 {
928 logfile = *value;
929 }
930 }
931 }
932}
933
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500934constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700935inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700936{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800937 /**
938 * Functions triggers appropriate requests on DBus
939 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700940 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700941 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700942 .methods(boost::beast::http::verb::get)(
943 [](const crow::Request&,
944 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
945
946 {
947 // Collections don't include the static data added by SubRoute
948 // because it has a duplicate entry for members
949 asyncResp->res.jsonValue["@odata.type"] =
950 "#LogServiceCollection.LogServiceCollection";
951 asyncResp->res.jsonValue["@odata.id"] =
952 "/redfish/v1/Systems/system/LogServices";
953 asyncResp->res.jsonValue["Name"] =
954 "System Log Services Collection";
955 asyncResp->res.jsonValue["Description"] =
956 "Collection of LogServices for this Computer System";
957 nlohmann::json& logServiceArray =
958 asyncResp->res.jsonValue["Members"];
959 logServiceArray = nlohmann::json::array();
960 logServiceArray.push_back(
961 {{"@odata.id",
962 "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500963#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700964 logServiceArray.push_back(
965 {{"@odata.id",
966 "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600967#endif
968
Jason M. Billsd53dd412019-02-12 17:16:22 -0800969#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700970 logServiceArray.push_back(
971 {{"@odata.id",
972 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800973#endif
Spencer Kub7028eb2021-10-26 15:27:35 +0800974
975#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
976 logServiceArray.push_back(
977 {{"@odata.id",
978 "/redfish/v1/Systems/system/LogServices/HostLogger"}});
979#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700980 asyncResp->res.jsonValue["Members@odata.count"] =
981 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800982
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700983 crow::connections::systemBus->async_method_call(
984 [asyncResp](const boost::system::error_code ec,
985 const std::vector<std::string>& subtreePath) {
986 if (ec)
987 {
988 BMCWEB_LOG_ERROR << ec;
989 return;
990 }
ZhikuiRena3316fc2020-01-29 14:58:08 -0800991
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700992 for (auto& pathStr : subtreePath)
993 {
994 if (pathStr.find("PostCode") != std::string::npos)
995 {
996 nlohmann::json& logServiceArrayLocal =
997 asyncResp->res.jsonValue["Members"];
998 logServiceArrayLocal.push_back(
George Liu0fda0f12021-11-16 10:06:17 +0800999 {{"@odata.id",
1000 "/redfish/v1/Systems/system/LogServices/PostCodes"}});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001001 asyncResp->res
1002 .jsonValue["Members@odata.count"] =
1003 logServiceArrayLocal.size();
1004 return;
1005 }
1006 }
1007 },
1008 "xyz.openbmc_project.ObjectMapper",
1009 "/xyz/openbmc_project/object_mapper",
1010 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1011 0, std::array<const char*, 1>{postCodeIface});
1012 });
1013}
1014
1015inline void requestRoutesEventLogService(App& app)
1016{
1017 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001018 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001019 .methods(
1020 boost::beast::http::verb::
1021 get)([](const crow::Request&,
1022 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1023 asyncResp->res.jsonValue["@odata.id"] =
1024 "/redfish/v1/Systems/system/LogServices/EventLog";
1025 asyncResp->res.jsonValue["@odata.type"] =
1026 "#LogService.v1_1_0.LogService";
1027 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1028 asyncResp->res.jsonValue["Description"] =
1029 "System Event Log Service";
1030 asyncResp->res.jsonValue["Id"] = "EventLog";
1031 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301032
1033 std::pair<std::string, std::string> redfishDateTimeOffset =
1034 crow::utility::getDateTimeOffsetNow();
1035
1036 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1037 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1038 redfishDateTimeOffset.second;
1039
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001040 asyncResp->res.jsonValue["Entries"] = {
1041 {"@odata.id",
1042 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1043 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1044
George Liu0fda0f12021-11-16 10:06:17 +08001045 {"target",
1046 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001047 });
1048}
1049
1050inline void requestRoutesJournalEventLogClear(App& app)
1051{
1052 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1053 "LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001054 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001055 .methods(boost::beast::http::verb::post)(
1056 [](const crow::Request&,
1057 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1058 // Clear the EventLog by deleting the log files
1059 std::vector<std::filesystem::path> redfishLogFiles;
1060 if (getRedfishLogFiles(redfishLogFiles))
ZhikuiRena3316fc2020-01-29 14:58:08 -08001061 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001062 for (const std::filesystem::path& file : redfishLogFiles)
ZhikuiRena3316fc2020-01-29 14:58:08 -08001063 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001064 std::error_code ec;
1065 std::filesystem::remove(file, ec);
ZhikuiRena3316fc2020-01-29 14:58:08 -08001066 }
1067 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001068
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001069 // Reload rsyslog so it knows to start new log files
1070 crow::connections::systemBus->async_method_call(
1071 [asyncResp](const boost::system::error_code ec) {
1072 if (ec)
1073 {
1074 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1075 << ec;
1076 messages::internalError(asyncResp->res);
1077 return;
1078 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001079
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001080 messages::success(asyncResp->res);
1081 },
1082 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1083 "org.freedesktop.systemd1.Manager", "ReloadUnit",
1084 "rsyslog.service", "replace");
1085 });
1086}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001087
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001088static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001089 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001090 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001091{
Jason M. Bills95820182019-04-22 16:25:34 -07001092 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001093 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001094 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001095 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001096 {
1097 return 1;
1098 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001099 std::string timestamp = logEntry.substr(0, space);
1100 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001101 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001102 if (entryStart == std::string::npos)
1103 {
1104 return 1;
1105 }
1106 std::string_view entry(logEntry);
1107 entry.remove_prefix(entryStart);
1108 // Use split to separate the entry into its fields
1109 std::vector<std::string> logEntryFields;
1110 boost::split(logEntryFields, entry, boost::is_any_of(","),
1111 boost::token_compress_on);
1112 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001113 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001114 {
1115 return 1;
1116 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001117 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001118
Jason M. Bills4851d452019-03-28 11:27:48 -07001119 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001120 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001121 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001122
Jason M. Bills4851d452019-03-28 11:27:48 -07001123 std::string msg;
1124 std::string severity;
1125 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001126 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001127 msg = message->message;
1128 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001129 }
1130
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001131 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001132 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001133 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001134 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001135 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001136 // If the first string is empty, assume there are no MessageArgs
1137 std::size_t messageArgsSize = 0;
1138 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001139 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001140 messageArgsSize = logEntryFields.size() - 1;
1141 }
1142
Ed Tanous23a21a12020-07-25 04:45:05 +00001143 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001144
1145 // Fill the MessageArgs into the Message
1146 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001147 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001148 {
1149 std::string argStr = "%" + std::to_string(++i);
1150 size_t argPos = msg.find(argStr);
1151 if (argPos != std::string::npos)
1152 {
1153 msg.replace(argPos, argStr.length(), messageArg);
1154 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001155 }
1156 }
1157
Jason M. Bills95820182019-04-22 16:25:34 -07001158 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1159 // format which matches the Redfish format except for the fractional seconds
1160 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001161 std::size_t dot = timestamp.find_first_of('.');
1162 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001163 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001164 {
Jason M. Bills95820182019-04-22 16:25:34 -07001165 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001166 }
1167
1168 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001169 logEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08001170 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001171 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001172 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001173 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001174 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001175 {"Id", logEntryID},
1176 {"Message", std::move(msg)},
1177 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001178 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001179 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001180 {"Severity", std::move(severity)},
1181 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001182 return 0;
1183}
1184
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001185inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001186{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001187 BMCWEB_ROUTE(app,
1188 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001189 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001190 .methods(boost::beast::http::verb::get)(
1191 [](const crow::Request& req,
1192 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1193 uint64_t skip = 0;
1194 uint64_t top = maxEntriesPerPage; // Show max entries by default
1195 if (!getSkipParam(asyncResp, req, skip))
Jason M. Bills95820182019-04-22 16:25:34 -07001196 {
Jason M. Bills95820182019-04-22 16:25:34 -07001197 return;
1198 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001199 if (!getTopParam(asyncResp, req, top))
Jason M. Bills897967d2019-07-29 17:05:30 -07001200 {
Jason M. Bills897967d2019-07-29 17:05:30 -07001201 return;
1202 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001203 // Collections don't include the static data added by SubRoute
1204 // because it has a duplicate entry for members
1205 asyncResp->res.jsonValue["@odata.type"] =
1206 "#LogEntryCollection.LogEntryCollection";
1207 asyncResp->res.jsonValue["@odata.id"] =
1208 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1209 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1210 asyncResp->res.jsonValue["Description"] =
1211 "Collection of System Event Log Entries";
Jason M. Bills897967d2019-07-29 17:05:30 -07001212
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001213 nlohmann::json& logEntryArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001214 asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001215 logEntryArray = nlohmann::json::array();
1216 // Go through the log files and create a unique ID for each
1217 // entry
1218 std::vector<std::filesystem::path> redfishLogFiles;
1219 getRedfishLogFiles(redfishLogFiles);
1220 uint64_t entryCount = 0;
1221 std::string logEntry;
1222
1223 // Oldest logs are in the last file, so start there and loop
1224 // backwards
1225 for (auto it = redfishLogFiles.rbegin();
1226 it < redfishLogFiles.rend(); it++)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001227 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001228 std::ifstream logStream(*it);
1229 if (!logStream.is_open())
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001230 {
1231 continue;
1232 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001233
1234 // Reset the unique ID on the first entry
1235 bool firstEntry = true;
1236 while (std::getline(logStream, logEntry))
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001237 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001238 entryCount++;
1239 // Handle paging using skip (number of entries to skip
1240 // from the start) and top (number of entries to
1241 // display)
1242 if (entryCount <= skip || entryCount > skip + top)
George Liuebd45902020-08-26 14:21:10 +08001243 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001244 continue;
George Liuebd45902020-08-26 14:21:10 +08001245 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001246
1247 std::string idStr;
1248 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
George Liuebd45902020-08-26 14:21:10 +08001249 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001250 continue;
George Liuebd45902020-08-26 14:21:10 +08001251 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001252
1253 if (firstEntry)
1254 {
1255 firstEntry = false;
1256 }
1257
1258 logEntryArray.push_back({});
1259 nlohmann::json& bmcLogEntry = logEntryArray.back();
1260 if (fillEventLogEntryJson(idStr, logEntry,
1261 bmcLogEntry) != 0)
Xiaochao Ma75710de2021-01-21 17:56:02 +08001262 {
1263 messages::internalError(asyncResp->res);
1264 return;
1265 }
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001266 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001267 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001268 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1269 if (skip + top < entryCount)
Ed Tanous271584a2019-07-09 16:24:22 -07001270 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001271 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001272 "/redfish/v1/Systems/system/LogServices/EventLog/"
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001273 "Entries?$skip=" +
1274 std::to_string(skip + top);
Adriana Kobylakf86bb902021-01-11 11:11:05 -06001275 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001276 });
1277}
Chicago Duan336e96c2019-07-15 14:22:08 +08001278
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001279inline void requestRoutesJournalEventLogEntry(App& app)
1280{
1281 BMCWEB_ROUTE(
1282 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001283 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001284 .methods(boost::beast::http::verb::get)(
1285 [](const crow::Request&,
1286 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1287 const std::string& param) {
1288 const std::string& targetID = param;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001289
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001290 // Go through the log files and check the unique ID for each
1291 // entry to find the target entry
1292 std::vector<std::filesystem::path> redfishLogFiles;
1293 getRedfishLogFiles(redfishLogFiles);
1294 std::string logEntry;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001295
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001296 // Oldest logs are in the last file, so start there and loop
1297 // backwards
1298 for (auto it = redfishLogFiles.rbegin();
1299 it < redfishLogFiles.rend(); it++)
1300 {
1301 std::ifstream logStream(*it);
1302 if (!logStream.is_open())
1303 {
1304 continue;
1305 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001306
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001307 // Reset the unique ID on the first entry
1308 bool firstEntry = true;
1309 while (std::getline(logStream, logEntry))
1310 {
1311 std::string idStr;
1312 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1313 {
1314 continue;
1315 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001316
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001317 if (firstEntry)
1318 {
1319 firstEntry = false;
1320 }
Xiaochao Ma75710de2021-01-21 17:56:02 +08001321
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001322 if (idStr == targetID)
1323 {
1324 if (fillEventLogEntryJson(
1325 idStr, logEntry,
1326 asyncResp->res.jsonValue) != 0)
1327 {
1328 messages::internalError(asyncResp->res);
1329 return;
1330 }
1331 return;
1332 }
1333 }
1334 }
1335 // Requested ID was not found
1336 messages::resourceMissingAtURI(asyncResp->res, targetID);
1337 });
1338}
1339
1340inline void requestRoutesDBusEventLogEntryCollection(App& app)
1341{
1342 BMCWEB_ROUTE(app,
1343 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001344 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001345 .methods(
1346 boost::beast::http::verb::
1347 get)([](const crow::Request&,
1348 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1349 // Collections don't include the static data added by SubRoute
1350 // because it has a duplicate entry for members
1351 asyncResp->res.jsonValue["@odata.type"] =
1352 "#LogEntryCollection.LogEntryCollection";
1353 asyncResp->res.jsonValue["@odata.id"] =
1354 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1355 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1356 asyncResp->res.jsonValue["Description"] =
1357 "Collection of System Event Log Entries";
1358
1359 // DBus implementation of EventLog/Entries
1360 // Make call to Logging Service to find all log entry objects
Xiaochao Ma75710de2021-01-21 17:56:02 +08001361 crow::connections::systemBus->async_method_call(
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001362 [asyncResp](const boost::system::error_code ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001363 const dbus::utility::ManagedObjectType& resp) {
Xiaochao Ma75710de2021-01-21 17:56:02 +08001364 if (ec)
1365 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001366 // TODO Handle for specific error code
1367 BMCWEB_LOG_ERROR
1368 << "getLogEntriesIfaceData resp_handler got error "
1369 << ec;
Xiaochao Ma75710de2021-01-21 17:56:02 +08001370 messages::internalError(asyncResp->res);
1371 return;
1372 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001373 nlohmann::json& entriesArray =
1374 asyncResp->res.jsonValue["Members"];
1375 entriesArray = nlohmann::json::array();
1376 for (auto& objectPath : resp)
1377 {
Ed Tanous914e2d52022-01-07 11:38:34 -08001378 const uint32_t* id = nullptr;
Ed Tanousc419c752022-01-26 12:19:54 -08001379 const uint64_t* timestamp = nullptr;
1380 const uint64_t* updateTimestamp = nullptr;
Ed Tanous914e2d52022-01-07 11:38:34 -08001381 const std::string* severity = nullptr;
1382 const std::string* message = nullptr;
1383 const std::string* filePath = nullptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001384 bool resolved = false;
1385 for (auto& interfaceMap : objectPath.second)
1386 {
1387 if (interfaceMap.first ==
1388 "xyz.openbmc_project.Logging.Entry")
1389 {
1390 for (auto& propertyMap : interfaceMap.second)
1391 {
1392 if (propertyMap.first == "Id")
1393 {
1394 id = std::get_if<uint32_t>(
1395 &propertyMap.second);
1396 }
1397 else if (propertyMap.first == "Timestamp")
1398 {
Ed Tanousc419c752022-01-26 12:19:54 -08001399 timestamp = std::get_if<uint64_t>(
1400 &propertyMap.second);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001401 }
1402 else if (propertyMap.first ==
1403 "UpdateTimestamp")
1404 {
Ed Tanousc419c752022-01-26 12:19:54 -08001405 updateTimestamp = std::get_if<uint64_t>(
1406 &propertyMap.second);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001407 }
1408 else if (propertyMap.first == "Severity")
1409 {
1410 severity = std::get_if<std::string>(
1411 &propertyMap.second);
1412 }
1413 else if (propertyMap.first == "Message")
1414 {
1415 message = std::get_if<std::string>(
1416 &propertyMap.second);
1417 }
1418 else if (propertyMap.first == "Resolved")
1419 {
Ed Tanous914e2d52022-01-07 11:38:34 -08001420 const bool* resolveptr =
1421 std::get_if<bool>(
1422 &propertyMap.second);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001423 if (resolveptr == nullptr)
1424 {
1425 messages::internalError(
1426 asyncResp->res);
1427 return;
1428 }
1429 resolved = *resolveptr;
1430 }
1431 }
1432 if (id == nullptr || message == nullptr ||
1433 severity == nullptr)
1434 {
1435 messages::internalError(asyncResp->res);
1436 return;
1437 }
1438 }
1439 else if (interfaceMap.first ==
1440 "xyz.openbmc_project.Common.FilePath")
1441 {
1442 for (auto& propertyMap : interfaceMap.second)
1443 {
1444 if (propertyMap.first == "Path")
1445 {
1446 filePath = std::get_if<std::string>(
1447 &propertyMap.second);
1448 }
1449 }
1450 }
1451 }
1452 // Object path without the
1453 // xyz.openbmc_project.Logging.Entry interface, ignore
1454 // and continue.
1455 if (id == nullptr || message == nullptr ||
Ed Tanousc419c752022-01-26 12:19:54 -08001456 severity == nullptr || timestamp == nullptr ||
1457 updateTimestamp == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001458 {
1459 continue;
1460 }
1461 entriesArray.push_back({});
1462 nlohmann::json& thisEntry = entriesArray.back();
1463 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1464 thisEntry["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001465 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001466 std::to_string(*id);
1467 thisEntry["Name"] = "System Event Log Entry";
1468 thisEntry["Id"] = std::to_string(*id);
1469 thisEntry["Message"] = *message;
1470 thisEntry["Resolved"] = resolved;
1471 thisEntry["EntryType"] = "Event";
1472 thisEntry["Severity"] =
1473 translateSeverityDbusToRedfish(*severity);
1474 thisEntry["Created"] =
Ed Tanousc419c752022-01-26 12:19:54 -08001475 crow::utility::getDateTimeUintMs(*timestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001476 thisEntry["Modified"] =
Ed Tanousc419c752022-01-26 12:19:54 -08001477 crow::utility::getDateTimeUintMs(*updateTimestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001478 if (filePath != nullptr)
1479 {
1480 thisEntry["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001481 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001482 std::to_string(*id) + "/attachment";
1483 }
1484 }
1485 std::sort(entriesArray.begin(), entriesArray.end(),
1486 [](const nlohmann::json& left,
1487 const nlohmann::json& right) {
1488 return (left["Id"] <= right["Id"]);
1489 });
1490 asyncResp->res.jsonValue["Members@odata.count"] =
1491 entriesArray.size();
Xiaochao Ma75710de2021-01-21 17:56:02 +08001492 },
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001493 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1494 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1495 });
1496}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001497
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001498inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001499{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001500 BMCWEB_ROUTE(
1501 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001502 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001503 .methods(boost::beast::http::verb::get)(
1504 [](const crow::Request&,
1505 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1506 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001507
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001508 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001509 std::string entryID = param;
1510 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001511
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001512 // DBus implementation of EventLog/Entries
1513 // Make call to Logging Service to find all log entry objects
1514 crow::connections::systemBus->async_method_call(
1515 [asyncResp, entryID](const boost::system::error_code ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001516 const GetManagedPropertyType& resp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001517 if (ec.value() == EBADR)
1518 {
1519 messages::resourceNotFound(
1520 asyncResp->res, "EventLogEntry", entryID);
1521 return;
1522 }
1523 if (ec)
1524 {
George Liu0fda0f12021-11-16 10:06:17 +08001525 BMCWEB_LOG_ERROR
1526 << "EventLogEntry (DBus) resp_handler got error "
1527 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001528 messages::internalError(asyncResp->res);
1529 return;
1530 }
Ed Tanous914e2d52022-01-07 11:38:34 -08001531 const uint32_t* id = nullptr;
Ed Tanousc419c752022-01-26 12:19:54 -08001532 const uint64_t* timestamp = nullptr;
1533 const uint64_t* updateTimestamp = nullptr;
Ed Tanous914e2d52022-01-07 11:38:34 -08001534 const std::string* severity = nullptr;
1535 const std::string* message = nullptr;
1536 const std::string* filePath = nullptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001537 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001538
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001539 for (auto& propertyMap : resp)
1540 {
1541 if (propertyMap.first == "Id")
1542 {
1543 id = std::get_if<uint32_t>(&propertyMap.second);
1544 }
1545 else if (propertyMap.first == "Timestamp")
1546 {
Ed Tanousc419c752022-01-26 12:19:54 -08001547 timestamp =
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001548 std::get_if<uint64_t>(&propertyMap.second);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001549 }
1550 else if (propertyMap.first == "UpdateTimestamp")
1551 {
Ed Tanousc419c752022-01-26 12:19:54 -08001552 updateTimestamp =
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001553 std::get_if<uint64_t>(&propertyMap.second);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001554 }
1555 else if (propertyMap.first == "Severity")
1556 {
1557 severity = std::get_if<std::string>(
1558 &propertyMap.second);
1559 }
1560 else if (propertyMap.first == "Message")
1561 {
1562 message = std::get_if<std::string>(
1563 &propertyMap.second);
1564 }
1565 else if (propertyMap.first == "Resolved")
1566 {
Ed Tanous914e2d52022-01-07 11:38:34 -08001567 const bool* resolveptr =
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001568 std::get_if<bool>(&propertyMap.second);
1569 if (resolveptr == nullptr)
1570 {
1571 messages::internalError(asyncResp->res);
1572 return;
1573 }
1574 resolved = *resolveptr;
1575 }
1576 else if (propertyMap.first == "Path")
1577 {
1578 filePath = std::get_if<std::string>(
1579 &propertyMap.second);
1580 }
1581 }
1582 if (id == nullptr || message == nullptr ||
Ed Tanousc419c752022-01-26 12:19:54 -08001583 severity == nullptr || timestamp == nullptr ||
1584 updateTimestamp == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001585 {
1586 messages::internalError(asyncResp->res);
1587 return;
1588 }
1589 asyncResp->res.jsonValue["@odata.type"] =
1590 "#LogEntry.v1_8_0.LogEntry";
1591 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08001592 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001593 std::to_string(*id);
1594 asyncResp->res.jsonValue["Name"] =
1595 "System Event Log Entry";
1596 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1597 asyncResp->res.jsonValue["Message"] = *message;
1598 asyncResp->res.jsonValue["Resolved"] = resolved;
1599 asyncResp->res.jsonValue["EntryType"] = "Event";
1600 asyncResp->res.jsonValue["Severity"] =
1601 translateSeverityDbusToRedfish(*severity);
1602 asyncResp->res.jsonValue["Created"] =
Ed Tanousc419c752022-01-26 12:19:54 -08001603 crow::utility::getDateTimeUintMs(*timestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001604 asyncResp->res.jsonValue["Modified"] =
Ed Tanousc419c752022-01-26 12:19:54 -08001605 crow::utility::getDateTimeUintMs(*updateTimestamp);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001606 if (filePath != nullptr)
1607 {
1608 asyncResp->res.jsonValue["AdditionalDataURI"] =
George Liu0fda0f12021-11-16 10:06:17 +08001609 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001610 std::to_string(*id);
1611 }
1612 },
1613 "xyz.openbmc_project.Logging",
1614 "/xyz/openbmc_project/logging/entry/" + entryID,
1615 "org.freedesktop.DBus.Properties", "GetAll", "");
1616 });
1617
1618 BMCWEB_ROUTE(
1619 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001620 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001621 .methods(boost::beast::http::verb::patch)(
1622 [](const crow::Request& req,
1623 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1624 const std::string& entryId) {
1625 std::optional<bool> resolved;
1626
1627 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1628 resolved))
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001629 {
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001630 return;
1631 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001632 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001633
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001634 crow::connections::systemBus->async_method_call(
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001635 [asyncResp, entryId](const boost::system::error_code ec) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001636 if (ec)
1637 {
1638 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1639 messages::internalError(asyncResp->res);
1640 return;
1641 }
1642 },
1643 "xyz.openbmc_project.Logging",
1644 "/xyz/openbmc_project/logging/entry/" + entryId,
1645 "org.freedesktop.DBus.Properties", "Set",
1646 "xyz.openbmc_project.Logging.Entry", "Resolved",
Ed Tanous168e20c2021-12-13 14:39:53 -08001647 dbus::utility::DbusVariantType(*resolved));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001648 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001649
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001650 BMCWEB_ROUTE(
1651 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001652 .privileges(redfish::privileges::deleteLogEntry)
1653
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001654 .methods(boost::beast::http::verb::delete_)(
1655 [](const crow::Request&,
1656 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1657 const std::string& param)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001658
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001659 {
1660 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001661
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001662 std::string entryID = param;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001663
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001664 dbus::utility::escapePathForDbus(entryID);
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001665
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001666 // Process response from Logging service.
1667 auto respHandler = [asyncResp, entryID](
1668 const boost::system::error_code ec) {
1669 BMCWEB_LOG_DEBUG
1670 << "EventLogEntry (DBus) doDelete callback: Done";
1671 if (ec)
1672 {
1673 if (ec.value() == EBADR)
1674 {
1675 messages::resourceNotFound(asyncResp->res,
1676 "LogEntry", entryID);
1677 return;
1678 }
1679 // TODO Handle for specific error code
George Liu0fda0f12021-11-16 10:06:17 +08001680 BMCWEB_LOG_ERROR
1681 << "EventLogEntry (DBus) doDelete respHandler got error "
1682 << ec;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001683 asyncResp->res.result(
1684 boost::beast::http::status::internal_server_error);
1685 return;
1686 }
1687
1688 asyncResp->res.result(boost::beast::http::status::ok);
1689 };
1690
1691 // Make call to Logging service to request Delete Log
1692 crow::connections::systemBus->async_method_call(
1693 respHandler, "xyz.openbmc_project.Logging",
1694 "/xyz/openbmc_project/logging/entry/" + entryID,
1695 "xyz.openbmc_project.Object.Delete", "Delete");
1696 });
1697}
1698
1699inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001700{
George Liu0fda0f12021-11-16 10:06:17 +08001701 BMCWEB_ROUTE(
1702 app,
1703 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001704 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001705 .methods(boost::beast::http::verb::get)(
1706 [](const crow::Request& req,
1707 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1708 const std::string& param)
Ed Tanous1da66f72018-07-27 16:13:37 -07001709
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001710 {
George Liu647b3cd2021-07-05 12:43:56 +08001711 if (!http_helpers::isOctetAccepted(
1712 req.getHeaderValue("Accept")))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001713 {
1714 asyncResp->res.result(
1715 boost::beast::http::status::bad_request);
1716 return;
1717 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001718
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001719 std::string entryID = param;
1720 dbus::utility::escapePathForDbus(entryID);
1721
1722 crow::connections::systemBus->async_method_call(
1723 [asyncResp,
1724 entryID](const boost::system::error_code ec,
1725 const sdbusplus::message::unix_fd& unixfd) {
1726 if (ec.value() == EBADR)
1727 {
1728 messages::resourceNotFound(
1729 asyncResp->res, "EventLogAttachment", entryID);
1730 return;
1731 }
1732 if (ec)
1733 {
1734 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1735 messages::internalError(asyncResp->res);
1736 return;
1737 }
1738
1739 int fd = -1;
1740 fd = dup(unixfd);
1741 if (fd == -1)
1742 {
1743 messages::internalError(asyncResp->res);
1744 return;
1745 }
1746
1747 long long int size = lseek(fd, 0, SEEK_END);
1748 if (size == -1)
1749 {
1750 messages::internalError(asyncResp->res);
1751 return;
1752 }
1753
1754 // Arbitrary max size of 64kb
1755 constexpr int maxFileSize = 65536;
1756 if (size > maxFileSize)
1757 {
1758 BMCWEB_LOG_ERROR
1759 << "File size exceeds maximum allowed size of "
1760 << maxFileSize;
1761 messages::internalError(asyncResp->res);
1762 return;
1763 }
1764 std::vector<char> data(static_cast<size_t>(size));
1765 long long int rc = lseek(fd, 0, SEEK_SET);
1766 if (rc == -1)
1767 {
1768 messages::internalError(asyncResp->res);
1769 return;
1770 }
1771 rc = read(fd, data.data(), data.size());
1772 if ((rc == -1) || (rc != size))
1773 {
1774 messages::internalError(asyncResp->res);
1775 return;
1776 }
1777 close(fd);
1778
1779 std::string_view strData(data.data(), data.size());
1780 std::string output =
1781 crow::utility::base64encode(strData);
1782
1783 asyncResp->res.addHeader("Content-Type",
1784 "application/octet-stream");
1785 asyncResp->res.addHeader("Content-Transfer-Encoding",
1786 "Base64");
1787 asyncResp->res.body() = std::move(output);
1788 },
1789 "xyz.openbmc_project.Logging",
1790 "/xyz/openbmc_project/logging/entry/" + entryID,
1791 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1792 });
1793}
1794
Spencer Kub7028eb2021-10-26 15:27:35 +08001795constexpr const char* hostLoggerFolderPath = "/var/log/console";
1796
1797inline bool
1798 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1799 std::vector<std::filesystem::path>& hostLoggerFiles)
1800{
1801 std::error_code ec;
1802 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1803 if (ec)
1804 {
1805 BMCWEB_LOG_ERROR << ec.message();
1806 return false;
1807 }
1808 for (const std::filesystem::directory_entry& it : logPath)
1809 {
1810 std::string filename = it.path().filename();
1811 // Prefix of each log files is "log". Find the file and save the
1812 // path
1813 if (boost::starts_with(filename, "log"))
1814 {
1815 hostLoggerFiles.emplace_back(it.path());
1816 }
1817 }
1818 // As the log files rotate, they are appended with a ".#" that is higher for
1819 // the older logs. Since we start from oldest logs, sort the name in
1820 // descending order.
1821 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1822 AlphanumLess<std::string>());
1823
1824 return true;
1825}
1826
1827inline bool
1828 getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1829 uint64_t& skip, uint64_t& top,
1830 std::vector<std::string>& logEntries, size_t& logCount)
1831{
1832 GzFileReader logFile;
1833
1834 // Go though all log files and expose host logs.
1835 for (const std::filesystem::path& it : hostLoggerFiles)
1836 {
1837 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1838 {
1839 BMCWEB_LOG_ERROR << "fail to expose host logs";
1840 return false;
1841 }
1842 }
1843 // Get lastMessage from constructor by getter
1844 std::string lastMessage = logFile.getLastMessage();
1845 if (!lastMessage.empty())
1846 {
1847 logCount++;
1848 if (logCount > skip && logCount <= (skip + top))
1849 {
1850 logEntries.push_back(lastMessage);
1851 }
1852 }
1853 return true;
1854}
1855
1856inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1857 const std::string& msg,
1858 nlohmann::json& logEntryJson)
1859{
1860 // Fill in the log entry with the gathered data.
1861 logEntryJson = {
1862 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1863 {"@odata.id",
1864 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1865 logEntryID},
1866 {"Name", "Host Logger Entry"},
1867 {"Id", logEntryID},
1868 {"Message", msg},
1869 {"EntryType", "Oem"},
1870 {"Severity", "OK"},
1871 {"OemRecordFormat", "Host Logger Entry"}};
1872}
1873
1874inline void requestRoutesSystemHostLogger(App& app)
1875{
1876 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1877 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08001878 .methods(
1879 boost::beast::http::verb::
1880 get)([](const crow::Request&,
1881 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1882 asyncResp->res.jsonValue["@odata.id"] =
1883 "/redfish/v1/Systems/system/LogServices/HostLogger";
1884 asyncResp->res.jsonValue["@odata.type"] =
1885 "#LogService.v1_1_0.LogService";
1886 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1887 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1888 asyncResp->res.jsonValue["Id"] = "HostLogger";
1889 asyncResp->res.jsonValue["Entries"] = {
1890 {"@odata.id",
1891 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1892 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001893}
1894
1895inline void requestRoutesSystemHostLoggerCollection(App& app)
1896{
1897 BMCWEB_ROUTE(app,
1898 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1899 .privileges(redfish::privileges::getLogEntry)
George Liu0fda0f12021-11-16 10:06:17 +08001900 .methods(
1901 boost::beast::http::verb::
1902 get)([](const crow::Request& req,
1903 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1904 uint64_t skip = 0;
1905 uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1906 // default, allow range 1 to
1907 // 1000 entries per page.
1908 if (!getSkipParam(asyncResp, req, skip))
1909 {
1910 return;
1911 }
1912 if (!getTopParam(asyncResp, req, top))
1913 {
1914 return;
1915 }
1916 asyncResp->res.jsonValue["@odata.id"] =
1917 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1918 asyncResp->res.jsonValue["@odata.type"] =
1919 "#LogEntryCollection.LogEntryCollection";
1920 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1921 asyncResp->res.jsonValue["Description"] =
1922 "Collection of HostLogger Entries";
1923 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1924 logEntryArray = nlohmann::json::array();
1925 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001926
George Liu0fda0f12021-11-16 10:06:17 +08001927 std::vector<std::filesystem::path> hostLoggerFiles;
1928 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1929 {
1930 BMCWEB_LOG_ERROR << "fail to get host log file path";
1931 return;
1932 }
1933
1934 size_t logCount = 0;
1935 // This vector only store the entries we want to expose that
1936 // control by skip and top.
1937 std::vector<std::string> logEntries;
1938 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1939 logCount))
1940 {
1941 messages::internalError(asyncResp->res);
1942 return;
1943 }
1944 // If vector is empty, that means skip value larger than total
1945 // log count
Ed Tanous26f69762022-01-25 09:49:11 -08001946 if (logEntries.empty())
George Liu0fda0f12021-11-16 10:06:17 +08001947 {
1948 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1949 return;
1950 }
Ed Tanous26f69762022-01-25 09:49:11 -08001951 if (!logEntries.empty())
George Liu0fda0f12021-11-16 10:06:17 +08001952 {
1953 for (size_t i = 0; i < logEntries.size(); i++)
Spencer Kub7028eb2021-10-26 15:27:35 +08001954 {
George Liu0fda0f12021-11-16 10:06:17 +08001955 logEntryArray.push_back({});
1956 nlohmann::json& hostLogEntry = logEntryArray.back();
1957 fillHostLoggerEntryJson(std::to_string(skip + i),
1958 logEntries[i], hostLogEntry);
Spencer Kub7028eb2021-10-26 15:27:35 +08001959 }
1960
George Liu0fda0f12021-11-16 10:06:17 +08001961 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1962 if (skip + top < logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08001963 {
George Liu0fda0f12021-11-16 10:06:17 +08001964 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1965 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1966 std::to_string(skip + top);
Spencer Kub7028eb2021-10-26 15:27:35 +08001967 }
George Liu0fda0f12021-11-16 10:06:17 +08001968 }
1969 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001970}
1971
1972inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1973{
1974 BMCWEB_ROUTE(
1975 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1976 .privileges(redfish::privileges::getLogEntry)
1977 .methods(boost::beast::http::verb::get)(
1978 [](const crow::Request&,
1979 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1980 const std::string& param) {
1981 const std::string& targetID = param;
1982
1983 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08001984
1985 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1986 const char* end = targetID.data() + targetID.size();
1987
1988 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
Spencer Kub7028eb2021-10-26 15:27:35 +08001989 if (ec == std::errc::invalid_argument)
1990 {
1991 messages::resourceMissingAtURI(asyncResp->res, targetID);
1992 return;
1993 }
1994 if (ec == std::errc::result_out_of_range)
1995 {
1996 messages::resourceMissingAtURI(asyncResp->res, targetID);
1997 return;
1998 }
1999
2000 std::vector<std::filesystem::path> hostLoggerFiles;
2001 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2002 {
2003 BMCWEB_LOG_ERROR << "fail to get host log file path";
2004 return;
2005 }
2006
2007 size_t logCount = 0;
2008 uint64_t top = 1;
2009 std::vector<std::string> logEntries;
2010 // We can get specific entry by skip and top. For example, if we
2011 // want to get nth entry, we can set skip = n-1 and top = 1 to
2012 // get that entry
2013 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2014 logEntries, logCount))
2015 {
2016 messages::internalError(asyncResp->res);
2017 return;
2018 }
2019
2020 if (!logEntries.empty())
2021 {
2022 fillHostLoggerEntryJson(targetID, logEntries[0],
2023 asyncResp->res.jsonValue);
2024 return;
2025 }
2026
2027 // Requested ID was not found
2028 messages::resourceMissingAtURI(asyncResp->res, targetID);
2029 });
2030}
2031
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002032inline void requestRoutesBMCLogServiceCollection(App& app)
2033{
2034 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002035 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002036 .methods(boost::beast::http::verb::get)(
2037 [](const crow::Request&,
2038 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2039 // Collections don't include the static data added by SubRoute
2040 // because it has a duplicate entry for members
2041 asyncResp->res.jsonValue["@odata.type"] =
2042 "#LogServiceCollection.LogServiceCollection";
2043 asyncResp->res.jsonValue["@odata.id"] =
2044 "/redfish/v1/Managers/bmc/LogServices";
2045 asyncResp->res.jsonValue["Name"] =
2046 "Open BMC Log Services Collection";
2047 asyncResp->res.jsonValue["Description"] =
2048 "Collection of LogServices for this Manager";
2049 nlohmann::json& logServiceArray =
2050 asyncResp->res.jsonValue["Members"];
2051 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002052#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002053 logServiceArray.push_back(
2054 {{"@odata.id",
2055 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002056#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002057#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002058 logServiceArray.push_back(
2059 {{"@odata.id",
2060 "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002061#endif
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002062 asyncResp->res.jsonValue["Members@odata.count"] =
2063 logServiceArray.size();
2064 });
2065}
Ed Tanous1da66f72018-07-27 16:13:37 -07002066
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002067inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002068{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002069 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002070 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002071 .methods(boost::beast::http::verb::get)(
2072 [](const crow::Request&,
2073 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jason M. Billse1f26342018-07-18 12:12:00 -07002074
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002075 {
2076 asyncResp->res.jsonValue["@odata.type"] =
2077 "#LogService.v1_1_0.LogService";
2078 asyncResp->res.jsonValue["@odata.id"] =
2079 "/redfish/v1/Managers/bmc/LogServices/Journal";
2080 asyncResp->res.jsonValue["Name"] =
2081 "Open BMC Journal Log Service";
2082 asyncResp->res.jsonValue["Description"] =
2083 "BMC Journal Log Service";
2084 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2085 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302086
2087 std::pair<std::string, std::string> redfishDateTimeOffset =
2088 crow::utility::getDateTimeOffsetNow();
2089 asyncResp->res.jsonValue["DateTime"] =
2090 redfishDateTimeOffset.first;
2091 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2092 redfishDateTimeOffset.second;
2093
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002094 asyncResp->res.jsonValue["Entries"] = {
2095 {"@odata.id",
2096 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
2097 });
2098}
Jason M. Billse1f26342018-07-18 12:12:00 -07002099
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002100static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2101 sd_journal* journal,
2102 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002103{
2104 // Get the Log Entry contents
2105 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002106
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002107 std::string message;
2108 std::string_view syslogID;
2109 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2110 if (ret < 0)
2111 {
2112 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2113 << strerror(-ret);
2114 }
2115 if (!syslogID.empty())
2116 {
2117 message += std::string(syslogID) + ": ";
2118 }
2119
Ed Tanous39e77502019-03-04 17:35:53 -08002120 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002121 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002122 if (ret < 0)
2123 {
2124 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2125 return 1;
2126 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002127 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002128
2129 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002130 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002131 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002132 if (ret < 0)
2133 {
2134 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002135 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002136
2137 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002138 std::string entryTimeStr;
2139 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002140 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002141 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002142 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002143
2144 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002145 bmcJournalLogEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08002146 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002147 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2148 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07002149 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002150 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002151 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07002152 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06002153 {"Severity", severity <= 2 ? "Critical"
2154 : severity <= 4 ? "Warning"
2155 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07002156 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07002157 {"Created", std::move(entryTimeStr)}};
2158 return 0;
2159}
2160
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002161inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002162{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002163 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002164 .privileges(redfish::privileges::getLogEntryCollection)
George Liu0fda0f12021-11-16 10:06:17 +08002165 .methods(
2166 boost::beast::http::verb::
2167 get)([](const crow::Request& req,
2168 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2169 static constexpr const long maxEntriesPerPage = 1000;
2170 uint64_t skip = 0;
2171 uint64_t top = maxEntriesPerPage; // Show max entries by default
2172 if (!getSkipParam(asyncResp, req, skip))
2173 {
2174 return;
2175 }
2176 if (!getTopParam(asyncResp, req, top))
2177 {
2178 return;
2179 }
2180 // Collections don't include the static data added by SubRoute
2181 // because it has a duplicate entry for members
2182 asyncResp->res.jsonValue["@odata.type"] =
2183 "#LogEntryCollection.LogEntryCollection";
2184 asyncResp->res.jsonValue["@odata.id"] =
2185 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2186 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2187 asyncResp->res.jsonValue["Description"] =
2188 "Collection of BMC Journal Entries";
2189 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2190 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002191
George Liu0fda0f12021-11-16 10:06:17 +08002192 // Go through the journal and use the timestamp to create a
2193 // unique ID for each entry
2194 sd_journal* journalTmp = nullptr;
2195 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2196 if (ret < 0)
2197 {
2198 BMCWEB_LOG_ERROR << "failed to open journal: "
2199 << strerror(-ret);
2200 messages::internalError(asyncResp->res);
2201 return;
2202 }
2203 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2204 journalTmp, sd_journal_close);
2205 journalTmp = nullptr;
2206 uint64_t entryCount = 0;
2207 // Reset the unique ID on the first entry
2208 bool firstEntry = true;
2209 SD_JOURNAL_FOREACH(journal.get())
2210 {
2211 entryCount++;
2212 // Handle paging using skip (number of entries to skip from
2213 // the start) and top (number of entries to display)
2214 if (entryCount <= skip || entryCount > skip + top)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002215 {
George Liu0fda0f12021-11-16 10:06:17 +08002216 continue;
2217 }
2218
2219 std::string idStr;
2220 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2221 {
2222 continue;
2223 }
2224
2225 if (firstEntry)
2226 {
2227 firstEntry = false;
2228 }
2229
2230 logEntryArray.push_back({});
2231 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2232 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2233 bmcJournalLogEntry) != 0)
2234 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002235 messages::internalError(asyncResp->res);
2236 return;
2237 }
George Liu0fda0f12021-11-16 10:06:17 +08002238 }
2239 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2240 if (skip + top < entryCount)
2241 {
2242 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2243 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2244 std::to_string(skip + top);
2245 }
2246 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002247}
Jason M. Billse1f26342018-07-18 12:12:00 -07002248
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002249inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002250{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002251 BMCWEB_ROUTE(app,
2252 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002253 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002254 .methods(boost::beast::http::verb::get)(
2255 [](const crow::Request&,
2256 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2257 const std::string& entryID) {
2258 // Convert the unique ID back to a timestamp to find the entry
2259 uint64_t ts = 0;
2260 uint64_t index = 0;
2261 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2262 {
2263 return;
2264 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002265
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002266 sd_journal* journalTmp = nullptr;
2267 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2268 if (ret < 0)
2269 {
2270 BMCWEB_LOG_ERROR << "failed to open journal: "
2271 << strerror(-ret);
2272 messages::internalError(asyncResp->res);
2273 return;
2274 }
2275 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2276 journal(journalTmp, sd_journal_close);
2277 journalTmp = nullptr;
2278 // Go to the timestamp in the log and move to the entry at the
2279 // index tracking the unique ID
2280 std::string idStr;
2281 bool firstEntry = true;
2282 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2283 if (ret < 0)
2284 {
2285 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2286 << strerror(-ret);
2287 messages::internalError(asyncResp->res);
2288 return;
2289 }
2290 for (uint64_t i = 0; i <= index; i++)
2291 {
2292 sd_journal_next(journal.get());
2293 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2294 {
2295 messages::internalError(asyncResp->res);
2296 return;
2297 }
2298 if (firstEntry)
2299 {
2300 firstEntry = false;
2301 }
2302 }
2303 // Confirm that the entry ID matches what was requested
2304 if (idStr != entryID)
2305 {
2306 messages::resourceMissingAtURI(asyncResp->res, entryID);
2307 return;
2308 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002309
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002310 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2311 asyncResp->res.jsonValue) != 0)
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002312 {
2313 messages::internalError(asyncResp->res);
2314 return;
2315 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002316 });
2317}
2318
2319inline void requestRoutesBMCDumpService(App& app)
2320{
2321 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002322 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08002323 .methods(
2324 boost::beast::http::verb::
2325 get)([](const crow::Request&,
2326 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2327 asyncResp->res.jsonValue["@odata.id"] =
2328 "/redfish/v1/Managers/bmc/LogServices/Dump";
2329 asyncResp->res.jsonValue["@odata.type"] =
2330 "#LogService.v1_2_0.LogService";
2331 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2332 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2333 asyncResp->res.jsonValue["Id"] = "Dump";
2334 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302335
George Liu0fda0f12021-11-16 10:06:17 +08002336 std::pair<std::string, std::string> redfishDateTimeOffset =
2337 crow::utility::getDateTimeOffsetNow();
2338 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2339 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2340 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302341
George Liu0fda0f12021-11-16 10:06:17 +08002342 asyncResp->res.jsonValue["Entries"] = {
2343 {"@odata.id",
2344 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2345 asyncResp->res.jsonValue["Actions"] = {
2346 {"#LogService.ClearLog",
2347 {{"target",
2348 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2349 {"#LogService.CollectDiagnosticData",
2350 {{"target",
2351 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
2352 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002353}
2354
2355inline void requestRoutesBMCDumpEntryCollection(App& app)
2356{
2357
2358 /**
2359 * Functions triggers appropriate requests on DBus
2360 */
2361 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002362 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002363 .methods(boost::beast::http::verb::get)(
2364 [](const crow::Request&,
2365 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2366 asyncResp->res.jsonValue["@odata.type"] =
2367 "#LogEntryCollection.LogEntryCollection";
2368 asyncResp->res.jsonValue["@odata.id"] =
2369 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2370 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2371 asyncResp->res.jsonValue["Description"] =
2372 "Collection of BMC Dump Entries";
2373
2374 getDumpEntryCollection(asyncResp, "BMC");
2375 });
2376}
2377
2378inline void requestRoutesBMCDumpEntry(App& app)
2379{
2380 BMCWEB_ROUTE(app,
2381 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002382 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002383 .methods(boost::beast::http::verb::get)(
2384 [](const crow::Request&,
2385 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2386 const std::string& param) {
2387 getDumpEntryById(asyncResp, param, "BMC");
2388 });
2389 BMCWEB_ROUTE(app,
2390 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002391 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002392 .methods(boost::beast::http::verb::delete_)(
2393 [](const crow::Request&,
2394 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2395 const std::string& param) {
2396 deleteDumpEntry(asyncResp, param, "bmc");
2397 });
2398}
2399
2400inline void requestRoutesBMCDumpCreate(App& app)
2401{
2402
George Liu0fda0f12021-11-16 10:06:17 +08002403 BMCWEB_ROUTE(
2404 app,
2405 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002406 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002407 .methods(boost::beast::http::verb::post)(
2408 [](const crow::Request& req,
2409 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2410 createDump(asyncResp, req, "BMC");
2411 });
2412}
2413
2414inline void requestRoutesBMCDumpClear(App& app)
2415{
George Liu0fda0f12021-11-16 10:06:17 +08002416 BMCWEB_ROUTE(
2417 app,
2418 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002419 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002420 .methods(boost::beast::http::verb::post)(
2421 [](const crow::Request&,
2422 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2423 clearDump(asyncResp, "BMC");
2424 });
2425}
2426
2427inline void requestRoutesSystemDumpService(App& app)
2428{
2429 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002430 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002431 .methods(boost::beast::http::verb::get)(
2432 [](const crow::Request&,
2433 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2434
2435 {
2436 asyncResp->res.jsonValue["@odata.id"] =
2437 "/redfish/v1/Systems/system/LogServices/Dump";
2438 asyncResp->res.jsonValue["@odata.type"] =
2439 "#LogService.v1_2_0.LogService";
2440 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2441 asyncResp->res.jsonValue["Description"] =
2442 "System Dump LogService";
2443 asyncResp->res.jsonValue["Id"] = "Dump";
2444 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302445
2446 std::pair<std::string, std::string> redfishDateTimeOffset =
2447 crow::utility::getDateTimeOffsetNow();
2448 asyncResp->res.jsonValue["DateTime"] =
2449 redfishDateTimeOffset.first;
2450 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2451 redfishDateTimeOffset.second;
2452
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002453 asyncResp->res.jsonValue["Entries"] = {
2454 {"@odata.id",
2455 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2456 asyncResp->res.jsonValue["Actions"] = {
2457 {"#LogService.ClearLog",
2458 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002459 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002460 {"#LogService.CollectDiagnosticData",
2461 {{"target",
George Liu0fda0f12021-11-16 10:06:17 +08002462 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002463 });
2464}
2465
2466inline void requestRoutesSystemDumpEntryCollection(App& app)
2467{
2468
2469 /**
2470 * Functions triggers appropriate requests on DBus
2471 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002472 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002473 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002474 .methods(boost::beast::http::verb::get)(
2475 [](const crow::Request&,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002476 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002477 asyncResp->res.jsonValue["@odata.type"] =
2478 "#LogEntryCollection.LogEntryCollection";
2479 asyncResp->res.jsonValue["@odata.id"] =
2480 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2481 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2482 asyncResp->res.jsonValue["Description"] =
2483 "Collection of System Dump Entries";
2484
2485 getDumpEntryCollection(asyncResp, "System");
2486 });
2487}
2488
2489inline void requestRoutesSystemDumpEntry(App& app)
2490{
2491 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002492 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002493 .privileges(redfish::privileges::getLogEntry)
2494
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002495 .methods(boost::beast::http::verb::get)(
2496 [](const crow::Request&,
2497 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2498 const std::string& param) {
2499 getDumpEntryById(asyncResp, param, "System");
2500 });
2501
2502 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002503 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002504 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002505 .methods(boost::beast::http::verb::delete_)(
2506 [](const crow::Request&,
2507 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2508 const std::string& param) {
2509 deleteDumpEntry(asyncResp, param, "system");
2510 });
2511}
2512
2513inline void requestRoutesSystemDumpCreate(App& app)
2514{
George Liu0fda0f12021-11-16 10:06:17 +08002515 BMCWEB_ROUTE(
2516 app,
2517 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002518 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002519 .methods(boost::beast::http::verb::post)(
2520 [](const crow::Request& req,
2521 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2522
2523 { createDump(asyncResp, req, "System"); });
2524}
2525
2526inline void requestRoutesSystemDumpClear(App& app)
2527{
George Liu0fda0f12021-11-16 10:06:17 +08002528 BMCWEB_ROUTE(
2529 app,
2530 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002531 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002532 .methods(boost::beast::http::verb::post)(
2533 [](const crow::Request&,
2534 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2535
2536 { clearDump(asyncResp, "System"); });
2537}
2538
2539inline void requestRoutesCrashdumpService(App& app)
2540{
2541 // Note: Deviated from redfish privilege registry for GET & HEAD
2542 // method for security reasons.
2543 /**
2544 * Functions triggers appropriate requests on DBus
2545 */
2546 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002547 // This is incorrect, should be:
2548 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002549 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002550 .methods(
2551 boost::beast::http::verb::
2552 get)([](const crow::Request&,
2553 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2554 // Copy over the static data to include the entries added by
2555 // SubRoute
2556 asyncResp->res.jsonValue["@odata.id"] =
2557 "/redfish/v1/Systems/system/LogServices/Crashdump";
2558 asyncResp->res.jsonValue["@odata.type"] =
2559 "#LogService.v1_2_0.LogService";
2560 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2561 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2562 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2563 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2564 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302565
2566 std::pair<std::string, std::string> redfishDateTimeOffset =
2567 crow::utility::getDateTimeOffsetNow();
2568 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2569 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2570 redfishDateTimeOffset.second;
2571
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002572 asyncResp->res.jsonValue["Entries"] = {
2573 {"@odata.id",
2574 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2575 asyncResp->res.jsonValue["Actions"] = {
2576 {"#LogService.ClearLog",
George Liu0fda0f12021-11-16 10:06:17 +08002577 {{"target",
2578 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002579 {"#LogService.CollectDiagnosticData",
George Liu0fda0f12021-11-16 10:06:17 +08002580 {{"target",
2581 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002582 });
2583}
2584
2585void inline requestRoutesCrashdumpClear(App& app)
2586{
George Liu0fda0f12021-11-16 10:06:17 +08002587 BMCWEB_ROUTE(
2588 app,
2589 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002590 // This is incorrect, should be:
2591 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002592 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002593 .methods(boost::beast::http::verb::post)(
2594 [](const crow::Request&,
2595 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2596 crow::connections::systemBus->async_method_call(
2597 [asyncResp](const boost::system::error_code ec,
2598 const std::string&) {
2599 if (ec)
2600 {
2601 messages::internalError(asyncResp->res);
2602 return;
2603 }
2604 messages::success(asyncResp->res);
2605 },
2606 crashdumpObject, crashdumpPath, deleteAllInterface,
2607 "DeleteAll");
2608 });
2609}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002610
zhanghch058d1b46d2021-04-01 11:18:24 +08002611static void
2612 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2613 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002614{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002615 auto getStoredLogCallback =
2616 [asyncResp, logID, &logEntryJson](
2617 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -08002618 const std::vector<
2619 std::pair<std::string, dbus::utility::DbusVariantType>>&
2620 params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002621 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002622 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002623 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2624 if (ec.value() ==
2625 boost::system::linux_error::bad_request_descriptor)
2626 {
2627 messages::resourceNotFound(asyncResp->res, "LogEntry",
2628 logID);
2629 }
2630 else
2631 {
2632 messages::internalError(asyncResp->res);
2633 }
2634 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002635 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002636
Johnathan Mantey043a0532020-03-10 17:15:28 -07002637 std::string timestamp{};
2638 std::string filename{};
2639 std::string logfile{};
Ed Tanous2c70f802020-09-28 14:29:23 -07002640 parseCrashdumpParameters(params, filename, timestamp, logfile);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002641
2642 if (filename.empty() || timestamp.empty())
2643 {
2644 messages::resourceMissingAtURI(asyncResp->res, logID);
2645 return;
2646 }
2647
2648 std::string crashdumpURI =
2649 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2650 logID + "/" + filename;
Ed Tanousd0dbeef2021-07-01 08:46:46 -07002651 logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002652 {"@odata.id", "/redfish/v1/Systems/system/"
2653 "LogServices/Crashdump/Entries/" +
2654 logID},
2655 {"Name", "CPU Crashdump"},
2656 {"Id", logID},
2657 {"EntryType", "Oem"},
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002658 {"AdditionalDataURI", std::move(crashdumpURI)},
2659 {"DiagnosticDataType", "OEM"},
2660 {"OEMDiagnosticDataType", "PECICrashdump"},
Johnathan Mantey043a0532020-03-10 17:15:28 -07002661 {"Created", std::move(timestamp)}};
2662 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002663 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002664 std::move(getStoredLogCallback), crashdumpObject,
2665 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002666 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002667}
2668
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002669inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002670{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002671 // Note: Deviated from redfish privilege registry for GET & HEAD
2672 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002673 /**
2674 * Functions triggers appropriate requests on DBus
2675 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002676 BMCWEB_ROUTE(app,
2677 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002678 // This is incorrect, should be.
2679 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002680 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002681 .methods(
2682 boost::beast::http::verb::
2683 get)([](const crow::Request&,
2684 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2685 // Collections don't include the static data added by SubRoute
2686 // because it has a duplicate entry for members
2687 auto getLogEntriesCallback = [asyncResp](
2688 const boost::system::error_code ec,
2689 const std::vector<std::string>&
2690 resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002691 if (ec)
2692 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002693 if (ec.value() !=
2694 boost::system::errc::no_such_file_or_directory)
2695 {
2696 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2697 << ec.message();
2698 messages::internalError(asyncResp->res);
2699 return;
2700 }
Johnathan Mantey043a0532020-03-10 17:15:28 -07002701 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002702 asyncResp->res.jsonValue["@odata.type"] =
2703 "#LogEntryCollection.LogEntryCollection";
2704 asyncResp->res.jsonValue["@odata.id"] =
2705 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2706 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2707 asyncResp->res.jsonValue["Description"] =
2708 "Collection of Crashdump Entries";
2709 nlohmann::json& logEntryArray =
2710 asyncResp->res.jsonValue["Members"];
2711 logEntryArray = nlohmann::json::array();
2712 std::vector<std::string> logIDs;
2713 // Get the list of log entries and build up an empty array big
2714 // enough to hold them
2715 for (const std::string& objpath : resp)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002716 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002717 // Get the log ID
2718 std::size_t lastPos = objpath.rfind('/');
2719 if (lastPos == std::string::npos)
2720 {
2721 continue;
2722 }
2723 logIDs.emplace_back(objpath.substr(lastPos + 1));
Johnathan Mantey043a0532020-03-10 17:15:28 -07002724
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002725 // Add a space for the log entry to the array
2726 logEntryArray.push_back({});
2727 }
2728 // Now go through and set up async calls to fill in the entries
2729 size_t index = 0;
2730 for (const std::string& logID : logIDs)
Johnathan Mantey043a0532020-03-10 17:15:28 -07002731 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002732 // Add the log entry to the array
2733 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Johnathan Mantey043a0532020-03-10 17:15:28 -07002734 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002735 asyncResp->res.jsonValue["Members@odata.count"] =
2736 logEntryArray.size();
Johnathan Mantey043a0532020-03-10 17:15:28 -07002737 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002738 crow::connections::systemBus->async_method_call(
2739 std::move(getLogEntriesCallback),
2740 "xyz.openbmc_project.ObjectMapper",
2741 "/xyz/openbmc_project/object_mapper",
2742 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2743 std::array<const char*, 1>{crashdumpInterface});
2744 });
2745}
Ed Tanous1da66f72018-07-27 16:13:37 -07002746
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002747inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002748{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002749 // Note: Deviated from redfish privilege registry for GET & HEAD
2750 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002751
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002752 BMCWEB_ROUTE(
2753 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002754 // this is incorrect, should be
2755 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002756 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002757 .methods(boost::beast::http::verb::get)(
2758 [](const crow::Request&,
2759 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2760 const std::string& param) {
2761 const std::string& logID = param;
2762 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2763 });
2764}
Ed Tanous1da66f72018-07-27 16:13:37 -07002765
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002766inline void requestRoutesCrashdumpFile(App& app)
2767{
2768 // Note: Deviated from redfish privilege registry for GET & HEAD
2769 // method for security reasons.
2770 BMCWEB_ROUTE(
2771 app,
2772 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002773 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002774 .methods(boost::beast::http::verb::get)(
2775 [](const crow::Request&,
2776 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2777 const std::string& logID, const std::string& fileName) {
2778 auto getStoredLogCallback =
2779 [asyncResp, logID, fileName](
2780 const boost::system::error_code ec,
Ed Tanous168e20c2021-12-13 14:39:53 -08002781 const std::vector<std::pair<
2782 std::string, dbus::utility::DbusVariantType>>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002783 resp) {
2784 if (ec)
2785 {
2786 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2787 << ec.message();
2788 messages::internalError(asyncResp->res);
2789 return;
2790 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002791
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002792 std::string dbusFilename{};
2793 std::string dbusTimestamp{};
2794 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002795
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002796 parseCrashdumpParameters(resp, dbusFilename,
2797 dbusTimestamp, dbusFilepath);
2798
2799 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2800 dbusFilepath.empty())
2801 {
2802 messages::resourceMissingAtURI(asyncResp->res,
2803 fileName);
2804 return;
2805 }
2806
2807 // Verify the file name parameter is correct
2808 if (fileName != dbusFilename)
2809 {
2810 messages::resourceMissingAtURI(asyncResp->res,
2811 fileName);
2812 return;
2813 }
2814
2815 if (!std::filesystem::exists(dbusFilepath))
2816 {
2817 messages::resourceMissingAtURI(asyncResp->res,
2818 fileName);
2819 return;
2820 }
Jason M. Bills2d314912022-01-12 13:59:01 -08002821 std::ifstream ifs(dbusFilepath,
2822 std::ios::in | std::ios::binary);
2823 asyncResp->res.body() = std::string(
2824 std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002825
2826 // Configure this to be a file download when accessed
2827 // from a browser
2828 asyncResp->res.addHeader("Content-Disposition",
2829 "attachment");
2830 };
2831 crow::connections::systemBus->async_method_call(
2832 std::move(getStoredLogCallback), crashdumpObject,
2833 crashdumpPath + std::string("/") + logID,
2834 "org.freedesktop.DBus.Properties", "GetAll",
2835 crashdumpInterface);
2836 });
2837}
2838
2839inline void requestRoutesCrashdumpCollect(App& app)
2840{
2841 // Note: Deviated from redfish privilege registry for GET & HEAD
2842 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08002843 BMCWEB_ROUTE(
2844 app,
2845 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002846 // The below is incorrect; Should be ConfigureManager
2847 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002848 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002849 .methods(
2850 boost::beast::http::verb::
2851 post)([](const crow::Request& req,
2852 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2853 std::string diagnosticDataType;
2854 std::string oemDiagnosticDataType;
2855 if (!redfish::json_util::readJson(
2856 req, asyncResp->res, "DiagnosticDataType",
2857 diagnosticDataType, "OEMDiagnosticDataType",
2858 oemDiagnosticDataType))
James Feist46229572020-02-19 15:11:58 -08002859 {
James Feist46229572020-02-19 15:11:58 -08002860 return;
2861 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002862
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002863 if (diagnosticDataType != "OEM")
2864 {
2865 BMCWEB_LOG_ERROR
2866 << "Only OEM DiagnosticDataType supported for Crashdump";
2867 messages::actionParameterValueFormatError(
2868 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
2869 "CollectDiagnosticData");
2870 return;
2871 }
2872
Ed Tanous98be3e32021-09-16 15:05:36 -07002873 auto collectCrashdumpCallback = [asyncResp,
2874 payload(task::Payload(req))](
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002875 const boost::system::error_code
2876 ec,
Ed Tanous98be3e32021-09-16 15:05:36 -07002877 const std::string&) mutable {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002878 if (ec)
2879 {
2880 if (ec.value() ==
2881 boost::system::errc::operation_not_supported)
2882 {
2883 messages::resourceInStandby(asyncResp->res);
2884 }
2885 else if (ec.value() ==
2886 boost::system::errc::device_or_resource_busy)
2887 {
2888 messages::serviceTemporarilyUnavailable(asyncResp->res,
2889 "60");
2890 }
2891 else
2892 {
2893 messages::internalError(asyncResp->res);
2894 }
2895 return;
2896 }
George Liu0fda0f12021-11-16 10:06:17 +08002897 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
2898 [](boost::system::error_code err,
2899 sdbusplus::message::message&,
2900 const std::shared_ptr<task::TaskData>& taskData) {
2901 if (!err)
2902 {
2903 taskData->messages.emplace_back(
2904 messages::taskCompletedOK(
2905 std::to_string(taskData->index)));
2906 taskData->state = "Completed";
2907 }
2908 return task::completed;
2909 },
2910 "type='signal',interface='org.freedesktop.DBus."
2911 "Properties',"
2912 "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002913 task->startTimer(std::chrono::minutes(5));
2914 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -07002915 task->payload.emplace(std::move(payload));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002916 };
2917
2918 if (oemDiagnosticDataType == "OnDemand")
2919 {
2920 crow::connections::systemBus->async_method_call(
2921 std::move(collectCrashdumpCallback), crashdumpObject,
2922 crashdumpPath, crashdumpOnDemandInterface,
2923 "GenerateOnDemandLog");
2924 }
2925 else if (oemDiagnosticDataType == "Telemetry")
2926 {
2927 crow::connections::systemBus->async_method_call(
2928 std::move(collectCrashdumpCallback), crashdumpObject,
2929 crashdumpPath, crashdumpTelemetryInterface,
2930 "GenerateTelemetryLog");
2931 }
2932 else
2933 {
2934 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2935 << oemDiagnosticDataType;
2936 messages::actionParameterValueFormatError(
2937 asyncResp->res, oemDiagnosticDataType,
2938 "OEMDiagnosticDataType", "CollectDiagnosticData");
2939 return;
2940 }
2941 });
2942}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002943
Andrew Geisslercb92c032018-08-17 07:56:14 -07002944/**
2945 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2946 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002947inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07002948{
Andrew Geisslercb92c032018-08-17 07:56:14 -07002949 /**
2950 * Function handles POST method request.
2951 * The Clear Log actions does not require any parameter.The action deletes
2952 * all entries found in the Entries collection for this Log Service.
2953 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07002954
George Liu0fda0f12021-11-16 10:06:17 +08002955 BMCWEB_ROUTE(
2956 app,
2957 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002958 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002959 .methods(boost::beast::http::verb::post)(
2960 [](const crow::Request&,
2961 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2962 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07002963
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002964 // Process response from Logging service.
2965 auto respHandler = [asyncResp](
2966 const boost::system::error_code ec) {
2967 BMCWEB_LOG_DEBUG
2968 << "doClearLog resp_handler callback: Done";
2969 if (ec)
2970 {
2971 // TODO Handle for specific error code
2972 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
2973 << ec;
2974 asyncResp->res.result(
2975 boost::beast::http::status::internal_server_error);
2976 return;
2977 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07002978
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002979 asyncResp->res.result(
2980 boost::beast::http::status::no_content);
2981 };
2982
2983 // Make call to Logging service to request Clear Log
2984 crow::connections::systemBus->async_method_call(
2985 respHandler, "xyz.openbmc_project.Logging",
2986 "/xyz/openbmc_project/logging",
2987 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2988 });
2989}
ZhikuiRena3316fc2020-01-29 14:58:08 -08002990
2991/****************************************************
2992 * Redfish PostCode interfaces
2993 * using DBUS interface: getPostCodesTS
2994 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002995inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08002996{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002997 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07002998 .privileges(redfish::privileges::getLogService)
George Liu0fda0f12021-11-16 10:06:17 +08002999 .methods(
3000 boost::beast::http::verb::
3001 get)([](const crow::Request&,
3002 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3003 asyncResp->res.jsonValue = {
3004 {"@odata.id",
3005 "/redfish/v1/Systems/system/LogServices/PostCodes"},
3006 {"@odata.type", "#LogService.v1_1_0.LogService"},
3007 {"Name", "POST Code Log Service"},
3008 {"Description", "POST Code Log Service"},
3009 {"Id", "BIOS POST Code Log"},
3010 {"OverWritePolicy", "WrapsWhenFull"},
3011 {"Entries",
3012 {{"@odata.id",
3013 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
Tejas Patil7c8c4052021-06-04 17:43:14 +05303014
George Liu0fda0f12021-11-16 10:06:17 +08003015 std::pair<std::string, std::string> redfishDateTimeOffset =
3016 crow::utility::getDateTimeOffsetNow();
3017 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3018 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3019 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303020
George Liu0fda0f12021-11-16 10:06:17 +08003021 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3022 {"target",
3023 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
3024 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003025}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003026
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003027inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003028{
George Liu0fda0f12021-11-16 10:06:17 +08003029 BMCWEB_ROUTE(
3030 app,
3031 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003032 // The following privilege is incorrect; It should be ConfigureManager
3033 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003034 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003035 .methods(boost::beast::http::verb::post)(
3036 [](const crow::Request&,
3037 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3038 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003039
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003040 // Make call to post-code service to request clear all
3041 crow::connections::systemBus->async_method_call(
3042 [asyncResp](const boost::system::error_code ec) {
3043 if (ec)
3044 {
3045 // TODO Handle for specific error code
3046 BMCWEB_LOG_ERROR
3047 << "doClearPostCodes resp_handler got error "
3048 << ec;
3049 asyncResp->res.result(boost::beast::http::status::
3050 internal_server_error);
3051 messages::internalError(asyncResp->res);
3052 return;
3053 }
3054 },
3055 "xyz.openbmc_project.State.Boot.PostCode0",
3056 "/xyz/openbmc_project/State/Boot/PostCode0",
3057 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3058 });
3059}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003060
3061static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003062 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303063 const boost::container::flat_map<
3064 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003065 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3066 const uint64_t skip = 0, const uint64_t top = 0)
3067{
3068 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003069 const message_registries::Message* message =
Manojkiran Eda4a0bf532021-04-21 22:46:14 +05303070 message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003071
3072 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003073 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003074
3075 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303076 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3077 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003078 {
3079 currentCodeIndex++;
3080 std::string postcodeEntryID =
3081 "B" + std::to_string(bootIndex) + "-" +
3082 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3083
3084 uint64_t usecSinceEpoch = code.first;
3085 uint64_t usTimeOffset = 0;
3086
3087 if (1 == currentCodeIndex)
3088 { // already incremented
3089 firstCodeTimeUs = code.first;
3090 }
3091 else
3092 {
3093 usTimeOffset = code.first - firstCodeTimeUs;
3094 }
3095
3096 // skip if no specific codeIndex is specified and currentCodeIndex does
3097 // not fall between top and skip
3098 if ((codeIndex == 0) &&
3099 (currentCodeIndex <= skip || currentCodeIndex > top))
3100 {
3101 continue;
3102 }
3103
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003104 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003105 // currentIndex
3106 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3107 {
3108 // This is done for simplicity. 1st entry is needed to calculate
3109 // time offset. To improve efficiency, one can get to the entry
3110 // directly (possibly with flatmap's nth method)
3111 continue;
3112 }
3113
3114 // currentCodeIndex is within top and skip or equal to specified code
3115 // index
3116
3117 // Get the Created time from the timestamp
3118 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003119 entryTimeStr =
3120 crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003121
3122 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3123 std::ostringstream hexCode;
3124 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303125 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003126 std::ostringstream timeOffsetStr;
3127 // Set Fixed -Point Notation
3128 timeOffsetStr << std::fixed;
3129 // Set precision to 4 digits
3130 timeOffsetStr << std::setprecision(4);
3131 // Add double to stream
3132 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3133 std::vector<std::string> messageArgs = {
3134 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3135
3136 // Get MessageArgs template from message registry
3137 std::string msg;
3138 if (message != nullptr)
3139 {
3140 msg = message->message;
3141
3142 // fill in this post code value
3143 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003144 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003145 {
3146 std::string argStr = "%" + std::to_string(++i);
3147 size_t argPos = msg.find(argStr);
3148 if (argPos != std::string::npos)
3149 {
3150 msg.replace(argPos, argStr.length(), messageArg);
3151 }
3152 }
3153 }
3154
Tim Leed4342a92020-04-27 11:47:58 +08003155 // Get Severity template from message registry
3156 std::string severity;
3157 if (message != nullptr)
3158 {
3159 severity = message->severity;
3160 }
3161
ZhikuiRena3316fc2020-01-29 14:58:08 -08003162 // add to AsyncResp
3163 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003164 nlohmann::json& bmcLogEntry = logEntryArray.back();
George Liu0fda0f12021-11-16 10:06:17 +08003165 bmcLogEntry = {
3166 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
3167 {"@odata.id",
3168 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3169 postcodeEntryID},
3170 {"Name", "POST Code Log Entry"},
3171 {"Id", postcodeEntryID},
3172 {"Message", std::move(msg)},
3173 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3174 {"MessageArgs", std::move(messageArgs)},
3175 {"EntryType", "Event"},
3176 {"Severity", std::move(severity)},
3177 {"Created", entryTimeStr}};
George Liu647b3cd2021-07-05 12:43:56 +08003178 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3179 {
3180 bmcLogEntry["AdditionalDataURI"] =
3181 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3182 postcodeEntryID + "/attachment";
3183 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003184 }
3185}
3186
zhanghch058d1b46d2021-04-01 11:18:24 +08003187static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003188 const uint16_t bootIndex,
3189 const uint64_t codeIndex)
3190{
3191 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303192 [aResp, bootIndex,
3193 codeIndex](const boost::system::error_code ec,
3194 const boost::container::flat_map<
3195 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3196 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003197 if (ec)
3198 {
3199 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3200 messages::internalError(aResp->res);
3201 return;
3202 }
3203
3204 // skip the empty postcode boots
3205 if (postcode.empty())
3206 {
3207 return;
3208 }
3209
3210 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3211
3212 aResp->res.jsonValue["Members@odata.count"] =
3213 aResp->res.jsonValue["Members"].size();
3214 },
Jonathan Doman15124762021-01-07 17:54:17 -08003215 "xyz.openbmc_project.State.Boot.PostCode0",
3216 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003217 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3218 bootIndex);
3219}
3220
zhanghch058d1b46d2021-04-01 11:18:24 +08003221static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003222 const uint16_t bootIndex,
3223 const uint16_t bootCount,
3224 const uint64_t entryCount, const uint64_t skip,
3225 const uint64_t top)
3226{
3227 crow::connections::systemBus->async_method_call(
3228 [aResp, bootIndex, bootCount, entryCount, skip,
3229 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303230 const boost::container::flat_map<
3231 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3232 postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003233 if (ec)
3234 {
3235 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3236 messages::internalError(aResp->res);
3237 return;
3238 }
3239
3240 uint64_t endCount = entryCount;
3241 if (!postcode.empty())
3242 {
3243 endCount = entryCount + postcode.size();
3244
3245 if ((skip < endCount) && ((top + skip) > entryCount))
3246 {
3247 uint64_t thisBootSkip =
3248 std::max(skip, entryCount) - entryCount;
3249 uint64_t thisBootTop =
3250 std::min(top + skip, endCount) - entryCount;
3251
3252 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3253 thisBootSkip, thisBootTop);
3254 }
3255 aResp->res.jsonValue["Members@odata.count"] = endCount;
3256 }
3257
3258 // continue to previous bootIndex
3259 if (bootIndex < bootCount)
3260 {
3261 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3262 bootCount, endCount, skip, top);
3263 }
3264 else
3265 {
3266 aResp->res.jsonValue["Members@odata.nextLink"] =
George Liu0fda0f12021-11-16 10:06:17 +08003267 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
ZhikuiRena3316fc2020-01-29 14:58:08 -08003268 std::to_string(skip + top);
3269 }
3270 },
Jonathan Doman15124762021-01-07 17:54:17 -08003271 "xyz.openbmc_project.State.Boot.PostCode0",
3272 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003273 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3274 bootIndex);
3275}
3276
zhanghch058d1b46d2021-04-01 11:18:24 +08003277static void
3278 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3279 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003280{
3281 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003282 sdbusplus::asio::getProperty<uint16_t>(
3283 *crow::connections::systemBus,
3284 "xyz.openbmc_project.State.Boot.PostCode0",
3285 "/xyz/openbmc_project/State/Boot/PostCode0",
3286 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3287 [aResp, entryCount, skip, top](const boost::system::error_code ec,
3288 const uint16_t bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003289 if (ec)
3290 {
3291 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3292 messages::internalError(aResp->res);
3293 return;
3294 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003295 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
3296 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003297}
3298
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003299inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003300{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003301 BMCWEB_ROUTE(app,
3302 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003303 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003304 .methods(boost::beast::http::verb::get)(
3305 [](const crow::Request& req,
3306 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3307 asyncResp->res.jsonValue["@odata.type"] =
3308 "#LogEntryCollection.LogEntryCollection";
3309 asyncResp->res.jsonValue["@odata.id"] =
3310 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3311 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3312 asyncResp->res.jsonValue["Description"] =
3313 "Collection of POST Code Log Entries";
3314 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3315 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003316
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003317 uint64_t skip = 0;
3318 uint64_t top = maxEntriesPerPage; // Show max entries by default
3319 if (!getSkipParam(asyncResp, req, skip))
3320 {
3321 return;
3322 }
3323 if (!getTopParam(asyncResp, req, top))
3324 {
3325 return;
3326 }
3327 getCurrentBootNumber(asyncResp, skip, top);
3328 });
3329}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003330
George Liu647b3cd2021-07-05 12:43:56 +08003331/**
3332 * @brief Parse post code ID and get the current value and index value
3333 * eg: postCodeID=B1-2, currentValue=1, index=2
3334 *
3335 * @param[in] postCodeID Post Code ID
3336 * @param[out] currentValue Current value
3337 * @param[out] index Index value
3338 *
3339 * @return bool true if the parsing is successful, false the parsing fails
3340 */
3341inline static bool parsePostCode(const std::string& postCodeID,
3342 uint64_t& currentValue, uint16_t& index)
3343{
3344 std::vector<std::string> split;
3345 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3346 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3347 {
3348 return false;
3349 }
3350
Ed Tanousca45aa32022-01-07 09:28:45 -08003351 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003352 const char* start = split[0].data() + 1;
Ed Tanousca45aa32022-01-07 09:28:45 -08003353 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003354 const char* end = split[0].data() + split[0].size();
3355 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3356
3357 if (ptrIndex != end || ecIndex != std::errc())
3358 {
3359 return false;
3360 }
3361
3362 start = split[1].data();
Ed Tanousca45aa32022-01-07 09:28:45 -08003363
3364 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003365 end = split[1].data() + split[1].size();
3366 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
George Liu647b3cd2021-07-05 12:43:56 +08003367
Ed Tanousdcf2ebc2022-01-25 10:07:45 -08003368 return ptrValue == end && ecValue != std::errc();
George Liu647b3cd2021-07-05 12:43:56 +08003369}
3370
3371inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3372{
George Liu0fda0f12021-11-16 10:06:17 +08003373 BMCWEB_ROUTE(
3374 app,
3375 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003376 .privileges(redfish::privileges::getLogEntry)
3377 .methods(boost::beast::http::verb::get)(
3378 [](const crow::Request& req,
3379 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3380 const std::string& postCodeID) {
3381 if (!http_helpers::isOctetAccepted(
3382 req.getHeaderValue("Accept")))
3383 {
3384 asyncResp->res.result(
3385 boost::beast::http::status::bad_request);
3386 return;
3387 }
3388
3389 uint64_t currentValue = 0;
3390 uint16_t index = 0;
3391 if (!parsePostCode(postCodeID, currentValue, index))
3392 {
3393 messages::resourceNotFound(asyncResp->res, "LogEntry",
3394 postCodeID);
3395 return;
3396 }
3397
3398 crow::connections::systemBus->async_method_call(
3399 [asyncResp, postCodeID, currentValue](
3400 const boost::system::error_code ec,
3401 const std::vector<std::tuple<
3402 uint64_t, std::vector<uint8_t>>>& postcodes) {
3403 if (ec.value() == EBADR)
3404 {
3405 messages::resourceNotFound(asyncResp->res,
3406 "LogEntry", postCodeID);
3407 return;
3408 }
3409 if (ec)
3410 {
3411 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3412 messages::internalError(asyncResp->res);
3413 return;
3414 }
3415
3416 size_t value = static_cast<size_t>(currentValue) - 1;
3417 if (value == std::string::npos ||
3418 postcodes.size() < currentValue)
3419 {
3420 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3421 messages::resourceNotFound(asyncResp->res,
3422 "LogEntry", postCodeID);
3423 return;
3424 }
3425
Ed Tanous46ff87b2022-01-07 09:25:51 -08003426 auto& [tID, c] = postcodes[value];
3427 if (c.empty())
George Liu647b3cd2021-07-05 12:43:56 +08003428 {
3429 BMCWEB_LOG_INFO << "No found post code data";
3430 messages::resourceNotFound(asyncResp->res,
3431 "LogEntry", postCodeID);
3432 return;
3433 }
Ed Tanous46ff87b2022-01-07 09:25:51 -08003434 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3435 const char* d = reinterpret_cast<const char*>(c.data());
3436 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08003437
3438 asyncResp->res.addHeader("Content-Type",
3439 "application/octet-stream");
3440 asyncResp->res.addHeader("Content-Transfer-Encoding",
3441 "Base64");
3442 asyncResp->res.body() =
3443 crow::utility::base64encode(strData);
3444 },
3445 "xyz.openbmc_project.State.Boot.PostCode0",
3446 "/xyz/openbmc_project/State/Boot/PostCode0",
3447 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3448 index);
3449 });
3450}
3451
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003452inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003453{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003454 BMCWEB_ROUTE(
3455 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003456 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003457 .methods(boost::beast::http::verb::get)(
3458 [](const crow::Request&,
3459 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3460 const std::string& targetID) {
George Liu647b3cd2021-07-05 12:43:56 +08003461 uint16_t bootIndex = 0;
3462 uint64_t codeIndex = 0;
3463 if (!parsePostCode(targetID, codeIndex, bootIndex))
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003464 {
3465 // Requested ID was not found
3466 messages::resourceMissingAtURI(asyncResp->res, targetID);
3467 return;
3468 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003469 if (bootIndex == 0 || codeIndex == 0)
3470 {
3471 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3472 << targetID;
3473 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003474
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003475 asyncResp->res.jsonValue["@odata.type"] =
3476 "#LogEntry.v1_4_0.LogEntry";
3477 asyncResp->res.jsonValue["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +08003478 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003479 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3480 asyncResp->res.jsonValue["Description"] =
3481 "Collection of POST Code Log Entries";
3482 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3483 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003484
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003485 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3486 });
3487}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003488
Ed Tanous1da66f72018-07-27 16:13:37 -07003489} // namespace redfish