/* Assigned Later */
boolean send_separate_queries = false;
final HashMap<String,String> oids = new HashMap<String,String>();
Target target;
HashMap<String, ArrayList<oid_data>> oid_hashmap = new HashMap<String, ArrayList<oid_data>>();
OctetString localEngineID = new OctetString(MPv3.createLocalEngineID());
if (port == null) {
port = "161";
}
if (version == null) {
version = "2c";
}
if (community == null) {
community = "public";
}
if ((separate_queries != null) && ((separate_queries.equals("on")) || separate_queries.equals("true"))) {
send_separate_queries = true;
}
Address targetAddress = GenericAddress.parse("udp:"+host+"/"+port);
for (Map.Entry<String, String> entry : config.entrySet())
{
String type = entry.getKey();
if (type.length() > 4) {
if (type.substring(0, 3).equals("oid")) {
String type_string;
String metric_type;
oid_data data = new oid_data();
data.name = type.substring(4);
data.oid = entry.getValue();
type_string = "type_" + data.name;
data.metric_type = config.get(type_string);
oids.put(data.name, data.oid);
if (oid_hashmap.containsKey(data.oid.substring(1))) {
ArrayList<oid_data> list = oid_hashmap.remove(data.oid.substring(1));
list.add(data);
oid_hashmap.put(data.oid.substring(1), list);
}
else {
ArrayList<oid_data> list = new ArrayList<oid_data>();
list.add(data);
oid_hashmap.put(data.oid.substring(1), list);
}
}
}
}
UdpAddress udpAddr = new UdpAddress(UdpAddress.ANY_IPADDRESS, 0);
Snmp snmp = new Snmp(new DefaultUdpTransportMapping(udpAddr));
/* the snmp connection is open... add a seprate try/catch so we can make sure we close it */
try {
if ((version.equals("1")) || (version.equals("2c"))) {
target = getCommunityTarget(targetAddress, Integer.parseInt(timeout), community, version);
}
else {
OID auth = null;
OID priv = null;
if (auth_protocol != null) {
if (auth_protocol.equals("MD5")) {
auth = AuthMD5.ID;
}
else if (auth_protocol.equals("SHA")) {
auth = AuthSHA.ID;
}
}
if (privacy_protocol != null) {
if (privacy_protocol.equals("AES128")) {
priv = PrivAES128.ID;
}
else if (privacy_protocol.equals("AES192")) {
priv = PrivAES192.ID;
}
else if (privacy_protocol.equals("AES256")) {
priv = PrivAES256.ID;
}
else if (privacy_protocol.equals("DES")) {
priv = PrivDES.ID;
}
}
SecurityProtocols sec = SecurityProtocols.getInstance();
((MPv3)snmp.getMessageProcessingModel(MPv3.ID)).setLocalEngineID(localEngineID.getValue());
USM usm = new USM(sec, localEngineID, 0);
SecurityModels.getInstance().addSecurityModel(usm);
UsmUser user = new UsmUser(new OctetString(security_name),
auth,
new OctetString(auth_passphrase),
priv,
new OctetString(privacy_passphrase));
snmp.getUSM().addUser(user);
SecurityModels.getInstance().addSecurityModel(new TSM(localEngineID, false));
target = getUserTarget(targetAddress, Integer.parseInt(timeout), security_level, security_name, security_engine);
}
snmp.listen();