def notify(self, check, bounce_url): if not self.channel.email_verified: return "Email not verified" headers = {"X-Bounce-Url": bounce_url} try: # Look up the sorting preference for this email address p = Profile.objects.get(user__email=self.channel.value) sort = p.sort except Profile.DoesNotExist: # Default sort order is by check's creation time sort = "created" # list() executes the query, to avoid DB access while # rendering a template ctx = { "check": check, "checks": list(self.checks()), "sort": sort, "now": timezone.now(), "unsub_link": self.channel.get_unsub_link() } emails.alert(self.channel.value, ctx, headers)
def notify(self, check): if not self.channel.email_verified: return "Email not verified" unsub_link = self.channel.get_unsub_link() headers = { "X-Status-Url": check.status_url, "List-Unsubscribe": "<%s>" % unsub_link, "List-Unsubscribe-Post": "List-Unsubscribe=One-Click", } from hc.accounts.models import Profile # If this email address has an associated account, include # a summary of projects the account has access to try: profile = Profile.objects.get(user__email=self.channel.email_value) projects = list(profile.projects()) except Profile.DoesNotExist: projects = None ctx = { "check": check, "ping": check.ping_set.order_by("created").last(), "projects": projects, "unsub_link": unsub_link, } emails.alert(self.channel.email_value, ctx, headers)
def notify(self, check): if not self.channel.email_verified: return "Email not verified" unsub_link = self.channel.get_unsub_link() headers = { "X-Status-Url": check.status_url, "List-Unsubscribe": "<%s>" % unsub_link, "List-Unsubscribe-Post": "List-Unsubscribe=One-Click", } try: # Look up the sorting preference for this email address p = Profile.objects.get(user__email=self.channel.email_value) sort = p.sort except Profile.DoesNotExist: # Default sort order is by check's creation time sort = "created" # list() executes the query, to avoid DB access while # rendering a template ctx = { "check": check, "checks": list(self.checks()), "sort": sort, "now": timezone.now(), "unsub_link": unsub_link, } emails.alert(self.channel.email_value, ctx, headers)
def runs_too_often(self): checks = Check.objects.filter(user=self.user) now = timezone.now() for check in checks: if now - self.last_ping < self.timeout - self.grace: ctx = {"check": check} for channel in self.channel_set.all(): emails.alert(channel.value, ctx) return "over"
def notify(self, check): if not self.channel.email_verified: return "Email not verified" ctx = { "check": check, "checks": self.checks(), "now": timezone.now() } emails.alert(self.channel.value, ctx)
def notify(self, check, bounce_url): if not self.channel.email_verified: return "Email not verified" headers = {"X-Bounce-Url": bounce_url} ctx = { "check": check, "checks": self.checks(), "now": timezone.now(), "unsub_link": self.channel.get_unsub_link() } emails.alert(self.channel.value, ctx, headers)
def send_stakeholder_alert(self): if self.status not in ("up", "down"): raise NotImplementedError("Unexpected status: %s" % self.status) stakeholders = StakeHolder.objects.filter(code=self.code) for stakeholder in stakeholders: now = timezone.now() notify_stakeholder = self.last_ping + self.timeout + self.grace +\ td(hours = stakeholder.hierachy) < now ctx = { "check": self, "now": now, } if notify_stakeholder: emails.alert(stakeholder.email, ctx)
def notify(self, check): # check if a team member should recieve notification-- if not self.channel.email_verified: return "Email not verified" show_upgrade_note = False if settings.USE_PAYMENTS and check.status == "up": if not check.user.profile.team_access_allowed: show_upgrade_note = True ctx = { "check": check, "checks": self.checks(), "now": timezone.now(), "show_upgrade_note": show_upgrade_note } emails.alert(self.channel.value, ctx)
def notify(self, check): if not self.channel.email_verified: return "Email not verified" show_upgrade_note = False if settings.USE_PAYMENTS and check.status == "up": if not check.user.profile.team_access_allowed: show_upgrade_note = True ctx = { "check": check, "checks": self.checks(), "now": timezone.now(), "show_upgrade_note": show_upgrade_note } emails.alert(self.channel.value, ctx)
def notify(self, check, notification=None) -> None: if not self.channel.email_verified: raise TransportError("Email not verified") unsub_link = self.channel.get_unsub_link() headers = { "List-Unsubscribe": "<%s>" % unsub_link, "List-Unsubscribe-Post": "List-Unsubscribe=One-Click", } if notification: headers["X-Status-Url"] = notification.status_url() from hc.accounts.models import Profile # If this email address has an associated account, include # a summary of projects the account has access to try: profile = Profile.objects.get(user__email=self.channel.email_value) projects = list(profile.projects()) except Profile.DoesNotExist: projects = None ping = None # Look up the last ping only if the Check instance has pk if check.pk: ping = check.ping_set.order_by("created").last() body = None if ping and ping.has_body(): body = ping.get_body() if body is None and ping.object_size: # Body is not uploaded to object storage yet. # Wait 5 seconds, then fetch the body again. time.sleep(5) body = ping.get_body() ctx = { "check": check, "ping": ping, "body": body, "projects": projects, "unsub_link": unsub_link, } emails.alert(self.channel.email_value, ctx, headers)
def notify(self, check): n = Notification(owner=check, channel=self) n.check_status = check.status if self.kind == "email" and self.email_verified: ctx = { "check": check, "checks": self.user.check_set.order_by("created"), "now": timezone.now() } emails.alert(self.value, ctx) n.save() elif self.kind == "webhook" and check.status == "down": try: r = requests.get(self.value, timeout=5) n.status = r.status_code except requests.exceptions.Timeout: # Well, we tried pass n.save() elif self.kind == "pd": if check.status == "down": event_type = "trigger" description = "%s is DOWN" % check.name_then_code() else: event_type = "resolve" description = "%s received a ping and is now UP" % \ check.name_then_code() payload = { "service_key": self.value, "incident_key": str(check.code), "event_type": event_type, "description": description, "client": "healthchecks.io", "client_url": settings.SITE_ROOT } url = "https://events.pagerduty.com/generic/2010-04-15/create_event.json" r = requests.post(url, data=json.dumps(payload)) n.status = r.status_code n.save()
def notify(self, check, bounce_url): if not self.channel.email_verified: return "Email not verified" headers = {"X-Bounce-Url": bounce_url} try: # Look up the sorting preference for this email address p = Profile.objects.get(user__email=self.channel.value) sort = p.sort except Profile.DoesNotExist: # Default sort order is by check's creation time sort = "created" ctx = { "check": check, "checks": self.checks(), "sort": sort, "now": timezone.now(), "unsub_link": self.channel.get_unsub_link() } emails.alert(self.channel.value, ctx, headers)
def notify(self, check): n = Notification(owner=check, channel=self) n.check_status = check.status if self.kind == "email" and self.email_verified: ctx = { "check": check, "checks": self.user.check_set.order_by("created"), "now": timezone.now() } emails.alert(self.value, ctx) n.save() elif self.kind == "webhook" and check.status == "down": try: r = requests.get(self.value, timeout=5) n.status = r.status_code except requests.exceptions.Timeout: # Well, we tried pass n.save() elif self.kind == "slack": tmpl = "integrations/slack_message.html" text = render_to_string(tmpl, {"check": check}) payload = { "text": text, "username": "******", "icon_url": "https://healthchecks.io/static/img/[email protected]" } r = requests.post(self.value, json=payload, timeout=5) n.status = r.status_code n.save() elif self.kind == "hipchat": tmpl = "integrations/hipchat_message.html" text = render_to_string(tmpl, {"check": check}) payload = { "message": text, "color": "green" if check.status == "up" else "red", } r = requests.post(self.value, json=payload, timeout=5) n.status = r.status_code n.save() elif self.kind == "pd": if check.status == "down": event_type = "trigger" description = "%s is DOWN" % check.name_then_code() else: event_type = "resolve" description = "%s received a ping and is now UP" % \ check.name_then_code() payload = { "service_key": self.value, "incident_key": str(check.code), "event_type": event_type, "description": description, "client": "healthchecks.io", "client_url": settings.SITE_ROOT } url = "https://events.pagerduty.com/generic/2010-04-15/create_event.json" r = requests.post(url, data=json.dumps(payload), timeout=5) n.status = r.status_code n.save() elif self.kind == "po": tmpl = "integrations/pushover_message.html" ctx = { "check": check, "down_checks": self.user.check_set.filter(status="down").exclude( code=check.code).order_by("created"), } text = render_to_string(tmpl, ctx).strip() if check.status == "down": title = "%s is DOWN" % check.name_then_code() else: title = "%s is now UP" % check.name_then_code() user_key, priority, _ = self.po_value payload = { "token": settings.PUSHOVER_API_TOKEN, "user": user_key, "message": text, "title": title, "html": 1, "priority": priority, } if priority == 2: # Emergency notification payload["retry"] = settings.PUSHOVER_EMERGENCY_RETRY_DELAY payload["expire"] = settings.PUSHOVER_EMERGENCY_EXPIRATION url = "https://api.pushover.net/1/messages.json" r = requests.post(url, data=payload, timeout=5) n.status = r.status_code n.save()
def notify(self, check): n = Notification(owner=check, channel=self) n.check_status = check.status if self.kind == "email" and self.email_verified: ctx = { "check": check, "checks": self.user.check_set.order_by("created"), "now": timezone.now() } emails.alert(self.value, ctx) n.save() elif self.kind == "webhook" and check.status == "down": try: r = requests.get(self.value, timeout=5) n.status = r.status_code except requests.exceptions.Timeout: # Well, we tried pass n.save() elif self.kind == "slack": tmpl = "integrations/slack_message.html" text = render_to_string(tmpl, {"check": check}) payload = { "text": text, "username": "******", "icon_url": "https://healthchecks.io/static/img/[email protected]" } r = requests.post(self.value, json=payload, timeout=5) n.status = r.status_code n.save() elif self.kind == "hipchat": tmpl = "integrations/hipchat_message.html" text = render_to_string(tmpl, {"check": check}) payload = { "message": text, "color": "green" if check.status == "up" else "red", } r = requests.post(self.value, json=payload, timeout=5) n.status = r.status_code n.save() elif self.kind == "pd": if check.status == "down": event_type = "trigger" description = "%s is DOWN" % check.name_then_code() else: event_type = "resolve" description = "%s received a ping and is now UP" % \ check.name_then_code() payload = { "service_key": self.value, "incident_key": str(check.code), "event_type": event_type, "description": description, "client": "healthchecks.io", "client_url": settings.SITE_ROOT } url = "https://events.pagerduty.com/generic/2010-04-15/create_event.json" r = requests.post(url, data=json.dumps(payload), timeout=5) n.status = r.status_code n.save() elif self.kind == "po": tmpl = "integrations/pushover_message.html" ctx = { "check": check, "down_checks": self.user.check_set.filter(status="down").exclude(code=check.code).order_by("created"), } text = render_to_string(tmpl, ctx).strip() if check.status == "down": title = "%s is DOWN" % check.name_then_code() else: title = "%s is now UP" % check.name_then_code() user_key, priority, _ = self.po_value payload = { "token": settings.PUSHOVER_API_TOKEN, "user": user_key, "message": text, "title": title, "html": 1, "priority": priority, } if priority == 2: # Emergency notification payload["retry"] = settings.PUSHOVER_EMERGENCY_RETRY_DELAY payload["expire"] = settings.PUSHOVER_EMERGENCY_EXPIRATION url = "https://api.pushover.net/1/messages.json" r = requests.post(url, data=payload, timeout=5) n.status = r.status_code n.save()
def notify(self, check): n = Notification(owner=check, channel=self) n.check_status = check.status if self.kind == "email" and self.email_verified: ctx = { "check": check, "checks": self.user.check_set.order_by("created"), "now": timezone.now() } emails.alert(self.value, ctx) n.save() elif self.kind == "webhook" and check.status == "down": try: r = requests.get(self.value, timeout=5) n.status = r.status_code except requests.exceptions.Timeout: # Well, we tried pass n.save() elif self.kind == "slack": tmpl = "integrations/slack_message.html" text = render_to_string(tmpl, {"check": check}) payload = { "text": text, "username": "******", "icon_url": "https://healthchecks.io/static/img/[email protected]" } r = requests.post(self.value, json=payload, timeout=5) n.status = r.status_code n.save() elif self.kind == "hipchat": tmpl = "integrations/hipchat_message.html" text = render_to_string(tmpl, {"check": check}) payload = { "message": text, "color": "green" if check.status == "up" else "red", } r = requests.post(self.value, json=payload, timeout=5) n.status = r.status_code n.save() elif self.kind == "pd": if check.status == "down": event_type = "trigger" description = "%s is DOWN" % check.name_then_code() else: event_type = "resolve" description = "%s received a ping and is now UP" % \ check.name_then_code() payload = { "service_key": self.value, "incident_key": str(check.code), "event_type": event_type, "description": description, "client": "healthchecks.io", "client_url": settings.SITE_ROOT } url = "https://events.pagerduty.com/generic/2010-04-15/create_event.json" r = requests.post(url, data=json.dumps(payload), timeout=5) n.status = r.status_code n.save()