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!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...