From: Frank Brehm Date: Thu, 25 Nov 2010 22:57:26 +0000 (+0000) Subject: Services dazu X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=845e03b155ae5a08dd5bf0e889f0ce75d6d204c0;p=my-stuff%2Fnagios.git Services dazu git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@133 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- diff --git a/bin/nagios/config.py b/bin/nagios/config.py index d972102..a2ad464 100644 --- a/bin/nagios/config.py +++ b/bin/nagios/config.py @@ -12,6 +12,7 @@ import logging import pprint import nagios.object.host import nagios.object.hostgroup +import nagios.object.service class NagiosConfigError(Exception): """Base class for exceptions in this module.""" @@ -58,6 +59,7 @@ class NagiosConfig(object): self.conf = {} self.objects = {} self.config_objects = {} + self.objects_read = {} # Logging-Setup self.logger = logging.getLogger('nagiosConfig') @@ -210,6 +212,7 @@ class NagiosConfig(object): "Liest die in der Konfiguration engegebenen Objekt-Kofigurations-Dateien ein" self.objects = {} + self.objects_read = {} files = set([]) dirs = set([]) @@ -250,7 +253,7 @@ class NagiosConfig(object): self.read_objectfile(file_name) pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 ) - self.logger.debug( "Gelesene Objekte:\n{0}".format( pp.pformat( self.objects ) ) ) + self.logger.debug( "Gelesene Objekte:\n{0}".format( pp.pformat( self.objects_read ) ) ) return @@ -292,10 +295,9 @@ class NagiosConfig(object): in_block = False - if not object_type in self.objects: - self.objects[object_type] = {} + if not object_type in self.objects_read: + self.objects_read[object_type] = [] - #self.objects[object_type].append( cur_object ) # Verifying the object: if object_type == 'host': @@ -304,15 +306,9 @@ class NagiosConfig(object): ( file_name, rownum ) = cur_object['__object_definition__'] if identifier is None: - self.logger.warn( "Couldn't verify host object structure of {0!r}({1})".format( + self.logger.warn( "Couldn't verify host object identifier of {0!r}({1})".format( file_name, rownum ) ) - else: - if identifier in self.objects['host']: - msg = "Host object {0!r} even exists, ".format( identifier ) - msg = msg + "new definition in {0!r}({1}) will not accepted.".format( file_name, rownum ) - self.logger.warn( msg ) - else: - self.objects['host'][identifier] = struct + self.objects_read[object_type].append( struct ) if object_type == 'hostgroup': @@ -321,15 +317,20 @@ class NagiosConfig(object): ( file_name, rownum ) = cur_object['__object_definition__'] if identifier is None: - self.logger.warn( "Couldn't verify hostgroup object structure of {0!r}({1})".format( + self.logger.warn( "Couldn't verify hostgroup object identifier of {0!r}({1})".format( file_name, rownum ) ) - else: - if identifier in self.objects['hostgroup']: - msg = "Hostgroup object {0!r} even exists, ".format( identifier ) - msg = msg + "new definition in {0!r}({1}) will not accepted.".format( file_name, rownum ) - self.logger.warn( msg ) - else: - self.objects['hostgroup'][identifier] = struct + self.objects_read[object_type].append( struct ) + + if object_type == 'service': + + ( identifier, struct ) = nagios.object.service.verify( cur_object, self.logger ) + self.logger.debug( "Found service object structure for {0!r}: {1}".format( identifier, pp.pformat( struct ) ) ) + ( file_name, rownum ) = cur_object['__object_definition__'] + + if identifier is None: + self.logger.warn( "Couldn't verify service object identifier of {0!r}({1})".format( + file_name, rownum ) ) + self.objects_read[object_type].append( struct ) cur_object = {} self.logger.debug( "Row {0} in {1}: finishing block of type {2!r}".format( row_num, file_name, object_type ) ) diff --git a/bin/nagios/object/service.py b/bin/nagios/object/service.py new file mode 100755 index 0000000..4ccf0e6 --- /dev/null +++ b/bin/nagios/object/service.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# $Id$ +# $URL$ + +import re +import pprint + +from nagios.object.verify import NagiosVerifyError, NagiosObjectVerifier + +#----------------------------------------------------------------------- +def verify( definition, logger ): + "Verifying a given definition block of a Nagion service" + + res = {} + identifier = None + + """ +define service{ + host_name host_names + hostgroup_name hostgroup_names + service_description service_description + display_name display_name + servicegroups servicegroup_names + is_volatile [0/1] + check_command command_name + initial_state [o,w,u,c] + max_check_attempts # + check_interval # + retry_interval # + active_checks_enabled [0/1] + passive_checks_enabled [0/1] + check_period timeperiod_name + obsess_over_service [0/1] + check_freshness [0/1] + freshness_threshold # + event_handler command_name + event_handler_enabled [0/1] + low_flap_threshold # + high_flap_threshold # + flap_detection_enabled [0/1] + flap_detection_options [o,w,c,u] + process_perf_data [0/1] + retain_status_information [0/1] + retain_nonstatus_information [0/1] + notification_interval # + first_notification_delay # + notification_period timeperiod_name + notification_options [w,u,c,r,f,s] + notifications_enabled [0/1] + contacts contacts + contact_groups contact_groups + stalking_options [o,w,u,c] + notes note_string + notes_url url + action_url url + icon_image image_file + icon_image_alt alt_string + use templates + register [0/1] + name object_name +} +""" + + valid_keys = set( [ + 'host_name', 'hostgroup_name', 'service_description', 'display_name', 'servicegroups', + 'is_volatile', 'check_command', 'initial_state', 'max_check_attempts', 'check_interval', + 'retry_interval', 'active_checks_enabled', 'passive_checks_enabled', 'check_period', + 'obsess_over_service', 'check_freshness', 'freshness_threshold', 'event_handler', + 'event_handler_enabled', 'low_flap_threshold', 'high_flap_threshold', 'flap_detection_enabled', + 'flap_detection_options', 'process_perf_data', 'retain_status_information', + 'notification_interval', 'first_notification_delay', 'notification_period', + 'notification_options', 'notifications_enabled', 'contacts', 'contact_groups', + 'stalking_options', 'notes', 'notes_url', 'action_url', 'icon_image', 'icon_image_alt', + 'use', 'register', 'name' + ] ) + + pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 ) + logger.debug( "Verifying service object structure:\n{0}".format( pp.pformat( definition ) ) ) + + for key in definition: + if key == '__object_definition__': + continue + if not key in valid_keys: + logger.warn( "Invalid Property {0!r} for service definition in {1}({2}).".format( + key, definition[key][1], definition[key][2] ) ) + + verifier = NagiosObjectVerifier( logger = logger ) + + # Einfache String-Eigenschaften + for key in ( 'name', 'service_description', 'display_name', 'check_command', 'check_period', 'event_handler', 'notification_period', + 'notes', 'notes_url', 'action_url', 'icon_image', 'icon_image_alt', ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for service definition in {1}({2}).".format( key, definition[key][1], definition[key][2] ) ) + else: + args = dict( file = definition[key][1], row = definition[key][2] ) + res[key] = verifier.verify_property( definition[key][0], 'string', args ) + except NagiosVerifyError as e: + logger.warn( "Property error for service definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + # Array-String-Eigenschaften + for key in ( 'use', 'host_name', 'hostgroup_name', 'servicegroups', 'contacts', 'contact_groups' ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for service definition in {1}({2}).".format( key, definition[key][1], definition[key][2] ) ) + else: + args = dict( file = definition[key][1], row = definition[key][2] ) + res[key] = verifier.verify_property( definition[key][0], 'array', args ) + except NagiosVerifyError as e: + logger.warn( "Property error for service definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + for key in ( 'initial_state', 'flap_detection_options', 'stalking_options' ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for service definition in {1}({2}).".format( key, definition[key][1], definition[key][2] ) ) + else: + args = dict( file = definition[key][1], row = definition[key][2], valid_values = set( [ 'o', 'w', 'u', 'c' ] ) ) + res[key] = verifier.verify_property( definition[key][0], 'set', args ) + except NagiosVerifyError as e: + logger.warn( "Property error for service definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + for key in ( 'notification_options' ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for service definition in {1}({2}).".format( key, definition[key][1], definition[key][2] ) ) + else: + args = dict( file = definition[key][1], row = definition[key][2], valid_values = set( [ 'w', 'u', 'c', 'r', 'f', 's' ] ) ) + res[key] = verifier.verify_property( definition[key][0], 'set', args ) + except NagiosVerifyError as e: + logger.warn( "Property error for service definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + for key in ( 'max_check_attempts', 'check_interval', 'retry_interval', 'freshness_threshold', 'low_flap_threshold', + 'high_flap_threshold', 'notification_interval', 'first_notification_delay', ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for service definition in {1}({2}).".format( key, definition[key][1], definition[key][2] ) ) + else: + args = dict( file = definition[key][1], row = definition[key][2] ) + res[key] = verifier.verify_property( definition[key][0], 'int', args ) + except NagiosVerifyError as e: + logger.warn( "Property error for service definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + for key in ( 'register', 'is_volatile', 'active_checks_enabled', 'passive_checks_enabled', 'obsess_over_host', 'check_freshness', + 'event_handler_enabled', 'flap_detection_enabled', 'process_perf_data', 'retain_status_information', + 'retain_nonstatus_information', 'notifications_enabled', ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for service definition in {1}({2}).".format( key, definition[key][1], definition[key][2] ) ) + else: + args = dict( file = definition[key][1], row = definition[key][2] ) + res[key] = verifier.verify_property( definition[key][0], 'bool', args ) + except NagiosVerifyError as e: + logger.warn( "Property error for service definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + if 'service_description' in res: + identifier = res['service_description'] + elif 'name' in res: + identifier = res['name'] + + return ( identifier, res ) + +# vim: fileencoding=utf-8 filetype=python ts=4 expandtab