#!/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.367.3.2.1.2.24.1.1.2.1 Black Toner
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.2.2 Cyan Toner
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.2.3 Magenta Toner
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.2.4 Yellow Toner
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.1 30
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.2 20
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.3 30
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.4 -100

# some data may look like
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.2.1 Toner
# .1.3.6.1.4.1.367.3.2.1.2.24.1.1.5.1 30


factory_settings["printer_supply_ricoh_default_levels"] = {
    "levels" : ( 20.0, 10.0 )
}


def parse_printer_supply_ricoh(info):
    parsed = {}
    for what, pages_text in info:
        name_reversed = what.split(" ")

        if len(name_reversed) == 2:
            name_reversed.reverse()

        name = " ".join(name_reversed)
        parsed[name] = int(pages_text)
    return parsed


def inventory_printer_supply_ricoh(parsed):
    return [ (key, {}) for key in parsed ]


def check_printer_supply_ricoh(item, params, parsed):
    def handle_regular(supply_level):
        infotext = "%.0f%%" % supply_level

        if supply_level <= crit:
            state = 2
        elif supply_level <= warn:
            state = 1
        else:
            state = 0

        if state > 0:
            infotext += " (warn/crit at %.0f%%/%.0f%%)" % (warn, crit)
        return state, infotext, supply_level

    def handle_negative(code):
        # the following codes are based on the MP C2800
        if code == -100:
            # does not apply level. Since we don't get a proper reading
            # the best we could do would be test against 0
            return 2, "almost empty (<10%)", 0
        elif code == -2:
            # cartridge removed?
            return 3, "unknown level", 0
        elif code == -3:
            # -3 = full is based on user claim, but other walks also show
            # the device itself does not alert in this state
            return 0, "100%", 100
        else:
            # unknown code
            return handle_regular(0)

    if type(params) == tuple:
        params = { "levels" : params }

    warn, crit = params["levels"]

    for name, supply_level in parsed.items():
        if item == name:
            if supply_level < 0:
                # negative levels usually have special meaning
                state, infotext, supply_level = handle_negative(supply_level)
            else:
                state, infotext, supply_level = handle_regular(supply_level)

            if "black" in name.lower():
                perf_type = "black"
            elif "cyan" in name.lower():
                perf_type = "cyan"
            elif "magenta" in name.lower():
                perf_type = "magenta"
            elif "yellow" in name.lower():
                perf_type = "yellow"
            else:
                perf_type = "other"

            perfdata = [ ("supply_toner_" + perf_type, supply_level, warn, crit, 0, 100) ]

            return state, infotext, perfdata


check_info['printer_supply_ricoh'] = {
    "parse_function"          : parse_printer_supply_ricoh,
    "inventory_function"      : inventory_printer_supply_ricoh,
    "check_function"          : check_printer_supply_ricoh,
    "service_description"     : "Supply %s",
    "has_perfdata"            : True,
    "group"                   : "printer_supply",
    "snmp_info"               : ( ".1.3.6.1.4.1.367.3.2.1.2.24.1.1", [ 2, 5 ] ),
    "snmp_scan_function"      : lambda oid: oid(".1.3.6.1.2.1.1.2.0") in [ ".1.3.6.1.4.1.367.1.1" ],
    "default_levels_variable" : "printer_supply_ricoh_default_levels",
}
