summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2011-01-03 21:17:34 -0600
committerNarayan Desai <desai@mcs.anl.gov>2011-01-03 21:17:34 -0600
commit1ae62017ffc2a0783567736573d72b7d16729770 (patch)
tree4b8d5fb5518a7f7b1e8c2df8c0874c4ccd0b65fa /src
parent71bb583ac04ff1c52e03d69a91bbb508783566f8 (diff)
downloadbcfg2-1ae62017ffc2a0783567736573d72b7d16729770.tar.gz
bcfg2-1ae62017ffc2a0783567736573d72b7d16729770.tar.bz2
bcfg2-1ae62017ffc2a0783567736573d72b7d16729770.zip
NagiosGen: Dependency support (from Stéphane Graber <stgraber@ubuntu.com>)
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: <Depends> <Depend name='something' on='something-else'> </Depends> 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).
Diffstat (limited to 'src')
-rw-r--r--src/lib/Server/Plugins/NagiosGen.py37
1 files changed, 36 insertions, 1 deletions
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: