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

# .1.3.6.1.4.1.789.1.5.8.1.1.1 = INTEGER: 1
# .1.3.6.1.4.1.789.1.5.8.1.1.2 = INTEGER: 2
# .1.3.6.1.4.1.789.1.5.8.1.1.3 = INTEGER: 3
# .1.3.6.1.4.1.789.1.5.8.1.2.1 = STRING: "vol0"
# .1.3.6.1.4.1.789.1.5.8.1.2.2 = STRING: "RvRBackup"
# .1.3.6.1.4.1.789.1.5.8.1.2.3 = STRING: "RundV"
# .1.3.6.1.4.1.789.1.5.8.1.3.1 = STRING: "67155442"
# .1.3.6.1.4.1.789.1.5.8.1.3.2 = STRING: "2600515058"
# .1.3.6.1.4.1.789.1.5.8.1.3.3 = STRING: "788575730"
# .1.3.6.1.4.1.789.1.5.8.1.4.1 = INTEGER: 1
# .1.3.6.1.4.1.789.1.5.8.1.4.2 = INTEGER: 1
# .1.3.6.1.4.1.789.1.5.8.1.4.3 = INTEGER: 1
# .1.3.6.1.4.1.789.1.5.8.1.5.1 = STRING: "online"
# .1.3.6.1.4.1.789.1.5.8.1.5.2 = STRING: "online"
# .1.3.6.1.4.1.789.1.5.8.1.5.3 = STRING: "online"
# .1.3.6.1.4.1.789.1.5.8.1.6.1 = STRING: "raid_dp"
# .1.3.6.1.4.1.789.1.5.8.1.6.2 = STRING: "raid_dp"
# .1.3.6.1.4.1.789.1.5.8.1.6.3 = STRING: "raid_dp"
# .1.3.6.1.4.1.789.1.5.8.1.7.1 = STRING: "root, diskroot, nosnap=off,
#  nosnapdir=off, minra=off, no_atime_update=off, raidtype=raid_dp, raidsize=16,
#  nvfail=off, snapmirrored=off, resyncsnaptime=60, create_ucode=off,
#  convert_ucode=off, maxdirsize=10485, fs_size_fixed=off, guarantee=volume,
#  svo_enable=off, svo_checksum=off, svo_allow_rman=off, svo_reject_errors=off,
#  no_i2p=off, fractional_reserve=100, extent=off, try_first=volume_grow"
# .1.3.6.1.4.1.789.1.5.8.1.7.2 = STRING: "nosnap=off, nosnapdir=off,
#  minra=off, no_atime_update=off, raidtype=raid_dp, raidsize=16, nvfail=off,
#  snapmirrored=off, resyncsnaptime=60, create_ucode=off, convert_ucode=off,
#  maxdirsize=10485, fs_size_fixed=off, guarantee=volume, svo_enable=off,
#  svo_checksum=off, svo_allow_rman=off, svo_reject_errors=off, no_i2p=off,
#  fractional_reserve=100, extent=off, try_first=volume_grow"
# .1.3.6.1.4.1.789.1.5.8.1.7.3 = STRING: "nosnap=on, nosnapdir=on,
#  minra=off, no_atime_update=off, raidtype=raid_dp, raidsize=16, nvfail=off,
#  snapmirrored=off, resyncsnaptime=60, create_ucode=off, convert_ucode=off,
#  maxdirsize=10485, fs_size_fixed=off, guarantee=volume, svo_enable=off,
#  svo_checksum=off, svo_allow_rman=off, svo_reject_errors=off, no_i2p=off,
#  fractional_reserve=100, extent=off, try_first=volume_grow"
# .1.3.6.1.4.1.789.1.5.8.1.8.1 = STRING: "1ddc9920-496e-11df-aae1-00a09800c998"
# .1.3.6.1.4.1.789.1.5.8.1.8.2 = STRING: "62ac1040-5a8d-11df-83fd-00a09800c998"
# .1.3.6.1.4.1.789.1.5.8.1.8.3 = STRING: "4edc66e0-d6a3-11df-8724-00a09800c998"
# .1.3.6.1.4.1.789.1.5.8.1.9.1 = STRING: "aggr0"
# .1.3.6.1.4.1.789.1.5.8.1.9.2 = STRING: "aggr0"
# .1.3.6.1.4.1.789.1.5.8.1.9.3 = STRING: "aggr0"
# .1.3.6.1.4.1.789.1.5.8.1.10.1 = INTEGER: 2
# .1.3.6.1.4.1.789.1.5.8.1.10.2 = INTEGER: 2
# .1.3.6.1.4.1.789.1.5.8.1.10.3 = INTEGER: 2
# .1.3.6.1.4.1.789.1.5.8.1.11.1 = INTEGER: 1
# .1.3.6.1.4.1.789.1.5.8.1.11.2 = INTEGER: 1
# .1.3.6.1.4.1.789.1.5.8.1.11.3 = INTEGER: 1
# .1.3.6.1.4.1.789.1.5.8.1.12.1 = ""
# .1.3.6.1.4.1.789.1.5.8.1.12.2 = ""
# .1.3.6.1.4.1.789.1.5.8.1.12.3 = ""
# .1.3.6.1.4.1.789.1.5.8.1.13.1 = ""
# .1.3.6.1.4.1.789.1.5.8.1.13.2 = ""
# .1.3.6.1.4.1.789.1.5.8.1.13.3 = ""

netapp_volumes_owner = {
    "1": "local",
    "2": "partner",
}


def inventory_netapp_volumes(info):
    # Only inventorize local volumes
    return [(line[0], None) for line in info if netapp_volumes_owner.get(line[2], "") == "local"]


def check_netapp_volumes(item, _not_used, info):
    for line in info:
        if line[0] == item:
            _name, fsid, owner, state, status = line

            output = "FSID: %s, Owner: %s, " % (fsid, netapp_volumes_owner.get(owner, "UNKNOWN"))

            ret_state = 0
            output += "State: %s" % state
            if state == "offline":
                ret_state = 1
                output += "(!)"

            output += ", Status: %s" % status
            if status == "reconstructing":
                ret_state = 1
                output += "(!)"
            elif status.split(",")[0] not in [
                "normal",
                "raid_dp",
                "raid0",
                "raid0, mirrored",
                "raid4",
                "mixed_raid_type",
            ]:
                ret_state = 2
                output += "(!!)"

            return (ret_state, output)
    return 3, "Volume not found"


check_info["netapp_volumes"] = {
    "check_function": check_netapp_volumes,
    "inventory_function": inventory_netapp_volumes,
    "service_description": "NetApp Vol %s",
    "snmp_info": (
        ".1.3.6.1.4.1.789.1.5.8.1",
        [
            "2",  # volName
            "3",  # volFSID
            "4",  # volOwningHost
            "5",  # volState
            "6",  # volStatus
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("NetApp Release")
    and oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.789"),
}
