#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2018             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 parse_orion_system(info):
    map_charge_states = {
        '1': (0, 'float charging'),
        '2': (0, 'discharge'),
        '3': (0, 'equalize'),
        '4': (0, 'boost'),
        '5': (0, 'battery test'),
        '6': (0, 'recharge'),
        '7': (0, 'separate charge'),
        '8': (0, 'event control charge'),
    }

    system_voltage, load_current, battery_current, battery_temp,\
        charge_state, _battery_current_limit, rectifier_current,\
        system_power = info[0]

    parsed = {
        'charging': {
            'Battery': map_charge_states.get(charge_state, (3, "unknown[%s]" % charge_state))
        },
        'temperature': {},
        'electrical': {},
    }

    if battery_temp != "2147483647":
        # From MIB: The max. value 2147483647 is used to indicate an invalid value."
        parsed['temperature']['Battery'] = int(battery_temp) * 0.1

    for what, value, factor in [
        ('voltage', system_voltage, 0.01),
        ('current', load_current, 0.1),
        ('power', system_power, 0.1),
    ]:
        if value != "2147483647":
            # From MIB: The max. value 2147483647 is used to indicate an invalid value."
            system_data = parsed['electrical'].setdefault('System', {})
            system_data[what] = int(value) * factor

    for item, value in [
        ('Battery', battery_current),
        ('Rectifier', rectifier_current),
    ]:
        if value != "2147483647":
            # From MIB: The max. value 2147483647 is used to indicate an invalid value."
            item_data = parsed['electrical'].setdefault(item, {})
            item_data['current'] = int(battery_temp) * 0.1

    return parsed


def inventory_orion_system_temp(parsed):
    for entity in parsed["temperature"]:
        yield entity, {}


def check_orion_system_temp(item, params, parsed):
    if item in parsed["temperature"]:
        return check_temperature(parsed['temperature'][item], params, 'orion_system_temp.%s' % item)


check_info['orion_system'] = {
    'parse_function': parse_orion_system,
    'inventory_function': inventory_orion_system_temp,
    'check_function': check_orion_system_temp,
    'service_description': 'Temperature %s',
    'snmp_info': (
        '.1.3.6.1.4.1.20246.2.3.1.1.1.2.3',
        [
            '1',  # ORION-BASE-MIB::dcSystemVoltage
            '2',  # ORION-BASE-MIB::dcLoadCurrent
            '3',  # ORION-BASE-MIB::dcBatteryCurrent
            '4',  # ORION-BASE-MIB::dcBatteryTemperature
            '5',  # ORION-BASE-MIB::dcChargeState
            '6',  # ORION-BASE-MIB::dcCurrentLimit
            '7',  # ORION-BASE-MIB::dcRectifierCurrent
            '8',  # ORION-BASE-MIB::dcSystemPower
        ]),
    'snmp_scan_function': lambda oid: oid('.1.3.6.1.2.1.1.2.0').startswith('.1.3.6.1.4.1.20246'),
    'has_perfdata': True,
    'group': 'temperature',
    'includes': ['temperature.include'],
}


def inventory_orion_system_charging(parsed):
    for entity in parsed["charging"]:
        yield entity, {}


def check_orion_system_charging(item, params, parsed):
    if item in parsed["charging"]:
        state, state_readable = parsed["charging"][item]
        return state, "Status: %s" % state_readable


check_info['orion_system.charging'] = {
    'inventory_function': inventory_orion_system_charging,
    'check_function': check_orion_system_charging,
    'service_description': 'Charge %s',
}


def inventory_orion_system_electrical(parsed):
    return inventory_elphase(parsed["electrical"])


def check_orion_system_electrical(item, params, parsed):
    return check_elphase(item, params, parsed["electrical"])


check_info['orion_system.dc'] = {
    'inventory_function': inventory_orion_system_electrical,
    'check_function': check_orion_system_electrical,
    'service_description': 'Direct Current %s',
    'has_perfdata': True,
    'group': 'ups_outphase',
    'includes': ['elphase.include'],
}
