Splunk Search

毎月最終日のデータのみカウントしたい

haruka_saito
Explorer

毎日取得しているデータがあり、そのうちその月の最終日のデータのみカウントしたいと考えております。
指定月の最終日のみでしたら方法がわかったのですが、月別に取得する方法がわかりません。
どうかご教授お願いいたします。

0 Karma
1 Solution

dkotake_splunk
Splunk Employee
Splunk Employee

こんな感じで汎用的に使えるかと思いますがどうでしょうか?

    <your search> earliest=-30d@d 
    [search <your search> earliest=-30d@d 
    | bucket span=1d _time 
    | stats first(_time) as time1 by date_month 
    | eval time2=time1+(3600*24) 
    | eval string="(_time>=".time1." _time<".time2." date_month=".date_month.")" 
    | stats values(string) as string 
    | nomv string 
    | rex field=string mode=sed "s/\)\s?\(/) OR (/g" 
    | return $string] 
    | stats count by date_month date_mday 
    | eval date=date_month."/".date_mday 
    | table date count

実行例:

index=_internal sourcetype=splunkd earliest=-30d@d 
[search index=_internal sourcetype=splunkd earliest=-30d@d 
| bucket span=1d _time 
| stats first(_time) as time1 by date_month 
| eval time2=time1+(3600*24) 
| eval string="(_time>=".time1." _time<".time2." date_month=".date_month.")" 
| stats values(string) as string 
| nomv string 
| rex field=string mode=sed "s/\)\s?\(/) OR (/g" 
| return $string] 
| stats count by date_month date_mday 
| eval date=date_month."/".date_mday 
| table date count

2つのearliest部分と、実際に検索したいindex等を指定すれば毎月末のみカウントされます。
注意点として、
your search
で指定されるイベントに必ず月末のイベントが含まれている必要があります。
毎月の最も新しいタイムスタンプのイベントを基準に、その日の24時間分をカウント対象にする検索条件です。

View solution in original post

dkotake_splunk
Splunk Employee
Splunk Employee

こんな感じで汎用的に使えるかと思いますがどうでしょうか?

    <your search> earliest=-30d@d 
    [search <your search> earliest=-30d@d 
    | bucket span=1d _time 
    | stats first(_time) as time1 by date_month 
    | eval time2=time1+(3600*24) 
    | eval string="(_time>=".time1." _time<".time2." date_month=".date_month.")" 
    | stats values(string) as string 
    | nomv string 
    | rex field=string mode=sed "s/\)\s?\(/) OR (/g" 
    | return $string] 
    | stats count by date_month date_mday 
    | eval date=date_month."/".date_mday 
    | table date count

実行例:

index=_internal sourcetype=splunkd earliest=-30d@d 
[search index=_internal sourcetype=splunkd earliest=-30d@d 
| bucket span=1d _time 
| stats first(_time) as time1 by date_month 
| eval time2=time1+(3600*24) 
| eval string="(_time>=".time1." _time<".time2." date_month=".date_month.")" 
| stats values(string) as string 
| nomv string 
| rex field=string mode=sed "s/\)\s?\(/) OR (/g" 
| return $string] 
| stats count by date_month date_mday 
| eval date=date_month."/".date_mday 
| table date count

2つのearliest部分と、実際に検索したいindex等を指定すれば毎月末のみカウントされます。
注意点として、
your search
で指定されるイベントに必ず月末のイベントが含まれている必要があります。
毎月の最も新しいタイムスタンプのイベントを基準に、その日の24時間分をカウント対象にする検索条件です。

haruka_saito
Explorer

イメージとしては下記のようにデータを抽出したいです。
2016年3月31日 10件
2016年4月30日 15件
2016年5月31日 20件

よろしくお願いいたします。

0 Karma

melonman
Motivator

例えば、今月(2016年6月9日14時現在)からみた、直近先月の最終日のみのデータを選択するばあいには、以下のように指定します。例として、_internal(Splunk自身のログを溜め込んでいるIndexのデータを対象に実施します。

index=_internal earliest=@mon-1d latest=@mon

上記のearliestの@mon-1dが、今月の頭から1日引いた(つまり先月の最終日)を指定しており、latestでは今月の頭が指定されています。具体的には、以下のような時間範囲になります。

# ./splunk/bin/splunk search 'index=_internal earliest=@mon-1d latest=@mon | head 1 |addinfo | table info_min_time info_max_time | convert ctime(*_time)'

INFO: Your timerange was substituted based on your search string
     info_min_time           info_max_time
----------------------- -----------------------
05/31/2016 00:00:00.000 06/01/2016 00:00:00.000

earliestとlatestで、5月31日0時以降〜6月1日0時未満の時間範囲が指定されます。

ですので、現在の月からみて、先月の最終日のみのデータを単純にcountするには、以下のようにしていただければ行けるとおもいます。

index=_internal earliest=@mon-1d latest=@mon | stats count

いかがでしょうか。

(追記)

index=main (earliest=@mon-1d latest=@mon) OR
 (earliest=-1mon@mon-1d latest=-1mon@mon) OR
 (earliest=-2mon@mon-1d latest=-2mon@mon) OR
 (earliest=-3mon@mon-1d latest=-3mon@mon) OR
 (earliest=-4mon@mon-1d latest=-4mon@mon) OR
 (earliest=-5mon@mon-1d latest=-5mon@mon) OR
 (earliest=-6mon@mon-1d latest=-6mon@mon) 
| bin _time span=1d 
| stats count by _time

より汎用的に行うのであれば、日付の計算を入れてできなくは無いと思いますが、
単純でちょっと強引ですが、上ではどうでしょうか。

結果は以下のようになります。

           _time            count
--------------------------- -----
2016-01-31 00:00:00.000 JST 12711
2016-03-31 00:00:00.000 JST 28067
2016-04-30 00:00:00.000 JST 31543
0 Karma

haruka_saito
Explorer

ご回答いただきありがとうございます。

index=_internal earliest=@mon-1d latest=@mon | stats count
こちらだと、前月の最終日のみのカウントになると思います。

説明がわかりづらかったら申訳ないのですが、
お聞きしたかったのは、下記のように毎月末日のデータのみをカウントするイメージです
2016年3月31日 10件
2016年4月30日 15件
2016年5月31日 20件

カウント方法がわからず、困っています。
どうかご教授お願いいたします。

0 Karma

melonman
Motivator

はじめの回答に追記いたしました、いかがでしょうか。

melonman
Motivator

メモです。

| tstats prestats=T count where index=main by _time span=1d
| where _time=relative_time(now(),"-1mon@mon-1d") OR
 _time=relative_time(now(),"-1mon@mon-1d") OR
 _time=relative_time(now(),"-2mon@mon-1d") OR
 _time=relative_time(now(),"-3mon@mon-1d") OR
 _time=relative_time(now(),"-4mon@mon-1d") OR
 _time=relative_time(now(),"-5mon@mon-1d") 
| stats count by _time
0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...