check_apaches: Rebuild plugin with quad check notation

This commit is contained in:
Sven Velt 2021-09-22 12:08:51 +02:00
parent 407d99c45d
commit c1c132199d

View file

@ -5,7 +5,7 @@
# (c) 2007-2011 by Sven Velt and team(ix) GmbH, Nuernberg, Germany # # (c) 2007-2011 by Sven Velt and team(ix) GmbH, Nuernberg, Germany #
# sv@teamix.net # # sv@teamix.net #
# (c) 2016- by Sven Velt, Germany # # (c) 2016- by Sven Velt, Germany #
# sven-mymonplugins@velt.biz # # sven-mymonplugins@velt.biz #
# # # #
# This file is part of "velt.biz - My Monitoring Plugins" # # This file is part of "velt.biz - My Monitoring Plugins" #
# a fork of "team(ix) Monitoring Plugins" in 2015 # # a fork of "team(ix) Monitoring Plugins" in 2015 #
@ -45,91 +45,158 @@ except ImportError:
print('- or a master snapshot at http://gogs.velt.biz/velt.biz/MyMonPlugins/archive/master.tar.gz\n') print('- or a master snapshot at http://gogs.velt.biz/velt.biz/MyMonPlugins/archive/master.tar.gz\n')
sys.exit(127) sys.exit(127)
############################################################
plugin = MonitoringPlugin( class CheckApaches(MonitoringPlugin):
pluginname='check_apaches',
tagforstatusline='APACHE',
description='Check Apache workers',
version='0.1',
)
plugin.add_cmdlineoption('-H', '', 'host', 'Hostname/IP to check', default='localhost') apache_busy = None
plugin.add_cmdlineoption('-p', '', 'port', 'port to connect', default=None) apache_idle = None
plugin.add_cmdlineoption('-P', '', 'proto', 'protocol to use', default='http') apache_scoreboard = None
plugin.add_cmdlineoption('-u', '', 'url', 'path to "server-status"', default='/server-status') apache_states = None
plugin.add_cmdlineoption('-a', '', 'httpauth', 'HTTP Username and Password, separated by ":"') data = None
plugin.add_cmdlineoption('-w', '', 'warn', 'warning thresold', default='20') url = None
plugin.add_cmdlineoption('-c', '', 'crit', 'warning thresold', default='50')
plugin.add_cmdlineoption('', '--statistics', 'statistics', 'Output worker statistics', action='store_true')
plugin.parse_cmdlineoptions() 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!')
if plugin.options.proto not in ['http', 'https']: self.apache_scoreboard = re.search('Scoreboard: (.*)\n', self.data)
plugin.back2nagios(3, 'Unknown protocol "' + plugin.options.proto + '"') 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
if not plugin.options.port:
if plugin.options.proto == 'https':
plugin.options.port = '443'
else:
plugin.options.port = '80'
url = plugin.options.proto + '://' + plugin.options.host + ':' + plugin.options.port + '/' + plugin.options.url + '?auto' def check_worker(self, target='busy', warn='', crit=''):
plugin.verbose(1, 'Status URL: ' + url) pd = {'label':'busy', 'unit':'', 'warn':warn, 'crit': crit, 'min':0}
if plugin.options.httpauth: if target == 'busy':
httpauth = plugin.options.httpauth.split(':') returncode = self.value_wc_to_returncode(self.apache_busy, warn, crit)
if len(httpauth) != 2: output = '%d busy workers' % self.apache_busy
plugin.back2nagios(3, 'Wrong format of authentication data! Need "USERNAME:PASSWORD", got "' + plugin.options.httpauth + '"') pd['value'] = self.apache_busy
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() elif target == 'idle':
passman.add_password(None, url, httpauth[0], httpauth[1]) returncode = self.value_wc_to_returncode(self.apache_idle, warn, crit)
authhandler = urllib.request.HTTPBasicAuthHandler(passman) output = '%d idle workers' % self.apache_idle
opener = urllib.request.build_opener(authhandler) pd['value'] = self.apache_idle
urllib.request.install_opener(opener) pd['label'] = 'idle'
try: return self.remember_check('worker', returncode, output, perfdata=[pd], target=target)
data = urllib.request.urlopen(url).read()
except urllib.error.HTTPError as e:
plugin.back2nagios(2, 'Could not read data! ' + str(e))
except urllib.error.URLError as e:
plugin.back2nagios(2, 'Could not connect to server!')
data = data.decode()
plugin.verbose(2, 'Got data:')
plugin.verbose(2, data)
try: def get_server_status(self):
idle = int(re.search('Idle(?:Workers|Servers): (\d+)\n', data).group(1)) if self.options.httpauth:
busy = int(re.search('Busy(?:Workers|Servers): (\d+)\n', data).group(1)) httpauth = self.options.httpauth.split(':')
except: if len(httpauth) != 2:
plugin.back2nagios(2, 'Could not analyze data!') 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)
states = None try:
if plugin.options.statistics: self.data = urllib.request.urlopen(self.url).read().decode()
scoreboard = re.search('Scoreboard: (.*)\n', data) except urllib.error.HTTPError as e:
if scoreboard: self.back2nagios(2, 'Could not read data! ' + str(e))
states = {'_':0, 'S':0, 'R':0, 'W':0, 'K':0, 'D':0, 'C':0, 'L':0, 'G':0, 'I':0, '.':0,} except urllib.error.URLError as e:
for worker in scoreboard.group(1): self.back2nagios(2, 'Could not connect to server!')
states[worker] += 1
plugin.add_multilineoutput(str(states['_']) + ' waiting for connection')
plugin.add_multilineoutput(str(states['S']) + ' starting up')
plugin.add_multilineoutput(str(states['R']) + ' reading request')
plugin.add_multilineoutput(str(states['W']) + ' writing/sending reply')
plugin.add_multilineoutput(str(states['K']) + ' keepalive')
plugin.add_multilineoutput(str(states['D']) + ' looking up in DNS')
plugin.add_multilineoutput(str(states['C']) + ' closing connection')
plugin.add_multilineoutput(str(states['L']) + ' logging')
plugin.add_multilineoutput(str(states['G']) + ' gracefully finishing')
plugin.add_multilineoutput(str(states['I']) + ' idle cleanup of worker')
plugin.add_multilineoutput(str(states['.']) + ' open slots(up to ServerLimit)')
returncode = plugin.value_wc_to_returncode(busy, plugin.options.warn, plugin.options.crit) pass
plugin.add_output(str(busy) + ' busy workers, ' + str(idle) + ' idle')
plugin.add_returncode(returncode) 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 + '"')
plugin.format_add_performancedata('busy', busy, '', warn=plugin.options.warn, crit=plugin.options.crit, min=0.0) if not self.options.port:
plugin.format_add_performancedata('idle', idle, '') if self.options.proto == 'https':
self.options.port = '443'
else:
self.options.port = '80'
plugin.exit() 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