scalar_int.c

This example 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.

If this module is compiled into an agent, you should be able to issue snmp commands that look something like (authentication information not shown in these commands):

00001 
00022 /*
00023  * start be including the appropriate header files 
00024  */
00025 #include <net-snmp/net-snmp-config.h>
00026 #include <net-snmp/net-snmp-includes.h>
00027 #include <net-snmp/agent/net-snmp-agent-includes.h>
00028 
00029 /*
00030  * Then, we declare the variables we want to be accessed 
00031  */
00032 static int      example1 = 42;  /* default value */
00033 
00034 /*
00035  * our initialization routine, automatically called by the agent 
00036  * (to get called, the function name must match init_FILENAME())
00037  */
00038 void
00039 init_scalar_int(void)
00040 {
00041     /*
00042      * the OID we want to register our integer at.  This should be a
00043      * fully qualified instance.  In our case, it's a scalar at:
00044      * NET-SNMP-EXAMPLES-MIB::netSnmpExampleInteger.0 (note the
00045      * trailing 0 which is required for any instantiation of any
00046      * scalar object) 
00047      */
00048     oid             my_registration_oid[] =
00049         { 1, 3, 6, 1, 4, 1, 8072, 2, 1, 1, 0 };
00050 
00051     /*
00052      * a debugging statement.  Run the agent with -Dexample_scalar_int to see
00053      * the output of this debugging statement. 
00054      */
00055     DEBUGMSGTL(("example_scalar_int",
00056                 "Initalizing example scalar int.  Default value = %d\n",
00057                 example1));
00058 
00059     /*
00060      * the line below registers our "example1" variable above as
00061      * accessible and makes it writable.  A read only version of the
00062      * same registration would merely call
00063      * register_read_only_int_instance() instead.
00064      * 
00065      * If we wanted a callback when the value was retrieved or set
00066      * (even though the details of doing this are handled for you),
00067      * you could change the NULL pointer below to a valid handler
00068      * function. 
00069      */
00070     netsnmp_register_int_instance("my example int variable",
00071                                   my_registration_oid,
00072                                   OID_LENGTH(my_registration_oid),
00073                                   &example1, NULL);
00074 
00075     DEBUGMSGTL(("example_scalar_int",
00076                 "Done initalizing example scalar int\n"));
00077 }

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