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


inventory_fujitsu_ca_ports = []


def parse_fjdarye500_ca_ports(info):
    map_modes = {
        "11": "CA",
        "12": "RA",
        "13": "CARA",
        "20": "Initiator",
    }

    parsed = {}
    for index, mode, read_iops, write_iops, read_mb, write_mb in info:
        mode_readable = map_modes[mode]
        port = parsed.setdefault(index, {
            "mode": mode_readable,
            "read_ios": int(read_iops),
            "read_throughput": int(read_mb) * 1024**2,
        })
        if mode_readable != "Initiator":
            port.update({
                "write_ios": int(write_iops),
                "write_throughput": int(write_mb) * 1024**2,
            })
    return parsed


def inventory_fjdarye500_ca_ports(parsed):
    settings = host_extra_conf_merged(host_name(), inventory_fujitsu_ca_ports)
    indices = settings.get('indices')
    modes = settings.get('modes', ['CA', 'CARA'])
    for index, attrs in parsed.iteritems():
        if indices and index not in indices:
            continue
        if modes and attrs['mode'] not in modes:
            continue
        yield index, {}


def check_fjdarye500_ca_ports(item, params, parsed):
    if item in parsed:
        mode = parsed[item]["mode"]
        yield 0, "Mode: %s" % mode
        for state, infotext, perfdata in check_diskstat_dict(item, params, parsed):
            if infotext and "Read: " in infotext and mode in ["CARA", "Initiator"]:
                infotext.replace("Read: ", "Initiator: ")
            if infotext and "Write: " in infotext and mode == "CARA":
                infotext.replace("Write: ", "Target: ")
            yield state, infotext, perfdata


check_info['fjdarye500_ca_ports'] = {
    'parse_function'        : parse_fjdarye500_ca_ports,
    'inventory_function'    : inventory_fjdarye500_ca_ports,
    'check_function'        : check_fjdarye500_ca_ports,
    'service_description'   : 'CA Port IO %s',
    'snmp_info'             : ('.1.3.6.1.4.1.211.1.21.1.150.5.5.2.1', [
                                # fjdaryPfCaPortRdIOPS
                                #     "This shows the READ IOPS for the CA,CARA mode.
                                #      The Initiator IOPS is shown for RA,Initiator mode."
                                # fjdaryPfCaPortWtIOPS
                                #     "This shows the WRITE IOPS for the CA,CARA mode.
                                #      The Target IOPS is shown for the RA mode.
                                #      This information is an invalid value for the Initiator mode."
                                # fjdaryPfCaPortRdTp
                                #     "This shows the amount of the READ Throughput for the CA,CARA mode.
                                #      The Initiator Throughput is shown for RA,Initiator mode.
                                #      The unit is MB/sec."
                                # fjdaryPfCaPortWtTp
                                #     "This shows the amount of the WRITE Throughput for the CA,CARA mode.
                                #      The Target Throughput is shown for the RA mode.
                                #      The unit is MB/sec.
                                #      This information is an invalid value for the Initiator mode."
                                '1',    # FJDARY-E150::fjdaryPfCaPortIndex
                                '2',    # FJDARY-E150::fjdaryPfCaPortMode
                                '3',    # FJDARY-E150::fjdaryPfCaPortRdIOPS
                                '4',    # FJDARY-E150::fjdaryPfCaPortWtIOPS
                                '5',    # FJDARY-E150::fjdaryPfCaPortRdTp
                                '6',    # FJDARY-E150::fjdaryPfCaPortWtTp
                              ]),
    'snmp_scan_function'    : lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.211.1.21.1.150",
    'has_perfdata'          : True,
    'group'                 : 'diskstat',
    'includes'              : ['diskstat.include']
}
