import os
from urllib import request
import requests
import datetime
import json
index_time = (datetime.datetime.now() + datetime.timedelta(days=-0)).strftime("%H%M")
if index_time == '0800':
exit()
###运维钉钉监控
headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "http://oapi.dingtalk.com/robot/send?access_token=4ff92016951904d668485d760efef44a4012f1ea605ece99fe83168ccf4477a6"
ip = ['172.16.196.66', '172.16.196.67', '172.16.196.68']
username = 'elastic'
password = 'XORPossw0rd123!'
for x in ip:
try:
url = 'http://%s:9200/_cat/health?'%(x)
zt = str(requests.get(url, auth=(username, password)).content).rstrip('\n')
zj = zt.rstrip('\n').split()[3]
if zj == "green":
pass
elif zj == "yellow":
json_text = {"msgtype": "text", "at": {"isAtAll": "true"},"text": {"content": "一体化ES集群,状态为yellow" %(ip)}}
requests.post(api_url, json.dumps(json_text), headers=headers).content
else:
json_text = {"msgtype": "text", "at": {"isAtAll": "true"},"text": {"content": "一体化ES集群,状态为red" %(ip)}}
requests.post(api_url, json.dumps(json_text), headers=headers).content
break
except:
json_text = {"msgtype": "text", "at": {"isAtAll": "true"},"text": {"content": "一体化ES集群%s节点状态异常,请检查!" % (x)}}
requests.post(api_url, json.dumps(json_text), headers=headers).content
如上代码:分别监控了es集群状态