Creating a session state table

In this recipe, you will learn how to leverage lookups to maintain a state table that will capture the first time a session was seen and continually update the existing session's information accordingly. You can use this to determine if a session has gone stale and has been abandoned or if someone is trying to hijack an old session.

Getting ready

To step through this recipe, you will need a running Splunk Enterprise server, with the sample data loaded from Chapter 1, Play Time – Getting Data In. You should be familiar with navigating the Splunk user interface.

How to do it…

Follow the steps in this recipe to create a state table of sessions:

  1. Log in to your Splunk server.
  2. Select the Operational Intelligence application.
  3. In the search bar, enter the following search and select to run it over Last 15 minutes:
    index=main sourcetype="access_combined" | eval firsttime=_time | eval lasttime=_time |  stats last(firsttime) as firsttime, first(lasttime) as lasttime by JSESSIONID | outputlookup createinapp=true session_state.csv
  4. You should see a tabulated list by session ID, listing the firsttime and lasttime columns. Splunk will also have created a lookup named sessions.csv as a result of the search.
    How to do it…
  5. Next, amend the query slightly as follows and rerun the search over Last 15 minutes again:
    index=main sourcetype="access_combined" | eval firsttime=_time | eval lasttime=_time | fields JSESSIONID firsttime lasttime | inputlookup session_state.csv append=true | stats last(firsttime) as firsttime, first(lasttime) as lasttime by JSESSIONID | outputlookup createinapp=true session_state.csv
  6. You should see a very similar (if not the same) list of times and session IDs. Now, let's save this search as a report. Click on the Save As link and select Report.
    How to do it…
  7. Enter cp07_session_state as the title and then click on Save.
    How to do it…
  8. Click on the Schedule link.
    How to do it…
  9. Check the Schedule Report box.
  10. In the Schedule dropdown, select Run on Cron Schedule, and then enter */15 * * * * in the Cron Expression field. This cron schedule means that run every 15 minutes. Then, click on Next and then on Save.
    How to do it…
  11. The state table is now created and will periodically update every 15 minutes. Let's now view the table. Enter the following search in the search bar of the Operational Intelligence application:
    |inputlookup session_state.csv 
    | eval firsttime_daysago=round((now()-firsttime)/60/60/24)
    | eval lasttime_daysago=round((now()-lasttime)/60/60/24)
    | convert ctime(firsttime), ctime(lasttime)
    | table JSESSIONID firsttime, firsttime_daysago, lasttime, lasttime_daysago
  12. You should be presented with a session table, listing the first time the session was seen, how many days ago the first time was, the last time the session was seen, and how many days ago the last time was.

How it works…

This recipe is designed to maintain some form of state about the sessions being used within the application we are monitoring. The data we are capturing includes both the first time the session ID was detected and the last time the session ID was detected. A lookup table is used to maintain the up-to-date states of the sessions over time and will be a lot faster to search than trying to search for all the sessions over time.

In this recipe, we initially started with two searches: the first search was used to create the lookup file as this did not exist, and the second search was the search that we chose to save and schedule. This second search brings in the existing lookup table we created in the first search, which is why the first search was performed.

As the first and second searches are similar, let's explain how the second search works with the help of the following table:

Search fragment

Description

index=main sourcetype="access_combined"

This tells Splunk to find all the web server logs in the main index.

| eval firsttime=_time | eval lasttime=_time

Here, we use the eval command to evaluate firsttime and lasttime. In this case, both times will be using the _time field in the event, which is the timestamp of the event.

| fields JSESSIONID firsttime lasttime

Next, we declare that we only want to use the JSESSIONID, firsttime, and lasttime fields.

| inputlookup session_state.csv append=true

Next, we leverage the inputlookup command to bring in the existing session_state.csv lookup file (created in the one-time first search). The results of this will be appended to the existing results of the search.

| stats last(firsttime) as firsttime, first(lasttime) as lasttime by JSESSIONID

Here, we leverage the last() and first() functions of the stats command to find the oldest firsttime date (using last) and the most recent lasttime date (using first), and list these two fields using JSESSIONID. This ensures that we keep a copy of the oldest date as the first time the session was seen and the most recent date as the last time the session was seen.

| outputlookup createinapp=true session_state.csv

Finally, we write back to the session_state.csv lookup using the outputlookup command. This replaces the old file with a new one that contains the results of our search.

As this search is scheduled to run every 15 minutes and look back over the past 15 minutes, it will build up a large table of session IDs in time. This can be very useful for tracking purposes.

Tip

You might want to amend the lookup population search to drop the session IDs that have a firsttime date older than a certain number of days so that the lookup does not keep filling up forever.

Once the lookup was saved and scheduled, the final part of the recipe involved putting together a search that leveraged inputlookup to view the data in the lookup file. You evaluated new fields to calculate the number of seconds between the present time (using now()) and both the firsttime and lasttime fields' epoch values. The convert command and the ctime function were then leveraged to display the firsttime and lasttime fields in a readable timestamp format rather than the epoch seconds.

There's more…

The state table approach described previously works well for many use cases, but a more scalable approach might be needed for larger state tables. The state table in the recipe is completely rewritten each time the search runs. Rather than doing this, we can leverage the Splunk KV store to maintain the session table and modify the table only when there are new records or when the existing records need to be updated. This approach is described next.

Use the Splunk KV store to maintain the session state table

We can actually convert the state table we just created into a KV store table and then modify the existing scheduled search accordingly.

Follow the next steps to convert the existing state table to leverage the Splunk KV store instead:

  1. Firstly, there is one small configuration step that we cannot do from the GUI. Create a new file called collections.conf. Into this file, enter the name of the KV collection as follows:
    [session_state]
  2. Save this file to the following location and restart Splunk:
    $SPLUNK_HOME/etc/apps/operational_intelligence/local
  3. Everything else can now be done from the GUI! We need to create the new KV lookup. Click on the Settings menu and then select the Lookups menu item. Then select Lookup Definitions to go to the Lookup Definitions screen.
  4. Select New to create a new lookup definition. Give the definition Name of session_state, select Type of File-Based, and select the session_state.csv in the Lookup file field. Then click Save.
    Use the Splunk KV store to maintain the session state table
  5. Now we need to convert this to a KV lookup. Click on the session_state lookup definition you just created to go back into the editing screen. Change Type to KV Store. As soon as you do this, you will see that Splunk has already filled in the Supported fields with the field names from the file lookup – pretty nice! Give the lookup Collection Name of session_state. This collection name must match the name of the collection you previously entered into collections.conf in the first step. Lastly, KV Store lookups in Splunk come with a hidden field called _key, which is a unique identifier of each row in the lookup. We are going to use this field to identify which rows we want to update in the future runs of our state table search. We need to add this as one of the Supported fields. Once this is done, click Save.
    Use the Splunk KV store to maintain the session state table
  6. Now we need to amend the search we created in the main recipe to leverage the new KV store instead of the CSV file. Click on the Reports menu item from within the Operational Intelligence application, and next to the cp07_session_state report line item, select Open in Search. Modify the search as follows:
    index=main sourcetype="access_combined" | eval firsttime=_time | eval lasttime=_time | fields JSESSIONID firsttime lasttime | stats last(firsttime) as firsttime, first(lasttime) as lasttime by JSESSIONID | lookup session_state JSESSIONID OUTPUTNEW _key as _key| outputlookup session_state append=t
  7. The search should run and present the familiar session state table. However, we have modified the search here to look up the new KV store lookup and return the hidden _key field if there is a matching JSESSIONID already in the KV store. Then we do an outputlookup to the session_state KV store using the append=t parameter. This will essentially update any existing records with a matching _key field. Any new records that do not have an existing entry in the KV store table will be appended to the KV table and assigned a unique _key field.
  8. Once the newly modified search has run correctly, click Save to update the existing cp07_session_state search with the new syntax. Congratulations! You are now using Splunk's KV store to maintain a session state table.

    Tip

    Splunk's Distributed Management Console (DMC) application that is bundled with Splunk has some good reporting around the KV Store instances that run within your Splunk environment. The link to the DMC can be found in the settings menu.

See also

You can refer to the following recipes for more information:

  • The Flagging suspect IP addresses recipe
  • The Adding hostnames to IP addresses recipe
  • The Searching ARIN for a given IP address recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.86.56