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

# <<<netapp_api_vs_traffic:sep(9)>>>
# lif:vserver	instance_uuid 4294967295	instance_name sb1	sent_errors 0	recv_errors 0 ...
# lif:vserver	instance_uuid 16	instance_name vsFS	sent_errors 0	recv_errors 0	..
# cifs:vserver	session_timed_out 17731	sd_max_ace_size 	cifs_latency 9403817108427	..
# iscsi_lif:vserver	iscsi_read_ops 4071295661	avg_write_latency 3429809602514	..


def inventory_netapp_api_vs_traffic(parsed, what):
    vservers = set(map(lambda x: x.split(".", 1)[1], parsed.keys()))
    for vserver in vservers:
        yield vserver, {}

def check_netapp_api_vs_traffic(item, _no_params, parsed):
    protocol_map = {
        "lif:vserver": ("Ethernet",
             # ( what                 perfname                perftext              scale     format_func)
            [  ("recv_data",          "if_in_octets",         "received data",      1,        get_bytes_human_readable),
               ("sent_data",          "if_out_octets",        "sent data",          1,        get_bytes_human_readable),
               ("recv_errors",        "if_in_errors",         "received errors",    1,        int),
               ("sent_errors",        "if_out_errors",        "sent errors",        1,        int),
               ("recv_packet",        "if_in_pkts",           "received packets",   1,        int),
               ("sent_packet",        "if_out_pkts",          "sent packets",       1,        int)]),

        "fcp_lif:vserver": ("FCP",
            [  ("avg_read_latency",   "fcp_read_latency",     "avg. Read latency",  0.001,    lambda x: "%.2f ms" % (x * 1000)),
               ("avg_write_latency",  "fcp_write_latency",    "avg. Write latency", 0.001,    lambda x: "%.2f ms" % (x * 1000)),
               ("read_data",          "fcp_read_data",        "read data",          1,        get_bytes_human_readable),
               ("write_data",         "fcp_write_data",       "write data",         1,        get_bytes_human_readable)]),

        "cifs:vserver": ("CIFS",
            [  ("cifs_read_latency",  "cifs_read_latency",    "read latency",       0.000000001, lambda x: "%.2f ms" % (x * 1000)),
               ("cifs_write_latency", "cifs_write_latency",   "write latency",      0.000000001, lambda x: "%.2f ms" % (x * 1000)),
               ("cifs_read_ops",      "cifs_read_ios",        "read OPs",           1,        int),
               ("cifs_write_ops",     "cifs_write_ios",       "write OPs",          1,        int)]),

        "iscsi_lif:vserver": ("iSCSI",
            [  ("avg_read_latency",   "iscsi_read_latency",   "avg. Read latency",  0.001,    lambda x: "%.2f ms" % (x * 1000)),
               ("avg_write_latency",  "iscsi_write_latency",  "avg. Write latency", 0.001,    lambda x: "%.2f ms" % (x * 1000)),
               ("read_data",          "iscsi_read_data",      "read data",          1,        get_bytes_human_readable),
               ("write_data",         "iscsi_write_data",     "write data",         1,        get_bytes_human_readable)]),

        "nfsv3": ("NFS",
            [  ("nfsv3_read_ops",     "nfs_read_ios",         "read OPs",           1,        int),
               ("nfsv3_write_ops",    "nfs_write_ios",        "write OPs",          1,        int)]),

        "nfsv4": ("NFSv4",
            [  ("nfsv4_read_ops",     "nfsv4_read_ios",       "read OPs",           1,        int),
               ("nfsv4_write_ops",    "nfsv4_write_ios",      "write OPs",          1,        int)]),

        "nfsv4_1": ("NFSv4.1",
            [  ("nfsv4_1_ops",        "nfsv4_1_ios",          "OPs",                1,        int) ])
    }
    vserver = item.split(" ", 3)

    now = time.time()
    for protocol, (protoname, values) in protocol_map.items():
        data = parsed.get("%s.%s" % (protocol, item))
        if not data:
            continue

        for what, perfname, perftext, scale, format_func in values:
            if what not in data:
                continue

            rate = get_rate(what, now, int(data[what]) * scale)
            yield 0, "%s %s: %s" % (protoname, perftext, format_func(rate)), [(perfname, rate)]


check_info["netapp_api_vs_traffic"] = {
    'parse_function'      : lambda info: netapp_api_parse_lines(info, custom_keys = ["protocol", "instance_name"]),
    'inventory_function'  : lambda parsed: inventory_netapp_api_vs_traffic(parsed, "lif:vserver"),
    'check_function'      : check_netapp_api_vs_traffic,
    'service_description' : 'Traffic vServer %s',
    'has_perfdata'        : True,
    'includes'            : [ "netapp_api.include" ]
}

