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代理端实现
一 实验背景 近期一个做焊接设备的朋友想在焊机上加入监控的新功能,实时获取焊机的温度.功耗等參数,还可简单控制,实现对集群焊接设备的网络化管理.而这个朋友不想在开发管理系统上花太多精力,想找一个 ...
随机推荐
- ubuntu中将某一程序设置为开机启动项的方法
一.简要说明 Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. 加载内核LILO启动之后,如果你选择了Linux作为准备引导的操作系统,第一个被加载 ...
- 读书笔记6pandas简单使用
一.序列Series,很像numpy中的array数组,可以由列表.元组.字典.numpy中的array来初始化 >>> from pandas import Series > ...
- Python shell对比
对Python.shell的一些思考 如果使用python去写脚本来处理日常事务的话,相对于shell是一件比较麻烦的事情,因为我可以使用shell在花费更少的时间内,比较熟练地使用awk.sed和g ...
- SVN清除,VS中SVN的错误以及全部替换
是tortoisesvn吧?右键,设置,已保存数据,认证数据,清除 ======= <<<<<<< .mine ||||||| .r15 >>&g ...
- linux 调试利器gdb, strace, pstack, pstree, lsof
1) 如何使用strace+pstack利器分析程序性能? http://www.cnblogs.com/bangerlee/archive/2012/04/30/2476190.html 此文有详细 ...
- java多线程实验 滚动字
package com.rgy.Test; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt ...
- 黑客编程教程(十三)多线程DOS程序
DOS基本原理相信大家都已经很熟悉了,DOS工具大家也用的很熟悉.在群里 经常有人说什么时候去DOS什么东西. 现在我们就自己编写一个DOS工具. #include <winsock2.h> ...
- nyoj228 士兵杀敌(5)插线问线
士兵杀敌(五) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为0~M,每次有任务的时候,总会有一批编号连在一起人请战 ...
- Freemarker-2.3.22 Demo - No02_绑定单个参数
package No02_绑定单个参数; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStre ...
- shell学习笔记之条件(二)
test或者[ #检查文件是否存在 if test -f read.c then ... fi if [ -f read.c ] then ... fi #如果then和if在同一行上,就应该用;把i ...