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

# Agent output:
# <<<netapp_api_disk:sep(9)>>>


def inventory_netapp_api_disk_summary(info):
    return [(None, {})]


def check_netapp_api_disk_summary(_no_item, params, info):
    disks_info = {}
    for line in info:
        disk_uid = line[0].split()[1]
        disks_info.setdefault(disk_uid, {})
        for entry in line[1:]:
            tokens = entry.split(" ", 1)
            disks_info[disk_uid][tokens[0]] = tokens[1]

    # Convert legacy levels
    if "broken_spare_ratio" in params:
        params = {"failed_spare_ratio": params["broken_spare_ratio"]}

    disks = disks_info

    # The device can contain disks who belongs to another filer. We only check the ones
    # belonging to this filer.
    # TODO Do we need that?
    my_disks = dict(
        [disk for disk in disks.items() if not disk[1].get("raid-state") in ["remote", "partner"]])

    disks_converted = []
    for disk in my_disks.itervalues():
        # Collection the disk identity
        disk_info = "Serial: %s" % disk.get("serial-number")
        if "physical-space" in disk:
            disk_info += ", Size: %s" % get_bytes_human_readable(int(disk["physical-space"]))
            disk['capacity'] = int(disk['physical-space'])

        disk['identifier'] = disk_info
        disk['type'] = False
        raid_type = disk.get("raid-type")
        raid_state = disk.get("raid-state")
        if raid_state == "broken":
            disk['state'] = 'failed'
        elif disk.get("prefailed", "false") not in ["false", "None"]:
            disk['state'] = 'prefailed'
        elif raid_state == "spare":
            disk['state'] = "spare"
        else:
            disk['state'] = 'ok'

        if raid_type in ["parity", "dparity"]:
            disk['type'] = 'parity'
        elif raid_type == "data":
            disk['type'] = 'data'

        disks_converted.append(disk)

    return check_filer_disks(disks_converted, params)


check_info["netapp_api_disk.summary"] = {
    'check_function': check_netapp_api_disk_summary,
    'inventory_function': inventory_netapp_api_disk_summary,
    'service_description': 'NetApp Disks Summary',
    'group': 'netapp_disks',
    'has_perfdata': True,
    'default_levels_variable': 'filer_disks_default_levels',
    'includes': ['filerdisks.include'],
}
