#!/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 parse_cups_queues(info):
    parsed = {}

    for num, line in enumerate(info):
        if line[0] == "printer":
            parsed[line[1]] = {
              'status_readable' : ' '.join(line[2:4]).replace(' ', '_').strip('.'),
              'output'          : ' '.join(line[2:]),
              'jobs'            : [],
            }
            if len(info) > num+1 and \
              not info[num+1][0] in [ 'printer', '---' ]:
                parsed[line[1]]['output'] += " (%s)" % \
                    " ".join(info[num+1])
        elif line[0] == "---":
            break

    queue_section = False
    for line in info:
        if line[0] == '---':
            queue_section = True
            continue

        item = line[0].split("-", 1)[0]
        if item in parsed and queue_section:
            # Handle different time formats...
            try: # Tue Jun 29 09:05:54 2010
                job_time = time.mktime(time.strptime(' '.join(line[-5:]), \
                                '%a %b %d %H:%M:%S %Y'))
            except: # Thu 29 Aug 2013 12:41:42 AM CEST
                job_time = time.mktime(time.strptime(' '.join(line[-7:]), \
                                '%a %d %b %Y %H:%M:%S %p %Z'))
            parsed[item]['jobs'].append(job_time)

    return parsed

def inventory_cups_queues(parsed):
    for item in parsed:
        yield item, {}

def check_cups_queues(item, params, parsed):
    if item in parsed:
        data = parsed[item]
        if type(params) == tuple and len(params) == 4:
            params = {"job_count"         : (params[0], params[1]),
                      "job_age"           : (params[2], params[3]),
                      'is_idle'           : 0,
                      'now_printing'      : 0,
                      'disabled_since'    : 2,
            }

        if data["status_readable"] in params:
            state = params[data["status_readable"]]
            yield state, data["output"]
        else:
            yield 3, "Undefinded status output in \"lpr -p\""

        now = time.time()
        jobs_count = len(data["jobs"])
        if jobs_count > 0:
            warn_num, crit_num = params["job_count"]
            yield 0, "Jobs: %d" % jobs_count, \
                     [ ("jobs", jobs_count, warn_num, crit_num, 0) ]

            warn_age, crit_age = params["job_age"]
            now = time.time()
            oldest = min(data["jobs"])
            oldest_readable = time.strftime("%c", time.localtime(oldest))

            if oldest < now - crit_age or jobs_count > crit_num:
                state = 2
            elif oldest < now - warn_age or jobs_count > warn_num:
                state = 1
            else:
                state = 0

            if state:
                yield state, "Oldest job is from %s" % oldest_readable
    else:
        yield 3, "Queue not found"


check_info["cups_queues"] = {
    'parse_function'            : parse_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'
}
