SNMP
Par PlaceOweb le mardi, mai 3 2011, 01:34 - Système - Lien permanent
Quelques notes sur SNMP, ses OID MIB et des implémentation en Java de SNMP (Simple Network Management Protocol).
- Simple network management protocol Simple Network Management Protocol
- Présentation du protocole SNMP
- OID Repository
- RFC
- How to install and configure Windows SNMP agent (XP/Vista/7/2000-2008) Comparé à celui de Microsoft l’agent SNMP de NuDesign est riche de fonctionnalités et apporte principalement le support de SNMP en version 3 qui garantie la sécurité des communications grâce à de l’authentification et du chiffrement entre agents et managers SNMP.
- agent snmp v3 sous windows (xp)
- SNMP OIDs for Synology DS-101 Tous ces OIDs peuvent être interrogés sans que le disque dur ne soit sorti de veille.
- Installation et configuration de Nagios pour débutants
- Nagios / SNMP tools
net-snmp provides the snmpd daemon's uptime in DISMAN-EVENT-MIB::sysUpTimeInstance (.1.3.6.1.2.1.1.3.0) The real system uptime is available as HOST-RESOURCES-MIB::hrSystemUptime.0 (.1.3.6.1.2.1.25.1.1.0) Many other devices like switches provide only the former OID. This script can read either of them. Use --sysUpTime or --hrSystemUptime to select the appropriate OID for each device.
- Simple Network Management Protocol
- Management Information Base Database Every variable in the Internet-standard Management Information Base (MIB) has a value that can be queried and used for this purpose.
1.3.6.1.2.1.1.3.0 uptime 1.3.6.1.2.1.1.1.0 les caractéristique d'une machine (matériellement parlant) 1.3.6.1.2.1.1.5.0 le nom de la machine 1.3.6.1.2.1.1.3.0 le temps et bla bla bla
# snmpwalk -v 2c -c public localhost .1.3.6.1.4.1.2021.13.16.2.1.1 LM-SENSORS-MIB::lmTempSensorsIndex = No Such Object available on this agent at this OID
- Package org.snmp4j Provides classes and interfaces for creating, sending, and receiving SNMP message
- org.snmp4j - Class Snmp
# snmpwalk -v 2c -c public localhost > /tmp/snmp # snmpwalk -v 2c -O n -c public localhost > /tmp/snmpn
Address targetAddress = GenericAddress.parse("udp:localhost/161"); String comm = "public"; OID oid = new OID(".1.3.6.1.2.1.1"); List<VariableBinding> result = SnmpUtils.walk(SnmpConstants.version2c,targetAddress,comm,oid); for (VariableBinding vb: result) { out.println(vb.getOid()+" : "+vb.getVariable().toString()+"<br/>"); }
package org.jbs.snmputils; import java.util.List; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.Address; import org.snmp4j.smi.OID; import org.snmp4j.smi.VariableBinding; /** * * @author masjoko */ public class SnmpUtils { public static String get(int version, Address targetaddress, String comm, OID oid) { if (version==SnmpConstants.version1) { return SnmpV1Utils.get(targetaddress, comm, oid); } else if (version==SnmpConstants.version2c) { return SnmpV2cUtils.get(targetaddress, comm, oid); } else { return null; } } public static VariableBinding getNext(int version, Address targetaddress, String comm, OID oid) { if (version==SnmpConstants.version1) { return SnmpV1Utils.getNext(targetaddress, comm, oid); } else if (version==SnmpConstants.version2c) { return SnmpV2cUtils.getNext(targetaddress, comm, oid); } else { return null; } } public static List<VariableBinding> walk(int version, Address address, String comm, OID rootOID) { if (version==SnmpConstants.version1) { return SnmpV1Utils.walk(address, comm, rootOID); } else if (version==SnmpConstants.version2c) { return SnmpV2cUtils.walk(address, comm, rootOID); } else { return null; } } }
package org.jbs.snmputils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.Address; import org.snmp4j.smi.Integer32; import org.snmp4j.smi.Null; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; public class SnmpV2cUtils { public static String get(Address targetaddress, String comm, OID oid) { String ret = null; TransportMapping transport; try { //create transport transport = new DefaultUdpTransportMapping(); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(targetaddress); target.setRetries(3); target.setTimeout(2000); target.setVersion(SnmpConstants.version2c); // create the PDU PDU pdu = new PDU(); pdu.setType(PDU.GET); //put the oid you want to get pdu.add(new VariableBinding(oid)); pdu.setNonRepeaters(0); //pdu string System.out.println("pdu " + pdu.toString()); Snmp snmp = new Snmp(transport); snmp.listen(); // send the PDU ResponseEvent responseEvent = snmp.send(pdu, target); Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,responseEvent.toString()); // extract the response PDU (could be null if timed out) PDU responsePDU = responseEvent.getResponse(); Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,responsePDU.toString()); Vector vbs = responsePDU.getVariableBindings(); if (vbs.size()>0) { VariableBinding vb = (VariableBinding) vbs.get(0); ret = vb.getVariable().toString(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ret; } public static VariableBinding getNext(Address targetaddress, String comm, OID oid) { VariableBinding ret = null; TransportMapping transport; try { //create transport transport = new DefaultUdpTransportMapping(); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(targetaddress); target.setRetries(3); target.setTimeout(2000); target.setVersion(SnmpConstants.version2c); // create the PDU PDU pdu = new PDU(); pdu.setType(PDU.GETNEXT); //put the oid you want to get pdu.add(new VariableBinding(oid)); pdu.setNonRepeaters(0); //pdu string System.out.println("pdu " + pdu.toString()); Snmp snmp = new Snmp(transport); snmp.listen(); // send the PDU ResponseEvent responseEvent = snmp.send(pdu, target); Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,responseEvent.toString()); // extract the response PDU (could be null if timed out) PDU responsePDU = responseEvent.getResponse(); Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,responsePDU.toString()); Vector vbs = responsePDU.getVariableBindings(); if (vbs.size()>0) { ret = (VariableBinding) vbs.get(0); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ret; } public static List<VariableBinding> walk(Address address, String comm, OID rootOID) { List<VariableBinding> ret = new ArrayList<VariableBinding>(); PDU requestPDU = new PDU(); requestPDU.add(new VariableBinding(rootOID)); requestPDU.setType(PDU.GETBULK); // maximum oid per pdu request requestPDU.setMaxRepetitions(5); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(comm)); target.setAddress(address); target.setVersion(SnmpConstants.version2c); try { TransportMapping transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); transport.listen(); boolean finished = false; int iter = 0; while (!finished) { VariableBinding vb = null; ResponseEvent respEvt = snmp.send(requestPDU, target); Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,"GETBULK iteration number "+iter++); PDU responsePDU = respEvt.getResponse(); if (responsePDU != null) { Vector vbs = responsePDU.getVariableBindings(); if (vbs!=null && vbs.size()>0) { for (int i=0; i<vbs.size(); i++) { // vb sanity check vb = (VariableBinding) vbs.get(i); if (vb.getOid() == null) { Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,"vb.getOid() == null"); finished = true; break; } else if (vb.getOid().size() < rootOID.size()) { Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,"vb.getOid().size() < targetOID.size()"); finished = true; break; } else if (rootOID.leftMostCompare(rootOID.size(), vb.getOid()) != 0) { Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,"targetOID.leftMostCompare() != 0)"); finished = true; break; } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,"Null.isExceptionSyntax(vb.getVariable().getSyntax())"); finished = true; break; } else if (vb.getOid().compareTo(rootOID) <= 0) { Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,"Variable received is not "+ "lexicographic successor of requested "+ "one:"); Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,vb.toString() + " <= "+rootOID); finished = true; break; } ret.add(vb); } } } if (!finished) { if (responsePDU == null) { Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,"responsePDU == null"); finished = true; } else if (responsePDU.getErrorStatus() != 0) { Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,"responsePDU.getErrorStatus() != 0"); Logger.getLogger(SnmpV2cUtils.class.getName()).log(Level.INFO,responsePDU.getErrorStatusText()); finished = true; } else { // Set up the variable binding for the next entry. requestPDU.setRequestID(new Integer32(0)); requestPDU.set(0, vb); } } } snmp.close(); } catch (IOException e) { e.printStackTrace(); } return ret; } }
// CPU Load pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.10.1.3.1"))); // 1 minute Load: pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.10.1.3.2"))); // 5 minute Load: pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.10.1.3.3"))); // 15 minute Load pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.11.11.0"))); // percentages of idle CPU time // RAM pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.4.5.0"))); // Total RAM in machine pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.4.6.0"))); // Total RAM used ??? FREE en fait ??? - http://www.oid-info.com/get/1.3.6.1.4.1.2021.4.6 // pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.4.6.11"))); // noSuchInstance - Total RAM Free - http://www.oid-info.com/get/1.3.6.1.4.1.2021.4.11 // Disk pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.9.1.6.1"))); // Total size of the disk/partion (kBytes) pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2021.9.1.7.1"))); // Available space on the disk // Uptime pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.0"))); // System Uptime (SNMPD) // 17:26:55 up 2:13, load average: 0.25, 0.21, 0.08 // 2:14:14.79 pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.25.1.1.0"))); // Hardware Uptime