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


def parse_emc_director_stats(info):
    directors = {}
    now = time.time()
    for line in info:
        directors["%s_FE" % line[0]] = {
              "node"                       : None,
              "read_ios"                   : get_rate("fe_readio.%s" % line[0],  now, int(line[1])),
              "write_ios"                  : get_rate("fe_writeio.%s" % line[0], now, int(line[2])),
              "queue_length"               : int(line[3]),
              "average_read_wait"          : float(line[5]) / 1000000,
              "average_write_wait"         : float(line[6]) / 1000000,
              "read_throughput"            : get_rate("fe_readbytes.%s" % line[0],  now, int(line[7])),
              "write_throughput"           : get_rate("fe_writebytes.%s" % line[0], now, int(line[8])),
        }
        directors["%s_BE" % line[0]] = {
              "node"                       : None,
              "read_ios"                   : get_rate("be_readio.%s" % line[0],  now, int(line[9])),
              "write_ios"                  : get_rate("be_writeio.%s" % line[0], now, int(line[10])),
              "average_read_wait"          : float(line[11]) / 1000000,
              "average_write_wait"         : float(line[12]) / 1000000,
              "read_throughput"            : get_rate("be_readbytes.%s" % line[0],  now, int(line[13])),
              "write_throughput"           : get_rate("be_writebytes.%s" % line[0], now, int(line[14])),
        }

    return directors

def inventory_emc_director_stats(parsed):
    return inventory_diskstat_generic(map(lambda x: (None, x), parsed.keys()))

def check_emc_director_stats(item, params, parsed):
    # The check_diskstat_dict function may compute average values
    # We won't allow this if some of the counters have wrapped
    if last_counter_wrap():
        raise MKCounterWrapped("Value overflow")
    return check_diskstat_dict(item, params, parsed)

check_info["emc_vplex_director_stats"] = {
    "parse_function"                : parse_emc_director_stats,
    "check_function"                : check_emc_director_stats,
    "inventory_function"            : inventory_emc_director_stats,
    "service_description"           : "Disk IO Director %s",
    "snmp_scan_function"            : lambda oid: oid(".1.3.6.1.2.1.1.1.0") == "" and\
                                                  oid(".1.3.6.1.4.1.1139.21.2.2.8.1.*"),
    "snmp_info"                     : (".1.3.6.1.4.1.1139.21.2.2", [
                                       "1.1.3", # vplexDirectorName

                                       "4.1.1", # vplexDirectorFEOpsRead
                                       "4.1.2", # vplexDirectorFEOpsWrite
                                       "4.1.3", # vplexDirectorFEOpsQueued
                                       "4.1.4", # vplexDirectorFEOpsActive
                                       "4.1.5", # vplexDirectorFEOpsAvgReadLatency
                                       "4.1.6", # vplexDirectorFEOpsAvgWriteLatency
                                       "4.1.7", # vplexDirectorFEBytesRead
                                       "4.1.8", # vplexDirectorFEBytesWrite

                                       "6.1.1", # vplexDirectorBEOpsRead
                                       "6.1.2", # vplexDirectorBEOpsWrite
                                       "6.1.5", # vplexDirectorBEOpsAvgReadLatency
                                       "6.1.6", # vplexDirectorBEOpsAvgWriteLatency
                                       "6.1.7", # vplexDirectorBEBytesRead
                                       "6.1.8", # vplexDirectorBEBytesWrite
                                       OID_END ]),
    "has_perfdata"                  : True,
    "group"                         : "diskstat",
    "default_levels_variable"       : "diskstat_default_levels",
    "includes"                      : [ "diskstat.include" ],
}

