LoginSignup
27
33

More than 5 years have passed since last update.

elasticsearch-curatorを使って簡単に時系列インデックスを運用する

Last updated at Posted at 2015-02-24
※curatorを新しい環境にインストールしたらコマンドラインがだいぶ変わってたので更新 試した環境は、[curator, version 3.0.3]
気付いた変更点サマリ
  • CentOS6.5でcuratorコマンドが動くようになった
  • --show-indicesがindices
  • deleteとかの削除日数指定する--older-thanを使うにあたり必須のオプションが出来た ※ない場合は、curator側で丁寧にこれ足りないとエラー出力してくれるからあまりはまらないとは思うが。。

概要

fluentd経由でElasticsearchにlogstash形式のインデックス使って、kibanaで可視化っていう流れでたまにAPI経由でインデックスを消しながら運用してたけど、そろそろ自動でインデックス運用しないといけないと思った。
twitterで @johtani さんにelasticsearch-curatorを教えてもらったので、検証した見た結果を書いてみる。

インストール

CentOSだと素直に動かなくて、少しはまった。。

$ pip install elasticsearch-curator
試しにshowしてみたら下記エラーが
$ curator show --show-indices
Traceback (most recent call last):
  File "/usr/bin/curator", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py", line 2603, in <module>
    """Split a string or iterable thereof into (section,content) pairs
  File "/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py", line 666, in require
    for callback in self.callbacks:
  File "/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg/pkg_resources.py", line 565, in resolve
    )
pkg_resources.DistributionNotFound: elasticsearch>=1.0.0,<2.0.0

調べてみると、こちらの公式QAの内容にあるように、CentOSではうまく動かないので、フルパスで実行せよとの事だった。

※2015/05/18現在CentOS6.5で問題なくcuratorコマンド動くようになっていた!
$ curator show indices --prefix=syslog-
syslog-2014.12
syslog-2015.01
syslog-2015.02
こんな長いパス毎回うっては非常に辛いので、aliasを下記のように切っておくことにした。

※ちなみに、スクリプトの場合は、対話式でないのでalias効かないからフルパスを指定してセット。

~/.bashrc
alias curator='/usr/bin/python /usr/lib/python2.6/site-packages/curator/curator_script.py'

検証してみた コマンドラインが変更してたので更新!(2015/05/18)

下記のように、[aaa-]というprefixをつけたインデックスを作り、deleteの操作を試してみた。

aaaプレフィックスがついたインデックス一覧を表示
$curator show indices --prefix aaa-
aaa-2014.12.01
aaa-2015.01.01
aaa-2015.02.01
aaa-2015.12.31
過去30日分を残して他を削除

下記のような形で処理が標準出力されるので、何をしているかわかりやすい。

$ curator delete --prefix aaa- --older-than 30 --time-unit days --timestring %Y.%m.%d
2015-02-24 11:33:00,587 INFO      Job starting...
2015-02-24 11:33:00,590 INFO      Deleting indices...
2015-02-24 11:33:00,636 INFO      delete_index operation succeeded on aaa-2014.12.01
2015-02-24 11:33:00,652 INFO      delete_index operation succeeded on aaa-2015.01.01
2015-02-24 11:33:00,653 INFO      aaa-2015.02.01 is within the threshold period (30 days).
2015-02-24 11:33:00,653 INFO      aaa-2015.12.31 is within the threshold period (30 days).
2015-02-24 11:33:00,653 INFO      Specified indices deleted.
2015-02-24 11:33:00,653 INFO      Done in 0:00:00.075631.

実際に消えていることを確認

$ curator show indices --prefix aaa-
aaa-2015.02.01
aaa-2015.12.31

クローズという、インデックスは削除せずに検索対象から外すコマンドも試してみた

$ curator close --prefix aaa- --older-than 10 --time-unit days --timestring %Y.%m.%d
2015-02-24 12:02:00,104 INFO      Job starting...
2015-02-24 12:02:00,106 INFO      Closing indices...
2015-02-24 12:02:00,130 INFO      close_index operation succeeded on aaa-2015.02.01
2015-02-24 12:02:00,131 INFO      aaa-2015.12.31 is within the threshold period (10 days).
2015-02-24 12:02:00,131 INFO      Closed specified indices.
2015-02-24 12:02:00,131 INFO      Done in 
0:00:00.037177.

メタデータは残るので、showでも見えるが検索対象からは除外されるのでメモリの有効利用は出来る

$ curator show indices --prefix aaa-
aaa-2015.02.01
aaa-2015.12.31

実際は、特定期間過ぎたものはdeleteして、定期的に検索対象にしなくてよいものはcloseみたいな感じでcrondを回せばいいかなと思う。

とりあえず下記のような感じでcron登録して毎日3時にインデックス削除するようにしてみた。

#0 3 * * * /usr/bin/python /usr/lib/python2.6/site-packages/curator/curator_script.py delete --prefix=aaa- --older-than 30
#0 3 * * * curator delete --prefix=aaa- --older-than 30 --time-unit days --timestring %Y.%m.%d

コマンドメモ

show

オプション 意味
indices インデックス一覧を表示
--prefix インデックスのプレフィックスを指定

サンプル

$ curator show indices --prefix syslog-
aaa-2014.12.01
aaa-2015.01.01
aaa-2015.02.0
aaa-2015.02.01
aaa-2015.12.31

delete

条件にマッチしたインデックスを削除

オプション 意味
--prefix インデックスのプレフィックスを指定
--older-than 指定日数分のインデックスを残し、それ以前を削除する ex 30 これを使う時は --timestringと--time-unitの指定が必須
--disk-space 指定した容量以上のインデックスを削除 ex 1024
--timestring 時間指定のパターンを指定 ex %Y.%m.%d
--time-unit [hours,days,weeks,months] デフォルトはdays

close

条件にマッチしたインデックスを削除はしないものの、検索対象から外す(openで復帰する事が出来る)

オプションはdeleteと同じ

こりゃ簡単に出来るし、便利だ!

参照:
http://blog.johtani.info/blog/2014/01/24/curator-tending-your-time-series-indices-in-japanese/
http://blog.johtani.info/blog/2014/07/28/curator-2-0-and-1-1/

27
33
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
27
33