Splunk Search

How to use stats to join 2 indexes based on 1 field

leonheart78
Explorer

Currently I have 2 indexes:
Index A contains ProgramID, User
Index B contains ID, Machine

I would like to use stats to join these 2 indexes based on ProgramID and ID, and get a table to reflect the User, Machine, and the Percent count of the Machine in the record.

How can I do it?
Thank you.

0 Karma
1 Solution

Runals
Motivator

I'm not quite sure what you mean by percent count of the machine. To join the data and assuming ID = ProgramID I'd probably do something like

index=A OR index=B sourcetype=... OR sourcetype=... | foreach User Machine [eval <<FIELD>> = coalesce(<<FIELD>>, "-")] | eval ProgramID = coalesce(ProgramID, ID) | stats count by Machine, User, ProgramID

I'm first making sure the fields User and Machine have data since they will be used in the stats command. Then I'm collapsing ProgramID and ID (based on my assumptions). Lastly a simple stats command to get the number of events per each Machine, User & ProgramID combo. If you want percent of something related to machine (ie how often does this machine or program show up relative to the result set) you probably would then need to use an eventstats command.

View solution in original post

mtranchita
Communicator

Would something like this get you what you are looking for?
index="Index A" | fields ProgramID, User | rename ProgramID AS ID | search [ index="Index B" | fields ID, Machine] | stats c(ID) as ID by User, Machine

0 Karma

Runals
Motivator

I'm not quite sure what you mean by percent count of the machine. To join the data and assuming ID = ProgramID I'd probably do something like

index=A OR index=B sourcetype=... OR sourcetype=... | foreach User Machine [eval <<FIELD>> = coalesce(<<FIELD>>, "-")] | eval ProgramID = coalesce(ProgramID, ID) | stats count by Machine, User, ProgramID

I'm first making sure the fields User and Machine have data since they will be used in the stats command. Then I'm collapsing ProgramID and ID (based on my assumptions). Lastly a simple stats command to get the number of events per each Machine, User & ProgramID combo. If you want percent of something related to machine (ie how often does this machine or program show up relative to the result set) you probably would then need to use an eventstats command.

MuS
Legend

Nice answer! Just to add a link to another answer which could be helpful in this case https://answers.splunk.com/answers/129424/how-to-compare-fields-over-multiple-sourcetypes-without-jo...

cheers, MuS

0 Karma

wrangler2x
Motivator

And here is a Splunk blog entry on coalesce: http://blogs.splunk.com/2014/03/21/search-command-coalesce/

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...