-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_twemproxy
executable file
·64 lines (56 loc) · 2.04 KB
/
check_twemproxy
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
#!/usr/bin/env python
# vim: set syntax=python
#
# This is a simple health check for twemproxy.
# This is pretty much a straight port of the ruby version hosted at:
# https://github.com/wanelo/nagios-checks/blob/master/check_twemproxy
#
import os
import sys
import argparse
import time
try:
import json
except ImportError:
import simplejson as json
# Nagios return codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# parse command line options
parser = argparse.ArgumentParser(description='Check health of twemproxy.')
parser.add_argument('--host', default='localhost', help='The hostname of the server to check', dest='host')
parser.add_argument('--port', default='22222', help='The stats port of the server to check', dest='port')
args = parser.parse_args()
# load and parse data from twemproxy stats port
first_raw = os.popen("nc %(host)s %(port)s" % { 'host': args.host, 'port': args.port }).read()
time.sleep(2)
second_raw = os.popen("nc %(host)s %(port)s" % { 'host': args.host, 'port': args.port }).read()
first_run = json.loads(first_raw)
second_run = json.loads(second_raw)
errors = {}
error_pools = {}
# scan data for errors
for pool, pool_info in second_run.items():
if hasattr(pool_info, 'items'):
for server, server_info in pool_info.items():
if hasattr(server_info, 'items'):
connections = int(server_info['server_connections'])
diff = int(server_info['requests']) - int(first_run[pool][server]['requests'])
if connections > 0:
continue
if diff == 0:
continue
errors[server] = 1
error_pools[pool] = 1
problem = ''
if error_pools:
problem += "error with pools [%s]\n" % ",".join(error_pools.keys())
problem += "problem with servers: %s" % ",".join(errors.keys())
if not problem:
print("TWEMPROXY OK : %s" % args.host)
sys.exit(STATE_OK)
else:
print("TWEMPROXY CRITICAL : %(host)s %(problem)s" % { 'host': args.host, 'problem': problem })
sys.exit(STATE_CRITICAL)