1.我们写一个MIB文件,含有两个节点,一个是只读权限的,一个是读写权限的。然后把这个文件命名为 Test0601.txt
然后保存到 /usr/local/snmp/share/snmp/mibs/,文件读写属性改为777。
--开始
TEST-MIB DEFINITIONS ::= BEGIN
--引入部分
IMPORTS
enterprises
FROM RFC1155-SMI
Integer32,OBJECT-TYPE
FROM SNMPv2-SMI
DisplayString
FROM SNMPv2-TC
TEXTUAL-CONVENTION
FROM SNMPv2-TC; --引用结束,用分号
--定义节点
--enterprises的OID是1.3.6.1.4
test OBJECT IDENTIFIER ::= {enterprises 77587}
readObject OBJECT IDENTIFIER ::= {test 1}
writeObject OBJECT IDENTIFIER ::= {test 2}
readobject OBJECT-TYPE --对象名称
SYNTAX Integer32 --类型
MAX-ACCESS read-only --访问方式
STATUS current --状态
DESCRIPTION "test read" --描述
::= {test 1} --父节点
writeObject OBJECT-TYPE --对象名称
SYNTAX DisplayString --类型
MAX-ACCESS read-write --访问方式
STATUS current --状态
DESCRIPTION "test write" --描述
::= {test 2} --父节点
--结束定义
END
2.使用如下命令验证是否添加成功。
ubuntu@ubuntu:/usr/local/snmp/bin$ ./snmptranslate -Tp -IR TEST-MIB::test
+--test(77587)
|
+-- -R-- Integer32 readobject(1)
|
+--readObject(1)
|
+-- -RW- String writeObject(2)
Textual Convention: DisplayString
Size: 0..255
3.使用如下命令生成.c文件,生成选项 2 - 1 -y , 文件路径 /usr/local/snmp/bin
ubuntu@ubuntu:/usr/local/snmp/bin$ sudo envMIBS="+/usr/local/snmp/share/snmp/mibs/TEST0601.txt" ./mib2c TEST-MIB::test
writing to -
mib2c has multiple configuration files depending on the type of
code you need to write. You must pick one depending on your need.
You requested mib2c to be run on the following part of the MIB tree:
OID: test
numeric translation: .1.3.6.1.4.1.77587
number of scalars within: 1
number of tables within: 0
number of notifications within: 0
First, do you want to generate code that is compatible with the
ucd-snmp 4.X line of code, or code for the newer Net-SNMP 5.X code
base (which provides a much greater choice of APIs to pick from). You
can also generate C-header files containing define statements for
column numbers, enums, etc. Or do you simply want to translate the
MIB structures into documentation?
1) ucd-snmp style code
2) Net-SNMP style code
3) Generate header files
4) Generate documentation
Select your choice : 2
**********************************************************************
GENERATING CODE FOR SCALAR OBJECTS:
**********************************************************************
It looks like you have some scalars in the mib you requested, so I
will now generate code for them if you wish. You have two choices
for scalar API styles currently. Pick between them, or choose not
to generate any code for the scalars:
1) If you're writing code for some generic scalars
(by hand use: "mib2c -c mib2c.scalar.conf test")
2) If you want to magically "tie" integer variables to integer
scalars
(by hand use: "mib2c -c mib2c.int_watch.conf test")
3) Don't generate any code for the scalars
Select your choice: 1
using the mib2c.scalar.conf configuration file to generate your code.
writing to test.h
writing to test.c
**********************************************************************
* NOTE WELL: The code generated by mib2c is only a template. *YOU* *
* must fill in the code before it'll work most of the time. In many *
* cases, spots that MUST be edited within the files are marked with *
* /* XXX */ or /* TODO */ comments. *
**********************************************************************
running indent on test.c
running indent on test.h
4.修改3
ubuntu@ubuntu:/usr/local/snmp/bin$ sudo env MIBS="+/usr/local/snmp/share/snmp/mibs/TEST0601.txt" mib2c -c mib2c.int_watch.conf readObject
writing to -
*** Warning: only generating code for nodes of MIB type INTEGER
writing to readObject.h
writing to readObject.c
running indent on readObject.h
running indent on readObject.c
ubuntu@ubuntu:/usr/local/snmp/bin$ sudo env MIBS="+/usr/local/snmp/share/snmp/mibs/TEST0601.txt" mib2c -c mib2c.scalar.conf writeObject
writing to writeObject.h
writing to writeObject.c
running indent on writeObject.c
running indent on writeObject.h
编译
ubuntu@ubuntu:/usr/local/snmp/bin$ sudo net-snmp-config --compile-subagent readObject readObject.c
generating the temporary code file: netsnmptmp.14252.c
void init_readObject(void);
checking for init_readObject in readObject.c
init_readObject(void)
checking for shutdown_readObject in readObject.c
running: gcc -g -O2 -DNETSNMP_ENABLE_IPV6 -fno-strict-aliasing -DNETSNMP_REMOVE_U64 -g -O2 -Ulinux -Dlinux=linux -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fwrapv -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/x86_64-linux-gnu/perl/5.22/CORE -I. -I/usr/local/include -o readObject netsnmptmp.14252.c readObject.c -L/usr/local/lib -lnetsnmpmibs -lnetsnmpagent -lnetsnmp -lnetsnmpmibs -ldl -lm -lpcre -lnetsnmpagent -lpcre -Wl,-E -lnetsnmp -lm -lcrypto
netsnmptmp.14252.c: In function ‘main’:
netsnmptmp.14252.c:228:7: warning: implicit declaration of function ‘unlink’ [-Wimplicit-function-declaration]
unlink(pid_file);
^
netsnmptmp.14252.c:238:65: warning: implicit declaration of function ‘getpid’ [-Wimplicit-function-declaration]
int len = snprintf(buf, sizeof(buf), "%ld\n", (long int)getpid());
^
netsnmptmp.14252.c:239:9: warning: implicit declaration of function ‘write’ [-Wimplicit-function-declaration]
write(fd, buf, len);
^
netsnmptmp.14252.c:240:9: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
close(fd);
^
removing the temporary code file: netsnmptmp.14252.c
subagent program readObject created
ubuntu@ubuntu:/usr/local/snmp/bin$ sudo net-snmp-config --compile-subagent writeObject writeObject.c
[sudo] password for ubuntu:
generating the temporary code file: netsnmptmp.14569.c
void init_writeObject(void);
checking for init_writeObject in writeObject.c
init_writeObject(void)
checking for shutdown_writeObject in writeObject.c
running: gcc -g -O2 -DNETSNMP_ENABLE_IPV6 -fno-strict-aliasing -DNETSNMP_REMOVE_U64 -g -O2 -Ulinux -Dlinux=linux -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fwrapv -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/x86_64-linux-gnu/perl/5.22/CORE -I. -I/usr/local/include -o writeObject netsnmptmp.14569.c writeObject.c -L/usr/local/lib -lnetsnmpmibs -lnetsnmpagent -lnetsnmp -lnetsnmpmibs -ldl -lm -lpcre -lnetsnmpagent -lpcre -Wl,-E -lnetsnmp -lm -lcrypto
netsnmptmp.14569.c: In function ‘main’:
netsnmptmp.14569.c:228:7: warning: implicit declaration of function ‘unlink’ [-Wimplicit-function-declaration]
unlink(pid_file);
^
netsnmptmp.14569.c:238:65: warning: implicit declaration of function ‘getpid’ [-Wimplicit-function-declaration]
int len = snprintf(buf, sizeof(buf), "%ld\n", (long int)getpid());
^
netsnmptmp.14569.c:239:9: warning: implicit declaration of function ‘write’ [-Wimplicit-function-declaration]
write(fd, buf, len);
^
netsnmptmp.14569.c:240:9: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
close(fd);
^
removing the temporary code file: netsnmptmp.14569.c
subagent program writeObject created
ubuntu@ubuntu:/usr/local/snmp/bin$