MyMonPlugins/check_apaches.py
2021-09-22 12:15:30 +02:00

203 lines
7.7 KiB
Python
Executable file

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
#####################################################################
# (c) 2007-2011 by Sven Velt and team(ix) GmbH, Nuernberg, Germany #
# sv@teamix.net #
# (c) 2016- by Sven Velt, Germany #
# sven-mymonplugins@velt.biz #
# #
# This file is part of "velt.biz - My Monitoring Plugins" #
# a fork of "team(ix) Monitoring Plugins" in 2015 #
# URL: https://gogs.velt.biz/velt.biz/MyMonPlugins/ #
# #
# This file 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, either version 2 of the License, #
# or (at your option) any later version. #
# #
# This file is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this file. If not, see <http://www.gnu.org/licenses/>. #
#####################################################################
import os
import re
import sys
import urllib.request, urllib.error, urllib.parse
try:
from MyMonPlugin import MonitoringPlugin
except ImportError:
print('==========================')
print('AIKS! Python import error!')
print('==========================\n')
print('Could not find class "MonitoringPlugin"!\n')
print('Did you download "%s"' % os.path.basename(sys.argv[0]))
print('without "MyMonPlugin/"?\n')
print('Please go back to')
print('https://gogs.velt.biz/velt.biz/MyMonPlugins and:')
print('- get a full archive at http://gogs.velt.biz/velt.biz/MyMonPlugins/releases')
print('- or a master snapshot at http://gogs.velt.biz/velt.biz/MyMonPlugins/archive/master.tar.gz\n')
sys.exit(127)
############################################################
class CheckApaches(MonitoringPlugin):
apache_busy = None
apache_idle = None
apache_scoreboard = None
apache_states = None
data = None
url = None
def analyze(self):
try:
self.apache_idle = int(re.search('Idle(?:Workers|Servers): (\d+)\n', self.data).group(1))
self.apache_busy = int(re.search('Busy(?:Workers|Servers): (\d+)\n', self.data).group(1))
except:
self.back2nagios(2, 'Could not analyze data!')
self.apache_scoreboard = re.search('Scoreboard: (.*)\n', self.data)
if self.apache_scoreboard:
self.apache_states = {'_':0, 'S':0, 'R':0, 'W':0, 'K':0, 'D':0, 'C':0, 'L':0, 'G':0, 'I':0, '.':0,}
for worker in self.apache_scoreboard.group(1):
self.apache_states[worker] += 1
pass
def check_worker(self, target='busy', warn='', crit=''):
pd = {'label':'worker_busy', 'unit':'', 'warn':warn, 'crit': crit, 'min':0}
if target == 'busy':
returncode = self.value_wc_to_returncode(self.apache_busy, warn, crit)
output = '%d busy workers' % self.apache_busy
pd['value'] = self.apache_busy
elif target == 'idle':
returncode = self.value_wc_to_returncode(self.apache_idle, warn, crit)
output = '%d idle workers' % self.apache_idle
pd['value'] = self.apache_idle
pd['label'] = 'worker_idle'
return self.remember_check('worker', returncode, output, perfdata=[pd], target=target)
def get_server_status(self):
if self.options.httpauth:
httpauth = self.options.httpauth.split(':')
if len(httpauth) != 2:
self.back2nagios(3, 'Wrong format of authentication data! Need "USERNAME:PASSWORD", got "' + self.options.httpauth + '"')
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, httpauth[0], httpauth[1])
authhandler = urllib.request.HTTPBasicAuthHandler(passman)
opener = urllib.request.build_opener(authhandler)
urllib.request.install_opener(opener)
try:
self.data = urllib.request.urlopen(self.url).read().decode()
except urllib.error.HTTPError as e:
self.back2nagios(2, 'Could not read data! ' + str(e))
except urllib.error.URLError as e:
self.back2nagios(2, 'Could not connect to server!')
pass
def set_status_url(self):
if self.options.url:
self.url = self.options.url
else:
if self.options.proto not in ['http', 'https']:
self.back2nagios(3, 'Unknown protocol "' + self.options.proto + '"')
if not self.options.port:
if self.options.proto == 'https':
self.options.port = '443'
else:
self.options.port = '80'
self.url = self.options.proto + '://' + self.options.host + ':' + self.options.port + '/' + self.options.uri + '?auto'
pass
############################################################
def main():
plugin = CheckApaches(
pluginname='check_apaches',
tagforstatusline='APACHE',
description='Check Apache workers',
version='0.1',
)
plugin.add_cmdlineoption('-H', '', 'host', 'Hostname/IP to check', default='localhost')
plugin.add_cmdlineoption('-p', '', 'port', 'port to connect', default=None)
plugin.add_cmdlineoption('-P', '', 'proto', 'protocol to use', default='http')
plugin.add_cmdlineoption('-u', '', 'uri', 'path to "server-status"', default='/server-status')
plugin.add_cmdlineoption('-U', '', 'url', 'complete URL to "server-status", overrides other options', default=None)
plugin.add_cmdlineoption('-a', '', 'httpauth', 'HTTP Username and Password, separated by ":"')
plugin.add_cmdlineoption('-w', '', 'warn', 'OBSOLETE: warning thresold', default='')
plugin.add_cmdlineoption('-c', '', 'crit', 'OBSOLETE: warning thresold', default='')
plugin.add_cmdlineoption('', '--statistics', 'statistics', 'Output worker statistics', action='store_true')
plugin.parse_cmdlineoptions()
plugin.set_status_url()
plugin.verbose(1, 'Status URL: ' + plugin.url)
plugin.get_server_status()
plugin.verbose(2, 'Got data:')
plugin.verbose(2, plugin.data)
plugin.analyze()
checks = plugin.args2checks()
# backward compatibility
if len(checks) == 0:
checks = [
('worker','busy','20','50'),
('worker','idle','20:','5:'),
]
for quad in checks:
(check, target, warn, crit) = tuple(quad)
if check == 'worker':
result = plugin.check_worker(target, warn, crit)
else:
result = plugin.remember_check(check, plugin.RETURNCODE['UNKNOWN'], 'Unknown check "' + check + '"!')
plugin.verbose(5, result)
if plugin.options.statistics:
plugin.add_multilineoutput(str(plugin.apache_states['_']) + ' waiting for connection')
plugin.add_multilineoutput(str(plugin.apache_states['S']) + ' starting up')
plugin.add_multilineoutput(str(plugin.apache_states['R']) + ' reading request')
plugin.add_multilineoutput(str(plugin.apache_states['W']) + ' writing/sending reply')
plugin.add_multilineoutput(str(plugin.apache_states['K']) + ' keepalive')
plugin.add_multilineoutput(str(plugin.apache_states['D']) + ' looking up in DNS')
plugin.add_multilineoutput(str(plugin.apache_states['C']) + ' closing connection')
plugin.add_multilineoutput(str(plugin.apache_states['L']) + ' logging')
plugin.add_multilineoutput(str(plugin.apache_states['G']) + ' gracefully finishing')
plugin.add_multilineoutput(str(plugin.apache_states['I']) + ' idle cleanup of worker')
plugin.add_multilineoutput(str(plugin.apache_states['.']) + ' open slots(up to ServerLimit)')
plugin.brain2output()
plugin.exit()
if __name__ == '__main__':
main()
#vim: ts=4 sts=4 sw=4