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. 关于centos7中使用rpm方式安装mysql5.7版本后无法使用root登录的问题

    最近在centos7中通过rpm方式安装了最新版本的mysql-server 5.7 (mysql57-community-release-el7-7.noarch.rpm) ,发现安装成功后无法使用 ...

  2. sql server中bit字段实现取反操作

    update Fct_StockMove set Disabled=Disabled^1 WHERE MoveId='DCE268E0-2CB3-4D17-AC4E-0046FB459CAD'; 1. ...

  3. 转:ASP.NET MVC 将IList<T>导出Excel文档的泛型类

    /// <summary> /// 提供将泛型集合数据导出Excel文档. /// </summary> /// <typeparam name="T" ...

  4. 用 Qt 中的 QDomDocument类 处理 XML 文件

    XML,全称为 “可扩展标记语言”(extensible markup language).是一种非常方便的数据交换与数据存储的工具. 我们在取得一个XML格式的文件后,需要作句法分析去提取发布方提供 ...

  5. [na][dhcp]华为DHCP-重要

    近日遇到遇到控制器和wac对接的一些问题.尤其是地址池这块排查起来比较费事,且这些命令不容易找到,以下是能经常用到的命令. 1,查看ip是否冲突: (看下conflict字段) 2,防止冲突命令: 3 ...

  6. asp.net web网站权限系统的简单设计

    权限设计总结一句话就是:拥有权限的主体来管控系统的各种资源资源,及在资源上体现的各种操作. 概括来说就是,权限离不开三要素: 1:权限的主体 2:管控的资源 3:各种操作 现对三要素分别做简单的阐述 ...

  7. 新手MySQL工程师必备命令速查手册

    MySQL的基本操作可以包括两个方面:MySQL常用语句如高频率使用的增删改查(CRUD)语句和MySQL高级功能,如存储过程.触发器.事务处理等.而这两个方面又可以细分如下: 1.MySQL常用语句 ...

  8. maven(1)------使用myeclipse构建maven项目

    maven官网:http://maven.apache.org/ 依据官网的说法,Maven是一个采用纯Java编写的开源项目管理工具,基于一个称为项目对象模型(POM)的概念,可以管理项目的生命周期 ...

  9. angular学习笔记(十三)

    本篇主要介绍控制器的$scope中的数据是如何被改变的: 以下三种方法,都可以改变$scope中的number值: 1. 表达式: <span ng-click="number=num ...

  10. 每日英语:Why 'The Voice' Is China's No. 1 TV Show

    U.S. fans of the hit talent show 'The Voice' may take for granted that its judges sit with their bac ...