Implementing SNMP SendTrap using Indy components

你说的曾经没有我的故事 提交于 2019-12-02 09:31:31

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!