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设计】。

相关文章

tensorflow 输出权重到csv或txt的实例

实例如下所示: import numpy as np W_val, b_val = sess.run([weights_tensor, biases_tensor]) np.save...

python3的UnicodeDecodeError解决方法

python3的UnicodeDecodeError解决方法

爬虫部分解码异常 response.content.decode() # 默认使用 utf-8 出现解码异常 以下是设计的通用解码 通过 text 获取编码 # 通过...

PyQt5下拉式复选框QComboCheckBox的实例

PyQt5下拉式复选框QComboCheckBox的实例

笔者在用PyQt5写GUI时碰到了需要使用下拉式复选框的情况,但是PyQt5中没有相应的组件,而网上找到的方法大多是qt使用的,所以不能直接拿来用。 没办法,在这种让人无奈的情况下,笔者...

在Pycharm terminal中字体大小设置的方法

如下所示: file->settings->Editor->General->Console里面的console commands history size 以上...

CentOS 7下Python 2.7升级至Python3.6.1的实战教程

CentOS 7下Python 2.7升级至Python3.6.1的实战教程

前言 大家应该都知道,Centos是目前最为流行的Linux服务器系统,其默认的Python 2.x,但是根据python社区的规划,在不久之后,整个社区将向Python3迁移,且将不在...