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

factory_settings['cisco_cpu_multiitem_default_levels'] = {
    'levels': (80.0, 90.0),
}


def parse_cisco_cpu_multiitem(info):
    ph_idx_to_desc = {}
    for idx, desc in info[1]:
        if desc.lower().startswith("cpu "):
            desc = desc[4:]
        ph_idx_to_desc[idx] = desc

    parsed = {}
    for idx, util in info[0]:
        name = ph_idx_to_desc.get(idx, idx)
        try:
            parsed[name] = {'util': float(util)}
        except ValueError:
            pass
    return parsed


@get_parsed_item_data
def check_cisco_cpu_multiitem(_no_item, params, data):
    warn, crit = params['levels']
    value = data['util']
    state, infotext = check_levels(value,
                                   None, (warn, crit),
                                   human_readable_func=get_percent_human_readable,
                                   infoname="Utilization in the last 5 minutes")[:2]
    yield state, infotext, [("util", value, warn, crit, 0, 100)]


check_info["cisco_cpu_multiitem"] = {
    'parse_function': parse_cisco_cpu_multiitem,
    'check_function': check_cisco_cpu_multiitem,
    'inventory_function': discover(),
    "group": "cpu_utilization_multiitem",
    "default_levels_variable": "cisco_cpu_multiitem_default_levels",
    'service_description': 'CPU utilization %s',
    'has_perfdata': True,
    'snmp_info': [
        (
            '.1.3.6.1.4.1.9.9.109.1.1.1',
            [
                '1.2',  # cpmCPUTotalPhysicalIndex
                '1.8',  # cpmCPUTotal5minRev
            ]),
        (
            '.1.3.6.1.2.1.47.1.1.1',
            [
                OID_END,  #OID index
                '1.7',  # entPhysicalName
            ])
    ],
    'snmp_scan_function': snmp_scan_cisco_cpu_multiitem,
    'includes': ["cisco_cpu_scan_functions.include"],
}
