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!

Get ready to show some Splunk Certification swagger at .conf24!

Dive into the deep end of data by earning a Splunk Certification at .conf24. We're enticing you again this ...

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Now On-Demand Join us to learn more about how you can leverage Service Level Objectives (SLOs) and the new ...

Database Performance Sidebar Panel Now on APM Database Query Performance & Service ...

We’ve streamlined the troubleshooting experience for database-related service issues by adding a database ...