snmp 학습 노트 의 3-netsnmp Agent 개발

snmp 학습 노트 의 3-netsnmp Agent 개발
November 27th, 2008
BianJiang
Leave a comment
Go to comments
  • By: Bian Jiang ([email protected])
  • From: http://www.b0rder.com
  • Date: 2008.11.11

  • 1.mib 라 이브 러 리 파일 BVCOM-SYSTEMUPTIME-MIB.txt:
    BVCOM-SYSTEMUPTIME-MIB DEFINITIONS ::= BEGIN
    
    IMPORTS
        TimeTicks   FROM SNMPv2-SMI
        enterprises      FROM SNMPv2-SMI
        OBJECT-TYPE, Integer32, MODULE-IDENTITY      FROM SNMPv2-SMI;
    
    bvcom    OBJECT IDENTIFIER ::= { enterprises 26814 }
    
    ipq6800    OBJECT IDENTIFIER ::= { bvcom 6800 }
    
    bvcomAgentModules   OBJECT IDENTIFIER ::= { ipq6800 1 }
    
    bvcomAgentModuleObject OBJECT-TYPE
        SYNTAX      Integer32
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION
        "This is an object that simply supports a writable integer
         when compiled into the agent.  See
         http://www.net-snmp.org/tutorial-5/toolkit/XXX for further
         implementation details."
        DEFVAL { 1 }
        ::= { bvcomAgentModules 1 }
    
    bvcomAgentSubagentObject OBJECT-TYPE
        SYNTAX      Integer32
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION
        "This is an object that simply supports a writable integer
         when attached to the agent.  The object should be accessible
         when the agentx subagent containing this object is attached.
         See http://www.net-snmp.org/tutorial-5/toolkit/XXX for
         further implementation details."
        DEFVAL { 2 }
        ::= { bvcomAgentModules 2 }
    
    bvcomAgentPluginObject OBJECT-TYPE
        SYNTAX      Integer32
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION
        "This is an object that simply supports a writable integer
         when attached to the agent.  This object should be accessible
         when the dynamic plugin has been loaded into the agent.  See
         http://www.net-snmp.org/tutorial-5/toolkit/XXX for further
         implementation details."
        DEFVAL { 3 }
        ::= { bvcomAgentModules 3 }
    
    END
    PS:      mib    /etc/profile     SNMPCONFPATH    
    
       
       
       
       
    export SNMPCONFPATH=/usr/local/share/snmp/
    export MIBS=ALL

    2.mib 라 이브 러 리 파일 을/usr/local/share/snmp/mibs/로 복사 합 니 다.
    sudo cp BVCOM-SYSTEMUPTIME-MIB.txt /usr/local/share/snmp/mibs/

    3.mib 라 이브 러 리 불 러 오기:
    cat /usr/local/share/snmp/snmp.conf
    mibs +BVCOM-SYSTEMUPTIME-MIB

    4.mib 가 정상적으로 불 러 오 는 지 확인 하기:
    border@debian:/work/border/snmp/example-demon$ snmptranslate -IR -Tp bvcom
    +--bvcom(26814)
       |
       +--ipq6800(6800)
          |
          +--bvcomAgentModules(1)
             |
             +-- -RW- Integer32 bvcomAgentModuleObject(1)
             +-- -RW- Integer32 bvcomAgentSubagentObject(2)
             +-- -RW- Integer32 bvcomAgentPluginObject(3)

    5.mib2c 가 지원 하 는 템 플 릿 보기:
    border@debian:/work/border/snmp/example-demon$ ls /usr/local/share/snmp/
    mib2c.access_functions.conf    mib2c.create-dataset.conf  mib2c.scalar.conf
    mib2c.array-user.conf          mib2c-data                 mib2c.table_data.conf
    mib2c.check_values.conf        mib2c.genhtml.conf         mibs
    mib2c.check_values_local.conf  mib2c.int_watch.conf       snmp.conf
    mib2c.column_defines.conf      mib2c.iterate_access.conf  snmp.conf~
    mib2c.column_enums.conf        mib2c.iterate.conf         snmpconf-data
    mib2c.column_storage.conf      mib2c.mfd.conf             snmpd.conf
    mib2c.conf                     mib2c.notify.conf          snmp_perl.pl
    mib2c.container.conf           mib2c.old-api.conf         snmp_perl_trapd.pl

    6.템 플 릿 을 통 해.c 와.h 파일 생 성:
    border@debian:/work/border/snmp/example-demon$ mib2c -c mib2c.int_watch.conf bvcomAgentModules
    writing to -
    *** Warning: only generating code for nodes of MIB type INTEGER
    writing to bvcomAgentModules.h
    writing to bvcomAgentModules.c
    running indent on bvcomAgentModules.c
    running indent on bvcomAgentModules.h

    7.snmp 를 통 해agent_api 수호 프로그램 example-demon.c 작성:
    #include <net-snmp/net-snmp-config.h>
    #include <net-snmp/net-snmp-includes.h>
    #include <net-snmp/agent/net-snmp-agent-includes.h>
    #include <signal.h>
    #include "bvcomAgentModules.h"
    
    static int keep_running;
    
    RETSIGTYPE
    stop_server(int a) {
        keep_running = 0;
    }
    
    int
    main (int argc, char **argv) {
    
        int agentx_subagent=0; /* change this if you want to be a SNMP master agent */
        int background = 0; /* change this if you want to run in the background */
        int syslog = 0; /* change this if you want to use syslog */
    
        /* print log errors to syslog or stderr */
    
        if (syslog)
            snmp_enable_calllog();
        else
            snmp_enable_stderrlog();
    
        /* we're an agentx subagent? */
        if (agentx_subagent) {
            /* make us a agentx client. */
            netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
        }
    
        /* run in background, if requested */
        if (background && netsnmp_daemonize(1, !syslog))
            exit(1);
    
        /* Initialize tcpip, if necessary */
        SOCK_STARTUP;
    
        /* Initialize the agent library */
        init_agent("example-demon"); //      
    
        /* Initialize our mib code here */
        printf("Before init bvcomAgentModules 
    "); init_bvcomAgentModules(); // printf("End init bvcomAgentModules
    "); /* initialize vacm/usm access control */ if (!agentx_subagent) { void init_vacm_vars(); void init_usmUser(); } /* Example-demon will be used to read example-demon.conf files. */ init_snmp("example-demon"); /* If we're going to be a snmp master agent, initial the ports */ if (!agentx_subagent) init_master_agent(); /* open the port to listen on (defaults to udp:161) */ printf("---------------------
    "); /* In case we recevie a request to stop (kill -TERM or kill -INT) */ keep_running = 1; signal(SIGTERM, stop_server); signal(SIGINT, stop_server); snmp_log(LOG_INFO,"example-demon is up and running.
    "); /* your main loop here... */ while(keep_running) { /* if you use select(), see snmp_select_info() in snmp_api(3) */ /* --- OR --- */ agent_check_and_process(1); /* 0 == don't block */ } /* at shutdown time */ snmp_shutdown("example-demon"); SOCK_CLEANUP; return 0; }

    8.Makefile:
    CC=gcc
    
    OBJS2=example-demon.o bvcomAgentModules.o
    TARGETS=example-demon
    
    CFLAGS=-I. `net-snmp-config --cflags`
    BUILDLIBS=`net-snmp-config --libs`
    BUILDAGENTLIBS=`net-snmp-config --agent-libs`
    
    # shared library flags (assumes gcc)
    DLFLAGS=-fPIC -shared
    
    all: $(TARGETS)
    
    example-demon: $(OBJS2)
        $(CC) -o example-demon $(OBJS2)  $(BUILDAGENTLIBS)
    
    clean:
        rm $(OBJS2) $(OBJS2) $(TARGETS)

    9.example-demon.conf:
    ###############################################################################
    # Access Control
    ###############################################################################
    
    #       sec.name  source          community
    com2sec local     localhost       public
    com2sec mynetwork 192.168.0.0/24      public
    
    ####
    # Second, map the security names into group names:
    
    #                 sec.model  sec.name
    group MyRWGroup    v1         local
    group MyRWGroup    v2c        local
    group MyRWGroup    usm        local
    group MyROGroup v1         mynetwork
    group MyROGroup v2c        mynetwork
    group MyROGroup usm        mynetwork
    
    ####
    # Third, create a view for us to let the groups have rights to:
    
    #           incl/excl subtree                          mask
    view all    included  .1                               80
    
    ####
    # Finally, grant the 2 groups access to the 1 view with different
    # write permissions:
    
    #              context sec.model sec.level match  read   write  notif
    access MyROGroup ""      any       noauth    exact  all    none   none
    access MyRWGroup ""      any       noauth    exact  all    all    none
    
    agentaddress 161

    10.example-demon 을 실행 할 때 슈퍼 관리자 로 실행 해 야 합 니 다.그렇지 않 으 면 오류 가 발생 할 수 있 습 니 다.
    sudo ./example-demon

    a.슈퍼 관리 자 를 사용 하지 않 았 을 때 보 고 된 오류:
    border@debian:/work/border/snmp/example-demon$ ./example-demon
    netsnmp_assert !"registration != duplicate" failed agent_registry.c:535 netsnmp_subtree_load()
    netsnmp_assert !"registration != duplicate" failed agent_registry.c:535 netsnmp_subtree_load()
    netsnmp_assert !"registration != duplicate" failed agent_registry.c:535 netsnmp_subtree_load()
    Before init bvcomAgentModules
    End init bvcomAgentModules
    Error opening specified endpoint "161"
    ---------------------
    example-demon is up and running.
    read_config_store open failure on /var/net-snmp/example-demon.conf
    read_config_store open failure on /var/net-snmp/example-demon.conf
    read_config_store open failure on /var/net-snmp/example-demon.conf

    b.지 정 된 endpoint""오 류 를 보고 하면 example-demon.conf 설정 파일 에 에이전트 address 161 이 없다 는 것 을 설명 합 니 다.
    11.설정 파일 을~/.snmp/디 렉 터 리 로 복사 합 니 다.
    cp example-demon.conf /home/border/.snmp/

    12.sudo ./example-demon:
    border@debian:~$ snmpwalk -v1 -c public localhost bvcom
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentModuleObject.0 = INTEGER: 68001
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentSubagentObject.0 = INTEGER: 68002
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentPluginObject.0 = INTEGER: 68003
    End of MIB

    인증:
    border@debian:~$ snmpget -v1 -c public localhost bvcomAgentModuleObject.0
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentModuleObject.0 = INTEGER: 68001
    
    border@debian:~$ snmpgetnext -v1 -c public localhost bvcomAgentModuleObject.0
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentSubagentObject.0 = INTEGER: 68002

    13.snmpv 3 지원 설정 파일 에 추가:
    rwuser border
    rwuser border1
    createUser border MD5 "bvcombjbj" DES
    createUser border1 SHA "bvcombjbj" AES
    (          :createUser border1 SHA "bvcombjbj" AES128)

    다음 명령 검증 을 통 해:
    a.MD5 검증:
    snmpwalk -v3 -l authPriv -u border -A bvcombjbj -X bvcombjbj localhost bvcom
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentModuleObject.0 = INTEGER: 68001
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentSubagentObject.0 = INTEGER: 68002
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentPluginObject.0 = INTEGER: 68003

    b.검증 SHA:
    snmpwalk -v3 -l authPriv -u border1 -a SHA -x AES -A bvcombjbj -X bvcombjbj localhost bvcom
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentModuleObject.0 = INTEGER: 68001
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentSubagentObject.0 = INTEGER: 68002
    BVCOM-SYSTEMUPTIME-MIB::bvcomAgentPluginObject.0 = INTEGER: 68003

    AES 128 을 사용한다 면-x AES 를-x AES 128 로 바 꿔 야 합 니 다.
    snmpwalk -v3 -l authPriv -u border1 -a SHA -x AES128 -A bvcombjbj -X bvcombjbj localhost bvcom

    참고:
    NET-SNMP 패키지 개발 단순 클 라 이언 트 에이전트http://b0rder.com/wiki/NetSnmp/NetSnmpSimpleAgentMib
    snmpd.examples 설정 정보 관련http://www.net-snmp.org/docs/man/snmpd.examples.html
  • snmp_agent_api http://www.net-snmp.org/docs/man/snmp_agent_api.html
  • Tutorial http://www.nwsmith.net/HintsTips/net-snmp-tutorial.htm
  • http://www.net-snmp.org/wiki/index.php/TUT:SNMPv3_Options
  • By: Bian Jiang ([email protected])
  • From: http://www.b0rder.com
  • Date: 2008.11.11

  • –EOF–

    좋은 웹페이지 즐겨찾기