问题
I need to report errors from my application on C++Builder via SNMP.
I started implementing SNMP SendTrap using Indy components.
void __fastcall TMainForm::btSendTrapClick(TObject *Sender)
{
UnicodeString myEnterprise = "1.5.5.5.5.5.5.5";
UnicodeString eventType = "1.5.5.5.5.5.5.5.1";
UnicodeString eventDistance = "1.5.5.5.5.5.5.5.2";
TIdSNMP * idSnmp = 0;
TSNMPInfo * infoSnmp = 0;
idSnmp = new TIdSNMP(NULL);
infoSnmp = new TSNMPInfo(idSnmp);
idSnmp->Host = edHost->Text;
idSnmp->Community = "public";
infoSnmp->Host = edHost->Text;
infoSnmp->Community = "public";
infoSnmp->Enterprise = myEnterprise;
infoSnmp->GenTrap = 6; // I've met such values
infoSnmp->SpecTrap = 1; // somewhere in inet
infoSnmp->MIBAdd(eventType,"ftCritical");
infoSnmp->MIBAdd(eventDistance,"2.357");
idSnmp->SendTrap();
delete idSnmp;
}
But when I run application there is no udp activity in my system. When I run something like this
idSnmp->QuickSend(sysDescr, "public", edHost->Text, val);
wireshark shows 192.168.100.21 192.168.100.19 SNMP 82 get-request 1.3.6.1.2.1.1.3.0
but when idSnmp->SendTrap()
wireshark sees nothing (filter for wireshark is UDP portrange 161-162 in both cases)
I'll be glad to see some remarks about my code or maybe working example of SendTrap :)
回答1:
You are not populating TIdSNMP::Trap
with any values. That is why TIdSNMP::SendTrap()
is not sending anything. There is nothing for it to send.
Try this instead:
void __fastcall TMainForm::btSendTrapClick(TObject *Sender)
{
String myEnterprise = _D("1.5.5.5.5.5.5.5");
String eventType = myEnterprise + _D(".1");
String eventDistance = myEnterprise + _D(".2");
TIdSNMP *idSnmp = new TIdSNMP(NULL);
idSnmp->Trap->Host = edHost->Text;
idSnmp->Trap->Community = _D("public");
idSnmp->Trap->Enterprise = myEnterprise;
idSnmp->Trap->GenTrap = 6; // I've met such values
idSnmp->Trap->SpecTrap = 1; // somewhere in inet
idSnmp->Trap->MIBAdd(eventType, _D("ftCritical"));
idSnmp->Trap->MIBAdd(eventDistance, _D("2.357"));
idSnmp->SendTrap();
delete idSnmp;
}
Alternatively, you can use TIdSNMP::QuickSendTrap()
instead:
void __fastcall TMainForm::btSendTrapClick(TObject *Sender)
{
String myEnterprise = _D("1.5.5.5.5.5.5.5");
String eventType = myEnterprise + _D(".1");
String eventDistance = myEnterprise + _D(".2");
TStringList *names = new TStringList;
names->Add(eventType);
names->Add(eventDistance);
TStringList *values = new TStringList;
values->AddObject(_D("ftCritical"), (TObject*)ASN1_OCTSTR);
values->AddObject(_D("2.357"), (TObject*)ASN1_OCTSTR);
TIdSNMP *idSnmp = new TIdSNMP(NULL);
idSnmp->QuickSendTrap(edHost->Text, myEnterprise, _D("public"), 162, 6, 1, names, values);
delete idSnmp;
delete names;
delete values;
}
Or, if you are compiling for mobile:
void __fastcall TMainForm::btSendTrapClick(TObject *Sender)
{
String myEnterprise = _D("1.5.5.5.5.5.5.5");
String eventType = myEnterprise + _D(".1");
String eventDistance = myEnterprise + _D(".2");
TIdMIBValueList *mibs = new TIdMIBValueList;
mibs->Add(TIdMIBValue(eventType, _D("ftCritical"), ASN1_OCTSTR));
mibs->Add(TIdMIBValue(eventDistance, _D("2.357"), ASN1_OCTSTR));
TIdSNMP *idSnmp = new TIdSNMP(NULL);
idSnmp->QuickSendTrap(edHost->Text, myEnterprise, _D("public"), 162, 6, 1, mibs);
delete idSnmp;
delete mibs;
}
来源:https://stackoverflow.com/questions/24400971/implementing-snmp-sendtrap-using-indy-components