Esempio n. 1
0
    def notify(self, alert, query, user, new_state, app, host, options):
        recipients = [
            email for email in options.get('addresses', '').split(',') if email
        ]

        if not recipients:
            logging.warning("No emails given. Skipping send.")

        html = """
        Check alert: {host}/alerts/{alert_id} / Check query: {host}/queries/{query_id}.
        """.format(host=host, alert_id=alert.id, query_id=query.id)
        logging.debug("Notifying: %s", recipients)

        try:
            alert_name = alert.name.encode('utf-8', 'ignore')
            state = new_state.upper()
            subject_template = options.get(
                'subject_template',
                settings.ALERTS_DEFAULT_MAIL_SUBJECT_TEMPLATE)
            message = Message(recipients=recipients,
                              subject=subject_template.format(
                                  alert_name=alert_name, state=state),
                              html=html)
            mail.send(message)
        except Exception:
            logging.exception("Mail send error.")
Esempio n. 2
0
def check_alerts_for_query(self, query_id):
    from redash.wsgi import app

    logger.debug("Checking query %d for alerts", query_id)
    query = models.Query.get_by_id(query_id)
    for alert in query.alerts:
        alert.query = query
        new_state = alert.evaluate()
        if new_state != alert.state:
            logger.info("Alert %d new state: %s", alert.id, new_state)
            old_state = alert.state
            alert.update_instance(state=new_state)

            if old_state == models.Alert.UNKNOWN_STATE and new_state == models.Alert.OK_STATE:
                logger.debug("Skipping notification (previous state was unknown and now it's ok).")
                continue

            # message = Message
            recipients = [s.email for s in alert.subscribers()]
            logger.debug("Notifying: %s", recipients)
            html = """
            Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
            """.format(host=settings.HOST, alert_id=alert.id, query_id=query.id)

            with app.app_context():
                message = Message(recipients=recipients,
                                  subject="[{1}] {0}".format(alert.name, new_state.upper()),
                                  html=html)

                mail.send(message)
Esempio n. 3
0
def send_mail(to, subject, html, text):
    try:
        message = Message(recipients=to, subject=subject, html=html, body=text)

        mail.send(message)
    except Exception:
        logger.exception("Failed sending message: %s", message.subject)
Esempio n. 4
0
    def notify(self, alert, query, user, new_state, app, host, options):
        recipients = [
            email for email in options.get("addresses", "").split(",") if email
        ]

        if not recipients:
            logging.warning("没有给定电子邮箱,发送已忽略。")

        if alert.custom_body:
            html = alert.custom_body
        else:
            html = """
            查看<a href="{host}/alerts/{alert_id}">提醒</a> / 
            查看<a href="{host}/queries/{query_id}">查询</a> </br>.
            """.format(host=host, alert_id=alert.id, query_id=query.id)
        logging.debug("Notifying: %s", recipients)

        try:
            state = new_state.upper()
            if alert.custom_subject:
                subject = alert.custom_subject
            else:
                subject_template = options.get(
                    "subject_template",
                    settings.ALERTS_DEFAULT_MAIL_SUBJECT_TEMPLATE)
                subject = subject_template.format(alert_name=alert.name,
                                                  state=state)

            message = Message(recipients=recipients,
                              subject=subject,
                              html=html)
            mail.send(message)
        except Exception:
            logging.exception("邮件发送错误。")
Esempio n. 5
0
def send_report(email, fn):
    """
    Builds a message within the application's context, attaches all the XLSX
    reports references in fns and sends them to the specified email.

    :param email: Destination email address.
    :param fns: Filenames of reports to attach.
    """
    # This is imported here, because it will be executed on the context
    # of the celery task. If imported at the beginning of the file, it
    # causes a circular dependency which results in the whole
    # application crashing,
    # PLEASE DO NOT FIX IT :)
    from redash.wsgi import app
    with app.app_context():
        message = Message(
            recipients=[email],
            subject="Mansion Global Analytics requested reports",
            html='Hi,<br><br>Please find attached the requested reports.'
        )

        # Attach all reports to message:
        with open(fn, 'r') as flo:
            message.attach(
                'report.xlsx',
                'application/zip',
                flo.read()
            )
        # Send the e-mail
        mail.send(message)
Esempio n. 6
0
def check_alerts_for_query(self, query_id):
    from redash.wsgi import app

    logger.debug("Checking query %d for alerts", query_id)
    query = models.Query.get_by_id(query_id)
    for alert in query.alerts:
        alert.query = query
        new_state = alert.evaluate()
        if new_state != alert.state:
            logger.info("Alert %d new state: %s", alert.id, new_state)
            old_state = alert.state
            alert.update_instance(state=new_state)

            if old_state == models.Alert.UNKNOWN_STATE and new_state == models.Alert.OK_STATE:
                logger.debug(
                    "Skipping notification (previous state was unknown and now it's ok)."
                )
                continue

            # message = Message
            recipients = [s.email for s in alert.subscribers()]
            logger.debug("Notifying: %s", recipients)
            html = """
            Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
            """.format(host=settings.HOST,
                       alert_id=alert.id,
                       query_id=query.id)

            with app.app_context():
                message = Message(recipients=recipients,
                                  subject="[{1}] {0}".format(
                                      alert.name, new_state.upper()),
                                  html=html)

                mail.send(message)
Esempio n. 7
0
    def notify(self, alert, query, user, new_state, app, host, options):
        recipients = [
            email for email in options.get("addresses", "").split(",") if email
        ]

        if not recipients:
            logging.warning("No emails given. Skipping send.")

        if alert.custom_body:
            html = alert.custom_body
        else:
            html = """
            Check <a href="{host}/alerts/{alert_id}">alert</a> / check
            <a href="{host}/queries/{query_id}">query</a> </br>.
            """.format(host=host, alert_id=alert.id, query_id=query.id)
        logging.debug("Notifying: %s", recipients)

        try:
            alert_name = alert.name.encode("utf-8", "ignore")
            state = new_state.upper()
            if alert.custom_subject:
                subject = alert.custom_subject
            else:
                subject_template = options.get(
                    "subject_template",
                    settings.ALERTS_DEFAULT_MAIL_SUBJECT_TEMPLATE)
                subject = subject_template.format(alert_name=alert_name,
                                                  state=state)

            message = Message(recipients=recipients,
                              subject=subject,
                              html=html)
            mail.send(message)
        except Exception:
            logging.exception("Mail send error.")
Esempio n. 8
0
def send_test_mail():
    from redash import mail
    from flask_mail import Message

    mail.send(
        Message(subject="Test Message from re:dash",
                recipients=[settings.MAIL_DEFAULT_SENDER],
                body="Test message."))
Esempio n. 9
0
def send_test_mail(email=None):
    from redash import mail
    from flask_mail import Message

    if email is None:
        email = settings.MAIL_DEFAULT_SENDER

    mail.send(Message(subject="Test Message from re:dash", recipients=[email], body="Test message."))
Esempio n. 10
0
def send_mail(to, subject, html, text):
    from redash.wsgi import app

    try:
        message = Message(recipients=to, subject=subject, html=html, body=text)

        mail.send(message)
    except Exception:
        logger.exception('Failed sending message: %s', message.subject)
Esempio n. 11
0
def send_mail(to, subject, html, text):
    try:
        message = Message(recipients=to,
                          subject=subject,
                          html=html,
                          body=text)

        mail.send(message)
    except Exception:
        logger.exception('Failed sending message: %s', message.subject)
Esempio n. 12
0
def notify_mail(alert, html, new_state, app):
    recipients = [s.email for s in alert.subscribers()]
    logger.debug("Notifying: %s", recipients)
    try:
        with app.app_context():
            message = Message(recipients=recipients,
                              subject="[{1}] {0}".format(alert.name.encode('utf-8', 'ignore'), new_state.upper()),
                              html=html)
            mail.send(message)
    except Exception:
        logger.exception("mail send ERROR.")
Esempio n. 13
0
def notify_mail(alert, html, new_state, app):
    recipients = [s.email for s in alert.subscribers()]
    logger.debug("Notifying: %s", recipients)
    try:
        with app.app_context():
            message = Message(
                recipients=recipients, subject="[{1}] {0}".format(alert.name, new_state.upper()), html=html
            )
            mail.send(message)
    except:
        logger.exception("mail send ERROR.")
Esempio n. 14
0
def send_test_mail(email=None):
    from redash import mail
    from flask_mail import Message

    if email is None:
        email = settings.MAIL_DEFAULT_SENDER

    mail.send(
        Message(subject="Test Message from re:dash",
                recipients=[email],
                body="Test message."))
Esempio n. 15
0
def send_test_mail(email=None):
    """
    Send test message to EMAIL (default: the address you defined in MAIL_DEFAULT_SENDER)
    """
    from redash import mail
    from flask_mail import Message

    if email is None:
        email = settings.MAIL_DEFAULT_SENDER

    mail.send(Message(subject="Test Message from Redash", recipients=[email],
                      body="Test message."))
Esempio n. 16
0
def send_test_mail(email=None):
    """
    Send test message to EMAIL (default: the address you defined in MAIL_DEFAULT_SENDER)
    """
    from redash import mail
    from flask_mail import Message

    if email is None:
        email = settings.MAIL_DEFAULT_SENDER

    mail.send(Message(subject="Test Message from Redash", recipients=[email],
                      body="Test message."))
Esempio n. 17
0
def send_mail(to, subject, html, text):
    from redash.wsgi import app

    try:
        with app.app_context():
            message = Message(recipients=to,
                              subject=subject,
                              html=html,
                              body=text)

            mail.send(message)
    except Exception:
        logger.exception('Failed sending message: %s', message.subject)
Esempio n. 18
0
    def notify(self, alert, query, user, new_state, app, host, options):
        recipients = [email for email in options.get('addresses').split(',') if email]
        html = """
        Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
        """.format(host=host, alert_id=alert.id, query_id=query.id)
        logging.debug("Notifying: %s", recipients)

        try:
            with app.app_context():
                message = Message(
                    recipients=recipients,
                    subject="[{1}] {0}".format(alert.name.encode('utf-8', 'ignore'), new_state.upper()),
                    html=html
                )
                mail.send(message)
        except Exception:
            logging.exception("mail send ERROR.")
Esempio n. 19
0
    def notify(self, alert, query, user, new_state, app, host, options):
        recipients = [
            email for email in options.get('addresses').split(',') if email
        ]
        html = """
        Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
        """.format(host=host, alert_id=alert.id, query_id=query.id)
        logging.debug("Notifying: %s", recipients)

        try:
            with app.app_context():
                message = Message(recipients=recipients,
                                  subject="[{1}] {0}".format(
                                      alert.name.encode('utf-8', 'ignore'),
                                      new_state.upper()),
                                  html=html)
                mail.send(message)
        except Exception:
            logging.exception("mail send ERROR.")
Esempio n. 20
0
    def notify(self, alert, query, user, new_state, app, host, options):
        recipients = [email for email in options.get('addresses', '').split(',') if email]

        if not recipients:
            logging.warning("No emails given. Skipping send.")

        html = """
        Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
        """.format(host=host, alert_id=alert.id, query_id=query.id)
        logging.debug("Notifying: %s", recipients)

        try:
            alert_name = alert.name.encode('utf-8', 'ignore')
            state = new_state.upper()
            subject_template = options.get('subject_template', settings.ALERTS_DEFAULT_MAIL_SUBJECT_TEMPLATE)
            message = Message(
                recipients=recipients,
                subject=subject_template.format(alert_name=alert_name, state=state),
                html=html
            )
            mail.send(message)
        except Exception:
            logging.exception("Mail send error.")
Esempio n. 21
0
    def notify(self, alert, query, user, new_state, app, host, options):
        recipients = [email for email in options.get("addresses", "").split(",") if email]

        if not recipients:
            logging.warning("No emails given. Skipping send.")

        html = """
        Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
        """.format(
            host=host, alert_id=alert.id, query_id=query.id
        )
        logging.debug("Notifying: %s", recipients)

        try:
            with app.app_context():
                message = Message(
                    recipients=recipients,
                    subject="[{1}] {0}".format(alert.name.encode("utf-8", "ignore"), new_state.upper()),
                    html=html,
                )
                mail.send(message)
        except Exception:
            logging.exception("Mail send error.")
Esempio n. 22
0
def send_test_mail():
    from redash import mail
    from flask_mail import Message

    mail.send(Message(subject="Test Message from re:dash", recipients=[settings.MAIL_DEFAULT_SENDER], body="Test message."))