Splunk Search

カウント結果の増加率を求めたい,カウントの増加率を出したい

clio706
Explorer

お世話になります。

勉強し始めたばかりなので、サーチ文の書き方についてご教示ください。

やりたいことは下記の通りです。

・月次でログの件数をカウントする。
・前月、前々月でカウント数の比較を行う。
・比較結果から増加率を算出する。

宜しくお願い致します。,お世話になります。
学び始めたばかりなので、サーチ文の書き方についてご教示ください。

月次でデータのカウントを取得し、前月分と前々月分で比較をし、
増加率を出したいと思っています。

出力イメージは下記の通りです。

date,Event_A,Event_B,Event_C
2019/09,100,200,50
2019/10,200,100,100
----------- 200%,50%,200%

宜しくお願い致します。

0 Karma
1 Solution

melonman
Motivator

さくっと表示させて終了するのであれば以下で行けるかと思います。

alt text

以下のサーチを実行して、表示をSingle Value, トレリス表示で、フォーマットから割合を指定で出てくると思います。

index="answers784984" earliest=-2mon@mon latest=@mon
| timechart span=month count by event

もう一つの例も記載しておきます。

他の方のコメントも期待しつつ..

$ ./splunk800/bin/splunk search '
index="answers784984" sourcetype="answers784984" earliest=-2mon@mon latest=@mon
| bin _time span=month
| stats count by _time event
| streamstats current=false last(count) as prev by event
| where _time == relative_time(now(), "-1mon@mon" )
| eval m2=strftime(relative_time(now(),"-2mon@mon"),"%Y/%m"), m1=strftime(relative_time(now(),"-1mon@mon"),"%Y/%m")
| eval rate_({m1})/({m2})=round((count/prev)*100)."%", count_{m1} = count, count_{m2} = prev
| table event, count_*, rate_*
'
INFO: Your timerange was substituted based on your search string

 event  count_2019/09 count_2019/10 rate_(2019/10)/(2019/09)
------- ------------- ------------- ------------------------
event_A            72            89 124%
event_B            79            76 96%
event_C            92            74 80%

さっくりですが、以下がやっていることです。

  • 1行目のイベントをフィルタする部分はご自分のものに置き換えていただくとして、earliest=-2mon@mon latest=@mon の部分で、2ヶ月前の1日0時以降〜今月の1日0時未満を時間範囲としてイベントを絞り込む。
  • _time (イベントの時刻情報)を月単位にする。(この時点で、もしイベントがあれば_timeは、2ヶ月前と1ヶ月前のみになるはず)
  • 月とイベントの組み合わせでカウント集計
  • 2ヶ月前のカウント情報を1ヶ月前のカウントの隣に並べる。(上の集計の時点でテーブルが_timeの昇降で並ぶはず)
  • 計算に不要なデータをフィルタアウト
  • 2ヶ月前と1ヶ月前のYYYY/MMを取得(あとでフィールド名としてつかうため)
  • 2ヶ月前のカウントと1ヶ月前のカウントを比較して対前月比を計算、それぞれの月のカウントのフィールド名をcount_YYYY/MMに変更
  • 出力に必要なフィールドのみに絞って出力

指定されているテーブルの出力形式の行と列がひっくりかえってますが、まずはサンプルということで。

ちなみに、データは以下のようなダミーデータを数カ月分いれて確認しました。

2019-11-22 00:10:16 76.202.195.51 event=event_B
2019-11-22 01:10:16 86.156.106.164 event=event_A
2019-11-22 09:10:16 109.55.61.156 event=event_B

いかがでしょか。

View solution in original post

0 Karma

to4kawa
Ultra Champion

こんにちわ
@melonman さんので十分だと思いましたので、少し変わった方面から。

| makeresults count=2
| streamstats count
| eval _time = if (count==2,relative_time(_time,"-3mon@mon"), relative_time(_time,"@mon"))
| makecontinuous 
| eval eventA=random() % 50 + 5 ,eventB=random() % 50 + 5, eventC=random() % 50 + 5
| fillnull count
| where count!=1
| table _time event*
`comment("this is sample data")`
| eval _time=strftime(_time,"%Y/%m")
| transpose header_field=_time
| eval sengetuhi = round('2019/09' / '2019/10' * 100, 1) . "%"
| eval sensengetuhi = round('2019/08' / '2019/10' * 100, 1) . "%"
| rename sengetuhi as "先月比"
| rename sensengetuhi as "先々月比"

この検索例は、どなたのところでも一応動くものということで。
コメントまでが timechartの結果だと思ってください。

コメントの後の evaltranspose で縦横変換した際 _time がそもそもepochなので文字に変換しています。

先々月との計算をするとなるのだと、こちらの形がやりやすいのではないでしょうか?

roundで小数点一桁までにしているところは必要に応じて変更してください。
わざわざ rename を使っているのは日本語フィールド名は正確には対応していないからです。

個人的にはフィールド名が日本語だと計算する時に""が必要だったり、必要なかったりするので、
最後に日本語に変更するようにしています。

0 Karma

clio706
Explorer

ありがとうございます。
いろいろやり方があることが分かり、大変勉強になりました。

0 Karma

melonman
Motivator

さくっと表示させて終了するのであれば以下で行けるかと思います。

alt text

以下のサーチを実行して、表示をSingle Value, トレリス表示で、フォーマットから割合を指定で出てくると思います。

index="answers784984" earliest=-2mon@mon latest=@mon
| timechart span=month count by event

もう一つの例も記載しておきます。

他の方のコメントも期待しつつ..

$ ./splunk800/bin/splunk search '
index="answers784984" sourcetype="answers784984" earliest=-2mon@mon latest=@mon
| bin _time span=month
| stats count by _time event
| streamstats current=false last(count) as prev by event
| where _time == relative_time(now(), "-1mon@mon" )
| eval m2=strftime(relative_time(now(),"-2mon@mon"),"%Y/%m"), m1=strftime(relative_time(now(),"-1mon@mon"),"%Y/%m")
| eval rate_({m1})/({m2})=round((count/prev)*100)."%", count_{m1} = count, count_{m2} = prev
| table event, count_*, rate_*
'
INFO: Your timerange was substituted based on your search string

 event  count_2019/09 count_2019/10 rate_(2019/10)/(2019/09)
------- ------------- ------------- ------------------------
event_A            72            89 124%
event_B            79            76 96%
event_C            92            74 80%

さっくりですが、以下がやっていることです。

  • 1行目のイベントをフィルタする部分はご自分のものに置き換えていただくとして、earliest=-2mon@mon latest=@mon の部分で、2ヶ月前の1日0時以降〜今月の1日0時未満を時間範囲としてイベントを絞り込む。
  • _time (イベントの時刻情報)を月単位にする。(この時点で、もしイベントがあれば_timeは、2ヶ月前と1ヶ月前のみになるはず)
  • 月とイベントの組み合わせでカウント集計
  • 2ヶ月前のカウント情報を1ヶ月前のカウントの隣に並べる。(上の集計の時点でテーブルが_timeの昇降で並ぶはず)
  • 計算に不要なデータをフィルタアウト
  • 2ヶ月前と1ヶ月前のYYYY/MMを取得(あとでフィールド名としてつかうため)
  • 2ヶ月前のカウントと1ヶ月前のカウントを比較して対前月比を計算、それぞれの月のカウントのフィールド名をcount_YYYY/MMに変更
  • 出力に必要なフィールドのみに絞って出力

指定されているテーブルの出力形式の行と列がひっくりかえってますが、まずはサンプルということで。

ちなみに、データは以下のようなダミーデータを数カ月分いれて確認しました。

2019-11-22 00:10:16 76.202.195.51 event=event_B
2019-11-22 01:10:16 86.156.106.164 event=event_A
2019-11-22 09:10:16 109.55.61.156 event=event_B

いかがでしょか。

0 Karma

clio706
Explorer

ありがとうございます。
想定通りの結果を抽出することが出来ました。
詳細の説明まであり、非常に勉強になりました。

0 Karma

melonman
Motivator

回答へAcceptいただけると助かります!

0 Karma

darrenfuller
Contributor

ファイルの数またはイベントの数をカウントしますか?

0 Karma

clio706
Explorer

ありがとうございます。
イベントの数をカウント致します。

0 Karma
Get Updates on the Splunk Community!

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...

.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 ...