blob: 1cda61c18228568b8723bb0bd1f934d7b294acef [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
18#include "node.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070019#include "registries.hpp"
20#include "registries/base_message_registry.hpp"
21#include "registries/openbmc_message_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080022#include "task.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070023
Jason M. Billse1f26342018-07-18 12:12:00 -070024#include <systemd/sd-journal.h>
25
Jason M. Bills4851d452019-03-28 11:27:48 -070026#include <boost/algorithm/string/split.hpp>
27#include <boost/beast/core/span.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070028#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080029#include <boost/system/linux_error.hpp>
Andrew Geisslercb92c032018-08-17 07:56:14 -070030#include <error_messages.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031
James Feist4418c7f2019-04-15 11:09:15 -070032#include <filesystem>
Jason M. Billscd225da2019-05-08 15:31:57 -070033#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080034#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070035
36namespace redfish
37{
38
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039constexpr char const* crashdumpObject = "com.intel.crashdump";
40constexpr char const* crashdumpPath = "/com/intel/crashdump";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050041constexpr char const* crashdumpInterface = "com.intel.crashdump";
42constexpr char const* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070043 "xyz.openbmc_project.Collection.DeleteAll";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050044constexpr char const* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070045 "com.intel.crashdump.OnDemand";
Gunnar Mills1214b7e2020-06-04 10:11:30 -050046constexpr char const* crashdumpRawPECIInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070047 "com.intel.crashdump.SendRawPeci";
Kenny L. Ku6eda7682020-06-19 09:48:36 -070048constexpr char const* crashdumpTelemetryInterface =
49 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070050
Jason M. Bills4851d452019-03-28 11:27:48 -070051namespace message_registries
52{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050053static const Message* getMessageFromRegistry(
54 const std::string& messageKey,
Jason M. Bills4851d452019-03-28 11:27:48 -070055 const boost::beast::span<const MessageEntry> registry)
56{
57 boost::beast::span<const MessageEntry>::const_iterator messageIt =
58 std::find_if(registry.cbegin(), registry.cend(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -050059 [&messageKey](const MessageEntry& messageEntry) {
Jason M. Bills4851d452019-03-28 11:27:48 -070060 return !std::strcmp(messageEntry.first,
61 messageKey.c_str());
62 });
63 if (messageIt != registry.cend())
64 {
65 return &messageIt->second;
66 }
67
68 return nullptr;
69}
70
Gunnar Mills1214b7e2020-06-04 10:11:30 -050071static const Message* getMessage(const std::string_view& messageID)
Jason M. Bills4851d452019-03-28 11:27:48 -070072{
73 // Redfish MessageIds are in the form
74 // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
75 // the right Message
76 std::vector<std::string> fields;
77 fields.reserve(4);
78 boost::split(fields, messageID, boost::is_any_of("."));
Gunnar Mills1214b7e2020-06-04 10:11:30 -050079 std::string& registryName = fields[0];
80 std::string& messageKey = fields[3];
Jason M. Bills4851d452019-03-28 11:27:48 -070081
82 // Find the right registry and check it for the MessageKey
83 if (std::string(base::header.registryPrefix) == registryName)
84 {
85 return getMessageFromRegistry(
86 messageKey, boost::beast::span<const MessageEntry>(base::registry));
87 }
88 if (std::string(openbmc::header.registryPrefix) == registryName)
89 {
90 return getMessageFromRegistry(
91 messageKey,
92 boost::beast::span<const MessageEntry>(openbmc::registry));
93 }
94 return nullptr;
95}
96} // namespace message_registries
97
James Feistf6150402019-01-08 10:36:20 -080098namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070099
Andrew Geisslercb92c032018-08-17 07:56:14 -0700100using GetManagedPropertyType = boost::container::flat_map<
Patrick Williams19bd78d2020-05-13 17:38:24 -0500101 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
102 int32_t, uint32_t, int64_t, uint64_t, double>>;
Andrew Geisslercb92c032018-08-17 07:56:14 -0700103
104using GetManagedObjectsType = boost::container::flat_map<
105 sdbusplus::message::object_path,
106 boost::container::flat_map<std::string, GetManagedPropertyType>>;
107
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500108inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -0700109{
110 if (s == "xyz.openbmc_project.Logging.Entry.Level.Alert")
111 {
112 return "Critical";
113 }
114 else if (s == "xyz.openbmc_project.Logging.Entry.Level.Critical")
115 {
116 return "Critical";
117 }
118 else if (s == "xyz.openbmc_project.Logging.Entry.Level.Debug")
119 {
120 return "OK";
121 }
122 else if (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency")
123 {
124 return "Critical";
125 }
126 else if (s == "xyz.openbmc_project.Logging.Entry.Level.Error")
127 {
128 return "Critical";
129 }
130 else if (s == "xyz.openbmc_project.Logging.Entry.Level.Informational")
131 {
132 return "OK";
133 }
134 else if (s == "xyz.openbmc_project.Logging.Entry.Level.Notice")
135 {
136 return "OK";
137 }
138 else if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
139 {
140 return "Warning";
141 }
142 return "";
143}
144
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500145static int getJournalMetadata(sd_journal* journal,
146 const std::string_view& field,
147 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700148{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500149 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700150 size_t length = 0;
151 int ret = 0;
152 // Get the metadata from the requested field of the journal entry
Ed Tanous271584a2019-07-09 16:24:22 -0700153 ret = sd_journal_get_data(journal, field.data(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500154 reinterpret_cast<const void**>(&data), &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700155 if (ret < 0)
156 {
157 return ret;
158 }
Ed Tanous39e77502019-03-04 17:35:53 -0800159 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700160 // Only use the content after the "=" character.
161 contents.remove_prefix(std::min(contents.find("=") + 1, contents.size()));
162 return ret;
163}
164
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500165static int getJournalMetadata(sd_journal* journal,
166 const std::string_view& field, const int& base,
167 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700168{
169 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800170 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700171 // Get the metadata from the requested field of the journal entry
172 ret = getJournalMetadata(journal, field, metadata);
173 if (ret < 0)
174 {
175 return ret;
176 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000177 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700178 return ret;
179}
180
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500181static bool getEntryTimestamp(sd_journal* journal, std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800182{
183 int ret = 0;
184 uint64_t timestamp = 0;
185 ret = sd_journal_get_realtime_usec(journal, &timestamp);
186 if (ret < 0)
187 {
188 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
189 << strerror(-ret);
190 return false;
191 }
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500192 entryTimestamp = crow::utility::getDateTime(
193 static_cast<std::time_t>(timestamp / 1000 / 1000));
194 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800195}
196
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500197static bool getSkipParam(crow::Response& res, const crow::Request& req,
198 uint64_t& skip)
Jason M. Bills16428a12018-11-02 12:42:29 -0700199{
James Feist5a7e8772020-07-22 09:08:38 -0700200 boost::urls::url_view::params_type::iterator it =
201 req.urlParams.find("$skip");
202 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700203 {
James Feist5a7e8772020-07-22 09:08:38 -0700204 std::string skipParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700206 skip = std::strtoul(skipParam.c_str(), &ptr, 10);
207 if (skipParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700208 {
209
210 messages::queryParameterValueTypeError(res, std::string(skipParam),
211 "$skip");
212 return false;
213 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700214 }
215 return true;
216}
217
Ed Tanous271584a2019-07-09 16:24:22 -0700218static constexpr const uint64_t maxEntriesPerPage = 1000;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500219static bool getTopParam(crow::Response& res, const crow::Request& req,
220 uint64_t& top)
Jason M. Bills16428a12018-11-02 12:42:29 -0700221{
James Feist5a7e8772020-07-22 09:08:38 -0700222 boost::urls::url_view::params_type::iterator it =
223 req.urlParams.find("$top");
224 if (it != req.urlParams.end())
Jason M. Bills16428a12018-11-02 12:42:29 -0700225 {
James Feist5a7e8772020-07-22 09:08:38 -0700226 std::string topParam = it->value();
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500227 char* ptr = nullptr;
James Feist5a7e8772020-07-22 09:08:38 -0700228 top = std::strtoul(topParam.c_str(), &ptr, 10);
229 if (topParam.empty() || *ptr != '\0')
Jason M. Bills16428a12018-11-02 12:42:29 -0700230 {
231 messages::queryParameterValueTypeError(res, std::string(topParam),
232 "$top");
233 return false;
234 }
Ed Tanous271584a2019-07-09 16:24:22 -0700235 if (top < 1U || top > maxEntriesPerPage)
Jason M. Bills16428a12018-11-02 12:42:29 -0700236 {
237
238 messages::queryParameterOutOfRange(
239 res, std::to_string(top), "$top",
240 "1-" + std::to_string(maxEntriesPerPage));
241 return false;
242 }
243 }
244 return true;
245}
246
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500247static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700248 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700249{
250 int ret = 0;
251 static uint64_t prevTs = 0;
252 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700253 if (firstEntry)
254 {
255 prevTs = 0;
256 }
257
Jason M. Bills16428a12018-11-02 12:42:29 -0700258 // Get the entry timestamp
259 uint64_t curTs = 0;
260 ret = sd_journal_get_realtime_usec(journal, &curTs);
261 if (ret < 0)
262 {
263 BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
264 << strerror(-ret);
265 return false;
266 }
267 // If the timestamp isn't unique, increment the index
268 if (curTs == prevTs)
269 {
270 index++;
271 }
272 else
273 {
274 // Otherwise, reset it
275 index = 0;
276 }
277 // Save the timestamp
278 prevTs = curTs;
279
280 entryID = std::to_string(curTs);
281 if (index > 0)
282 {
283 entryID += "_" + std::to_string(index);
284 }
285 return true;
286}
287
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500288static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700289 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700290{
Ed Tanous271584a2019-07-09 16:24:22 -0700291 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700292 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700293 if (firstEntry)
294 {
295 prevTs = 0;
296 }
297
Jason M. Bills95820182019-04-22 16:25:34 -0700298 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700299 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700300 std::tm timeStruct = {};
301 std::istringstream entryStream(logEntry);
302 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
303 {
304 curTs = std::mktime(&timeStruct);
305 }
306 // If the timestamp isn't unique, increment the index
307 if (curTs == prevTs)
308 {
309 index++;
310 }
311 else
312 {
313 // Otherwise, reset it
314 index = 0;
315 }
316 // Save the timestamp
317 prevTs = curTs;
318
319 entryID = std::to_string(curTs);
320 if (index > 0)
321 {
322 entryID += "_" + std::to_string(index);
323 }
324 return true;
325}
326
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500327static bool getTimestampFromID(crow::Response& res, const std::string& entryID,
328 uint64_t& timestamp, uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700329{
330 if (entryID.empty())
331 {
332 return false;
333 }
334 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800335 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700336
337 auto underscorePos = tsStr.find("_");
338 if (underscorePos != tsStr.npos)
339 {
340 // Timestamp has an index
341 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800342 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700343 indexStr.remove_prefix(underscorePos + 1);
344 std::size_t pos;
345 try
346 {
Ed Tanous39e77502019-03-04 17:35:53 -0800347 index = std::stoul(std::string(indexStr), &pos);
Jason M. Bills16428a12018-11-02 12:42:29 -0700348 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500349 catch (std::invalid_argument&)
Jason M. Bills16428a12018-11-02 12:42:29 -0700350 {
351 messages::resourceMissingAtURI(res, entryID);
352 return false;
353 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500354 catch (std::out_of_range&)
Jason M. Bills16428a12018-11-02 12:42:29 -0700355 {
356 messages::resourceMissingAtURI(res, entryID);
357 return false;
358 }
359 if (pos != indexStr.size())
360 {
361 messages::resourceMissingAtURI(res, entryID);
362 return false;
363 }
364 }
365 // Timestamp has no index
366 std::size_t pos;
367 try
368 {
Ed Tanous39e77502019-03-04 17:35:53 -0800369 timestamp = std::stoull(std::string(tsStr), &pos);
Jason M. Bills16428a12018-11-02 12:42:29 -0700370 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500371 catch (std::invalid_argument&)
Jason M. Bills16428a12018-11-02 12:42:29 -0700372 {
373 messages::resourceMissingAtURI(res, entryID);
374 return false;
375 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500376 catch (std::out_of_range&)
Jason M. Bills16428a12018-11-02 12:42:29 -0700377 {
378 messages::resourceMissingAtURI(res, entryID);
379 return false;
380 }
381 if (pos != tsStr.size())
382 {
383 messages::resourceMissingAtURI(res, entryID);
384 return false;
385 }
386 return true;
387}
388
Jason M. Bills95820182019-04-22 16:25:34 -0700389static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500390 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700391{
392 static const std::filesystem::path redfishLogDir = "/var/log";
393 static const std::string redfishLogFilename = "redfish";
394
395 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500396 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700397 std::filesystem::directory_iterator(redfishLogDir))
398 {
399 // If we find a redfish log file, save the path
400 std::string filename = dirEnt.path().filename();
401 if (boost::starts_with(filename, redfishLogFilename))
402 {
403 redfishLogFiles.emplace_back(redfishLogDir / filename);
404 }
405 }
406 // As the log files rotate, they are appended with a ".#" that is higher for
407 // the older logs. Since we don't expect more than 10 log files, we
408 // can just sort the list to get them in order from newest to oldest
409 std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
410
411 return !redfishLogFiles.empty();
412}
413
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500414inline void getDumpEntryCollection(std::shared_ptr<AsyncResp>& asyncResp,
415 const std::string& dumpType)
416{
417 std::string dumpPath;
418 if (dumpType == "BMC")
419 {
420 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
421 }
422 else if (dumpType == "System")
423 {
424 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
425 }
426 else
427 {
428 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
429 messages::internalError(asyncResp->res);
430 return;
431 }
432
433 crow::connections::systemBus->async_method_call(
434 [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
435 GetManagedObjectsType& resp) {
436 if (ec)
437 {
438 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
439 messages::internalError(asyncResp->res);
440 return;
441 }
442
443 nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
444 entriesArray = nlohmann::json::array();
445
446 for (auto& object : resp)
447 {
448 bool foundDumpEntry = false;
449 for (auto& interfaceMap : object.second)
450 {
451 if (interfaceMap.first ==
452 ("xyz.openbmc_project.Dump.Entry." + dumpType))
453 {
454 foundDumpEntry = true;
455 break;
456 }
457 }
458
459 if (foundDumpEntry == false)
460 {
461 continue;
462 }
463 std::time_t timestamp;
464 uint64_t size = 0;
465 entriesArray.push_back({});
466 nlohmann::json& thisEntry = entriesArray.back();
467 const std::string& path =
468 static_cast<const std::string&>(object.first);
469 std::size_t lastPos = path.rfind("/");
470 if (lastPos == std::string::npos)
471 {
472 continue;
473 }
474 std::string entryID = path.substr(lastPos + 1);
475
476 for (auto& interfaceMap : object.second)
477 {
478 if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
479 {
480
481 for (auto& propertyMap : interfaceMap.second)
482 {
483 if (propertyMap.first == "Size")
484 {
485 auto sizePtr =
486 std::get_if<uint64_t>(&propertyMap.second);
487 if (sizePtr == nullptr)
488 {
489 messages::internalError(asyncResp->res);
490 break;
491 }
492 size = *sizePtr;
493 break;
494 }
495 }
496 }
497 else if (interfaceMap.first ==
498 "xyz.openbmc_project.Time.EpochTime")
499 {
500
501 for (auto& propertyMap : interfaceMap.second)
502 {
503 if (propertyMap.first == "Elapsed")
504 {
505 const uint64_t* usecsTimeStamp =
506 std::get_if<uint64_t>(&propertyMap.second);
507 if (usecsTimeStamp == nullptr)
508 {
509 messages::internalError(asyncResp->res);
510 break;
511 }
512 timestamp =
513 static_cast<std::time_t>(*usecsTimeStamp);
514 break;
515 }
516 }
517 }
518 }
519
520 thisEntry["@odata.type"] = "#LogEntry.v1_5_1.LogEntry";
521 thisEntry["@odata.id"] = dumpPath + entryID;
522 thisEntry["Id"] = entryID;
523 thisEntry["EntryType"] = "Event";
524 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
525 thisEntry["Name"] = dumpType + " Dump Entry";
526
527 thisEntry["Oem"]["OpenBmc"]["@odata.type"] =
528 "#OemLogEntry.v1_0_0.OpenBmc";
529 thisEntry["Oem"]["OpenBmc"]["AdditionalDataSizeBytes"] = size;
530
531 if (dumpType == "BMC")
532 {
533 thisEntry["Oem"]["OpenBmc"]["DiagnosticDataType"] =
534 "Manager";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500535 }
536 else if (dumpType == "System")
537 {
538 thisEntry["Oem"]["OpenBmc"]["DiagnosticDataType"] = "OEM";
539 thisEntry["Oem"]["OpenBmc"]["OEMDiagnosticDataType"] =
540 "System";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500541 }
542 }
543 asyncResp->res.jsonValue["Members@odata.count"] =
544 entriesArray.size();
545 },
546 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
547 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
548}
549
550inline void getDumpEntryById(std::shared_ptr<AsyncResp>& asyncResp,
551 const std::string& entryID,
552 const std::string& dumpType)
553{
554 std::string dumpPath;
555 if (dumpType == "BMC")
556 {
557 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
558 }
559 else if (dumpType == "System")
560 {
561 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
562 }
563 else
564 {
565 BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
566 messages::internalError(asyncResp->res);
567 return;
568 }
569
570 crow::connections::systemBus->async_method_call(
571 [asyncResp, entryID, dumpPath, dumpType](
572 const boost::system::error_code ec, GetManagedObjectsType& resp) {
573 if (ec)
574 {
575 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
576 messages::internalError(asyncResp->res);
577 return;
578 }
579
580 for (auto& objectPath : resp)
581 {
582 if (objectPath.first.str.find(
583 "/xyz/openbmc_project/dump/entry/" + entryID) ==
584 std::string::npos)
585 {
586 continue;
587 }
588
589 bool foundDumpEntry = false;
590 for (auto& interfaceMap : objectPath.second)
591 {
592 if (interfaceMap.first ==
593 ("xyz.openbmc_project.Dump.Entry." + dumpType))
594 {
595 foundDumpEntry = true;
596 break;
597 }
598 }
599 if (foundDumpEntry == false)
600 {
601 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
602 messages::internalError(asyncResp->res);
603 return;
604 }
605
606 std::time_t timestamp;
607 uint64_t size = 0;
608
609 for (auto& interfaceMap : objectPath.second)
610 {
611 if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
612 {
613 for (auto& propertyMap : interfaceMap.second)
614 {
615 if (propertyMap.first == "Size")
616 {
617 auto sizePtr =
618 std::get_if<uint64_t>(&propertyMap.second);
619 if (sizePtr == nullptr)
620 {
621 messages::internalError(asyncResp->res);
622 break;
623 }
624 size = *sizePtr;
625 break;
626 }
627 }
628 }
629 else if (interfaceMap.first ==
630 "xyz.openbmc_project.Time.EpochTime")
631 {
632 for (auto& propertyMap : interfaceMap.second)
633 {
634 if (propertyMap.first == "Elapsed")
635 {
636 const uint64_t* usecsTimeStamp =
637 std::get_if<uint64_t>(&propertyMap.second);
638 if (usecsTimeStamp == nullptr)
639 {
640 messages::internalError(asyncResp->res);
641 break;
642 }
643 timestamp =
644 static_cast<std::time_t>(*usecsTimeStamp);
645 break;
646 }
647 }
648 }
649 }
650
651 asyncResp->res.jsonValue["@odata.type"] =
652 "#LogEntry.v1_5_1.LogEntry";
653 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
654 asyncResp->res.jsonValue["Id"] = entryID;
655 asyncResp->res.jsonValue["EntryType"] = "Event";
656 asyncResp->res.jsonValue["Created"] =
657 crow::utility::getDateTime(timestamp);
658 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
659
660 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["@odata.type"] =
661 "#OemLogEntry.v1_0_0.OpenBmc";
662 asyncResp->res
663 .jsonValue["Oem"]["OpenBmc"]["AdditionalDataSizeBytes"] =
664 size;
665
666 if (dumpType == "BMC")
667 {
668 asyncResp->res
669 .jsonValue["Oem"]["OpenBmc"]["DiagnosticDataType"] =
670 "Manager";
671 asyncResp->res
672 .jsonValue["Oem"]["OpenBmc"]["AdditionalDataURI"] =
673 "/redfish/v1/Managers/bmc/LogServices/Dump/"
674 "attachment/" +
675 entryID;
676 }
677 else if (dumpType == "System")
678 {
679 asyncResp->res
680 .jsonValue["Oem"]["OpenBmc"]["DiagnosticDataType"] =
681 "OEM";
682 asyncResp->res
683 .jsonValue["Oem"]["OpenBmc"]["OEMDiagnosticDataType"] =
684 "System";
685 asyncResp->res
686 .jsonValue["Oem"]["OpenBmc"]["AdditionalDataURI"] =
687 "/redfish/v1/Systems/system/LogServices/Dump/"
688 "attachment/" +
689 entryID;
690 }
691 }
692 },
693 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
694 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
695}
696
697inline void deleteDumpEntry(crow::Response& res, const std::string& entryID)
698{
699 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
700
701 auto respHandler = [asyncResp](const boost::system::error_code ec) {
702 BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
703 if (ec)
704 {
705 BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
706 << ec;
707 messages::internalError(asyncResp->res);
708 return;
709 }
710 };
711 crow::connections::systemBus->async_method_call(
712 respHandler, "xyz.openbmc_project.Dump.Manager",
713 "/xyz/openbmc_project/dump/entry/" + entryID,
714 "xyz.openbmc_project.Object.Delete", "Delete");
715}
716
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500717inline void createDumpTaskCallback(const crow::Request& req,
718 std::shared_ptr<AsyncResp> asyncResp,
719 const uint32_t& dumpId,
720 const std::string& dumpPath,
721 const std::string& dumpType)
722{
723 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanouscb13a392020-07-25 19:02:03 +0000724 [dumpId, dumpPath, dumpType, asyncResp](
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500725 boost::system::error_code err, sdbusplus::message::message& m,
726 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanouscb13a392020-07-25 19:02:03 +0000727 if (err)
728 {
729 messages::internalError(asyncResp->res);
730 return false;
731 }
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500732 std::vector<std::pair<
733 std::string,
734 std::vector<std::pair<std::string, std::variant<std::string>>>>>
735 interfacesList;
736
737 sdbusplus::message::object_path objPath;
738
739 m.read(objPath, interfacesList);
740
741 for (auto& interface : interfacesList)
742 {
743 if (interface.first ==
744 ("xyz.openbmc_project.Dump.Entry." + dumpType))
745 {
746 nlohmann::json retMessage = messages::success();
747 taskData->messages.emplace_back(retMessage);
748
749 std::string headerLoc =
750 "Location: " + dumpPath + std::to_string(dumpId);
751 taskData->payload->httpHeaders.emplace_back(
752 std::move(headerLoc));
753
754 taskData->state = "Completed";
755 return task::completed;
756 }
757 }
758 return !task::completed;
759 },
760 "type='signal',interface='org.freedesktop.DBus."
761 "ObjectManager',"
762 "member='InterfacesAdded', "
763 "path='/xyz/openbmc_project/dump'");
764
765 task->startTimer(std::chrono::minutes(3));
766 task->populateResp(asyncResp->res);
767 task->payload.emplace(req);
768}
769
770inline void createDump(crow::Response& res, const crow::Request& req,
771 const std::string& dumpType)
772{
773 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
774
775 std::string dumpPath;
776 if (dumpType == "BMC")
777 {
778 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
779 }
780 else if (dumpType == "System")
781 {
782 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
783 }
784 else
785 {
786 BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
787 messages::internalError(asyncResp->res);
788 return;
789 }
790
791 std::optional<std::string> diagnosticDataType;
792 std::optional<std::string> oemDiagnosticDataType;
793
794 if (!redfish::json_util::readJson(
795 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
796 "OEMDiagnosticDataType", oemDiagnosticDataType))
797 {
798 return;
799 }
800
801 if (dumpType == "System")
802 {
803 if (!oemDiagnosticDataType || !diagnosticDataType)
804 {
805 BMCWEB_LOG_ERROR << "CreateDump action parameter "
806 "'DiagnosticDataType'/"
807 "'OEMDiagnosticDataType' value not found!";
808 messages::actionParameterMissing(
809 asyncResp->res, "CollectDiagnosticData",
810 "DiagnosticDataType & OEMDiagnosticDataType");
811 return;
812 }
813 else if ((*oemDiagnosticDataType != "System") ||
814 (*diagnosticDataType != "OEM"))
815 {
816 BMCWEB_LOG_ERROR << "Wrong parameter values passed";
817 messages::invalidObject(asyncResp->res,
818 "System Dump creation parameters");
819 return;
820 }
821 }
822 else if (dumpType == "BMC")
823 {
824 if (!diagnosticDataType)
825 {
826 BMCWEB_LOG_ERROR << "CreateDump action parameter "
827 "'DiagnosticDataType' not found!";
828 messages::actionParameterMissing(
829 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
830 return;
831 }
832 else if (*diagnosticDataType != "Manager")
833 {
834 BMCWEB_LOG_ERROR
835 << "Wrong parameter value passed for 'DiagnosticDataType'";
836 messages::invalidObject(asyncResp->res,
837 "BMC Dump creation parameters");
838 return;
839 }
840 }
841
842 crow::connections::systemBus->async_method_call(
843 [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
844 const uint32_t& dumpId) {
845 if (ec)
846 {
847 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
848 messages::internalError(asyncResp->res);
849 return;
850 }
851 BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
852
853 createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
854 },
855 "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
856 "xyz.openbmc_project.Dump.Create", "CreateDump");
857}
858
Asmitha Karunanithi80319af2020-05-07 05:30:21 -0500859inline void clearDump(crow::Response& res, const std::string& dumpInterface)
860{
861 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
862 crow::connections::systemBus->async_method_call(
863 [asyncResp](const boost::system::error_code ec,
864 const std::vector<std::string>& subTreePaths) {
865 if (ec)
866 {
867 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
868 messages::internalError(asyncResp->res);
869 return;
870 }
871
872 for (const std::string& path : subTreePaths)
873 {
874 std::size_t pos = path.rfind("/");
875 if (pos != std::string::npos)
876 {
877 std::string logID = path.substr(pos + 1);
878 deleteDumpEntry(asyncResp->res, logID);
879 }
880 }
881 },
882 "xyz.openbmc_project.ObjectMapper",
883 "/xyz/openbmc_project/object_mapper",
884 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
885 "/xyz/openbmc_project/dump", 0,
886 std::array<std::string, 1>{dumpInterface});
887}
888
Johnathan Mantey043a0532020-03-10 17:15:28 -0700889static void ParseCrashdumpParameters(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500890 const std::vector<std::pair<std::string, VariantType>>& params,
891 std::string& filename, std::string& timestamp, std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -0700892{
893 for (auto property : params)
894 {
895 if (property.first == "Timestamp")
896 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500897 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500898 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700899 if (value != nullptr)
900 {
901 timestamp = *value;
902 }
903 }
904 else if (property.first == "Filename")
905 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500906 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500907 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700908 if (value != nullptr)
909 {
910 filename = *value;
911 }
912 }
913 else if (property.first == "Log")
914 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500915 const std::string* value =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500916 std::get_if<std::string>(&property.second);
Johnathan Mantey043a0532020-03-10 17:15:28 -0700917 if (value != nullptr)
918 {
919 logfile = *value;
920 }
921 }
922 }
923}
924
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500925constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800926class SystemLogServiceCollection : public Node
Ed Tanous1da66f72018-07-27 16:13:37 -0700927{
928 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700929 SystemLogServiceCollection(App& app) :
Ed Tanous029573d2019-02-01 10:57:49 -0800930 Node(app, "/redfish/v1/Systems/system/LogServices/")
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800931 {
932 entityPrivileges = {
933 {boost::beast::http::verb::get, {{"Login"}}},
934 {boost::beast::http::verb::head, {{"Login"}}},
935 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
936 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
937 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
938 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
939 }
940
941 private:
942 /**
943 * Functions triggers appropriate requests on DBus
944 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000945 void doGet(crow::Response& res, const crow::Request&,
946 const std::vector<std::string>&) override
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800947 {
948 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800949 // Collections don't include the static data added by SubRoute because
950 // it has a duplicate entry for members
951 asyncResp->res.jsonValue["@odata.type"] =
952 "#LogServiceCollection.LogServiceCollection";
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800953 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous029573d2019-02-01 10:57:49 -0800954 "/redfish/v1/Systems/system/LogServices";
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800955 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
956 asyncResp->res.jsonValue["Description"] =
957 "Collection of LogServices for this Computer System";
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500958 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800959 logServiceArray = nlohmann::json::array();
Ed Tanous029573d2019-02-01 10:57:49 -0800960 logServiceArray.push_back(
961 {{"@odata.id", "/redfish/v1/Systems/system/LogServices/EventLog"}});
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500962#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
raviteja-bc9bb6862020-02-03 11:53:32 -0600963 logServiceArray.push_back(
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500964 {{"@odata.id", "/redfish/v1/Systems/system/LogServices/Dump"}});
raviteja-bc9bb6862020-02-03 11:53:32 -0600965#endif
966
Jason M. Billsd53dd412019-02-12 17:16:22 -0800967#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
968 logServiceArray.push_back(
Anthony Wilson08a4e4b2019-04-12 08:23:05 -0500969 {{"@odata.id",
970 "/redfish/v1/Systems/system/LogServices/Crashdump"}});
Jason M. Billsd53dd412019-02-12 17:16:22 -0800971#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -0800972 asyncResp->res.jsonValue["Members@odata.count"] =
973 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800974
975 crow::connections::systemBus->async_method_call(
976 [asyncResp](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500977 const std::vector<std::string>& subtreePath) {
ZhikuiRena3316fc2020-01-29 14:58:08 -0800978 if (ec)
979 {
980 BMCWEB_LOG_ERROR << ec;
981 return;
982 }
983
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500984 for (auto& pathStr : subtreePath)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800985 {
986 if (pathStr.find("PostCode") != std::string::npos)
987 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000988 nlohmann::json& logServiceArrayLocal =
ZhikuiRena3316fc2020-01-29 14:58:08 -0800989 asyncResp->res.jsonValue["Members"];
Ed Tanous23a21a12020-07-25 04:45:05 +0000990 logServiceArrayLocal.push_back(
ZhikuiRena3316fc2020-01-29 14:58:08 -0800991 {{"@odata.id", "/redfish/v1/Systems/system/"
992 "LogServices/PostCodes"}});
993 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous23a21a12020-07-25 04:45:05 +0000994 logServiceArrayLocal.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -0800995 return;
996 }
997 }
998 },
999 "xyz.openbmc_project.ObjectMapper",
1000 "/xyz/openbmc_project/object_mapper",
1001 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001002 std::array<const char*, 1>{postCodeIface});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001003 }
1004};
1005
1006class EventLogService : public Node
1007{
1008 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001009 EventLogService(App& app) :
Ed Tanous029573d2019-02-01 10:57:49 -08001010 Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001011 {
1012 entityPrivileges = {
1013 {boost::beast::http::verb::get, {{"Login"}}},
1014 {boost::beast::http::verb::head, {{"Login"}}},
1015 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1016 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1017 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1018 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1019 }
1020
1021 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00001022 void doGet(crow::Response& res, const crow::Request&,
1023 const std::vector<std::string>&) override
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001024 {
1025 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1026
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001027 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous029573d2019-02-01 10:57:49 -08001028 "/redfish/v1/Systems/system/LogServices/EventLog";
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001029 asyncResp->res.jsonValue["@odata.type"] =
1030 "#LogService.v1_1_0.LogService";
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001031 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1032 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
Gunnar Mills73ec8302020-04-14 16:02:42 -05001033 asyncResp->res.jsonValue["Id"] = "EventLog";
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001034 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1035 asyncResp->res.jsonValue["Entries"] = {
1036 {"@odata.id",
Ed Tanous029573d2019-02-01 10:57:49 -08001037 "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
Gunnar Millse7d6c8b2019-07-03 11:30:01 -05001038 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1039
1040 {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
1041 "Actions/LogService.ClearLog"}};
Jason M. Bills489640c2019-05-17 09:56:36 -07001042 }
1043};
1044
Tim Lee1f56a3a2019-10-09 10:17:57 +08001045class JournalEventLogClear : public Node
Jason M. Bills489640c2019-05-17 09:56:36 -07001046{
1047 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001048 JournalEventLogClear(App& app) :
Jason M. Bills489640c2019-05-17 09:56:36 -07001049 Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1050 "LogService.ClearLog/")
1051 {
1052 entityPrivileges = {
1053 {boost::beast::http::verb::get, {{"Login"}}},
1054 {boost::beast::http::verb::head, {{"Login"}}},
1055 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1056 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1057 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1058 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1059 }
1060
1061 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00001062 void doPost(crow::Response& res, const crow::Request&,
1063 const std::vector<std::string>&) override
Jason M. Bills489640c2019-05-17 09:56:36 -07001064 {
1065 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1066
1067 // Clear the EventLog by deleting the log files
1068 std::vector<std::filesystem::path> redfishLogFiles;
1069 if (getRedfishLogFiles(redfishLogFiles))
1070 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001071 for (const std::filesystem::path& file : redfishLogFiles)
Jason M. Bills489640c2019-05-17 09:56:36 -07001072 {
1073 std::error_code ec;
1074 std::filesystem::remove(file, ec);
1075 }
1076 }
1077
1078 // Reload rsyslog so it knows to start new log files
1079 crow::connections::systemBus->async_method_call(
1080 [asyncResp](const boost::system::error_code ec) {
1081 if (ec)
1082 {
1083 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1084 messages::internalError(asyncResp->res);
1085 return;
1086 }
1087
1088 messages::success(asyncResp->res);
1089 },
1090 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1091 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1092 "replace");
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001093 }
1094};
1095
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001096static int fillEventLogEntryJson(const std::string& logEntryID,
Jason M. Bills95820182019-04-22 16:25:34 -07001097 const std::string logEntry,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001098 nlohmann::json& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001099{
Jason M. Bills95820182019-04-22 16:25:34 -07001100 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001101 // First get the Timestamp
1102 size_t space = logEntry.find_first_of(" ");
1103 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001104 {
1105 return 1;
1106 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001107 std::string timestamp = logEntry.substr(0, space);
1108 // Then get the log contents
1109 size_t entryStart = logEntry.find_first_not_of(" ", space);
1110 if (entryStart == std::string::npos)
1111 {
1112 return 1;
1113 }
1114 std::string_view entry(logEntry);
1115 entry.remove_prefix(entryStart);
1116 // Use split to separate the entry into its fields
1117 std::vector<std::string> logEntryFields;
1118 boost::split(logEntryFields, entry, boost::is_any_of(","),
1119 boost::token_compress_on);
1120 // We need at least a MessageId to be valid
1121 if (logEntryFields.size() < 1)
1122 {
1123 return 1;
1124 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001125 std::string& messageID = logEntryFields[0];
Jason M. Bills95820182019-04-22 16:25:34 -07001126
Jason M. Bills4851d452019-03-28 11:27:48 -07001127 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001128 const message_registries::Message* message =
Jason M. Bills4851d452019-03-28 11:27:48 -07001129 message_registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001130
Jason M. Bills4851d452019-03-28 11:27:48 -07001131 std::string msg;
1132 std::string severity;
1133 if (message != nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001134 {
Jason M. Bills4851d452019-03-28 11:27:48 -07001135 msg = message->message;
1136 severity = message->severity;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001137 }
1138
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001139 // Get the MessageArgs from the log if there are any
1140 boost::beast::span<std::string> messageArgs;
1141 if (logEntryFields.size() > 1)
Jason M. Bills4851d452019-03-28 11:27:48 -07001142 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001143 std::string& messageArgsStart = logEntryFields[1];
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001144 // If the first string is empty, assume there are no MessageArgs
1145 std::size_t messageArgsSize = 0;
1146 if (!messageArgsStart.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001147 {
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001148 messageArgsSize = logEntryFields.size() - 1;
1149 }
1150
Ed Tanous23a21a12020-07-25 04:45:05 +00001151 messageArgs = {&messageArgsStart, messageArgsSize};
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001152
1153 // Fill the MessageArgs into the Message
1154 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001155 for (const std::string& messageArg : messageArgs)
Jason M. Bills15a86ff2019-06-18 13:49:54 -07001156 {
1157 std::string argStr = "%" + std::to_string(++i);
1158 size_t argPos = msg.find(argStr);
1159 if (argPos != std::string::npos)
1160 {
1161 msg.replace(argPos, argStr.length(), messageArg);
1162 }
Jason M. Bills4851d452019-03-28 11:27:48 -07001163 }
1164 }
1165
Jason M. Bills95820182019-04-22 16:25:34 -07001166 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1167 // format which matches the Redfish format except for the fractional seconds
1168 // between the '.' and the '+', so just remove them.
1169 std::size_t dot = timestamp.find_first_of(".");
1170 std::size_t plus = timestamp.find_first_of("+");
1171 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001172 {
Jason M. Bills95820182019-04-22 16:25:34 -07001173 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001174 }
1175
1176 // Fill in the log entry with the gathered data
Jason M. Bills95820182019-04-22 16:25:34 -07001177 logEntryJson = {
Andrew Geisslercb92c032018-08-17 07:56:14 -07001178 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Ed Tanous029573d2019-02-01 10:57:49 -08001179 {"@odata.id",
Jason M. Bills897967d2019-07-29 17:05:30 -07001180 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
Jason M. Bills95820182019-04-22 16:25:34 -07001181 logEntryID},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001182 {"Name", "System Event Log Entry"},
Jason M. Bills95820182019-04-22 16:25:34 -07001183 {"Id", logEntryID},
1184 {"Message", std::move(msg)},
1185 {"MessageId", std::move(messageID)},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001186 {"MessageArgs", std::move(messageArgs)},
1187 {"EntryType", "Event"},
Jason M. Bills95820182019-04-22 16:25:34 -07001188 {"Severity", std::move(severity)},
1189 {"Created", std::move(timestamp)}};
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001190 return 0;
1191}
1192
Anthony Wilson27062602019-04-22 02:10:09 -05001193class JournalEventLogEntryCollection : public Node
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001194{
1195 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001196 JournalEventLogEntryCollection(App& app) :
Ed Tanous029573d2019-02-01 10:57:49 -08001197 Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001198 {
1199 entityPrivileges = {
1200 {boost::beast::http::verb::get, {{"Login"}}},
1201 {boost::beast::http::verb::head, {{"Login"}}},
1202 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1203 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1204 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1205 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1206 }
1207
1208 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001209 void doGet(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00001210 const std::vector<std::string>&) override
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001211 {
1212 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous271584a2019-07-09 16:24:22 -07001213 uint64_t skip = 0;
1214 uint64_t top = maxEntriesPerPage; // Show max entries by default
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001215 if (!getSkipParam(asyncResp->res, req, skip))
1216 {
1217 return;
1218 }
1219 if (!getTopParam(asyncResp->res, req, top))
1220 {
1221 return;
1222 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001223 // Collections don't include the static data added by SubRoute because
1224 // it has a duplicate entry for members
1225 asyncResp->res.jsonValue["@odata.type"] =
1226 "#LogEntryCollection.LogEntryCollection";
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001227 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous029573d2019-02-01 10:57:49 -08001228 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001229 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1230 asyncResp->res.jsonValue["Description"] =
1231 "Collection of System Event Log Entries";
Andrew Geisslercb92c032018-08-17 07:56:14 -07001232
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001233 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001234 logEntryArray = nlohmann::json::array();
Jason M. Bills95820182019-04-22 16:25:34 -07001235 // Go through the log files and create a unique ID for each entry
1236 std::vector<std::filesystem::path> redfishLogFiles;
1237 getRedfishLogFiles(redfishLogFiles);
Ed Tanousb01bf292019-03-25 19:25:26 +00001238 uint64_t entryCount = 0;
Jason M. Billscd225da2019-05-08 15:31:57 -07001239 std::string logEntry;
Jason M. Bills95820182019-04-22 16:25:34 -07001240
1241 // Oldest logs are in the last file, so start there and loop backwards
Jason M. Billscd225da2019-05-08 15:31:57 -07001242 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1243 it++)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001244 {
Jason M. Billscd225da2019-05-08 15:31:57 -07001245 std::ifstream logStream(*it);
Jason M. Bills95820182019-04-22 16:25:34 -07001246 if (!logStream.is_open())
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001247 {
1248 continue;
1249 }
1250
Jason M. Billse85d6b12019-07-29 17:01:15 -07001251 // Reset the unique ID on the first entry
1252 bool firstEntry = true;
Jason M. Bills95820182019-04-22 16:25:34 -07001253 while (std::getline(logStream, logEntry))
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001254 {
Jason M. Bills95820182019-04-22 16:25:34 -07001255 entryCount++;
1256 // Handle paging using skip (number of entries to skip from the
1257 // start) and top (number of entries to display)
1258 if (entryCount <= skip || entryCount > skip + top)
1259 {
1260 continue;
1261 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001262
Jason M. Bills95820182019-04-22 16:25:34 -07001263 std::string idStr;
Jason M. Billse85d6b12019-07-29 17:01:15 -07001264 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills95820182019-04-22 16:25:34 -07001265 {
1266 continue;
1267 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001268
Jason M. Billse85d6b12019-07-29 17:01:15 -07001269 if (firstEntry)
1270 {
1271 firstEntry = false;
1272 }
1273
Jason M. Bills95820182019-04-22 16:25:34 -07001274 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001275 nlohmann::json& bmcLogEntry = logEntryArray.back();
Jason M. Bills95820182019-04-22 16:25:34 -07001276 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
1277 {
1278 messages::internalError(asyncResp->res);
1279 return;
1280 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001281 }
1282 }
1283 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1284 if (skip + top < entryCount)
1285 {
1286 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Jason M. Bills95820182019-04-22 16:25:34 -07001287 "/redfish/v1/Systems/system/LogServices/EventLog/"
1288 "Entries?$skip=" +
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001289 std::to_string(skip + top);
1290 }
Anthony Wilson08a4e4b2019-04-12 08:23:05 -05001291 }
1292};
1293
Jason M. Bills897967d2019-07-29 17:05:30 -07001294class JournalEventLogEntry : public Node
1295{
1296 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001297 JournalEventLogEntry(App& app) :
Jason M. Bills897967d2019-07-29 17:05:30 -07001298 Node(app,
1299 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
1300 std::string())
1301 {
1302 entityPrivileges = {
1303 {boost::beast::http::verb::get, {{"Login"}}},
1304 {boost::beast::http::verb::head, {{"Login"}}},
1305 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1306 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1307 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1308 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1309 }
1310
1311 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00001312 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001313 const std::vector<std::string>& params) override
Jason M. Bills897967d2019-07-29 17:05:30 -07001314 {
1315 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1316 if (params.size() != 1)
1317 {
1318 messages::internalError(asyncResp->res);
1319 return;
1320 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001321 const std::string& targetID = params[0];
Jason M. Bills897967d2019-07-29 17:05:30 -07001322
1323 // Go through the log files and check the unique ID for each entry to
1324 // find the target entry
1325 std::vector<std::filesystem::path> redfishLogFiles;
1326 getRedfishLogFiles(redfishLogFiles);
1327 std::string logEntry;
1328
1329 // Oldest logs are in the last file, so start there and loop backwards
1330 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1331 it++)
1332 {
1333 std::ifstream logStream(*it);
1334 if (!logStream.is_open())
1335 {
1336 continue;
1337 }
1338
1339 // Reset the unique ID on the first entry
1340 bool firstEntry = true;
1341 while (std::getline(logStream, logEntry))
1342 {
1343 std::string idStr;
1344 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1345 {
1346 continue;
1347 }
1348
1349 if (firstEntry)
1350 {
1351 firstEntry = false;
1352 }
1353
1354 if (idStr == targetID)
1355 {
1356 if (fillEventLogEntryJson(idStr, logEntry,
1357 asyncResp->res.jsonValue) != 0)
1358 {
1359 messages::internalError(asyncResp->res);
1360 return;
1361 }
1362 return;
1363 }
1364 }
1365 }
1366 // Requested ID was not found
1367 messages::resourceMissingAtURI(asyncResp->res, targetID);
1368 }
1369};
1370
Anthony Wilson08a4e4b2019-04-12 08:23:05 -05001371class DBusEventLogEntryCollection : public Node
1372{
1373 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001374 DBusEventLogEntryCollection(App& app) :
Anthony Wilson08a4e4b2019-04-12 08:23:05 -05001375 Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1376 {
1377 entityPrivileges = {
1378 {boost::beast::http::verb::get, {{"Login"}}},
1379 {boost::beast::http::verb::head, {{"Login"}}},
1380 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1381 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1382 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1383 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1384 }
1385
1386 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00001387 void doGet(crow::Response& res, const crow::Request&,
1388 const std::vector<std::string>&) override
Anthony Wilson08a4e4b2019-04-12 08:23:05 -05001389 {
1390 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1391
1392 // Collections don't include the static data added by SubRoute because
1393 // it has a duplicate entry for members
1394 asyncResp->res.jsonValue["@odata.type"] =
1395 "#LogEntryCollection.LogEntryCollection";
Anthony Wilson08a4e4b2019-04-12 08:23:05 -05001396 asyncResp->res.jsonValue["@odata.id"] =
1397 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1398 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1399 asyncResp->res.jsonValue["Description"] =
1400 "Collection of System Event Log Entries";
1401
Andrew Geisslercb92c032018-08-17 07:56:14 -07001402 // DBus implementation of EventLog/Entries
1403 // Make call to Logging Service to find all log entry objects
1404 crow::connections::systemBus->async_method_call(
1405 [asyncResp](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001406 GetManagedObjectsType& resp) {
Andrew Geisslercb92c032018-08-17 07:56:14 -07001407 if (ec)
1408 {
1409 // TODO Handle for specific error code
1410 BMCWEB_LOG_ERROR
1411 << "getLogEntriesIfaceData resp_handler got error "
1412 << ec;
1413 messages::internalError(asyncResp->res);
1414 return;
1415 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001416 nlohmann::json& entriesArray =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001417 asyncResp->res.jsonValue["Members"];
1418 entriesArray = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001419 for (auto& objectPath : resp)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001420 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001421 for (auto& interfaceMap : objectPath.second)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001422 {
1423 if (interfaceMap.first !=
1424 "xyz.openbmc_project.Logging.Entry")
1425 {
1426 BMCWEB_LOG_DEBUG << "Bailing early on "
1427 << interfaceMap.first;
1428 continue;
1429 }
1430 entriesArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001431 nlohmann::json& thisEntry = entriesArray.back();
1432 uint32_t* id = nullptr;
Ed Tanous66664f22019-10-11 13:05:49 -07001433 std::time_t timestamp{};
George Liud139c232020-08-18 18:48:57 +08001434 std::time_t updateTimestamp{};
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001435 std::string* severity = nullptr;
1436 std::string* message = nullptr;
George Liud139c232020-08-18 18:48:57 +08001437
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001438 for (auto& propertyMap : interfaceMap.second)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001439 {
1440 if (propertyMap.first == "Id")
1441 {
Patrick Williams8d78b7a2020-05-13 11:24:20 -05001442 id = std::get_if<uint32_t>(&propertyMap.second);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001443 if (id == nullptr)
1444 {
Asmitha Karunanithi0f1d8ed2020-08-02 11:11:47 -05001445 messages::internalError(asyncResp->res);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001446 }
1447 }
1448 else if (propertyMap.first == "Timestamp")
1449 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001450 const uint64_t* millisTimeStamp =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001451 std::get_if<uint64_t>(&propertyMap.second);
1452 if (millisTimeStamp == nullptr)
1453 {
Asmitha Karunanithi0f1d8ed2020-08-02 11:11:47 -05001454 messages::internalError(asyncResp->res);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001455 }
George Liuebd45902020-08-26 14:21:10 +08001456 else
1457 {
1458 timestamp = crow::utility::getTimestamp(
1459 *millisTimeStamp);
1460 }
George Liud139c232020-08-18 18:48:57 +08001461 }
1462 else if (propertyMap.first == "UpdateTimestamp")
1463 {
1464 const uint64_t* millisTimeStamp =
1465 std::get_if<uint64_t>(&propertyMap.second);
1466 if (millisTimeStamp == nullptr)
1467 {
1468 messages::internalError(asyncResp->res);
1469 }
George Liuebd45902020-08-26 14:21:10 +08001470 else
1471 {
1472 updateTimestamp =
1473 crow::utility::getTimestamp(
1474 *millisTimeStamp);
1475 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001476 }
1477 else if (propertyMap.first == "Severity")
1478 {
1479 severity = std::get_if<std::string>(
1480 &propertyMap.second);
1481 if (severity == nullptr)
1482 {
Asmitha Karunanithi0f1d8ed2020-08-02 11:11:47 -05001483 messages::internalError(asyncResp->res);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001484 }
1485 }
1486 else if (propertyMap.first == "Message")
1487 {
1488 message = std::get_if<std::string>(
1489 &propertyMap.second);
1490 if (message == nullptr)
1491 {
Asmitha Karunanithi0f1d8ed2020-08-02 11:11:47 -05001492 messages::internalError(asyncResp->res);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001493 }
1494 }
1495 }
1496 thisEntry = {
George Liud139c232020-08-18 18:48:57 +08001497 {"@odata.type", "#LogEntry.v1_6_0.LogEntry"},
Andrew Geisslercb92c032018-08-17 07:56:14 -07001498 {"@odata.id",
1499 "/redfish/v1/Systems/system/LogServices/EventLog/"
1500 "Entries/" +
1501 std::to_string(*id)},
Anthony Wilson27062602019-04-22 02:10:09 -05001502 {"Name", "System Event Log Entry"},
Andrew Geisslercb92c032018-08-17 07:56:14 -07001503 {"Id", std::to_string(*id)},
1504 {"Message", *message},
1505 {"EntryType", "Event"},
1506 {"Severity",
1507 translateSeverityDbusToRedfish(*severity)},
George Liud139c232020-08-18 18:48:57 +08001508 {"Created", crow::utility::getDateTime(timestamp)},
1509 {"Modified",
1510 crow::utility::getDateTime(updateTimestamp)}};
Andrew Geisslercb92c032018-08-17 07:56:14 -07001511 }
1512 }
1513 std::sort(entriesArray.begin(), entriesArray.end(),
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001514 [](const nlohmann::json& left,
1515 const nlohmann::json& right) {
Andrew Geisslercb92c032018-08-17 07:56:14 -07001516 return (left["Id"] <= right["Id"]);
1517 });
1518 asyncResp->res.jsonValue["Members@odata.count"] =
1519 entriesArray.size();
1520 },
1521 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1522 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001523 }
1524};
1525
Anthony Wilson08a4e4b2019-04-12 08:23:05 -05001526class DBusEventLogEntry : public Node
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001527{
1528 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001529 DBusEventLogEntry(App& app) :
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001530 Node(app,
Ed Tanous029573d2019-02-01 10:57:49 -08001531 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/",
1532 std::string())
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001533 {
1534 entityPrivileges = {
1535 {boost::beast::http::verb::get, {{"Login"}}},
1536 {boost::beast::http::verb::head, {{"Login"}}},
1537 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1538 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1539 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1540 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1541 }
1542
1543 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00001544 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001545 const std::vector<std::string>& params) override
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001546 {
1547 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous029573d2019-02-01 10:57:49 -08001548 if (params.size() != 1)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001549 {
1550 messages::internalError(asyncResp->res);
1551 return;
1552 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001553 const std::string& entryID = params[0];
Andrew Geisslercb92c032018-08-17 07:56:14 -07001554
Andrew Geisslercb92c032018-08-17 07:56:14 -07001555 // DBus implementation of EventLog/Entries
1556 // Make call to Logging Service to find all log entry objects
1557 crow::connections::systemBus->async_method_call(
1558 [asyncResp, entryID](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001559 GetManagedPropertyType& resp) {
Andrew Geisslercb92c032018-08-17 07:56:14 -07001560 if (ec)
1561 {
1562 BMCWEB_LOG_ERROR
1563 << "EventLogEntry (DBus) resp_handler got error " << ec;
1564 messages::internalError(asyncResp->res);
1565 return;
1566 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001567 uint32_t* id = nullptr;
Ed Tanous66664f22019-10-11 13:05:49 -07001568 std::time_t timestamp{};
George Liud139c232020-08-18 18:48:57 +08001569 std::time_t updateTimestamp{};
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001570 std::string* severity = nullptr;
1571 std::string* message = nullptr;
George Liud139c232020-08-18 18:48:57 +08001572
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001573 for (auto& propertyMap : resp)
Andrew Geisslercb92c032018-08-17 07:56:14 -07001574 {
1575 if (propertyMap.first == "Id")
1576 {
1577 id = std::get_if<uint32_t>(&propertyMap.second);
1578 if (id == nullptr)
1579 {
Asmitha Karunanithi0f1d8ed2020-08-02 11:11:47 -05001580 messages::internalError(asyncResp->res);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001581 }
1582 }
1583 else if (propertyMap.first == "Timestamp")
1584 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001585 const uint64_t* millisTimeStamp =
Andrew Geisslercb92c032018-08-17 07:56:14 -07001586 std::get_if<uint64_t>(&propertyMap.second);
1587 if (millisTimeStamp == nullptr)
1588 {
Asmitha Karunanithi0f1d8ed2020-08-02 11:11:47 -05001589 messages::internalError(asyncResp->res);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001590 }
George Liuebd45902020-08-26 14:21:10 +08001591 else
1592 {
1593 timestamp =
1594 crow::utility::getTimestamp(*millisTimeStamp);
1595 }
George Liud139c232020-08-18 18:48:57 +08001596 }
1597 else if (propertyMap.first == "UpdateTimestamp")
1598 {
1599 const uint64_t* millisTimeStamp =
1600 std::get_if<uint64_t>(&propertyMap.second);
1601 if (millisTimeStamp == nullptr)
1602 {
1603 messages::internalError(asyncResp->res);
1604 }
George Liuebd45902020-08-26 14:21:10 +08001605 else
1606 {
1607 updateTimestamp =
1608 crow::utility::getTimestamp(*millisTimeStamp);
1609 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001610 }
1611 else if (propertyMap.first == "Severity")
1612 {
1613 severity =
1614 std::get_if<std::string>(&propertyMap.second);
1615 if (severity == nullptr)
1616 {
Asmitha Karunanithi0f1d8ed2020-08-02 11:11:47 -05001617 messages::internalError(asyncResp->res);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001618 }
1619 }
1620 else if (propertyMap.first == "Message")
1621 {
1622 message = std::get_if<std::string>(&propertyMap.second);
1623 if (message == nullptr)
1624 {
Asmitha Karunanithi0f1d8ed2020-08-02 11:11:47 -05001625 messages::internalError(asyncResp->res);
Andrew Geisslercb92c032018-08-17 07:56:14 -07001626 }
1627 }
1628 }
Ed Tanous271584a2019-07-09 16:24:22 -07001629 if (id == nullptr || message == nullptr || severity == nullptr)
1630 {
1631 return;
1632 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07001633 asyncResp->res.jsonValue = {
George Liud139c232020-08-18 18:48:57 +08001634 {"@odata.type", "#LogEntry.v1_6_0.LogEntry"},
Andrew Geisslercb92c032018-08-17 07:56:14 -07001635 {"@odata.id",
1636 "/redfish/v1/Systems/system/LogServices/EventLog/"
1637 "Entries/" +
1638 std::to_string(*id)},
Anthony Wilson27062602019-04-22 02:10:09 -05001639 {"Name", "System Event Log Entry"},
Andrew Geisslercb92c032018-08-17 07:56:14 -07001640 {"Id", std::to_string(*id)},
1641 {"Message", *message},
1642 {"EntryType", "Event"},
1643 {"Severity", translateSeverityDbusToRedfish(*severity)},
George Liud139c232020-08-18 18:48:57 +08001644 {"Created", crow::utility::getDateTime(timestamp)},
1645 {"Modified", crow::utility::getDateTime(updateTimestamp)}};
Andrew Geisslercb92c032018-08-17 07:56:14 -07001646 },
1647 "xyz.openbmc_project.Logging",
1648 "/xyz/openbmc_project/logging/entry/" + entryID,
1649 "org.freedesktop.DBus.Properties", "GetAll",
1650 "xyz.openbmc_project.Logging.Entry");
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001651 }
Chicago Duan336e96c2019-07-15 14:22:08 +08001652
Ed Tanouscb13a392020-07-25 19:02:03 +00001653 void doDelete(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001654 const std::vector<std::string>& params) override
Chicago Duan336e96c2019-07-15 14:22:08 +08001655 {
1656
1657 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1658
1659 auto asyncResp = std::make_shared<AsyncResp>(res);
1660
1661 if (params.size() != 1)
1662 {
1663 messages::internalError(asyncResp->res);
1664 return;
1665 }
1666 std::string entryID = params[0];
1667
1668 dbus::utility::escapePathForDbus(entryID);
1669
1670 // Process response from Logging service.
1671 auto respHandler = [asyncResp](const boost::system::error_code ec) {
1672 BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1673 if (ec)
1674 {
1675 // TODO Handle for specific error code
1676 BMCWEB_LOG_ERROR
1677 << "EventLogEntry (DBus) doDelete respHandler got error "
1678 << ec;
1679 asyncResp->res.result(
1680 boost::beast::http::status::internal_server_error);
1681 return;
1682 }
1683
1684 asyncResp->res.result(boost::beast::http::status::ok);
1685 };
1686
1687 // Make call to Logging service to request Delete Log
1688 crow::connections::systemBus->async_method_call(
1689 respHandler, "xyz.openbmc_project.Logging",
1690 "/xyz/openbmc_project/logging/entry/" + entryID,
1691 "xyz.openbmc_project.Object.Delete", "Delete");
1692 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001693};
1694
1695class BMCLogServiceCollection : public Node
1696{
1697 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001698 BMCLogServiceCollection(App& app) :
Ed Tanous4ed77cd2018-10-15 08:08:07 -07001699 Node(app, "/redfish/v1/Managers/bmc/LogServices/")
Ed Tanous1da66f72018-07-27 16:13:37 -07001700 {
Ed Tanous1da66f72018-07-27 16:13:37 -07001701 entityPrivileges = {
Jason M. Billse1f26342018-07-18 12:12:00 -07001702 {boost::beast::http::verb::get, {{"Login"}}},
1703 {boost::beast::http::verb::head, {{"Login"}}},
1704 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1705 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1706 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1707 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Ed Tanous1da66f72018-07-27 16:13:37 -07001708 }
1709
1710 private:
1711 /**
1712 * Functions triggers appropriate requests on DBus
1713 */
Ed Tanouscb13a392020-07-25 19:02:03 +00001714 void doGet(crow::Response& res, const crow::Request&,
1715 const std::vector<std::string>&) override
Ed Tanous1da66f72018-07-27 16:13:37 -07001716 {
Jason M. Billse1f26342018-07-18 12:12:00 -07001717 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1da66f72018-07-27 16:13:37 -07001718 // Collections don't include the static data added by SubRoute because
1719 // it has a duplicate entry for members
Jason M. Billse1f26342018-07-18 12:12:00 -07001720 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanous1da66f72018-07-27 16:13:37 -07001721 "#LogServiceCollection.LogServiceCollection";
Jason M. Billse1f26342018-07-18 12:12:00 -07001722 asyncResp->res.jsonValue["@odata.id"] =
1723 "/redfish/v1/Managers/bmc/LogServices";
1724 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1725 asyncResp->res.jsonValue["Description"] =
Ed Tanous1da66f72018-07-27 16:13:37 -07001726 "Collection of LogServices for this Manager";
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001727 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001728 logServiceArray = nlohmann::json::array();
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001729#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
1730 logServiceArray.push_back(
1731 {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Dump"}});
1732#endif
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001733#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1734 logServiceArray.push_back(
Anthony Wilson08a4e4b2019-04-12 08:23:05 -05001735 {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001736#endif
Jason M. Billse1f26342018-07-18 12:12:00 -07001737 asyncResp->res.jsonValue["Members@odata.count"] =
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001738 logServiceArray.size();
Ed Tanous1da66f72018-07-27 16:13:37 -07001739 }
1740};
1741
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001742class BMCJournalLogService : public Node
Ed Tanous1da66f72018-07-27 16:13:37 -07001743{
1744 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001745 BMCJournalLogService(App& app) :
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001746 Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Jason M. Billse1f26342018-07-18 12:12:00 -07001747 {
Jason M. Billse1f26342018-07-18 12:12:00 -07001748 entityPrivileges = {
1749 {boost::beast::http::verb::get, {{"Login"}}},
1750 {boost::beast::http::verb::head, {{"Login"}}},
1751 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1752 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1753 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1754 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1755 }
1756
1757 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00001758 void doGet(crow::Response& res, const crow::Request&,
1759 const std::vector<std::string>&) override
Jason M. Billse1f26342018-07-18 12:12:00 -07001760 {
1761 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Jason M. Billse1f26342018-07-18 12:12:00 -07001762 asyncResp->res.jsonValue["@odata.type"] =
1763 "#LogService.v1_1_0.LogService";
Ed Tanous0f74e642018-11-12 15:17:05 -08001764 asyncResp->res.jsonValue["@odata.id"] =
1765 "/redfish/v1/Managers/bmc/LogServices/Journal";
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001766 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
1767 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
1768 asyncResp->res.jsonValue["Id"] = "BMC Journal";
Jason M. Billse1f26342018-07-18 12:12:00 -07001769 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Jason M. Billscd50aa42019-02-12 17:09:02 -08001770 asyncResp->res.jsonValue["Entries"] = {
1771 {"@odata.id",
Ed Tanous086be232019-05-23 11:47:09 -07001772 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
Jason M. Billse1f26342018-07-18 12:12:00 -07001773 }
1774};
1775
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001776static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1777 sd_journal* journal,
1778 nlohmann::json& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07001779{
1780 // Get the Log Entry contents
1781 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07001782
Ed Tanous39e77502019-03-04 17:35:53 -08001783 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07001784 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07001785 if (ret < 0)
1786 {
1787 BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1788 return 1;
1789 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001790
1791 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07001792 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07001793 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07001794 if (ret < 0)
1795 {
1796 BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
Jason M. Billse1f26342018-07-18 12:12:00 -07001797 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001798
1799 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07001800 std::string entryTimeStr;
1801 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07001802 {
Jason M. Bills16428a12018-11-02 12:42:29 -07001803 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07001804 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001805
1806 // Fill in the log entry with the gathered data
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001807 bmcJournalLogEntryJson = {
Andrew Geisslercb92c032018-08-17 07:56:14 -07001808 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001809 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1810 bmcJournalLogEntryID},
Jason M. Billse1f26342018-07-18 12:12:00 -07001811 {"Name", "BMC Journal Entry"},
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001812 {"Id", bmcJournalLogEntryID},
Jason M. Bills16428a12018-11-02 12:42:29 -07001813 {"Message", msg},
Jason M. Billse1f26342018-07-18 12:12:00 -07001814 {"EntryType", "Oem"},
1815 {"Severity",
Jason M. Billsb6a61a52019-08-01 14:26:15 -07001816 severity <= 2 ? "Critical" : severity <= 4 ? "Warning" : "OK"},
Ed Tanous086be232019-05-23 11:47:09 -07001817 {"OemRecordFormat", "BMC Journal Entry"},
Jason M. Billse1f26342018-07-18 12:12:00 -07001818 {"Created", std::move(entryTimeStr)}};
1819 return 0;
1820}
1821
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001822class BMCJournalLogEntryCollection : public Node
Jason M. Billse1f26342018-07-18 12:12:00 -07001823{
1824 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001825 BMCJournalLogEntryCollection(App& app) :
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001826 Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Jason M. Billse1f26342018-07-18 12:12:00 -07001827 {
Jason M. Billse1f26342018-07-18 12:12:00 -07001828 entityPrivileges = {
1829 {boost::beast::http::verb::get, {{"Login"}}},
1830 {boost::beast::http::verb::head, {{"Login"}}},
1831 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1832 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1833 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1834 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1835 }
1836
1837 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001838 void doGet(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00001839 const std::vector<std::string>&) override
Jason M. Billse1f26342018-07-18 12:12:00 -07001840 {
1841 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001842 static constexpr const long maxEntriesPerPage = 1000;
Ed Tanous271584a2019-07-09 16:24:22 -07001843 uint64_t skip = 0;
1844 uint64_t top = maxEntriesPerPage; // Show max entries by default
Jason M. Bills16428a12018-11-02 12:42:29 -07001845 if (!getSkipParam(asyncResp->res, req, skip))
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001846 {
Jason M. Bills16428a12018-11-02 12:42:29 -07001847 return;
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001848 }
Jason M. Bills16428a12018-11-02 12:42:29 -07001849 if (!getTopParam(asyncResp->res, req, top))
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001850 {
Jason M. Bills16428a12018-11-02 12:42:29 -07001851 return;
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001852 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001853 // Collections don't include the static data added by SubRoute because
1854 // it has a duplicate entry for members
1855 asyncResp->res.jsonValue["@odata.type"] =
1856 "#LogEntryCollection.LogEntryCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08001857 asyncResp->res.jsonValue["@odata.id"] =
1858 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
Jason M. Billse1f26342018-07-18 12:12:00 -07001859 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001860 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
Jason M. Billse1f26342018-07-18 12:12:00 -07001861 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1862 asyncResp->res.jsonValue["Description"] =
1863 "Collection of BMC Journal Entries";
Ed Tanous0f74e642018-11-12 15:17:05 -08001864 asyncResp->res.jsonValue["@odata.id"] =
1865 "/redfish/v1/Managers/bmc/LogServices/BmcLog/Entries";
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001866 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Jason M. Billse1f26342018-07-18 12:12:00 -07001867 logEntryArray = nlohmann::json::array();
1868
1869 // Go through the journal and use the timestamp to create a unique ID
1870 // for each entry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001871 sd_journal* journalTmp = nullptr;
Jason M. Billse1f26342018-07-18 12:12:00 -07001872 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1873 if (ret < 0)
1874 {
1875 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001876 messages::internalError(asyncResp->res);
Jason M. Billse1f26342018-07-18 12:12:00 -07001877 return;
1878 }
1879 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1880 journalTmp, sd_journal_close);
1881 journalTmp = nullptr;
Ed Tanousb01bf292019-03-25 19:25:26 +00001882 uint64_t entryCount = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -07001883 // Reset the unique ID on the first entry
1884 bool firstEntry = true;
Jason M. Billse1f26342018-07-18 12:12:00 -07001885 SD_JOURNAL_FOREACH(journal.get())
1886 {
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001887 entryCount++;
1888 // Handle paging using skip (number of entries to skip from the
1889 // start) and top (number of entries to display)
1890 if (entryCount <= skip || entryCount > skip + top)
1891 {
1892 continue;
1893 }
1894
Jason M. Bills16428a12018-11-02 12:42:29 -07001895 std::string idStr;
Jason M. Billse85d6b12019-07-29 17:01:15 -07001896 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
Jason M. Billse1f26342018-07-18 12:12:00 -07001897 {
Jason M. Billse1f26342018-07-18 12:12:00 -07001898 continue;
1899 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001900
Jason M. Billse85d6b12019-07-29 17:01:15 -07001901 if (firstEntry)
1902 {
1903 firstEntry = false;
1904 }
1905
Jason M. Billse1f26342018-07-18 12:12:00 -07001906 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001907 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001908 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
1909 bmcJournalLogEntry) != 0)
Jason M. Billse1f26342018-07-18 12:12:00 -07001910 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001911 messages::internalError(asyncResp->res);
Jason M. Billse1f26342018-07-18 12:12:00 -07001912 return;
1913 }
1914 }
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001915 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1916 if (skip + top < entryCount)
1917 {
1918 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001919 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Jason M. Bills193ad2f2018-09-26 15:08:52 -07001920 std::to_string(skip + top);
1921 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001922 }
1923};
1924
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001925class BMCJournalLogEntry : public Node
Jason M. Billse1f26342018-07-18 12:12:00 -07001926{
1927 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001928 BMCJournalLogEntry(App& app) :
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001929 Node(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/",
Jason M. Billse1f26342018-07-18 12:12:00 -07001930 std::string())
1931 {
1932 entityPrivileges = {
1933 {boost::beast::http::verb::get, {{"Login"}}},
1934 {boost::beast::http::verb::head, {{"Login"}}},
1935 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1936 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1937 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1938 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1939 }
1940
1941 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00001942 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001943 const std::vector<std::string>& params) override
Jason M. Billse1f26342018-07-18 12:12:00 -07001944 {
1945 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1946 if (params.size() != 1)
1947 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001948 messages::internalError(asyncResp->res);
Jason M. Billse1f26342018-07-18 12:12:00 -07001949 return;
1950 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001951 const std::string& entryID = params[0];
Jason M. Billse1f26342018-07-18 12:12:00 -07001952 // Convert the unique ID back to a timestamp to find the entry
Jason M. Billse1f26342018-07-18 12:12:00 -07001953 uint64_t ts = 0;
Ed Tanous271584a2019-07-09 16:24:22 -07001954 uint64_t index = 0;
Jason M. Bills16428a12018-11-02 12:42:29 -07001955 if (!getTimestampFromID(asyncResp->res, entryID, ts, index))
Jason M. Billse1f26342018-07-18 12:12:00 -07001956 {
Jason M. Bills16428a12018-11-02 12:42:29 -07001957 return;
Jason M. Billse1f26342018-07-18 12:12:00 -07001958 }
1959
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001960 sd_journal* journalTmp = nullptr;
Jason M. Billse1f26342018-07-18 12:12:00 -07001961 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1962 if (ret < 0)
1963 {
1964 BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001965 messages::internalError(asyncResp->res);
Jason M. Billse1f26342018-07-18 12:12:00 -07001966 return;
1967 }
1968 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
1969 journalTmp, sd_journal_close);
1970 journalTmp = nullptr;
1971 // Go to the timestamp in the log and move to the entry at the index
Jason M. Billsaf07e3f2019-08-01 14:41:39 -07001972 // tracking the unique ID
1973 std::string idStr;
1974 bool firstEntry = true;
Jason M. Billse1f26342018-07-18 12:12:00 -07001975 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
Manojkiran Eda2056b6d2020-05-28 08:57:36 +05301976 if (ret < 0)
1977 {
1978 BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
1979 << strerror(-ret);
1980 messages::internalError(asyncResp->res);
1981 return;
1982 }
Ed Tanous271584a2019-07-09 16:24:22 -07001983 for (uint64_t i = 0; i <= index; i++)
Jason M. Billse1f26342018-07-18 12:12:00 -07001984 {
1985 sd_journal_next(journal.get());
Jason M. Billsaf07e3f2019-08-01 14:41:39 -07001986 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1987 {
1988 messages::internalError(asyncResp->res);
1989 return;
1990 }
1991 if (firstEntry)
1992 {
1993 firstEntry = false;
1994 }
Jason M. Billse1f26342018-07-18 12:12:00 -07001995 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001996 // Confirm that the entry ID matches what was requested
Jason M. Billsaf07e3f2019-08-01 14:41:39 -07001997 if (idStr != entryID)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001998 {
1999 messages::resourceMissingAtURI(asyncResp->res, entryID);
2000 return;
2001 }
2002
2003 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2004 asyncResp->res.jsonValue) != 0)
Jason M. Billse1f26342018-07-18 12:12:00 -07002005 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002006 messages::internalError(asyncResp->res);
Jason M. Billse1f26342018-07-18 12:12:00 -07002007 return;
2008 }
2009 }
2010};
2011
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002012class BMCDumpService : public Node
raviteja-bc9bb6862020-02-03 11:53:32 -06002013{
2014 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002015 BMCDumpService(App& app) :
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002016 Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
raviteja-bc9bb6862020-02-03 11:53:32 -06002017 {
2018 entityPrivileges = {
2019 {boost::beast::http::verb::get, {{"Login"}}},
2020 {boost::beast::http::verb::head, {{"Login"}}},
2021 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2022 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2023 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2024 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2025 }
2026
2027 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002028 void doGet(crow::Response& res, const crow::Request&,
2029 const std::vector<std::string>&) override
raviteja-bc9bb6862020-02-03 11:53:32 -06002030 {
2031 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2032
2033 asyncResp->res.jsonValue["@odata.id"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002034 "/redfish/v1/Managers/bmc/LogServices/Dump";
raviteja-bc9bb6862020-02-03 11:53:32 -06002035 asyncResp->res.jsonValue["@odata.type"] =
2036 "#LogService.v1_1_0.LogService";
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002037 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2038 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
2039 asyncResp->res.jsonValue["Id"] = "Dump";
raviteja-bc9bb6862020-02-03 11:53:32 -06002040 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
raviteja-bc9bb6862020-02-03 11:53:32 -06002041 asyncResp->res.jsonValue["Entries"] = {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002042 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
2043 asyncResp->res.jsonValue["Actions"] = {
2044 {"#LogService.ClearLog",
2045 {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2046 "Actions/LogService.ClearLog"}}},
2047 {"Oem",
2048 {{"#OemLogService.CollectDiagnosticData",
2049 {{"target",
2050 "/redfish/v1/Managers/bmc/LogServices/Dump/"
2051 "Actions/Oem/OemLogService.CollectDiagnosticData"}}}}}};
raviteja-bc9bb6862020-02-03 11:53:32 -06002052 }
2053};
2054
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002055class BMCDumpEntryCollection : public Node
raviteja-bc9bb6862020-02-03 11:53:32 -06002056{
2057 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002058 BMCDumpEntryCollection(App& app) :
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002059 Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
raviteja-bc9bb6862020-02-03 11:53:32 -06002060 {
2061 entityPrivileges = {
2062 {boost::beast::http::verb::get, {{"Login"}}},
2063 {boost::beast::http::verb::head, {{"Login"}}},
2064 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2065 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2066 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2067 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2068 }
2069
2070 private:
2071 /**
2072 * Functions triggers appropriate requests on DBus
2073 */
Ed Tanouscb13a392020-07-25 19:02:03 +00002074 void doGet(crow::Response& res, const crow::Request&,
2075 const std::vector<std::string>&) override
raviteja-bc9bb6862020-02-03 11:53:32 -06002076 {
2077 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2078
2079 asyncResp->res.jsonValue["@odata.type"] =
2080 "#LogEntryCollection.LogEntryCollection";
2081 asyncResp->res.jsonValue["@odata.id"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002082 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
2083 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
raviteja-bc9bb6862020-02-03 11:53:32 -06002084 asyncResp->res.jsonValue["Description"] =
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002085 "Collection of BMC Dump Entries";
raviteja-bc9bb6862020-02-03 11:53:32 -06002086
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002087 getDumpEntryCollection(asyncResp, "BMC");
raviteja-bc9bb6862020-02-03 11:53:32 -06002088 }
2089};
2090
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002091class BMCDumpEntry : public Node
raviteja-bc9bb6862020-02-03 11:53:32 -06002092{
2093 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002094 BMCDumpEntry(App& app) :
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002095 Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/",
raviteja-bc9bb6862020-02-03 11:53:32 -06002096 std::string())
2097 {
2098 entityPrivileges = {
2099 {boost::beast::http::verb::get, {{"Login"}}},
2100 {boost::beast::http::verb::head, {{"Login"}}},
2101 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2102 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2103 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2104 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2105 }
2106
2107 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002108 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002109 const std::vector<std::string>& params) override
raviteja-bc9bb6862020-02-03 11:53:32 -06002110 {
2111 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2112 if (params.size() != 1)
2113 {
2114 messages::internalError(asyncResp->res);
2115 return;
2116 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002117 getDumpEntryById(asyncResp, params[0], "BMC");
raviteja-bc9bb6862020-02-03 11:53:32 -06002118 }
2119
Ed Tanouscb13a392020-07-25 19:02:03 +00002120 void doDelete(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002121 const std::vector<std::string>& params) override
raviteja-bc9bb6862020-02-03 11:53:32 -06002122 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002123 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
raviteja-bc9bb6862020-02-03 11:53:32 -06002124 if (params.size() != 1)
2125 {
2126 messages::internalError(asyncResp->res);
2127 return;
2128 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002129 deleteDumpEntry(asyncResp->res, params[0]);
2130 }
2131};
raviteja-bc9bb6862020-02-03 11:53:32 -06002132
Asmitha Karunanithia43be802020-05-07 05:05:36 -05002133class BMCDumpCreate : public Node
2134{
2135 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002136 BMCDumpCreate(App& app) :
Asmitha Karunanithia43be802020-05-07 05:05:36 -05002137 Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2138 "Actions/Oem/"
2139 "OemLogService.CollectDiagnosticData/")
2140 {
2141 entityPrivileges = {
2142 {boost::beast::http::verb::get, {{"Login"}}},
2143 {boost::beast::http::verb::head, {{"Login"}}},
2144 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2145 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2146 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2147 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2148 }
2149
2150 private:
2151 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00002152 const std::vector<std::string>&) override
Asmitha Karunanithia43be802020-05-07 05:05:36 -05002153 {
2154 createDump(res, req, "BMC");
2155 }
2156};
2157
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05002158class BMCDumpClear : public Node
2159{
2160 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002161 BMCDumpClear(App& app) :
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05002162 Node(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2163 "Actions/"
2164 "LogService.ClearLog/")
2165 {
2166 entityPrivileges = {
2167 {boost::beast::http::verb::get, {{"Login"}}},
2168 {boost::beast::http::verb::head, {{"Login"}}},
2169 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2170 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2171 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2172 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2173 }
2174
2175 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002176 void doPost(crow::Response& res, const crow::Request&,
2177 const std::vector<std::string>&) override
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05002178 {
2179 clearDump(res, "xyz.openbmc_project.Dump.Entry.BMC");
2180 }
2181};
2182
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002183class SystemDumpService : public Node
2184{
2185 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002186 SystemDumpService(App& app) :
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002187 Node(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2188 {
2189 entityPrivileges = {
2190 {boost::beast::http::verb::get, {{"Login"}}},
2191 {boost::beast::http::verb::head, {{"Login"}}},
2192 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2193 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2194 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2195 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2196 }
raviteja-bc9bb6862020-02-03 11:53:32 -06002197
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002198 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002199 void doGet(crow::Response& res, const crow::Request&,
2200 const std::vector<std::string>&) override
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002201 {
2202 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
raviteja-bc9bb6862020-02-03 11:53:32 -06002203
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002204 asyncResp->res.jsonValue["@odata.id"] =
2205 "/redfish/v1/Systems/system/LogServices/Dump";
2206 asyncResp->res.jsonValue["@odata.type"] =
2207 "#LogService.v1_1_0.LogService";
2208 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2209 asyncResp->res.jsonValue["Description"] = "System Dump LogService";
2210 asyncResp->res.jsonValue["Id"] = "Dump";
2211 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2212 asyncResp->res.jsonValue["Entries"] = {
2213 {"@odata.id",
2214 "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
2215 asyncResp->res.jsonValue["Actions"] = {
2216 {"#LogService.ClearLog",
2217 {{"target", "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2218 "LogService.ClearLog"}}},
2219 {"Oem",
2220 {{"#OemLogService.CollectDiagnosticData",
2221 {{"target",
2222 "/redfish/v1/Systems/system/LogServices/Dump/Actions/Oem/"
2223 "OemLogService.CollectDiagnosticData"}}}}}};
2224 }
2225};
2226
2227class SystemDumpEntryCollection : public Node
2228{
2229 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002230 SystemDumpEntryCollection(App& app) :
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002231 Node(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2232 {
2233 entityPrivileges = {
2234 {boost::beast::http::verb::get, {{"Login"}}},
2235 {boost::beast::http::verb::head, {{"Login"}}},
2236 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2237 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2238 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2239 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2240 }
2241
2242 private:
2243 /**
2244 * Functions triggers appropriate requests on DBus
2245 */
Ed Tanouscb13a392020-07-25 19:02:03 +00002246 void doGet(crow::Response& res, const crow::Request&,
2247 const std::vector<std::string>&) override
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002248 {
2249 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2250
2251 asyncResp->res.jsonValue["@odata.type"] =
2252 "#LogEntryCollection.LogEntryCollection";
2253 asyncResp->res.jsonValue["@odata.id"] =
2254 "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2255 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
2256 asyncResp->res.jsonValue["Description"] =
2257 "Collection of System Dump Entries";
2258
2259 getDumpEntryCollection(asyncResp, "System");
2260 }
2261};
2262
2263class SystemDumpEntry : public Node
2264{
2265 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002266 SystemDumpEntry(App& app) :
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002267 Node(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/",
2268 std::string())
2269 {
2270 entityPrivileges = {
2271 {boost::beast::http::verb::get, {{"Login"}}},
2272 {boost::beast::http::verb::head, {{"Login"}}},
2273 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2274 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2275 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2276 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2277 }
2278
2279 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002280 void doGet(crow::Response& res, const crow::Request&,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002281 const std::vector<std::string>& params) override
2282 {
2283 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2284 if (params.size() != 1)
2285 {
2286 messages::internalError(asyncResp->res);
2287 return;
2288 }
2289 getDumpEntryById(asyncResp, params[0], "System");
2290 }
2291
Ed Tanouscb13a392020-07-25 19:02:03 +00002292 void doDelete(crow::Response& res, const crow::Request&,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05002293 const std::vector<std::string>& params) override
2294 {
2295 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2296 if (params.size() != 1)
2297 {
2298 messages::internalError(asyncResp->res);
2299 return;
2300 }
2301 deleteDumpEntry(asyncResp->res, params[0]);
raviteja-bc9bb6862020-02-03 11:53:32 -06002302 }
2303};
2304
Asmitha Karunanithia43be802020-05-07 05:05:36 -05002305class SystemDumpCreate : public Node
2306{
2307 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002308 SystemDumpCreate(App& app) :
Asmitha Karunanithia43be802020-05-07 05:05:36 -05002309 Node(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2310 "Actions/Oem/"
2311 "OemLogService.CollectDiagnosticData/")
2312 {
2313 entityPrivileges = {
2314 {boost::beast::http::verb::get, {{"Login"}}},
2315 {boost::beast::http::verb::head, {{"Login"}}},
2316 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2317 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2318 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2319 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2320 }
2321
2322 private:
2323 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00002324 const std::vector<std::string>&) override
Asmitha Karunanithia43be802020-05-07 05:05:36 -05002325 {
2326 createDump(res, req, "System");
2327 }
2328};
2329
raviteja-b013487e2020-03-03 03:20:48 -06002330class SystemDumpClear : public Node
2331{
2332 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002333 SystemDumpClear(App& app) :
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05002334 Node(app, "/redfish/v1/Systems/system/LogServices/Dump/"
raviteja-b013487e2020-03-03 03:20:48 -06002335 "Actions/"
2336 "LogService.ClearLog/")
2337 {
2338 entityPrivileges = {
2339 {boost::beast::http::verb::get, {{"Login"}}},
2340 {boost::beast::http::verb::head, {{"Login"}}},
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05002341 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2342 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2343 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
raviteja-b013487e2020-03-03 03:20:48 -06002344 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2345 }
2346
2347 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002348 void doPost(crow::Response& res, const crow::Request&,
2349 const std::vector<std::string>&) override
raviteja-b013487e2020-03-03 03:20:48 -06002350 {
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05002351 clearDump(res, "xyz.openbmc_project.Dump.Entry.System");
raviteja-b013487e2020-03-03 03:20:48 -06002352 }
2353};
2354
Jason M. Bills424c4172019-03-21 13:50:33 -07002355class CrashdumpService : public Node
Jason M. Billse1f26342018-07-18 12:12:00 -07002356{
2357 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002358 CrashdumpService(App& app) :
Jason M. Bills424c4172019-03-21 13:50:33 -07002359 Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
Ed Tanous1da66f72018-07-27 16:13:37 -07002360 {
AppaRao Puli39460282020-04-07 17:03:04 +05302361 // Note: Deviated from redfish privilege registry for GET & HEAD
2362 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002363 entityPrivileges = {
AppaRao Puli39460282020-04-07 17:03:04 +05302364 {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
2365 {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
Jason M. Billse1f26342018-07-18 12:12:00 -07002366 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2367 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2368 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2369 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Ed Tanous1da66f72018-07-27 16:13:37 -07002370 }
2371
2372 private:
2373 /**
2374 * Functions triggers appropriate requests on DBus
2375 */
Ed Tanouscb13a392020-07-25 19:02:03 +00002376 void doGet(crow::Response& res, const crow::Request&,
2377 const std::vector<std::string>&) override
Ed Tanous1da66f72018-07-27 16:13:37 -07002378 {
Jason M. Billse1f26342018-07-18 12:12:00 -07002379 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1da66f72018-07-27 16:13:37 -07002380 // Copy over the static data to include the entries added by SubRoute
Ed Tanous0f74e642018-11-12 15:17:05 -08002381 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Bills424c4172019-03-21 13:50:33 -07002382 "/redfish/v1/Systems/system/LogServices/Crashdump";
Jason M. Billse1f26342018-07-18 12:12:00 -07002383 asyncResp->res.jsonValue["@odata.type"] =
2384 "#LogService.v1_1_0.LogService";
Gunnar Mills4f50ae42020-02-06 15:29:57 -06002385 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2386 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2387 asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
Jason M. Billse1f26342018-07-18 12:12:00 -07002388 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2389 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Jason M. Billscd50aa42019-02-12 17:09:02 -08002390 asyncResp->res.jsonValue["Entries"] = {
2391 {"@odata.id",
Jason M. Bills424c4172019-03-21 13:50:33 -07002392 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
Jason M. Billse1f26342018-07-18 12:12:00 -07002393 asyncResp->res.jsonValue["Actions"] = {
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002394 {"#LogService.ClearLog",
2395 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2396 "Actions/LogService.ClearLog"}}},
Ed Tanous1da66f72018-07-27 16:13:37 -07002397 {"Oem",
Jason M. Bills424c4172019-03-21 13:50:33 -07002398 {{"#Crashdump.OnDemand",
2399 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002400 "Actions/Oem/Crashdump.OnDemand"}}},
2401 {"#Crashdump.Telemetry",
2402 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2403 "Actions/Oem/Crashdump.Telemetry"}}}}}};
Ed Tanous1da66f72018-07-27 16:13:37 -07002404
2405#ifdef BMCWEB_ENABLE_REDFISH_RAW_PECI
Jason M. Billse1f26342018-07-18 12:12:00 -07002406 asyncResp->res.jsonValue["Actions"]["Oem"].push_back(
Jason M. Bills424c4172019-03-21 13:50:33 -07002407 {"#Crashdump.SendRawPeci",
Anthony Wilson08a4e4b2019-04-12 08:23:05 -05002408 {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
2409 "Actions/Oem/Crashdump.SendRawPeci"}}});
Ed Tanous1da66f72018-07-27 16:13:37 -07002410#endif
Ed Tanous1da66f72018-07-27 16:13:37 -07002411 }
2412};
2413
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002414class CrashdumpClear : public Node
2415{
2416 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002417 CrashdumpClear(App& app) :
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002418 Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
2419 "LogService.ClearLog/")
2420 {
AppaRao Puli39460282020-04-07 17:03:04 +05302421 // Note: Deviated from redfish privilege registry for GET & HEAD
2422 // method for security reasons.
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002423 entityPrivileges = {
AppaRao Puli39460282020-04-07 17:03:04 +05302424 {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
2425 {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002426 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2427 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2428 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2429 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2430 }
2431
2432 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002433 void doPost(crow::Response& res, const crow::Request&,
2434 const std::vector<std::string>&) override
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002435 {
2436 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2437
2438 crow::connections::systemBus->async_method_call(
2439 [asyncResp](const boost::system::error_code ec,
Ed Tanouscb13a392020-07-25 19:02:03 +00002440 const std::string&) {
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002441 if (ec)
2442 {
2443 messages::internalError(asyncResp->res);
2444 return;
2445 }
2446 messages::success(asyncResp->res);
2447 },
2448 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
2449 }
2450};
2451
Jason M. Billse855dd22019-10-08 11:37:48 -07002452static void logCrashdumpEntry(std::shared_ptr<AsyncResp> asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002453 const std::string& logID,
2454 nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07002455{
Johnathan Mantey043a0532020-03-10 17:15:28 -07002456 auto getStoredLogCallback =
2457 [asyncResp, logID, &logEntryJson](
2458 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002459 const std::vector<std::pair<std::string, VariantType>>& params) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002460 if (ec)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002461 {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002462 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2463 if (ec.value() ==
2464 boost::system::linux_error::bad_request_descriptor)
2465 {
2466 messages::resourceNotFound(asyncResp->res, "LogEntry",
2467 logID);
2468 }
2469 else
2470 {
2471 messages::internalError(asyncResp->res);
2472 }
2473 return;
Jason M. Bills1ddcf012019-11-26 14:59:21 -08002474 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002475
Johnathan Mantey043a0532020-03-10 17:15:28 -07002476 std::string timestamp{};
2477 std::string filename{};
2478 std::string logfile{};
2479 ParseCrashdumpParameters(params, filename, timestamp, logfile);
2480
2481 if (filename.empty() || timestamp.empty())
2482 {
2483 messages::resourceMissingAtURI(asyncResp->res, logID);
2484 return;
2485 }
2486
2487 std::string crashdumpURI =
2488 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2489 logID + "/" + filename;
2490 logEntryJson = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
2491 {"@odata.id", "/redfish/v1/Systems/system/"
2492 "LogServices/Crashdump/Entries/" +
2493 logID},
2494 {"Name", "CPU Crashdump"},
2495 {"Id", logID},
2496 {"EntryType", "Oem"},
2497 {"OemRecordFormat", "Crashdump URI"},
2498 {"Message", std::move(crashdumpURI)},
2499 {"Created", std::move(timestamp)}};
2500 };
Jason M. Billse855dd22019-10-08 11:37:48 -07002501 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002502 std::move(getStoredLogCallback), crashdumpObject,
2503 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002504 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Jason M. Billse855dd22019-10-08 11:37:48 -07002505}
2506
Jason M. Bills424c4172019-03-21 13:50:33 -07002507class CrashdumpEntryCollection : public Node
Ed Tanous1da66f72018-07-27 16:13:37 -07002508{
2509 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002510 CrashdumpEntryCollection(App& app) :
Jason M. Bills424c4172019-03-21 13:50:33 -07002511 Node(app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
Ed Tanous1da66f72018-07-27 16:13:37 -07002512 {
AppaRao Puli39460282020-04-07 17:03:04 +05302513 // Note: Deviated from redfish privilege registry for GET & HEAD
2514 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002515 entityPrivileges = {
AppaRao Puli39460282020-04-07 17:03:04 +05302516 {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
2517 {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
Jason M. Billse1f26342018-07-18 12:12:00 -07002518 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2519 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2520 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2521 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Ed Tanous1da66f72018-07-27 16:13:37 -07002522 }
2523
2524 private:
2525 /**
2526 * Functions triggers appropriate requests on DBus
2527 */
Ed Tanouscb13a392020-07-25 19:02:03 +00002528 void doGet(crow::Response& res, const crow::Request&,
2529 const std::vector<std::string>&) override
Ed Tanous1da66f72018-07-27 16:13:37 -07002530 {
Jason M. Billse1f26342018-07-18 12:12:00 -07002531 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1da66f72018-07-27 16:13:37 -07002532 // Collections don't include the static data added by SubRoute because
2533 // it has a duplicate entry for members
Jason M. Billse1f26342018-07-18 12:12:00 -07002534 auto getLogEntriesCallback = [asyncResp](
2535 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002536 const std::vector<std::string>& resp) {
Jason M. Billse1f26342018-07-18 12:12:00 -07002537 if (ec)
2538 {
2539 if (ec.value() !=
2540 boost::system::errc::no_such_file_or_directory)
Ed Tanous1da66f72018-07-27 16:13:37 -07002541 {
Jason M. Billse1f26342018-07-18 12:12:00 -07002542 BMCWEB_LOG_DEBUG << "failed to get entries ec: "
2543 << ec.message();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002544 messages::internalError(asyncResp->res);
Jason M. Billse1f26342018-07-18 12:12:00 -07002545 return;
Ed Tanous1da66f72018-07-27 16:13:37 -07002546 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002547 }
2548 asyncResp->res.jsonValue["@odata.type"] =
2549 "#LogEntryCollection.LogEntryCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002550 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Bills424c4172019-03-21 13:50:33 -07002551 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Jason M. Bills424c4172019-03-21 13:50:33 -07002552 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
Jason M. Billse1f26342018-07-18 12:12:00 -07002553 asyncResp->res.jsonValue["Description"] =
Jason M. Bills424c4172019-03-21 13:50:33 -07002554 "Collection of Crashdump Entries";
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002555 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Jason M. Billse1f26342018-07-18 12:12:00 -07002556 logEntryArray = nlohmann::json::array();
Jason M. Billse855dd22019-10-08 11:37:48 -07002557 std::vector<std::string> logIDs;
2558 // Get the list of log entries and build up an empty array big
2559 // enough to hold them
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002560 for (const std::string& objpath : resp)
Jason M. Billse1f26342018-07-18 12:12:00 -07002561 {
Jason M. Billse855dd22019-10-08 11:37:48 -07002562 // Get the log ID
Jason M. Billse1f26342018-07-18 12:12:00 -07002563 std::size_t lastPos = objpath.rfind("/");
Jason M. Billse855dd22019-10-08 11:37:48 -07002564 if (lastPos == std::string::npos)
Jason M. Billse1f26342018-07-18 12:12:00 -07002565 {
Jason M. Billse855dd22019-10-08 11:37:48 -07002566 continue;
Jason M. Billse1f26342018-07-18 12:12:00 -07002567 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002568 logIDs.emplace_back(objpath.substr(lastPos + 1));
2569
2570 // Add a space for the log entry to the array
2571 logEntryArray.push_back({});
2572 }
2573 // Now go through and set up async calls to fill in the entries
2574 size_t index = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002575 for (const std::string& logID : logIDs)
Jason M. Billse855dd22019-10-08 11:37:48 -07002576 {
2577 // Add the log entry to the array
2578 logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
Jason M. Billse1f26342018-07-18 12:12:00 -07002579 }
2580 asyncResp->res.jsonValue["Members@odata.count"] =
2581 logEntryArray.size();
2582 };
Ed Tanous1da66f72018-07-27 16:13:37 -07002583 crow::connections::systemBus->async_method_call(
2584 std::move(getLogEntriesCallback),
2585 "xyz.openbmc_project.ObjectMapper",
2586 "/xyz/openbmc_project/object_mapper",
2587 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002588 std::array<const char*, 1>{crashdumpInterface});
Ed Tanous1da66f72018-07-27 16:13:37 -07002589 }
2590};
2591
Jason M. Bills424c4172019-03-21 13:50:33 -07002592class CrashdumpEntry : public Node
Ed Tanous1da66f72018-07-27 16:13:37 -07002593{
2594 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002595 CrashdumpEntry(App& app) :
Jason M. Billsd53dd412019-02-12 17:16:22 -08002596 Node(app,
Jason M. Bills424c4172019-03-21 13:50:33 -07002597 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/",
Ed Tanous1da66f72018-07-27 16:13:37 -07002598 std::string())
2599 {
AppaRao Puli39460282020-04-07 17:03:04 +05302600 // Note: Deviated from redfish privilege registry for GET & HEAD
2601 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002602 entityPrivileges = {
AppaRao Puli39460282020-04-07 17:03:04 +05302603 {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
2604 {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
Jason M. Billse1f26342018-07-18 12:12:00 -07002605 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2606 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2607 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2608 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Ed Tanous1da66f72018-07-27 16:13:37 -07002609 }
2610
2611 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002612 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002613 const std::vector<std::string>& params) override
Ed Tanous1da66f72018-07-27 16:13:37 -07002614 {
Jason M. Billse1f26342018-07-18 12:12:00 -07002615 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1da66f72018-07-27 16:13:37 -07002616 if (params.size() != 1)
2617 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002618 messages::internalError(asyncResp->res);
Ed Tanous1da66f72018-07-27 16:13:37 -07002619 return;
2620 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002621 const std::string& logID = params[0];
Jason M. Billse855dd22019-10-08 11:37:48 -07002622 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2623 }
2624};
2625
2626class CrashdumpFile : public Node
2627{
2628 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002629 CrashdumpFile(App& app) :
Jason M. Billse855dd22019-10-08 11:37:48 -07002630 Node(app,
2631 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/"
2632 "<str>/",
2633 std::string(), std::string())
2634 {
AppaRao Puli39460282020-04-07 17:03:04 +05302635 // Note: Deviated from redfish privilege registry for GET & HEAD
2636 // method for security reasons.
Jason M. Billse855dd22019-10-08 11:37:48 -07002637 entityPrivileges = {
AppaRao Puli39460282020-04-07 17:03:04 +05302638 {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
2639 {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
Jason M. Billse855dd22019-10-08 11:37:48 -07002640 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2641 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2642 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2643 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2644 }
2645
2646 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002647 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002648 const std::vector<std::string>& params) override
Jason M. Billse855dd22019-10-08 11:37:48 -07002649 {
2650 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2651 if (params.size() != 2)
2652 {
2653 messages::internalError(asyncResp->res);
2654 return;
2655 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002656 const std::string& logID = params[0];
2657 const std::string& fileName = params[1];
Jason M. Billse855dd22019-10-08 11:37:48 -07002658
Johnathan Mantey043a0532020-03-10 17:15:28 -07002659 auto getStoredLogCallback =
2660 [asyncResp, logID, fileName](
2661 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002662 const std::vector<std::pair<std::string, VariantType>>& resp) {
Johnathan Mantey043a0532020-03-10 17:15:28 -07002663 if (ec)
2664 {
2665 BMCWEB_LOG_DEBUG << "failed to get log ec: "
2666 << ec.message();
2667 messages::internalError(asyncResp->res);
2668 return;
2669 }
Jason M. Billse855dd22019-10-08 11:37:48 -07002670
Johnathan Mantey043a0532020-03-10 17:15:28 -07002671 std::string dbusFilename{};
2672 std::string dbusTimestamp{};
2673 std::string dbusFilepath{};
Jason M. Billse855dd22019-10-08 11:37:48 -07002674
Johnathan Mantey043a0532020-03-10 17:15:28 -07002675 ParseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2676 dbusFilepath);
2677
2678 if (dbusFilename.empty() || dbusTimestamp.empty() ||
2679 dbusFilepath.empty())
2680 {
2681 messages::resourceMissingAtURI(asyncResp->res, fileName);
2682 return;
2683 }
2684
2685 // Verify the file name parameter is correct
2686 if (fileName != dbusFilename)
2687 {
2688 messages::resourceMissingAtURI(asyncResp->res, fileName);
2689 return;
2690 }
2691
2692 if (!std::filesystem::exists(dbusFilepath))
2693 {
2694 messages::resourceMissingAtURI(asyncResp->res, fileName);
2695 return;
2696 }
2697 std::ifstream ifs(dbusFilepath, std::ios::in |
2698 std::ios::binary |
2699 std::ios::ate);
2700 std::ifstream::pos_type fileSize = ifs.tellg();
2701 if (fileSize < 0)
2702 {
2703 messages::generalError(asyncResp->res);
2704 return;
2705 }
2706 ifs.seekg(0, std::ios::beg);
2707
2708 auto crashData = std::make_unique<char[]>(
2709 static_cast<unsigned int>(fileSize));
2710
2711 ifs.read(crashData.get(), static_cast<int>(fileSize));
2712
2713 // The cast to std::string is intentional in order to use the
2714 // assign() that applies move mechanics
2715 asyncResp->res.body().assign(
2716 static_cast<std::string>(crashData.get()));
2717
2718 // Configure this to be a file download when accessed from
2719 // a browser
2720 asyncResp->res.addHeader("Content-Disposition", "attachment");
2721 };
Ed Tanous1da66f72018-07-27 16:13:37 -07002722 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002723 std::move(getStoredLogCallback), crashdumpObject,
2724 crashdumpPath + std::string("/") + logID,
Johnathan Mantey043a0532020-03-10 17:15:28 -07002725 "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
Ed Tanous1da66f72018-07-27 16:13:37 -07002726 }
2727};
2728
Jason M. Bills424c4172019-03-21 13:50:33 -07002729class OnDemandCrashdump : public Node
Ed Tanous1da66f72018-07-27 16:13:37 -07002730{
2731 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002732 OnDemandCrashdump(App& app) :
Jason M. Bills424c4172019-03-21 13:50:33 -07002733 Node(app,
2734 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
2735 "Crashdump.OnDemand/")
Ed Tanous1da66f72018-07-27 16:13:37 -07002736 {
AppaRao Puli39460282020-04-07 17:03:04 +05302737 // Note: Deviated from redfish privilege registry for GET & HEAD
2738 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002739 entityPrivileges = {
AppaRao Puli39460282020-04-07 17:03:04 +05302740 {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
2741 {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2742 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2743 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2744 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2745 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Ed Tanous1da66f72018-07-27 16:13:37 -07002746 }
2747
2748 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002749 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00002750 const std::vector<std::string>&) override
Ed Tanous1da66f72018-07-27 16:13:37 -07002751 {
Jason M. Billse1f26342018-07-18 12:12:00 -07002752 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1da66f72018-07-27 16:13:37 -07002753
James Feistfe306722020-03-12 16:32:08 -07002754 auto generateonDemandLogCallback = [asyncResp,
2755 req](const boost::system::error_code
2756 ec,
Ed Tanouscb13a392020-07-25 19:02:03 +00002757 const std::string&) {
James Feist46229572020-02-19 15:11:58 -08002758 if (ec)
2759 {
2760 if (ec.value() == boost::system::errc::operation_not_supported)
Ed Tanous1da66f72018-07-27 16:13:37 -07002761 {
James Feist46229572020-02-19 15:11:58 -08002762 messages::resourceInStandby(asyncResp->res);
Ed Tanous1da66f72018-07-27 16:13:37 -07002763 }
James Feist46229572020-02-19 15:11:58 -08002764 else if (ec.value() ==
2765 boost::system::errc::device_or_resource_busy)
2766 {
2767 messages::serviceTemporarilyUnavailable(asyncResp->res,
2768 "60");
2769 }
2770 else
2771 {
2772 messages::internalError(asyncResp->res);
2773 }
2774 return;
2775 }
2776 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002777 [](boost::system::error_code err, sdbusplus::message::message&,
2778 const std::shared_ptr<task::TaskData>& taskData) {
James Feist66afe4f2020-02-24 13:09:58 -08002779 if (!err)
2780 {
James Feiste5d50062020-05-11 17:29:00 -07002781 taskData->messages.emplace_back(
2782 messages::taskCompletedOK(
2783 std::to_string(taskData->index)));
James Feist831d6b02020-03-12 16:31:30 -07002784 taskData->state = "Completed";
James Feist66afe4f2020-02-24 13:09:58 -08002785 }
James Feist32898ce2020-03-10 16:16:52 -07002786 return task::completed;
James Feist66afe4f2020-02-24 13:09:58 -08002787 },
James Feist46229572020-02-19 15:11:58 -08002788 "type='signal',interface='org.freedesktop.DBus.Properties',"
2789 "member='PropertiesChanged',arg0namespace='com.intel."
2790 "crashdump'");
2791 task->startTimer(std::chrono::minutes(5));
2792 task->populateResp(asyncResp->res);
James Feistfe306722020-03-12 16:32:08 -07002793 task->payload.emplace(req);
James Feist46229572020-02-19 15:11:58 -08002794 };
Ed Tanous1da66f72018-07-27 16:13:37 -07002795 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002796 std::move(generateonDemandLogCallback), crashdumpObject,
2797 crashdumpPath, crashdumpOnDemandInterface, "GenerateOnDemandLog");
Ed Tanous1da66f72018-07-27 16:13:37 -07002798 }
2799};
2800
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002801class TelemetryCrashdump : public Node
2802{
2803 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002804 TelemetryCrashdump(App& app) :
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002805 Node(app,
2806 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
2807 "Crashdump.Telemetry/")
2808 {
2809 // Note: Deviated from redfish privilege registry for GET & HEAD
2810 // method for security reasons.
2811 entityPrivileges = {
2812 {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
2813 {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2814 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2815 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2816 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2817 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2818 }
2819
2820 private:
2821 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00002822 const std::vector<std::string>&) override
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002823 {
2824 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2825
2826 auto generateTelemetryLogCallback = [asyncResp, req](
2827 const boost::system::error_code
2828 ec,
Ed Tanouscb13a392020-07-25 19:02:03 +00002829 const std::string&) {
Kenny L. Ku6eda7682020-06-19 09:48:36 -07002830 if (ec)
2831 {
2832 if (ec.value() == boost::system::errc::operation_not_supported)
2833 {
2834 messages::resourceInStandby(asyncResp->res);
2835 }
2836 else if (ec.value() ==
2837 boost::system::errc::device_or_resource_busy)
2838 {
2839 messages::serviceTemporarilyUnavailable(asyncResp->res,
2840 "60");
2841 }
2842 else
2843 {
2844 messages::internalError(asyncResp->res);
2845 }
2846 return;
2847 }
2848 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
2849 [](boost::system::error_code err, sdbusplus::message::message&,
2850 const std::shared_ptr<task::TaskData>& taskData) {
2851 if (!err)
2852 {
2853 taskData->messages.emplace_back(
2854 messages::taskCompletedOK(
2855 std::to_string(taskData->index)));
2856 taskData->state = "Completed";
2857 }
2858 return task::completed;
2859 },
2860 "type='signal',interface='org.freedesktop.DBus.Properties',"
2861 "member='PropertiesChanged',arg0namespace='com.intel."
2862 "crashdump'");
2863 task->startTimer(std::chrono::minutes(5));
2864 task->populateResp(asyncResp->res);
2865 task->payload.emplace(req);
2866 };
2867 crow::connections::systemBus->async_method_call(
2868 std::move(generateTelemetryLogCallback), crashdumpObject,
2869 crashdumpPath, crashdumpTelemetryInterface, "GenerateTelemetryLog");
2870 }
2871};
2872
Jason M. Billse1f26342018-07-18 12:12:00 -07002873class SendRawPECI : public Node
Ed Tanous1da66f72018-07-27 16:13:37 -07002874{
2875 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002876 SendRawPECI(App& app) :
Jason M. Bills424c4172019-03-21 13:50:33 -07002877 Node(app,
2878 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/Oem/"
2879 "Crashdump.SendRawPeci/")
Ed Tanous1da66f72018-07-27 16:13:37 -07002880 {
AppaRao Puli39460282020-04-07 17:03:04 +05302881 // Note: Deviated from redfish privilege registry for GET & HEAD
2882 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07002883 entityPrivileges = {
2884 {boost::beast::http::verb::get, {{"ConfigureComponents"}}},
2885 {boost::beast::http::verb::head, {{"ConfigureComponents"}}},
2886 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2887 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2888 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2889 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2890 }
2891
2892 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002893 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00002894 const std::vector<std::string>&) override
Ed Tanous1da66f72018-07-27 16:13:37 -07002895 {
Jason M. Billse1f26342018-07-18 12:12:00 -07002896 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002897 std::vector<std::vector<uint8_t>> peciCommands;
Ed Tanousb1556422018-10-16 14:09:17 -07002898
Karthick Sundarrajanf0b6ae02020-01-17 13:32:58 -08002899 if (!json_util::readJson(req, res, "PECICommands", peciCommands))
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002900 {
Karthick Sundarrajanf0b6ae02020-01-17 13:32:58 -08002901 return;
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002902 }
Karthick Sundarrajanf0b6ae02020-01-17 13:32:58 -08002903 uint32_t idx = 0;
2904 for (auto const& cmd : peciCommands)
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002905 {
Karthick Sundarrajanf0b6ae02020-01-17 13:32:58 -08002906 if (cmd.size() < 3)
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002907 {
Karthick Sundarrajanf0b6ae02020-01-17 13:32:58 -08002908 std::string s("[");
2909 for (auto const& val : cmd)
2910 {
2911 if (val != *cmd.begin())
2912 {
2913 s += ",";
2914 }
2915 s += std::to_string(val);
2916 }
2917 s += "]";
2918 messages::actionParameterValueFormatError(
2919 res, s, "PECICommands[" + std::to_string(idx) + "]",
2920 "SendRawPeci");
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002921 return;
2922 }
Karthick Sundarrajanf0b6ae02020-01-17 13:32:58 -08002923 idx++;
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002924 }
Ed Tanous1da66f72018-07-27 16:13:37 -07002925 // Callback to return the Raw PECI response
Jason M. Billse1f26342018-07-18 12:12:00 -07002926 auto sendRawPECICallback =
2927 [asyncResp](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002928 const std::vector<std::vector<uint8_t>>& resp) {
Jason M. Billse1f26342018-07-18 12:12:00 -07002929 if (ec)
2930 {
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002931 BMCWEB_LOG_DEBUG << "failed to process PECI commands ec: "
Jason M. Billse1f26342018-07-18 12:12:00 -07002932 << ec.message();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002933 messages::internalError(asyncResp->res);
Jason M. Billse1f26342018-07-18 12:12:00 -07002934 return;
2935 }
2936 asyncResp->res.jsonValue = {{"Name", "PECI Command Response"},
2937 {"PECIResponse", resp}};
2938 };
Ed Tanous1da66f72018-07-27 16:13:37 -07002939 // Call the SendRawPECI command with the provided data
2940 crow::connections::systemBus->async_method_call(
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07002941 std::move(sendRawPECICallback), crashdumpObject, crashdumpPath,
Karthick Sundarrajan8724c292020-01-06 09:04:48 -08002942 crashdumpRawPECIInterface, "SendRawPeci", peciCommands);
Ed Tanous1da66f72018-07-27 16:13:37 -07002943 }
2944};
2945
Andrew Geisslercb92c032018-08-17 07:56:14 -07002946/**
2947 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2948 */
2949class DBusLogServiceActionsClear : public Node
2950{
2951 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002952 DBusLogServiceActionsClear(App& app) :
Andrew Geisslercb92c032018-08-17 07:56:14 -07002953 Node(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
Gunnar Mills7af91512020-04-14 22:16:57 -05002954 "LogService.ClearLog/")
Andrew Geisslercb92c032018-08-17 07:56:14 -07002955 {
2956 entityPrivileges = {
2957 {boost::beast::http::verb::get, {{"Login"}}},
2958 {boost::beast::http::verb::head, {{"Login"}}},
2959 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2960 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2961 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2962 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
2963 }
2964
2965 private:
2966 /**
2967 * Function handles POST method request.
2968 * The Clear Log actions does not require any parameter.The action deletes
2969 * all entries found in the Entries collection for this Log Service.
2970 */
Ed Tanouscb13a392020-07-25 19:02:03 +00002971 void doPost(crow::Response& res, const crow::Request&,
2972 const std::vector<std::string>&) override
Andrew Geisslercb92c032018-08-17 07:56:14 -07002973 {
2974 BMCWEB_LOG_DEBUG << "Do delete all entries.";
2975
2976 auto asyncResp = std::make_shared<AsyncResp>(res);
2977 // Process response from Logging service.
2978 auto resp_handler = [asyncResp](const boost::system::error_code ec) {
2979 BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
2980 if (ec)
2981 {
2982 // TODO Handle for specific error code
2983 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
2984 asyncResp->res.result(
2985 boost::beast::http::status::internal_server_error);
2986 return;
2987 }
2988
2989 asyncResp->res.result(boost::beast::http::status::no_content);
2990 };
2991
2992 // Make call to Logging service to request Clear Log
2993 crow::connections::systemBus->async_method_call(
2994 resp_handler, "xyz.openbmc_project.Logging",
2995 "/xyz/openbmc_project/logging",
2996 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2997 }
2998};
ZhikuiRena3316fc2020-01-29 14:58:08 -08002999
3000/****************************************************
3001 * Redfish PostCode interfaces
3002 * using DBUS interface: getPostCodesTS
3003 ******************************************************/
3004class PostCodesLogService : public Node
3005{
3006 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07003007 PostCodesLogService(App& app) :
ZhikuiRena3316fc2020-01-29 14:58:08 -08003008 Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3009 {
3010 entityPrivileges = {
3011 {boost::beast::http::verb::get, {{"Login"}}},
3012 {boost::beast::http::verb::head, {{"Login"}}},
3013 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3014 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3015 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3016 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3017 }
3018
3019 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00003020 void doGet(crow::Response& res, const crow::Request&,
3021 const std::vector<std::string>&) override
ZhikuiRena3316fc2020-01-29 14:58:08 -08003022 {
3023 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3024
3025 asyncResp->res.jsonValue = {
3026 {"@odata.id", "/redfish/v1/Systems/system/LogServices/PostCodes"},
3027 {"@odata.type", "#LogService.v1_1_0.LogService"},
3028 {"@odata.context", "/redfish/v1/$metadata#LogService.LogService"},
3029 {"Name", "POST Code Log Service"},
3030 {"Description", "POST Code Log Service"},
3031 {"Id", "BIOS POST Code Log"},
3032 {"OverWritePolicy", "WrapsWhenFull"},
3033 {"Entries",
3034 {{"@odata.id",
3035 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
3036 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3037 {"target", "/redfish/v1/Systems/system/LogServices/PostCodes/"
3038 "Actions/LogService.ClearLog"}};
3039 }
3040};
3041
3042class PostCodesClear : public Node
3043{
3044 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07003045 PostCodesClear(App& app) :
ZhikuiRena3316fc2020-01-29 14:58:08 -08003046 Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
3047 "LogService.ClearLog/")
3048 {
3049 entityPrivileges = {
3050 {boost::beast::http::verb::get, {{"Login"}}},
3051 {boost::beast::http::verb::head, {{"Login"}}},
AppaRao Puli39460282020-04-07 17:03:04 +05303052 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
3053 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
3054 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
3055 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
ZhikuiRena3316fc2020-01-29 14:58:08 -08003056 }
3057
3058 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00003059 void doPost(crow::Response& res, const crow::Request&,
3060 const std::vector<std::string>&) override
ZhikuiRena3316fc2020-01-29 14:58:08 -08003061 {
3062 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3063
3064 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3065 // Make call to post-code service to request clear all
3066 crow::connections::systemBus->async_method_call(
3067 [asyncResp](const boost::system::error_code ec) {
3068 if (ec)
3069 {
3070 // TODO Handle for specific error code
3071 BMCWEB_LOG_ERROR
3072 << "doClearPostCodes resp_handler got error " << ec;
3073 asyncResp->res.result(
3074 boost::beast::http::status::internal_server_error);
3075 messages::internalError(asyncResp->res);
3076 return;
3077 }
3078 },
3079 "xyz.openbmc_project.State.Boot.PostCode",
3080 "/xyz/openbmc_project/State/Boot/PostCode",
3081 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3082 }
3083};
3084
3085static void fillPostCodeEntry(
3086 std::shared_ptr<AsyncResp> aResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003087 const boost::container::flat_map<uint64_t, uint64_t>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003088 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3089 const uint64_t skip = 0, const uint64_t top = 0)
3090{
3091 // Get the Message from the MessageRegistry
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003092 const message_registries::Message* message =
ZhikuiRena3316fc2020-01-29 14:58:08 -08003093 message_registries::getMessage("OpenBMC.0.1.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003094
3095 uint64_t currentCodeIndex = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003096 nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003097
3098 uint64_t firstCodeTimeUs = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003099 for (const std::pair<uint64_t, uint64_t>& code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003100 {
3101 currentCodeIndex++;
3102 std::string postcodeEntryID =
3103 "B" + std::to_string(bootIndex) + "-" +
3104 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3105
3106 uint64_t usecSinceEpoch = code.first;
3107 uint64_t usTimeOffset = 0;
3108
3109 if (1 == currentCodeIndex)
3110 { // already incremented
3111 firstCodeTimeUs = code.first;
3112 }
3113 else
3114 {
3115 usTimeOffset = code.first - firstCodeTimeUs;
3116 }
3117
3118 // skip if no specific codeIndex is specified and currentCodeIndex does
3119 // not fall between top and skip
3120 if ((codeIndex == 0) &&
3121 (currentCodeIndex <= skip || currentCodeIndex > top))
3122 {
3123 continue;
3124 }
3125
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003126 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003127 // currentIndex
3128 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3129 {
3130 // This is done for simplicity. 1st entry is needed to calculate
3131 // time offset. To improve efficiency, one can get to the entry
3132 // directly (possibly with flatmap's nth method)
3133 continue;
3134 }
3135
3136 // currentCodeIndex is within top and skip or equal to specified code
3137 // index
3138
3139 // Get the Created time from the timestamp
3140 std::string entryTimeStr;
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -05003141 entryTimeStr = crow::utility::getDateTime(
3142 static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
ZhikuiRena3316fc2020-01-29 14:58:08 -08003143
3144 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3145 std::ostringstream hexCode;
3146 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
3147 << code.second;
3148 std::ostringstream timeOffsetStr;
3149 // Set Fixed -Point Notation
3150 timeOffsetStr << std::fixed;
3151 // Set precision to 4 digits
3152 timeOffsetStr << std::setprecision(4);
3153 // Add double to stream
3154 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3155 std::vector<std::string> messageArgs = {
3156 std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3157
3158 // Get MessageArgs template from message registry
3159 std::string msg;
3160 if (message != nullptr)
3161 {
3162 msg = message->message;
3163
3164 // fill in this post code value
3165 int i = 0;
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003166 for (const std::string& messageArg : messageArgs)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003167 {
3168 std::string argStr = "%" + std::to_string(++i);
3169 size_t argPos = msg.find(argStr);
3170 if (argPos != std::string::npos)
3171 {
3172 msg.replace(argPos, argStr.length(), messageArg);
3173 }
3174 }
3175 }
3176
Tim Leed4342a92020-04-27 11:47:58 +08003177 // Get Severity template from message registry
3178 std::string severity;
3179 if (message != nullptr)
3180 {
3181 severity = message->severity;
3182 }
3183
ZhikuiRena3316fc2020-01-29 14:58:08 -08003184 // add to AsyncResp
3185 logEntryArray.push_back({});
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003186 nlohmann::json& bmcLogEntry = logEntryArray.back();
ZhikuiRena3316fc2020-01-29 14:58:08 -08003187 bmcLogEntry = {
3188 {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
3189 {"@odata.context", "/redfish/v1/$metadata#LogEntry.LogEntry"},
3190 {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
3191 "PostCodes/Entries/" +
3192 postcodeEntryID},
3193 {"Name", "POST Code Log Entry"},
3194 {"Id", postcodeEntryID},
3195 {"Message", std::move(msg)},
3196 {"MessageId", "OpenBMC.0.1.BIOSPOSTCode"},
3197 {"MessageArgs", std::move(messageArgs)},
3198 {"EntryType", "Event"},
3199 {"Severity", std::move(severity)},
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -05003200 {"Created", entryTimeStr}};
ZhikuiRena3316fc2020-01-29 14:58:08 -08003201 }
3202}
3203
3204static void getPostCodeForEntry(std::shared_ptr<AsyncResp> aResp,
3205 const uint16_t bootIndex,
3206 const uint64_t codeIndex)
3207{
3208 crow::connections::systemBus->async_method_call(
3209 [aResp, bootIndex, codeIndex](
3210 const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003211 const boost::container::flat_map<uint64_t, uint64_t>& postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003212 if (ec)
3213 {
3214 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3215 messages::internalError(aResp->res);
3216 return;
3217 }
3218
3219 // skip the empty postcode boots
3220 if (postcode.empty())
3221 {
3222 return;
3223 }
3224
3225 fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3226
3227 aResp->res.jsonValue["Members@odata.count"] =
3228 aResp->res.jsonValue["Members"].size();
3229 },
3230 "xyz.openbmc_project.State.Boot.PostCode",
3231 "/xyz/openbmc_project/State/Boot/PostCode",
3232 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3233 bootIndex);
3234}
3235
3236static void getPostCodeForBoot(std::shared_ptr<AsyncResp> aResp,
3237 const uint16_t bootIndex,
3238 const uint16_t bootCount,
3239 const uint64_t entryCount, const uint64_t skip,
3240 const uint64_t top)
3241{
3242 crow::connections::systemBus->async_method_call(
3243 [aResp, bootIndex, bootCount, entryCount, skip,
3244 top](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003245 const boost::container::flat_map<uint64_t, uint64_t>& postcode) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003246 if (ec)
3247 {
3248 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3249 messages::internalError(aResp->res);
3250 return;
3251 }
3252
3253 uint64_t endCount = entryCount;
3254 if (!postcode.empty())
3255 {
3256 endCount = entryCount + postcode.size();
3257
3258 if ((skip < endCount) && ((top + skip) > entryCount))
3259 {
3260 uint64_t thisBootSkip =
3261 std::max(skip, entryCount) - entryCount;
3262 uint64_t thisBootTop =
3263 std::min(top + skip, endCount) - entryCount;
3264
3265 fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3266 thisBootSkip, thisBootTop);
3267 }
3268 aResp->res.jsonValue["Members@odata.count"] = endCount;
3269 }
3270
3271 // continue to previous bootIndex
3272 if (bootIndex < bootCount)
3273 {
3274 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3275 bootCount, endCount, skip, top);
3276 }
3277 else
3278 {
3279 aResp->res.jsonValue["Members@odata.nextLink"] =
3280 "/redfish/v1/Systems/system/LogServices/PostCodes/"
3281 "Entries?$skip=" +
3282 std::to_string(skip + top);
3283 }
3284 },
3285 "xyz.openbmc_project.State.Boot.PostCode",
3286 "/xyz/openbmc_project/State/Boot/PostCode",
3287 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3288 bootIndex);
3289}
3290
3291static void getCurrentBootNumber(std::shared_ptr<AsyncResp> aResp,
3292 const uint64_t skip, const uint64_t top)
3293{
3294 uint64_t entryCount = 0;
3295 crow::connections::systemBus->async_method_call(
3296 [aResp, entryCount, skip,
3297 top](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003298 const std::variant<uint16_t>& bootCount) {
ZhikuiRena3316fc2020-01-29 14:58:08 -08003299 if (ec)
3300 {
3301 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3302 messages::internalError(aResp->res);
3303 return;
3304 }
3305 auto pVal = std::get_if<uint16_t>(&bootCount);
3306 if (pVal)
3307 {
3308 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3309 }
3310 else
3311 {
3312 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3313 }
3314 },
3315 "xyz.openbmc_project.State.Boot.PostCode",
3316 "/xyz/openbmc_project/State/Boot/PostCode",
3317 "org.freedesktop.DBus.Properties", "Get",
3318 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3319}
3320
3321class PostCodesEntryCollection : public Node
3322{
3323 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07003324 PostCodesEntryCollection(App& app) :
ZhikuiRena3316fc2020-01-29 14:58:08 -08003325 Node(app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3326 {
3327 entityPrivileges = {
3328 {boost::beast::http::verb::get, {{"Login"}}},
3329 {boost::beast::http::verb::head, {{"Login"}}},
3330 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3331 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3332 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3333 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3334 }
3335
3336 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003337 void doGet(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +00003338 const std::vector<std::string>&) override
ZhikuiRena3316fc2020-01-29 14:58:08 -08003339 {
3340 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3341
3342 asyncResp->res.jsonValue["@odata.type"] =
3343 "#LogEntryCollection.LogEntryCollection";
3344 asyncResp->res.jsonValue["@odata.context"] =
3345 "/redfish/v1/"
3346 "$metadata#LogEntryCollection.LogEntryCollection";
3347 asyncResp->res.jsonValue["@odata.id"] =
3348 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3349 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3350 asyncResp->res.jsonValue["Description"] =
3351 "Collection of POST Code Log Entries";
3352 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3353 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3354
3355 uint64_t skip = 0;
3356 uint64_t top = maxEntriesPerPage; // Show max entries by default
3357 if (!getSkipParam(asyncResp->res, req, skip))
3358 {
3359 return;
3360 }
3361 if (!getTopParam(asyncResp->res, req, top))
3362 {
3363 return;
3364 }
3365 getCurrentBootNumber(asyncResp, skip, top);
3366 }
3367};
3368
3369class PostCodesEntry : public Node
3370{
3371 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07003372 PostCodesEntry(App& app) :
ZhikuiRena3316fc2020-01-29 14:58:08 -08003373 Node(app,
3374 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/",
3375 std::string())
3376 {
3377 entityPrivileges = {
3378 {boost::beast::http::verb::get, {{"Login"}}},
3379 {boost::beast::http::verb::head, {{"Login"}}},
3380 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
3381 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
3382 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
3383 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
3384 }
3385
3386 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00003387 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003388 const std::vector<std::string>& params) override
ZhikuiRena3316fc2020-01-29 14:58:08 -08003389 {
3390 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3391 if (params.size() != 1)
3392 {
3393 messages::internalError(asyncResp->res);
3394 return;
3395 }
3396
Gunnar Mills1214b7e2020-06-04 10:11:30 -05003397 const std::string& targetID = params[0];
ZhikuiRena3316fc2020-01-29 14:58:08 -08003398
3399 size_t bootPos = targetID.find('B');
3400 if (bootPos == std::string::npos)
3401 {
3402 // Requested ID was not found
3403 messages::resourceMissingAtURI(asyncResp->res, targetID);
3404 return;
3405 }
3406 std::string_view bootIndexStr(targetID);
3407 bootIndexStr.remove_prefix(bootPos + 1);
3408 uint16_t bootIndex = 0;
3409 uint64_t codeIndex = 0;
3410 size_t dashPos = bootIndexStr.find('-');
3411
3412 if (dashPos == std::string::npos)
3413 {
3414 return;
3415 }
3416 std::string_view codeIndexStr(bootIndexStr);
3417 bootIndexStr.remove_suffix(dashPos);
3418 codeIndexStr.remove_prefix(dashPos + 1);
3419
3420 bootIndex = static_cast<uint16_t>(
Ed Tanous23a21a12020-07-25 04:45:05 +00003421 strtoul(std::string(bootIndexStr).c_str(), nullptr, 0));
3422 codeIndex = strtoul(std::string(codeIndexStr).c_str(), nullptr, 0);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003423 if (bootIndex == 0 || codeIndex == 0)
3424 {
3425 BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3426 << params[0];
3427 }
3428
3429 asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3430 asyncResp->res.jsonValue["@odata.context"] =
3431 "/redfish/v1/$metadata#LogEntry.LogEntry";
3432 asyncResp->res.jsonValue["@odata.id"] =
3433 "/redfish/v1/Systems/system/LogServices/PostCodes/"
3434 "Entries";
3435 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3436 asyncResp->res.jsonValue["Description"] =
3437 "Collection of POST Code Log Entries";
3438 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3439 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3440
3441 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3442 }
3443};
3444
Ed Tanous1da66f72018-07-27 16:13:37 -07003445} // namespace redfish