Python 脚本获取ES 存储容量的实例

yipeiwu_com6年前Python基础

最近有需求统计ES存储容量,之前用PHP实现的,考虑到以后可能会经常写脚本查询,故用python写了一个脚本,代码如下:

import urllib
import urllib2
import sys
es_service_addr = sys.argv[1]
 
url = "http://" + es_service_addr + "/_cat/indices?v";
req = urllib2.Request(url)
res_data = urllib2.urlopen(req)
res = res_data.read()
 
list = res.split('\n')
 
title = list[0].split()
length = len(list)
data = list[1:length]
map={}
for i in title:
	map[i] = title.index(i)
capacity_used = 0;
 
for i in data:
	value = i.split()
	l = len(value)
	if l > 0 :
		store_size = value[map['store.size']].lower()
		if "k" in store_size:
			capacity_used += int(store_size[:-1]) * 1024
		elif "m" in store_size:
			capacity_used += int(store_size[:-1]) * 1024 * 1024
		elif "g" in store_size:
			capacity_used += int(store_size[:-1]) * 1024 * 1024 * 1024
		elif "p" in store_size:
			capacity_used += int(store_size[:-1]) * 1024 * 1024 * 1024 * 1024
		elif "p" in store_size:
			capacity_used += int(store_size[:-1]) * 1024 * 1024 * 1024 * 1024 * 1024
		else:
			capacity_used += int(store_size[:-1])
 
print str(capacity_used) + " Bytes"

背景:

Python 脚本获取ES 存储容量

通过ES 查询的结果如图所示,脚本实现的作用就是统计store.size 的值。

以上这篇Python 脚本获取ES 存储容量的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

基于python的七种经典排序算法(推荐)

基于python的七种经典排序算法(推荐)

一、排序的基本概念和分类 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。排序算法,就是如何使得记录按照要求排列的方法。 排序的稳定性: 经过某...

详解Python with/as使用说明

with/as 使用open打开过文件的对with/as都已经非常熟悉,其实with/as是对try/finally的一种替代方案。 当某个对象支持一种称为"环境管理协议"的协议时,就...

python中aioysql(异步操作MySQL)的方法

python异步IO初探 探索异步IO执之前,先说说IO的种类 1.阻塞IO最简单,即读写数据时,需要等待操作完成,才能继续执行。进阶的做法就是用多线程来处理需要IO的部分,缺点是开销会...

python 内置函数filter

python 内置函数filter class filter(object): """ filter(function or None, iterable) --> fil...

基于python log取对数详解

log()方法返回x的自然对数,对于x>0。 语法 以下是log()方法的语法: import math math.log( x ) 注意:此函数是无法直接访问的,所以我...