Classes | |
class | OID |
Wrapper for net-snmp's OID arrays. More... | |
class | PDU |
Wrapper for net-snmp's PDU structures. More... | |
class | Varlist |
Wrapper for net-snmp's snmp_variable_list pointer. More... | |
Typedefs | |
typedef std::map< SNMPpp::OID, netsnmp_variable_list * > | MapOidVarList |
A std::map where the OID is the key and the value is a net-snmp variable list. This is used by SNMPpp::PDU and SNMPpp::Varlist. More... | |
typedef void * | SessionHandle |
net-snmp uses a void * for the "Single API" opaque session pointer, but SNMPpp prefers to have an actual named type to make it more obvious when passing the session pointer by reference in C++. More... | |
typedef std::set< OID > | SetOID |
A std::set of OIDs. More... | |
typedef std::vector< OID > | VecOID |
A std::vector of OIDs. More... | |
Functions | |
long | centisecondsNetSnmpUptime (void) |
Return the uptime in centiseconds. More... | |
long | centisecondsPidStarted (pid_t pid=0) |
Return the uptime in centiseconds. More... | |
long | centisecondsSince (const time_t t) |
Return the time since t . More... | |
long | centisecondsUptime (void) |
Return the number of centiseconds since the last reboot. More... | |
void | closeSession (SessionHandle &sessionHandle) |
Sessions must be closed when no longer needed. More... | |
SNMPpp::Varlist | createV2TrapVarlist (const SNMPpp::OID &o, const long uptime=SNMPpp::centisecondsNetSnmpUptime()) |
Initialize a varlist to be eventually used with one of the sendV2Trap() functions. More... | |
SNMPpp::PDU | get (SNMPpp::SessionHandle &session, SNMPpp::PDU &pdu) |
Alias to SNMPpp::sync() for convenience, and to be consistent with the various other SNMPpp::get...() calls. More... | |
SNMPpp::PDU | get (SNMPpp::SessionHandle &session, const SNMPpp::OID &o) |
Get a single OID. More... | |
SNMPpp::PDU | get (SNMPpp::SessionHandle &session, const SetOID &oids) |
Get several specific OIDs at once. More... | |
SNMPpp::PDU | getBulk (SNMPpp::SessionHandle &session, const SNMPpp::OID &o, const int maxRepetitions=50, const int nonRepeaters=0) |
Getbulk request, starting with the given OID. More... | |
SNMPpp::PDU | getBulk (SNMPpp::SessionHandle &session, SNMPpp::PDU &pdu, const int maxRepetitions=50, const int nonRepeaters=0) |
Getbulk request, possibly with multiple starting points. More... | |
SNMPpp::PDU | getNext (SNMPpp::SessionHandle &session, const SNMPpp::OID &o) |
Get the next OID. More... | |
SNMPpp::PDU | getNext (SNMPpp::SessionHandle &session, SNMPpp::PDU &request) |
Get the next OID. More... | |
void | initializeNetSnmpAgent (const std::string &name="snmppp") |
Initialize net-snmp AgentX. More... | |
void | netsnmpDisableLogging (void) |
Disable net-snmp logging. More... | |
void | netsnmpLogStdErr (const bool enabled=true) |
Toggle net-snmp logging to STDERR. More... | |
void | netsnmpLogSyslog (const bool enabled=true) |
Toggle net-snmp logging to syslog. More... | |
void | netsnmpLogToFile (const bool enabled=true, const std::string &filename="/var/log/netsnmp.log") |
Toggle net-snmp logging to an external file. More... | |
void | openSession (SessionHandle &sessionHandle, const std::string &server="udp:127.0.0.1:161", const std::string &community="public", const int version=SNMP_VERSION_2c, const int retryAttempts=3) |
Open a net-snmp session and return a session handle. More... | |
void | sendV2Trap (const SNMPpp::OID &o, const long uptime=SNMPpp::centisecondsNetSnmpUptime()) |
Send a SNMPv2 trap to all configured trapsinks in net-snmp. More... | |
void | sendV2Trap (SNMPpp::Varlist &varlist) |
Send a SNMPv2 trap to alll configured trapsinks in net-snmp. More... | |
void | sendV2Trap (SNMPpp::PDU &pdu) |
Similar to SNMPpp::sendV2Trap( SNMPpp::Varlist &vl ). More... | |
void | shutdownNetSnmpAgent (const std::string &name="snmppp") |
De-initialize net-snmp AgentX. More... | |
SNMPpp::PDU | sync (SNMPpp::SessionHandle &session, SNMPpp::PDU &request) |
Send a PDU using the given SNMPpp::SessionHandle, and wait for a reply. More... | |
std::string | version (void) |
Return the SNMPpp version number. More... | |
typedef std::map< SNMPpp::OID, netsnmp_variable_list *> SNMPpp::MapOidVarList |
A std::map where the OID is the key and the value is a net-snmp variable list. This is used by SNMPpp::PDU and SNMPpp::Varlist.
typedef void* SNMPpp::SessionHandle |
net-snmp uses a void * for the "Single API" opaque session pointer, but SNMPpp prefers to have an actual named type to make it more obvious when passing the session pointer by reference in C++.
For example, see the return value of snmp_sess_open();
.
typedef std::set<OID> SNMPpp::SetOID |
A std::set of OIDs.
typedef std::vector<OID> SNMPpp::VecOID |
A std::vector of OIDs.
long SNMPpp::centisecondsNetSnmpUptime | ( | void | ) |
Return the uptime in centiseconds.
The first variable in SNMPv2 traps is 'sysUpTime', which is defined as:
"the time ... since the network management portion of the system was re-initialized"
But in all likelihood, this value isn't easily available from code which is using SNMPpp. So several other possibly useful values have been made available for convenience. Of those, centisecondsNetSnmpUptime() is most likely the value you need to use.
long SNMPpp::centisecondsPidStarted | ( | pid_t | pid = 0 ) |
Return the uptime in centiseconds.
[in] | pid | If set to zero then the current process ID will be queried by calling getpid(). |
long SNMPpp::centisecondsSince | ( | const time_t | t) |
Return the time since t
.
zero
is returned.long SNMPpp::centisecondsUptime | ( | void | ) |
Return the number of centiseconds since the last reboot.
void SNMPpp::closeSession | ( | SNMPpp::SessionHandle & | sessionHandle) |
Sessions must be closed when no longer needed.
This can be done by calling snmp_sess_close()
directly, or SNMPpp::closeSession().
SNMPpp::Varlist SNMPpp::createV2TrapVarlist | ( | const SNMPpp::OID & | o, |
const long | uptime = SNMPpp::centisecondsNetSnmpUptime() |
||
) |
Initialize a varlist to be eventually used with one of the sendV2Trap() functions.
If you'd like to send a trap with multiple variables, you can use this method as a starting point to easily create a varlist with the first two OIDs filled in correctly. Once you finish adding variables to the varlist, call SNMPpp::sendV2Trap( SNMPpp::Varlist &vl ) to have the trap sent out.
std::invalid_argument | if the OID is empty. |
SNMPpp::PDU SNMPpp::get | ( | SNMPpp::SessionHandle & | session, |
SNMPpp::PDU & | pdu | ||
) |
Alias to SNMPpp::sync() for convenience, and to be consistent with the various other SNMPpp::get...() calls.
SNMPpp::PDU SNMPpp::get | ( | SNMPpp::SessionHandle & | session, |
const SNMPpp::OID & | o | ||
) |
Get a single OID.
std::invalid_argument | if the OID is empty. |
SNMPpp::PDU SNMPpp::get | ( | SNMPpp::SessionHandle & | session, |
const SetOID & | oids | ||
) |
Get several specific OIDs at once.
std::invalid_argument | if the SetOID is empty. |
SNMPpp::PDU SNMPpp::getBulk | ( | SNMPpp::SessionHandle & | session, |
const SNMPpp::OID & | o, | ||
const int | maxRepetitions = 50 , |
||
const int | nonRepeaters = 0 |
||
) |
Getbulk request, starting with the given OID.
SNMPpp::PDU SNMPpp::getBulk | ( | SNMPpp::SessionHandle & | session, |
SNMPpp::PDU & | pdu, | ||
const int | maxRepetitions = 50 , |
||
const int | nonRepeaters = 0 |
||
) |
Getbulk request, possibly with multiple starting points.
std::invalid_argument | if the PDU is empty. |
SNMPpp::PDU SNMPpp::getNext | ( | SNMPpp::SessionHandle & | session, |
const SNMPpp::OID & | o | ||
) |
Get the next OID.
std::invalid_argument | if the OID is empty. |
SNMPpp::PDU SNMPpp::getNext | ( | SNMPpp::SessionHandle & | session, |
SNMPpp::PDU & | request | ||
) |
Get the next OID.
std::invalid_argument | if the PDU is empty. |
If the PDU is already of type SNMPpp::PDU::kGetNext then it is sent unchanged to the server described in SNMPpp::SessionHandle. But if the PDU is of any other type – such as a response PDU – then we'll look through the varlist and take the last OID listed. Using this, a response PDU can quickly be re-used to get the next OID from a SNMP server.
For example, note how the output PDU is then re-used as input in this simple while
loop:
void SNMPpp::initializeNetSnmpAgent | ( | const std::string & | name = "snmppp" ) |
Initialize net-snmp AgentX.
std::runtime_error | if net-snmp init_agent(name) returned an error. |
void SNMPpp::netsnmpDisableLogging | ( | void | ) |
Disable net-snmp logging.
void SNMPpp::netsnmpLogStdErr | ( | const bool | enabled = true ) |
Toggle net-snmp logging to STDERR.
void SNMPpp::netsnmpLogSyslog | ( | const bool | enabled = true ) |
Toggle net-snmp logging to syslog.
void SNMPpp::netsnmpLogToFile | ( | const bool | enabled = true , |
const std::string & | filename = "/var/log/netsnmp.log" |
||
) |
Toggle net-snmp logging to an external file.
void SNMPpp::openSession | ( | SNMPpp::SessionHandle & | sessionHandle, |
const std::string & | server = "udp:127.0.0.1:161" , |
||
const std::string & | community = "public" , |
||
const int | version = SNMP_VERSION_2c , |
||
const int | retryAttempts = 3 |
||
) |
Open a net-snmp session and return a session handle.
The session handle will be needed for all other net-snmp calls. Any previous value in "handle" will be blindly overwritten, so don't re-use handles unless you've remembered to call either snmp_sess_close()
or SNMPpp::closeSession().
struct snmp_session
!std::runtime_error | if snmp_sess_open() fails to return a valid new session. |
void SNMPpp::sendV2Trap | ( | const SNMPpp::OID & | o, |
const long | uptime = SNMPpp::centisecondsNetSnmpUptime() |
||
) |
Send a SNMPv2 trap to all configured trapsinks in net-snmp.
void SNMPpp::sendV2Trap | ( | SNMPpp::Varlist & | varlist) |
Send a SNMPv2 trap to alll configured trapsinks in net-snmp.
If you have a variable list already made up with at least 2 entries describing the uptime and the trap, call this function. Note you can use this to send as many additional OIDs as you want with the trap. The variable list is freed prior to returning to the caller.
std::invalid_argument | if varlist is empty. |
std::invalid_argument | if the varlist contains less than 2 OIDs. |
std::runtime_error | if netsnmp_send_traps(...) returned an error. |
void SNMPpp::sendV2Trap | ( | SNMPpp::PDU & | pdu) |
Similar to SNMPpp::sendV2Trap( SNMPpp::Varlist &vl ).
The PDU is freed prior to returning to the caller.
std::invalid_argument | if the PDU is empty. |
void SNMPpp::shutdownNetSnmpAgent | ( | const std::string & | name = "snmppp" ) |
De-initialize net-snmp AgentX.
SNMPpp::PDU SNMPpp::sync | ( | SNMPpp::SessionHandle & | session, |
SNMPpp::PDU & | request | ||
) |
Send a PDU using the given SNMPpp::SessionHandle, and wait for a reply.
This is the method all other "get" functions call to perform the underlying request->response sync with the SNMP server.
std::invalid_argument | if the PDU is empty. |
std::invalid_argument | if the session handle is NULL. |
std::runtime_error | if snmp_sess_synch_response() returned an error. |
std::string SNMPpp::version | ( | void | ) |