def test_get_synthetics_notification_emails(self): """ Check that the get_synthetics_notification_emails function gets a list of email addresses receiving alerts for the specified monitor. """ monitor_id = '924a289a-6997-41ba-92be-bbe497b49753' emails = ['*****@*****.**', '*****@*****.**'] response_json = { 'count': 2, 'emails': emails, } responses.add(responses.GET, '{0}/monitors/{1}/notifications'.format(newrelic.SYNTHETICS_API_URL, monitor_id), json=response_json, status=200) self.assertEqual(newrelic.get_synthetics_notification_emails(monitor_id), emails)
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)