Splunk Search

How to pass start time to gentimes with a subquery to append in the search results?

nadid
Path Finder

Hi all,

I'm trying to create a query that gets the number of occurrences of certain Event per month. For that i get the number of elements with the following query:

index=kpis|convert timeformat="%Y-%m" ctime(_time) AS year_month|chart count(Event) as events over year_month

This will return only the months where there is actual data. For adding the months that are not in there since one year ago i thought that i can use gentimes. To do so I've tried to add :

|append [| gentimes start=08/24/2014  increment=28d|convert timeformat="%Y-%m" ctime(starttime) AS year_month|table year_month|dedup year_month]|fillnull

resulting in

index=kpis|convert timeformat="%Y-%m" ctime(_time) AS year_month|chart count(Event) as events over year_month|append [| gentimes start=08/24/2014  increment=28d|convert timeformat="%Y-%m" ctime(starttime) AS year_month|table year_month|dedup year_month]|fillnull

But now i want to add the start dynamically out of a subquery. I thought doing:

index=kpis|convert timeformat="%Y-%m" ctime(_time) AS year_month|chart count(Event) as events over year_month|append [| gentimes start=[search index="notexisting"|stats count|eval time=relative_time(now(),"-1y")|convert ctime(time) as time timeformat="\"%m/%d/%Y\""  |return $time]  increment=28d|convert timeformat="%Y-%m" ctime(starttime) AS year_month|table year_month|dedup year_month]|fillnull

But I receive the following error:

[subsearch]: command="gentimes", generatetimestamps requires start=mm/dd/yyyy:hh:mm:ss and optional takes 'end' and 'increment' values.

Even using as the error suggest

index=kpis|convert timeformat="%Y-%m" ctime(_time) AS year_month|chart count(Event) as events over year_month|append [| gentimes start=[search index="notexisting"|stats count|eval time=relative_time(now(),"-1y")|convert ctime(time) as time timeformat="\"%m/%d/%Y:%H:%M:%S\""  |return $time]  increment=28d|convert timeformat="%Y-%m" ctime(starttime) AS year_month|table year_month|dedup year_month]|fillnull

that is expected to return "08/21/2014:16:34:18" as the error indicates it still fails with

[subsearch]: command="gentimes", generatetimestamps requires start=mm/dd/yyyy:hh:mm:ss and optional takes 'end' and 'increment' values.

Even if i remove the quotes:

index=kpis|convert timeformat="%Y-%m" ctime(_time) AS year_month|chart count(Event) as events over year_month|append [| gentimes start=[search index="notexisting"|stats count|eval time=relative_time(now(),"-1y")|convert ctime(time) as time timeformat="%m/%d/%Y:%H:%M:%S"  |return $time]  increment=28d|convert timeformat="%Y-%m" ctime(starttime) AS year_month|table year_month|dedup year_month]|fillnull

The error is the same.

Is there a way to pass a relative time to gentimes? and append it to the query?

0 Karma
1 Solution

somesoni2
SplunkTrust
SplunkTrust

Try something like this

 index=kpis|convert timeformat="%Y-%m" ctime(_time) AS year_month|chart count(Event) as events over year_month|append [| gentimes start=-1 |eval temp=mvrange(relative_time(now(),"-1y"),now(),28*86400) | table temp | mvexpand temp | eval  year_month=strftime(temp,"%y-%m") |table year_month|dedup year_month]|fillnull

View solution in original post

somesoni2
SplunkTrust
SplunkTrust

Try something like this

 index=kpis|convert timeformat="%Y-%m" ctime(_time) AS year_month|chart count(Event) as events over year_month|append [| gentimes start=-1 |eval temp=mvrange(relative_time(now(),"-1y"),now(),28*86400) | table temp | mvexpand temp | eval  year_month=strftime(temp,"%y-%m") |table year_month|dedup year_month]|fillnull

diogofgm
SplunkTrust
SplunkTrust

Why not use something like?

index=kpis | timechart span=1mon count(Event) 
------------
Hope I was able to help you. If so, some karma would be appreciated.
0 Karma

nadid
Path Finder

While the answer solves the problem of the months that we have data, does not do the same for the months that we don't have. I'm trying to use gentimes to fill the gaps and to ensure that each month there is data on it.

For instance if i do that that you suggest. The query returns 2015-01 and 2015-02 but not the rest of the months, even if the range i'm using is year to date.

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...