uclinux下ucd-snmp的編譯

admin @ 2014-03-25 , reply:0

一、開發環境:

  1.   操作系統:uClinux-ifax-2007-03-16.tar.gz
  2.   交叉編譯工具:arm-elf-tools-20030314.sh
  3.   硬體平台:Samsung 4510B (snds-100)

二、編譯ucd-snmp主要步驟
1.配置uClinux
修改config/config.in文件,確保改文件中有如下內容:
配置用戶程序,選中ucd-snmp

2.使用snmpconf工具生成配置文件,內容如下:
###########################################################################
#
# snmpd.conf
#
#   - created by the snmpconf configuration program
#
###########################################################################
# SECTION: System Information Setup
#
#   This section defines some of the information reported in
#   the "system" mib group in the mibII tree.
 
# syslocation: The [typically physical] location of the system.
#   Note that setting this value here means that when trying to
#   perform an snmp SET operation to the sysLocation.0 variable will make
#   the agent return the "notWritable" error code.  IE, including
#   this token in the snmpd.conf file will disable write access to
#   the variable.
#   arguments:  location_string
 
syslocation  xi'an
 
# syscontact: The contact information for the administrator
#   Note that setting this value here means that when trying to
#   perform an snmp SET operation to the sysContact.0 variable will make
#   the agent return the "notWritable" error code.  IE, including
#   this token in the snmpd.conf file will disable write access to
#   the variable.
#   arguments:  contact_string
 
syscontact  lazy_linux@126.com
 
# sysservices: The proper value for the sysServices object.
#   arguments:  sysservices_number
 
sysservices 77
sysservices 0*1 + 0*2 + 0*4 + 0*8 + 0*64
sysservices 0*1 + 0*2 + 1*4 + 1*8 + 0*64 
 
###########################################################################
# SECTION: Access Control Setup
#
#   This section defines who is allowed to talk to your running
#   snmp agent. 
# rwuser: a SNMPv3 read-write user
#   arguments:  user [noauth|auth|priv] [restriction_oid]
 
rwuser  rw3 noauth
 
# rwcommunity: a SNMPv1/SNMPv2c read-write access community name
#   arguments:  community [default|hostname|network/bits] [oid]
 
rwcommunity  rw12  
  
###########################################################################
# SECTION: Trap Destinations
#
#   Here we define who the agent will send traps to.
 
# trap2sink: A SNMPv2c trap receiver
#   arguments: host [community] [portnum]
 
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管理節點
UCD-DEMO-MIB DEFINITIONS ::= BEGIN 
IMPORTS
    MODULE-IDENTITY, OBJECT-TYPE, Integer32 FROM SNMPv2-SMI
    ucdavis                                          FROM UCD-SNMP-MIB;
 
ucdDemoMIB MODULE-IDENTITY
    LAST-UPDATED "9912090000Z"
    ORGANIZATION "University of California, Davis"
    CONTACT-INFO
         "The ucd-snmp-coders mailing list is the best place to
          write for public questions about the ucd-snmp
          project: ucd-snmp-coders@ucd-snmp.ucdavis.edu
 
          Primary Author: Wes Hardaker
 
          postal:     IT-DCAS
                     UCDavis
                      Davis CA  95616
         phone:      +1 530 754-7571
         email:      wjhardaker@ucdavis.edu"
    DESCRIPTION
         "The UCD-SNMP Demonstration MIB."
    REVISION       "9912090000Z"
    DESCRIPTION
         "SMIv2 version converted from older MIB definitions."
::= { ucdavis 14 }
//該節點位於ucdavis下的第14個節點
 
ucdDemoMIBObjects OBJECT IDENTIFIER ::= { ucdDemoMIB 1 }
//在UCD-SNMP-MIB中定義,
// --   ucdDemoMIB       OBJECT IDENTIFIER ::= { ucdavis  14 } - UCD-DEMO-MIB
//OID: ucdavis.14.1
 
ucdDemoPublic OBJECT IDENTIFIER ::= { ucdDemoMIBObjects 1 }
//OID: ucdavis.14.1.1
 
ucdDemoResetKeys OBJECT-TYPE
    SYNTAX        Integer32 (0..2147483647)
    MAX-ACCESS    read-write
    STATUS        current
    DESCRIPTION
         "A set of value 1 to this object resets the
          demonstration user's auth and priv keys to the
          keys based on the P->Ku->Kul transformation of the
          value of the ucdDemoPasspharse object.
 
          Values other than 1 are ignored."
::= { ucdDemoPublic 1 }
//OID: ucdavis.14.1.1
 
ucdDemoPublicString OBJECT-TYPE
    SYNTAX        OCTET STRING (SIZE(0..1024))
    MAX-ACCESS    read-write
    STATUS        current
    DESCRIPTION
         "A publicly settable string that can be set for testing
          snmpsets.  This value has no real usage other than
          testing purposes."
    ::= { ucdDemoPublic 2 }
 
ucdDemoUserList OBJECT-TYPE
    SYNTAX        OCTET STRING
    MAX-ACCESS    read-only
    STATUS        current
    DESCRIPTION
         "The list of users affected by the ucdDemoResetKeys object."
    ::= { ucdDemoPublic 3 }
 
ucdDemoPassphrase  OBJECT-TYPE
    SYNTAX        OCTET STRING
    MAX-ACCESS    read-only
    STATUS        current
    DESCRIPTION
         "The demo passphrase that ucdDemoResetKeys changes each
          users localized key to based on the P->Ku->Kul transformation."
    ::= { ucdDemoPublic 4 }
 
//定義了2個trap類型的節點
TheTrap1 NOTIFICATION-TYPE
         STATUS current
         DESCRIPTION
                 "This is 1st trap."
         ::= { ucdDemoPublic 5 }
//OID: ucdavis.14.1.5
 
 
TheTrap2 NOTIFICATION-TYPE
         SYNTAX Integer32
         MAX-ACCESS       read-write
         STATUS   mandatory
         DESCRIPTION
                 "This is 2nd trap."
         ::= { ucdDemoPublic 6 }
//OID: ucdavis.14.1.6
 
END

2.編譯的頭文件
/uClinux-dist/user/ucdsnmp/agent/Makefile
在MIBOBJS中增加需要編譯的c文件的目標文件名稱:
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
 
/uClinux-dist/user/ucdsnmp/agent/mibgroup/Makefile

OBJS和SRCS中增加目標文件名稱和c文件名稱

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
 
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中增加自定義的初始化函數名稱

/* This file is automatically generated by configure.  Do not modify by hand. */
  init_system_mib();
  init_sysORTable();
  init_at();
  init_interfaces();
  init_snmp_mib();
  init_tcp();
  init_icmp();
  init_ip();
  init_udp();
  init_vacm_vars();
  init_memory();
  init_proc();
  init_versioninfo();
  init_pass();
  init_pass_persist();
  init_disk();
  init_loadave();
  init_extensible();
  init_errormib();
  init_registry();
  init_file();
  init_snmpEngine();
  init_snmpMPDStats();
  init_usmStats();
  init_usmUser();
  init_var_route();
  init_ipfwacc();
  init_ucdDemoPublic();

/uClinux-dist/user/ucdsnmp/agent/mibgroup/mib_module_includes.h中增加自定義的頭文件名稱

/* This file is automatically generated by configure.  Do not modify by hand. */
#include "mibgroup/mibII.h"
#include "mibgroup/ucd_snmp.h"
#include "mibgroup/snmpv3mibs.h"
#include "mibgroup/mibII/system_mib.h"
#include "mibgroup/mibII/sysORTable.h"
#include "mibgroup/mibII/at.h"
#include "mibgroup/mibII/interfaces.h"
#include "mibgroup/mibII/snmp_mib.h"
#include "mibgroup/mibII/tcp.h"
#include "mibgroup/mibII/icmp.h"
#include "mibgroup/mibII/ip.h"
#include "mibgroup/mibII/udp.h"
#include "mibgroup/mibII/vacm_vars.h"
#include "mibgroup/ucd-snmp/memory.h"
#include "mibgroup/ucd-snmp/proc.h"
#include "mibgroup/ucd-snmp/versioninfo.h"
#include "mibgroup/ucd-snmp/pass.h"
#include "mibgroup/ucd-snmp/pass_persist.h"
#include "mibgroup/ucd-snmp/disk.h"
#include "mibgroup/ucd-snmp/loadave.h"
#include "mibgroup/ucd-snmp/extensible.h"
#include "mibgroup/ucd-snmp/errormib.h"
#include "mibgroup/ucd-snmp/registry.h"
#include "mibgroup/ucd-snmp/file.h"
#include "mibgroup/snmpv3/snmpEngine.h"
#include "mibgroup/snmpv3/snmpMPDStats.h"
#include "mibgroup/snmpv3/usmStats.h"
#include "mibgroup/snmpv3/usmUser.h"
#include "mibgroup/util_funcs.h"
#include "mibgroup/mibII/var_route.h"
#include "mibgroup/mibII/route_write.h"
#include "mibgroup/misc/ipfwacc.h"
#include "mibgroup/examples/ucdDemoPublic.h"

3..c文件:實現SNMP get,set,trap等功能
/uClinux-dist/user/ucdsnmp/agent/mibgroup/examples/ucdDemoPublic.c

/* ucdDemoPublic.c */
#include
#include
#include
#include
#include
 
#include
 
#include "mibincl.h"
#include "snmpv3.h"
#include "snmpusm.h"
#include "agent_read_config.h"
#include "ucdDemoPublic.h"
#include "util_funcs.h"
 
#define MYMAX 1024
#define MAXUSERS 10
 
int num=0;
static char demoUsers[MAXUSERS][MYMAX+1];
static char demopass[MYMAX+1];
 
char mynode_str[300];
 
void ucdDemo_parse_user(const char *word, char *line) {
  if (num == MAXUSERS)
    return;
 
  if (strlen(line) > MYMAX)
    return;
 
  strcpy(demoUsers[num++], line);
}
 
void ucdDemo_parse_userpass(const char *word, char *line) {
  if (strlen(line) > MYMAX)
    return;
 
  strcpy(demopass, line);
}
 
/* this variable defines function callbacks and type return information
   for the ucdDemoPublic mib */
 
//與MIB中的定義相對應
struct variable2 ucdDemoPublic_variables[] = {
  { UCDDEMORESETKEYS    , ASN_INTEGER   , RWRITE, var_ucdDemoPublic, 1, { 1 } },
  { UCDDEMOPUBLICSTRING , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 2 } },
  { UCDDEMOUSERLIST     , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 3 } },
  { UCDDEMOPASSPHRASE   , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 4 } },
  { 5, ASN_INTEGER, RWRITE, var_ucdDemoPublic, 1, {5}},
  { 6, ASN_INTEGER, RWRITE, var_ucdDemoPublic, 1, {6}}
};
 
/* Define the OID pointer to the top of the mib tree that we're
   registering underneath */
oid ucdDemoPublic_variables_oid[] = { 1,3,6,1,4,1,2021,14,1,1 };
 
void init_ucdDemoPublic(void) {
  REGISTER_MIB( "examples/ucdDemoPublic", ucdDemoPublic_variables,
                 variable2, ucdDemoPublic_variables_oid);
  snmpd_register_config_handler("demoUser", ucdDemo_parse_user, NULL, "USER");
  snmpd_register_config_handler("demoPass", ucdDemo_parse_userpass, NULL, "PASSPHASE");
  //定義定時的trap發送
  snmp_alarm_register(10, SA_REPEAT, snmploop_callback, NULL);
}
 
//定義自定義的定時trap動作
void snmploop_callback(unsigned int reg, void *clientarg){
         send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 1);
}
 
unsigned char publicString[MYMAX+1];
long long_ret;
 
unsigned char *
var_ucdDemoPublic(
    struct variable *vp,
    oid     *name,
    size_t  *length,
    int     exact,
    size_t  *var_len,
    WriteMethod **write_method)
{
//  static long long_ret;
  static char string[MYMAX+1], *cp;
  int i;
 
  *write_method = 0;           /* assume it isnt writable for the time being */
  *var_len = sizeof(long_ret); /* assume an integer and change later if not */
 
  if (header_generic(vp,name,length,exact,var_len,write_method))
      return 0;
 
  /* this is where we do the value assignments for the mib results. */
  switch(vp->magic) {
 
    case UCDDEMORESETKEYS:
      *write_method = write_ucdDemoResetKeys;
      //long_ret = 0;
      return (unsigned char *) &long_ret;
 
    case UCDDEMOPUBLICSTRING:
      *write_method = write_ucdDemoPublicString;
      *var_len = strlen(publicString);
      return (unsigned char *) publicString;
 
    case UCDDEMOUSERLIST:
      cp = string;
      for(i=0; i < num; i++) {
        sprintf(cp, " %s", demoUsers[i]);
        cp = cp + strlen(cp);
      }
      *var_len = strlen(string);
      return (unsigned char *) string;
     
    case UCDDEMOPASSPHRASE:
      *var_len = strlen(demopass);
      return (unsigned char *) demopass;

    case 5:
          long_ret=0;
          *write_method = write_mynodetrap;
          return (u_char *)&long_ret;

   case 6:
        /*
         * This object is essentially "write-only".
         * It only exists to trigger the sending of a v2 trap.
         * Reading it will always return 0.
         */
        long_ret = 0; /*FIX*/
        *write_method = write_mynodetrap2;
        return (u_char *)&long_ret;
     
    default:
      DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ucdDemoPublic ", vp->magic));
  }
  return 0;
}
 
int
write_ucdDemoResetKeys(
   int      action,
   u_char   *var_val,
   u_char   var_val_type,
   size_t   var_val_len,
   u_char   *statP,
   oid      *name,
   size_t   name_len)
{
  /* variables we may use later */
  //static long long_ret;
  static unsigned char string[1500];
  static oid objid[MAX_OID_LEN];
  static struct counter64 c64;
  int bigsize=1000;
  unsigned char *engineID;
  int engineIDLen;
  int i;
  struct usmUser *user;
 
  if (var_val_type != ASN_INTEGER) {
         printf("ASN_INTEGER ");
      DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoResetKeys not ASN_INTEGER "));
      return SNMP_ERR_WRONGTYPE;
  }
  if (var_val_len > sizeof(long_ret)) {
      DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoResetKeys: bad length "));
      return SNMP_ERR_WRONGLENGTH;
  }
  if (action == COMMIT) {
         int fd = open("/dev/lamp", O_RDWR, 0644);
         if(fd<0){
                 printf("open /dev/lamp error ");
                 return SNMP_ERR_NOERROR;
         }
         printf("new value: %x ", *(long*)var_val);
 
         if( *(long *)var_val == 0) write(fd,"00 ", 3);
         if( *(long *)var_val == 1) write(fd,"01 ", 3);
         if( *(long *)var_val == 2) write(fd,"10 ", 3);
         if( *(long *)var_val == 3) write(fd,"11 ", 3);
 
         close(fd);
                
      long_ret = *((long *) var_val);
      /*if (long_ret == 1) {
        engineID = snmpv3_generate_engineID(&engineIDLen);
        for(i=0; i < num; i++) {
          user = usm_get_user(engineID, engineIDLen, demoUsers[i]);
          if (user) {
            usm_set_user_password(user, "userSetAuthPass", demopass);
            usm_set_user_password(user, "userSetPrivPass", demopass);
          }
        }
         reset the keys
      }*/
  }
  return SNMP_ERR_NOERROR;
}
 
int
write_ucdDemoPublicString(
   int      action,
   u_char   *var_val,
   u_char   var_val_type,
   size_t   var_val_len,
   u_char   *statP,
   oid      *name,
   size_t   name_len)
{
  if (var_val_type != ASN_OCTET_STR) {
      DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoPublicString not ASN_OCTET_STR "));
      return SNMP_ERR_WRONGTYPE;
  }
  if (var_val_len > MYMAX) {
      DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoPublicString: bad length "));
      return SNMP_ERR_WRONGLENGTH;
  }
  if (action == COMMIT) {
      strcpy(publicString, var_val);
  }
  return SNMP_ERR_NOERROR;
}
 
int write_mynodetrap(int action,
                  u_char * var_val,
                  u_char var_val_type,
                  size_t var_val_len,
                  u_char * statP, oid * name, size_t name_len)
{
    long            intval;
    DEBUGMSGTL(("mynode", "write_mynodetrap entered: action=%d ",
                action));
    switch (action) {
    case RESERVE1:  
        /*
         *  The only acceptable value is the integer 1
         */
        if (var_val_type != ASN_INTEGER) {
            DEBUGMSGTL(("mynode", "%x not integer type", var_val_type));
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len > sizeof(long)) {
            DEBUGMSGTL(("mynode", "wrong length %x", var_val_len));
            return SNMP_ERR_WRONGLENGTH;
        }
 
        intval = *((long *) var_val);
        if (intval != 1) {
            DEBUGMSGTL(("mynode", "wrong value %x", intval));
            return SNMP_ERR_WRONGVALUE;
        }
        break;
 
    case RESERVE2:
        /*
         * No resources are required....
         */
        break;
 
    case FREE:
        /*
         * ... so no resources need be freed
         */
        break;
 
    case ACTION:
        /*
         *  Having triggered the sending of a trap,
         *   it would be impossible to revoke this,
         *   so we can't actually invoke the action here.
         */
        break;
 
    case UNDO:
        /*
         * We haven't done anything yet,
         * so there's nothing to undo
         */
        break;
 
    case COMMIT:
        /*
         *  Everything else worked, so it's now safe
         *   to trigger the trap.
         *  Note that this is *only* acceptable since
         *   the trap sending routines are "failsafe".
         *  (In fact, they can fail, but they return no
         *   indication of this, which is the next best thing!)
         */
        DEBUGMSGTL(("mynode", "write_mynodetrap sending the trap ",
                    action));
        send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 99);
        DEBUGMSGTL(("mynode", "write_mynodetrap trap sent ", action));
        break;
 
    }
    return SNMP_ERR_NOERROR;
}
 
/*
 * this documents how to send a SNMPv2 (and higher) trap via the
 * send_v2trap() API.
 *
 * Coding SNMP-v2 Trap:
 *
 * The SNMPv2-Trap PDU contains at least a pair of object names and
 * values: - sysUpTime.0 whose value is the time in hundredths of a
 * second since the netwok management portion of system was last
 * reinitialized.  - snmpTrapOID.0 which is part of the trap group SNMPv2
 * MIB whose value is the object-id of the specific trap you have defined
 * in your own MIB.  Other variables can be added to caracterize the
 * trap.
 *
 * The function send_v2trap adds automaticallys the two objects but the
 * value of snmpTrapOID.0 is 0.0 by default. If you want to add your trap
 * name, you have to reconstruct this object and to add your own
 * variable.
 *
 */
  
int write_mynodetrap2(int action,
                   u_char * var_val,
                   u_char var_val_type,
                   size_t var_val_len,
                   u_char * statP, oid * name, size_t name_len)
{
    long            intval;
    /*
     * these variales will be used when we send the trap
     */
    oid             objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };/* snmpTrapOID.0 */
    oid             demo_trap[] = { 1, 3, 6, 1, 4, 1, 2021, 13, 990 };  /*demo-trap */
    oid             mynode_string_oid[] =
        { 1, 3, 6, 1, 4, 1, 2021, 254, 1, 0 };
 
    static struct variable_list var_trap;
    static struct variable_list var_obj;
 
    DEBUGMSGTL(("mynode", "write_mynodetrap2 entered: action=%d ",
                action));
    switch (action) {
    case RESERVE1:
        /*
         *  The only acceptable value is the integer 1
         */
        if (var_val_type != ASN_INTEGER) {
            DEBUGMSGTL(("mynode", "%x not integer type", var_val_type));
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len > sizeof(long)) {
            DEBUGMSGTL(("mynode", "wrong length %x", var_val_len));
            return SNMP_ERR_WRONGLENGTH;
        }
 
        intval = *((long *) var_val);
        if (intval != 1) {
            DEBUGMSGTL(("mynode", "wrong value %x", intval));
            return SNMP_ERR_WRONGVALUE;
        }
        break;
 
    case RESERVE2:
        /*
         * No resources are required....
         */
        break;
 
    case FREE:
        /*
         * ... so no resources need be freed
         */
        break;
 
    case ACTION:
        /*
         *  Having triggered the sending of a trap,
         *   it would be impossible to revoke this,
         *   so we can't actually invoke the action here.
         */
      
        //mynode_int = intval;
        //Temp_ret=intval;
        break;
 
    case UNDO:
        /*
         * We haven't done anything yet,
         * so there's nothing to undo
         */
       
        break;
 
    case COMMIT:
        /*
         *  Everything else worked, so it's now safe
         *   to trigger the trap.
         *  Note that this is *only* acceptable since
         *   the trap sending routines are "failsafe".
         *  (In fact, they can fail, but they return no
         *   indication of this, which is the next best thing!)
         */
 
        /*
         * trap definition objects
         */
 
        //Temp_ret=intval;
 
        var_trap.next_variable = &var_obj;      /* next variable */
        var_trap.name = objid_snmptrap;        /* snmpTrapOID.0 */
        var_trap.name_length = sizeof(objid_snmptrap) / sizeof(oid);    /* number of sub-ids */
        var_trap.type = ASN_OBJECT_ID; 
        var_trap.val.objid = demo_trap; /* demo-trap objid   */
        var_trap.val_len = sizeof(demo_trap);    /* length in bytes (not number of subids!) */
  
        /*
         * additional objects
         */
        
        strcpy(mynode_str, "HHHHHHHHHHH");
        var_obj.next_variable = NULL;   /* No more variables after this one */
        var_obj.name = mynode_string_oid; 
      ;   var_obj.name_length = sizeof(mynode_string_oid) / sizeof(oid); /* number of sub-ids */
        var_obj.type = ASN_OCTET_STR;   /* type of variable */
        var_obj.val.string = mynode_str; /* value */
        var_obj.val_len = strlen(mynode_str);
        DEBUGMSGTL(("mynode", "write_mynodetrap2 sending the v2 trap ",
                    action));
        send_v2trap(&var_trap);
        DEBUGMSGTL(("mynode", "write_mynodetrap2 v2 trap sent ",
                    action));  
 
        break;
 
    }
    return SNMP_ERR_NOERROR;
}
  
 4..h文件:頭文件
/uClinux-dist/user/ucdsnmp/agent/mibgroup/examples/ ucdDemoPublic.h
/* ucdDemoPublic.h */
 
#ifndef _MIBGROUP_UCDDEMOPUBLIC_H
#define _MIBGROUP_UCDDEMOPUBLIC_H
 
#include
#include
 
/* we use header_generic and checkmib from the util_funcs module */
 
config_require(util_funcs)
 
/* Magic number definitions: */
 
#define   UCDDEMORESETKEYS      1
#define   UCDDEMOPUBLICSTRING   2
#define   UCDDEMOUSERLIST       3
#define   UCDDEMOPASSPHRASE     4
 
/* function definitions */
 
extern void   init_ucdDemoPublic(void);
extern FindVarMethod var_ucdDemoPublic;
 
//自定義的定時trap函數
SNMPAlarmCallback  snmploop_callback;
WriteMethod write_ucdDemoResetKeys;
WriteMethod write_ucdDemoPublicString;
 
//自定義的函數聲明
WriteMethod write_mynodetrap;
WriteMethod write_mynodetrap2;
 
#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報文的接收情況。




[admin via 研發互助社區 ] uclinux下ucd-snmp的編譯已經有1190次圍觀

http://cocdig.com/docs/show-post-42078.html