input {
file {
path => [ "/var/log/syslog" ] #定义日志路径
type => "syslog"
start_position => "beginning"
ignore_older =>0
}
file {
path => "/var/log/nginx/*access.log"
codec => json
start_position => "beginning"
type => "nginx-acc"
}
file {
path => "/var/log/nginx/*error.log"
start_position => "beginning"
type => "nginx-error"
ignore_older =>0
}
file {
path => [ "/data/mongo/mongo.log" ]
type => "mongo"
start_position => "beginning"
#ignore_older =>0
}
}
filter {
if [type] == "syslog" {
grok { #grok 功能将字符串转换为相应的字段,方便检索
match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
add_field => [ "received_at", "%{@timestamp}" ]
add_field => [ "received_from", "%{host}" ]
}
date {
match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
}
}
if [type] == "mongo" {
grok {
#mongo_v3 match => ["message","%{TIMESTAMP_ISO8601:timestamp}\s+%{MONGO3_SEVERITY:severity}\s+%{MONGO3_COMPONENT:component}\s+(?:\[%{DATA:context}\])?\s+%{GREEDYDATA:body}"]
match => ["message","%{SYSLOGTIMESTAMP:timestamp} \[%{WORD:component}\] %{GREEDYDATA:body}"] #mongo_v2
}
if[body]=~"ms$" {
grok {
match => ["body","query\s+%{WORD:db_name}\.%{WORD:collection_name}.*}.*\}(\s+%{NUMBER:spend_time:int}ms$)?"]
}
}
date {
match => [ "timestamp", "UNIX", "YYYY-MM-dd HH:mm:ss", "ISO8601" ]
remove_field => ["timestamp"]
}
}
if [type] == "nginx-error" {
grok {
match => { "message" => "(?<timestamp>%{YEAR}[./-]%{MONTHNUM}[./-]%{MONTHDAY}[- ]%{TIME}) \[%{LOGLEVEL:severity}\] %{POSINT:pid}#%{NUMBER}: %{GREEDYDATA:errormessage}(?:, client: (?<client>%{IP}|%{HOSTNAME}))(?:, server: %{IPORHOST:server})(?:, request: %{QS:request})?(?:, upstream: \"%{URI:upstream}\")?(?:, host: %{QS:host})?(?: referrer: \"%{URI:referrer}|-\")?" }
overwrite => [ "message" ]
}
date {
match => [ "nginx_error_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
remove_field => [ "timestamp" ]
}
}
}
output {
if [type] == "nginx-acc" {
elasticsearch { #存储
hosts => ["127.0.0.1:9200"]
index => "nginx_access-%{+YYYY.MM.dd}"
}
}
if [type] == "nginx-error" {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "nginx_error-%{+YYYY.MM.dd}"
}
}
if [type] == "syslog" {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "syslog-%{+YYYY.MM.dd}"
}
}
if [type] == "mongo" {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "mongo-%{+YYYY.MM.dd}"
}
}
}