Splunk Search

How to plot a chart for the closing stock price on each business day from my JSON data?

ramavadde
New Member

Basic search source="outdb.json" sym=TSCO.L returns the below records:

Time    Event
01/08/2014 
00:00:00.000    
{ [-] 
    _id: { [+] 
   } 
    ac:   251.90 
    c:   253.60 
    d:  01/08/2014 
    dv:  0 
    h:   258.47 
    l:   251.85 
    o:   258.00 
    sym:  TSCO.L 
    v:   34272100 
}

Show as raw text:

c = 253.60 d = 01/08/2014 host = 127.0.0.1 source = outdb.json sourcetype = _json

Actual input:

{"_id":{"$oid":"5528ec0ea231b60ffc1cdb15"},"ac":" 251.90","c":" 253.60","d":"01/08/2014","dv":0,"h":" 258.47","l":" 251.85","o":" 258.00","sym":"TSCO.L","v":" 34272100"}

I want to plot a graph for closing price "c":" 253.60" with c on y-axis and _time or d on x-axis.

0 Karma
1 Solution

cpetterborg
SplunkTrust
SplunkTrust

Can you change the data going into splunk to be a JSON string only (with the date in the JSON string)? This will make the parsing of the data automatic, and then you can just get the values for each event without trying. Then with that, the closing values (c) can be plotted most easily with something like:

source="outdb.json" sym=TSCO.L | timechart span=1m avg(c)

Perhaps you want the min or max, then use min(c) or max(c) instead of avg(c).

If you can't change the data coming in to a pure JSON string, then use the following to extract the closing value:

source="outdb.json" sym=TSCO.L | rex field=_raw ",\"c\":\"(?P<closing>\d*\.\d*>\"" | timechart span=1m avg(closing)

You may wish to change the span in the timechart to something else if you don't want 1 minute buckets for the graph.

View solution in original post

0 Karma

cpetterborg
SplunkTrust
SplunkTrust

Can you change the data going into splunk to be a JSON string only (with the date in the JSON string)? This will make the parsing of the data automatic, and then you can just get the values for each event without trying. Then with that, the closing values (c) can be plotted most easily with something like:

source="outdb.json" sym=TSCO.L | timechart span=1m avg(c)

Perhaps you want the min or max, then use min(c) or max(c) instead of avg(c).

If you can't change the data coming in to a pure JSON string, then use the following to extract the closing value:

source="outdb.json" sym=TSCO.L | rex field=_raw ",\"c\":\"(?P<closing>\d*\.\d*>\"" | timechart span=1m avg(closing)

You may wish to change the span in the timechart to something else if you don't want 1 minute buckets for the graph.

0 Karma

ramavadde
New Member

Uploaded the data in pure JSON format and below is the record.

{"ac":" 251.90","c":" 253.60","d":"01/08/2014","dv":0,"h":" 258.47","l":" 251.85","o":" 258.00","sym":"TSCO.L","v":" 34272100"}

01/08/2014  { [-] 
00:00:00.000    ac:  251.90 
   c:  253.60 
   d: 01/08/2014 
   dv: 0 
   h:  258.47 
   l:  251.85 
   o:  258.00 
   sym: TSCO.L 
   v:  34272100 
}
Show as raw text c = 253.60 d = 01/08/2014 host = 127.0.0.1 source = outdb.json sourcetype = _json

source="outdb.json" sourcetype="_json" sym=TSCO.L|timechart span=1m avg© - _time returned values but not the avg©.

0 Karma

ramavadde
New Member

Before uploading into Splunk:
{"ac":" 281.70","c":" 293.40","d":"01/04/2014","dv":0,"h":" 296.54","l":" 291.86","o":" 296.05","sym":"TSCO.L","v":" 22471300"}
{"ac":" 283.62","c":" 295.40","d":"31/03/2014","dv":0,"h":" 300.30","l":" 294.95","o":" 299.20","sym":"TSCO.L","v":" 18308200"}
{"ac":" 285.93","c":" 297.80","d":"28/03/2014","dv":0,"h":" 298.85","l":" 295.14","o":" 295.40","sym":"TSCO.L","v":" 20253800"}
{"ac":" 281.65","c":" 293.35","d":"27/03/2014","dv":0,"h":" 294.85","l":" 291.90","o":" 294.35","sym":"TSCO.L","v":" 48372300"}
{"ac":" 282.62","c":" 294.35","d":"26/03/2014","dv":0,"h":" 298.35","l":" 293.90","o":" 295.40","sym":"TSCO.L","v":" 23528000"}
{"ac":" 281.99","c":" 293.70","d":"25/03/2014","dv":0,"h":" 296.29","l":" 291.70","o":" 293.65","sym":"TSCO.L","v":" 26760800"}
{"ac":" 281.65","c":" 293.35","d":"24/03/2014","dv":0,"h":" 295.25","l":" 291.36","o":" 291.70","sym":"TSCO.L","v":" 40970500"}
{"ac":" 279.30","c":" 290.90","d":"21/03/2014","dv":0,"h":" 294.35","l":" 289.25","o":" 292.50","sym":"TSCO.L","v":" 78318900"}
{"ac":" 280.84","c":" 292.50","d":"20/03/2014","dv":0,"h":" 295.50","l":" 288.12","o":" 292.30","sym":"TSCO.L","v":" 24065300"}
{"ac":" 282.86","c":" 294.60","d":"19/03/2014","dv":0,"h":" 302.20","l":" 294.25","o":" 299.45","sym":"TSCO.L","v":" 30891300"}
{"ac":" 288.28","c":" 300.25","d":"18/03/2014","dv":0,"h":" 304.40","l":" 298.30","o":" 298.60","sym":"TSCO.L","v":" 25514700"}

0 Karma

ramdaspr
Contributor
... |rex field=_raw "\"c\"\:\"\s+(?<val>\d+\.+\d{2})\"" | rex field=_raw "d\":\"(?<time>\S+)\"\,"| eval _time = strptime(time,"%d/%m/%Y")||chart max(val) by _time
0 Karma

ramavadde
New Member

After removing the extra pipe before chart command, it is working. Thank you all for your prompt response to help with this. Obviously I have to practice my rex skills.

0 Karma

cpetterborg
SplunkTrust
SplunkTrust

Can you send about 10 sample lines of raw data?

0 Karma

ramavadde
New Member

I have tried the following:
source="outdb.json" sym=TSCO.L | rex field=_raw ",\"c\":\"(?P\d*.\d*>\"" | timechart span=1m avg(closing)

Output - Error in 'rex' command: Encountered the following error while compiling the regex ',"c":"(?P\d*.\d*>"': Regex: missing )

then tried - source="outdb.json" sym=TSCO.L | rex field=_raw ",\"c\":\"(?P\d*.\d*>\")" | timechart span=1m avg(closing)

output - No results found

I will try and remove the mongodb _id and ObjectId fields from the source and upload into splunk.

Thanks for your help.

0 Karma

ramavadde
New Member

Just found the datatype of the value of "c" is string. I have tried the following but no luck.
source="outdb.json" sym=TSCO.L|stats values(c) as Price|eval Price = tonumber(Price)|table Price is not printing any thing. However it is fine upto and including stats.

0 Karma
Get Updates on the Splunk Community!

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...