-
Notifications
You must be signed in to change notification settings - Fork 73
/
ESzabbix.py
executable file
·223 lines (204 loc) · 7.45 KB
/
ESzabbix.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
# Created by Aaron Mildenstein on 19 SEP 2012
# Switchted from pyes to Elasticsearch for better Health Monitoring by Marcel Alburg on 17 JUN 2014
# Adjusted for ELK 2.3 by Javier Barroso on 17 AUG 2016
from elasticsearch import Elasticsearch
import sys
import json
import shelve
import os
import time
# Define the fail message
def zbx_fail():
print "ZBX_NOTSUPPORTED"
sys.exit(2)
def use_cache(file):
if not os.access(file,os.F_OK):
return False
now = int(time.time())
file_modified = int(os.stat(file).st_mtime)
if now - file_modified < 60:
return True
else:
return False
searchkeys = ['query_total', 'fetch_time_in_millis', 'fetch_total', 'fetch_time', 'query_current', 'fetch_current', 'query_time_in_millis']
getkeys = ['missing_total', 'exists_total', 'current', 'time_in_millis', 'missing_time_in_millis', 'exists_time_in_millis', 'total']
docskeys = ['count', 'deleted']
indexingkeys = ['delete_time_in_millis', 'index_total', 'index_current', 'delete_total', 'index_time_in_millis', 'delete_current']
storekeys = ['size_in_bytes', 'throttle_time_in_millis']
cachekeys = ['filter_size_in_bytes', 'field_size_in_bytes', 'field_evictions']
clusterkeys_direct = docskeys + storekeys
clusterkeys_indirect = searchkeys + getkeys + indexingkeys
returnval = None
conn = None
user=str(os.getuid())
clustercache_file = "/tmp/clusterstats.cache." + user
nodescache_file = "/tmp/nodestats.cache." + user
lock_file="/tmp/ESzabbix.lock." + user
# now = time.time()
# logging = open("/tmp/whathappens.log."+str(now)+"-"+sys.argv[1]+"-"+sys.argv[2], "w");
# logging.write(" ".join(sys.argv)+"\n")
# Waiting to somebody write the cache
while os.access(lock_file,os.F_OK):
# logging.write("Waiting a second ...\n")
time.sleep(1)
# __main__
# We need to have two command-line args:
# sys.argv[1]: The node name or "cluster"
# sys.argv[2]: The "key" (status, filter_size_in_bytes, etc)
if len(sys.argv) < 3:
zbx_fail()
# Try to establish a connection to elasticsearch
try:
conn = Elasticsearch('localhost:9200', sniff_on_start=False)
except Exception, e:
zbx_fail()
if sys.argv[1] == 'cluster' and sys.argv[2] in clusterkeys_direct:
nodestats = None
# now=time.strftime("%Y%m%d-%H:%M:%S")
if use_cache(clustercache_file):
# logging.write(str(now) + ": Using cluster cache\n")
nodestats = shelve.open(clustercache_file)
nodestats = nodestats['stats']
else:
# logging.write(str(now) + ": Generate lockfile and cluster cache\n")
lock=open (lock_file, "w")
try:
nodestats = conn.cluster.stats()
shelf = shelve.open(clustercache_file)
shelf['stats']=nodestats
shelf.close()
lock.close()
except Exception, e:
if os.access(lock_file, os.F_OK):
os.remove(lock_file)
zbx_fail()
if os.access(lock_file, os.F_OK):
os.remove(lock_file)
if sys.argv[2] in docskeys:
returnval = nodestats['indices']['docs'][sys.argv[2]]
elif sys.argv[2] in storekeys:
returnval = nodestats['indices']['store'][sys.argv[2]]
elif sys.argv[1] == 'cluster' and sys.argv[2] in clusterkeys_indirect:
nodestats = None
# now=time.strftime("%Y%m%d-%H:%M:%S")
if use_cache(nodescache_file):
# logging.write(str(now)+": Using node cache\n")
nodestats = shelve.open(nodescache_file)
nodestats = nodestats['stats']
else:
# logging.write(str(now)+": Generate lockfile and node cache\n")
lock=open (lock_file, "w")
try:
nodestats = conn.nodes.stats()
shelf = shelve.open(nodescache_file)
shelf['stats']=nodestats
shelf.close()
lock.close()
except Exception, e:
if os.access(lock_file, os.F_OK):
os.remove(lock_file)
zbx_fail()
if os.access(lock_file, os.F_OK):
os.remove(lock_file)
else:
pass # something is wrong if this is executed ...
subtotal = 0
for nodename in conn.nodes.info()['nodes'].keys():
try:
if sys.argv[2] in indexingkeys:
indexstats = nodestats['nodes'][nodename]['indices']['indexing']
elif sys.argv[2] in getkeys:
indexstats = nodestats['nodes'][nodename]['indices']['get']
elif sys.argv[2] in searchkeys:
indexstats = nodestats['nodes'][nodename]['indices']['search']
except Exception, e:
pass
try:
if sys.argv[2] in indexstats:
subtotal += indexstats[sys.argv[2]]
except Exception, e:
pass
returnval = subtotal
elif sys.argv[1] == "cluster":
# Try to pull the managers object data
try:
escluster = conn.cluster
except Exception, e:
if sys.argv[2] == 'status':
returnval = "red"
else:
zbx_fail()
# Try to get a value to match the key provided
try:
returnval = escluster.health()[sys.argv[2]]
except Exception, e:
if sys.argv[2] == 'status':
returnval = "red"
else:
zbx_fail()
# If the key is "status" then we need to map that to an integer
if sys.argv[2] == 'status':
if returnval == 'green':
returnval = 0
elif returnval == 'yellow':
returnval = 1
elif returnval == 'red':
returnval = 2
else:
zbx_fail()
# Mod to check if ES service is up
elif sys.argv[1] == 'service':
if sys.argv[2] == 'status':
if conn.ping():
returnval = 1
else:
returnval = 0
else: # Not clusterwide, check the next arg
nodestats = None
# now=time.strftime("%Y%m%d-%H:%M:%S")
if use_cache(nodescache_file):
# logging.write(str(now)+": Usando cache nodos\n")
nodestats = shelve.open(nodescache_file)
nodestats = nodestats['stats']
else:
# logging.write(str(now)+": Creando lockfile y cache nodo\n")
lock=open (lock_file, "w")
try:
nodestats = conn.nodes.stats()
shelf = shelve.open(nodescache_file)
shelf['stats']=nodestats
shelf.close()
lock.close()
except Exception, e:
if os.access(lock_file, os.F_OK):
os.remove(lock_file)
zbx_fail()
if os.access(lock_file, os.F_OK):
os.remove(lock_file)
for nodename in conn.nodes.info()['nodes'].keys():
if sys.argv[1] in nodestats['nodes'][nodename]['name']:
if sys.argv[2] in indexingkeys:
stats = nodestats['nodes'][nodename]['indices']['indexing']
elif sys.argv[2] in storekeys:
stats = nodestats['nodes'][nodename]['indices']['store']
elif sys.argv[2] in getkeys:
stats = nodestats['nodes'][nodename]['indices']['get']
elif sys.argv[2] in docskeys:
stats = nodestats['nodes'][nodename]['indices']['docs']
elif sys.argv[2] in searchkeys:
stats = nodestats['nodes'][nodename]['indices']['search']
try:
returnval = stats[sys.argv[2]]
except Exception, e:
pass
# now=time.strftime("%Y%m%d-%H:%M:%S")
# logging.write(str(now)+": Finalizando ("+str(returnval)+")\n")
# logging.close()
# If we somehow did not get a value here, that's a problem. Send back the standard
# ZBX_NOTSUPPORTED
if returnval is None:
zbx_fail()
else:
print returnval
# End