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

相关文章

python3.6使用urllib完成下载的实例

如下所示: url = u'http://tieba.baidu.com/f?kw=权利的游戏&ie=utf-8&pn=50' url = quote(url, safe =...

Python操作redis实例小结【String、Hash、List、Set等】

本文实例总结了Python操作redis方法。分享给大家供大家参考,具体如下: python连接方式可参考:/post/161353.htm 这里介绍详细使用 1、String 操作 r...

python中format()函数的简单使用教程

python中format()函数的简单使用教程

先给大家介绍下python中format函数,在文章下面给大家介绍python.format()函数的简单使用 ---恢复内容开始--- python中format函数用于字符串的格式化...

centos 安装Python3 及对应的pip教程详解

安装Python3 安装Python依赖: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-d...

pytorch实现用Resnet提取特征并保存为txt文件的方法

接触pytorch一天,发现pytorch上手的确比TensorFlow更快。可以更方便地实现用预训练的网络提特征。 以下是提取一张jpg图像的特征的程序: # -*- coding...