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


def inventory_3par_hosts(parsed):
    for entry in parsed.get('members', {}):
        if "name" in entry:
            yield (entry['name'], None)


def check_3par_hosts(item, _no_params, parsed):
    for entry in parsed.get('members', {}):
        if item == entry.get('name'):
            yield 0, "ID: %s" % entry['id']

            os = entry.get('descriptors', {}).get('os')
            if os is not None:
                yield 0, "OS: %s" % os

            if len(entry.get('FCPaths', 0)) > 0:
                yield 0, "FC Paths: %s" % len(entry['FCPaths'])
            elif len(entry.get('iSCSIPaths', 0)) > 0:
                yield 0, "iSCSI Paths: %s" % len(entry['iSCSIPaths'])


check_info['3par_hosts'] = {
    'parse_function': parse_3par,
    'inventory_function': inventory_3par_hosts,
    'check_function': check_3par_hosts,
    'service_description': "Host %s",
}
