diff options
Diffstat (limited to 'src/lib/Server/Plugins/NagiosGen.py')
-rw-r--r-- | src/lib/Server/Plugins/NagiosGen.py | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/src/lib/Server/Plugins/NagiosGen.py b/src/lib/Server/Plugins/NagiosGen.py index 8ea0a1547..82326b760 100644 --- a/src/lib/Server/Plugins/NagiosGen.py +++ b/src/lib/Server/Plugins/NagiosGen.py @@ -1,6 +1,11 @@ '''This module implements a Nagios configuration generator''' -import re, os, glob, socket, logging +import glob +import logging +import os +import re +import socket + import Bcfg2.Server.Plugin LOGGER = logging.getLogger('Bcfg2.Plugins.NagiosGen') @@ -20,16 +25,16 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin, name = 'NagiosGen' __version__ = '0.6' __author__ = 'bcfg-dev@mcs.anl.gov' - + def __init__(self, core, datastore): Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore) Bcfg2.Server.Plugin.Generator.__init__(self) 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'} + 'group':'root', 'perms':'0400'} self.server_attrib = {'encoding': 'ascii', 'owner':'nagios', \ 'group':'nagios', 'perms':'0440'} @@ -39,7 +44,7 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin, host_groups = [grp for grp in metadata.groups if \ os.path.isfile('%s/%s-group.cfg' % (self.data, grp))] host_config = host_config_fmt % \ - (metadata.hostname, metadata.hostname, host_address ) + (metadata.hostname, metadata.hostname, host_address) if host_groups: host_config += ' hostgroups %s\n' % (",".join(host_groups)) @@ -48,9 +53,9 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin, 'NagiosGen.xml' in metadata.Properties and \ metadata.Properties['NagiosGen.xml'].data.find(metadata.hostname) \ is not None: - directives = list (metadata.Properties['NagiosGen.xml'].data.find(metadata.hostname)) + directives = list(metadata.Properties['NagiosGen.xml'].data.find(metadata.hostname)) for item in directives: - host_config += ' %-32s %s\n' % ( item.tag, item.text ) + host_config += ' %-32s %s\n' % (item.tag, item.text) else: host_config += ' use default\n' @@ -58,17 +63,17 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin, host_config += '}\n' entry.text = host_config [entry.attrib.__setitem__(key, value) for \ - (key, value) in self.client_attrib.iteritems()] + (key, value) in self.client_attrib.iteritems()] try: - fileh = open( "%s/%s-host.cfg" % \ - (self.data, metadata.hostname), 'w' ) + 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, _): '''Build monolithic server configuration file''' host_configs = glob.glob('%s/*-host.cfg' % self.data) @@ -76,20 +81,20 @@ class NagiosGen(Bcfg2.Server.Plugin.Plugin, host_data = "" group_data = "" for host in host_configs: - hostfile = open( host, 'r' ) + 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' ) + 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()] + (key, value) in self.server_attrib.iteritems()] try: - fileh = open( "%s/nagiosgen.cfg" % (self.data), 'w' ) + fileh = open("%s/nagiosgen.cfg" % (self.data), 'w') fileh.write(group_data + host_data) fileh.close() except OSError, ioerr: |