blob: db19ecc3fbc4c5e62646259d025e0b65906d188d [file] [log] [blame]
Ratan Guptaec26fa62018-04-16 15:28:36 +05301#include "snmp_notification.hpp"
Patrick Williams1334b7b2021-02-22 17:15:12 -06002
Ratan Gupta63476192018-04-19 16:55:32 +05303#include "snmp_util.hpp"
Patrick Williams1334b7b2021-02-22 17:15:12 -06004#include "xyz/openbmc_project/Common/error.hpp"
Ratan Guptaec26fa62018-04-16 15:28:36 +05305
6#include <phosphor-logging/elog-errors.hpp>
7#include <phosphor-logging/log.hpp>
8
Ratan Guptaec26fa62018-04-16 15:28:36 +05309namespace phosphor
10{
11namespace network
12{
13namespace snmp
14{
15
16using namespace phosphor::logging;
17using namespace sdbusplus::xyz::openbmc_project::Common::Error;
18
19using snmpSessionPtr =
20 std::unique_ptr<netsnmp_session, decltype(&::snmp_close)>;
21
22bool Notification::addPDUVar(netsnmp_pdu& pdu, const OID& objID,
23 size_t objIDLen, u_char type, Value val)
24{
25 netsnmp_variable_list* varList = nullptr;
26 switch (type)
27 {
28 case ASN_INTEGER:
29 {
Patrick Williams7d4bd222020-05-13 11:05:10 -050030 auto ltmp = std::get<int32_t>(val);
Ratan Guptaec26fa62018-04-16 15:28:36 +053031 varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type,
32 &ltmp, sizeof(ltmp));
33 }
34 break;
35 case ASN_UNSIGNED:
36 {
Patrick Williams7d4bd222020-05-13 11:05:10 -050037 auto ltmp = std::get<uint32_t>(val);
Ratan Guptaec26fa62018-04-16 15:28:36 +053038 varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type,
39 &ltmp, sizeof(ltmp));
40 }
41 break;
42 case ASN_OPAQUE_U64:
43 {
Patrick Williams7d4bd222020-05-13 11:05:10 -050044 auto ltmp = std::get<uint64_t>(val);
Ratan Guptaec26fa62018-04-16 15:28:36 +053045 varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type,
46 &ltmp, sizeof(ltmp));
47 }
48 break;
49 case ASN_OCTET_STR:
50 {
Patrick Williams7d4bd222020-05-13 11:05:10 -050051 const auto& value = std::get<std::string>(val);
Ratan Guptaec26fa62018-04-16 15:28:36 +053052 varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type,
53 value.c_str(), value.length());
54 }
55 break;
56 }
57 return (varList == nullptr ? false : true);
58}
59
60void Notification::sendTrap()
61{
Ratan Guptaec26fa62018-04-16 15:28:36 +053062 constexpr auto comm = "public";
Ratan Guptaec26fa62018-04-16 15:28:36 +053063 netsnmp_session session{0};
64
65 snmp_sess_init(&session);
66
67 init_snmp("snmpapp");
68
69 // TODO: https://github.com/openbmc/openbmc/issues/3145
70 session.version = SNMP_VERSION_2c;
71 session.community = (u_char*)comm;
72 session.community_len = strlen(comm);
73 session.callback = nullptr;
74 session.callback_magic = nullptr;
75
Ratan Gupta63476192018-04-19 16:55:32 +053076 auto mgrs = getManagers();
Ratan Guptaec26fa62018-04-16 15:28:36 +053077
Ratan Gupta63476192018-04-19 16:55:32 +053078 for (auto& mgr : mgrs)
Ratan Guptaec26fa62018-04-16 15:28:36 +053079 {
Ratan Gupta63476192018-04-19 16:55:32 +053080 session.peername = const_cast<char*>(mgr.c_str());
81 // create the session
82 auto ss = snmp_add(
83 &session,
84 netsnmp_transport_open_client("snmptrap", session.peername),
85 nullptr, nullptr);
86 if (!ss)
Ratan Guptaec26fa62018-04-16 15:28:36 +053087 {
Ratan Gupta63476192018-04-19 16:55:32 +053088 log<level::ERR>("Unable to get the snmp session.",
89 entry("SNMPMANAGER=%s", mgr.c_str()));
90 elog<InternalFailure>();
91 }
92
93 // Wrap the raw pointer in RAII
94 snmpSessionPtr sessionPtr(ss, &::snmp_close);
95
96 ss = nullptr;
97
98 auto pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
99 if (!pdu)
100 {
101 log<level::ERR>("Failed to create notification PDU");
102 elog<InternalFailure>();
103 }
104
Ratan Gupta0d5094b2021-03-05 19:25:26 +0530105 // https://tools.ietf.org/search/rfc3416#page-22
106 // add the sysUpTime.0 [RFC3418]
107 auto sysuptime = get_uptime();
108 std::string sysuptimeStr = std::to_string(sysuptime);
109
110 if (snmp_add_var(pdu, sysuptimeOID, sizeof(sysuptimeOID) / sizeof(oid),
111 't', sysuptimeStr.c_str()))
112
113 {
114 log<level::ERR>("Failed to add the SNMP var(systime)");
115 snmp_free_pdu(pdu);
116 elog<InternalFailure>();
117 }
118
Ratan Gupta63476192018-04-19 16:55:32 +0530119 pdu->trap_type = SNMP_TRAP_ENTERPRISESPECIFIC;
120
121 auto trapInfo = getTrapOID();
122
Ratan Gupta0d5094b2021-03-05 19:25:26 +0530123 // add the snmpTrapOID.0 [RFC3418]
Ratan Gupta63476192018-04-19 16:55:32 +0530124 if (!snmp_pdu_add_variable(pdu, SNMPTrapOID,
125 sizeof(SNMPTrapOID) / sizeof(oid),
126 ASN_OBJECT_ID, trapInfo.first.data(),
127 trapInfo.second * sizeof(oid)))
128 {
129 log<level::ERR>("Failed to add the SNMP var(trapID)");
Ratan Guptaec26fa62018-04-16 15:28:36 +0530130 snmp_free_pdu(pdu);
131 elog<InternalFailure>();
132 }
Ratan Guptaec26fa62018-04-16 15:28:36 +0530133
Ratan Gupta63476192018-04-19 16:55:32 +0530134 auto objectList = getFieldOIDList();
135
136 for (const auto& object : objectList)
137 {
138 if (!addPDUVar(*pdu, std::get<0>(object), std::get<1>(object),
139 std::get<2>(object), std::get<3>(object)))
140 {
141 log<level::ERR>("Failed to add the SNMP var");
142 snmp_free_pdu(pdu);
143 elog<InternalFailure>();
144 }
145 }
146 // pdu is freed by snmp_send
147 if (!snmp_send(sessionPtr.get(), pdu))
148 {
149 log<level::ERR>("Failed to send the snmp trap.");
150 elog<InternalFailure>();
151 }
Ratan Guptaec26fa62018-04-16 15:28:36 +0530152
Ratan Gupta212f53e2018-04-30 17:28:05 +0530153 log<level::DEBUG>("Sent SNMP Trap", entry("MGR=%s", mgr.c_str()));
154 }
Ratan Guptaec26fa62018-04-16 15:28:36 +0530155}
156
157} // namespace snmp
158} // namespace network
159} // namespace phosphor