snmp agent 表格實現(子代理方式實現)
前奏參見如下:
http://blog.sina.com.cn/s/blog_8f3de3250100xhao.html
http://blog.csdn.net/hepeng597/article/details/8782868
http://blog.csdn.net/rheostat/article/details/8172580
問題解決:
http://bbs.csdn.net/topics/340248598
實現:
/*
* Note: this file originally auto-generated by mib2c using
* $
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "dpiProject.h"
#define NNN 30
#define STRMAX 200
#define COLUMN_MIN 1
#define COLUMN_MAX 11
struct dpiTaskTable_entry *
dpiTaskTable_createEntry(u_long gnTaskId, char *gnTaskName, size_t gnTaskName_len);
/** Initializes the dpiProject module */
void
init_dpiProject(void)
{
/* here we initialize all the tables we're planning on supporting */
initialize_table_dpiTaskTable();
}
/** Initialize the dpiTaskTable table by defining its contents and how it's structured */
void
initialize_table_dpiTaskTable(void)
{
const oid dpiTaskTable_oid[] = {1,3,6,1,4,1,22222,0,1,0,0};
const size_t dpiTaskTable_oid_len = OID_LENGTH(dpiTaskTable_oid);
netsnmp_handler_registration *reg;
netsnmp_iterator_info *iinfo;
netsnmp_table_registration_info *table_info;
DEBUGMSGTL(("dpiProject:init", "initializing table dpiTaskTable\n"));
reg = netsnmp_create_handler_registration(
"dpiTaskTable", dpiTaskTable_handler,
dpiTaskTable_oid, dpiTaskTable_oid_len,
HANDLER_CAN_RONLY
);
table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
netsnmp_table_helper_add_indexes(table_info,
ASN_UNSIGNED, /* index: gnTaskId */
0);
//定義最大最小列數
table_info->min_column = COLUMN_MIN;
table_info->max_column = COLUMN_MAX;
iinfo = SNMP_MALLOC_TYPEDEF( netsnmp_iterator_info );
iinfo->get_first_data_point = dpiTaskTable_get_first_data_point;
iinfo->get_next_data_point = dpiTaskTable_get_next_data_point;
iinfo->table_reginfo = table_info;
netsnmp_register_table_iterator( reg, iinfo );
netsnmp_inject_handler_before( reg,
netsnmp_get_cache_handler(DPITASKTABLE_TIMEOUT,
dpiTaskTable_load, dpiTaskTable_free,
dpiTaskTable_oid, dpiTaskTable_oid_len),
TABLE_ITERATOR_NAME);
/* Initialise the contents of the table here */
//初始化表格
dpiTaskTable_createEntry(1, "dpi agent1", strlen("dpi agent1"));
dpiTaskTable_createEntry(2, "dpi agent1", strlen("dpi agent1"));
dpiTaskTable_createEntry(3, "dpi agent1", strlen("dpi agent1"));
}
/* Typical data structure for a row entry */
//總共11列
struct dpiTaskTable_entry {
/* Index values */
//u_long gnTaskId;
/* Column values */
u_long gnTaskId;
char gnTaskName[NNN];
size_t gnTaskName_len;
//char gnTaskState[NNN];
//size_t gnTaskState_len;
//u_long gnTaskCpuUsage;
//u_long gnTaskMemoryUsed;
//char gnTaskStartTime[NNN];
//size_t gnTaskStartTime_len;
//char gnTaskUpTime[NNN];
//size_t gnTaskUpTime_len;
/* Illustrate using a simple linked list */
int valid;
struct dpiTaskTable_entry *next;
};
struct dpiTaskTable_entry *dpiTaskTable_head;
/* create a new row in the (unsorted) table */
struct dpiTaskTable_entry *
dpiTaskTable_createEntry(u_long gnTaskId, char *gnTaskName, size_t gnTaskName_len)
{
struct dpiTaskTable_entry *entry;
entry = SNMP_MALLOC_TYPEDEF(struct dpiTaskTable_entry);
if (!entry)
return NULL;
entry->gnTaskId = gnTaskId;
snmp_log(LOG_ERR,"entry->gnTaskId(%d)\n", entry->gnTaskId);
//實現建立表
entry->gnTaskName_len = gnTaskName_len;
strncpy(entry->gnTaskName, gnTaskName, gnTaskName_len);
entry->next = dpiTaskTable_head;
dpiTaskTable_head = entry;
return entry;
}
/* remove a row from the table */
void
dpiTaskTable_removeEntry( struct dpiTaskTable_entry *entry ) {
struct dpiTaskTable_entry *ptr, *prev;
if (!entry)
return; /* Nothing to remove */
for ( ptr = dpiTaskTable_head, prev = NULL;
ptr != NULL;
prev = ptr, ptr = ptr->next ) {
if ( ptr == entry )
break;
}
if ( !ptr )
return; /* Can't find it */
if ( prev == NULL )
dpiTaskTable_head = ptr->next;
else
prev->next = ptr->next;
SNMP_FREE( entry ); /* XXX - release any other internal resources */
}
/* Example cache handling - set up linked list from a suitable file */
int
dpiTaskTable_load( netsnmp_cache *cache, void *vmagic ) {
FILE *fp;
struct dpiTaskTable_entry *this;
char buf[STRMAX];
/* The basic load routine template assumes that the data to
be reported is held in a file - with one row of the file
for each row of the table.
If your data is available via a different API, you
should amend this initial block (and the control of the
'while' loop) accordingly.
'XXX' marks where the template is incomplete and
code will definitely need to be added. */
fp = fopen( "/data/for/dpiTaskTable", "r" );
if ( !fp ) {
return -1;
}
while ( fgets( buf, STRMAX, fp )) {
this = SNMP_MALLOC_TYPEDEF( struct dpiTaskTable_entry );
/* XXX - Unpick 'buf' to extract the individual field values
and then populate the 'this' data structure with them */
this->next = dpiTaskTable_head;
dpiTaskTable_head = this; /* Iterate helper is fine with unordered lists! */
}
fclose(fp);
return 0; /* OK */
}
void
dpiTaskTable_free( netsnmp_cache *cache, void *vmagic ) {
struct dpiTaskTable_entry *this, *that;
for ( this = dpiTaskTable_head; this; this=that ) {
that = this->next;
SNMP_FREE( this ); /* XXX - release any other internal resources */
}
dpiTaskTable_head = NULL;
}
/* Example iterator hook routines - using 'get_next' to do most of the work */
netsnmp_variable_list *
dpiTaskTable_get_first_data_point(void **my_loop_context,
void **my_data_context,
netsnmp_variable_list *put_index_data,
netsnmp_iterator_info *mydata)
{
*my_loop_context = dpiTaskTable_head;
return dpiTaskTable_get_next_data_point(my_loop_context, my_data_context,
put_index_data, mydata );
}
netsnmp_variable_list *
dpiTaskTable_get_next_data_point(void **my_loop_context,
void **my_data_context,
netsnmp_variable_list *put_index_data,
netsnmp_iterator_info *mydata)
{
struct dpiTaskTable_entry *entry = (struct dpiTaskTable_entry *)*my_loop_context;
netsnmp_variable_list *idx = put_index_data;
if ( entry ) {
snmp_set_var_typed_integer( idx, ASN_UNSIGNED, entry->gnTaskId );
idx = idx->next_variable;
*my_data_context = (void *)entry;
*my_loop_context = (void *)entry->next;
return put_index_data;
} else {
return NULL;
}
}
/** handles requests for the dpiTaskTable table */
int
dpiTaskTable_handler(
netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests) {
netsnmp_request_info *request;
netsnmp_table_request_info *table_info;
struct dpiTaskTable_entry *table_entry;
DEBUGMSGTL(("dpiProject:handler", "Processing request (%d)\n", reqinfo->mode));
switch (reqinfo->mode) {
/*
* Read-support (also covers GetNext requests)
*/
case MODE_GET:
for (request=requests; request; request=request->next) {
table_entry = (struct dpiTaskTable_entry *)
netsnmp_extract_iterator_context(request);
table_info = netsnmp_extract_table_info( request);
switch (table_info->colnum) {
case COLUMN_GNTASKID:
if ( !table_entry ) {
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_integer( request->requestvb, ASN_UNSIGNED,
table_entry->gnTaskId);
break;
case COLUMN_GNTASKNAME:
if ( !table_entry ) {
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
table_entry->gnTaskName,
table_entry->gnTaskName_len);
break;
#if 0
case COLUMN_GNTASKSTATE:
if ( !table_entry ) {
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
table_entry->gnTaskState,
table_entry->gnTaskState_len);
break;
case COLUMN_GNTASKCPUUSAGE:
if ( !table_entry ) {
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_integer( request->requestvb, ASN_UNSIGNED,
table_entry->gnTaskCpuUsage);
break;
case COLUMN_GNTASKMEMORYUSED:
if ( !table_entry ) {
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_integer( request->requestvb, ASN_UNSIGNED,
table_entry->gnTaskMemoryUsed);
break;
case COLUMN_GNTASKSTARTTIME:
if ( !table_entry ) {
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
table_entry->gnTaskStartTime,
table_entry->gnTaskStartTime_len);
break;
case COLUMN_GNTASKUPTIME:
if ( !table_entry ) {
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHINSTANCE);
continue;
}
snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
table_entry->gnTaskUpTime,
table_entry->gnTaskUpTime_len);
break;
#endif
default:
netsnmp_set_request_error(reqinfo, request,
SNMP_NOSUCHOBJECT);
break;
}
}
break;
}
return SNMP_ERR_NOERROR;
}
說明:
主要是通過m2c 工具生成.c.h檔案
1.生成程式碼模板:變數:env MIBS="+/usr/local/share/snmp/mibs/XXX-MIB.txt" mib2c xxx(對應xxx.c,xxx.h)
表格:env MIBS="+/usr/local/share/snmp/mibs/XXX-MIB.txt" mib2c -c mib2c.iterate.conf xxx(對應xxx.c,xxx.h)
2.修改模板程式碼。通過devInfo_handler模組提供的api
模板程式碼流程:init_dpiProject-》initialize_table_xxxTable-》xxxTable_createEntry
修改處:
1.在initialize_table_xxxTable修改表格列數最小最大值
2.initialize_table_xxxTable加人xxxTable_createEntry初始化
3.在xxxTable_createEntry實現表格資料獲取(過devInfo_handler模組提供的api)
有三種實現整合到net snmp當中的方式:靜態、動態、子代理。本模組採用子代理方式