智能處理elk集羣索引日誌

#!/usr/bin/python
#encoding:utf-8
import requests
import time
import datetime
import commands
import re
import paramiko
import string
#date_time = time.strftime('%Y.%m.%d')
#fiveDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 5))
#fiveDayAgo_style = fiveDayAgo.strftime("%Y.%m.%d")
#index='logstash-' + fiveDayAgo_style

def produce_index_kaoqin():
index_kaoqin = commands.getoutput("curl -XGET -u credit:credit 'http://10.24.0.13:900/_cat/indices/kaoqin-*'|awk '{print $3}'")
global index_kaoqin_list
index_kaoqin_list = index_kaoqin.split('\n')
del index_kaoqin_list[0]
del index_kaoqin_list[0]
del index_kaoqin_list[0]
index_kaoqin_list.sort()
def produce_index_filebeat():
index_filebeat = commands.getoutput("curl -XGET -u credit:credit 'http://10.24.0.13:900/_cat/indices/filebeat-*'|awk '{print $3}'")
global index_filebeat_list
index_filebeat_list = index_filebeat.split('\n')
del index_filebeat_list[0]
del index_filebeat_list[0]
del index_filebeat_list[0]
index_filebeat_list.sort()
def produce_index_logstash():
index_logstash = commands.getoutput("curl -XGET -u credit:credit 'http://10.24.0.13:900/_cat/indices/logstash-*'|awk '{print $3}'")
global index_logstash_list
index_logstash_list = index_logstash.split('\n')
del index_logstash_list[0]
del index_logstash_list[0]
del index_logstash_list[0]
index_logstash_list.sort()

def diskspace_data_get(host_ip,command):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#client.connect(SRC_IP,username='root',password='520157000')
client.connect(host_ip)
stdin,stdout,stdeer = client.exec_command(command)
global result_diskspace
result_diskspace = stdout.read()
client.close()

if name == 'main':
command_opt = "df -h |awk '$6 ~ /opt/{print $5}'|awk -F'%' '{print $1}'"
command_other = "df -h |awk 'NR==2{print $5}'|awk -F'%' '{print $1}'"
ip_list = ['10.24.0.110','10.24.0.111','10.24.0.132','10.24.0.133','10.24.0.134']
for host_ip in ip_list:
if host_ip == '10.24.0.133' or host_ip == '10.24.0.134':
diskspace_data_get(host_ip,command_opt)
else:
diskspace_data_get(host_ip,command_other)

    result_diskspace_center = re.sub(r'\s','',result_diskspace)
    result_diskspace_center_int = int(result_diskspace_center)
    print result_diskspace_center_int
    while True:
        if result_diskspace_center_int >= 80:
            produce_index_filebeat()
            produce_index_logstash()
            produce_index_kaoqin()
            count_kaoqin = 0
            count_logstash = 0
            count_filebeat = 0
            print host_ip
            for index_filebeat in index_filebeat_list:
                count_filebeat += 1
                url = ['http://','10.24.0.13',':900/',index_filebeat]
                url_name = ''.join(url)
                command = ['curl -XDELETE -u credit:credit',' ',url_name]
                command_filebeat = ''.join(command)
                filebeat_delete = commands.getoutput(command_filebeat)
                print index_filebeat
                if count_filebeat > 1:
                    break
            for index_kaoqin in index_kaoqin_list:
                count_kaoqin += 1
                url = ['http://','10.24.0.13',':900/',index_kaoqin]
                url_name = ''.join(url)
                command = ['curl -XDELETE -u credit:credit',' ',url_name]
                command_kaoqin = ''.join(command)
                kaoqin_delete = commands.getoutput(command_kaoqin)
                print index_kaoqin
                if count_kaoqin > 1:
                    break
            for index_logstash in index_logstash_list:
                count_logstash += 1
                url = ['http://','10.214.0.13',':900/',index_logstash]
                url_name = ''.join(url)
                command = ['curl -XDELETE -u credit:credit',' ',url_name]
                command_logstash = ''.join(command)
                logstash_delete = commands.getoutput(command_logstash)
                print index_logstash
                if count_logstash > 0:
                    break
            time.sleep(60)

            if host_ip == '10.24.0.13' or host_ip == '10.24.0.13':
                            diskspace_data_get(host_ip,command_opt)
                    else:
                            diskspace_data_get(host_ip,command_other)

                    result_diskspace_center = re.sub(r'\s','',result_diskspace)
                    result_diskspace_center_int = int(result_diskspace_center)
            print result_diskspace_center_int
            if result_diskspace_center_int < 80:
                print "磁盤容量是 %s 已經正常"%result_diskspace_center_int
                break

        else:
            break
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章