This commit is contained in:
insanity 2017-04-26 15:14:47 +09:00
parent 14a521dbed
commit 7c7602a11c
5 changed files with 259 additions and 131 deletions

View File

@ -27,7 +27,6 @@
<artifactId>crawler</artifactId> <artifactId>crawler</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -2,6 +2,9 @@ package com.loafle.overflow.crawler.snmp;
import com.loafle.overflow.crawler.Crawler; 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.Item;
import com.loafle.overflow.crawler.config.Query;
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;
@ -10,6 +13,8 @@ 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.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -20,67 +25,88 @@ public class SNMPCrawler extends Crawler {
@Override @Override
public Object getInternal(Config config) { public Object getInternal(Config config) {
Object retObj = null; Object retObj = null;
// String targetVer = (String)config.get("version"); String targetVer = (String)config.getTarget().getAuth().get("version");
// switch (targetVer) { switch (targetVer) {
// case "v2c": case "v2c":
// retObj = getByV2c(config); retObj = getByV2c(config);
// break; break;
// case "v3": case "v3":
// retObj = getByV3(config); retObj = getByV3(config);
// break; break;
// default: default:
// new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace(); new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
// } }
return retObj; Map m = (Map<String, String>)retObj;
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;
} }
private Object getByV2c(Config config) { private Object getByV2c(Config config) {
Snmp snmp = null; Snmp snmp = null;
// TransportMapping<? extends Address> transport = null; TransportMapping<? extends Address> transport = 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); SNMPv2c snmpV2 = new SNMPv2c(snmp);
//
// String ip = (String)config.get("ip"); Map<String, Object> authInfo = config.getTarget().getAuth();
// String port = (String)config.get("port"); Connection connInfo = config.getTarget().getConnection();
// String community = (String)config.get("community"); List<Item> items = config.getItems();
// String[] oids = (String[])config.get("oids"); String[] oids = new String[items.get(0).getQueries().size()];
// String method = (String)config.get("method"); for (Item item : items) {
// List<Query> queries = item.getQueries();
// switch (method) { for (int i=0; i<queries.size();i++) {
// case "validate": oids[i] = queries.get(i).getQuery();
// return snmpV2.validate(ip, port, community); }
// case "get": }
// return snmpV2.get(ip, port, community, oids);
// case "walk": String ip = connInfo.getIp();
// return snmpV2.walk(ip, port, community, oids[0]); String port = connInfo.getPort();
// default: String community = (String)authInfo.get("community");
// new Exception("Unknown SNMP command : " + method).printStackTrace(); return snmpV2.get(ip, port, community, oids);
// }
// /*
// } catch (Exception e) { switch (method) {
// e.printStackTrace(); case "validate":
// } finally { return snmpV2.validate(ip, port, community);
// if (snmp != null) { case "get":
// try { return snmpV2.get(ip, port, community, oids);
// snmp.close(); case "walk":
// } catch (IOException e) { return snmpV2.walk(ip, port, community, oids[0]);
// e.printStackTrace(); default:
// } new Exception("Unknown SNMP command : " + method).printStackTrace();
// } }
// if (transport != null) { */
// try {
// transport.close(); } catch (Exception e) {
// } catch (IOException e) { e.printStackTrace();
// 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; return null;
} }
@ -88,51 +114,68 @@ public class SNMPCrawler extends Crawler {
Snmp snmp = null; Snmp snmp = null;
TransportMapping<? extends Address> transport = null; TransportMapping<? extends Address> transport = null;
// try { try {
// transport = new DefaultUdpTransportMapping(); transport = new DefaultUdpTransportMapping();
// snmp = new Snmp(transport); snmp = new Snmp(transport);
// transport.listen(); transport.listen();
// SNMPv3 snmpV3 = new SNMPv3(snmp); SNMPv3 snmpV3 = new SNMPv3(snmp);
//
// String ip = (String)config.get("ip");
// String port = (String)config.get("port"); Map<String, Object> authInfo = config.getTarget().getAuth();
// String user = (String)config.get("user"); Connection connInfo = config.getTarget().getConnection();
// String authType = (String)config.get("authType"); List<Item> items = config.getItems();
// String authPass = (String)config.get("authPass"); String[] oids = new String[items.get(0).getQueries().size()];
// String privType = (String)config.get("privType"); for (Item item : items) {
// String privPass = (String)config.get("privPass"); List<Query> queries = item.getQueries();
// String[] oids = (String[])config.get("oids"); for (int i=0; i<queries.size();i++) {
// String method = (String)config.get("method"); oids[i] = queries.get(i).getQuery();
// }
// switch (method) { }
// case "validate":
// return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass); String ip = connInfo.getIp();
// case "get": String port = connInfo.getPort();
// return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids); String method = "get";
// case "walk":
// return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]); String user = (String)authInfo.get("user");
// default: String authType = (String)authInfo.get("authType");
// new Exception("Unknown SNMP command : " + method).printStackTrace(); String authPass = (String)authInfo.get("authPass");
// } String privType = (String)authInfo.get("privType");
// String privPass = (String)authInfo.get("privPass");
// } catch (Exception e) {
// new Exception("An Exception happend : " + e.getMessage()).printStackTrace(); return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
// } finally {
// if (snmp != null) { /*
// try { switch (method) {
// snmp.close(); case "validate":
// } catch (IOException e) { return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass);
// e.printStackTrace(); case "get":
// } return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
// } case "walk":
// if (transport != null) { return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]);
// try { default:
// transport.close(); new Exception("Unknown SNMP command : " + method).printStackTrace();
// } catch (IOException e) { }
// e.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;
} }
} }

View File

@ -1,14 +1,14 @@
package com.loafle; package com.loafle;
import static org.junit.Assert.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loafle.overflow.crawler.config.Config;
import com.loafle.overflow.crawler.snmp.SNMPCrawler; import com.loafle.overflow.crawler.snmp.SNMPCrawler;
import com.sun.xml.internal.ws.api.ResourceLoader;
import org.junit.Test; import org.junit.Test;
import java.util.HashMap; import java.io.File;
import java.util.Map; import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class AppTest { public class AppTest {
@ -24,41 +24,27 @@ public class AppTest {
* loafle3 MD5 "qwer5795" : AUTHNOPRIV * loafle3 MD5 "qwer5795" : AUTHNOPRIV
* loafle4 :NOAUTHNOPRIV * loafle4 :NOAUTHNOPRIV
*/ */
@Test @Test
public void testSNMP() { public void testSNMP() throws Exception {
SNMPCrawler c = new SNMPCrawler(); SNMPCrawler c = new SNMPCrawler();
Map config = new HashMap(); ObjectMapper mapper = new ObjectMapper();
String p = ResourceLoader.class.getClass().getResource("/config/examplev3.json").getPath();
Config config = mapper.readValue(new File(p),Config.class);
config.put("ip", "192.168.1.215"); Object result = c.getInternal(config);
config.put("port", "161");
// config.put("version", "v2c"); if(result instanceof Boolean) {
// config.put("community", "public"); System.out.println("validate : " + result);
// config.put("method", "get"); }else {
// config.put("oids", new String[]{"1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.2.2.1.6.2"}); printResult((Map<String, String>)result);
}
config.put("version", "v3");
config.put("user", "loafle");
config.put("authType", "MD5");
config.put("authPass", "qwer5795");
config.put("privType", "DES");
config.put("privPass", "qweqwe123");
config.put("method", "walk");
config.put("oids", new String[]{"1.3.6.1.2.1.25.2.3.1.3"});
// Object result = c.getInternal(config);
//
// if(result instanceof Boolean) {
// System.out.println("validate : " + result);
// }else {
// printResult((Map<String, String>)result);
// }
} }
private void printResult(Map<String, String> result) { private void printResult(Map<String, String> result) {
for (Map.Entry<String, String> entry : result.entrySet()) { for (Map.Entry<String, String> entry : result.entrySet()) {
System.out.printf("oid: %s - value: %s\n", entry.getKey(), entry.getValue()); System.out.printf(" %s - %s\n", entry.getKey(), entry.getValue());
} }
} }

View File

@ -0,0 +1,58 @@
{
"id" : "insanity",
"target" : {
"connection" : {
"ip" : "192.168.1.215",
"port" : "161",
"ssl" : false,
"portType" : "udp"
},
"auth" : {
"community" : "public",
"version" : "v2c"
}
},
"schedule" : {
"interval" : "10"
},
"crawler" : {
"name":"snmp_crawler",
"container":"java"
},
"items" : [
{
"metrics" : [
"mem.total",
"mem.free",
"mem.shared",
"mem.buffered",
"mem.cached",
"mem.totalFree",
"swap.avaliable"
],
"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"
}
]
}
]
}

View File

@ -0,0 +1,42 @@
{
"id" : "insanity",
"target" : {
"connection" : {
"ip" : "192.168.1.215",
"port" : "161",
"ssl" : false,
"portType" : "udp"
},
"auth" : {
"version" : "v3",
"user" : "loafle",
"authType" : "MD5",
"authPass" : "qwer5795",
"privType" : "DES",
"privPass" : "qweqwe123"
}
},
"schedule" : {
"interval" : "10"
},
"crawler" : {
"name":"snmp_crawler",
"container":"java"
},
"items" : [
{
"metrics" : [
"nic[0].inoctets",
"nic[1].inoctets"
],
"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"
}
]
}
]
}