1. 首先创建一个简单的含有table变量的mib文件取名test-trap.mib,后缀名也可以是.txt 实际操作没有区别;
TEST-TRAP-MIB DEFINITIONS ::= BEGIN
IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, TimeTicks FROM SNMPv2-SMI
DisplayString, FROM SNMPv2-TC
enterprises
FROM RFC1155-SMI;
test OBJECT IDENTIFIER ::= { enterprises 1000 }
TestTraps OBJECT IDENTIFIER ::= { test 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
注:其中TestTrapDescription是trap触发时上送的信息,内容可以自定义,trap cpuRatioHigh中object{ }中可以添加多个对象,都将是trap触发时上送的信息;
2. 将创建好的mib文件放入默认目录中,一般默认目录为/usr/local/net-snmp, 或者放到别的目录中,在配置文件snmpd.conf中增加mibs +/home//mibs/mytrap test-trap.mib,或者直接修改默认的mib路径。
3.使用mib2c -c mib2c.notify.conf TEST-TRAP-MIB::TestTraps命令生成相应的.c和.h文件,如果该命令出错(ERROR: You don't have the SNMP perl module installed. Please obtain
this by getting the latest source release of the net-snmp toolkit from
http://www.net-snmp.org/download/ . Once you download the source and
unpack it, the perl module is contained in the perl/SNMP directory.
See the README file there for instructions.)请正常make make install重新安装net-snmp,原因未知。
生成的.c文件如下
/*
* Note: this file originally auto-generated by mib2c using
* $
*/
#include
#include
#include
#include "TestTraps.h"
extern const oid snmptrap_oid[];
extern const size_t snmptrap_oid_len;
int
send_cpuRatioHigh_trap( void )
{
netsnmp_variable_list *var_list = NULL;
const oid cpuRatioHigh_oid[] = { 1,3,6,1,4,1,1000,1,1 };
const oid TestTrapDescription_oid[] = { 1,3,6,1,4,1,1000,1,2,1, 0 };
/*
* 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,
TestTrapDescription_oid, OID_LENGTH(TestTrapDescription_oid),
ASN_OCTET_STR,
/* Set an appropriate value for TestTrapDescription */
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;
}
/*
* Note: this file originally auto-generated by mib2c using
* $
*/
#include
#include
#include
#include "TestTraps.h"
extern const oid snmptrap_oid[] = {1,3,6,1,6,3,1,1,4,1,0};
const size_t snmptrap_oid_len = OID_LENGTH(snmptrap_oid);
void init_TestTraps(void)
{
DEBUGMSGTL(("TestTraps","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,1000,1,1 };
const oid TestTrapDescription_oid[] = { 1,3,6,1,4,1,1000,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,
TestTrapDescription_oid, OID_LENGTH(TestTrapDescription_oid),
ASN_OCTET_STR,
/* Set an appropriate value for TestTrapDescription */
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);
}
6. 配置trap触发环境,在snmpd.conf添加trap2sink localhost或者trap2sink 127.0.0.1:162,重新启动snmpd 并用-c执行snmpd.conf
然后新建snmptrapd的配置文件snmptrapd.conf添加authcommunity log,execute,net public,为了能清楚看到trap的打印消息用命令
snmptrapd -d -f -Lo -c snmptrapd.conf启动snmptrapd
则每过5秒可以看到trap返回来的告警信息;
注:本人的一点见解,不足之处可以多多交流指正;