#!/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.12148.9.3.1.0 --> ELTEK-DISTRIBUTED-MIB::batteryName.0
# .1.3.6.1.4.1.12148.9.3.2.0 5485 --> ELTEK-DISTRIBUTED-MIB::batteryVoltage.0
# .1.3.6.1.4.1.12148.9.3.3.0 0 --> ELTEK-DISTRIBUTED-MIB::batteryCurrent.0
# .1.3.6.1.4.1.12148.9.3.4.0 19 --> ELTEK-DISTRIBUTED-MIB::batteryTemp.0
# .1.3.6.1.4.1.12148.9.3.5.0 0 --> ELTEK-DISTRIBUTED-MIB::batteryBreakerStatus.0


#   .--breaker status------------------------------------------------------.
#   |   _                    _                   _        _                |
#   |  | |__  _ __ ___  __ _| | _____ _ __   ___| |_ __ _| |_ _   _ ___    |
#   |  | '_ \| '__/ _ \/ _` | |/ / _ \ '__| / __| __/ _` | __| | | / __|   |
#   |  | |_) | | |  __/ (_| |   <  __/ |    \__ \ || (_| | |_| |_| \__ \   |
#   |  |_.__/|_|  \___|\__,_|_|\_\___|_|    |___/\__\__,_|\__|\__,_|___/   |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |                             main check                               |
#   '----------------------------------------------------------------------'


def parse_eltek_battery(info):
    voltage, current, temp, breaker_status = info[0]
    return {
        'supply' : {
            "Supply" : {
                'voltage' : float(voltage) / 100,
                'current' : float(current),
        }},
        'temp'    : float(temp),
        'breaker' : breaker_status,
    }


def inventory_eltek_battery(parsed):
    if "breaker" in parsed:
        return [ (None, None) ]


def check_eltek_battery(_no_item, _no_params, parsed):
    if "breaker" in parsed:
        map_status = {
            "0" : (0, "normal"),
            "1" : (2, "alarm"),
        }
        state, state_readable = map_status[parsed["breaker"]]
        return state, "Status: %s" % state_readable


check_info['eltek_battery'] = {
    'parse_function'            : parse_eltek_battery,
    'inventory_function'        : inventory_eltek_battery,
    'check_function'            : check_eltek_battery,
    'service_description'       : 'Battery Breaker Status',
    'snmp_info'                 : (".1.3.6.1.4.1.12148.9.3", [
                                        "2",    # batteryVoltage
                                        "3",    # batteryCurrent
                                        "4",    # batteryTemp
                                        "5",    # batteryBreakerStatus
                                  ]),
    'snmp_scan_function'        : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.12148.9"),
    'includes'                  : [ 'elphase.include', 'temperature.include' ],
}


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


# suggested by customer
factory_settings['eltek_battery_temp_default_variables'] = {
    'levels' : (27.0, 35.0),
}


def inventory_eltek_battery_temp(parsed):
    if 'temp' in parsed:
        return [ ("Battery", {}) ]


def check_eltek_battery_temp(item, params, parsed):
    # For temp checks we need an item but we have only one
    if 'temp' in parsed:
        return check_temperature(parsed['temp'], params, "eltek_battery_temp_Battery")


check_info['eltek_battery.temp'] = {
    'inventory_function'        : inventory_eltek_battery_temp,
    'check_function'            : check_eltek_battery_temp,
    'service_description'       : 'Temperature %s',
    'has_perfdata'              : True,
    'group'                     : 'temperature',
    'default_levels_variable'   : 'eltek_battery_temp_default_variables',
}


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


# suggested by customer
factory_settings['eltek_battery_phase_default_variables'] = {
    'voltage' : (52, 48),
    'current' : (50, 76),
}


check_info['eltek_battery.supply'] = {
    'inventory_function'        : lambda parsed: inventory_elphase(parsed["supply"]),
    'check_function'            : lambda item, params, parsed: check_elphase(item, params, parsed["supply"]),
    'service_description'       : 'Battery %s',
    'has_perfdata'              : True,
    'group'                     : 'el_inphase',
    'default_levels_variable'   : 'eltek_battery_temp_phase_variables',
}
