教你如何在ucd-snmp中定义MIB(1)


我们知道在SNMP协议中具有MIB模块,这个模块的功能是进行性管理的。那么在mib2c工具中如何进行MIB的写入呢?今天我们就来简单介绍一下。mib2c是ucd-snmp 4.2新加入的由perl语言编写的工具程序. 该工具程序需要SNMP perl扩展模块支持. 首先, 将上面的MIB定义文件拷贝到目录/usr/local/share/snmp/mibs下. 然后, 以如下命令运行mib2c工具:

/usr/local/bin/mib2c ustScalarSet

mib2c在当前目录下生成两个文件: ustScalarSet.h 和ustScalarSet.c. 文件如下.

ustScalarSet.h :

1) /* This file was generated by mib2c and is intended for use as a mib module
for the ucd-snmp snmpd agent. */

2) #ifndef _MIBGROUP_USTSCALARSET_H
3) #define _MIBGROUP_USTSCALARSET_H

4) /* we may use header_generic and header_simple_table from the util_funcs module */
5) config_require(util_funcs)

6) /* function prototypes */
7) void init_ustScalarSet(void);
8) FindVarMethod var_ustScalarSet;
9) WriteMethod write_ustSSSimpleString;

10) #endif /* _MIBGROUP_USTSCALARSET_H */

ustScalarSet.c :

1) /* Most of this file was generated by the mib2c perl script. */

2) #ifdef IN_UCD_SNMP_SOURCE
3) /* If we're compiling this file inside the ucd-snmp source tree */
4) /* This should always be included first before anything else */
5) #include <config.h>;

6) /* minimal include directives */
7) #include "mibincl.h"
8) #include "util_funcs.h"

9) #else /* !IN_UCD_SNMP_SOURCE */

10) #include <ucd-snmp/ucd-snmp-config.h>;
11) #include <ucd-snmp/ucd-snmp-includes.h>;
12) #include <ucd-snmp/ucd-snmp-agent-includes.h>;

13) #endif /* !IN_UCD_SNMP_SOURCE */

14) #if HAVE_STRING_H
15) #include <string.h>;
16) #else
17) #include <strings.h>;
18) #endif

19) #include "ustScalarSet.h"

20) /*
21) ustScalarSet_variables_oid:
22) this is the top level oid that we want to register under. This
23) is essentially a prefix, with the suffix appearing in the
24) variable below.
25) */

26) static oid ustScalarSet_variables_oid[] =
27) { 1,3,6,1,4,1,2021,13,4242,1,1 };

28) /*
29) Global variables to store data we're interesting in serving:
30) */
31) static char *ustSSSimpleString;
32) static size_t ustSSSimpleString_len;
33) static time_t lastChanged=0;

34) /*
35) variable2 ustScalarSet_variables:
36) this variable defines function callbacks and type return information
37) for the ustScalarSet mib section
38) */

39) struct variable2 ustScalarSet_variables[] = {
40) #define USTSSSIMPLESTRING 1
41) { USTSSSIMPLESTRING , ASN_OCTET_STR , RWRITE, var_ustScalarSet, 1, { 1 } },
42) #define USTSSSECONDSSINCECHANGED 2
43) { USTSSSECONDSSINCECHANGED, ASN_TIMETICKS , RONLY , var_ustScalarSet, 1, { 2 } },

44) };
45) /* (L = length of the oidsuffix) */

46) /* deinit call for supporting dynamic shared object loading/unloading */
47) void deinit_ustScalarSet(void) {
48) DEBUGMSGTL(("ustScalarSet","unloading\n"));
49) unregister_mib(ustScalarSet_variables_oid,
50) sizeof(ustScalarSet_variables_oid)/sizeof(oid));
51) }

52) /*
53) init_ustScalarSet():
54) Initialization routine. This is called when the agent starts up.
55) At a minimum, registration of your variables should take place here.
56) */
57) void init_ustScalarSet(void) {
58) DEBUGMSGTL(("ustScalarSet","initializing\n"));

59) /* register ourselves with the agent to handle our mib tree */
60) REGISTER_MIB("ustScalarSet", ustScalarSet_variables, variable2,
61) ustScalarSet_variables_oid);

62) /* place any other initialization junk you need here */
63) ustSSSimpleString = strdup("Hello World");
64) ustSSSimpleString_len = strlen(ustSSSimpleString);
65) lastChanged = time(NULL);
66) }

67) /*
68) var_ustScalarSet():
69) This function is called every time the agent gets a request for
70) a scalar variable that might be found within your mib section
71) registered above. It is up to you to do the right thing and
72) return the correct value.
73) You should also correct the value of "var_len" if necessary.
74) *
75) Please see the documentation for more information about writing
76) module extensions, and check out the examples in the examples
77) and mibII directories.
78) */


相关内容