Splunk Search

Most Recent record with a specific field value

adsFab
Engager

Hi,

I am try to get the most recent value and search for specific status

item itemdesc _time status
ITEM01 COKE 2021-09-21 22:00:05 FAILED
ITEM01 COKE 2021-09-20 13:00:15 FAILED

ITEM02 COKE 2021-09-21 21:00:12 PASSED
ITEM02 COKE 2021-09-21 20:00:05 PASSED
ITEM02 COKE 2021-09-21 19:00:05 FAILED

ITEM03 COKE 2021-09-20 12:00:05 FAILED
ITEM03 COKE 2021-09-19 11:00:15 PASSED

Need to check most recent status by item, and pull only if status = Failed

O/p
ITEM01 COKE 2021-09-21 22:00:05 FAILED
ITEM03 COKE 2021-09-20 12:00:05 FAILED

In this case ITEM02 is ignored since most recent status is PASSED

 

Labels (2)
0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

@adsFab 

Paste this search to the window, which shows you how to do this

| makeresults
| eval _raw="item itemdesc _time status
ITEM01 COKE 2021-09-21T22:00:05 FAILED
ITEM01 COKE 2021-09-20T13:00:15 FAILED
ITEM02 COKE 2021-09-21T21:00:12 PASSED
ITEM02 COKE 2021-09-21T20:00:05 PASSED
ITEM02 COKE 2021-09-21T19:00:05 FAILED
ITEM03 COKE 2021-09-20T12:00:05 FAILED
ITEM03 COKE 2021-09-19T11:00:15 PASSED"
| multikv forceheader=1
| eval _time=strptime(time, "%FT%T")
| table _time item itemdesc status
| stats latest(status) as status latest(_time) as _time by item itemdesc
| where status="FAILED"

The first part up to 'table...' is setting up your data. Just then use stats latest() to get the last status and time for each item and then just filter on the FAILED status.

Hope this helps.

 

View solution in original post

bowesmana
SplunkTrust
SplunkTrust

@adsFab 

Paste this search to the window, which shows you how to do this

| makeresults
| eval _raw="item itemdesc _time status
ITEM01 COKE 2021-09-21T22:00:05 FAILED
ITEM01 COKE 2021-09-20T13:00:15 FAILED
ITEM02 COKE 2021-09-21T21:00:12 PASSED
ITEM02 COKE 2021-09-21T20:00:05 PASSED
ITEM02 COKE 2021-09-21T19:00:05 FAILED
ITEM03 COKE 2021-09-20T12:00:05 FAILED
ITEM03 COKE 2021-09-19T11:00:15 PASSED"
| multikv forceheader=1
| eval _time=strptime(time, "%FT%T")
| table _time item itemdesc status
| stats latest(status) as status latest(_time) as _time by item itemdesc
| where status="FAILED"

The first part up to 'table...' is setting up your data. Just then use stats latest() to get the last status and time for each item and then just filter on the FAILED status.

Hope this helps.

 

adsFab
Engager

Thank you so much.  @bowesmana Your solution worked

0 Karma

ashvinpandey
Contributor

@adsFab Try using the below command after your index and sourcetype:

| search status="FAILED"
| stats latest(status) as status latest(_time) as time by item
| convert ctime(time)

Also, If this reply helps you, an upvote would be appreciated.

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...