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.

325 lines
10 KiB

10 years ago
10 years ago
10 years ago
8 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
  1. # coding: utf-8
  2. import hashlib
  3. import json
  4. import uuid
  5. from datetime import datetime, timedelta as td
  6. from croniter import croniter
  7. from django.conf import settings
  8. from django.core.checks import Warning
  9. from django.contrib.auth.models import User
  10. from django.db import models
  11. from django.urls import reverse
  12. from django.utils import timezone
  13. from hc.api import transports
  14. from hc.lib import emails
  15. STATUSES = (
  16. ("up", "Up"),
  17. ("down", "Down"),
  18. ("new", "New"),
  19. ("paused", "Paused")
  20. )
  21. DEFAULT_TIMEOUT = td(days=1)
  22. DEFAULT_GRACE = td(hours=1)
  23. CHECK_KINDS = (("simple", "Simple"),
  24. ("cron", "Cron"))
  25. CHANNEL_KINDS = (("email", "Email"),
  26. ("webhook", "Webhook"),
  27. ("hipchat", "HipChat"),
  28. ("slack", "Slack"),
  29. ("pd", "PagerDuty"),
  30. ("po", "Pushover"),
  31. ("pushbullet", "Pushbullet"),
  32. ("opsgenie", "OpsGenie"),
  33. ("victorops", "VictorOps"))
  34. PO_PRIORITIES = {
  35. -2: "lowest",
  36. -1: "low",
  37. 0: "normal",
  38. 1: "high",
  39. 2: "emergency"
  40. }
  41. class Check(models.Model):
  42. class Meta:
  43. # sendalerts command will query using these
  44. index_together = ["status", "user", "alert_after"]
  45. name = models.CharField(max_length=100, blank=True)
  46. tags = models.CharField(max_length=500, blank=True)
  47. code = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True)
  48. user = models.ForeignKey(User, blank=True, null=True)
  49. created = models.DateTimeField(auto_now_add=True)
  50. kind = models.CharField(max_length=10, default="simple",
  51. choices=CHECK_KINDS)
  52. timeout = models.DurationField(default=DEFAULT_TIMEOUT)
  53. grace = models.DurationField(default=DEFAULT_GRACE)
  54. schedule = models.CharField(max_length=100, default="* * * * *")
  55. tz = models.CharField(max_length=36, default="UTC")
  56. n_pings = models.IntegerField(default=0)
  57. last_ping = models.DateTimeField(null=True, blank=True)
  58. alert_after = models.DateTimeField(null=True, blank=True, editable=False)
  59. status = models.CharField(max_length=6, choices=STATUSES, default="new")
  60. def name_then_code(self):
  61. if self.name:
  62. return self.name
  63. return str(self.code)
  64. def url(self):
  65. return settings.PING_ENDPOINT + str(self.code)
  66. def log_url(self):
  67. return settings.SITE_ROOT + reverse("hc-log", args=[self.code])
  68. def email(self):
  69. return "%s@%s" % (self.code, settings.PING_EMAIL_DOMAIN)
  70. def send_alert(self):
  71. if self.status not in ("up", "down"):
  72. raise NotImplementedError("Unexpected status: %s" % self.status)
  73. errors = []
  74. for channel in self.channel_set.all():
  75. error = channel.notify(self)
  76. if error not in ("", "no-op"):
  77. errors.append((channel, error))
  78. return errors
  79. def get_grace_start(self):
  80. """ Return the datetime when grace period starts. """
  81. # The common case, grace starts after timeout
  82. if self.kind == "simple":
  83. return self.last_ping + self.timeout
  84. # The complex case, next ping is expected based on cron schedule
  85. with timezone.override(self.tz):
  86. last_naive = timezone.make_naive(self.last_ping)
  87. it = croniter(self.schedule, last_naive)
  88. next_naive = it.get_next(datetime)
  89. return timezone.make_aware(next_naive, is_dst=False)
  90. def get_status(self, now=None):
  91. """ Return "up" if the check is up or in grace, otherwise "down". """
  92. if self.status in ("new", "paused"):
  93. return self.status
  94. if now is None:
  95. now = timezone.now()
  96. return "up" if self.get_grace_start() + self.grace > now else "down"
  97. def get_alert_after(self):
  98. """ Return the datetime when check potentially goes down. """
  99. return self.get_grace_start() + self.grace
  100. def in_grace_period(self):
  101. """ Return True if check is currently in grace period. """
  102. if self.status in ("new", "paused"):
  103. return False
  104. grace_start = self.get_grace_start()
  105. grace_end = grace_start + self.grace
  106. return grace_start < timezone.now() < grace_end
  107. def assign_all_channels(self):
  108. if self.user:
  109. channels = Channel.objects.filter(user=self.user)
  110. self.channel_set.add(*channels)
  111. def tags_list(self):
  112. return [t.strip() for t in self.tags.split(" ") if t.strip()]
  113. def to_dict(self):
  114. pause_rel_url = reverse("hc-api-pause", args=[self.code])
  115. result = {
  116. "name": self.name,
  117. "ping_url": self.url(),
  118. "pause_url": settings.SITE_ROOT + pause_rel_url,
  119. "tags": self.tags,
  120. "timeout": int(self.timeout.total_seconds()),
  121. "grace": int(self.grace.total_seconds()),
  122. "n_pings": self.n_pings,
  123. "status": self.get_status()
  124. }
  125. if self.last_ping:
  126. result["last_ping"] = self.last_ping.isoformat()
  127. result["next_ping"] = (self.last_ping + self.timeout).isoformat()
  128. else:
  129. result["last_ping"] = None
  130. result["next_ping"] = None
  131. return result
  132. @classmethod
  133. def check(cls, **kwargs):
  134. errors = super(Check, cls).check(**kwargs)
  135. trigger_detected = False
  136. try:
  137. dummy = Check(last_ping=timezone.now())
  138. dummy.save()
  139. dummy.refresh_from_db()
  140. trigger_detected = bool(dummy.alert_after)
  141. dummy.delete()
  142. except:
  143. pass
  144. if trigger_detected:
  145. err = Warning(
  146. "Obsolete 'update_alert_after' trigger exists in database.",
  147. hint="Please remove the trigger with 'manage.py droptriggers'",
  148. id="hc.api.E001")
  149. errors.append(err)
  150. return errors
  151. class Ping(models.Model):
  152. n = models.IntegerField(null=True)
  153. owner = models.ForeignKey(Check)
  154. created = models.DateTimeField(auto_now_add=True)
  155. scheme = models.CharField(max_length=10, default="http")
  156. remote_addr = models.GenericIPAddressField(blank=True, null=True)
  157. method = models.CharField(max_length=10, blank=True)
  158. ua = models.CharField(max_length=200, blank=True)
  159. class Channel(models.Model):
  160. code = models.UUIDField(default=uuid.uuid4, editable=False)
  161. user = models.ForeignKey(User)
  162. created = models.DateTimeField(auto_now_add=True)
  163. kind = models.CharField(max_length=20, choices=CHANNEL_KINDS)
  164. value = models.TextField(blank=True)
  165. email_verified = models.BooleanField(default=False)
  166. checks = models.ManyToManyField(Check)
  167. def assign_all_checks(self):
  168. checks = Check.objects.filter(user=self.user)
  169. self.checks.add(*checks)
  170. def make_token(self):
  171. seed = "%s%s" % (self.code, settings.SECRET_KEY)
  172. seed = seed.encode("utf8")
  173. return hashlib.sha1(seed).hexdigest()
  174. def send_verify_link(self):
  175. args = [self.code, self.make_token()]
  176. verify_link = reverse("hc-verify-email", args=args)
  177. verify_link = settings.SITE_ROOT + verify_link
  178. emails.verify_email(self.value, {"verify_link": verify_link})
  179. @property
  180. def transport(self):
  181. if self.kind == "email":
  182. return transports.Email(self)
  183. elif self.kind == "webhook":
  184. return transports.Webhook(self)
  185. elif self.kind == "slack":
  186. return transports.Slack(self)
  187. elif self.kind == "hipchat":
  188. return transports.HipChat(self)
  189. elif self.kind == "pd":
  190. return transports.PagerDuty(self)
  191. elif self.kind == "victorops":
  192. return transports.VictorOps(self)
  193. elif self.kind == "pushbullet":
  194. return transports.Pushbullet(self)
  195. elif self.kind == "po":
  196. return transports.Pushover(self)
  197. elif self.kind == "opsgenie":
  198. return transports.OpsGenie(self)
  199. else:
  200. raise NotImplementedError("Unknown channel kind: %s" % self.kind)
  201. def notify(self, check):
  202. # Make 3 attempts--
  203. for x in range(0, 3):
  204. error = self.transport.notify(check) or ""
  205. if error in ("", "no-op"):
  206. break # Success!
  207. if error != "no-op":
  208. n = Notification(owner=check, channel=self)
  209. n.check_status = check.status
  210. n.error = error
  211. n.save()
  212. return error
  213. def test(self):
  214. return self.transport().test()
  215. @property
  216. def po_value(self):
  217. assert self.kind == "po"
  218. user_key, prio = self.value.split("|")
  219. prio = int(prio)
  220. return user_key, prio, PO_PRIORITIES[prio]
  221. @property
  222. def value_down(self):
  223. assert self.kind == "webhook"
  224. parts = self.value.split("\n")
  225. return parts[0]
  226. @property
  227. def value_up(self):
  228. assert self.kind == "webhook"
  229. parts = self.value.split("\n")
  230. return parts[1] if len(parts) == 2 else ""
  231. @property
  232. def slack_team(self):
  233. assert self.kind == "slack"
  234. if not self.value.startswith("{"):
  235. return None
  236. doc = json.loads(self.value)
  237. return doc["team_name"]
  238. @property
  239. def slack_channel(self):
  240. assert self.kind == "slack"
  241. if not self.value.startswith("{"):
  242. return None
  243. doc = json.loads(self.value)
  244. return doc["incoming_webhook"]["channel"]
  245. @property
  246. def slack_webhook_url(self):
  247. assert self.kind == "slack"
  248. if not self.value.startswith("{"):
  249. return self.value
  250. doc = json.loads(self.value)
  251. return doc["incoming_webhook"]["url"]
  252. def latest_notification(self):
  253. return Notification.objects.filter(channel=self).latest()
  254. class Notification(models.Model):
  255. class Meta:
  256. get_latest_by = "created"
  257. owner = models.ForeignKey(Check)
  258. check_status = models.CharField(max_length=6)
  259. channel = models.ForeignKey(Channel)
  260. created = models.DateTimeField(auto_now_add=True)
  261. error = models.CharField(max_length=200, blank=True)