snmp walk a potřebne upravy objektu

verze3
Michal 2026-08-30 23:28:19 +02:00
parent c17aa65da1
commit aeab64b40c
3 changed files with 99 additions and 58 deletions

View File

@ -5,8 +5,6 @@ import org.snmp4j.PDU;
import org.snmp4j.Snmp; import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent; import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants; import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory; import org.snmp4j.util.DefaultPDUFactory;
@ -14,6 +12,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import jnet.lib.object.SnmpWalkItem; import jnet.lib.object.SnmpWalkItem;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.SMIConstants;
import org.snmp4j.smi.VariableBinding;
public class SnmpWalk { public class SnmpWalk {
@ -22,8 +23,7 @@ public class SnmpWalk {
public static List<SnmpWalkItem> walk(String rootOid, CommunityTarget target, AtomicBoolean cancelFlag) throws Exception { public static List<SnmpWalkItem> walk(String rootOid, CommunityTarget target, AtomicBoolean cancelFlag) throws Exception {
List<SnmpWalkItem> result = new ArrayList<>(); List<SnmpWalkItem> result = new ArrayList<>();
try (DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); try (DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport)) {
Snmp snmp = new Snmp(transport)) {
transport.listen(); transport.listen();
int pduType = (target.getVersion() == SnmpConstants.version1) ? PDU.GETNEXT : PDU.GETBULK; int pduType = (target.getVersion() == SnmpConstants.version1) ? PDU.GETNEXT : PDU.GETBULK;
@ -40,34 +40,44 @@ public class SnmpWalk {
pdu.add(new VariableBinding(currentOid)); pdu.add(new VariableBinding(currentOid));
ResponseEvent event = snmp.send(pdu, target); ResponseEvent event = snmp.send(pdu, target);
if (event == null) break; if (event == null) {
break;
}
PDU responsePDU = event.getResponse(); PDU responsePDU = event.getResponse();
if (responsePDU == null) break; if (responsePDU == null) {
break;
}
// Oprava: používáme List, nikoli pole // Oprava: používáme List, nikoli pole
List<? extends VariableBinding> vbs = responsePDU.getVariableBindings(); List<? extends VariableBinding> vbs = responsePDU.getVariableBindings();
if (vbs == null || vbs.isEmpty()) break; if (vbs == null || vbs.isEmpty()) {
break;
}
for (VariableBinding vb : vbs) { for (VariableBinding vb : vbs) {
if (vb == null) continue; if (vb == null) {
continue;
}
OID oid = vb.getOid(); OID oid = vb.getOid();
if (oid == null) continue; if (oid == null) {
continue;
}
int syntax = vb.getVariable().getSyntax(); int syntax = vb.getVariable().getSyntax();
if (syntax == org.snmp4j.smi.SMIConstants.EXCEPTION_END_OF_MIB_VIEW) { if (syntax == SMIConstants.EXCEPTION_END_OF_MIB_VIEW) {
finished = true; finished = true;
break; break;
} }
if (syntax == org.snmp4j.smi.SMIConstants.EXCEPTION_NO_SUCH_OBJECT || if (syntax == SMIConstants.EXCEPTION_NO_SUCH_OBJECT
syntax == org.snmp4j.smi.SMIConstants.EXCEPTION_NO_SUCH_INSTANCE) { || syntax == SMIConstants.EXCEPTION_NO_SUCH_INSTANCE) {
continue; continue;
} }
result.add(new SnmpWalkItem( result.add(new SnmpWalkItem(
oid.toString(), oid.toString(),
vb.getVariable().toString(), vb.getVariable().toString(),
getTypeName(syntax) getTypeName(syntax)
)); ));
currentOid = oid; currentOid = oid;
} }
@ -80,22 +90,69 @@ public class SnmpWalk {
return result; return result;
} }
private static String getTypeName(int syntax) { public static String getTypeName(int syntax) {
switch (syntax) { switch (syntax) {
case org.snmp4j.smi.SMIConstants.SYNTAX_INTEGER: return "Integer32"; case SMIConstants.SYNTAX_INTEGER:
case org.snmp4j.smi.SMIConstants.SYNTAX_OCTET_STRING: return "OctetString"; return "Integer32";
case org.snmp4j.smi.SMIConstants.SYNTAX_NULL: return "Null"; case SMIConstants.SYNTAX_OCTET_STRING:
case org.snmp4j.smi.SMIConstants.SYNTAX_OBJECT_IDENTIFIER:return "OID"; return "OctetString";
case org.snmp4j.smi.SMIConstants.SYNTAX_IPADDRESS: return "IpAddress"; case SMIConstants.SYNTAX_NULL:
case org.snmp4j.smi.SMIConstants.SYNTAX_COUNTER32: return "Counter32"; return "Null";
case org.snmp4j.smi.SMIConstants.SYNTAX_GAUGE32: return "Gauge32"; case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
case org.snmp4j.smi.SMIConstants.SYNTAX_TIMETICKS: return "TimeTicks"; return "OID";
case org.snmp4j.smi.SMIConstants.SYNTAX_OPAQUE: return "Opaque"; case SMIConstants.SYNTAX_IPADDRESS:
case org.snmp4j.smi.SMIConstants.SYNTAX_COUNTER64: return "Counter64"; return "IpAddress";
case org.snmp4j.smi.SMIConstants.EXCEPTION_NO_SUCH_OBJECT: return "NoSuchObject"; case SMIConstants.SYNTAX_COUNTER32:
case org.snmp4j.smi.SMIConstants.EXCEPTION_NO_SUCH_INSTANCE:return "NoSuchInstance"; return "Counter32";
case org.snmp4j.smi.SMIConstants.EXCEPTION_END_OF_MIB_VIEW: return "EndOfMibView"; case SMIConstants.SYNTAX_GAUGE32:
default: return "Unknown (" + syntax + ")"; return "Gauge32";
case SMIConstants.SYNTAX_TIMETICKS:
return "TimeTicks";
case SMIConstants.SYNTAX_OPAQUE:
return "Opaque";
case SMIConstants.SYNTAX_COUNTER64:
return "Counter64";
case SMIConstants.EXCEPTION_NO_SUCH_OBJECT:
return "NoSuchObject";
case SMIConstants.EXCEPTION_NO_SUCH_INSTANCE:
return "NoSuchInstance";
case SMIConstants.EXCEPTION_END_OF_MIB_VIEW:
return "EndOfMibView";
default:
return "Unknown (" + syntax + ")";
} }
} }
}
public static int getTypeId(String syntax) {
switch (syntax) {
case "Integer32":
return SMIConstants.SYNTAX_INTEGER;
case "OctetString":
return SMIConstants.SYNTAX_OCTET_STRING;
case "Null":
return SMIConstants.SYNTAX_NULL;
case "OID":
return SMIConstants.SYNTAX_OBJECT_IDENTIFIER;
case "IpAddress":
return SMIConstants.SYNTAX_IPADDRESS;
case "Gauge32":
return SMIConstants.SYNTAX_GAUGE32;
case "Counter32":
return SMIConstants.SYNTAX_COUNTER32;
case "TimeTicks":
return SMIConstants.SYNTAX_TIMETICKS;
case "Opaque":
return SMIConstants.SYNTAX_OPAQUE;
case "Counter64":
return SMIConstants.SYNTAX_COUNTER64;
case "NoSuchObject":
return SMIConstants.EXCEPTION_NO_SUCH_OBJECT;
case "NoSuchInstance":
return SMIConstants.EXCEPTION_NO_SUCH_INSTANCE;
case "EndOfMibView":
return SMIConstants.EXCEPTION_END_OF_MIB_VIEW;
default:
return 0;
}
}
}

View File

@ -8,15 +8,6 @@ import org.snmp4j.smi.Variable;
public class NodeProbe implements Serializable { public class NodeProbe implements Serializable {
public static final String DATA_TYPE_INTEGER = "INTEGER";
public static final String DATA_TYPE_COUNTER = "COUNTER";
public static final String DATA_TYPE_GAUGE = "GAUGE";
public static final String DATA_TYPE_TIMESTICK = "TIMETICKS";
public static final String DATA_TYPE_IPADDRESS = "IPADDRESS";
public static final String DATA_TYPE_STRING = "STRING";
public static final String DATA_TYPE_OID = "OID";
public static final String DATA_TYPE_NULL = "NULL";
public static final String CONVERSION_TYPE_NONE = "NONE"; public static final String CONVERSION_TYPE_NONE = "NONE";
public static final String CONVERSION_TYPE_DIVIDE = "DIVIDE"; public static final String CONVERSION_TYPE_DIVIDE = "DIVIDE";
public static final String CONVERSION_TYPE_MULTIPLY = "MULTIPLY"; public static final String CONVERSION_TYPE_MULTIPLY = "MULTIPLY";
@ -27,7 +18,7 @@ public class NodeProbe implements Serializable {
private int id; private int id;
private String name; private String name;
private String oid; private String oid;
private String dataType; private int dataType;
private String conversionType; private String conversionType;
private double conversionValue; private double conversionValue;
private String conversionExpression; private String conversionExpression;
@ -38,7 +29,7 @@ public class NodeProbe implements Serializable {
private int nodeId; private int nodeId;
private boolean viewOnMap; private boolean viewOnMap;
public NodeProbe(int id, String name, String oid, String dataType, String conversionType, double conversionValue, String conversionExpression, String unit, int valueMap, int interval, boolean viewOnMap) { public NodeProbe(int id, String name, String oid, int dataType, String conversionType, double conversionValue, String conversionExpression, String unit, int valueMap, int interval, boolean viewOnMap) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.oid = oid; this.oid = oid;
@ -80,11 +71,11 @@ public class NodeProbe implements Serializable {
this.oid = oid; this.oid = oid;
} }
public String getDataType() { public int getDataType() {
return dataType; return dataType;
} }
public void setDataType(String dataType) { public void setDataType(int dataType) {
this.dataType = dataType; this.dataType = dataType;
} }

View File

@ -6,16 +6,6 @@ import java.util.List;
public class SnmpTemplate implements Serializable { public class SnmpTemplate implements Serializable {
public static final String DATA_TYPE_INTEGER = "INTEGER";
public static final String DATA_TYPE_COUNTER = "COUNTER";
public static final String DATA_TYPE_GAUGE = "GAUGE";
public static final String DATA_TYPE_TIMESTICK = "TIMETICKS";
public static final String DATA_TYPE_IPADDRESS = "IPADDRESS";
public static final String DATA_TYPE_STRING = "STRING";
public static final String DATA_TYPE_OID = "OID";
public static final String DATA_TYPE_NULL = "NULL";
public static final String CONVERSION_TYPE_NONE = "NONE"; public static final String CONVERSION_TYPE_NONE = "NONE";
public static final String CONVERSION_TYPE_DIVIDE = "DIVIDE"; public static final String CONVERSION_TYPE_DIVIDE = "DIVIDE";
public static final String CONVERSION_TYPE_MULTIPLY = "MULTIPLY"; public static final String CONVERSION_TYPE_MULTIPLY = "MULTIPLY";
@ -27,7 +17,7 @@ public class SnmpTemplate implements Serializable {
private int deviceType; private int deviceType;
private String name; private String name;
private String oid; private String oid;
private String dataType; private int dataType;
private String conversionType; private String conversionType;
private double conversionValue; private double conversionValue;
private String conversionExpression; private String conversionExpression;
@ -35,7 +25,7 @@ public class SnmpTemplate implements Serializable {
private int valueMapId; private int valueMapId;
private String description; private String description;
public SnmpTemplate(int id, int deviceType, String name, String oid, String dataType, String conversionType, double conversionValue, String conversionExpression, String unit, int valueMapId, String description) { public SnmpTemplate(int id, int deviceType, String name, String oid, int dataType, String conversionType, double conversionValue, String conversionExpression, String unit, int valueMapId, String description) {
this.id = id; this.id = id;
this.deviceType = deviceType; this.deviceType = deviceType;
this.name = name; this.name = name;
@ -49,6 +39,9 @@ public class SnmpTemplate implements Serializable {
this.description = description; this.description = description;
} }
public SnmpTemplate() {
}
public int getId() { public int getId() {
return id; return id;
} }
@ -81,11 +74,11 @@ public class SnmpTemplate implements Serializable {
this.oid = oid; this.oid = oid;
} }
public String getDataType() { public int getDataType() {
return dataType; return dataType;
} }
public void setDataType(String dataType) { public void setDataType(int dataType) {
this.dataType = dataType; this.dataType = dataType;
} }