From: Frank Brehm Date: Thu, 25 Nov 2010 22:00:55 +0000 (+0000) Subject: Hostgruppen dazu X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=07dd82633f8bb984309d215531efd476d34e5715;p=my-stuff%2Fnagios.git Hostgruppen dazu git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@132 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- diff --git a/bin/nagios/config.py b/bin/nagios/config.py index 71ee8db..d972102 100644 --- a/bin/nagios/config.py +++ b/bin/nagios/config.py @@ -11,6 +11,7 @@ import warnings import logging import pprint import nagios.object.host +import nagios.object.hostgroup class NagiosConfigError(Exception): """Base class for exceptions in this module.""" @@ -313,6 +314,23 @@ class NagiosConfig(object): else: self.objects['host'][identifier] = struct + if object_type == 'hostgroup': + + ( identifier, struct ) = nagios.object.hostgroup.verify( cur_object, self.logger ) + self.logger.debug( "Found hostgroup 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 hostgroup object structure 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 + 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/host.py b/bin/nagios/object/host.py index 8fa4aed..6bdeded 100644 --- a/bin/nagios/object/host.py +++ b/bin/nagios/object/host.py @@ -61,6 +61,9 @@ define host{ statusmap_image image_file 2d_coords x_coord,y_coord 3d_coords x_coord,y_coord,z_coord + use templates + register [0/1] + name object_name } """ @@ -147,7 +150,7 @@ define host{ except NagiosVerifyError as e: logger.warn( "Property error for host definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) - for key in ( 'active_checks_enabled', 'passive_checks_enabled', 'obsess_over_host', 'check_freshness', 'flap_detection_enabled', + for key in ( 'register', 'active_checks_enabled', 'passive_checks_enabled', 'obsess_over_host', 'check_freshness', 'flap_detection_enabled', 'process_perf_data', 'retain_status_information', 'retain_nonstatus_information', 'notifications_enabled', ): if key in definition: try: diff --git a/bin/nagios/object/hostgroup.py b/bin/nagios/object/hostgroup.py new file mode 100644 index 0000000..39a52ad --- /dev/null +++ b/bin/nagios/object/hostgroup.py @@ -0,0 +1,93 @@ +#!/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 e given definition block as a Nagios host group" + + res = {} + identifier = None + + """ +define hostgroup{ + *hostgroup_name hostgroup_name + *alias alias + members hosts + hostgroup_members hostgroups + notes note_string + notes_url url + action_url url + use templates + register [0/1] + name object_name +} +""" + + valid_keys = set( [ + 'hostgroup_name', 'alias', 'members', 'hostgroup_members', 'notes', 'notes_url', 'action_url', + 'use', 'register', 'name' + ] ) + + pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 ) + logger.debug( "Verifying hostgroup 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 hostgroup definition in {1}({2}).".format( + key, definition[key][1], definition[key][2] ) ) + + verifier = NagiosObjectVerifier( logger = logger ) + + # Einfache String-Eigenschaften + for key in ( 'name', 'hostgroup_name', 'alias', 'notes', 'notes_url', 'action_url', ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for hostgroup 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 hostgroup definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + # Array-String-Eigenschaften + for key in ( 'use', 'members', 'hostgroup_members', ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for host 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 host definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + for key in ( 'register', ): + if key in definition: + try: + if key in res: + logger.warn( "Double entry {0} for host 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 host definition in {0}({1}): {2}".format( definition[key][1], definition[key][2], e ) ) + + if 'hostgroup_name' in res: + identifier = res['hostgroup_name'] + elif 'name' in res: + identifier = res['name'] + + return ( identifier, res ) + +# vim: fileencoding=utf-8 filetype=python ts=4 expandtab