#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.fireeye import *  # pylint: disable=wildcard-import,unused-wildcard-import
# .1.3.6.1.4.1.25597.13.1.44.0 0
#.1.3.6.1.4.1.25597.13.1.45.0 603
#.1.3.6.1.4.1.25597.13.1.46.0 8
#.1.3.6.1.4.1.25597.13.1.47.0 0
#.1.3.6.1.4.1.25597.13.1.48.0 96
#.1.3.6.1.4.1.25597.13.1.49.0 0

factory_settings['fireeye_mailq'] = {
    'deferred': (1, 50),
    'hold': (500, 1000),
    'drop': (50, 500),
}


def parse_fireeye_mailq(info):
    try:
        queue_infos = info[0]
    except IndexError:
        return {}
    parsed = {}
    for index, item in enumerate(['Deferred', 'Hold', 'Incoming', 'Active', 'Drop']):
        parsed.update({item: queue_infos[index]})
    return parsed


def check_fireeye_mailq(item, params, parsed):
    for queue, value in parsed.items():
        infotext = 'Mails in %s queue: %s' % (queue.lower(), value)
        warn, crit = params.get(queue.lower(), (None, None))
        counter = 'mail_queue_%s_length' % queue.lower()
        yield fireeye_check_generic(infotext, counter, int(value), warn, crit)


check_info['fireeye_mailq'] = {
    'parse_function': parse_fireeye_mailq,
    'inventory_function': lambda parsed: inventory_fireeye_generic(parsed, False, True),
    'check_function': check_fireeye_mailq,
    'service_description': 'Mail Queues',
    'default_levels_variable': 'fireeye_mailq',
    'group': 'fireeye_mailq',
    'snmp_info': (
        '.1.3.6.1.4.1.25597.13.1',
        [
            '44',  # FE-FIREEYE-MIB::feDeferredEmailCount
            '45',  # FE-FIREEYE-MIB::feHoldQueueEmailCount
            '47',  # FE-FIREEYE-MIB::feIncomingEmailCount
            '48',  # FE-FIREEYE-MIB::feActiveEmailCount
            '49',  # FE-FIREEYE-MIB::feDropEmailCount
        ]),
    'snmp_scan_function': scan_fireeye,
    'has_perfdata': True,
}
