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] [<Main Nagios Konfig Datei>]'
+ usage = u'%s [Optionen] [<Main Nagios Konfig Datei>]'
)
-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 )
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
@type test_option: boolean
@return: None
'''
+
self.prog = prog
self.version = version
self.description = description
--- /dev/null
+#!/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 <frank@brehm-online.com>'
+__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
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([])
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.