From ed726f368bf182861331191829719aab5a9b0ca5 Mon Sep 17 00:00:00 2001 From: "jackdaw@loafle.com" Date: Tue, 25 Apr 2017 19:47:42 +0900 Subject: [PATCH] =?UTF-8?q?ResultSet=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 - .../overflow/crawler/sql/SQLCrawler.java | 87 ++++++++----------- .../java/com/loafle/overflow/AppTest.java | 71 +++++++++++---- src/test/resources/config/mysql.json | 44 ++++++++++ src/test/resources/config/pgsql.json | 39 +++++++++ .../config/sqlserver_connection_count.json | 39 +++++++++ .../config/sqlserver_multiple_key_array.json | 45 ++++++++++ 7 files changed, 257 insertions(+), 70 deletions(-) create mode 100644 src/test/resources/config/mysql.json create mode 100644 src/test/resources/config/pgsql.json create mode 100644 src/test/resources/config/sqlserver_connection_count.json create mode 100644 src/test/resources/config/sqlserver_multiple_key_array.json diff --git a/pom.xml b/pom.xml index 88e33c5..6f90a1f 100644 --- a/pom.xml +++ b/pom.xml @@ -42,13 +42,11 @@ system ${basedir}/src/lib/ojdbc7.jar - com.loafle.overflow crawler 1.0.0-SNAPSHOT - \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/crawler/sql/SQLCrawler.java b/src/main/java/com/loafle/overflow/crawler/sql/SQLCrawler.java index def016e..d9afba4 100644 --- a/src/main/java/com/loafle/overflow/crawler/sql/SQLCrawler.java +++ b/src/main/java/com/loafle/overflow/crawler/sql/SQLCrawler.java @@ -1,13 +1,15 @@ package com.loafle.overflow.crawler.sql; + 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.loafle.overflow.crawler.result.*; import java.sql.*; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.sql.ResultSet; +import java.util.*; /** * Created by insanity on 17. 4. 11. @@ -27,66 +29,47 @@ public class SQLCrawler extends Crawler { Connection conn = null; Statement stmt = null; ResultSet rs = null; - try { -// conn = DriverManager.getConnection( -// (String)config.get("url"), -// (String)config.get("user"), -// (String)config.get("pw")); + String url = (String)config.getTarget().getAuth().get("url"); + String id = (String)config.getTarget().getAuth().get("id"); + String pw = (String)config.getTarget().getAuth().get("pw"); + + List resultSets = new ArrayList<>(); + + try { + conn = DriverManager.getConnection(url, id, pw); stmt = conn.createStatement(); -// rs = stmt.executeQuery((String)config.get("query")); + for (Item item : config.getItems()) { - ResultSetMetaData md = rs.getMetaData(); - int cnt = md.getColumnCount(); + Query query = item.getQuery(); + OFResultSet resultSet = OFResultSetRow.newInstance(item); + Map meta = resultSet.getMeta(); - List metaList = new ArrayList(); + rs = stmt.executeQuery((String)query.getQueryInfo().get("query")); - for (int i =0 ; i < cnt ; ++i) { - DBMetaInfo o = new DBMetaInfo(); - o.setName(md.getColumnName(i+1)); - metaList.add(o); - } - - List> datas = new ArrayList>(); - while(rs.next()){ - Map row = new HashMap(); - for (DBMetaInfo o : metaList) { - row.put(o.getName(), rs.getString(o.getName())); + while(rs.next()) { + List row = new ArrayList<>(Arrays.asList(new String[meta.size()])); + for (Map.Entry info : meta.entrySet()) { + String data = rs.getString(info.getKey()); + data = data.trim(); + row.set(info.getValue().intValue(),data); + } + resultSet.addRow(row); } - datas.add(row); - } - return datas; + resultSets.add(resultSet); + rs.close(); + } + return resultSets; } catch (Exception e) { + e.printStackTrace(); new Exception(e.getMessage()).printStackTrace(); - } finally { - if (conn != null) { - try { - conn.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - if (stmt != null) { - try { - stmt.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - + if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}} + if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}} + if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}} } return null; } diff --git a/src/test/java/com/loafle/overflow/AppTest.java b/src/test/java/com/loafle/overflow/AppTest.java index 4d164d0..a440cb0 100644 --- a/src/test/java/com/loafle/overflow/AppTest.java +++ b/src/test/java/com/loafle/overflow/AppTest.java @@ -1,16 +1,21 @@ package com.loafle.overflow; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.loafle.overflow.crawler.config.Config; +import com.loafle.overflow.crawler.result.OFResultSet; import com.loafle.overflow.crawler.sql.SQLCrawler; import org.junit.Ignore; import org.junit.Test; +import java.io.File; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; public class AppTest { -// //MySQL + // //MySQL // info.setUrl("jdbc:mysql://192.168.1.215:3306"); // ?useSSL=true&verifyServerCertificate=false // info.setId("root"); // info.setPw("qwe123"); @@ -34,23 +39,57 @@ public class AppTest { // info.setPw("qwer5795QWER"); // info.setSSL(false); // info.setQuery("select * from v$sysstat"); + private Config getConfig(String path) throws IOException { + ClassLoader classLoader = getClass().getClassLoader(); + String filePath = classLoader.getResource(path).getFile(); + + ObjectMapper mapper = new ObjectMapper(); + Config c = mapper.readValue(new File(filePath),Config.class); + return c; + } + + private void print(String s, List mm) { + + System.out.println(s); + + Map m = new HashMap<>(); + for(OFResultSet r : mm) { + m.putAll(r.getData()); + } + + for (Map.Entry item : m.entrySet()) { + System.out.println("key=" + item.getKey() + " ||||||||||| value=" + item.getValue()); + } + } @Test @Ignore - public void testSQL() { -// SQLCrawler sc = new SQLCrawler(); -// Map config = new HashMap(); -// config.put("url", "jdbc:mysql://192.168.1.215:3306"); -// config.put("user", "root"); -// config.put("pw", "qwe123"); -// config.put("ssl", false); -// config.put("query", "show session status"); -// -// Object result = sc.getInternal(config); -// for(Map m : (List>)result) { -// for (Map.Entry entry : m.entrySet()) { -// System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); -// } -// } + public void testMYSQL() throws IOException { + Object result = new SQLCrawler().getInternal( getConfig("config/mysql.json")); + print("Mysql row", (List) result); + } + + @Test + @Ignore + public void testPGSQL() throws IOException { + Object result = new SQLCrawler().getInternal(getConfig("config/pgsql.json")); + print("PGSql", (List) result); + } + + @Test + @Ignore + public void testSQLServerConnection_Count() throws IOException { + Object result = new SQLCrawler().getInternal(getConfig("config/sqlserver_connection_count.json")); + print("MSSql Server", (List) result); + + } + + + + @Test + @Ignore + public void testSQLServerMultiKeyArray() throws IOException { + Object result = new SQLCrawler().getInternal(getConfig("config/sqlserver_multiple_key_array.json")); + print("MSSql Server MuoltiArray", (List< OFResultSet>) result); } } \ No newline at end of file diff --git a/src/test/resources/config/mysql.json b/src/test/resources/config/mysql.json new file mode 100644 index 0000000..f1ff962 --- /dev/null +++ b/src/test/resources/config/mysql.json @@ -0,0 +1,44 @@ +{ + "id" : "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734", + "target" : { + "connection" : { + "ip" : "192.168.1.104", + "port" : "6379", + "ssl" : false, + "portType" : "tcp" + }, + "auth" : { + "url":"jdbc:mysql://192.168.1.215:3306", + "id":"root", + "pw":"qwe123" + } + }, + "schedule" : { + "interval" : "10" + }, + "crawler" : { + "name":"mysql", + "container":"java_proxy" + }, + "items" : [ + { + "metrics" : [ + "net.connection_count" + ], + "query": + { + "queryInfo" : { + "query":"show status where `variable_name` = 'Connections'", + "parseDirection" : "row", + "valueColumn" : "Value", + "keyColumns" : [ + "Variable_name" + ] + }, + "keys" : [ + "Connections" + ] + } + } + ] +} \ No newline at end of file diff --git a/src/test/resources/config/pgsql.json b/src/test/resources/config/pgsql.json new file mode 100644 index 0000000..8f98c9d --- /dev/null +++ b/src/test/resources/config/pgsql.json @@ -0,0 +1,39 @@ +{ + "id" : "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734", + "target" : { + "connection" : { + "ip" : "192.168.1.107", + "port" : "5432", + "ssl" : false, + "portType" : "tcp" + }, + "auth" : { + "url":"jdbc:postgresql://192.168.1.107:5432/postgres", + "id":"postgres", + "pw":"!@#$qwer1234" + } + }, + "schedule" : { + "interval" : "10" + }, + "crawler" : { + "name":"pgsql", + "container":"java_proxy" + }, + "items" : [ + { + "metrics" : [ + "net.connection_count" + ], + "query": { + "queryInfo":{ + "query" : "select count(pid) as connection_count from pg_catalog.pg_stat_activity where state <> 'idle'" , + "parseDirection":"col" + }, + "keys" : [ + "connection_count" + ] + } + } + ] +} \ No newline at end of file diff --git a/src/test/resources/config/sqlserver_connection_count.json b/src/test/resources/config/sqlserver_connection_count.json new file mode 100644 index 0000000..4205924 --- /dev/null +++ b/src/test/resources/config/sqlserver_connection_count.json @@ -0,0 +1,39 @@ +{ + "id" : "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734", + "target" : { + "connection" : { + "ip" : "192.168.1.103", + "port" : "1433", + "ssl" : false, + "portType" : "tcp" + }, + "auth" : { + "url":"jdbc:sqlserver://192.168.1.106:1433;", + "id":"sa", + "pw":"qwe123" + } + }, + "schedule" : { + "interval" : "10" + }, + "crawler" : { + "name":"mssql", + "container":"java_proxy" + }, + "items" : [ + { + "metrics" : [ + "net.connection_count" + ], + "query": { + "queryInfo" : { + "query": "select count(session_id) as connection_count from sys.dm_exec_connections where session_id = @@SPID", + "parseDirection" : "col" + }, + "keys" : [ + "connection_count" + ] + } + } + ] +} \ No newline at end of file diff --git a/src/test/resources/config/sqlserver_multiple_key_array.json b/src/test/resources/config/sqlserver_multiple_key_array.json new file mode 100644 index 0000000..f84d4d5 --- /dev/null +++ b/src/test/resources/config/sqlserver_multiple_key_array.json @@ -0,0 +1,45 @@ +{ + "id" : "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734", + "target" : { + "connection" : { + "ip" : "192.168.1.103", + "port" : "1433", + "ssl" : false, + "portType" : "tcp" + }, + "auth" : { + "url":"jdbc:sqlserver://192.168.1.106:1433;", + "id":"sa", + "pw":"qwe123", + "query" : "select * from master.dbo.sysprocesses" + } + }, + "schedule" : { + "interval" : "10" + }, + "crawler" : { + "name":"mssql", + "container":"java_proxy" + }, + "items" : [ + { + "metrics" : [ + "object[$0].db[$1].datafile_size", + "object[$0].db[$1].logfile_size" + ], + "query": { + "queryInfo" : { + "query": "select object_name,instance_name, counter_name, cntr_value from sys.dm_os_performance_counters where ( counter_name = 'Data File(s) Size (KB)' or counter_name = 'Log File(s) Size (KB)' ) AND object_name = 'SQLServer:Databases'", + "parseDirection" : "row", + "arrayColumns" : [ "object_name","instance_name"], + "keyColumns" : ["counter_name"], + "valueColumn" : "cntr_value" + }, + "keys" : [ + "Data File(s) Size (KB)", + "Log File(s) Size (KB)" + ] + } + } + ] +} \ No newline at end of file