Splunk Search

How to group events by host name and display the most recent "Message" field value from each group?

ardave
Explorer

If I have fields such as:

  • _time = timestamp
  • host = the host name
  • Message = either "up" or "down"

How do I group by the host name and then select the most recent Message from each group?

I've figured out
| stats last(_time) as _time by host
but the Messages are not unique within each group, so I need to figure out how to retrieve only the latest Message to be included with each group/record in my search results.

Thank you!

Tags (4)
1 Solution

acharlieh
Influencer

You could use latest(Message) or last(Message) as an additional field in your stats command (e.g. | stats last(_time) as _time last(Message) as Message by host ). There is a good reference for Functions for stats in the docs.

Depending on your ultimate goal and what your input data looks like, if you're only interested in the last event for each host, you could also make use of the dedup command instead. Something like: | dedup host

View solution in original post

acharlieh
Influencer

You could use latest(Message) or last(Message) as an additional field in your stats command (e.g. | stats last(_time) as _time last(Message) as Message by host ). There is a good reference for Functions for stats in the docs.

Depending on your ultimate goal and what your input data looks like, if you're only interested in the last event for each host, you could also make use of the dedup command instead. Something like: | dedup host

ardave
Explorer

That does the trick. I lacked the intuition to figure out how to include multiple fields using last(). I had tried

last(fieldA, fieldB)

and

| last(fieldA) | last(fieldB)

but had not tried the way you recommended, which works. Thanks!

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...