def test_get_synthetics_monitor(self): """ Check that the get_synthetics_monitor function fetches the details of a specific monitor from the Synthetics API. """ monitor_id = 'd35c6c1c-23a5-4a67-b3b0-287c34e84664' response_json = { 'apiVersion': '0.2.2', 'createdAt': '2016-06-06T07:11:51.859+0000', 'frequency': 5, 'id': monitor_id, 'locations': ['AWS_US_EAST_1'], 'modifiedAt': '2016-06-06T07:11:51.859+0000', 'name': 'test-simple', 'slaThreshold': 7.0, 'status': 'ENABLED', 'type': 'SIMPLE', 'uri': 'http://newrelic-test.stage.opencraft.hosting/', 'userId': 0, } responses.add(responses.GET, '{0}/monitors/{1}'.format(newrelic.SYNTHETICS_API_URL, monitor_id), json=response_json, status=200) self.assertEqual(newrelic.get_synthetics_monitor(monitor_id), response_json) self.assertEqual(len(responses.calls), 1) request_headers = responses.calls[0].request.headers self.assertEqual(request_headers['x-api-key'], 'admin-api-key')
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)
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)