#!/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-
# ails.  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 inventory_apc_ats_status(info):
    if len(info) ==  1:
        return [ (None, saveint(info[0][1]) ) ]
    return []

def check_apc_ats_status(_no_item, source, info):
    comstatus, selected_source, redundancy, overcurrent, ps5, ps24 = map(saveint, info[0])
    state = 0
    messages = []

    #current source of power
    sources = { 1 : "A", 2 : "B" }
    if source != selected_source:
        state = 2
        messages.append("Power source Changed from %s to %s(!!)" % \
        (sources[source], sources[selected_source]))
    else:
        messages.append("Power source %s selected" % sources[source])

    #current communication status of the Automatic Transfer Switch.
    if comstatus == 1:
        state = max(1,state)
        messages.append("Communication Status: never Discovered(!)")
    elif comstatus == 3:
        state = 2
        messages.append("Communication Status: lost(!!)")

    # current redundancy state of the ATS.
    # Lost(1) indicates that the ATS is unable to switch over to the alternate power source
    # if the current source fails. Redundant(2) indicates that the ATS will switch
    # over to the alternate power source if the current source fails.
    if redundancy != 2:
        state = 2
        messages.append("redundancy lost(!!)")
    else:
        messages.append("Device fully redundant")

    # current state of the ATS. atsOverCurrent(1) indicates that the ATS has i
    # exceeded the output current threshold and will not allow a switch
    # over to the alternate power source if the current source fails.
    # atsCurrentOK(2) indicates that the output current is below the output current threshold.
    if overcurrent == 1:
        state = 2
        messages.append("exceedet ouput current threshold(!!)")

    # 5Volt power supply
    if ps5 != 2:
        state = 2
        messages.append("5V power supply failed(!!)")

    # 24Volt power supply
    if ps24 != 2:
        state = 2
        messages.append("24V power suppy failed(!!)")

    return state, ", ".join(messages)

check_info["apc_ats_status"] = {
    "check_function"        : check_apc_ats_status,
    "inventory_function"    : inventory_apc_ats_status,
    "service_description"   : "ATS Status",
    "has_perfdata"          : False,
    "snmp_scan_function"    : lambda oid: ".1.3.6.1.4.1.318.1.3.11" in oid(".1.3.6.1.2.1.1.2.0"),
    "snmp_info"             : ( ".1.3.6.1.4.1.318.1.1.8.5.1", [
                                                          "1.0", # atsStatusCommStatus
                                                          "2.0", # atsStatusSelectedSource
                                                          "3.0", # atsStatusRedundancyState
                                                          "4.0", # atsStatusOverCurrentState
                                                          "5.0", # atsStatus5VPowerSupply
                                                          "6.0", # atsStatus24VPowerSupply
                                                        ] ),
}

