Splunk Search

How to edit my search to get new errors from today, and that not occurred in last 7 days?

manjulanam
New Member

Can you please help with the following search? It returns 0 events. I want all the errors that occurred today, and not in last 7days

sourcetype="xx" source="err.log" error earliest=-1d@d  
| stats count AS thisweek by _Error  
| appendcols [ search  source="err.log" error earliest=-7d@d latest=-1d@d
| stats count AS lastweek by _Error]
| where  !isnum(lastweek)
| table thisweek lastweek _Error

Thank you!

0 Karma
1 Solution

lguinn2
Legend

I wouldn't use appendcols for a start. Use append instead, and a final stats to combine the two

sourcetype="xx" source="err.log" error earliest=-1d@d  
| stats count AS thisweek by _Error  
| append [ search  source="err.log" error earliest=-7d@d latest=-1d@d
      | stats count AS lastweek by _Error]
| stats first(*) as * by _Error
| where thisweek > 0 and lastweek = 0

You could also do this with no sub-searches at all, which may be necessary if you are working with very large data sets:

sourcetype="xx" source="err.log" error earliest=-7d@d  
| eval category = if(_time < relative_time(now(),"-1d@d"),"last week","this week")
| stats count(eval(category=="this week")) AS thisweek count(eval(category=="last week")) AS lastweek by _Error  
| where thisweek > 0 and lastweek = 0

View solution in original post

0 Karma

lguinn2
Legend

I wouldn't use appendcols for a start. Use append instead, and a final stats to combine the two

sourcetype="xx" source="err.log" error earliest=-1d@d  
| stats count AS thisweek by _Error  
| append [ search  source="err.log" error earliest=-7d@d latest=-1d@d
      | stats count AS lastweek by _Error]
| stats first(*) as * by _Error
| where thisweek > 0 and lastweek = 0

You could also do this with no sub-searches at all, which may be necessary if you are working with very large data sets:

sourcetype="xx" source="err.log" error earliest=-7d@d  
| eval category = if(_time < relative_time(now(),"-1d@d"),"last week","this week")
| stats count(eval(category=="this week")) AS thisweek count(eval(category=="last week")) AS lastweek by _Error  
| where thisweek > 0 and lastweek = 0
0 Karma

manjulanam
New Member

Thank you Iguinn!, I tried both of the search queries you recommended, but still get No results found.
The original search I posted, returns results if the lastweek query is within last 2 days, but for 7days it almost seems like finalizing results and returns no results

0 Karma

manjulanam
New Member

I used your query with no subsearches replaced or with following and it worked!!
Thank you very much, really appreciate your help!!

| table thisweek lastweek _Error
|where lastweek = 0

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...