blob: 983d58954fa22ea041b24183ac42bd96f91b72bf [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 IBM 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 */
Aatir7b291ec2019-11-19 10:37:37 -060016#include "config.h"
17
18#include "../bcd_time.hpp"
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +080019#include "../json_utils.hpp"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080020#include "../paths.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050021#include "../pel.hpp"
Aatir7b291ec2019-11-19 10:37:37 -060022#include "../pel_types.hpp"
23#include "../pel_values.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050024
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +080025#include <Python.h>
26
Aatir186ce8c2019-10-20 15:13:39 -050027#include <CLI/CLI.hpp>
Aatir7b291ec2019-11-19 10:37:37 -060028#include <bitset>
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +080029#include <fstream>
Aatir186ce8c2019-10-20 15:13:39 -050030#include <iostream>
Aatir7b291ec2019-11-19 10:37:37 -060031#include <phosphor-logging/log.hpp>
32#include <regex>
Aatir186ce8c2019-10-20 15:13:39 -050033#include <string>
Aatir7b291ec2019-11-19 10:37:37 -060034#include <xyz/openbmc_project/Common/File/error.hpp>
Aatir186ce8c2019-10-20 15:13:39 -050035
Aatir7b291ec2019-11-19 10:37:37 -060036namespace fs = std::filesystem;
Aatir186ce8c2019-10-20 15:13:39 -050037using namespace phosphor::logging;
38using namespace openpower::pels;
Matt Spinler27de6f32020-02-21 08:35:57 -060039using sdbusplus::exception::SdBusError;
Aatir7b291ec2019-11-19 10:37:37 -060040namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
41namespace message = openpower::pels::message;
42namespace pv = openpower::pels::pel_values;
Aatir186ce8c2019-10-20 15:13:39 -050043
Matt Spinler27de6f32020-02-21 08:35:57 -060044using PELFunc = std::function<void(const PEL&)>;
Matt Spinler0d804ef2020-05-12 16:16:26 -050045message::Registry registry(getPELReadOnlyDataPath() / message::registryFileName,
Matt Spinlerd8e29002020-04-09 09:11:22 -050046 false);
Matt Spinler27de6f32020-02-21 08:35:57 -060047namespace service
48{
49constexpr auto logging = "xyz.openbmc_project.Logging";
50} // namespace service
51
52namespace interface
53{
54constexpr auto deleteObj = "xyz.openbmc_project.Object.Delete";
55constexpr auto deleteAll = "xyz.openbmc_project.Collection.DeleteAll";
56} // namespace interface
57
58namespace object_path
59{
60constexpr auto logEntry = "/xyz/openbmc_project/logging/entry/";
61constexpr auto logging = "/xyz/openbmc_project/logging";
62} // namespace object_path
63
Aatire340c132019-12-09 14:19:29 -060064/**
Aatir37822f62019-12-10 14:40:27 -060065 * @brief helper function to get PEL commit timestamp from file name
66 * @retrun BCDTime - PEL commit timestamp
67 * @param[in] std::string - file name
68 */
69BCDTime fileNameToTimestamp(const std::string& fileName)
70{
71 std::string token = fileName.substr(0, fileName.find("_"));
72 int i = 0;
73 BCDTime tmp;
74 if (token.length() >= 14)
75 {
76 try
77 {
78 tmp.yearMSB = std::stoi(token.substr(i, 2), 0, 16);
79 }
80 catch (std::exception& err)
81 {
82 std::cout << "Conversion failure: " << err.what() << std::endl;
83 }
84 i += 2;
85 try
86 {
87 tmp.yearLSB = std::stoi(token.substr(i, 2), 0, 16);
88 }
89 catch (std::exception& err)
90 {
91 std::cout << "Conversion failure: " << err.what() << std::endl;
92 }
93 i += 2;
94 try
95 {
96 tmp.month = std::stoi(token.substr(i, 2), 0, 16);
97 }
98 catch (std::exception& err)
99 {
100 std::cout << "Conversion failure: " << err.what() << std::endl;
101 }
102 i += 2;
103 try
104 {
105 tmp.day = std::stoi(token.substr(i, 2), 0, 16);
106 }
107 catch (std::exception& err)
108 {
109 std::cout << "Conversion failure: " << err.what() << std::endl;
110 }
111 i += 2;
112 try
113 {
114 tmp.hour = std::stoi(token.substr(i, 2), 0, 16);
115 }
116 catch (std::exception& err)
117 {
118 std::cout << "Conversion failure: " << err.what() << std::endl;
119 }
120 i += 2;
121 try
122 {
123 tmp.minutes = std::stoi(token.substr(i, 2), 0, 16);
124 }
125 catch (std::exception& err)
126 {
127 std::cout << "Conversion failure: " << err.what() << std::endl;
128 }
129 i += 2;
130 try
131 {
132 tmp.seconds = std::stoi(token.substr(i, 2), 0, 16);
133 }
134 catch (std::exception& err)
135 {
136 std::cout << "Conversion failure: " << err.what() << std::endl;
137 }
138 i += 2;
139 try
140 {
141 tmp.hundredths = std::stoi(token.substr(i, 2), 0, 16);
142 }
143 catch (std::exception& err)
144 {
145 std::cout << "Conversion failure: " << err.what() << std::endl;
146 }
147 }
148 return tmp;
149}
150
151/**
152 * @brief helper function to get PEL id from file name
153 * @retrun uint32_t - PEL id
154 * @param[in] std::string - file name
155 */
156uint32_t fileNameToPELId(const std::string& fileName)
157{
158 uint32_t num = 0;
159 try
160 {
161 num = std::stoi(fileName.substr(fileName.find("_") + 1), 0, 16);
162 }
163 catch (std::exception& err)
164 {
165 std::cout << "Conversion failure: " << err.what() << std::endl;
166 }
167 return num;
168}
169
170/**
Aatire340c132019-12-09 14:19:29 -0600171 * @brief helper function to check string suffix
172 * @retrun bool - true with suffix matches
173 * @param[in] std::string - string to check for suffix
174 * @param[in] std::string - suffix string
175 */
176bool ends_with(const std::string& str, const std::string& end)
177{
178 size_t slen = str.size(), elen = end.size();
179 if (slen < elen)
180 return false;
181 while (elen)
182 {
183 if (str[--slen] != end[--elen])
184 return false;
185 }
186 return true;
187}
188
Aatir37822f62019-12-10 14:40:27 -0600189/**
190 * @brief get data form raw PEL file.
191 * @param[in] std::string Name of file with raw PEL
192 * @return std::vector<uint8_t> char vector read from raw PEL file.
193 */
Aatirbad5f8a2019-12-10 15:27:16 -0600194std::vector<uint8_t> getFileData(const std::string& name)
Aatir37822f62019-12-10 14:40:27 -0600195{
196 std::ifstream file(name, std::ifstream::in);
197 if (file.good())
198 {
199 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
200 std::istreambuf_iterator<char>()};
201 return data;
202 }
203 else
204 {
Aatir37822f62019-12-10 14:40:27 -0600205 return {};
206 }
207}
208
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800209/**
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800210 * @brief Initialize Python interpreter and gather all UD parser modules under
211 * the paths found in Python sys.path and the current user directory.
212 * This is to prevent calling a non-existant module which causes Python
213 * to print an import error message and breaking JSON output.
214 *
215 * @return std::vector<std::string> Vector of plugins found in filesystem
216 */
217std::vector<std::string> getPlugins()
218{
219 Py_Initialize();
220 std::vector<std::string> plugins;
221 std::vector<std::string> siteDirs;
222 PyObject* pName = PyUnicode_FromString("sys");
223 PyObject* pModule = PyImport_Import(pName);
224 Py_XDECREF(pName);
225 PyObject* pDict = PyModule_GetDict(pModule);
226 Py_XDECREF(pModule);
227 PyObject* pResult = PyDict_GetItemString(pDict, "path");
228 PyObject* pValue = PyUnicode_FromString(".");
229 PyList_Append(pResult, pValue);
230 Py_XDECREF(pValue);
231 auto list_size = PyList_Size(pResult);
232 for (auto i = 0; i < list_size; i++)
233 {
234 PyObject* item = PyList_GetItem(pResult, i);
235 PyObject* pBytes = PyUnicode_AsEncodedString(item, "utf-8", "~E~");
236 const char* output = PyBytes_AS_STRING(pBytes);
237 Py_XDECREF(pBytes);
238 std::string tmpStr(output);
239 siteDirs.push_back(tmpStr);
240 }
241 for (const auto& dir : siteDirs)
242 {
243 if (fs::exists(dir + "/udparsers"))
244 {
245 for (const auto& entry : fs::directory_iterator(dir + "/udparsers"))
246 {
247 if (entry.is_directory() and
248 fs::exists(entry.path().string() + "/" +
249 entry.path().stem().string() + ".py"))
250 {
251 plugins.push_back(entry.path().stem());
252 }
253 }
254 }
255 }
256 return plugins;
257}
258
259/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800260 * @brief Creates JSON string of a PEL entry if fullPEL is false or prints to
261 * stdout the full PEL in JSON if fullPEL is true
262 * @param[in] itr - std::map iterator of <uint32_t, BCDTime>
263 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800264 * @param[in] includeInfo - Boolean to include informational PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800265 * @param[in] fullPEL - Boolean to print full JSON representation of PEL
266 * @param[in] foundPEL - Boolean to check if any PEL is present
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800267 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800268 * @param[in] plugins - Vector of strings of plugins found in filesystem
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800269 * @return std::string - JSON string of PEL entry (empty if fullPEL is true)
270 */
Aatir7b291ec2019-11-19 10:37:37 -0600271template <typename T>
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800272std::string genPELJSON(T itr, bool hidden, bool includeInfo, bool fullPEL,
273 bool& foundPEL,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800274 const std::optional<std::regex>& scrubRegex,
275 const std::vector<std::string>& plugins)
Aatir7b291ec2019-11-19 10:37:37 -0600276{
277 std::size_t found;
278 std::string val;
279 char tmpValStr[50];
280 std::string listStr;
281 char name[50];
282 sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", itr.second.yearMSB,
283 itr.second.yearLSB, itr.second.month, itr.second.day,
284 itr.second.hour, itr.second.minutes, itr.second.seconds,
285 itr.second.hundredths, itr.first);
286 std::string fileName(name);
287 fileName = EXTENSION_PERSIST_DIR "/pels/logs/" + fileName;
288 try
289 {
Aatir37822f62019-12-10 14:40:27 -0600290 std::vector<uint8_t> data = getFileData(fileName);
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800291 if (data.empty())
Aatir37822f62019-12-10 14:40:27 -0600292 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800293 log<level::ERR>("Empty PEL file",
294 entry("FILENAME=%s", fileName.c_str()));
295 return listStr;
296 }
297 PEL pel{data};
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800298 if (!pel.valid())
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800299 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800300 return listStr;
301 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800302 if (!includeInfo && pel.userHeader().severity() == 0)
303 {
304 return listStr;
305 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800306 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
307 if (!hidden && actionFlags.test(hiddenFlagBit))
308 {
309 return listStr;
310 }
311 if (pel.primarySRC() && scrubRegex)
312 {
313 val = pel.primarySRC().value()->asciiString();
314 if (std::regex_search(trimEnd(val), scrubRegex.value(),
315 std::regex_constants::match_not_null))
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800316 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800317 return listStr;
318 }
319 }
320 if (fullPEL)
321 {
322 if (!foundPEL)
323 {
324 std::cout << "[\n";
325 foundPEL = true;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800326 }
327 else
Aatir7b291ec2019-11-19 10:37:37 -0600328 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800329 std::cout << ",\n\n";
330 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800331 pel.toJSON(registry, plugins);
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800332 }
333 else
334 {
335 // id
336 listStr += "\t\"" +
337 getNumberString("0x%X", pel.privateHeader().id()) +
338 "\": {\n";
339 // ASCII
340 if (pel.primarySRC())
341 {
342 val = pel.primarySRC().value()->asciiString();
343 listStr += "\t\t\"SRC\": \"" + trimEnd(val) + "\",\n";
344 // Registry message
345 auto regVal = pel.primarySRC().value()->getErrorDetails(
346 registry, DetailLevel::message, true);
347 if (regVal)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800348 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800349 val = regVal.value();
350 listStr += "\t\t\"Message\": \"" + val + "\",\n";
Aatir37822f62019-12-10 14:40:27 -0600351 }
Aatir7b291ec2019-11-19 10:37:37 -0600352 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800353 else
354 {
355 listStr += "\t\t\"SRC\": \"No SRC\",\n";
356 }
357 // platformid
358 listStr += "\t\t\"PLID\": \"" +
359 getNumberString("0x%X", pel.privateHeader().plid()) +
360 "\",\n";
361 // creatorid
362 std::string creatorID =
363 getNumberString("%c", pel.privateHeader().creatorID());
364 val = pv::creatorIDs.count(creatorID) ? pv::creatorIDs.at(creatorID)
365 : "Unknown Creator ID";
366 listStr += "\t\t\"CreatorID\": \"" + val + "\",\n";
367 // subsytem
368 std::string subsystem = pv::getValue(pel.userHeader().subsystem(),
369 pel_values::subsystemValues);
370 listStr += "\t\t\"Subsystem\": \"" + subsystem + "\",\n";
371 // commit time
372 sprintf(tmpValStr, "%02X/%02X/%02X%02X %02X:%02X:%02X",
373 pel.privateHeader().commitTimestamp().month,
374 pel.privateHeader().commitTimestamp().day,
375 pel.privateHeader().commitTimestamp().yearMSB,
376 pel.privateHeader().commitTimestamp().yearLSB,
377 pel.privateHeader().commitTimestamp().hour,
378 pel.privateHeader().commitTimestamp().minutes,
379 pel.privateHeader().commitTimestamp().seconds);
380 val = std::string(tmpValStr);
381 listStr += "\t\t\"Commit Time\": \"" + val + "\",\n";
382 // severity
383 std::string severity = pv::getValue(pel.userHeader().severity(),
384 pel_values::severityValues);
385 listStr += "\t\t\"Sev\": \"" + severity + "\",\n ";
386 // compID
387 listStr += "\t\t\"CompID\": \"" +
388 getNumberString(
389 "0x%X", pel.privateHeader().header().componentID) +
390 "\",\n ";
391 found = listStr.rfind(",");
392 if (found != std::string::npos)
393 {
394 listStr.replace(found, 1, "");
395 listStr += "\t},\n";
396 }
397 foundPEL = true;
Aatir7b291ec2019-11-19 10:37:37 -0600398 }
399 }
400 catch (std::exception& e)
401 {
402 log<level::ERR>("Hit exception while reading PEL File",
403 entry("FILENAME=%s", fileName.c_str()),
404 entry("ERROR=%s", e.what()));
405 }
406 return listStr;
407}
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800408
Aatir7b291ec2019-11-19 10:37:37 -0600409/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800410 * @brief Print a list of PELs or a JSON array of PELs
411 * @param[in] order - Boolean to print in reverse orser
412 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800413 * @param[in] includeInfo - Boolean to include informational PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800414 * @param[in] fullPEL - Boolean to print full PEL into a JSON array
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800415 * @param[in] scrubRegex - SRC regex object
Aatir7b291ec2019-11-19 10:37:37 -0600416 */
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800417void printPELs(bool order, bool hidden, bool includeInfo, bool fullPEL,
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800418 const std::optional<std::regex>& scrubRegex)
Aatir7b291ec2019-11-19 10:37:37 -0600419{
420 std::string listStr;
421 std::map<uint32_t, BCDTime> PELs;
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800422 std::vector<std::string> plugins;
Aatir7b291ec2019-11-19 10:37:37 -0600423 listStr = "{\n";
424 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
425 it != fs::directory_iterator(); ++it)
426 {
427 if (!fs::is_regular_file((*it).path()))
428 {
429 continue;
430 }
Aatir37822f62019-12-10 14:40:27 -0600431 else
Aatir7b291ec2019-11-19 10:37:37 -0600432 {
Aatir37822f62019-12-10 14:40:27 -0600433 PELs.emplace(fileNameToPELId((*it).path().filename()),
434 fileNameToTimestamp((*it).path().filename()));
Aatir7b291ec2019-11-19 10:37:37 -0600435 }
436 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800437 bool foundPEL = false;
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800438 if (fullPEL)
439 {
440 plugins = getPlugins();
441 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800442 auto buildJSON = [&listStr, &hidden, &includeInfo, &fullPEL, &foundPEL,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800443 &scrubRegex, &plugins](const auto& i) {
444 listStr += genPELJSON(i, hidden, includeInfo, fullPEL, foundPEL,
445 scrubRegex, plugins);
Aatir37822f62019-12-10 14:40:27 -0600446 };
Aatir7b291ec2019-11-19 10:37:37 -0600447 if (order)
448 {
449 std::for_each(PELs.rbegin(), PELs.rend(), buildJSON);
450 }
451 else
452 {
453 std::for_each(PELs.begin(), PELs.end(), buildJSON);
454 }
455
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800456 if (foundPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600457 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800458 if (fullPEL)
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800459 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800460 std::cout << "]" << std::endl;
461 }
462 else
463 {
464 std::size_t found;
465 found = listStr.rfind(",");
466 if (found != std::string::npos)
467 {
468 listStr.replace(found, 1, "");
469 listStr += "}\n";
470 printf("%s", listStr.c_str());
471 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800472 }
473 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800474 else
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800475 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800476 std::string emptyJSON = fullPEL ? "[]" : "{}";
477 std::cout << emptyJSON << std::endl;
Aatir7b291ec2019-11-19 10:37:37 -0600478 }
479}
Aatir186ce8c2019-10-20 15:13:39 -0500480
Matt Spinler27de6f32020-02-21 08:35:57 -0600481/**
482 * @brief Calls the function passed in on the PEL with the ID
483 * passed in.
484 *
485 * @param[in] id - The string version of the PEL ID, either with or
486 * without the 0x prefix.
487 * @param[in] func - The std::function<void(const PEL&)> function to run.
488 */
489void callFunctionOnPEL(const std::string& id, const PELFunc& func)
490{
491 std::string pelID{id};
492 std::transform(pelID.begin(), pelID.end(), pelID.begin(), toupper);
493
494 if (pelID.find("0X") == 0)
495 {
496 pelID.erase(0, 2);
497 }
498
499 bool found = false;
500
501 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
502 it != fs::directory_iterator(); ++it)
503 {
504 // The PEL ID is part of the filename, so use that to find the PEL.
505
506 if (!fs::is_regular_file((*it).path()))
507 {
508 continue;
509 }
510
511 if (ends_with((*it).path(), pelID))
512 {
513 found = true;
514
515 auto data = getFileData((*it).path());
516 if (!data.empty())
517 {
518 PEL pel{data};
519
520 try
521 {
522 func(pel);
523 }
524 catch (std::exception& e)
525 {
526 std::cerr
527 << " Internal function threw an exception: " << e.what()
528 << "\n";
529 exit(1);
530 }
531 }
532 else
533 {
534 std::cerr << "Could not read PEL file\n";
535 exit(1);
536 }
537 break;
538 }
539 }
540
541 if (!found)
542 {
543 std::cerr << "PEL not found\n";
544 exit(1);
545 }
546}
547
548/**
549 * @brief Delete a PEL by deleting its corresponding event log.
550 *
551 * @param[in] pel - The PEL to delete
552 */
553void deletePEL(const PEL& pel)
554{
555 std::string path{object_path::logEntry};
556 path += std::to_string(pel.obmcLogID());
557
558 try
559 {
560 auto bus = sdbusplus::bus::new_default();
561 auto method = bus.new_method_call(service::logging, path.c_str(),
562 interface::deleteObj, "Delete");
563 auto reply = bus.call(method);
564 }
565 catch (const SdBusError& e)
566 {
567 std::cerr << "D-Bus call to delete event log " << pel.obmcLogID()
568 << " failed: " << e.what() << "\n";
569 exit(1);
570 }
571}
572
573/**
574 * @brief Delete all PELs by deleting all event logs.
575 */
576void deleteAllPELs()
577{
578 try
579 {
580 // This may move to an audit log some day
581 log<level::INFO>("peltool deleting all event logs");
582
583 auto bus = sdbusplus::bus::new_default();
584 auto method =
585 bus.new_method_call(service::logging, object_path::logging,
586 interface::deleteAll, "DeleteAll");
587 auto reply = bus.call(method);
588 }
589 catch (const SdBusError& e)
590 {
591 std::cerr << "D-Bus call to delete all event logs failed: " << e.what()
592 << "\n";
593 exit(1);
594 }
595}
596
Matt Spinler1b420bc2020-02-21 08:54:25 -0600597/**
598 * @brief Display a single PEL
599 *
600 * @param[in] pel - the PEL to display
601 */
602void displayPEL(const PEL& pel)
603{
604 if (pel.valid())
605 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800606 auto plugins = getPlugins();
607 pel.toJSON(registry, plugins);
Matt Spinler1b420bc2020-02-21 08:54:25 -0600608 }
609 else
610 {
611 std::cerr << "PEL was malformed\n";
612 exit(1);
613 }
614}
615
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800616/**
617 * @brief Print number of PELs
618 * @param[in] hidden - Bool to include hidden logs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800619 * @param[in] includeInfo - Bool to include informational logs
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800620 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800621 */
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800622void printPELCount(bool hidden, bool includeInfo,
623 const std::optional<std::regex>& scrubRegex)
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800624{
625 std::size_t count = 0;
626 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
627 it != fs::directory_iterator(); ++it)
628 {
629 if (!fs::is_regular_file((*it).path()))
630 {
631 continue;
632 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800633 std::vector<uint8_t> data = getFileData((*it).path());
634 if (data.empty())
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800635 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800636 continue;
637 }
638 PEL pel{data};
639 if (!pel.valid())
640 {
641 continue;
642 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800643 if (!includeInfo && pel.userHeader().severity() == 0)
644 {
645 continue;
646 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800647 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
648 if (!hidden && actionFlags.test(hiddenFlagBit))
649 {
650 continue;
651 }
652 if (pel.primarySRC() && scrubRegex)
653 {
654 std::string val = pel.primarySRC().value()->asciiString();
655 if (std::regex_search(trimEnd(val), scrubRegex.value(),
656 std::regex_constants::match_not_null))
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800657 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800658 continue;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800659 }
660 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800661 count++;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800662 }
663 std::cout << "{\n"
664 << " \"Number of PELs found\": "
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800665 << getNumberString("%d", count) << "\n}\n";
666}
667
668/**
669 * @brief Generate regex pattern object from file contents
670 * @param[in] scrubFile - File containing regex pattern
671 * @return std::regex - SRC regex object
672 */
673std::regex genRegex(std::string& scrubFile)
674{
675 std::string pattern;
676 std::ifstream contents(scrubFile);
677 if (contents.fail())
678 {
679 std::cerr << "Can't open \"" << scrubFile << "\"\n";
680 exit(1);
681 }
682 std::string line;
683 while (std::getline(contents, line))
684 {
685 if (!line.empty())
686 {
687 pattern.append(line + "|");
688 }
689 }
690 try
691 {
692 std::regex scrubRegex(pattern, std::regex::icase);
693 return scrubRegex;
694 }
695 catch (std::regex_error& e)
696 {
697 if (e.code() == std::regex_constants::error_collate)
698 std::cerr << "Invalid collating element request\n";
699 else if (e.code() == std::regex_constants::error_ctype)
700 std::cerr << "Invalid character class\n";
701 else if (e.code() == std::regex_constants::error_escape)
702 std::cerr << "Invalid escape character or trailing escape\n";
703 else if (e.code() == std::regex_constants::error_backref)
704 std::cerr << "Invalid back reference\n";
705 else if (e.code() == std::regex_constants::error_brack)
706 std::cerr << "Mismatched bracket ([ or ])\n";
707 else if (e.code() == std::regex_constants::error_paren)
708 {
709 // to catch return code error_badrepeat when error_paren is retured
710 // instead
711 size_t pos = pattern.find_first_of("*+?{");
712 while (pos != std::string::npos)
713 {
714 if (pos == 0 || pattern.substr(pos - 1, 1) == "|")
715 {
716 std::cerr
717 << "A repetition character (*, ?, +, or {) was not "
718 "preceded by a valid regular expression\n";
719 exit(1);
720 }
721 pos = pattern.find_first_of("*+?{", pos + 1);
722 }
723 std::cerr << "Mismatched parentheses (( or ))\n";
724 }
725 else if (e.code() == std::regex_constants::error_brace)
726 std::cerr << "Mismatched brace ({ or })\n";
727 else if (e.code() == std::regex_constants::error_badbrace)
728 std::cerr << "Invalid range inside a { }\n";
729 else if (e.code() == std::regex_constants::error_range)
730 std::cerr << "Invalid character range (e.g., [z-a])\n";
731 else if (e.code() == std::regex_constants::error_space)
732 std::cerr << "Insufficient memory to handle regular expression\n";
733 else if (e.code() == std::regex_constants::error_badrepeat)
734 std::cerr << "A repetition character (*, ?, +, or {) was not "
735 "preceded by a valid regular expression\n";
736 else if (e.code() == std::regex_constants::error_complexity)
737 std::cerr << "The requested match is too complex\n";
738 else if (e.code() == std::regex_constants::error_stack)
739 std::cerr << "Insufficient memory to evaluate a match\n";
740 exit(1);
741 }
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800742}
743
Aatir186ce8c2019-10-20 15:13:39 -0500744static void exitWithError(const std::string& help, const char* err)
745{
746 std::cerr << "ERROR: " << err << std::endl << help << std::endl;
747 exit(-1);
748}
749
750int main(int argc, char** argv)
751{
752 CLI::App app{"OpenBMC PEL Tool"};
753 std::string fileName;
Aatire340c132019-12-09 14:19:29 -0600754 std::string idPEL;
Matt Spinler27de6f32020-02-21 08:35:57 -0600755 std::string idToDelete;
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800756 std::string scrubFile;
757 std::optional<std::regex> scrubRegex;
Aatire340c132019-12-09 14:19:29 -0600758 bool listPEL = false;
759 bool listPELDescOrd = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800760 bool hidden = false;
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800761 bool includeInfo = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600762 bool deleteAll = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800763 bool showPELCount = false;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800764 bool fullPEL = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600765
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800766 app.set_help_flag("--help", "Print this help message and exit");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800767 app.add_option("--file", fileName, "Display a PEL using its Raw PEL file");
Aatire340c132019-12-09 14:19:29 -0600768 app.add_option("-i, --id", idPEL, "Display a PEL based on its ID");
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800769 app.add_flag("-a", fullPEL, "Display all PELs");
Aatirbad5f8a2019-12-10 15:27:16 -0600770 app.add_flag("-l", listPEL, "List PELs");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800771 app.add_flag("-n", showPELCount, "Show number of PELs");
Aatir7b291ec2019-11-19 10:37:37 -0600772 app.add_flag("-r", listPELDescOrd, "Reverse order of output");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800773 app.add_flag("-h", hidden, "Include hidden PELs");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800774 app.add_flag("-f,--info", includeInfo, "Include informational PELs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600775 app.add_option("-d, --delete", idToDelete, "Delete a PEL based on its ID");
776 app.add_flag("-D, --delete-all", deleteAll, "Delete all PELs");
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800777 app.add_option("-s, --scrub", scrubFile,
778 "File containing SRC regular expressions to ignore");
Matt Spinler27de6f32020-02-21 08:35:57 -0600779
Aatir186ce8c2019-10-20 15:13:39 -0500780 CLI11_PARSE(app, argc, argv);
781
782 if (!fileName.empty())
783 {
784 std::vector<uint8_t> data = getFileData(fileName);
785 if (!data.empty())
786 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800787 auto plugins = getPlugins();
Aatir186ce8c2019-10-20 15:13:39 -0500788 PEL pel{data};
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800789 pel.toJSON(registry, plugins);
Aatir186ce8c2019-10-20 15:13:39 -0500790 }
791 else
792 {
793 exitWithError(app.help("", CLI::AppFormatMode::All),
794 "Raw PEL file can't be read.");
795 }
796 }
Aatire340c132019-12-09 14:19:29 -0600797 else if (!idPEL.empty())
798 {
Matt Spinler1b420bc2020-02-21 08:54:25 -0600799 callFunctionOnPEL(idPEL, displayPEL);
Aatire340c132019-12-09 14:19:29 -0600800 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800801 else if (fullPEL || listPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600802 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800803 if (!scrubFile.empty())
804 {
805 scrubRegex = genRegex(scrubFile);
806 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800807 printPELs(listPELDescOrd, hidden, includeInfo, fullPEL, scrubRegex);
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800808 }
809 else if (showPELCount)
810 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800811 if (!scrubFile.empty())
812 {
813 scrubRegex = genRegex(scrubFile);
814 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800815 printPELCount(hidden, includeInfo, scrubRegex);
Aatir7b291ec2019-11-19 10:37:37 -0600816 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600817 else if (!idToDelete.empty())
818 {
819 callFunctionOnPEL(idToDelete, deletePEL);
820 }
821 else if (deleteAll)
822 {
823 deleteAllPELs();
824 }
Aatir186ce8c2019-10-20 15:13:39 -0500825 else
826 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800827 std::cout << app.help("", CLI::AppFormatMode::All) << std::endl;
Aatir186ce8c2019-10-20 15:13:39 -0500828 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800829 Py_Finalize();
Aatir186ce8c2019-10-20 15:13:39 -0500830 return 0;
831}