1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <stdlib.h>
#include <snmp_pp.h>
#include <iostream>
using namespace std;
bool run = true;
void callback(int reason, Snmp *snmp, Pdu &pdu, SnmpTarget &target, void *data)
{
Vb nextVb;
int pdu_error;
// What is the reason for this callback?
if (reason == SNMP_CLASS_NOTIFICATION)
{
// get the notify id for traps
Oid id;
pdu.get_notify_id(id);
}
// Look at the error status of the Pdu
pdu_error = pdu.get_error_status();
if (pdu_error)
{
return;
}
// The Pdu must contain at least one Vb
if (pdu.get_vb_count() == 0)
{
return;
}
// If we received a inform pdu, we have to send a response
if (pdu.get_type() == sNMP_PDU_INFORM)
{
// just change the value of the first vb
pdu.get_vb(nextVb, 0);
snmp->response(pdu, target);
}
}
int main ()
{
Snmp::socket_startup();
int status=0;
UdpAddress address("192.168.0.11");
if(!address.valid())
{
printf("Address IP error");
}
Snmp snmp(status,address);
snmp.notify_set_listen_port(4700);
while (run)
{
OidCollection oidc;
TargetCollection targetc;
status = snmp.notify_register(oidc, targetc, callback, &snmp);
if (status == SNMP_CLASS_SUCCESS)
{
printf("\n TRAP SUCESS\n");
}
Sleep(5000);
//agent->receive();
}
return 0;
} |
Partager