Split a string with delimiter and assign to new key - logstash

(EatDataForBreakfast) #1

I am trying to import csv into elasticsearch using logstash. I have a field in the csv with is an IP:Port pair. I would like to split the IP and port, and assign them to two new keys though the logstash config file.

I looked into kv , but kv seems to require the keys and values pair in the string. Mine is only value.

Sample csv data:


In the above case, I already map the second field as "IP_Port" (using csv=> column) Now, I want to split as IP= and port=8080 (key=IP,value= and alike for port. Then mutate the datatype for the IP as 'ip datatype'.

After that, I can include the IP to geoip conversion using the following snippet, if I want to convert that extracted IP and use it to do geomapping.

geoip {
source => "Source IP"
target => "geoip"
database => "/etc/logstash/GeoLiteCity.dat"
add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}" ]
mutate {
convert => [ "[geoip][coordinates]", "float"]
Any suggestions on this?

(Christian Dahlqvist) #2

Use a csv filter to parse the record and get the ip and port into a single field. Then run another csv filter on the ip field, but with : used as a separator. You could use a grok or dissect filter as well.

(Aaron Mildenstein) #3

You don't need the add_field directives. The geoip plugin does all that for you. I would use a mapping rather than setting the field as a float with mutate.

(EatDataForBreakfast) #4

Thankyou for the suggestions All.
I found the grok option gave me precisely what I wanted. I could extract the IP (v4 or v6) using the below. I realized I dont need the port into a field, so this one worked realy faster in terms of processing as well.

filter {
grok {
match => { "message" => "%{IP:peer_ip}" }

(EatDataForBreakfast) #5

Thankyou Aaron. Figured it out the output mapping already had geo_point mapping. So it worked out well.

(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.