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 系统调用的实例详解             &nbs...

Python实现常见的回文字符串算法

回文 利用python 自带的翻转 函数 reversed() def is_plalindrome(string): return string == ''.join(list(...

Python 加密的实例详解

 Python 加密的实例详解 hashlib支持md5,sha1,sha256,sha384,sha512,用法和md5一样  import hashlib...

在python里面运用多继承方法详解

在python里面运用多继承方法详解

如何在PYTHON里面运用多继承 class Father: def hobby(self): print("love to play video game.")...

python实现的二叉树定义与遍历算法实例

python实现的二叉树定义与遍历算法实例

本文实例讲述了python实现的二叉树定义与遍历算法。分享给大家供大家参考,具体如下: 初学python,需要实现一个决策树,首先实践一下利用python实现一个二叉树数据结构。建树的时...