blob: afbbaee34298f404c2570eb5ad43e631d3d2bd39 [file] [log] [blame]
Jason M. Bills5e049d32018-10-19 12:59:38 -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#include <systemd/sd-journal.h>
17
Jason M. Billsc4a336f2019-04-23 10:43:10 -070018#include <boost/algorithm/string.hpp>
Ed Tanous90926992020-09-11 12:59:33 -070019#include <boost/asio/io_service.hpp>
Jason M. Bills5e049d32018-10-19 12:59:38 -070020#include <boost/container/flat_map.hpp>
21#include <boost/container/flat_set.hpp>
Zhikui Ren672bdfc2020-07-14 11:37:01 -070022#include <pulse_event_monitor.hpp>
23#include <sdbusplus/asio/object_server.hpp>
24#include <sel_logger.hpp>
25#include <threshold_event_monitor.hpp>
Charles Hsudbd77b92020-10-29 11:20:34 +080026#include <watchdog_event_monitor.hpp>
George Hung486e42e2021-04-14 20:20:42 +080027#ifdef SEL_LOGGER_MONITOR_THRESHOLD_ALARM_EVENTS
28#include <threshold_alarm_event_monitor.hpp>
29#endif
Zhikui Ren672bdfc2020-07-14 11:37:01 -070030
Jason M. Billsc4a336f2019-04-23 10:43:10 -070031#include <filesystem>
32#include <fstream>
Jason M. Bills5e049d32018-10-19 12:59:38 -070033#include <iomanip>
34#include <iostream>
Jason M. Bills5e049d32018-10-19 12:59:38 -070035#include <sstream>
Jason M. Bills5e049d32018-10-19 12:59:38 -070036
Lei YUe526b862020-12-03 15:41:59 +080037#ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
38#include <phosphor-logging/elog-errors.hpp>
39#include <phosphor-logging/elog.hpp>
40#include <phosphor-logging/log.hpp>
41#include <xyz/openbmc_project/Logging/SEL/error.hpp>
42
43using namespace phosphor::logging;
44using SELCreated =
45 sdbusplus::xyz::openbmc_project::Logging::SEL::Error::Created;
46#endif
47
Jason M. Bills5e049d32018-10-19 12:59:38 -070048struct DBusInternalError final : public sdbusplus::exception_t
49{
Zhikui Ren672bdfc2020-07-14 11:37:01 -070050 const char* name() const noexcept override
Jason M. Bills5e049d32018-10-19 12:59:38 -070051 {
52 return "org.freedesktop.DBus.Error.Failed";
53 };
Zhikui Ren672bdfc2020-07-14 11:37:01 -070054 const char* description() const noexcept override
Jason M. Bills5e049d32018-10-19 12:59:38 -070055 {
56 return "internal error";
57 };
Zhikui Ren672bdfc2020-07-14 11:37:01 -070058 const char* what() const noexcept override
Jason M. Bills5e049d32018-10-19 12:59:38 -070059 {
60 return "org.freedesktop.DBus.Error.Failed: "
61 "internal error";
62 };
63};
64
Lei YUe526b862020-12-03 15:41:59 +080065#ifndef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
Zhikui Ren672bdfc2020-07-14 11:37:01 -070066static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc4a336f2019-04-23 10:43:10 -070067{
68 // Loop through the directory looking for ipmi_sel log files
Zhikui Ren672bdfc2020-07-14 11:37:01 -070069 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Billsc4a336f2019-04-23 10:43:10 -070070 std::filesystem::directory_iterator(selLogDir))
71 {
72 std::string filename = dirEnt.path().filename();
73 if (boost::starts_with(filename, selLogFilename))
74 {
75 // If we find an ipmi_sel log file, save the path
76 selLogFiles.emplace_back(selLogDir / filename);
77 }
78 }
79 // As the log files rotate, they are appended with a ".#" that is higher for
80 // the older logs. Since we don't expect more than 10 log files, we
81 // can just sort the list to get them in order from newest to oldest
82 std::sort(selLogFiles.begin(), selLogFiles.end());
83
84 return !selLogFiles.empty();
85}
86
Jason M. Bills5e049d32018-10-19 12:59:38 -070087static unsigned int initializeRecordId(void)
88{
Jason M. Billsc4a336f2019-04-23 10:43:10 -070089 std::vector<std::filesystem::path> selLogFiles;
90 if (!getSELLogFiles(selLogFiles))
Jason M. Bills5e049d32018-10-19 12:59:38 -070091 {
Jason M. Billsc4a336f2019-04-23 10:43:10 -070092 return selInvalidRecID;
Jason M. Bills5e049d32018-10-19 12:59:38 -070093 }
Jason M. Billsc4a336f2019-04-23 10:43:10 -070094 std::ifstream logStream(selLogFiles.front());
95 if (!logStream.is_open())
Jason M. Bills5e049d32018-10-19 12:59:38 -070096 {
Jason M. Billsc4a336f2019-04-23 10:43:10 -070097 return selInvalidRecID;
98 }
99 std::string line;
100 std::string newestEntry;
101 while (std::getline(logStream, line))
102 {
103 newestEntry = line;
104 }
Jason M. Bills5e049d32018-10-19 12:59:38 -0700105
Jason M. Billsc4a336f2019-04-23 10:43:10 -0700106 std::vector<std::string> newestEntryFields;
107 boost::split(newestEntryFields, newestEntry, boost::is_any_of(" ,"),
108 boost::token_compress_on);
109 if (newestEntryFields.size() < 4)
110 {
111 return selInvalidRecID;
Jason M. Bills5e049d32018-10-19 12:59:38 -0700112 }
Jason M. Billsc4a336f2019-04-23 10:43:10 -0700113
114 return std::stoul(newestEntryFields[1]);
Jason M. Bills5e049d32018-10-19 12:59:38 -0700115}
116
Charles Boyer9f476e82021-07-29 16:33:01 -0500117#ifdef SEL_LOGGER_CLEARS_SEL
118static unsigned int recordId = initializeRecordId();
119
120void clearSelLogFiles()
121{
122 // Clear the SEL by deleting the log files
123 std::vector<std::filesystem::path> selLogFiles;
124 if (getSELLogFiles(selLogFiles))
125 {
126 for (const std::filesystem::path& file : selLogFiles)
127 {
128 std::error_code ec;
129 std::filesystem::remove(file, ec);
130 }
131 }
132
133 recordId = selInvalidRecID;
134
135 // Reload rsyslog so it knows to start new log files
136 boost::asio::io_service io;
137 auto dbus = std::make_shared<sdbusplus::asio::connection>(io);
138 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
139 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
140 "org.freedesktop.systemd1.Manager", "ReloadUnit");
141 rsyslogReload.append("rsyslog.service", "replace");
142 try
143 {
144 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
145 }
146 catch (sdbusplus::exception_t& e)
147 {
148 std::cerr << e.what() << "\n";
149 }
150}
151#endif
152
Jason M. Bills5e049d32018-10-19 12:59:38 -0700153static unsigned int getNewRecordId(void)
154{
Charles Boyer9f476e82021-07-29 16:33:01 -0500155#ifndef SEL_LOGGER_CLEARS_SEL
Jason M. Bills5e049d32018-10-19 12:59:38 -0700156 static unsigned int recordId = initializeRecordId();
157
Jason M. Bills6afe9562019-08-29 16:33:55 -0700158 // If the log has been cleared, also clear the current ID
159 std::vector<std::filesystem::path> selLogFiles;
160 if (!getSELLogFiles(selLogFiles))
161 {
162 recordId = selInvalidRecID;
163 }
Charles Boyer9f476e82021-07-29 16:33:01 -0500164#endif
Jason M. Bills6afe9562019-08-29 16:33:55 -0700165
Jason M. Bills5e049d32018-10-19 12:59:38 -0700166 if (++recordId >= selInvalidRecID)
167 {
168 recordId = 1;
169 }
170 return recordId;
171}
Lei YUe526b862020-12-03 15:41:59 +0800172#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700173
Zhikui Ren672bdfc2020-07-14 11:37:01 -0700174static void toHexStr(const std::vector<uint8_t>& data, std::string& hexStr)
Jason M. Bills5e049d32018-10-19 12:59:38 -0700175{
176 std::stringstream stream;
177 stream << std::hex << std::uppercase << std::setfill('0');
William A. Kennington III2e437262021-07-30 12:04:19 -0700178 for (int v : data)
Jason M. Bills5e049d32018-10-19 12:59:38 -0700179 {
180 stream << std::setw(2) << v;
181 }
182 hexStr = stream.str();
183}
184
Jason M. Bills97be3532018-11-02 13:09:16 -0700185template <typename... T>
William A. Kennington IIId585c592021-07-30 12:10:25 -0700186static uint16_t selAddSystemRecord([[maybe_unused]] const std::string& message,
187 const std::string& path,
188 const std::vector<uint8_t>& selData,
189 const bool& assert, const uint16_t& genId,
190 [[maybe_unused]] T&&... metadata)
Jason M. Bills5e049d32018-10-19 12:59:38 -0700191{
192 // Only 3 bytes of SEL event data are allowed in a system record
193 if (selData.size() > selEvtDataMaxSize)
194 {
195 throw std::invalid_argument("Event data too large");
196 }
197 std::string selDataStr;
198 toHexStr(selData, selDataStr);
199
Lei YUe526b862020-12-03 15:41:59 +0800200#ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
201 using namespace xyz::openbmc_project::Logging::SEL;
Lei YU7d8a3002021-01-21 17:16:19 +0800202 auto entryID = report<SELCreated>(
Lei YUe526b862020-12-03 15:41:59 +0800203 Created::RECORD_TYPE(selSystemType), Created::GENERATOR_ID(genId),
204 Created::SENSOR_DATA(selDataStr.c_str()), Created::EVENT_DIR(assert),
205 Created::SENSOR_PATH(path.c_str()));
Lei YU7d8a3002021-01-21 17:16:19 +0800206 return static_cast<uint16_t>(entryID);
Lei YUe526b862020-12-03 15:41:59 +0800207#else
Jason M. Bills5e049d32018-10-19 12:59:38 -0700208 unsigned int recordId = getNewRecordId();
Jason M. Bills97be3532018-11-02 13:09:16 -0700209 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", selPriority,
210 "MESSAGE_ID=%s", selMessageId, "IPMI_SEL_RECORD_ID=%d",
211 recordId, "IPMI_SEL_RECORD_TYPE=%x", selSystemType,
212 "IPMI_SEL_GENERATOR_ID=%x", genId,
213 "IPMI_SEL_SENSOR_PATH=%s", path.c_str(),
214 "IPMI_SEL_EVENT_DIR=%x", assert, "IPMI_SEL_DATA=%s",
215 selDataStr.c_str(), std::forward<T>(metadata)..., NULL);
Jason M. Bills5e049d32018-10-19 12:59:38 -0700216 return recordId;
Lei YUe526b862020-12-03 15:41:59 +0800217#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700218}
219
William A. Kennington IIId585c592021-07-30 12:10:25 -0700220static uint16_t selAddOemRecord([[maybe_unused]] const std::string& message,
Zhikui Ren672bdfc2020-07-14 11:37:01 -0700221 const std::vector<uint8_t>& selData,
222 const uint8_t& recordType)
Jason M. Bills5e049d32018-10-19 12:59:38 -0700223{
224 // A maximum of 13 bytes of SEL event data are allowed in an OEM record
225 if (selData.size() > selOemDataMaxSize)
226 {
227 throw std::invalid_argument("Event data too large");
228 }
229 std::string selDataStr;
230 toHexStr(selData, selDataStr);
231
Lei YUe526b862020-12-03 15:41:59 +0800232#ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
233 using namespace xyz::openbmc_project::Logging::SEL;
Lei YU7d8a3002021-01-21 17:16:19 +0800234 auto entryID = report<SELCreated>(
235 Created::RECORD_TYPE(recordType), Created::GENERATOR_ID(0),
236 Created::SENSOR_DATA(selDataStr.c_str()), Created::EVENT_DIR(0),
237 Created::SENSOR_PATH(""));
238 return static_cast<uint16_t>(entryID);
Lei YUe526b862020-12-03 15:41:59 +0800239#else
Jason M. Bills5e049d32018-10-19 12:59:38 -0700240 unsigned int recordId = getNewRecordId();
241 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", selPriority,
242 "MESSAGE_ID=%s", selMessageId, "IPMI_SEL_RECORD_ID=%d",
243 recordId, "IPMI_SEL_RECORD_TYPE=%x", recordType,
244 "IPMI_SEL_DATA=%s", selDataStr.c_str(), NULL);
245 return recordId;
Lei YUe526b862020-12-03 15:41:59 +0800246#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700247}
248
William A. Kennington IIId585c592021-07-30 12:10:25 -0700249int main(int, char*[])
Jason M. Bills5e049d32018-10-19 12:59:38 -0700250{
251 // setup connection to dbus
252 boost::asio::io_service io;
253 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
254
255 // IPMI SEL Object
256 conn->request_name(ipmiSelObject);
257 auto server = sdbusplus::asio::object_server(conn);
258
259 // Add SEL Interface
260 std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceAddSel =
261 server.add_interface(ipmiSelPath, ipmiSelAddInterface);
262
263 // Add a new SEL entry
264 ifaceAddSel->register_method(
Zhikui Ren672bdfc2020-07-14 11:37:01 -0700265 "IpmiSelAdd", [](const std::string& message, const std::string& path,
266 const std::vector<uint8_t>& selData,
267 const bool& assert, const uint16_t& genId) {
Jason M. Bills5e049d32018-10-19 12:59:38 -0700268 return selAddSystemRecord(message, path, selData, assert, genId);
269 });
270 // Add a new OEM SEL entry
271 ifaceAddSel->register_method(
272 "IpmiSelAddOem",
Zhikui Ren672bdfc2020-07-14 11:37:01 -0700273 [](const std::string& message, const std::vector<uint8_t>& selData,
274 const uint8_t& recordType) {
Jason M. Bills5e049d32018-10-19 12:59:38 -0700275 return selAddOemRecord(message, selData, recordType);
276 });
Charles Boyer9f476e82021-07-29 16:33:01 -0500277
278#ifdef SEL_LOGGER_CLEARS_SEL
279#ifndef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
280 // Clear SEL entries
281 ifaceAddSel->register_method("Clear", []() { clearSelLogFiles(); });
282#endif
283#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700284 ifaceAddSel->initialize();
285
286#ifdef SEL_LOGGER_MONITOR_THRESHOLD_EVENTS
Zhikui Ren25b26e12020-06-26 20:18:19 -0700287 sdbusplus::bus::match::match thresholdAssertMonitor =
288 startThresholdAssertMonitor(conn);
Jason M. Bills5e049d32018-10-19 12:59:38 -0700289#endif
290
Nikhil Potadeafbaa092019-03-06 16:18:13 -0800291#ifdef REDFISH_LOG_MONITOR_PULSE_EVENTS
292 sdbusplus::bus::match::match pulseEventMonitor =
293 startPulseEventMonitor(conn);
294#endif
295
Charles Hsudbd77b92020-10-29 11:20:34 +0800296#ifdef SEL_LOGGER_MONITOR_WATCHDOG_EVENTS
297 sdbusplus::bus::match::match watchdogEventMonitor =
298 startWatchdogEventMonitor(conn);
299#endif
George Hung486e42e2021-04-14 20:20:42 +0800300
301#ifdef SEL_LOGGER_MONITOR_THRESHOLD_ALARM_EVENTS
302 startThresholdAlarmMonitor(conn);
303#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700304 io.run();
305
306 return 0;
307}