#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             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.


def check_wmi_cpuload(item, params, parsed):
    # the processor queue length is logically similar to linux cpu load as
    # it states the number of processes waiting to be assigned cpu time.
    # unlike cpu load this does not include processes currently being processed,
    # so unless to cpu is (almost) fully utilized, this will be 0.

    load = int(parsed["system_perf"].get(0, "ProcessorQueueLength"))
    this_time = get_wmi_time(parsed["system_perf"], 0)
    load5min = get_average("load_5min", this_time, load, 5)
    load15min = get_average("load_15min", this_time, load, 15)

    try:
        cores = int(parsed["computer_system"].get(0, "NumberOfLogicalProcessors"))
    except (ValueError, KeyError), e:
        # NumberOfLogicalProcessors can be an empty string, not sure why
        cores = int(parsed["computer_system"].get(0, "NumberOfProcessors"))

    return check_cpu_load_generic(params, (load, load5min, load15min), cores)


check_info['wmi_cpuload'] = {
    'inventory_function'  : inventory_wmi_table,
    'check_function'      : check_wmi_cpuload,
    'parse_function'      : parse_wmi_table,
    'has_perfdata'        : True,
    'service_description' : "Processor Queue",
    'includes'            : ['wmi.include', 'cpu_load.include']
}
