Splunk Dev

Why do I get an empty output when subtracting one time field from the main query and another from the subquery?

nikhilesh_cvx
New Member

I am retrieving two time fields one from main query and other from subquery. When I subtract both fields, I get blank output.
Query I am using:

index=main host=*  *CRgsSessionInfo*  PrimaryUserLogin=PrimaryUserLogOn | eval Time = _time | append [search  host=* *CRgsSessionInfo PrimaryUserLogin=PrimaryUserLogoff  | eval Time1 = _time ] |  eval Diff= Time1-Time  |table Diff

marked code. dmj

0 Karma

somesoni2
Revered Legend

Easier fix would be to use appendcols command instead of append (assuming both your searches gives a single rows result). With some cleanup/best practices, it'd look like this

index=main host=*  *CRgsSessionInfo*  PrimaryUserLogin=PrimaryUserLogOn | eval Time = _time | table Time| appendcols [search  host=* *CRgsSessionInfo PrimaryUserLogin=PrimaryUserLogoff  | eval Time1 = _time  | table Time1] |  eval Diff= Time1-Time  |table Diff

Better approach is the one suggested by @DalJeanis, when you can completely eliminate subsearch, so look at that.

0 Karma

DalJeanis
Legend

So, there are a number of issues here.

First, you are not giving yourself all the other contextual fields, so you have no way to interpret your results and why they are empty. If you looked at the results without the table command, you would be able to get farther.

Second, you are appending two sets of records, so there are separate records, some for logons and some for logoffs. none of those records have both a logon and a logoff on them, so the diff will always be null. You can actually select all those same records at the same time, but you still need to link them.

Third, you are not connecting the two records in any way. stats is one example of a verb that can be used to pull together different records, if you have a mutual key field. One would assume there is a user field, and a host field. so maybe this is the way they should be connected.

index=main host=* *CRgsSessionInfo* (PrimaryUserLogin="PrimaryUserLogOn" OR  PrimaryUserLogin="PrimaryUserLogoff")
| fields _time host user PrimaryUserLogin
| eval timein=case(PrimaryUserLogin="PrimaryUserLogOn",_time)
| eval timeout=case(PrimaryUserLogin="PrimaryUserLogoff",_time)
| stats count min(timein) as timein max(timeout) as timeout by host user
| eval Diff =timeout-timein

If count is greater than 2, then it means you have multiple logins and logouts and you need a more complex search.

nikhilesh_cvx
New Member

Hey Daljeanis,

Thank you, above query worked for me. But my count is greater than 2 can you suggest something to handle multiple logins?

0 Karma
Get Updates on the Splunk Community!

Splunk at Cisco Live 2025: Learning, Innovation, and a Little Bit of Mr. Brightside

Pack your bags (and maybe your dancing shoes)—Cisco Live is heading to San Diego, June 8–12, 2025, and Splunk ...

Splunk App Dev Community Updates – What’s New and What’s Next

Welcome to your go-to roundup of everything happening in the Splunk App Dev Community! Whether you're building ...

The Latest Cisco Integrations With Splunk Platform!

Join us for an exciting tech talk where we’ll explore the latest integrations in Cisco + Splunk! We’ve ...