Esempio n. 1
0
 def test_create_synthetics_monitor(self):
     """
     Check that the create_synthetics_monitor function creates a monitor
     and returns its id.
     """
     monitor_id = '924a289a-6997-41ba-92be-bbe497b49753'
     monitor_url = '{0}/monitors/{1}'.format(newrelic.SYNTHETICS_API_URL,
                                             monitor_id)
     responses.add(responses.POST,
                   '{0}/monitors'.format(newrelic.SYNTHETICS_API_URL),
                   adding_headers={'Location': monitor_url},
                   status=201)
     url = 'http://newrelic-test.stage.opencraft.hosting/'
     self.assertEqual(newrelic.create_synthetics_monitor(url), monitor_id)
     self.assertEqual(len(responses.calls), 1)
     request_json = json.loads(responses.calls[0].request.body.decode())
     request_headers = responses.calls[0].request.headers
     self.assertEqual(request_headers['x-api-key'], 'admin-api-key')
     self.assertEqual(
         request_json, {
             'name': url,
             'uri': url,
             'type': 'SIMPLE',
             'frequency': 5,
             'locations': ['AWS_US_EAST_1'],
             'status': 'ENABLED',
         })
Esempio n. 2
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)
Esempio n. 3
0
    def _update_url_monitors(self, alert_policy):
        """
        Create a monitor for each URL to monitor, and delete the other monitors.
        In the end, each monitor will have an associated alert condition.
        Monitors and alert conditions will be created under the policy passed as a parameter.
        """
        urls_to_monitor_dict = self._urls_to_monitor
        urls_to_monitor = set(urls_to_monitor_dict)  # Store locally so we don't keep re-computing this
        already_monitored_urls = set()

        for monitor in self.new_relic_availability_monitors.all():
            url = newrelic.get_synthetics_monitor(monitor.pk)['uri']
            if url in urls_to_monitor:
                already_monitored_urls.add(url)
                # Check if the monitor has an associated alert condition
                # and create one if it doesn't. This helps when the alert condition
                # wasn't created in a previous invocation due to some issue.
                if not monitor.new_relic_alert_conditions.exists():
                    alert_condition_id = newrelic.add_alert_condition(
                        alert_policy.id, monitor.id, urls_to_monitor_dict[url]
                    )
                    monitor.new_relic_alert_conditions.create(id=alert_condition_id, alert_policy=alert_policy)
            else:
                self.logger.info('Deleting New Relic Synthetics monitor for old public URL %s', url)
                monitor.delete()

        for url in urls_to_monitor - already_monitored_urls:
            self.logger.info('Creating New Relic Synthetics monitor for new public URL %s', url)
            new_monitor_id = newrelic.create_synthetics_monitor(url)
            monitor = self.new_relic_availability_monitors.create(pk=new_monitor_id)
            alert_condition_id = newrelic.add_alert_condition(
                alert_policy.id, new_monitor_id, urls_to_monitor_dict[url]
            )
            monitor.new_relic_alert_conditions.create(id=alert_condition_id, alert_policy=alert_policy)
Esempio n. 4
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('Checking New Relic Synthetics monitors')

        urls_to_monitor = self._urls_to_monitor  # Store locally so we don't keep re-computing this
        already_monitored_urls = set()

        for monitor in self.new_relic_availability_monitors.all():
            url = newrelic.get_synthetics_monitor(monitor.pk)['uri']
            if url in urls_to_monitor:
                already_monitored_urls.add(url)
            else:
                self.logger.info(
                    'Deleting New Relic Synthetics monitor for old public URL %s',
                    url)
                monitor.delete()

        for url in urls_to_monitor - already_monitored_urls:
            self.logger.info(
                'Creating New Relic Synthetics monitor for new public URL %s',
                url)
            new_monitor_id = newrelic.create_synthetics_monitor(url)
            self.new_relic_availability_monitors.create(pk=new_monitor_id)

        # Set up email alerts.
        # We add emails here but never remove them - that must be done manually (or the monitor deleted)
        # in order to reduce the chance of bugs or misconfigurations accidentally supressing monitors.
        emails_to_monitor = set([email for name, email in settings.ADMINS] +
                                self.additional_monitoring_emails)
        if emails_to_monitor:
            for monitor in self.new_relic_availability_monitors.all():
                emails_current = set(
                    newrelic.get_synthetics_notification_emails(monitor.id))
                emails_to_add = list(emails_to_monitor - emails_current)
                if emails_to_add:
                    self.logger.info('Adding email(s) to monitor %s: %s',
                                     monitor.id, ', '.join(emails_to_add))
                    newrelic.add_synthetics_email_alerts(
                        monitor.id, emails_to_add)