Exemplo n.º 1
0
    def get_next_valid_day(self, t):
        if self.get_next_future_timerange_valid(t) is None:
            # this day is finish, we check for next period
            (start_time, end_time) = self.get_start_and_end_time(get_day(t)+86400)
        else:
            (start_time, end_time) = self.get_start_and_end_time(t)

        if t <= start_time:
            return get_day(start_time)

        if self.is_time_day_valid(t):
            return get_day(t)
        return None
Exemplo n.º 2
0
    def get_next_valid_day(self, t):
        if self.get_next_future_timerange_valid(t) is None:
            # this day is finish, we check for next period
            (start_time,
             end_time) = self.get_start_and_end_time(get_day(t) + 86400)
        else:
            (start_time, end_time) = self.get_start_and_end_time(t)

        if t <= start_time:
            return get_day(start_time)

        if self.is_time_day_valid(t):
            return get_day(t)
        return None
Exemplo n.º 3
0
    def get_next_valid_time_from_t(self, t):
        #print "\tDR Get next valid from:", time.asctime(time.localtime(t))
        #print "DR Get next valid from:", t
        if self.is_time_valid(t):
            return t

        #print "DR Get next valid from:", time.asctime(time.localtime(t))
        # First we search fot the day of t
        t_day = self.get_next_valid_day(t)

        #print "DR: T next valid day", time.asctime(time.localtime(t_day))

        # We search for the min of all tr.start > sec_from_morning
        # if it's the next day, use a start of the day search for timerange
        if t < t_day:
            sec_from_morning = self.get_next_future_timerange_valid(t_day)
        else:  # t is in this day, so look from t (can be in the evening or so)
            sec_from_morning = self.get_next_future_timerange_valid(t)
        #print "DR: sec from morning", sec_from_morning

        if sec_from_morning is not None:
            if t_day is not None and sec_from_morning is not None:
                return t_day + sec_from_morning

        # Then we search for the next day of t
        # The sec will be the min of the day
        t = get_day(t) + 86400
        t_day2 = self.get_next_valid_day(t)
        sec_from_morning = self.get_next_future_timerange_valid(t_day2)
        if t_day2 is not None and sec_from_morning is not None:
            return t_day2 + sec_from_morning
        else:
            # I'm not find any valid time
            return None
Exemplo n.º 4
0
    def get_next_valid_time_from_t(self, t):
        #print "\tDR Get next valid from:", time.asctime(time.localtime(t))
        #print "DR Get next valid from:", t
        if self.is_time_valid(t):
            return t

        #print "DR Get next valid from:", time.asctime(time.localtime(t))
        # First we search fot the day of t
        t_day = self.get_next_valid_day(t)

        #print "DR: T next valid day", time.asctime(time.localtime(t_day))

        # We search for the min of all tr.start > sec_from_morning
        # if it's the next day, use a start of the day search for timerange
        if t < t_day:
            sec_from_morning = self.get_next_future_timerange_valid(t_day)
        else:  # t is in this day, so look from t (can be in the evening or so)
            sec_from_morning = self.get_next_future_timerange_valid(t)
        #print "DR: sec from morning", sec_from_morning

        if sec_from_morning is not None:
            if t_day is not None and sec_from_morning is not None:
                return t_day + sec_from_morning

        # Then we search for the next day of t
        # The sec will be the min of the day
        t = get_day(t) + 86400
        t_day2 = self.get_next_valid_day(t)
        sec_from_morning = self.get_next_future_timerange_valid(t_day2)
        if t_day2 is not None and sec_from_morning is not None:
            return t_day2 + sec_from_morning
        else:
            # I'm not find any valid time
            return None
Exemplo n.º 5
0
    def check_and_do_archive(self, first_pass=False):
        now = int(time.time())
        # first check if the file last mod (or creation) was
        # not our day
        try:
            t_last_mod = int(float(str(os.path.getmtime(self.path))))
        except OSError:  # there should be no path from now, so no move :)
            return False
        #print "Ctime %d" % os.path.getctime(self.path)
        t_last_mod_day = get_day(t_last_mod)
        today = get_day(now)
        # Will be saved with the date of yesterday because all elements are from yesterday
        yesterday = get_day(now - 3600)
        #print "Dates: t_last_mod: %d, t_last_mod_day: %d, today: %d" % (t_last_mod, t_last_mod_day, today)
        if t_last_mod_day != today:
            logger.info("We are archiving the old log file")

            # For the first pass, it's not already open
            if not first_pass:
                self.file.close()

            # Now we move it
            # Get a new name like MM

            # f_name is like nagios.log
            f_name = os.path.basename(self.path)
            # remove the ext -> (nagios,.log)
            (f_base_name, ext) = os.path.splitext(f_name)
            # make the good looking day for archive name
            # like -05-09-2010-00
            d = datetime.datetime.fromtimestamp(yesterday)
            s_day = d.strftime("-%m-%d-%Y-00")
            archive_name = f_base_name + s_day + ext
            file_archive_path = os.path.join(self.archive_path, archive_name)
            logger.info("Moving the old log file from %s to %s" %
                        (self.path, file_archive_path))

            shutil.move(self.path, file_archive_path)

            # and we overwrite it
            logger.debug("I open the log file %s" % self.path)
            self.file = open(self.path, 'a')

            return True
        return False
Exemplo n.º 6
0
    def get_next_valid_day(self, t):
        if self.get_next_future_timerange_valid(t) is None:
            #this day is finish, we check for next period
            (start_time, end_time) = self.get_start_and_end_time(get_day(t)+86400)
        else:
            (start_time, end_time) = self.get_start_and_end_time(t)

        #print self.__class__
        #print "Get next valid day start/end for", time.asctime(time.localtime(t))
        #print "Start", time.asctime(time.localtime(start_time))
        #print "End", time.asctime(time.localtime(end_time))

        if t <= start_time:
            return get_day(start_time)

        if self.is_time_day_valid(t):
            return get_day(t)
        return None
Exemplo n.º 7
0
    def check_and_do_archive(self, first_pass = False):
        now = int(time.time())
        #first check if the file last mod (or creation) was
        #not our day
        try :
            t_last_mod = int(float(str(os.path.getmtime(self.path))))
        except OSError: #there should be no path from now, so no move :)
            return False
        #print "Ctime %d" % os.path.getctime(self.path)
        t_last_mod_day = get_day(t_last_mod)
        today = get_day(now)
        # Will be saved with the date of yesterday because all elemetns arefrom yesterday
        yesterday = get_day(now-3600)
        #print "Dates: t_last_mod : %d, t_last_mod_day: %d, today : %d" % (t_last_mod, t_last_mod_day, today)
        if t_last_mod_day != today:
            logger.log("We are archiving the old log file")

            #For the first pass, it's not already open
            if not first_pass:
                self.file.close()

            #Now we move it
            #Get a new name like MM

            #f_name is like nagios.log
            f_name = os.path.basename(self.path)
            #remove the ext -> (nagios,.log)
            (f_base_name, ext) = os.path.splitext(f_name)
            #make the good looking day for archive name
            #like -05-09-2010-00
            d = datetime.datetime.fromtimestamp(yesterday)
            s_day = d.strftime("-%m-%d-%Y-00")
            archive_name = f_base_name+s_day+ext
            file_archive_path = os.path.join(self.archive_path, archive_name)
            logger.log("Moving the old log file from %s to %s" % (self.path, file_archive_path))

            shutil.move(self.path, file_archive_path)

            #and we overwrite it
            print "I open the log file %s" % self.path
            self.file = open(self.path,'a')

            return True
        return False
Exemplo n.º 8
0
    def get_next_invalid_day(self, t):
        # print "Look in", self.__dict__
        # print 'DR: get_next_invalid_day for', time.asctime(time.localtime(t))
        if self.is_time_day_invalid(t):
            # print "EARLY RETURN"
            return t

        next_future_timerange_invalid = self.get_next_future_timerange_invalid(
            t)
        # print "next_future_timerange_invalid:", next_future_timerange_invalid

        # If today there is no more unavailable timerange, search the next day
        if next_future_timerange_invalid is None:
            # print 'DR: get_next_future_timerange_invalid is None'
            # this day is finish, we check for next period
            (start_time, end_time) = self.get_start_and_end_time(get_day(t))
        else:
            # print 'DR: get_next_future_timerange_invalid is',
            # print time.asctime(time.localtime(next_future_timerange_invalid))
            (start_time, end_time) = self.get_start_and_end_time(t)

        # (start_time, end_time) = self.get_start_and_end_time(t)

        # print "START", time.asctime(time.localtime(start_time)),
        # print "END", time.asctime(time.localtime(end_time))
        # The next invalid day can be t day if there a possible
        # invalid time range (timerange is not 00->24
        if next_future_timerange_invalid is not None:
            if start_time <= t <= end_time:
                # print "Early Return next invalid day:", time.asctime(time.localtime(get_day(t)))
                return get_day(t)
            if start_time >= t:
                # print "start_time >= t:", time.asctime(time.localtime(get_day(start_time)))
                return get_day(start_time)
        else:
            # Else, there is no possibility than in our start_time<->end_time we got
            # any invalid time (full period out). So it's end_time+1 sec (tomorrow of end_time)
            return get_day(end_time + 1)

        return None
Exemplo n.º 9
0
    def get_next_invalid_day(self, t):
        # print "Look in", self.__dict__
        # print 'DR: get_next_invalid_day for', time.asctime(time.localtime(t))
        if self.is_time_day_invalid(t):
            # print "EARLY RETURN"
            return t

        next_future_timerange_invalid = self.get_next_future_timerange_invalid(t)
        # print "next_future_timerange_invalid:", next_future_timerange_invalid

        # If today there is no more unavailable timerange, search the next day
        if next_future_timerange_invalid is None:
            # print 'DR: get_next_future_timerange_invalid is None'
            # this day is finish, we check for next period
            (start_time, end_time) = self.get_start_and_end_time(get_day(t))
        else:
            # print 'DR: get_next_future_timerange_invalid is',
            # print time.asctime(time.localtime(next_future_timerange_invalid))
            (start_time, end_time) = self.get_start_and_end_time(t)

        # (start_time, end_time) = self.get_start_and_end_time(t)

        # print "START", time.asctime(time.localtime(start_time)),
        # print "END", time.asctime(time.localtime(end_time))
        # The next invalid day can be t day if there a possible
        # invalid time range (timerange is not 00->24
        if next_future_timerange_invalid is not None:
            if start_time <= t <= end_time:
                # print "Early Return next invalid day:", time.asctime(time.localtime(get_day(t)))
                return get_day(t)
            if start_time >= t:
                # print "start_time >= t:", time.asctime(time.localtime(get_day(start_time)))
                return get_day(start_time)
        else:
            # Else, there is no possibility than in our start_time<->end_time we got
            # any invalid time (full period out). So it's end_time+1 sec (tomorrow of end_time)
            return get_day(end_time + 1)

        return None
Exemplo n.º 10
0
    def get_next_invalid_day(self, t):
        #print 'DR: get_next_invalid_day for', time.asctime(time.localtime(t))
        if self.is_time_day_invalid(t):
            return t

        next_future_timerange_invalid = self.get_next_future_timerange_invalid(t)
        #print "next_future_timerange_invalid:", next_future_timerange_invalid

        #If today there is no more unavalable timerange, search the next day
        if next_future_timerange_invalid is None:
            #print 'DR: get_next_future_timerange_invalid is None'
            #this day is finish, we check for next period
            (start_time, end_time) = self.get_start_and_end_time(get_day(t)+86400)
        else:
            #print 'DR: get_next_future_timerange_invalid is', time.asctime(time.localtime(next_future_timerange_invalid))
            (start_time, end_time) = self.get_start_and_end_time(t)
            #res = get_day(t) + next_future_timerange_invalid
            #print "Early return"
            #return res

        #print 'DR:Start:', time.asctime(time.localtime(start_time))
        #print 'DR:End:', time.asctime(time.localtime(end_time))
        #The next invalid day can be t day if there a possible
        #invalid time range (timerange is not 00->24
        if next_future_timerange_invalid is not None:
            if start_time <= t <= end_time:
                #print "Early Return next invalid day:", time.asctime(time.localtime(get_day(t)))
                return get_day(t)
            if start_time >= t :
                return get_day(start_time)
        else:#Else, there is no possibility than in our start_time<->end_time we got
            #any invalid time (full period out). So it's end_time+1 sec (tomorow of end_time)
            #print "Full period out, got end_time", time.asctime(time.localtime(get_day(end_time +1)))
            return get_day(end_time +1)

        return None
Exemplo n.º 11
0
    def get_next_invalid_time_from_t(self, t):
        #print 'DR:get_next_invalid_time_from_t', time.asctime(time.localtime(t))
        if not self.is_time_valid(t):
            #print "DR: cool, t is invalid",  time.asctime(time.localtime(t))
            return t
        #else:
        #    print "DR: Arg, t is valid", time.asctime(time.localtime(t))

        #First we search fot the day of t
        t_day = self.get_next_invalid_day(t)
        #print "Get next invalid day:", time.asctime(time.localtime(t_day))
        #print "Is valid day?", self.is_time_valid(t_day)

        #We search for the min of all tr.start > sec_from_morning
        #starts = []
        #for tr in self.timeranges:
        #    tr_start = tr.hstart * 3600 + tr.mstart * 3600
        #    if tr_start >= sec_from_morning:
        #        starts.append(tr_start)

        #tr can't be valid, or it will be return at the begining
        sec_from_morning = self.get_next_future_timerange_invalid(t)
        #print "TOTO sec_from_morning:", sec_from_morning
        #Ok we've got a next invalid day and a invalid possibility in
        #timerange, so the next invalid is this day+sec_from_morning
        #print "T_day", t_day, "Sec from morning", sec_from_morning
        if t_day is not None and sec_from_morning is not None:
            return t_day + sec_from_morning + 1

        #We've got a day but no sec_from_morning : the timerange is full (0->24h)
        #so the next invalid is this day at the day_start
        if t_day is not None and sec_from_morning is None:
            return t_day

        #Then we search for the next day of t
        #The sec will be the min of the day
        t = get_day(t)+86400
        t_day2 = self.get_next_invalid_day(t)
        sec_from_morning = self.get_next_future_timerange_invalid(t_day2)
        if t_day2 is not None and sec_from_morning is not None:
            return t_day2 + sec_from_morning + 1

        if t_day2 is not None and sec_from_morning is None:
            return t_day2
        else:
            #I'm not find any valid time
            return None
Exemplo n.º 12
0
    def get_next_invalid_time_from_t(self, t):
        if not self.is_time_valid(t):
            return t

        # First we search fot the day of t
        t_day = self.get_next_invalid_day(t)
        #print "F**K NEXT DAY", time.asctime(time.localtime(t_day))

        # We search for the min of all tr.start > sec_from_morning
        # if it's the next day, use a start of the day search for timerange
        if t < t_day:
            sec_from_morning = self.get_next_future_timerange_invalid(t_day)
        else:  # t is in this day, so look from t (can be in the evening or so)
            sec_from_morning = self.get_next_future_timerange_invalid(t)
        #print "DR: sec from morning", sec_from_morning

        # tr can't be valid, or it will be return at the beginning
        #sec_from_morning = self.get_next_future_timerange_invalid(t)

        # Ok we've got a next invalid day and a invalid possibility in
        # timerange, so the next invalid is this day+sec_from_morning
        #print "T_day", t_day, "Sec from morning", sec_from_morning
        if t_day is not None and sec_from_morning is not None:
            return t_day + sec_from_morning + 1

        # We've got a day but no sec_from_morning: the timerange is full (0->24h)
        # so the next invalid is this day at the day_start
        if t_day is not None and sec_from_morning is None:
            return t_day

        # Then we search for the next day of t
        # The sec will be the min of the day
        t = get_day(t) + 86400
        t_day2 = self.get_next_invalid_day(t)
        sec_from_morning = self.get_next_future_timerange_invalid(t_day2)
        if t_day2 is not None and sec_from_morning is not None:
            return t_day2 + sec_from_morning + 1

        if t_day2 is not None and sec_from_morning is None:
            return t_day2
        else:
            # I'm not find any valid time
            return None
Exemplo n.º 13
0
    def get_next_invalid_time_from_t(self, t):
        if not self.is_time_valid(t):
            return t

        # First we search fot the day of t
        t_day = self.get_next_invalid_day(t)
        #print "F**K NEXT DAY", time.asctime(time.localtime(t_day))

        # We search for the min of all tr.start > sec_from_morning
        # if it's the next day, use a start of the day search for timerange
        if t < t_day:
            sec_from_morning = self.get_next_future_timerange_invalid(t_day)
        else:  # t is in this day, so look from t (can be in the evening or so)
            sec_from_morning = self.get_next_future_timerange_invalid(t)
        #print "DR: sec from morning", sec_from_morning

        # tr can't be valid, or it will be return at the beginning
        #sec_from_morning = self.get_next_future_timerange_invalid(t)

        # Ok we've got a next invalid day and a invalid possibility in
        # timerange, so the next invalid is this day+sec_from_morning
        #print "T_day", t_day, "Sec from morning", sec_from_morning
        if t_day is not None and sec_from_morning is not None:
            return t_day + sec_from_morning + 1

        # We've got a day but no sec_from_morning: the timerange is full (0->24h)
        # so the next invalid is this day at the day_start
        if t_day is not None and sec_from_morning is None:
            return t_day

        # Then we search for the next day of t
        # The sec will be the min of the day
        t = get_day(t) + 86400
        t_day2 = self.get_next_invalid_day(t)
        sec_from_morning = self.get_next_future_timerange_invalid(t_day2)
        if t_day2 is not None and sec_from_morning is not None:
            return t_day2 + sec_from_morning + 1

        if t_day2 is not None and sec_from_morning is None:
            return t_day2
        else:
            # I'm not find any valid time
            return None
Exemplo n.º 14
0
    def get_next_valid_time_from_t(self, t):
        #print "DR Get next valid from:", time.asctime(time.localtime(t))
        #print "DR Get next valid from:", t
        if self.is_time_valid(t):
            return t

        #print "DR Get next valid from:", time.asctime(time.localtime(t))
        #First we search fot the day of t
        t_day = self.get_next_valid_day(t)
        sec_from_morning = self.get_min_sec_from_morning()

        #print "Search for t", time.asctime(time.localtime(t))
        #print "DR: next day", time.asctime(time.localtime(t_day))
        #print "DR: sec from morning", time.asctime(time.localtime(sec_from_morning))

        #We search for the min of all tr.start > sec_from_morning
        starts = []
        for tr in self.timeranges:
            tr_start = tr.hstart * 3600 + tr.mstart * 3600
            if tr_start >= sec_from_morning:
                starts.append(tr_start)

        #tr can't be valid, or it will be return at the begining
        sec_from_morning = self.get_next_future_timerange_valid(t)
        #print "DR: sec from morning", time.asctime(time.localtime(sec_from_morning))
        #print "Sec from morning", t_day
        if sec_from_morning is not None:
            if t_day is not None and sec_from_morning is not None:
                return t_day + sec_from_morning

        #Then we search for the next day of t
        #The sec will be the min of the day
        t = get_day(t)+86400
        t_day2 = self.get_next_valid_day(t)
        sec_from_morning = self.get_next_future_timerange_valid(t_day2)
        if t_day2 is not None and sec_from_morning is not None:
            return t_day2 + sec_from_morning
        else:
            #I'm not find any valid time
            return None
Exemplo n.º 15
0
 def get_min_from_t(self, t):
     if self.is_time_valid(t):
         return t
     t_day_epoch = get_day(t)
     tr_mins = self.get_min_sec_from_morning()
     return t_day_epoch + tr_mins
Exemplo n.º 16
0
 def get_min_from_t(self, t):
     if self.is_time_valid(t):
         return t
     t_day_epoch = get_day(t)
     tr_mins = self.get_min_sec_from_morning()
     return t_day_epoch + tr_mins