blob: 772cb84e61521511c2d4589118e8d67a6e08ba22 [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;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800222 std::array<std::string, 2> parserDirs = {"udparsers", "srcparsers"};
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800223 PyObject* pName = PyUnicode_FromString("sys");
224 PyObject* pModule = PyImport_Import(pName);
225 Py_XDECREF(pName);
226 PyObject* pDict = PyModule_GetDict(pModule);
227 Py_XDECREF(pModule);
228 PyObject* pResult = PyDict_GetItemString(pDict, "path");
229 PyObject* pValue = PyUnicode_FromString(".");
230 PyList_Append(pResult, pValue);
231 Py_XDECREF(pValue);
232 auto list_size = PyList_Size(pResult);
233 for (auto i = 0; i < list_size; i++)
234 {
235 PyObject* item = PyList_GetItem(pResult, i);
236 PyObject* pBytes = PyUnicode_AsEncodedString(item, "utf-8", "~E~");
237 const char* output = PyBytes_AS_STRING(pBytes);
238 Py_XDECREF(pBytes);
239 std::string tmpStr(output);
240 siteDirs.push_back(tmpStr);
241 }
242 for (const auto& dir : siteDirs)
243 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800244 for (const auto& parserDir : parserDirs)
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800245 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800246 if (fs::exists(dir + "/" + parserDir))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800247 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800248 for (const auto& entry :
249 fs::directory_iterator(dir + "/" + parserDir))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800250 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800251 if (entry.is_directory() and
252 fs::exists(entry.path().string() + "/" +
253 entry.path().stem().string() + ".py"))
254 {
255 plugins.push_back(entry.path().stem());
256 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800257 }
258 }
259 }
260 }
261 return plugins;
262}
263
264/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800265 * @brief Creates JSON string of a PEL entry if fullPEL is false or prints to
266 * stdout the full PEL in JSON if fullPEL is true
267 * @param[in] itr - std::map iterator of <uint32_t, BCDTime>
268 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800269 * @param[in] includeInfo - Boolean to include informational PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800270 * @param[in] fullPEL - Boolean to print full JSON representation of PEL
271 * @param[in] foundPEL - Boolean to check if any PEL is present
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800272 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800273 * @param[in] plugins - Vector of strings of plugins found in filesystem
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800274 * @return std::string - JSON string of PEL entry (empty if fullPEL is true)
275 */
Aatir7b291ec2019-11-19 10:37:37 -0600276template <typename T>
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800277std::string genPELJSON(T itr, bool hidden, bool includeInfo, bool fullPEL,
278 bool& foundPEL,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800279 const std::optional<std::regex>& scrubRegex,
280 const std::vector<std::string>& plugins)
Aatir7b291ec2019-11-19 10:37:37 -0600281{
282 std::size_t found;
283 std::string val;
284 char tmpValStr[50];
285 std::string listStr;
286 char name[50];
287 sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", itr.second.yearMSB,
288 itr.second.yearLSB, itr.second.month, itr.second.day,
289 itr.second.hour, itr.second.minutes, itr.second.seconds,
290 itr.second.hundredths, itr.first);
291 std::string fileName(name);
292 fileName = EXTENSION_PERSIST_DIR "/pels/logs/" + fileName;
293 try
294 {
Aatir37822f62019-12-10 14:40:27 -0600295 std::vector<uint8_t> data = getFileData(fileName);
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800296 if (data.empty())
Aatir37822f62019-12-10 14:40:27 -0600297 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800298 log<level::ERR>("Empty PEL file",
299 entry("FILENAME=%s", fileName.c_str()));
300 return listStr;
301 }
302 PEL pel{data};
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800303 if (!pel.valid())
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800304 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800305 return listStr;
306 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800307 if (!includeInfo && pel.userHeader().severity() == 0)
308 {
309 return listStr;
310 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800311 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
312 if (!hidden && actionFlags.test(hiddenFlagBit))
313 {
314 return listStr;
315 }
316 if (pel.primarySRC() && scrubRegex)
317 {
318 val = pel.primarySRC().value()->asciiString();
319 if (std::regex_search(trimEnd(val), scrubRegex.value(),
320 std::regex_constants::match_not_null))
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800321 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800322 return listStr;
323 }
324 }
325 if (fullPEL)
326 {
327 if (!foundPEL)
328 {
329 std::cout << "[\n";
330 foundPEL = true;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800331 }
332 else
Aatir7b291ec2019-11-19 10:37:37 -0600333 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800334 std::cout << ",\n\n";
335 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800336 pel.toJSON(registry, plugins);
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800337 }
338 else
339 {
340 // id
341 listStr += "\t\"" +
342 getNumberString("0x%X", pel.privateHeader().id()) +
343 "\": {\n";
344 // ASCII
345 if (pel.primarySRC())
346 {
347 val = pel.primarySRC().value()->asciiString();
348 listStr += "\t\t\"SRC\": \"" + trimEnd(val) + "\",\n";
349 // Registry message
350 auto regVal = pel.primarySRC().value()->getErrorDetails(
351 registry, DetailLevel::message, true);
352 if (regVal)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800353 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800354 val = regVal.value();
355 listStr += "\t\t\"Message\": \"" + val + "\",\n";
Aatir37822f62019-12-10 14:40:27 -0600356 }
Aatir7b291ec2019-11-19 10:37:37 -0600357 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800358 else
359 {
360 listStr += "\t\t\"SRC\": \"No SRC\",\n";
361 }
362 // platformid
363 listStr += "\t\t\"PLID\": \"" +
364 getNumberString("0x%X", pel.privateHeader().plid()) +
365 "\",\n";
366 // creatorid
367 std::string creatorID =
368 getNumberString("%c", pel.privateHeader().creatorID());
369 val = pv::creatorIDs.count(creatorID) ? pv::creatorIDs.at(creatorID)
370 : "Unknown Creator ID";
371 listStr += "\t\t\"CreatorID\": \"" + val + "\",\n";
372 // subsytem
373 std::string subsystem = pv::getValue(pel.userHeader().subsystem(),
374 pel_values::subsystemValues);
375 listStr += "\t\t\"Subsystem\": \"" + subsystem + "\",\n";
376 // commit time
377 sprintf(tmpValStr, "%02X/%02X/%02X%02X %02X:%02X:%02X",
378 pel.privateHeader().commitTimestamp().month,
379 pel.privateHeader().commitTimestamp().day,
380 pel.privateHeader().commitTimestamp().yearMSB,
381 pel.privateHeader().commitTimestamp().yearLSB,
382 pel.privateHeader().commitTimestamp().hour,
383 pel.privateHeader().commitTimestamp().minutes,
384 pel.privateHeader().commitTimestamp().seconds);
385 val = std::string(tmpValStr);
386 listStr += "\t\t\"Commit Time\": \"" + val + "\",\n";
387 // severity
388 std::string severity = pv::getValue(pel.userHeader().severity(),
389 pel_values::severityValues);
390 listStr += "\t\t\"Sev\": \"" + severity + "\",\n ";
391 // compID
392 listStr += "\t\t\"CompID\": \"" +
393 getNumberString(
394 "0x%X", pel.privateHeader().header().componentID) +
395 "\",\n ";
396 found = listStr.rfind(",");
397 if (found != std::string::npos)
398 {
399 listStr.replace(found, 1, "");
400 listStr += "\t},\n";
401 }
402 foundPEL = true;
Aatir7b291ec2019-11-19 10:37:37 -0600403 }
404 }
405 catch (std::exception& e)
406 {
407 log<level::ERR>("Hit exception while reading PEL File",
408 entry("FILENAME=%s", fileName.c_str()),
409 entry("ERROR=%s", e.what()));
410 }
411 return listStr;
412}
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800413
Aatir7b291ec2019-11-19 10:37:37 -0600414/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800415 * @brief Print a list of PELs or a JSON array of PELs
416 * @param[in] order - Boolean to print in reverse orser
417 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800418 * @param[in] includeInfo - Boolean to include informational PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800419 * @param[in] fullPEL - Boolean to print full PEL into a JSON array
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800420 * @param[in] scrubRegex - SRC regex object
Aatir7b291ec2019-11-19 10:37:37 -0600421 */
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800422void printPELs(bool order, bool hidden, bool includeInfo, bool fullPEL,
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800423 const std::optional<std::regex>& scrubRegex)
Aatir7b291ec2019-11-19 10:37:37 -0600424{
425 std::string listStr;
426 std::map<uint32_t, BCDTime> PELs;
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800427 std::vector<std::string> plugins;
Aatir7b291ec2019-11-19 10:37:37 -0600428 listStr = "{\n";
429 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
430 it != fs::directory_iterator(); ++it)
431 {
432 if (!fs::is_regular_file((*it).path()))
433 {
434 continue;
435 }
Aatir37822f62019-12-10 14:40:27 -0600436 else
Aatir7b291ec2019-11-19 10:37:37 -0600437 {
Aatir37822f62019-12-10 14:40:27 -0600438 PELs.emplace(fileNameToPELId((*it).path().filename()),
439 fileNameToTimestamp((*it).path().filename()));
Aatir7b291ec2019-11-19 10:37:37 -0600440 }
441 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800442 bool foundPEL = false;
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800443 if (fullPEL)
444 {
445 plugins = getPlugins();
446 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800447 auto buildJSON = [&listStr, &hidden, &includeInfo, &fullPEL, &foundPEL,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800448 &scrubRegex, &plugins](const auto& i) {
449 listStr += genPELJSON(i, hidden, includeInfo, fullPEL, foundPEL,
450 scrubRegex, plugins);
Aatir37822f62019-12-10 14:40:27 -0600451 };
Aatir7b291ec2019-11-19 10:37:37 -0600452 if (order)
453 {
454 std::for_each(PELs.rbegin(), PELs.rend(), buildJSON);
455 }
456 else
457 {
458 std::for_each(PELs.begin(), PELs.end(), buildJSON);
459 }
460
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800461 if (foundPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600462 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800463 if (fullPEL)
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800464 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800465 std::cout << "]" << std::endl;
466 }
467 else
468 {
469 std::size_t found;
470 found = listStr.rfind(",");
471 if (found != std::string::npos)
472 {
473 listStr.replace(found, 1, "");
474 listStr += "}\n";
475 printf("%s", listStr.c_str());
476 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800477 }
478 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800479 else
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800480 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800481 std::string emptyJSON = fullPEL ? "[]" : "{}";
482 std::cout << emptyJSON << std::endl;
Aatir7b291ec2019-11-19 10:37:37 -0600483 }
484}
Aatir186ce8c2019-10-20 15:13:39 -0500485
Matt Spinler27de6f32020-02-21 08:35:57 -0600486/**
487 * @brief Calls the function passed in on the PEL with the ID
488 * passed in.
489 *
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800490 * @param[in] id - The string version of the PEL or BMC Log ID, either with or
Matt Spinler27de6f32020-02-21 08:35:57 -0600491 * without the 0x prefix.
492 * @param[in] func - The std::function<void(const PEL&)> function to run.
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800493 * @param[in] useBMC - if true, search by BMC Log ID, else search by PEL ID
Matt Spinler27de6f32020-02-21 08:35:57 -0600494 */
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800495void callFunctionOnPEL(const std::string& id, const PELFunc& func, bool useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600496{
497 std::string pelID{id};
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800498 if (!useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600499 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800500 std::transform(pelID.begin(), pelID.end(), pelID.begin(), toupper);
501
502 if (pelID.find("0X") == 0)
503 {
504 pelID.erase(0, 2);
505 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600506 }
507
508 bool found = false;
509
510 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
511 it != fs::directory_iterator(); ++it)
512 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800513 // The PEL ID is part of the filename, so use that to find the PEL if
514 // "useBMC" is set to false, otherwise we have to search within the PEL
Matt Spinler27de6f32020-02-21 08:35:57 -0600515
516 if (!fs::is_regular_file((*it).path()))
517 {
518 continue;
519 }
520
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800521 if ((ends_with((*it).path(), pelID) && !useBMC) || useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600522 {
Matt Spinler27de6f32020-02-21 08:35:57 -0600523 auto data = getFileData((*it).path());
524 if (!data.empty())
525 {
526 PEL pel{data};
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800527 if (!useBMC ||
528 (useBMC && pel.obmcLogID() == std::stoul(id, nullptr, 0)))
Matt Spinler27de6f32020-02-21 08:35:57 -0600529 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800530 found = true;
531 try
532 {
533 func(pel);
534 break;
535 }
536 catch (std::exception& e)
537 {
538 std::cerr << " Internal function threw an exception: "
539 << e.what() << "\n";
540 exit(1);
541 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600542 }
543 }
544 else
545 {
546 std::cerr << "Could not read PEL file\n";
547 exit(1);
548 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600549 }
550 }
551
552 if (!found)
553 {
554 std::cerr << "PEL not found\n";
555 exit(1);
556 }
557}
558
559/**
560 * @brief Delete a PEL by deleting its corresponding event log.
561 *
562 * @param[in] pel - The PEL to delete
563 */
564void deletePEL(const PEL& pel)
565{
566 std::string path{object_path::logEntry};
567 path += std::to_string(pel.obmcLogID());
568
569 try
570 {
571 auto bus = sdbusplus::bus::new_default();
572 auto method = bus.new_method_call(service::logging, path.c_str(),
573 interface::deleteObj, "Delete");
574 auto reply = bus.call(method);
575 }
576 catch (const SdBusError& e)
577 {
578 std::cerr << "D-Bus call to delete event log " << pel.obmcLogID()
579 << " failed: " << e.what() << "\n";
580 exit(1);
581 }
582}
583
584/**
585 * @brief Delete all PELs by deleting all event logs.
586 */
587void deleteAllPELs()
588{
589 try
590 {
591 // This may move to an audit log some day
592 log<level::INFO>("peltool deleting all event logs");
593
594 auto bus = sdbusplus::bus::new_default();
595 auto method =
596 bus.new_method_call(service::logging, object_path::logging,
597 interface::deleteAll, "DeleteAll");
598 auto reply = bus.call(method);
599 }
600 catch (const SdBusError& e)
601 {
602 std::cerr << "D-Bus call to delete all event logs failed: " << e.what()
603 << "\n";
604 exit(1);
605 }
606}
607
Matt Spinler1b420bc2020-02-21 08:54:25 -0600608/**
609 * @brief Display a single PEL
610 *
611 * @param[in] pel - the PEL to display
612 */
613void displayPEL(const PEL& pel)
614{
615 if (pel.valid())
616 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800617 auto plugins = getPlugins();
618 pel.toJSON(registry, plugins);
Matt Spinler1b420bc2020-02-21 08:54:25 -0600619 }
620 else
621 {
622 std::cerr << "PEL was malformed\n";
623 exit(1);
624 }
625}
626
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800627/**
628 * @brief Print number of PELs
629 * @param[in] hidden - Bool to include hidden logs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800630 * @param[in] includeInfo - Bool to include informational logs
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800631 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800632 */
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800633void printPELCount(bool hidden, bool includeInfo,
634 const std::optional<std::regex>& scrubRegex)
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800635{
636 std::size_t count = 0;
637 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
638 it != fs::directory_iterator(); ++it)
639 {
640 if (!fs::is_regular_file((*it).path()))
641 {
642 continue;
643 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800644 std::vector<uint8_t> data = getFileData((*it).path());
645 if (data.empty())
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800646 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800647 continue;
648 }
649 PEL pel{data};
650 if (!pel.valid())
651 {
652 continue;
653 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800654 if (!includeInfo && pel.userHeader().severity() == 0)
655 {
656 continue;
657 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800658 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
659 if (!hidden && actionFlags.test(hiddenFlagBit))
660 {
661 continue;
662 }
663 if (pel.primarySRC() && scrubRegex)
664 {
665 std::string val = pel.primarySRC().value()->asciiString();
666 if (std::regex_search(trimEnd(val), scrubRegex.value(),
667 std::regex_constants::match_not_null))
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800668 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800669 continue;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800670 }
671 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800672 count++;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800673 }
674 std::cout << "{\n"
675 << " \"Number of PELs found\": "
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800676 << getNumberString("%d", count) << "\n}\n";
677}
678
679/**
680 * @brief Generate regex pattern object from file contents
681 * @param[in] scrubFile - File containing regex pattern
682 * @return std::regex - SRC regex object
683 */
684std::regex genRegex(std::string& scrubFile)
685{
686 std::string pattern;
687 std::ifstream contents(scrubFile);
688 if (contents.fail())
689 {
690 std::cerr << "Can't open \"" << scrubFile << "\"\n";
691 exit(1);
692 }
693 std::string line;
694 while (std::getline(contents, line))
695 {
696 if (!line.empty())
697 {
698 pattern.append(line + "|");
699 }
700 }
701 try
702 {
703 std::regex scrubRegex(pattern, std::regex::icase);
704 return scrubRegex;
705 }
706 catch (std::regex_error& e)
707 {
708 if (e.code() == std::regex_constants::error_collate)
709 std::cerr << "Invalid collating element request\n";
710 else if (e.code() == std::regex_constants::error_ctype)
711 std::cerr << "Invalid character class\n";
712 else if (e.code() == std::regex_constants::error_escape)
713 std::cerr << "Invalid escape character or trailing escape\n";
714 else if (e.code() == std::regex_constants::error_backref)
715 std::cerr << "Invalid back reference\n";
716 else if (e.code() == std::regex_constants::error_brack)
717 std::cerr << "Mismatched bracket ([ or ])\n";
718 else if (e.code() == std::regex_constants::error_paren)
719 {
720 // to catch return code error_badrepeat when error_paren is retured
721 // instead
722 size_t pos = pattern.find_first_of("*+?{");
723 while (pos != std::string::npos)
724 {
725 if (pos == 0 || pattern.substr(pos - 1, 1) == "|")
726 {
727 std::cerr
728 << "A repetition character (*, ?, +, or {) was not "
729 "preceded by a valid regular expression\n";
730 exit(1);
731 }
732 pos = pattern.find_first_of("*+?{", pos + 1);
733 }
734 std::cerr << "Mismatched parentheses (( or ))\n";
735 }
736 else if (e.code() == std::regex_constants::error_brace)
737 std::cerr << "Mismatched brace ({ or })\n";
738 else if (e.code() == std::regex_constants::error_badbrace)
739 std::cerr << "Invalid range inside a { }\n";
740 else if (e.code() == std::regex_constants::error_range)
741 std::cerr << "Invalid character range (e.g., [z-a])\n";
742 else if (e.code() == std::regex_constants::error_space)
743 std::cerr << "Insufficient memory to handle regular expression\n";
744 else if (e.code() == std::regex_constants::error_badrepeat)
745 std::cerr << "A repetition character (*, ?, +, or {) was not "
746 "preceded by a valid regular expression\n";
747 else if (e.code() == std::regex_constants::error_complexity)
748 std::cerr << "The requested match is too complex\n";
749 else if (e.code() == std::regex_constants::error_stack)
750 std::cerr << "Insufficient memory to evaluate a match\n";
751 exit(1);
752 }
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800753}
754
Aatir186ce8c2019-10-20 15:13:39 -0500755static void exitWithError(const std::string& help, const char* err)
756{
757 std::cerr << "ERROR: " << err << std::endl << help << std::endl;
758 exit(-1);
759}
760
761int main(int argc, char** argv)
762{
763 CLI::App app{"OpenBMC PEL Tool"};
764 std::string fileName;
Aatire340c132019-12-09 14:19:29 -0600765 std::string idPEL;
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800766 std::string bmcId;
Matt Spinler27de6f32020-02-21 08:35:57 -0600767 std::string idToDelete;
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800768 std::string scrubFile;
769 std::optional<std::regex> scrubRegex;
Aatire340c132019-12-09 14:19:29 -0600770 bool listPEL = false;
771 bool listPELDescOrd = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800772 bool hidden = false;
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800773 bool includeInfo = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600774 bool deleteAll = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800775 bool showPELCount = false;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800776 bool fullPEL = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600777
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800778 app.set_help_flag("--help", "Print this help message and exit");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800779 app.add_option("--file", fileName, "Display a PEL using its Raw PEL file");
Aatire340c132019-12-09 14:19:29 -0600780 app.add_option("-i, --id", idPEL, "Display a PEL based on its ID");
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800781 app.add_option("--bmc-id", bmcId,
782 "Display a PEL based on its BMC Event ID");
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800783 app.add_flag("-a", fullPEL, "Display all PELs");
Aatirbad5f8a2019-12-10 15:27:16 -0600784 app.add_flag("-l", listPEL, "List PELs");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800785 app.add_flag("-n", showPELCount, "Show number of PELs");
Aatir7b291ec2019-11-19 10:37:37 -0600786 app.add_flag("-r", listPELDescOrd, "Reverse order of output");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800787 app.add_flag("-h", hidden, "Include hidden PELs");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800788 app.add_flag("-f,--info", includeInfo, "Include informational PELs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600789 app.add_option("-d, --delete", idToDelete, "Delete a PEL based on its ID");
790 app.add_flag("-D, --delete-all", deleteAll, "Delete all PELs");
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800791 app.add_option("-s, --scrub", scrubFile,
792 "File containing SRC regular expressions to ignore");
Matt Spinler27de6f32020-02-21 08:35:57 -0600793
Aatir186ce8c2019-10-20 15:13:39 -0500794 CLI11_PARSE(app, argc, argv);
795
796 if (!fileName.empty())
797 {
798 std::vector<uint8_t> data = getFileData(fileName);
799 if (!data.empty())
800 {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800801 auto plugins = getPlugins();
Aatir186ce8c2019-10-20 15:13:39 -0500802 PEL pel{data};
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800803 pel.toJSON(registry, plugins);
Aatir186ce8c2019-10-20 15:13:39 -0500804 }
805 else
806 {
807 exitWithError(app.help("", CLI::AppFormatMode::All),
808 "Raw PEL file can't be read.");
809 }
810 }
Aatire340c132019-12-09 14:19:29 -0600811 else if (!idPEL.empty())
812 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800813 callFunctionOnPEL(idPEL, displayPEL, false);
814 }
815 else if (!bmcId.empty())
816 {
817 callFunctionOnPEL(bmcId, displayPEL, true);
Aatire340c132019-12-09 14:19:29 -0600818 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800819 else if (fullPEL || listPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600820 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800821 if (!scrubFile.empty())
822 {
823 scrubRegex = genRegex(scrubFile);
824 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800825 printPELs(listPELDescOrd, hidden, includeInfo, fullPEL, scrubRegex);
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800826 }
827 else if (showPELCount)
828 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800829 if (!scrubFile.empty())
830 {
831 scrubRegex = genRegex(scrubFile);
832 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800833 printPELCount(hidden, includeInfo, scrubRegex);
Aatir7b291ec2019-11-19 10:37:37 -0600834 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600835 else if (!idToDelete.empty())
836 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800837 callFunctionOnPEL(idToDelete, deletePEL, false);
Matt Spinler27de6f32020-02-21 08:35:57 -0600838 }
839 else if (deleteAll)
840 {
841 deleteAllPELs();
842 }
Aatir186ce8c2019-10-20 15:13:39 -0500843 else
844 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800845 std::cout << app.help("", CLI::AppFormatMode::All) << std::endl;
Aatir186ce8c2019-10-20 15:13:39 -0500846 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800847 Py_Finalize();
Aatir186ce8c2019-10-20 15:13:39 -0500848 return 0;
849}