SNMP Trap Authoring

Source: Internet
Author: User
Tags snmp

1. MIB Library
View NetThe installation directory for the-SNMP. /usr/share/snmp/MIBs directory: NET-snmp-examples-Mib.mib The contents of this part are as follows: Netsnmpexampleheartbeatrate OBJECT-TYPE SYNTAX Integer32 MAX-access accessible- for-Notify STATUS current DESCRIPTION"a simple integer object, to act as A payload for thenetsnmpexampleheartbeatnotification. The value has no real meaning, but isNominally the interval (inchseconds) between successive heartbeat notifications.":: = {netsnmpexamplenotificationobjects1}netsnmpexampleheartbeatname OBJECT-TYPE SYNTAX snmpadminstring MAX-access accessible- for-Notify STATUS current DESCRIPTION"A Simple string object, to act as an optional payload forThe netsnmpexampleheartbeatnotification. This varbind isNot part of the notification definition, so isOptional and need not being includedinchThe notification payload. The value has a real meaning, but the romantically inclinedObjectof the sender's affection,and hence the cause of the heart beating faster.":: = {netsnmpexamplenotificationobjects2}netsnmpexampleheartbeatnotification NOTIFICATION-TYPE OBJECTS {netsnmpexampleheartbeatrate} STATUS current DESCRIPTION"An example notification, used to illustrate thedefinition and generation of trap and inform PDUs (including the use of both standard and additional VarbindsinchThe notification payload). This notification'll typically be sent every -SecondsusingThe code foundinchThe example Module Agent/mibgroup/examples/notification.c":: = {Netsnmpexamplenotificationprefix1} netsnmpexamplenotification OBJECT-TYPE SYNTAX snmpadminstring MAX-access accessible- for-notify STATUS obsolete DESCRIPTION"This object is improperly defined for its original purpose,And should no longer be used.":: = {netsnmpexamplenotifications1 }
2, the source file its corresponding test code in: NET-snmp-5.7.2/agent/mibgroup/examples/NOTIFICATION.C If you want to write it yourself, follow the net-snmp-examples-the trap is written in the Mib.mib file,
the tools for generating. C and. h from the MIB are then used to generate the. c and. h files using the mib2c.nofity.conf configuration file.
, and then modify the function's return value and parameters, and so on, change to similar to NOFIFICATION.C can be run.
NOTIFICATION.C sends a trap to the MiB browser each 30 seconds, as shown below.

#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 ()*/voidInit_notification (void) {DEBUGMSGTL ("example_notification", "initializing (setting callback alarm) \ n")); Snmp_alarm_register ( -,/*seconds*/Sa_repeat,/*repeat (every-seconds).*/Send_example_notification,/*Our callback*/NULL/*no callback data needed*/ );}voidsend_example_notification (unsignedintClientreg,void*Clientarg) { /** Define the OID for the notification we ' re going to send * net-snmp-examples-mib::netsnmpexampleheartbeatnot Ification*/OID notification_oid[]= { 1,3,6,1,4,1,8072,2,3,0,1 }; size_t Notification_oid_len=oid_length (notification_oid); StaticU_long count =0; /** In the notification, we had to assign our notification OIDs 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's where we store the variables to being sent in the trap*/netsnmp_variable_list*notification_vars =NULL; Const Char*heartbeat_name ="A girl named Maria"; #ifdef random_heartbeatintHeartbeat_rate = rand ()% -;#else intHeartbeat_rate = -;#endifDebugmsgtl (("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 did 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 would 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);}

The installation directory of the Mg-soft software also contains some MIB libraries for traps. For example, ups-mib.my. There is a way to write the MIB library for SNMP trap. keywords, notification-TYPE. Upstraponbattery notification-TYPE OBJECTS {upsestimatedminutesremaining, upssecondsonbattery, upsconfiglowbatttime} statu S Current DESCRIPTION"The UPS is operating on battery power. This trap is persistent andIsResent at one minute intervals until the UPS either turns off orIsNo longer running on battery.":: = {Upstraps1} upstraptestcompleted notification-TYPE OBJECTS {upstestid, Upstestspinlock, Upstestresultssummary, Upstestresultsdetail, Upsteststarttime, Upstestelapsedtime} STATUS Current DESCRIPTION"This trap is sent upon completion of a UPS diagnostic test.":: = {Upstraps2} upstrapalarmentryadded notification-TYPE OBJECTS {--Upsalarmid, UPSALARMDESCR} STATUS current DESCRIPTION"This trap was sent each time a alarm is inserted into to the alarm table. ItIsfor Upsalarmonbattery and upsalarmtestinprogress. 3} upstrapalarmentryremoved notification-type OBJECTS {--  Upsalarmid, UPSALARMDESCR} STATUS current DESCRIPTION  "is sent on the removal of all alarms except for upsalarmtestinprogress. 4} 

SNMP Trap Authoring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.