#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
#
# copyright (c) 2006 praKsys
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
#

from ReportMonitor import ReportMonitor

import sys, email, time, re

from email.MIMEText import MIMEText
from email.Utils import quote
from email.Header import decode_header

from ConfigParser import ConfigParser

def getMailPart(mail, part):
    result = ''
    for m in decode_header(mail.get(part)) :
        result = result + " " + m[0]
    return result.strip()
        
def main():
    """Check in databases that everything goes as expected"""

    # get list of all monitored ids
    config = ConfigParser()
    try:
        config.read(['/etc/praksys/reportmonitor/reportmonitor.conf'])
    except:
        print "Failed to load config"
        raise

    ids = config.sections()

    # free config object as soon as possible
    del config
    
    for id in ids:
        
        # get report monitor object for this id
        monitor = ReportMonitor(id)
        result = monitor.check()
        if result != "":
            monitor.sendAlert(result)

        # free monitor object
        del monitor

if __name__ == "__main__":
    main()

