net-snmp 5.7
watched.c
00001 /*
00002  * watched.c
00003  * $Id$
00004  *
00005  */
00013 /*
00014  * start by including the appropriate header files 
00015  */
00016 #include <net-snmp/net-snmp-config.h>
00017 #include <net-snmp/net-snmp-includes.h>
00018 #include <net-snmp/agent/net-snmp-agent-includes.h>
00019 
00020 void init_watched_string(void);
00021 
00022 void init_watched(void)
00023 {
00024     init_watched_string();
00025 }
00026 
00027 void init_watched_string(void)
00028 {
00029     /*
00030      * the storage for our string. It must be static or allocated.
00031      * we use static here for simplicity.
00032      */
00033     static char my_string[256] = "So long, and thanks for all the fish!";
00034 
00035     /*
00036      * the OID we want to register our string at.  This should be a
00037      * fully qualified instance.  In our case, it's a scalar at:
00038      * NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0  (note the trailing
00039      *  0 which is required for any instantiation of any scalar object) 
00040      */
00041     oid             my_registration_oid[] =
00042         { 1, 3, 6, 1, 4, 1, 8072, 2, 1, 3, 0 };
00043 
00044     /*
00045      * variables needed for registration
00046      */
00047     netsnmp_handler_registration *reginfo;
00048     static netsnmp_watcher_info watcher_info;
00049     int watcher_flags;
00050 
00051     /*
00052      * a debugging statement.  Run the agent with -Dexample_string_instance
00053      * to see the output of this debugging statement. 
00054      */
00055     DEBUGMSGTL(("example_string_instance",
00056                 "Initalizing example string instance.  Default value = %s\n",
00057                 my_string));
00058 
00059     /*
00060      * If we wanted a callback when the value was retrieved or set
00061      * (even though the details of doing this are handled for you),
00062      * you could change the NULL pointer below to a valid handler
00063      * function.
00064      *
00065      * Change RWRITE to RONLY for a read-only string.
00066      */
00067     reginfo = netsnmp_create_handler_registration("my example string", NULL,
00068                                                   my_registration_oid,
00069                                                   OID_LENGTH(my_registration_oid),
00070                                                   HANDLER_CAN_RWRITE);
00071                                                   
00072     /*
00073      * the three options for a string watcher are:
00074      *   fixed size string (length never changes)
00075      *   variable size (length can be 0 - MAX, for some MAX)
00076      *   c string (length can be 0 - MAX-1 for some max, \0 is not a valid
00077      *     character in the string, the length is provided by strlen)
00078      *
00079      * we'll use a variable length string.
00080      */
00081     watcher_flags = WATCHER_MAX_SIZE;
00082 
00083     /*
00084      * create the watcher info for our string.
00085      */
00086     netsnmp_init_watcher_info6(&watcher_info, my_string, strlen(my_string),
00087                                ASN_OCTET_STR, watcher_flags,
00088                                sizeof(my_string), NULL);
00089 
00090     /*
00091      * the line below registers our "my_string" variable above as
00092      * accessible and makes it writable. 
00093      */
00094     netsnmp_register_watched_instance(reginfo, &watcher_info);
00095 
00096     DEBUGMSGTL(("example_string_instance",
00097                 "Done initalizing example string instance\n"));
00098 }