config metric test

This commit is contained in:
jackdaw@loafle.com 2017-04-21 18:24:01 +09:00
parent 615345cac6
commit f0b9602f6d
7 changed files with 162 additions and 53 deletions

View File

@ -16,7 +16,6 @@
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<name>com.loafle.overflow.crawler_redis</name> <name>com.loafle.overflow.crawler_redis</name>
<properties> <properties>
<redis.client.version>2.9.0</redis.client.version> <redis.client.version>2.9.0</redis.client.version>
</properties> </properties>

View File

@ -1,10 +0,0 @@
package com.loafle.overflow.crawler.redis;
import java.util.Map;
public class App {
public static void main(String[] args) {
RedisCralwer redisCralwer = new RedisCralwer();
Map<String, Object> r = redisCralwer.collectMetric("192.168.1.215", (short) 6379, "","ALL");
}
}

View File

@ -2,10 +2,14 @@ package com.loafle.overflow.crawler.redis;
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 redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -13,7 +17,6 @@ import java.util.logging.Logger;
* Created by geek on 2017-04-12. * Created by geek on 2017-04-12.
*/ */
public class RedisCralwer extends Crawler { public class RedisCralwer extends Crawler {
private Jedis jedis;
private static final Logger LOGGER = Logger.getLogger(RedisCralwer.class.getName()); private static final Logger LOGGER = Logger.getLogger(RedisCralwer.class.getName());
@ -24,58 +27,76 @@ public class RedisCralwer extends Crawler {
@Override @Override
public Object getInternal(Config c) throws Exception { public Object getInternal(Config c) throws Exception {
LOGGER.log(Level.INFO ,"getInternal call"); LOGGER.log(Level.INFO ,"getInternal call");
//접속 return collectMetric(c);
Map<String, Object> re = collectMetric("192.168.1.215", (short) 6379, "","ALL");
return re;
} }
protected Map<String, Object> collectMetric(String ip, short port, String authpw ,String category ) { protected Map<String, Object> collectMetric(Config c) {
Jedis jedis = new Jedis(ip, port,false);
String t = jedis.set("AUTH", authpw);
String dd = jedis.info(category);
// 파싱 String targetIP = c.getTarget().getConnection().getIp();
Map<String, Object> re = parseToMap(dd); int targetPort = Integer.valueOf(c.getTarget().getConnection().getPort());
System.out.println("re : "+re); String authpw = (String)c.getTarget().getAuth().get("authpw");
//연결 해제
jedis.close(); Jedis jedis = null;
//데이터 출력 List<Item> items = c.getItems();
return re; Map<String,Object> returnMap = new HashMap<>();
try {
jedis = new Jedis(targetIP, targetPort,false);
if (authpw != null && !authpw.equals("")) {
String code = jedis.auth(authpw);
if (!code.equals("")) {
// set error auth
}
}
for (Item item : items) {
List<String> metrics = item.getMetrics();
for (Query query : item.getQueries()) {
String info = jedis.info(query.getQuery());
Map<String, String> resultMap = (Map<String, String>) parseToMap(info).get(query.getQuery());
for (int index =0 ; index < query.getKeys().size() ; ++index) {
returnMap.put(metrics.get(index),resultMap.get(query.getKeys().get(index)));
}
}
}
} catch (Exception e) {
throw e;
} finally {
if (jedis != null) {
jedis.close();
}
}
return returnMap;
} }
private Map<String, Object> parseToMap(String source) { public Map<String, Object> parseToMap(String source) {
String[] categorys = source.split("\r\n\r\n"); String[] categorys = source.split("\r\n\r\n");
Map<String, Object> result = new HashMap<String, Object>(); Map<String, Object> result = new HashMap<String, Object>();
for (String category : categorys) { for (String category : categorys) {
if (category.length() == 0) {
continue; if (category.length() == 0) {continue;}
}
Map<String, String> sm = new HashMap<String,String>(); Map<String, String> sm = new HashMap<String,String>();
String[] lines = category.split("\r\n"); String[] lines = category.split("\r\n");
int idx = 0; int idx = 0;
for (String line : lines) { for (String line : lines) {
if (idx == 0) {
idx++;
continue;
}
if (line.length() == 0) { if (idx == 0) {idx++;continue;}
continue; if (line.length() == 0) {continue;}
}
String[] items = line.split(":"); String[] items = line.split(":");
if (items.length != 0 && items.length > 1) {
if (items.length != 0&& items.length > 1) {
sm.put(items[0], items[1]); sm.put(items[0], items[1]);
} }
} }
String[] ca = lines[0].split(" "); String[] ca = lines[0].split(" ");
result.put(ca[1], sm); result.put(ca[1], sm);
} }
return result; return result;

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="3 seconds">
<contextName>crawler_java</contextName>
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.loafle" level="ALL" />
</configuration>

View File

@ -1,11 +0,0 @@
package com.loafle.overflow;
import static org.junit.Assert.*;
import org.junit.Test;
public class AppTest {
@Test
public void testSum() {
assertEquals(1,1);
}
}

View File

@ -0,0 +1,34 @@
package com.loafle.overflow.crawler.redis;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loafle.overflow.crawler.config.Config;
import org.junit.Ignore;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.io.File;
import java.util.Map;
import static org.junit.Assert.*;
/**
* Created by root on 17. 4. 21.
*/
public class RedisCralwerTest {
@Test
@Ignore
public void collectMetric() throws Exception {
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);
RedisCralwer rc = new RedisCralwer();
Map<String,Object> data = (Map<String, Object>) rc.getInternal(c);
assertEquals(data.size(),7);
}
}

View File

@ -0,0 +1,59 @@
{
"id" : "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734",
"target" : {
"connection" : {
"ip" : "192.168.1.104",
"port" : "6379",
"ssl" : false,
"portType" : "tcp"
},
"auth" : {
}
},
"schedule" : {
"interval" : "10"
},
"crawler" : {
"name":"redis_protocol_crawler",
"container":"network_crawler"
},
"items" : [
{
"metrics" : [
"cpu.usage.system",
"cpu.usage.user",
"cpu.usage.system_children",
"cpu.usage.user_children"
],
"queries":[
{
"query" : "CPU" ,
"keys" : [
"used_cpu_sys",
"used_cpu_user",
"used_cpu_sys_children",
"used_cpu_user_children"
]
}
]
},
{
"metrics" : [
"memory.usage.used",
"memory.usage.rss",
"memory.usage.reak"
],
"queries":[
{
"query" : "Memory" ,
"keys" : [
"used_memory",
"used_memory_rss",
"used_memory_peak"
]
}
]
}
]
}