1 /** 2 * @brief SNMP Error Notification class. 3 * 4 * This file is part of phosphor-snmp project. 5 * 6 * Copyright (c) 2018 IBM Corporation 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 * Note: In near future this file will be autogenerated by the custom parser. 21 * 22 */ 23 24 #pragma once 25 26 // net-snmp requires a very specific header include order. 27 // disable clang-format around this block 28 // clang-format off 29 #include <net-snmp/net-snmp-config.h> 30 #include <net-snmp/net-snmp-includes.h> 31 #include <net-snmp/agent/net-snmp-agent-includes.h> 32 // clang-format on 33 34 #include <sdbusplus/server.hpp> 35 36 #include <sstream> 37 #include <string> 38 #include <tuple> 39 #include <vector> 40 41 namespace phosphor 42 { 43 namespace network 44 { 45 namespace snmp 46 { 47 48 using OID = std::array<oid, MAX_OID_LEN>; 49 using OID_LEN = size_t; 50 using Type = u_char; 51 52 using Value = std::variant<uint32_t, uint64_t, int32_t, std::string>; 53 // Generic snmp trap ID 54 oid SNMPTrapOID[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0}; 55 oid sysuptimeOID[] = {1, 3, 6, 1, 2, 1, 1, 3, 0}; 56 57 using Object = std::tuple<OID, OID_LEN, Type, Value>; 58 59 /** @brief Get the ASN object type from the given templatized type. 60 * Specialize this template for handling a specific type. 61 * @tparam T - type of object from ASN type would be decided. 62 * @returns the ASN object type. 63 */ 64 template <typename T> 65 u_char getASNType() = delete; 66 67 template <> 68 u_char getASNType<uint32_t>() 69 { 70 return ASN_UNSIGNED; 71 } 72 73 template <> 74 u_char getASNType<uint64_t>() 75 { 76 return ASN_OPAQUE_U64; 77 } 78 79 template <> 80 u_char getASNType<int32_t>() 81 { 82 return ASN_INTEGER; 83 } 84 85 template <> 86 u_char getASNType<std::string>() 87 { 88 return ASN_OCTET_STR; 89 } 90 91 /** @class Notification 92 * @brief Notification interface. 93 * 94 * This class implements the sendTrap function which 95 * send the list of objects defined by the specific notification 96 * to the configured SNMP manager. 97 */ 98 99 class Notification 100 { 101 public: 102 Notification() = default; 103 Notification(const Notification&) = delete; 104 Notification(Notification&&) = default; 105 Notification& operator=(const Notification&) = delete; 106 Notification& operator=(Notification&&) = default; 107 virtual ~Notification() = default; 108 109 /** @brief Send the snmp trap to the configured 110 * manager. 111 */ 112 void sendTrap(); 113 114 protected: 115 /** @brief Add the variable in the snmp pdu object. 116 * @param[in] pdu - SNMP pdu object. 117 * @param[in] objID - SNMP object identifier. 118 * @param[in] objIDLen - Object identifier length. 119 * @param[in] type - ASN type of object. 120 * @param[in] val - Value of the object. 121 * @returns true on success otherwise false. 122 */ 123 bool addPDUVar(netsnmp_pdu& pdu, const OID& objID, size_t objIDLen, 124 u_char type, Value val); 125 126 /** @brief get the SNMP notification type in the mib 127 * defined format. 128 * This is pure virtual function all the subclasses 129 * need to provide its own defined type. 130 * @returns the notification type string. 131 */ 132 virtual std::pair<OID, OID_LEN> getTrapOID() = 0; 133 134 /** @brief get all the objects meta data defined under 135 * this notification. 136 */ 137 virtual std::vector<Object> getFieldOIDList() = 0; 138 }; 139 140 class TestErrorNotification; 141 142 /** @class ErrorNotification 143 * @brief subclass of Notification 144 * 145 * A Error Notification represents the objects needed by the 146 * Error Object. 147 */ 148 class OBMCErrorNotification : public Notification 149 { 150 private: 151 uint32_t OBMCErrorID = 0; 152 uint64_t OBMCErrorTimestamp = 0; 153 int32_t OBMCErrorSeverity = 0; 154 std::string OBMCErrorMessage; 155 156 public: 157 OBMCErrorNotification() = default; 158 OBMCErrorNotification(const OBMCErrorNotification&) = delete; 159 OBMCErrorNotification(OBMCErrorNotification&&) = default; 160 OBMCErrorNotification& operator=(const OBMCErrorNotification&) = delete; 161 OBMCErrorNotification& operator=(OBMCErrorNotification&&) = default; 162 ~OBMCErrorNotification() = default; 163 164 /** @brief Constructor 165 * @param[in] id - The error entry id. 166 * @param[in] ts - The commit timestamp. 167 * @param[in] sev - The severity of the error. 168 * @param[in] msg - The message of the error. 169 */ 170 OBMCErrorNotification(uint32_t id, uint64_t ts, int32_t sev, 171 std::string msg) : 172 OBMCErrorID(id), 173 OBMCErrorTimestamp(ts), OBMCErrorSeverity(sev), OBMCErrorMessage(msg) 174 {} 175 176 protected: 177 std::pair<OID, OID_LEN> getTrapOID() override 178 { 179 // notification sub types 180 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 0, 1}; 181 OID_LEN idLen = 11; 182 return std::make_pair<OID, OID_LEN>(std::move(id), std::move(idLen)); 183 } 184 185 std::vector<Object> getFieldOIDList() override 186 { 187 std::vector<Object> objectList; 188 189 OID_LEN idLen = 11; 190 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 1}; 191 auto type = getASNType<decltype(OBMCErrorID)>(); 192 193 objectList.emplace_back(id, idLen, type, OBMCErrorID); 194 195 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 2}; 196 type = getASNType<decltype(OBMCErrorTimestamp)>(); 197 198 objectList.emplace_back(id, idLen, type, OBMCErrorTimestamp); 199 200 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 3}; 201 type = getASNType<decltype(OBMCErrorSeverity)>(); 202 203 objectList.emplace_back(id, idLen, type, OBMCErrorSeverity); 204 205 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 4}; 206 type = getASNType<decltype(OBMCErrorMessage)>(); 207 208 objectList.emplace_back(id, idLen, type, OBMCErrorMessage); 209 210 return objectList; 211 } 212 213 friend class TestErrorNotification; 214 }; 215 216 } // namespace snmp 217 } // namespace network 218 } // namespace phosphor 219