From c1c132199da64a2165e6f435987657f5d3dbbefb Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Wed, 22 Sep 2021 12:08:51 +0200 Subject: [PATCH] check_apaches: Rebuild plugin with quad check notation --- check_apaches.py | 211 +++++++++++++++++++++++++++++++---------------- 1 file changed, 139 insertions(+), 72 deletions(-) diff --git a/check_apaches.py b/check_apaches.py index 75ee5b6..7089b7c 100755 --- a/check_apaches.py +++ b/check_apaches.py @@ -5,7 +5,7 @@ # (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 # +# sven-mymonplugins@velt.biz # # # # This file is part of "velt.biz - My Monitoring Plugins" # # 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') sys.exit(127) +############################################################ -plugin = MonitoringPlugin( - pluginname='check_apaches', - tagforstatusline='APACHE', - description='Check Apache workers', - version='0.1', - ) +class CheckApaches(MonitoringPlugin): -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', '', 'url', 'path to "server-status"', default='/server-status') -plugin.add_cmdlineoption('-a', '', 'httpauth', 'HTTP Username and Password, separated by ":"') -plugin.add_cmdlineoption('-w', '', 'warn', 'warning thresold', default='20') -plugin.add_cmdlineoption('-c', '', 'crit', 'warning thresold', default='50') -plugin.add_cmdlineoption('', '--statistics', 'statistics', 'Output worker statistics', action='store_true') + apache_busy = None + apache_idle = None + apache_scoreboard = None + apache_states = None + data = None + url = None -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']: - plugin.back2nagios(3, 'Unknown protocol "' + plugin.options.proto + '"') + 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 -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' -plugin.verbose(1, 'Status URL: ' + url) + def check_worker(self, target='busy', warn='', crit=''): + pd = {'label':'busy', 'unit':'', 'warn':warn, 'crit': crit, 'min':0} -if plugin.options.httpauth: - httpauth = plugin.options.httpauth.split(':') - if len(httpauth) != 2: - plugin.back2nagios(3, 'Wrong format of authentication data! Need "USERNAME:PASSWORD", got "' + plugin.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) + 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'] = 'idle' -try: - 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!') + return self.remember_check('worker', returncode, output, perfdata=[pd], target=target) -data = data.decode() -plugin.verbose(2, 'Got data:') -plugin.verbose(2, data) -try: - idle = int(re.search('Idle(?:Workers|Servers): (\d+)\n', data).group(1)) - busy = int(re.search('Busy(?:Workers|Servers): (\d+)\n', data).group(1)) -except: - plugin.back2nagios(2, 'Could not analyze data!') + 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) -states = None -if plugin.options.statistics: - scoreboard = re.search('Scoreboard: (.*)\n', data) - if scoreboard: - states = {'_':0, 'S':0, 'R':0, 'W':0, 'K':0, 'D':0, 'C':0, 'L':0, 'G':0, 'I':0, '.':0,} - for worker in scoreboard.group(1): - 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)') + 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!') -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) -plugin.format_add_performancedata('idle', idle, '') + if not self.options.port: + 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