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


def parse_brocade_sys(info):
    try:
        return {
            "cpu_util": int(info[0][0]),
            "mem_used_percent": int(info[0][1]),
        }
    except (IndexError, ValueError):
        return {}


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


def inventory_brocade_sys_mem(parsed):
    return [(None, None)]


def check_brocade_sys_mem(item, params, parsed):
    mem_used_percent = parsed["mem_used_percent"]
    infotext = "%s%%" % mem_used_percent
    if not params:
        perfdata = [("mem_used_percent", mem_used_percent)]
        return 0, infotext, perfdata
    else:
        warn, crit = params
        perfdata = [("mem_used_percent", mem_used_percent, warn, crit)]
        levelstext = " (warn/crit at %d/%d%%)" % (warn, crit)
        if mem_used_percent >= crit:
            status = 2
        elif mem_used_percent >= warn:
            status = 1
        else:
            status = 0
        if status:
            infotext += levelstext
        return status, infotext, perfdata


check_info["brocade_sys.mem"] = {
    "inventory_function": inventory_brocade_sys_mem,
    "check_function": check_brocade_sys_mem,
    "service_description": "Memory",
    "group": "memory_relative",
    "has_perfdata": True,
}

# .
#   .--CPU-----------------------------------------------------------------.
#   |                           ____ ____  _   _                           |
#   |                          / ___|  _ \| | | |                          |
#   |                         | |   | |_) | | | |                          |
#   |                         | |___|  __/| |_| |                          |
#   |                          \____|_|    \___/                           |
#   |                                                                      |
#   '----------------------------------------------------------------------'


def inventory_brocade_sys(parsed):
    return [(None, None)]


def check_brocade_sys(item, params, parsed):
    return check_cpu_util(parsed["cpu_util"], params)


def scan_brocade_sys(oid):
    return (
        oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.1588.2.1.1")
        or
        # Extreme SLX9640; SLX Operating System Version 20.1.X:
        oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.1916.2.306"
    )


check_info["brocade_sys"] = {
    "parse_function": parse_brocade_sys,
    "inventory_function": inventory_brocade_sys,
    "check_function": check_brocade_sys,
    "service_description": "CPU utilization",
    "snmp_info": (
        ".1.3.6.1.4.1.1588.2.1.1.1.26",
        [  # Brocade-REG-MIB
            "1",  # swCpuUsage
            "6",  # swMemoryUsage
        ],
    ),
    "snmp_scan_function": scan_brocade_sys,
    "group": "cpu_utilization",
    "has_perfdata": True,
}
