Пример #1
0
    def enable_monitoring(self):
        """
        Enable monitoring on this instance.
        """
        if not settings.NEWRELIC_ADMIN_USER_API_KEY:
            self.logger.warning('Skipping monitoring setup, '
                                'NEWRELIC_ADMIN_USER_API_KEY not set')
            return
        self.logger.info('Setting up New Relic Synthetics monitors')

        # Delete existing monitors if they don't monitor this instance's
        # public urls
        monitors = newrelic.get_synthetics_monitors()
        already_enabled = [monitor for monitor in monitors
                           if monitor['uri'] in self._urls_to_monitor]
        already_enabled_ids = {enabled['id'] for enabled in already_enabled}
        for monitor in self.new_relic_availability_monitors.exclude(
                pk__in=already_enabled_ids):
            monitor.delete()

        # Add monitors for urls that are not already being monitored
        already_enabled_urls = {enabled['uri'] for enabled in already_enabled}
        for url in self._urls_to_monitor - already_enabled_urls:
            monitor_id = newrelic.create_synthetics_monitor(url)
            self.new_relic_availability_monitors.create(pk=monitor_id)

            # Set up email alerts
            if settings.ADMINS:
                emails = [email for name, email in settings.ADMINS]
                newrelic.add_synthetics_email_alerts(monitor_id, emails)