logstash每天往es建好索引,按天生成,就目前的需求,需要清理不需要的数据,以保证最新日志的速度展示,哈哈,瞎搞了这个脚本,路过的大神批评。

#!/usr/bin/env python
# coding:utf8
#author: shantuwqk@163.com import sys,datetime
import os,time,commands HOST = '192.168.35.125'
PORT = '9200' #排除不需要删除的索引列表
NotDEL = ['g1_sms_collect'] def Del_Indices(last4):
TMP_LIST = []
DEL_DAY_LIST = []
file_info = '/tmp/es_indices_list.txt'
CURLCMD = 'curl http://%s:%s/_cat/indices?v > %s'%(HOST,PORT,file_info)
CURLINDEX_INFO = "cat /tmp/es_indices_list.txt | grep -v 'health status index'|egrep '*-%s'|awk '{print $3}'"%last4
#DEL_DAY_INFO = "%s|awk '{print $3}'"%CURLINDEX_INFO s,v = commands.getstatusoutput(CURLCMD)
if s == 0:
with open(file_info,'r') as f:
st,vi = commands.getstatusoutput(CURLINDEX_INFO)
TMP_LIST.append(vi)
#DEL_DAY_LIST.append(vi)
else:
print "\033[31;1m curl cmd execute faild\033[0m"
# print DEL_DAY_LIST
for i in TMP_LIST[0].split('\n'):
DEL_DAY_LIST.append(i)
#sd,vd = commands.getstatusoutput('curl -XDELETE http://%s:%s/%s'%(HOST,PORT,i))
#print sd,vd,
print DEL_DAY_LIST
try:
for t in NotDEL:
DEL_DAY_LIST.remove("%s-%s"%(t,last4))
except Exception:
pass
for index in DEL_DAY_LIST:
sd,vd = commands.getstatusoutput('curl -XDELETE http://%s:%s/%s'%(HOST,PORT,index)) if __name__ == '__main__':
last4day = (datetime.date.today() - datetime.timedelta(days=3)).strftime('%Y.%m.%d')
#print last4day
Del_Indices(last4day)

最近由于es shard 老出问题, 在网上看有优化的功能,所以又扩展了以下脚本:做日常定时任务,主要保含清理和优化索引功能

#!/usr/bin/env python
# coding:utf8
#author:shantuwqk@163.com import sys,datetime
import os,time,commands
import sys,os HOST = 'localhost'
PORT = '9200'
#ES_ROOT = os.path.dirname(os.path.dirname(__file__))
ES_ROOT = '/'.join(os.getcwd().split('/')[:-1])
ES_BIN = ES_ROOT + '/bin'
ES_INDICES = ES_ROOT + '/data/elasticsearch/nodes/0/indices'
ES_OPTIMIZE = "_optimize"
ES_DEL_VAL = "_optimize?only_expunge_deletes=true"
print ES_ROOT
print ES_BIN
print ES_INDICES def Indiceslist(date):
indiceslist = []
s,v = commands.getstatusoutput("ls %s -l| awk '{print $9}'|egrep \"*-%s\""%(ES_INDICES,date))
#print "#########indices",s,v
if s == 0:
for i in v.split('\n'):
indiceslist.append(i)
return indiceslist
#print indiceslist def Del_Indices(date):
NotDEL = ['g1_sms_collect']
DEL_DAY_LIST = []
print date,
print Indiceslist(date)
for i in Indiceslist(date):
print i,
DEL_DAY_LIST.append(i)
try:
for t in NotDEL:
DEL_DAY_LIST.remove("%s-%s"%(t,date))
except Exception:
pass
print DEL_DAY_LIST
for index in DEL_DAY_LIST:
sd,vd = commands.getstatusoutput('curl -XDELETE http://%s:%s/%s'%(HOST,PORT,index))
if sd == 0:
print "DEL indices Sucess !!"
else:
print "DEL indices Failed !!"
ds,dv = commands.getstatusoutput('curl -XPOST http://%s:%s/%s/%s'%(HOST,PORT,index,ES_DEL_VAL))
if ds == 0:
print "Cel indices Sucess !!"
else:
print "Cel indices Failed !!" def es_optimize(date):
#print Indiceslist(date)
for i in Indiceslist(date):
print "curl -XPOST \"http://%s:%s/%s/%s\""%(HOST,PORT,i,ES_OPTIMIZE)
s,v = commands.getstatusoutput("curl -XPOST \"http://%s:%s/%s/%s\""%(HOST,PORT,i,ES_OPTIMIZE))
print s,v,
if s == 0:
print "\033[32;1mES Optimize Success !!!\033[0m"
else:
print "\033[31;1m Es Optimze Failed!!!\033[0m" def handle_cluster_status():
clustet_status = "curl -XGET \'http://localhost:9200/_cluster/health?pretty=true\'" def help_prompt():
print """
This program prints files to the standard output.
Options include:
--version : Prints the version number
--help : Helpful tips
--task : To operate on missions [delindices|optimze]
sample : python es_manage.py --task delindices
: python es_manage.py --task optimze
""" def error_prompt():
print '\033[31;1mUnknown option.Please refer to the help...\033[0m'
print "==============================================================="
help_prompt()
print "---------------------------------------------------------------"
sys.exit(1) if __name__ == '__main__': if len(sys.argv) == 1:
print "\033[31;1m No action specified.\033[0m"
help_prompt()
sys.exit(1)
if len(sys.argv) == 2 and sys.argv[1].startswith('--'):
if sys.argv[1][2:] == 'version':
print 'Version 1.0'
sys.exit(1)
elif sys.argv[1][2:] == 'help':
help_prompt()
if len(sys.argv) == 3 and sys.argv[1][2:] == 'task' and sys.argv[2] == "delindices":
DelDay = (datetime.date.today() - datetime.timedelta(days=3)).strftime('%Y.%m.%d')
Del_Indices(DelDay)
elif len(sys.argv) == 3 and sys.argv[1][2:] == 'task' and sys.argv[2] == "optimze":
yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime('%Y.%m.%d')
today = (datetime.date.today() - datetime.timedelta(days=0)).strftime('%Y.%m.%d')
print es_optimize(yesterday)
es_optimize(today)
else:
error_prompt()
exit(1)

kle 日志收集系统维护之清理索引及索引优化脚本的更多相关文章

  1. 用fabric部署维护kle日志收集系统

    最近搞了一个logstash kafka elasticsearch kibana 整合部署的日志收集系统.部署参考lagstash + elasticsearch + kibana 3 + kafk ...

  2. Docker搭建EFK日志收集系统,并自定义es索引名

    EFK架构图 一.EFK简介 EFK不是一个软件,而是一套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用,是目前主流的一种日志系统. EFK是三个开源软件的缩写,分 ...

  3. 基于Flume的美团日志收集系统 架构和设计 改进和优化

    3种解决办法 https://tech.meituan.com/mt-log-system-arch.html 基于Flume的美团日志收集系统(一)架构和设计 - https://tech.meit ...

  4. Docker日志收集最佳实践

    传统日志处理 说到日志,我们以前处理日志的方式如下: · 日志写到本机磁盘上 · 通常仅用于排查线上问题,很少用于数据分析 ·需要时登录到机器上,用grep.awk等工具分析 那么,这种方式有什么缺点 ...

  5. K8S学习笔记之k8s日志收集实战

    0x00 简介 本文主要介绍在k8s中收集应用的日志方案,应用运行中日志,一般情况下都需要收集存储到一个集中的日志管理系统中,可以方便对日志进行分析统计,监控,甚至用于机器学习,智能分析应用系统问题, ...

  6. 用ELK搭建简单的日志收集分析系统【转】

    缘起 在微服务开发过程中,一般都会利用多台服务器做分布式部署,如何能够把分散在各个服务器中的日志归集起来做分析处理,是一个微服务服务需要考虑的一个因素. 搭建一个日志系统 搭建一个日志系统需要考虑一下 ...

  7. 日志收集系统搭建-BELK

    前言 日志是我们分析系统运行情况.问题定位.优化分析等主要数据源头.目前,主流的业务系统都采用了分布式.微服务的形式.如果想要查看日志,就需要从不同的节点上去查看,而且对于整个业务链路也非常不清晰.因 ...

  8. ELK+kafka构建日志收集系统

    ELK+kafka构建日志收集系统   原文  http://lx.wxqrcode.com/index.php/post/101.html   背景: 最近线上上了ELK,但是只用了一台Redis在 ...

  9. [转载] 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等

    原文: http://www.36dsj.com/archives/25042 接上一部分:一共81个,开源大数据处理工具汇总(上),第二部分主要收集整理的内容主要有日志收集系统.消息系统.分布式服务 ...

随机推荐

  1. 软工1816 · 第八次作业(课堂实战)- 项目UML设计(团队)

    本次作业博客 团队信息 队名:起床一起肝活队 原组长: 白晨曦(101) 原组员: 李麒 (123) 陈德斌(104) 何裕捷(214) 黄培鑫(217) 王焕仁(233) 林志华(128) 乐忠豪( ...

  2. Redis的sentinel机制(sentinel节点IP为:192.168.23.10) “哨兵”

    万一主节点打击,主从模型将会停止工作,为了解决这个问题,Redis提供了一个sentinel(哨兵),以此来实现主从切换的功能,一旦主节点宕机了,sentinel将会在从节点中挑一个作为主节点.与zo ...

  3. (十三)Jmeter之Bean Shell 的使用(二)

    该文章来自:http://www.cnblogs.com/puresoul/p/5092628.html 上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean she ...

  4. Js 中实现重定向的几种方式

    之所以要总结这个,是因为在项目中使用了 AJAX,当请求成功时需要重定向到另一个页面. 1 . <script type="text/javascript"> wind ...

  5. [OS] 系统调用

  6. Codeforces707Div2

    A Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was ups ...

  7. 修改apache的注册表值提高系统权限

    In Windows environments when a service is registered with the system a new key is created in the reg ...

  8. Linux内核分析第五周——扒开系统调用的“三层皮”(下)

    Linux内核分析第五周--扒开系统调用的"三层皮"(下) 李雪琦+原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.1 ...

  9. vmware中ubuntu虚拟机扩容

    https://blog.csdn.net/ldzm_edu/article/details/78893721

  10. Java高级应用之泛型与反射20170627

    /*************************************************************************************************** ...