Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347561
  • 博文数量: 100
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 521
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-31 11:37
个人简介

活到老,学到老

文章分类

全部博文(100)

文章存档

2018年(1)

2017年(2)

2016年(11)

2015年(82)

2014年(4)

我的朋友

分类: LINUX

2015-07-01 13:47:45

.c和.h生成,上一篇总结文章里面已经说了,也是使用mib2c同样的方法。
这里只贴出来.c的模板代码,基本可以实现set操作。具体到每一个case处理,我还没有去深入的研究。

点击(此处)折叠或打开

  1. #include <net-snmp/net-snmp-config.h>
  2. #include <net-snmp/net-snmp-includes.h>
  3. #include <net-snmp/agent/net-snmp-agent-includes.h>
  4. #include "confNewVersion.h"
  5. #include<stdlib.h>
  6. /** Initializes the confNewVersion module */
  7. void
  8. init_confNewVersion(void)
  9. {
  10.     const oid confNewVersion_oid[] = { 1,3,6,1,4,1,201566,1,6,3 };

  11.   DEBUGMSGTL(("confNewVersion", "Initializing\n"));

  12.     netsnmp_register_scalar(
  13.         netsnmp_create_handler_registration("confNewVersion", handle_confNewVersion,
  14.                                confNewVersion_oid, OID_LENGTH(confNewVersion_oid),
  15.                                HANDLER_CAN_RWRITE
  16.         ));
  17. }
  18. static char buff[256]="beijing";
  19. /*void init_baseinfo(void)
  20. {
  21.     memset(buff,'\0',sizeof(buff));
  22.     memcpy(buff,"beijing",sizeof(buff));
  23. }*/
  24. int
  25. handle_confNewVersion(netsnmp_mib_handler *handler,
  26.                           netsnmp_handler_registration *reginfo,
  27.                           netsnmp_agent_request_info *reqinfo,
  28.                           netsnmp_request_info *requests)
  29. {
  30.     int ret;
  31.     char cmd[50];
  32.     /* We are never called for a GETNEXT if it's registered as a
  33.        "instance", as it's "magically" handled for us. */

  34.     /* a instance handler also only hands us one request at a time, so
  35.        we don't need to loop over a list of requests; we'll only get one. */
  36.     
  37.     switch(reqinfo->mode) {

  38.         case MODE_GET:
  39.             //1
  40.             snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
  41.                                     buff /* XXX: a pointer to the scalar's data */,
  42.                                      strlen(buff)/* XXX: the length of the data in bytes */);
  43.             break;

  44.         /*
  45.          * SET REQUEST
  46.          *
  47.          * multiple states in the transaction. See:
  48.          * http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg
  49.          */
  50.         case MODE_SET_RESERVE1:
  51.             //1
  52.             /* or you could use netsnmp_check_vb_type_and_size instead */
  53.             ret = netsnmp_check_vb_type(requests->requestvb, ASN_OCTET_STR);
  54.             if ( ret != SNMP_ERR_NOERROR ) {
  55.                 netsnmp_set_request_error(reqinfo, requests, ret );
  56.             }
  57.             break;

  58.         case MODE_SET_RESERVE2:
  59.             //1
  60.             /* XXX malloc "undo" storage buffer */
  61.             if (0/* XXX if malloc, or whatever, failed: */) {
  62.                 netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_RESOURCEUNAVAILABLE);
  63.             }
  64.             break;

  65.         case MODE_SET_FREE:
  66.             //0
  67.             /* XXX: free resources allocated in RESERVE1 and/or
  68.                RESERVE2. Something failed somewhere, and the states
  69.                below won't be called. */
  70.             break;

  71.         case MODE_SET_ACTION:
  72.             /* XXX: perform the value change here */
  73.             //1
  74.             if (0/* XXX: error? */) {
  75.                 netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_COMMITFAILED/* some error */);
  76.             }
  77.             break;

  78.         case MODE_SET_COMMIT:
  79.             /* XXX: delete temporary storage */
  80.             memcpy(buff,requests->requestvb->buf,requests->requestvb->val_len);
  81.             buff[requests->requestvb->val_len] ='\0'; 
  82.             //1
  83.             if (0/* XXX: error? */) {
  84.                 /* try _really_really_ hard to never get to this point */
  85.                 netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_COMMITFAILED);
  86.             }
  87.             break;

  88.         case MODE_SET_UNDO:
  89.             /* XXX: UNDO and return to previous value for the object */
  90.             if (0/* XXX: error? */) {
  91.                 /* try _really_really_ hard to never get to this point */
  92.                 netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_UNDOFAILED);
  93.             }
  94.             break;

  95.         default:
  96.             /* we should never get here, so this is a really bad error */
  97.             snmp_log(LOG_ERR, "unknown mode (%d) in handle_confNewVersion\n", reqinfo->mode );
  98.             return SNMP_ERR_GENERR;
  99.     }

  100.     return SNMP_ERR_NOERROR;
  101. }
代码同样的方法去编译运行。
通过命令:snmpset -c private -v 2c localhost confNewVersion.0 s hebei 去测试。
阅读(4542) | 评论(0) | 转发(0) |
0

上一篇:linux 下snmp的扩展

下一篇:linux snmptrap

给主人留下些什么吧!~~