Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 1 | #include "snmp_notification.hpp" |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 2 | #include "snmp_util.hpp" |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 3 | |
| 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | |
| 7 | #include "xyz/openbmc_project/Common/error.hpp" |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace network |
| 12 | { |
| 13 | namespace snmp |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 18 | |
| 19 | using snmpSessionPtr = |
| 20 | std::unique_ptr<netsnmp_session, decltype(&::snmp_close)>; |
| 21 | |
| 22 | bool 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 Williams | 7d4bd22 | 2020-05-13 11:05:10 -0500 | [diff] [blame] | 30 | auto ltmp = std::get<int32_t>(val); |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 31 | varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type, |
| 32 | <mp, sizeof(ltmp)); |
| 33 | } |
| 34 | break; |
| 35 | case ASN_UNSIGNED: |
| 36 | { |
Patrick Williams | 7d4bd22 | 2020-05-13 11:05:10 -0500 | [diff] [blame] | 37 | auto ltmp = std::get<uint32_t>(val); |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 38 | varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type, |
| 39 | <mp, sizeof(ltmp)); |
| 40 | } |
| 41 | break; |
| 42 | case ASN_OPAQUE_U64: |
| 43 | { |
Patrick Williams | 7d4bd22 | 2020-05-13 11:05:10 -0500 | [diff] [blame] | 44 | auto ltmp = std::get<uint64_t>(val); |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 45 | varList = snmp_pdu_add_variable(&pdu, objID.data(), objIDLen, type, |
| 46 | <mp, sizeof(ltmp)); |
| 47 | } |
| 48 | break; |
| 49 | case ASN_OCTET_STR: |
| 50 | { |
Patrick Williams | 7d4bd22 | 2020-05-13 11:05:10 -0500 | [diff] [blame] | 51 | const auto& value = std::get<std::string>(val); |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 52 | 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 | |
| 60 | void Notification::sendTrap() |
| 61 | { |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 62 | constexpr auto comm = "public"; |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 63 | 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 Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 76 | auto mgrs = getManagers(); |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 77 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 78 | for (auto& mgr : mgrs) |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 79 | { |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 80 | 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 Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 87 | { |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 88 | 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 | |
| 105 | pdu->trap_type = SNMP_TRAP_ENTERPRISESPECIFIC; |
| 106 | |
| 107 | auto trapInfo = getTrapOID(); |
| 108 | |
| 109 | if (!snmp_pdu_add_variable(pdu, SNMPTrapOID, |
| 110 | sizeof(SNMPTrapOID) / sizeof(oid), |
| 111 | ASN_OBJECT_ID, trapInfo.first.data(), |
| 112 | trapInfo.second * sizeof(oid))) |
| 113 | { |
| 114 | log<level::ERR>("Failed to add the SNMP var(trapID)"); |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 115 | snmp_free_pdu(pdu); |
| 116 | elog<InternalFailure>(); |
| 117 | } |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 118 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 119 | auto objectList = getFieldOIDList(); |
| 120 | |
| 121 | for (const auto& object : objectList) |
| 122 | { |
| 123 | if (!addPDUVar(*pdu, std::get<0>(object), std::get<1>(object), |
| 124 | std::get<2>(object), std::get<3>(object))) |
| 125 | { |
| 126 | log<level::ERR>("Failed to add the SNMP var"); |
| 127 | snmp_free_pdu(pdu); |
| 128 | elog<InternalFailure>(); |
| 129 | } |
| 130 | } |
| 131 | // pdu is freed by snmp_send |
| 132 | if (!snmp_send(sessionPtr.get(), pdu)) |
| 133 | { |
| 134 | log<level::ERR>("Failed to send the snmp trap."); |
| 135 | elog<InternalFailure>(); |
| 136 | } |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 137 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 138 | log<level::DEBUG>("Sent SNMP Trap", entry("MGR=%s", mgr.c_str())); |
| 139 | } |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | } // namespace snmp |
| 143 | } // namespace network |
| 144 | } // namespace phosphor |