예제 #1
0
    def __init__(self, spec='now', utc=False, tz_name=None):
        if utc and tz_name:
            raise ValueError(
                'Ambiguous time zone. Do not use "utc" and "tz_name" parameter at the same time.'
            )

        tz = pytz.timezone(tz_name) if tz_name else None
        if utc:
            self.timezone = pytz.UTC
        elif tz_name:
            self.timezone = tz
        else:
            self.timezone = tzlocal.get_localzone()

        if isinstance(spec, Number):
            self.time = datetime.utcfromtimestamp(
                spec) if utc else datetime.fromtimestamp(spec, tz)
        else:
            now = datetime.utcnow() if utc else datetime.now(tz)
            delta = parse_timedelta(spec)
            if delta:
                self.time = now + delta
            elif spec == 'now':
                self.time = now
            else:
                self.time = parse_datetime(spec)
예제 #2
0
    def __init__(self, history_wrapper, weeks=4, snap_to_bin=True, bin_size='1h', dict_extractor_path=''):

        self.history_wrapper = history_wrapper
        self.weeks = weeks
        self.snap_to_bin = snap_to_bin
        self.bin_size = parse_timedelta(bin_size)
        self.dict_extractor_path = dict_extractor_path
예제 #3
0
파일: time_.py 프로젝트: porrl/zmon-worker
 def __init__(self, spec='now', utc=False):
     now = (datetime.utcnow() if utc else datetime.now())
     delta = parse_timedelta(spec)
     if delta:
         self.time = now + delta
     elif spec == 'now':
         self.time = now
     else:
         self.time = parse_datetime(spec)
예제 #4
0
    def __init__(self,
                 history_wrapper,
                 weeks=4,
                 snap_to_bin=True,
                 bin_size='1h',
                 dict_extractor_path=''):

        self.history_wrapper = history_wrapper
        self.weeks = weeks
        self.snap_to_bin = snap_to_bin
        self.bin_size = parse_timedelta(bin_size)
        self.dict_extractor_path = dict_extractor_path
예제 #5
0
    def __init__(self, spec='now', utc=False):
        now = (datetime.utcnow() if utc else datetime.now())

        if isinstance(spec, Number):
            self.time = datetime.utcfromtimestamp(spec) if utc else datetime.fromtimestamp(spec)
        else:
            delta = parse_timedelta(spec)
            if delta:
                self.time = now + delta
            elif spec == 'now':
                self.time = now
            else:
                self.time = parse_datetime(spec)
예제 #6
0
    def __init__(self, spec='now', utc=False):
        now = (datetime.utcnow() if utc else datetime.now())

        if isinstance(spec, Number):
            self.time = datetime.utcfromtimestamp(
                spec) if utc else datetime.fromtimestamp(spec)
        else:
            delta = parse_timedelta(spec)
            if delta:
                self.time = now + delta
            elif spec == 'now':
                self.time = now
            else:
                self.time = parse_datetime(spec)
예제 #7
0
파일: zmon_.py 프로젝트: porrl/zmon-worker
    def stale_active_alerts(self, multiplier=2, offset='5m'):
        '''
        Returns a list of alerts that weren't executed in a given period of time. The period is calculated using
        multiplier and offset: check's interval * multiplier + offset.
        Parameters
        ----------
        multiplier: int
            Multiplier for check's interval.
        offset: str
            Time offset, for details see parse_timedelta function in zmon-worker/src/function/time_.py.
        Returns
        -------
        list
            A list of stale active alerts.
        '''

        alert_entities = self.__redis.keys(self.ZMON_ALERTS_ENTITIES_PATTERN)

        # Load evaluated alerts and their entities from redis.
        p = self.__redis.pipeline()
        for key in alert_entities:
            p.hkeys(key)
        entities = p.execute()
        evaluated_alerts = dict((int(key.split(':')[2]), entities[i]) for (i, key) in enumerate(alert_entities))

        # Load check results for previously loaded alerts and entities.
        check_ids = []
        for alert in self.__alerts:
            if alert['id'] in evaluated_alerts:
                for entity in evaluated_alerts[alert['id']]:
                    p.lindex('zmon:checks:{}:{}'.format(alert['check_id'], entity), 0)
                    check_ids.append('{}:{}'.format(alert['check_id'], entity))
        results = p.execute()
        check_results = dict((check_id, json.loads(results[i])['ts']) for (i, check_id) in enumerate(check_ids)
                             if results[i])

        return [{'id': alert['id'], 'team': alert['team'], 'responsible_team': alert['responsible_team']} for alert in
                self.__alerts if self.__is_alert_stale(alert, evaluated_alerts, check_results, multiplier,
                parse_timedelta(offset).total_seconds())]