summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2008-10-08 12:54:05 +0000
committerNarayan Desai <desai@mcs.anl.gov>2008-10-08 12:54:05 +0000
commit8311349047d3dc28dcaefba9003b60b663ed3939 (patch)
treed2478f09bb1ddd987c319d7d5f45b93c6cea5006 /src/lib
parentcb5e67f9134fa6445c1360fb4293259d1c41a0c9 (diff)
downloadbcfg2-8311349047d3dc28dcaefba9003b60b663ed3939.tar.gz
bcfg2-8311349047d3dc28dcaefba9003b60b663ed3939.tar.bz2
bcfg2-8311349047d3dc28dcaefba9003b60b663ed3939.zip
Initial import of NagioGen
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4928 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Server/Plugins/NagiosGen.py78
-rw-r--r--src/lib/Server/Plugins/__init__.py2
2 files changed, 79 insertions, 1 deletions
diff --git a/src/lib/Server/Plugins/NagiosGen.py b/src/lib/Server/Plugins/NagiosGen.py
new file mode 100644
index 000000000..30ac93984
--- /dev/null
+++ b/src/lib/Server/Plugins/NagiosGen.py
@@ -0,0 +1,78 @@
+'''This module implements a Nagios configuration generator'''
+
+import re, os, glob, socket, logging
+import Bcfg2.Server.Plugin
+
+LOGGER = logging.getLogger('Bcfg2.Plugins.NagiosGen')
+class NagiosGen(Bcfg2.Server.Plugin.Plugin):
+ '''NagiosGen is a Bcfg2 plugin that dynamically generates
+ Nagios configuration file based on Bcfg2 data.'''
+ __name__ = 'NagiosGen'
+ __version__ = '0.6'
+ __author__ = 'bcfg-dev@mcs.anl.gov'
+
+ def __init__(self, core, datastore):
+ Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
+ self.Entries = {'ConfigFile':
+ {'/etc/nagiosgen.status' : self.createhostconfig,
+ '/etc/nagios/nagiosgen.cfg': self.createserverconfig}}
+
+ self.client_attrib = {'encoding': 'ascii', 'owner':'root', \
+ 'group':'root', 'perms':'0400'}
+ self.server_attrib = {'encoding': 'ascii', 'owner':'nagios', \
+ 'group':'nagios', 'perms':'0440'}
+
+ def createhostconfig(self, entry, metadata):
+ '''Build host specific configuration file'''
+ host_address = socket.gethostbyname(metadata.hostname)
+ host_groups = filter(lambda x: os.path.isfile('%s/%s-group.cfg' % \
+ (self.data, x)), metadata.groups)
+ host_config = \
+ 'define host{\n\
+ use default\n\
+ host_name %s\n\
+ alias %s\n\
+ address %s\n' % \
+ (metadata.hostname, metadata.hostname, host_address )
+ if host_groups:
+ host_config += ' hostgroups %s\n' % (",".join(host_groups))
+ host_config += ' }\n'
+ entry.text = host_config
+ [entry.attrib.__setitem__(key, value) for \
+ (key, value) in self.client_attrib.iteritems()]
+ try:
+ fileh = open( "%s/%s-host.cfg" % \
+ (self.data, metadata.hostname), 'w' )
+ fileh.write(host_config)
+ fileh.close()
+ except OSError, ioerr:
+ LOGGER.error("Failed to write %s/%s-host.cfg" % \
+ (self.data, metadata.hostname))
+ LOGGER.error(ioerr)
+
+ def createserverconfig(self, entry, metadata):
+ '''Build monolithic server configuration file'''
+ host_configs = glob.glob('%s/*-host.cfg' % self.data)
+ group_configs = glob.glob('%s/*-group.cfg' % self.data)
+ host_data = ""
+ group_data = ""
+ for host in host_configs:
+ hostfile = open( host, 'r' )
+ host_data += hostfile.read()
+ hostfile.close()
+ for group in group_configs:
+ group_name = re.sub("(-group.cfg|.*/(?=[^/]+))", "", group)
+ if host_data.find(group_name) != -1:
+ groupfile = open( group, 'r' )
+ group_data += groupfile.read()
+ groupfile.close()
+ entry.text = group_data + host_data
+ [entry.attrib.__setitem__(key, value) for \
+ (key, value) in self.server_attrib.iteritems()]
+ try:
+ fileh = open( "%s/nagiosgen.cfg" % (self.data), 'w' )
+ fileh.write(group_data + host_data)
+ fileh.close()
+ except OSError, ioerr:
+ LOGGER.error("Failed to write %s/nagiosgen.cfg" % (self.data))
+ LOGGER.error(ioerr)
diff --git a/src/lib/Server/Plugins/__init__.py b/src/lib/Server/Plugins/__init__.py
index 00c161d64..9707fbfeb 100644
--- a/src/lib/Server/Plugins/__init__.py
+++ b/src/lib/Server/Plugins/__init__.py
@@ -1,6 +1,6 @@
'''imports for Bcfg2.Server.Plugins'''
__revision__ = '$Revision$'
-__all__ = ['Account', 'Base', 'Bundler', 'Cfg', 'Hostbase', 'Metadata',
+__all__ = ['Account', 'Base', 'Bundler', 'Cfg', 'Hostbase', 'Metadata', 'NagiosGen',
'Pkgmgr', 'Rules', 'SSHbase', 'Statistics', 'Svcmgr', 'TCheetah',
'SGenshi', 'TGenshi', 'Vhost']