python监控es状态

需求

用python写一个监控es状态的脚本

实例

监控es的机器

1
2
3
4
5
6
7
8
9
10
11
配置文件

[root@Ops-script monitor_elasticsearch]# cat escluster_ip.ini
[mail]
name = sy@xxx.com
[tax_es]
cluster_ip = 192.168.50.7:9200,192.168.50.8:9200,192.168.50.9:9200
name_pass = none,none
[analysis_es]
cluster_ip = 192.168.50.24:9200,192.168.50.25:9200
name_pass = none,none

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[root@Ops-script monitor_elasticsearch]# cat monitor_elastic_cluster.py

import urllib,socket,time,json
import logging,requests,configparser,os

# 定义日志格式读取配置文件
cur_path = os.path.dirname(os.path.realpath(__file__))
logging.basicConfig(format='[ %(asctime)s ] -- %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=cur_path+"/scripts.log",
level=logging.INFO)
config_path = os.path.join(cur_path,"escluster_ip.ini")
conf = configparser.ConfigParser()
conf.read(config_path)
esgroup = conf.sections()

def GetClusterIp():
# 按集群名循环检查,集群名为配置文件中的标题
for group in esgroup:
# 获取到 mail 中的邮件地址不再继续循环
if group == "mail":
mailname = conf.get(group,"name")
continue
# 获取到一个集群 ip 后进行 get 请求
iplist = conf.get(group,"cluster_ip").split(",")
for esip in iplist:
url = "http://%s/_cat/health"%esip
try:
# 超时时间为 3 秒, request 的请求值不是200的将全部置为400
# 如果是请求成功,则获取 status 的状态和 status_num 的百分比
response = requests.get(url,timeout=3)
request = response.status_code
status = response.text.split()[3]
status_num = response.text.split()[-1]
except:
request = 400
# 请求值为 200 后,检查 status 值非 green 状态发送报警邮件,并不在继续检查本集群内的剩余节点
if request == 200:
if status != "green":
mailtitle = "elasticsearch集群 [ %s ], ip为:%s ,查状态: %s, 状态百分比: %s"%(group,esip,status,status_num)
mailtxt = "elasticsearch集群 [ %s ]\n\nip为: [ %s ]\n\n检查状态为: %s\n\n状态百分比: %s"%(group,esip,status,status_num)
command = "echo -e %s%s%s | mail -s %s%s%s %s"%('"',mailtxt,'"','"',mailtitle,'"',mailname)
logging.info(mailtitle)
os.system(command)
break
# 请求值不是 200 ,报警后继续检查集群内剩余 ip
else:
mailtitle = "elasticsearch集群 [ %s ], ip为:%s, 请求超时,请检查端口"%(group,esip)
mailtxt = "elasticsearch集群 [ %s ]\n\nip: [ %s ]\n\n请求超时,请检查端口"%(group,esip)
command = "echo -e %s%s%s | mail -s %s%s%s %s"%('"',mailtxt,'"','"',mailtitle,'"',mailname)
logging.info(mailtitle)
os.system(command)

if __name__ == "__main__":
GetClusterIp()
Donate