python定时清理ES 索引

DevOps ELK评论1,230字数 1456阅读4分51秒阅读模式

只保留三天

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import datetime

# 时间转化为字符串

now_time = datetime.datetime.now().strptime(datetime.datetime.now().strftime("%Y.%m.%d"),"%Y.%m.%d")
os.system("curl -XGET http://127.0.0.1:9200/_cat/indices > date.txt")

with open("date.txt","r") as f:
    for line in f.readlines():
        index = line.strip().split()[2]

        try:
            index_strftime = datetime.datetime.strptime(index.split("-")[-1], "%Y.%m.%d")
            Ca = (now_time - index_strftime)
            if str(Ca).split()[0] == "0:00:00":
                continue
            elif int(str(Ca).split()[0]) >= 3:
                command = "curl -XDELETE http://127.0.0.1:9200/%s" % index
                print(command)
                os.system(command)
            else:
                print(index,"no del")
        except:
            pass

添加定时任务

[root@us-prod-sre-eslog-node-2 centos]# crontab -e
00 03 * * * /bin/python /home/centos/delete-es.py
此处为隐藏的内容
注册登录后,方可查看

继续阅读
ELK最后更新:2022-11-4
DevOps
  • 本文由 发表于 2022年8月8日 14:58:41
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
EFK集群[案例] ELK

EFK集群[案例]

Elasticsearch集群配置信息 硬件配置信息 机器名/节点名 IP 内存 cpu 磁盘 us...
评论  0  访客  0

发表评论