Splunk Search

Extract hostname from folder path

rahiparikh
Explorer

Hi,

How can I extract hostname from path?

/dir/server1/*.log
/dir/server2/*.log
/dir/server3/*.log

I want server1, server2 and server3 to be hostname. My current config --

[ INPUTS.CONF ]

[monitor:///dir/*/messages]
host_regex = /dir/([\w\.\-_a-zA-Z0-9]+)/
source = /var/log/messages
disabled = 0
followTail = 1

I am pre-processing logs ( i.e. transforming before sending them to indexer ) and this config is applied to pre-processing instance. This either doesn't extract the information or extracts wrong information.

1 Solution

joelshprentz
Path Finder

Since the hostname is always the second element in your path, it would be easier to use the segment number (2 in your case).

Replace the host_regex line with:

host_segment = 2

See the documentation for more details.

View solution in original post

lguinn2
Legend

I would also suggest that you not use the source= parameter; source is supposed to be the name of the file being monitored. Splunk can determine this accurately and automatically.

0 Karma

joelshprentz
Path Finder

Since the hostname is always the second element in your path, it would be easier to use the segment number (2 in your case).

Replace the host_regex line with:

host_segment = 2

See the documentation for more details.

rahiparikh
Explorer

@joelshprentz - Thanks! But, same problem! Splunk think's host name field should be the splunk forwarder's name and not folder name! 😞

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...