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

网络 网络管理
文章摘要:mib2c是ucd-snmp 4.2加入的工具。那么就可以在ucd-snmp中进行MIB的编写了。那么具体的代码,我们在文中已经为大家详细写好了。

我们知道在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) */

#p#

79) unsigned char *
80) var_ustScalarSet(struct variable *vp,
81) oid *name,
82) size_t *length,
83) int exact,
84) size_t *var_len,
85) WriteMethod **write_method)
86) {
87) /* variables we may use later */
88) static long long_ret;

89) if (header_generic(vp,name,length,exact,var_len,write_method)
90) == MATCH_FAILED )
91) return NULL;

92) /*
93) this is where we do the value assignments for the mib results.
94) */
95) switch(vp->;magic) {
96) case USTSSSIMPLESTRING:
97) *write_method = write_ustSSSimpleString;
98) *var_len = ustSSSimpleString_len;
99) return (unsigned char *) ustSSSimpleString;

100) case USTSSSECONDSSINCECHANGED:
101) /* TimeTicks are seconds*100 */
102) long_ret = (time(NULL) - lastChanged)*100;
103) *var_len = sizeof(long_ret);
104) return (unsigned char *) &long_ret;

105) default:
106) ERROR_MSG("");
107) }
108) return NULL;
109) }

110) int
111) write_ustSSSimpleString(int action,
112) u_char *var_val,
113) u_char var_val_type,
114) size_t var_val_len,
115) u_char *statP,
116) oid *name,
117) size_t name_len)
118) {
119) static unsigned char *new_string = 0, *old_string = 0;
120) static size_t size, old_size;

121) /* this long complex series of "action"s is to preserve proper
122) transaction handling with other transactions in the same set
123) request. */

124) switch ( action ) {
125) case RESERVE1:
126) /* check to see that everything is possible */
127) if (var_val_type != ASN_OCTET_STR){
128) fprintf(stderr,
129) "write to ustSSSimpleString not ASN_OCTET_STR\n");
130) return SNMP_ERR_WRONGTYPE;
131) }
132) break;

133) case RESERVE2:
134) /* allocate memory needed here. */
135) size = var_val_len;
136) new_string = (char *) malloc(size+1);
137) if (new_string == NULL) {
138) return SNMP_ERR_GENERR; /* malloc failed! */
139) }
140) break;

141) case ACTION:
142) /* Actually make the change requested. Note that
143) anything done here must be reversable in the UNDO case */
144) if (new_string) {
145) old_string = ustSSSimpleString;
146) old_size = ustSSSimpleString_len;
147) memcpy(new_string, var_val, var_val_len);
148) new_string[var_val_len] = 0;
149) ustSSSimpleString = new_string;
150) ustSSSimpleString_len = size;
151) new_string = NULL;
152) } else {
153) /* something seriously wrong if we got here */
154) return SNMP_ERR_GENERR;
155) }
156) break;

157) case UNDO:
158) /* Back out any changes made in the ACTION case */
159) if (old_string == NULL) {
160) return SNMP_ERR_UNDOFAILED;
161) }
162) if (ustSSSimpleString)
163) free(ustSSSimpleString);
164) ustSSSimpleString = old_string;
165) ustSSSimpleString_len = old_size;
166) break;

167) case COMMIT:
168) /* Things are working well, so it's now safe to make the change
169) permanently. Make sure that anything done here can't fail! */
170) lastChanged = time(NULL);
171) break;

172) /* Treat the rest the same as FREE */
173) case FREE:
174) // break;
175) /* Release any resources that have been allocated */
176) if (new_s tring) {
177) free(new_string);
178) new_string = NULL;
179) }
180) if (old_string) {
181) free(old_string);
182) old_string = NULL;
183) }
184) break;

185) }
186) return SNMP_ERR_NOERROR;
187) }

我们定义的MIB模块是如此的简单,我们只需要在mib2c生成的代码上作黑体标识的修改即可.

责任编辑:佟健 来源: 互联网
相关推荐

2010-07-01 14:53:09

SNMPMIBUCD-SNMP

2010-07-01 12:38:31

ucd-snmp

2010-07-01 13:58:50

UCD-SNMP

2010-07-05 10:16:31

ucd-snmpSNMP Agent

2010-07-05 09:37:19

ucd-snmpsnmpd扩展

2010-06-29 13:58:17

SNMPMIB

2010-07-02 14:04:06

SNMP MIB

2010-06-29 14:06:49

SNMP MIB

2010-06-30 10:31:34

SNMP MIB

2010-07-01 14:05:43

SNMPMIB

2010-05-24 17:18:54

Linux SNMP

2010-06-29 16:29:52

SNMP协议管理

2009-07-28 08:39:56

Linux应用软件Linux应用

2022-04-27 08:55:01

Spring外部化配置

2009-09-25 11:06:38

Hibernate实例

2022-04-22 09:20:06

FreeBSD 13MySQL数据库

2021-08-31 08:01:40

STM32DSP指令

2021-04-04 22:56:47

Linux循环用户

2022-11-28 08:23:14

IDEAGradle配置

2020-12-15 09:26:20

LinuxVLAN命令
点赞
收藏

51CTO技术栈公众号