#!/usr/bin/python
# Fetches mails from inbox of a pop mailbox to gather configuration
# and state information generated by the livedump command. We use
# the mail subject to identify the mails. Mails with a subject
# "state" do contain the nagios checkresults in the mail body and
# mails with subjects starting with the string "config" do contain 
# nagios configuration definitions. All chars after the config string
# in the subject are used to identfy the sending system. This is
# an ident of the system to support multiple sending systems.
#
# Configuration is written to etc/nagios/conf.d/remote-<ident>.cfg.
# The status info is sent in nagios state result file format and
# written to tmp/nagios/checkresults directory in a temporary file
# which is then processed by nagios.

import os
import sys
import poplib
import re
import quopri
import tempfile

omd_root = os.environ.get('OMD_ROOT')
pop_server = "pop.1und1.de" 
pop_user   = "monitor@grillwerkstatt.at"
pop_pass   = "LTWmonitor2013"

M = poplib.POP3_SSL(pop_server)
M.user(pop_user)
M.pass_(pop_pass)

config_ids = []
status_ids = []
numMessages = len(M.list()[1])

status_mails = []
config_mails = []

pattern = re.compile(r"^Subject: (.*)")
for i in range(numMessages):
    for header_lines in M.top(i + 1, 0)[1]:
        matches = pattern.match(header_lines)
        if matches:
            subject = matches.group(1).strip()
            if subject == "status":
                status_mails.append(i + 1)
            elif subject.startswith("config"):
                ident = subject[6:].strip().replace("/", "_").replace(".", "_")
                if not ident:
                    ident = "no-ident"
                config_mails.append((i + 1, ident))

# handle config mails. Only last available is interesting.
# Extract mail body and use this as retrieved config.
if config_mails:
    mail_index, ident = config_mails[-1]
    code, mail, num = M.retr(mail_index)
    body_start_index = mail.index('')
    body = quopri.decodestring('\n'.join(mail[body_start_index:]))

    file("%s/etc/nagios/conf.d/remote-%s.cfg" % 
         (omd_root, ident), "w").write(body)

if status_mails:
    for index in status_mails:
        code, mail, code = M.retr(index)
        body_start_index = mail.index('')
        body = quopri.decodestring('\n'.join(mail[body_start_index:]))

        fd, path = tempfile.mkstemp('', 'c', "%s/tmp/nagios/checkresults" % omd_root)
        os.write(fd, body)
        os.close(fd)
        file(path + ".ok", "w")

# alle mails loeschen
try:
    popstate, msglist, code = M.list()
    for entry in msglist:
        msgid, size = entry.split()
        response = M.dele(msgid)
        if response != "+OK":
            raise Exception("Error from POP server: [%s]" % response)
            break

except Exception, e:
    sys.stderr.write(msg + "Error deleting message: %s" % e)
    sys.exit(1)

# Close mailbox, commit changes
M.quit()
