blob: 98c0530821b10b462c8205ec80c33a48725d664c [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 Tanousc3526342023-03-06 13:37:53 -080019#include <boost/asio/io_context.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
JinFuLin7c2810b2022-12-02 13:55:28 +080030#ifdef SEL_LOGGER_MONITOR_HOST_ERROR_EVENTS
31#include <host_error_event_monitor.hpp>
32#endif
Zhikui Ren672bdfc2020-07-14 11:37:01 -070033
Jason M. Billsc4a336f2019-04-23 10:43:10 -070034#include <filesystem>
35#include <fstream>
Jason M. Bills5e049d32018-10-19 12:59:38 -070036#include <iomanip>
37#include <iostream>
Jason M. Bills5e049d32018-10-19 12:59:38 -070038#include <sstream>
Jason M. Bills5e049d32018-10-19 12:59:38 -070039
40struct DBusInternalError final : public sdbusplus::exception_t
41{
Zhikui Ren672bdfc2020-07-14 11:37:01 -070042 const char* name() const noexcept override
Jason M. Bills5e049d32018-10-19 12:59:38 -070043 {
44 return "org.freedesktop.DBus.Error.Failed";
Patrick Williamsa138ebd2021-09-08 15:46:34 -050045 }
Zhikui Ren672bdfc2020-07-14 11:37:01 -070046 const char* description() const noexcept override
Jason M. Bills5e049d32018-10-19 12:59:38 -070047 {
48 return "internal error";
Patrick Williamsa138ebd2021-09-08 15:46:34 -050049 }
Zhikui Ren672bdfc2020-07-14 11:37:01 -070050 const char* what() const noexcept override
Jason M. Bills5e049d32018-10-19 12:59:38 -070051 {
52 return "org.freedesktop.DBus.Error.Failed: "
53 "internal error";
Patrick Williamsa138ebd2021-09-08 15:46:34 -050054 }
55
56 int get_errno() const noexcept override
57 {
58 return EACCES;
59 }
Jason M. Bills5e049d32018-10-19 12:59:38 -070060};
61
Lei YUe526b862020-12-03 15:41:59 +080062#ifndef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
Zhikui Ren672bdfc2020-07-14 11:37:01 -070063static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc4a336f2019-04-23 10:43:10 -070064{
65 // Loop through the directory looking for ipmi_sel log files
Zhikui Ren672bdfc2020-07-14 11:37:01 -070066 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Billsc4a336f2019-04-23 10:43:10 -070067 std::filesystem::directory_iterator(selLogDir))
68 {
69 std::string filename = dirEnt.path().filename();
70 if (boost::starts_with(filename, selLogFilename))
71 {
72 // If we find an ipmi_sel log file, save the path
73 selLogFiles.emplace_back(selLogDir / filename);
74 }
75 }
76 // As the log files rotate, they are appended with a ".#" that is higher for
77 // the older logs. Since we don't expect more than 10 log files, we
78 // can just sort the list to get them in order from newest to oldest
79 std::sort(selLogFiles.begin(), selLogFiles.end());
80
81 return !selLogFiles.empty();
82}
83
Jason M. Bills5e049d32018-10-19 12:59:38 -070084static unsigned int initializeRecordId(void)
85{
Jason M. Billsc4a336f2019-04-23 10:43:10 -070086 std::vector<std::filesystem::path> selLogFiles;
87 if (!getSELLogFiles(selLogFiles))
Jason M. Bills5e049d32018-10-19 12:59:38 -070088 {
Jason M. Billsc4a336f2019-04-23 10:43:10 -070089 return selInvalidRecID;
Jason M. Bills5e049d32018-10-19 12:59:38 -070090 }
Jason M. Billsc4a336f2019-04-23 10:43:10 -070091 std::ifstream logStream(selLogFiles.front());
92 if (!logStream.is_open())
Jason M. Bills5e049d32018-10-19 12:59:38 -070093 {
Jason M. Billsc4a336f2019-04-23 10:43:10 -070094 return selInvalidRecID;
95 }
96 std::string line;
97 std::string newestEntry;
98 while (std::getline(logStream, line))
99 {
100 newestEntry = line;
101 }
Jason M. Bills5e049d32018-10-19 12:59:38 -0700102
Jason M. Billsc4a336f2019-04-23 10:43:10 -0700103 std::vector<std::string> newestEntryFields;
104 boost::split(newestEntryFields, newestEntry, boost::is_any_of(" ,"),
105 boost::token_compress_on);
106 if (newestEntryFields.size() < 4)
107 {
108 return selInvalidRecID;
Jason M. Bills5e049d32018-10-19 12:59:38 -0700109 }
Jason M. Billsc4a336f2019-04-23 10:43:10 -0700110
111 return std::stoul(newestEntryFields[1]);
Jason M. Bills5e049d32018-10-19 12:59:38 -0700112}
113
Charles Boyer9f476e82021-07-29 16:33:01 -0500114#ifdef SEL_LOGGER_CLEARS_SEL
115static unsigned int recordId = initializeRecordId();
116
117void clearSelLogFiles()
118{
119 // Clear the SEL by deleting the log files
120 std::vector<std::filesystem::path> selLogFiles;
121 if (getSELLogFiles(selLogFiles))
122 {
123 for (const std::filesystem::path& file : selLogFiles)
124 {
125 std::error_code ec;
126 std::filesystem::remove(file, ec);
127 }
128 }
129
130 recordId = selInvalidRecID;
131
132 // Reload rsyslog so it knows to start new log files
Ed Tanousc3526342023-03-06 13:37:53 -0800133 boost::asio::io_context io;
Charles Boyer9f476e82021-07-29 16:33:01 -0500134 auto dbus = std::make_shared<sdbusplus::asio::connection>(io);
Patrick Williamsccef2272022-07-22 19:26:54 -0500135 sdbusplus::message_t rsyslogReload = dbus->new_method_call(
Charles Boyer9f476e82021-07-29 16:33:01 -0500136 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
137 "org.freedesktop.systemd1.Manager", "ReloadUnit");
138 rsyslogReload.append("rsyslog.service", "replace");
139 try
140 {
Patrick Williamsccef2272022-07-22 19:26:54 -0500141 sdbusplus::message_t reloadResponse = dbus->call(rsyslogReload);
Charles Boyer9f476e82021-07-29 16:33:01 -0500142 }
Patrick Williams3f4cd972021-10-06 12:42:50 -0500143 catch (const sdbusplus::exception_t& e)
Charles Boyer9f476e82021-07-29 16:33:01 -0500144 {
145 std::cerr << e.what() << "\n";
146 }
147}
148#endif
149
Jason M. Bills5e049d32018-10-19 12:59:38 -0700150static unsigned int getNewRecordId(void)
151{
Charles Boyer9f476e82021-07-29 16:33:01 -0500152#ifndef SEL_LOGGER_CLEARS_SEL
Jason M. Bills5e049d32018-10-19 12:59:38 -0700153 static unsigned int recordId = initializeRecordId();
154
Jason M. Bills6afe9562019-08-29 16:33:55 -0700155 // If the log has been cleared, also clear the current ID
156 std::vector<std::filesystem::path> selLogFiles;
157 if (!getSELLogFiles(selLogFiles))
158 {
159 recordId = selInvalidRecID;
160 }
Charles Boyer9f476e82021-07-29 16:33:01 -0500161#endif
Jason M. Bills6afe9562019-08-29 16:33:55 -0700162
Jason M. Bills5e049d32018-10-19 12:59:38 -0700163 if (++recordId >= selInvalidRecID)
164 {
165 recordId = 1;
166 }
167 return recordId;
168}
Lei YUe526b862020-12-03 15:41:59 +0800169#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700170
Zhikui Ren672bdfc2020-07-14 11:37:01 -0700171static void toHexStr(const std::vector<uint8_t>& data, std::string& hexStr)
Jason M. Bills5e049d32018-10-19 12:59:38 -0700172{
173 std::stringstream stream;
174 stream << std::hex << std::uppercase << std::setfill('0');
William A. Kennington III2e437262021-07-30 12:04:19 -0700175 for (int v : data)
Jason M. Bills5e049d32018-10-19 12:59:38 -0700176 {
177 stream << std::setw(2) << v;
178 }
179 hexStr = stream.str();
180}
181
Jason M. Bills97be3532018-11-02 13:09:16 -0700182template <typename... T>
Konstantin Aladyshev6f5342d2023-04-19 09:23:11 +0000183static void selAddSystemRecord(
184 [[maybe_unused]] std::shared_ptr<sdbusplus::asio::connection> conn,
185 [[maybe_unused]] const std::string& message, const std::string& path,
186 const std::vector<uint8_t>& selData, const bool& assert,
187 const uint16_t& genId, [[maybe_unused]] T&&... metadata)
Jason M. Bills5e049d32018-10-19 12:59:38 -0700188{
189 // Only 3 bytes of SEL event data are allowed in a system record
190 if (selData.size() > selEvtDataMaxSize)
191 {
192 throw std::invalid_argument("Event data too large");
193 }
194 std::string selDataStr;
195 toHexStr(selData, selDataStr);
196
Lei YUe526b862020-12-03 15:41:59 +0800197#ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
Konstantin Aladyshev6f5342d2023-04-19 09:23:11 +0000198 sdbusplus::message_t AddToLog = conn->new_method_call(
199 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
200 "xyz.openbmc_project.Logging.Create", "Create");
201
202 std::string journalMsg(message + " from " + path + ": " +
203 " RecordType=" + std::to_string(selSystemType) +
204 ", GeneratorID=" + std::to_string(genId) +
205 ", EventDir=" + std::to_string(assert) +
206 ", EventData=" + selDataStr);
207
208 AddToLog.append(journalMsg,
209 "xyz.openbmc_project.Logging.Entry.Level.Informational",
210 std::map<std::string, std::string>(
211 {{"SENSOR_PATH", path},
212 {"GENERATOR_ID", std::to_string(genId)},
213 {"RECORD_TYPE", std::to_string(selSystemType)},
214 {"EVENT_DIR", std::to_string(assert)},
215 {"SENSOR_DATA", selDataStr}}));
216 conn->call(AddToLog);
Lei YUe526b862020-12-03 15:41:59 +0800217#else
Jason M. Bills5e049d32018-10-19 12:59:38 -0700218 unsigned int recordId = getNewRecordId();
Jason M. Bills97be3532018-11-02 13:09:16 -0700219 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", selPriority,
220 "MESSAGE_ID=%s", selMessageId, "IPMI_SEL_RECORD_ID=%d",
221 recordId, "IPMI_SEL_RECORD_TYPE=%x", selSystemType,
222 "IPMI_SEL_GENERATOR_ID=%x", genId,
223 "IPMI_SEL_SENSOR_PATH=%s", path.c_str(),
224 "IPMI_SEL_EVENT_DIR=%x", assert, "IPMI_SEL_DATA=%s",
225 selDataStr.c_str(), std::forward<T>(metadata)..., NULL);
Lei YUe526b862020-12-03 15:41:59 +0800226#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700227}
228
Konstantin Aladyshev6f5342d2023-04-19 09:23:11 +0000229static void selAddOemRecord(
230 [[maybe_unused]] std::shared_ptr<sdbusplus::asio::connection> conn,
231 [[maybe_unused]] const std::string& message,
232 const std::vector<uint8_t>& selData, const uint8_t& recordType)
Jason M. Bills5e049d32018-10-19 12:59:38 -0700233{
234 // A maximum of 13 bytes of SEL event data are allowed in an OEM record
235 if (selData.size() > selOemDataMaxSize)
236 {
237 throw std::invalid_argument("Event data too large");
238 }
239 std::string selDataStr;
240 toHexStr(selData, selDataStr);
241
Lei YUe526b862020-12-03 15:41:59 +0800242#ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
Konstantin Aladyshev6f5342d2023-04-19 09:23:11 +0000243 sdbusplus::message_t AddToLog = conn->new_method_call(
244 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
245 "xyz.openbmc_project.Logging.Create", "Create");
246
247 std::string journalMsg(
248 message + ": " + " RecordType=" + std::to_string(recordType) +
249 ", GeneratorID=" + std::to_string(0) +
250 ", EventDir=" + std::to_string(0) + ", EventData=" + selDataStr);
251
252 AddToLog.append(journalMsg,
253 "xyz.openbmc_project.Logging.Entry.Level.Informational",
254 std::map<std::string, std::string>(
255 {{"SENSOR_PATH", ""},
256 {"GENERATOR_ID", std::to_string(0)},
257 {"RECORD_TYPE", std::to_string(recordType)},
258 {"EVENT_DIR", std::to_string(0)},
259 {"SENSOR_DATA", selDataStr}}));
260 conn->call(AddToLog);
Lei YUe526b862020-12-03 15:41:59 +0800261#else
Jason M. Bills5e049d32018-10-19 12:59:38 -0700262 unsigned int recordId = getNewRecordId();
263 sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", selPriority,
264 "MESSAGE_ID=%s", selMessageId, "IPMI_SEL_RECORD_ID=%d",
265 recordId, "IPMI_SEL_RECORD_TYPE=%x", recordType,
266 "IPMI_SEL_DATA=%s", selDataStr.c_str(), NULL);
Lei YUe526b862020-12-03 15:41:59 +0800267#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700268}
269
William A. Kennington IIId585c592021-07-30 12:10:25 -0700270int main(int, char*[])
Jason M. Bills5e049d32018-10-19 12:59:38 -0700271{
272 // setup connection to dbus
Ed Tanousc3526342023-03-06 13:37:53 -0800273 boost::asio::io_context io;
Jason M. Bills5e049d32018-10-19 12:59:38 -0700274 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
275
276 // IPMI SEL Object
277 conn->request_name(ipmiSelObject);
278 auto server = sdbusplus::asio::object_server(conn);
279
280 // Add SEL Interface
281 std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceAddSel =
282 server.add_interface(ipmiSelPath, ipmiSelAddInterface);
283
284 // Add a new SEL entry
285 ifaceAddSel->register_method(
Konstantin Aladyshev6f5342d2023-04-19 09:23:11 +0000286 "IpmiSelAdd",
287 [conn](const std::string& message, const std::string& path,
288 const std::vector<uint8_t>& selData, const bool& assert,
289 const uint16_t& genId) {
290 return selAddSystemRecord(conn, message, path, selData, assert, genId);
Jason M. Bills5e049d32018-10-19 12:59:38 -0700291 });
292 // Add a new OEM SEL entry
Patrick Williams69f7fa32023-05-10 07:50:41 -0500293 ifaceAddSel->register_method("IpmiSelAddOem",
Konstantin Aladyshev6f5342d2023-04-19 09:23:11 +0000294 [conn](const std::string& message,
295 const std::vector<uint8_t>& selData,
296 const uint8_t& recordType) {
297 return selAddOemRecord(conn, message, selData, recordType);
Patrick Williams69f7fa32023-05-10 07:50:41 -0500298 });
Charles Boyer9f476e82021-07-29 16:33:01 -0500299
300#ifdef SEL_LOGGER_CLEARS_SEL
301#ifndef SEL_LOGGER_SEND_TO_LOGGING_SERVICE
302 // Clear SEL entries
303 ifaceAddSel->register_method("Clear", []() { clearSelLogFiles(); });
304#endif
305#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700306 ifaceAddSel->initialize();
307
308#ifdef SEL_LOGGER_MONITOR_THRESHOLD_EVENTS
Patrick Williamsccef2272022-07-22 19:26:54 -0500309 sdbusplus::bus::match_t thresholdAssertMonitor =
Zhikui Ren25b26e12020-06-26 20:18:19 -0700310 startThresholdAssertMonitor(conn);
Jason M. Bills5e049d32018-10-19 12:59:38 -0700311#endif
312
Nikhil Potadeafbaa092019-03-06 16:18:13 -0800313#ifdef REDFISH_LOG_MONITOR_PULSE_EVENTS
Patrick Williamsccef2272022-07-22 19:26:54 -0500314 sdbusplus::bus::match_t pulseEventMonitor = startPulseEventMonitor(conn);
Nikhil Potadeafbaa092019-03-06 16:18:13 -0800315#endif
316
Charles Hsudbd77b92020-10-29 11:20:34 +0800317#ifdef SEL_LOGGER_MONITOR_WATCHDOG_EVENTS
Patrick Williamsccef2272022-07-22 19:26:54 -0500318 sdbusplus::bus::match_t watchdogEventMonitor =
Charles Hsudbd77b92020-10-29 11:20:34 +0800319 startWatchdogEventMonitor(conn);
320#endif
George Hung486e42e2021-04-14 20:20:42 +0800321
322#ifdef SEL_LOGGER_MONITOR_THRESHOLD_ALARM_EVENTS
323 startThresholdAlarmMonitor(conn);
324#endif
JinFuLin7c2810b2022-12-02 13:55:28 +0800325
326#ifdef SEL_LOGGER_MONITOR_HOST_ERROR_EVENTS
327 startHostErrorEventMonitor(conn);
328#endif
Jason M. Bills5e049d32018-10-19 12:59:38 -0700329 io.run();
330
331 return 0;
332}