#!/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 inventory_emc_isilon_cpu_utilization(info):
    # the device reports cpu utilization for each core and a total. This interprets only the total
    return [(None, None)]


def check_emc_isilon_cpu_utilization(item, params, info):
    def range_index(value, *dividers):
        idx = 0
        for div in dividers:
            if value < div:
                break
            else:
                idx += 1
        return idx

    # expecting only one line because why would there be multiple totals?
    for line in info:
        # all utilizations are in per mil
        # grouping user+nice and system+interrupt, the same way cpu_util.include does
        user_perc = (int(line[0]) + int(line[1])) * 0.1
        system_perc = int(line[2]) * 0.1
        interrupt_perc = int(line[3]) * 0.1
        total_perc = (user_perc + system_perc + interrupt_perc)

        infotext = [
            "user: %.1f%%" % user_perc,
            ", system: %.1f%%" % system_perc,
            ", total: %.1f%%" % total_perc
        ]

        status = 0
        if params:
            warn, crit = params
            status = range_index(total_perc, warn, crit)

            if status != 0:
                infotext.append(" (warn/crit at %.1f%%/%.1f%%)" % (warn, crit))

        # perfometer expects a wait value but we don't have data for that
        perfdata = [("user", "%.3f" % user_perc), ("system", "%.3f" % system_perc),
                    ("interrupt", "%.3f" % interrupt_perc)]

        return status, "".join(infotext), perfdata


check_info["emc_isilon_cpu"] = {
    "check_function": check_emc_isilon_cpu_utilization,
    "inventory_function": inventory_emc_isilon_cpu_utilization,
    "service_description": "Node CPU utilization",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.12124.2.2.3",
        [
            1,  #nodeCPUUser
            2,  #nodeCPUNice
            3,  #nodeCPUSystem
            4,  #nodeCPUInterrupt
        ]),
    "snmp_scan_function": lambda oid: "isilon" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    "group": "cpu_utilization",
}
