Splunk Search

how to query time spent by user in a console

anikeshp7
Path Finder

Hi I want to create a report to display  time spent by user in a console

Being beginner doesnt know how to query .

Any suggestions ?

 

index="123" AND organizationId="0123000000000342" logRecordType=ailtn ("appName":"Collections_Platform" AND "appType":"Console")

Labels (7)

anikeshp7
Path Finder

login and logout

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Cool! How do you identify which user has logged in and which has logged out? Can a user log in more than once concurrently? If concurrent, does the overlapping period get double counted? If the log in session terminates abnormally, is that also logged as an end of session? Does each event have a session id which ties all these events together?

anikeshp7
Path Finder

-We can identify the users using the userID.

-If a user log in more than once concurrently, overlapping period doesn't get double counted

-If the log in session terminates abnormally, then that will be logged as an end of session

-Each event do have a session id

ITWhisperer
SplunkTrust
SplunkTrust
index="123" AND organizationId="0123000000000342" logRecordType=ailtn ("appName":"Collections_Platform" AND "appType":"Console")
| stats values(userid) as userid, earliest(_time) as startofsession, latest(_time) as endofsession by sessionid
| eval timeloggedon=endofsession-startofsession
| stats sum(timeloggedon) as totalsessiontime by userid
| fields userid totalsessiontime

This assumes you have already extracted userid and sessionid

anikeshp7
Path Finder

I also want to know if the time displayed is which format and how can i format it to display in Hrs:Min

0 Karma

isoutamo
SplunkTrust
SplunkTrust
Those are seconds as original values were epoch. Easiest way is to use tostring with duration:

eval dispTime = tostring(totalsessiontime, "duration")

r. Ismo

anikeshp7
Path Finder

@isoutamothe dispTime column is  coming as empty using this

eval dispTime = tostring(totalsessiontime, "duration")

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Your total session times represent multiple days. Perhaps you need to calculate days, hours, and minutes as separate values or perhaps use strftime(totalsessiontime,"%j %H:%M") although this is likely to give days as 3-digits with leading zeros, but you could strip them afterwards

anikeshp7
Path Finder

@ITWhisperer  is this what You are asking to write as

stats earliest(_time) as startofsession, latest(_time) as endofsession by sessionKey userId

| eval timeloggedon=endofsession-startofsession

|eval strftime(timeloggedon,"%j %H:%M") as temp

|stats sum(temp) as timeSpentOnConsole by userId

| lookup 2clicTest.csv UserID AS userId OUTPUT Name AS NAME

| fields NAME timeSpentOnConsole | sort -timeSpentOnConsole | where NAME != ""

ITWhisperer
SplunkTrust
SplunkTrust
stats earliest(_time) as startofsession, latest(_time) as endofsession by sessionKey userId
| eval timeloggedon=endofsession-startofsession
| stats sum(timeloggedon) as timeSpentOnConsole by userId
| lookup 2clicTest.csv UserID AS userId OUTPUT Name AS NAME
| fields NAME timeSpentOnConsole 
| sort -timeSpentOnConsole 
| where NAME != ""
| fieldformat timeSpentOnConsole = strftime(timeSpentOnConsole ,"%j %H:%M")

anikeshp7
Path Finder

Thanks for this.

One questions, multiplying days by 24 and adding it into hours would be a good approach to display time spent as hours:min

 

How would I do that ? Or shall i simply display as strftime(timeSpentOnConsole ,"%T")

ITWhisperer
SplunkTrust
SplunkTrust

%T will take a modulus of 24. Try this:

stats earliest(_time) as startofsession, latest(_time) as endofsession by sessionKey userId
| eval timeloggedon=endofsession-startofsession
| stats sum(timeloggedon) as timeSpentOnConsole by userId
| lookup 2clicTest.csv UserID AS userId OUTPUT Name AS NAME
| fields NAME timeSpentOnConsole 
| sort -timeSpentOnConsole 
| where NAME != ""
| eval hours=round(timeSpentOnConsole /(60*60))
| eval minutes=round((timeSpentOnConsole / 60) % 60)
| eval timeSpentOnConsole =tostring(hours)." hours, ".tostring(minutes)." minutes"
| fields - hours minutes

anikeshp7
Path Finder

@ITWhispereryou are awesome . Thanks

0 Karma

anikeshp7
Path Finder

The events are generating but I see no data in statistics.

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Given that your screenshot shows timeSpentOnConsole and this is a field calculate from the statistics, what data are you not seeing?

0 Karma

anikeshp7
Path Finder

@ITWhispererThe previous query you wrote wasn't returning stats so had to tweak little bit.

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

So what do you have now and what isn't working?

0 Karma

isoutamo
SplunkTrust
SplunkTrust
Can you give to us some scrambled example log entries so community could easier help you with this case?
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

How do you know when a user is in a console and when they are not?

0 Karma
Get Updates on the Splunk Community!

Earn a $35 Gift Card for Answering our Splunk Admins & App Developer Survey

Survey for Splunk Admins and App Developers is open now! | Earn a $35 gift card!      Hello there,  Splunk ...

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...