MIBs extension of Net-snmp
- MIBs extension of Net-snmp
- 1 Compiling and installing NET-SNMP
- 2 Writing MIB files
- MIB File description
- A simple example
- 3 Making a custom MIB file effective
- 4 Implementing Agent Agents
- Use the MIB2C program to generate. C and. h files.
- Read-only node readobject.c and readObject.h modifications.
- Compile a bit
- Run a test, please.
- Modification of Read-write node writeobject.c
- Compile and run the test.
Related Concepts of MIB
In SNMP network management, the Management Repository MIB (Management information Base) is information that can be accessed through the Network Management protocol. This information is more specifically understood as the managed resources in the network management, and the resources in the net administration are represented by objects, and each object represents a property of a certain aspect of the managed resource, and the collection of these objects forms a management information base.
Let's talk about the system environment.
[email protected]:~$ uname -aLinux o-pc 3.19.0-21-generic #21-Ubuntu SMP Sun Jun 14 18:31:11 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
1 Compiling and installing NET-SNMP
The specific will not say, see here:
net-snmp-5.7.3 Configuring the compilation installation http://www.cnblogs.com/oloroso/p/4595123.html
2 Writing MIB file MiB file description
MIB files are ASN.1
described in syntax, so in order to precisely define the management objects in the MIB, the user has to refer to some ASN.1 syntax related documents such as RFC1155, RFC1212, etc. to define the device's own MIB. ASN.1 is an abstract syntactic representation (Abstract Syntax Notation One)
of the abbreviation, for each management object it is described in text, the general file suffix name with ". MiB".
For an example of an MIB file, you can see the NET-SNMP directory after the installation, typically /usr/local/net-snmp/share/snmp/mibs/
.
ls /usr/local/net-snmp/share/snmp/mibs/AGENTX-MIB.txt IPV6-TCP-MIB.txt SNMP-NOTIFICATION-MIB.txt...IPV6-MIB.txt SNMP-MPD-MIB.txt UDP-MIB.txt
A simple example
Here we write a MIB file that contains two nodes, one that is read-only, and one with read and write permissions. Then name the file myTest.mib
and save it to the /usr/local/net-snmp/share/snmp/mibs/
directory.
--开始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
3 Making a custom MIB file effective
The simplest way to make this custom MIB work is to add its contents to an existing MIB file. For example cat myTest.mib >> 原有MIB文件
, then restart the snmpd
service. But this is a speculative approach.
A normal practice is to:
- Add the name defined at the beginning of the custom MIB file to the environment variable
MIBS
, such as TEST-MIB DEFINITIONS ::= BEGIN
in here TEST-MIB
.
You can also add to snmpd
the configuration file. A row was added to the file because it was taken on the local environment variable that was added to the bash
~/.bashrc
export MIBS=+TEST-MIB
- Kill
snmpd
the process, and then restart it.
- Use
snmptranslate
to see if the custom is TEST-MIB
loaded.
[email protected]:~/snmp/mibs$ /usr/local/net-snmp/bin/snmptranslate -Tp -IR test+--test(77587) | +-- -R-- Integer32 readObject(1) +-- -RW- String writeObject(2) Textual Convention: DisplayString Size: 0..255
4 Implementing Agent Agents
We can first try to get the value of the node defined earlier readObject
.
Because enterprises
the OID is 1.3.6.1.4
, and test
is enterprises
the leaf (77585), but readObject
also test
the leaf node (1). So its OID is 1.3.6.1.4.77585.1
.
Use Snmpget below to test
[email protected]:~/snmp/mibs$ /usr/local/net-snmp/bin/snmpget -c public -v 2c localhost 1.3.6.1.4.1.77587.1TEST-MIB::readObject = No Such Object available on this agent at this OID
The result is no Such Object available on the This agent at the this OID, i.e.
Use the MIB2C program to generate
.c
And
.h
File.
With the following command, the -c
configuration file followed by the two command is different because the two node types are not the same. The first one is the configuration of the number (Integer32,counter,time), and the second is the other type.
mib2c -c mib2c.int_watch.conf readObjectmib2c -c mib2c.scalar.conf writeObject
Read-only node readobject.c and readObject.h modifications.
ReadObject.h file is not changed, there is no need here.
Let's take a look at the readobject.c file. (Here I have deleted the unimportant.)
The modification is to add a init_readObject
sentence to the function readObject = 12345;
. This is a follow-on nature. All we need to know is that when used snmpget
to get the readObject
value of a node, the value of the variable is obtained.
#include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <net-snmp/ agent/net-snmp-agent-includes.h> #include "readObject.h"//This sentence is MIB2C generated, the default value is set to 0long readobject = 0; /* Xxx:set default value */voidinit_readobject (void) {netsnmp_handler_registration *reg; Const OID Readobject_oid[] = {1,3,6,1,4,1,77587,1}; Static Netsnmp_watcher_info Readobject_winfo; DEBUGMSGTL ("ReadObject", "Initializing the ReadObject module\n");/********************************************** /////This is what I modified in order to verify its valid ReadObject = 12345;/******************************************************/debugmsg TL ("ReadObject", "Initializing readobject scalar integer. Default value =%ld\n ", readobject)); reg = Netsnmp_create_handler_registration ("ReadObject", NULL, Readobject_oid, Oid_length (readobj ect_oid), handler_can_ronly); Netsnmp_init_watcher_info (&readobject_winfo, &rEadobject, sizeof (long), Asn_integer, watcher_fixed_size); if (Netsnmp_register_watched_scalar (Reg & Readobject_winfo) < 0) {Snmp_log (Log_err, "Failed to register watched ReadObject"); } DEBUGMSGTL (("ReadObject", "Done initalizing readobject module\n"));}
Compile a bit
You need to use another tool when compiling net-snmp-config
. This tool is used to do two things, one is to generate intermediate code, and then use GCC to compile it.
Why do you generate intermediate code? You see that readObject.c
there is no main function in the above survival.
The specific use is as follows
net-snmp-config --compile-subagent readObject readObject.c
- –compile-subagent means compiling to a subagent program.
- ReadObject is the post-compilation output program name
- READOBJECT.C is the file to be compiled
You can also add --norm
parameters to prevent the compilation of the resulting intermediate code file from being deleted. We can try.
[email protected]:~/snmp/mibs$ net-snmp-config--compile-subagent--norm readObject Readobject.cgenerating the Tmporary code file:netsnmptmp.25506.cvoid init_readobject (void); Checking for Init_ ReadObject in Readobject.cinit_readobject (void) checking for shutdown_readobject in READOBJECT.CRUNNING:GCC- Fno-strict-aliasing-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.20/core-i.-I/ Usr/local/net-snmp/include-o readobject netsnmptmp.25506.c Readobject.c-l/usr/local/net-snmp/lib-lnetsnmpmibs- LNETSNMPAGENT-LNETSNMP-LNETSNMPMIBS-LDL-LNETSNMPAGENT-WL,-E-LNETSNMP leaving the Tmporary code FILE:NETSNMPTMP.2 5506.csubagent program ReadObject created
After compiling, we can see that a compiled program and a file appear in the current directory readObject
netsnmptmp.25506.c
. You open it and you can see that this is a main
. C source file with a function. The code after the simplification is as follows.
#include "readObject.h"const char *app_name = "readObject";static int reconfig = 0;extern int netsnmp_running;intmain (int argc, char **argv){ ... while ((arg = getopt(argc, argv, ...)) != EOF) { ... } ... /* 初始化agent库 * /*initialize the agent library */ init_agent(app_name); /* i初始化你的mib代码 */ /* initialize your mib code here */ init_readObject(); /* readObject will be used to read readObject.conf files. */ /* readObject will be used to read readObject.conf files. */ init_snmp("readObject"); ... exit(0);}
Run a test, please.
Here we start this readObject
program, and then go back to snmpget
retrieve readObeject
the value of the node.
Note that this readObject
default is the daemon.
Get the results as shown below and you can see the successful acquisition.
[email protected]:~/snmp/mibs$ /usr/local/net-snmp/bin/snmpget -c public -v 2c localhost 1.3.6.1.4.1.77587.1...TEST-MIB::readObject.0 = INTEGER: 12345
Modification of Read-write node writeobject.c
The following is the modified writeobject.c file
#include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <net-snmp/ agent/net-snmp-agent-includes.h> #include "writeObject.h"//is added here, buf is used to save the value set by the control side, and is also used to return. #define BUFSIZE 1024static char buf[bufsize] = "Test Write"; Give a default value/** initializes the WriteObject module */voidinit_writeobject (void) {Const OID writeobject_oid[] = {1,3,6,1,4, 1,77587,2}; Debugmsgtl (("WriteObject", "initializing\n")); Netsnmp_register_scalar (Netsnmp_create_handler_registration ("WriteObject", Handle_writeobject, Writeobject_oid, Oid_length (writeobject_oid), handler_can_rwrite));} Inthandle_writeobject (Netsnmp_mib_handler *handler, Netsnmp_handler_registration *reginfo, Netsnmp_agent_request_info *reqinfo, Netsnmp_request_info *requests) {int ret; Switch (Reqinfo->mode) {//Is get operation case MOde_get:snmp_set_var_typed_value (REQUESTS->REQUESTVB, ASN_OCTET_STR,/* Fill in buf here, to return the data to the control side */ BUF/* xxx:a Pointer to the scalar's data *//////* Here is the number of buf bytes, note the writeobject type */strlen (BUF)//Xxx:the Leng Th of the data in bytes */); Break /* * SET REQUEST * * Multiple states in the transaction. See: * http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg *///The following is the set operation, that is, Snmpset Case MODE_SET_RESERVE1://This either/* or you could use netsnmp_check_vb_type_and_size instead */ ret = Netsnmp_check_vb_type (REQUESTS->REQUESTVB, ASN_OCTET_STR); if (ret! = snmp_err_noerror) {netsnmp_set_request_error (Reqinfo, requests, ret); } break; Case MODE_SET_RESERVE2://This also regardless of it/* XXX malloc "undo" Storage Buffer *///We do not need to dynamically request memory, skip directly if (0/* XXX if malloc, or WHATever, failed: */) {Netsnmp_set_request_error (Reqinfo, requests, snmp_err_resourceunavailable); } break; Case Mode_set_free:/* xxx:free resources allocated in RESERVE1 and/or RESERVE2. Something failed somewhere, and the states below won ' t be called. */break; /****************************************************************////Here is our focus, the data sent from the control end is here to get case Mode_set_ac tion:/* Xxx:perform The value change here */* get data from the control side using the Snmpset */memcpy (Buf,reque Sts->requestvb->buf,requests->requestvb->val_len); if (0/* xxx:error */) {//This first regardless of netsnmp_set_request_error (Reqinfo, requests, 0/* Some error */); } break;//, it's all over. Case MODE_SET_COMMIT:/* xxx:delete temporary storage */ if (0/* Xxx:error */) {/* try _really_really_ hard to NEVer get to this point */Netsnmp_set_request_error (Reqinfo, requests, snmp_err_commitfailed); } break; Case Mode_set_undo:/* Xxx:undo and return to previous value for the object */if (0/* xxx:error? */) {/* Try _really_really_ hard-never get to this point */Netsnmp_set_request_error (R Eqinfo, requests, snmp_err_undofailed); } break; Default:/* We should never get here, so the is a really bad error */Snmp_log (Log_err, "Unknown mo De (%d) in handle_writeobject\n ", Reqinfo->mode); return snmp_err_generr; } return snmp_err_noerror;}
Compile and run the test.
Compile or the same as the previous said, it is not detailed said.
Run once and try to get it once. The snmpbulkget used here are not scattered snmpget. It's because of caprice.
[email protected]:~/snmp/mibs$ snmpbulkget -c public -v 2c localhost writeObject.0MY-TEST-MIB::writeObject.0 = STRING: test Write
[email protected]:~/snmp/mibs$ snmpget -c public -v 2c localhost writeObject.0MY-TEST-MIB::writeObject.0 = STRING: test Write
Can see big get is no problem, then set up a try. Set to use the snmpset
tool, using the same way and snmpget
similar. Just need to add the type and data of the data to be set at the end.
snmpset -c public -v 2c localhost writeObject.0 s "nihao"
The data type parameter can be used snmpset --help
to view the results as follows
TYPE: one of i, u, t, a, o, s, x, d, b i: INTEGER, u: unsigned INTEGER, t: TIMETICKS, a: IPADDRESS o: OBJID, s: STRING, x: HEX STRING, d: DECIMAL STRING, b: BITS U: unsigned int64, I: signed int64, F: float, D: double
After the setup is complete, try again.
[email protected]:~/snmp/mibs$ snmpget -c public -v 2c localhost writeObject.0MY-TEST-MIB::writeObject.0 = STRING: nihao
If the test is not successful, check the SNMPD configuration file (usually snmpd.conf) for permission settings.
[email protected]:~/snmp/mibs$ snmpset -c public -v 2c localhost writeObject.0 s "nihao"Error in packet.Reason: noAccessFailed object: MY-TEST-MIB::writeObject.0
NET-SNMP mibs extension (under Linux)