config metric test
This commit is contained in:
parent
84b0299dd6
commit
9f2b8adec9
1
pom.xml
1
pom.xml
@ -16,6 +16,7 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>com.loafle.overflow.crawler_mongo</name>
|
||||
|
||||
|
||||
<properties>
|
||||
<mogodb.client.version>3.4.2</mogodb.client.version>
|
||||
</properties>
|
||||
|
@ -2,48 +2,72 @@ package com.loafle.overflow.crawler.mongo;
|
||||
|
||||
import com.loafle.overflow.crawler.Crawler;
|
||||
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.MongoClientOptions;
|
||||
import com.mongodb.ServerAddress;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
|
||||
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 {
|
||||
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());
|
||||
result = new HashMap<String, Object>();
|
||||
mongoClient = new MongoClient(new ServerAddress(targetIP, targetPort),
|
||||
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()){
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}finally {
|
||||
if (mb != null) {
|
||||
mb.close();
|
||||
if (mongoClient != null) {
|
||||
mongoClient.close();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> testColect(String ip, String port, String dbName) {
|
||||
return this.collectMetric(ip,port,dbName);
|
||||
return returnMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getInternal(Config config) throws Exception {
|
||||
//Map<String ,Object> re = collectMetric((String) map.get("ip"), (String)map.get("port"), (String)map.get("dbName"));
|
||||
//System.out.println("re = " + re);
|
||||
//return re;
|
||||
return null;
|
||||
return this.collectMetric(config);
|
||||
}
|
||||
}
|
@ -2,20 +2,37 @@
|
||||
package com.loafle.overflow.crawler;
|
||||
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.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.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MongoCrawlerTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void TestCollectMetric() {
|
||||
MongoCrawler cr = new MongoCrawler();
|
||||
Map<String,Object> m = cr.testColect("192.168.1.104", "27017", "admin");
|
||||
public void TestCollectMetric() throws Exception {
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
46
src/test/resources/config/example.json
Normal file
46
src/test/resources/config/example.json
Normal 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"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user