Splunk Search

How to perform subtraction on results?

brosselle
New Member

I'm currently using this in a search:
index=OS sourcetype=cpu | timechart avg(pctIdle) by host

This typically gives a result of around 96% for the host I'm running the query on. I want to subtract that from 100 to give a result of 4%. I know this should be simple, but I haven't got it to work yet. Any help is appreciated!

0 Karma
1 Solution

somesoni2
Revered Legend

So after you timechart command, you'll get column _time and one column for each host with name of the column being the host name (e.g. _time host1 host2 etc). How to do any operation on those fields, you'd need to use the exact field name (which is the host name), so you can either use the hardcoded name of the host like this (putting it in single quotes in the expression parts as it may have special chars)

index=OS sourcetype=cpu | timechart avg(pctIdle) by host
| eval "HardCodedHostName"=100-'HardCodedHostName'

OR if you want to do this for all host columns, you can use this foreach command (the <<FIELD>> to be used literally the way it's used here, no replacement required. see this for more details on foreach command)

index=OS sourcetype=cpu | timechart avg(pctIdle) by host
| foreach * [| eval "<<FIELD>>"=100-'<<FIELD>>']  [1]: 

View solution in original post

0 Karma

PowerPacked
Builder

Hi @brosselle

try this below query

index=OS sourcetype=cpu | timechart avg(pctIdle) by host |eval calc = (100 - 'hostfieldnamefrompreviuosresults') | fields calc hostfieldnamefrompreviuosresults

Thanks

0 Karma

somesoni2
Revered Legend

So after you timechart command, you'll get column _time and one column for each host with name of the column being the host name (e.g. _time host1 host2 etc). How to do any operation on those fields, you'd need to use the exact field name (which is the host name), so you can either use the hardcoded name of the host like this (putting it in single quotes in the expression parts as it may have special chars)

index=OS sourcetype=cpu | timechart avg(pctIdle) by host
| eval "HardCodedHostName"=100-'HardCodedHostName'

OR if you want to do this for all host columns, you can use this foreach command (the <<FIELD>> to be used literally the way it's used here, no replacement required. see this for more details on foreach command)

index=OS sourcetype=cpu | timechart avg(pctIdle) by host
| foreach * [| eval "<<FIELD>>"=100-'<<FIELD>>']  [1]: 
0 Karma

brosselle
New Member

Wow. I would have never come up with this:
| foreach * [| eval "<>"=100-'<>']

Worked perfectly. Thank you!

0 Karma
Get Updates on the Splunk Community!

Upcoming Webinar: Unmasking Insider Threats with Slunk Enterprise Security’s UEBA

Join us on Wed, Dec 10. at 10AM PST / 1PM EST for a live webinar and demo with Splunk experts! Discover how ...

.conf25 technical session recap of Observability for Gen AI: Monitoring LLM ...

If you’re unfamiliar, .conf is Splunk’s premier event where the Splunk community, customers, partners, and ...

A Season of Skills: New Splunk Courses to Light Up Your Learning Journey

There’s something special about this time of year—maybe it’s the glow of the holidays, maybe it’s the ...