Yamu-SLK-MIB.txt文件编写
mib文件讲解https://blog.csdn.net/shanzhizi/article/details/15340305
-- Yamu-SLK-MIB.txt
Yamu-SLK-MIB DEFINITIONS ::= BEGIN
IMPORTS
OBJECT-GROUP, MODULE-COMPLIANCE, NOTIFICATION-GROUP
FROM SNMPv2-CONF
enterprises, Integer32, Unsigned32, OBJECT-TYPE, MODULE-IDENTITY,
NOTIFICATION-TYPE
FROM SNMPv2-SMI
DisplayString
FROM SNMPv2-TC;
Yamu MODULE-IDENTITY
LAST-UPDATED "201903010000Z" --必须以Z结尾
ORGANIZATION
"yamu company"
CONTACT-INFO
"ww.he@yamu.com"
DESCRIPTION
"Yamu Snmp Server MIB."
::= { enterprises 201903 }
Soft OBJECT IDENTIFIER ::= { Yamu 1 }
DnsQps OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Example : 8888"
::= { Soft 1 }
DhcpQps OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Example : 8888"
::= { Soft 2 }
DhcpLps OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Example : 8888"
::= { Soft 3 }
END
-- Yamu-SLK-MIB.txt
生成文件
mib2c Test-SLK-MIB::Test 出现的选项依次选2和1.
Yamu.c Yamu.h
实现函数
举例:
实现
int get_dns_qps()
{
FILE* fp = popen("cat /sys/kernel/nap/stat | grep receive_accept","r");
if(NULL == fp)
{
//printf("popen(cat /sys/kernel/nap/stat | grep receive_accept) error\n");
return 0;
}
char cmd_res[1024] = {'\0'};
int len = fread(cmd_res,1024,1,fp);
if(len <= 0)
{
pclose(fp);
return 0;
}
int str_len = strlen(cmd_res);
if(str_len <= 0)
{
pclose(fp);
//printf("%d-debug\n",__LINE__);
return 0;
}
//printf("len=%d,str_len=%d\n",len,str_len);
//printf("%s\n",res);
char*p_buf = cmd_res;
char*p_tmp = cmd_res;
char* p_data = NULL;
int sum = 0;
while(*p_buf++)
{
if(*p_buf == '\n')
{
*p_buf = '\0';
//printf("%s\n",p_tmp);
p_data = strrchr(p_tmp,' ');
if(NULL != p_data)
{
p_data += 1;
sum += atoi(p_data);
}
p_buf++;
p_tmp = p_buf;
}
}
pclose(fp);
return sum;
}
生成代码
int
handle_DnsQps(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
/*
* We are never called for a GETNEXT if it's registered as a
* "instance", as it's "magically" handled for us.
*/
/*
* a instance handler also only hands us one request at a time, so
* we don't need to loop over a list of requests; we'll only get one.
*/
int qps = get_dns_qps(); // 添加代码
switch (reqinfo->mode) {
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
/*
* XXX: a pointer to the scalar's data
*/ &qps,
/*
* XXX: the length of the data in bytes
*/ sizeof(qps));
break;
default:
/*
* we should never get here, so this is a really bad error
*/
snmp_log(LOG_ERR, "unknown mode (%d) in handle_DnsQps\n",reqinfo->mode);
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
生成二进制
net-snmp-config --compile-subagent yamu_agent Yamu.c
启动
snmpd -f -Lo -C --rwcommunity=public --master=agentx
-f 启在前台
-Lo 日志设置为标准输出
-C默认配置文件启动
--master=agentx 以主代理启动
主代理启动
snmpd --master=agentx //默认启动在后台
启动子代理
./yamu_agent -f -Lo //前台
./yamu_agent //后台
网友评论