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

def inventory_isilon_temp(info, is_cpu):
    for sensor_name, value in info:
        item_name = isilon_temp_item_name(sensor_name)
        if is_cpu == item_name.startswith("CPU"):
            yield item_name, {}


def check_isilon_temp(item, params, info):
    for sensor_name, value in info:
        if item == isilon_temp_item_name(sensor_name):
            return check_temperature(float(value), params, "isilon_%s" % item)

# Expected sensor names:
# "Temp Until CPU Throttle (CPU 0)"
# "Temp Until CPU Throttle (CPU 1)"
# "Temp Chassis 1 (ISI T1)"
# "Temp Front Panel"
# "Temp Power Supply 1"
# "Temp Power Supply 2"
# "Temp System"
def isilon_temp_item_name(sensor_name):
    if "CPU Throttle" in sensor_name:
        return sensor_name.split("(")[1].split(")")[0] # "CPU 1"
    else:
        return sensor_name[5:] # "Front Panel"


#   .--Air Temperature-----------------------------------------------------.
#   |                              _    _                                  |
#   |                             / \  (_)_ __                             |
#   |                            / _ \ | | '__|                            |
#   |                           / ___ \| | |                               |
#   |                          /_/   \_\_|_|                               |
#   |                                                                      |
#   |     _____                                   _                        |
#   |    |_   _|__ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |      | |/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |      | |  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      |_|\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

factory_settings["emc_isilon_temp_default_levels"] = {
    "levels" : (28, 33), # assumed useful levels for ambient / air temperature
}


check_info['emc_isilon_temp'] = {
  "inventory_function"        : lambda info: inventory_isilon_temp(info, is_cpu=False),
  "check_function"            : check_isilon_temp,
  "service_description"       : "Temperature %s",
  "has_perfdata"              : True,
  "group"                     : "temperature",
  "snmp_info"                 : ( ".1.3.6.1.4.1.12124.2.54.1", [
                                  "3", # ISILON-MIB::tempSensorDescription
                                  "4", # ISILON-MIB::tempSensorValue
                                ]),
  "snmp_scan_function"        : lambda oid: "isilon" in oid(".1.3.6.1.2.1.1.1.0").lower(),
  "includes"                  : [ "temperature.include" ],
  "default_levels_variable"   : "emc_isilon_temp_default_levels",
}


#.
#   .--CPU Temperature-----------------------------------------------------.
#   |                           ____ ____  _   _                           |
#   |                          / ___|  _ \| | | |                          |
#   |                         | |   | |_) | | | |                          |
#   |                         | |___|  __/| |_| |                          |
#   |                          \____|_|    \___/                           |
#   |                                                                      |
#   |     _____                                   _                        |
#   |    |_   _|__ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |      | |/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |      | |  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      |_|\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

factory_settings["emc_isilon_temp_cpu_default_levels"] = {
    "levels" : (75, 85), # assumed useful levels for ambient / air temperature
}

check_info['emc_isilon_temp.cpu'] = {
  "inventory_function"      : lambda info                           : inventory_isilon_temp(info, is_cpu=True),
  "check_function"          : check_isilon_temp,
  "service_description"     : "Temperature %s",
  "has_perfdata"            : True,
  "group"                   : "temperature",
  "includes"                : [ "temperature.include" ],
  "default_levels_variable" : "emc_isilon_temp_cpu_default_levels",
}
