2to3: check_apaches plus manual work

This commit is contained in:
Sven Velt 2021-09-22 12:07:41 +02:00
parent 195b0c5b90
commit 407d99c45d

View file

@ -1,10 +1,10 @@
#!/usr/bin/env python
#!/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 #
# (c) 2016- by Sven Velt, Germany #
# sven-mymonplugins@velt.biz #
# #
# This file is part of "velt.biz - My Monitoring Plugins" #
@ -28,26 +28,30 @@
import os
import re
import sys
import urllib2
import urllib.request, urllib.error, urllib.parse
try:
from monitoringplugin import MonitoringPlugin
from MyMonPlugin import MonitoringPlugin
except ImportError:
print '=========================='
print 'AIKS! Python import error!'
print '==========================\n'
print 'Could not find "monitoringplugin.py"!\n'
print 'Did you download "%s"' % os.path.basename(sys.argv[0])
print 'without "monitoringplugin.py"?\n'
print 'Please go back to'
print 'https://gogs.velt.biz/velt.biz/MyMonPlugins and download it,'
print 'or even better:'
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'
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)
plugin = MonitoringPlugin(pluginname='check_apaches', tagforstatusline='APACHE', description='Check Apache workers', version='0.1')
plugin = MonitoringPlugin(
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)
@ -76,20 +80,22 @@ 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 = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, httpauth[0], httpauth[1])
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
authhandler = urllib.request.HTTPBasicAuthHandler(passman)
opener = urllib.request.build_opener(authhandler)
urllib.request.install_opener(opener)
try:
data = urllib2.urlopen(url).read()
except urllib2.HTTPError, e:
data = urllib.request.urlopen(url).read()
except urllib.error.HTTPError as e:
plugin.back2nagios(2, 'Could not read data! ' + str(e))
except urllib2.URLError, e:
except urllib.error.URLError as e:
plugin.back2nagios(2, 'Could not connect to server!')
plugin.verbose(2, 'Got data:\n' + data)
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))