summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2011-05-06 08:13:20 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2011-05-06 08:13:20 -0400
commita4de0e44bd10761f5ec7d948fd0a3824720a90f2 (patch)
treedf582678a12f77a29fede1965727fb27c94ecbe1 /tools
parente38ea05100ddf670898f1d11ffc09b9d44435f27 (diff)
downloadbcfg2-a4de0e44bd10761f5ec7d948fd0a3824720a90f2.tar.gz
bcfg2-a4de0e44bd10761f5ec7d948fd0a3824720a90f2.tar.bz2
bcfg2-a4de0e44bd10761f5ec7d948fd0a3824720a90f2.zip
Rewrote NagiosGen config to use NagiosGen/config.xml, which
understands <Group> and <Client> tags, rather than the client-specific Properties/NagiosGen.xml and the group-specific but limited NagiosGen/parents.xml. Includes schema and bcfg2-lint updates necessary. Wrote conversion tool, nagiosgen-convert.py, which converts everything but the <default/> tag in the old NagiosGen.xml, which cannot be reasonably converted to StructFile format. Also removed a _lot_ of string modification in NagiosGen.py, which should make it a fair bit faster.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/nagiosgen-convert.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/tools/nagiosgen-convert.py b/tools/nagiosgen-convert.py
new file mode 100755
index 000000000..2c2142735
--- /dev/null
+++ b/tools/nagiosgen-convert.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+
+import os
+import sys
+import lxml.etree
+
+import Bcfg2.Options
+
+def main():
+ opts = {'repo': Bcfg2.Options.SERVER_REPOSITORY}
+ setup = Bcfg2.Options.OptionParser(opts)
+ setup.parse(sys.argv[1:])
+ repo = setup['repo']
+ oldconfigfile = os.path.join(repo, 'Properties', 'NagiosGen.xml')
+ newconfigpath = os.path.join(repo, 'NagiosGen')
+ newconfigfile = os.path.join(newconfigpath, 'config.xml')
+ parentsfile = os.path.join(newconfigpath, 'parents.xml')
+
+ if not os.path.exists(oldconfigfile):
+ print("%s does not exist, nothing to do" % oldconfigfile)
+ return 1
+
+ if not os.path.exists(newconfigpath):
+ print("%s does not exist, cannot write %s" %
+ (newconfigpath, newconfigfile))
+ return 2
+
+ newconfig = lxml.etree.XML("<NagiosGen/>")
+
+ oldconfig = lxml.etree.parse(oldconfigfile)
+ for host in oldconfig.getroot().getchildren():
+ if host.tag == lxml.etree.Comment:
+ # skip comments
+ continue
+
+ if host.tag == 'default':
+ print("default tag will not be converted; use a suitable Group tag instead")
+ continue
+
+ newhost = lxml.etree.Element("Client", name=host.tag)
+ for opt in host:
+ newopt = lxml.etree.Element("Option", name=opt.tag)
+ newopt.text = opt.text
+ newhost.append(newopt)
+ newconfig.append(newhost)
+
+ # parse the parents config, if it exists
+ if os.path.exists(parentsfile):
+ parentsconfig = lxml.etree.parse(parentsfile)
+ for el in parentsconfig.xpath("//Depend"):
+ newhost = newconfig.find("Client[@name='%s']" % el.get("name"))
+ if newhost is not None:
+ newparents = newhost.find("Option[@name='parents']")
+ if newparents is not None:
+ newparents.text += "," + el.get("on")
+ else:
+ newparents = lxml.etree.Element("Option", name="parents")
+ newparents.text = el.get("on")
+ newhost.append(newparents)
+ else:
+ newhost = lxml.etree.Element("Client", name=el.get("name"))
+ newparents = lxml.etree.Element("Option", name="parents")
+ newparents.text = el.get("on")
+ newhost.append(newparents)
+ newconfig.append(newhost)
+
+ try:
+ open(newconfigfile, 'w').write(lxml.etree.tostring(newconfig,
+ pretty_print=True))
+ print("%s written" % newconfigfile)
+ except IOError:
+ print("Failed to write %s" % newconfigfile)
+
+if __name__ == '__main__':
+ sys.exit(main())