#!/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_load import *  # pylint: disable=wildcard-import,unused-wildcard-import
# <<<hpux_cpu>>>
#  2:23pm  up 196 days,  4:47,  5 users,  load average: 0.05, 0.04, 0.04
#      8 logical processors (2 per socket)

cpuload_default_levels = (5.0, 10.0)


def inventory_hpux_cpu_load(info):
    if len(info) >= 1 and 'load' in info[0]:
        return [(None, "cpuload_default_levels")]


def check_hpux_cpu_load(item, params, info):
    load = [float(l.strip(',')) for l in info[0][-3:]]
    # Assuming 1 cpu when none can be found in the agent output
    try:
        num_cpus = int(info[1][0])
    except Exception:
        num_cpus = 1

    return check_cpu_load_generic(params, load, num_cpus)


check_info["hpux_cpu"] = {
    'check_function': check_hpux_cpu_load,
    'inventory_function': inventory_hpux_cpu_load,
    'service_description': 'CPU load',
    'has_perfdata': True,
    "group": "cpu_load",
}
