ELK收集日志到mysql数据库
場景需求
在使用ELK對日志進行收集的時候,如果需要對數(shù)據(jù)進行存檔,可以考慮使用數(shù)據(jù)庫的方式。為了便于查詢,可以同時寫一份數(shù)據(jù)到Elasticsearch 中。
環(huán)境準(zhǔn)備
CentOS7系統(tǒng):
- 192.168.20.60 node1 Kibana ES Logstash Nginx
- 192.168.20.61 node2 ES MariaDB
這里使用收集Nginx日志到數(shù)據(jù)庫和ES中作為示例。
配置數(shù)據(jù)庫
安裝好數(shù)據(jù)庫后,配置,并授權(quán):
MariaDB [(none)]> create database elk character set utf8 collate utf8_bin; Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> grant all privileges on elk.* to elk@'192.168.20.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)在node1上測試數(shù)據(jù)庫的連接:
[root@node1 ~]# yum install mariadb -y [root@node1 ~]# mysql -uelk -p123456 -h 192.168.20.61在node1上安裝Logstash, 可以直接從官方下載rpm包安裝,Elasticsearch和Kibana的安裝跳過,可參考Kibana使用Nginx代理驗證和ELK日志管理平臺部署簡介的前半部分,這里不再贅述。
[root@node1 ~]# yum install logstash-5.6.5.rpm -y [root@node1 ~]# systemctl start logstash配置JDBC數(shù)據(jù)庫驅(qū)動
按裝logstash的數(shù)據(jù)庫驅(qū)動需要先安裝gem源:
[root@node1 ~]# yum install gem -y [root@node1 ~]# gem -v 2.0.14.1 [root@node1 ~]# gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ https://gems.ruby-china.org/ added to sources https://rubygems.org/ removed from sources [root@node1 ~]# gem source list *** CURRENT SOURCES ***https://gems.ruby-china.org/查看當(dāng)前已經(jīng)安裝的插件:
[root@node1 ~]# /usr/share/logstash/bin/logstash-plugin list安裝JDBC驅(qū)動:
[root@node1 ~]# /usr/share/logstash/bin/logstash-plugin install logstash-output-jdbc Validating logstash-output-jdbc Installing logstash-output-jdbc Installation successful安裝需要等待一段時間,查看是否安裝成功:
[root@node1 ~]# /usr/share/logstash/bin/logstash-plugin list|grep jdbc logstash-input-jdbc logstash-output-jdbc下載數(shù)據(jù)庫的JDBC驅(qū)動:https://dev.mysql.com/downloads/connector/j/ 上傳到服務(wù)器。驅(qū)動的路徑必須嚴(yán)格一致,否則連接數(shù)據(jù)庫會報錯。
[root@node1 ~]# tar xf mysql-connector-java-5.1.45.tar.gz [root@node1 ~]# cd mysql-connector-java-5.1.45 [root@node1 mysql-connector-java-5.1.45]# mkdir -p /usr/share/logstash/vendor/jar/jdbc [root@node1 mysql-connector-java-5.1.45]# cp mysql-connector-java-5.1.45-bin.jar /usr/share/logstash/vendor/jar/jdbc/ [root@node1 ~]# chown -R logstash.logstash /usr/share/logstash/vendor/jar/jdbc/配置Nginx日志格式
要使日志以指定的表中字段的方式存儲,需要將Nginx的日志格式改寫為json格式,修改nginx.conf問,將日志格式配置部分替換為:
log_format access_log_json '{"host":"$http_x_real_ip","client_ip":"$remote_addr","log_time":"$time_iso8601","request":"$request","status":"$status","body_bytes_sent":"$body_bytes_sent","req_time":"$request_time","AgentVersion":"$http_user_agent"}';access_log /var/log/nginx/access.log access_log_json;檢查語法,并重新加載nginx:
nginx -t nginx -s reload查看日志中新日志的格式是否是json格式。
創(chuàng)建數(shù)據(jù)表
我們在數(shù)據(jù)庫中存儲數(shù)據(jù)的時候,沒有必要存儲日志的所有內(nèi)容,只需存儲我們需要的重要信息即可,可以根據(jù)自身的需求進行取舍。
注意:數(shù)據(jù)表中需要創(chuàng)建time字段,time的默認值設(shè)置為CURRENT_TIMESTAMP.
創(chuàng)建數(shù)據(jù)表語句(只獲取部分數(shù)據(jù)):
MariaDB [elk]> create table nginx_log(host varchar(128),client_ip varchar(128),status int(4),req_time float(8,3),AgentVersion varchar(512), time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;查看表結(jié)構(gòu):
MariaDB [elk]> desc nginx_log; +-------------+--------------+------+-----+-------------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+-------------------+-------+ | host | varchar(128) | YES | | NULL | | | client_ip | varchar(128) | YES | | NULL | | | status | int(4) | YES | | NULL | | | req_time | float(8,3) | YES | | NULL | | | AgentVersion | varchar(512) | YES | | NULL | | | time | timestamp | NO | | CURRENT_TIMESTAMP | | +-------------+--------------+------+-----+-------------------+-------+ 6 rows in set (0.00 sec)配置Logstash將日志寫入數(shù)據(jù)庫
創(chuàng)建Logstash的配置文件:
[root@node1 ~]# vim /etc/logstash/conf.d/nginx_log.conf
測試文件,查看是否正確:
[root@node1 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx_log.conf -t WARNING: Could not find logstash.yml which is typically located in $LS_HOME/config or /etc/logstash. You can specify the path using --path.settings. Continuing using the defaults Could not find log4j2 configuration at path /usr/share/logstash/config/log4j2.properties. Using default config which logs errors to the console Configuration OK如果發(fā)現(xiàn)配置文件正確,但是日志無法收集,可以使用前臺啟動的方式,查看日志信息:
[root@node1 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx_log.conf提示: 如果是使用root操作,使用前臺啟動的方式也就是以root用戶啟動,使用系統(tǒng)systemd啟動使用的是logstash用戶,如果前臺啟動正常,而后臺啟動無法收集日志,一般是目錄或者文件權(quán)限問題。
重啟logstash,訪問nginx生成日志,并查看Elasticsearch是否已經(jīng)收集日志:
[root@node1 ~]# systemctl restart logstashES中已經(jīng)自動創(chuàng)建的數(shù)據(jù)信息:
在Kibana中添加日志信息,用于展示,在輸入名稱之后,選擇使用時間戳的方式,會自動檢索出對應(yīng)的信息:
當(dāng)Kibana上有數(shù)據(jù)展示后,我們可以查看數(shù)據(jù)庫,發(fā)現(xiàn)數(shù)據(jù)庫中已經(jīng)存儲了日志信息:
[root@node2 elasticsearch-head]# mysql -uroot -p123456 -e "select * from elk.nginx_log;"|head -10 host client_ip status req_time AgentVersion time - 192.168.20.191 304 0.023 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:33:39 - 192.168.20.191 200 0.042 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:33:39 - 192.168.20.191 200 0.030 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:33:39 - 192.168.20.191 200 0.042 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:34:33 - 192.168.20.191 200 0.380 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:34:33 - 192.168.20.191 200 0.195 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:34:37 - 192.168.20.191 200 0.034 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:34:45 - 192.168.20.191 200 0.016 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:34:59 - 192.168.20.191 200 0.570 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 2017-12-18 11:35:00將Nginx 日志收集到數(shù)據(jù)庫中就完成了。
如果在收集日志的時候,出現(xiàn)數(shù)據(jù)庫中沒有數(shù)據(jù),或者有些字段的數(shù)據(jù)無法獲取,故障排除的思路是:
1、查看nginx的日志文件格式是否為json格式,日志輸出是否正常。
2、查看es 或者kibana上的日志是否完整,展示是否正常。
3、查看logstash的conf配置文件是否正常,字段標(biāo)注的個數(shù),名稱有無對應(yīng)上。
4、測試配置文件,刷新日志,如果kibana上展示正常,一般是logstash配置文件沒有對應(yīng)上字段,或者數(shù)據(jù)庫權(quán)限問題。
轉(zhuǎn)載于:https://blog.51cto.com/tryingstuff/2050360
總結(jié)
以上是生活随笔為你收集整理的ELK收集日志到mysql数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【IBatis.Net】 各种配置说明
- 下一篇: Splash 占位图适配 全面屏笔记