Splunk Search

Different results when search is run in web UI then in python script.

tbeason
Engager

When I run this search in the Web UI I get the correct results.  When it is run in a python script the "count(eval(RequestTime<2.00)) as PlaybackNumSuccessful" returns 0 when it should not.

 

 

search index=cdvr host=* AND source="/var/log/nginx/access.log" AND sourcetype="gemini-ecdn-nginx-access"
| rex field=_raw ".*?\t.*?\t.*?\t.*?\t(?<Method>\w+)\s/(?<URI>.+?)\sHTTP.+?\t.*?\t(?<Status>.+?)\t.*?\t.*?\t.*?\t.*?\s.*?\t.*?\t(?<host_header>.+?)\t"
        | rex field=URI "(?<RecordingID>.*)\.(?<resource>.*)?\?.*"
        | dedup RecordingID
        | search Method=GET resource="m3u8"
        | stats
        count(eval(RequestTime<2.00)) as PlaybackNumSuccessful
        count(eval(RecordingID)) as PlaybackNumTotal
        | eval PlaybackNumFailed=(PlaybackNumTotal-PlaybackNumSuccessful)
        | eval SuccessPer = (PlaybackNumSuccessful/PlaybackNumTotal)*100
        | eval PlaybackLatencyLessThan2SecSuccessRate=round(SuccessPer, 3)."%"
        | fields PlaybackNumTotal PlaybackNumFailed PlaybackLatencyLessThan2SecSuccessRate

 

 

Any ideas why?

Labels (2)
Tags (1)
0 Karma

DalJeanis
Legend

There are a number of things I'd check.  Python is finicky about indentation, so I'd probably write a python script with a cut-back version of the SPL to make sure what the values are immediately before the stats command, if none of the following fix it.

Here are some ideas - 

1) Run the stats together on one line.

2) Put quotes around GET

3) Use tonumber to force RequestTime to be a number, in case for some reason it is being evaluated as a string.

4) Make sure that all lines are at the same indentation.

5) Add commas between each clause in the stats line.

 

 

search index=cdvr host=* AND source="/var/log/nginx/access.log" AND sourcetype="gemini-ecdn-nginx-access"
| rex field=_raw ".*?\t.*?\t.*?\t.*?\t(?<Method>\w+)\s/(?<URI>.+?)\sHTTP.+?\t.*?\t(?<Status>.+?)\t.*?\t.*?\t.*?\t.*?\s.*?\t.*?\t(?<host_header>.+?)\t"
        | rex field=URI "(?<RecordingID>.*)\.(?<resource>.*)?\?.*"
        | dedup RecordingID
        | search Method="GET" resource="m3u8"
        | stats count(eval(tonumber(RequestTime)<2.00)) as PlaybackNumSuccessful count(eval(RecordingID)) as PlaybackNumTotal
        | eval PlaybackNumFailed=(PlaybackNumTotal-PlaybackNumSuccessful)
        | eval SuccessPer = (PlaybackNumSuccessful/PlaybackNumTotal)*100
        | eval PlaybackLatencyLessThan2SecSuccessRate=round(SuccessPer, 3)."%"
        | fields PlaybackNumTotal PlaybackNumFailed PlaybackLatencyLessThan2SecSuccessRate

 

 

 

There's one further thing to try ...

 

 

        | stats sum(eval(case(tonumber(RequestTime)<2.00,1, true(),0))) as PlaybackNumSuccessful, sum(eval(case(tonumber(RequestTime)>=2.00,1,true(),0))) as PlaybackNumLong, sum(eval(case(isnull(RequestTime),1, true(),0))) as PlaybackNumNull, count(eval(RecordingID)) as PlaybackNumTotal

 

 

That will give you  information about whether the RequestTime field is being interpreted incorrectly or not recognized at all.  

tbeason
Engager

What ended up making the search work from python was to explicitly add "RequestTime" to the regex.  For some reason it worked without it on the web UI.

rex field=_raw ".*?\\t.*?\\t.*?\\t.*?\t(?<Method>\w+)\s/(?<URI>.+?)\sHTTP.+?\\t.*?\\t(?<Status>.+?)\\t.*?\\t.*?\\t.*?\\t.*?\\t(?<RequestTime>.+?)\s"

0 Karma

bowesmana
SplunkTrust
SplunkTrust

Is the time range different in each search? In the UI, the time would be applicable to the time zone of the UI, if your timestamps are in UTC for eaxmple.

Whereas, I'm not sure how the timezone for the search would be interpreted running as the Python script, but probably the system time zone.

Not sure if this will help, but I have had a similar issue with different results for different searches based on time zone settings.

 

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...