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

def parse_ups_cps_battery(info):
    parsed = {}

    if info[0][0]:
        parsed["capacity"] = int(info[0][0])

    # The MIB explicitly declares this to be Celsius
    if info[0][1]:
        parsed["temperature"] = int(info[0][1])

    # A TimeTick is 1/100 s
    if info[0][2]:
        parsed["battime"] = float(info[0][2]) / 100
    return parsed


#.
#   .--Temperature---------------------------------------------------------.
#   |     _____                                   _                        |
#   |    |_   _|__ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |      | |/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |      | |  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      |_|\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

def inventory_ups_cps_battery_temp(parsed):
    if "temperature" in parsed:
        return [ ("Battery", {}) ]


def check_ups_cps_battery_temp(item, params, parsed):
    return check_temperature(parsed["temperature"], params, "ups_cps_battery_temp")


check_info["ups_cps_battery.temp"] = {
    "inventory_function"    : inventory_ups_cps_battery_temp,
    "check_function"        : check_ups_cps_battery_temp,
    "service_description"   : "Temperature %s",
    "group"                 : "temperature",
    "includes"              : [ "temperature.include" ],
}


#.
#   .--Capacity------------------------------------------------------------.
#   |                ____                       _ _                        |
#   |               / ___|__ _ _ __   __ _  ___(_) |_ _   _                |
#   |              | |   / _` | '_ \ / _` |/ __| | __| | | |               |
#   |              | |__| (_| | |_) | (_| | (__| | |_| |_| |               |
#   |               \____\__,_| .__/ \__,_|\___|_|\__|\__, |               |
#   |                         |_|                     |___/                |
#   '----------------------------------------------------------------------'

factory_settings["ups_cps_battery"] = {
    "capacity"  : ( 95, 90 ),
}


def inventory_ups_cps_battery(parsed):
    if "capacity" in parsed:
        return [ (None, {}) ]


def check_ups_cps_battery(item, params, parsed):

    def check_lower_levels(value, levels):
        if not levels:
            return 0
        else:
            warn, crit = levels
            if value < crit:
                return 2
            elif value < warn:
                return 1
            else:
                return 0

    capacity = parsed["capacity"]
    capacity_params = params["capacity"]
    capacity_status = check_lower_levels(capacity, capacity_params)
    if capacity_status:
        levelstext = " (warn/crit at %d/%d%%)" % capacity_params
    else:
        levelstext = ""
    yield capacity_status, ("Capacity at %d%%" % capacity) + levelstext

    battime = parsed["battime"]
    # WATO rule stores remaining time in minutes
    battime_params = params.get("battime")
    battime_status = check_lower_levels(battime/60, battime_params)
    if battime_status:
        levelstext = " (warn/crit at %d/%d min)" % battime_params
    else:
        levelstext = ""
    yield battime_status, ("%.0f minutes remaining on battery" % (battime/60)) + levelstext


check_info["ups_cps_battery"] = {
    "parse_function"            : parse_ups_cps_battery,
    "default_levels_variable"   : "ups_cps_battery",
    "inventory_function"        : inventory_ups_cps_battery,
    "check_function"            : check_ups_cps_battery,
    "service_description"       : "UPS Battery",
    "snmp_scan_function"        : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.3808.1.1.1"),
    "snmp_info"                 : (".1.3.6.1.4.1.3808.1.1.1.2.2", [
                                        "1",    # upsAdvanceBatteryCapacity
                                        "3",    # upsAdvanceBatteryTemperature
                                        "4",    # upsAdvanceBatteryRunTimeRemaining
                                  ]),
    "group"                     : "ups_capacity",
}
