snmp trap编写
1、MIB库
查看net-snmp的安装目录。/usr/share/snmp/mibs目录下:
NET-SNMP-EXAMPLES-MIB.mib本件部分内容如下:
netSnmpExampleHeartbeatRate OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS accessible-for-notify
STATUS current
DESCRIPTION
"A simple integer object, to act as a payload for the
netSnmpExampleHeartbeatNotification. The value has
no real meaning, but is nominally the interval (in
seconds) between successive heartbeat notifications."
::= { netSnmpExampleNotificationObjects }
netSnmpExampleHeartbeatName OBJECT-TYPE
SYNTAX SnmpAdminString
MAX-ACCESS accessible-for-notify
STATUS current
DESCRIPTION
"A simple string object, to act as an optional payload
for the netSnmpExampleHeartbeatNotification. This varbind
is not part of the notification definition, so is optional
and need not be included in the notification payload.
The value has no real meaning, but the romantically inclined
may take it to be the object of the sender's affection,
and hence the cause of the heart beating faster."
::= { netSnmpExampleNotificationObjects } netSnmpExampleHeartbeatNotification NOTIFICATION-TYPE
OBJECTS { netSnmpExampleHeartbeatRate }
STATUS current
DESCRIPTION
"An example notification, used to illustrate the
definition and generation of trap and inform PDUs
(including the use of both standard and additional
varbinds in the notification payload).
This notification will typically be sent every
seconds, using the code found in the example module
agent/mibgroup/examples/notification.c"
::= { netSnmpExampleNotificationPrefix } netSnmpExampleNotification OBJECT-TYPE
SYNTAX SnmpAdminString
MAX-ACCESS accessible-for-notify
STATUS obsolete
DESCRIPTION
"This object was improperly defined for its original purpose,
and should no longer be used."
::= { netSnmpExampleNotifications }
2、源文件
其对应的测试代码在:net-snmp-5.7./agent/mibgroup/examples/notification.c
如果要自己编写的话,那么按照NET-SNMP-EXAMPLES-MIB.mib文件中trap的编写方法编写trap,
再通过mib生成.c和.h的工具使用mib2c.nofity.conf这个配置文件生成.c和.h文件。
,再修改一下函数的返回值和参数等,更改成与nofification.c类似就可以运行。
notification.c如下所示,每个30秒发送一个trap给mib browser。 #include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h> /*
* contains prototypes
*/
#include "notification.h" /*
* our initialization routine
* (to get called, the function name must match init_FILENAME()
*/
void
init_notification(void)
{
DEBUGMSGTL(("example_notification",
"initializing (setting callback alarm)\n"));
snmp_alarm_register(, /* seconds */
SA_REPEAT, /* repeat (every 30 seconds). */
send_example_notification, /* our callback */
NULL /* no callback data needed */
);
}
void
send_example_notification(unsigned int clientreg, void *clientarg)
{
/*
* define the OID for the notification we're going to send
* NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification
*/
oid notification_oid[] =
{ , , , , , , , , , , };
size_t notification_oid_len = OID_LENGTH(notification_oid);
static u_long count = ; /*
* In the notification, we have to assign our notification OID to
* the snmpTrapOID.0 object. Here is it's definition.
*/
oid objid_snmptrap[] = { , , , , , , , , , , };
size_t objid_snmptrap_len = OID_LENGTH(objid_snmptrap); /*
* define the OIDs for the varbinds we're going to include
* with the notification -
* NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate and
* NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatName
*/
oid hbeat_rate_oid[] = { , , , , , , , , , , , };
size_t hbeat_rate_oid_len = OID_LENGTH(hbeat_rate_oid);
oid hbeat_name_oid[] = { , , , , , , , , , , , };
size_t hbeat_name_oid_len = OID_LENGTH(hbeat_name_oid); /*
* here is where we store the variables to be sent in the trap
*/
netsnmp_variable_list *notification_vars = NULL;
const char *heartbeat_name = "A girl named Maria";
#ifdef RANDOM_HEARTBEAT
int heartbeat_rate = rand() % ;
#else
int heartbeat_rate = ;
#endif DEBUGMSGTL(("example_notification", "defining the trap\n")); /*
* add in the trap definition object
*/
snmp_varlist_add_variable(¬ification_vars,
/*
* the snmpTrapOID.0 variable
*/
objid_snmptrap, objid_snmptrap_len,
/*
* value type is an OID
*/
ASN_OBJECT_ID,
/*
* value contents is our notification OID
*/
(u_char *) notification_oid,
/*
* size in bytes = oid length * sizeof(oid)
*/
notification_oid_len * sizeof(oid)); /*
* add in the additional objects defined as part of the trap
*/ snmp_varlist_add_variable(¬ification_vars,
hbeat_rate_oid, hbeat_rate_oid_len,
ASN_INTEGER,
(u_char *)&heartbeat_rate,
sizeof(heartbeat_rate)); /*
* if we want to insert additional objects, we do it here
*/
if (heartbeat_rate < ) {
snmp_varlist_add_variable(¬ification_vars,
hbeat_name_oid, hbeat_name_oid_len,
ASN_OCTET_STR,
heartbeat_name, strlen(heartbeat_name));
} /*
* send the trap out. This will send it to all registered
* receivers (see the "SETTING UP TRAP AND/OR INFORM DESTINATIONS"
* section of the snmpd.conf manual page.
*/
++count;
DEBUGMSGTL(("example_notification", "sending trap %ld\n",count));
send_v2trap(notification_vars); /*
* free the created notification variable list
*/
DEBUGMSGTL(("example_notification", "cleaning up\n"));
snmp_free_varbind(notification_vars);
}
mg-soft软件的安装目录中也包含了一些关于trap的mib库.例如UPS-MIB.my。其中有关于snmp trap的mib库编写方法。关键字,NOTIFICATION-TYPE。
upsTrapOnBattery NOTIFICATION-TYPE
OBJECTS { upsEstimatedMinutesRemaining, upsSecondsOnBattery,
upsConfigLowBattTime }
STATUS current
DESCRIPTION
"The UPS is operating on battery power. This trap is
persistent and is resent at one minute intervals until
the UPS either turns off or is no longer running on
battery."
::= { upsTraps 1 } upsTrapTestCompleted NOTIFICATION-TYPE
OBJECTS { upsTestId, upsTestSpinLock,
upsTestResultsSummary, upsTestResultsDetail,
upsTestStartTime, upsTestElapsedTime }
STATUS current
DESCRIPTION
"This trap is sent upon completion of a UPS diagnostic
test."
::= { upsTraps 2 } upsTrapAlarmEntryAdded NOTIFICATION-TYPE
OBJECTS {
-- upsAlarmId,
upsAlarmDescr }
STATUS current
DESCRIPTION
"This trap is sent each time an alarm is inserted into
to the alarm table. It is sent on the insertion of
all alarms except for upsAlarmOnBattery and
upsAlarmTestInProgress."
::= { upsTraps 3 } upsTrapAlarmEntryRemoved NOTIFICATION-TYPE
OBJECTS {
-- upsAlarmId,
upsAlarmDescr }
STATUS current
DESCRIPTION
"This trap is sent each time an alarm is removed from
the alarm table. It is sent on the removal of all
alarms except for upsAlarmTestInProgress."
::= { upsTraps 4 }
snmp trap编写的更多相关文章
- (转)浅谈 Linux 系统中的 SNMP Trap
原文:https://www.ibm.com/developerworks/cn/linux/l-cn-snmp/index.html 简介 本文讲解 SNMP Trap,在介绍 Trap 概念之前, ...
- SNMP学习笔记之SNMP TRAP简介、流程以及使用Python实现接受Trap信息
0x00 SNMP TRAP简介 SNMP(Simple Network Management Protocol) trap是一种很有用,但是也容易让人难以理解的协议. 虽然名字叫做简单网络管理协议, ...
- 使用Zabbix的SNMP trap监控类型监控设备的一个例子
本文以监控绿盟设备为例. 1.登录被监控的设备的管理系统,配置snmptrap地址指向zabbix服务器或代理服务器. snmptrap地址也叫陷阱. 2.验证是否能在zabbix服务器或代理服务器上 ...
- 使用Wireshark抓取SNMP Trap包
Wireshark SNMP Trap 过滤关键字:snmp && udp.dstport == 162
- SNMP TRAP报文解析
转载地址: https://blog.csdn.net/eric_sunah/article/details/19557683 SNMP的报文格式 SNMP代理和管理站通过SNMP协议中的标准消息进行 ...
- 关于Snmp的Trap代码开发之坑
最近是被这个snmp的trap给坑了,其实回想起来主要是对这个不了解.特别是对snmp协议研究不够深入, 真的不想看这些协议东西,只想大概知道.结果在开发snmp trap时候被坑了,下面列下自己踩到 ...
- snmp 学习
SNMP:“简单网络管理协议”,用于网络管理的协议.SNMP用于网络设备的管理.SNMP的工作方式:管理员需要向设备获取数据,所以SNMP提供了“读”操作:管理员需要向设备执行设置操作,所以SNMP提 ...
- snmp数据包分析
今天看了一下snmp数据包的报文格式,用wireshark抓了两个数据包来分析. 先说说snmp get-request的书报包格式吧,get-next-request,get-response,se ...
- 基于W5500的嵌入式SNMP代理端实现
一 实验背景 近期一个做焊接设备的朋友想在焊机上加入监控的新功能,实时获取焊机的温度.功耗等參数,还可简单控制,实现对集群焊接设备的网络化管理.而这个朋友不想在开发管理系统上花太多精力,想找一个 ...
随机推荐
- oracle Database link 创建
http://www.cnblogs.com/yhason/p/3735319.html
- Git 基本分支规范
基本代码分支应该分为两类,一类是主要分支,包括线上主分支 Master 和开发主分支Develop:另一类是辅助分支,包括测试分支 Release,线上紧急修复分支 Hotfix,以及功能开发分支 F ...
- 读书笔记5基于matplotlib画图
一.导入需要的模块 import numpy as np import matplotlib.pyplot as plt import seaborn as sns import scipy.stat ...
- 使用C语言调用mysql数据库编程实战以及技巧
今天编写使用C语言调用mysql数据库编程实战以及技巧.为其它IT同行作为參考,当然有错误能够留言,共同学习. 一.mysql数据库的C语言经常使用接口API 1.首先当然是链接数据库mysql_re ...
- [javase学习笔记]-6.3 对象的内存体现
这一节我们来简单的看一看对象在内存中是什么样子呢,怎样体现. 我们以上一节的測试代码为例. 我们在函数的内存分配分析过.当该代码执行时,首先会载入主函数在栈内存中为main函数分配一个空间: 然后执行 ...
- WCF入门学习3-配置文件与部署iis
配置文件设置 --------------------------------------------------- 创建的时候都会有个配置文件,其实有一个WCF配置编辑器,右键就可以点出来设置. 需 ...
- Linux系统中 Sublime Text 中文 GBK 文件乱码问题
Sublime Text 是一个很不错编辑器,具有漂亮的界面和强大的功能.再加上丰富的插件,而且还跨平台,绝对是一款实打实的神器啊! 众所周知,Sublime Text 对中文支持的极差,可以说几乎就 ...
- ny214 单调递增子序列(二) 动态规划
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序 ...
- 如何保证RabbitMQ的消息不丢失及其背后的原理
一.消息为什么丢失 RabbitMQ默认情况下的交换机和队列以及消息是非持久化的,也就是说在服务器重启或者宕机恢复后,之前创建的交换机和队列都将不复存在,之前未消费的消息也就消失不见了.原因在于每个队 ...
- VC++ 进度条的使用
进度条控件封装在CProcessCtrl类中 1.SetRange和SetRange32方法来设置进度条显示范围. 语法格式: void SetRange(short nLower, short nU ...