├── .idea ├── .gitignore ├── artifacts │ └── formtest_jar.xml ├── misc.xml ├── modules.xml ├── uiDesigner.xml └── vcs.xml ├── README.md ├── error.jpg ├── formtest.iml ├── lib ├── commons-io-2.7.jar ├── druid-1.1.22.jar └── mysql-connector-java-8.0.15.jar ├── monitor.jpg ├── out └── production │ └── formtest │ ├── META-INF │ └── MANIFEST.MF │ └── com │ ├── intellij │ └── uiDesigner │ │ └── core │ │ ├── AbstractLayout.class │ │ ├── DimensionInfo.class │ │ ├── GridConstraints.class │ │ ├── GridLayoutManager.class │ │ ├── HorizontalInfo.class │ │ ├── LayoutState.class │ │ ├── Spacer.class │ │ ├── SupportCode$TextWithMnemonic.class │ │ ├── SupportCode.class │ │ ├── Util.class │ │ └── VerticalInfo.class │ └── wk │ ├── MySqlStParser.class │ ├── StatusColumnCellRenderer.class │ └── monitor.class └── src ├── META-INF └── MANIFEST.MF └── com └── wk ├── MainForm.form ├── MainForm.java ├── Monitor.java ├── MySqlStParser.java └── StatusColumnCellRenderer.java /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/artifacts/formtest_jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | $PROJECT_DIR$/out/artifacts/formtest_jar 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MysqlMonitor 2 | 重复造轮子不是问题,问题是要造的有价值。此工具主要用于安全工作者代码审计,网上现存的工具看了一圈没有对报错的SQL语句进行记录,这不正是代码审计时最需要的吗。 3 | >mysql 高版本 general_log 不会记录执行错误的SQL语句到日志,需要在配置文件中[mysqld]或者[mysqld_safe]设置log-raw=1 4 | 5 | ![avatar](https://github.com/J0hnWalker/MysqlMonitor/blob/master/monitor.jpg) 6 | ![avatar](https://github.com/J0hnWalker/MysqlMonitor/blob/master/error.jpg) 7 | -------------------------------------------------------------------------------- /error.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/error.jpg -------------------------------------------------------------------------------- /formtest.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /lib/commons-io-2.7.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/lib/commons-io-2.7.jar -------------------------------------------------------------------------------- /lib/druid-1.1.22.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/lib/druid-1.1.22.jar -------------------------------------------------------------------------------- /lib/mysql-connector-java-8.0.15.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/lib/mysql-connector-java-8.0.15.jar -------------------------------------------------------------------------------- /monitor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/monitor.jpg -------------------------------------------------------------------------------- /out/production/formtest/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: com.wk.MainForm 3 | 4 | -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/AbstractLayout.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/AbstractLayout.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/DimensionInfo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/DimensionInfo.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/GridConstraints.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/GridConstraints.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/GridLayoutManager.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/GridLayoutManager.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/HorizontalInfo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/HorizontalInfo.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/LayoutState.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/LayoutState.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/Spacer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/Spacer.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/SupportCode$TextWithMnemonic.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/SupportCode$TextWithMnemonic.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/SupportCode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/SupportCode.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/Util.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/Util.class -------------------------------------------------------------------------------- /out/production/formtest/com/intellij/uiDesigner/core/VerticalInfo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/intellij/uiDesigner/core/VerticalInfo.class -------------------------------------------------------------------------------- /out/production/formtest/com/wk/MySqlStParser.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/wk/MySqlStParser.class -------------------------------------------------------------------------------- /out/production/formtest/com/wk/StatusColumnCellRenderer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/wk/StatusColumnCellRenderer.class -------------------------------------------------------------------------------- /out/production/formtest/com/wk/monitor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/J0hnWalker/MysqlMonitor/86e030a66180a4449e53a41fa546ff970cf785f1/out/production/formtest/com/wk/monitor.class -------------------------------------------------------------------------------- /src/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Main-Class: com.wk.MainForm 3 | 4 | -------------------------------------------------------------------------------- /src/com/wk/MainForm.form: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 |
179 | -------------------------------------------------------------------------------- /src/com/wk/MainForm.java: -------------------------------------------------------------------------------- 1 | package com.wk; 2 | 3 | import javax.swing.*; 4 | import javax.swing.JScrollPane; 5 | import javax.swing.table.DefaultTableModel; 6 | import java.awt.event.ActionEvent; 7 | import java.awt.event.ActionListener; 8 | import java.sql.Connection; 9 | import java.sql.SQLException; 10 | 11 | public class MainForm { 12 | public static Monitor m; 13 | public static Connection conn; 14 | public static Thread t1; 15 | public static Connection getConn(){ 16 | return conn; 17 | } 18 | public static int cacheNum = 100; 19 | public static DefaultTableModel defaultModel = null; 20 | 21 | public MainForm() { 22 | button1.addActionListener(new ActionListener() { 23 | @Override 24 | public void actionPerformed(ActionEvent e) { 25 | t1 = new Thread() { 26 | public void run() { 27 | MainForm.m = new Monitor(); 28 | String username = usernameField.getText(); 29 | char[] pw = passwordField.getPassword(); 30 | String password = new String(pw); 31 | int port = Integer.valueOf(portField.getText()); 32 | String host = hostField.getText(); 33 | String dbname = dbField.getText(); 34 | MainForm.conn = MainForm.m.connectMysql(username, password, dbname, host, port, JP); 35 | if (MainForm.conn != null) { 36 | button1.setEnabled(false); 37 | MainForm.m.execSql(MainForm.conn, "set global general_log=on;"); 38 | String[] logStatus = MainForm.m.execSql(MainForm.conn, "show variables like 'general_log';").trim().split("\t"); 39 | while (!Thread.currentThread().isInterrupted()) { 40 | try { 41 | m.logMonitor(MainForm.m.logSwitch(MainForm.conn, bottomlabel), MainForm.cacheNum, table1, defaultModel); 42 | Thread.sleep(1000); 43 | } catch (InterruptedException e) { 44 | Thread.currentThread().interrupt(); 45 | } 46 | } 47 | } 48 | } 49 | }; 50 | t1.start(); 51 | } 52 | }); 53 | button2.addActionListener(new ActionListener() { 54 | @Override 55 | public void actionPerformed(ActionEvent e) { 56 | Connection cn = getConn(); 57 | if (cn != null) { 58 | try { 59 | MainForm.m.execSql(MainForm.conn, "set global general_log=off;"); 60 | cn.close(); 61 | t1.interrupt(); 62 | defaultModel.setRowCount(0); 63 | bottomlabel.setText("连接关闭"); 64 | } catch (SQLException ex) { 65 | ex.printStackTrace(); 66 | } 67 | } 68 | if (button2 == e.getSource()){ 69 | button1.setEnabled(true); 70 | } 71 | } 72 | }); 73 | String header[] = new String[] { "id", "requestText", "return", "requestType", "time"}; 74 | defaultModel = new DefaultTableModel(0, 0){ 75 | @Override 76 | public Class getColumnClass(int column){ 77 | Class returnValue; 78 | if (column == 0) 79 | { 80 | //returnValue = getValueAt(0, column).getClass(); 81 | returnValue = Integer.class; 82 | } 83 | else{ 84 | returnValue = String.class; 85 | } 86 | return returnValue; 87 | } 88 | }; 89 | defaultModel.setColumnIdentifiers(header); 90 | table1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 91 | table1.setModel(defaultModel); 92 | table1.getColumnModel().getColumn(0).setPreferredWidth(20); 93 | table1.getColumnModel().getColumn(1).setPreferredWidth(500); 94 | table1.getColumnModel().getColumn(2).setPreferredWidth(100); 95 | table1.getColumnModel().getColumn(3).setPreferredWidth(100); 96 | table1.getColumnModel().getColumn(4).setPreferredWidth(100); 97 | JScrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 98 | JScrollPane2.setViewportView(table1); 99 | comboBox1.addItem(100); 100 | comboBox1.addItem(200); 101 | comboBox1.addItem(500); 102 | comboBox1.addActionListener(new ActionListener() { 103 | @Override 104 | public void actionPerformed(ActionEvent e) { 105 | //System.out.println(comboBox1.getSelectedItem()); 106 | MainForm.cacheNum = (Integer) comboBox1.getSelectedItem(); 107 | } 108 | }); 109 | } 110 | 111 | public static void main(String[] args) { 112 | try { 113 | UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 114 | //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 115 | } catch (UnsupportedLookAndFeelException e) { 116 | } catch (ClassNotFoundException e) { 117 | } catch (InstantiationException e) { 118 | } catch (IllegalAccessException e) { 119 | } 120 | JFrame frame = new JFrame("MysqlLogMonitor"); 121 | frame.setSize(1000, 500); //窗体初始大小 122 | frame.setLocationRelativeTo(null); //居中显示 123 | frame.setContentPane(new MainForm().panelMain); 124 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 125 | frame.pack(); 126 | frame.setVisible(true); 127 | 128 | } 129 | 130 | 131 | private JButton button1; 132 | private JPanel panelMain; 133 | private JTextField usernameField; 134 | private JTextField portField; 135 | private JButton button2; 136 | private JPasswordField passwordField; 137 | private JPanel panel2; 138 | private JComboBox comboBox1; 139 | private JLabel bottomlabel; 140 | private JLabel hostLabel; 141 | private JTextField hostField; 142 | private JLabel dblabel; 143 | private JTextField dbField; 144 | private JTable table1; 145 | private JScrollPane JScrollPane2; 146 | private JButton 断开连接Button; 147 | private JOptionPane JP; 148 | } -------------------------------------------------------------------------------- /src/com/wk/Monitor.java: -------------------------------------------------------------------------------- 1 | package com.wk; 2 | 3 | import javax.swing.*; 4 | import javax.swing.table.*; 5 | import java.sql.Connection; 6 | import java.sql.DriverManager; 7 | import java.sql.SQLException; 8 | import java.sql.PreparedStatement; 9 | import java.sql.ResultSet; 10 | import java.sql.ResultSetMetaData; 11 | import java.text.SimpleDateFormat; 12 | import java.util.Date; 13 | import java.util.Vector; 14 | import java.util.regex.*; 15 | import java.io.File; 16 | import java.nio.charset.Charset; 17 | import org.apache.commons.io.input.Tailer; 18 | import org.apache.commons.io.input.TailerListenerAdapter; 19 | 20 | public class Monitor { 21 | 22 | public static Connection connectMysql(String user, String pass, String dbname, String host, int port, JOptionPane JP){ 23 | Connection conn = null; 24 | Date d = new Date(); 25 | SimpleDateFormat time = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]"); 26 | try { 27 | Class.forName("com.mysql.cj.jdbc.Driver"); 28 | conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%d/%s?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", host, port, dbname), user, pass); 29 | //System.out.println(time.format(d) + ": 数据库连接成功"); 30 | return conn; 31 | 32 | } catch (ClassNotFoundException | SQLException e) { 33 | //e.printStackTrace(); 34 | //System.out.println(time.format(d) + ": 数据库连接失败..." + e.getMessage()); 35 | JOptionPane.showMessageDialog(null, e.getMessage(), new String("连接失败"), JOptionPane.ERROR_MESSAGE); 36 | return null; 37 | } 38 | } 39 | 40 | public static String execSql(Connection conn, String sql){ 41 | PreparedStatement ps = null; 42 | ResultSet rs = null; 43 | try { 44 | ps = conn.prepareStatement(sql); 45 | rs = ps.executeQuery(); 46 | StringBuilder results = new StringBuilder(); 47 | //检索此 ResultSet对象的列的数量,类型和属性 48 | ResultSetMetaData resultSetMetaData = rs.getMetaData(); 49 | //返回列数 50 | int column = resultSetMetaData.getColumnCount(); 51 | while(rs.next()){ 52 | for (int i = 1; i <= column; i++){ 53 | //System.out.print(rs.getObject(i) + "\t"); 54 | results.append(rs.getObject(i)).append("\t"); 55 | } 56 | //System.out.print("\n"); 57 | results.append("\n"); 58 | } 59 | String text = results.toString(); 60 | rs.close(); 61 | ps.close(); 62 | //conn.close(); 63 | return text; 64 | } catch (SQLException | NullPointerException e) { 65 | //e.printStackTrace(); 66 | return null; 67 | } 68 | } 69 | 70 | 71 | public static String logSwitch(Connection conn, JLabel label){ 72 | Date d = new Date(); 73 | SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 74 | SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd"); 75 | String path = execSql(conn, "select @@datadir;").trim().replace("\\", "/") + String.format("%s.txt", time1.format(d)); 76 | String[] logfile = null; 77 | if(conn != null) { 78 | //String ver = execSql(conn, "select VERSION();"); 79 | //System.out.print(time.format(d) + ": 数据库版本: " + ver); 80 | String[] logStatus = execSql(conn, "show variables like 'general_log';").trim().split("\t"); 81 | 82 | 83 | if (logStatus[1].equals("OFF")) { 84 | //System.out.println(time.format(d) + ": 正在尝试开启日志模式"); 85 | label.setText(time.format(d) + ": 正在尝试开启日志模式"); 86 | execSql(conn, "set global general_log=on;"); 87 | logStatus = execSql(conn, "show variables like 'general_log';").trim().split("\t"); 88 | if (logStatus[1].equals("ON")) { 89 | //开启日志模式后设置log文件存放路径 90 | //System.out.println(time.format(d) + ": 日志模式已开启"); 91 | label.setText(time.format(d) + ": 日志模式已开启"); 92 | //System.out.println(path); 93 | execSql(conn, "set global general_log_file='" + path + "';"); 94 | logfile = execSql(conn, "show variables like 'general_log_file';").trim().split("\t"); 95 | //System.out.println(time.format(d) + ": 日志文件: " + logfile[1]); 96 | //System.out.println(time.format(d) + ": 日志监听中..."); 97 | label.setText(time.format(d) + ": 日志监听中..."); 98 | try { 99 | // logMonitor(logfile[1]); 100 | } catch (Exception e) { 101 | e.printStackTrace(); 102 | } 103 | } 104 | } else { 105 | //System.out.println(time.format(d) + ": 日志模式已开启"); 106 | //System.out.println(time.format(d) + ": 日志监听中..."); 107 | label.setText(time.format(d) + ": 日志模式已开启"); 108 | label.setText(time.format(d) + ": 日志监听中..."); 109 | execSql(conn, "set global general_log_file='" + path + "';"); 110 | logfile = execSql(conn, "show variables like 'general_log_file';").trim().split("\t"); 111 | try { 112 | // logMonitor(logfile[1]); 113 | } catch (Exception e) { 114 | e.printStackTrace(); 115 | } 116 | } 117 | 118 | } 119 | return logfile[1]; 120 | } 121 | 122 | public void logMonitor(String logfile, int cacheNum, JTable table, DefaultTableModel defaultModel){ 123 | SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss:SSSS"); 124 | 125 | Pattern r = Pattern.compile("Query\\s*(.*)"); 126 | MySqlStParser mtp = new MySqlStParser(); 127 | table.setAutoCreateRowSorter(true); 128 | File file = new File(logfile); 129 | Charset charset = Charset.forName("UTF-8"); 130 | Tailer tailer = new Tailer(file, charset, new TailerListenerAdapter(){ 131 | int count = 1; 132 | @Override 133 | public void handle(String line) { 134 | //增加的文件的内容 135 | String resultText = null; 136 | //System.out.println(line); 137 | Matcher m = r.matcher(line); 138 | if(m.find()){ 139 | String querycontent = ""; 140 | if(!m.group(1).equals("")){ 141 | if(m.group(1).equals("shutdown")){ 142 | querycontent = "shutdown"; 143 | resultText = "Mysql ShutDown"; 144 | } 145 | else{ 146 | querycontent = m.group(1); 147 | if(mtp.MySqlParser(querycontent)){ 148 | resultText = "Syntax Error"; 149 | table.getColumnModel().getColumn(1).setCellRenderer(new StatusColumnCellRenderer()); 150 | } 151 | else{ 152 | resultText = "OK"; 153 | } 154 | } 155 | Vector vRow = new Vector(); 156 | vRow.add(count); 157 | vRow.add(querycontent); 158 | vRow.add(resultText); 159 | vRow.add("Query"); 160 | vRow.add(time.format(new Date())); 161 | defaultModel.addRow(vRow); 162 | table.setRowSorter(null); 163 | if(defaultModel.getRowCount() > 0){ 164 | RowSorter sorter = new TableRowSorter(defaultModel); 165 | table.setRowSorter(sorter); 166 | table.setAutoCreateRowSorter(true); 167 | defaultModel.fireTableDataChanged(); 168 | } 169 | if(defaultModel.getRowCount()>cacheNum){ 170 | defaultModel.setRowCount(0); 171 | count = 0; 172 | } 173 | count += 1; 174 | } 175 | } 176 | } 177 | },500,true, false, 8192); 178 | tailer.run(); 179 | } 180 | 181 | public static void main(String[] args){ 182 | 183 | } 184 | } -------------------------------------------------------------------------------- /src/com/wk/MySqlStParser.java: -------------------------------------------------------------------------------- 1 | package com.wk; 2 | 3 | import com.alibaba.druid.sql.parser.ParserException; 4 | import java.util.List; 5 | import com.alibaba.druid.sql.ast.SQLStatement; 6 | import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; 7 | 8 | public class MySqlStParser { 9 | 10 | public static Boolean MySqlParser(String sql) { 11 | boolean isValid = false; 12 | try { 13 | MySqlStatementParser parser = new MySqlStatementParser(sql); 14 | List statementList = parser.parseStatementList(); 15 | } 16 | catch (ParserException e){ 17 | isValid = true; 18 | //System.out.println(e.getMessage()); 19 | } 20 | return isValid; 21 | } 22 | 23 | public static void main(String[] args){ 24 | System.out.println(MySqlParser("select user")); 25 | } 26 | } -------------------------------------------------------------------------------- /src/com/wk/StatusColumnCellRenderer.java: -------------------------------------------------------------------------------- 1 | package com.wk; 2 | 3 | import javax.swing.*; 4 | import javax.swing.table.DefaultTableCellRenderer; 5 | import java.awt.*; 6 | 7 | public class StatusColumnCellRenderer extends DefaultTableCellRenderer { 8 | @Override 9 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { 10 | 11 | //Cells are by default rendered as a JLabel. 12 | JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); 13 | 14 | //Get the status for the current row. 15 | if (table.getValueAt(row, 2).equals("Syntax Error")) { 16 | l.setBackground(new Color(255,51,51)); 17 | } 18 | else { 19 | if (row%2 == 0){ 20 | l.setBackground(new Color(255,255,255)); 21 | } 22 | else{ 23 | l.setBackground(new Color(242,242,242)); 24 | } 25 | //System.out.println(l.getBackground()); 26 | } 27 | if (isSelected){ 28 | table.setSelectionBackground(new Color(242,242,242)); 29 | table.setSelectionForeground(new Color(132,172,160)); 30 | } 31 | //Return the JLabel which renders the cell. 32 | return l; 33 | 34 | } 35 | } 36 | --------------------------------------------------------------------------------