附9 elasticsearch-curator + Linux定时任务

时间:2022-10-17 08:32:46

官网教程入口:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/index.html

一、下载安装

  • 下载:sudo pip install elasticsearch-curator
  • 更新:sudo pip install -U elasticsearch-curator

版本:curator4.1.0

二、配置文件

1、config.yml

 # Remember, leave a key empty if there is no value.  None will be a string,
# not a Python "NoneType"
client:
hosts: ["127.0.0.1:9200"]
url_prefix:
use_ssl: False
certificate:
client_cert:
client_key:
aws_key:
aws_secret_key:
aws_region:
ssl_no_validate: False
http_auth:
timeout: 30
master_only: False logging:
loglevel: INFO
logfile:
logformat: default
blacklist: ['elasticsearch', 'urllib3']

说明:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/configfile.html

2、action.yml

 ---
# Remember, leave a key empty if there is no value. None will be a string,
# not a Python "NoneType"
#
# Also remember that all examples have 'disable_action' set to True. If you
# want to use this action as a template, be sure to set this to False after
# copying it.
actions:
1:
action: delete_indices
description: "Delete indices older than 45 days (based on index name), for logstash- prefixed indices.
Ignore the error if the filter does not result in an actionable list of indices (ignore_empty_list) and exit cleanly"
options:
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: logstash-
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 5
exclude:

注意:action上边那个1是action id,必须得有

说明:

三、测试

curator --config ~/Desktop/server/elk/es-curator-4.1.0/config.yml ~/Desktop/server/elk/es-curator-4.1.0/action.yml

四、做成Linux定时任务

1、编写sh脚本:curator-delete-index.sh

#!/bin/sh
/usr/local/bin/curator --config /Users/enniu1/Desktop/server/elk/es-curator-4.1.0/config.yml /Users/enniu1/Desktop/server/elk/es-curator-4.1.0/action.yml
echo "delete index success"

注意:最好使用命令的全路径名,否则可能找不到。

2、赋予权限

  • chmod 777 /Users/enniu1/Desktop/server/elk/es-curator-4.1.0/curator-delete-index.sh

3、创建定时任务:crontab

  • crontab -e:打开了vi,输入:

    • 30 16 * * * /Users/enniu1/Desktop/server/elk/es-curator-4.1.0/curator-delete-index.sh,之后保存退出vi

  • crontab -l:查看所有的root用户的定时任务

附:crontab的文件格式

分 时 日 月 星期 要运行的命令

  • 第1列分钟1~59
  • 第2列小时1~23(0表示子夜)
  • 第3列日1~31
  • 第4列月1~12
  • 第5列星期0~7(0和7表示星期天)
  • 第6列要运行的命令