监控elasticsearch cluster 集群
通过主动模式将数据发送给zabbix server
1 import json
2 import struct
3 import socket
4 import re
5 from elasticsearch import Elasticsearch
6
7
8 def get_dict_value(res):
9 for k, v in res.items():
10 value = v
11 return value
12
13 def get_metric_trapper(hosts):
14 sending_data = {'request': 'sender data', 'data': []}
15 es = Elasticsearch(hosts=hosts)
16 res = es.nodes.stats(node_id='_local')['nodes']
17 v = get_dict_value(res)
18 host_name = v['name']
19 for key_info in monitor_key_info:
20 key_one = key_info['key_one']
21 key_two = key_info['key_two']
22 key_three = key_info['key_three']
23 sending_data['data'].append({
24 'host': host_name,
25 'value': v[key_one][key_two][key_three],
26 'key': 'es.{}.{}.{}'.format(key_one, key_two, key_three)
27 })
28 return sending_data
29
30 def send_data(sender_data):
31 code = 0
32 if len(sender_data['data']) == 0:
33 code = 3
34 return code
35 mydata = json.dumps(sender_data)
36 data_length = len(mydata)
37 data_header = str(struct.pack('q', data_length))
38 data_to_send = 'ZBXD1' + str(data_header) + str(mydata)
39 socket.setdefaulttimeout(30)
40 try:
41 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
42 sock.connect(('zabbix-IP', 10051))
43 sock.send(data_to_send)
44 except Exception as e:
45 print(e)
46 code = 255
47 return code
48 response_header = sock.recv(5)
49 if not response_header == 'ZBXD1':
50 code = 254
51 return code
52 response_data_header = sock.recv(8)
53 response_data_header = response_data_header[:4]
54 response_len = struct.unpack('i', response_data_header)[0]
55 response_raw = sock.recv(response_len)
56 sock.close()
57 response = json.loads(response_raw)
58 match = re.match('^.*failed.+?(d+).*$', response['info'].lower() if 'info' in response else '')
59 if match is None:
60 code = 2
61 return code
62 else:
63 fails = int(match.group(1))
64 if fails > 0:
65 code = 1
66 return code
67 return code
68
69
70 if __name__ == '__main__':
71 hosts = 'ip:9200'
72 monitor_key_info = [
73 {'key_one': 'jvm', 'key_two': 'mem', 'key_three': 'heap_used_percent'},
74 {'key_one': 'jvm', 'key_two': 'mem', 'key_three': 'heap_used_in_bytes'},
75 {'key_one': 'jvm', 'key_two': 'mem', 'key_three': 'heap_max_in_bytes'},
76 {'key_one': 'indices', 'key_two': 'store', 'key_three': 'size_in_bytes'},
77 {'key_one': 'indices', 'key_two': 'segments', 'key_three': 'count'},
78 {'key_one': 'indices', 'key_two': 'search', 'key_three': 'query_time_in_millis'},
79 {'key_one': 'indices', 'key_two': 'indexing', 'key_three': 'index_time_in_millis'},
80 {'key_one': 'indices', 'key_two': 'docs', 'key_three': 'count'},
81 {'key_one': 'indices', 'key_two': 'docs', 'key_three': 'deleted'},
82 {'key_one': 'os', 'key_two': 'cpu', 'key_three': 'percent'},
83 ]
84 v = get_metric_trapper(hosts=hosts)
85 relust = send_data(v)
86 print(relust)