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!

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

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

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...