Splunk Search

Multiple Regex on a single file name

bellsam
New Member

Hello!

I'm using splunk to monitor kubernetes pod log files. Which sit on the nodes, the file name is as follows:

podname_namespace_dockername.log

Within the input.conf file, I would like to dynamically label the log file before sending it up to Splunk cloud. I'd like to use the podname field as the hostname, then the namespace as the second field.

I can easily get the host section, but I'm having difficulty getting the regex match on the second part of the file name. I went through the doco and used some regex (PCRE) validators and they advised I'm doing it right.

Any help would be greatly appreciated.

My attempt:

watch all files in

 [monitor:///var/log/containers/*.log]

extract host from the first group in the filename

 host_regex = /var/log/containers/(.*)_.*_.*\.log

extract namespace from the first group in the filename

 namespace = /var/log/containers/.*_(.*)_.*\.log
Tags (1)
0 Karma

mayurr98
Super Champion

hey try something like this!

host_regex = /var/log/containers/([^_]+)
namespace = /var/log/containers/[^_]+_([^_]+)

I do not know what is namespace as I have not found in the docs of inputs.conf but I have written the regex for you.
Also if you want to extract host and namespace at search time then you can try this run anywhere search

| makeresults | eval source="/var/log/containers/podname_namespace_dockername.log" | rex field=source "\/var\/log\/containers\/(?<host>[^_]+)_(?<namespace>[^_]+)_.*\.log"

Let me know if this helps you!

0 Karma

micahkemp
Champion

Are you having trouble getting a regex to match, or are you having issues creating a new indexed field (like host, source, etc)?

0 Karma

bellsam
New Member

I assumed just by specifying the regex for a new value it would create a new indexed field. But that's probably what I'm doing wrong.

0 Karma

ddrillic
Ultra Champion

Please try -

index=<any_index>  
| eval _raw="/var/log/containers/podname_xxxx_dockername.log"
| rex field=_raw "/var/log/containers/(?<host>.*)_(?<namespace>.*)_.*\.log"

It extracts host and namespace.

Therefore, for host your solution should work -

 host_regex = /var/log/containers/(.*)_.*_.*\.log

It doesn't seem that you can do the same for namespace.

0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...