| 1 |
/* |
| 2 |
* watched.c |
| 3 |
* $Id$ |
| 4 |
* |
| 5 |
*/ |
| 6 |
/** @example watched.c |
| 7 |
* These examples creates some scalar registrations that allows |
| 8 |
* some simple variables to be accessed via SNMP. In a more |
| 9 |
* realistic example, it is likely that these variables would also be |
| 10 |
* manipulated in other ways outside of SNMP gets/sets. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* |
| 14 |
* start by including the appropriate header files |
| 15 |
*/ |
| 16 |
#include <net-snmp/net-snmp-config.h> |
| 17 |
#include <net-snmp/net-snmp-includes.h> |
| 18 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 19 |
|
| 20 |
void init_watched_string(void); |
| 21 |
|
| 22 |
void init_watched(void) |
| 23 |
{ |
| 24 |
init_watched_string(); |
| 25 |
} |
| 26 |
|
| 27 |
void init_watched_string(void) |
| 28 |
{ |
| 29 |
/* |
| 30 |
* the storage for our string. It must be static or allocated. |
| 31 |
* we use static here for simplicity. |
| 32 |
*/ |
| 33 |
static char my_string[256] = "So long, and thanks for all the fish!"; |
| 34 |
|
| 35 |
/* |
| 36 |
* the OID we want to register our string at. This should be a |
| 37 |
* fully qualified instance. In our case, it's a scalar at: |
| 38 |
* NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0 (note the trailing |
| 39 |
* 0 which is required for any instantiation of any scalar object) |
| 40 |
*/ |
| 41 |
oid my_registration_oid[] = |
| 42 |
{ 1, 3, 6, 1, 4, 1, 8072, 2, 1, 3, 0 }; |
| 43 |
|
| 44 |
/* |
| 45 |
* variables needed for registration |
| 46 |
*/ |
| 47 |
netsnmp_handler_registration *reginfo; |
| 48 |
static netsnmp_watcher_info watcher_info; |
| 49 |
int watcher_flags; |
| 50 |
|
| 51 |
/* |
| 52 |
* a debugging statement. Run the agent with -Dexample_string_instance |
| 53 |
* to see the output of this debugging statement. |
| 54 |
*/ |
| 55 |
DEBUGMSGTL(("example_string_instance", |
| 56 |
"Initalizing example string instance. Default value = %s\n", |
| 57 |
my_string)); |
| 58 |
|
| 59 |
/* |
| 60 |
* If we wanted a callback when the value was retrieved or set |
| 61 |
* (even though the details of doing this are handled for you), |
| 62 |
* you could change the NULL pointer below to a valid handler |
| 63 |
* function. |
| 64 |
* |
| 65 |
* Change RWRITE to RONLY for a read-only string. |
| 66 |
*/ |
| 67 |
reginfo = netsnmp_create_handler_registration("my example string", NULL, |
| 68 |
my_registration_oid, |
| 69 |
OID_LENGTH(my_registration_oid), |
| 70 |
HANDLER_CAN_RWRITE); |
| 71 |
|
| 72 |
/* |
| 73 |
* the three options for a string watcher are: |
| 74 |
* fixed size string (length never changes) |
| 75 |
* variable size (length can be 0 - MAX, for some MAX) |
| 76 |
* c string (length can be 0 - MAX-1 for some max, \0 is not a valid |
| 77 |
* character in the string, the length is provided by strlen) |
| 78 |
* |
| 79 |
* we'll use a variable length string. |
| 80 |
*/ |
| 81 |
watcher_flags = WATCHER_MAX_SIZE; |
| 82 |
|
| 83 |
/* |
| 84 |
* create the watcher info for our string. |
| 85 |
*/ |
| 86 |
netsnmp_init_watcher_info6(&watcher_info, my_string, strlen(my_string), |
| 87 |
ASN_OCTET_STR, watcher_flags, |
| 88 |
sizeof(my_string), NULL); |
| 89 |
|
| 90 |
/* |
| 91 |
* the line below registers our "my_string" variable above as |
| 92 |
* accessible and makes it writable. |
| 93 |
*/ |
| 94 |
netsnmp_register_watched_instance(reginfo, &watcher_info); |
| 95 |
|
| 96 |
DEBUGMSGTL(("example_string_instance", |
| 97 |
"Done initalizing example string instance\n")); |
| 98 |
} |