I need to report errors from my application on C++Builder via SNMP.
I started implementing SNMP SendTrap using Indy components.
void __fastcall TMainForm
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;
}