snmp 代理端扩展源码分析
2008-05-05 17:53
#include
#include
#include
#if HAVE_STDLIB_H
#include
#endif
#if TIME_WITH_SYS_TIME
# ifdef WIN32
# include
# else
# include
# endif
# include
#else
# if HAVE_SYS_TIME_H
# include
# else
# include
# endif
#endif
#include "util_funcs.h"
包含自己的头文件
#include "Display_time.h"
#define EXAMPLE_STR_LEN 300
#define EXAMPLE_STR_DEFAULT "life the universe andeverything"
int
example_int = 42;
char example_str[EXAMPLE_STR_LEN];
void example_parse_config_exampleint(const char *token, char*cptr);
void example_parse_config_examplestr(const char *token, char*cptr);
void example_free_config_exampleint(void);
void example_free_config_examplestr(void);
这个数组的类型是struct variableN, 其中N是这个数组中OID号的最长的数,
即:结构体最后一个成员(这个成员是个数组)定义了MIB Tree OID的底层数字。
N定义了MIB TreeOID的底层的层数(也就是这个数组的长度)。
所有有效的N数字都定义在了文件中。
struct variableN类型成员的说明:
1):FoxmailINT:这个magic number是在自己的头文件Display_time.h中宏定义,
这个参数被用来传递给CallBack 例程,用来决定那个object被查询。
2):ASN_INTEGER:这个参数说明了object的类型,所有有效的类型在snmp_impl.h文件中列表说明。
3):RONLY:这个参数说明了object是否能够被set。
4):var_foxmail:当有object被查询时,这个CallBack 例程被调用。
一般的情况下,同一个文件中的所有的object使用相同的allBack 例程。
5):1:MIBTree OID的底层数字的层数。(这个数字决定了struct variableN中的N)
6):{1}:MIBTree OID的底层数字。
struct variable2 example_variables[] = {
{EXAMPLESTRING, ASN_OCTET_STR, RONLY, var_example, 1, {1}},
{EXAMPLEINTEGER, ASN_INTEGER, RWRITE, var_example, 2, {2,1}},
{EXAMPLEOBJECTID, ASN_OBJECT_ID, RONLY, var_example, 2, {2,2}},
{EXAMPLETIMETICKS, ASN_TIMETICKS, RONLY, var_example, 1,{3}},
{EXAMPLEIPADDRESS, ASN_IPADDRESS, RONLY, var_example, 1,{4}},
{EXAMPLECOUNTER, ASN_COUNTER, RONLY, var_example, 1, {5}},
{EXAMPLEGAUGE, ASN_GAUGE, RONLY, var_example, 1, {6}},
{EXAMPLETRIGGERTRAP, ASN_INTEGER, RWRITE, var_example, 1,{7}},
{EXAMPLETRIGGERTRAP2, ASN_INTEGER, RWRITE, var_example, 1,{8}}
};
下面这个数组定义了MIB Tree OID的顶层数字。
oid example_variables_oid[] = { 1, 3, 6, 1, 4, 1, 2021, 254 };
这个例程在Agent程序开始的时候被调用,用来初始化可能被查询的Object。
void init_example(void)
{
注册我们自己的MIBTree,以便Agent查询的时候能够处理。
参数:
1)descr: 描述这个MIB Tree
2)var: 变量结构体,类型struct variableN。
3)vartype:类型struct variableN
4)theoid: MIB Tree的顶层数字
REGISTER_MIB("example", example_variables, variable2,
example_variables_oid);
把example_str变量设上默认字符串。example_int已经在上面初始化了。
strncpy(example_str, EXAMPLE_STR_DEFAULT, EXAMPLE_STR_LEN);