File: //lib/python2.7/site-packages/lap/mongodb.py
import socket
def __run__(params):
try:
import pymongo
except ImportError:
return [2, "CRITICAL - Error: Please install python-pymongo"]
try:
host = params.get("host", "localhost")
port = int(params.get("port", "27017"))
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]
try:
connection = pymongo.Connection(host, port)
except AttributeError:
connection = pymongo.MongoClient(host, port)
else:
try:
connection = pymongo.Connection(host, port, slave_okay=True)
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]
infos = {}
try:
ismaster = connection.admin.command("ismaster", "1")
repl_status = connection.admin.command('replSetGetStatus')
server_status = connection.admin.command('serverStatus')
server_status.update(ismaster)
server_status.update(repl_status)
infos.update({'me': socket.gethostbyaddr(server_status['host'].split(':')[0])[0].split('.')[0]})
infos.update({'repset_name': server_status['set']})
infos.update({'version': server_status['version']})
for member in server_status['members']:
m_name = socket.gethostbyaddr(member['name'].split(':')[0])[0]
s_host = socket.gethostbyaddr(server_status['host'].split(':')[0])[0]
if m_name == s_host:
infos.update({'status': member['stateStr']})
except:
try:
server_status = connection.admin.command('serverStatus')
infos.update({'me': socket.gethostbyaddr(server_status['host'].split(':')[0])[0].split('.')[0]})
infos.update({'version': server_status['version']})
except Exception, e:
return [ 2, "CRITICAL - Error: %s" % repr(e) ]
try:
if 'repset_name' in infos:
if not infos.get('status', 'unknown') in [ 'PRIMARY', 'SECONDARY', 'ARBITER' ]:
return [2, "CRITICAL - This server has the status %(status)s" % infos]
else:
return [0, "OK - version: %(version)s, replicaset name: %(repset_name)s, server status: %(status)s" % infos]
else:
return [0, "OK - version: %(version)s, this server is standalone" % infos]
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]