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

arris_cmts_cpu_default_levels = ( 90, 95 )

def inventory_arris_cmts_cpu(info):
    for oid_id, cpu_id, cpu_idle_util in info:
        # Sadly the cpu_id seams empty. Referring to
        # the MIB, its slot id
        if cpu_id:
            yield cpu_id, 'arris_cmts_cpu_default_levels'
        else:
            # Fallback to the oid end
            item = int(oid_id) - 1
            yield item, 'arris_cmts_cpu_default_levels'

def check_arris_cmts_cpu(item, params, info):
    for oid_id, cpu_id, cpu_idle_util in info:
        # see inventory function
        if cpu_id:
            citem = cpu_id
        else:
            citem = int(oid_id) -1

        if citem == item:
            # We get the IDLE percentage, but need the usage
            cpu_util = 100 - int(cpu_idle_util)
            warn, crit = params
            infotext = "Current utilization is: %d %% " % cpu_util
            levels = " (warn/crit at %.1f/%.1f %%)" % (warn, crit)
            perfdata = [ ( "util", cpu_util, warn, crit ) ]
            if cpu_util >= crit:
                yield 2, infotext + levels, perfdata
            elif cpu_util >= warn:
                yield 1, infotext + levels, perfdata
            else:
                yield 0, infotext, perfdata
            return
    yield 3, "CPU information not found"

check_info["arris_cmts_cpu"] = {
    "check_function"        : check_arris_cmts_cpu,
    "inventory_function"    : inventory_arris_cmts_cpu,
    "service_description"   : "CPU utilization Module %s",
    "has_perfdata"          : True,
    "snmp_scan_function"    : arris_cmts_scan_function,
    "snmp_info"             : ( ".1.3.6.1.4.1.4998.1.1.5.3.1.1.1", [
                                          OID_END,
                                          1, # cadCpuCardId
                                          8, # cadIdleCpuRecentPercent
                                          ] ),
    "group"                 : "cpu_utilization_multiitem",
    "includes"              : [ "arris_cmts.include" ]
}

