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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...