diff --git a/check_cups.py b/check_cups.py index e8833fe..b331996 100755 --- a/check_cups.py +++ b/check_cups.py @@ -23,7 +23,7 @@ # along with this file. If not, see . # ##################################################################### -import datetime +import os import re import subprocess import sys @@ -59,27 +59,26 @@ plugin.add_cmdlineoption('-J', '--check-jobs', 'check_jobs', 'check job queue', plugin.add_cmdlineoption('-w', '', 'warn', 'warning thresold for old jobs (seconds)', default='3600') plugin.add_cmdlineoption('-c', '', 'crit', 'warning thresold for old jobs (seconds)', default='86400') -plugin.add_cmdlineoption('', '--mymontools-testmode', 'mymontools_testmode', None, default=False, action='store_true') +plugin.add_cmdlineoption('', '--mymonplugins-testmode', 'mymonplugins_testmode', None, default=False, action='store_true') plugin.parse_cmdlineoptions() ############################################################################## ##### Testmode -if plugin.options.mymontools_testmode: +if plugin.options.mymonplugins_testmode: # Because we need tzlocal from dateutil.tz - mymontools_testmode = {} + mymonplugins_testmode = {} - mymontools_testmode['lpstat -a'] = '''Printer_One accepting requests since 1970-01-01T00:00:00 CEST -Printer_Two accepting requests since 2016-01-01T00:00:00 CEST -Printer_Three accepting requests since 2116-01-01T00:00:00 CEST -Printer_Rejecting not accepting requests since 2016-02-01T00:00:00 CEST - + mymonplugins_testmode['lpstat -a'] = '''Printer_One accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST +Printer_Two accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST +Printer_Three accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST +Printer_Rejecting not accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST - Rejecting Jobs'''.split('\n') - from dateutil.tz import tzlocal - mymontools_testmode['lpstat -o'] = '''Printer_One-1234 username 12345 1970-01-01T00:00:00 CEST -Printer_Two-1234 username 12345 %s''' % datetime.datetime.now(tzlocal()).strftime('%FT%T %Z') - mymontools_testmode['lpstat -o'] = mymontools_testmode['lpstat -o'].split('\n') + mymonplugins_testmode['lpstat -o'] = '''Printer_One-1234 username 12345 Thu 01 Jan 1970 01:07:19 AM CET +Printer_Two-1234 username 12345 %s''' % time.strftime('%a %d %b %Y %I:%M:%S %p %Z', time.localtime()) + mymonplugins_testmode['lpstat -o'] = mymonplugins_testmode['lpstat -o'].split('\n') ############################################################################## @@ -107,30 +106,27 @@ def check_printer_queue(output_printer_queue): ############################################################################## def check_job_queue(output_job_queue): - try: - import dateutil.parser - from dateutil.tz import tzlocal - except ImportError: - print('Python module "dateutil" required, please install!') - sys.exit(3) - - m = re.compile('(\d{4}\D\d{2}\D\d{2}\D\d{2}\D\d{2}\D\d{2}\D\w{3,5})') + m = re.compile('(\w{3} \d\d \w{3} \d{4} \d\d:\d\d:\d\d (AM|PM) \w{3,5})') nowsecs = long( time.time() ) jobs_warn = [] jobs_crit = [] for line in output_job_queue: + plugin.verbose(3, line) f = m.search(line) if not f: + plugin.verbose(1, 'No timestamp found in "%s"' % line) continue - tstamp = dateutil.parser.parse(f.group(0)) + tstamp = time.strptime(f.group(1), '%a %d %b %Y %I:%M:%S %p %Z') - tsecs = long( tstamp.strftime('%s') ) - rc = plugin.value_wc_to_returncode((nowsecs-tsecs), plugin.options.warn, plugin.options.crit) + tsecs = long( time.mktime(tstamp) ) + age = nowsecs - tsecs + rc = plugin.value_wc_to_returncode(age, plugin.options.warn, plugin.options.crit) if rc == 1: jobs_warn.append(line) elif rc == 2: jobs_crit.append(line) + plugin.verbose(1, 'Job is %s seconds old, state/returncode: %s' % (age, rc)) jobs = len(output_job_queue) jobs_old = len(jobs_warn) + len(jobs_crit) @@ -147,11 +143,14 @@ def check_job_queue(output_job_queue): ############################################################################## def call_cmd(cmdline): - if plugin.options.mymontools_testmode: - return (mymontools_testmode.get(' '.join(cmdline)), '', 0) + if plugin.options.mymonplugins_testmode: + return (mymonplugins_testmode.get(' '.join(cmdline)), '', 0) + + myenv = dict(os.environ) + myenv['LC_ALL'] = 'C' try: - cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE) + cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE, env=myenv) (sout, serr) = cmd.communicate() if sout: sout = sout.lstrip().rstrip().split('\n')