1 #include "snmp_trap.hpp"
2
3 #include <phosphor-logging/elog-errors.hpp>
4 #include <phosphor-logging/elog.hpp>
5 #include <phosphor-logging/lg2.hpp>
6 #include <snmp.hpp>
7 #include <snmp_notification.hpp>
8 #include <xyz/openbmc_project/Common/error.hpp>
9 #include <xyz/openbmc_project/Logging/Entry/server.hpp>
10 namespace phosphor
11 {
12 namespace dbus
13 {
14 namespace monitoring
15 {
16 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
17 using namespace phosphor::network::snmp;
18 using InternalFailure =
19 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
20
21 static constexpr auto entry = "xyz.openbmc_project.Logging.Entry";
22
trap(sdbusplus::message_t & msg) const23 void ErrorTrap::trap(sdbusplus::message_t& msg) const
24 {
25 sdbusplus::message::object_path path;
26 msg.read(path);
27 PathInterfacesAdded intfs;
28 msg.read(intfs);
29 auto it = intfs.find(entry);
30 if (it == intfs.end())
31 {
32 return;
33 }
34 auto& propMap = it->second;
35 auto errorID = std::get<uint32_t>(propMap.at("Id"));
36 auto timestamp = std::get<uint64_t>(propMap.at("Timestamp"));
37 auto sev = std::get<Entry::Level>(propMap.at("Severity"));
38 auto isev = static_cast<uint8_t>(sev);
39 auto message = std::get<std::string>(propMap.at("Message"));
40 auto additionalData = std::get<std::map<std::string, std::string>>(
41 propMap.at("AdditionalData"));
42 for (auto& [k, v] : additionalData)
43 {
44 message.append(" ");
45 message.append(k + "=" + v);
46 }
47 try
48 {
49 sendTrap<OBMCErrorNotification>(errorID, timestamp, isev, message);
50 }
51 catch (const InternalFailure& e)
52 {
53 lg2::error(
54 "Failed to send SNMP trap: {ERROR}, ERROR_ID = {EID}, TIMESTAMP = {TSP}, SEVERITY = {SEVERITY}, MESSAGE = {MSG}",
55 "ERROR", e, "EID", errorID, "TSP", timestamp, "SEVERITY",
56 convertForMessage(sev), "MSG", message);
57 }
58 }
59 } // namespace monitoring
60 } // namespace dbus
61 } // namespace phosphor
62