Splunk Search

I'm trying to count the number of times a particular mvfield value occurs.

snandaku
Engager

Event data set is as follows:
{
"actions":["CREATE","DELETE", "MODIFY"],
"topic":"image",
"event_time":"2015-05-14T00:39:52Z",
}

I have tried the following:
source_type=<source> | mvexpand actions | stats count(eval(actions=="CREATE")) as "Number of File Creations"
but it doesn't work. What am I doing wrong? Any help would be much appreciated!

Tags (1)
0 Karma
1 Solution

martin_mueller
SplunkTrust
SplunkTrust

If your actions field is correctly extracted as an mv field then you can just search on it:

sourcetype=foo actions=CREATE | stats count as "Number of File Creations"

View solution in original post

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

If your actions field is correctly extracted as an mv field then you can just search on it:

sourcetype=foo actions=CREATE | stats count as "Number of File Creations"
0 Karma

martin_mueller
SplunkTrust
SplunkTrust

Make sure you're actually extracting fields from those JSON events.

0 Karma

snandaku
Engager

That was exactly the problem, thank you so much Martin! I used the interactive field extractor to extract the actions field, and it works now.

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

Searching for the term CREATE will work as long as no other fields contain that term.

What happens when you run this:

sourcetype=foo | table _time action

Do you get a column action? Does each row contain multiple values underneath each other or one long string?

0 Karma

woodcock
Esteemed Legend

He means this:

sourcetype=foo | table _time actions

snandaku
Engager

When I try sourcetype=foo | table _time actions

I get a column _time populated with timestamps and an empty column actions (just blank).

0 Karma

snandaku
Engager

Thanks Martin. Could you please explain what you mean by "if your actions field is correctly extracted as an mv field"? What's the correct way to extract an mv field?

While the above expression didn't work for me, I used something similar and successfully got the count of all events containing a CREATE action. This is what worked:

sourcetype=foo CREATE | stats count as "Number of File Creations"

For some reason it didn't like the actions=CREATE. Any idea why?

0 Karma

woodcock
Esteemed Legend

Try this:

source_type=source | eval lenmatch=length("CREATE") | eval len=length(actions) | eval copy=actions| rex field=copy mode=sed "s/CREATE//g" | eval lencopy=len(copy) | eval numValues= (len - lencopy) / lenmatch
0 Karma

martin_mueller
SplunkTrust
SplunkTrust

If actions is a multivalue field as specified in the question then treating it as a huge string is not worth bonus karma points 😛

0 Karma

woodcock
Esteemed Legend

I suppose it could be condensed to this:

source_type=source | eval len=length(actions) | eval copy=actions| rex field=copy mode=sed "s/CREATE//g" | eval numValues= (len - len(copy)) / 6
0 Karma

snandaku
Engager

Thanks for your reply woodcock! I tried this out but it didn't work for me. Perhaps what's missing is the stats count portion?

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...