#!/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 agent output:
# <<<ibm_svc_portsas:sep(58)>>>
# 0:1:6Gb:1:node1:500507680305D3C0:online::host:host_controller:0:1
# 1:2:6Gb:1:node1:500507680309D3C0:online::host:host_controller:0:2
# 2:3:6Gb:1:node1:50050768030DD3C0:online::host:host_controller:0:3
# 3:4:6Gb:1:node1:500507680311D3C0:offline:500507680474F03F:none:enclosure:0:4
# 4:5:N/A:1:node1:500507680315D3C0:offline_unconfigured::none:host_controller:1:1
# 5:6:N/A:1:node1:500507680319D3C0:offline_unconfigured::none:host_controller:1:2
# 6:7:N/A:1:node1:50050768031DD3C0:offline_unconfigured::none:host_controller:1:3
# 7:8:N/A:1:node1:500507680321D3C0:offline_unconfigured::none:host_controller:1:4
# 8:1:6Gb:2:node2:500507680305D3C1:online::host:host_controller:0:1
# 9:2:6Gb:2:node2:500507680309D3C1:online::host:host_controller:0:2
# 10:3:6Gb:2:node2:50050768030DD3C1:online::host:host_controller:0:3
# 11:4:6Gb:2:node2:500507680311D3C1:offline:500507680474F07F:none:enclosure:0:4
# 12:5:N/A:2:node2:500507680315D3C1:offline_unconfigured::none:host_controller:1:1
# 13:6:N/A:2:node2:500507680319D3C1:offline_unconfigured::none:host_controller:1:2
# 14:7:N/A:2:node2:50050768031DD3C1:offline_unconfigured::none:host_controller:1:3
# 15:8:N/A:2:node2:500507680321D3C1:offline_unconfigured::none:host_controller:1:4

# the corresponding header line
#id:port_id:port_speed:node_id:node_name:WWPN:status:switch_WWPN:attachment:type:adapter_location:adapter_port_id


def parse_ibm_svc_portsas(info):
    dflt_header = [
        'id',
        'port_id',
        'port_speed',
        'node_id',
        'node_name',
        'WWPN',
        'status',
        'switch_WWPN',
        'attachment',
        'type',
        'adapter_location',
        'adapter_port_id',
    ]
    parsed = {}
    for id_, rows in parse_ibm_svc_with_header(info, dflt_header).iteritems():
        try:
            data = rows[0]
        except IndexError:
            continue
        if "node_id" in data and "adapter_location" in data and "adapter_port_id" in data:
            item_name = "Node %s Slot %s Port %s" % (data['node_id'], data['adapter_location'],
                                                     data['adapter_port_id'])
        else:
            item_name = "Port %s" % id_
        parsed.setdefault(item_name, data)
    return parsed


def inventory_ibm_svc_portsas(parsed):
    for item_name, data in parsed.iteritems():
        status = data['status']
        if status == 'offline_unconfigured':
            continue
        yield item_name, {'current_state': status}


@get_parsed_item_data
def check_ibm_svc_portsas(item, params, data):
    # This can be removed soon, because the age of the check
    # was only about 7 days:
    if not params:
        params = {'current_state': 'offline'}

    sasport_status = data['status']

    infotext = "Status: %s" % sasport_status
    if sasport_status == params['current_state']:
        state = 0
    else:
        state = 2

    infotext += ", Speed: %s, Type: %s" % (data['port_speed'], data['type'])

    return state, infotext


check_info["ibm_svc_portsas"] = {
    "parse_function": parse_ibm_svc_portsas,
    "check_function": check_ibm_svc_portsas,
    "inventory_function": inventory_ibm_svc_portsas,
    "service_description": "SAS %s",
    "includes": ["ibm_svc.include"],
}
