check_cups.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env python2
  2. # -*- encoding: utf-8 -*-
  3. #####################################################################
  4. # (c) 2016 by Sven Velt, Germany #
  5. # sven-mymonplugins@velt.biz #
  6. # #
  7. # This file is part of "velt.biz - My Monitoring Plugins" #
  8. # a fork of "team(ix) Monitoring Plugins" in 2015 #
  9. # URL: https://gogs.velt.biz/velt.biz/MyMonPlugins/ #
  10. # #
  11. # This file is free software: you can redistribute it and/or modify #
  12. # it under the terms of the GNU General Public License as published #
  13. # by the Free Software Foundation, either version 2 of the License, #
  14. # or (at your option) any later version. #
  15. # #
  16. # This file is distributed in the hope that it will be useful, but #
  17. # WITHOUT ANY WARRANTY; without even the implied warranty of #
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  19. # GNU General Public License for more details. #
  20. # #
  21. # You should have received a copy of the GNU General Public License #
  22. # along with this file. If not, see <http://www.gnu.org/licenses/>. #
  23. #####################################################################
  24. import os
  25. import re
  26. import subprocess
  27. import sys
  28. import time
  29. try:
  30. from monitoringplugin import MonitoringPlugin
  31. except ImportError:
  32. print '=========================='
  33. print 'AIKS! Python import error!'
  34. print '==========================\n'
  35. print 'Could not find "monitoringplugin.py"!\n'
  36. print 'Did you download "%s"' % os.path.basename(sys.argv[0])
  37. print 'without "monitoringplugin.py"?\n'
  38. print 'Please go back to'
  39. print 'https://gogs.velt.biz/velt.biz/MyMonPlugins and download it,'
  40. print 'or even better:'
  41. print 'get a full archive at http://gogs.velt.biz/velt.biz/MyMonPlugins/releases'
  42. print 'or a master snapshot at http://gogs.velt.biz/velt.biz/MyMonPlugins/archive/master.tar.gz\n'
  43. sys.exit(127)
  44. plugin = MonitoringPlugin(
  45. pluginname='check_cups',
  46. tagforstatusline='CUPS',
  47. description='Check CUPS jobs and printer queues',
  48. version='0.1',
  49. )
  50. plugin.add_cmdlineoption('-P', '--check-printers', 'check_printers', 'check printer queue', default=False, action='store_true' )
  51. plugin.add_cmdlineoption('-J', '--check-jobs', 'check_jobs', 'check job queue', default=False, action='store_true')
  52. plugin.add_cmdlineoption('-w', '', 'warn', 'warning thresold for old jobs (seconds)', default='3600')
  53. plugin.add_cmdlineoption('-c', '', 'crit', 'warning thresold for old jobs (seconds)', default='86400')
  54. plugin.add_cmdlineoption('', '--mymonplugins-testmode', 'mymonplugins_testmode', None, default=False, action='store_true')
  55. plugin.parse_cmdlineoptions()
  56. ##############################################################################
  57. ##### Testmode
  58. if plugin.options.mymonplugins_testmode:
  59. # Because we need tzlocal from dateutil.tz
  60. mymonplugins_testmode = {}
  61. mymonplugins_testmode['lpstat -a'] = '''Printer_One accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST
  62. Printer_Two accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST
  63. Printer_Three accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST
  64. Printer_Rejecting not accepting requests since Thu 16 Jun 2016 02:57:36 PM CEST -
  65. Rejecting Jobs'''.split('\n')
  66. mymonplugins_testmode['lpstat -o'] = '''Printer_One-1234 username 12345 Thu 01 Jan 1970 01:07:19 AM CET
  67. Printer_Two-1234 username 12345 %s''' % time.strftime('%a %d %b %Y %I:%M:%S %p %Z', time.localtime())
  68. mymonplugins_testmode['lpstat -o'] = mymonplugins_testmode['lpstat -o'].split('\n')
  69. ##############################################################################
  70. def check_printer_queue(output_printer_queue):
  71. lidx = 0
  72. printers = []
  73. printers_bad = []
  74. while lidx < len(output_printer_queue):
  75. printer = output_printer_queue[lidx].split(' ')[0]
  76. if output_printer_queue[lidx].find('not accepting') > 0:
  77. reason = output_printer_queue[(lidx+1)].replace('\t', '').lstrip().rstrip()
  78. printers_bad.append( (printer, reason,) )
  79. lidx += 1
  80. printers.append( printer )
  81. lidx += 1
  82. plugin.remember_check('All Printers', 0, '%d printer%s found' % (len(printers), len(printers) != 1 and 's' or ''), perfdata=[ {'label':'printers', 'value':len(printers), 'unit':''}, ] )
  83. for p in printers_bad:
  84. plugin.remember_check('%s' % p[0], 1, p[1])
  85. ##############################################################################
  86. def check_job_queue(output_job_queue):
  87. m = re.compile('(\w{3} \d\d \w{3} \d{4} \d\d:\d\d:\d\d (AM|PM) \w{3,5})')
  88. nowsecs = long( time.time() )
  89. jobs_warn = []
  90. jobs_crit = []
  91. for line in output_job_queue:
  92. plugin.verbose(3, line)
  93. f = m.search(line)
  94. if not f:
  95. plugin.verbose(1, 'No timestamp found in "%s"' % line)
  96. continue
  97. tstamp = time.strptime(f.group(1), '%a %d %b %Y %I:%M:%S %p %Z')
  98. tsecs = long( time.mktime(tstamp) )
  99. age = nowsecs - tsecs
  100. rc = plugin.value_wc_to_returncode(age, plugin.options.warn, plugin.options.crit)
  101. if rc == 1:
  102. jobs_warn.append(line)
  103. elif rc == 2:
  104. jobs_crit.append(line)
  105. plugin.verbose(1, 'Job is %s seconds old, state/returncode: %s' % (age, rc))
  106. jobs = len(output_job_queue)
  107. jobs_old = len(jobs_warn) + len(jobs_crit)
  108. perfdata = []
  109. perfdata.append( { 'label':'jobs', 'value':'%d' % jobs, 'unit':'', } )
  110. perfdata.append( { 'label':'jobs_old', 'value':'%d' % jobs_old, 'unit':'', } )
  111. plugin.remember_check('Jobs', 0, '%d job%s found' % (jobs, jobs != 1 and 's' or ''), perfdata=perfdata )
  112. for j in jobs_crit:
  113. plugin.remember_check('Job %s' % j.split(' ')[0], 2, m.search(j).group(0) )
  114. for j in jobs_warn:
  115. plugin.remember_check('Job %s' % j.split(' ')[0], 1, m.search(j).group(0) )
  116. ##############################################################################
  117. def call_cmd(cmdline):
  118. if plugin.options.mymonplugins_testmode:
  119. return (mymonplugins_testmode.get(' '.join(cmdline)), '', 0)
  120. myenv = dict(os.environ)
  121. myenv['LC_ALL'] = 'C'
  122. try:
  123. cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE, env=myenv)
  124. (sout, serr) = cmd.communicate()
  125. if sout:
  126. sout = sout.lstrip().rstrip().split('\n')
  127. else:
  128. sout = []
  129. except OSError:
  130. plugin.back2nagios(3, 'Could not execute "%s"' % ' '.join(cmdline))
  131. return (sout, serr, cmd.returncode)
  132. ##############################################################################
  133. allchecks = not( plugin.options.check_printers or plugin.options.check_jobs )
  134. if allchecks or plugin.options.check_printers:
  135. (sout, serr, retcode) = call_cmd(['lpstat', '-a'])
  136. check_printer_queue(sout)
  137. if allchecks or plugin.options.check_jobs:
  138. (sout, serr, retcode) = call_cmd(['lpstat', '-o'])
  139. check_job_queue(sout)
  140. # Exit
  141. plugin.brain2output()
  142. plugin.exit()