Getting Data In

assign sourcetype based upon file name

a212830
Champion

Hi,

Is there a way to dynamically assign sourcetype based upon input filename?

Tags (1)

mbonsack_splunk
Splunk Employee
Splunk Employee

alacercogitatus's answer does work, though the separators for the MetaData keys are single colons, not double, i.e.

DEST_KEY = MetaData:Sourcetype
SOURCE_KEY = MetaData:Source

After that it worked great!

0 Karma

jrodman
Splunk Employee
Splunk Employee

alacercogitatus's answer is certainly correct. You can set the sourcetype at parsing time (typically on your indexer) with props and transforms. For some problems this is the way to go.

However, if you want the sourcetype set up from the beginning, so you can control things like the "CHARSET" (encoding), NO_BINARY_CHECK and so on, then you can also control the sourcetype set at the input layer in tailing. For this to work, you need to be willing to put configuration on your forwarder, and it would look something like

props.conf:

[source::< pattern >]
sourcetype = what_you_want

< pattern > is whatever you need to match the file. The simplest thing that gets everything you want and doesn't get false positives is kind of a local decision, but often it looks something like:





[source::/var/log/my_app/*.log]
sourcetype = my_app




If you use both these techniques, then the value set at the input layer will be used until it is possibly modified by the props/transforms pass. Usually though, you want to use one approach or the other for a particular dataset, lest you make things too confusing.

alacercogitatus
SplunkTrust
SplunkTrust

There is. You will need to specify this in your props/transforms files any where indexing is being performed.

props.conf
[source::...regex_to_match_filename]
TRANSFORMS-fs = force-sourcetype-st

transforms.conf
[force-sourcetype-st]
DEST_KEY = MetaData::Sourcetype
SOURCE_KEY = MetaData::Source
REGEX = YOUR_REGEX_TO_PULL_THE_FILENAME
FORMAT = sourcetype::$1
WRITE_META = true

Referances
http://docs.splunk.com/Documentation/Splunk/latest/admin/Transformsconf
http://docs.splunk.com/Documentation/Splunk/latest/admin/Propsconf

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...