diff --git a/pom.xml b/pom.xml index 8da6d50..981e60f 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,6 @@ 1.0.0-SNAPSHOT com.loafle.overflow.crawler_redis - 2.9.0 diff --git a/src/main/java/com/loafle/overflow/crawler/redis/App.java b/src/main/java/com/loafle/overflow/crawler/redis/App.java deleted file mode 100644 index 851f8e4..0000000 --- a/src/main/java/com/loafle/overflow/crawler/redis/App.java +++ /dev/null @@ -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 r = redisCralwer.collectMetric("192.168.1.215", (short) 6379, "","ALL"); - } -} \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/crawler/redis/RedisCralwer.java b/src/main/java/com/loafle/overflow/crawler/redis/RedisCralwer.java index b25a97c..e9ca118 100644 --- a/src/main/java/com/loafle/overflow/crawler/redis/RedisCralwer.java +++ b/src/main/java/com/loafle/overflow/crawler/redis/RedisCralwer.java @@ -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 re = collectMetric("192.168.1.215", (short) 6379, "","ALL"); - - return re; + return collectMetric(c); } - protected Map 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 collectMetric(Config c) { - // 파싱 - Map 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 items = c.getItems(); + Map 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 metrics = item.getMetrics(); + + for (Query query : item.getQueries()) { + + String info = jedis.info(query.getQuery()); + Map resultMap = (Map) 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 parseToMap(String source) { + public Map parseToMap(String source) { String[] categorys = source.split("\r\n\r\n"); Map result = new HashMap(); for (String category : categorys) { - if (category.length() == 0) { - continue; - } + + if (category.length() == 0) {continue;} Map sm = new HashMap(); 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; diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..b2974db --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,17 @@ + + + crawler_java + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/AppTest.java b/src/test/java/com/loafle/overflow/AppTest.java deleted file mode 100644 index 6208f31..0000000 --- a/src/test/java/com/loafle/overflow/AppTest.java +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/crawler/redis/RedisCralwerTest.java b/src/test/java/com/loafle/overflow/crawler/redis/RedisCralwerTest.java new file mode 100644 index 0000000..ed9344b --- /dev/null +++ b/src/test/java/com/loafle/overflow/crawler/redis/RedisCralwerTest.java @@ -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 data = (Map) rc.getInternal(c); + + assertEquals(data.size(),7); + + + } +} \ No newline at end of file diff --git a/src/test/resources/config/example.json b/src/test/resources/config/example.json new file mode 100644 index 0000000..ef8207c --- /dev/null +++ b/src/test/resources/config/example.json @@ -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" + ] + } + ] + } + ] +} \ No newline at end of file