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


#<<<scaleio_volume>>>
#VOLUME f6a9425800000002:
#        ID                                                 f6a9425800000002
#        NAME                                               SEASIOCF1001
#        SIZE                                               8.0 TB (8192 GB)
#        USER_DATA_READ_BWC                                 0 IOPS 0 Bytes per-second
#        USER_DATA_WRITE_BWC                                0 IOPS 0 Bytes per-second
#
#VOLUME f6a9425900000003:
#        ID                                                 f6a9425900000003
#        NAME                                               SEASIOCF2001
#        SIZE                                               5.0 TB (5120 GB)
#        USER_DATA_READ_BWC                                 0 IOPS 0 Bytes per-second
#        USER_DATA_WRITE_BWC                                0 IOPS 0 Bytes per-second
#


def inventory_scaleio_volume(parsed):
    for entry in parsed:
        yield entry, {}


def check_scaleio_volume(item, params, parsed):
    change_unit = {
        "KB" : "MB",
        "MB" : "GB",
        "GB" : "TB",
    }

    data = get_scaleio_data(item, parsed)
    if not data:
        return

    name = data["NAME"][0]
    total = int(data["SIZE"][2].strip("("))
    unit = data["SIZE"][3].strip(")")

    # Assuming the API will never report
    # a number bigger than 1048576
    if total > 1024:
        total = total / 1024
        unit = change_unit[unit]
    yield 0, "Name: %s, Size: %.1f %s" % (name, total, unit)

    read_data = data["USER_DATA_READ_BWC"]
    write_data = data["USER_DATA_WRITE_BWC"]
    for io_type in list(check_diskstat_dict(item, params, get_disks(item, read_data, write_data))):
        yield io_type


check_info['scaleio_volume'] = {
    'parse_function'        : lambda info: parse_scaleio(info, "VOLUME"),
    'inventory_function'    : inventory_scaleio_volume,
    'check_function'        : check_scaleio_volume,
    'service_description'   : 'ScaleIO Volume %s',
    'includes'              : [ 'scaleio.include', 'diskstat.include' ],
    'has_perfdata'          : True,
    'group'                 : 'diskstat',
}
