免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1677 | 回复: 0
打印 上一主题 下一主题

net-snmp [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-20 13:24 |只看该作者 |倒序浏览

The following is a very simple example taken from the Net-SNMP site. It initializes a session, adds a MIB in the current working directory to the MIB tree, creates a GET PDU, packs 2 OIDs into the PDU, then sends a synchronous request and returns the response and is finally processed.
#include
#include
#include
int main(int argc, char ** argv)
{
  struct snmp_session session;
  struct snmp_session *sess_handle;
  struct snmp_pdu *pdu;                  
  struct snmp_pdu *response;
  struct variable_list *vars;            
  oid id_oid[MAX_OID_LEN];
  oid serial_oid[MAX_OID_LEN];
  size_t id_len = MAX_OID_LEN;
  size_t serial_len = MAX_OID_LEN;
  int status;                             
  struct tree * mib_tree;
       
  /*********************/
  if(argv[1] == NULL){
        printf("Please supply a hostname\n");
        exit(1);
  }
  init_snmp("APC Check");
  snmp_sess_init( &session );
   session.version = SNMP_VERSION_1;
   session.community = "public";
   session.community_len = strlen(session.community);
   session.peername = argv[1];
  sess_handle = snmp_open(&session);
  add_mibdir(".");
  mib_tree = read_mib("PowerNet-MIB.txt");
  pdu = snmp_pdu_create(SNMP_MSG_GET);
  read_objid("PowerNet-MIB::upsBasicIdentModel.0", id_oid, &id_len);
   snmp_add_null_var(pdu, id_oid, id_len);
  read_objid("PowerNet-MIB::upsAdvIdentSerialNumber.0", serial_oid, &serial_len);
   snmp_add_null_var(pdu, serial_oid, serial_len);
        
  status = snmp_synch_response(sess_handle, pdu, &response);
        
  for(vars = response->variables; vars; vars = vars->next_variable)
        print_value(vars->name, vars->name_length, vars);
  snmp_free_pdu(response);
  snmp_close(sess_handle);
        
  return (0);
}
This example can be compiled like this and run like this:
$ gcc `net-snmp-config --cflags` `net-snmp-config --libs` \
> `net-snmp-config --external-libs` snmp_test.c -o snmp_test
$ ./snmp_test apc
STRING: "Silcon DP340E"
STRING: "SE00XXXXXX   "
$
You can see in the code that init_snmp() gets the ball rolling. snmp_sess_init() accepts a session structure address, this function will populate the bulk of the session structure with default values. Once the session structure is primed we can modify it to meet our needs, such as defining the SNMP version, community name (not forgetting the length of the community name), and the hostname of agent referred to as the peername. When the session structure is ready for use, we can open the session with snmp_open() which will return a new session structure as a handle.
While you normally should refrain from using non-installed MIBs, in this example I've demonstrated how to add a directory to the internal MIB path and then read a MIB into the MIB tree using the add_mibdir() and read_mib() functions.
Now that the session is open and the MIBs we'll use are loaded into the tree we can create and populate the PDU(s) we'll be sending. snmp_pdu_create() will setup the base PDU information based on the supplied type of PDU (specified in macro form) and return the populated PDU structure. Now that we have the PDU we can use the read_objid() and snmp_add_null_var() functions to first read the OID from the MIB and then add that OID as a variable to the variable list used by the PDU. We can repeat this process several times to continue packing our PDU full of OIDs. Typically you'll see this type of operation wrapped in a loop, but I've illustrated it long hand for simplicity sake.
Once our session is open and our PDU is ready with all the OIDs we want crammed into the variable list we can actually send the request and await the response. This action is done in a single step using the snmp_synch_response() function. The function is passed the open session handle, the PDU to send, and an empty PDU structure to accept the response which includes the populated values for each OID in the variable list.
At this point you can extract and manipulate the returned data either by utilizing built in functions such as print_value() or by simply directly accessing the structures. Other convince functions for value output can be found in the net-snmp/library/mib.h header.
Finally, to properly clean up, the PDU(s) should be freed and the sessions closed using the snmp_free_pdu() and snmp_close() functions.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/56374/showart_2154689.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP