watched.c

These examples creates some scalar registrations that allows some simple variables to be accessed via SNMP. In a more realistic example, it is likely that these variables would also be manipulated in other ways outside of SNMP gets/sets.

00001 /*
00002  * watched.c
00003  * $Id: watched.c 12097 2005-04-20 18:03:47Z rstory $
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     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      * create the registration info for our string. If you want to
00061      *
00062      * If we wanted a callback when the value was retrieved or set
00063      * (even though the details of doing this are handled for you),
00064      * you could change the NULL pointer below to a valid handler
00065      * function.
00066      *
00067      * Change RWRITE to RONLY for a read-only string.
00068      */
00069     reginfo = netsnmp_create_handler_registration("my example string", NULL,
00070                                                   my_registration_oid,
00071                                                   OID_LENGTH(my_registration_oid),
00072                                                   HANDLER_CAN_RWRITE);
00073                                                   
00074     /*
00075      * the two options for a string watcher are:
00076      *   fixed size string (length never changes)
00077      *   variable size (length can be 0 - MAX, for some MAX)
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, and set the max size.
00085      */
00086     watcher_info =
00087         netsnmp_create_watcher_info(my_string, strlen(my_string),
00088                                     ASN_OCTET_STR, watcher_flags);
00089     watcher_info->max_size = sizeof(my_string);
00090 
00091     /*
00092      * the line below registers our "my_string" variable above as
00093      * accessible and makes it writable. 
00094      * 
00095      * If we wanted a callback when the value was retrieved or set
00096      * (even though the details of doing this are handled for you),
00097      * you could change the NULL pointer below to a valid handler
00098      * function. 
00099      */
00100     netsnmp_register_watched_instance(reginfo, watcher_info);
00101 
00102     DEBUGMSGTL(("example_string_instance",
00103                 "Done initalizing example string instance\n"));
00104 }

net-snmpに対してSat Sep 5 13:14:19 2009に生成されました。  doxygen 1.4.7