やあ、みんな
だよ
いつもの作者は「記事の内容がよくわからない」と言われて凹んだので、僕が呼ばれたよ
よろしくね。
今回はみんながよく使うtimechartコマンドを説明するよ。
Macosxで動かしているので、WindowsやLinuxの人はディレクトリやフォルダを読み替えてね。
今回使うもの
時間:過去24時間やモード:スマートモードは変更しないでね
timechartについて
Splunk>docsはこちら
対応する統計量の表を持つ時系列チャートを作成します。
だって。
基本的な構文は
| コマンド | 集計かeval | by | 列で分割するフィールド |
|---|---|---|---|
| timechart | count | by | host |
になるよ。 オプションでよく使うのは
| オプション | 説明 |
|---|---|
| span | 集計する時間間隔 _過去24時間_の検索だと span=30minとなる。 |
| limit | 表示する列数、その以外の列は_OTHER_として集計 デフォルトだと11以上の列は_OTHER_となる。 |
| useother | _OTHER_を表示を制御。 limitで制限していなければ全ての列の表示となる |
かな。
基本的な使い方
index=_internal
| timechart count as Counts
| _time | Counts |
|---|---|
| 2020/05/09 06:00:00 | 8700 |
| 2020/05/09 06:30:00 | ... |
とても簡単な検索。index=_internalはSplunk自体のログで指定した時間、今回だと_過去24時間_で数えてる。
あとあと使うときはコマンドの名前は変更したほうがいいよね。
基本的な使い方(byで区切る)
index=_internal
| timechart count as Counts by sourcetype
| _time | mlspl-3 | mongod | scheduler | splunk_archiver-2 | splunk_web_access | splunk_web_service | splunkd | splunkd_access | splunkd_stdout | splunkd_ui_access | OTHER |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 2020/05/09 06:00:00 | 0 | 0 | 1 | 1 | 0 | 0 | 8676 | 22 | 0 | 0 | 0 |
| 2020/05/09 06:30:00 | 0 | 0 | 1 | 1 | 0 | 0 | 8676 | 22 | 0 | 0 | ... |
さっきと少し変えてby sourcetypeをつけて検索すると、soucetypeで列が区切られた表ができたよ。
![]()
soucetypeの値が10以上あるから、数が少ないものはOTHERでまとめられているね。
基本的な使い方(limitで区切る)
index=_internal
| timechart limit=5 count as Counts by sourcetype
| _time | mongod | splunk_web_service | splunkd | splunkd_access | splunkd_ui_access | OTHER |
|---|---|---|---|---|---|---|
| 2020/05/09 06:00:00 | 0 | 0 | 8676 | 22 | 0 | 2 |
| 2020/05/09 06:30:00 | 0 | 0 | 8676 | 22 | 0 | ... |
今度はさっきの検索にlimitをつけてみたよ。表示する列を制限できているのがわかるよね。
基本的な使い方(usuotherでOTHERを消す)
index=_internal
| timechart limit=5 useother=false count as Counts by sourcetype
| _time | mongod | splunk_web_service | splunkd | splunkd_access | splunkd_ui_access
|:--|--:|--:|--:|--:|--:|--:|
| 2020/05/09 06:00:00 | 0 | 0 | 8676 | 22 | 0 |
| 2020/05/09 06:30:00 | 0 | 0 | 8676 | 22 | ...|
今度はさっきの検索にuseother=falseをつけてみたよ。_OTHER_がなくなったよね。
基本的な使い方(limitなしでusuotherでOTHERを消す)
index=_internal
| timechart useother=false count as Counts by sourcetype
| _time | mlspl-3 | mongod | scheduler | splunk_archiver-2 | splunk_web_access | splunk_web_service | splunkd | splunkd_access | splunkd_stdout | splunkd_ui_access |
|:--|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:|--:|
| 2020/05/09 06:00:00 | 0 | 0 | 1 | 1 | 0 | 0 | 8676 | 22 | 0 | 0 |
| 2020/05/09 06:30:00 | 0 | 0 | 1 | 1 | 0 | 0 | 8676 | 22 | 0 | ... |
今度はさっきの検索から limitをはずしてみたよ。
基本的な使い方(byで区切る)の結果から_OTHER_がなくなったよね。
ただなくなっただけだから全部の値を表示しているわけではないよ。
基本的な使い方(全ての値を表示)
index=_internal
| timechart limit=0 count as Counts by sourcetype
| _time | mlspl-3 | mongod | scheduler | splunk_archiver-2 | splunk_btool | splunk_web_access | splunk_web_service | splunkd | splunkd_access | splunkd_conf | splunkd_stderr | splunkd_stdout | splunkd_ui_access |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2020/05/09 06:00:00 | 0 | 0 | 1 | 1 | 0 | 3 | 4 | 9299 | 61 | 0 | 0 | 0 | 843 |
| 2020/05/09 06:30:00 | 0 | 0 | 1 | 1 | 0 | 3 | 4 | 9299 | 61 | 0 | 0 | 0 | ... |
soucetypeは13種類あったんだね。limit=0で全部表示してくれたよ。
応用編(evalとuntable)
さっきまでで基本的な使い方は大丈夫だよね。
これからは応用編。いきなりすごい事になってるけど気にしないでね。
index=_internal
| timechart limit=5 useother=f span=10min count as Counts by sourcetype
| untable _time sourcetype Counts
| timechart span=1h eval(round(avg(Counts),0)) by sourcetype
| _time | mongod | splunk_web_service | splunkd | splunkd_access | splunkd_ui_access |
|---|---|---|---|---|---|
| 2020/05/09 07:00 | 0 | 2 | 3082 | 15 | 235 |
| 2020/05/09 08:00 | 22 | 45 | 3162 | 47 | 393 |
| 2020/05/09 09:00 | 43 | 89 | 3343 | 182 | ... |
![]()
timechartはevalが使えるんだ!と言いたいだけなのに、なんでこんなクエリー作っちゃうんだろう。説明がたいへんだよ・・・。
ちょうどいいからuntableも説明してみよう。
timechartとuntableとstats
いきなりだけど、次の2つの検索をやってみて、結果を比べて欲しいな。
index=_internal
| timechart limit=5 useother=f span=10min count as Counts by sourcetype
| untable _time sourcetype Counts
index=_internal
| bin _time span=10min
| stats count as Counts by _time sourcetype
| _time | sourcetype | Counts |
|---|---|---|
| 2020/05/09 07:00:00 | splunk_web_access | 3 |
| 2020/05/09 07:00:00 | splunk_web_service | 4 |
| 2020/05/09 07:00:00 | splunkd | 3101 |
| 2020/05/09 07:00:00 | splunkd_access | 28 |
| 2020/05/09 07:00:00 | splunkd_ui_access | ... |
出てきた結果の列は一緒だね。
でも timechartは5つのsourcetypeの10分毎の数がでているのに、statsはない時間やsourcetypeも時間によってはないよね
このように timechartは指定した時間で表を作ってくれるんだ。これがtimechartの特徴なんだよ。
なので検索する時には、単純にログに書かれている時間だけを集計したいのか、それとも特定の時間内での数を集計したいのかでtimechartとbin stats使い分けるといいよ。
![]()
timechartは普通に使っていると表示の制限がかかっているので、間違いやすいというとこもあるよ。
untableのことを話していないって?
| _time | mongod | splunk_web_service | splunkd | splunkd_access | splunkd_ui_access |
|---|---|---|---|---|---|
| 2020/05/09 07:00 | 0 | 2 | 3082 | 15 | 235 |
| 2020/05/09 08:00 | 22 | 45 | 3162 | 47 | 393 |
このあと、この数字をいろいろ加工しようとした時、困っちゃうから、untableしようね。
![]()
timechartの後はこれ、と覚えるといいよ。
| コマンド | 引数1 | 引数2 | 引数3 |
|---|---|---|---|
| untable | _time | timechart byの引数 | 値の列名 |
詳しい話はSplunk>docsを読んでね。
eval
ようやくevalに戻ってきた。
index=_internal
| timechart limit=5 useother=f span=10min count as Counts by sourcetype
| untable _time sourcetype Counts
| timechart span=1h eval(round(avg(Counts),0)) by sourcetype
前の話が長すぎて忘れちゃったから、も一回だすね。
このクエリーはいったん10分毎集計して、1時間毎の平均を出しているんだ。
![]()
timechartはevalが使えるから、avg()を使っても数字がきれいにできるんだよ。
If you use an eval expression, the split-by clause is required.
eval式を使用する場合はsplit-by句が必要です。
なので、byで列を分ける時に使えるんだよ
合計値に対してのパーセント
パーセント表示がみんな好きそうだから、やり方を書いておくね
index=_internal
| timechart limit=5 useother=f span=1h count as Counts by sourcetype
| untable _time sourcetype Counts
| eventstats sum(Counts) as total by sourcetype
| eval perc = round(Counts / total * 100,2)."%"
いったんtimechartで時間毎の表を作った後untableして、eventstatsで合計を計算するんだ。
あとはevalで計算するとパーセントがでるよ。
お好みでtableとか使って綺麗にしてね。
これをstatsで作ると、さっき説明したとおり、ない時間とかができるから、あんまりよくないよね。
まとめ
いろいろと話してきたけど、大丈夫かな?
![]()
timechartはもっといろいろなことができるんだけど、これ以上話すと👹が出てきそうなのでやめとくね。
じゃ、またね〜
リクエストまってま〜す
SPLはガチなので、結構わかりづらいかもしれない。