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

# Example output from agent:
# <<<ibm_svc_enclosurestats:sep(58)>>>
# 1:power_w:207:218:140410113051
# 1:temp_c:22:22:140410113246
# 1:temp_f:71:71:140410113246
# 2:power_w:126:128:140410113056
# 2:temp_c:21:21:140410113246
# 2:temp_f:69:69:140410113246
# 3:power_w:123:126:140410113041
# 3:temp_c:22:22:140410113246
# 3:temp_f:71:71:140410113246
# 4:power_w:133:138:140410112821
# 4:temp_c:22:23:140410112836
# 4:temp_f:71:73:140410112836


#   .--temperature---------------------------------------------------------.
#   |      _                                      _                        |
#   |     | |_ ___ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |     | __/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |     | ||  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      \__\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

factory_settings["ibm_svc_enclosurestats_temperature_default_levels"] = {
    "levels": (35, 40)
}

def inventory_ibm_svc_enclosurestats_temp(info):
    for enclosure_id, stat_name, stat_current, stat_peak, stat_peak_time in info:
        if stat_name == "temp_c":
            yield enclosure_id, {}

def check_ibm_svc_enclosurestats_temp(item, params, info):
    for enclosure_id, stat_name, stat_current, stat_peak, stat_peak_time in info:
        if enclosure_id == item and stat_name == "temp_c":
            return check_temperature(int(stat_current), params, "ibm_svc_enclosurestats_%s" % item)


check_info["ibm_svc_enclosurestats.temp"] = {
    "check_function"          : check_ibm_svc_enclosurestats_temp,
    "inventory_function"      : inventory_ibm_svc_enclosurestats_temp,
    "service_description"     : "Temperature Enclosure %s",
    "has_perfdata"            : True,
    "group"                   : "temperature",
    "includes"                : [ "temperature.include" ],
    'default_levels_variable' : "ibm_svc_enclosurestats_temperature_default_levels"
}

#.
#   .--power---------------------------------------------------------------.
#   |                                                                      |
#   |                    _ __   _____      _____ _ __                      |
#   |                   | '_ \ / _ \ \ /\ / / _ \ '__|                     |
#   |                   | |_) | (_) \ V  V /  __/ |                        |
#   |                   | .__/ \___/ \_/\_/ \___|_|                        |
#   |                   |_|                                                |
#   '----------------------------------------------------------------------'

def inventory_ibm_svc_enclosurestats_power(info):
    inventory = []
    for enclosure_id, stat_name, stat_current, stat_peak, stat_peak_time in info:
        if stat_name == "power_w":
            inventory.append( (enclosure_id, None) )
    return inventory

def check_ibm_svc_enclosurestats_power(item, _no_params, info):
    perfdata = []

    for enclosure_id, stat_name, stat_current, stat_peak, stat_peak_time in info:
        if enclosure_id == item and stat_name == "power_w":
            stat_current = int(stat_current)
            perfdata = [ ('power', str(stat_current)+"Watt") ]
            return 0, "Enclosure %s Power Consumption is %s Watt" % (enclosure_id, stat_current), perfdata

    return 3, "Power for enclosure %s not found in agent output" % item

check_info["ibm_svc_enclosurestats.power"] = {
    "check_function"        : check_ibm_svc_enclosurestats_power,
    "inventory_function"    : inventory_ibm_svc_enclosurestats_power,
    "service_description"   : "Power Enclosure %s",
    "has_perfdata"          : True,
}

#.
