Module: check_mk
Branch: master
Commit: 36bfe40ab2d4c1eecb6ff03c0c2e6de340fd73ef
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=36bfe40ab2d4c1…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon Nov 15 13:29:38 2010 +0100
webconf: you now can configure host aliases
---
web/htdocs/webconf.py | 73 +++++++++++++++++++++++++++++++++++++------------
1 files changed, 55 insertions(+), 18 deletions(-)
diff --git a/web/htdocs/webconf.py b/web/htdocs/webconf.py
index 14db080..fd96a2d 100644
--- a/web/htdocs/webconf.py
+++ b/web/htdocs/webconf.py
@@ -22,8 +22,9 @@ def read_configuration_file(filename):
if os.path.exists(path):
variables = {
- "all_hosts" : [],
- "ipaddresses" : {},
+ "all_hosts" : [],
+ "ipaddresses" : {},
+ "extra_host_conf" : { "alias" : [] },
}
execfile(path, variables, variables)
hosts = {}
@@ -32,7 +33,13 @@ def read_configuration_file(filename):
hostname = parts[0]
tags = parts[1:]
ipaddress = variables["ipaddresses"].get(hostname)
- hosts[hostname] = (ipaddress, tags)
+ aliases = host_extra_conf(hostname,
variables["extra_host_conf"]["alias"])
+ if len(aliases) > 0:
+ alias = aliases[0]
+ else:
+ alias = None
+ hosts[hostname] = (alias, ipaddress, tags)
+
return hosts
else:
return {}
@@ -40,10 +47,13 @@ def read_configuration_file(filename):
def write_configuration_file(filename, hosts):
all_hosts = []
ipaddresses = {}
+ aliases = []
hostnames = hosts.keys()
hostnames.sort()
for hostname in hostnames:
- ipaddress, tags = hosts[hostname]
+ alias, ipaddress, tags = hosts[hostname]
+ if alias:
+ aliases.append((alias, [hostname]))
all_hosts.append("|".join([hostname] + tags))
if ipaddress:
ipaddresses[hostname] = ipaddress
@@ -54,12 +64,24 @@ def write_configuration_file(filename, hosts):
if len(all_hosts) > 0:
out.write("all_hosts += ")
out.write(pprint.pformat(all_hosts))
+ if len(aliases) > 0:
+ out.write("\n\nif 'alias' not in extra_host_conf:\n
extra_host_conf['alias'] = []\n")
+ out.write("\nextra_host_conf['alias'] += ")
+ out.write(pprint.pformat(aliases))
if len(ipaddresses) > 0:
out.write("\n\nipaddresses.update(")
out.write(pprint.pformat(ipaddresses))
out.write(")")
out.write("\n")
+# This is a dummy implementation which works without tags
+# and implements only a special case of Check_MK's real logic.
+def host_extra_conf(hostname, conflist):
+ for value, hostlist in conflist:
+ if hostname in hostlist:
+ return [value]
+ return []
+
def check_filename():
filename = html.var("filename")
if not filename:
@@ -106,18 +128,22 @@ def page_index(h):
# Show table of hosts in this file
html.write("<table class=services>\n")
- html.write("<tr><th>Hostname</th><th>IP
Address</th><th>Tags</th><th>Actions</th></tr>\n")
+
html.write("<tr><th>Hostname</th><th>Alias</th>"
+ "<th>IP
Address</th><th>Tags</th><th>Actions</th></tr>\n")
odd = "even"
hostnames = hosts.keys()
hostnames.sort()
for hostname in hostnames:
- ipaddress, tags = hosts[hostname]
+ alias, ipaddress, tags = hosts[hostname]
edit_url = "webconf_edithost.py?filename=%s&host=%s" % (filename,
hostname)
clone_url = "webconf_edithost.py?filename=%s&clone=%s" % (filename,
hostname)
delete_url = "webconf.py?filename=%s&_delete=%s" % (filename,
hostname)
odd = odd == "odd" and "even" or "odd"
- html.write("<tr class=\"data
%s0\"><td>%s</td>" % (odd, hostname))
+
+ html.write('<tr class="data %s0"><td><a
href="%s">%s</a></td>' %
+ (odd, edit_url, hostname))
+ html.write("<td>%s</td>" % (alias and alias or
""))
if not ipaddress:
try:
ip = socket.gethostbyname(hostname)
@@ -132,8 +158,8 @@ def page_index(h):
html.buttonlink(delete_url, "Delete")
html.write("</td>")
html.write("</tr>\n")
- html.write("</table>\n")
+ html.write("</table>\n")
html.footer()
@@ -148,17 +174,16 @@ def page_edithost(h):
clonename = html.var("clone")
if clonename:
title = "Create clone of %s" % clonename
- ipaddress, tags = hosts[clonename]
+ alias, ipaddress, tags = hosts[clonename]
mode = "clone"
elif hostname in hosts:
title = "Edit host " + hostname
- ipaddress, tags = hosts.get(hostname)
+ alias, ipaddress, tags = hosts.get(hostname)
mode = "edit"
else:
title = "Create new host"
- ipaddress, tags = None, []
+ alias, ipaddress, tags = None, None, []
mode = "new"
-
html.header(title)
html.begin_context_buttons()
@@ -169,9 +194,14 @@ def page_edithost(h):
# Form submitted
if html.var("save") and html.check_transaction():
try:
+ alias = html.var("alias")
+ if not alias:
+ alias = None # make sure no alias is set - not an empty one
+
ipaddress = html.var("ipaddress")
if not ipaddress:
- ipaddress = None
+ ipaddress = None # make sure no IP address is set
+
tags = []
for tagno, (tagname, taglist) in enumerate(config.host_tags):
value = html.var("tag_%d" % tagno)
@@ -188,7 +218,7 @@ def page_edithost(h):
elif not re.match("^[a-zA-Z0-9-_.]+$", hostname):
raise MKUserError("name", "Invalid host name: must
contain only characters, digits, dash, underscore and dot.")
- hosts[hostname] = (ipaddress, tags)
+ hosts[hostname] = (alias, ipaddress, tags)
write_configuration_file(filename, hosts)
# html.set_browser_redirect(1, "webconf.py?filename=%s" %
htmllib.urlencode(filename))
html.message("Saved changes.")
@@ -200,18 +230,27 @@ def page_edithost(h):
html.begin_form("edithost")
- html.write("<table class=form>\n")
+ html.write('<table class=form>\n')
+
+ # host name
html.write("<tr><td class=legend>Hostname</td><td
class=content>")
if hostname and mode == "edit":
html.write(hostname)
else:
html.text_input("name")
html.write("</td></tr>\n")
+
+ # alias
+ html.write("<tr><td
class=legend>Alias<br><i>(optional</i></td><td
class=content>")
+ html.text_input("alias", alias)
+ html.write("</td></tr>\n")
+
+ # IP address
html.write("<tr><td class=legend>IP-Address<br>"
"<i>Leave empty for automatic<br>"
"IP address lookup via DNS</td><td class=content>")
html.text_input("ipaddress", ipaddress)
- html.write("</td>\n")
+ html.write("</td></tr>\n")
# Host tags
found_tags = []
@@ -313,8 +352,6 @@ def page_services(h):
html.write("</td></tr>\n")
html.write("</table>\n")
html.end_form()
-
-
html.footer()