Splunk Search

How to write a search where if a certain string is found in a log, set Status=1, otherwise Status=0?

svivekananda007
Engager

I need to find a string in a log and set/unset a field depending on this.
Ex: field Status = 1 or 0.
I should say if(a_log_event contains "connected") then Status=1, otherwise 0

Please help me with this

THanks

Tags (4)
1 Solution

woodcock
Esteemed Legend

Like this:

... | eval Status=if(like(_raw, "%connected%"), 1, 0)

View solution in original post

woodcock
Esteemed Legend

If you are more used to Splunk SPL search syntax, you could do it like this:

 ... | eval Status=if(searchmatch("*connected*"), 1, 0)

woodcock
Esteemed Legend

Like this:

... | eval Status=if(like(_raw, "%connected%"), 1, 0)

timmag
Explorer

Say suppose, I get those logs every minute. Is there a way where I can create a field where if I get successive '0' in status(More than once), the field would display the status as error?

0 Karma

Nextbeat
Path Finder

| bucket _time span=1m | stats count(eval(like(<field>, "<status%>"))) AS count BY _time | eval <new_field>=if(count > 1, "error", "")

Use the bucket function to view events per minute. Then use stats to count a desired field by a value using the percent sign as a wildcard. The second eval statement creates a new field and looks for counts greater than one. If there are any counts greater than one, "error" will be displayed for that event within the new field. Otherwise, nothing will be displayed for the new field.

0 Karma

Nextbeat
Path Finder

This definitely works. It's good to know that % acts like a wildcard for eval statements in Splunk.

0 Karma

svivekananda007
Engager

THis worked. thanks. can you suggest me a way to keep Status=1 until "disconnected" is encountered in a log.

Thanks

0 Karma

garigis
Engager

Thanks for this! I've been trying to figure this out for about an hour and tried a bunch of other stuff.

0 Karma

woodcock
Esteemed Legend

Be sure to UpVote!

kristian_kolb
Ultra Champion

Define what you mean by "keep"? This evaluation creates a new field on a per-event basis. It is not keeping a state. Remember that a log searching tool is not necessarily the best way for finding out a state, because for whatever timerange you search, you might always miss that important piece of state information that was logged 5 minutes before your search time span...

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: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

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

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