kle 日志收集系统维护之清理索引及索引优化脚本
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 日志收集系统维护之清理索引及索引优化脚本的更多相关文章
- 用fabric部署维护kle日志收集系统
最近搞了一个logstash kafka elasticsearch kibana 整合部署的日志收集系统.部署参考lagstash + elasticsearch + kibana 3 + kafk ...
- Docker搭建EFK日志收集系统,并自定义es索引名
EFK架构图 一.EFK简介 EFK不是一个软件,而是一套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用,是目前主流的一种日志系统. EFK是三个开源软件的缩写,分 ...
- 基于Flume的美团日志收集系统 架构和设计 改进和优化
3种解决办法 https://tech.meituan.com/mt-log-system-arch.html 基于Flume的美团日志收集系统(一)架构和设计 - https://tech.meit ...
- Docker日志收集最佳实践
传统日志处理 说到日志,我们以前处理日志的方式如下: · 日志写到本机磁盘上 · 通常仅用于排查线上问题,很少用于数据分析 ·需要时登录到机器上,用grep.awk等工具分析 那么,这种方式有什么缺点 ...
- K8S学习笔记之k8s日志收集实战
0x00 简介 本文主要介绍在k8s中收集应用的日志方案,应用运行中日志,一般情况下都需要收集存储到一个集中的日志管理系统中,可以方便对日志进行分析统计,监控,甚至用于机器学习,智能分析应用系统问题, ...
- 用ELK搭建简单的日志收集分析系统【转】
缘起 在微服务开发过程中,一般都会利用多台服务器做分布式部署,如何能够把分散在各个服务器中的日志归集起来做分析处理,是一个微服务服务需要考虑的一个因素. 搭建一个日志系统 搭建一个日志系统需要考虑一下 ...
- 日志收集系统搭建-BELK
前言 日志是我们分析系统运行情况.问题定位.优化分析等主要数据源头.目前,主流的业务系统都采用了分布式.微服务的形式.如果想要查看日志,就需要从不同的节点上去查看,而且对于整个业务链路也非常不清晰.因 ...
- ELK+kafka构建日志收集系统
ELK+kafka构建日志收集系统 原文 http://lx.wxqrcode.com/index.php/post/101.html 背景: 最近线上上了ELK,但是只用了一台Redis在 ...
- [转载] 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等
原文: http://www.36dsj.com/archives/25042 接上一部分:一共81个,开源大数据处理工具汇总(上),第二部分主要收集整理的内容主要有日志收集系统.消息系统.分布式服务 ...
随机推荐
- 半期考html5小游戏制作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- unix系统内核优点
1.可靠性高 unix的可靠性2.伸缩性强 unix的伸缩性3.开放性好 unix的开放性4.网络功能强 unix的网络功能这是UNIX系统的又一重要特色,特别是作为Internet网络技术基础的TC ...
- java 队列的使用(转载)
转载声明:http://blog.csdn.net/lzy_lizhiyang/article/details/48311925 先我们要知道使用队列的目的是什么?一般情况下,如果是一些及时消息的处理 ...
- Alpha 冲刺5
队名:日不落战队 安琪(队长) 今天完成的任务 组织第五次站立式会议(半冲刺总结交流会). 完成草稿箱前端界面. 明天的计划 回收站前端界面. 尝试去调用数据. 还剩下的任务 信息修改前端界面. 遇到 ...
- nodepad++ 格式化xml插件
1.用插件管理器安装xmltools插件 2.使用如下菜单格式化xml: 所有插件下载地址: http://sourceforge.net/projects/npp-plugins/files/
- IP ,路由
ifconfig 命令 ip信息 enp0s3: flags=4163<UP(已经启用),BROADCAST(支持广播),RUNNING,MULTICAST(支持多播)> ...
- 词法分析用c++实现的
#include<stdio.h>#include<string.h>int i,j,k,sign,flag,number,run;char ch;char word[10]; ...
- Kafka发布订阅消息
Maven <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-cli ...
- 【经典数据结构】Trie
在计算机科学中,trie,又称前缀树或字典树,是一种有种树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有子孙都有相同的前 ...
- UVA11737_Extreme Primitive Society
这是隐藏的最深的一个水题.其隐藏性能如此之好,是因为题目的描述十分蛋疼,写了好多好多的废话. 让我们这种过不了六级的孩子情何以堪啊. 是这样的,给你若干个矩形,每次在所有的矩形中两两组合形成许多许多新 ...