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其中的方式:静态、动态、子代理。本模块採用子代理方式
snmp agent 表格实现(子代理方式实现)的更多相关文章
- net-snmp子代理(SubAgent)编写详述
net-snmp子代理(SubAgent)编写 net-snmp子代理(SubAgent)编写 Netsnmp_Node_Handler MIB/OID定义 1.头文件test.h的编写 2.test ...
- snmpd 子代理模式编译测试
1.参考链接 1)Net-snmp添加子代理示例 https://blog.csdn.net/eyf0917/article/details/39546651 2.操作步骤 1)网络拷贝下面的文件 ...
- zabbix配置文件详解--服务(server)端、客户(agent)端、代理(proxy)端
在zabbix服务(server)端.客户(agent)端.代理(proxy)端分别对应着一个配置文件,即:zabbix_server.conf,zabbix_agentd.conf,zabbix_p ...
- SNMP AGENT函数介绍
http://wenku.baidu.com/view/6a7903a9d1f34693daef3e9f.html 一. SNMP AGENT在SNMP框架中的位置 1.1 SNMP是被广泛接受并投 ...
- Atitit 代理与分销系统(1) 子代理 充值总额功能设计概览 sum() groubpy subagt
Atitit 代理与分销系统(1) 子代理 充值总额功能设计概览 sum() groubpy subagt Keyword 分组与聚合操作. 一个for做分组...里面的做聚合... 数据g操作查 ...
- JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明
1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...
- Atitit 动态调用webservice与客户端代理方式调用
Atitit 动态调用webservice与客户端代理方式调用 方式1: 使用call.invoke 直接调用WSDL,缺点:麻烦,不推荐--特别是JAVA调用.NET的WS时,会有不少的问题需要解 ...
- .NET环境下导出Excel表格的两种方式和导入两种类型的Excel表格
一.导出Excel表格的两种方式,其中两种方式指的是导出XML数据类型的Excel(即保存的时候可以只需要修改扩展名为.xls)和真正的Excel这两种. using System; using Sy ...
- 利用loadrunner代理方式,录制手机APP脚本
利用loadrunner代理方式录制手机(iPhone.android)应用程序HTTP脚本 工具/原料 loadrunner 智能手机 方法/步骤 利用笔记本网卡或者类似360随身wifi,在安 ...
随机推荐
- SpringMVC+MyBatis (druid、logback)
数据库连接池是阿里巴巴的druid.日志框架式logback 1.整合SpringMVCspringMybatis-servlet.xml: <?xml version="1.0&qu ...
- 洛谷P1200 [USACO1.1]你的飞碟在这儿
题目描述 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支持者.因此,他们要用一种聪明的方案让这些小组提前知道谁会被彗星带走 ...
- NARF(Normal Aligned Radial Feature)关键点
NARF(Normal Aligned Radial Feature)关键点是为了从深度图像中识别物体而提出的,对NARF关键点的提取过程有以下要求: a) 提取的过程考虑边缘以及物体表面变化信息在内 ...
- C# double保留四位小数
2.保留N位,四舍五入 . decimal d= decimal.Round(decimal.Parse("0.55555"),4); 3.保留N位四舍五入 Math.Round( ...
- Ubuntu16.04 “有线未托管”有线网络不可用问题解决
Ubuntu16.04 “有线未托管”问题解决 电脑上安装的Ubuntu16.04 是通过先安装Ubuntu Server后在通过命令 sudo tasksel 安装的Gnome桌面环境,安装完成后发 ...
- java.sql.SQLException:错误 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
错误 方法 添加后面的内容即可
- 03013_JDBC工具类
1.“获得数据库连接”操作,将在以后的增删改查所有功能中都存在,可以封装工具类JDBCUtils.提供获取连接对象的方法,从而达到代码的重复利用. 2.该工具类提供方法:public static C ...
- CSUOJ 1603 Scheduling the final examination
1603: Scheduling the final examination Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 49 Solved: 1 ...
- ArcGIS Engine 9.3启动程序报错
报错1: 没有注册类 (异常来自 HRESULT:0x80040154 解决办法: 由于是X64系统,将项目的生成目标该成X86就解决了. 报错2: 正试图在 OS 加载程序锁内执行托管代码.不要尝试 ...
- 父类与子类的virtual
父类加了virtual,子类不需要加virtual,多余.加了也不会报错. 父类中不是virtual,子类是virtual,那么父类中的不是虚函数,子类及子子类的派生类中该函数才是虚函数