#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2016             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.

# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.39 1 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.cpu1-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.40 5 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.cpu2-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.41 1 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.sys-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.39 CPU_TEMP: +36.00 C --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.cpu1-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.40 No temperature information available. --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.cpu2-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.41 SYS_TEMP: +34.00 C --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.sys-temp

# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.39 5 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.cpu1-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.40 5 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.cpu2-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.41 5 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.sys-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.39 --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.cpu1-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.40 --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.cpu2-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.41 --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.sys-temp

# Suggested by customer
factory_settings['infoblox_temp_default_levels'] = {
    "levels": (40, 50),
}


def parse_infoblox_temp(info):
    map_states = {
        "1": (0, "working"),
        "2": (1, "warning"),
        "3": (2, "failed"),
        "4": (1, "inactive"),
        "5": (3, "unknown"),
    }

    parsed = {}
    # Just for a better handling
    for index, state, descr in \
        zip(["", "1", "2", ""], info[0], info[1])[1:]:
        if ":" in descr:
            name, val_str = descr.split(":", 1)
            val, unit = val_str.split()
            val = float(val)

        else:
            name = descr
            val = None
            unit = None

        what_name = "%s %s" % (name, index)
        parsed.setdefault(what_name.strip(), {
            "state": map_states[state],
            "reading": val,
            "unit": unit,
        })

    return parsed


def inventory_infoblox_temp(parsed):
    for name, infos in parsed.items():
        if infos["reading"] is not None and infos["unit"] is not None:
            yield name, {}


def check_infoblox_temp(item, params, parsed):
    if item in parsed:
        reading = parsed[item]["reading"]
        devunit = parsed[item]["unit"].lower()
        devstate, devstatename = parsed[item]["state"]
        return check_temperature(reading,
                                 params,
                                 "infoblox_cpu_temp_%s" % item,
                                 dev_status=devstate,
                                 dev_status_name=devstatename,
                                 dev_unit=devunit)


check_info['infoblox_temp'] = {
    'parse_function': parse_infoblox_temp,
    'inventory_function': inventory_infoblox_temp,
    'check_function': check_infoblox_temp,
    'service_description': 'Temperature %s',
    'has_perfdata': True,
    'snmp_info': (
        ".1.3.6.1.4.1.7779.3.1.1.2.1.10.1",
        ["2", "3"],
        [
            OID_END,
            "39",  # IB-PLATFORMONE-MIB::ibNodeService[Desc/Status].cpu1-temp
            "40",  # IB-PLATFORMONE-MIB::ibNodeService[Desc/Status].cpu2-temp
            "41",  # IB-PLATFORMONE-MIB::ibNodeService[Desc/Status].sys-temp
        ]),
    'snmp_scan_function': scan_infoblox,
    'group': 'temperature',
    'default_levels_variable': 'infoblox_temp_default_levels',
    'includes': ["infoblox.include", "temperature.include"],
}
