Splunk Search

hostname extraction regex

carmackd
Communicator

Can someone please help me with a regex to extract the host name from a filename. I've got two different file naming formats.

file name 1 hostname-syslog-info-2010-08

file name 2 hostname.dev.mydomain.compdomain.com

Tags (1)
0 Karma

Lowell
Super Champion

You could try:

host_regex = [/\\]([A-Za-z0-9][A-Za-z0-9._-]+?)(-(syslog|info|\d\d\d\d-\d\d))*($|[/\\])

This approach looks for any valid hostname characters (which could include a "-") and then also has an explicit list of things that you don't want as part of the hostname, like dates, or the terms "syslog" or "info". Right now these "ignored" terms must be separate by a "-" but that could be extended.

I'm not sure if your examples are of a filename (end of the path) or a directory name (somewhere in the middle of the path), but this regex should work in either case.

The two hostnames extracted would be (1) "hostname", and (2) "hostname.dev.mydomain.compdomain.com" I assume that's what you wan't, you didn't explicitly list what you wanted to see from each example.

Also, something like "my-host.com-syslog", would be assigned the hostname "my-host.com"

bbingham
Builder

I'm not sure how this fits in what you're trying to regex, but something as simple as:

([A-z0-9]+).*

will grab any letter or numbers up to your first - or . and place it in $1.

Get Updates on the Splunk Community!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

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