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

@ -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)
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'
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: if len(httpauth) != 2:
plugin.back2nagios(3, 'Wrong format of authentication data! Need "USERNAME:PASSWORD", got "' + plugin.options.httpauth + '"') self.back2nagios(3, 'Wrong format of authentication data! Need "USERNAME:PASSWORD", got "' + self.options.httpauth + '"')
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, httpauth[0], httpauth[1]) passman.add_password(None, url, httpauth[0], httpauth[1])
authhandler = urllib.request.HTTPBasicAuthHandler(passman) authhandler = urllib.request.HTTPBasicAuthHandler(passman)
opener = urllib.request.build_opener(authhandler) opener = urllib.request.build_opener(authhandler)
urllib.request.install_opener(opener) urllib.request.install_opener(opener)
try: try:
data = urllib.request.urlopen(url).read() self.data = urllib.request.urlopen(self.url).read().decode()
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
plugin.back2nagios(2, 'Could not read data! ' + str(e)) self.back2nagios(2, 'Could not read data! ' + str(e))
except urllib.error.URLError as e: except urllib.error.URLError as e:
plugin.back2nagios(2, 'Could not connect to server!') self.back2nagios(2, 'Could not connect to server!')
data = data.decode() pass
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!')
states = None def set_status_url(self):
if plugin.options.statistics: if self.options.url:
scoreboard = re.search('Scoreboard: (.*)\n', data) self.url = self.options.url
if scoreboard: else:
states = {'_':0, 'S':0, 'R':0, 'W':0, 'K':0, 'D':0, 'C':0, 'L':0, 'G':0, 'I':0, '.':0,} if self.options.proto not in ['http', 'https']:
for worker in scoreboard.group(1): self.back2nagios(3, 'Unknown protocol "' + self.options.proto + '"')
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) if not self.options.port:
if self.options.proto == 'https':
self.options.port = '443'
else:
self.options.port = '80'
plugin.add_output(str(busy) + ' busy workers, ' + str(idle) + ' idle') self.url = self.options.proto + '://' + self.options.host + ':' + self.options.port + '/' + self.options.uri + '?auto'
plugin.add_returncode(returncode) pass
plugin.format_add_performancedata('busy', busy, '', warn=plugin.options.warn, crit=plugin.options.crit, min=0.0) ############################################################
plugin.format_add_performancedata('idle', idle, '')
plugin.exit() 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