Logstash 日誌收集
阿新 • • 發佈:2020-08-18
一.Logstash收集日誌
1.Logstash的配置檔案
[root@web01 ~]# vim /etc/logstash/logstash.yml
path.config: /etc/logstash/conf.d
2.logstash收集日誌檔案到檔案
[root@web01 ~]# vim /etc/logstash/conf.d/file_file.conf input { file { path => "/var/log/messages" start_position => "beginning" } } output { file { path => "/tmp/messages_%{+YYYY-MM-dd}.log" } }
3.logstash收集日誌檔案到ES
[root@web01 ~]# vim /etc/logstash/conf.d/file_es.conf input { file { path => "/var/log/messages" start_position => "beginning" } } output { elasticsearch { hosts => ["172.16.1.51:9200"] index => "messages_%{+YYYY-MM-dd}.log" } }
4.Logstash收集多日誌到檔案
[root@web01 ~]# vim /etc/logstash/conf.d/file_file.conf input { file { type => "messages_log" path => "/var/log/messages" start_position => "beginning" } file { type => "secure_log" path => "/var/log/secure" start_position => "beginning" } } output { if [type] == "messages_log" { file { path => "/tmp/messages_%{+YYYY-MM-dd}" } } if [type] == "secure_log" { file { path => "/tmp/secure_%{+YYYY-MM-dd}" } } }
5.Logstash收集多日誌到ES
1)方法一:
[root@web01 ~]# vim /etc/logstash/conf.d/more_es.conf
input {
file {
type => "messages_log"
path => "/var/log/messages"
start_position => "beginning"
}
file {
type => "secure_log"
path => "/var/log/secure"
start_position => "beginning"
}
}
output {
if [type] == "messages_log" {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "messages_%{+YYYY-MM-dd}"
}
}
if [type] == "secure_log" {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "secure_%{+YYYY-MM-dd}"
}
}
}
[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es.conf &
#啟動後檢視頁面
2)方法二:
[root@web01 ~]# vim /etc/logstash/conf.d/more_es_2.conf
input {
file {
type => "messages_log"
path => "/var/log/messages"
start_position => "beginning"
}
file {
type => "secure_log"
path => "/var/log/secure"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "%{type}_%{+YYYY-MM-dd}"
}
}
[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es_2.conf --path.data=/data/logstash/more_es_2 &
3)啟動多例項
#建立不同的資料目錄
[root@web01 ~]# mkdir /data/logstash/more_es_2
[root@web01 ~]# mkdir /data/logstash/more_es
#啟動時使用--path.data指定資料目錄
[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es.conf --path.data=/data/logstash/more_es &
[root@web01 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/more_es_2.conf --path.data=/data/logstash/more_es_2 &
#如果資源充足,可以使用多例項收集多日誌,如果伺服器資源不足,啟動不了多例項,配置一個檔案收集多日誌啟動
二、Logstash收集Tomcat日誌
1.安裝Tomcat
1.安裝java環境
[root@web01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm
2.上傳包
[root@web01 ~]# rz apache-tomcat-10.0.0-M7.tar.gz
3.解壓
[root@web01 ~]# tar xf apache-tomcat-10.0.0-M7.tar.gz -C /usr/local/
4.做軟連線
[root@web01 ~]# ln -s /usr/local/apache-tomcat-10.0.0-M7 /usr/local/tomcat
5.啟動Tomcat
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh
6.訪問頁面 10.0.0.7:8080
2.配置Logstash收集Tomcat日誌到檔案
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_file.conf
input {
file {
path => "/usr/local/tomcat/logs/localhost_access_log.*.txt"
start_position => "beginning"
}
}
output {
file {
path => "/tmp/tomcat_%{+YYYY-MM-dd}.log"
}
}
3.配置Logstash收集Tomcat日誌到ES
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_es.conf
input {
file {
path => "/usr/local/tomcat/logs/localhost_access_log.*.txt"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tomcat_%{+YYYY-MM-dd}.log"
}
}
三、收集Tomcat日誌修改格式
#收集tomcat日誌,當遇到報錯時,一條報錯會被分割成很多條資料,不方便檢視
解決方法:
1.修改tomcat日誌格式為json
1)開發修改輸出日誌為json
2)修改tomcat配置,日誌格式為json
2.使用logstash的input外掛下的mutiline模組
1.方法一:修改tomcat日誌格式
1)配置tomcat日誌為json格式
[root@web01 ~]# vim /usr/local/tomcat/conf/server.xml
#把原來的日誌格式註釋,新增我們的格式
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="tomcat_access_json" suffix=".log"
pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","method":"%r","status":"%s","SendBytes":"%b","Query?string":"%q","partner":"%{Referer}i","AgentVersion":"%{User-Agent}i"}"/>
2)重啟tomcat
[root@web01 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh
3)配置收集新的tomcat日誌
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
file {
path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tomcat_json_%{+YYYY-MM-dd}.log"
}
}
2.方法二:使用mutiline模組收集日誌
1)配置收集日誌測試
[root@web01 ~]# vim /etc/logstash/conf.d/test_mutiline.conf
input {
stdin {
codec => multiline {
#以[開頭
pattern => "^\["
#匹配到
negate => true
#向上合併,向下合併是next
what => "previous"
}
}
}
output {
stdout {
codec => json
}
}
#測試,輸入內容不會直接輸出,當遇到以 [ 開頭才會收集以上的日誌
2)配置收集tomcat錯誤日誌
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_mutiline.conf
input {
file {
path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
start_position => "beginning"
codec => multiline {
pattern => "^\["
negate => true
what => "previous"
}
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tomcat_json_%{+YYYY-MM-dd}"
codec => "json"
}
}
3)將錯誤日誌寫入
[root@web01 ~]# cat 1.txt >> /usr/local/tomcat/logs/tomcat_access_json.2020-08-14.log
4)頁面檢視資料
四、收集Nginx日誌
1.安裝Nginx
[root@web01 ~]# yum install -y nginx
2.配置Nginx日誌格式
[root@web01 ~]# vim /etc/nginx/nginx.conf
... ...
http {
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log /var/log/nginx/access.log json;
... ...
3.配置收集Nginx日誌
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "nginx_json_%{+YYYY-MM-dd}.log"
}
}
五、獲取的日誌引數分離
1.方法一:
1)修改tomcat日誌收集配置
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
file {
path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
start_position => "beginning"
}
}
#把收集到的資料進行處理
filter {
json {
source => "message"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tomcat_json_%{+YYYY-MM-dd}.log"
}
}
2)去掉多餘資料
#message資料已經拆分,資料還在,去掉message資料
filter {
json {
source => "message"
remove_field => ["message"]
}
}
2.方法二:
1)修改收集Nginx日誌的配置
#nginx不需要配置修改獲取日誌,只需要收集同時修改格式即可
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "nginx_json_%{+YYYY-MM-dd}.log"
}
}
六、Logstash收集日誌寫入redis
1.安裝redis
2.配置將資料寫入redis
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_to_redis.conf
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
}
output {
redis {
host => "172.16.1.51"
port => "6379"
data_type => "list"
db => "0"
key => "nginx_log"
}
}
4.收集Nginx和tomcat日誌到redis
[root@web01 ~]# vim /etc/logstash/conf.d/more_to_redis.conf
input {
file {
type => "nginx_log"
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
file {
type => "tomcat_log"
path => "/usr/local/tomcat/logs/tomcat_access_json.*.log"
start_position => "beginning"
codec => "json"
}
}
output {
if [type] == "nginx_log" {
redis {
host => "172.16.1.51"
port => "6379"
data_type => "list"
db => "0"
key => "nginx_log"
}
}
if [type] == "tomcat_log" {
redis {
host => "172.16.1.51"
port => "6379"
data_type => "list"
db => "1"
key => "tomcat_log"
}
}
}
#驗證:訪問Nginx和tomcat頁面,檢視redis裡面有沒有key
127.0.0.1:6379> LLEN nginx_log
(integer) 1
127.0.0.1:6379> LLEN nginx_log
(integer) 888
127.0.0.1:6379> LRANGE nginx_log 0 -1
5.配置將redis取出,寫入ES
[root@db02 ~]# yum localinstall -y logstash-6.6.0.rpm
[root@db02 ~]# vim /etc/logstash/conf.d/redis_to_es.conf
input {
redis {
host => "172.16.1.51"
port => "6379"
db => "0"
data_type => "list"
key => "nginx_log"
}
redis {
host => "172.16.1.51"
port => "6379"
db => "1"
data_type => "list"
key => "tomcat_log"
}
}
output {
if [type] == "nginx_log" {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "nginx_log_%{+YYYY-MM-dd}"
}
}
if [type] == "tomcat_log" {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tomcat_log_%{+YYYY-MM-dd}"
}
}
}
七、通過TCP/UDP收集日誌
1.配置收集日誌
[root@web01 ~]# vim /etc/logstash/conf.d/tcp.conf
input {
tcp {
port => "1234"
mode => "server"
}
}
output {
stdout {}
}
2.使用telnet測試
[root@db02 ~]# telnet 172.16.1.7 1234
Trying 172.16.1.7...
Connected to 172.16.1.7.
Escape character is '^]'.
123
345
#輸出內容
{
"@timestamp" => 2020-08-17T02:23:05.833Z,
"host" => "172.16.1.52",
"port" => 33002,
"message" => "\r",
"@version" => "1"
}
{
"@timestamp" => 2020-08-17T02:23:32.562Z,
"host" => "172.16.1.52",
"port" => 33002,
"message" => "123\r",
"@version" => "1"
}
{
"@timestamp" => 2020-08-17T02:23:38.300Z,
"host" => "172.16.1.52",
"port" => 33002,
"message" => "345\r",
"@version" => "1"
}
3.使用nc工具測試
#安裝
[root@db02 ~]# yum install -y nc
#使用nc工具
[root@db02 ~]# nc 172.16.1.7 1234
123
456
#使用nc工具收集日誌到logstash的伺服器
[root@web01 ~]# tail -f /var/log/nginx/access.log | nc 10.0.0.7 1234 &
[1] 29595
#傳送偽裝置資料
[root@web01 ~]# echo "偽裝置測試" > /dev/tcp/10.0.0.7/1234
4.收集日誌到ES
[root@web01 ~]# vim /etc/logstash/conf.d/tcp.conf
input {
tcp {
port => "1234"
mode => "server"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tcp_log_%{+YYYY-MM-dd}"
}
}
八、Logstash配合rsyslog收集haproxy日誌
1.rsyslog介紹
在centos 6及之前的版本叫做syslog,centos 7開始叫做rsyslog,根據官方的介紹,rsyslog(2013年版本)可以達到每秒轉發百萬條日誌的級別,官方網址:http://www.rsyslog.com/
2.安裝
[root@web01 ~]# yum isntall -y rsyslog
3.配置rsyslog
[root@web01 ~]# vim /etc/rsyslog.conf
#開啟註釋
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
#新增日誌收集級別
local6.* @@172.16.1.52:2222
4.安裝haproxy
[root@web01 ~]# yum install -y haproxy
5.配置haproxy
[root@web01 ~]# vim /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /var/lib/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /var/run/haproxy.pid
log 127.0.0.1 local6 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth haadmin:123456
#frontend web_port
frontend web_port
bind 0.0.0.0:80
mode http
option httplog
log global
option forwardfor
###################ACL Setting##########################
acl pc hdr_dom(host) -i www.elk.com
acl mobile hdr_dom(host) -i m.elk.com
###################USE ACL##############################
use_backend pc_host if pc
use_backend mobile_host if mobile
########################################################
backend pc_host
mode http
option httplog
balance source
server web1 10.0.0.53:8081 check inter 2000 rise 3 fall 2 weight 1
backend mobile_host
mode http
option httplog
balance source
server web1 10.0.0.53:8080 check inter 2000 rise 3 fall 2 weight 1
[root@web01 ~]# vim /etc/haproxy/haproxy.cfg
#全域性配置
global
#最大併發
maxconn 100000
#安全機制
chroot /var/lib/haproxy
#指定啟動的使用者和組
uid 99
gid 99
#守護程序
daemon
#haproxy的程序數
nbproc 1
#指定pid檔案
pidfile /var/run/haproxy.pid
#指定日誌級別
log 127.0.0.1 local6 info
#預設配置
defaults
#開啟長連線
option http-keep-alive
#獲取使用者真實IP
option forwardfor
#最大連線數
maxconn 100000
#支援http協議
mode http
#設定連線超時時間
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
#監控狀態
listen status
#支援http
mode http
#監聽埠
bind 0.0.0.0:9999
#啟動
stats enable
#日誌級別
log global
#訪問uri地址
stats uri /haproxy-status
#狀態頁使用者名稱和密碼
stats auth haadmin:123456
#frontend web_port
frontend web_port
bind 0.0.0.0:80
mode http
option httplog
log global
option forwardfor
###################ACL Setting##########################
acl nginx hdr_dom(host) -i www.nginx.com
acl tomcat hdr_dom(host) -i www.tomcat.com
###################USE ACL##############################
use_backend nginx_host if nginx
use_backend tomcat_host if tomcat
########################################################
backend nginx_host
mode http
option httplog
balance source
server web01 10.0.0.7:8081 check inter 2000 rise 3 fall 2 weight 1
backend tomcat_host
mode http
option httplog
balance source
server web01 10.0.0.7:8080 check inter 2000 rise 3 fall 2 weight 1
6.修改Nginx啟動埠
[root@web01 ~]# vim /etc/nginx/nginx.conf
server {
listen 8081 default_server;
...
7.啟動服務
#啟動haproxy
[root@web01 ~]# systemctl start haproxy.service
#啟動rsyslog
[root@web01 ~]# systemctl start rsyslog
#驗證
[root@web01 ~]# netstat -lntp
8.訪問狀態頁面
http://10.0.0.7:9999/haproxy-status
haadmin
123456
9.測試訪問Nginx和tomcat
#配置本地hosts
10.0.0.7 www.nginx.com
10.0.0.7 www.tomcat.com
#訪問頁面
http://www.nginx.com/
http://www.tomcat.com/
10.測試配置收集proxy日誌
[root@db02 ~]# vim /etc/logstash/conf.d/haproxy.conf
input {
syslog {
port => "2222"
}
}
output {
stdout {}
}
#訪問haproxy的頁面,檢視有無輸出
11.配置收集proxy日誌到ES
[root@db02 ~]# vim /etc/logstash/conf.d/haproxy_es.conf
input {
syslog {
port => "2222"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "haproxy_log_%{+YYYY-MM-dd}"
}
}