Splunk Search

Add field to timechart

schufi01
Path Finder

Hi,

I got a timechart that shows the number of critical package losses per day. Additionally I calculate the average of critical package losses within the chosen timespan and show it as a straight line in the timechart. Now I would like to add a line, that tells me, how many different hosts were used on every single day. (E.g Monday 36, Tuesday 64, Wednesday 55, ...)

Thats the code that calculates the number of critical package losses per day and the average within the chosen timespan.

| eval Amount= numberLostPackages
| timechart span=24h count(Amount) as Count sum(Amount) as Total aligntime=@d
| eventstats sum(Count) as TotalCount
| addinfo 
| eval Duration=(info_max_time-info_min_time)/86400
| eval Average=TotalCount/Duration

 

Labels (2)
0 Karma
1 Solution

jodonald
Explorer

Hi @schufi01 

Using the distinct_count() function in your timechart will give you the number of different hosts each day.  so your search would look like this:

 

| eval Amount= numberLostPackages
| timechart span=24h count(Amount) as Count sum(Amount) as Total distinct_count(host) as unique_hosts aligntime=@d
| eventstats sum(Count) as TotalCount
| addinfo 
| eval Duration=(info_max_time-info_min_time)/86400
| eval Average=TotalCount/Duration

 

Additionally, you can modify the span to align on days instead of hours which would allow for you to drop the aligntime from your timechart.  so the final search would look like this:

 

| eval Amount= numberLostPackages
| timechart span=1d count(Amount) as Count sum(Amount) as Total distinct_count(host)
| eventstats sum(Count) as TotalCount
| addinfo 
| eval Duration=(info_max_time-info_min_time)/86400
| eval Average=TotalCount/Duration

 

Hope that helps.

View solution in original post

jodonald
Explorer

Hi @schufi01 

Using the distinct_count() function in your timechart will give you the number of different hosts each day.  so your search would look like this:

 

| eval Amount= numberLostPackages
| timechart span=24h count(Amount) as Count sum(Amount) as Total distinct_count(host) as unique_hosts aligntime=@d
| eventstats sum(Count) as TotalCount
| addinfo 
| eval Duration=(info_max_time-info_min_time)/86400
| eval Average=TotalCount/Duration

 

Additionally, you can modify the span to align on days instead of hours which would allow for you to drop the aligntime from your timechart.  so the final search would look like this:

 

| eval Amount= numberLostPackages
| timechart span=1d count(Amount) as Count sum(Amount) as Total distinct_count(host)
| eventstats sum(Count) as TotalCount
| addinfo 
| eval Duration=(info_max_time-info_min_time)/86400
| eval Average=TotalCount/Duration

 

Hope that helps.

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!

From Data to Insight: Announcing the Winners of the Splunk Dashboard Contest

Hi Splunkers, First off, thank you to everyone who participated in our very first From Data to Insight: The ...

Splunk Developers: Construct Your Future at the .conf26 Builder Bar

Calling all Splunk architects, platform admins, and app developers: the site is open, and the blueprints are ...

Quick connection discovery mode for forwarders

When a Splunk forwarder loses connectivity to its indexers, it does not always reconnect immediately. In many ...