Splunk Search

How to make search efficient with spath and regex

vjzone
Path Finder

How can I make this search efficient?

earliest=-1m source="/var/log/aws/opsworks/opsworks-agent.statistics.log"  host="*prod*" Reported statistics data
| dedup host
| rex field=_raw "Reported statistics data: (?<json>.*)\N"
| fields json, host
| spath input=json
| rename stats.memory.free as memFree, stats.memory.total as memtotal
| eval memFreePer=memFree/memtotal*100
| table host, memFreePer, stats.cpu.idle
0 Karma
1 Solution

woodcock
Esteemed Legend

There is not greater efficiency to be had other than to explicitly specify an index; here is that along with some other clarification adjustments:

index="YouShouldAlwaysSpecifyAnIndex" AND source="/var/log/aws/opsworks/opsworks-agent.statistics.log" AND host="prod" AND Reported AND statistics AND data
| dedup host
| rex "Reported statistics data: (?<json>.*)\N"
| fields json host
| spath input=json
| rename stats.memory.free AS memFree, stats.memory.total AS memtotal
| eval memFreePer = 100 * memFree / memtotal
| table host, memFreePer, stats.cpu.idle

View solution in original post

woodcock
Esteemed Legend

There is not greater efficiency to be had other than to explicitly specify an index; here is that along with some other clarification adjustments:

index="YouShouldAlwaysSpecifyAnIndex" AND source="/var/log/aws/opsworks/opsworks-agent.statistics.log" AND host="prod" AND Reported AND statistics AND data
| dedup host
| rex "Reported statistics data: (?<json>.*)\N"
| fields json host
| spath input=json
| rename stats.memory.free AS memFree, stats.memory.total AS memtotal
| eval memFreePer = 100 * memFree / memtotal
| table host, memFreePer, stats.cpu.idle

vjzone
Path Finder

Thanks Jpolvino. Unfortunately splunk is administered by another group and I'll have to raise a request for it.

0 Karma

jpolvino
Builder

Is it possible for stats.memory.free and stats.memory.total to be extracted as fields as the log is ingested? This would save you search-time overhead and the cost of some disk space and ingest overhead.

0 Karma

vjzone
Path Finder

Thanks woodcock. Every one of our logs has index=main, which is why i chose to ignore it. Apart from that, is the query alright?

0 Karma

woodcock
Esteemed Legend

Yes, efficiency-wise, there is nothing to do, but there some best-practices that provide additional clarity as shown in my answer. Is ALL of your data in index=main or just the data you need for this purpose? If the former, you should REALLY fix that. In any case, do not ever run a search without specifying index= somewhere.

0 Karma

vjzone
Path Finder

Yes every application log is with index=main. I have asked for a change and it'll hopefully happen soon.

Appreciate your response.

0 Karma

richgalloway
SplunkTrust
SplunkTrust

What makes you think it's inefficient? What does Job Inspector say?
Have you tried replacing 'Reported statistics data' with ' "Reported statistics data:" ' in the base search?

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

vjzone
Path Finder

Its not too bad. I was wondering if there was a better way to do this. Since it has both regex and spath and also an eval

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!

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...

Design, Compete, Win: Submit Your Best Splunk Dashboards for a .conf26 Pass

Hello Splunkers,  We’re excited to kick off a Splunk Dashboard contest! We know that dashboards are a primary ...

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...