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

# +------------------------------------------------------------------+
# | This file has been contributed by:                               |
# |                                                                  |
# | Peter Lauk <lauk@stuttgart-airport.com>           Copyright 2010 |
# +------------------------------------------------------------------+

# In some cases the name of the part is not uniq. e.g. for c5900
# devices. In this cases add the color from the dedicated OID to
# the item name.
#
# Example output for this case:
#
#['Toner Cartridge OKI DATA CORP', '100', '30', 'black']
#['Toner Cartridge OKI DATA CORP', '100', '10', 'cyan']
#['Toner Cartridge OKI DATA CORP', '100', '10', 'magenta']
#['Toner Cartridge OKI DATA CORP', '100', '40', 'yellow']
#['Image Drum Unit OKI DATA CORP', '20000', '-409', '']
#['Image Drum Unit OKI DATA CORP', '20000', '7969', '']
#['Image Drum Unit OKI DATA CORP', '20000', '11597', '']
#['Image Drum Unit OKI DATA CORP', '20000', '4621', '']
#['Belt Unit OKI DATA CORP', '60000', '47371', '']
#['Fuser Unit OKI DATA CORP', '60000', '26174', '']
#['Waste Toner box OKI DATA CORP', '1', '-2', '']

printer_supply_default_levels = (20, 10)

# When the printer reports -3 as fill threshold the toner
# might be empty or might have some small remaining capacities
# the exact amount is unknown. This makes the nagios state reported
# in this state configurable
printer_supply_some_remaining_status = 1

# Workaround for toners and drum units in c5900 devices
# which have equal names for the single parts.
# Add the color description to that item
def printer_supply_fix_infos(info):
    colors = []
    for index, line in enumerate(info):
        if line[0].startswith('Toner Cartridge') \
           or line[0].startswith('Image Drum Unit'):
            if line[3]:
                colors += [ line[3] ]
                color = line[3]
            elif line[3] == '':
                color = colors[index - len(colors)]
            info[index][0] = '%s %s' % (color.title(), line[0])
    return info

def inventory_printer_supply(checkname, info):
    return [ (line[0], "printer_supply_default_levels") for line in printer_supply_fix_infos(info) ]

def check_printer_supply(item, params, info):
    for line in printer_supply_fix_infos(info):
        if line[0] == item:
            maxlevel = float(line[1])
            current = savefloat(line[2])
            leftperc = 100.0 * current / maxlevel
            warn, crit = params # in percent
            infotext = "%.0f%% (levels at %.0f%% / %.0f%%)" % (leftperc, warn, crit)
            perfdata = [ ("pages", current, warn / 100.0 * maxlevel, crit / 100.0 * maxlevel, 0, maxlevel ) ]
            if current == -1 or maxlevel == -1:
                return (0, "OK - there are no restrictions on this supply")
            elif current == -2 or maxlevel == -2:
                return (3, "UNKNOWN - Couldn't get info about supply")
            elif current == -3:
                return (printer_supply_some_remaining_status, "%s - Some remaining" % 
                        (nagios_state_names[printer_supply_some_remaining_status]), perfdata)
            elif leftperc <= crit:
                return (2, "CRIT - %s" % infotext, perfdata)
            elif leftperc <= warn:
                return (1, "WARNING - %s" % infotext, perfdata)
            else:
                return (0, "OK - %s" % infotext, perfdata)

    return (3, 'UNKNOWN - not found')

check_info['printer_supply'] = (check_printer_supply, "Supply %s", 1, inventory_printer_supply)

snmp_info['printer_supply'] = ( ".1.3.6.1.2.1.43", [ '11.1.1.6',
                                                     '11.1.1.8',
                                                     '11.1.1.9',
                                                     '12.1.1.4' ] )

check_config_variables.append("printer_supply_some_remaining_status")

snmp_scan_functions['printer_supply'] = \
    lambda oid: oid(".1.3.6.1.2.1.43.11.1.1.6.1.1") != None
