现在的位置: 首页 > 综合 > 正文

net-snmp开发

2019年05月30日 ⁄ 综合 ⁄ 共 6213字 ⁄ 字号 评论关闭

NET-SNMP 开发

一、系统环境
操作系统:Fedora 8 内核:Linux 2.6
编译环境:gcc 4.1.2 代码版本:net-snmp-5.7.2.tar.gz
(下载地址:http://sourceforge.net/projects/net- snmp/files/net-snmp/5.7.2/)

二、安装 net-snmp

tar zxvf net-snmp-5.7.2.tar.gz
cd net-snmp-5.7.2
./configure --enable-mfd-rewrites --enable-embedded-perl --with-perl-modules --with-default-snmp-version="2" --with-sys-contact="linux" --with-sys-location="China"
#此时会提示还需要一些参数,直接回车就行了
make
make install

此时一个标准的snmp代理端已经安装完成(默认安装在/usr/local/share/snmp/路径下,snmpd即为代理程序)

三、配置snmp

vim /usr/local/share/snmp/snpmd.conf

输入如下两行配置,保存(其中public为默认读密码 ,123456为读写密码)

rocommunity  public
rwcommunity  123456

运行 snmpd -f -Le,如有错误,可能查看日志
测试代理是否正常工作,输入:

snmpwalk -v 1 -c public localhost 1.3.6.1.2.1.1
#如有输出,则配置成功
SNMPv2-MIB::sysDescr.0 = STRING: Linux localhost 2.6.18-164.el5 #1 SMP Thu Sep 3 03:33:56 EDT 2009 i686
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-TC::linux
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (1047697) 2:54:36.97
SNMPv2-MIB::sysContact.0 = STRING: linux
SNMPv2-MIB::sysName.0 = STRING: mylinux
SNMPv2-MIB::sysLocation.0 = STRING: China
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORID.1 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.2 = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID.3 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.5 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.6 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.7 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.8 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORDescr.1 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.3 = STRING: The management information definitions for the SNMP User-based Security Model.
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.5 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.8 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.2 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.3 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.4 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.5 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.6 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.7 = Timeticks: (1) 0:00:00.01
SNMPv2-MIB::sysORUpTime.8 = Timeticks: (1) 0:00:00.01

四、自定义MIB
以下是自定义ZHM05-MIB.txt文件的内容,只定义了SysSt一个点

ZHM05-MIB DEFINITIONS::= BEGIN
 
IMPORTS
 enterprises, OBJECT-TYPE, Integer32
    FROM SNMPv2-SMI
 TEXTUAL-CONVENTION, DisplayString
    FROM SNMPv2-TC;
 ZHM05 OBJECT IDENTIFIER::={enterprises 310}
 Warn OBJECT IDENTIFIER::={ZHM05 1}
 
SysSt OBJECT-TYPE
 SYNTAX      Integer32
 ACCESS      read-write
 STATUS      current
 DESCRIPTION "zhm05 SysSt"
 ::={Warn 1}
 
END

放到 /usr/local/share/snmp/mibs文件夹中

修改/usr/local/share/snmp /snmp.conf,添加自定义MIB库
输入如下并保存:

mibs +ZHM05-MIB

然后运行snmptranslate,检查MIB文件是否正确

snmptranslate -IR -Tp bvcom
#如果能正确出现层次界面,则正确
+--ZHM05(310)
   |
   +--Warn(1)
      |
      +-- -RW- Integer32 SysSt(1)

使用mib2c生成源代码,mib2c有许多模板,可按自己的实际选择模块

mib2c -c mib2c.scalar.conf Warn

生成.c和.h文件后,根据实际情况修改文件中的代码(需要传输的数据等)

五、agent开发
通过net-snmp可以方便地开发自己的代理,集成到项目中,而不使用 snmpd
app.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 "Warn.h"
 
static int keep_running;
 
RETSIGTYPE stop_server(int a)
{
    keep_running = 0;
}
 
int main()
{
    int agentx_subagent = 0;
    int background = 0;
    int syslog = 0;
 
    if (syslog)
        snmp_enable_calllog();
    else
        snmp_enable_stderrlog();
 
    if (agentx_subagent)
        netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
 
    if (background && netsnmp_deamonize(1, !syslog))
        exit(1);
 
    SOCK_STARTUP;
 
    init_agent("snmpd");
 
    printf("Before init Modulesn");
 
    init_Warn();
    printf("After init Modulesn");
 
    if (!agentx_subagent)
    {
        void init_vacm_vars();
        void init_usmUser();
    }
 
    init_snmp("snmpd");
 
    if (!agentx_subagent)
        init_master_agent();
 
    printf("----------------------n");
    keep_running = 1;
    signal(SIGTERM, stop_server);
    signal(SIGINT, stop_server);
 
    while (keep_running)
        agent_check_and_process(1);
 
    snmp_shutdown("snmpd");
    SOCK_CLEANUP;
    return 0;
}

Makefile

CC=gcc
OBJS2=app.o Warn.o
TARGETS=app
CFLAGS=-I. net-snmp-config --cflags
BUILDLIBS=net-snmp-config --libs
BUILDAGENTLIBS=net-snmp-config --agent-libs
 
DLFLAGS=-fPIC -shared
all:$(TARGETS)
app:$(OBJS2)
    $(CC) -o $(TARGETS) $(OBJS2) $(BUILDAGENTLIBS)
clean:
    rm $(OBJS2) $(TARGETS)

六、发送trap
目前本人暂时只研究了API发送TRAP,没有研究通过mib2c.notify.conf模板生成TRAP
代码如下

int TrapSendTest()
{
    netsnmp_session session, *pSession;
    netsnmp_pdu *pdu;
    int status = 0;
    oid ZHM05_oid[] = { 1, 3, 6, 1, 4, 1, 310 };
    oid SysSt_oid[] = { 1, 3, 6, 1, 4, 1, 310, 1, 1, 0};
    int name_length = MAX_OID_LEN;
 
    char *cp1 = "localhost", *cp2 = "public";
    snmp_sess_init(&session);
    session.version = SNMP_VERSION_1;
    session.peername = cp1;
    session.remote_port = 162;
    session.community = (unsigned char*)cp2;
    session.community_len = strlen((char*)session.community);
    session.retries = 3;
    session.timeout = 2000;
    session.sessid = 0;
    pSession = snmp_add(&session,
        netsnmp_transport_open_client("snmptrap", session.peername),
        NULL, NULL);
 
//    status = create_trap_session("localhost", 0, "public", SNMP_VERSION_1, SNMP_MSG_TRAP);    
    if (pSession == NULL)
    {
        perror("snmptrap");
    }
    pdu = snmp_pdu_create(SNMP_MSG_TRAP);
 
    pdu->enterprise = (oid*)malloc(sizeof(ZHM05_oid));
    memcpy(pdu->enterprise, ZHM05_oid, sizeof(ZHM05_oid));
    pdu->enterprise_length = sizeof(ZHM05_oid)/sizeof(oid);
 
    pdu->trap_type = 6;    
        //TRAP的类型,coldStart(0)、warmStart(1、linkDown(2)、linkUp(3)、
        //authenticationFailure(4)、egpNeighborLoss(5)、enterpriseSpecific(6);
 
    pdu->specific_type = 0;
 
    status = snmp_add_var(pdu, SysSt_oid, sizeof(SysSt_oid)/sizeof(oid), 'i', "1");
//    status = snmp_add_var(pdu, "SysSt.0", name_length, 'i', (char*)&dd);
/*    if (status != 0)
    {
        snmp_sess_perror("add", &session);
        return -1;
    }*/
    //send_trap_vars(6, 0, pdu->variables);
    status = snmp_send(pSession, pdu);
    if (status != 0)
    {
        snmp_sess_perror("send", &session);
        return -2;
    }
    snmpd_free_trapsinks();
    snmp_free_pdu(pdu);
    return 0;
}

通过snmptrapd验证,程序已发送TRAP,管理端可正常接收TRAP

抱歉!评论已关闭.