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!

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...

.conf24 | Learning Tracks for Security, Observability, Platform, and Developers!

.conf24 is taking place at The Venetian in Las Vegas from June 11 - 14. Continue reading to learn about the ...

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...