Splunk Search

help on appendcols subsearch

jip31
Motivator

Hello

I use the search below in order to calculate a volume percentage

 

 

| inputlookup host.csv 
| lookup lookup_patch "Computer" as host output FileName, StateName
| search StateName="Non-Compl" 
| stats dc(host) as host by StateName FileName 
| stats sum(host) as NbNonCompliantPatchesIndHost
| appendcols 
 [| inputlookup host.csv  
| stats dc(host) as NbIndHost] 
| eval Perc=round((NbNonCompliantPatchesIndHost/NbIndHost)*100,2)
| table Perc, NbIndHost, NbNonCompliantPatchesIndHost

 

 

 No I need to calculate the volume percentage by SITE

So I add a clause "by SITE" in my stats command but it's wrong because sometimes the percentage is over 100% because NbNonCompliantPatchesIndHost is > NbIndHost

What is the solution to do this please?

 

 

| inputlookup host.csv 
| lookup lookup_patch "Computer" as host output FileName, StateName
| lookup fo_all HOSTNAME as host output SITE 
| search StateName="Non-Compl" 
| stats dc(host) as host by StateName FileName SITE
| stats sum(host) as NbNonCompliantPatchesIndHost by SITE
| appendcols 
 [| inputlookup host.csv 
| lookup fo_all HOSTNAME as host output SITE 
| stats dc(host) as NbIndHost by SITE] 
| eval Perc=round((NbNonCompliantPatchesIndHost/NbIndHost)*100,2)
| table SITE Perc, NbIndHost, NbNonCompliantPatchesIndHost

 

 

 

 

Tags (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

One factor to consider is how appendcols works.

In the first query, each subsearch returned a single result so appending one to the other worked well.

In the second query, each subsearch returns multiple results which may not be the same.  If each list of sites is not identical, then appending one to the other will produce nonsense.

To prevent this, use append instead of appendcols then use stats to combine to two results.

| inputlookup host.csv 
| lookup lookup_patch "Computer" as host output FileName, StateName
| lookup fo_all HOSTNAME as host output SITE 
| search StateName="Non-Compl" 
| stats dc(host) as host by StateName FileName SITE
| stats sum(host) as NbNonCompliantPatchesIndHost by SITE
| append
 [| inputlookup host.csv 
  | lookup fo_all HOSTNAME as host output SITE 
  | stats dc(host) as NbIndHost by SITE] 
| stats values(*) as * by SITE
| eval Perc=round((NbNonCompliantPatchesIndHost/NbIndHost)*100,2)
| table SITE Perc, NbIndHost, NbNonCompliantPatchesIndHost

Note that this may not solve the problem, but will eliminate one possible source of error.

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

View solution in original post

0 Karma

richgalloway
SplunkTrust
SplunkTrust

One factor to consider is how appendcols works.

In the first query, each subsearch returned a single result so appending one to the other worked well.

In the second query, each subsearch returns multiple results which may not be the same.  If each list of sites is not identical, then appending one to the other will produce nonsense.

To prevent this, use append instead of appendcols then use stats to combine to two results.

| inputlookup host.csv 
| lookup lookup_patch "Computer" as host output FileName, StateName
| lookup fo_all HOSTNAME as host output SITE 
| search StateName="Non-Compl" 
| stats dc(host) as host by StateName FileName SITE
| stats sum(host) as NbNonCompliantPatchesIndHost by SITE
| append
 [| inputlookup host.csv 
  | lookup fo_all HOSTNAME as host output SITE 
  | stats dc(host) as NbIndHost by SITE] 
| stats values(*) as * by SITE
| eval Perc=round((NbNonCompliantPatchesIndHost/NbIndHost)*100,2)
| table SITE Perc, NbIndHost, NbNonCompliantPatchesIndHost

Note that this may not solve the problem, but will eliminate one possible source of error.

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

jip31
Motivator

Thanks it works but I have another question

I filter the results of my search from a SITE dropdown list in order to display the volume percentage for a SITE

I added the token filters in the example below

But with your "stats values" command all the SITE are displayed. How to display only the choice done from the dropdown list please?

| inputlookup host.csv 
| lookup lookup_patch "Computer" as host output FileName, StateName
| lookup fo_all HOSTNAME as host output SITE 
| search StateName="Non-Compl" | search SITE=$tok_filtersite$
| stats dc(host) as host by StateName FileName SITE
| stats sum(host) as NbNonCompliantPatchesIndHost by SITE
| append
 [| inputlookup host.csv 
  | lookup fo_all HOSTNAME as host output SITE 
| search SITE=$tok_filtersite$
  | stats dc(host) as NbIndHost by SITE] 
| stats values(*) as * by SITE
| eval Perc=round((NbNonCompliantPatchesIndHost/NbIndHost)*100,2)
| table SITE Perc, NbIndHost, NbNonCompliantPatchesIndHost

 

 

0 Karma

jip31
Motivator

perfect rich!

many thanks to you

0 Karma
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!

ATTENTION: We’re Moving! (AGAIN!)

The Splunk Community Slack is undergoing a system migration to keep our workspace secure and ...

Deep Dive: Optimizing Telemetry Pipelines in Splunk Observability Cloud

In this session, we will peel back the layers of Splunk Observability Cloud’s cost-optimization features. ...

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 ...