Splunk Search

How can I pull the duration or datetime difference with the given value?

iqbalintouch
Path Finder

I am trying to extract the time duration in tabular format of check-in and check-out value, can someone please help. I am adding these value in my base search query:

index=* sourcetype=* host=* chkin=* chkout=* | eval TimeDifference= chkout-chkin | stats count by chkin,chkout

now there is a field called chkin which indicate the customer check-in date and chkout indicate customer checkout date...now I need to pull the difference of check-in and check-out to identify the stay period of customer ?

I am beginner so don't have much knowledge to use more commands in my search query.

Thanks in advance!

0 Karma

woodcock
Esteemed Legend

It is unlikely that any answer will actually work because you have not shown us sample data but maybe something like this:

index=YouShouldAlwaysSpecifyAnIndex sourcetype=AndSourcetypeToo chkin="*" chkout="*"
| rename COMMENT AS "If both fields are not in each event, total failure happened above"
| eval StayTimeSeconds = chkout - chkin
| rename COMMENT AS "if both values are not 'time_t' integers (AKA 'epoch'), total failure happened above"
| stats avg(StayTimeSeconds) AS AvgStayTime BY host
| eval AvgStayTime=tostring(AvgStayTime, "duration")
0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi iqbalintouch,
you have to use the epochtime format before calculate a time diff:

| your_search
| eval TimeDifference=strptime(chkout,"%Y-%m-%d %H:%M:%S")-strptime(chkin,"%Y-%m-%d %H:%M:%S") 
| ...

obviously verify the date format of your chkout and chkin used in strptime.
I don't understand why you want to calculate Time Difference if you don't use it in the following command.
Remember that after Time Difference calculation if you run a stats count like | stats count by chkin,chkout, you don't see Time Difference, to see it you have to add to stats command also the option values(TimeDifference) AS TimeDifference.
Bye.
Giuseppe

0 Karma
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!

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...

Skip the Awkward Silence: Have a .conf-ersation at .conf26

Picture this. You arrive at .conf26 already having your socializing and networking plans mapped out. No ...

Rethinking Zero Trust: From Product Purchases to Logical Control Evidence

Implementing Zero Trust (ZT) across complex environments often falters at the very beginning due to a ...