|
| 1 | +package net.thekingofduck; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.sql.*; |
| 5 | +import java.text.SimpleDateFormat; |
| 6 | +import java.util.Date; |
| 7 | +import org.apache.commons.cli.*; |
| 8 | + |
| 9 | +/** |
| 10 | + * Project: MySQLMonitor |
| 11 | + * Date:2020/10/8 8:58 下午 |
| 12 | + * Email:CoolCat@gzsec.org |
| 13 | + * Github:https://github.com/TheKingOfDuck |
| 14 | + * |
| 15 | + * @author CoolCat |
| 16 | + * @version 1.0.0 |
| 17 | + */ |
| 18 | +public class MySQLMonitor { |
| 19 | + |
| 20 | + public static String ftime() { |
| 21 | + SimpleDateFormat ftime = new SimpleDateFormat("HH:mm:ss"); |
| 22 | + return ftime.format(new Date()); |
| 23 | + } |
| 24 | + |
| 25 | + public static void banner() { |
| 26 | + String banner = "\n" + |
| 27 | + " __ __ _____ ____ _ __ __ _ _ \n" + |
| 28 | + "| \\/ | / ____|/ __ \\| | | \\/ | (_) | \n" + |
| 29 | + "| \\ / |_ _| (___ | | | | | | \\ / | ___v1.0_ __ _| |_ ___ _ __ \n" + |
| 30 | + "| |\\/| | | | |\\___ \\| | | | | | |\\/| |/ _ \\| '_ \\| | __/ _ \\| '__|\n" + |
| 31 | + "| | | | |_| |____) | |__| | |____| | | | (_) | | | | | || (_) | | \n" + |
| 32 | + "|_| |_|\\__, |_____/ \\___\\_\\______|_| |_|\\___/|_| |_|_|\\__\\___/|_| \n" + |
| 33 | + " __/ | https://github.com/TheKingOfDuck/MySQLMonitor \n" + |
| 34 | + " |___/ "; |
| 35 | + System.out.println(banner); |
| 36 | + } |
| 37 | + |
| 38 | + public static void main(String[] args) throws ClassNotFoundException, ParseException { |
| 39 | + banner(); |
| 40 | + |
| 41 | + CommandLineParser parser = new BasicParser(); |
| 42 | + Options options = new Options(); |
| 43 | + |
| 44 | + options.addOption("h", "host", true, "mysql host"); |
| 45 | + |
| 46 | + options.addOption("p", "port", true, "mysql port"); |
| 47 | + |
| 48 | + options.addOption("user", "username", true, "mysql username"); |
| 49 | + |
| 50 | + options.addOption("pass", "password", true, "mysql password"); |
| 51 | + |
| 52 | + options.addOption("help", "help", false, "Help Info"); |
| 53 | + |
| 54 | + CommandLine commandLine = parser.parse(options, args); |
| 55 | + |
| 56 | + String helpinfo = String.format("[?]CommandLine:\n" + |
| 57 | + "\t-h\t--host\t\tmysql host\n" + |
| 58 | + "\t-p\t--port\t\tmysql port\n" + |
| 59 | + "\t-user\t--username\tmysql username\n" + |
| 60 | + "\t-pass\t--password\tmysql password\n" + |
| 61 | + "\t-help\t--help\t\thelp info\n\n" + |
| 62 | + "eg:java -jar MySQLMonitor.jar -h 127.0.0.1 -user CoolCat -pass mysqlmonitor"); |
| 63 | + if (commandLine.hasOption("help")) { |
| 64 | + System.out.println(helpinfo); |
| 65 | + System.exit(0); |
| 66 | + } |
| 67 | + if (args.length < 3){ |
| 68 | + System.out.println(helpinfo); |
| 69 | + System.exit(0); |
| 70 | + } |
| 71 | + |
| 72 | + String dbhost = "127.0.0.1"; |
| 73 | + String dbport = "3306"; |
| 74 | + String dbuser = "root"; |
| 75 | + String dbpass = "root"; |
| 76 | + |
| 77 | + if (commandLine.hasOption("h")) { |
| 78 | + dbhost = commandLine.getOptionValue("h"); |
| 79 | + } |
| 80 | + if (commandLine.hasOption("p")) { |
| 81 | + dbport = commandLine.getOptionValue("p"); |
| 82 | + } |
| 83 | + if (commandLine.hasOption("user")) { |
| 84 | + dbuser = commandLine.getOptionValue("user"); |
| 85 | + } |
| 86 | + if (commandLine.hasOption("pass")) { |
| 87 | + dbpass = commandLine.getOptionValue("pass"); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + String JDBC_DRIVER = null; |
| 92 | + String DB_URL = null; |
| 93 | + |
| 94 | + // 注册 JDBC 驱动 |
| 95 | + try { |
| 96 | + // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL |
| 97 | + JDBC_DRIVER = "com.mysql.jdbc.Driver"; |
| 98 | + DB_URL = String.format("jdbc:mysql://%s:%s/mysql",dbhost,dbport); |
| 99 | + Class.forName(JDBC_DRIVER); |
| 100 | + }catch (Exception e){ |
| 101 | + // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL |
| 102 | + JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; |
| 103 | + DB_URL = String.format("jdbc:mysql://%s:%s/mysql?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC",dbhost,dbport); |
| 104 | + Class.forName(JDBC_DRIVER); |
| 105 | + } |
| 106 | + |
| 107 | + Connection conn = null; |
| 108 | + Statement stmt = null; |
| 109 | + |
| 110 | + |
| 111 | + try{ |
| 112 | + |
| 113 | + // 打开链接 |
| 114 | + System.out.println(String.format("[%s] %s",ftime(),"Try connect to mysql...")); |
| 115 | + conn = DriverManager.getConnection(DB_URL,dbuser,dbpass); |
| 116 | + |
| 117 | + // 执行查询 |
| 118 | + stmt = conn.createStatement(); |
| 119 | + DatabaseMetaData dbinfo = conn.getMetaData(); |
| 120 | + |
| 121 | + System.out.println(String.format("[%s] Database version: %s",ftime(),dbinfo.getDatabaseProductVersion())); |
| 122 | + stmt.executeQuery("set global general_log='ON'"); |
| 123 | + |
| 124 | + |
| 125 | + ResultSet r = stmt.executeQuery("show variables like 'log_output'"); |
| 126 | + |
| 127 | + String log_output = null; |
| 128 | + while (r.next()){ |
| 129 | + log_output = r.getString("Value"); |
| 130 | + } |
| 131 | + System.out.println(String.format("[%s] Log output: %s",ftime(),log_output)); |
| 132 | + if (!log_output.equals("TABLE")){ |
| 133 | + System.out.println(String.format("[%s] Set global log_output='table'",ftime())); |
| 134 | + stmt.executeQuery("set global log_output='table'"); |
| 135 | + } |
| 136 | + r.close(); |
| 137 | + stmt.close(); |
| 138 | + |
| 139 | + } catch(Exception se){ |
| 140 | + // 处理 JDBC 错误 |
| 141 | + se.printStackTrace(); |
| 142 | + }// 处理 Class.forName 错误 |
| 143 | + finally{ |
| 144 | + // 关闭资源 |
| 145 | + try{ |
| 146 | + if(stmt!=null) stmt.close(); |
| 147 | + }catch(SQLException se2){ |
| 148 | + se2.printStackTrace(); |
| 149 | + } |
| 150 | + try{ |
| 151 | + if(conn!=null) conn.close(); |
| 152 | + }catch(SQLException se){ |
| 153 | + se.printStackTrace(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + try { |
| 158 | + Connection conn2 = null; |
| 159 | + Statement stmt2 = null; |
| 160 | + |
| 161 | + conn2 = DriverManager.getConnection(DB_URL,dbuser,dbpass); |
| 162 | + |
| 163 | + // 执行查询 |
| 164 | + stmt2 = conn2.createStatement(); |
| 165 | + |
| 166 | + while (true){ |
| 167 | + |
| 168 | + String logsql = "select * from mysql.general_log where command_type =\"Query\" OR command_type =\"Execute\" order by event_time desc limit 2"; |
| 169 | + |
| 170 | + ResultSet log = stmt2.executeQuery(logsql); |
| 171 | + while (log.next()){ |
| 172 | + String logres = log.getString("argument"); |
| 173 | + if (!logres.equals(logsql)){ |
| 174 | + System.out.println(String.format("[%s] %s",ftime(),logres)); |
| 175 | + } |
| 176 | + //不适当休眠一下会疯狂查询 占用cpu资源。 |
| 177 | + Thread.sleep(100); |
| 178 | + } |
| 179 | + } |
| 180 | + }catch (Exception e){ |
| 181 | + e.printStackTrace(); |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | +} |
0 commit comments