Splunk Search

how to find difference between two "stats count" used in two different saved search

snabi
Explorer

So i have two saved search queries
1. sourcetype="x" "attempted" source="y" | stats count
2. sourcetype="x" "Failed" source="y" | stats count

i need to create a search query which will calculate

Passed item = (sourcetype="x" "attempted" source="y" | stats count) - (sourcetype="x" "Failed" source="y" | stats count)

and display Passed item count by hours

Tags (1)

sideview
SplunkTrust
SplunkTrust

sourcetype="x" source="y" (attempted OR failed) | eval isAttempted=if(searchmatch("attempted"),1,0) | eval isFailed=if(searchmatch("failed"),1,0) | stats sum(isAttempted) as attempts sum(isFailed) as failures | eval successes=attempts-failures

However I would examine the matching events for the "attempted" and "failed" searches very carefully to maked sure there aren't any false positives or negatives. Possibly this is already a boiled-down example. Note that you can have very complex boolean logic in the (attempted OR failed) side, and you can have the correspondingly complex boolean logic embedded into the searchmatch() functions so you'll be fine even if your actual case is more complex.

UPDATE:

If using eval syntax right in the stats command is more your style, that can be done too and it becomes a lot more compact:

sourcetype="x" source="y" (attempted OR failed) | stats count(eval(searchmatch("attempted"))) as attempts count(eval(searchmatch("failed"))) as failures | eval successes=attempts-failures

lukejadamec
Super Champion

It looks like you're trying to subtract apples from oranges, which cannot even be done on paper let alone splunk.
If you're trying to remove a subset of a set and the remaining items by hours, then you don't want to use stat counts. Just use a search that excludes them like this:
sourcetype="x" source="y" "attempted" | search NOT (sourcetype="x" source="y" "Failed") | stats count by _time,passeditem

Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...