monitor-utils/nagios/check_modjk.py

98 lines
2.5 KiB
Python
Raw Normal View History

2013-12-25 16:44:10 +01:00
#!/usr/bin/env python
'''
Nagios compatible plugin to check Apache Modjk
requires
- Python >= 2.6
- status worker enable
'''
from optparse import OptionParser
import urllib2
import re
EXIT_CODE = {
2013-12-29 09:28:36 +01:00
'OK': 0,
'WARN': 1,
'CRIT': 2,
'UNKNOWN': 3,
2013-12-25 16:44:10 +01:00
}
2014-01-01 17:14:13 +01:00
def prepare_opts():
2013-12-29 09:28:36 +01:00
'''
Parse option from the shell
'''
def help():
2014-01-01 17:14:13 +01:00
print 'How many workers are in OK state and Activated'
2013-12-29 09:28:36 +01:00
print ''
parser.print_help()
def err( string ):
print 'Error: {0}'.format( string )
help()
exit(1)
parser = OptionParser()
parser.add_option('-u', '--url', dest='url', type='string', help='modjk status worker url')
parser.add_option('-c', '--critical', dest='critical', type='int', help='warning threshold', default=-1)
parser.add_option('-w', '--warning', dest='warning', type='int', help='critical threshold', default=-1)
parser.add_option('-t', '--timeout', dest='timeout', type='float', help='how many seconds to wait for each http request', default=5)
(opts, args) = parser.parse_args()
# Input Validation
if not opts.url:
err('missing Modjk Status http url')
2014-01-01 17:14:13 +01:00
if opts.warning < opts.critical:
err('-w can not be smaller than -c')
2013-12-29 09:28:36 +01:00
if opts.warning < 0 or opts.critical < 0:
err('-w and -c must be a positive number')
return opts
2013-12-25 16:44:10 +01:00
2014-01-01 17:14:13 +01:00
def get_error_workers(url, timeout):
2013-12-29 09:28:36 +01:00
'''
Query the Modjk status worker for bad workers
'''
2014-01-01 17:14:13 +01:00
get_node = re.compile(r'Member: name=(.*) type=')
ret = set([])
total = 0
response = urllib2.urlopen(url+'?mime=txt', timeout=timeout).read()
for member in re.findall( r'^Member: .*', response, re.M):
total += 1
if 'state=OK' in member and 'activation=ACT' in member:
ret.add(
get_node.search(member).groups(0)[0]
2013-12-29 09:28:36 +01:00
)
2014-01-01 17:14:13 +01:00
return (list(ret), total)
2013-12-25 16:44:10 +01:00
if __name__ == '__main__':
2014-01-01 17:14:13 +01:00
opts = prepare_opts()
2013-12-29 09:28:36 +01:00
try:
2014-01-01 17:14:13 +01:00
(errorWorkers, total) = get_error_workers(
2013-12-29 09:28:36 +01:00
opts.url, opts.timeout
)
except urllib2.URLError as e:
print 'UNKNOWN: Cant query jkstatus worker for data'
exit(EXIT_CODE['UNKNOWN'])
count = len(errorWorkers)
state = ''
2014-01-07 15:52:42 +01:00
if count > opts.warning or (opts.warning == 1 and count == 1):
2013-12-29 09:28:36 +01:00
state = 'OK'
2014-01-01 17:14:13 +01:00
elif opts.warning >= count > opts.critical:
2013-12-29 09:28:36 +01:00
state = 'WARN'
else:
state = 'CRIT'
2014-01-01 17:14:13 +01:00
print '{0}: {1}/{2} workers are OK and ACT {3}'.format(
state, count, total, ','.join(errorWorkers)
2013-12-29 09:28:36 +01:00
)
exit(EXIT_CODE[state])