config metric test
This commit is contained in:
parent
615345cac6
commit
f0b9602f6d
1
pom.xml
1
pom.xml
|
@ -16,7 +16,6 @@
|
|||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>com.loafle.overflow.crawler_redis</name>
|
||||
|
||||
|
||||
<properties>
|
||||
<redis.client.version>2.9.0</redis.client.version>
|
||||
</properties>
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -2,10 +2,14 @@ package com.loafle.overflow.crawler.redis;
|
|||
|
||||
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 redis.clients.jedis.Jedis;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
@ -13,7 +17,6 @@ import java.util.logging.Logger;
|
|||
* Created by geek on 2017-04-12.
|
||||
*/
|
||||
public class RedisCralwer extends Crawler {
|
||||
private Jedis jedis;
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(RedisCralwer.class.getName());
|
||||
|
||||
|
@ -24,58 +27,76 @@ public class RedisCralwer extends Crawler {
|
|||
@Override
|
||||
public Object getInternal(Config c) throws Exception {
|
||||
LOGGER.log(Level.INFO ,"getInternal call");
|
||||
//접속
|
||||
|
||||
Map<String, Object> re = collectMetric("192.168.1.215", (short) 6379, "","ALL");
|
||||
|
||||
return re;
|
||||
return collectMetric(c);
|
||||
}
|
||||
|
||||
protected Map<String, Object> collectMetric(String ip, short port, String authpw ,String category ) {
|
||||
Jedis jedis = new Jedis(ip, port,false);
|
||||
String t = jedis.set("AUTH", authpw);
|
||||
String dd = jedis.info(category);
|
||||
protected Map<String, Object> collectMetric(Config c) {
|
||||
|
||||
// 파싱
|
||||
Map<String, Object> re = parseToMap(dd);
|
||||
String targetIP = c.getTarget().getConnection().getIp();
|
||||
int targetPort = Integer.valueOf(c.getTarget().getConnection().getPort());
|
||||
|
||||
System.out.println("re : "+re);
|
||||
//연결 해제
|
||||
jedis.close();
|
||||
//데이터 출력
|
||||
return re;
|
||||
String authpw = (String)c.getTarget().getAuth().get("authpw");
|
||||
|
||||
Jedis jedis = null;
|
||||
List<Item> items = c.getItems();
|
||||
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");
|
||||
Map<String, Object> result = new HashMap<String, Object>();
|
||||
|
||||
for (String category : categorys) {
|
||||
if (category.length() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (category.length() == 0) {continue;}
|
||||
Map<String, String> sm = new HashMap<String,String>();
|
||||
String[] lines = category.split("\r\n");
|
||||
|
||||
int idx = 0;
|
||||
for (String line : lines) {
|
||||
if (idx == 0) {
|
||||
idx++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.length() == 0) {
|
||||
continue;
|
||||
}
|
||||
if (idx == 0) {idx++;continue;}
|
||||
if (line.length() == 0) {continue;}
|
||||
|
||||
String[] items = line.split(":");
|
||||
|
||||
if (items.length != 0&& items.length > 1) {
|
||||
if (items.length != 0 && items.length > 1) {
|
||||
sm.put(items[0], items[1]);
|
||||
}
|
||||
}
|
||||
String[] ca = lines[0].split(" ");
|
||||
|
||||
result.put(ca[1], sm);
|
||||
}
|
||||
return result;
|
||||
|
|
17
src/main/resources/logback.xml
Normal file
17
src/main/resources/logback.xml
Normal 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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
||||
}
|
||||
}
|
59
src/test/resources/config/example.json
Normal file
59
src/test/resources/config/example.json
Normal 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"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user