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

# .1.3.6.1.4.1.318.1.1.12.1.1.0  "sf9pdu1" --> PowerNet-MIB::rPDUIdentName.0
# .1.3.6.1.4.1.318.1.1.12.1.9.0 1 --> PowerNet-MIB::rPDUIdentDeviceNumPhases.0
# .1.3.6.1.4.1.318.1.1.12.1.16.0 0 --> PowerNet-MIB::rPDUIdentDevicePowerWatts.0
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 160 --> PowerNet-MIB::rPDULoadStatusLoad.1 (measured in tenths of Amps)
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.3.1 1 --> PowerNet-MIB::rPDULoadStatusLoadState.1

# .1.3.6.1.4.1.318.1.1.12.1.1.0 FOOBAR --> PowerNet-MIB::rPDUIdentName.0
# .1.3.6.1.4.1.318.1.1.12.1.9.0 1 --> PowerNet-MIB::rPDUIdentDeviceNumPhases.0
# .1.3.6.1.4.1.318.1.1.12.1.16.0 1587 --> PowerNet-MIB::rPDUIdentDevicePowerWatts.0
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1 0 --> PowerNet-MIB::rPDULoadStatusLoad.1
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.2 0 --> PowerNet-MIB::rPDULoadStatusLoad.2
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.3 0 --> PowerNet-MIB::rPDULoadStatusLoad.3
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.3.1 1 --> PowerNet-MIB::rPDULoadStatusLoadStates.1
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.3.2 1 --> PowerNet-MIB::rPDULoadStatusLoadStates.2
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.3.3 1 --> PowerNet-MIB::rPDULoadStatusLoadStates.3
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.4.1 1 --> PowerNet-MIB::rPDULoadStatusPhaseNumber.1
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.4.2 0 --> PowerNet-MIB::rPDULoadStatusPhaseNumber.2
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.4.3 0 --> PowerNet-MIB::rPDULoadStatusPhaseNumber.3
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.5.1 0 --> PowerNet-MIB::rPDULoadStatusBankNumber.1
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.5.2 1 --> PowerNet-MIB::rPDULoadStatusBankNumber.2
# .1.3.6.1.4.1.318.1.1.12.2.3.1.1.5.3 2 --> PowerNet-MIB::rPDULoadStatusBankNumber.3

# examples num phase/banks: 1/0,    => parsed = device phase
#                           1/2,    => parsed = device phase + 2 banks
#                           3/0     => parsed = device phase + 3 phases

_APC_RACKPDU_POWER_STATE_MAP = {
    "1": (0, "load normal"),
    "2": (2, "load low"),
    "3": (1, "load near over load"),
    "4": (2, "load over load"),
}


def get_status_info(amperage_str, device_state):
    return float(amperage_str) / 10, _APC_RACKPDU_POWER_STATE_MAP[device_state]


def parse_apc_rackpdu_power(info):

    parsed = {}
    device_info, n_phases, phase_bank_info = info
    pdu_ident_name, power_str = device_info[0]
    device_name = "Device %s" % pdu_ident_name

    parsed.setdefault(device_name, {"power": float(power_str)})

    if n_phases[0][0] == '1':
        parsed[device_name]["current"] = get_status_info(*phase_bank_info[0][:2])
        phase_bank_info = phase_bank_info[1:]

    for amperage_str, device_state, phase_num, bank_num in phase_bank_info:
        if bank_num != '0':
            name_part = "Bank"
            num = bank_num
        elif phase_num != '0':
            name_part = "Phase"
            num = phase_num
        else:
            continue
        parsed.setdefault("%s %s" % (name_part, num),
                          {"current": get_status_info(amperage_str, device_state)})

    return parsed


check_info["apc_rackpdu_power"] = {
    'parse_function': parse_apc_rackpdu_power,
    'inventory_function': inventory_elphase,
    'check_function': check_elphase,
    'service_description': 'PDU %s',
    'has_perfdata': True,
    'snmp_info': [('.1.3.6.1.4.1.318.1.1.12.1', [
        '1',  # PowerNet-MIB::rPDUIdentName
        '16',  # PowerNet-MIB::rPDUIdentDevicePowerWatts
    ]),
                  ('.1.3.6.1.4.1.318.1.1.12.2.1', [
                      '2',  # PowerNet-MIB::rPDULoadDevNumPhases
                  ]),
                  ('.1.3.6.1.4.1.318.1.1.12.2.3.1.1', [
                      '2',  # PowerNet-MIB::rPDULoadStatusLoad
                      '3',  # PowerNet-MIB::rPDULoadStatusLoadState
                      '4',  # PowerNet-MIB::rPDULoadStatusPhaseNumber
                      '5'  # PowerNet-MIB::rPDULoadStatusBankNumber
                  ])],
    'snmp_scan_function': lambda oid: oid(".1.3.6.1.2.1.1.1.0").lower().startswith("apc web/snmp") \
                                      and oid(".1.3.6.1.4.1.318.1.1.12.1.*"),
    'group': 'el_inphase',
    'includes': ['elphase.include'],
}
