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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

What’s New in Splunk AI: Volume 02

Welcome to the second edition of “What’s New in Splunk AI” where we look at the latest and greatest updates, ...

Splunk App Dev Quarterly Roundup: AI, Agents, and Innovation!

Another quarter, another wave of innovation. From complex integrations to pushing the limits ...

Federated Search for Dynamic Data Self Storage Is Now Generally Available on Splunk ...

 Splunk is excited to announce the General Availability of Federated Search for Dynamic Data Self Storage ...