You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
9.8 KiB

  1. from django.conf import settings
  2. from django.template.loader import render_to_string
  3. from django.utils import timezone
  4. import json
  5. import requests
  6. from six.moves.urllib.parse import quote
  7. from hc.accounts.models import Profile
  8. from hc.lib import emails
  9. def tmpl(template_name, **ctx):
  10. template_path = "integrations/%s" % template_name
  11. # \xa0 is non-breaking space. It causes SMS messages to use UCS2 encoding
  12. # and cost twice the money.
  13. return render_to_string(template_path, ctx).strip().replace(u"\xa0", " ")
  14. class Transport(object):
  15. def __init__(self, channel):
  16. self.channel = channel
  17. def notify(self, check):
  18. """ Send notification about current status of the check.
  19. This method returns None on success, and error message
  20. on error.
  21. """
  22. raise NotImplementedError()
  23. def is_noop(self, check):
  24. """ Return True if transport will ignore check's current status.
  25. This method is overriden in Webhook subclass where the user can
  26. configure webhook urls for "up" and "down" events, and both are
  27. optional.
  28. """
  29. return False
  30. def checks(self):
  31. return self.channel.user.check_set.order_by("created")
  32. class Email(Transport):
  33. def notify(self, check, bounce_url):
  34. if not self.channel.email_verified:
  35. return "Email not verified"
  36. headers = {"X-Bounce-Url": bounce_url}
  37. try:
  38. # Look up the sorting preference for this email address
  39. p = Profile.objects.get(user__email=self.channel.value)
  40. sort = p.sort
  41. except Profile.DoesNotExist:
  42. # Default sort order is by check's creation time
  43. sort = "created"
  44. ctx = {
  45. "check": check,
  46. "checks": self.checks(),
  47. "sort": sort,
  48. "now": timezone.now(),
  49. "unsub_link": self.channel.get_unsub_link()
  50. }
  51. emails.alert(self.channel.value, ctx, headers)
  52. class HttpTransport(Transport):
  53. @classmethod
  54. def _request(cls, method, url, **kwargs):
  55. try:
  56. options = dict(kwargs)
  57. if "headers" not in options:
  58. options["headers"] = {}
  59. options["timeout"] = 5
  60. options["headers"]["User-Agent"] = "healthchecks.io"
  61. r = requests.request(method, url, **options)
  62. if r.status_code not in (200, 201, 204):
  63. return "Received status code %d" % r.status_code
  64. except requests.exceptions.Timeout:
  65. # Well, we tried
  66. return "Connection timed out"
  67. except requests.exceptions.ConnectionError:
  68. return "Connection failed"
  69. @classmethod
  70. def get(cls, url):
  71. # Make 3 attempts--
  72. for x in range(0, 3):
  73. error = cls._request("get", url)
  74. if error is None:
  75. break
  76. return error
  77. @classmethod
  78. def post(cls, url, **kwargs):
  79. # Make 3 attempts--
  80. for x in range(0, 3):
  81. error = cls._request("post", url, **kwargs)
  82. if error is None:
  83. break
  84. return error
  85. class Webhook(HttpTransport):
  86. def prepare(self, template, check, urlencode=False):
  87. """ Replace variables with actual values.
  88. There should be no bad translations if users use $ symbol in
  89. check's name or tags, because $ gets urlencoded to %24
  90. """
  91. def safe(s):
  92. return quote(s) if urlencode else s
  93. result = template
  94. if "$CODE" in result:
  95. result = result.replace("$CODE", str(check.code))
  96. if "$STATUS" in result:
  97. result = result.replace("$STATUS", check.status)
  98. if "$NOW" in result:
  99. s = timezone.now().replace(microsecond=0).isoformat()
  100. result = result.replace("$NOW", safe(s))
  101. if "$NAME" in result:
  102. result = result.replace("$NAME", safe(check.name))
  103. if "$TAG" in result:
  104. for i, tag in enumerate(check.tags_list()):
  105. placeholder = "$TAG%d" % (i + 1)
  106. result = result.replace(placeholder, safe(tag))
  107. return result
  108. def is_noop(self, check):
  109. if check.status == "down" and not self.channel.value_down:
  110. return True
  111. if check.status == "up" and not self.channel.value_up:
  112. return True
  113. return False
  114. def notify(self, check):
  115. url = self.channel.value_down
  116. if check.status == "up":
  117. url = self.channel.value_up
  118. assert url
  119. url = self.prepare(url, check, urlencode=True)
  120. if self.channel.post_data:
  121. payload = self.prepare(self.channel.post_data, check)
  122. return self.post(url, data=payload.encode("utf-8"))
  123. else:
  124. return self.get(url)
  125. class Slack(HttpTransport):
  126. def notify(self, check):
  127. text = tmpl("slack_message.json", check=check)
  128. payload = json.loads(text)
  129. return self.post(self.channel.slack_webhook_url, json=payload)
  130. class HipChat(HttpTransport):
  131. def notify(self, check):
  132. text = tmpl("hipchat_message.json", check=check)
  133. payload = json.loads(text)
  134. self.channel.refresh_hipchat_access_token()
  135. return self.post(self.channel.hipchat_webhook_url, json=payload)
  136. class OpsGenie(HttpTransport):
  137. def notify(self, check):
  138. payload = {
  139. "apiKey": self.channel.value,
  140. "alias": str(check.code),
  141. "source": "healthchecks.io"
  142. }
  143. if check.status == "down":
  144. payload["tags"] = ",".join(check.tags_list())
  145. payload["message"] = tmpl("opsgenie_message.html", check=check)
  146. payload["note"] = tmpl("opsgenie_note.html", check=check)
  147. url = "https://api.opsgenie.com/v1/json/alert"
  148. if check.status == "up":
  149. url += "/close"
  150. return self.post(url, json=payload)
  151. class PagerDuty(HttpTransport):
  152. URL = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
  153. def notify(self, check):
  154. description = tmpl("pd_description.html", check=check)
  155. payload = {
  156. "vendor": settings.PD_VENDOR_KEY,
  157. "service_key": self.channel.pd_service_key,
  158. "incident_key": str(check.code),
  159. "event_type": "trigger" if check.status == "down" else "resolve",
  160. "description": description,
  161. "client": settings.SITE_NAME,
  162. "client_url": settings.SITE_ROOT
  163. }
  164. return self.post(self.URL, json=payload)
  165. class Pushbullet(HttpTransport):
  166. def notify(self, check):
  167. text = tmpl("pushbullet_message.html", check=check)
  168. url = "https://api.pushbullet.com/v2/pushes"
  169. headers = {
  170. "Access-Token": self.channel.value,
  171. "Conent-Type": "application/json"
  172. }
  173. payload = {
  174. "type": "note",
  175. "title": "healthchecks.io",
  176. "body": text
  177. }
  178. return self.post(url, json=payload, headers=headers)
  179. class Pushover(HttpTransport):
  180. URL = "https://api.pushover.net/1/messages.json"
  181. def notify(self, check):
  182. others = self.checks().filter(status="down").exclude(code=check.code)
  183. ctx = {
  184. "check": check,
  185. "down_checks": others,
  186. }
  187. text = tmpl("pushover_message.html", **ctx)
  188. title = tmpl("pushover_title.html", **ctx)
  189. user_key, prio = self.channel.value.split("|")
  190. payload = {
  191. "token": settings.PUSHOVER_API_TOKEN,
  192. "user": user_key,
  193. "message": text,
  194. "title": title,
  195. "html": 1,
  196. "priority": int(prio),
  197. }
  198. # Emergency notification
  199. if prio == "2":
  200. payload["retry"] = settings.PUSHOVER_EMERGENCY_RETRY_DELAY
  201. payload["expire"] = settings.PUSHOVER_EMERGENCY_EXPIRATION
  202. return self.post(self.URL, data=payload)
  203. class VictorOps(HttpTransport):
  204. def notify(self, check):
  205. description = tmpl("victorops_description.html", check=check)
  206. mtype = "CRITICAL" if check.status == "down" else "RECOVERY"
  207. payload = {
  208. "entity_id": str(check.code),
  209. "message_type": mtype,
  210. "entity_display_name": check.name_then_code(),
  211. "state_message": description,
  212. "monitoring_tool": "healthchecks.io",
  213. }
  214. return self.post(self.channel.value, json=payload)
  215. class Discord(HttpTransport):
  216. def notify(self, check):
  217. text = tmpl("slack_message.json", check=check)
  218. payload = json.loads(text)
  219. url = self.channel.discord_webhook_url + "/slack"
  220. return self.post(url, json=payload)
  221. class Telegram(HttpTransport):
  222. SM = "https://api.telegram.org/bot%s/sendMessage" % settings.TELEGRAM_TOKEN
  223. @classmethod
  224. def send(cls, chat_id, text):
  225. return cls.post(cls.SM, json={
  226. "chat_id": chat_id,
  227. "text": text,
  228. "parse_mode": "html"
  229. })
  230. def notify(self, check):
  231. text = tmpl("telegram_message.html", check=check)
  232. return self.send(self.channel.telegram_id, text)
  233. class Sms(HttpTransport):
  234. URL = 'https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json'
  235. def is_noop(self, check):
  236. return check.status != "down"
  237. def notify(self, check):
  238. profile = Profile.objects.for_user(self.channel.user)
  239. if not profile.authorize_sms():
  240. return "Monthly SMS limit exceeded"
  241. url = self.URL % settings.TWILIO_ACCOUNT
  242. auth = (settings.TWILIO_ACCOUNT, settings.TWILIO_AUTH)
  243. text = tmpl("sms_message.html", check=check,
  244. site_name=settings.SITE_NAME)
  245. data = {
  246. 'From': settings.TWILIO_FROM,
  247. 'To': self.channel.value,
  248. 'Body': text,
  249. }
  250. return self.post(url, data=data, auth=auth)