MySQL 审计日志 McAfee 审计 支持 MySQL 8.0
思路:SSH 连接服务器,读取 json 文件,处理数据,前端展示
方案:
- 读取到 json 文件内容,简单处理,前端直接展示
- 定时任务,读取到 json 文件内容,处理数据,保存到 MySQL,后端写接口,前端展示
重要依赖:
<!-- ssh -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
部分代码展示:
public void exec(String host, String user, String password, String[] cmd) {
try {
Connection connection = new Connection(host);
connection.connect();
boolean authenticate = connection.authenticateWithPassword(user, password);
if (!authenticate) {
system.out.println("服务器认证失败");
}
Session session = null;
for (int i = 0; i < cmd.length; i++) {
session = connection.openSession();
session.execCommand(cmd[i]);
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
stdout.close();
session.close();
}
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test() {
exec("【主机】", "【用户名】", "【密码】", new String[]{"tail -n 20 /var/lib/mysql/mysql-audit.json | grep \"\""});
}