#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             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.

# Example output from agent:
# <<<ibm_svc_enclosure:sep(58)>>>
# 1:online:control:yes:0:io_grp0:2072-24C:7804037:2:2:2:2:24
# 2:online:expansion:yes:0:io_grp0:2072-24E:7804306:2:2:2:2:24
# 3:online:expansion:yes:0:io_grp0:2072-24E:7804326:2:2:2:2:24
# 4:online:expansion:yes:0:io_grp0:2072-24E:7804352:2:2:2:2:24

# After a firmware upgrade the output looked like this:
# 1:online:control:yes:0:io_grp0:2072-24C:7804037:2:2:2:2:24:0:0
# 2:online:expansion:yes:0:io_grp0:2072-24E:7804306:2:2:2:2:24:0:0
# 3:online:expansion:yes:0:io_grp0:2072-24E:7804326:2:2:2:2:24:0:0
# 4:online:expansion:yes:0:io_grp0:2072-24E:7804352:2:2:2:2:24:0:0

# FW >= 7.8
# 1:online:control:yes:0:io_grp0:2072-24C:7804037:2:2:2:2:24:0:0:0:0
# 2:online:expansion:yes:0:io_grp0:2072-24E:7804306:2:2:2:2:24:0:0:0:0
# 3:online:expansion:yes:0:io_grp0:2072-24E:7804326:2:2:2:2:24:0:0:0:0
# 4:online:expansion:yes:0:io_grp0:2072-24E:7804352:2:2:2:2:24:0:0:0:0

# The names of the columns are:
# id:status:type:managed:IO_group_id:IO_group_name:product_MTM:serial_number:total_canisters:online_canisters:total_PSUs:online_PSUs:drive_slots:total_fan_modules:online_fan_modules:total_sems:online_sems

# IBM-FLASH900
# <<<ibm_svc_enclosure:sep(58)>>>
# id:status:type:product_MTM:serial_number:total_canisters:online_canisters:online_PSUs:drive_slots
# 1:online:control:9843-AE2:6860407:2:2:2:12


def parse_ibm_svc_enclosure(info):
    dflt_header = _get_ibm_svc_enclosure_dflt_header(info)
    if dflt_header is None:
        return {}

    parsed = {}
    for id_, rows in parse_ibm_svc_with_header(info, dflt_header).iteritems():
        try:
            data = rows[0]
        except IndexError:
            continue
        parsed.setdefault(id_, data)
    return parsed


def _try_int(string):
    try:
        return int(string)
    except (ValueError, TypeError):
        return None


@get_parsed_item_data
def check_ibm_svc_enclosure(item, params, data):
    if params is None:
        params = {}

    enclosure_status = data['status']
    if enclosure_status == "online":
        status = 0
    else:
        status = 2
    yield status, 'Status: %s' % enclosure_status

    for key, label in [
        ('canisters', 'canisters'),
        ('PSUs', 'PSUs'),
        ('fan_modules', 'fan modules'),
        ('sems', 'secondary expander modules'),
    ]:
        online = _try_int(data.get("online_%s" % key))
        total = _try_int(data.get("total_%s" % key))
        if online is None:
            continue
        # Valid values for WATO rule value levels_lower_online_canisters are
        # False, which shall be mapped to (total, total) or (warn, crit).
        levels_lower = params.get("levels_lower_online_%s" % key) or (total, total)
        levels = (None, None) + levels_lower
        state, infotext, _perfdata = check_levels(online,
                                                  None,
                                                  levels,
                                                  human_readable_func=int,
                                                  infoname="Online %s" % label)
        if total is not None:
            infotext += " of %s" % total
        yield state, infotext


check_info["ibm_svc_enclosure"] = {
    "parse_function": parse_ibm_svc_enclosure,
    "check_function": check_ibm_svc_enclosure,
    "inventory_function": discover(),
    "service_description": "Enclosure %s",
    "includes": ["ibm_svc.include"],
    "group": "ibm_svc_enclosure",
}

#   .--helper--------------------------------------------------------------.
#   |                    _          _                                      |
#   |                   | |__   ___| |_ __   ___ _ __                      |
#   |                   | '_ \ / _ \ | '_ \ / _ \ '__|                     |
#   |                   | | | |  __/ | |_) |  __/ |                        |
#   |                   |_| |_|\___|_| .__/ \___|_|                        |
#   |                                |_|                                   |
#   '----------------------------------------------------------------------'


def _get_ibm_svc_enclosure_dflt_header(info):
    try:
        first_line = info[0]
    except IndexError:
        return

    if len(first_line) == 9:
        return [
            'id',
            'status',
            'type',
            'product_MTM',
            'serial_number',
            'total_canisters',
            'online_canisters',
            'online_PSUs',
            'drive_slots',
        ]
    elif len(first_line) == 13:
        return [
            'id',
            'status',
            'type',
            'managed',
            'IO_group_id',
            'IO_group_name',
            'product_MTM',
            'serial_number',
            'total_canisters',
            'online_canisters',
            'total_PSUs',
            'online_PSUs',
            'drive_slots',
        ]
    elif len(first_line) == 15:
        return [
            'id',
            'status',
            'type',
            'managed',
            'IO_group_id',
            'IO_group_name',
            'product_MTM',
            'serial_number',
            'total_canisters',
            'online_canisters',
            'total_PSUs',
            'online_PSUs',
            'drive_slots',
            'total_fan_modules',
            'online_fan_modules',
        ]
    elif len(first_line) == 17:
        return [
            'id',
            'status',
            'type',
            'managed',
            'IO_group_id',
            'IO_group_name',
            'product_MTM',
            'serial_number',
            'total_canisters',
            'online_canisters',
            'total_PSUs',
            'online_PSUs',
            'drive_slots',
            'total_fan_modules',
            'online_fan_modules',
            'total_sems',
            'online_sems',
        ]
    return
