Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 1 | #include "snmp_notification.hpp" |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 2 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 3 | #include "snmp_util.hpp" |
Patrick Williams | 1334b7b | 2021-02-22 17:15:12 -0600 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 5 | |
| 6 | #include <phosphor-logging/elog-errors.hpp> |
| 7 | #include <phosphor-logging/log.hpp> |
| 8 | |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 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 | 34d129a | 2021-12-04 21:04:51 +0530 | [diff] [blame] | 63 | netsnmp_session session{}; |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 64 | snmp_sess_init(&session); |
| 65 | |
| 66 | init_snmp("snmpapp"); |
| 67 | |
| 68 | // TODO: https://github.com/openbmc/openbmc/issues/3145 |
| 69 | session.version = SNMP_VERSION_2c; |
| 70 | session.community = (u_char*)comm; |
| 71 | session.community_len = strlen(comm); |
| 72 | session.callback = nullptr; |
| 73 | session.callback_magic = nullptr; |
| 74 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 75 | auto mgrs = getManagers(); |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 76 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 77 | for (auto& mgr : mgrs) |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 78 | { |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 79 | session.peername = const_cast<char*>(mgr.c_str()); |
| 80 | // create the session |
| 81 | auto ss = snmp_add( |
| 82 | &session, |
| 83 | netsnmp_transport_open_client("snmptrap", session.peername), |
| 84 | nullptr, nullptr); |
| 85 | if (!ss) |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 86 | { |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 87 | log<level::ERR>("Unable to get the snmp session.", |
| 88 | entry("SNMPMANAGER=%s", mgr.c_str())); |
| 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 | |
Ratan Gupta | 0d5094b | 2021-03-05 19:25:26 +0530 | [diff] [blame] | 104 | // https://tools.ietf.org/search/rfc3416#page-22 |
| 105 | // add the sysUpTime.0 [RFC3418] |
| 106 | auto sysuptime = get_uptime(); |
| 107 | std::string sysuptimeStr = std::to_string(sysuptime); |
| 108 | |
| 109 | if (snmp_add_var(pdu, sysuptimeOID, sizeof(sysuptimeOID) / sizeof(oid), |
| 110 | 't', sysuptimeStr.c_str())) |
| 111 | |
| 112 | { |
| 113 | log<level::ERR>("Failed to add the SNMP var(systime)"); |
| 114 | snmp_free_pdu(pdu); |
| 115 | elog<InternalFailure>(); |
| 116 | } |
| 117 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 118 | pdu->trap_type = SNMP_TRAP_ENTERPRISESPECIFIC; |
| 119 | |
| 120 | auto trapInfo = getTrapOID(); |
| 121 | |
Ratan Gupta | 0d5094b | 2021-03-05 19:25:26 +0530 | [diff] [blame] | 122 | // add the snmpTrapOID.0 [RFC3418] |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 123 | if (!snmp_pdu_add_variable(pdu, SNMPTrapOID, |
| 124 | sizeof(SNMPTrapOID) / sizeof(oid), |
| 125 | ASN_OBJECT_ID, trapInfo.first.data(), |
| 126 | trapInfo.second * sizeof(oid))) |
| 127 | { |
| 128 | log<level::ERR>("Failed to add the SNMP var(trapID)"); |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 129 | snmp_free_pdu(pdu); |
| 130 | elog<InternalFailure>(); |
| 131 | } |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 132 | |
Ratan Gupta | 6347619 | 2018-04-19 16:55:32 +0530 | [diff] [blame] | 133 | auto objectList = getFieldOIDList(); |
| 134 | |
| 135 | for (const auto& object : objectList) |
| 136 | { |
| 137 | if (!addPDUVar(*pdu, std::get<0>(object), std::get<1>(object), |
| 138 | std::get<2>(object), std::get<3>(object))) |
| 139 | { |
| 140 | log<level::ERR>("Failed to add the SNMP var"); |
| 141 | snmp_free_pdu(pdu); |
| 142 | elog<InternalFailure>(); |
| 143 | } |
| 144 | } |
| 145 | // pdu is freed by snmp_send |
| 146 | if (!snmp_send(sessionPtr.get(), pdu)) |
| 147 | { |
| 148 | log<level::ERR>("Failed to send the snmp trap."); |
| 149 | elog<InternalFailure>(); |
| 150 | } |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 151 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 152 | log<level::DEBUG>("Sent SNMP Trap", entry("MGR=%s", mgr.c_str())); |
| 153 | } |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace snmp |
| 157 | } // namespace network |
| 158 | } // namespace phosphor |