From: Frank Brehm Date: Sun, 12 Dec 2010 21:43:31 +0000 (+0000) Subject: Speziellen Option parser für Nagios Konfiguration dazu X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=5dba04bbeb950c2cdcb84f7770f21d5f66ad099f;p=my-stuff%2Fnagios.git Speziellen Option parser für Nagios Konfiguration dazu git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@150 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- diff --git a/bin/check_nagios_config.py b/bin/check_nagios_config.py index f2dbee7..6a33c0b 100755 --- a/bin/check_nagios_config.py +++ b/bin/check_nagios_config.py @@ -4,35 +4,30 @@ import sys import pprint from nagios.config import NagiosConfig, NagiosConfigError -from fbrehm.common.getopt import BaseOptParser +from nagios.cfg.getopts import NagiosConfigOptionParser +#from fbrehm.common.getopt import BaseOptParser #print "Modul-Suchpfade:" #for p in sys.path: # print " -", p -#print "Namen aus dem NagiosConfig-Objekt:" -#for name in dir(NagiosConfig): -# print " -", name -#print "" - -opt_parser = BaseOptParser( - version = '0.0.2', - prog = 'check_nagios_config.py', +opt_parser = NagiosConfigOptionParser( + version = '0.0.3', + prog = 'check_nagios_config.py', description = u'Überprüft die Syntax einer Nagios-Konfiguration.', - usage = u'%s [Optionen] [
]' + usage = u'%s [Optionen] [
]' ) -opt_parser.setBaseOptionHelp( 'test', u'Simuliert nur verändernde Aktionen, wenn gesetzt' ) -opt_parser.setBaseOptionHelp( 'verbose', u'Setzt das Ausführlichkeitsniveau der Debug-Meldungen.' ) -opt_parser.setBaseOptionHelp( 'version', u'Gibt die Versionsnummer des Programms aus und beendet sich' ) -opt_parser.setBaseOptionHelp( 'help', u'Stellt diese Hilfe dar und beendet sich' ) +opt_parser.setBaseOptionHelp( 'test', u'Simuliert nur verändernde Aktionen, wenn gesetzt' ) +opt_parser.setBaseOptionHelp( 'verbose', u'Setzt das Ausführlichkeitsniveau der Debug-Meldungen.' ) +opt_parser.setBaseOptionHelp( 'version', u'Gibt die Versionsnummer des Programms aus und beendet sich' ) +opt_parser.setBaseOptionHelp( 'help', u'Stellt diese Hilfe dar und beendet sich' ) +opt_parser.setBaseOptionHelp( 'cfg', u'Pfad zur Nagios-Hauptkonfigurations-Datei (Vorgabe: %default)' ) +opt_parser.setBaseOptionHelp( 'from-cache', u'Soll die Objektkonfiguration vom Objektcache anstatt von den normalen Konfigurationsdateien gelesen werden?' ) opt_parser.getOpts() verbose = opt_parser.options.verbose - -file = None -if len(opt_parser.args): - file = opt_parser.args[0] +file = opt_parser.options.config_file try: nagios_conf = NagiosConfig( verbose = verbose, new_file = file ) @@ -41,10 +36,6 @@ except NagiosConfigError as e: print >> sys.stderr, e sys.exit(2) - -#print "Nagios-Objekt: " + repr( nagios_conf ) - -#print nagios_conf.dump_config() -nagios_conf.read_objects() +nagios_conf.read_objects( from_cache = opt_parser.options.from_cache ) # vim: fileencoding=utf-8 filetype=python ts=4 expandtab diff --git a/bin/fbrehm/common/getopt.py b/bin/fbrehm/common/getopt.py index 9671e4a..b6772e4 100755 --- a/bin/fbrehm/common/getopt.py +++ b/bin/fbrehm/common/getopt.py @@ -52,6 +52,7 @@ class BaseOptParser(object): @type test_option: boolean @return: None ''' + self.prog = prog self.version = version self.description = description diff --git a/bin/nagios/cfg/getopts.py b/bin/nagios/cfg/getopts.py new file mode 100755 index 0000000..c8891b9 --- /dev/null +++ b/bin/nagios/cfg/getopts.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +''' +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: (c) 2010-2011 by Frank Brehm, Berlin +@license: GPL3 +@version: 0.0.1 +@summary: An option parser for Nagios configuration things. +''' + +__author__ = 'Frank Brehm ' +__contact__ = 'frank@brehm-online.com' +__copyright__ = '(C) 2010 by Frank Brehm, Berlin' +__version__ = '0.0.1' +__license__ = 'GPL3' + +from fbrehm.common.getopt import BaseOptParser + +class NagiosConfigOptionParser(BaseOptParser): + '''This is class derived form BaseOptParser to parse + command line options for Nagios python scripts. + ''' + + #--------------------------------------------------------------------- + def __init__( self, prog = '%prog', + version = None, + description = '', + usage = 'Usage: %s [options]' + ): + ''' + Costructor. + @param prog: The name of the calling process (e.g. sys.argv[0]) + @type prog: str + @param version: The version string to use + @type version: str + @param description: The Description the process should use + @type description: str + @param usage: An usage string fro the help screen, must have a '%s' for the program name + @type usage: str + @return: None + ''' + + super( NagiosConfigOptionParser, self ).__init__( + prog = prog, + version = version, + description = description, + usage = usage, + test_option = False + ) + + self.addOption( + '--config-file', + '--cfg', + '-c', + type = 'string', + default = '/etc/nagios/nagios.cfg', + dest = 'config_file', + metavar = "FILE", + help = 'Path to the Nagios main configuration file. (Default: %default)', + ) + + self.addOption( + '--read-from-object-cache', + '--from-cache', + '-C', + default = False, + action = 'store_true', + dest = 'from_cache', + help = 'Should the object configuration read from object cache file instead from the normal configuration files.', + ) + + #--------------------------------------------------------------------- + +# vim: fileencoding=utf-8 filetype=python ts=4 expandtab diff --git a/bin/nagios/config.py b/bin/nagios/config.py index a807c91..f52fc75 100644 --- a/bin/nagios/config.py +++ b/bin/nagios/config.py @@ -244,12 +244,17 @@ class NagiosConfig(LoggingObject): return res #------------------------------------------------------ - def read_objects( self ): + def read_objects( self, from_cache = False ): """Reads the object configuration files from main configuration into self.objects + @param from_cache: Should the object configuration read from object cache file instead from the normal configuration files + @type from_cache: boolean @return: None @rtype: None """ + if from_cache: + return self.read_object_cache_file() + self.objects = {} self.objects_read = {} files = set([]) @@ -299,6 +304,16 @@ class NagiosConfig(LoggingObject): return + #------------------------------------------------------ + def read_object_cache_file( self ): + '''Reads objects from object cache file. + @return: None + @rtype: None + ''' + + self.logger.warning( "read_object_cache_file() not implemented." ) + sys.exit(2) + #------------------------------------------------------ def read_objectfile( self, file_name ): """Reads a particular object definition file and saves the structures in self.read_objects.