i am collecting logs using OSSEC and forwarding JSON logs to logstash using logstash-forwarder. this is my logstash configuration.
input {
lumberjack {
port => 10516
type => "lumberjack"
ssl_certificate => "/etc/pki/tls/certs/logstash-forwarder.crt"
ssl_key => "/etc/pki/tls/private/logstash-forwarder.key"
codec => json
}
}
filter {
json {
source => "message"
}
}
output {
elasticsearch {
host => localhost
}
}
i would like to extract the host indicated on "location" field inside the parenthesis and create a dedicated tag because logstash only sees OSSEC as the source host because it forwards the logs. below is the sample output of logstash.
{
"_index": "logstash-2015.09.23",
"_type": "ossec-alerts",
"_id": "AU_4Q1Hc5OjGfEBnRiWa",
"_score": null,
"_source": {
"rule": {
"level": 3,
"comment": "Nginx error message.",
"sidid": 31301
},
"srcip": "192.168.192.10",
"location": "(logstash) 192.168.212.104->/var/log/nginx/error.log",
"full_log": "2015/09/23 11:33:24 [error] 1057#0: *562 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.192.10, server: _, request: \"POST /elasticsearch/.kibana/__kibanaQueryValidator/_validate/query?explain=true&ignore_unavailable=true HTTP/1.1\", upstream: \"http://[::1]:5601/elasticsearch/.kibana/__kibanaQueryValidator/_validate/query?explain=true&ignore_unavailable=true\", host: \"192.168.212.104\", referrer: \"http://192.168.212.104/\"",
"@version": "1",
"@timestamp": "2015-09-23T03:33:25.588Z",
"type": "ossec-alerts",
"file": "/var/ossec/logs/alerts/alerts.json",
"host": "ossec",
"offset": "51048"
},
"fields": {
"@timestamp": [
1442979205588
]
},
"sort": [
1442979205588
]
}
Once you apply the json{} filter, you're left with a bunch of fields. You can now apply more filters to those fields, including grok{} for making more fields!
What you need is the grok filter . You can use the grok debugger to find the best patterns for you. The following pattern should work for your location
field:
\(%{HOST:host}\) %{IP:srcip}->%{PATH:path}
In logstash filter section :
grok {
match => { "location" => "\(%{HOST:host}\) %{IP:srcip}->%{PATH:path}" }
overwrite => [ "host", "srcip" ]
}
overwrite
is necessary because you already have fields host
and srcip
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.