From 1ae62017ffc2a0783567736573d72b7d16729770 Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Mon, 3 Jan 2011 21:17:34 -0600 Subject: =?UTF-8?q?NagiosGen:=20Dependency=20support=20(from=20St=C3=A9pha?= =?UTF-8?q?ne=20Graber=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From the patch description: As we have several hundred machines in Bcfg2 at work and are using NagiosGen to generate our Nagios configuration, the dependency tree was quite a mess. The following patch adds support for a 'parents.xml' file in the NagiosGen directory. This one contains a basic dependency tree like this: The plugin will then use this file to add 'parents' tag to nagiosgen.cfg when possible. It's being added when generating nagiosgen.cfg instead of when generating the individual host entries for two reasons: - To avoid having to wait for a run of bcfg2 on all machines when changing parents.xml - To also support dynamic parents for manually added entries in NagiosGen (example: network equipement). --- src/lib/Server/Plugins/NagiosGen.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lib/Server/Plugins/NagiosGen.py b/src/lib/Server/Plugins/NagiosGen.py index cd6f843fb..14277b63d 100644 --- a/src/lib/Server/Plugins/NagiosGen.py +++ b/src/lib/Server/Plugins/NagiosGen.py @@ -2,6 +2,7 @@ import glob import logging +import lxml.etree import os import re import socket @@ -45,6 +46,18 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin, 'type':'file', 'perms':'0440'} + def getparents(self, hostname): + """Return parents for given hostname.""" + depends=[] + if not os.path.isfile('%s/parents.xml' % (self.data)): + return depends + + tree = lxml.etree.parse('%s/parents.xml' % (self.data)) + for entry in tree.findall('.//Depend'): + if entry.attrib['name'] == hostname: + depends.append(entry.attrib['on']) + return depends + def createhostconfig(self, entry, metadata): """Build host specific configuration file.""" host_address = socket.gethostbyname(metadata.hostname) @@ -94,8 +107,30 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin, group_data = "" for host in host_configs: hostfile = open(host, 'r') - host_data += hostfile.read() + hostname=host.split('/')[-1].replace('-host.cfg','') + parents=self.getparents(hostname) + if parents: + hostlines = hostfile.readlines() + else: + hostdata = hostfile.read() hostfile.close() + + if parents: + hostdata='' + addparents=True + for line in hostlines: + line=line.replace('\n','') + if 'parents' in line: + line+=','+','.join(parents) + addparents=False + if '}' in line: + line='' + hostdata+="%s\n" % line + if addparents: + hostdata+=" parents %s\n" % ','.join(parents) + hostdata+="}\n" + + host_data += hostdata for group in group_configs: group_name = re.sub("(-group.cfg|.*/(?=[^/]+))", "", group) if host_data.find(group_name) != -1: -- cgit v1.2.3-1-g7c22