#!/usr/bin/env python

import operator

exit_code_output = {0: 'OK',
                    1: 'WARNING',
                    2: 'CRITICAL',
                    3: 'UNKNOWN',
                   }

exit_code = 0
# Get threshold
data = {}
outputs = []
output = ""
perf_data = ""
operator_name = 'ge'
thyallperfs = allperfs(self)
for metric_name, metric_value in thyallperfs.items():
    warn = None
    crit = None
    #print "Hello %s, %s" % (metric_name , metric_value.value)
    mta = metric_name.split('-')[1]
    if not mta in data:
        data[mta] = {}
    data[mta] = int(metric_value.value)
    warn = self.host.customs.get('_MAILQUEUE_MAX_WARN' , None)
    crit = self.host.customs.get('_MAILQUEUE_MAX_CRIT' , None)

    this_exit_code = 0
    if not warn is None:
        warn = int(warn)
        if getattr(operator, operator_name)(data[mta], warn):
            this_exit_code = 1
    if not crit is None:
        crit = int(crit)
        if getattr(operator, operator_name)(data[mta], crit):
            this_exit_code = 2

    if this_exit_code > 0:
        outputs.append('%s mail(s) in %s queue' % (data[mta], mta))

    if exit_code < this_exit_code:
        exit_code = this_exit_code


    perf_data += " %s=%d;%s;%s;0;" % ('%s-queuelength' % mta, data[mta], str(warn or ''), str(crit or ''))


# Finish output
output = " - ".join((data.keys()[0].upper(), exit_code_output[exit_code]))
if len(outputs) > 0:
    output += ': ' + ', '.join(outputs) 
# Set ouput
set_value(self, output, perf_data, exit_code)

