#!/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_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",
    "includes": ["size_trend.include", "df.include", "hitachi_hnas.include"],
}

#.
#   .--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",
}
