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 1 }
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 2 }
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
30 seconds, using the code found in the example module
agent/mibgroup/examples/notification.c"
::= { netSnmpExampleNotificationPrefix 1 }
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 1 }
2、源文件
其對應的測試代碼在:net-snmp-5.7.2/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(30, /* 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[] =
{ 1, 3, 6, 1, 4, 1, 8072, 2, 3, 0, 1 };
size_t notification_oid_len = OID_LENGTH(notification_oid);
static u_long count = 0;
/*
* In the notification, we have to assign our notification OID to
* the snmpTrapOID.0 object. Here is it's definition.
*/
oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
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[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 1, 0 };
size_t hbeat_rate_oid_len = OID_LENGTH(hbeat_rate_oid);
oid hbeat_name_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 2, 0 };
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() % 60;
#else
int heartbeat_rate = 30;
#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 < 30 ) {
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 }