使用Indy组件实施SNMP SendTrap [英] Implementing SNMP SendTrap using Indy components

查看:370
本文介绍了使用Indy组件实施SNMP SendTrap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过SNMP在C ++ Builder上报告我的应用程序中的错误.

I need to report errors from my application on C++Builder via SNMP.

我开始使用Indy组件实现SNMP SendTrap.

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;
}

但是,当我运行应用程序时,我的系统中没有udp活动.当我运行这样的东西

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显示192.168.100.21 192.168.100.19 SNMP 82 get-request 1.3.6.1.2.1.1.3.0

wireshark shows 192.168.100.21 192.168.100.19 SNMP 82 get-request 1.3.6.1.2.1.1.3.0

但是当idSnmp->SendTrap() wireshark没有看到任何内容时(两种情况下,wireshark的过滤器都是UDP端口范围161-162)

but when idSnmp->SendTrap() wireshark sees nothing (filter for wireshark is UDP portrange 161-162 in both cases)

很高兴看到有关我的代码或SendTrap的工作示例的一些评论:)

I'll be glad to see some remarks about my code or maybe working example of SendTrap :)

推荐答案

您没有在TIdSNMP::Trap中填充任何值.这就是为什么TIdSNMP::SendTrap()没有发送任何内容的原因.没有东西可以发送.

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.

尝试以下方法:

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;
}

或者,您可以改用TIdSNMP::QuickSendTrap():

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;
}

这篇关于使用Indy组件实施SNMP SendTrap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆