#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             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.


factory_settings['ups_modulys_battery_default_levels'] = {
    "capacity": (95, 90),
    "battime": (0, 0)
}


def inventory_ups_modulys_battery(info):
    return [ (None, {}) ]


def check_ups_modulys_battery(item, params, info):
    warn_left, crit_left = params["battime"]
    warn_perc, crit_perc = params["capacity"]
    for condition, elapsed_sec, remaining_min, capacity, temperature in info:
        condition, elapsed_sec, capacity = map(lambda x: int(x), (condition, elapsed_sec, capacity))
        if elapsed_sec == 0:
            # not on battery so the it will not run out
            remaining_min = sys.maxint
        elif not remaining_min:
            # The "remaining_min" value isn't always reported and we don't know why.
            # One theory is that it will be reported if on battery but we have no data to verify.
            #
            # If the theory holds true, this branch is never taken. If it doesn't then its likely
            # the information is only available on some variants of the device or in some
            # configurations. We can still report useful data based on "capacity"
            remaining_min = sys.maxint
        else:
            remaining_min = int(remaining_min)

        # test 1: on mains or on battery?
        if elapsed_sec == 0:
            yield 0, "on mains"
        else:
            # we already have configurable levels on remaining capacity which
            # would be superfluous if we reported the discharging state itself
            # as a problem. Thus this reports "OK"
            yield 0, "discharging for %d minutes" % (elapsed_sec / 60)

        # test 2: battery health
        if condition == 1:
            yield 1, "battery health weak"
        elif condition == 2:
            yield 2, "battery needs to be replaced"

        # test 3: remaining capacity
        if remaining_min < crit_left or capacity < crit_left:
            status = 2
        elif remaining_min < warn_left or capacity < warn_perc:
            status = 1
        else:
            status = 0

        infotext = ""

        if remaining_min < warn_left:
            infotext = "%d minutes remaining (warn/crit at %d/%d min)" %\
                (remaining_min, warn_left, crit_left)

        if capacity < warn_perc:
            infotext = "%d percent charged (warn/crit at %d/%d perc)" %\
                (capacity, warn_perc, crit_perc)

        yield status, infotext


check_info["ups_modulys_battery"] = {
    "inventory_function"        : inventory_ups_modulys_battery,
    "check_function"            : check_ups_modulys_battery,
    "service_description"       : "Battery Charge",
    "default_levels_variable"   : "ups_modulys_battery_default_levels",
    "group"                     : "ups_capacity",
    "snmp_info"                 : (".1.3.6.1.4.1.2254.2.4.7", [
                                        "1", # dupsBatteryCondition
                                        "4", # dupsSecondsOnBattery
                                        "5", # dupsBatteryEstimatedTime
                                        "8", # dupsBatteryCapacity
                                        "9", # dupsBatteryTemperature
                                  ]),
    "snmp_scan_function"        : lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.2254.2.4",
}


def inventory_ups_modulys_battery_temp(info):
    for condition, elapsed_sec, remaining_min, capacity, temperature in info:
        if temperature:
            yield "Battery", {}


def check_ups_modulys_battery_temp(item, params, info):
    for condition, elapsed_sec, remaining_min, capacity, temperature in info:
        return check_temperature(int(temperature), params, "ups_modulys_battery_temp_%s" % item)


check_info["ups_modulys_battery.temp"] = {
    "inventory_function"      : inventory_ups_modulys_battery_temp,
    "check_function"          : check_ups_modulys_battery_temp,
    "has_perfdata"            : True,
    "service_description"     : "Temperature %s",
    "group"                   : "temperature",
    "default_levels_variable" : "ups_modulys_battery_temp_default_levels",
}
