全部博文(176)
分类: LINUX
2009-07-30 14:57:29
$gunzip net-snmp-5.0.pre2.tar.gzThis will dump all the souce into $/net-snmp-5.0.pre2
$tar xvf net-snmp-5.0.pre2.tar
$./configure --with-mib-modules="agentx"The "configure" command configure the agent to use the AgentX protocol. This is a IETF defined protocol that allows a master/client relationship between agents and subagents. The last two command should not theoretically have to be to used ... but without them .. things do not seem to work. Now, we have to setup the snmpd configuration file, before "snmpd" can work properly.
$make
$make install
$cd local; make install; cd ..
$cd mibs; make install; cd ..
$ cp $/EXAMPLE.conf /usr/local/share/snmp/snmpd.conf
$ln -s /usr/local/lib/libnetsnmp-0.5.0.0.2.so /lib/libnetsnmp-0.5.0.0.2.so
$ln -s /usr/local/lib/libnetsnmpagent-0.5.0.0.2.so /lib/libnetsnmpagent-0.5.0.0.2.so
$ln -s /usr/local/lib/libnetsnmphelpers-0.5.0.0.2.so /lib/libnetsnmphelpers-0.5.0.0.2.so
$ln -s /usr/local/lib/libnetsnmpmibs-0.5.0.0.2.so /lib/libnetsnmpmibs-0.5.0.0.2.so
$ ps awwux | grep snmpif you see an earlier snmpd deamon ... kill it
$ cd $/net-snmp-5.0.pre2/agentThis should start the "snmpd" agent but keep it attached to the current terminal (which is useful since we want to kill it very soon).
$ ./snmpd -f -L
$snmpget -v 1 -c democommunity localhost system.sysUpTime.0If snmpd was installed correctly, this gives up the timeticks the snmpd agent has been up (NOT how long your system was up !!). If you get an error .. retrace your steps from the beginning.
You can now use "^C" to kill the snmpd deamon in the first window.
JM-TEST-1-MIB DEFINITIONS ::= BEGIN
IMPORTS
MODULE-IDENTITY,
OBJECT-TYPE,
INTEGER
FROM SNMPv2-SMI;
jmtest MODULE-IDENTITY
LAST-UPDATED "200203210000Z"
ORGANIZATION "Temple U"
CONTACT-INFO
"None yet."
DESCRIPTION
"AgentX testing MIB"
REVISION "200203210000Z"
DESCRIPTION
"None yet."
::= { experimental 72}
firstKey OBJECT-TYPE
SYNTAX INTEGER (0..100)
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"Value initialized to 0 and on each
access:
- Return current val.
- increment"
::= { jmtest 1 }
END
This mib represents a resource "firstKey" whose initial value is 0 and whose value gets incremented every time it is queried.
Note that this MIB will be registered under the 1.3.6.1.3.72 heirarchy. This choice is arbitrary and should only be used for experiments. For real world implementation you should get a "real" OID. Existing OIDs and guidelines on getting new ones can be fo und . This page is maintained by the current (March 2002) IETF/IESG chair, so he probably knows what he is talking about.
Copy this mib into the mibs directory
$cp JM-TEST-1-MIB.txt /usr/local/share/snmp/mibs
$echo "mibs +JM-TEST-1-MIB" >> /usr/local/share/snmp/snmp.confThis adds a directive to the snmp tools configuration asking them load our mib.
$snmptranslate -IR -Tp experimentalThis command will draw the present tree under the experimental branch. Omit the last parameter and you will get the whole tree (as currenly understood by the snmp tools). The output should look like:
Unlinked OID in JM-TEST-1-MIB: jmtest ::= { experimental 72 }If you get the above output, then we are in good shape so far. The real work, howeve still remains. We must now write the subagent that will handle queries on under this OID branch.
+--experimental(3)
|
+--jmtest(72)
+-- -RW- INTEGER firstKey(1)
Range: 0..100
The subagent is an independent program that communicates with the master agent (snmpd in our case) using the AgentX protocol.
The basic steps of writing the code is as follows:
So for an example of subagent code for simple scalar objects look at $/agent/mibgroups/example/example.c.
We have adapted example.c for our mib (JM-TEST-1-MIB.txt) as .
Download and$mkdir $/agent/mibgroup/examples/subagentNow we create the executable using the net-snmp-config tools as:
$cp example2.c $/agent/mibgroup/examples/subagent
$cp example.h $/agent/mibgroup/examples/subagent
$net-snmp-config --compile-subagent example2 example2.c -I../../../mibgroupThis produces a executable called example2. example2 is our subagent. Voila !
To test whether things are still working.
In first window:
$cd $/agentThis will start the snmpd deamon in the debugging mode (you will see LOTS of messages).
$./snmpd -f -L -D
In the second window:
$cd agent/mibgroup/examples/subagentFinally in the third window, the command and output should look like (output is shown in bold):
$./example2
[root@x mibs]# snmpget -v 1 -c democommunity localhost firstKey.0In the output above notice that every snmpget query returns an increasing value of "firstKey" and snmpset lets us set "firstKey" to an arbitrary value (within a defined range).
Unlinked OID in JM-TEST-1-MIB: jmtest ::= { experimental 72 }
JM-TEST-1-MIB::firstKey.0 = 1
[root@x mibs]# snmpget -v 1 -c democommunity localhost 1.3.6.1.3.72.1.0
Unlinked OID in JM-TEST-1-MIB: jmtest ::= { experimental 72 }
JM-TEST-1-MIB::firstKey.0 = 2
[root@x mibs]# snmpset -v 1 -c democommunity localhost 1.3.6.1.3.72.1.0 i 10
Unlinked OID in JM-TEST-1-MIB: jmtest ::= { experimental 72 }
JM-TEST-1-MIB::firstKey.0 = 10
[root@x mibs]# snmpget -v 1 -c democommunity localhost firstKey.0
Unlinked OID in JM-TEST-1-MIB: jmtest ::= { experimental 72 }
JM-TEST-1-MIB::firstKey.0 = 10
[root@x mibs]# snmpget -v 1 -c democommunity localhost 1.3.6.1.3.72.1.0
Unlinked OID in JM-TEST-1-MIB: jmtest ::= { experimental 72 }
JM-TEST-1-MIB::firstKey.0 = 11
[root@x mibs]# snmpget -v 1 -c democommunity localhost 1.3.6.1.3.72.1.0
Unlinked OID in JM-TEST-1-MIB: jmtest ::= { experimental 72 }
JM-TEST-1-MIB::firstKey.0 = 12
[root@x mibs]# snmpwalk -v 1 -c democommunity localhost firstKey
Unlinked OID in JM-TEST-1-MIB: jmtest ::= { experimental 72 }
JM-TEST-1-MIB::firstKey.0 = 13
We now have a working subagent. In real applications the subagent could be embedded in the application to be managed.