#!/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

# 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


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) < 15: # old format
                line = line + ["0", "0"] # do not modify line!

            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 = line

            # Check status
            message = "Enclosure %s is %s" % (enclosure_id, enclosure_status)
            if enclosure_status == "online":
                status = 0
            else:
                status = 2
                message += "(!!)"

            # Check canisters
            if online_canisters == total_canisters:
                message += ", all %s canisters are online" % total_canisters
            else:
                status = 2
                message += ", only %s of %s canisters are online(!!)" % (online_canisters, total_canisters)

            # Check PSUs
            if online_PSUs == total_PSUs:
                message += ", all %s PSUs are online" % total_PSUs
            else:
                status = 2
                message += ", only %s of %s PSUs are online(!!)" % (online_PSUs, total_PSUs)

            # Check FANs (only new firmware)
            if online_fan_modules == total_fan_modules:
                if total_fan_modules != "0":
                    message += ", all %s fan modules are online" % total_fan_modules
            else:
                status = 2
                message += ", only %s of %s fan modules are online(!!)" % (online_fan_modules, total_fan_modules)

            return status, message

    return 3, "Enclosure %s not found in agent output" % item

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

