forked from appfirst/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_resque.py
executable file
·96 lines (86 loc) · 3.89 KB
/
check_resque.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
#!/usr/bin/env python
'''
Created on May 31, 2012
@author: Yangming
'''
import nagios
from nagios import CommandBasedPlugin as plugin
import commands
import statsd
import argparse
class ResqueChecker(nagios.BatchStatusPlugin):
def __init__(self, *args, **kwargs):
super(ResqueChecker, self).__init__(*args, **kwargs)
# Hack to determine uniqueness of script defs
check = argparse.ArgumentParser()
check.add_argument("-H", "--host", required=False, type=str)
check.add_argument("-p", "--port", required=False, type=int)
chk, unknown = check.parse_known_args()
self.parser.add_argument("-f", "--filename", required=False, type=str, default='pd@resque_redis-cli')
self.parser.add_argument("-u", "--user", required=False, type=str)
self.parser.add_argument("-s", "--password", required=False, type=str)
self.parser.add_argument("-H", "--host", required=False, type=str)
self.parser.add_argument("-p", "--port", required=False, type=int)
self.parser.add_argument("-n", "--database", required=False, type=int)
self.parser.add_argument("-z", "--appname", required=False, type=str, default='resque')
self.parser.add_argument("--unique", required=False, type=str, default=str(chk.host)+str(chk.port))
@plugin.command("QUEUE_LENGTH")
@statsd.gauge
def get_queue_length(self, request):
stats = {}
query = "smembers resque:queues"
output = self.run_query(request, query)
total = 0
status_code = nagios.Status.OK
query_pattern = "llen resque:queue:%s"
for q in output.split('\n'):
if q:
v = self.run_query(request, query_pattern % q)
stats[q] = int(v)
total += stats[q]
sc = self.verdict(v, request.warn, request.crit)
if sc == nagios.Status.WARNING and status_code == nagios.Status.OK:
status_code = nagios.Status.WARNING
elif sc == nagios.Status.CRITICAL:
status_code = nagios.Status.CRITICAL
r = nagios.Result(request.option, status_code, '%s jobs in queues' % total, request.appname);
r.add_performance_data('total', total, warn=request.warn, crit=request.crit)
for k, v in stats.iteritems():
r.add_performance_data(k, v, warn=request.warn, crit=request.crit)
return r
@plugin.command("JOB_PROCESSED")
@statsd.counter
def get_job_processed(self, request):
query = "get resque:stat:processed"
output = self.run_query(request, query)
if output is None:
delta = 0
else:
value = int(output)
laststats = self.retrieve_last_status(request)
delta = value - laststats.setdefault("processed", 0)
laststats["processed"] = value
self.save_status(request, laststats)
return self.get_result(request, value, '%s jobs in processed' % delta)
def run_query(self, request, query):
cmd_template = "redis-cli --raw"
if request.password is not None:
cmd_template += " -a %s" % request.password
if request.database is not None:
cmd_template += " -n %s" % request.database
if request.host is not None:
cmd_template += " -h %s" % request.host
if request.port is not None:
cmd_template += " -p %s" % request.port
cmd = "%s %s" % (cmd_template, query)
if request.user:
cmd = nagios.rootify(cmd, request.user)
output = commands.getoutput(cmd)
if "command not found" in output:
raise nagios.ServiceInaccessibleError(request, output)
elif output.strip() == "":
raise nagios.ServiceInaccessibleError(request, output)
return output
if __name__ == "__main__":
import sys
ResqueChecker().run(sys.argv[1:])