requests-清理elasticsearch脚本

#!/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()
posted @ 2021-05-30 15:10  SRE运维充电站  阅读(68)  评论(0编辑  收藏  举报