config metric test

This commit is contained in:
jackdaw@loafle.com 2017-04-21 18:24:29 +09:00
parent 84b0299dd6
commit 9f2b8adec9
4 changed files with 114 additions and 26 deletions

View File

@ -16,6 +16,7 @@
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<name>com.loafle.overflow.crawler_mongo</name> <name>com.loafle.overflow.crawler_mongo</name>
<properties> <properties>
<mogodb.client.version>3.4.2</mogodb.client.version> <mogodb.client.version>3.4.2</mogodb.client.version>
</properties> </properties>

View File

@ -2,48 +2,72 @@ package com.loafle.overflow.crawler.mongo;
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.Item;
import com.loafle.overflow.crawler.config.Query;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import org.bson.Document; import org.bson.Document;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class MongoCrawler extends Crawler { public class MongoCrawler extends Crawler {
protected Map<String, Object> collectMetric(String ip, String port, String dbName) { public Map<String, Object> collectMetric(Config c) throws Exception {
MongoClient mongoClient = null;
Map<String, Object> returnMap = new HashMap<String, Object>();
String dataBaseName = "admin";
String statusCommand = "serverStatus";
String targetIP = c.getTarget().getConnection().getIp();
short targetPort = Short.parseShort(c.getTarget().getConnection().getPort());
MongoClient mb = null;
Map<String, Object> result = null;
try { try {
short p = Short.parseShort(port);
mb = new MongoClient(ip, p);
MongoDatabase database = mb.getDatabase(dbName);
Document serverStatus = database.runCommand(new Document("serverStatus", 1));
// System.out.println("serverStatus.size() = " + serverStatus.size()); mongoClient = new MongoClient(new ServerAddress(targetIP, targetPort),
result = new HashMap<String, Object>(); MongoClientOptions.builder()
.serverSelectionTimeout(1000)
.connectTimeout(1000)
.socketTimeout(1000).build()
);
MongoDatabase database = mongoClient.getDatabase(dataBaseName);
Document serverStatus = database.runCommand(new Document(statusCommand, 1));
Map<String,Object> resultMap = new HashMap<String, Object>();
for (Map.Entry<String, Object> set : serverStatus.entrySet()){ for (Map.Entry<String, Object> set : serverStatus.entrySet()){
result.put(set.getKey(), set.getValue()); resultMap.put(set.getKey(), set.getValue());
}
List<Item> items = c.getItems();
for (Item item : items) {
List<String> metrics = item.getMetrics();
for (Query query : item.getQueries()) {
Map<String, String> re = (Map<String, String>) resultMap.get(query.getQuery());
for (int index =0 ; index < query.getKeys().size() ; ++index) {
returnMap.put(metrics.get(index),re.get(query.getKeys().get(index)));
}
}
} }
}catch (Exception e) { }catch (Exception e) {
e.printStackTrace(); throw e;
}finally { }finally {
if (mb != null) { if (mongoClient != null) {
mb.close(); mongoClient.close();
} }
} }
return result; return returnMap;
}
public Map<String, Object> testColect(String ip, String port, String dbName) {
return this.collectMetric(ip,port,dbName);
} }
@Override @Override
public Object getInternal(Config config) throws Exception { public Object getInternal(Config config) throws Exception {
//Map<String ,Object> re = collectMetric((String) map.get("ip"), (String)map.get("port"), (String)map.get("dbName")); return this.collectMetric(config);
//System.out.println("re = " + re);
//return re;
return null;
} }
} }

View File

@ -2,20 +2,37 @@
package com.loafle.overflow.crawler; package com.loafle.overflow.crawler;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loafle.overflow.crawler.config.Config;
import com.loafle.overflow.crawler.mongo.MongoCrawler; import com.loafle.overflow.crawler.mongo.MongoCrawler;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class MongoCrawlerTest { public class MongoCrawlerTest {
@Test @Test
@Ignore @Ignore
public void TestCollectMetric() { public void TestCollectMetric() throws Exception {
MongoCrawler cr = new MongoCrawler();
Map<String,Object> m = cr.testColect("192.168.1.104", "27017", "admin");
assertEquals(23, m.size());
// read test resources config/example.json
ClassLoader classLoader = getClass().getClassLoader();
String path = classLoader.getResource("config/example.json").getFile();
ObjectMapper mapper = new ObjectMapper();
Config c = mapper.readValue(new File(path),Config.class);
MongoCrawler cr = new MongoCrawler();
Map<String,Object> m = cr.collectMetric(c);
assertEquals(6, m.size());
} }
} }

View File

@ -0,0 +1,46 @@
{
"id" : "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734",
"target" : {
"connection" : {
"ip" : "192.168.1.104",
"port" : "27017",
"ssl" : false,
"portType" : "tcp"
},
"auth" : {
}
},
"schedule" : {
"interval" : "10"
},
"crawler" : {
"name":"mongodb",
"container":"java_proxy"
},
"items" : [
{
"metrics" : [
"memory.usage.bits",
"memory.usage.rss",
"memory.usage.vmem",
"memory.usage.supported",
"memory.usage.mapped",
"memory.usage.mappedWithJournal"
],
"queries":[
{
"query" : "mem" ,
"keys" : [
"bits",
"resident",
"virtual",
"supported",
"mapped",
"mappedWithJournal"
]
}
]
}
]
}