new config
This commit is contained in:
snoop 2017-04-26 17:18:44 +09:00
parent 7c7602a11c
commit eedd15e5fc
5 changed files with 290 additions and 159 deletions

1
.gitignore vendored
View File

@ -20,6 +20,7 @@
# Gradle: # Gradle:
.idea/**/gradle.xml .idea/**/gradle.xml
.idea/**/libraries .idea/**/libraries
.idea/
# Mongo Explorer plugin: # Mongo Explorer plugin:
.idea/**/mongoSettings.xml .idea/**/mongoSettings.xml

View File

@ -4,7 +4,8 @@ import com.loafle.overflow.crawler.Crawler;
import com.loafle.overflow.crawler.config.Config; import com.loafle.overflow.crawler.config.Config;
import com.loafle.overflow.crawler.config.Connection; import com.loafle.overflow.crawler.config.Connection;
import com.loafle.overflow.crawler.config.Item; import com.loafle.overflow.crawler.config.Item;
import com.loafle.overflow.crawler.config.Query;
import com.loafle.overflow.crawler.result.OFResultSet;
import com.loafle.overflow.crawler.snmp.version.SNMPv2c; import com.loafle.overflow.crawler.snmp.version.SNMPv2c;
import com.loafle.overflow.crawler.snmp.version.SNMPv3; import com.loafle.overflow.crawler.snmp.version.SNMPv3;
import org.snmp4j.Snmp; import org.snmp4j.Snmp;
@ -13,6 +14,7 @@ import org.snmp4j.smi.Address;
import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.transport.DefaultUdpTransportMapping;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -25,69 +27,51 @@ public class SNMPCrawler extends Crawler {
@Override @Override
public Object getInternal(Config config) { public Object getInternal(Config config) {
Object retObj = null;
String targetVer = (String)config.getTarget().getAuth().get("version");
switch (targetVer) {
case "v2c":
retObj = getByV2c(config);
break;
case "v3":
retObj = getByV3(config);
break;
default:
new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
}
Map m = (Map<String, String>)retObj; // String targetVer = (String)config.getTarget().getAuth().get("version");
// switch (targetVer) {
// case "v2c":
// retObj = getByV2c(config);
// break;
// case "v3":
// retObj = getByV3(config);
// break;
// default:
// new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
// }
Map<String, String> keyMap = new LinkedHashMap<>(); return getSNMP(config);
List<String> metrics = config.getItems().get(0).getMetrics();
int i = 0;
for (Object key : m.keySet()) {
keyMap.put(metrics.get(i), (String)m.get(key));
i++;
}
return keyMap;
} }
private Object getByV2c(Config config) { private List<OFResultSet> getSNMP(Config config) {
Snmp snmp = null; Snmp snmp = null;
TransportMapping<? extends Address> transport = null; TransportMapping<? extends Address> transport = null;
List<OFResultSet> rslist = null;
try { try {
transport = new DefaultUdpTransportMapping(); transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport); snmp = new Snmp(transport);
transport.listen(); transport.listen();
SNMPv2c snmpV2 = new SNMPv2c(snmp);
Map<String, Object> authInfo = config.getTarget().getAuth(); rslist = new ArrayList<>();
Connection connInfo = config.getTarget().getConnection();
List<Item> items = config.getItems(); List<Item> items = config.getItems();
String[] oids = new String[items.get(0).getQueries().size()]; String[] oids = null;
Map<String, String> snmpResultMap = null;
for (Item item : items) { for (Item item : items) {
List<Query> queries = item.getQueries(); oids = new String[item.getKeys().size()];
for (int i=0; i<queries.size();i++) { for (int i=0; i<item.getKeys().size();i++) {
oids[i] = queries.get(i).getQuery(); oids[i] = item.getKeys().get(i).getKey();
} }
}
String ip = connInfo.getIp(); snmpResultMap = getSNMPV(snmp, config, oids);
String port = connInfo.getPort(); rslist.add(convertResultSet(snmpResultMap, item));
String community = (String)authInfo.get("community");
return snmpV2.get(ip, port, community, oids);
/*
switch (method) {
case "validate":
return snmpV2.validate(ip, port, community);
case "get":
return snmpV2.get(ip, port, community, oids);
case "walk":
return snmpV2.walk(ip, port, community, oids[0]);
default:
new Exception("Unknown SNMP command : " + method).printStackTrace();
} }
*/ return rslist;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -107,75 +91,217 @@ public class SNMPCrawler extends Crawler {
} }
} }
} }
return null;
return rslist;
} }
private Object getByV3(Config config) { private Map<String, String> getSNMPV(Snmp snmp, Config config, String[] oids) throws Exception {
Snmp snmp = null;
TransportMapping<? extends Address> transport = null;
try { String targetVer = (String)config.getTarget().getAuth().get("version");
transport = new DefaultUdpTransportMapping(); switch (targetVer) {
snmp = new Snmp(transport); case "v2c":
transport.listen(); return getV2c(snmp, config, oids);
SNMPv3 snmpV3 = new SNMPv3(snmp); case "v3":
return getV3(snmp, config, oids);
default:
Map<String, Object> authInfo = config.getTarget().getAuth(); new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
Connection connInfo = config.getTarget().getConnection();
List<Item> items = config.getItems();
String[] oids = new String[items.get(0).getQueries().size()];
for (Item item : items) {
List<Query> queries = item.getQueries();
for (int i=0; i<queries.size();i++) {
oids[i] = queries.get(i).getQuery();
}
}
String ip = connInfo.getIp();
String port = connInfo.getPort();
String method = "get";
String user = (String)authInfo.get("user");
String authType = (String)authInfo.get("authType");
String authPass = (String)authInfo.get("authPass");
String privType = (String)authInfo.get("privType");
String privPass = (String)authInfo.get("privPass");
return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
/*
switch (method) {
case "validate":
return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass);
case "get":
return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
case "walk":
return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]);
default:
new Exception("Unknown SNMP command : " + method).printStackTrace();
}
*/
} catch (Exception e) {
new Exception("An Exception happend : " + e.getMessage()).printStackTrace();
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (transport != null) {
try {
transport.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
return null; return null;
} }
private Map<String, String> getV2c(Snmp snmp, Config config, String[] oids) throws Exception {
SNMPv2c snmpV2 = new SNMPv2c(snmp);
Map<String, Object> authInfo = config.getTarget().getAuth();
Connection connInfo = config.getTarget().getConnection();
String ip = connInfo.getIp();
String port = connInfo.getPort();
String community = (String)authInfo.get("community");
return snmpV2.get(ip, port, community, oids);
}
private Map<String, String> getV3(Snmp snmp, Config config, String[] oids) throws Exception {
SNMPv3 snmpV3 = new SNMPv3(snmp);
Map<String, Object> authInfo = config.getTarget().getAuth();
Connection connInfo = config.getTarget().getConnection();
String ip = connInfo.getIp();
String port = connInfo.getPort();
// String method = config.get;
String user = (String)authInfo.get("user");
String authType = (String)authInfo.get("authType");
String authPass = (String)authInfo.get("authPass");
String privType = (String)authInfo.get("privType");
String privPass = (String)authInfo.get("privPass");
return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
}
protected OFResultSet convertResultSet(Map<String, String> map, Item item) {
OFResultSet rs = OFResultSet.newInstance(item);
List<String> row = new ArrayList<>();
for (String key : map.keySet()) {
row.add(map.get(key));
}
rs.addRow(row);
return rs;
}
//
// private List<OFResultSet> getByV2c(Config config) {
// Snmp snmp = null;
// TransportMapping<? extends Address> transport = null;
//
// List<OFResultSet> rslist = null;
//
// try {
// transport = new DefaultUdpTransportMapping();
// snmp = new Snmp(transport);
// transport.listen();
// SNMPv2c snmpV2 = new SNMPv2c(snmp);
//
// rslist = new ArrayList<>();
//
// Map<String, Object> authInfo = config.getTarget().getAuth();
// Connection connInfo = config.getTarget().getConnection();
//
// String ip = connInfo.getIp();
// String port = connInfo.getPort();
// String community = (String)authInfo.get("community");
//
// List<Item> items = config.getItems();
// String[] oids = null;
// for (Item item : items) {
// oids = new String[item.getKeys().size()];
//// List<Query> queries = item.getQueries();
// for (int i=0; i<item.getKeys().size();i++) {
// oids[i] = item.getKeys().get(i).getKey();
// }
//
// rslist.add(convertResultSet(snmpV2.get(ip, port, community, oids), item));
//
// }
// return rslist;
// /*
// switch (method) {
// case "validate":
// return snmpV2.validate(ip, port, community);
// case "get":
// return snmpV2.get(ip, port, community, oids);
// case "walk":
// return snmpV2.walk(ip, port, community, oids[0]);
// default:
// new Exception("Unknown SNMP command : " + method).printStackTrace();
// }
// */
//
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// if (snmp != null) {
// try {
// snmp.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (transport != null) {
// try {
// transport.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// return null;
// }
//
// private Object getByV3(Config config) {
// Snmp snmp = null;
// TransportMapping<? extends Address> transport = null;
//
// List<OFResultSet> rslist = null;
//
// try {
// transport = new DefaultUdpTransportMapping();
// snmp = new Snmp(transport);
// transport.listen();
// SNMPv3 snmpV3 = new SNMPv3(snmp);
//
// rslist = new ArrayList<>();
//
// Map<String, Object> authInfo = config.getTarget().getAuth();
// Connection connInfo = config.getTarget().getConnection();
//
// String ip = connInfo.getIp();
// String port = connInfo.getPort();
//// String method = config.get;
//
// String user = (String)authInfo.get("user");
// String authType = (String)authInfo.get("authType");
// String authPass = (String)authInfo.get("authPass");
// String privType = (String)authInfo.get("privType");
// String privPass = (String)authInfo.get("privPass");
//
// List<Item> items = config.getItems();
// String[] oids = null;
// for (Item item : items) {
// oids = new String[item.getKeys().size()];
//// List<Query> queries = item.getQueries();
// for (int i=0; i<item.getKeys().size();i++) {
// oids[i] = item.getKeys().get(i).getKey();
// }
//
//// rslist.add(convertResultSet(snmpV2.get(ip, port, community, oids), item));
// rslist.add(convertResultSet(snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids), item));
// }
//
// return rslist;
//
// /*
// switch (method) {
// case "validate":
// return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass);
// case "get":
// return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
// case "walk":
// return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]);
// default:
// new Exception("Unknown SNMP command : " + method).printStackTrace();
// }
// */
//
// } catch (Exception e) {
// new Exception("An Exception happend : " + e.getMessage()).printStackTrace();
// } finally {
// if (snmp != null) {
// try {
// snmp.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (transport != null) {
// try {
// transport.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// return null;
// }
} }

View File

@ -3,11 +3,13 @@ package com.loafle;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.loafle.overflow.crawler.config.Config; import com.loafle.overflow.crawler.config.Config;
import com.loafle.overflow.crawler.result.OFResultSet;
import com.loafle.overflow.crawler.snmp.SNMPCrawler; import com.loafle.overflow.crawler.snmp.SNMPCrawler;
import com.sun.xml.internal.ws.api.ResourceLoader; import com.sun.xml.internal.ws.api.ResourceLoader;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import java.util.List;
import java.util.Map; import java.util.Map;
public class AppTest { public class AppTest {
@ -26,7 +28,7 @@ public class AppTest {
*/ */
@Test @Test
public void testSNMP() throws Exception { public void testSNMP3() throws Exception {
SNMPCrawler c = new SNMPCrawler(); SNMPCrawler c = new SNMPCrawler();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
String p = ResourceLoader.class.getClass().getResource("/config/examplev3.json").getPath(); String p = ResourceLoader.class.getClass().getResource("/config/examplev3.json").getPath();
@ -37,14 +39,36 @@ public class AppTest {
if(result instanceof Boolean) { if(result instanceof Boolean) {
System.out.println("validate : " + result); System.out.println("validate : " + result);
}else { }else {
printResult((Map<String, String>)result); printResult((List<OFResultSet>)result);
} }
} }
private void printResult(Map<String, String> result) { @Test
for (Map.Entry<String, String> entry : result.entrySet()) { public void testSNMP2c() throws Exception {
System.out.printf(" %s - %s\n", entry.getKey(), entry.getValue()); SNMPCrawler c = new SNMPCrawler();
ObjectMapper mapper = new ObjectMapper();
String p = ResourceLoader.class.getClass().getResource("/config/examplev2.json").getPath();
Config config = mapper.readValue(new File(p),Config.class);
Object result = c.getInternal(config);
if(result instanceof Boolean) {
System.out.println("validate : " + result);
}else {
printResult((List<OFResultSet>)result);
}
}
private void printResult(List<OFResultSet> resultList) {
for(int indexI = 0 ; indexI < resultList.size(); ++indexI) {
Map<String,String> resultMap = resultList.get(indexI).getData();
System.out.println("============================================");
for( String key : resultMap.keySet() ){
System.out.println( String.format("key : %s ||| value : %s", key, resultMap.get(key)) );
}
System.out.println("============================================");
} }
} }

View File

@ -2,14 +2,14 @@
"id" : "insanity", "id" : "insanity",
"target" : { "target" : {
"connection" : { "connection" : {
"ip" : "192.168.1.215", "ip" : "192.168.1.254",
"port" : "161", "port" : "161",
"ssl" : false, "ssl" : false,
"portType" : "udp" "portType" : "udp"
}, },
"auth" : { "auth" : {
"community" : "public", "version":"v2c",
"version" : "v2c" "community" : "loafle"
} }
}, },
"schedule" : { "schedule" : {
@ -21,38 +21,18 @@
}, },
"items" : [ "items" : [
{ {
"metrics" : [ "keys" : [
"mem.total", {"metric":"system.uptime", "key":"1.3.6.1.2.1.1.3.0"}
"mem.free",
"mem.shared",
"mem.buffered",
"mem.cached",
"mem.totalFree",
"swap.avaliable"
], ],
"queries":[ "queryInfo" : {
{ "query": "mem",
"query" : ".1.3.6.1.4.1.2021.4.5.0" "extend" : {
}, "method": "get"
{
"query" : ".1.3.6.1.4.1.2021.4.6.0"
},
{
"query" : ".1.3.6.1.4.1.2021.4.13.0"
},
{
"query" : ".1.3.6.1.4.1.2021.4.14.0"
},
{
"query" : ".1.3.6.1.4.1.2021.4.15.0"
},
{
"query" : ".1.3.6.1.4.1.2021.4.11.0"
},
{
"query" : ".1.3.6.1.4.1.2021.4.4.0"
} }
] },
"mappingInfo" : {
"parseDirection" : "col"
}
} }
] ]
} }

View File

@ -25,18 +25,18 @@
}, },
"items" : [ "items" : [
{ {
"metrics" : [ "keys" : [
"nic[0].inoctets", {"metric":"system.uptime", "key":"1.3.6.1.2.1.1.3.0"}
"nic[1].inoctets"
], ],
"queries":[ "queryInfo" : {
{ "query": "mem",
"query" : "1.3.6.1.2.1.2.2.1.10.1" "extend" : {
}, "method": "get"
{
"query" : ".1.3.6.1.2.1.2.2.1.10.2"
} }
] },
"mappingInfo" : {
"parseDirection" : "col"
}
} }
] ]
} }