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

时间:2021-05-22

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

import urllibimport urllib2import syses_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"

背景:

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

以上这篇Python 脚本获取ES 存储容量的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章