#!/usr/bin/python

import os, socket, sys

if os.getenv("OMD_ROOT"):
    socket_path = os.getenv("OMD_ROOT") + "/tmp/run/mkeventd/status"
else:
    socket_path = None

def query(query, remote_host):
    global socket_path
    try:
        if remote_host and ':' in remote_host:
            parts = remote_host.split(":")
            host = parts[0]
            if len(parts) == 2:
                port = int(parts[1])
            else:
                port = 6558
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(10)
            sock.connect((host, port))
        else:
            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            sock.settimeout(3)
            if remote_host and remote_host[0] == '/': 
                socket_path = remote_host
            elif remote_host:
                sys.stderr.write("Invalid socket specification '%s'\n" % remote_host)
                sys.exit(3)
            if not socket_path:
                sys.stderr.write("You running out of OMD. Please specify a socket with -H.\n")
                sys.exit(3)
            sock.connect(socket_path)

        sock.send(query)

        response_text = ""
        while True:
            chunk = sock.recv(8192)
            response_text += chunk
            if not chunk:
                break

        return eval(response_text)
    except SyntaxError, e:
        sys.stdout.write("UNKNOWN - Invalid answer from event daemon\n%s\nQuery was:\n%s\n" \
                % (e, query))
        sys.exit(3)

    except Exception, e:
        if remote_host and ':' in remote_host:
            via = "TCP %s" % (remote_host)
        else:
            via = "UNIX socket %s" % socket_path
        sys.stdout.write("UNKNOWN - Cannot connect to event daemon via %s: %s\n" % (via, e))
        sys.exit(3)

try:
    remote_host = None
    try:
        del sys.argv[sys.argv.index('-a')]
        opt_ignore_acknowledged = True
    except:
        opt_ignore_acknowledged = False

    if sys.argv[1] == '-H':
        remote_host = sys.argv[2]
        del sys.argv[1:3]
    host_name = sys.argv[1]
    if len(sys.argv) > 2:
        application = sys.argv[2]
    else:
        application = None
except:
    sys.stdout.write("""Usage: check_mkevents [-H SOCKET] [-a] HOST [APPLICATION]

 -a         do not take into account acknowledged events.
 -H SOCKET  how to connect to the status socket of the Event Console.
            Use either HOST:PORT for TCP (e.g. 10.10.0.139:4478) or
            a path to a file for a local UNIX socket (e.g. 
            /var/run/nagios/rw/mkeventd/status).
""")
    sys.exit(3)

q = "GET events\n" \
    "Filter: event_host =~ %s\n" % host_name

if application:
    q += "Filter: event_application ~~ %s\n" % application

q += "Filter: event_phase in open ack\n"

response = query(q, remote_host)
headers = response[0]
worst_state = 0
worst_row = None
count = 0
unhandled = 0
for line in response[1:]:
    count += 1
    row = dict(zip(headers, line))
    p = row["event_phase"]
    if p == 'open' or not opt_ignore_acknowledged:
        s = row["event_state"]
        if s == 3:
            if worst_state < 2:
                worst_state = 3
                worst_row = row
        elif s >= worst_state:
            worst_state = s
            worst_row = row
    if p == 'open':
        unhandled += 1

nagios_state_names = {
    0 : "OK",
    1 : "WARN",
    2 : "CRIT",
    3 : "UNKNOWN",
}

if count == 0 and application:
    sys.stdout.write("OK - no events for %s on host %s\n" % (application, host_name))
elif count == 0:
    sys.stdout.write("OK - no events for %s\n" % host_name)
else:
    sys.stdout.write(nagios_state_names[worst_state] + \
         " - %d events (%d unacknowledged)" % (count, unhandled))
    if worst_row:
        sys.stdout.write(", worst state is %s (Last line: %s)" % \
         (nagios_state_names[worst_state], worst_row['event_text'].encode('utf-8')))
    sys.stdout.write("\n")

sys.exit(worst_state)

