#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# Example output from agent:
# <<<vms_cpu>>>
# 1 99.17 0.54 0.18 0.00


def parse_vms_cpu(info):
    parsed = {}
    try:
        parsed['num_cpus'] = int(info[0][0])
        for i, key in enumerate(('idle', 'user', 'wait_interrupt', 'wait_npsync'), 1):
            parsed[key] = float(info[0][i]) / parsed['num_cpus']
    except (IndexError, ValueError):
        return {}

    return parsed


def inventory_vms_cpu(info):
    if info:
        yield None, {}


def check_vms_cpu(_no_item, params, parsed):
    # ancient tuple rule
    # and legacy default None prior to 1.6
    params = transform_cpu_iowait(params)

    user = parsed["user"]
    wait = parsed["wait_interrupt"] + parsed["wait_npsync"]
    util = 100. - parsed["idle"]
    system = util - user - wait

    yield check_levels(user,
                       "user",
                       None,
                       human_readable_func=get_percent_human_readable,
                       infoname="User")
    yield check_levels(system,
                       "system",
                       None,
                       human_readable_func=get_percent_human_readable,
                       infoname="System")
    yield check_levels(wait,
                       "wait",
                       params.get("iowait"),
                       human_readable_func=get_percent_human_readable,
                       infoname="Wait")

    for util_result in check_cpu_util(util, params):
        yield util_result

    num_cpus = parsed['num_cpus']
    unit = "CPU" if num_cpus == 1 else "CPUs"
    yield check_levels(num_cpus,
                       'cpu_entitlement',
                       None,
                       unit=unit,
                       infoname="100% corresponding to")


check_info['vms_cpu'] = {
    "parse_function": parse_vms_cpu,
    "check_function": check_vms_cpu,
    "inventory_function": inventory_vms_cpu,
    "service_description": "CPU utilization",
    "has_perfdata": True,
    "group": "cpu_iowait",
    "includes": ["transforms.include", "cpu_util.include"],
}
