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

juniper_screenos_cpu_default_levels = (80.0, 90.0)


def inventory_juniper_screenos_cpu(info):
    return [(None, "juniper_screenos_cpu_default_levels")]


def check_juniper_screenos_cpu(_no_item, params, info):
    util1, util15 = map(float, info[0])
    warn, crit = params.get("util", (None, None)) if isinstance(params, dict) else params
    label15 = ""
    state = 0
    if util15 >= crit:
        state = 2
        label15 = "(!!)"
    elif util15 >= warn:
        state = max(state, 1)
        label15 = "(!)"

    perf = [
        ("util1", util1, warn, crit),
        ("util15", util15, warn, crit),
    ]

    message = "%d%% 1min, %d%% 15min%s (warn/crit at %d%%/%d%%)" % (
        util1,
        util15,
        label15,
        warn,
        crit,
    )
    return state, message, perf


check_info["juniper_screenos_cpu"] = {
    "check_function": check_juniper_screenos_cpu,
    "inventory_function": inventory_juniper_screenos_cpu,
    "group": "cpu_utilization",
    "service_description": "CPU utilization",
    "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.3224.1"),
    "snmp_info": (
        ".1.3.6.1.4.1.3224.16.1",
        [
            2,  # jnxOperating1MinLoadAvg
            4,  # jnxOperating15MinLoadAvg
        ],
    ),
}
