Splunk Search

How to extract a variable number of fields?

hulahoop
Splunk Employee
Splunk Employee

Given the following data sample of 4 events where each event has a number immediately after the timestamp that indicates the number of hosts to be listed:

10/07/2013:09:00:00 3 "host1" "host2" "host3" foo
10/07/2013:09:01:00 2 "host1" "host2" bar
10/07/2013:09:02:00 4 "host1" "host2" "host3" "host4" baz
10/07/2013:09:03:00 1 "host1" foobarbaz

So in the first event, there are 3 hosts (host1, host2 and host3) and an unrelated field following the 3 host names (foo).

Is it possible to use rex or props/transforms to intelligently capture the number of hosts expected, then use that number to capture the following host names into a multi-value field, then just pick up where we leave off and extract the fields following the host parade? I guess I could write a custom search command to return all the fields sensibly, but am thinking there might be a clever way to do this inline.

_d_
Splunk Employee
Splunk Employee

Yes. This produces a multivalued host field from the search bar:

| stats count| eval _raw="10/07/2013:09:02:00 4 host1 host2 host3 host4 foo bar vi is great" | rex "00\s(?<var>\d+)(?<rest>.*)"| rex field=rest max_match=10 "(?<rest>\S+?) " | eval host=mvindex(rest, 0, var-1)

Put the first rex as an EXTRACT in props.conf, the second as a REPORT with MV_ADD=10 in props & transforms.conf and the last eval in props as EVAL-host. Make sure you change the first rex regex so that it's anchored properly in your data.

Edit: Change max_match to a sensible number, too.

d.

kphillipson
Path Finder

Nice! I've learned something as well!

0 Karma

kphillipson
Path Finder

The first part is easy with regex however I can't think of a way to do the second part in splunk they way you would like.

You can do an extraction for each host in "" as the same field. If they are in "".

First part:
rex ":\d+\s(?P\d+)\s"
Second part:
rex "\"(?P[^"]+)\""

0 Karma
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...