Пример #1
0
def report_failed_events_by_bot(bot, report_interval):
    """
    send out report for failed events for a bot over a given duration measured
    back from the moment of the call  via email

    :arg str bot: the host (short) name of the bot to report on

    :arg `object` report_interval: the time interval for which the report is
        calculated. it is either a :class:`datetime.timedelta` instance or a
        :class:`dict` suitable for constructing a :classL`datetime.timedelta`
        instance, for instance {'hours': 1, 'seconds': 1}

    :returns: the result of the email send operation
    :rtype: str

    :raises:

        :exc:`Exception` if an exception was thrown while sending the alert

    """
    subscription = base_utils.get_subscription(
        'Exchange Failed Send Receive By Bot')

    data = queries.dead_bodies(
        data_source='mail_collector.mailbotmessage',
        filter_exp='event__event_registered_on__gte',
        not_seen_after=base_utils.MomentOfTime.past(
            time_delta=report_interval),
        event__source_host__host_name=bot,
        event__event_status__iexact='fail').\
        order_by('-mail_message_identifier', 'event__event_type_sort')

    try:
        ret = base_utils.borgs_are_hailing(
            data=data, subscription=subscription, logger=LOGGER,
            time_delta=report_interval,
            level=get_preference('exchange__server_error'), bot=bot)
    except Exception as error:
        raise error

    if ret:
        return (
            'emailed exchange failed send receive events report for bot %s'
            % bot)

    return (
        'could not email exchange failed send receive events report for bot %s'
        % bot)
Пример #2
0
def report_failed_events_by_site(site, report_interval):
    """
    send out report with failed events for a site via email

    :arg str site: the host (short) name of the bot to report on

    :arg `object` report_interval: the time interval for which the report is
        calculated. it is either a :class:`datetime.timedelta` instance or a
        :class:`dict` suitable for constructing a :class:`datetime.timedelta`
        instance


    :returns: the result of the email send operation
    :rtype: str

    :raises:

        :exc:`Exception` if an exception was thrown while sending the alert

    """
    subscription = Subscription.get_subscription(
        'Exchange Failed Send Receive By Site')

    data = queries.dead_bodies(
        data_source='mail_collector.mailbotmessage',
        filter_exp='event__event_registered_on__gte',
        not_seen_after=base_utils.MomentOfTime.past(
            time_delta=report_interval),
        event__source_host__site__site=site,
        event__event_status__iexact='fail').\
        order_by('-mail_message_identifier', 'event__event_type_sort')

    if Email.send_email(data=data,
                        subscription=subscription,
                        time_delta=report_interval,
                        level=get_preference('exchange__server_error'),
                        site=site):
        LOG.info(
            'emailed exchange failed send receive events report for site'
            ' %s', site)
        return

    LOG.warning(
        'could not email exchange failed send receive events report for'
        ' site %s', site)
Пример #3
0
def report_events_by_site(site, report_interval, report_level):
    """
    send out report with events for a site via email

    :arg str site: the site to report on

    :arg `object` report_interval: the time interval for which the report is
        calculated. it is either a :class:`datetime.timedelta` instance or a
        :class:`dict` suitable for constructing a :class:`datetime.timedelta`
        instance

    :arg str report_level: INFO|WARN|ERROR

    :returns: the result of the email send operation
    :rtype: str

    :raises:

        :exc:`Exception` if an exception was thrown while sending the alert

    """
    subscription = base_utils.get_subscription('Exchange Send Receive By Site')

    data = queries.dead_bodies(
        data_source='mail_collector.mailbotmessage',
        filter_exp='event__event_registered_on__gte',
        not_seen_after=base_utils.MomentOfTime.past(
            time_delta=report_interval),
        event__source_host__site__site=site).\
        order_by('-mail_message_identifier', 'event__event_type_sort')

    try:
        ret = base_utils.borgs_are_hailing(
            data=data, subscription=subscription, logger=LOGGER,
            time_delta=report_interval, level=report_level, site=site)
    except Exception as error:
        raise error

    if ret:
        return (
            'emailed exchange send receive events report for site %s' % site)

    return (
        'could not email exchange send receive events report for site %s'
        % site)
Пример #4
0
def report_events_by_bot(bot, report_interval, report_level):
    """
    send out report for events for a bot over a given duration measured
    back from the moment of the call  via email

    :arg str bot: the host (short) name of the bot to report on

    :arg `object` report_interval: the time interval for which the report is
        calculated. it is either a :class:`datetime.timedelta` instance or a
        :class:`dict` suitable for constructing a :class:`datetime.timedelta`
        instance

    :arg str report_level: INFO|WARN|ERROR pre-pended to the email subject line


    :returns: the result of the email send operation
    :rtype: str

    :raises:
        :exc:`Exception` if an exception was thrown while sending the alert

    """
    subscription = Subscription.get_subscription(
        'Exchange Send Receive By Bot')

    data = queries.dead_bodies(
        data_source='mail_collector.mailbotmessage',
        filter_exp='event__event_registered_on__gte',
        not_seen_after=base_utils.MomentOfTime.past(
            time_delta=report_interval),
        event__source_host__host_name=bot).\
        order_by('-mail_message_identifier', 'event__event_type_sort')

    if Email.send_email(data=data,
                        subscription=subscription,
                        time_delta=report_interval,
                        level=report_level,
                        bot=bot):
        LOG.info('emailed exchange send receive events report for bot %s', bot)
        return

    LOG.warning(
        'could not email exchange send receive events report for bot %s', bot)
Пример #5
0
def bring_out_your_dead(data_source,
                        filter_exp,
                        subscription,
                        url_annotate=False,
                        level=None,
                        filter_pref=None,
                        **base_filters):
    """
    generic task to raise email alerts about
    :ref:`Mail Collector Application` entities that have been in an
    abnormal state for a given duration measured going back from the
    current moment

    :arg str data_source: the reference to a
        :class:`django.db.models.Model` in the form of 'app_label.model_name'

    :arg str filter_exp: a django filter lhs expression (field_name__lookup);
        it is actually geared to deal with datetime fields

    :arg str subscription: the reference to the
        :class:`p_soc_auto_base.models.Subscription` instance to be used
        for rendering the email alert

    :arg bool url_annotate:

        extend the queryset with the entity URL; default ``False``

    :arg str level: INFO|WARN|ERROR to add to the subject line of the email
        alert; default ``None``

    :arg `object` filter_pref: either a
        :class:`django.utils.timezone.timedelta` instance or a :class:`dict`
        suitable as argument for constructing a
        :class:`django.utils.timezone.timedelta` like {'days': ``float``,
        'hours': ``float``, 'seconds': ``float``}; default ``None``.
        when ``None`` the value is picked up from the
        :class:`citrus_borg.dynamic_preferences_registry.ExchangeDefaultError`
        dynamic setting

    :arg \*\*base_filters: additional django lookup style arguments to be
        applied to the queryset

    :returns: the result of the email send operation
    :rtype: str

    :raises:

        :exc:`TypeError` if filter_pref cannot be cast to
        ``datetime.timedelta``

        :exc:`Exception` if an exception was thrown while sending the alert

    example::

        qs=dead_bodies('mail_collector.mailhost','exchange_last_seen__lte',
                    not_seen_after={'minutes': 1}, enabled=True)

    """

    if level is None:
        level = get_preference('exchange__default_level')

    subscription = Subscription.get_subscription(subscription)

    if filter_pref is None:
        filter_pref = get_preference('exchange__default_error')

    if not isinstance(filter_pref, dict):
        filter_pref = get_preference(filter_pref)
        if not isinstance(filter_pref, timezone.timedelta):
            raise TypeError(
                'Invalid object type %s. Must be datetime.timedelta.' %
                type(filter_pref))

    not_seen_after = base_utils.MomentOfTime.past(time_delta=filter_pref)

    data = queries.dead_bodies(data_source,
                               filter_exp,
                               not_seen_after=not_seen_after,
                               url_annotate=url_annotate,
                               **base_filters)

    if not data and not get_preference('exchange__empty_alerts'):
        LOG.info('no %s data found for %s', level, subscription.subscription)
        return

    if Email.send_email(data=data,
                        subscription=subscription,
                        time_delta=filter_pref,
                        level=level):
        LOG.info('emailed data for %s', data.model._meta.verbose_name_plural)
        return

    LOG.warning('could not email data for %s',
                data.model._meta.verbose_name_plural)