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

threads_default_levels = {"levels": ("levels", (2000, 4000))}


def inventory_bluecat_threads(info):
    if info:
        return [(None, "threads_default_levels")]


def check_bluecat_threads(item, params, info):
    nthreads = int(info[0][0])
    warn, crit = None, None
    if "levels" in params and params["levels"] != "no_levels":
        warn, crit = params["levels"][1]
    perfdata = [("threads", nthreads, warn, crit, 0)]
    if crit is not None and nthreads >= crit:
        return (2, "%d threads (critical at %d)" % (nthreads, crit), perfdata)
    elif warn is not None and nthreads >= warn:
        return (1, "%d threads (warning at %d)" % (nthreads, warn), perfdata)
    return (0, "%d threads" % (nthreads,), perfdata)


check_info["bluecat_threads"] = {
    "check_function": check_bluecat_threads,
    "inventory_function": inventory_bluecat_threads,
    "service_description": "Number of threads",
    "group": "threads",
    "has_perfdata": True,
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(
        ".1.3.6.1.4.1.13315.100.200"
    ),
    "snmp_info": (
        ".1.3.6.1.4.1.13315.100.200.1.1.2",
        [
            1,  # activeThreadCount
        ],
    ),
}
