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(&notification_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(&notification_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(&notification_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编写的更多相关文章

  1. (转)浅谈 Linux 系统中的 SNMP Trap

    原文:https://www.ibm.com/developerworks/cn/linux/l-cn-snmp/index.html 简介 本文讲解 SNMP Trap,在介绍 Trap 概念之前, ...

  2. SNMP学习笔记之SNMP TRAP简介、流程以及使用Python实现接受Trap信息

    0x00 SNMP TRAP简介 SNMP(Simple Network Management Protocol) trap是一种很有用,但是也容易让人难以理解的协议. 虽然名字叫做简单网络管理协议, ...

  3. 使用Zabbix的SNMP trap监控类型监控设备的一个例子

    本文以监控绿盟设备为例. 1.登录被监控的设备的管理系统,配置snmptrap地址指向zabbix服务器或代理服务器. snmptrap地址也叫陷阱. 2.验证是否能在zabbix服务器或代理服务器上 ...

  4. 使用Wireshark抓取SNMP Trap包

    Wireshark SNMP Trap 过滤关键字:snmp && udp.dstport == 162

  5. SNMP TRAP报文解析

    转载地址: https://blog.csdn.net/eric_sunah/article/details/19557683 SNMP的报文格式 SNMP代理和管理站通过SNMP协议中的标准消息进行 ...

  6. 关于Snmp的Trap代码开发之坑

    最近是被这个snmp的trap给坑了,其实回想起来主要是对这个不了解.特别是对snmp协议研究不够深入, 真的不想看这些协议东西,只想大概知道.结果在开发snmp trap时候被坑了,下面列下自己踩到 ...

  7. snmp 学习

    SNMP:“简单网络管理协议”,用于网络管理的协议.SNMP用于网络设备的管理.SNMP的工作方式:管理员需要向设备获取数据,所以SNMP提供了“读”操作:管理员需要向设备执行设置操作,所以SNMP提 ...

  8. snmp数据包分析

    今天看了一下snmp数据包的报文格式,用wireshark抓了两个数据包来分析. 先说说snmp get-request的书报包格式吧,get-next-request,get-response,se ...

  9. 基于W5500的嵌入式SNMP代理端实现

     一 实验背景 近期一个做焊接设备的朋友想在焊机上加入监控的新功能,实时获取焊机的温度.功耗等參数,还可简单控制,实现对集群焊接设备的网络化管理.而这个朋友不想在开发管理系统上花太多精力,想找一个 ...

随机推荐

  1. Windows 上安装docker

    Windows 上安装docker //(第一种)下载boot2docker https://github.com/boot2docker/windows-installer/releases //( ...

  2. Android 常见面试题

    这些面试是我之前总结的 .觉得还不错,就贴出来与大家分享一下.当中有不少问题.也是我以前被面试官问过的问题,另一些基础问题总结(既然是基础知识 ,必定是成为一名的 Android 开发者 所必须掌握的 ...

  3. 认识k_BackingField,微软自己的序列化和反序列化

    事情从Json的序列化和反序列化说起. 在C#2.0的项目中,以前经常使用Json.Net实现序列化和反序列化.后来从c#3.0中开始使用新增的DataContractJsonSerializer进行 ...

  4. android framework-下载Android系统源代码

    □ apt-get install git-core curl #先下载这两个工具 □ mkdir android-froyo #建立下载目录 □ cd android-froyo #进入下载目录 □ ...

  5. angular学习笔记(四)- input元素的ng-model属性

    input元素的ng-model属性: 用于将input的值和变量的值进行双向绑定 <!DOCTYPE html> <html ng-app> <head> < ...

  6. 每日英语:Does Evolution Want Us To Be Unhappy?

    Samuel Johnson called it the vanity of human wishes, and Buddhists talk about the endless cycle of d ...

  7. hdoj1241 Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. gulp入门与一些基本设置

    这里是gulp入门的一些操作,实现了编译sass文件.压缩.合并.添加版本号等基本功能. 友情提示,如果npm出现无法下载可以安装 cnpm.在安装完Nodejs 后 npm install cnpm ...

  9. configure new Linux/Mac

    vim   http://www.cnblogs.com/wswang/p/5088078.html sublime   http://jingyan.baidu.com/article/3aed63 ...

  10. 采用Filter的方法解决HttpServletRequest.getParameter乱码的问题

    其实就是利用这么一个原理: byte[] bytes = str.getBytes("iso-8859-1"); String result = new String(bytes, ...