arm net-snmp使用
参考MIB如下(该参考MIB主要获取ARM的时间和状态):
MIB简述参考:https://blog.csdn.net/shanzhizi/article/details/15340305
```c
-- RSU-STATE-MIB.my
RSU-STATE-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;
-- October 09, 2002 at 14:50 GMT
-- 1.3.6.1.4.1.16535
RSU-state MODULE-IDENTITY
LAST-UPDATED "202007161450Z" -- October 09, 2002 at 14:50 GMT
ORGANIZATION
"DEMO"
CONTACT-INFO
"Basic state of antenna"
DESCRIPTION
"RSU's Server MIB."
::= { enterprises 16535 }
-- Node definitions
-- This part will include all details about the RSU-state.
-- 1.3.6.1.4.1.16535.1
Time OBJECT IDENTIFIER ::= { RSU-state 1 }
-- 1.3.6.1.4.1.16535.1.1
GetTime OBJECT-TYPE
SYNTAX DisplayString (SIZE (0..100))
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Example : 2013/4/11"
::= { Time 1 }
-- 1.3.6.1.4.1.16535.2
state OBJECT IDENTIFIER ::= { RSU-state 2 }
-- 1.3.6.1.4.1.16535.2.1
GetState OBJECT-TYPE
SYNTAX DisplayString (SIZE (0..100))
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Example : normal"
::= { state 1 }
END
-- RSU-STATE-MIB.my
编写好后使用 snmptranslate -Tp -IR RSU-STATE-MIB::RSU-state 检查MIB是否有问题,如下:
运行命令生成RSU_state.h,RSU_state.c文件
命令:env MIBS="+/usr/local/net-snmp/share/snmp/mibs/RSU-STATE-MIB.txt" mib2c RSU-state
注意:RSU-STATE-MIB.txt要拷贝到安装目录去,生成的文件也在哪里,运行命令的时候会要求选择,依次输入2,1就行。
修改生成的C文件:
未修改前:
/*
* 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 "RSU_state.h"
/** Initializes the RSU_state module */
void
init_RSU_state(void)
{
const oid GetTime_oid[] = { 1,3,6,1,4,1,16535,1,1 };
const oid GetState_oid[] = { 1,3,6,1,4,1,16535,2,1 };
DEBUGMSGTL(("RSU_state", "Initializing\n"));
netsnmp_register_scalar(
netsnmp_create_handler_registration("GetTime", handle_GetTime,
GetTime_oid, OID_LENGTH(GetTime_oid),
HANDLER_CAN_RONLY
));
netsnmp_register_scalar(
netsnmp_create_handler_registration("GetState", handle_GetState,
GetState_oid, OID_LENGTH(GetState_oid),
HANDLER_CAN_RONLY
));
}
int
handle_GetTime(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. */
switch(reqinfo->mode) {
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
/* XXX: a pointer to the scalar's data */,
/* XXX: the length of the data in bytes */);
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetTime\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
int
handle_GetState(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. */
switch(reqinfo->mode) {
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
/* XXX: a pointer to the scalar's data */,
/* XXX: the length of the data in bytes */);
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetState\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
修改后:
/*
* 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 "RSU_state.h"
#include <time.h>
#include <string.h>
/** Initializes the RSU_state module */
void
init_RSU_state(void)
{
const oid GetTime_oid[] = { 1,3,6,1,4,1,16535,1,1 };
const oid GetState_oid[] = { 1,3,6,1,4,1,16535,2,1 };
DEBUGMSGTL(("RSU_state", "Initializing\n"));
netsnmp_register_scalar(
netsnmp_create_handler_registration("GetTime", handle_GetTime,
GetTime_oid, OID_LENGTH(GetTime_oid),
HANDLER_CAN_RONLY
));
netsnmp_register_scalar(
netsnmp_create_handler_registration("GetState", handle_GetState,
GetState_oid, OID_LENGTH(GetState_oid),
HANDLER_CAN_RONLY
));
}
int
handle_GetTime(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. */
time_t t;
switch(reqinfo->mode) {
case MODE_GET:
time(&t);
char szTime[100]={0};
snprintf(szTime,100,"%s",ctime(&t));
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
szTime,
strlen(szTime));
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetTime\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
int
handle_GetState(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. */
char srustate[100]={0};
strcpy(srustate,"normal"); //测试 直接赋值了
switch(reqinfo->mode) {
case MODE_GET:
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
srustate,
strlen(srustate));
break;
default:
/* we should never get here, so this is a really bad error */
snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetState\n", reqinfo->mode );
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
将生成的.c .h拷贝到源码目录 net-snmp-5.7.3/agent/mibgroup下
./configure --host=arm-linux --build=i686-linux --with-cc=arm-linux-gnueabihf-gcc --with-ar=arm-linux-gnueabihf-ar --with-endianness=little --disable-embedded-perl --disable-shared --prefix=/my_file/share/net-snmp-5.7.3/my_lib --with-mib-modules=RSU-state
make
make install
将生成的snmpd应用替换ARM里面的就行了,重启ARM
启动snmpd:
snmpd -c /usr/local/share/snmp/snmpd.conf -f -Le -d -M /usr/local/share/snmp/mibs/
测试:
snmpwalk -v 1或2c(代表SNMP版本) -c SNMP读团体密码 IP地址 OID(对象标示符)
snmpwalk -v 2c -c public localhost 1.3.6.1.4.1.16535.2.1
snmpwalk -v 2c -c public localhost 1.3.6.1.4.1.16535.1.1
x86也可以使用下面工具导入MIB来测试
来源:oschina
链接:https://my.oschina.net/u/4342884/blog/4406628