【转】与大家分享uclinux上的ucd-snmp开发过程

http://linux.chinaunix.net/bbs/viewthread.php?tid=903536

最近在做arm上的网关系统,现将uclinux上的ucd-snmp开发过程与大家分享,希望大侠们多多指教!
由于上传文件大小限制,不能上传完整的doc文件,需要的可以和我联系。
_________________________________________________________________

一、开发环境:

1.  操作系统:uClinux-ifax-2007-03-16.tar.gz

2.  交叉编译工具:arm-elf-tools-20030314.sh

3.  硬件平台:Samsung 4510B (snds-100)

二、编译ucdsnmp主要步骤

1.配置uClinux
修改config/config.in文件,确保改文件中有如下内容:

配置用户程序,选中ucd-snmp

2.使用snmpconf工具生成配置文件,内容如下:

  1. ###########################################################################
  2. #
  3. # snmpd.conf
  4. #
  5. #   - created by the snmpconf configuration program
  6. #
  7. ###########################################################################
  8. # SECTION: System Information Setup
  9. #
  10. #   This section defines some of the information reported in
  11. #   the "system" mib group in the mibII tree.

  12. # syslocation: The [typically physical] location of the system.
  13. #   Note that setting this value here means that when trying to
  14. #   perform an snmp SET operation to the sysLocation.0 variable will make
  15. #   the agent return the "notWritable" error code.  IE, including
  16. #   this token in the snmpd.conf file will disable write access to
  17. #   the variable.
  18. #   arguments:  location_string

  19. syslocation  xi'an

  20. # syscontact: The contact information for the administrator
  21. #   Note that setting this value here means that when trying to
  22. #   perform an snmp SET operation to the sysContact.0 variable will make
  23. #   the agent return the "notWritable" error code.  IE, including
  24. #   this token in the snmpd.conf file will disable write access to
  25. #   the variable.
  26. #   arguments:  contact_string

  27. syscontact  [email]lazy_linux@126.com[/email]

  28. # sysservices: The proper value for the sysServices object.
  29. #   arguments:  sysservices_number

  30. sysservices 77
  31. sysservices 0*1 + 0*2 + 0*4 + 0*8 + 0*64
  32. sysservices 0*1 + 0*2 + 1*4 + 1*8 + 0*64



  33. ###########################################################################
  34. # SECTION: Access Control Setup
  35. #
  36. #   This section defines who is allowed to talk to your running
  37. #   snmp agent.

  38. # rwuser: a SNMPv3 read-write user
  39. #   arguments:  user [noauth|auth|priv] [restriction_oid]

  40. rwuser  rw3 noauth 

  41. # rwcommunity: a SNMPv1/SNMPv2c read-write access community name
  42. #   arguments:  community [default|hostname|network/bits] [oid]

  43. rwcommunity  rw12  



  44. ###########################################################################
  45. # SECTION: Trap Destinations
  46. #
  47. #   Here we define who the agent will send traps to.

  48. # trap2sink: A SNMPv2c trap receiver
  49. #   arguments: host [community] [portnum]

  50. trap2sink  202.117.129.75  
复制代码



3.编译生成image.rom并下载到目标板上,运行如下:

4.在开发主机上进行验证


三、扩展ucdsnmp主要步骤

1.主要修改的文件

MIB文件:定义扩展的MIB库

编译的头文件:需要修改头文件,使得编译时包括用户扩展的功能

.c文件:实现SNMP get,set,trap等功能

.h文件:头文件

2.MIB文件

/uClinux-dist/user/ucdsnmp/mibs/UCD-DEMO-MIB.txt

其中定义了用户扩展的agent管理节点

  1. UCD-DEMO-MIB DEFINITIONS ::= BEGIN

  2. IMPORTS
  3.     MODULE-IDENTITY, OBJECT-TYPE, Integer32 FROM SNMPv2-SMI
  4.     ucdavis                                          FROM UCD-SNMP-MIB;

  5. ucdDemoMIB MODULE-IDENTITY
  6.     LAST-UPDATED "9912090000Z"
  7.     ORGANIZATION "University of California, Davis"
  8.     CONTACT-INFO
  9.          "The ucd-snmp-coders mailing list is the best place to 
  10.           write for public questions about the ucd-snmp
  11.           project: [email]ucd-snmp-coders@ucd-snmp.ucdavis.edu[/email]

  12.           Primary Author: Wes Hardaker

  13.           postal:     IT-DCAS
  14.                      UCDavis
  15.                       Davis CA  95616
  16.          phone:      +1 530 754-7571
  17.          email:      [email]wjhardaker@ucdavis.edu[/email]"
  18.     DESCRIPTION
  19.          "The UCD-SNMP Demonstration MIB."
  20.     REVISION       "9912090000Z"
  21.     DESCRIPTION
  22.          "SMIv2 version converted from older MIB definitions."
  23. ::= { ucdavis 14 }
  24. //该节点位于ucdavis下的第14个节点

  25. ucdDemoMIBObjects OBJECT IDENTIFIER ::= { ucdDemoMIB 1 }
  26. //在UCD-SNMP-MIB中定义,
  27. // --   ucdDemoMIB       OBJECT IDENTIFIER ::= { ucdavis  14 } - UCD-DEMO-MIB
  28. //OID: ucdavis.14.1

  29. ucdDemoPublic OBJECT IDENTIFIER ::= { ucdDemoMIBObjects 1 }
  30. //OID: ucdavis.14.1.1

  31. ucdDemoResetKeys OBJECT-TYPE
  32.     SYNTAX        Integer32 (0..2147483647)
  33.     MAX-ACCESS    read-write
  34.     STATUS        current
  35.     DESCRIPTION
  36.          "A set of value 1 to this object resets the
  37.           demonstration user's auth and priv keys to the
  38.           keys based on the P->Ku->Kul transformation of the
  39.           value of the ucdDemoPasspharse object.

  40.           Values other than 1 are ignored."
  41. ::= { ucdDemoPublic 1 }
  42. //OID: ucdavis.14.1.1

  43. ucdDemoPublicString OBJECT-TYPE
  44.     SYNTAX        OCTET STRING (SIZE(0..1024))
  45.     MAX-ACCESS    read-write
  46.     STATUS        current
  47.     DESCRIPTION
  48.          "A publicly settable string that can be set for testing 
  49.           snmpsets.  This value has no real usage other than
  50.           testing purposes."
  51.     ::= { ucdDemoPublic 2 }

  52. ucdDemoUserList OBJECT-TYPE
  53.     SYNTAX        OCTET STRING
  54.     MAX-ACCESS    read-only
  55.     STATUS        current
  56.     DESCRIPTION
  57.          "The list of users affected by the ucdDemoResetKeys object."
  58.     ::= { ucdDemoPublic 3 }

  59. ucdDemoPassphrase  OBJECT-TYPE
  60.     SYNTAX        OCTET STRING
  61.     MAX-ACCESS    read-only
  62.     STATUS        current
  63.     DESCRIPTION
  64.          "The demo passphrase that ucdDemoResetKeys changes each 
  65.           users localized key to based on the P->Ku->Kul transformation."
  66.     ::= { ucdDemoPublic 4 }

  67. //定义了2个trap类型的节点
  68. TheTrap1 NOTIFICATION-TYPE
  69.          STATUS current
  70.          DESCRIPTION
  71.                  "This is 1st trap."
  72.          ::= { ucdDemoPublic 5 }
  73. //OID: ucdavis.14.1.5


  74. TheTrap2 NOTIFICATION-TYPE
  75.          SYNTAX Integer32
  76.          MAX-ACCESS       read-write
  77.          STATUS   mandatory
  78.          DESCRIPTION
  79.                  "This is 2nd trap."
  80.          ::= { ucdDemoPublic 6 }
  81. //OID: ucdavis.14.1.6

  82. END
复制代码




2.编译的头文件

/uClinux-dist/user/ucdsnmp/agent/Makefile

在MIBOBJS中增加需要编译的c文件的目标文件名称:

  1. MIBOBJS         =   mibgroup/mibII/system_mib.o  mibgroup/misc/ipfwacc.o mibgroup/mibII/sysORTable.o  mibgroup/mibII/at.o  mibgroup/mibII/interfaces.o  mibgroup/mibII/snmp_mib.o  mibgroup/mibII/tcp.o  mibgroup/mibII/icmp.o  mibgroup/mibII/ip.o  mibgroup/mibII/udp.o  mibgroup/mibII/vacm_vars.o  mibgroup/ucd-snmp/memory.o  mibgroup/ucd-snmp/proc.o  mibgroup/ucd-snmp/versioninfo.o  mibgroup/ucd-snmp/pass.o  mibgroup/ucd-snmp/pass_persist.o  mibgroup/ucd-snmp/disk.o  mibgroup/ucd-snmp/loadave.o  mibgroup/ucd-snmp/extensible.o  mibgroup/ucd-snmp/errormib.o  mibgroup/ucd-snmp/registry.o  mibgroup/ucd-snmp/file.o  mibgroup/snmpv3/snmpEngine.o  mibgroup/snmpv3/snmpMPDStats.o  mibgroup/snmpv3/usmStats.o  mibgroup/snmpv3/usmUser.o  mibgroup/util_funcs.o  mibgroup/mibII/var_route.o  mibgroup/mibII/route_write.o    mib_modules.o mibgroup/examples/ucdDemoPublic.o

  2. /uClinux-dist/user/ucdsnmp/agent/mibgroup/Makefile
复制代码



OBJS和SRCS中增加目标文件名称和c文件名称

  1. OBJS =  misc/ipfwacc.o mibII/system_mib.o mibII/sysORTable.o mibII/at.o mibII/interfaces.o mibII/snmp_mib.o mibII/tcp.o mibII/icmp.o mibII/ip.o mibII/udp.o mibII/vacm_vars.o ucd-snmp/memory.o ucd-snmp/proc.o ucd-snmp/versioninfo.o ucd-snmp/pass.o ucd-snmp/pass_persist.o ucd-snmp/disk.o ucd-snmp/loadave.o ucd-snmp/extensible.o ucd-snmp/errormib.o ucd-snmp/registry.o ucd-snmp/file.o snmpv3/snmpEngine.o snmpv3/snmpMPDStats.o snmpv3/usmStats.o snmpv3/usmUser.o util_funcs.o mibII/var_route.o mibII/route_write.o examples/ucdDemoPublic.o

  2. SRCS =  misc/ipfwacc.c mibII/system_mib.c mibII/sysORTable.c mibII/at.c mibII/interfaces.c mibII/snmp_mib.c mibII/tcp.c mibII/icmp.c mibII/ip.c mibII/udp.c mibII/vacm_vars.c ucd-snmp/memory.c ucd-snmp/proc.c ucd-snmp/versioninfo.c ucd-snmp/pass.c ucd-snmp/pass_persist.c ucd-snmp/disk.c ucd-snmp/loadave.c ucd-snmp/extensible.c ucd-snmp/errormib.c ucd-snmp/registry.c ucd-snmp/file.c snmpv3/snmpEngine.c snmpv3/snmpMPDStats.c snmpv3/usmStats.c snmpv3/usmUser.c util_funcs.c mibII/var_route.c mibII/route_write.c examples/ucdDemoPublic.c
复制代码






/uClinux-dist/user/ucdsnmp/agent/mibgroup/mib_module_inits.h

中增加自定义的初始化函数名称

  1. /* This file is automatically generated by configure.  Do not modify by hand. */
  2.   init_system_mib();
  3.   init_sysORTable();
  4.   init_at();
  5.   init_interfaces();
  6.   init_snmp_mib();
  7.   init_tcp();
  8.   init_icmp();
  9.   init_ip();
  10.   init_udp();
  11.   init_vacm_vars();
  12.   init_memory();
  13.   init_proc();
  14.   init_versioninfo();
  15.   init_pass();
  16.   init_pass_persist();
  17.   init_disk();
  18.   init_loadave();
  19.   init_extensible();
  20.   init_errormib();
  21.   init_registry();
  22.   init_file();
  23.   init_snmpEngine();
  24.   init_snmpMPDStats();
  25.   init_usmStats();
  26.   init_usmUser();
  27.   init_var_route();
  28.   init_ipfwacc();
  29.   init_ucdDemoPublic();
复制代码





/uClinux-dist/user/ucdsnmp/agent/mibgroup/mib_module_includes.h

中增加自定义的头文件名称

  1. /* This file is automatically generated by configure.  Do not modify by hand. */
  2. #include "mibgroup/mibII.h"
  3. #include "mibgroup/ucd_snmp.h"
  4. #include "mibgroup/snmpv3mibs.h"
  5. #include "mibgroup/mibII/system_mib.h"
  6. #include "mibgroup/mibII/sysORTable.h"
  7. #include "mibgroup/mibII/at.h"
  8. #include "mibgroup/mibII/interfaces.h"
  9. #include "mibgroup/mibII/snmp_mib.h"
  10. #include "mibgroup/mibII/tcp.h"
  11. #include "mibgroup/mibII/icmp.h"
  12. #include "mibgroup/mibII/ip.h"
  13. #include "mibgroup/mibII/udp.h"
  14. #include "mibgroup/mibII/vacm_vars.h"
  15. #include "mibgroup/ucd-snmp/memory.h"
  16. #include "mibgroup/ucd-snmp/proc.h"
  17. #include "mibgroup/ucd-snmp/versioninfo.h"
  18. #include "mibgroup/ucd-snmp/pass.h"
  19. #include "mibgroup/ucd-snmp/pass_persist.h"
  20. #include "mibgroup/ucd-snmp/disk.h"
  21. #include "mibgroup/ucd-snmp/loadave.h"
  22. #include "mibgroup/ucd-snmp/extensible.h"
  23. #include "mibgroup/ucd-snmp/errormib.h"
  24. #include "mibgroup/ucd-snmp/registry.h"
  25. #include "mibgroup/ucd-snmp/file.h"
  26. #include "mibgroup/snmpv3/snmpEngine.h"
  27. #include "mibgroup/snmpv3/snmpMPDStats.h"
  28. #include "mibgroup/snmpv3/usmStats.h"
  29. #include "mibgroup/snmpv3/usmUser.h"
  30. #include "mibgroup/util_funcs.h"
  31. #include "mibgroup/mibII/var_route.h"
  32. #include "mibgroup/mibII/route_write.h"
  33. #include "mibgroup/misc/ipfwacc.h"
  34. #include "mibgroup/examples/ucdDemoPublic.h"
复制代码



3..c文件:实现SNMP get,set,trap等功能

/uClinux-dist/user/ucdsnmp/agent/mibgroup/examples/ucdDemoPublic.c

  1. /* ucdDemoPublic.c */
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>

  7. #include <config.h>

  8. #include "mibincl.h"
  9. #include "snmpv3.h"
  10. #include "snmpusm.h"
  11. #include "agent_read_config.h"
  12. #include "ucdDemoPublic.h"
  13. #include "util_funcs.h"

  14. #define MYMAX 1024
  15. #define MAXUSERS 10

  16. int num=0;
  17. static char demoUsers[MAXUSERS][MYMAX+1];
  18. static char demopass[MYMAX+1];

  19. char mynode_str[300];

  20. void ucdDemo_parse_user(const char *word, char *line) {
  21.   if (num == MAXUSERS)
  22.     return;

  23.   if (strlen(line) > MYMAX)
  24.     return;
  25.   
  26.   strcpy(demoUsers[num++], line);
  27. }


  28. void ucdDemo_parse_userpass(const char *word, char *line) {
  29.   if (strlen(line) > MYMAX)
  30.     return;
  31.   
  32.   strcpy(demopass, line);
  33. }

  34. /* this variable defines function callbacks and type return information 
  35.    for the ucdDemoPublic mib */

  36. //与MIB中的定义相对应
  37. struct variable2 ucdDemoPublic_variables[] = {
  38.   { UCDDEMORESETKEYS    , ASN_INTEGER   , RWRITE, var_ucdDemoPublic, 1, { 1 } },
  39.   { UCDDEMOPUBLICSTRING , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 2 } },
  40.   { UCDDEMOUSERLIST     , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 3 } },
  41.   { UCDDEMOPASSPHRASE   , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 4 } },
  42.   { 5, ASN_INTEGER, RWRITE, var_ucdDemoPublic, 1, {5}},
  43.   { 6, ASN_INTEGER, RWRITE, var_ucdDemoPublic, 1, {6}}
  44. };

  45. /* Define the OID pointer to the top of the mib tree that we're
  46.    registering underneath */
  47. oid ucdDemoPublic_variables_oid[] = { 1,3,6,1,4,1,2021,14,1,1 };

  48. void init_ucdDemoPublic(void) {
  49.   REGISTER_MIB( "examples/ucdDemoPublic", ucdDemoPublic_variables,
  50.                  variable2, ucdDemoPublic_variables_oid);
  51.   snmpd_register_config_handler("demoUser", ucdDemo_parse_user, NULL, "USER");
  52.   snmpd_register_config_handler("demoPass", ucdDemo_parse_userpass, NULL, "PASSPHASE");
  53.   //定义定时的trap发送
  54.   snmp_alarm_register(10, SA_REPEAT, snmploop_callback, NULL);
  55. }

  56. //定义自定义的定时trap动作
  57. void snmploop_callback(unsigned int reg, void *clientarg){
  58.          send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 1);
  59. }


  60. unsigned char publicString[MYMAX+1];
  61. long long_ret;

  62. unsigned char *
  63. var_ucdDemoPublic(
  64.     struct variable *vp,
  65.     oid     *name,
  66.     size_t  *length,
  67.     int     exact,
  68.     size_t  *var_len,
  69.     WriteMethod **write_method)
  70. {
  71. //  static long long_ret;
  72.   static char string[MYMAX+1], *cp;
  73.   int i;
  74.   
  75.   *write_method = 0;           /* assume it isnt writable for the time being */
  76.   *var_len = sizeof(long_ret); /* assume an integer and change later if not */

  77.   if (header_generic(vp,name,length,exact,var_len,write_method))
  78.       return 0;

  79.   /* this is where we do the value assignments for the mib results. */
  80.   switch(vp->magic) {

  81.     case UCDDEMORESETKEYS:
  82.       *write_method = write_ucdDemoResetKeys;
  83.       //long_ret = 0;
  84.       return (unsigned char *) &long_ret;

  85.     case UCDDEMOPUBLICSTRING:
  86.       *write_method = write_ucdDemoPublicString;
  87.       *var_len = strlen(publicString);
  88.       return (unsigned char *) publicString;

  89.     case UCDDEMOUSERLIST:
  90.       cp = string;
  91.       for(i=0; i < num; i++) {
  92.         sprintf(cp, " %s", demoUsers[i]);
  93.         cp = cp + strlen(cp);
  94.       }
  95.       *var_len = strlen(string);
  96.       return (unsigned char *) string;
  97.       
  98.     case UCDDEMOPASSPHRASE:
  99.       *var_len = strlen(demopass);
  100.       return (unsigned char *) demopass;


  101.     case 5:
  102.           long_ret=0;
  103.           *write_method = write_mynodetrap;
  104.           return (u_char *)&long_ret;


  105.    case 6:
  106.         /*
  107.          * This object is essentially "write-only".
  108.          * It only exists to trigger the sending of a v2 trap.
  109.          * Reading it will always return 0.
  110.          */
  111.         long_ret = 0; /*FIX*/
  112.         *write_method = write_mynodetrap2;
  113.         return (u_char *)&long_ret;

  114.       
  115.     default:
  116.       DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ucdDemoPublic\n", vp->magic));
  117.   }
  118.   return 0;
  119. }

  120. int
  121. write_ucdDemoResetKeys(
  122.    int      action,
  123.    u_char   *var_val,
  124.    u_char   var_val_type,
  125.    size_t   var_val_len,
  126.    u_char   *statP,
  127.    oid      *name,
  128.    size_t   name_len)
  129. {
  130.   /* variables we may use later */
  131.   //static long long_ret;
  132.   static unsigned char string[1500];
  133.   static oid objid[MAX_OID_LEN];
  134.   static struct counter64 c64;
  135.   int bigsize=1000;
  136.   unsigned char *engineID;
  137.   int engineIDLen;
  138.   int i;
  139.   struct usmUser *user;

  140.   if (var_val_type != ASN_INTEGER) {
  141.          printf("ASN_INTEGER\n");
  142.       DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoResetKeys not ASN_INTEGER\n"));
  143.       return SNMP_ERR_WRONGTYPE;
  144.   }
  145.   if (var_val_len > sizeof(long_ret)) {
  146.       DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoResetKeys: bad length\n"));
  147.       return SNMP_ERR_WRONGLENGTH;
  148.   }
  149.   if (action == COMMIT) {
  150.          int fd = open("/dev/lamp", O_RDWR, 0644);
  151.          if(fd<0){
  152.                  printf("open /dev/lamp error\n"); 
  153.                  return SNMP_ERR_NOERROR;
  154.          }
  155.          printf("new value: %x\n", *(long*)var_val);

  156.          if( *(long *)var_val == 0) write(fd,"00\n", 3);
  157.          if( *(long *)var_val == 1) write(fd,"01\n", 3);
  158.          if( *(long *)var_val == 2) write(fd,"10\n", 3);
  159.          if( *(long *)var_val == 3) write(fd,"11\n", 3);

  160.          close(fd);
  161.                  
  162.       long_ret = *((long *) var_val);
  163.       /*if (long_ret == 1) {
  164.         engineID = snmpv3_generate_engineID(&engineIDLen);
  165.         for(i=0; i < num; i++) {
  166.           user = usm_get_user(engineID, engineIDLen, demoUsers[i]);
  167.           if (user) {
  168.             usm_set_user_password(user, "userSetAuthPass", demopass);
  169.             usm_set_user_password(user, "userSetPrivPass", demopass);
  170.           }
  171.         }
  172.          reset the keys 
  173.       }*/
  174.   }
  175.   return SNMP_ERR_NOERROR;
  176. }

  177. int
  178. write_ucdDemoPublicString(
  179.    int      action,
  180.    u_char   *var_val,
  181.    u_char   var_val_type,
  182.    size_t   var_val_len,
  183.    u_char   *statP,
  184.    oid      *name,
  185.    size_t   name_len)
  186. {
  187.   if (var_val_type != ASN_OCTET_STR) {
  188.       DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoPublicString not ASN_OCTET_STR\n"));
  189.       return SNMP_ERR_WRONGTYPE;
  190.   }
  191.   if (var_val_len > MYMAX) {
  192.       DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoPublicString: bad length\n"));
  193.       return SNMP_ERR_WRONGLENGTH;
  194.   }
  195.   if (action == COMMIT) {
  196.       strcpy(publicString, var_val);
  197.   }
  198.   return SNMP_ERR_NOERROR;
  199. }



  200. int
  201. write_mynodetrap(int action,
  202.                   u_char * var_val,
  203.                   u_char var_val_type,
  204.                   size_t var_val_len,
  205.                   u_char * statP, oid * name, size_t name_len)
  206. {
  207.     long            intval;

  208.     DEBUGMSGTL(("mynode", "write_mynodetrap entered: action=%d\n",
  209.                 action));
  210.     switch (action) {
  211.     case RESERVE1:   
  212.         /*
  213.          *  The only acceptable value is the integer 1
  214.          */
  215.         if (var_val_type != ASN_INTEGER) {
  216.             DEBUGMSGTL(("mynode", "%x not integer type", var_val_type));
  217.             return SNMP_ERR_WRONGTYPE;
  218.         }
  219.         if (var_val_len > sizeof(long)) {
  220.             DEBUGMSGTL(("mynode", "wrong length %x", var_val_len));
  221.             return SNMP_ERR_WRONGLENGTH;
  222.         }

  223.         intval = *((long *) var_val);
  224.         if (intval != 1) {
  225.             DEBUGMSGTL(("mynode", "wrong value %x", intval));
  226.             return SNMP_ERR_WRONGVALUE;
  227.         }
  228.         break;

  229.     case RESERVE2:
  230.         /*
  231.          * No resources are required.... 
  232.          */
  233.         break;

  234.     case FREE:
  235.         /*
  236.          * ... so no resources need be freed 
  237.          */
  238.         break;

  239.     case ACTION:
  240.         /*
  241.          *  Having triggered the sending of a trap,
  242.          *   it would be impossible to revoke this,
  243.          *   so we can't actually invoke the action here.
  244.          */
  245.         break;

  246.     case UNDO:
  247.         /*
  248.          * We haven't done anything yet,
  249.          * so there's nothing to undo 
  250.          */
  251.         break;

  252.     case COMMIT:
  253.         /*
  254.          *  Everything else worked, so it's now safe
  255.          *   to trigger the trap.
  256.          *  Note that this is *only* acceptable since
  257.          *   the trap sending routines are "failsafe".
  258.          *  (In fact, they can fail, but they return no
  259.          *   indication of this, which is the next best thing!)
  260.          */
  261.         DEBUGMSGTL(("mynode", "write_mynodetrap sending the trap\n",
  262.                     action));
  263.         send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 99);
  264.         DEBUGMSGTL(("mynode", "write_mynodetrap trap sent\n", action));
  265.         break;

  266.     }
  267.     return SNMP_ERR_NOERROR;
  268. }

  269. /*
  270. * this documents how to send a SNMPv2 (and higher) trap via the
  271. * send_v2trap() API.
  272. * 
  273. * Coding SNMP-v2 Trap:
  274. * 
  275. * The SNMPv2-Trap PDU contains at least a pair of object names and
  276. * values: - sysUpTime.0 whose value is the time in hundredths of a
  277. * second since the netwok management portion of system was last
  278. * reinitialized.  - snmpTrapOID.0 which is part of the trap group SNMPv2
  279. * MIB whose value is the object-id of the specific trap you have defined
  280. * in your own MIB.  Other variables can be added to caracterize the
  281. * trap.
  282. * 
  283. * The function send_v2trap adds automaticallys the two objects but the
  284. * value of snmpTrapOID.0 is 0.0 by default. If you want to add your trap
  285. * name, you have to reconstruct this object and to add your own
  286. * variable.
  287. * 
  288. */



  289. int
  290. write_mynodetrap2(int action,
  291.                    u_char * var_val,
  292.                    u_char var_val_type,
  293.                    size_t var_val_len,
  294.                    u_char * statP, oid * name, size_t name_len)
  295. {
  296.     long            intval;

  297.     /*
  298.      * these variales will be used when we send the trap 
  299.      */
  300.     oid             objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };/* snmpTrapOID.0 */
  301.     oid             demo_trap[] = { 1, 3, 6, 1, 4, 1, 2021, 13, 990 };  /*demo-trap */
  302.     oid             mynode_string_oid[] =
  303.         { 1, 3, 6, 1, 4, 1, 2021, 254, 1, 0 };

  304.     static struct variable_list var_trap;
  305.     static struct variable_list var_obj;

  306.     DEBUGMSGTL(("mynode", "write_mynodetrap2 entered: action=%d\n",
  307.                 action));
  308.     switch (action) {
  309.     case RESERVE1:
  310.         /*
  311.          *  The only acceptable value is the integer 1
  312.          */
  313.         if (var_val_type != ASN_INTEGER) {
  314.             DEBUGMSGTL(("mynode", "%x not integer type", var_val_type));
  315.             return SNMP_ERR_WRONGTYPE;
  316.         }
  317.         if (var_val_len > sizeof(long)) {
  318.             DEBUGMSGTL(("mynode", "wrong length %x", var_val_len));
  319.             return SNMP_ERR_WRONGLENGTH;
  320.         }

  321.         intval = *((long *) var_val);
  322.         if (intval != 1) {
  323.             DEBUGMSGTL(("mynode", "wrong value %x", intval));
  324.             return SNMP_ERR_WRONGVALUE;
  325.         }
  326.         break;

  327.     case RESERVE2:
  328.         /*
  329.          * No resources are required.... 
  330.          */
  331.         break;

  332.     case FREE:
  333.         /*
  334.          * ... so no resources need be freed 
  335.          */
  336.         break;

  337.     case ACTION:
  338.         /*
  339.          *  Having triggered the sending of a trap,
  340.          *   it would be impossible to revoke this,
  341.          *   so we can't actually invoke the action here.
  342.          */
  343.        
  344.         //mynode_int = intval;
  345.         //Temp_ret=intval;
  346.         break;

  347.     case UNDO:
  348.         /*
  349.          * We haven't done anything yet,
  350.          * so there's nothing to undo 
  351.          */
  352.         
  353.         break;

  354.     case COMMIT:
  355.         /*
  356.          *  Everything else worked, so it's now safe
  357.          *   to trigger the trap.
  358.          *  Note that this is *only* acceptable since
  359.          *   the trap sending routines are "failsafe".
  360.          *  (In fact, they can fail, but they return no
  361.          *   indication of this, which is the next best thing!)
  362.          */

  363.         /*
  364.          * trap definition objects 
  365.          */

  366.         //Temp_ret=intval;

  367.         var_trap.next_variable = &var_obj;      /* next variable */
  368.         var_trap.name = objid_snmptrap;        /* snmpTrapOID.0 */
  369.         var_trap.name_length = sizeof(objid_snmptrap) / sizeof(oid);    /* number of sub-ids */
  370.         var_trap.type = ASN_OBJECT_ID;  
  371.         var_trap.val.objid = demo_trap; /* demo-trap objid   */
  372.         var_trap.val_len = sizeof(demo_trap);    /* length in bytes (not number of subids!) */


  373.         /*
  374.          * additional objects 
  375.          */
  376.          
  377.         strcpy(mynode_str, "HHHHHHHHHHH");
  378.         var_obj.next_variable = NULL;   /* No more variables after this one */
  379.         var_obj.name = mynode_string_oid;  
  380.         var_obj.name_length = sizeof(mynode_string_oid) / sizeof(oid); /* number of sub-ids */
  381.         var_obj.type = ASN_OCTET_STR;   /* type of variable */
  382.         var_obj.val.string = mynode_str; /* value */
  383.         var_obj.val_len = strlen(mynode_str);
  384.         DEBUGMSGTL(("mynode", "write_mynodetrap2 sending the v2 trap\n",
  385.                     action));
  386.         send_v2trap(&var_trap);
  387.         DEBUGMSGTL(("mynode", "write_mynodetrap2 v2 trap sent\n",
  388.                     action));   

  389.         break;

  390.     }
  391.     return SNMP_ERR_NOERROR;
  392. }  
复制代码



4..h文件:头文件
/uClinux-dist/user/ucdsnmp/agent/mibgroup/examples/ ucdDemoPublic.h

  1. /* ucdDemoPublic.h */

  2. #ifndef _MIBGROUP_UCDDEMOPUBLIC_H
  3. #define _MIBGROUP_UCDDEMOPUBLIC_H

  4. #include <snmp_api.h>
  5. #include <snmp_alarm.h>

  6. /* we use header_generic and checkmib from the util_funcs module */

  7. config_require(util_funcs)

  8. /* Magic number definitions: */

  9. #define   UCDDEMORESETKEYS      1
  10. #define   UCDDEMOPUBLICSTRING   2
  11. #define   UCDDEMOUSERLIST       3
  12. #define   UCDDEMOPASSPHRASE     4

  13. /* function definitions */

  14. extern void   init_ucdDemoPublic(void);
  15. extern FindVarMethod var_ucdDemoPublic;

  16. //自定义的定时trap函数
  17. SNMPAlarmCallback  snmploop_callback;
  18. WriteMethod write_ucdDemoResetKeys;
  19. WriteMethod write_ucdDemoPublicString;

  20. //自定义的函数声明
  21. WriteMethod write_mynodetrap;
  22. WriteMethod write_mynodetrap2;

  23. #endif /* _MIBGROUP_UCDDEMOPUBLIC_H */
复制代码



四、调试输出
1.开发板的命令运行:
snmptrapd –c /etc/snmpd.conf -V

其中-c /etc/snmpd.conf 用来指定配置文件

2.开发主机的测试命令及其输出:


可以看到可以使用get,set来读取并设置节点的值,由于1.3.6.1.4.1.2021.14.1.1.6.0是trap节点,所以当其值被设置后,就会自动向管理站发送trap。

如果需要检测agent的trap报文,需要在管理站运行snmptrapd命令,可以如下使用:

snmptrapd –f –Lo

另外,如果agent上有定时的trap报文,同样可以使用snmptrapd命令进行检测,如图为10秒中一次的定时trap报文的接收情况。

posted @ 2010-03-23 11:02  3qfly  阅读(1154)  评论(0编辑  收藏  举报