From 2f904bc9152120ea1746d33daf7eabbb084ff978 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 13 Dec 2010 20:39:10 +0000 Subject: [PATCH] Check Timeperiods beendet git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@152 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- bin/nagios/cfg/struct.py | 385 +++++++++++++++++++++++++-------------- 1 file changed, 244 insertions(+), 141 deletions(-) diff --git a/bin/nagios/cfg/struct.py b/bin/nagios/cfg/struct.py index a7770f5..f6caf04 100755 --- a/bin/nagios/cfg/struct.py +++ b/bin/nagios/cfg/struct.py @@ -257,151 +257,151 @@ Returns None if the key is invalid.""" '3d_coords': ( 'intarray', { 'count_min': 3, 'count_max': 3 } ), }, 'service': { - 'host_name': ( 'array', None ), - 'hostgroup_name': ( 'array', None ), - 'service_description': ( 'string', None ), - 'display_name': ( 'string', None ), - 'servicegroups': ( 'array', None ), - 'is_volatile': ( 'bool', None ), - 'check_command': ( 'string', None ), - 'initial_state': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c' ] ) } ), - 'max_check_attempts': ( 'int', None ), - 'check_interval': ( 'int', None ), - 'retry_interval': ( 'int', None ), - 'normal_check_interval': ( 'int', { 'deprecated': True, } ), - 'retry_check_interval': ( 'int', { 'deprecated': True, } ), - 'active_checks_enabled': ( 'bool', None ), - 'passive_checks_enabled': ( 'bool', None ), - 'parallelize_check': ( 'bool', { 'deprecated': True, } ), - 'check_period': ( 'string', None ), - 'failure_prediction_enabled': ( 'bool', None ), - 'obsess_over_service': ( 'bool', None ), - 'check_freshness': ( 'bool', None ), - 'freshness_threshold': ( 'int', None ), - 'event_handler': ( 'string', None ), - 'event_handler_enabled': ( 'bool', None ), - 'low_flap_threshold': ( 'int', None ), - 'high_flap_threshold': ( 'int', None ), - 'flap_detection_enabled': ( 'bool', None ), - 'flap_detection_options': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c' ] ) } ), - 'process_perf_data': ( 'bool', None ), - 'retain_status_information': ( 'bool', None ), - 'retain_nonstatus_information': ( 'bool', None ), - 'notification_interval': ( 'int', None ), - 'first_notification_delay': ( 'int', None ), - 'notification_period': ( 'string', None ), - 'notification_options': ( 'set', { 'valid_values': set( [ 'w', 'u', 'c', 'r', 'f', 's', ] ) } ), - 'notifications_enabled': ( 'bool', None ), - 'contacts': ( 'array', None ), - 'contact_groups': ( 'array', None ), - 'stalking_options': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c' ] ) } ), - 'notes': ( 'string', None ), - 'notes_url': ( 'string', None ), - 'action_url': ( 'string', None ), - 'icon_image': ( 'string', None ), - 'icon_image_alt': ( 'string', None ), + 'host_name': ( 'array', { 'mandantory': True } ), + 'hostgroup_name': ( 'array', None ), + 'service_description': ( 'string', { 'mandantory': True } ), + 'display_name': ( 'string', None ), + 'servicegroups': ( 'array', None ), + 'is_volatile': ( 'bool', None ), + 'check_command': ( 'string', { 'mandantory': True } ), + 'initial_state': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c' ] ) } ), + 'max_check_attempts': ( 'int', { 'mandantory': True } ), + 'check_interval': ( 'int', { 'mandantory': True } ), + 'retry_interval': ( 'int', { 'mandantory': True } ), + 'normal_check_interval': ( 'int', { 'deprecated': True, } ), + 'retry_check_interval': ( 'int', { 'deprecated': True, } ), + 'active_checks_enabled': ( 'bool', None ), + 'passive_checks_enabled': ( 'bool', None ), + 'parallelize_check': ( 'bool', { 'deprecated': True, } ), + 'check_period': ( 'string', { 'mandantory': True } ), + 'failure_prediction_enabled': ( 'bool', None ), + 'obsess_over_service': ( 'bool', None ), + 'check_freshness': ( 'bool', None ), + 'freshness_threshold': ( 'int', None ), + 'event_handler': ( 'string', None ), + 'event_handler_enabled': ( 'bool', None ), + 'low_flap_threshold': ( 'int', None ), + 'high_flap_threshold': ( 'int', None ), + 'flap_detection_enabled': ( 'bool', None ), + 'flap_detection_options': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c' ] ) } ), + 'process_perf_data': ( 'bool', None ), + 'retain_status_information': ( 'bool', None ), + 'retain_nonstatus_information': ( 'bool', None ), + 'notification_interval': ( 'int', { 'mandantory': True } ), + 'first_notification_delay': ( 'int', None ), + 'notification_period': ( 'string', { 'mandantory': True } ), + 'notification_options': ( 'set', { 'valid_values': set( [ 'w', 'u', 'c', 'r', 'f', 's', ] ) } ), + 'notifications_enabled': ( 'bool', None ), + 'contacts': ( 'array', { 'mandantory': True } ), + 'contact_groups': ( 'array', { 'mandantory': True } ), + 'stalking_options': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c' ] ) } ), + 'notes': ( 'string', None ), + 'notes_url': ( 'string', None ), + 'action_url': ( 'string', None ), + 'icon_image': ( 'string', None ), + 'icon_image_alt': ( 'string', None ), }, 'hostgroup': { - 'hostgroup_name': ( 'string', None ), - 'alias': ( 'string', None ), - 'members': ( 'array', None ), - 'hostgroup_members': ( 'array', None ), - 'notes': ( 'string', None ), - 'notes_url': ( 'string', None ), - 'action_url': ( 'string', None ), + 'hostgroup_name': ( 'string', { 'mandantory': True } ), + 'alias': ( 'string', { 'mandantory': True } ), + 'members': ( 'array', None ), + 'hostgroup_members': ( 'array', None ), + 'notes': ( 'string', None ), + 'notes_url': ( 'string', None ), + 'action_url': ( 'string', None ), }, 'servicegroup': { - 'servicegroup_name': ( 'string', None ), - 'alias': ( 'string', None ), - 'members': ( 'array', None ), - 'servicegroup_members': ( 'array', None ), - 'notes': ( 'string', None ), - 'notes_url': ( 'string', None ), - 'action_url': ( 'string', None ), + 'servicegroup_name': ( 'string', { 'mandantory': True } ), + 'alias': ( 'string', { 'mandantory': True } ), + 'members': ( 'array', None ), + 'servicegroup_members': ( 'array', None ), + 'notes': ( 'string', None ), + 'notes_url': ( 'string', None ), + 'action_url': ( 'string', None ), }, 'contact': { - 'contact_name': ( 'string', None ), - 'alias': ( 'string', None ), - 'contactgroups': ( 'array', None ), - 'host_notifications_enabled': ( 'bool', None ), - 'service_notifications_enabled': ( 'bool', None ), - 'host_notification_period': ( 'string', None ), - 'service_notification_period': ( 'string', None ), - 'host_notification_options': ( 'set', { 'valid_values': set( [ 'd', 'u', 'r', 'f', 's', 'n' ] ) } ), - 'service_notification_options': ( 'set', { 'valid_values': set( [ 'w', 'u', 'c', 'r', 'f', 's', 'n' ] ) } ), - 'host_notification_commands': ( 'string', None ), - 'service_notification_commands': ( 'string', None ), - 'email': ( 'string', None ), - 'pager': ( 'string', None ), - 'addressx': ( 'string', None ), - 'can_submit_commands': ( 'bool', None ), - 'retain_status_information': ( 'bool', None ), - 'retain_nonstatus_information': ( 'bool', None ), + 'contact_name': ( 'string', { 'mandantory': True } ), + 'alias': ( 'string', None ), + 'contactgroups': ( 'array', None ), + 'host_notifications_enabled': ( 'bool', { 'mandantory': True } ), + 'service_notifications_enabled': ( 'bool', { 'mandantory': True } ), + 'host_notification_period': ( 'string', { 'mandantory': True } ), + 'service_notification_period': ( 'string', { 'mandantory': True } ), + 'host_notification_options': ( 'set', { 'mandantory': True, 'valid_values': set( [ 'd', 'u', 'r', 'f', 's', 'n' ] ) } ), + 'service_notification_options': ( 'set', { 'mandantory': True, 'valid_values': set( [ 'w', 'u', 'c', 'r', 'f', 's', 'n' ] ) } ), + 'host_notification_commands': ( 'string', { 'mandantory': True } ), + 'service_notification_commands': ( 'string', { 'mandantory': True } ), + 'email': ( 'string', None ), + 'pager': ( 'string', None ), + 'addressx': ( 'string', None ), + 'can_submit_commands': ( 'bool', None ), + 'retain_status_information': ( 'bool', None ), + 'retain_nonstatus_information': ( 'bool', None ), }, 'contactgroup': { - 'contactgroup_name': ( 'string', None ), - 'alias': ( 'string', None ), - 'members': ( 'array', None ), - 'contactgroup_members': ( 'array', None ), + 'contactgroup_name': ( 'string', { 'mandantory': True } ), + 'alias': ( 'string', { 'mandantory': True } ), + 'members': ( 'array', None ), + 'contactgroup_members': ( 'array', None ), }, 'timeperiod': { - 'timeperiod_name': ( 'string', None ), - 'alias': ( 'string', None ), + 'timeperiod_name': ( 'string', { 'mandantory': True } ), + 'alias': ( 'string', { 'mandantory': True } ), #'[weekday]': timeranges #'[exception]': timeranges - 'exclude': ( 'array', None ), + 'exclude': ( 'array', None ), }, 'command': { - 'command_name': ( 'string', None ), - 'command_line': ( 'string', None ), + 'command_name': ( 'string', { 'mandantory': True } ), + 'command_line': ( 'string', { 'mandantory': True } ), }, 'servicedependency': { - 'dependent_host_name': ( 'array', None ), - 'dependent_hostgroup_name': ( 'array', None ), - 'dependent_service_description': ( 'array', None ), - 'host_name': ( 'array', None ), - 'hostgroup_name': ( 'array', None ), - 'service_description': ( 'array', None ), - 'inherits_parent': ( 'bool', None ), - 'execution_failure_criteria': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c', 'p', 'n' ] ) } ), #[o,w,u,c,p,n] - 'notification_failure_criteria': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c', 'p', 'n' ] ) } ), #[o,w,u,c,p,n] - 'dependency_period': ( 'string', None ), + 'dependent_host_name': ( 'array', { 'mandantory': True } ), + 'dependent_hostgroup_name': ( 'array', None ), + 'dependent_service_description': ( 'array', { 'mandantory': True } ), + 'host_name': ( 'array', { 'mandantory': True } ), + 'hostgroup_name': ( 'array', None ), + 'service_description': ( 'array', { 'mandantory': True } ), + 'inherits_parent': ( 'bool', None ), + 'execution_failure_criteria': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c', 'p', 'n' ] ) } ), #[o,w,u,c,p,n] + 'notification_failure_criteria': ( 'set', { 'valid_values': set( [ 'o', 'w', 'u', 'c', 'p', 'n' ] ) } ), #[o,w,u,c,p,n] + 'dependency_period': ( 'string', None ), }, 'serviceescalation': { - 'host_name': ( 'array', None ), - 'hostgroup_name': ( 'array', None ), - 'service_description': ( 'string', None ), - 'contacts': ( 'array', None ), - 'contact_groups': ( 'array', None ), - 'first_notification': ( 'int', None ), - 'last_notification': ( 'int', None ), - 'notification_interval': ( 'int', None ), - 'escalation_period': ( 'string', None ), - 'escalation_options': ( 'set', { 'valid_values': set( [ 'w', 'u', 'c', 'r' ] ) } ), #[w,u,c,r] + 'host_name': ( 'array', { 'mandantory': True } ), + 'hostgroup_name': ( 'array', None ), + 'service_description': ( 'string', { 'mandantory': True } ), + 'contacts': ( 'array', { 'mandantory': True } ), + 'contact_groups': ( 'array', { 'mandantory': True } ), + 'first_notification': ( 'int', { 'mandantory': True } ), + 'last_notification': ( 'int', { 'mandantory': True } ), + 'notification_interval': ( 'int', { 'mandantory': True } ), + 'escalation_period': ( 'string', None ), + 'escalation_options': ( 'set', { 'valid_values': set( [ 'w', 'u', 'c', 'r' ] ) } ), #[w,u,c,r] }, 'hostdependency': { - 'dependent_host_name': ( 'array', None ), - 'dependent_hostgroup_name': ( 'array', None ), - 'host_name': ( 'array', None ), - 'hostgroup_name': ( 'array', None ), - 'inherits_parent': ( 'bool', None ), - 'execution_failure_criteria': ( 'set', { 'valid_values': set( [ 'o', 'd', 'u', 'p', 'n' ] ) } ), #[o,d,u,p,n] - 'notification_failure_criteria': ( 'set', { 'valid_values': set( [ 'o', 'd', 'u', 'p', 'n' ] ) } ), #[o,d,u,p,n] - 'dependency_period': ( 'string', None ), + 'dependent_host_name': ( 'array', { 'mandantory': True } ), + 'dependent_hostgroup_name': ( 'array', None ), + 'host_name': ( 'array', { 'mandantory': True } ), + 'hostgroup_name': ( 'array', None ), + 'inherits_parent': ( 'bool', None ), + 'execution_failure_criteria': ( 'set', { 'valid_values': set( [ 'o', 'd', 'u', 'p', 'n' ] ) } ), #[o,d,u,p,n] + 'notification_failure_criteria': ( 'set', { 'valid_values': set( [ 'o', 'd', 'u', 'p', 'n' ] ) } ), #[o,d,u,p,n] + 'dependency_period': ( 'string', None ), }, 'hostescalation': { - 'host_name': ( 'string', None ), - 'hostgroup_name': ( 'array', None ), - 'contacts': ( 'array', None ), - 'contact_groups': ( 'array', None ), - 'first_notification': ( 'int', None ), - 'last_notification': ( 'int', None ), - 'notification_interval': ( 'int', None ), - 'escalation_period': ( 'string', None ), - 'escalation_options': ( 'set', { 'valid_values': set( [ 'd', 'u', 'r' ] ) } ), #[d,u,r] + 'host_name': ( 'string', { 'mandantory': True } ), + 'hostgroup_name': ( 'array', None ), + 'contacts': ( 'array', { 'mandantory': True } ), + 'contact_groups': ( 'array', { 'mandantory': True } ), + 'first_notification': ( 'int', { 'mandantory': True } ), + 'last_notification': ( 'int', { 'mandantory': True } ), + 'notification_interval': ( 'int', { 'mandantory': True } ), + 'escalation_period': ( 'string', None ), + 'escalation_options': ( 'set', { 'valid_values': set( [ 'd', 'u', 'r' ] ) } ), #[d,u,r] }, 'hostextinfo': { - 'host_name': ( 'string', None ), + 'host_name': ( 'string', { 'mandantory': True } ), 'hostgroup_name': ( 'array', { 'deprecated': True, } ), 'notes': ( 'string', None ), 'notes_url': ( 'string', None ), @@ -414,12 +414,12 @@ Returns None if the key is invalid.""" '3d_coords': ( 'intarray', { 'count_min': 3, 'count_max': 3 } ), }, 'serviceextinfo': { - 'host_name': ( 'array', None ), - 'service_description': ( 'string', None ), - 'notes': ( 'string', None ), - 'notes_url': ( 'string', None ), - 'icon_image': ( 'string', None ), - 'icon_image_alt': ( 'string', None ), + 'host_name': ( 'array', { 'mandantory': True } ), + 'service_description': ( 'string', { 'mandantory': True } ), + 'notes': ( 'string', None ), + 'notes_url': ( 'string', None ), + 'icon_image': ( 'string', None ), + 'icon_image_alt': ( 'string', None ), }, } @@ -454,12 +454,16 @@ returns None on failure""" if match is not None: return None - re_time = r'(?:(?:(?:[01][0-9]|2[0-3]):[0-5][0-9])|24:00)' - re_timerange = re_time + r'\s*-\s*' + re_time - re_timeranges = r'(' + re_timerange + r')(?:,(' + re_timerange + r'))*' - re_weekdays = r'(monday|tuesday|wednesday|thursday|friday|saturday|sunday)' - re_isodate = r'(\d\d\d\d)-(0[1-9]|1[0-2])-([0-2][1-9]|3[01])' - re_monthname = r'(january|ferbruary|march|april|may|june|july|august|september|october|november|december)' + if self.verbose >= 3: + self.logger.debug( "Inspecting timeperiod line:\n\t{0!r}".format( line ) ) + + re_time = r'(?:(?:(?:[01][0-9]|2[0-3]):[0-5][0-9])|24:00)' + re_timerange = re_time + r'\s*-\s*' + re_time + re_timeranges = r'(' + re_timerange + r')(?:,(' + re_timerange + r'))*' + re_weekdays = r'(monday|tuesday|wednesday|thursday|friday|saturday|sunday)' + re_isodate = r'(\d\d\d\d)-(0[1-9]|1[0-2])-([0-2][1-9]|3[01])' + re_monthname = r'(january|february|march|april|may|june|july|august|september|october|november|december)' + re_monthname_wd = r'(day|january|february|march|april|may|june|july|august|september|october|november|december)' pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 ) @@ -470,6 +474,13 @@ returns None on failure""" match = re.search( regex, line ) if match is not None: + if self.verbose >= 3: + liste = [] + for k in match.groups(): + liste.append(k) + self.logger.debug( "Found matches:\n\t{0}".format( pp.pformat( liste ) ) ) + del liste + key = match.group(1) value = [] for v in match.groups()[1:-1]: @@ -477,16 +488,20 @@ returns None on failure""" return ( key, value ) # Now search single ISO formatted date or a range of two ISO formatted dates - regex = r'^' + re_isodate + r'(?:\s*-\s*' + re_isodate + r')?(?:\s*/\s*(\d+))\s+' + re_timeranges + r'$' - if self.verbose >= 3: + regex = r'^' + re_isodate + r'(?:\s*-\s*' + re_isodate + r')?(?:\s*/\s*(\d+))?\s+' + re_timeranges + r'$' + # 1 2 3 4 5 6 7 8 ... + + if self.verbose >= 4: self.logger.debug( "Timeperiod Regex {0!r}.\n\t{1!r}".format( regex, line ) ) match = re.search( regex, line ) if match is not None: - liste = [] - for k in match.groups(): - liste.append(k) - self.logger.debug( "Found matches:\n\t{0}".format( pp.pformat( liste ) ) ) + if self.verbose >= 3: + liste = [] + for k in match.groups(): + liste.append(k) + self.logger.debug( "Found matches:\n\t{0}".format( pp.pformat( liste ) ) ) + del liste year = int(match.group(1)) month = int(match.group(2)) @@ -513,10 +528,10 @@ returns None on failure""" self.logger.warning( "Invalid date found in line {0!r}.".format( line ) ) return None - key = key + ' - ' + date_end.isoformat() + key += ' - ' + date_end.isoformat() if match.group(7) is not None: - key = key + ' / ' + match.group(7) + key += ' / ' + match.group(7) # Formatting the value ... value = [] @@ -524,6 +539,94 @@ returns None on failure""" value.append(v) return ( key, value ) + # Searching for a date in the form 'monday 3' or 'monday 3 - thursday 4' or 'monday 3 - thursday 4 / 2' + regex = r'^' + re_weekdays + r'\s+(-?\d+)(?:\s+' + re_monthname + r')?' + # 1 2 3 + regex += r'(?:\s+-\s+' + re_weekdays + r'\s+(-?\d+)(?:\s+' + re_monthname + r')?)?' + # 4 5 6 + regex += r'(?:\s*/\s*(\d+))?\s+' + re_timeranges + r'$' + # 7 8... + if self.verbose >= 4: + self.logger.debug( "Timeperiod Regex {0!r}.\n\t{1!r}".format( regex, line ) ) + match = re.search( regex, line ) + if match is not None: + + if self.verbose >= 3: + liste = [] + for k in match.groups(): + liste.append(k) + self.logger.debug( "Found matches:\n\t{0}".format( pp.pformat( liste ) ) ) + del liste + + key = "{0} {1}".format( match.group(1), match.group(2) ) + if match.group(3) is not None: + key += ' ' + match.group(3) + if match.group(4) is not None: + key += ' - ' + "{0} {1}".format( match.group(4), match.group(5) ) + if match.group(6) is not None: + key += ' ' + match.group(6) + if match.group(7) is not None: + key += ' / ' + match.group(7) + + value = [] + for v in match.groups()[7:-1]: + value.append(v) + return ( key, value ) + + # Searching for a date in the form 'day 2' or 'february 10' or 'february -1' or 'day 1 - 15' or 'day 20 - -1' or 'july 10 - 15' + regex = r'^' + re_monthname_wd + r'\s+(-?\d+)' + '(?:\s+-\s+(-?\d+))?' + # 1 2 3 + regex += r'(?:\s*/\s*(\d+))?\s+' + re_timeranges + r'$' + # 4 5... + if self.verbose >= 4: + self.logger.debug( "Timeperiod Regex {0!r}.\n\t{1!r}".format( regex, line ) ) + match = re.search( regex, line ) + if match is not None: + + if self.verbose >= 3: + liste = [] + for k in match.groups(): + liste.append(k) + self.logger.debug( "Found matches:\n\t{0}".format( pp.pformat( liste ) ) ) + del liste + + key = "{0} {1}".format( match.group(1), match.group(2) ) + if match.group(3) is not None: + key += ' - {0}'.format( match.group(3) ) + if match.group(4) is not None: + key += ' / ' + match.group(4) + + value = [] + for v in match.groups()[4:-1]: + value.append(v) + return ( key, value ) + + # And last but not least searching for a date in the form 'april 10 - may 15' or 'july 10 - august 15 / 2' + regex = r'^' + re_monthname_wd + r'\s+(-?\d+)\s+-\s+' + re_monthname_wd + r'\s+(-?\d+)' + # 1 2 3 4 + regex += r'(?:\s*/\s*(\d+))?\s+' + re_timeranges + r'$' + # 5 6 ... + if self.verbose >= 4: + self.logger.debug( "Timeperiod Regex {0!r}.\n\t{1!r}".format( regex, line ) ) + match = re.search( regex, line ) + if match is not None: + + if self.verbose >= 3: + liste = [] + for k in match.groups(): + liste.append(k) + self.logger.debug( "Found matches:\n\t{0}".format( pp.pformat( liste ) ) ) + del liste + + key = "{0} {1} - {2} {3}".format( match.group(1), match.group(2), match.group(3), match.group(4) ) + if match.group(5) is not None: + key += ' / ' + match.group(5) + + value = [] + for v in match.groups()[5:-1]: + value.append(v) + return ( key, value ) + # Currently only a stub return None -- 2.39.5