/src/cucky/jGuard/snmp

main
Michal 2024-05-23 15:04:42 +02:00
parent 8e8b3ec31b
commit f9c84518ed
1 changed files with 153 additions and 0 deletions

153
SNMPManager.java Normal file
View File

@ -0,0 +1,153 @@
package cucky.jGuard.snmp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TreeEvent;
import org.snmp4j.util.TreeUtils;
public class SNMPManager {
private Snmp snmp = null;
private String address = null;
private String community = "public";
private int version;
public static final int version1 = SnmpConstants.version1;
public static final int version2c = SnmpConstants.version2c;
public static final int version3 = SnmpConstants.version3;
/**
* Constructor
*
* @param add
*/
public SNMPManager(String address, String port, String community, int version) {
this.address = "udp:"+address+"/"+port;
this.community = community;
this.version = version;
}
/**
* Start the Snmp session. If you forget the listen() method you will not
* get any answers because the communication is asynchronous and the
* listen() method listens for answers.
*
* @throws IOException
*/
public void start() throws IOException {
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
// Do not forget this line!
transport.listen();
}
/**
* Method which takes a single OID and returns the response from the agent
* as a String.
*
* @param oid
* @return
* @throws IOException
*/
public String getAsString(OID oid) throws IOException {
ResponseEvent event = get(new OID[]{oid});
return event.getResponse().get(0).getVariable().toString();
}
/**
* This method is capable of handling multiple OIDs
*
* @param oids
* @return
* @throws IOException
*/
public ResponseEvent get(OID oids[]) throws IOException {
PDU pdu = new PDU();
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}
pdu.setType(PDU.GET);
ResponseEvent event = snmp.send(pdu, getTarget(), null);
if (event != null) {
return event;
}
throw new RuntimeException("GET timed out");
}
/**
* This method returns a Target, which contains information about where the
* data should be fetched and how.
*
* @return
*/
private Target getTarget() {
Address targetAddress = GenericAddress.parse(this.address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(this.community));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(version);
return target;
}
//////////////////
public String getSysDescr() throws IOException{
return getAsString(SnmpConstants.sysDescr);
}
public String getUptime() throws IOException{
return getAsString(SnmpConstants.sysUpTime);
}
public List<TreeEvent> getTree(String sOid){
TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
List<TreeEvent> events = treeUtils.getSubtree(getTarget(), new OID(sOid));
return events;
}
public ArrayList<String> getInterfaceList(){
ArrayList<String> retStr = new ArrayList<String>();
List<TreeEvent> events = getTree("1.3.6.1.2.1.2.2.1.2");
if(events != null || events.size() != 0) {
for (TreeEvent event : events) {
if (event.isError()) {
continue;
}
VariableBinding[] varBindings = event.getVariableBindings();
if (varBindings == null || varBindings.length == 0) {
continue;
}
for (VariableBinding varBinding : varBindings) {
if (varBinding == null) {
continue;
}
retStr.add(varBinding.getVariable().toString());
}
}
}
return retStr;
}
}