#!/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.705.2.3.2.1.2.1 3997 --> MG-SNMP-STS-MIB::stsmgSource1PhasePhaseVoltage.1
# .1.3.6.1.4.1.705.2.3.2.1.2.2 4017 --> MG-SNMP-STS-MIB::stsmgSource1PhasePhaseVoltage.2
# .1.3.6.1.4.1.705.2.3.2.1.2.3 4012 --> MG-SNMP-STS-MIB::stsmgSource1PhasePhaseVoltage.3
# .1.3.6.1.4.1.705.2.3.2.1.3.1 0 --> MG-SNMP-STS-MIB::stsmgSource1Current.1
# .1.3.6.1.4.1.705.2.3.2.1.3.2 0 --> MG-SNMP-STS-MIB::stsmgSource1Current.2
# .1.3.6.1.4.1.705.2.3.2.1.3.3 0 --> MG-SNMP-STS-MIB::stsmgSource1Current.3
# .1.3.6.1.4.1.705.2.3.2.1.4.1 0 --> MG-SNMP-STS-MIB::stsmgSource1ActivePower.1
# .1.3.6.1.4.1.705.2.3.2.1.4.2 0 --> MG-SNMP-STS-MIB::stsmgSource1ActivePower.2
# .1.3.6.1.4.1.705.2.3.2.1.4.3 0 --> MG-SNMP-STS-MIB::stsmgSource1ActivePower.3
# .1.3.6.1.4.1.705.2.3.16.0 499 --> MG-SNMP-STS-MIB::stsmgSource1Frequency.0

#
#
# .1.3.6.1.4.1.705.2.4.2.1.2.1 3946 --> MG-SNMP-STS-MIB::stsmgSource2PhasePhaseVoltage.1
# .1.3.6.1.4.1.705.2.4.2.1.2.2 3970 --> MG-SNMP-STS-MIB::stsmgSource2PhasePhaseVoltage.2
# .1.3.6.1.4.1.705.2.4.2.1.2.3 3955 --> MG-SNMP-STS-MIB::stsmgSource2PhasePhaseVoltage.3
# .1.3.6.1.4.1.705.2.4.2.1.3.1 170 --> MG-SNMP-STS-MIB::stsmgSource2Current.1
# .1.3.6.1.4.1.705.2.4.2.1.3.2 155 --> MG-SNMP-STS-MIB::stsmgSource2Current.2
# .1.3.6.1.4.1.705.2.4.2.1.3.3 146 --> MG-SNMP-STS-MIB::stsmgSource2Current.3
# .1.3.6.1.4.1.705.2.4.2.1.4.1 3700 --> MG-SNMP-STS-MIB::stsmgSource2ActivePower.1
# .1.3.6.1.4.1.705.2.4.2.1.4.2 3500 --> MG-SNMP-STS-MIB::stsmgSource2ActivePower.2
# .1.3.6.1.4.1.705.2.4.2.1.4.3 3300 --> MG-SNMP-STS-MIB::stsmgSource2ActivePower.3
# .1.3.6.1.4.1.705.2.4.16.0 499 --> MG-SNMP-STS-MIB::stsmgSource2Frequency.0

factory_settings['apc_sts_inputs_default_levels'] = {
}

def parse_apc_sts_inputs(info):
    parsed = {}
    phase = 1
    for voltage, current, power in info:
        source = int(voltage.split('.')[0]) - 2
        parsed["Source %d Phase %d" % ( source, phase) ] = {
                "voltage"   : int(voltage.split('.')[1]) / 10.0,
                "current"   : int(current) / 10.0,
                "power"     : int(power),
        }
        phase += 1
        if phase == 4:
            phase = 1
    return parsed

check_info["apc_sts_inputs"] = {
    'parse_function'            : parse_apc_sts_inputs,
    'inventory_function'        : inventory_elphase,
    'check_function'            : check_elphase,
    'service_description'       : 'Input %s',
    'default_levels_variable'   : 'apc_sts_inputs_default_levels',
    'includes'                  : [ "elphase.include", ],
    'group'                     : 'el_inphase',
    "has_perfdata"              : True,
    "snmp_scan_function"        : lambda oid: ".1.3.6.1.4.1.705.2.2" in oid(".1.3.6.1.2.1.1.2.0"),
    "snmp_info"                 : ( ".1.3.6.1.4.1.705.2", [ 3, 4 ] , [
                                                                       "2.1.2", # Phase Voltage
                                                                       "2.1.3", # Current
                                                                       "2.1.4", # Active Power
                                                                       ] ),
}

