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


# the check handles only power supply input voltage currently, but there
# are sensors for 1.0V, 1.5V, 3.3V, 12V, ... outputs.
emc_isilon_power_default_levels = (0.5, 0.0)


# Power Supply 1 Input Voltage --> Power Supply 1 Input
# Battery 1 Voltage (now) --> Battery 1 (now)
# Voltage 1.5v --> 1.5v
def isilon_power_item_name(sensor_name):
    return sensor_name.replace("Voltage", "").replace("  ", " ").strip()


def inventory_emc_isilon_power(info):
    for line in info:
        # only monitor power supply currently
        if "Power Supply" in line[0]:
            yield isilon_power_item_name(line[0]), "emc_isilon_power_default_levels"


def check_emc_isilon_power(item, params, info):
    for line in info:
        if item == isilon_power_item_name(line[0]):
            volt = float(line[1])

            infotext = "%.1f V" % volt
            warn_lower, crit_lower = params
            levelstext = " (warn/crit below %.1f/%.1f V)" % (warn_lower, crit_lower)

            if volt < crit_lower:
                state = 2
                infotext += levelstext
            elif volt < warn_lower:
                state = 1
                infotext += levelstext
            else:
                state = 0

            return state, infotext


check_info["emc_isilon_power"] = {
    "check_function"          : check_emc_isilon_power,
    "inventory_function"      : inventory_emc_isilon_power,
    "service_description"     : "Voltage %s",
    "has_perfdata"            : False,
    "snmp_info"               : (".1.3.6.1.4.1.12124.2.55.1",
                                 [
                                     3, # powerSensorDescription
                                     4, # powerSensorValue
                                 ]
                                 ),
    "snmp_scan_function"      : lambda oid: "isilon" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    "group"                   : "evolt",
}

