Splunk Enterprise

Subsearch to store value

jam90
Engager

Hello, 

I am running two separate queries to extract values:

First query

 

index=abc status=error | stats count AS FailCount

 

Second query

 

index=abc status=planning | stats count AS TotalPlanned

 

Both queries are working well and giving expected results. 

When I combine them using sub search, I am getting error:

 

index=abc status=error
| stats count AS FailCount
[ search index=abc status=planning
| stats count AS TotalPlanned
| table TotalPlanned ]
| eval percentageFailed=(FailCount/TotalPlanned)*100 

 

Error message:

 

Error in 'stats' command: The argument '(( TotalPlanned=761 )) is invalid'

 

Note: The count 761 is a valid count for TotalPlanned, so it did perform that calculation. 

Labels (1)
Tags (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

It may help to think of a subsearch like a macro.  Just as the contents of a macro replace the macro name in a query, so, too, do the results of a subsearch replace the subsearch text in the query.  Therefore, it's important that the results of the subsearch make sense, semantically.

In the example query, once the subsearch completes, Splunk tries to execute this

index=abc status=error
| stats count AS FailCount
(( TotalPlanned=761 ))
| eval percentageFailed=(FailCount/TotalPlanned)*100 

which is not a valid query.

One fix is to use the appendcols command with the subsearch

index=abc status=error
| stats count AS FailCount
| appendcols [ search index=abc status=planning
  | stats count AS TotalPlanned
  | table TotalPlanned ]
| eval percentageFailed=(FailCount/TotalPlanned)*100 

 

---
If this reply helps you, Karma would be appreciated.

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| stats count(eval(status="error")) AS FailCount count(eval(status="planning")) AS TotalPlanned
| eval percentageFailed=(FailCount/TotalPlanned)*10

richgalloway
SplunkTrust
SplunkTrust

It may help to think of a subsearch like a macro.  Just as the contents of a macro replace the macro name in a query, so, too, do the results of a subsearch replace the subsearch text in the query.  Therefore, it's important that the results of the subsearch make sense, semantically.

In the example query, once the subsearch completes, Splunk tries to execute this

index=abc status=error
| stats count AS FailCount
(( TotalPlanned=761 ))
| eval percentageFailed=(FailCount/TotalPlanned)*100 

which is not a valid query.

One fix is to use the appendcols command with the subsearch

index=abc status=error
| stats count AS FailCount
| appendcols [ search index=abc status=planning
  | stats count AS TotalPlanned
  | table TotalPlanned ]
| eval percentageFailed=(FailCount/TotalPlanned)*100 

 

---
If this reply helps you, Karma would be appreciated.
Get Updates on the Splunk Community!

Earn a $35 Gift Card for Answering our Splunk Admins & App Developer Survey

Survey for Splunk Admins and App Developers is open now! | Earn a $35 gift card!      Hello there,  Splunk ...

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...