只保留七天的索引
shell版
#!/bin/bash #hexm@2016.10.18 #只保留一周es日志 logName=( 51-nginxaccesslog 51-nginxerrorlog 51-phperrorlog ) #es配置文件 config=/usr/local/app/elasticsearch-2.3.4/config/elasticsearch.yml #日期 time=`date -d "7 day ago" +-%Y.%m.%d` ip=`grep "network.host" ${config} | awk '{print$2}'` port=`grep "http.port" ${config} | awk '{print$2}'` #es监听的ip和端口 ipPort=${ip}:${port} #循环删除 for ((i=0;i<${#logName[*]};i++)) do name=${logName[$i]}${time} curl -XDELETE "http://${ipPort}/${name}" done
python版
#!/usr/bin/python # -*- coding:utf-8 -*- #hexm@2016.10.18 #只保留一周es日志 # import commands from datetime import datetime, timedelta config = "/usr/local/app/elasticsearch-2.3.4/config/elasticsearch.yml" logName = ('51-nginxaccesslog', '51-nginxerrorlog', '51-phperrorlog') ip = commands.getoutput(""" grep "network.host" %s | awk '{print$2}' """ % config) port = commands.getoutput(""" grep "http.port" %s | awk '{print$2}' """ % config) tm = datetime.now() + timedelta(days=-7) tm = tm.strftime("%Y.%m.%d") for name in logName: url = "http://" + str(ip) + ":" + str(port) + "/" + name + "-" + tm print url
每天凌晨三点执行
###删除es索引 00 03 * * * /bin/bash /root/bin/delete_es.sh &>/dev/null &