#!/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.blade_scan import *  # pylint: disable=wildcard-import,unused-wildcard-import
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.1 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.2 2
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.3 3
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.4 4
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.2.5 5
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.1 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.2 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.3 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.4 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.3.5 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.1 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.2 3
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.3 255
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.4 0
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.4.5 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.1 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.2 12
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.3 9
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.4 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.5.5 1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.1 ESX1
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.2 ESX109
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.3 ESX110
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.4 ESX137
#.1.3.6.1.4.1.2.3.51.2.22.1.5.1.1.6.5 ESX138


def inventory_blade_blades(info):
    for line in info:
        if line[2] == '1':
            yield (line[0], None)


def check_blade_blades(item, _no_params, info):
    map_exists = {'0': (2, "false"), '1': (0, "true")}

    map_power = {
        '0': (2, "off"),
        '1': (0, "on"),
        '3': (1, "standby"),
        '4': (1, "hibernate"),
        '255': (3, "unknown"),
    }

    map_health = {
        '0': (3, "unknown"),
        '1': (0, "good"),
        '2': (1, "warning"),
        '3': (2, "critical"),
        '4': (1, "kernel mode"),
        '5': (0, "discovering"),
        '6': (2, "communications error"),
        '7': (2, "no power"),
        '8': (1, "flashing"),
        '9': (2, "initialization Failure"),
        '10': (2, "insuffiecient power"),
        '11': (2, "power denied"),
        '12': (1, "maintenance mode"),
        '13': (1, "firehose dump"),
    }

    for line in info:
        if line[0] == item:
            # name
            yield 0, line[4]
            # exist_state
            state, state_readable = map_exists[line[1]]
            yield state, "Exists: %s" % state_readable
            # power_state
            state, state_readable = map_power[line[2]]
            yield state, "Power: %s" % state_readable
            # health_state
            state, state_readable = map_health[line[3]]
            yield state, "Health: %s" % state_readable


check_info["blade_blades"] = {
    'inventory_function': inventory_blade_blades,
    'check_function': check_blade_blades,
    'service_description': "Blade %s",
    'snmp_info': (
        ".1.3.6.1.4.1.2.3.51.2.22.1.5.1.1",
        [  # BLADE-MIB
            2,  # bladeId
            3,  # bladeExists
            4,  # bladePowerState
            5,  # bladeHealthState
            6,  # bladeName
        ]),
    'snmp_scan_function': scan_blade_modules,
}
