4

I im newbie in splunk. I have this json:

"request": {
    "headers": [
        {
            "name": "x-real-ip",
            "value": "10.31.68.186"
        },
        {
            "name": "x-forwarded-for",
            "value": "10.31.68.186"
        },
        {
            "name": "x-nginx-proxy",
            "value": "true"
        }

I need to pick a value when the property name has "x-real-ip" value.

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
Igor Eulálio
  • 91
  • 1
  • 7

3 Answers3

4

There are a couple ways to do this - here's the one I use most often (presuming you also want the value along side the name):

index=ndx sourcetype=srctp request.headers{}.name="x-real-ip"
| eval combined=mvzip(request.headers{}.name,request.headers{}.value,"|")
| mvexpand combined
| search combined="x-real-ip*"

This skips all events that don't have "x-real-ip" somewhere in the request.headers{}.name multivalue field

Next, it combines the two multivalue fields (name & value) into a single mv field, separated by the | character

Then expand the resultset so you're looking at one line at a time

Finally, you look for only results that have the value "x-real-ip" in them

If you'd like to then extract the value from the combined field, add the following line:

| rex field-combined "\|(?<x_real_ip>.+)"

And, of course, you can do whatever other SPL operations on your data you wish

warren
  • 32,620
  • 21
  • 85
  • 124
0

I tried @Warren's answer but I got the following error:

Error in 'eval' command: The expression is malformed. Expected ).

You need to add a rename because the {} charcters in mvzip causes problems. This is the query that works:

index=ndx sourcetype=srctp request.headers{}.name="x-real-ip"
| rename request.headers{}.name AS headerName, request.headers{}.value AS headerValue 
| eval reviewers=mvzip(headerName,headerValue ,"|")
| mvexpand reviewers
| search reviewers="x-real-ip*"
Sarneet Kaur
  • 2,860
  • 1
  • 14
  • 12
Rafi
  • 2,433
  • 1
  • 25
  • 33
-2
your search
| rex max_match=0 "name\":\s\"(?<fieldname>[^\"]+)"
| rex max_match=0 "value\":\s\"(?<fieldvalue>[^\"]+)"
| eval tmp=mvzip(fieldname,fieldvalue,"=")
| rename tmp as _raw
| kv
| fields - _* field*

When you ask a question, please present the correct information. You've run out of logs in the process.

  • 1
    why are you rex'ing out fieldnames when Splunk has already parsed the JSON into its requisite key-value pairs? – warren May 11 '20 at 12:48
  • hi @warren `when Splunk has already parsed the JSON` Really? With this questioner's information, we don't know that. That's why I used `rex`. – Toshihisa Kawamata May 12 '20 at 00:42
  • 1
    yes, we do know that. Splunk parses JSON when it receives it: it's a native data format :) – warren May 12 '20 at 12:21