Forked from vmihailenco/monit_supervisor_programs.py
Last active
December 17, 2015 13:39
-
-
Save jsoucheiron/5618469 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Based on Vladimir Mihailenco's work | |
import sys | |
import socket | |
import xmlrpclib | |
import argparse | |
def supervisord_processes(address): | |
try: | |
server = xmlrpclib.Server(address) | |
return server.supervisor.getAllProcessInfo() | |
except socket.error: # supervisord is down | |
return None | |
except xmlrpclib.ProtocolError: # auth failed | |
return None | |
def ok(message): | |
sys.stdout.write('OK: %s' % message) | |
sys.exit(0) | |
def warning(message): | |
sys.stdout.write('WARNING: %s' % message) | |
sys.exit(1) | |
def critical(message): | |
sys.stdout.write('CRITICAL: %s' % message) | |
sys.exit(2) | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Monit script for supervisord.') | |
parser.add_argument('-H','--hostname',metavar='hostname', type=str, help='supervisord hostname', required=True) | |
parser.add_argument('-P','--port',metavar='port', type=int, help='supervisord port',default=9001) | |
parser.add_argument('-p','--password',metavar='password', type=str,help='supervisord password', default='') | |
parser.add_argument('-u','--username',metavar='username', type=str,help='supervisord username', default='') | |
args = parser.parse_args() | |
if args.username == '': | |
address = 'http://%s:%d/RPC2' % (args.hostname,args.port) | |
elif args.password == '': | |
address = 'http://%s@%s:%d/RPC2' % (args.username,args.hostname,args.port) | |
else: | |
address = 'http://%s:%s@%s:%d/RPC2' % (args.username,args.password,args.hostname,args.port) | |
pl = supervisord_processes(address) | |
if not pl: | |
critical('Can not get processes.') | |
checked_names = [] | |
for p in pl: | |
if p['state'] != 20: | |
critical( | |
'Process %s has state=%s (expected 20).' % | |
(p['name'], p['state'])) | |
checked_names.append(p['name']) | |
ok(', '.join(checked_names)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment