check_apaches.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. #####################################################################
  4. # (c) 2007-2011 by Sven Velt and team(ix) GmbH, Nuernberg, Germany #
  5. # sv@teamix.net #
  6. # #
  7. # This file is part of "team(ix) Monitoring Plugins" #
  8. # URL: http://oss.teamix.org/projects/monitoringplugins/ #
  9. # #
  10. # This file is free software: you can redistribute it and/or modify #
  11. # it under the terms of the GNU General Public License as published #
  12. # by the Free Software Foundation, either version 2 of the License, #
  13. # or (at your option) any later version. #
  14. # #
  15. # This file is distributed in the hope that it will be useful, but #
  16. # WITHOUT ANY WARRANTY; without even the implied warranty of #
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  18. # GNU General Public License for more details. #
  19. # #
  20. # You should have received a copy of the GNU General Public License #
  21. # along with this file. If not, see <http://www.gnu.org/licenses/>. #
  22. #####################################################################
  23. import os
  24. import re
  25. import sys
  26. import urllib2
  27. try:
  28. from monitoringplugin import MonitoringPlugin
  29. except ImportError:
  30. print '=========================='
  31. print 'AIKS! Python import error!'
  32. print '==========================\n'
  33. print 'Could not find "monitoringplugin.py"!\n'
  34. print 'Did you download "%s"' % os.path.basename(sys.argv[0])
  35. print 'without "monitoringplugin.py"?\n'
  36. print 'Please go back to'
  37. print 'http://oss.teamix.org/projects/monitoringplugins/ and download it,'
  38. print 'or even better:'
  39. print 'get a hole archive at http://oss.teamix.org/projects/monitoringplugins/files\n'
  40. sys.exit(127)
  41. plugin = MonitoringPlugin(pluginname='check_apaches', tagforstatusline='APACHE', description='Check Apache workers', version='0.1')
  42. plugin.add_cmdlineoption('-H', '', 'host', 'Hostname/IP to check', default='localhost')
  43. plugin.add_cmdlineoption('-p', '', 'port', 'port to connect', default=None)
  44. plugin.add_cmdlineoption('-P', '', 'proto', 'protocol to use', default='http')
  45. plugin.add_cmdlineoption('-u', '', 'url', 'path to "server-status"', default='/server-status')
  46. plugin.add_cmdlineoption('-a', '', 'httpauth', 'HTTP Username and Password, separated by ":"')
  47. plugin.add_cmdlineoption('-w', '', 'warn', 'warning thresold', default='20')
  48. plugin.add_cmdlineoption('-c', '', 'crit', 'warning thresold', default='50')
  49. plugin.add_cmdlineoption('', '--statistics', 'statistics', 'Output worker statistics', action='store_true')
  50. plugin.parse_cmdlineoptions()
  51. if plugin.options.proto not in ['http', 'https']:
  52. plugin.back2nagios(3, 'Unknown protocol "' + plugin.options.proto + '"')
  53. if not plugin.options.port:
  54. if plugin.options.proto == 'https':
  55. plugin.options.port = '443'
  56. else:
  57. plugin.options.port = '80'
  58. url = plugin.options.proto + '://' + plugin.options.host + ':' + plugin.options.port + '/' + plugin.options.url + '?auto'
  59. plugin.verbose(1, 'Status URL: ' + url)
  60. if plugin.options.httpauth:
  61. httpauth = plugin.options.httpauth.split(':')
  62. if len(httpauth) != 2:
  63. plugin.back2nagios(3, 'Wrong format of authentication data! Need "USERNAME:PASSWORD", got "' + plugin.options.httpauth + '"')
  64. passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  65. passman.add_password(None, url, httpauth[0], httpauth[1])
  66. authhandler = urllib2.HTTPBasicAuthHandler(passman)
  67. opener = urllib2.build_opener(authhandler)
  68. urllib2.install_opener(opener)
  69. try:
  70. data = urllib2.urlopen(url).read()
  71. except urllib2.HTTPError, e:
  72. plugin.back2nagios(2, 'Could not read data! ' + str(e))
  73. except urllib2.URLError, e:
  74. plugin.back2nagios(2, 'Could not connect to server!')
  75. plugin.verbose(2, 'Got data:\n' + data)
  76. try:
  77. idle = int(re.search('Idle(?:Workers|Servers): (\d+)\n', data).group(1))
  78. busy = int(re.search('Busy(?:Workers|Servers): (\d+)\n', data).group(1))
  79. except:
  80. plugin.back2nagios(2, 'Could not analyze data!')
  81. states = None
  82. if plugin.options.statistics:
  83. scoreboard = re.search('Scoreboard: (.*)\n', data)
  84. if scoreboard:
  85. states = {'_':0, 'S':0, 'R':0, 'W':0, 'K':0, 'D':0, 'C':0, 'L':0, 'G':0, 'I':0, '.':0,}
  86. for worker in scoreboard.group(1):
  87. states[worker] += 1
  88. plugin.add_multilineoutput(str(states['_']) + ' waiting for connection')
  89. plugin.add_multilineoutput(str(states['S']) + ' starting up')
  90. plugin.add_multilineoutput(str(states['R']) + ' reading request')
  91. plugin.add_multilineoutput(str(states['W']) + ' writing/sending reply')
  92. plugin.add_multilineoutput(str(states['K']) + ' keepalive')
  93. plugin.add_multilineoutput(str(states['D']) + ' looking up in DNS')
  94. plugin.add_multilineoutput(str(states['C']) + ' closing connection')
  95. plugin.add_multilineoutput(str(states['L']) + ' logging')
  96. plugin.add_multilineoutput(str(states['G']) + ' gracefully finishing')
  97. plugin.add_multilineoutput(str(states['I']) + ' idle cleanup of worker')
  98. plugin.add_multilineoutput(str(states['.']) + ' open slots(up to ServerLimit)')
  99. returncode = plugin.value_wc_to_returncode(busy, plugin.options.warn, plugin.options.crit)
  100. plugin.add_output(str(busy) + ' busy workers, ' + str(idle) + ' idle')
  101. plugin.add_returncode(returncode)
  102. plugin.format_add_performancedata('busy', busy, '', warn=plugin.options.warn, crit=plugin.options.crit, min=0.0)
  103. plugin.format_add_performancedata('idle', idle, '')
  104. plugin.exit()