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

# Output may have 11 fields:
# id:fc_io_port_id:port_id:type:port_speed:node_id:node_name:WWPN:nportid:status:attachment
# Example output from agent:
# <<<ibm_svc_portfc:sep(58)>>>
# 0:1:1:fc:8Gb:1:node1:5005076803042126:030400:active:switch
# 1:2:2:fc:8Gb:1:node1:5005076803082126:040400:active:switch
# 2:3:3:fc:N/A:1:node1:50050768030C2126:000000:inactive_unconfigured:none
# 3:4:4:fc:N/A:1:node1:5005076803102126:000000:inactive_unconfigured:none
# 8:1:1:fc:8Gb:2:node2:5005076803042127:030500:active:switch
# 9:2:2:fc:8Gb:2:node2:5005076803082127:040500:active:switch
# 10:3:3:fc:N/A:2:node2:50050768030C2127:000000:inactive_unconfigured:none
# 11:4:4:fc:N/A:2:node2:5005076803102127:000000:inactive_unconfigured:none
#
# Output may have 12 fields:
# id:fc_io_port_id:port_id:type:port_speed:node_id:node_name:WWPN:nportid:status:attachment:cluster_use
# Example output from agent:
# <<<ibm_svc_portfc:sep(58)>>>
# 0:1:1:fc:8Gb:1:node1:5005076803042126:030400:active:switch:local_partner
# 1:2:2:fc:8Gb:1:node1:5005076803082126:040400:active:switch:local_partner
# 2:3:3:fc:N/A:1:node1:50050768030C2126:000000:inactive_unconfigured:none:local_partner
# 3:4:4:fc:N/A:1:node1:5005076803102126:000000:inactive_unconfigured:none:local_partner
# 8:1:1:fc:8Gb:2:node2:5005076803042127:030500:active:switch:local_partner
# 9:2:2:fc:8Gb:2:node2:5005076803082127:040500:active:switch:local_partner
# 10:3:3:fc:N/A:2:node2:50050768030C2127:000000:inactive_unconfigured:none:local_partner
# 11:4:4:fc:N/A:2:node2:5005076803102127:000000:inactive_unconfigured:none:local_partner


def inventory_ibm_svc_portfc(info):
    for line in info:
        if len(line) in (11, 12, 14) and line[9] == "active":
            yield line[0], None

def check_ibm_svc_portfc(item, _no_params, info):
    for line in info:
        if len(line) in (11, 12, 14):
            if line[0] == item:
                fc_port_status = line[9]

                if fc_port_status == "active":
                    state = 0
                else:
                    state = 2

                return state, fc_port_status

check_info["ibm_svc_portfc"] = {
    "check_function"        : check_ibm_svc_portfc,
    "inventory_function"    : inventory_ibm_svc_portfc,
    "service_description"   : "FC Port %s",
}

