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. 不能与abstruct共同修饰方法的关键字

    一 抽象类:动态方法至少有一个是抽象方法. 其中abstruct关键字修饰的方法不能与哪些关键字共同修饰? 1private 因为private修饰的方法在子类中是隐藏的.不可见的.而abstruct ...

  2. mysql-5.7 innodb_buffer_pool刷新机制详解

    一.innodb的脏页刷新机制说明: 1.当innodb中的脏页比例超过innodb_max_dirty_pages_pct_lwm的值时,这个时候innodb就会开始刷新脏页到磁盘. 2.当inno ...

  3. php导出excel(xls或xlsx)

    $titles = array('订单号','商品结算码','合同号','供应商名称','专柜','商品名称','商品货号','商品单价','商品总价','供应商结算金额','商品数量','商品促销优 ...

  4. textureCache中的等价路径问题

    自己的引擎里做了个简单的TextueCache,每次新创建一个纹理,先到TextureCache里查找有没有路径相同的,如果有就直接返回纹理,如果没有加载图片创建纹理并将图片路径缓存起来.另外为了标准 ...

  5. C#操作txt文件并清空添加操作

    C#操作txt文件,进行清空添加操作的例子.代码: //把txt清空 FileStream stream = File.Open(Adr,FileMode.OpenOrCreate,FileAcces ...

  6. 【Android】3.11 地理编码功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 地理编码指的是将地址信息建立空间坐标关系的过程,提供了地理坐标和地址之间相互转换的能力. 地理编码分 ...

  7. 关于haproxy负载均衡的算法整理

    目前haproxy支持的负载均衡算法有如下8种: 1:roudrobin 表示简单的轮询,每个服务器根据权重轮流使用,在服务器的处理时间平均分配的情况下这是最流畅和公平的算法.该算法是动态的,对于实例 ...

  8. css 点点加载demo

    content字符生成配合CSS3 animation的点点点loading效果实例 代码: CSS代码: dot { display: inline-block; height: 1em; line ...

  9. HDOJ 1814 Peaceful Commission

    经典2sat裸题,dfs的2sat能够方便输出字典序最小的解... Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Mem ...

  10. Oracle 外键约束子表、父表

    CREATE TABLE employees(     employee_id      NUMBER(6),     last_name        VARCHAR2(25) NOT NULL, ...