blob: e27408ab20475696ec7eea47c8f1b616adb834b1 [file] [log] [blame]
Ratan Guptaec26fa62018-04-16 15:28:36 +05301#include "snmp_notification.hpp"
2
3#include <phosphor-logging/elog-errors.hpp>
4#include <phosphor-logging/log.hpp>
5
6#include "xyz/openbmc_project/Common/error.hpp"
7
8namespace phosphor
9{
10namespace network
11{
12namespace snmp
13{
14
15using namespace phosphor::logging;
16using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17
18using snmpSessionPtr =
19 std::unique_ptr<netsnmp_session, decltype(&::snmp_close)>;
20
21bool Notification::addPDUVar(netsnmp_pdu& pdu, const OID& objID,
22 size_t objIDLen, u_char type, Value val)
23{
24 netsnmp_variable_list* varList = nullptr;
25 switch (type)
26 {
27 case ASN_INTEGER:
28 {
29 auto ltmp = val.get<int32_t>();
30 varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type,
31 &ltmp, sizeof(ltmp));
32 }
33 break;
34 case ASN_UNSIGNED:
35 {
36 auto ltmp = val.get<uint32_t>();
37 varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type,
38 &ltmp, sizeof(ltmp));
39 }
40 break;
41 case ASN_OPAQUE_U64:
42 {
43 auto ltmp = val.get<uint64_t>();
44 varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type,
45 &ltmp, sizeof(ltmp));
46 }
47 break;
48 case ASN_OCTET_STR:
49 {
50 auto value = val.get<std::string>();
51 varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type,
52 value.c_str(), value.length());
53 }
54 break;
55 }
56 return (varList == nullptr ? false : true);
57}
58
59void Notification::sendTrap()
60{
61 log<level::DEBUG>("Sending SNMP Trap");
62
63 constexpr auto comm = "public";
64 constexpr auto localHost = "127.0.0.1";
65 netsnmp_session session{0};
66
67 snmp_sess_init(&session);
68
69 init_snmp("snmpapp");
70
71 // TODO: https://github.com/openbmc/openbmc/issues/3145
72 session.version = SNMP_VERSION_2c;
73 session.community = (u_char*)comm;
74 session.community_len = strlen(comm);
75 session.callback = nullptr;
76 session.callback_magic = nullptr;
77
78 // TODO:- get it from settings D-bus object.
79 session.peername = const_cast<char*>(localHost);
80
81 // create the session
82 auto ss = snmp_add(
83 &session, netsnmp_transport_open_client("snmptrap", session.peername),
84 nullptr, nullptr);
85 if (!ss)
86 {
87 log<level::ERR>("Unable to get the snmp session.",
88 entry("SNMPMANAGER=%s", session.peername));
89 elog<InternalFailure>();
90 }
91
92 // Wrap the raw pointer in RAII
93 snmpSessionPtr sessionPtr(ss, &::snmp_close);
94
95 ss = nullptr;
96
97 auto pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
98 if (!pdu)
99 {
100 log<level::ERR>("Failed to create notification PDU");
101 elog<InternalFailure>();
102 }
103
104 pdu->trap_type = SNMP_TRAP_ENTERPRISESPECIFIC;
105
106 auto trapInfo = getTrapOID();
107
108 if (!snmp_pdu_add_variable(
109 pdu, SNMPTrapOID, sizeof(SNMPTrapOID) / sizeof(oid), ASN_OBJECT_ID,
110 trapInfo.first.data(), trapInfo.second * sizeof(oid)))
111 {
112 log<level::ERR>("Failed to add the SNMP var(trapID)");
113 snmp_free_pdu(pdu);
114 elog<InternalFailure>();
115 }
116
117 auto objectList = getFieldOIDList();
118
119 for (const auto& object : objectList)
120 {
121 if (!addPDUVar(*pdu, std::get<0>(object), std::get<1>(object),
122 std::get<2>(object), std::get<3>(object)))
123 {
124 log<level::ERR>("Failed to add the SNMP var");
125 snmp_free_pdu(pdu);
126 elog<InternalFailure>();
127 }
128 }
129
130 // pdu is freed by snmp_send
131 if (!snmp_send(sessionPtr.get(), pdu))
132 {
133 log<level::ERR>("Failed to send the snmp trap.");
134 elog<InternalFailure>();
135 }
136
137 log<level::DEBUG>("Sent SNMP Trap");
138}
139
140} // namespace snmp
141} // namespace network
142} // namespace phosphor