Python用于清理elasticsearch脚本~

王婆卖瓜,自卖自夸一下,这代码越写越规范了,真香!
用于清理elasticsearch数据,放着自己记录看一下。第一次用yield
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' elasticsearch clean '''
__author__ = 'jiangjw'

import requests
import heapq
import traceback
import datetime


class Esclean(object):
    def __init__(self, host, port):
        self.host = host
        self.port = str(port)

    def setting(self):
        self.urlName = 'http://' + self.host + ":" + self.port + '/_cat/indices'
        self.urlDel = 'http://' + self.host + ':' + self.port + '/'

    def getName(self):
        try:
            dataResponse = requests.get(self.urlName)
            dataResponse.raise_for_status()
            dataName = dataResponse.text
            # 循环每一行,再通过split截取需要的部分,写入列表
            for line in dataName.splitlines():
                name = line.split(' ')[2]
                yield name
        except:
            traceback.print_exc()

    def filterName(self):
        today = datetime.date.today()
        todayNumber = int(str(today).replace('-', ""))
        self.filebeat = {}
        for name in self.getName():
            # 截取最后一截,用于比对
            nameDate = name.replace('.', '-').split('-')[-3:]
            nameDate = ''.join(nameDate)
            if nameDate.isdigit():
                nameDate = int(nameDate)
                if nameDate > 99999999:
                    nameDate = str(nameDate)[-6:]
                    self.filebeat[nameDate] = name
                    continue
                if nameDate + 2 <= todayNumber:
                    delName = name
                    yield delName

    def filterBeat(self):
        beatName = heapq.nlargest(1, self.filebeat)
        for i in self.filebeat:
            if i not in beatName:
                yield self.filebeat.get(i)

    def deleteName(self):
        for u in self.filterName():
            try:
                deleteUrl = self.urlDel + u
                response = requests.delete(url=deleteUrl)
                response.raise_for_status()
            except:
                traceback.print_exc()

        for f in self.filterBeat():
            try:
                deleteUrl = self.urlDel + f
                response = requests.delete(url=deleteUrl)
                response.raise_for_status()
            except:
                traceback.print_exc()


def main():
    start = datetime.datetime.now()
    host = '192.168.110.174'
    port = 9200
    es = Esclean(host=host, port=port)
    es.setting()
    es.deleteName()
    end = datetime.datetime.now()
    print("{}运行完成,脚本共耗时:{}s".format(start, (end - start).seconds))


if __name__ == '__main__':
    main()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值