Splunk Search

Return 0 when "no results found"

moizmmz
Path Finder

Hello,

In the following query, I'm hoping to return the value 0 to my dashboard panel if no results are found by the search query.

index=test "slice_played" externalUserID="$ext$" assetID!="806d682119ac46d18b9f4a5f3dc20b10"
assetID!="5c117f3141244a3a9d6899395b5c65aa" $ass$
| stats sum(duration) as "y_seconds"

Please help! I've tried using the solution asked in a similar question, but to no avail.

Tags (1)
0 Karma

woodcock
Esteemed Legend

Add this to the bottom of your search SPL string:

| appendpipe [stats count | where count=0]

You can thank @martin_mueller for this!

MuS
Legend

Hi moizmmz,

need to trick Splunk for this, because if there are no events Splunk will show No results found. So you need to add fake events and do some logic around. Try this run everywhere search:

index=_internal sourcetype!=splunkd_* baz
| append 
    [| makeresults 
    | stats count(foo) AS count 
    | eval _raw="Nothing to see here, move along!"] 
| streamstats count AS line_num 
| eval head_num=if((line_num > 1),(line_num - 1),1) 
| where (true() XOR ((count == 0) AND (head_num < line_num))) 
| fields - column, count, head_num, line_num 
| sort -_time | table *

The benefit of this search is that if there are no events it will only show one column with the message 😉

Hope this helps to get you started ...

cheers, MuS

PS: another example can be found here https://answers.splunk.com/answers/704513/how-do-i-show-0-when-no-results-are-found-in-a-rep.html

Vijeta
Influencer

@moizmmz- Try below

index=test "slice_played" externalUserID="$ext$" assetID!="806d682119ac46d18b9f4a5f3dc20b10" assetID!="5c117f3141244a3a9d6899395b5c65aa" $ass$ | append [|makeresults| eval duration=0]|stats sum(duration) as "y_seconds"
0 Karma

koshyk
Super Champion

Please try

<your search>..
| fillnull value=0 duration 
| stats sum(duration)  as "y_seconds"
0 Karma

moizmmz
Path Finder

Tried this. Doesn't Work 😞

0 Karma

koshyk
Super Champion

please provide some sample data and final output how you want to look like and we could write it for u

0 Karma

moizmmz
Path Finder

Thats the thing though. For now, it returns "No results found". So instead of seeing "No results found" in my dashboard panel, I want to see 0 !!

0 Karma

koshyk
Super Champion

strange. what i've done is, if the value is empty, put the value to 0. So it will sum up 0 and should show 0. But anyways

0 Karma

moizmmz
Path Finder

Yeah!!! that's exactly what I thought!

0 Karma

moizmmz
Path Finder

Thanks though 🙂

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
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, ...