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

# <<<hp_msa_psu>>>
# power-supplies 1 durable-id psu_1.1
# power-supplies 1 enclosure-id 1
# power-supplies 1 serial-number 7CE451T700
# power-supplies 1 description FRU,Pwr Sply,595W,AC,2U,LC,HP
# power-supplies 1 name PSU 1, Left
# power-supplies 1 revision D1
# power-supplies 1 model 592267-002
# power-supplies 1 vendor 0x
# power-supplies 1 location Enclosure 1 - Left
# power-supplies 1 position Left
# power-supplies 1 position-numeric 0
# power-supplies 1 part-number 592267-002
# power-supplies 1 dash-level
# power-supplies 1 fru-shortname AC Power Supply
# power-supplies 1 mfg-date 2014-10-29 16:57:47
# power-supplies 1 mfg-date-numeric 1414601867
# power-supplies 1 mfg-location Zhongshan,Guangdong,CN
# power-supplies 1 mfg-vendor-id 0x
# power-supplies 1 configuration-serialnumber 7CE451T700
# power-supplies 1 dc12v 1195
# power-supplies 1 dc5v 508
# power-supplies 1 dc33v 336
# power-supplies 1 dc12i 548
# power-supplies 1 dc5i 489
# power-supplies 1 dctemp 34
# power-supplies 1 health OK
# power-supplies 1 health-numeric 0
# power-supplies 1 health-reason
# power-supplies 1 health-recommendation
# power-supplies 1 status Up
# power-supplies 1 status-numeric 0

#   .--health--------------------------------------------------------------.
#   |                    _                _ _   _                          |
#   |                   | |__   ___  __ _| | |_| |__                       |
#   |                   | '_ \ / _ \/ _` | | __| '_ \                      |
#   |                   | | | |  __/ (_| | | |_| | | |                     |
#   |                   |_| |_|\___|\__,_|_|\__|_| |_|                     |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |                            main check                                |
#   '----------------------------------------------------------------------'

check_info['hp_msa_psu'] = {
    'parse_function': parse_hp_msa,
    'inventory_function': inventory_hp_msa_health,
    'check_function': check_hp_msa_health,
    'service_description': 'Power Supply Health %s',
    'includes': ["hp_msa.include"],
}

#.
#   .--voltage-------------------------------------------------------------.
#   |                             _ _                                      |
#   |                 __   _____ | | |_ __ _  __ _  ___                    |
#   |                 \ \ / / _ \| | __/ _` |/ _` |/ _ \                   |
#   |                  \ V / (_) | | || (_| | (_| |  __/                   |
#   |                   \_/ \___/|_|\__\__,_|\__, |\___|                   |
#   |                                        |___/                         |
#   '----------------------------------------------------------------------'

# Just an assumption
factory_settings["hp_msa_psu_default_levels"] = {
    "levels_33v_lower": (3.25, 3.20),
    "levels_33v_upper": (3.4, 3.45),
    "levels_5v_lower": (4.9, 4.8),
    "levels_5v_upper": (5.1, 5.2),
    "levels_12v_lower": (11.9, 11.8),
    "levels_12v_upper": (12.1, 12.2),
}


def inventory_hp_msa_psu(parsed):
    """detect if PSU info is invalid

    Some fields where deprecated for HP MSA 1050/2050.
    If the PSU is freezing and has no voltage we assume
    that means data is not valid
    """
    indicators = ('dc12v', 'dc5v', 'dc33v', 'dc12i', 'dc5i', 'dctemp')
    for item, data in parsed.iteritems():
        if any(data.get(i) != '0' for i in indicators):
            yield item, {}


@get_parsed_item_data
def check_hp_msa_psu(_item, params, data):
    for psu_type, psu_type_readable, levels_type in [("dc12v", "12 V", "levels_12v_"),
                                                     ("dc5v", "5 V", "levels_5v_"),
                                                     ("dc33v", "3.3 V", "levels_33v_")]:
        psu_voltage = float(data[psu_type]) / 100
        levels = params[levels_type + "upper"] + params[levels_type + "lower"]
        yield check_levels(psu_voltage, None, levels, unit='V', infoname=psu_type_readable)


check_info['hp_msa_psu.sensor'] = {
    'inventory_function': inventory_hp_msa_psu,
    'check_function': check_hp_msa_psu,
    'service_description': 'Power Supply Voltage %s',
    'default_levels_variable': "hp_msa_psu_default_levels",
    'group': 'hp_msa_psu_voltage',
    'includes': ["hp_msa.include"],
}

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

factory_settings["hp_msa_psu_temp_default_levels"] = {
    "levels": (40, 45),  # Just assumed
}


@get_parsed_item_data
def check_hp_msa_psu_temp(item, params, data):
    return check_temperature(float(data["dctemp"]), params, "hp_msa_psu_temp_%s" % item)


check_info['hp_msa_psu.temp'] = {
    'inventory_function': inventory_hp_msa_psu,
    'check_function': check_hp_msa_psu_temp,
    'service_description': 'Temperature Power Supply %s',
    'has_perfdata': True,
    'group': 'temperature',
    'default_levels_variable': 'hp_msa_psu_temp_default_levels',
    'includes': ["temperature.include", "hp_msa.include"],
}
