前言:
当前大家对“elk查日志”大致比较着重,我们都需要学习一些“elk查日志”的相关文章。那么小编在网摘上收集了一些对于“elk查日志””的相关资讯,希望兄弟们能喜欢,大家一起来了解一下吧!在小公司干运维的,因为没有专业的DBA,所以数据库维护的工作也落在运维的头上。在实际工作中,有时候项目更新,需要执行数据库脚本,或者有时候需要修改数据,就给了生产库的权限他们,而他们做了什么操作,也是没有记录的。如果有人恶意篡改、删除数据,运维去查又查不到,这黑锅想甩也甩不掉。当然并不是所有场合都适用的。
一、MySQL数据库启用审计
系统环境: Centos 6.5
数据库版本: mardib 10.0.20
机器2台: 192.168.1.75 maridb、Filebeat
192.168.1.76 ELK、redis
1.数据库审计插件选择
mysql只有企业版有审计插件,一般我们小公司用的社区版没有,所以要想对数据库启用审计,只能选择第三方插件。
Percona audit plugin,这个插件生成的SQL,有些字符使用json.load的时候会报错,不能解析成json,在导入ELK的时候,需要把这些字符替换掉,稍微麻烦一点。
Macfee audit Plugin,这个插件日志信息比较详细,当然对性能影响也很大。
maridb audit Plugin,mardib自带的审计插件,只能对所有操作审计
注:数据库启用审计插件会有10%-15%左右的性能损失;如果数据库读写比较频繁的话,建议不要启用。
2.数据库审计插件安装
我这里选择的是Macfee audit Plugin,因为他的审计日志默认就是json格式的,可以直接导入ES
下载插件:
下载offset-extract.sh 脚本:
查看mysql插件目录
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'plugin_dir';
+---------------+-----------------------------+
| Variable_name | Value |
+---------------+-----------------------------+
| plugin_dir | /home/app/mysql/lib/plugin/ |
+---------------+-----------------------------+
1 row in set (0.00 sec)
解压插件,拷贝到mysql插件目录下
#unzip audit-plugin-mariadb-10.0-1.0.8-527-linux-x86_64.zip
#cd audit-plugin-mariadb-10.0-1.0.8-527
#mv libaudit_plugin.so /home/app/mysql/lib/plugin/
#chmod +x /home/app/mysql/lib/plugin/libaudit_plugin.so
计算偏移量
# chmod +x offset-extract.sh
# ./offset-extract.sh /home/app/mysql/bin/mysqld
170918 16:54:24 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
170918 16:54:24 [Note] /home/app/mysql/bin/mysqld (mysqld 10.0.20-MariaDB-log) starting as process 33977 ...
//offsets for: /home/app/mysql/bin/mysqld (10.0.20-MariaDB-log)
{"10.0.20-MariaDB","55803b5574dbce1cc358899f094d8d04", 13408, 13472, 6192, 7792, 88, 3000, 8, 0, 16, 24},
注:偏移量,设置不对日志会报错如下报错
[Note] Audit Plugin: setup_offsets audit_offsets: (null) validate_checksum: 1 offsets_by_version:
[ERROR] Couldn't load plugin named 'AUDIT' with soname 'libaudit_plugin.so'.
修改mysql配置文件,增加以下参数
重启mysql
#vim /etc/my.cnf
plugin-load=AUDIT=libaudit_plugin.so
audit_json_file=1 #开启审计功能
audit_json_socket_name=/home/app/mysql/data/audit.sock
audit_json_socket=OFF
audit_json_log_file=/home/app/mysql/log/audit.log #审计日志保存路径
audit_record_cmds=connect,drop,alert,select,update,delete,inser,create #
审计命令,默认记录所有命令
audit_validate_checksum=OFF
audit_offsets=13408, 13472, 6192, 7792, 88, 3000, 8, 0, 16, 24 #前面offsets脚本值
#audit_record_objs = db1.tb1,db2.* #audit记录操作的对象,默认为记录所有对象
#/etc/init.d/mysqld restart
查看审计日志
# tail -2 /home/app/mysql/log/audit.log
{"msg-type":"activity","date":"1505383729436","thread-id":"4","query-id":"16","user":"root","priv_user":"root","host":"localhost","cmd":"create_table","objects":[{"db":"db1","name":"t1","obj_type":"TABLE"}],"query":"CREATE TABLE t1(id int,name char)"}
{"msg-type":"activity","date":"1505383792066","thread-id":"4","query-id":"17","user":"root","priv_user":"root","host":"localhost","cmd":"insert","objects":[{"db":"db1","name":"t1","obj_type":"TABLE"}],"query":"insert into t1(id,name) values (1,yunweiadmin)"}
二、数据库审计日志导入ELK
首先来看看我们的ELK架构,使用传统的ELK架构,logstash-forwarder我们已经使用Filebeat替代掉了,原因大家也清楚,lostash运行在java虚拟机上,资源消耗比较大。
1.修改Filebeat配置文件
[root@localhost ~]# cat /etc/filebeat/filebeat.yml
filebeat.prospectors:
- input_type: log
paths:
/home/app/mysql/log/audit.log
document_type: mysql-audit-log
registry_file: /var/lib/filebeat/registry
processors:
- include_fields:
fields: ["message"]
- drop_fields:
fields: ["beat", "offset"]
output.redis:
hosts: ["192.168.1.76:6379"]
data_type: "list"
key: "logstash:redis"
path.logs: /tmp/filebeatlogs
logging.to_files: true
logging.files:
2.修改logstash配置文件
input {
redis {
host => "localhost"
data_type => "list"
key => "logstash:redis"
type => "mysql-audit-log"
}
}
filter {
if [type] == "mysql-audit-log" {
json {
source => "message"
remove_field => [ "beat","message"]
}
date {
match => [ "date", "UNIX_MS" ]
target => "@timestamp"
remove_field => "date"
}
ruby {
code => "event.timestamp.time.localtime"
}
}
}
output {
if [type] == "mysql-audit-log" {
elasticsearch {
hosts => ["localhost:9200"]
index => "mysql-audit-log-%{+YYYY-MM}"
}
}
}
3.访问kibana,添加索引,效果如下
标签: #elk查日志