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

vms_cpu_default_levels = None

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


def inventory_vms_cpu(info):
    if len(info) > 0:
        return [(None, "vms_cpu_default_levels")]

def check_vms_cpu(item, params, info):
    num_cpus = int(info[0][0])
    idle, user, wait_interrupt, wait_npsync = map(lambda x: float(x) / num_cpus, info[0][1:])
    wait = wait_interrupt + wait_npsync
    system = 100.0 - idle - user - wait
    infotext = "user: %2.1f%%, system: %2.1f%%, wait: %2.1f%%" % (user, system, wait)
    state = 0
    perfdata = [
          ( "user",   user ),
          ( "system", system ),
          ( "wait",   wait, 0, 0 ) ]
    if params:
        perfdata = [
              ( "user",   user ),
              ( "system", system ),
              ( "wait",   wait, warn, crit ) ]
        warn, crit = params
        if wait >= crit:
            state = 2
            infotext += '(!!)'
        elif wait >= warn:
            state = 1
            infotext += '(!)'

    return (state, infotext, perfdata)

check_info['vms_cpu'] = {
    "check_function" :      check_vms_cpu,
    "inventory_function" :  inventory_vms_cpu,
    "service_description" : "CPU utilization",
    "has_perfdata" :        True,
    "group" :               "cpu_iowait",
}
