这里我们建立一个mib文件,命名为TEST-TRAP-MIB.txt,放在/usr/local/net-snmp/share/snmp/mibs/目录下因为这个目录是snmpd的默认目录,只要把MIB库放入该目录就可以自动加载MIB库,否则需要修改snmpd.conf文件,自定义的MIB文件如下:
--开始
TEST-TRAP-MIB DEFINITIONS ::= BEGIN
--引入部分
IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, TimeTicks FROM SNMPv2-SMI
DisplayString, FROM SNMPv2-TC
enterprises
FROM RFC1155-SMI; --引用结束,用分号
--定义节点
--enterprises的OID是1.3.6.1.4
testTrap OBJECT IDENTIFIER ::= { enterprises 12581 }
TestTraps OBJECT IDENTIFIER ::= { testTrap 1 }
cpuRatioHigh NOTIFICATION-TYPE
OBJECTS {SystemTrapDescription}
STATUS current
DESCRIPTION "."
::= { TestTraps 1 }
TestDescription OBJECT IDENTIFIER ::= { TestTraps 2 }
TestTrapDescription OBJECT-TYPE
SYNTAX DisplayString (SIZE (0..256))
MAX-ACCESS read-only
STATUS current
DESCRIPTION " "
::= { TestDescription 1 }
--结束定义
END
写完后我们测一个MIB库有没有问题,在linux机器上用snmptranslate-Tp -IR TEST-GET-MIB::testGet显示结果如下:(这个测试不需要启动snmpd进程)
[root@localhostbin]# ./snmptranslate -Tp -IR TEST-TRAP-MIB::testTrap
+--testTrap(12581)
|
+--TestTraps(1)
|
+--cpuRatioHigh(1)
|
+--TestDescription(2)
|
+-- -R-- String TestTrapDescription(1)
Textual Convention: DisplayString
Size: 0..256
OK,snmp自动发现了这个MIB库, 有了自定义的OID,接下来开始添加处理程序。
执行envMIBS="+/usr/local/net-snmp/share/snmp/mibs/TEST-TRAP-MIB.txt" ./mib2ctestTRAP,会引导你逐渐生成testTrap.h和testTrap.c,先选2再选1然后再选y。
生成的testTrap.h如下
/*
* Note: this file originally auto-generated by mib2c using
* $
*/
#ifndef TESTTRAP_H
#define TESTTRAP_H
/*
* function declarations
*/
int send_cpuRatioHigh_trap(void);
#endif /* TESTTRAP_H */
修改后
testTrap.c
文件代码如下:
/*
* Note: this file originally auto-generated by mib2c using
* $
*/
#include
#include
#include
#include "testTrap.h"
void read_cpudata_repeat(unsigned int clientreg, void *clientarg);
extern const oid snmptrap_oid[] = {1,3,6,1,6,3,1,1,4,1,0};
extern const size_t snmptrap_oid_len = OID_LENGTH(snmptrap_oid);
void init_testTrap(void)
{
DEBUGMSGTL(("testTrap","Initializing\n"));
snmp_alarm_register(1,SA_REPEAT,read_cpudata_repeat, NULL);
}
int
send_cpuRatioHigh_trap(void)
{
netsnmp_variable_list *var_list = NULL;
const oid cpuRatioHigh_oid[] = { 1, 3, 6, 1, 4, 1, 12581, 1, 1 };
const oid SystemTrapDescription_oid[] ={ 1,3,6,1,4,1,12581,1,2,1, 0 };
static char TestTrapDescription[50];
strcpy(TestTrapDescription,"CPU使用率过高");
/*
* Set the snmpTrapOid.0 value
*/
snmp_varlist_add_variable(&var_list,
snmptrap_oid, snmptrap_oid_len,
ASN_OBJECT_ID,
cpuRatioHigh_oid, sizeof(cpuRatioHigh_oid));
/*
* Add any objects from the trap definition
*/
snmp_varlist_add_variable(&var_list,
SystemTrapDescription_oid,
OID_LENGTH(SystemTrapDescription_oid),
ASN_OCTET_STR,
/*
* Set an appropriate value for SystemTrapDescription
*/
NULL, 0);
/*
* Add any extra (optional) objects here
*/
/*
* Send the trap to the list of configured destinations
* and clean up
*/
send_v2trap(var_list);
snmp_free_varbind(var_list);
return SNMP_ERR_NOERROR;
}
void judge_send_cputrap(int cpu)
{
static unsigned int cputrap_clientreg = 0;
if(cpu > 80)
{
if(cputrap_clientreg == 0){
send_cpuRatioHigh_trap();
cputrap_clientreg = snmp_alarm_register(5,SA_REPEAT,send_cpuRatioHigh_trap,NULL);
}
}
else
{
if(cputrap_clientreg != 0)
{
snmp_alarm_unregister(cputrap_clientreg);
cputrap_clientreg = 0;
}
}
}
void read_cpudata_repeat(unsigned int clientreg, void *clientarg)
{
int cpu = 90;
judge_send_cputrap(cpu);
}
把testGet.c和testGet.h复制到/net-snmp-5.7.3/agent/mibgroups/路径下
设置编译参数(红色部分即为加上我们自己的mib模块)
[[email protected]]# ./configure --prefix=/usr/local/net-snmp --with-mib-modules="testTrap
查看文件net-snmp-5.7.3/agent/mibgroup/mib_module_inits.h,发现已经添加到初始化中。
/*This file is automatically generated by configure. Do not modify by hand. */
if (should_init("testGet"))init_testTrap
编译并安装
[[email protected]]# make
[[email protected]]# make instal
配置trap触发环境
在snmpd.conf添加trap2sink localhost或者trap2sink 127.0.0.1:162
新建snmptrapd的配置文件snmptrapd.conf添加authcommunity log,execute,net public
启动snmpd服务
[root@localhostsbin]# ./snmpd -f -Le
Turning on AgentXmaster support.
NET-SNMP version5.7.3
No Shmem line in/proc/meminfo
启动snmptrapd 服务
[root@localhostsbin]# ./snmptrapd -d -f -Lo -c snmptrapd.conf
则每过5秒可以看到trap返回来的告警信息,说明我们添加自定义MIB trap操作成功!