LoginSignup
1
1

More than 3 years have passed since last update.

検索時刻帯を変更する

Posted at

Splunkのindexの中の時間はepochtime。
ユーザの時刻帯に応じて表示や検索期間が決まる。

Splunkを世界的に使っているところだとユーザの時刻帯はバラバラということもある(らしい)
個人的には専用のAppとユーザ、ダッシュボードを作って時刻帯を統一すればいいと思うのだけど、そうもいかない人向けのクエリー

UTCが欲しいとネタが被っているといえば被っている。

SPL

tz_change.spl
| makeresults 
| addinfo 
| eval local_tz=strftime(_time,"%:::z") 
| eval tz_EST="-5"
| eval time_arg=if(tz_EST - local_tz < 0 , "@d".(tz_EST - local_tz)."h","@d+".printf("%2d",tz_EST - local_tz)."h")
| eval local_earliest=round(relative_time(info_min_time,"@d"))
| eval earliest=round(relative_time(info_min_time,time_arg))
| fields - _* info*
| foreach *earliest [ eval human_readable_<<FIELD>> = strftime('<<FIELD>>' , "%FT%T")]

過去24時間の検索を東部標準時間(EST)の00:00からに合わせている。

earliest human_readable_earliest human_readable_local_earliest local_earliest local_tz time_arg tz_EST
1583629200 2020-03-08T10:00:00 2020-03-09T00:00:00 1583679600 +09 @d-14h -5

使用方法

usage.spl
index=main sourcetype=foo search_word [ makeresults
....
| fields earliest
| format "" "" "" "" "" ""]

解説

  • addinfotime pickerearliestlatestの値を取り出している。
  • %:::zで時刻帯を2桁の数字で取り出す。 cf. Date and time format variables
  • relative_timeの引数をevalで作成
  • printf()を使用しているのは+00(UTC)の時にうまく動かすため。
  • EST(東部標準時 (Eastern Time Zone))なのは元ネタがこれなので。
  • formatearliestをそのままの形でメインサーチに渡している。

まとめ

addinfoの時間もユーザの時刻帯に引っ張られるので、一方のログがUTCで他のログがJSTとかだとログに書いてある時間と検索期間、_timeの値が違うので戸惑う時がある。
ユーザの時刻帯に関わらずいっそUTCで検索してやるってのをクエリーでやる場合はこんな形でやるといいと思います :sweat:その場面てあるのかな

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1