blob: 11cdeffd891169765a7f96f0e5504e5a8d176a7a [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 Tanous45ca1b82022-03-25 13:07:27 -070037#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070038#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039
George Liu647b3cd2021-07-05 12:43:56 +080040#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070041#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080042#include <optional>
Ed Tanous26702d02021-11-03 15:02:33 -070043#include <span>
Jason M. Billscd225da2019-05-08 15:31:57 -070044#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080045#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070046
47namespace redfish
48{
49
Gunnar Mills1214b7e2020-06-04 10:11:30 -050050constexpr char const* crashdumpObject = "com.intel.crashdump";
51constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050052constexpr char const* crashdumpInterface = "com.intel.crashdump";
53constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070054 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050055constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070056 "com.intel.crashdump.OnDemand";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070057constexpr char const* crashdumpTelemetryInterface =
58 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070059
Ed Tanousfffb8c12022-02-07 23:53:03 -080060namespace registries
Jason M. Bills4851d452019-03-28 11:27:48 -070061{
Ed Tanous26702d02021-11-03 15:02:33 -070062static const Message*
63 getMessageFromRegistry(const std::string& messageKey,
64 const std::span<const MessageEntry> registry)
Jason M. Bills4851d452019-03-28 11:27:48 -070065{
Ed Tanous002d39b2022-05-31 08:59:27 -070066 std::span<const MessageEntry>::iterator messageIt =
67 std::find_if(registry.begin(), registry.end(),
68 [&messageKey](const MessageEntry& messageEntry) {
69 return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
Ed Tanous26702d02021-11-03 15:02:33 -070070 });
71 if (messageIt != registry.end())
Jason M. Bills4851d452019-03-28 11:27:48 -070072 {
73 return &messageIt->second;
74 }
75
76 return nullptr;
77}
78
Gunnar Mills1214b7e2020-06-04 10:11:30 -050079static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070080{
81 // Redfish MessageIds are in the form
82 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
83 // the right Message
84 std::vector<std::string> fields;
85 fields.reserve(4);
86 boost::split(fields, messageID, boost::is_any_of("."));
Gunnar Mills1214b7e2020-06-04 10:11:30 -050087 std::string& registryName = fields[0];
88 std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -070089
90 // Find the right registry and check it for the MessageKey
91 if (std::string(base::header.registryPrefix) == registryName)
92 {
93 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070094 messageKey, std::span<const MessageEntry>(base::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -070095 }
96 if (std::string(openbmc::header.registryPrefix) == registryName)
97 {
98 return getMessageFromRegistry(
Ed Tanous26702d02021-11-03 15:02:33 -070099 messageKey, std::span<const MessageEntry>(openbmc::registry));
Jason M. Bills4851d452019-03-28 11:27:48 -0700100 }
101 return nullptr;
102}
Ed Tanousfffb8c12022-02-07 23:53:03 -0800103} // namespace registries
Jason M. Bills4851d452019-03-28 11:27:48 -0700104
James Feistf6150402019-01-08 10:36:20 -0800105namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -0700106
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500107inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700108{
Ed Tanousd4d25792020-09-29 15:15:03 -0700109 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
110 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
111 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
112 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700113 {
114 return "Critical";
115 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700116 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
117 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
118 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -0700119 {
120 return "OK";
121 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700122 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -0700123 {
124 return "Warning";
125 }
126 return "";
127}
128
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700129inline static int getJournalMetadata(sd_journal* journal,
130 const std::string_view& field,
131 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700132{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500133 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700134 size_t length = 0;
135 int ret = 0;
136 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800137 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
138 const void** dataVoid = reinterpret_cast<const void**>(&data);
139
140 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700141 if (ret < 0)
142 {
143 return ret;
144 }
Ed Tanous39e77502019-03-04 17:35:53 -0800145 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700146 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000147 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700148 return ret;
149}
150
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700151inline static int getJournalMetadata(sd_journal* journal,
152 const std::string_view& field,
153 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700154{
155 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800156 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700157 // Get the metadata from the requested field of the journal entry
158 ret = getJournalMetadata(journal, field, metadata);
159 if (ret < 0)
160 {
161 return ret;
162 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000163 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700164 return ret;
165}
166
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700167inline static bool getEntryTimestamp(sd_journal* journal,
168 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800169{
170 int ret = 0;
171 uint64_t timestamp = 0;
172 ret = sd_journal_get_realtime_usec(journal, &timestamp);
173 if (ret < 0)
174 {
175 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
176 << strerror(-ret);
177 return false;
178 }
Nan Zhou1d8782e2021-11-29 22:23:18 -0800179 entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500180 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800181}
Ed Tanous50b8a432022-02-03 16:29:50 -0800182
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700183inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
184 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700185{
186 int ret = 0;
187 static uint64_t prevTs = 0;
188 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700189 if (firstEntry)
190 {
191 prevTs = 0;
192 }
193
Jason M. Bills16428a12018-11-02 12:42:29 -0700194 // Get the entry timestamp
195 uint64_t curTs = 0;
196 ret = sd_journal_get_realtime_usec(journal, &curTs);
197 if (ret < 0)
198 {
199 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
200 << strerror(-ret);
201 return false;
202 }
203 // If the timestamp isn't unique, increment the index
204 if (curTs == prevTs)
205 {
206 index++;
207 }
208 else
209 {
210 // Otherwise, reset it
211 index = 0;
212 }
213 // Save the timestamp
214 prevTs = curTs;
215
216 entryID = std::to_string(curTs);
217 if (index > 0)
218 {
219 entryID += "_" + std::to_string(index);
220 }
221 return true;
222}
223
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500224static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700225 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700226{
Ed Tanous271584a2019-07-09 16:24:22 -0700227 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700228 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700229 if (firstEntry)
230 {
231 prevTs = 0;
232 }
233
Jason M. Bills95820182019-04-22 16:25:34 -0700234 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700235 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700236 std::tm timeStruct = {};
237 std::istringstream entryStream(logEntry);
238 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
239 {
240 curTs = std::mktime(&timeStruct);
241 }
242 // If the timestamp isn't unique, increment the index
243 if (curTs == prevTs)
244 {
245 index++;
246 }
247 else
248 {
249 // Otherwise, reset it
250 index = 0;
251 }
252 // Save the timestamp
253 prevTs = curTs;
254
255 entryID = std::to_string(curTs);
256 if (index > 0)
257 {
258 entryID += "_" + std::to_string(index);
259 }
260 return true;
261}
262
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700263inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800264 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
265 const std::string& entryID, uint64_t& timestamp,
266 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700267{
268 if (entryID.empty())
269 {
270 return false;
271 }
272 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800273 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700274
Ed Tanous81ce6092020-12-17 16:54:55 +0000275 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800276 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700277 {
278 // Timestamp has an index
279 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800280 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700281 indexStr.remove_prefix(underscorePos + 1);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700282 auto [ptr, ec] = std::from_chars(
283 indexStr.data(), indexStr.data() + indexStr.size(), index);
284 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700285 {
Ed Tanousace85d62021-10-26 12:45:59 -0700286 messages::resourceMissingAtURI(
287 asyncResp->res, crow::utility::urlFromPieces(entryID));
Jason M. Bills16428a12018-11-02 12:42:29 -0700288 return false;
289 }
290 }
291 // Timestamp has no index
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700292 auto [ptr, ec] =
293 std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
294 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700295 {
Ed Tanousace85d62021-10-26 12:45:59 -0700296 messages::resourceMissingAtURI(asyncResp->res,
297 crow::utility::urlFromPieces(entryID));
Jason M. Bills16428a12018-11-02 12:42:29 -0700298 return false;
299 }
300 return true;
301}
302
Jason M. Bills95820182019-04-22 16:25:34 -0700303static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500304 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700305{
306 static const std::filesystem::path redfishLogDir = "/var/log";
307 static const std::string redfishLogFilename = "redfish";
308
309 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500310 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700311 std::filesystem::directory_iterator(redfishLogDir))
312 {
313 // If we find a redfish log file, save the path
314 std::string filename = dirEnt.path().filename();
315 if (boost::starts_with(filename, redfishLogFilename))
316 {
317 redfishLogFiles.emplace_back(redfishLogDir / filename);
318 }
319 }
320 // As the log files rotate, they are appended with a ".#" that is higher for
321 // the older logs. Since we don't expect more than 10 log files, we
322 // can just sort the list to get them in order from newest to oldest
323 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
324
325 return !redfishLogFiles.empty();
326}
327
Claire Weinanfdd26902022-03-01 14:18:25 -0800328std::string getDumpEntriesPath(const std::string& dumpType)
329{
330 std::string entriesPath;
331
332 if (dumpType == "BMC")
333 {
334 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
335 }
336 else if (dumpType == "FaultLog")
337 {
338 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
339 }
340 else if (dumpType == "System")
341 {
342 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
343 }
344 else
345 {
346 BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
347 << dumpType;
348 }
349
350 // Returns empty string on error
351 return entriesPath;
352}
353
zhanghch058d1b46d2021-04-01 11:18:24 +0800354inline void
355 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
356 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500357{
Claire Weinanfdd26902022-03-01 14:18:25 -0800358 std::string entriesPath = getDumpEntriesPath(dumpType);
359 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500360 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500361 messages::internalError(asyncResp->res);
362 return;
363 }
364
365 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800366 [asyncResp, entriesPath,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800367 dumpType](const boost::system::error_code ec,
368 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700369 if (ec)
370 {
371 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
372 messages::internalError(asyncResp->res);
373 return;
374 }
375
Claire Weinanfdd26902022-03-01 14:18:25 -0800376 // Remove ending slash
377 std::string odataIdStr = entriesPath;
378 if (!odataIdStr.empty())
379 {
380 odataIdStr.pop_back();
381 }
382
383 asyncResp->res.jsonValue["@odata.type"] =
384 "#LogEntryCollection.LogEntryCollection";
385 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
386 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
387 asyncResp->res.jsonValue["Description"] =
388 "Collection of " + dumpType + " Dump Entries";
389
Ed Tanous002d39b2022-05-31 08:59:27 -0700390 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
391 entriesArray = nlohmann::json::array();
392 std::string dumpEntryPath =
393 "/xyz/openbmc_project/dump/" +
394 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
395
396 std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
397 return AlphanumLess<std::string>()(l.first.filename(),
398 r.first.filename());
399 });
400
401 for (auto& object : resp)
402 {
403 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500404 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700405 continue;
406 }
407 uint64_t timestamp = 0;
408 uint64_t size = 0;
409 std::string dumpStatus;
410 nlohmann::json thisEntry;
411
412 std::string entryID = object.first.filename();
413 if (entryID.empty())
414 {
415 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500416 }
417
Ed Tanous002d39b2022-05-31 08:59:27 -0700418 for (auto& interfaceMap : object.second)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500419 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700420 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500421 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700422 for (const auto& propertyMap : interfaceMap.second)
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500423 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700424 if (propertyMap.first == "Status")
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500425 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700426 const auto* status =
427 std::get_if<std::string>(&propertyMap.second);
428 if (status == nullptr)
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500429 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700430 messages::internalError(asyncResp->res);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500431 break;
432 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700433 dumpStatus = *status;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500434 }
435 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700436 }
437 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
438 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500439
Ed Tanous002d39b2022-05-31 08:59:27 -0700440 for (auto& propertyMap : interfaceMap.second)
441 {
442 if (propertyMap.first == "Size")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500443 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700444 const auto* sizePtr =
445 std::get_if<uint64_t>(&propertyMap.second);
446 if (sizePtr == nullptr)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500447 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700448 messages::internalError(asyncResp->res);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500449 break;
450 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700451 size = *sizePtr;
452 break;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500453 }
454 }
455 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700456 else if (interfaceMap.first ==
457 "xyz.openbmc_project.Time.EpochTime")
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500458 {
Asmitha Karunanithi35440d12021-09-07 11:17:57 -0500459
Ed Tanous002d39b2022-05-31 08:59:27 -0700460 for (const auto& propertyMap : interfaceMap.second)
461 {
462 if (propertyMap.first == "Elapsed")
463 {
464 const uint64_t* usecsTimeStamp =
465 std::get_if<uint64_t>(&propertyMap.second);
466 if (usecsTimeStamp == nullptr)
467 {
468 messages::internalError(asyncResp->res);
469 break;
470 }
471 timestamp = (*usecsTimeStamp / 1000 / 1000);
472 break;
473 }
474 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500475 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500476 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700477
478 if (dumpStatus !=
479 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
480 !dumpStatus.empty())
481 {
482 // Dump status is not Complete, no need to enumerate
483 continue;
484 }
485
486 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800487 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700488 thisEntry["Id"] = entryID;
489 thisEntry["EntryType"] = "Event";
490 thisEntry["Created"] = crow::utility::getDateTimeUint(timestamp);
491 thisEntry["Name"] = dumpType + " Dump Entry";
492
Ed Tanous002d39b2022-05-31 08:59:27 -0700493 if (dumpType == "BMC")
494 {
495 thisEntry["DiagnosticDataType"] = "Manager";
496 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800497 entriesPath + entryID + "/attachment";
498 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700499 }
500 else if (dumpType == "System")
501 {
502 thisEntry["DiagnosticDataType"] = "OEM";
503 thisEntry["OEMDiagnosticDataType"] = "System";
504 thisEntry["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800505 entriesPath + entryID + "/attachment";
506 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700507 }
508 entriesArray.push_back(std::move(thisEntry));
509 }
510 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500511 },
512 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
513 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
514}
515
zhanghch058d1b46d2021-04-01 11:18:24 +0800516inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700517 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800518 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500519{
Claire Weinanfdd26902022-03-01 14:18:25 -0800520 std::string entriesPath = getDumpEntriesPath(dumpType);
521 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500522 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500523 messages::internalError(asyncResp->res);
524 return;
525 }
526
527 crow::connections::systemBus->async_method_call(
Claire Weinanfdd26902022-03-01 14:18:25 -0800528 [asyncResp, entryID, dumpType,
529 entriesPath](const boost::system::error_code ec,
530 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700531 if (ec)
532 {
533 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
534 messages::internalError(asyncResp->res);
535 return;
536 }
537
538 bool foundDumpEntry = false;
539 std::string dumpEntryPath =
540 "/xyz/openbmc_project/dump/" +
541 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
542
543 for (const auto& objectPath : resp)
544 {
545 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500546 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700547 continue;
548 }
549
550 foundDumpEntry = true;
551 uint64_t timestamp = 0;
552 uint64_t size = 0;
553 std::string dumpStatus;
554
555 for (const auto& interfaceMap : objectPath.second)
556 {
557 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
558 {
559 for (const auto& propertyMap : interfaceMap.second)
560 {
561 if (propertyMap.first == "Status")
562 {
563 const std::string* status =
564 std::get_if<std::string>(&propertyMap.second);
565 if (status == nullptr)
566 {
567 messages::internalError(asyncResp->res);
568 break;
569 }
570 dumpStatus = *status;
571 }
572 }
573 }
574 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
575 {
576 for (const auto& propertyMap : interfaceMap.second)
577 {
578 if (propertyMap.first == "Size")
579 {
580 const uint64_t* sizePtr =
581 std::get_if<uint64_t>(&propertyMap.second);
582 if (sizePtr == nullptr)
583 {
584 messages::internalError(asyncResp->res);
585 break;
586 }
587 size = *sizePtr;
588 break;
589 }
590 }
591 }
592 else if (interfaceMap.first ==
593 "xyz.openbmc_project.Time.EpochTime")
594 {
595 for (const auto& propertyMap : interfaceMap.second)
596 {
597 if (propertyMap.first == "Elapsed")
598 {
599 const uint64_t* usecsTimeStamp =
600 std::get_if<uint64_t>(&propertyMap.second);
601 if (usecsTimeStamp == nullptr)
602 {
603 messages::internalError(asyncResp->res);
604 break;
605 }
606 timestamp = *usecsTimeStamp / 1000 / 1000;
607 break;
608 }
609 }
610 }
611 }
612
613 if (dumpStatus !=
614 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
615 !dumpStatus.empty())
616 {
617 // Dump status is not Complete
618 // return not found until status is changed to Completed
619 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
620 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500621 return;
622 }
623
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 asyncResp->res.jsonValue["@odata.type"] =
625 "#LogEntry.v1_8_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800626 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700627 asyncResp->res.jsonValue["Id"] = entryID;
628 asyncResp->res.jsonValue["EntryType"] = "Event";
629 asyncResp->res.jsonValue["Created"] =
630 crow::utility::getDateTimeUint(timestamp);
631 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500632
Ed Tanous002d39b2022-05-31 08:59:27 -0700633 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500634 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700635 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
636 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800637 entriesPath + entryID + "/attachment";
638 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500639 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700640 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500641 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700642 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
643 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
644 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800645 entriesPath + entryID + "/attachment";
646 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500647 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700648 }
649 if (!foundDumpEntry)
650 {
651 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
652 messages::internalError(asyncResp->res);
653 return;
654 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500655 },
656 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
657 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
658}
659
zhanghch058d1b46d2021-04-01 11:18:24 +0800660inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800661 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500662 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500663{
Ed Tanous002d39b2022-05-31 08:59:27 -0700664 auto respHandler =
665 [asyncResp, entryID](const boost::system::error_code ec) {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500666 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
667 if (ec)
668 {
George Liu3de8d8b2021-03-22 17:49:39 +0800669 if (ec.value() == EBADR)
670 {
671 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
672 return;
673 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500674 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
Claire Weinanfdd26902022-03-01 14:18:25 -0800675 << ec << " entryID=" << entryID;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500676 messages::internalError(asyncResp->res);
677 return;
678 }
679 };
680 crow::connections::systemBus->async_method_call(
681 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500682 "/xyz/openbmc_project/dump/" +
683 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
684 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500685 "xyz.openbmc_project.Object.Delete", "Delete");
686}
687
zhanghch058d1b46d2021-04-01 11:18:24 +0800688inline void
Ed Tanous98be3e32021-09-16 15:05:36 -0700689 createDumpTaskCallback(task::Payload&& payload,
zhanghch058d1b46d2021-04-01 11:18:24 +0800690 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
691 const uint32_t& dumpId, const std::string& dumpPath,
692 const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500693{
694 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500695 [dumpId, dumpPath, dumpType](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500696 boost::system::error_code err, sdbusplus::message::message& m,
697 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700698 if (err)
699 {
700 BMCWEB_LOG_ERROR << "Error in creating a dump";
701 taskData->state = "Cancelled";
Asmitha Karunanithi6145ed62020-09-17 23:40:03 -0500702 return task::completed;
Ed Tanous002d39b2022-05-31 08:59:27 -0700703 }
704
705 dbus::utility::DBusInteracesMap interfacesList;
706
707 sdbusplus::message::object_path objPath;
708
709 m.read(objPath, interfacesList);
710
711 if (objPath.str ==
712 "/xyz/openbmc_project/dump/" +
713 std::string(boost::algorithm::to_lower_copy(dumpType)) +
714 "/entry/" + std::to_string(dumpId))
715 {
716 nlohmann::json retMessage = messages::success();
717 taskData->messages.emplace_back(retMessage);
718
719 std::string headerLoc =
720 "Location: " + dumpPath + std::to_string(dumpId);
721 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
722
723 taskData->state = "Completed";
724 return task::completed;
725 }
726 return task::completed;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500727 },
Jason M. Bills4978b632022-02-22 14:17:43 -0800728 "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500729 "member='InterfacesAdded', "
730 "path='/xyz/openbmc_project/dump'");
731
732 task->startTimer(std::chrono::minutes(3));
733 task->populateResp(asyncResp->res);
Ed Tanous98be3e32021-09-16 15:05:36 -0700734 task->payload.emplace(std::move(payload));
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500735}
736
zhanghch058d1b46d2021-04-01 11:18:24 +0800737inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
738 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500739{
Claire Weinanfdd26902022-03-01 14:18:25 -0800740 std::string dumpPath = getDumpEntriesPath(dumpType);
741 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500742 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500743 messages::internalError(asyncResp->res);
744 return;
745 }
746
747 std::optional<std::string> diagnosticDataType;
748 std::optional<std::string> oemDiagnosticDataType;
749
Willy Tu15ed6782021-12-14 11:03:16 -0800750 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500751 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
752 "OEMDiagnosticDataType", oemDiagnosticDataType))
753 {
754 return;
755 }
756
757 if (dumpType == "System")
758 {
759 if (!oemDiagnosticDataType || !diagnosticDataType)
760 {
Jason M. Bills4978b632022-02-22 14:17:43 -0800761 BMCWEB_LOG_ERROR
762 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500763 messages::actionParameterMissing(
764 asyncResp->res, "CollectDiagnosticData",
765 "DiagnosticDataType & OEMDiagnosticDataType");
766 return;
767 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700768 if ((*oemDiagnosticDataType != "System") ||
769 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500770 {
771 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
Ed Tanousace85d62021-10-26 12:45:59 -0700772 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500773 return;
774 }
775 }
776 else if (dumpType == "BMC")
777 {
778 if (!diagnosticDataType)
779 {
George Liu0fda0f12021-11-16 10:06:17 +0800780 BMCWEB_LOG_ERROR
781 << "CreateDump action parameter 'DiagnosticDataType' not found!";
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500782 messages::actionParameterMissing(
783 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
784 return;
785 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700786 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500787 {
788 BMCWEB_LOG_ERROR
789 << "Wrong parameter value passed for 'DiagnosticDataType'";
Ed Tanousace85d62021-10-26 12:45:59 -0700790 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500791 return;
792 }
793 }
794
795 crow::connections::systemBus->async_method_call(
Ed Tanous98be3e32021-09-16 15:05:36 -0700796 [asyncResp, payload(task::Payload(req)), dumpPath,
797 dumpType](const boost::system::error_code ec,
798 const uint32_t& dumpId) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -0700799 if (ec)
800 {
801 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
802 messages::internalError(asyncResp->res);
803 return;
804 }
805 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500806
Ed Tanous002d39b2022-05-31 08:59:27 -0700807 createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath,
808 dumpType);
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500809 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500810 "xyz.openbmc_project.Dump.Manager",
811 "/xyz/openbmc_project/dump/" +
812 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500813 "xyz.openbmc_project.Dump.Create", "CreateDump");
814}
815
zhanghch058d1b46d2021-04-01 11:18:24 +0800816inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
817 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500818{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500819 std::string dumpTypeLowerCopy =
820 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +0800821
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500822 crow::connections::systemBus->async_method_call(
Ed Tanousb9d36b42022-02-26 21:42:46 -0800823 [asyncResp, dumpType](
824 const boost::system::error_code ec,
825 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700826 if (ec)
827 {
828 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
829 messages::internalError(asyncResp->res);
830 return;
831 }
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500832
Ed Tanous002d39b2022-05-31 08:59:27 -0700833 for (const std::string& path : subTreePaths)
834 {
835 sdbusplus::message::object_path objPath(path);
836 std::string logID = objPath.filename();
837 if (logID.empty())
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500838 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700839 continue;
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500840 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700841 deleteDumpEntry(asyncResp, logID, dumpType);
842 }
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500843 },
844 "xyz.openbmc_project.ObjectMapper",
845 "/xyz/openbmc_project/object_mapper",
846 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500847 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
848 std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
849 dumpType});
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500850}
851
Ed Tanousb9d36b42022-02-26 21:42:46 -0800852inline static void
853 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
854 std::string& filename, std::string& timestamp,
855 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700856{
857 for (auto property : params)
858 {
859 if (property.first == "Timestamp")
860 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500861 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500862 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700863 if (value != nullptr)
864 {
865 timestamp = *value;
866 }
867 }
868 else if (property.first == "Filename")
869 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500870 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500871 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700872 if (value != nullptr)
873 {
874 filename = *value;
875 }
876 }
877 else if (property.first == "Log")
878 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500879 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500880 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700881 if (value != nullptr)
882 {
883 logfile = *value;
884 }
885 }
886 }
887}
888
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500889constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700890inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -0700891{
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800892 /**
893 * Functions triggers appropriate requests on DBus
894 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700895 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -0700896 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -0700897 .methods(boost::beast::http::verb::get)(
898 [&app](const crow::Request& req,
899 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000900 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700901 {
902 return;
903 }
904 // Collections don't include the static data added by SubRoute
905 // because it has a duplicate entry for members
906 asyncResp->res.jsonValue["@odata.type"] =
907 "#LogServiceCollection.LogServiceCollection";
908 asyncResp->res.jsonValue["@odata.id"] =
909 "/redfish/v1/Systems/system/LogServices";
910 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
911 asyncResp->res.jsonValue["Description"] =
912 "Collection of LogServices for this Computer System";
913 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
914 logServiceArray = nlohmann::json::array();
915 nlohmann::json::object_t eventLog;
916 eventLog["@odata.id"] =
917 "/redfish/v1/Systems/system/LogServices/EventLog";
918 logServiceArray.push_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500919#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -0700920 nlohmann::json::object_t dumpLog;
921 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
922 logServiceArray.push_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -0600923#endif
924
Jason M. Billsd53dd412019-02-12 17:16:22 -0800925#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -0700926 nlohmann::json::object_t crashdump;
927 crashdump["@odata.id"] =
928 "/redfish/v1/Systems/system/LogServices/Crashdump";
929 logServiceArray.push_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -0800930#endif
Spencer Kub7028eb2021-10-26 15:27:35 +0800931
932#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -0700933 nlohmann::json::object_t hostlogger;
934 hostlogger["@odata.id"] =
935 "/redfish/v1/Systems/system/LogServices/HostLogger";
936 logServiceArray.push_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +0800937#endif
Ed Tanous002d39b2022-05-31 08:59:27 -0700938 asyncResp->res.jsonValue["Members@odata.count"] =
939 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800940
Ed Tanous002d39b2022-05-31 08:59:27 -0700941 crow::connections::systemBus->async_method_call(
942 [asyncResp](const boost::system::error_code ec,
943 const dbus::utility::MapperGetSubTreePathsResponse&
944 subtreePath) {
945 if (ec)
946 {
947 BMCWEB_LOG_ERROR << ec;
948 return;
949 }
Ed Tanous45ca1b82022-03-25 13:07:27 -0700950
Ed Tanous002d39b2022-05-31 08:59:27 -0700951 for (const auto& pathStr : subtreePath)
952 {
953 if (pathStr.find("PostCode") != std::string::npos)
954 {
955 nlohmann::json& logServiceArrayLocal =
956 asyncResp->res.jsonValue["Members"];
957 logServiceArrayLocal.push_back(
958 {{"@odata.id",
959 "/redfish/v1/Systems/system/LogServices/PostCodes"}});
960 asyncResp->res.jsonValue["Members@odata.count"] =
961 logServiceArrayLocal.size();
962 return;
963 }
964 }
965 },
966 "xyz.openbmc_project.ObjectMapper",
967 "/xyz/openbmc_project/object_mapper",
968 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
969 std::array<const char*, 1>{postCodeIface});
Ed Tanous45ca1b82022-03-25 13:07:27 -0700970 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700971}
972
973inline void requestRoutesEventLogService(App& app)
974{
975 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -0700976 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -0700977 .methods(boost::beast::http::verb::get)(
978 [&app](const crow::Request& req,
979 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000980 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700981 {
982 return;
983 }
984 asyncResp->res.jsonValue["@odata.id"] =
985 "/redfish/v1/Systems/system/LogServices/EventLog";
986 asyncResp->res.jsonValue["@odata.type"] =
987 "#LogService.v1_1_0.LogService";
988 asyncResp->res.jsonValue["Name"] = "Event Log Service";
989 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
990 asyncResp->res.jsonValue["Id"] = "EventLog";
991 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +0530992
Ed Tanous002d39b2022-05-31 08:59:27 -0700993 std::pair<std::string, std::string> redfishDateTimeOffset =
994 crow::utility::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +0530995
Ed Tanous002d39b2022-05-31 08:59:27 -0700996 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
997 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
998 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +0530999
Ed Tanous002d39b2022-05-31 08:59:27 -07001000 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1001 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1002 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001003
Ed Tanous002d39b2022-05-31 08:59:27 -07001004 {"target",
1005 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001006 });
1007}
1008
1009inline void requestRoutesJournalEventLogClear(App& app)
1010{
Jason M. Bills4978b632022-02-22 14:17:43 -08001011 BMCWEB_ROUTE(
1012 app,
1013 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001014 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001015 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001016 [&app](const crow::Request& req,
1017 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001018 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001019 {
1020 return;
1021 }
1022 // Clear the EventLog by deleting the log files
1023 std::vector<std::filesystem::path> redfishLogFiles;
1024 if (getRedfishLogFiles(redfishLogFiles))
1025 {
1026 for (const std::filesystem::path& file : redfishLogFiles)
1027 {
1028 std::error_code ec;
1029 std::filesystem::remove(file, ec);
1030 }
1031 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001032
Ed Tanous002d39b2022-05-31 08:59:27 -07001033 // Reload rsyslog so it knows to start new log files
1034 crow::connections::systemBus->async_method_call(
1035 [asyncResp](const boost::system::error_code ec) {
1036 if (ec)
1037 {
1038 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1039 messages::internalError(asyncResp->res);
1040 return;
1041 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001042
Ed Tanous002d39b2022-05-31 08:59:27 -07001043 messages::success(asyncResp->res);
1044 },
1045 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1046 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1047 "replace");
1048 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001049}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001050
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001051static int fillEventLogEntryJson(const std::string& logEntryID,
Ed Tanousb5a76932020-09-29 16:16:58 -07001052 const std::string& logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001053 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001054{
Jason M. Bills95820182019-04-22 16:25:34 -07001055 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001056 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001057 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001058 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001059 {
1060 return 1;
1061 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001062 std::string timestamp = logEntry.substr(0, space);
1063 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001064 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001065 if (entryStart == std::string::npos)
1066 {
1067 return 1;
1068 }
1069 std::string_view entry(logEntry);
1070 entry.remove_prefix(entryStart);
1071 // Use split to separate the entry into its fields
1072 std::vector<std::string> logEntryFields;
1073 boost::split(logEntryFields, entry, boost::is_any_of(","),
1074 boost::token_compress_on);
1075 // We need at least a MessageId to be valid
Ed Tanous26f69762022-01-25 09:49:11 -08001076 if (logEntryFields.empty())
Jason M. Billscd225da2019-05-08 15:31:57 -07001077 {
1078 return 1;
1079 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001080 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001081
Jason M. Bills4851d452019-03-28 11:27:48 -07001082 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001083 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001084
Sui Chen54417b02022-03-24 14:59:52 -07001085 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001086 {
Sui Chen54417b02022-03-24 14:59:52 -07001087 BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1088 return 0;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001089 }
1090
Sui Chen54417b02022-03-24 14:59:52 -07001091 std::string msg = message->message;
1092
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001093 // Get the MessageArgs from the log if there are any
Ed Tanous26702d02021-11-03 15:02:33 -07001094 std::span<std::string> messageArgs;
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001095 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001096 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001097 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001098 // If the first string is empty, assume there are no MessageArgs
1099 std::size_t messageArgsSize = 0;
1100 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001101 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001102 messageArgsSize = logEntryFields.size() - 1;
1103 }
1104
Ed Tanous23a21a12020-07-25 04:45:05 +00001105 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001106
1107 // Fill the MessageArgs into the Message
1108 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001109 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001110 {
1111 std::string argStr = "%" + std::to_string(++i);
1112 size_t argPos = msg.find(argStr);
1113 if (argPos != std::string::npos)
1114 {
1115 msg.replace(argPos, argStr.length(), messageArg);
1116 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001117 }
1118 }
1119
Jason M. Bills95820182019-04-22 16:25:34 -07001120 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1121 // format which matches the Redfish format except for the fractional seconds
1122 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001123 std::size_t dot = timestamp.find_first_of('.');
1124 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001125 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001126 {
Jason M. Bills95820182019-04-22 16:25:34 -07001127 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001128 }
1129
1130 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001131 logEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08001132 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001133 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001134 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001135 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001136 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001137 {"Id", logEntryID},
1138 {"Message", std::move(msg)},
1139 {"MessageId", std::move(messageID)},
Ed Tanousf23b7292020-10-15 09:41:17 -07001140 {"MessageArgs", messageArgs},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001141 {"EntryType", "Event"},
Sui Chen54417b02022-03-24 14:59:52 -07001142 {"Severity", message->messageSeverity},
Jason M. Bills95820182019-04-22 16:25:34 -07001143 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001144 return 0;
1145}
1146
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001147inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001148{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001149 BMCWEB_ROUTE(app,
1150 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001151 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001152 .methods(boost::beast::http::verb::get)(
1153 [&app](const crow::Request& req,
1154 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1155 query_param::QueryCapabilities capabilities = {
1156 .canDelegateTop = true,
1157 .canDelegateSkip = true,
1158 };
1159 query_param::Query delegatedQuery;
1160 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001161 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001162 {
1163 return;
1164 }
1165 // Collections don't include the static data added by SubRoute
1166 // because it has a duplicate entry for members
1167 asyncResp->res.jsonValue["@odata.type"] =
1168 "#LogEntryCollection.LogEntryCollection";
1169 asyncResp->res.jsonValue["@odata.id"] =
1170 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1171 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1172 asyncResp->res.jsonValue["Description"] =
1173 "Collection of System Event Log Entries";
1174
1175 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1176 logEntryArray = nlohmann::json::array();
1177 // Go through the log files and create a unique ID for each
1178 // entry
1179 std::vector<std::filesystem::path> redfishLogFiles;
1180 getRedfishLogFiles(redfishLogFiles);
1181 uint64_t entryCount = 0;
1182 std::string logEntry;
1183
1184 // Oldest logs are in the last file, so start there and loop
1185 // backwards
1186 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1187 it++)
1188 {
1189 std::ifstream logStream(*it);
1190 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001191 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001192 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001193 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001194
Ed Tanous002d39b2022-05-31 08:59:27 -07001195 // Reset the unique ID on the first entry
1196 bool firstEntry = true;
1197 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001198 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001199 entryCount++;
1200 // Handle paging using skip (number of entries to skip
1201 // from the start) and top (number of entries to
1202 // display)
1203 if (entryCount <= delegatedQuery.skip ||
1204 entryCount > delegatedQuery.skip + delegatedQuery.top)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001205 {
Jason M. Bills4978b632022-02-22 14:17:43 -08001206 continue;
1207 }
1208
Ed Tanous002d39b2022-05-31 08:59:27 -07001209 std::string idStr;
1210 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001211 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001212 continue;
1213 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001214
Ed Tanous002d39b2022-05-31 08:59:27 -07001215 if (firstEntry)
1216 {
1217 firstEntry = false;
1218 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001219
Ed Tanous002d39b2022-05-31 08:59:27 -07001220 logEntryArray.push_back({});
1221 nlohmann::json& bmcLogEntry = logEntryArray.back();
1222 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
1223 {
1224 messages::internalError(asyncResp->res);
1225 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001226 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001227 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001228 }
1229 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1230 if (delegatedQuery.skip + delegatedQuery.top < entryCount)
1231 {
1232 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1233 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
1234 std::to_string(delegatedQuery.skip + delegatedQuery.top);
1235 }
Jason M. Bills4978b632022-02-22 14:17:43 -08001236 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001237}
Chicago Duan336e96c2019-07-15 14:22:08 +08001238
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001239inline void requestRoutesJournalEventLogEntry(App& app)
1240{
1241 BMCWEB_ROUTE(
1242 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001243 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001244 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001245 [&app](const crow::Request& req,
1246 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1247 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001248 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001249 {
1250 return;
1251 }
1252 const std::string& targetID = param;
1253
1254 // Go through the log files and check the unique ID for each
1255 // entry to find the target entry
1256 std::vector<std::filesystem::path> redfishLogFiles;
1257 getRedfishLogFiles(redfishLogFiles);
1258 std::string logEntry;
1259
1260 // Oldest logs are in the last file, so start there and loop
1261 // backwards
1262 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1263 it++)
1264 {
1265 std::ifstream logStream(*it);
1266 if (!logStream.is_open())
1267 {
1268 continue;
1269 }
1270
1271 // Reset the unique ID on the first entry
1272 bool firstEntry = true;
1273 while (std::getline(logStream, logEntry))
1274 {
1275 std::string idStr;
1276 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001277 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001278 continue;
1279 }
1280
1281 if (firstEntry)
1282 {
1283 firstEntry = false;
1284 }
1285
1286 if (idStr == targetID)
1287 {
1288 if (fillEventLogEntryJson(idStr, logEntry,
1289 asyncResp->res.jsonValue) != 0)
1290 {
1291 messages::internalError(asyncResp->res);
1292 return;
1293 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001294 return;
1295 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001296 }
1297 }
1298 // Requested ID was not found
1299 messages::resourceMissingAtURI(asyncResp->res,
1300 crow::utility::urlFromPieces(targetID));
1301 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001302}
1303
1304inline void requestRoutesDBusEventLogEntryCollection(App& app)
1305{
1306 BMCWEB_ROUTE(app,
1307 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001308 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001309 .methods(boost::beast::http::verb::get)(
1310 [&app](const crow::Request& req,
1311 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001312 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001313 {
1314 return;
1315 }
1316 // Collections don't include the static data added by SubRoute
1317 // because it has a duplicate entry for members
1318 asyncResp->res.jsonValue["@odata.type"] =
1319 "#LogEntryCollection.LogEntryCollection";
1320 asyncResp->res.jsonValue["@odata.id"] =
1321 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1322 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1323 asyncResp->res.jsonValue["Description"] =
1324 "Collection of System Event Log Entries";
1325
1326 // DBus implementation of EventLog/Entries
1327 // Make call to Logging Service to find all log entry objects
1328 crow::connections::systemBus->async_method_call(
1329 [asyncResp](const boost::system::error_code ec,
1330 const dbus::utility::ManagedObjectType& resp) {
1331 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001332 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001333 // TODO Handle for specific error code
1334 BMCWEB_LOG_ERROR
1335 << "getLogEntriesIfaceData resp_handler got error " << ec;
1336 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001337 return;
1338 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001339 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1340 entriesArray = nlohmann::json::array();
1341 for (const auto& objectPath : resp)
1342 {
1343 const uint32_t* id = nullptr;
1344 const uint64_t* timestamp = nullptr;
1345 const uint64_t* updateTimestamp = nullptr;
1346 const std::string* severity = nullptr;
1347 const std::string* message = nullptr;
1348 const std::string* filePath = nullptr;
1349 bool resolved = false;
1350 for (const auto& interfaceMap : objectPath.second)
1351 {
1352 if (interfaceMap.first ==
1353 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001354 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001355 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001356 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001357 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001358 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001359 id = std::get_if<uint32_t>(&propertyMap.second);
1360 }
1361 else if (propertyMap.first == "Timestamp")
1362 {
1363 timestamp =
1364 std::get_if<uint64_t>(&propertyMap.second);
1365 }
1366 else if (propertyMap.first == "UpdateTimestamp")
1367 {
1368 updateTimestamp =
1369 std::get_if<uint64_t>(&propertyMap.second);
1370 }
1371 else if (propertyMap.first == "Severity")
1372 {
1373 severity = std::get_if<std::string>(
1374 &propertyMap.second);
1375 }
1376 else if (propertyMap.first == "Message")
1377 {
1378 message = std::get_if<std::string>(
1379 &propertyMap.second);
1380 }
1381 else if (propertyMap.first == "Resolved")
1382 {
1383 const bool* resolveptr =
1384 std::get_if<bool>(&propertyMap.second);
1385 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001386 {
1387 messages::internalError(asyncResp->res);
1388 return;
1389 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001390 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001391 }
1392 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001393 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001394 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001395 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001396 messages::internalError(asyncResp->res);
1397 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001398 }
1399 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001400 else if (interfaceMap.first ==
1401 "xyz.openbmc_project.Common.FilePath")
1402 {
1403 for (const auto& propertyMap : interfaceMap.second)
1404 {
1405 if (propertyMap.first == "Path")
1406 {
1407 filePath = std::get_if<std::string>(
1408 &propertyMap.second);
1409 }
1410 }
1411 }
1412 }
1413 // Object path without the
1414 // xyz.openbmc_project.Logging.Entry interface, ignore
1415 // and continue.
1416 if (id == nullptr || message == nullptr ||
1417 severity == nullptr || timestamp == nullptr ||
1418 updateTimestamp == nullptr)
1419 {
1420 continue;
1421 }
1422 entriesArray.push_back({});
1423 nlohmann::json& thisEntry = entriesArray.back();
1424 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1425 thisEntry["@odata.id"] =
1426 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1427 std::to_string(*id);
1428 thisEntry["Name"] = "System Event Log Entry";
1429 thisEntry["Id"] = std::to_string(*id);
1430 thisEntry["Message"] = *message;
1431 thisEntry["Resolved"] = resolved;
1432 thisEntry["EntryType"] = "Event";
1433 thisEntry["Severity"] =
1434 translateSeverityDbusToRedfish(*severity);
1435 thisEntry["Created"] =
1436 crow::utility::getDateTimeUintMs(*timestamp);
1437 thisEntry["Modified"] =
1438 crow::utility::getDateTimeUintMs(*updateTimestamp);
1439 if (filePath != nullptr)
1440 {
1441 thisEntry["AdditionalDataURI"] =
1442 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1443 std::to_string(*id) + "/attachment";
1444 }
1445 }
1446 std::sort(
1447 entriesArray.begin(), entriesArray.end(),
1448 [](const nlohmann::json& left, const nlohmann::json& right) {
1449 return (left["Id"] <= right["Id"]);
1450 });
1451 asyncResp->res.jsonValue["Members@odata.count"] =
1452 entriesArray.size();
1453 },
1454 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1455 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001456 });
1457}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001458
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001459inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001460{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001461 BMCWEB_ROUTE(
1462 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001463 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001464 .methods(boost::beast::http::verb::get)(
1465 [&app](const crow::Request& req,
1466 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1467 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001468 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001469 {
1470 return;
1471 }
1472 std::string entryID = param;
1473 dbus::utility::escapePathForDbus(entryID);
1474
1475 // DBus implementation of EventLog/Entries
1476 // Make call to Logging Service to find all log entry objects
1477 crow::connections::systemBus->async_method_call(
1478 [asyncResp, entryID](const boost::system::error_code ec,
1479 const dbus::utility::DBusPropertiesMap& resp) {
1480 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001481 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001482 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1483 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001484 return;
1485 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001486 if (ec)
1487 {
1488 BMCWEB_LOG_ERROR
1489 << "EventLogEntry (DBus) resp_handler got error " << ec;
1490 messages::internalError(asyncResp->res);
1491 return;
1492 }
1493 const uint32_t* id = nullptr;
1494 const uint64_t* timestamp = nullptr;
1495 const uint64_t* updateTimestamp = nullptr;
1496 const std::string* severity = nullptr;
1497 const std::string* message = nullptr;
1498 const std::string* filePath = nullptr;
1499 bool resolved = false;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001500
Ed Tanous002d39b2022-05-31 08:59:27 -07001501 for (const auto& propertyMap : resp)
1502 {
1503 if (propertyMap.first == "Id")
1504 {
1505 id = std::get_if<uint32_t>(&propertyMap.second);
1506 }
1507 else if (propertyMap.first == "Timestamp")
1508 {
1509 timestamp = std::get_if<uint64_t>(&propertyMap.second);
1510 }
1511 else if (propertyMap.first == "UpdateTimestamp")
1512 {
1513 updateTimestamp =
1514 std::get_if<uint64_t>(&propertyMap.second);
1515 }
1516 else if (propertyMap.first == "Severity")
1517 {
1518 severity = std::get_if<std::string>(&propertyMap.second);
1519 }
1520 else if (propertyMap.first == "Message")
1521 {
1522 message = std::get_if<std::string>(&propertyMap.second);
1523 }
1524 else if (propertyMap.first == "Resolved")
1525 {
1526 const bool* resolveptr =
1527 std::get_if<bool>(&propertyMap.second);
1528 if (resolveptr == nullptr)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001529 {
1530 messages::internalError(asyncResp->res);
1531 return;
1532 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001533 resolved = *resolveptr;
1534 }
1535 else if (propertyMap.first == "Path")
1536 {
1537 filePath = std::get_if<std::string>(&propertyMap.second);
1538 }
1539 }
1540 if (id == nullptr || message == nullptr || severity == nullptr ||
1541 timestamp == nullptr || updateTimestamp == nullptr)
1542 {
1543 messages::internalError(asyncResp->res);
1544 return;
1545 }
1546 asyncResp->res.jsonValue["@odata.type"] =
1547 "#LogEntry.v1_8_0.LogEntry";
1548 asyncResp->res.jsonValue["@odata.id"] =
1549 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1550 std::to_string(*id);
1551 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1552 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1553 asyncResp->res.jsonValue["Message"] = *message;
1554 asyncResp->res.jsonValue["Resolved"] = resolved;
1555 asyncResp->res.jsonValue["EntryType"] = "Event";
1556 asyncResp->res.jsonValue["Severity"] =
1557 translateSeverityDbusToRedfish(*severity);
1558 asyncResp->res.jsonValue["Created"] =
1559 crow::utility::getDateTimeUintMs(*timestamp);
1560 asyncResp->res.jsonValue["Modified"] =
1561 crow::utility::getDateTimeUintMs(*updateTimestamp);
1562 if (filePath != nullptr)
1563 {
1564 asyncResp->res.jsonValue["AdditionalDataURI"] =
1565 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1566 std::to_string(*id) + "/attachment";
1567 }
1568 },
1569 "xyz.openbmc_project.Logging",
1570 "/xyz/openbmc_project/logging/entry/" + entryID,
1571 "org.freedesktop.DBus.Properties", "GetAll", "");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001572 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001573
1574 BMCWEB_ROUTE(
1575 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001576 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001577 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001578 [&app](const crow::Request& req,
1579 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1580 const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001581 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001582 {
1583 return;
1584 }
1585 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001586
Ed Tanous002d39b2022-05-31 08:59:27 -07001587 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1588 resolved))
1589 {
1590 return;
1591 }
1592 BMCWEB_LOG_DEBUG << "Set Resolved";
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001593
Ed Tanous002d39b2022-05-31 08:59:27 -07001594 crow::connections::systemBus->async_method_call(
1595 [asyncResp, entryId](const boost::system::error_code ec) {
1596 if (ec)
1597 {
1598 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1599 messages::internalError(asyncResp->res);
1600 return;
1601 }
1602 },
1603 "xyz.openbmc_project.Logging",
1604 "/xyz/openbmc_project/logging/entry/" + entryId,
1605 "org.freedesktop.DBus.Properties", "Set",
1606 "xyz.openbmc_project.Logging.Entry", "Resolved",
1607 dbus::utility::DbusVariantType(*resolved));
1608 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001609
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001610 BMCWEB_ROUTE(
1611 app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001612 .privileges(redfish::privileges::deleteLogEntry)
1613
Ed Tanous002d39b2022-05-31 08:59:27 -07001614 .methods(boost::beast::http::verb::delete_)(
1615 [&app](const crow::Request& req,
1616 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1617 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001618 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001619 {
1620 return;
1621 }
1622 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1623
1624 std::string entryID = param;
1625
1626 dbus::utility::escapePathForDbus(entryID);
1627
1628 // Process response from Logging service.
1629 auto respHandler =
1630 [asyncResp, entryID](const boost::system::error_code ec) {
1631 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1632 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001633 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001634 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001635 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001636 messages::resourceNotFound(asyncResp->res, "LogEntry",
1637 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001638 return;
1639 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001640 // TODO Handle for specific error code
1641 BMCWEB_LOG_ERROR
1642 << "EventLogEntry (DBus) doDelete respHandler got error "
1643 << ec;
1644 asyncResp->res.result(
1645 boost::beast::http::status::internal_server_error);
1646 return;
1647 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001648
Ed Tanous002d39b2022-05-31 08:59:27 -07001649 asyncResp->res.result(boost::beast::http::status::ok);
1650 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001651
Ed Tanous002d39b2022-05-31 08:59:27 -07001652 // Make call to Logging service to request Delete Log
1653 crow::connections::systemBus->async_method_call(
1654 respHandler, "xyz.openbmc_project.Logging",
1655 "/xyz/openbmc_project/logging/entry/" + entryID,
1656 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous45ca1b82022-03-25 13:07:27 -07001657 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001658}
1659
1660inline void requestRoutesDBusEventLogEntryDownload(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001661{
George Liu0fda0f12021-11-16 10:06:17 +08001662 BMCWEB_ROUTE(
1663 app,
1664 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
Ed Tanoused398212021-06-09 17:05:54 -07001665 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001666 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001667 [&app](const crow::Request& req,
1668 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1669 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001670 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001671 {
1672 return;
1673 }
1674 if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
1675 {
1676 asyncResp->res.result(boost::beast::http::status::bad_request);
1677 return;
1678 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001679
Ed Tanous002d39b2022-05-31 08:59:27 -07001680 std::string entryID = param;
1681 dbus::utility::escapePathForDbus(entryID);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001682
Ed Tanous002d39b2022-05-31 08:59:27 -07001683 crow::connections::systemBus->async_method_call(
1684 [asyncResp, entryID](const boost::system::error_code ec,
1685 const sdbusplus::message::unix_fd& unixfd) {
1686 if (ec.value() == EBADR)
1687 {
1688 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1689 entryID);
1690 return;
1691 }
1692 if (ec)
1693 {
1694 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1695 messages::internalError(asyncResp->res);
1696 return;
1697 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001698
Ed Tanous002d39b2022-05-31 08:59:27 -07001699 int fd = -1;
1700 fd = dup(unixfd);
1701 if (fd == -1)
1702 {
1703 messages::internalError(asyncResp->res);
1704 return;
1705 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001706
Ed Tanous002d39b2022-05-31 08:59:27 -07001707 long long int size = lseek(fd, 0, SEEK_END);
1708 if (size == -1)
1709 {
1710 messages::internalError(asyncResp->res);
1711 return;
1712 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001713
Ed Tanous002d39b2022-05-31 08:59:27 -07001714 // Arbitrary max size of 64kb
1715 constexpr int maxFileSize = 65536;
1716 if (size > maxFileSize)
1717 {
1718 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1719 << maxFileSize;
1720 messages::internalError(asyncResp->res);
1721 return;
1722 }
1723 std::vector<char> data(static_cast<size_t>(size));
1724 long long int rc = lseek(fd, 0, SEEK_SET);
1725 if (rc == -1)
1726 {
1727 messages::internalError(asyncResp->res);
1728 return;
1729 }
1730 rc = read(fd, data.data(), data.size());
1731 if ((rc == -1) || (rc != size))
1732 {
1733 messages::internalError(asyncResp->res);
1734 return;
1735 }
1736 close(fd);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001737
Ed Tanous002d39b2022-05-31 08:59:27 -07001738 std::string_view strData(data.data(), data.size());
1739 std::string output = crow::utility::base64encode(strData);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001740
Ed Tanous002d39b2022-05-31 08:59:27 -07001741 asyncResp->res.addHeader("Content-Type",
1742 "application/octet-stream");
1743 asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
1744 asyncResp->res.body() = std::move(output);
1745 },
1746 "xyz.openbmc_project.Logging",
1747 "/xyz/openbmc_project/logging/entry/" + entryID,
1748 "xyz.openbmc_project.Logging.Entry", "GetEntry");
1749 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001750}
1751
Spencer Kub7028eb2021-10-26 15:27:35 +08001752constexpr const char* hostLoggerFolderPath = "/var/log/console";
1753
1754inline bool
1755 getHostLoggerFiles(const std::string& hostLoggerFilePath,
1756 std::vector<std::filesystem::path>& hostLoggerFiles)
1757{
1758 std::error_code ec;
1759 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1760 if (ec)
1761 {
1762 BMCWEB_LOG_ERROR << ec.message();
1763 return false;
1764 }
1765 for (const std::filesystem::directory_entry& it : logPath)
1766 {
1767 std::string filename = it.path().filename();
1768 // Prefix of each log files is "log". Find the file and save the
1769 // path
1770 if (boost::starts_with(filename, "log"))
1771 {
1772 hostLoggerFiles.emplace_back(it.path());
1773 }
1774 }
1775 // As the log files rotate, they are appended with a ".#" that is higher for
1776 // the older logs. Since we start from oldest logs, sort the name in
1777 // descending order.
1778 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1779 AlphanumLess<std::string>());
1780
1781 return true;
1782}
1783
1784inline bool
1785 getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
Ed Tanousc937d2b2022-04-05 09:58:00 -07001786 uint64_t skip, uint64_t top,
Spencer Kub7028eb2021-10-26 15:27:35 +08001787 std::vector<std::string>& logEntries, size_t& logCount)
1788{
1789 GzFileReader logFile;
1790
1791 // Go though all log files and expose host logs.
1792 for (const std::filesystem::path& it : hostLoggerFiles)
1793 {
1794 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1795 {
1796 BMCWEB_LOG_ERROR << "fail to expose host logs";
1797 return false;
1798 }
1799 }
1800 // Get lastMessage from constructor by getter
1801 std::string lastMessage = logFile.getLastMessage();
1802 if (!lastMessage.empty())
1803 {
1804 logCount++;
1805 if (logCount > skip && logCount <= (skip + top))
1806 {
1807 logEntries.push_back(lastMessage);
1808 }
1809 }
1810 return true;
1811}
1812
1813inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1814 const std::string& msg,
1815 nlohmann::json& logEntryJson)
1816{
1817 // Fill in the log entry with the gathered data.
1818 logEntryJson = {
1819 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1820 {"@odata.id",
1821 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1822 logEntryID},
1823 {"Name", "Host Logger Entry"},
1824 {"Id", logEntryID},
1825 {"Message", msg},
1826 {"EntryType", "Oem"},
1827 {"Severity", "OK"},
1828 {"OemRecordFormat", "Host Logger Entry"}};
1829}
1830
1831inline void requestRoutesSystemHostLogger(App& app)
1832{
1833 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1834 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07001835 .methods(boost::beast::http::verb::get)(
1836 [&app](const crow::Request& req,
1837 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001838 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001839 {
1840 return;
1841 }
1842 asyncResp->res.jsonValue["@odata.id"] =
1843 "/redfish/v1/Systems/system/LogServices/HostLogger";
1844 asyncResp->res.jsonValue["@odata.type"] =
1845 "#LogService.v1_1_0.LogService";
1846 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1847 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1848 asyncResp->res.jsonValue["Id"] = "HostLogger";
1849 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1850 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1851 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001852}
1853
1854inline void requestRoutesSystemHostLoggerCollection(App& app)
1855{
1856 BMCWEB_ROUTE(app,
1857 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1858 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 .methods(boost::beast::http::verb::get)(
1860 [&app](const crow::Request& req,
1861 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1862 query_param::QueryCapabilities capabilities = {
1863 .canDelegateTop = true,
1864 .canDelegateSkip = true,
1865 };
1866 query_param::Query delegatedQuery;
1867 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001868 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001869 {
1870 return;
1871 }
1872 asyncResp->res.jsonValue["@odata.id"] =
1873 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1874 asyncResp->res.jsonValue["@odata.type"] =
1875 "#LogEntryCollection.LogEntryCollection";
1876 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1877 asyncResp->res.jsonValue["Description"] =
1878 "Collection of HostLogger Entries";
1879 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1880 logEntryArray = nlohmann::json::array();
1881 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08001882
Ed Tanous002d39b2022-05-31 08:59:27 -07001883 std::vector<std::filesystem::path> hostLoggerFiles;
1884 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1885 {
1886 BMCWEB_LOG_ERROR << "fail to get host log file path";
1887 return;
1888 }
1889
1890 size_t logCount = 0;
1891 // This vector only store the entries we want to expose that
1892 // control by skip and top.
1893 std::vector<std::string> logEntries;
1894 if (!getHostLoggerEntries(hostLoggerFiles, delegatedQuery.skip,
1895 delegatedQuery.top, logEntries, logCount))
1896 {
1897 messages::internalError(asyncResp->res);
1898 return;
1899 }
1900 // If vector is empty, that means skip value larger than total
1901 // log count
1902 if (logEntries.empty())
1903 {
1904 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1905 return;
1906 }
1907 if (!logEntries.empty())
1908 {
1909 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08001910 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 logEntryArray.push_back({});
1912 nlohmann::json& hostLogEntry = logEntryArray.back();
1913 fillHostLoggerEntryJson(std::to_string(delegatedQuery.skip + i),
1914 logEntries[i], hostLogEntry);
George Liu0fda0f12021-11-16 10:06:17 +08001915 }
1916
Ed Tanous002d39b2022-05-31 08:59:27 -07001917 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1918 if (delegatedQuery.skip + delegatedQuery.top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08001919 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001920 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1921 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1922 std::to_string(delegatedQuery.skip + delegatedQuery.top);
George Liu0fda0f12021-11-16 10:06:17 +08001923 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001924 }
George Liu0fda0f12021-11-16 10:06:17 +08001925 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001926}
1927
1928inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1929{
1930 BMCWEB_ROUTE(
1931 app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1932 .privileges(redfish::privileges::getLogEntry)
1933 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001934 [&app](const crow::Request& req,
1935 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1936 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001937 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001938 {
1939 return;
1940 }
1941 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08001942
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08001944
Ed Tanous002d39b2022-05-31 08:59:27 -07001945 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1946 const char* end = targetID.data() + targetID.size();
Ed Tanousca45aa32022-01-07 09:28:45 -08001947
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1949 if (ec == std::errc::invalid_argument)
1950 {
1951 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1952 return;
1953 }
1954 if (ec == std::errc::result_out_of_range)
1955 {
1956 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1957 return;
1958 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001959
Ed Tanous002d39b2022-05-31 08:59:27 -07001960 std::vector<std::filesystem::path> hostLoggerFiles;
1961 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1962 {
1963 BMCWEB_LOG_ERROR << "fail to get host log file path";
1964 return;
1965 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001966
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 size_t logCount = 0;
1968 uint64_t top = 1;
1969 std::vector<std::string> logEntries;
1970 // We can get specific entry by skip and top. For example, if we
1971 // want to get nth entry, we can set skip = n-1 and top = 1 to
1972 // get that entry
1973 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
1974 logCount))
1975 {
1976 messages::internalError(asyncResp->res);
1977 return;
1978 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001979
Ed Tanous002d39b2022-05-31 08:59:27 -07001980 if (!logEntries.empty())
1981 {
1982 fillHostLoggerEntryJson(targetID, logEntries[0],
1983 asyncResp->res.jsonValue);
1984 return;
1985 }
Spencer Kub7028eb2021-10-26 15:27:35 +08001986
Ed Tanous002d39b2022-05-31 08:59:27 -07001987 // Requested ID was not found
1988 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1989 });
Spencer Kub7028eb2021-10-26 15:27:35 +08001990}
1991
Claire Weinanfdd26902022-03-01 14:18:25 -08001992constexpr char const* dumpManagerIface =
1993 "xyz.openbmc_project.Collection.DeleteAll";
1994inline void handleLogServicesCollectionGet(
1995 crow::App& app, const crow::Request& req,
1996 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1997{
1998 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1999 {
2000 return;
2001 }
2002 // Collections don't include the static data added by SubRoute
2003 // because it has a duplicate entry for members
2004 asyncResp->res.jsonValue["@odata.type"] =
2005 "#LogServiceCollection.LogServiceCollection";
2006 asyncResp->res.jsonValue["@odata.id"] =
2007 "/redfish/v1/Managers/bmc/LogServices";
2008 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2009 asyncResp->res.jsonValue["Description"] =
2010 "Collection of LogServices for this Manager";
2011 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2012 logServiceArray = nlohmann::json::array();
2013
2014#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2015 logServiceArray.push_back(
2016 {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2017#endif
2018
2019 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2020
2021#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2022 auto respHandler =
2023 [asyncResp](
2024 const boost::system::error_code ec,
2025 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2026 if (ec)
2027 {
2028 BMCWEB_LOG_ERROR
2029 << "handleLogServicesCollectionGet respHandler got error "
2030 << ec;
2031 // Assume that getting an error simply means there are no dump
2032 // LogServices. Return without adding any error response.
2033 return;
2034 }
2035
2036 nlohmann::json& logServiceArrayLocal =
2037 asyncResp->res.jsonValue["Members"];
2038
2039 for (const std::string& path : subTreePaths)
2040 {
2041 if (path == "/xyz/openbmc_project/dump/bmc")
2042 {
2043 logServiceArrayLocal.push_back(
2044 {{"@odata.id",
2045 "/redfish/v1/Managers/bmc/LogServices/Dump"}});
2046 }
2047 else if (path == "/xyz/openbmc_project/dump/faultlog")
2048 {
2049 logServiceArrayLocal.push_back(
2050 {{"@odata.id",
2051 "/redfish/v1/Managers/bmc/LogServices/FaultLog"}});
2052 }
2053 }
2054
2055 asyncResp->res.jsonValue["Members@odata.count"] =
2056 logServiceArrayLocal.size();
2057 };
2058
2059 crow::connections::systemBus->async_method_call(
2060 respHandler, "xyz.openbmc_project.ObjectMapper",
2061 "/xyz/openbmc_project/object_mapper",
2062 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2063 "/xyz/openbmc_project/dump", 0,
2064 std::array<const char*, 1>{dumpManagerIface});
2065#endif
2066}
2067
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002068inline void requestRoutesBMCLogServiceCollection(App& app)
2069{
2070 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002071 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002072 .methods(boost::beast::http::verb::get)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002073 std::bind_front(handleLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002074}
Ed Tanous1da66f72018-07-27 16:13:37 -07002075
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002076inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002077{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002078 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002079 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002080 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002081 [&app](const crow::Request& req,
2082 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002083 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 {
2085 return;
2086 }
2087 asyncResp->res.jsonValue["@odata.type"] =
2088 "#LogService.v1_1_0.LogService";
2089 asyncResp->res.jsonValue["@odata.id"] =
2090 "/redfish/v1/Managers/bmc/LogServices/Journal";
2091 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2092 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2093 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2094 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302095
Ed Tanous002d39b2022-05-31 08:59:27 -07002096 std::pair<std::string, std::string> redfishDateTimeOffset =
2097 crow::utility::getDateTimeOffsetNow();
2098 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2099 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2100 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302101
Ed Tanous002d39b2022-05-31 08:59:27 -07002102 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2103 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2104 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002105}
Jason M. Billse1f26342018-07-18 12:12:00 -07002106
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002107static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2108 sd_journal* journal,
2109 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002110{
2111 // Get the Log Entry contents
2112 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002113
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002114 std::string message;
2115 std::string_view syslogID;
2116 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2117 if (ret < 0)
2118 {
2119 BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2120 << strerror(-ret);
2121 }
2122 if (!syslogID.empty())
2123 {
2124 message += std::string(syslogID) + ": ";
2125 }
2126
Ed Tanous39e77502019-03-04 17:35:53 -08002127 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002128 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002129 if (ret < 0)
2130 {
2131 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2132 return 1;
2133 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002134 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002135
2136 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002137 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002138 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002139 if (ret < 0)
2140 {
2141 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07002142 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002143
2144 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002145 std::string entryTimeStr;
2146 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002147 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002148 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002149 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002150
2151 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002152 bmcJournalLogEntryJson = {
George Liu647b3cd2021-07-05 12:43:56 +08002153 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002154 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2155 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07002156 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08002157 {"Id", bmcJournalLogEntryID},
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002158 {"Message", std::move(message)},
Jason M. Billse1f26342018-07-18 12:12:00 -07002159 {"EntryType", "Oem"},
Patrick Williams738c1e62021-02-22 17:14:25 -06002160 {"Severity", severity <= 2 ? "Critical"
2161 : severity <= 4 ? "Warning"
2162 : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07002163 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07002164 {"Created", std::move(entryTimeStr)}};
2165 return 0;
2166}
2167
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002168inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002169{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002170 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002171 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002172 .methods(boost::beast::http::verb::get)(
2173 [&app](const crow::Request& req,
2174 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2175 query_param::QueryCapabilities capabilities = {
2176 .canDelegateTop = true,
2177 .canDelegateSkip = true,
2178 };
2179 query_param::Query delegatedQuery;
2180 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002181 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002182 {
2183 return;
2184 }
2185 // Collections don't include the static data added by SubRoute
2186 // because it has a duplicate entry for members
2187 asyncResp->res.jsonValue["@odata.type"] =
2188 "#LogEntryCollection.LogEntryCollection";
2189 asyncResp->res.jsonValue["@odata.id"] =
2190 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2191 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2192 asyncResp->res.jsonValue["Description"] =
2193 "Collection of BMC Journal Entries";
2194 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2195 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002196
Ed Tanous002d39b2022-05-31 08:59:27 -07002197 // Go through the journal and use the timestamp to create a
2198 // unique ID for each entry
2199 sd_journal* journalTmp = nullptr;
2200 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2201 if (ret < 0)
2202 {
2203 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2204 messages::internalError(asyncResp->res);
2205 return;
2206 }
2207 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2208 journalTmp, sd_journal_close);
2209 journalTmp = nullptr;
2210 uint64_t entryCount = 0;
2211 // Reset the unique ID on the first entry
2212 bool firstEntry = true;
2213 SD_JOURNAL_FOREACH(journal.get())
2214 {
2215 entryCount++;
2216 // Handle paging using skip (number of entries to skip from
2217 // the start) and top (number of entries to display)
2218 if (entryCount <= delegatedQuery.skip ||
2219 entryCount > delegatedQuery.skip + delegatedQuery.top)
George Liu0fda0f12021-11-16 10:06:17 +08002220 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002221 continue;
2222 }
2223
2224 std::string idStr;
2225 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2226 {
2227 continue;
2228 }
2229
2230 if (firstEntry)
2231 {
2232 firstEntry = false;
2233 }
2234
2235 logEntryArray.push_back({});
2236 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2237 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2238 bmcJournalLogEntry) != 0)
2239 {
George Liu0fda0f12021-11-16 10:06:17 +08002240 messages::internalError(asyncResp->res);
2241 return;
2242 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002243 }
2244 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2245 if (delegatedQuery.skip + delegatedQuery.top < entryCount)
2246 {
2247 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2248 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2249 std::to_string(delegatedQuery.skip + delegatedQuery.top);
2250 }
George Liu0fda0f12021-11-16 10:06:17 +08002251 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002252}
Jason M. Billse1f26342018-07-18 12:12:00 -07002253
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002254inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002255{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002256 BMCWEB_ROUTE(app,
2257 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002258 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002259 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002260 [&app](const crow::Request& req,
2261 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2262 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002263 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002264 {
2265 return;
2266 }
2267 // Convert the unique ID back to a timestamp to find the entry
2268 uint64_t ts = 0;
2269 uint64_t index = 0;
2270 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2271 {
2272 return;
2273 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002274
Ed Tanous002d39b2022-05-31 08:59:27 -07002275 sd_journal* journalTmp = nullptr;
2276 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2277 if (ret < 0)
2278 {
2279 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2280 messages::internalError(asyncResp->res);
2281 return;
2282 }
2283 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2284 journalTmp, sd_journal_close);
2285 journalTmp = nullptr;
2286 // Go to the timestamp in the log and move to the entry at the
2287 // index tracking the unique ID
2288 std::string idStr;
2289 bool firstEntry = true;
2290 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2291 if (ret < 0)
2292 {
2293 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2294 << strerror(-ret);
2295 messages::internalError(asyncResp->res);
2296 return;
2297 }
2298 for (uint64_t i = 0; i <= index; i++)
2299 {
2300 sd_journal_next(journal.get());
2301 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2302 {
2303 messages::internalError(asyncResp->res);
2304 return;
2305 }
2306 if (firstEntry)
2307 {
2308 firstEntry = false;
2309 }
2310 }
2311 // Confirm that the entry ID matches what was requested
2312 if (idStr != entryID)
2313 {
2314 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2315 return;
2316 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002317
Ed Tanous002d39b2022-05-31 08:59:27 -07002318 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2319 asyncResp->res.jsonValue) != 0)
2320 {
2321 messages::internalError(asyncResp->res);
2322 return;
2323 }
2324 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002325}
2326
Claire Weinanfdd26902022-03-01 14:18:25 -08002327inline void
2328 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2329 const std::string& dumpType)
2330{
2331 std::string dumpPath;
2332 std::string overWritePolicy;
2333 bool collectDiagnosticDataSupported = false;
2334
2335 if (dumpType == "BMC")
2336 {
2337 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2338 overWritePolicy = "WrapsWhenFull";
2339 collectDiagnosticDataSupported = true;
2340 }
2341 else if (dumpType == "FaultLog")
2342 {
2343 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2344 overWritePolicy = "Unknown";
2345 collectDiagnosticDataSupported = false;
2346 }
2347 else if (dumpType == "System")
2348 {
2349 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2350 overWritePolicy = "WrapsWhenFull";
2351 collectDiagnosticDataSupported = true;
2352 }
2353 else
2354 {
2355 BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2356 << dumpType;
2357 messages::internalError(asyncResp->res);
2358 return;
2359 }
2360
2361 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2362 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2363 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2364 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2365 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2366 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2367
2368 std::pair<std::string, std::string> redfishDateTimeOffset =
2369 crow::utility::getDateTimeOffsetNow();
2370 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2371 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2372 redfishDateTimeOffset.second;
2373
2374 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2375 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2376 dumpPath + "/Actions/LogService.ClearLog";
2377
2378 if (collectDiagnosticDataSupported)
2379 {
2380 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2381 ["target"] =
2382 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2383 }
2384}
2385
2386inline void handleLogServicesDumpServiceGet(
2387 crow::App& app, const std::string& dumpType, const crow::Request& req,
2388 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2389{
2390 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2391 {
2392 return;
2393 }
2394 getDumpServiceInfo(asyncResp, dumpType);
2395}
2396
2397inline void handleLogServicesDumpEntriesCollectionGet(
2398 crow::App& app, const std::string& dumpType, const crow::Request& req,
2399 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2400{
2401 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2402 {
2403 return;
2404 }
2405 getDumpEntryCollection(asyncResp, dumpType);
2406}
2407
2408inline void handleLogServicesDumpEntryGet(
2409 crow::App& app, const std::string& dumpType, const crow::Request& req,
2410 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2411 const std::string& dumpId)
2412{
2413 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2414 {
2415 return;
2416 }
2417 getDumpEntryById(asyncResp, dumpId, dumpType);
2418}
2419
2420inline void handleLogServicesDumpEntryDelete(
2421 crow::App& app, const std::string& dumpType, const crow::Request& req,
2422 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2423 const std::string& dumpId)
2424{
2425 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2426 {
2427 return;
2428 }
2429 deleteDumpEntry(asyncResp, dumpId, dumpType);
2430}
2431
2432inline void handleLogServicesDumpCollectDiagnosticDataPost(
2433 crow::App& app, const std::string& dumpType, const crow::Request& req,
2434 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2435{
2436 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2437 {
2438 return;
2439 }
2440 createDump(asyncResp, req, dumpType);
2441}
2442
2443inline void handleLogServicesDumpClearLogPost(
2444 crow::App& app, const std::string& dumpType, const crow::Request& req,
2445 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2446{
2447 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2448 {
2449 return;
2450 }
2451 clearDump(asyncResp, dumpType);
2452}
2453
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002454inline void requestRoutesBMCDumpService(App& app)
2455{
2456 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002457 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002458 .methods(boost::beast::http::verb::get)(std::bind_front(
2459 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002460}
2461
2462inline void requestRoutesBMCDumpEntryCollection(App& app)
2463{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002464 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002465 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08002466 .methods(boost::beast::http::verb::get)(std::bind_front(
2467 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002468}
2469
2470inline void requestRoutesBMCDumpEntry(App& app)
2471{
2472 BMCWEB_ROUTE(app,
2473 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002474 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002475 .methods(boost::beast::http::verb::get)(std::bind_front(
2476 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2477
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002478 BMCWEB_ROUTE(app,
2479 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002480 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08002481 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2482 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002483}
2484
2485inline void requestRoutesBMCDumpCreate(App& app)
2486{
George Liu0fda0f12021-11-16 10:06:17 +08002487 BMCWEB_ROUTE(
2488 app,
2489 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002490 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002491 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08002492 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2493 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002494}
2495
2496inline void requestRoutesBMCDumpClear(App& app)
2497{
George Liu0fda0f12021-11-16 10:06:17 +08002498 BMCWEB_ROUTE(
2499 app,
2500 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002501 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08002502 .methods(boost::beast::http::verb::post)(std::bind_front(
2503 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2504}
2505
2506inline void requestRoutesFaultLogDumpService(App& app)
2507{
2508 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2509 .privileges(redfish::privileges::getLogService)
2510 .methods(boost::beast::http::verb::get)(std::bind_front(
2511 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2512}
2513
2514inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2515{
2516 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2517 .privileges(redfish::privileges::getLogEntryCollection)
2518 .methods(boost::beast::http::verb::get)(
2519 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2520 std::ref(app), "FaultLog"));
2521}
2522
2523inline void requestRoutesFaultLogDumpEntry(App& app)
2524{
2525 BMCWEB_ROUTE(app,
2526 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2527 .privileges(redfish::privileges::getLogEntry)
2528 .methods(boost::beast::http::verb::get)(std::bind_front(
2529 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2530
2531 BMCWEB_ROUTE(app,
2532 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2533 .privileges(redfish::privileges::deleteLogEntry)
2534 .methods(boost::beast::http::verb::delete_)(std::bind_front(
2535 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2536}
2537
2538inline void requestRoutesFaultLogDumpClear(App& app)
2539{
2540 BMCWEB_ROUTE(
2541 app,
2542 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2543 .privileges(redfish::privileges::postLogService)
2544 .methods(boost::beast::http::verb::post)(std::bind_front(
2545 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002546}
2547
2548inline void requestRoutesSystemDumpService(App& app)
2549{
2550 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07002551 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07002552 .methods(boost::beast::http::verb::get)(
2553 [&app](const crow::Request& req,
2554 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002555 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002556 {
2557 return;
2558 }
2559 asyncResp->res.jsonValue["@odata.id"] =
2560 "/redfish/v1/Systems/system/LogServices/Dump";
2561 asyncResp->res.jsonValue["@odata.type"] =
2562 "#LogService.v1_2_0.LogService";
2563 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2564 asyncResp->res.jsonValue["Description"] = "System Dump LogService";
2565 asyncResp->res.jsonValue["Id"] = "Dump";
2566 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302567
Ed Tanous002d39b2022-05-31 08:59:27 -07002568 std::pair<std::string, std::string> redfishDateTimeOffset =
2569 crow::utility::getDateTimeOffsetNow();
2570 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2571 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2572 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302573
Ed Tanous002d39b2022-05-31 08:59:27 -07002574 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2575 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2576 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2577 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog";
Ed Tanous14766872022-03-15 10:44:42 -07002578
Ed Tanous002d39b2022-05-31 08:59:27 -07002579 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2580 ["target"] =
2581 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData";
Ed Tanous45ca1b82022-03-25 13:07:27 -07002582 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002583}
2584
2585inline void requestRoutesSystemDumpEntryCollection(App& app)
2586{
2587
2588 /**
2589 * Functions triggers appropriate requests on DBus
2590 */
Asmitha Karunanithib2a32892021-07-13 11:56:15 -05002591 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002592 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002593 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002594 [&app](const crow::Request& req,
2595 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002596 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002597 {
2598 return;
2599 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002600 getDumpEntryCollection(asyncResp, "System");
2601 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002602}
2603
2604inline void requestRoutesSystemDumpEntry(App& app)
2605{
2606 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002607 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002608 .privileges(redfish::privileges::getLogEntry)
2609
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002610 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002611 [&app](const crow::Request& req,
2612 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2613 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002614 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Claire Weinanc7a6d662022-06-13 16:36:39 -07002615 {
2616 return;
2617 }
2618 getDumpEntryById(asyncResp, param, "System");
Ed Tanous002d39b2022-05-31 08:59:27 -07002619 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002620
2621 BMCWEB_ROUTE(app,
John Edward Broadbent864d6a12021-06-09 10:12:48 -07002622 "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002623 .privileges(redfish::privileges::deleteLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002624 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002625 [&app](const crow::Request& req,
2626 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2627 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002628 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002629 {
2630 return;
2631 }
2632 deleteDumpEntry(asyncResp, param, "system");
2633 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002634}
2635
2636inline void requestRoutesSystemDumpCreate(App& app)
2637{
George Liu0fda0f12021-11-16 10:06:17 +08002638 BMCWEB_ROUTE(
2639 app,
2640 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07002641 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002642 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002643 [&app](const crow::Request& req,
2644 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002645 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002646 {
2647 return;
2648 }
2649 createDump(asyncResp, req, "System");
2650 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002651}
2652
2653inline void requestRoutesSystemDumpClear(App& app)
2654{
George Liu0fda0f12021-11-16 10:06:17 +08002655 BMCWEB_ROUTE(
2656 app,
2657 "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002658 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002659 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002660 [&app](const crow::Request& req,
2661 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002662
Ed Tanous45ca1b82022-03-25 13:07:27 -07002663 {
Carson Labrado3ba00072022-06-06 19:40:56 +00002664 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002665 {
2666 return;
2667 }
2668 clearDump(asyncResp, "System");
2669 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002670}
2671
2672inline void requestRoutesCrashdumpService(App& app)
2673{
2674 // Note: Deviated from redfish privilege registry for GET & HEAD
2675 // method for security reasons.
2676 /**
2677 * Functions triggers appropriate requests on DBus
2678 */
2679 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07002680 // This is incorrect, should be:
2681 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002682 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002683 .methods(boost::beast::http::verb::get)(
2684 [&app](const crow::Request& req,
2685 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002686 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002687 {
2688 return;
2689 }
2690 // Copy over the static data to include the entries added by
2691 // SubRoute
2692 asyncResp->res.jsonValue["@odata.id"] =
2693 "/redfish/v1/Systems/system/LogServices/Crashdump";
2694 asyncResp->res.jsonValue["@odata.type"] =
2695 "#LogService.v1_2_0.LogService";
2696 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2697 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2698 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2699 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2700 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302701
Ed Tanous002d39b2022-05-31 08:59:27 -07002702 std::pair<std::string, std::string> redfishDateTimeOffset =
2703 crow::utility::getDateTimeOffsetNow();
2704 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2705 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2706 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302707
Ed Tanous002d39b2022-05-31 08:59:27 -07002708 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2709 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2710 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2711 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2712 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2713 ["target"] =
2714 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002715 });
2716}
2717
2718void inline requestRoutesCrashdumpClear(App& app)
2719{
George Liu0fda0f12021-11-16 10:06:17 +08002720 BMCWEB_ROUTE(
2721 app,
2722 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07002723 // This is incorrect, should be:
2724 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07002725 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002726 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002727 [&app](const crow::Request& req,
2728 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002729 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002730 {
2731 return;
2732 }
2733 crow::connections::systemBus->async_method_call(
2734 [asyncResp](const boost::system::error_code ec,
2735 const std::string&) {
2736 if (ec)
2737 {
2738 messages::internalError(asyncResp->res);
2739 return;
2740 }
2741 messages::success(asyncResp->res);
2742 },
2743 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
2744 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002745}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002746
zhanghch058d1b46d2021-04-01 11:18:24 +08002747static void
2748 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2749 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002750{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002751 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08002752 [asyncResp, logID,
2753 &logEntryJson](const boost::system::error_code ec,
2754 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002755 if (ec)
2756 {
2757 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2758 if (ec.value() ==
2759 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002760 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002761 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002762 }
2763 else
2764 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002765 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002766 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002767 return;
2768 }
2769
2770 std::string timestamp{};
2771 std::string filename{};
2772 std::string logfile{};
2773 parseCrashdumpParameters(params, filename, timestamp, logfile);
2774
2775 if (filename.empty() || timestamp.empty())
2776 {
2777 messages::resourceMissingAtURI(asyncResp->res,
2778 crow::utility::urlFromPieces(logID));
2779 return;
2780 }
2781
2782 std::string crashdumpURI =
2783 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2784 logID + "/" + filename;
2785 nlohmann::json logEntry = {
2786 {"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2787 {"@odata.id",
2788 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2789 logID},
2790 {"Name", "CPU Crashdump"},
2791 {"Id", logID},
2792 {"EntryType", "Oem"},
2793 {"AdditionalDataURI", std::move(crashdumpURI)},
2794 {"DiagnosticDataType", "OEM"},
2795 {"OEMDiagnosticDataType", "PECICrashdump"},
2796 {"Created", std::move(timestamp)}};
2797
2798 // If logEntryJson references an array of LogEntry resources
2799 // ('Members' list), then push this as a new entry, otherwise set it
2800 // directly
2801 if (logEntryJson.is_array())
2802 {
2803 logEntryJson.push_back(logEntry);
2804 asyncResp->res.jsonValue["Members@odata.count"] =
2805 logEntryJson.size();
2806 }
2807 else
2808 {
2809 logEntryJson = logEntry;
2810 }
2811 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002812 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002813 std::move(getStoredLogCallback), crashdumpObject,
2814 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002815 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002816}
2817
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002818inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002819{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002820 // Note: Deviated from redfish privilege registry for GET & HEAD
2821 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002822 /**
2823 * Functions triggers appropriate requests on DBus
2824 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002825 BMCWEB_ROUTE(app,
2826 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002827 // This is incorrect, should be.
2828 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002829 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07002830 .methods(boost::beast::http::verb::get)(
2831 [&app](const crow::Request& req,
2832 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002833 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002834 {
2835 return;
2836 }
2837 crow::connections::systemBus->async_method_call(
2838 [asyncResp](const boost::system::error_code ec,
2839 const std::vector<std::string>& resp) {
2840 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002841 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002842 if (ec.value() !=
2843 boost::system::errc::no_such_file_or_directory)
2844 {
2845 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2846 << ec.message();
2847 messages::internalError(asyncResp->res);
2848 return;
2849 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002850 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002851 asyncResp->res.jsonValue["@odata.type"] =
2852 "#LogEntryCollection.LogEntryCollection";
2853 asyncResp->res.jsonValue["@odata.id"] =
2854 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2855 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2856 asyncResp->res.jsonValue["Description"] =
2857 "Collection of Crashdump Entries";
2858 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
2859 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08002860
Ed Tanous002d39b2022-05-31 08:59:27 -07002861 for (const std::string& path : resp)
2862 {
2863 const sdbusplus::message::object_path objPath(path);
2864 // Get the log ID
2865 std::string logID = objPath.filename();
2866 if (logID.empty())
2867 {
2868 continue;
2869 }
2870 // Add the log entry to the array
2871 logCrashdumpEntry(asyncResp, logID,
2872 asyncResp->res.jsonValue["Members"]);
2873 }
2874 },
2875 "xyz.openbmc_project.ObjectMapper",
2876 "/xyz/openbmc_project/object_mapper",
2877 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
2878 std::array<const char*, 1>{crashdumpInterface});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002879 });
2880}
Ed Tanous1da66f72018-07-27 16:13:37 -07002881
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002882inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002883{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002884 // Note: Deviated from redfish privilege registry for GET & HEAD
2885 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002886
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002887 BMCWEB_ROUTE(
2888 app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002889 // this is incorrect, should be
2890 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07002891 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002892 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002893 [&app](const crow::Request& req,
2894 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2895 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002896 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002897 {
2898 return;
2899 }
2900 const std::string& logID = param;
2901 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2902 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002903}
Ed Tanous1da66f72018-07-27 16:13:37 -07002904
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002905inline void requestRoutesCrashdumpFile(App& app)
2906{
2907 // Note: Deviated from redfish privilege registry for GET & HEAD
2908 // method for security reasons.
2909 BMCWEB_ROUTE(
2910 app,
2911 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002912 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002913 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002914 [&app](const crow::Request& req,
2915 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2916 const std::string& logID, const std::string& fileName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002917 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002918 {
2919 return;
2920 }
2921 auto getStoredLogCallback =
2922 [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
2923 const boost::system::error_code ec,
2924 const std::vector<
2925 std::pair<std::string, dbus::utility::DbusVariantType>>&
2926 resp) {
2927 if (ec)
2928 {
2929 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2930 messages::internalError(asyncResp->res);
2931 return;
2932 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002933
Ed Tanous002d39b2022-05-31 08:59:27 -07002934 std::string dbusFilename{};
2935 std::string dbusTimestamp{};
2936 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08002937
Ed Tanous002d39b2022-05-31 08:59:27 -07002938 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2939 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002940
Ed Tanous002d39b2022-05-31 08:59:27 -07002941 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2942 dbusFilepath.empty())
2943 {
2944 messages::resourceMissingAtURI(asyncResp->res, url);
2945 return;
2946 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002947
Ed Tanous002d39b2022-05-31 08:59:27 -07002948 // Verify the file name parameter is correct
2949 if (fileName != dbusFilename)
2950 {
2951 messages::resourceMissingAtURI(asyncResp->res, url);
2952 return;
2953 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002954
Ed Tanous002d39b2022-05-31 08:59:27 -07002955 if (!std::filesystem::exists(dbusFilepath))
2956 {
2957 messages::resourceMissingAtURI(asyncResp->res, url);
2958 return;
2959 }
2960 std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
2961 asyncResp->res.body() =
2962 std::string(std::istreambuf_iterator<char>{ifs}, {});
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002963
Ed Tanous002d39b2022-05-31 08:59:27 -07002964 // Configure this to be a file download when accessed
2965 // from a browser
2966 asyncResp->res.addHeader("Content-Disposition", "attachment");
2967 };
2968 crow::connections::systemBus->async_method_call(
2969 std::move(getStoredLogCallback), crashdumpObject,
2970 crashdumpPath + std::string("/") + logID,
2971 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2972 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002973}
2974
Jason M. Billsc5a4c822022-01-06 15:51:23 -08002975enum class OEMDiagnosticType
2976{
2977 onDemand,
2978 telemetry,
2979 invalid,
2980};
2981
Ed Tanousf7725d72022-03-07 12:46:00 -08002982inline OEMDiagnosticType
2983 getOEMDiagnosticType(const std::string_view& oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08002984{
2985 if (oemDiagStr == "OnDemand")
2986 {
2987 return OEMDiagnosticType::onDemand;
2988 }
2989 if (oemDiagStr == "Telemetry")
2990 {
2991 return OEMDiagnosticType::telemetry;
2992 }
2993
2994 return OEMDiagnosticType::invalid;
2995}
2996
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002997inline void requestRoutesCrashdumpCollect(App& app)
2998{
2999 // Note: Deviated from redfish privilege registry for GET & HEAD
3000 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003001 BMCWEB_ROUTE(
3002 app,
3003 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003004 // The below is incorrect; Should be ConfigureManager
3005 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003006 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003007 .methods(boost::beast::http::verb::post)(
3008 [&app](const crow::Request& req,
3009 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003010 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003011 {
3012 return;
3013 }
3014 std::string diagnosticDataType;
3015 std::string oemDiagnosticDataType;
3016 if (!redfish::json_util::readJsonAction(
3017 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3018 "OEMDiagnosticDataType", oemDiagnosticDataType))
3019 {
3020 return;
3021 }
3022
3023 if (diagnosticDataType != "OEM")
3024 {
3025 BMCWEB_LOG_ERROR
3026 << "Only OEM DiagnosticDataType supported for Crashdump";
3027 messages::actionParameterValueFormatError(
3028 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3029 "CollectDiagnosticData");
3030 return;
3031 }
3032
3033 OEMDiagnosticType oemDiagType =
3034 getOEMDiagnosticType(oemDiagnosticDataType);
3035
3036 std::string iface;
3037 std::string method;
3038 std::string taskMatchStr;
3039 if (oemDiagType == OEMDiagnosticType::onDemand)
3040 {
3041 iface = crashdumpOnDemandInterface;
3042 method = "GenerateOnDemandLog";
3043 taskMatchStr = "type='signal',"
3044 "interface='org.freedesktop.DBus.Properties',"
3045 "member='PropertiesChanged',"
3046 "arg0namespace='com.intel.crashdump'";
3047 }
3048 else if (oemDiagType == OEMDiagnosticType::telemetry)
3049 {
3050 iface = crashdumpTelemetryInterface;
3051 method = "GenerateTelemetryLog";
3052 taskMatchStr = "type='signal',"
3053 "interface='org.freedesktop.DBus.Properties',"
3054 "member='PropertiesChanged',"
3055 "arg0namespace='com.intel.crashdump'";
3056 }
3057 else
3058 {
3059 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3060 << oemDiagnosticDataType;
3061 messages::actionParameterValueFormatError(
3062 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3063 "CollectDiagnosticData");
3064 return;
3065 }
3066
3067 auto collectCrashdumpCallback =
3068 [asyncResp, payload(task::Payload(req)),
3069 taskMatchStr](const boost::system::error_code ec,
3070 const std::string&) mutable {
3071 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003072 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003073 if (ec.value() == boost::system::errc::operation_not_supported)
3074 {
3075 messages::resourceInStandby(asyncResp->res);
3076 }
3077 else if (ec.value() ==
3078 boost::system::errc::device_or_resource_busy)
3079 {
3080 messages::serviceTemporarilyUnavailable(asyncResp->res,
3081 "60");
3082 }
3083 else
3084 {
3085 messages::internalError(asyncResp->res);
3086 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003087 return;
3088 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003089 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
3090 [](boost::system::error_code err, sdbusplus::message::message&,
3091 const std::shared_ptr<task::TaskData>& taskData) {
3092 if (!err)
3093 {
3094 taskData->messages.emplace_back(messages::taskCompletedOK(
3095 std::to_string(taskData->index)));
3096 taskData->state = "Completed";
3097 }
3098 return task::completed;
3099 },
3100 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003101
Ed Tanous002d39b2022-05-31 08:59:27 -07003102 task->startTimer(std::chrono::minutes(5));
3103 task->populateResp(asyncResp->res);
3104 task->payload.emplace(std::move(payload));
3105 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003106
Ed Tanous002d39b2022-05-31 08:59:27 -07003107 crow::connections::systemBus->async_method_call(
3108 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3109 iface, method);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003110 });
3111}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003112
Andrew Geisslercb92c032018-08-17 07:56:14 -07003113/**
3114 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3115 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003116inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003117{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003118 /**
3119 * Function handles POST method request.
3120 * The Clear Log actions does not require any parameter.The action deletes
3121 * all entries found in the Entries collection for this Log Service.
3122 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003123
George Liu0fda0f12021-11-16 10:06:17 +08003124 BMCWEB_ROUTE(
3125 app,
3126 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003127 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003128 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003129 [&app](const crow::Request& req,
3130 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003131 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003132 {
3133 return;
3134 }
3135 BMCWEB_LOG_DEBUG << "Do delete all entries.";
Andrew Geisslercb92c032018-08-17 07:56:14 -07003136
Ed Tanous002d39b2022-05-31 08:59:27 -07003137 // Process response from Logging service.
3138 auto respHandler = [asyncResp](const boost::system::error_code ec) {
3139 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3140 if (ec)
3141 {
3142 // TODO Handle for specific error code
3143 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3144 asyncResp->res.result(
3145 boost::beast::http::status::internal_server_error);
3146 return;
3147 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003148
Ed Tanous002d39b2022-05-31 08:59:27 -07003149 asyncResp->res.result(boost::beast::http::status::no_content);
3150 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003151
Ed Tanous002d39b2022-05-31 08:59:27 -07003152 // Make call to Logging service to request Clear Log
3153 crow::connections::systemBus->async_method_call(
3154 respHandler, "xyz.openbmc_project.Logging",
3155 "/xyz/openbmc_project/logging",
3156 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3157 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003158}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003159
3160/****************************************************
3161 * Redfish PostCode interfaces
3162 * using DBUS interface: getPostCodesTS
3163 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003164inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003165{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003166 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003167 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003168 .methods(boost::beast::http::verb::get)(
3169 [&app](const crow::Request& req,
3170 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003171 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003172 {
3173 return;
3174 }
Ed Tanous14766872022-03-15 10:44:42 -07003175
Ed Tanous002d39b2022-05-31 08:59:27 -07003176 asyncResp->res.jsonValue["@odata.id"] =
3177 "/redfish/v1/Systems/system/LogServices/PostCodes";
3178 asyncResp->res.jsonValue["@odata.type"] =
3179 "#LogService.v1_1_0.LogService";
3180 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3181 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3182 asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
3183 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3184 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3185 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303186
Ed Tanous002d39b2022-05-31 08:59:27 -07003187 std::pair<std::string, std::string> redfishDateTimeOffset =
3188 crow::utility::getDateTimeOffsetNow();
3189 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3190 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3191 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303192
Ed Tanous002d39b2022-05-31 08:59:27 -07003193 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3194 {"target",
3195 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
George Liu0fda0f12021-11-16 10:06:17 +08003196 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003197}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003198
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003199inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003200{
George Liu0fda0f12021-11-16 10:06:17 +08003201 BMCWEB_ROUTE(
3202 app,
3203 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003204 // The following privilege is incorrect; It should be ConfigureManager
3205 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003206 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003207 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003208 [&app](const crow::Request& req,
3209 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003210 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003211 {
3212 return;
3213 }
3214 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
ZhikuiRena3316fc2020-01-29 14:58:08 -08003215
Ed Tanous002d39b2022-05-31 08:59:27 -07003216 // Make call to post-code service to request clear all
3217 crow::connections::systemBus->async_method_call(
3218 [asyncResp](const boost::system::error_code ec) {
3219 if (ec)
3220 {
3221 // TODO Handle for specific error code
3222 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3223 << ec;
3224 asyncResp->res.result(
3225 boost::beast::http::status::internal_server_error);
3226 messages::internalError(asyncResp->res);
3227 return;
3228 }
3229 },
3230 "xyz.openbmc_project.State.Boot.PostCode0",
3231 "/xyz/openbmc_project/State/Boot/PostCode0",
3232 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3233 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003234}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003235
3236static void fillPostCodeEntry(
zhanghch058d1b46d2021-04-01 11:18:24 +08003237 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303238 const boost::container::flat_map<
3239 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003240 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3241 const uint64_t skip = 0, const uint64_t top = 0)
3242{
3243 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003244 const registries::Message* message =
3245 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003246
3247 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003248 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003249
3250 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303251 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3252 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003253 {
3254 currentCodeIndex++;
3255 std::string postcodeEntryID =
3256 "B" + std::to_string(bootIndex) + "-" +
3257 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3258
3259 uint64_t usecSinceEpoch = code.first;
3260 uint64_t usTimeOffset = 0;
3261
3262 if (1 == currentCodeIndex)
3263 { // already incremented
3264 firstCodeTimeUs = code.first;
3265 }
3266 else
3267 {
3268 usTimeOffset = code.first - firstCodeTimeUs;
3269 }
3270
3271 // skip if no specific codeIndex is specified and currentCodeIndex does
3272 // not fall between top and skip
3273 if ((codeIndex == 0) &&
3274 (currentCodeIndex <= skip || currentCodeIndex > top))
3275 {
3276 continue;
3277 }
3278
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003279 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003280 // currentIndex
3281 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3282 {
3283 // This is done for simplicity. 1st entry is needed to calculate
3284 // time offset. To improve efficiency, one can get to the entry
3285 // directly (possibly with flatmap's nth method)
3286 continue;
3287 }
3288
3289 // currentCodeIndex is within top and skip or equal to specified code
3290 // index
3291
3292 // Get the Created time from the timestamp
3293 std::string entryTimeStr;
Nan Zhou1d8782e2021-11-29 22:23:18 -08003294 entryTimeStr =
3295 crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003296
3297 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3298 std::ostringstream hexCode;
3299 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303300 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003301 std::ostringstream timeOffsetStr;
3302 // Set Fixed -Point Notation
3303 timeOffsetStr << std::fixed;
3304 // Set precision to 4 digits
3305 timeOffsetStr << std::setprecision(4);
3306 // Add double to stream
3307 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3308 std::vector<std::string> messageArgs = {
3309 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3310
3311 // Get MessageArgs template from message registry
3312 std::string msg;
3313 if (message != nullptr)
3314 {
3315 msg = message->message;
3316
3317 // fill in this post code value
3318 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003319 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003320 {
3321 std::string argStr = "%" + std::to_string(++i);
3322 size_t argPos = msg.find(argStr);
3323 if (argPos != std::string::npos)
3324 {
3325 msg.replace(argPos, argStr.length(), messageArg);
3326 }
3327 }
3328 }
3329
Tim Leed4342a92020-04-27 11:47:58 +08003330 // Get Severity template from message registry
3331 std::string severity;
3332 if (message != nullptr)
3333 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08003334 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08003335 }
3336
ZhikuiRena3316fc2020-01-29 14:58:08 -08003337 // add to AsyncResp
3338 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003339 nlohmann::json& bmcLogEntry = logEntryArray.back();
George Liu0fda0f12021-11-16 10:06:17 +08003340 bmcLogEntry = {
3341 {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
3342 {"@odata.id",
3343 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3344 postcodeEntryID},
3345 {"Name", "POST Code Log Entry"},
3346 {"Id", postcodeEntryID},
3347 {"Message", std::move(msg)},
3348 {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3349 {"MessageArgs", std::move(messageArgs)},
3350 {"EntryType", "Event"},
3351 {"Severity", std::move(severity)},
3352 {"Created", entryTimeStr}};
George Liu647b3cd2021-07-05 12:43:56 +08003353 if (!std::get<std::vector<uint8_t>>(code.second).empty())
3354 {
3355 bmcLogEntry["AdditionalDataURI"] =
3356 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3357 postcodeEntryID + "/attachment";
3358 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003359 }
3360}
3361
zhanghch058d1b46d2021-04-01 11:18:24 +08003362static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003363 const uint16_t bootIndex,
3364 const uint64_t codeIndex)
3365{
3366 crow::connections::systemBus->async_method_call(
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303367 [aResp, bootIndex,
3368 codeIndex](const boost::system::error_code ec,
3369 const boost::container::flat_map<
3370 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3371 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003372 if (ec)
3373 {
3374 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3375 messages::internalError(aResp->res);
3376 return;
3377 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003378
Ed Tanous002d39b2022-05-31 08:59:27 -07003379 // skip the empty postcode boots
3380 if (postcode.empty())
3381 {
3382 return;
3383 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003384
Ed Tanous002d39b2022-05-31 08:59:27 -07003385 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003386
Ed Tanous002d39b2022-05-31 08:59:27 -07003387 aResp->res.jsonValue["Members@odata.count"] =
3388 aResp->res.jsonValue["Members"].size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08003389 },
Jonathan Doman15124762021-01-07 17:54:17 -08003390 "xyz.openbmc_project.State.Boot.PostCode0",
3391 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003392 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3393 bootIndex);
3394}
3395
zhanghch058d1b46d2021-04-01 11:18:24 +08003396static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003397 const uint16_t bootIndex,
3398 const uint16_t bootCount,
3399 const uint64_t entryCount, const uint64_t skip,
3400 const uint64_t top)
3401{
3402 crow::connections::systemBus->async_method_call(
3403 [aResp, bootIndex, bootCount, entryCount, skip,
3404 top](const boost::system::error_code ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303405 const boost::container::flat_map<
3406 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3407 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003408 if (ec)
3409 {
3410 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3411 messages::internalError(aResp->res);
3412 return;
3413 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003414
Ed Tanous002d39b2022-05-31 08:59:27 -07003415 uint64_t endCount = entryCount;
3416 if (!postcode.empty())
3417 {
3418 endCount = entryCount + postcode.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08003419
Ed Tanous002d39b2022-05-31 08:59:27 -07003420 if ((skip < endCount) && ((top + skip) > entryCount))
ZhikuiRena3316fc2020-01-29 14:58:08 -08003421 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003422 uint64_t thisBootSkip = std::max(skip, entryCount) - entryCount;
3423 uint64_t thisBootTop =
3424 std::min(top + skip, endCount) - entryCount;
3425
3426 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3427 thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003428 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003429 aResp->res.jsonValue["Members@odata.count"] = endCount;
3430 }
3431
3432 // continue to previous bootIndex
3433 if (bootIndex < bootCount)
3434 {
3435 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3436 bootCount, endCount, skip, top);
3437 }
3438 else
3439 {
3440 aResp->res.jsonValue["Members@odata.nextLink"] =
3441 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3442 std::to_string(skip + top);
3443 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003444 },
Jonathan Doman15124762021-01-07 17:54:17 -08003445 "xyz.openbmc_project.State.Boot.PostCode0",
3446 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08003447 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3448 bootIndex);
3449}
3450
zhanghch058d1b46d2021-04-01 11:18:24 +08003451static void
3452 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3453 const uint64_t skip, const uint64_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003454{
3455 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003456 sdbusplus::asio::getProperty<uint16_t>(
3457 *crow::connections::systemBus,
3458 "xyz.openbmc_project.State.Boot.PostCode0",
3459 "/xyz/openbmc_project/State/Boot/PostCode0",
3460 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3461 [aResp, entryCount, skip, top](const boost::system::error_code ec,
3462 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003463 if (ec)
3464 {
3465 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3466 messages::internalError(aResp->res);
3467 return;
3468 }
3469 getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
Jonathan Doman1e1e5982021-06-11 09:36:17 -07003470 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08003471}
3472
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003473inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003474{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003475 BMCWEB_ROUTE(app,
3476 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003477 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003478 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003479 [&app](const crow::Request& req,
3480 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003481 query_param::QueryCapabilities capabilities = {
3482 .canDelegateTop = true,
3483 .canDelegateSkip = true,
3484 };
3485 query_param::Query delegatedQuery;
3486 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00003487 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07003488 {
3489 return;
3490 }
3491 asyncResp->res.jsonValue["@odata.type"] =
3492 "#LogEntryCollection.LogEntryCollection";
3493 asyncResp->res.jsonValue["@odata.id"] =
3494 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3495 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3496 asyncResp->res.jsonValue["Description"] =
3497 "Collection of POST Code Log Entries";
3498 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3499 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003500
Ed Tanous002d39b2022-05-31 08:59:27 -07003501 getCurrentBootNumber(asyncResp, delegatedQuery.skip,
3502 delegatedQuery.top);
3503 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003504}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003505
George Liu647b3cd2021-07-05 12:43:56 +08003506/**
3507 * @brief Parse post code ID and get the current value and index value
3508 * eg: postCodeID=B1-2, currentValue=1, index=2
3509 *
3510 * @param[in] postCodeID Post Code ID
3511 * @param[out] currentValue Current value
3512 * @param[out] index Index value
3513 *
3514 * @return bool true if the parsing is successful, false the parsing fails
3515 */
3516inline static bool parsePostCode(const std::string& postCodeID,
3517 uint64_t& currentValue, uint16_t& index)
3518{
3519 std::vector<std::string> split;
3520 boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3521 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3522 {
3523 return false;
3524 }
3525
Ed Tanousca45aa32022-01-07 09:28:45 -08003526 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003527 const char* start = split[0].data() + 1;
Ed Tanousca45aa32022-01-07 09:28:45 -08003528 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003529 const char* end = split[0].data() + split[0].size();
3530 auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3531
3532 if (ptrIndex != end || ecIndex != std::errc())
3533 {
3534 return false;
3535 }
3536
3537 start = split[1].data();
Ed Tanousca45aa32022-01-07 09:28:45 -08003538
3539 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
George Liu647b3cd2021-07-05 12:43:56 +08003540 end = split[1].data() + split[1].size();
3541 auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
George Liu647b3cd2021-07-05 12:43:56 +08003542
Ed Tanousdcf2ebc2022-01-25 10:07:45 -08003543 return ptrValue == end && ecValue != std::errc();
George Liu647b3cd2021-07-05 12:43:56 +08003544}
3545
3546inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3547{
George Liu0fda0f12021-11-16 10:06:17 +08003548 BMCWEB_ROUTE(
3549 app,
3550 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08003551 .privileges(redfish::privileges::getLogEntry)
3552 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003553 [&app](const crow::Request& req,
3554 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3555 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003556 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003557 {
3558 return;
3559 }
3560 if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
3561 {
3562 asyncResp->res.result(boost::beast::http::status::bad_request);
3563 return;
3564 }
George Liu647b3cd2021-07-05 12:43:56 +08003565
Ed Tanous002d39b2022-05-31 08:59:27 -07003566 uint64_t currentValue = 0;
3567 uint16_t index = 0;
3568 if (!parsePostCode(postCodeID, currentValue, index))
3569 {
3570 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3571 return;
3572 }
George Liu647b3cd2021-07-05 12:43:56 +08003573
Ed Tanous002d39b2022-05-31 08:59:27 -07003574 crow::connections::systemBus->async_method_call(
3575 [asyncResp, postCodeID, currentValue](
3576 const boost::system::error_code ec,
3577 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3578 postcodes) {
3579 if (ec.value() == EBADR)
3580 {
3581 messages::resourceNotFound(asyncResp->res, "LogEntry",
3582 postCodeID);
3583 return;
3584 }
3585 if (ec)
3586 {
3587 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3588 messages::internalError(asyncResp->res);
3589 return;
3590 }
George Liu647b3cd2021-07-05 12:43:56 +08003591
Ed Tanous002d39b2022-05-31 08:59:27 -07003592 size_t value = static_cast<size_t>(currentValue) - 1;
3593 if (value == std::string::npos || postcodes.size() < currentValue)
3594 {
3595 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3596 messages::resourceNotFound(asyncResp->res, "LogEntry",
3597 postCodeID);
3598 return;
3599 }
George Liu647b3cd2021-07-05 12:43:56 +08003600
Ed Tanous002d39b2022-05-31 08:59:27 -07003601 const auto& [tID, c] = postcodes[value];
3602 if (c.empty())
3603 {
3604 BMCWEB_LOG_INFO << "No found post code data";
3605 messages::resourceNotFound(asyncResp->res, "LogEntry",
3606 postCodeID);
3607 return;
3608 }
3609 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3610 const char* d = reinterpret_cast<const char*>(c.data());
3611 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08003612
Ed Tanous002d39b2022-05-31 08:59:27 -07003613 asyncResp->res.addHeader("Content-Type",
3614 "application/octet-stream");
3615 asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
3616 asyncResp->res.body() = crow::utility::base64encode(strData);
3617 },
3618 "xyz.openbmc_project.State.Boot.PostCode0",
3619 "/xyz/openbmc_project/State/Boot/PostCode0",
3620 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3621 });
George Liu647b3cd2021-07-05 12:43:56 +08003622}
3623
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003624inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003625{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003626 BMCWEB_ROUTE(
3627 app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003628 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003629 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003630 [&app](const crow::Request& req,
3631 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3632 const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003633 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003634 {
3635 return;
3636 }
3637 uint16_t bootIndex = 0;
3638 uint64_t codeIndex = 0;
3639 if (!parsePostCode(targetID, codeIndex, bootIndex))
3640 {
3641 // Requested ID was not found
3642 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3643 return;
3644 }
3645 if (bootIndex == 0 || codeIndex == 0)
3646 {
3647 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3648 << targetID;
3649 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003650
Ed Tanous002d39b2022-05-31 08:59:27 -07003651 asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3652 asyncResp->res.jsonValue["@odata.id"] =
3653 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3654 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3655 asyncResp->res.jsonValue["Description"] =
3656 "Collection of POST Code Log Entries";
3657 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3658 asyncResp->res.jsonValue["Members@odata.count"] = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003659
Ed Tanous002d39b2022-05-31 08:59:27 -07003660 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3661 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003662}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003663
Ed Tanous1da66f72018-07-27 16:13:37 -07003664} // namespace redfish