#!/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 = {}
    parsed = {}
    for idx, desc in info[1]:
        if desc.lower().startswith("cpu "):
           desc = desc[4:]
        ph_idx_to_desc[idx] = desc

    for idx, util in info[0]:
        if idx in ph_idx_to_desc:
            name = ph_idx_to_desc[idx]
        else:
            name = idx
        parsed[name] = {
            'util': float(util),
        }
    return parsed

def inventory_cisco_cpu_multiitem(parsed):
    for name in parsed:
        yield name, {}

def check_cisco_cpu_multiitem(item, params, parsed):
    warn, crit = params['levels']
    if item in parsed:
        util = parsed[item]['util']

        state = 0
        if util >= crit:
            state = 2
        elif util >= warn:
            state = 1

        infotext = "%s%% utilization in the last 5 minutes" % util
        if state > 0:
            infotext += " (warn/crit at %s%%/%s%%)" % (warn, crit)

        yield state, infotext, [("util", util, warn, crit, 0, 100)]

    else:
       yield 3, "No information about the CPU utilization available"

check_info["cisco_cpu_multiitem"] = {
    'parse_function'          : parse_cisco_cpu_multiitem,
    'check_function'          : check_cisco_cpu_multiitem,
    'inventory_function'      : inventory_cisco_cpu_multiitem,
    "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'      : lambda oid: 'cisco' in oid('.1.3.6.1.2.1.1.1.0').lower() and \
                                       oid('.1.3.6.1.4.1.9.9.109.1.1.1.*') == None,
}
