Module: check_mk
Branch: master
Commit: 4896b7c3834ea68fbfac8936e76b013325701edb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4896b7c3834ea6…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Mon Apr 1 13:51:29 2019 +0200
apache_status: refactor
Change-Id: I5d8098d6623717cc186e16990d499986a4b4f69d
---
agents/plugins/apache_status | 143 ++++++++++++++++++++++++-------------------
1 file changed, 79 insertions(+), 64 deletions(-)
diff --git a/agents/plugins/apache_status b/agents/plugins/apache_status
index 7b4c739..2f390c8 100755
--- a/agents/plugins/apache_status
+++ b/agents/plugins/apache_status
@@ -48,28 +48,32 @@ import sys
import urllib2
import ssl
-config_dir = os.getenv("MK_CONFDIR", "/etc/check_mk")
-config_file = config_dir + "/apache_status.conf"
-
-if not os.path.exists(config_file):
- config_file = config_dir + "/apache_status.cfg"
-
# We have to deal with socket timeouts. Python > 2.6
# supports timeout parameter for the urllib2.urlopen method
# but we are on a python 2.5 system here which seem to use the
# default socket timeout. We are local here so set it to 1 second.
socket.setdefaulttimeout(5.0)
-# None or list of (proto, ipaddress, port) tuples.
-# proto is 'http' or 'https'
-servers = None
-ssl_ports = [443]
-if os.path.exists(config_file):
- execfile(config_file)
+def get_config():
+ config_dir = os.getenv("MK_CONFDIR", "/etc/check_mk")
+ config_file = config_dir + "/apache_status.conf"
+
+ if not os.path.exists(config_file):
+ config_file = config_dir + "/apache_status.cfg"
+ # None or list of (proto, ipaddress, port) tuples.
+ # proto is 'http' or 'https'
+ config = {
+ "servers": None,
+ "ssl_ports": [443],
+ }
+ if os.path.exists(config_file):
+ execfile(config_file, config)
+ return config
-def try_detect_servers():
+
+def try_detect_servers(ssl_ports):
results = []
for netstat_line in os.popen('netstat -tlnp 2>/dev/null').readlines():
@@ -115,60 +119,71 @@ def try_detect_servers():
return results
-if servers is None:
- servers = try_detect_servers()
+def main():
+ config = get_config()
+ servers = config["servers"]
+ ssl_ports = config["ssl_ports"]
+
+ if servers is None:
+ servers = try_detect_servers(ssl_ports)
-if not servers:
- sys.exit(0)
+ if not servers:
+ return 0
-sys.stdout.write('<<<apache_status>>>\n')
-for server in servers:
- if isinstance(server, tuple):
- proto, address, port = server
- page = 'server-status'
- else:
- proto = server['protocol']
- address = server['address']
- port = server['port']
- page = server.get('page', 'server-status')
+ sys.stdout.write('<<<apache_status>>>\n')
+ for server in servers:
+ if isinstance(server, tuple):
+ proto, address, port = server
+ page = 'server-status'
+ else:
+ proto = server['protocol']
+ address = server['address']
+ port = server['port']
+ page = server.get('page', 'server-status')
- portspec = ':%d' % port if port else ''
+ portspec = ':%d' % port if port else ''
- try:
- url = '%s://%s%s/%s?auto' % (proto, address, portspec, page)
- # Try to fetch the status page for each server
try:
- request = urllib2.Request(url, headers={"Accept":
"text/plain"})
- fd = urllib2.urlopen(request)
- except urllib2.URLError, e:
- if 'unknown protocol' in str(e):
- # HACK: workaround misconfigurations where port 443 is used for
- # serving non ssl secured http
- url = 'http://%s%s/server-status?auto' % (address, portspec)
- fd = urllib2.urlopen(url)
- else:
- raise
+ url = '%s://%s%s/%s?auto' % (proto, address, portspec, page)
+ # Try to fetch the status page for each server
+ try:
+ request = urllib2.Request(url, headers={"Accept":
"text/plain"})
+ fd = urllib2.urlopen(request)
+ except urllib2.URLError, e:
+ if 'unknown protocol' in str(e):
+ # HACK: workaround misconfigurations where port 443 is used for
+ # serving non ssl secured http
+ url = 'http://%s%s/server-status?auto' % (address, portspec)
+ fd = urllib2.urlopen(url)
+ else:
+ raise
+ except Exception, e:
+ if 'doesn\'t match' in str(e) and address in
("127.0.0.1", "[::1]", "localhost"):
+ # HACK: workaround if SSL port is found and localhost is using
+ # SSL connections but certificate does not match
+ no_cert_context = ssl.create_default_context()
+ no_cert_context.check_hostname = False
+ no_cert_context.verify_mode = ssl.CERT_NONE
+ fd = urllib2.urlopen(url, context=no_cert_context)
+ else:
+ raise
+
+ for line in fd.read().split('\n'):
+ if not line.strip():
+ continue
+ if line.lstrip()[0] == '<':
+ # Seems to be html output. Skip this server.
+ break
+
+ sys.stdout.write("%s %s %s\n" % (address, port, line))
+ except urllib2.HTTPError, e:
+ sys.stderr.write('HTTP-Error (%s%s): %s %s\n' % (address, portspec,
e.code, e))
+
except Exception, e:
- if 'doesn\'t match' in str(e) and address in
("127.0.0.1", "[::1]", "localhost"):
- # HACK: workaround if SSL port is found and localhost is using
- # SSL connections but certificate does not match
- no_cert_context = ssl.create_default_context()
- no_cert_context.check_hostname = False
- no_cert_context.verify_mode = ssl.CERT_NONE
- fd = urllib2.urlopen(url, context=no_cert_context)
- else:
- raise
-
- for line in fd.read().split('\n'):
- if not line.strip():
- continue
- if line.lstrip()[0] == '<':
- # Seems to be html output. Skip this server.
- break
-
- sys.stdout.write("%s %s %s\n" % (address, port, line))
- except urllib2.HTTPError, e:
- sys.stderr.write('HTTP-Error (%s%s): %s %s\n' % (address, portspec,
e.code, e))
-
- except Exception, e:
- sys.stderr.write('Exception (%s%s): %s\n' % (address, portspec, e))
+ sys.stderr.write('Exception (%s%s): %s\n' % (address, portspec, e))
+
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())