#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2019             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# exemplary output of special agent agent_ucs_bladecenter (<TAB> is tabulator):
# storageControllerHealth<TAB>dn
# sys/rack-unit-1/board/storage-SAS-SLOT-HBA/vd-0 <TAB>id SLOT-HBA<TAB>health Good
# storageControllerHealth<TAB>dn
# sys/rack-unit-2/board/storage-SAS-SLOT-HBA/vd-0 <TAB>id SLOT-HBA<TAB>health Good


def parse_ucs_c_rack_server_health(info):
    """
    Input: list of lists containing storage controller health data on a per rack basis.
    Output: Returns dict with indexed Rack Units mapped to keys and lowercase health string mapped to value
    'health' if rack server has racks attached or empty dict if not.
    """
    parsed = {}
    for _, dn, _id, health in info:
        rack_storage_board = dn.replace("dn sys/", "").replace("rack-unit-", "Rack unit ").replace(
            "/board/storage-", " Storage ").replace("-", " ").replace("/", " ")
        parsed[rack_storage_board] = health.replace("health ", "").lower()
    return parsed


def inventory_ucs_c_rack_server_health(parsed):
    """
    Input: dict containing items as keys or empty dict.
    Output: Yields indexed racks and storage controllers as items (e.g. Rack Unit 1 Storage SAS SLOT HBA vd 0) in case parsed contains items.
    """
    for key in parsed.iterkeys():
        yield key, {}


@get_parsed_item_data
def check_ucs_c_rack_server_health(item, params, health):
    """
    Check function is called only in case parsed is a dict and item exists as key in parsed[item].
    All other potential bad case conditions are handled by @get_parsed_item_data.
    """
    # Dict keys are storage controller health strings provided via special agent -> XML
    # API of servers. Dict values are corresponding check status.
    # For information about the data provided by the special agent
    # "storageControllerHealth" refer to Cisco C-Series Rack Server XML 2.0 Schema files:
    # [https://community.cisco.com/t5/unified-computing-system/cisco-ucs-c-series-standalone-xml-schema/ta-p/3646798]
    # Note: The possible string values are not defined/documented in the XML schema.
    # "Good" is the only value known from exemplary data output. Pre-process the
    # data to lowercase only chars.
    health_to_status_mapping = {
        'good': 0,
    }

    try:
        status = health_to_status_mapping[health]
        status_readable = health
    except KeyError:
        status = 3
        status_readable = "unknown[%s]" % health
    yield status, "Status: %s" % status_readable


check_info["ucs_c_rack_server_health"] = {
    'parse_function': parse_ucs_c_rack_server_health,
    'inventory_function': inventory_ucs_c_rack_server_health,
    'check_function': check_ucs_c_rack_server_health,
    'service_description': 'Health %s',
}
