Getting Data In

How to set the source in a collect command to a variable?

creiglow
Explorer

I am working with the collect command an want to set the source to a variable, not a string.

| eval myDynamicSource=index."-".sourcetype
| collect index=garbage source=myDynamicSource

However, the source in the collect is always just the name of the variable even if I use no quotes, double quotes or single quotes.

Any idea what I am missing?

Thanks,
--Keith

0 Karma
1 Solution

arjunpkishore5
Motivator

To my knowledge, collect operates on the entire Input, as in, it's not a streaming command. This is why if you cancel the search(with a collect statement at the end) before it has completed, none of the records are saved. So you cannot dynamically change the metadata directly on the collect, but you can do a workaround using props and transforms. here is how.

your collect remains as is | collect index=garbage source=myDynamicSource

In transforms.conf, you have this. In this, you're extracting the value of myDynamicSource and assigning it to the metadata field source

[setDynamicSource]
REGEX = (myDynamicSource)=(.+)
FORMAT = source::$2
DEST_KEY= MetaData:Source

In props.conf, you just apply the above transform to the source myDynamicSource

[source::myDynamicSource]
TRANSFORMS-set_source = setDynamicSource

Hope this helps.

Cheers

View solution in original post

0 Karma

arjunpkishore5
Motivator

To my knowledge, collect operates on the entire Input, as in, it's not a streaming command. This is why if you cancel the search(with a collect statement at the end) before it has completed, none of the records are saved. So you cannot dynamically change the metadata directly on the collect, but you can do a workaround using props and transforms. here is how.

your collect remains as is | collect index=garbage source=myDynamicSource

In transforms.conf, you have this. In this, you're extracting the value of myDynamicSource and assigning it to the metadata field source

[setDynamicSource]
REGEX = (myDynamicSource)=(.+)
FORMAT = source::$2
DEST_KEY= MetaData:Source

In props.conf, you just apply the above transform to the source myDynamicSource

[source::myDynamicSource]
TRANSFORMS-set_source = setDynamicSource

Hope this helps.

Cheers

0 Karma

frankfowler
Explorer

I dont see this as working :

I have transforms.conf and props.conf set as you stated above in my etc/system/local area.

my search is as follows :

index=xxx | eval myDynamicSource=source | collect index=xxx source=myDynamicSource

 

i see the source still set to myDynamicSource.

 

what am i missing ?

0 Karma
Get Updates on the Splunk Community!

Enterprise Security Content Update (ESCU) | New Releases

In December, the Splunk Threat Research Team had 1 release of new security content via the Enterprise Security ...

Why am I not seeing the finding in Splunk Enterprise Security Analyst Queue?

(This is the first of a series of 2 blogs). Splunk Enterprise Security is a fantastic tool that offers robust ...

Index This | What are the 12 Days of Splunk-mas?

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