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

apc_ats_output_default_levels = {"output_voltage_max": (240, 250), "load_perc_max": (85, 95)}


def inventory_apc_ats_output(info):
    return [(x[0], "apc_ats_output_default_levels") for x in info]


def check_apc_ats_output(item, params, info):
    for line in info:
        if line[0] == item:
            vac, watt, ampere, load_perc = map(int, line[1:])
            ampere = ampere / 10
            state = 0
            messages = []
            perf = []

            # Output Voltage Max
            warn, crit = params.get('output_voltage_max', (None, None))
            perf.append(('volt', vac, warn, crit))
            base_msg = "%d Volt" % vac
            if warn and crit:
                levels = " its to high (Warning/Critical %d/%d)" % (warn, crit)
            else:
                levels = ""
            if crit and vac > crit:
                state = 2
                base_msg += levels + "(!!)"
            elif warn and vac > warn:
                state = 1
                base_msg += levels + "(!)"

            # Output Voltage Min
            warn, crit = params.get('output_voltage_min', (None, None))
            if warn and crit:
                levels = " its to low (warn/crit %d/%d)" % (warn, crit)
            else:
                levels = ""
            if crit and vac < crit:
                state = 2
                base_msg += levels + "(!!)"
            elif warn and vac < warn:
                state = max(state, 1)
                base_msg += levels + "(!)"

            messages.append(base_msg)

            # Watt
            perf.append(('watt', watt))
            messages.append("%d Watt" % watt)

            # Ampere and Percent Load
            perf.append(("ampere", ampere))
            messages.append("%d Ampere" % ampere)

            warn, crit = params.get('load_perc_max', (None, None))
            if warn and crit:
                levels = " its to high (warn/crit %d/%d) " % (warn, crit)
            else:
                levels = ""
            base_msg = "Load: %d%%" % load_perc
            if crit and load_perc > crit:
                state = 2
                base_msg += " " + levels + "(!!)"
            elif warn and load_perc > warn:
                state = max(state, 1)
                base_msg += " " + levels + "(!)"
            warn, crit = params.get('load_perc_min', (None, None))
            if warn and crit:
                levels = " its to low (warn/crit %d/%d) " % (warn, crit)
            else:
                levels = ""
            if crit and load_perc < crit:
                state = 2
                base_msg += " " + levels + "(!!)"
            elif warn and load_perc < warn:
                state = max(state, 1)
                base_msg += " " + levels + "(!)"
            perf.append(("load_perc", load_perc, warn, crit))
            messages.append(base_msg)

            return state, "Running with: " + ", ".join(messages), perf
    return 3, "Power phase not found"


check_info["apc_ats_output"] = {
    "check_function": check_apc_ats_output,
    "group": "apc_ats_output",
    "inventory_function": inventory_apc_ats_output,
    "service_description": "Phase %s output",
    "has_perfdata": True,
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") in [
        ".1.3.6.1.4.1.318.1.3.11",
        ".1.3.6.1.4.1.318.1.3.32",
    ],
    "snmp_info": (
        ".1.3.6.1.4.1.318.1.1.8.5.4.3.1",
        [
            "1",  # atsOutputPhaseTableIndex
            #"2", # atsOutputPhaseIndex 1-3 = Phase 1-3, 4 = Neutral
            "3",  # atsOutputVoltage (VAC)
            "13",  # atsOutputPower (Watt)
            "4",  # atsOutputCurrent (0.1 AMPERE)
            "10",  # atsOutputPercentLoad
        ]),
}
