Ejemplo n.º 1
0
 def set_notifications(self, r, name, notifications):
     target_tz = pytz.timezone(r.user.timezone)
     monitor = r.user.monitors[name]
     for notification in notifications:
         destination = r.user.destinations[notification['name']]
         ad = r.session.query(AlertDestination).filter_by(
                         destination_id=destination.destination_id,
                         monitor_id=monitor.monitor_id).one()
         ad.notify_down_on = decode_enum(AlertDestinationNotifyOn, notification['notify_down_on'])
         ad.notify_up = notification['notify_up']
         if notification['qp_start']:
             ad.qp_start = utcdatetimefromiso(notification['qp_start'], target_tz).time()
         else:
             ad.qp_start = None
         if notification['qp_end']:
             ad.qp_end = utcdatetimefromiso(notification['qp_end'], target_tz).time()
         else:
             ad.qp_end = None
         r.session.flush()
     r.hub.touchMonitor(monitor.monitor_id)
     return True
Ejemplo n.º 2
0
    def generate(self, monitor, target_tz, date_for):
        start = utcdatetimefromiso(date_for, target_tz)
        end = start + timedelta(days=1)
        facet_names = [item['facet'] for item in self.report_plan if item.has_key('facet')]

        facets = self.session.query(Facet).filter(Facet.kind==monitor.kind).filter(Facet.name.in_(facet_names)).all()

        facet_ids = []
        facet_by_id = dict()
        for facet in facets:
            facet_ids.append(facet.facet_id)
            facet_by_id[facet.facet_id] = facet
            

        pre_result = dict()
        
        # get facets
        query = select([t_monitor_log.c.facet_id, t_monitor_log.c.ts, t_monitor_log.c.node, t_monitor_log.c.value],  
                       and_(t_monitor_log.c.monitor_id==monitor.monitor_id,
                            t_monitor_log.c.facet_id.in_(facet_ids),
                            t_monitor_log.c.ts >= datetime_to_ts(start),
                            t_monitor_log.c.ts < datetime_to_ts(end)))
        for facet_id, ts, node, value in self.session.execute(query):
            facet = facet_by_id[facet_id]
            pre_result.setdefault(ts, dict())[facet.name] = value
            if node is not None:
                pre_result.setdefault(ts, dict())['node'] = node

        # get errors
        query = select([t_error_log.c.ts, t_error_log.c.node, t_error_log.c.error],  
                       and_(t_error_log.c.monitor_id==monitor.monitor_id,
                            t_error_log.c.ts >= datetime_to_ts(start),
                            t_error_log.c.ts < datetime_to_ts(end)))

        for ts, node, err in self.session.execute(query):
            pre_result.setdefault(ts, dict())['error'] = err
        
        result = []
        sorted_ts = pre_result.keys()
        sorted_ts.sort()
        for ts in sorted_ts:
            data = pre_result[ts]
            data['ts'] = long(datetime_to_ts(ts_to_datetime(ts).astimezone(target_tz)))
            result.append(data)

        return [self.report_plan, result]