check_cups.py: Correct timezone parsing

This commit is contained in:
Sven Velt 2016-10-12 12:23:21 +02:00
parent ed17f572dc
commit 16364832dd

View file

@ -23,7 +23,7 @@
# along with this file. If not, see <http://www.gnu.org/licenses/>. # # along with this file. If not, see <http://www.gnu.org/licenses/>. #
##################################################################### #####################################################################
import datetime import os
import re import re
import subprocess import subprocess
import sys 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('-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('-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() plugin.parse_cmdlineoptions()
############################################################################## ##############################################################################
##### Testmode ##### Testmode
if plugin.options.mymontools_testmode: if plugin.options.mymonplugins_testmode:
# Because we need tzlocal from dateutil.tz # 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 mymonplugins_testmode['lpstat -a'] = '''Printer_One accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST
Printer_Two accepting requests since 2016-01-01T00:00:00 CEST Printer_Two accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST
Printer_Three accepting requests since 2116-01-01T00:00:00 CEST Printer_Three accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST
Printer_Rejecting not accepting requests since 2016-02-01T00:00:00 CEST - Printer_Rejecting not accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST -
Rejecting Jobs'''.split('\n') Rejecting Jobs'''.split('\n')
from dateutil.tz import tzlocal mymonplugins_testmode['lpstat -o'] = '''Printer_One-1234 username 12345 Thu 01 Jan 1970 01:07:19 AM CET
mymontools_testmode['lpstat -o'] = '''Printer_One-1234 username 12345 1970-01-01T00:00:00 CEST Printer_Two-1234 username 12345 %s''' % time.strftime('%a %d %b %Y %I:%M:%S %p %Z', time.localtime())
Printer_Two-1234 username 12345 %s''' % datetime.datetime.now(tzlocal()).strftime('%FT%T %Z') mymonplugins_testmode['lpstat -o'] = mymonplugins_testmode['lpstat -o'].split('\n')
mymontools_testmode['lpstat -o'] = mymontools_testmode['lpstat -o'].split('\n')
############################################################################## ##############################################################################
@ -107,30 +106,27 @@ def check_printer_queue(output_printer_queue):
############################################################################## ##############################################################################
def check_job_queue(output_job_queue): def check_job_queue(output_job_queue):
try: m = re.compile('(\w{3} \d\d \w{3} \d{4} \d\d:\d\d:\d\d (AM|PM) \w{3,5})')
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})')
nowsecs = long( time.time() ) nowsecs = long( time.time() )
jobs_warn = [] jobs_warn = []
jobs_crit = [] jobs_crit = []
for line in output_job_queue: for line in output_job_queue:
plugin.verbose(3, line)
f = m.search(line) f = m.search(line)
if not f: if not f:
plugin.verbose(1, 'No timestamp found in "%s"' % line)
continue 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') ) tsecs = long( time.mktime(tstamp) )
rc = plugin.value_wc_to_returncode((nowsecs-tsecs), plugin.options.warn, plugin.options.crit) age = nowsecs - tsecs
rc = plugin.value_wc_to_returncode(age, plugin.options.warn, plugin.options.crit)
if rc == 1: if rc == 1:
jobs_warn.append(line) jobs_warn.append(line)
elif rc == 2: elif rc == 2:
jobs_crit.append(line) jobs_crit.append(line)
plugin.verbose(1, 'Job is %s seconds old, state/returncode: %s' % (age, rc))
jobs = len(output_job_queue) jobs = len(output_job_queue)
jobs_old = len(jobs_warn) + len(jobs_crit) jobs_old = len(jobs_warn) + len(jobs_crit)
@ -147,11 +143,14 @@ def check_job_queue(output_job_queue):
############################################################################## ##############################################################################
def call_cmd(cmdline): def call_cmd(cmdline):
if plugin.options.mymontools_testmode: if plugin.options.mymonplugins_testmode:
return (mymontools_testmode.get(' '.join(cmdline)), '', 0) return (mymonplugins_testmode.get(' '.join(cmdline)), '', 0)
myenv = dict(os.environ)
myenv['LC_ALL'] = 'C'
try: try:
cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE) cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE, env=myenv)
(sout, serr) = cmd.communicate() (sout, serr) = cmd.communicate()
if sout: if sout:
sout = sout.lstrip().rstrip().split('\n') sout = sout.lstrip().rstrip().split('\n')