-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcheck_es_unassigned_shards.py
63 lines (48 loc) · 2 KB
/
check_es_unassigned_shards.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
#!/usr/bin/python
from nagioscheck import NagiosCheck, UsageError
from nagioscheck import PerformanceMetric, Status
import urllib2
import optparse
import base64
try:
import json
except ImportError:
import simplejson as json
class ESShardsCheck(NagiosCheck):
def __init__(self):
NagiosCheck.__init__(self)
self.add_option('H', 'host', 'host', 'The cluster to check')
self.add_option('P', 'port', 'port', 'The ES port - defaults to 9200')
self.add_option('u', 'username', 'username', 'username to login into ES port')
self.add_option('p', 'password', 'password', 'password to login into ES port')
def check(self, opts, args):
host = opts.host
port = int(opts.port or '9200')
username = opts.username
password = opts.password
try:
url=urllib2.Request(r'http://%s:%d/_cluster/health' % (host, port))
if username and password:
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
url.add_header("Authorization","Basic %s" % base64string)
response = urllib2.urlopen(url)
except urllib2.HTTPError, e:
raise Status('unknown', ("API failure", None,
"API failure:\n\n%s" % str(e)))
except urllib2.URLError, e:
raise Status('critical', (e.reason))
response_body = response.read()
try:
es_cluster_health = json.loads(response_body)
except ValueError:
raise Status('unknown', ("API returned nonsense",))
unassigned_shards = es_cluster_health['unassigned_shards']
if unassigned_shards != 0:
raise Status('CRITICAL',
"There are '%s' unassigned shards in the cluster"
% (unassigned_shards))
else:
raise Status('OK',
"All shards in the cluster are currently assigned")
if __name__ == "__main__":
ESShardsCheck().run()