]> Frank Brehm's Git Trees - my-stuff/nagios.git/commitdiff
Mit timeperiods weitergemacht
authorFrank Brehm <frank@brehm-online.com>
Sun, 5 Dec 2010 20:38:29 +0000 (20:38 +0000)
committerFrank Brehm <frank@brehm-online.com>
Sun, 5 Dec 2010 20:38:29 +0000 (20:38 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/nagios/trunk@145 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

bin/nagios/cfg/struct.py

index 4ae3549baab71624130422c46c3a766178c38e9c..f7af332dd901f8b3b8bb725ba1468b013842b121 100755 (executable)
@@ -7,6 +7,7 @@
 import os
 import re
 import pprint
+from datetime import date
 
 from nagios.object.verify import NagiosVerifyError, NagiosObjectVerifier
 
@@ -443,24 +444,70 @@ 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_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])'
 
+        pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
+
+        # first search for a simple weekday
         regex = r'^' + re_weekdays + r'\s+' + re_timeranges + r'$'
         #self.logger.debug( "Timeperiod Regex {0!r}.\n\t{1!r}".format( regex, line ) )
-
         match = re.search( regex, line )
         if match is not None:
 
-            pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
             key = match.group(1)
             value = []
             for v in match.groups()[1:-1]:
                 value.append(v)
             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+' + re_timeranges + r'$'
+        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 ) ) )
+
+            year  = int(match.group(1))
+            month = int(match.group(2))
+            day   = int(match.group(3))
+
+            try:
+                date_start = date( year, month, day )
+            except ValueError as e:
+                self.logger.warn( "Invalid date found in line {0!r}.".format( line ) )
+                return None
+
+            key = date_start.isoformat()
+
+            # It's a date range ...
+            if match.group(4) is not None:
+
+                year  = int(match.group(4))
+                month = int(match.group(5))
+                day   = int(match.group(6))
+
+                try:
+                    date_end = date( year, month, day )
+                except ValueError as e:
+                    self.logger.warn( "Invalid date found in line {0!r}.".format( line ) )
+                    return None
+
+                key = key + ' - ' + date_end.isoformat()
+
+            # Formatting the value ...
+            value = []
+            for v in match.groups()[6:-1]:
+                value.append(v)
+            return ( key, value )
+
         # Currently only a stub
         return None