ELK -- 配置使用 (二)
一,logstash 使用
1,收集系统日志
在192.168.204.130 主机上,通过 logstash 写配置文件的方式来采集所需的日志。
其运行的方式是:
[root@linux-test-2 ~]#/usr/share/logstash/bin/logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/all.conf
1,登陆 192.168.204.130 主机,在/etc/logstash/conf.d/ 目录下,建立一个 all.conf 文件,用于采集系统日志。
其 logstash 的基础写法是,必须包含有 input {} out {} ,一个输入,一个输出的。
如:编写一个采集linux 系统日志的 配置文件,其内容如下:
input {
# 收集系统日志,采用syslog 插件
syslog { ## 通过 syslog 插件方式,
type => "system-syslog" ## 定义一个类型
host => "192.168.204.128" ## 采集当前主机的host ip
port => "514" ## 端口
}
file { ## 通过 file 文件方式,
path => "/var/log/messages" ## 采集文件的具体文件信息
type => "system-log" ## 定义一个类型
start_position => "beginning" ## 从哪里开始采集
}
file {
path => "/var/log/elasticsearch/my-els-server.log"
type => "es-error"
start_position => "beginning"
codec => multiline { ## 多行处理插件
pattern => "^\[" ## 匹配 "[" 符号
negate => true ## 判断是否匹配中 true
what => "previous" ## 匹配上,则事件为前一个。
}
}
file {
path => "/var/log/nginx/access_json.log"
codec => json ## json 格式的日志处理
type => "nginx-log"
start_position => "beginning"
}
}
output {
if [type] == "system-syslog" { ## 判断什么类型
elasticsearch { ## 存储到es 中
hosts => ["192.168.204.128:9200"] ## es 的主机
index => "system-syslog-%{+YYYY.MM.dd}" ## 存储的索引是什么,这个届时需要在 kibana 手动进行配置索引
}
}
if [type] == "system-log" {
elasticsearch {
hosts => ["192.168.204.128:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-error" {
elasticsearch {
hosts => ["192.168.204.128:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
if [type] == "nginx-log" {
elasticsearch {
hosts => ["192.168.204.128:9200"]
index => "nginx-log"
}
}
}
2,打开 /etc/rsyslog.conf ,修改配置文件
#*.* @@remote-host:514
改为:
*.* @@192.168.204.128:514 (让linux监控514端口)
3,启动 syslog 服务。
2,收集JAVA日志
收集JAVA 日志,编写一个 java.conf 的文件,其内容如下:
input {
# 收集es日志(java类,其中codec = multiline 是对多行进行合并,匹配 [ 符号开头,下一行未匹配到 [ 符号的,合并到上一行中。)
file {
path => "/var/log/elasticsearch/my-els-server.log"
type => "es-error"
start_position => "beginning"
codec => multiline {
pattern => "^\["
negate => true
what => "previous"
}
}
output {
elasticsearch {
hosts => ["192.168.204.128:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
3,收集nginx日志
对 nginx 日志进行收集,需对nginx的日志格式进行修改,修改为日志输出为 json 格式。
修改nginx的配置文件,添加 json 格式日志,如下:
主要添加:具体可以参考官方配置方式
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domain":"$host",'
'"host":"$server_addr",'
'"size":"$body_bytes_sent",'
'"responsentime":"$request_time",'
'"referer":"$http_referer",'
'"useragent":"$http_user_agent"'
'}';
access_log /var/log/nginx/access_json.log json;
3,编写 logstash 的配置文件:
input {
file {
path => "/var/log/nginx/access_json.log"
codec => json
type => "nginx-log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["192.168.204.128:9200"]
index => "nginx-log"
}
}

浙公网安备 33010602011771号