Splunk Search

Is there a way for Lookup file to return field names?

ansif
Motivator

Is there a way to achieve this?

 

I have  a lookup table with 2 columns alert_type and short_description.

 

alert_type | short_description

cpu              | "The Host".host."cpu utilization is high".cpu_perc."%"

mem            | "The memory in the host ".host."is high with a percentage of ".mem_perc."%"

 

When alert type matches it should return short_description and the fields in the short description should replace with field values( host,cpu_perc and mem_perc)

 

Example :

The Host abcd.com cpu utilization is high 90 %

instead of a string

"The Host".host."cpu utilization is high".cpu_perc."%"

 

Labels (1)
0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

Yes you can do

| lookup lookup_file.csv alert_type OUTPUT short_description
| eval msg=short_description
| foreach mem_perc host cpu_perc [ eval msg=replace(msg, "<<FIELD>>", <<FIELD>>) ]

Note that here, the 'host' will get replaced twice, as there are two "host" strings. So, you may want to surround your replacement anchors with some identifer, e.g. if your description is

"The memory in the host {host} is high with a percentage of {mem_perc}%"

then use

| foreach mem_perc host cpu_perc [ eval msg=replace(msg, "{<<FIELD>>}", <<FIELD>>) ]

View solution in original post

0 Karma

bowesmana
SplunkTrust
SplunkTrust

You can't do that and, for example, use printf(short_description, ...) but you could use a series of replace statements, e.g.

| lookup lookup_file.csv alert_type OUTPUT short_description
| eval msg=replace(short_description, "mem_perc", mem_perc)
| eval msg=replace(msg, "host", host)
| eval msg=replace(msg, "cpu_perc", cpu_perc)

You could put that all in a macro if you wanted re-use and just change the macro as needed. 

0 Karma

ansif
Motivator

Is there a way to do an iterate using foreach or something to replace?

 

Like all  fields in the short_description which needs to replace is prefix with "var_" , thus a foreach to replace all var_* with *?

 

 

0 Karma

bowesmana
SplunkTrust
SplunkTrust

Yes you can do

| lookup lookup_file.csv alert_type OUTPUT short_description
| eval msg=short_description
| foreach mem_perc host cpu_perc [ eval msg=replace(msg, "<<FIELD>>", <<FIELD>>) ]

Note that here, the 'host' will get replaced twice, as there are two "host" strings. So, you may want to surround your replacement anchors with some identifer, e.g. if your description is

"The memory in the host {host} is high with a percentage of {mem_perc}%"

then use

| foreach mem_perc host cpu_perc [ eval msg=replace(msg, "{<<FIELD>>}", <<FIELD>>) ]
0 Karma

ansif
Motivator

| makeresults
| eval host="abc.com",mem_perc="32",short_description="The memory in the host {host} is high with a percentage of {mem_perc}%"
| eval msg=short_description
| foreach host mem_perc cpu_perc
[ eval msg=replace(msg, "{<<FIELD>>}", <<FIELD>>) ]

 

The above woks however if the field is not available then it is not working. For example cpu_perc is not available above so it is showing blank in msg

0 Karma

bowesmana
SplunkTrust
SplunkTrust

Test for null and use some standard text, i.e.

| foreach host mem_perc cpu_perc
  [ eval _tmp=if(isnotnull('<<FIELD>>'), '<<FIELD>>', "Unknown"), 
         msg=replace(msg, "{<<FIELD>>}", _tmp) 
    | fields - _tmp ]
0 Karma
Get Updates on the Splunk Community!

Accelerating Observability as Code with the Splunk AI Assistant

We’ve seen in previous posts what Observability as Code (OaC) is and how it’s now essential for managing ...

Integrating Splunk Search API and Quarto to Create Reproducible Investigation ...

 Splunk is More Than Just the Web Console For Digital Forensics and Incident Response (DFIR) practitioners, ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...