#!/usr/bin/python
import stat

import sys

sys.path.append("/usr/lib")
from mongodb_health.basecheck import *

instance = sys.argv[1]
maxLag = 1800
args = len(sys.argv)
if args == 3:
    maxLag = sys.argv[args - 1]
__MONGO_MONITOR = "/etc/mongo-monitor.conf"
user = None
password = None
if os.path.exists(__MONGO_MONITOR):
    filestat = os.stat(__MONGO_MONITOR)
    if stat.S_IMODE(filestat.st_mode) == 384 and filestat.st_gid == 0 and filestat.st_uid == 0:
        file = open(__MONGO_MONITOR)
        user = file.readline().strip()
        password = file.readline().strip()
    else:
        print "2;Failed;set root:root ownership and 600 permissions to file /etc/mongo-monitor.conf"
        exit()
port = find_port(instance)
if not port:
    print "0;Ok;" + instance + " is not configured"
else:
    check_rs(port, user, password, maxLag)


