#!/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.df 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.hitachi_hnas 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.size_trend import *  # pylint: disable=wildcard-import,unused-wildcard-import


def parse_hitachi_hnas_volume(info):
    volumes, virtual_volumes, quotas = info

    map_label, parsed_volumes = parse_physical_volumes(volumes)
    parsed_virtual_volumes = parse_virtual_volumes(map_label, virtual_volumes, quotas)

    return {
        'volumes': parsed_volumes,
        'virtual_volumes': parsed_virtual_volumes,
    }


#.
#   .--Volume--------------------------------------------------------------.
#   |                __     __    _                                        |
#   |                \ \   / /__ | |_   _ _ __ ___   ___                   |
#   |                 \ \ / / _ \| | | | | '_ ` _ \ / _ \                  |
#   |                  \ V / (_) | | |_| | | | | | |  __/                  |
#   |                   \_/ \___/|_|\__,_|_| |_| |_|\___|                  |
#   |                                                                      |
#   '----------------------------------------------------------------------'


def inventory_hitachi_hnas_volume(parsed):
    return df_inventory(volume for volume in parsed['volumes'])


def check_hitachi_hnas_volume(item, params, parsed):
    volume_data = parsed['volumes'].get(item)
    if not volume_data:
        yield 3, "Volume %s not found" % item
        return
    status, size_mb, avail_mb, evs = volume_data

    if size_mb and avail_mb:
        fslist = [(item, size_mb, avail_mb, 0)]
        yield df_check_filesystem_list(item, params, fslist)
    else:
        yield 0, "no filesystem size information", []

    state_map = {
        'mounted': 0,
        'unformatted': 1,
        'formatted': 1,
        'needsChecking': 2,
    }

    if status == 'unidentified':
        yield 3, 'Volume reports unidentified status'
    else:
        yield state_map[status], 'Status: %s' % status

    yield 0, 'assigned to EVS %s' % evs


check_info["hitachi_hnas_volume"] = {
    "parse_function": parse_hitachi_hnas_volume,
    "check_function": check_hitachi_hnas_volume,
    "inventory_function": inventory_hitachi_hnas_volume,
    "service_description": "Volume %s",
    "has_perfdata": True,
    "snmp_info": [
        (  # BLUEARC-SERVER-MIB
            ".1.3.6.1.4.1.11096.6.1.1.1.3.5.2.1",
            [
                # volumeEntry
                1,  # volumeSysDriveIndex
                3,  # volumeLabel
                4,  # volumeStatus
                5,  # volumeCapacity
                6,  # volumeFreeCapacity
                7,  # volumeEnterpriseVirtualServer
            ]),
        (  # BLUEARC-TITAN-MIB
            ".1.3.6.1.4.1.11096.6.2.1.2.1.2.1",
            [
                # virtualVolumeTitanEntry
                OID_END,  # needed for referencing between tables
                1,  # virtualVolumeTitanSpanId
                2,  # virtualVolumeTitanName
            ]),
        (  # BLUEARC-TITAN-MIB
            ".1.3.6.1.4.1.11096.6.2.1.2.1.7.1",
            [
                # virtualVolumeTitanQuotasEntry
                OID_END,  # needed for referencing between tables
                3,  # virtualVolumeTitanQuotasTargetType
                4,  # virtualVolumeTitanQuotasUsage
                6,  # virtualVolumeTitanQuotasUsageLimit
            ]),
    ],
    "snmp_scan_function": hitachin_hnas_scan_function,
    "group": "filesystem",
    "default_levels_variable": "filesystem_default_levels",
}

#.
#   .--Virt. Volume--------------------------------------------------------.
#   |     __     ___      _      __     __    _                            |
#   |     \ \   / (_)_ __| |_    \ \   / /__ | |_   _ _ __ ___   ___       |
#   |      \ \ / /| | '__| __|    \ \ / / _ \| | | | | '_ ` _ \ / _ \      |
#   |       \ V / | | |  | |_ _    \ V / (_) | | |_| | | | | | |  __/      |
#   |        \_/  |_|_|   \__(_)    \_/ \___/|_|\__,_|_| |_| |_|\___|      |
#   |                                                                      |
#   +----------------------------------------------------------------------+


def inventory_hitachi_hnas_virtual_volume(parsed):
    return df_inventory(virtual_volume for virtual_volume in parsed['virtual_volumes'])


def check_hitachi_hnas_virtual_volume(item, params, parsed):
    quota = parsed['virtual_volumes'][item]
    if not quota:
        return 0, 'no quota defined', []

    size_mb, avail_mb = quota
    if size_mb and avail_mb:
        fslist = [(item, size_mb, avail_mb, 0)]
        return df_check_filesystem_list(item, params, fslist)
    return 0, 'no quota size information', []


check_info["hitachi_hnas_volume.virtual"] = {
    "inventory_function": inventory_hitachi_hnas_virtual_volume,
    "check_function": check_hitachi_hnas_virtual_volume,
    "service_description": "Virtual Volume %s",
    "has_perfdata": True,
    "group": "filesystem",
    "default_levels_variable": "filesystem_default_levels",
}
