#!/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 inventory_ibm_svc_enclosure(info):
    inventory = []
    for line in info:
        enclosure_id = line[0]
        inventory.append((enclosure_id, None))
    return inventory


def check_ibm_svc_enclosure(item, _no_params, info):
    for line in info:
        if line[0] == item:
            if len(line) == 9:
                line = line[:3] + [None, None, None] + line[3:7] + [
                    None
                ] + line[7:] + [None, None, None, None]
            elif len(line) == 13:
                line = line + [None, None, None, None]
            elif len(line) == 15:
                line = line + [None, None]
            elif len(line) == 17:
                pass
            else:
                yield 3, "Unknown format"
                return

            enclosure_id, enclosure_status, enclosure_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_secondary_expander_modules, online_secondary_expander_modules = line

            # Check status
            if enclosure_status == "online":
                status = 0
            else:
                status = 2
            yield status, "Status: %s" % enclosure_status

            for online, total, text in [
                (online_canisters, total_canisters, 'canisters'),
                (online_PSUs, total_PSUs, "PSUs"),
                (online_fan_modules, total_fan_modules, 'fan modules'),
                (online_secondary_expander_modules,
                 total_secondary_expander_modules,
                 'secondary expander modules'),
            ]:
                if online is None:
                    continue
                state = 0
                infotext = "Online %s: %s" % (text, online)
                if total is not None and online != total:
                    state = 2
                    infotext += " of %s" % total
                yield state, infotext


check_info["ibm_svc_enclosure"] = {
    "check_function": check_ibm_svc_enclosure,
    "inventory_function": inventory_ibm_svc_enclosure,
    "service_description": "Enclosure %s",
}
