#!/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.

# Example output from agent:
# <<<cups_queues>>>
# printer lpr1 disabled since Wed Jun 16 14:21:14 2010 -
#     reason unknown
# printer lpr2 now printing lpr2-3.  enabled since Tue Jun 29 09:22:04 2010
#     Wiederherstellbar: Der Netzwerk-Host „lpr2“ ist beschäftigt, erneuter Versuch in 30 Sekunden …
# printer spr1 is idle.  enabled since Thu Mar 11 14:28:23 2010
# printer spr2 is idle.  enabled since Thu Mar 11 14:28:23 2010
# printer spr3 is idle.  enabled since Thu Mar 11 14:28:23 2010
# printer spr4 is idle.  enabled since Thu Mar 11 14:28:23 2010
# printer spr5 is idle.  enabled since Thu Mar 11 14:28:23 2010
# printer spr6 disabled since Mon Jun 21 10:29:39 2010 -
#     /usr/lib/cups/backend/lpd failed
# printer spr7 is idle.  enabled since Thu Mar 11 14:28:23 2010
# printer spr8 is idle.  enabled since Thu Mar 11 14:28:23 2010
# ---
# lpr2-2                  root              1024   Tue Jun 29 09:02:35 2010
# lpr2-3                  root              1024   Tue Jun 29 09:05:54 2010
# zam19-113565 Sebastian Hirschdobler 3561472 Fri Jul 31 12:58:01 2015


# Default thresholds
factory_settings['cups_queues_default_levels'] = {
        'job_count'         : (5, 10),      # warn/crit for queue entries
        'job_age'           : (360, 720),   # warn/crit for entry age in seconds
        'is_idle'           : 0,            # state for "is idle"
        'now_printing'      : 0,            # state for "now printing"
        'disabled_since'    : 2,            # state for "disbaled since"
}


def inventory_cups_queues(info):
    return [ ('%s' % line[1], {}) for line in info if line[0] == 'printer' ]


def check_cups_queues(item, params, info):
    if type(params) == tuple and len(params) == 4:
        params = {"job_count" : (params[0], params[1]),
                  "job_age"   : (params[2], params[3]),
        }

    warn_num, crit_num = params["job_count"]
    warn_age, crit_age = params["job_age"]
    state   = 3
    output  = "Queue not found"
    jobs    = 0
    now     = time.time()
    oldest  = now
    for num, line in enumerate(info):
        # Parse printer status
        if line[0] == 'printer' and line[1] == item:
            statusoutput = ' '.join(line[2:])
            status = ' '.join(line[2:4])

            # If the next line does not start with "printer" append it as additional output
            if len(info) > num+1 and not info[num+1][0] in [ 'printer', '---' ]:
                statusoutput += " (%s)" % " ".join(info[num+1])

            if status in [ "is idle.", "now printing", "disabled since" ]:
                state = params[status.replace(" ", "_").replace(".", "")]
                output = "%s" % statusoutput
            else:
                state = 3
                output = "Undefined status output in \"lpr -p\""

        # Parse job
        elif line[0].startswith(item+'-'):
            # This is a queue item count the number of items and check the max age
            jobs += 1

            # Try to parse different time formats. TODO: Couldn't we run the lpr command
            # with some environment var to fixate the date format?
            try:
                # Tue Jun 29 09:05:54 2010
                timestring = ' '.join(line[-5:])
                parsed = time.strptime(timestring, '%a %b %d %H:%M:%S %Y')
            except:
                # Thu 29 Aug 2013 12:41:42 AM CEST
                timestring = ' '.join(line[-7:])
                parsed = time.strptime(timestring, '%a %d %b %Y %H:%M:%S %p %Z')

            dt = time.mktime(parsed)
            if dt < oldest:
                oldest = dt

    jobOutput = 'Jobs: %d' % jobs
    if jobs > 0:
        if oldest < now - crit_age or jobs > crit_num:
            state = 2
        elif oldest < now - warn_age or jobs > warn_num:
            state = 1
        jobOutput += ', Oldest job is from %s' % time.strftime("%c", time.localtime(oldest))

    return (state, output + " - " + jobOutput, [ ("jobs", jobs, warn_num, crit_num, 0) ])


check_info["cups_queues"] = {
    'inventory_function'        : inventory_cups_queues,
    'check_function'            : check_cups_queues,
    'service_description'       : 'CUPS Queue %s',
    'has_perfdata'              : True,
    'default_levels_variable'   : 'cups_queues_default_levels',
    'group'                     : 'cups_queues'
}
