#!/usr/bin/python

import sys
import os
import time
import json
import stat

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

instance = sys.argv[1]
param = sys.argv[2]
threshold = sys.argv[3]
db_name = None

port=find_port(instance)
__MONGO_MONITOR = "/etc/mongo-monitor.conf"
STATE_FILE = "/var/spool/monrun/mongodb-health.state"
#STATE_MAXAGE = 300
STATE_MAXAGE = 20

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 == 600 and filestat.st_uid == 600:
    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()
if not port:
    print "0;Ok;" + instance + " is not configured"
    exit(0)

current = None
prev = None
serverStatus = {}

if param == 'rs_indexes_consistency':
    import mongodb_health.indexes
    result = mongodb_health.indexes.check(databases = threshold, port = port, user = user, password = password)
    if result != None:
        print result
    exit(0)

if param == 'disk_read' or param == 'disk_write':
    # we don't need to store it in serverStatus JSON
    import mongodb_health.disk_usage
    window = 1
    try:
        window = int(sys.argv[4])
    except:
        pass

    (read, write, error) = mongodb_health.disk_usage.get_disk_usage(avg_window = window)
    if read != None and write != None:
        if param == 'disk_read':
            outputResult('disk_read', read, read, threshold)
        if param == 'disk_write':
            outputResult('disk_write', write, write, threshold)
    else:
        print "1;Cannot get data: {0}".format(error)

    exit(0)

# as far as monrun cannot get several checks from one script and we do not want to jack off mongo
# we're calling serverStatus only once during STATE_MAXAGE seconds and saving interesting values to state file
# so subsequent calls of this script will use cached values from file
if len(sys.argv) > 4:
    if sys.argv[4] == 'refresh' and os.path.exists(STATE_FILE):
        os.unlink(STATE_FILE)

if os.path.exists(STATE_FILE) and time.time() - os.path.getctime(STATE_FILE) < STATE_MAXAGE:
    serverStatus = loadServerStatus(STATE_FILE)
    if not serverStatus:
        serverStatus = flushServerStatus(port, user, password, STATE_FILE)

else:
    if not os.path.exists(STATE_FILE):
        serverStatus = flushServerStatus(port, user, password, STATE_FILE)

    elif time.time() - os.path.getctime(STATE_FILE) >= STATE_MAXAGE:
        serverStatus = loadServerStatus(STATE_FILE)
        if not serverStatus:
            serverStatus = flushServerStatus(port, user, password, STATE_FILE)
        else:
            serverStatus['prev'] = serverStatus['current']
            serverStatus['current'] = getServerStatus(host = 'localhost', port = port, user = user, password = password)

    f = open(STATE_FILE, 'w')
    json.dump(serverStatus, f, default = lambda obj: (obj.isoformat() if hasattr(obj, 'isoformat') else None) )
    f.flush()
    os.fsync(f.fileno())
    f.close()

try:
    current = serverStatus['current'][param]
    prev = serverStatus['prev'][param]
except KeyError:
    print "1;Failed;Unknown parameter {0}".format(param)
    exit(0)
except:
    serverStatus = flushServerStatus(port, user, password, STATE_FILE)
    current = serverStatus['current'][param]
    prev = serverStatus['prev'][param]

if current == 'n/a':
    print "1;Failed;{0} parameter is unavailable".format(param)
    exit(0)

outputResult(param, current, prev, threshold)



