Splunk Search

Error in eval command for subsearch returning no results

systemjack
Explorer

I have a subsearch that may or may not return results on an hourly basis. I'm trying to capture the resulting value in an eval statement for later use, but the overall search errors out with "Error in 'eval' command: Failed to parse the provided arguments. Usage: eval dest_key = expression" when the subsearch returns no results. Otherwise things work fine.

The search is similar to:

index="foo" | eval [search index=hourly_summary `additional_filters` | head 100
| stats max(bean_count) as max_count | fields + max_count 
| format "" "" "AND" "" "OR" ""]
| `more_stuff_using_max_count`

What I'd really like to be able to do is populate max_count with a default value when the subsearch doesn't return results using fillnull or similar. Is there a way to force at least one default result?

Tags (2)
1 Solution

dwaddle
SplunkTrust
SplunkTrust

This is insanely ugly, but it appears to do what you want.

index="foo" | eval max_count=[ search index=hourly_summary `additional_filters` | head 100
| stats max(bean_count) as max_count | fields query 
| format "" "" "" "" "" "" 
| rex mode=sed field=search "s/NOT \(\)/default_value" ]
| `more_stuff_using_max_count`

Here is another option of which I am slightly less embarrassed:

index="foo" | eval [search index=hourly_summary `additional_filters` | head 100
| stats max(eval(coalesce(bean_count,0))) as max_count | fields + max_count 
| format "" "" "AND" "" "OR" ""]
| `more_stuff_using_max_count`

View solution in original post

dwaddle
SplunkTrust
SplunkTrust

This is insanely ugly, but it appears to do what you want.

index="foo" | eval max_count=[ search index=hourly_summary `additional_filters` | head 100
| stats max(bean_count) as max_count | fields query 
| format "" "" "" "" "" "" 
| rex mode=sed field=search "s/NOT \(\)/default_value" ]
| `more_stuff_using_max_count`

Here is another option of which I am slightly less embarrassed:

index="foo" | eval [search index=hourly_summary `additional_filters` | head 100
| stats max(eval(coalesce(bean_count,0))) as max_count | fields + max_count 
| format "" "" "AND" "" "OR" ""]
| `more_stuff_using_max_count`

carasso
Splunk Employee
Splunk Employee

forget the 'fields' or 'format' commands, just use 'return':

index="foo" 
| eval [search index=hourly_summary `additional_filters` 
                | head 100 
                | stats max(eval(coalesce(bean_count,0))) as max_count 
                | return max_count 
     ] 

you can test this with this search ("|stats count" just makes a dummy event):

| stats count | eval [| stats count | eval elvis=5 | return elvis]

systemjack
Explorer

It works! And much prettier than the ugly kludges I was trying that didn't work. I added a trailing slash to the regex and changed "as max_count" to "as query".

The second one didn't work for me. Probably because I simplified the search I posted here. Bean_count is actually based off a multi-value field created by sistats. I could probably get it working but I still don't grok stats and mvfields.

Thank you so much 🙂

Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...