forked from matt448/nagios-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_rabbitmq_cluster.py
executable file
·139 lines (111 loc) · 4.25 KB
/
check_rabbitmq_cluster.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
#!/usr/bin/python
##########################################################
#
# Written by Matthew McMillan
# @matthewmcmillan
# https://matthewcmcmillan.blogspot.com
# https://github.com/matt448/nagios-checks
#
#
# This Nagios check looks at the status of nodes in a RabbitMQ cluster
# and alerts if any node is not running or the minimum number of nodes
# is not present in the cluster. This check only needs to be run on one
# node in the cluster.
#
# Requires the rabbitmqadmin command to be installed. Edit rabbitmqadminCmd
# if the rabbitmqadmin command isn't in /usr/local/bin.
#
import sys
import re
import argparse
import json
from pprint import pprint
from subprocess import check_output
from subprocess import check_call
def printUsage():
print
print "Example: ", sys.argv[0], "--host somehost --port 15672 --minnodes 2"
print
#Parse command line arguments
parser = argparse.ArgumentParser(description='This script is a Nagios check that \
monitors nodes in a rabbitMQ cluster.')
parser.add_argument('--host', dest='host', type=str, required=True,
help='Hostname of rabbitmq server.')
parser.add_argument('--port', dest='port', type=int, required=True,
help='Port number for rabbitmqadmin http management interface.')
parser.add_argument('--minnodes', dest='minnodes', type=int, required=True,
help='Minimum number of nodes required in the cluster.')
parser.add_argument('--debug', action='store_true', help='Enable debug output.')
args = parser.parse_args()
# Assign command line args to variable names
hostName = args.host
portNum = args.port
minNodes = args.minnodes
rabbitmqadminCmd = '/usr/local/bin/rabbitmqadmin'
responseData = []
depthList = []
statusMsgList = []
statusMsg = ""
msgLine = ""
perfdataMsg = ""
warnCount = 0
critCount = 0
exitCode = 3
#Run rabbitmqctl command to dump list of queues
output=check_output([rabbitmqadminCmd, "list", "nodes", "--format=raw_json", "--port="+str(portNum), "--host="+hostName ])
if args.debug:
print '----------------OUTPUT------------------'
print str(output)
print '----------------------------------------'
##########################################
#Parse the JSON data and put it in a dict
responseData = json.loads(output)
###########################################
# Find number of nodes and compare against
# the minnodes value
nodeCount = len(responseData)
if args.debug:
print 'NUMBER OF NODES: '+str(nodeCount)
if nodeCount < minNodes:
if args.debug:
print 'ERROR: only ' + str(nodeCount) + ' of ' + str(minNodes) + ' nodes found'
statusMsgList.append('[CRIT: ' + str(nodeCount) + ' of ' + str(minNodes) + ' Nodes found]')
critCount += 1 #Increment critCount to indicate error state
else:
statusMsgList.append('[OK: ' + str(nodeCount) + ' of ' + str(minNodes) + ' Nodes found]')
##################################################
# Loop through the nodes in the responseData and
# check the running state for each.
for node in responseData:
if args.debug:
print node['name'] + ': ' + str(node['running']);
if not node['running']:
critCount += 1 #Increment critCount to indicate error state
#Add node status to output message list
statusMsgList.append('[NODE:' + node['name'] + ' STATUS:*NOT RUNNING*]')
else:
statusMsgList.append('[NODE:' + node['name'] + ' STATUS:running]')
# Set exit code based on number of warnings and criticals
if warnCount == 0 and critCount == 0:
statusMsgList.insert(0, "OK - RabbitMQ Cluster")
exitCode = 0
elif warnCount > 0 and critCount == 0:
statusMsgList.insert(0, "WARNING - RabbitMQ Cluster")
exitCode = 1
elif critCount > 0:
statusMsgList.insert(0, "CRITICAL - RabbitMQ Cluster")
exitCode = 2
else:
statusMsgList.insert(0, "UNKNOWN - RabbitMQ Cluster")
exitCode = 3
# Build status message output
for msg in statusMsgList:
statusMsg += msg + " "
# Build perfdata output
#for index in range(len(qList)):
# perfdataMsg += qList[index] + "=" + str(depthList[index]) + ";" + str(warnDepth) + ";" + str(critDepth) + "; "
# Print final output for Nagios
print statusMsg + "|" + perfdataMsg
# Exit with appropriate code
exit(exitCode)