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:
.idea/**/gradle.xml
.idea/**/libraries
.idea/
# Mongo Explorer plugin:
.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.Connection;
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.SNMPv3;
import org.snmp4j.Snmp;
@ -13,6 +14,7 @@ import org.snmp4j.smi.Address;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -25,69 +27,51 @@ public class SNMPCrawler extends Crawler {
@Override
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<>();
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;
return getSNMP(config);
}
private Object getByV2c(Config config) {
private List<OFResultSet> getSNMP(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);
Map<String, Object> authInfo = config.getTarget().getAuth();
Connection connInfo = config.getTarget().getConnection();
rslist = new ArrayList<>();
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) {
List<Query> queries = item.getQueries();
for (int i=0; i<queries.size();i++) {
oids[i] = queries.get(i).getQuery();
oids = new String[item.getKeys().size()];
for (int i=0; i<item.getKeys().size();i++) {
oids[i] = item.getKeys().get(i).getKey();
}
}
String ip = connInfo.getIp();
String port = connInfo.getPort();
String community = (String)authInfo.get("community");
return snmpV2.get(ip, port, community, oids);
snmpResultMap = getSNMPV(snmp, config, oids);
rslist.add(convertResultSet(snmpResultMap, item));
/*
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) {
e.printStackTrace();
@ -107,75 +91,217 @@ public class SNMPCrawler extends Crawler {
}
}
}
return null;
return rslist;
}
private Object getByV3(Config config) {
Snmp snmp = null;
TransportMapping<? extends Address> transport = null;
private Map<String, String> getSNMPV(Snmp snmp, Config config, String[] oids) throws Exception {
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
SNMPv3 snmpV3 = new SNMPv3(snmp);
Map<String, Object> authInfo = config.getTarget().getAuth();
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();
}
}
String targetVer = (String)config.getTarget().getAuth().get("version");
switch (targetVer) {
case "v2c":
return getV2c(snmp, config, oids);
case "v3":
return getV3(snmp, config, oids);
default:
new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
}
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.loafle.overflow.crawler.config.Config;
import com.loafle.overflow.crawler.result.OFResultSet;
import com.loafle.overflow.crawler.snmp.SNMPCrawler;
import com.sun.xml.internal.ws.api.ResourceLoader;
import org.junit.Test;
import java.io.File;
import java.util.List;
import java.util.Map;
public class AppTest {
@ -26,7 +28,7 @@ public class AppTest {
*/
@Test
public void testSNMP() throws Exception {
public void testSNMP3() throws Exception {
SNMPCrawler c = new SNMPCrawler();
ObjectMapper mapper = new ObjectMapper();
String p = ResourceLoader.class.getClass().getResource("/config/examplev3.json").getPath();
@ -37,14 +39,36 @@ public class AppTest {
if(result instanceof Boolean) {
System.out.println("validate : " + result);
}else {
printResult((Map<String, String>)result);
printResult((List<OFResultSet>)result);
}
}
private void printResult(Map<String, String> result) {
for (Map.Entry<String, String> entry : result.entrySet()) {
System.out.printf(" %s - %s\n", entry.getKey(), entry.getValue());
@Test
public void testSNMP2c() throws Exception {
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",
"target" : {
"connection" : {
"ip" : "192.168.1.215",
"ip" : "192.168.1.254",
"port" : "161",
"ssl" : false,
"portType" : "udp"
},
"auth" : {
"community" : "public",
"version" : "v2c"
"version":"v2c",
"community" : "loafle"
}
},
"schedule" : {
@ -21,38 +21,18 @@
},
"items" : [
{
"metrics" : [
"mem.total",
"mem.free",
"mem.shared",
"mem.buffered",
"mem.cached",
"mem.totalFree",
"swap.avaliable"
"keys" : [
{"metric":"system.uptime", "key":"1.3.6.1.2.1.1.3.0"}
],
"queries":[
{
"query" : ".1.3.6.1.4.1.2021.4.5.0"
},
{
"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"
"queryInfo" : {
"query": "mem",
"extend" : {
"method": "get"
}
]
},
"mappingInfo" : {
"parseDirection" : "col"
}
}
]
}

View File

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