使用net-snmp做自己的agent
2009-04-15 10:28
330 查看
参见http://net-snmp.sourceforge.net/tutorial/tutorial-4/agent/04-basic-code.html
1. 注册mib
REGISTER_MIB( "example", example_variables, variable2,example_variables_oid );
参数分别为:
"example" : 注册的mib module的名称
example_variables: 相关的变量(之后详述)
variable2: 是example_variables的类型
example_variables_oid : 这个module的root OID
2. 设置 相关变量
struct variable2 example_variables[] = {
{ EXAMPLESTRING, ASN_OCTET_STR, RONLY, var_example, 1, {1}}
};
各个分量的注释:
a magic number (the #defined integer constant described earlier) 这个magic就是用在routine里面,switch (vp->magic)用的
a type indicator (from the values listed in <snmplib/snmp_impl.h>)
an access indicator (essentially
the name of the routine used to handle this entry,真正处理请求的地方
the length of the OID suffix used, and
an array of integers specifying this suffix (more on this in a moment) 表示sufix是什么
像上面这个例子, {1}, 就表示了 这个的oid就是 example_variables_oid,1
3. routine 处理请求的地方
unsigned char *
var_example(struct variable *vp,
oid *name,
size_t *length,
int exact,
size_t *var_len,
WriteMethod **set_method)
Four of these parameters are used for passing in information about the request, these being: 四个参数是传进来的
// header file, for the object under consideration.
// Note that the
// completed into a fully qualified OID, by prepending
// the prefix common to the whole array.
oid *name; // The OID from the request
int *length; // The length of this OID
int exact; // A flag to indicate whether this is an exact
// request (GET/SET) or an 'inexact' one (GETNEXT)
[/code]
Four of the parameters are used to return information about the answer. The function also returns a pointer to the actual data for the variable requested (or NULL if this data is not available for any reason). The other result parameters are: 四个参数是传出去的
这个函数进入后,先要判断这次的oid是否在范围之内。如果是get-next就要对oid做相应的处理。
vp->magic表示了实际要获取的位置。 最后根据这个magic值返回response
例如
switch(vp->magic) {
case IFNAME:
if ( strlen(ifXEntry.ifName) > IFNAME_DISPLAY_STRING_LEN)
*var_len = IFNAME_DISPLAY_STRING_LEN;
else
*var_len = strlen(ifXEntry.ifName);
return (unsigned char *) ifXEntry.ifName;
break;
default:
ERROR_MSG("");
}
1. 注册mib
REGISTER_MIB( "example", example_variables, variable2,example_variables_oid );
参数分别为:
"example" : 注册的mib module的名称
example_variables: 相关的变量(之后详述)
variable2: 是example_variables的类型
example_variables_oid : 这个module的root OID
2. 设置 相关变量
struct variable2 example_variables[] = {
{ EXAMPLESTRING, ASN_OCTET_STR, RONLY, var_example, 1, {1}}
};
各个分量的注释:
a magic number (the #defined integer constant described earlier) 这个magic就是用在routine里面,switch (vp->magic)用的
a type indicator (from the values listed in <snmplib/snmp_impl.h>)
an access indicator (essentially
RWRITEor
RONLY)
the name of the routine used to handle this entry,真正处理请求的地方
the length of the OID suffix used, and
an array of integers specifying this suffix (more on this in a moment) 表示sufix是什么
像上面这个例子, {1}, 就表示了 这个的oid就是 example_variables_oid,1
3. routine 处理请求的地方
unsigned char *
var_example(struct variable *vp,
oid *name,
size_t *length,
int exact,
size_t *var_len,
WriteMethod **set_method)
Four of these parameters are used for passing in information about the request, these being: 四个参数是传进来的
struct variable *vp; // The entry in the [code]variableN array from the
// header file, for the object under consideration.
// Note that the
namefield of this structure has been
// completed into a fully qualified OID, by prepending
// the prefix common to the whole array.
oid *name; // The OID from the request
int *length; // The length of this OID
int exact; // A flag to indicate whether this is an exact
// request (GET/SET) or an 'inexact' one (GETNEXT)
[/code]
Four of the parameters are used to return information about the answer. The function also returns a pointer to the actual data for the variable requested (or NULL if this data is not available for any reason). The other result parameters are: 四个参数是传出去的
oid *name; // The OID being returned int *length; // The length of this OID int *var_len; // The length of the answer being returned WriteMethod **write_method; // A pointer to the SET function for this variable
这个函数进入后,先要判断这次的oid是否在范围之内。如果是get-next就要对oid做相应的处理。
vp->magic表示了实际要获取的位置。 最后根据这个magic值返回response
例如
switch(vp->magic) {
case IFNAME:
if ( strlen(ifXEntry.ifName) > IFNAME_DISPLAY_STRING_LEN)
*var_len = IFNAME_DISPLAY_STRING_LEN;
else
*var_len = strlen(ifXEntry.ifName);
return (unsigned char *) ifXEntry.ifName;
break;
default:
ERROR_MSG("");
}
相关文章推荐
- windows下使用net-snmp实现agent扩展(一)
- windows下使用net-snmp实现agent扩展(四)
- windows下使用net-snmp实现agent扩展(二)
- Net-snmp 使用c 扩展 sub-agent
- windows下使用net-snmp实现agent扩展(一)
- windows下使用net-snmp实现agent扩展(二)
- 基于Net-SNMP的Agent的开发和使用
- windows下使用net-snmp实现agent扩展(二)
- windows下使用net-snmp实现agent扩展(四)
- windows下使用net-snmp实现agent扩展(一)
- windows下使用net-snmp实现agent扩展(三)
- windows下使用net-snmp实现agent扩展(三)
- windows下使用net-snmp实现agent扩展(四)
- windows使用net-snmp
- net-snmp agent开发详解
- 如何使用snmp++和agent++开发包
- LINUX 下基于net-snmp使用MRTG监控服务器流量
- Net-Snmp工具(学习SNMP的工具,开源项目)简单使用
- Windows下使用net-snmp-5.4.1扩展代理所遇问题及解决
- NET-SNMP工具的使用