snmp++操作之trap

snmp六种操作简述

Get(获取操作):

Get 操作用于从管理设备向代理设备获取指定 OID(对象标识符)的值。
管理设备发送 Get 报文包含要获取的 OID 列表,代理设备则根据这些 OID 返回对应的值。

Get Next(获取下一个操作):

Get Next 操作用于从代理设备获取比指定 OID 更大的第一个 OID 的值。
管理设备发送 Get Next 报文包含一个或多个 OID,代理设备则返回比这些 OID 大的第一个 OID 的值。

Get Bulk(批量获取操作):

Get Bulk 操作允许一次获取多个 OID 的值,减少交互次数,提高效率。
管理设备发送 Get Bulk 报文指定起始 OID、请求的个数等参数,代理设备返回相应的值。

Set(设置操作):

Set 操作用于向代理设备设定指定 OID 的值,用于配置设备的参数。
管理设备发送 Set 报文包含要设置的 OID 和对应的值,代理设备根据指定的 OID 进行配置。

Inform(通知操作):

Inform 操作用于向管理设备发送一个通知,通知管理设备某些事件的发生。
代理设备主动发送 Inform 报文给指定的管理设备,通知特定的事件或状态。

Trap(陷阱操作):

Trap 操作用于代理设备向管理设备发送一个告警或事件通知,通知管理设备发生了某些重要的事件。
代理设备可以主动发送 Trap 报文给一个或多个管理设备,告知管理设备设备的状态或发生的事件。
这些操作是 SNMP 协议中常用的几种,用于实现设备间的监控、通知和配置等功能。通过这些操作,网络管理员可以监控网络设备的状态、配置参数、响应事件等,帮助及时发现和解决网络问题

trap操作

1.管理程序开启监听
2.代理程序去对某个oid发起trap操作
3.管理程序监听到trap,并打印

代理程序

#define COLDSTART "1.3.6.1.6.3.1.1.5.1"
#define PAYLOADID "1.3.6.1.4.1.11.2.16.2"
#define PAYLOAD "SNMP++ Trap Send Test"
#define ZYXID "1.3.6.1.4.1.11.2.16.3"
#define ZYX "zyx is so great"

#define ENTERPRISE "1.3.6.1.2.1.1.1.2.0.1"

int main(int argc, char **argv)
{
   Snmp::socket_startup();  // Initialize socket subsystem

   //---------[ make a GenAddress and Oid object to retrieve ]---------------
   UdpAddress address( "127.0.0.1");      // make a SNMP++ Generic address
   if ( !address.valid())
   {           // check validity of address
      cout << "Invalid Address or DNS Name, " << argv[1] << "\n";
   }
   Oid oid( COLDSTART);    // default is ColdStart

   //---------[ determine options to use ]-----------------------------------
   snmp_version version=version2c;                  // default is v2
   u_short port=161;                               // default snmp port is 161
   OctetStr community("public");                   // community name
   Oid ent(ENTERPRISE);                            // default enterprise

   //----------[ create a SNMP++ session ]-----------------------------------
   int status;
   Snmp *snmp = new Snmp(status, "127.0.0.1");

   if ( status != SNMP_CLASS_SUCCESS)
   {
      cout << "SNMP++ Session Create Fail, " << snmp->error_msg(status) << "\n";
      return 1;
   }

   //--------[ build up SNMP++ object needed ]-------------------------------
   Pdu pdu;                               // construct a Pdu object
   Vb vb;                                 // variable binding object to use
   vb.set_oid(PAYLOADID);                 // example oid for trap payload
   vb.set_value(PAYLOAD);                 // example string for payload
   pdu += vb;                              // append the vb to the pdu
   Vb vb2;
   vb2.set_oid(ZYXID);
   vb2.set_value(ZYX);
   pdu += vb2;
   pdu.set_notify_id( oid);               // set the id of the trap
   pdu.set_notify_enterprise( ent);       // set up the enterprise of the trap
   address.set_port(port);
   CTarget ctarget( address);             // make a target using the address

    ctarget.set_version( version);         // set the SNMP version SNMPV1 or V2
    ctarget.set_readcommunity( community); // set the read community name

   //-------[ Send the trap  ]------------------------------------------------
   cout << "SNMP++ Trap to " ;

   cout << " Community=" << community.get_printable() << endl << flush;

   SnmpTarget *target;

    target = &ctarget;

   status = snmp->trap( pdu,*target);
   cout << "SNMP++ Trap Send Status = " << snmp->error_msg( status) << "\n";

  Snmp::socket_cleanup();  // Shut down socket subsystem
}

管理程序

#ifdef SNMP_PP_NAMESPACE
using namespace Snmp_pp;
#endif
using namespace std;


void callback( int reason, Snmp *snmp, Pdu &pdu, SnmpTarget &target, void *cd)
{

    cout<<"callback"<<endl;
    Vb nextVb;
    int pdu_error;
    GenAddress addr;

    target.get_address(addr);
    UdpAddress from(addr);

    if(reason != -7)
    {
        cout << "reason: " << reason << " "<< "msg: " << snmp->error_msg(reason) << " "<< "from: " << from.get_printable() << " ";
    }
    else
    {
         cout << "from: " << from.get_printable() << " ";
    }
    pdu_error = pdu.get_error_status();
    if (pdu_error)
    {
        cout << "Response contains error: "<< snmp->error_msg(pdu_error)<< endl;
    }
    Oid id;
    pdu.get_notify_id(id);
    cout << "ID:  " << id.get_printable() <<endl;
    for (int i=0; i<pdu.get_vb_count(); i++)
    {
        pdu.get_vb(nextVb, i);
        cout << "Oid: " << nextVb.get_printable_oid() << ": "<< "Val: " << nextVb.get_printable_value() << " "<<endl;
    }
    if ( pdu.get_type() == sNMP_PDU_INFORM)//当请求是inform时开始回应
    {
        cout << "pdu type: " << pdu.get_type() << endl;
        cout << "sending response " << endl;
        nextVb.set_value("This is the response.");
        pdu.set_vb(nextVb, 0);
       // sleep(3);
       int status =snmp->response(pdu, target);
       if ( status == SNMP_CLASS_SUCCESS)
       {
            cout<<"success to response";
       }
       else
       {
           cout << "fail to reponse:" << snmp->error_msg(status) << "\n";
       }

    }
    cout <<"--------------------------------------------------" <<endl;
}

int main(int argc, char **argv)
{
  int trap_port= 161; // no need to be root

  //----------[ create a SNMP++ session ]-----------------------------------
  int status;
  Snmp::socket_startup();  // Initialize socket subsystem
  Snmp snmp(status);                // check construction status
  if ( status != SNMP_CLASS_SUCCESS)
  {
    cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
    return 1;
  }

   OidCollection oidc;
   TargetCollection targetc;

   cout << "Trying to register for traps on port " << trap_port << "." << endl;
   snmp.notify_set_listen_port(trap_port);//设置监听端口
   status = snmp.notify_register(oidc, targetc, callback, NULL);//注册回调函数,当有trap到来时会触发回调函数
   if (status != SNMP_CLASS_SUCCESS)
   {
     cout << "Error register for notify (" << status << "): "<< snmp.error_msg(status) << endl;
     exit(1);
   }
   else
   {
       cout << "Waiting for traps/informs..." << endl;
   }
  snmp.start_poll_thread(1000);

  cout << "press return to stop\n";
  getc(stdin);

  snmp.stop_poll_thread();

  Snmp::socket_cleanup();  // Shut down socket subsystem
}

代理程序运行截图
在这里插入图片描述
管理程序运行截图
在这里插入图片描述
管理程序先启动,开始监听。出现红色框打印字符。当代理程序运行的时候,出现绿色框打印字符

注意

在trap操作中,代理程序发起trap操作。管理程序只需要接受这个操作就行了。是不需要回复代理程序的。如果需要管理程序回复,那个操作应该时inform。

  • 18
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值