现在我需要通过SNMP协议来获得一台UPS的电量等信息,我已经配置好到了UPS的IP地址等。也下载到了APC UPS相应的MIB文件,现在我不知道怎么编程来获得UPS信息,APC的资料说明是要通过SNMP协议。 大虾帮帮忙,说说怎么做,这是我们整个项目的一部分,所以最好使用C++(纯c++),我找到了snmp++开源库,但还不知道怎么去用

解决方案 »

  1.   

    1、在MIB中找到你要查询的信息的ID;
    2、在SNMP++库中找到查询ID的接口;
    3、调用接口查询
      

  2.   

    参考资料:http://www.7880.com/Info/Article-52135a40.html
      

  3.   

    有了snmp++,可以查看它里面自带的example code,有get的,有main函数,应该可以直接给你合入。
    get的方法就是要你的OID,你需要查看mib文件,找到你对应信息的OID值,这样就可以get 对应OID,然后得到返回数据,自己处理
      

  4.   

    To   compile   your   SNMP++   based   application   in   VC,   please   consult   the   following   rules:   
        
      Header   files:   
        
      (这里是SNMP++的include目录的位置)   
      #include   "C:\System   Backup\D$\Snmp\Agent++\snmp++\include\snmp_pp.h"   
      #include   "C:\System   Backup\D$\Snmp\Agent++\snmp++\include\collect.h"   
      #include   "C:\System   Backup\D$\Snmp\Agent++\snmp++\include\notifyqueue.h"   
        
      Linked   Libs:   
        
      snmpPP.lib   deslib.lib   agentpp.lib   ws2_32.lib   
        
      Compiler   options:   
        
      /nologo   /MLd   /W3   /Gm   /GX   /ZI   /Od   /I   "C:\System"   /I   "C:\System   Backup\D$\Snmp\Agent++\snmp++\include"(这里是你自己的SNMP++的include目录的位置)   /D   "WIN32"   /D   "_DEBUG"   /D   "_CONSOLE"   /D   "_MBCS"   /Fp"Debug/trap_recv.pch"   /YX   /Fo"Debug/"   /Fd"Debug/"   /FD   /GZ   
      

  5.   

    用snmp++发送get请求示例          
        
      #include   “snmp_pp.h”   
      #define   SYSDESCR   “1.3.6.1.2.1.1.1.0”     //   Object   ID   for   System   Descriptor   
        
      void   get_system_descriptor()   
      {   
          int   status;                 //   return   status      
          CTarget   ctarget(   (IpAddress)   “10.4.8.5”);//   SNMP++   v1   target   
          Vb   vb(   SYSDESCR);             //   SNMP++   Variable   Binding   
          Pdu   pdu;                    //   SNMP++   PDU   
        
        //-------[   Construct   a   SNMP++   SNMP   Object   ]---------------------------   
          Snmp   snmp(   status);            //   Create   a   SNMP++   session   
          if   (   status   !=   SNMP_CLASS_SUCCESS)     
        
              {   //   check   creation   status   
         cout   <<   snmp.error_msg(   status);    // if   fail,   print   error   string   
         return;    
        
              }   
        
          //-------[   Invoke   a   SNMP++   Get   ]---------------------------------------   
          pdu   +=   vb;                   // add   the   variable   binding   
          if   ( (status   =   snmp.get( pdu,   ctarget))   !=   SNMP_CLASS_SUCCESS)   
         cout   <<   snmp.error_msg(   status);   
          else     
        
              {   
         pdu.get_vb(   vb,0);           //   extract   the   variable   binding   
         cout   <<   “System   Descriptor   =   ”<<   vb.get_printable_value();    
        
              } //   print   out   
        
      };    
      

  6.   

    用snmp++发送get请求示例    #include   “snmp_pp.h”   
      #define   SYSDESCR   “1.3.6.1.2.1.1.1.0” //Object ID for System Descriptor   
        
      void   get_system_descriptor()   
      {   
          int   status;          //   return status      
          CTarget ctarget((IpAddress)“10.4.8.5”);//SNMP++ v1 target   
          Vb vb(SYSDESCR);         //   SNMP++ Variable Binding   
          Pdu   pdu;            //   SNMP++   PDU   
        
        //-------[   Construct a SNMP++ SNMP Object   ]---------------------------   
          Snmp   snmp(   status);         //   Create a SNMP++ session   
          if(status != SNMP_CLASS_SUCCESS)     
          {   
              //   check   creation   status   
           cout << snmp.error_msg(status); // if fail,   print   error   string   
               return;    
           }   
          //-------[   Invoke a SNMP++ Get   ]--------------------------------------- 
          pdu += vb;           // add   the   variable   binding   
          if((status = snmp.get(pdu,ctarget)) != SNMP_CLASS_SUCCESS)   
             cout << snmp.error_msg(status);   
          else     
          {   
            pdu.get_vb(vb,0);  //   extract   the   variable   binding   
            cout<<“System Descriptor = ”<<   vb.get_printable_value();    
        
            } //   print   out   
        
      };    
      

  7.   

    首先谢谢楼上几位的热情回答,现在我们另外一个人告诉我,说用net-snmp这个库比较好,但是他有不能说出来好在哪里。我只有明天一天的时间来做这个,希望大虾继续赐教下(当然我也在网上查找信息,可惜没看到么有价值的信息),我到底是net-snmp还是用agent++ snmp++,那个对我的开发从难易度,方便等方面,毕竟只有明天一天的时间。再次谢谢