#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.diskstat import *  # pylint: disable=wildcard-import,unused-wildcard-import
# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.scaleio import *  # pylint: disable=wildcard-import,unused-wildcard-import
#<<<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',
    'has_perfdata': True,
    'group': 'diskstat',
}
