| 1 |
/** @example notification.c |
| 2 |
* This example shows how to send a notification from inside the |
| 3 |
* agent. In this case we do something really boring to decide |
| 4 |
* whether to send a notification or not: we simply sleep for 30 |
| 5 |
* seconds and send it, then we sleep for 30 more and send it again. |
| 6 |
* We do this through the snmp_alarm mechanisms (which are safe to |
| 7 |
* use within the agent. Don't use the system alarm() call, it won't |
| 8 |
* work properly). Normally, you would probably want to do something |
| 9 |
* to test whether or not to send an alarm, based on the type of mib |
| 10 |
* module you were creating. |
| 11 |
* |
| 12 |
* When this module is compiled into the agent (run configure with |
| 13 |
* --with-mib-modules="examples/notification") then it should send |
| 14 |
* out traps, which when received by the snmptrapd daemon will look |
| 15 |
* roughly like: |
| 16 |
* |
| 17 |
* 2002-05-08 08:57:05 localhost.localdomain [udp:127.0.0.1:32865]: |
| 18 |
* sysUpTimeInstance = Timeticks: (3803) 0:00:38.03 snmpTrapOID.0 = OID: netSnmpExampleNotification |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/* |
| 23 |
* start be including the appropriate header files |
| 24 |
*/ |
| 25 |
#include <net-snmp/net-snmp-config.h> |
| 26 |
#include <net-snmp/net-snmp-includes.h> |
| 27 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 28 |
|
| 29 |
/* |
| 30 |
* contains prototypes |
| 31 |
*/ |
| 32 |
#include "notification.h" |
| 33 |
|
| 34 |
/* |
| 35 |
* our initialization routine |
| 36 |
* (to get called, the function name must match init_FILENAME() |
| 37 |
*/ |
| 38 |
void |
| 39 |
init_notification(void) |
| 40 |
{ |
| 41 |
DEBUGMSGTL(("example_notification", |
| 42 |
"initializing (setting callback alarm)\n")); |
| 43 |
snmp_alarm_register(30, /* seconds */ |
| 44 |
SA_REPEAT, /* repeat (every 30 seconds). */ |
| 45 |
send_example_notification, /* our callback */ |
| 46 |
NULL /* no callback data needed */ |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** here we send a SNMP v2 trap (which can be sent through snmpv3 and |
| 51 |
* snmpv1 as well) and send it out. |
| 52 |
* |
| 53 |
* The various "send_trap()" calls allow you to specify traps in different |
| 54 |
* formats. And the various "trapsink" directives allow you to specify |
| 55 |
* destinations to receive different formats. |
| 56 |
* But *all* traps are sent to *all* destinations, regardless of how they |
| 57 |
* were specified. |
| 58 |
* |
| 59 |
* |
| 60 |
* I.e. it's |
| 61 |
* @verbatim |
| 62 |
* ___ trapsink |
| 63 |
* / |
| 64 |
* send_easy_trap \___ [ Trap ] ____ trap2sink |
| 65 |
* ___ [ Generator ] |
| 66 |
* send_v2trap / [ ] ----- informsink |
| 67 |
* \____ |
| 68 |
* trapsess |
| 69 |
* |
| 70 |
* *Not* |
| 71 |
* send_easy_trap -------------------> trapsink |
| 72 |
* send_v2trap -------------------> trap2sink |
| 73 |
* ???? -------------------> informsink |
| 74 |
* ???? -------------------> trapsess |
| 75 |
* @endverbatim |
| 76 |
*/ |
| 77 |
void |
| 78 |
send_example_notification(unsigned int clientreg, void *clientarg) |
| 79 |
{ |
| 80 |
/* |
| 81 |
* define the OID for the notification we're going to send |
| 82 |
* NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification |
| 83 |
*/ |
| 84 |
oid notification_oid[] = |
| 85 |
{ 1, 3, 6, 1, 4, 1, 8072, 2, 3, 0, 1 }; |
| 86 |
size_t notification_oid_len = OID_LENGTH(notification_oid); |
| 87 |
static u_long count = 0; |
| 88 |
|
| 89 |
/* |
| 90 |
* In the notification, we have to assign our notification OID to |
| 91 |
* the snmpTrapOID.0 object. Here is it's definition. |
| 92 |
*/ |
| 93 |
oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 }; |
| 94 |
size_t objid_snmptrap_len = OID_LENGTH(objid_snmptrap); |
| 95 |
|
| 96 |
/* |
| 97 |
* define the OIDs for the varbinds we're going to include |
| 98 |
* with the notification - |
| 99 |
* NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate and |
| 100 |
* NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatName |
| 101 |
*/ |
| 102 |
oid hbeat_rate_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 1, 0 }; |
| 103 |
size_t hbeat_rate_oid_len = OID_LENGTH(hbeat_rate_oid); |
| 104 |
oid hbeat_name_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 2, 0 }; |
| 105 |
size_t hbeat_name_oid_len = OID_LENGTH(hbeat_name_oid); |
| 106 |
|
| 107 |
/* |
| 108 |
* here is where we store the variables to be sent in the trap |
| 109 |
*/ |
| 110 |
netsnmp_variable_list *notification_vars = NULL; |
| 111 |
const char *heartbeat_name = "A girl named Maria"; |
| 112 |
#ifdef RANDOM_HEARTBEAT |
| 113 |
int heartbeat_rate = rand() % 60; |
| 114 |
#else |
| 115 |
int heartbeat_rate = 30; |
| 116 |
#endif |
| 117 |
|
| 118 |
DEBUGMSGTL(("example_notification", "defining the trap\n")); |
| 119 |
|
| 120 |
/* |
| 121 |
* add in the trap definition object |
| 122 |
*/ |
| 123 |
snmp_varlist_add_variable(¬ification_vars, |
| 124 |
/* |
| 125 |
* the snmpTrapOID.0 variable |
| 126 |
*/ |
| 127 |
objid_snmptrap, objid_snmptrap_len, |
| 128 |
/* |
| 129 |
* value type is an OID |
| 130 |
*/ |
| 131 |
ASN_OBJECT_ID, |
| 132 |
/* |
| 133 |
* value contents is our notification OID |
| 134 |
*/ |
| 135 |
(u_char *) notification_oid, |
| 136 |
/* |
| 137 |
* size in bytes = oid length * sizeof(oid) |
| 138 |
*/ |
| 139 |
notification_oid_len * sizeof(oid)); |
| 140 |
|
| 141 |
/* |
| 142 |
* add in the additional objects defined as part of the trap |
| 143 |
*/ |
| 144 |
|
| 145 |
snmp_varlist_add_variable(¬ification_vars, |
| 146 |
hbeat_rate_oid, hbeat_rate_oid_len, |
| 147 |
ASN_INTEGER, |
| 148 |
(u_char *)&heartbeat_rate, |
| 149 |
sizeof(heartbeat_rate)); |
| 150 |
|
| 151 |
/* |
| 152 |
* if we want to insert additional objects, we do it here |
| 153 |
*/ |
| 154 |
if (heartbeat_rate < 30 ) { |
| 155 |
snmp_varlist_add_variable(¬ification_vars, |
| 156 |
hbeat_name_oid, hbeat_name_oid_len, |
| 157 |
ASN_OCTET_STR, |
| 158 |
heartbeat_name, strlen(heartbeat_name)); |
| 159 |
} |
| 160 |
|
| 161 |
/* |
| 162 |
* send the trap out. This will send it to all registered |
| 163 |
* receivers (see the "SETTING UP TRAP AND/OR INFORM DESTINATIONS" |
| 164 |
* section of the snmpd.conf manual page. |
| 165 |
*/ |
| 166 |
++count; |
| 167 |
DEBUGMSGTL(("example_notification", "sending trap %ld\n",count)); |
| 168 |
send_v2trap(notification_vars); |
| 169 |
|
| 170 |
/* |
| 171 |
* free the created notification variable list |
| 172 |
*/ |
| 173 |
DEBUGMSGTL(("example_notification", "cleaning up\n")); |
| 174 |
snmp_free_varbind(notification_vars); |
| 175 |
} |