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实现博客上进行自动翻页

先上一张代码及代码运行后的输出结果的图! 下面上代码: # coding=utf-8 import os import time from selenium import web...

Python实现的径向基(RBF)神经网络示例

本文实例讲述了Python实现的径向基(RBF)神经网络。分享给大家供大家参考,具体如下: from numpy import array, append, vstack, tran...

python+pyqt5实现KFC点餐收银系统

python+pyqt5实现KFC点餐收银系统

本文实例为大家分享了python实现KFC点餐收银系统的具体代码,供大家参考,具体内容如下 这个kfc收银系统我实现了的以下功能: 1.正常餐品结算和找零。 2.基本套餐结算和找零。 3...

python set集合使用方法解析

这篇文章主要介绍了python set集合使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 定义 定义:在{}中用逗号隔开...

python如何使用socketserver模块实现并发聊天

这篇文章主要介绍了python如何使用socketserver模块实现并发聊天,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 利用so...