Knowledge Manager Manual

 


Look up fields from external data sources

This documentation does not apply to the most recent version of Splunk. Click here for the latest version.

Look up fields from external data sources

Use the dynamic fields lookup feature to add fields to your events with information from an external source, such as a static table (CSV file) or an external (Python) command. You can also base more sophisticated lookups on time information.

For example, if you are monitoring logins with Splunk and have IP addresses and timestamps for those logins in your Splunk index, you can use a dynamic field lookup to map the IP address and timestamp to the MAC address and username information for the matching IP and timestamp data that you have in your DHCP logs.

To set up a lookup:

1. Edit transforms.conf to define your lookup table.

Currently you can define two kinds of lookup tables: static lookups (which utilize CSV files) and external lookups (which utilize Python scripts). The arguments you use in your transforms stanza indicate the type of lookup table you want to define. Use filename for static lookups and external_cmd for external lookups.

Note: A lookup table must have at least two columns. Each column may have multiple instances of the same value (multi-valued fields).

2. Edit props.conf to apply your lookup table.

This step is the same for both static and external lookups. In this configuration file, you specify the fields to match and output (or outputnew, if you don't want to overwrite the output fields) from the lookup table that you defined in transforms.conf.

Note: You can have more than one fields lookup defined in a single source stanza. Each lookup definition should begin with a unique LOOKUP-<class> key; for example, if you have multiple tables, you can name them: LOOKUP-table1, LOOKUP-table2, etc.

3. Restart Splunk to implement the changes you made to the configuration files.

After restart, you should see the output fields from your lookup table listed in the fields picker. From there, you can select the fields to display in each of the matching events.

Important: Do not edit these conf files in $SPLUNK_HOME/etc/system/default. Instead, you should edit the file in $SPLUNK_HOME/etc/system/local/ or $SPLUNK_HOME/etc/apps/<app_name>/local/.


Set up a fields lookup based on a static file

The simplest fields lookup is based on a static table CSV file. The CSV file needs to be located in one of two places:

Important: If this lookups directory does not exist, you will need to create it.

1. Edit transforms.conf to define your lookup table.

In transforms.conf, add a stanza to define your lookup table. The name of the stanza is also the name of your lookup table. You will use this transform in props.conf.

In this stanza, reference the CSV file's name:

[myLookup]
filename = <filename>
max_matches = <integer>

Optionally, you can specify the number of matching entries to apply to an event. max_matches indicates that the first (in file order) <integer> entries are used. By default, max_matches is 100 for lookups that are not time-based.

2. Edit props.conf to apply your lookup table.

In props.conf, add a stanza with the lookup key. This stanza specifies the lookup table that you defined in transforms.conf and indicates how Splunk should apply it to your events:

[<stanza name>]
LOOKUP-<class> = $TRANSFORM <match_field_in_table> OUTPUT|OUTPUTNEW <output_field_in_table>

Use the AS clause if the field names in the lookup table and your events do not match or if you want to rename the field in your event:

[<stanza name>]
LOOKUP-<class> = $TRANSFORM <match_field_in_table> AS <match_field_in_event> 
OUTPUT|OUTPUTNEW <output_field_in_table> AS <output_field_in_event>

You can have more than one field after the OUTPUT|OUTPUTNEW clause. If you don't use OUTPUT|OUTPUTNEW, Splunk adds all the field names and values from the lookup table to your events.

3. Restart Splunk.

Example of static fields lookup

Here's an example of setting up lookups for HTTP status codes in an access_combined log. In this example, you want to match the status field in your lookup table (http_status.csv) with the field in your events. Then, you add the status description and status type fields into your events.

The following is the http_status.csv file. You can put this into $SPLUNK_HOME/etc/apps/<app_name>/lookups/. If you're using this in the Search App, put the file into $SPLUNK_HOME/etc/apps/search/lookups/:

status,status_description,status_type
100,Continue,Informational
101,Switching Protocols,Informational
200,OK,Successful
201,Created,Successful
202,Accepted,Successful
203,Non-Authoritative Information,Successful
204,No Content,Successful
205,Reset Content,Successful
206,Partial Content,Successful
300,Multiple Choices,Redirection
301,Moved Permanently,Redirection
302,Found,Redirection
303,See Other,Redirection
304,Not Modified,Redirection
305,Use Proxy,Redirection
307,Temporary Redirect,Redirection
400,Bad Request,Client Error
401,Unauthorized,Client Error
402,Payment Required,Client Error
403,Forbidden,Client Error
404,Not Found,Client Error
405,Method Not Allowed,Client Error
406,Not Acceptable,Client Error
407,Proxy Authentication Required,Client Error
408,Request Timeout,Client Error
409,Conflict,Client Error
410,Gone,Client Error
411,Length Required,Client Error
412,Precondition Failed,Client Error
413,Request Entity Too Large,Client Error
414,Request-URI Too Long,Client Error
415,Unsupported Media Type,Client Error
416,Requested Range Not Satisfiable,Client Error
417,Expectation Failed,Client Error
500,Internal Server Error,Server Error
501,Not Implemented,Server Error
502,Bad Gateway,Server Error
503,Service Unavailable,Server Error
504,Gateway Timeout,Server Error
505,HTTP Version Not Supported,Server Error

1. In a transforms.conf file located in either $SPLUNK_HOME/etc/system/local/ or $SPLUNK_HOME/etc/apps/<app_name>/local, put:

[http_status]
filename = http_status.csv

2. In a props.conf file, located in either $SPLUNK_HOME/etc/system/local/ or $SPLUNK_HOME/etc/apps/<app_name>/local/, put:

[access_combined]
LOOKUP-table = http_status status OUTPUT status_description, status_type

This assumes your events have sourcetype = access_combined. The stanza can also apply to events by source or host, as specified in props.conf.

3. Restart Splunk.

Use search results to populate a lookup table

You can use the results of a saved search to populate a lookup table. In a local or app-specific copy of savedsearches.conf:

1. Define a lookup. Optionally, test it with a search that uses the lookup search command to make sure it is correct.

2. Enable the lookup population action.

3. Tell Splunk where to copy your lookup table. For steps 2 and 3, add these two lines into the stanza for your saved search:

action.populate_lookup = 1
action.populate_lookup.dest = <string>

The action.populate_lookup.dest value is the path to a CSV file where Splunk copies the search results. For this action to work, the destination directories must already exist. They are either $SPLUNK_HOME/etc/system/lookups or $SPLUNK_HOME/etc/<app_name>/lookups.

Because Splunk copies the results of the saved search to a CSV file, you can set up your fields lookup the same way you set up a static lookup.


Set up a fields lookup based on an external command

For dynamic or external lookups, your transforms.conf stanza references the command or script and arguments to invoke. You can also specify the type of command or script to invoke:

[myLookup]
external_cmd = <string>
external_type = python
fields_list = <string>
max_matches = <integer>

Use fields_list to list all the fields supported by the external command, delimited by a comma and space.

Note: Currently, Splunk only supports Python scripts for external command-based field lookups. Python scripts used for these lookups must be located in one of three places:

Note: When writing your Python script, if you refer to any external resources (such as a file), the reference must be relative to the directory where the script is located.

Example of external fields lookup

Here's an example of how you might use external lookups to match with information from a DNS server. In this example, external_lookup.py is a script that:

1. In a transforms.conf file, put:

[dnsLookup]
external_cmd = external_lookup.py host ip
fields_list = host, ip

2. In a props.conf file, put:

[access_combined]
LOOKUP-dns = dnsLookup host OUTPUT ip

For a reverse DNS lookup, your props.conf stanza would be:

[access_combined]
LOOKUP-rdns = dnsLookup ip OUTPUT host

3. Restart Splunk.

More about the external lookup script

When designing your external lookup script, keep in mind that it needs to take in a partially empty CSV file and output a filled-in CSV file. The arguments that you pass to the script are the headers for these input and output files.

In the DNS lookup example above, the CSV file contains 2 fields, "host" and "ip". The fields that you pass to this script are the ones you specify in transforms.conf:

external_cmd = dnslookup.py host ip

Note: If you don't pass these arguments, the script will return an error.

When you run the search command:

... | lookup dnsLookup host

You're telling Splunk to use the lookup table that you defined in transforms.conf as [dnsLookup] and pass into the external command script the values for the "host" field as a CSV file, which may look like this:

host,ip
work.com
home.net

Basically, this is a CSV file with the header "host" and "ip", but missing values for ip. The two headers are included because they are the fields you specified in the fields_list parameter of transforms.conf.

The script then outputs the following CSV file and returns it to Splunk, which populates the ip field in your results:

host,ip
work.com,127.0.0.1
home.net,127.0.0.2

Set up a time-based fields lookup

If your static or external lookup table has a field value that represents time, you can use this time field to set up your fields lookup. For time-based (or temporal) lookups, add the following lines to your lookup stanza in transforms.conf:

time_field = <field_name>
time_format = <string>

If time_field is present, by default max_matches is 1. Also, the first matching entry in descending order is applied.

Use the time_format key to specify the strptime format of your time_field. By default, time_format is UTC.

For a match to occur with time-based lookups, you can also specify offsets for the minimum and maximum amounts of time that an event may be later than a lookup entry. To do this, add the following lines to your stanza:

max_offset_secs = <integer>
min_offset_secs = <integer>

By default, there is no maximum offset and the minimum offset is 0.

Example of time-based fields lookup

Here's an example of how you might use DHCP logs to identify users on your network based on their IP address and the timestamp. Let's say the DHCP logs are in a file, dhcp.csv, which contains the timestamp, IP address, and the user's name and MAC address.

1. In a transforms.conf file, put:

[dhcpLookup]
filename = dhcp.csv
time_field = timestamp
time_format = %d/%m/%y %H:%M:%S

2. In a props.conf file, put:

[dhcp]
LOOKUP-table = dhcpLookup ip mac OUTPUT user

3. Restart Splunk.

This documentation applies to the following versions of Splunk: 4.0 , 4.0.1 , 4.0.2 , 4.0.3 , 4.0.4 , 4.0.5 , 4.0.6 , 4.0.7 , 4.0.8 , 4.0.9 , 4.0.10 , 4.0.11 View the Article History for its revisions.


You must be logged into splunk.com in order to post comments. Log in now.

Was this documentation topic helpful?

If you'd like to hear back from us, please provide your email address:

We'd love to hear what you think about this topic or the documentation as a whole. Feedback you enter here will be delivered to the documentation team.

Feedback submitted, thanks!