es自适应需求监控
#_*_coding:utf-8 _*_
from elasticsearch import Elasticsearch
import re
import time
from dingding import dd_info
import json
class Es_monitor():
def __init__(self):
###es连接
self.connect = Elasticsearch("http://xxx:9200",http_auth=('f', 'o'),port=9200)
def es_status(self):
mes = self.connect.cat.health()
if re.search("red",mes):
data = '{"content":{"es_cluster_status":"es集群状态为red"}}'
elif re.search("yellow", mes):
data = '{"content":{"es_cluster_status":"es集群状态为yellow"}}'
dd_info(data)
def pool(self):
mes = self.connect.cat.nodes(h="n")
nodesname = mes[0:-1].strip().split('\n')
try:
for node in nodesname:
jsonData = self.connect.nodes.stats(node_id=node)
nodeID = jsonData['nodes'].keys()
search = jsonData['nodes'][list(nodeID)[0]]['thread_pool']['search']['threads']
load = jsonData['nodes'][list(nodeID)[0]]['os']['cpu']['load_average']
cpu = jsonData['nodes'][list(nodeID)[0]]['process']['cpu']['percent']
#write = jsonData['nodes'][list(nodeID)[0]]['thread_pool']['write']['threads']
if search > 100:
data = '{"content":{"es_search_pool":"%s节点es查询线程池大于100"}}'%(node)
dd_info(data)
#elif load['1m'] > 20:
# data = '{"content":{"es_load":"%s节点负载[load(1m):%s,load(5m):%s]"}}'%(node,load['1m'],load['5m'])
# dd_info(data)
elif cpu > 95:
data = '{"content":{"es_cpu":"%s节点cpu使用率为%s"}}'%(node,cpu)
dd_info(data)
except:
pass
def es_search_rate(self):
mes =self.connect.indices.stats()
time.sleep(1)
mes_2 = self.connect.indices.stats()
search_result = ( mes_2['_all']['primaries']['search']['query_total'] - mes['_all']['primaries']['search']['query_total'])
#write_result = (mes_2['_all']['total']['indexing']['index_total'] - mes['_all']['total']['indexing']['index_total']) / 8
order_search_rate = mes_2['indices']['-96-2020-2']['primaries']['search']['query_total'] - mes['indices']['-96-2020-2']['primaries']['search']['query_total']
customer_search_rate = mes_2['indices']['tms-pro-customer-95']['primaries']['search']['query_total'] - mes['indices']['tms-pro-customer-95']['primaries']['search']['query_total']
try:
if search_result > 8000:
data = '{"content":{"es_search_rate":"es查询qps大于8000,当前值:%s/s"}}'%(search_result)
dd_info(data)
elif order_search_rate > 1300:
data = '{"content":{"es_search_rate":"Order索引查询qps大于1300,当前值:%s/s"}}'%(order_search_rate)
dd_info(data)
elif customer_search_rate> 1300:
data = '{"content":{"es_search_rate":"Customer索引查询qps大于800,当前值:%s/s"}}'%(customer_search_rate)
dd_info(data)
except:
pass
def main(self):
#self.es_status()
self.pool()
self.es_search_rate()
if __name__ == '__main__':
ES = Es_monitor()
##es集群状态
ES.main()
钉钉告警
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import sys
import json
import datetime
import requests
def dd_info(data):
url = 'https://oapi.dingtalk.com/robot/'
#url = 'https://open.feishu.cn/open-apis/bot/'
headers = {
'Content-Type': 'application/json;charset=utf-8'
}
formdata = {
"msgtype": "text",
"text": {"content": data
}
}
mes = requests.post(url=url, data=json.dumps(formdata), headers=headers)
mes.text
if __name__ == '__main__':
dd_info()
查询索引监控
import os
import time
import json
def mes(index):
message = os.popen('curl -s -u xxx:xxx -XGET "http://xxxxx:9200/%s/_stats"'%(index))
m = message.read()
message.close()
messages = json.loads(m)
mes_qps = messages['_all']['primaries']['search']['query_total']
mes_millis = messages['_all']['primaries']['search']['query_time_in_millis']
return mes_qps,mes_millis
def read():
while True:
###查询值存入字典,做对比。
values = dict()
values['qps'] = mes()[0]
values['millis'] = mes()[1]
time.sleep(10)
###单次索引查询,9为分片数
if int(mes()[0]) - 9 == int(values['qps']):
value = (int(mes()[1]) - int(values['millis'])) / 9
print(value)
###查询无延迟
elif int(values['qps']) == int(mes()[0]):
value = int(mes()[1]) - int(values['millis'])
print(value)
###均次查询
else:
value = (int(mes()[1]) - int(values['millis'])) / (int(mes()[0]) - int(values['qps']))
print(value)
#else:
# num = (int(mes()[0]) - int(values['qps']))
# values = int(mes()[1]) - int(values['millis'])
# value= int(values) / int(num)
if __name__ == '__main__':
read(‘xxxxx’)
写入数据监控
import os
import time
import json
def mes():
message = os.popen('curl -s -u xxx:xxxx -XGET "http://xxxxx:9200/xxxxxxxx/_stats/indexing"')
m = message.read()
message.close()
messages = json.loads(m)
mes_write = messages['_all']['primaries']['indexing']['index_total']
return mes_write
def write():
while True:
###查询值存入字典,做对比。
values = dict()
values['write'] = mes()
time.sleep(10)
###索引写入计算
value = (int(mes())-int(values['write']))/9
print(value)
if __name__ == '__main__':
write()