Splunk Search

How to return a different field from mvfilter?

psimoes
Loves-to-Learn

Hello,

I have the following example json data:

 

 

 

spec: {
	field1: X,
	field2: Y,
	field3: Z,
	containers: [
		{
			name: A
			privileged: true
		},
		{
			name: B
		},
		{
			name: C
			privileged: true
		}
	]	
}

 

 

 

I'm trying to write a query that only returns privileged containers. I've been trying to use mvfilter but that won't return the name of the container. Here's what I was trying to do:

 

 

 

index=MyIndex spec.containers{}.privileged=true
| eval priv_containers=mvfilter(match('spec.containers{}.privileged',"true"))
| stats values(priv_containers) count by field1, field2, field3

 

 

 

This will, however, just return "true" in the priv_containers values column, instead of the container's name. What would be the best way to accomplish that?

Labels (5)
Tags (2)
0 Karma

yuanliu
SplunkTrust
SplunkTrust

JSON array must first be converted to multivalue before you can use mv-functions.  The classic method to do this is mvexpand together with spath.

 

| spath input=spec path=spec.containers{}
| mvexpand spec.containers{}
| spath input=spec.containers{}
| where privileged == "true"

 

With your sample data, output is like

nameprivilegedspec.containers{}spec.field1spec.field2spec.field3
Atrue{ "name": "A", "privileged": "true" }XYZ
Ctrue{ "name": "C", "privileged": "true" }XYZ

If the array is big and events are many, mvexpand risk running out of memory.  So, Splunk 8 introduced a group of JSON functions.  The following is more memory efficient (and likely more efficient in general), but the output is multivalued.

 

| spath input=spec path=spec.containers{}
| fields - spec.containers{}.*
| eval privileged_containers = mvfilter(json_extract('spec.containers{}', "privileged") == "true")

 

Your sample data would give something like

privileged_containers
spec.containers{}
spec.field1spec.field2spec.field3
{ "name": "A", "privileged": "true" }
{ "name": "C", "privileged": "true" }
{ "name": "A", "privileged": "true" }
{ "name": "B" }
{ "name": "C", "privileged": "true" }
XYZ

 

BTW, please post JSON in raw text and make sure the format is compliant with proper quotes, etc. so volunteers don't have to waste time reconstruct data or worry about noncompliant data.  Here is a compliant emulation that you can play with and compare with real data

 

| makeresults
| eval _raw = "{\"spec\": {
        \"field1\": \"X\",
        \"field2\": \"Y\",
        \"field3\": \"Z\",
        \"containers\": [
                {
                        \"name\": \"A\",
                        \"privileged\": \"true\"
                },
                {
                        \"name\": \"B\"
                },
                {
                        \"name\": \"C\",
                        \"privileged\": \"true\"
                }
        ]
}}"
| spath
``` data emulation above ```

 

Hope this helps.

Tags (3)
0 Karma
Get Updates on the Splunk Community!

Registration for Splunk University is Now Open!

Are you ready for an adventure in learning?   Brace yourselves because Splunk University is back, and it's ...

Splunkbase | Splunk Dashboard Examples App for SimpleXML End of Life

The Splunk Dashboard Examples App for SimpleXML will reach end of support on Dec 19, 2024, after which no new ...

Understanding Generative AI Techniques and Their Application in Cybersecurity

Watch On-Demand Artificial intelligence is the talk of the town nowadays, with industries of all kinds ...