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.

654 lines
21 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
6 years ago
7 years ago
6 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.db import models
  9. from django.urls import reverse
  10. from django.utils import timezone
  11. from hc.accounts.models import Project
  12. from hc.api import transports
  13. from hc.lib import emails
  14. import pytz
  15. STATUSES = (("up", "Up"), ("down", "Down"), ("new", "New"), ("paused", "Paused"))
  16. DEFAULT_TIMEOUT = td(days=1)
  17. DEFAULT_GRACE = td(hours=1)
  18. NEVER = datetime(3000, 1, 1, tzinfo=pytz.UTC)
  19. CHECK_KINDS = (("simple", "Simple"), ("cron", "Cron"))
  20. CHANNEL_KINDS = (
  21. ("email", "Email"),
  22. ("webhook", "Webhook"),
  23. ("hipchat", "HipChat"),
  24. ("slack", "Slack"),
  25. ("pd", "PagerDuty"),
  26. ("pagertree", "PagerTree"),
  27. ("pagerteam", "Pager Team"),
  28. ("po", "Pushover"),
  29. ("pushbullet", "Pushbullet"),
  30. ("opsgenie", "OpsGenie"),
  31. ("victorops", "VictorOps"),
  32. ("discord", "Discord"),
  33. ("telegram", "Telegram"),
  34. ("sms", "SMS"),
  35. ("zendesk", "Zendesk"),
  36. ("trello", "Trello"),
  37. ("matrix", "Matrix"),
  38. )
  39. PO_PRIORITIES = {-2: "lowest", -1: "low", 0: "normal", 1: "high", 2: "emergency"}
  40. def isostring(dt):
  41. """Convert the datetime to ISO 8601 format with no microseconds. """
  42. if dt:
  43. return dt.replace(microsecond=0).isoformat()
  44. class Check(models.Model):
  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, unique=True)
  48. desc = models.TextField(blank=True)
  49. project = models.ForeignKey(Project, models.CASCADE)
  50. created = models.DateTimeField(auto_now_add=True)
  51. kind = models.CharField(max_length=10, default="simple", 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. subject = models.CharField(max_length=100, blank=True)
  57. n_pings = models.IntegerField(default=0)
  58. last_ping = models.DateTimeField(null=True, blank=True)
  59. last_start = models.DateTimeField(null=True, blank=True)
  60. last_ping_was_fail = models.NullBooleanField(default=False)
  61. has_confirmation_link = models.BooleanField(default=False)
  62. alert_after = models.DateTimeField(null=True, blank=True, editable=False)
  63. status = models.CharField(max_length=6, choices=STATUSES, default="new")
  64. def __str__(self):
  65. return "%s (%d)" % (self.name or self.code, self.id)
  66. def name_then_code(self):
  67. if self.name:
  68. return self.name
  69. return str(self.code)
  70. def url(self):
  71. return settings.PING_ENDPOINT + str(self.code)
  72. def details_url(self):
  73. return settings.SITE_ROOT + reverse("hc-details", args=[self.code])
  74. def email(self):
  75. return "%s@%s" % (self.code, settings.PING_EMAIL_DOMAIN)
  76. def get_grace_start(self):
  77. """ Return the datetime when the grace period starts.
  78. If the check is currently new, paused or down, return None.
  79. """
  80. # NEVER is a constant sentinel value (year 3000).
  81. # Using None instead would make the logic clunky.
  82. result = NEVER
  83. if self.kind == "simple" and self.status == "up":
  84. result = self.last_ping + self.timeout
  85. elif self.kind == "cron" and self.status == "up":
  86. # The complex case, next ping is expected based on cron schedule.
  87. # Don't convert to naive datetimes (and so avoid ambiguities around
  88. # DST transitions). Croniter will handle the timezone-aware datetimes.
  89. zone = pytz.timezone(self.tz)
  90. last_local = timezone.localtime(self.last_ping, zone)
  91. it = croniter(self.schedule, last_local)
  92. result = it.next(datetime)
  93. if self.last_start and self.status != "down":
  94. result = min(result, self.last_start)
  95. if result != NEVER:
  96. return result
  97. def going_down_after(self):
  98. """ Return the datetime when the check goes down.
  99. If the check is new or paused, and not currently running, return None.
  100. If the check is already down, also return None.
  101. """
  102. grace_start = self.get_grace_start()
  103. if grace_start is not None:
  104. return grace_start + self.grace
  105. def get_status(self, now=None, with_started=True):
  106. """ Return current status for display. """
  107. if now is None:
  108. now = timezone.now()
  109. if self.last_start:
  110. if now >= self.last_start + self.grace:
  111. return "down"
  112. elif with_started:
  113. return "started"
  114. if self.status in ("new", "paused", "down"):
  115. return self.status
  116. grace_start = self.get_grace_start()
  117. grace_end = grace_start + self.grace
  118. if now >= grace_end:
  119. return "down"
  120. if now >= grace_start:
  121. return "grace"
  122. return "up"
  123. def assign_all_channels(self):
  124. channels = Channel.objects.filter(project=self.project)
  125. self.channel_set.set(channels)
  126. def tags_list(self):
  127. return [t.strip() for t in self.tags.split(" ") if t.strip()]
  128. def matches_tag_set(self, tag_set):
  129. return tag_set.issubset(self.tags_list())
  130. def to_dict(self):
  131. update_rel_url = reverse("hc-api-update", args=[self.code])
  132. pause_rel_url = reverse("hc-api-pause", args=[self.code])
  133. channel_codes = [str(ch.code) for ch in self.channel_set.all()]
  134. result = {
  135. "name": self.name,
  136. "ping_url": self.url(),
  137. "update_url": settings.SITE_ROOT + update_rel_url,
  138. "pause_url": settings.SITE_ROOT + pause_rel_url,
  139. "tags": self.tags,
  140. "grace": int(self.grace.total_seconds()),
  141. "n_pings": self.n_pings,
  142. "status": self.get_status(),
  143. "channels": ",".join(sorted(channel_codes)),
  144. "last_ping": isostring(self.last_ping),
  145. "next_ping": isostring(self.get_grace_start()),
  146. "desc": self.desc,
  147. }
  148. if self.kind == "simple":
  149. result["timeout"] = int(self.timeout.total_seconds())
  150. elif self.kind == "cron":
  151. result["schedule"] = self.schedule
  152. result["tz"] = self.tz
  153. return result
  154. def ping(self, remote_addr, scheme, method, ua, body, action):
  155. if action == "start":
  156. self.last_start = timezone.now()
  157. # Don't update "last_ping" field.
  158. elif action == "ign":
  159. pass
  160. else:
  161. self.last_start = None
  162. self.last_ping = timezone.now()
  163. new_status = "down" if action == "fail" else "up"
  164. if self.status != new_status:
  165. flip = Flip(owner=self)
  166. flip.created = self.last_ping
  167. flip.old_status = self.status
  168. flip.new_status = new_status
  169. flip.save()
  170. self.status = new_status
  171. self.alert_after = self.going_down_after()
  172. self.n_pings = models.F("n_pings") + 1
  173. self.has_confirmation_link = "confirm" in str(body).lower()
  174. self.save()
  175. self.refresh_from_db()
  176. ping = Ping(owner=self)
  177. ping.n = self.n_pings
  178. if action in ("start", "fail", "ign"):
  179. ping.kind = action
  180. ping.remote_addr = remote_addr
  181. ping.scheme = scheme
  182. ping.method = method
  183. # If User-Agent is longer than 200 characters, truncate it:
  184. ping.ua = ua[:200]
  185. ping.body = body[:10000]
  186. ping.save()
  187. class Ping(models.Model):
  188. id = models.BigAutoField(primary_key=True)
  189. n = models.IntegerField(null=True)
  190. owner = models.ForeignKey(Check, models.CASCADE)
  191. created = models.DateTimeField(auto_now_add=True)
  192. kind = models.CharField(max_length=6, blank=True, null=True)
  193. scheme = models.CharField(max_length=10, default="http")
  194. remote_addr = models.GenericIPAddressField(blank=True, null=True)
  195. method = models.CharField(max_length=10, blank=True)
  196. ua = models.CharField(max_length=200, blank=True)
  197. body = models.CharField(max_length=10000, blank=True, null=True)
  198. class Channel(models.Model):
  199. name = models.CharField(max_length=100, blank=True)
  200. code = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
  201. project = models.ForeignKey(Project, models.CASCADE)
  202. created = models.DateTimeField(auto_now_add=True)
  203. kind = models.CharField(max_length=20, choices=CHANNEL_KINDS)
  204. value = models.TextField(blank=True)
  205. email_verified = models.BooleanField(default=False)
  206. checks = models.ManyToManyField(Check)
  207. def __str__(self):
  208. if self.name:
  209. return self.name
  210. if self.kind == "email":
  211. return "Email to %s" % self.email_value
  212. elif self.kind == "sms":
  213. return "SMS to %s" % self.sms_number
  214. elif self.kind == "slack":
  215. return "Slack %s" % self.slack_channel
  216. elif self.kind == "telegram":
  217. return "Telegram %s" % self.telegram_name
  218. return self.get_kind_display()
  219. def to_dict(self):
  220. return {"id": str(self.code), "name": self.name, "kind": self.kind}
  221. def assign_all_checks(self):
  222. checks = Check.objects.filter(project=self.project)
  223. self.checks.add(*checks)
  224. def make_token(self):
  225. seed = "%s%s" % (self.code, settings.SECRET_KEY)
  226. seed = seed.encode()
  227. return hashlib.sha1(seed).hexdigest()
  228. def send_verify_link(self):
  229. args = [self.code, self.make_token()]
  230. verify_link = reverse("hc-verify-email", args=args)
  231. verify_link = settings.SITE_ROOT + verify_link
  232. emails.verify_email(self.email_value, {"verify_link": verify_link})
  233. def get_unsub_link(self):
  234. args = [self.code, self.make_token()]
  235. verify_link = reverse("hc-unsubscribe-alerts", args=args)
  236. return settings.SITE_ROOT + verify_link
  237. @property
  238. def transport(self):
  239. if self.kind == "email":
  240. return transports.Email(self)
  241. elif self.kind == "webhook":
  242. return transports.Webhook(self)
  243. elif self.kind == "slack":
  244. return transports.Slack(self)
  245. elif self.kind == "hipchat":
  246. return transports.HipChat(self)
  247. elif self.kind == "pd":
  248. return transports.PagerDuty(self)
  249. elif self.kind == "pagertree":
  250. return transports.PagerTree(self)
  251. elif self.kind == "pagerteam":
  252. return transports.PagerTeam(self)
  253. elif self.kind == "victorops":
  254. return transports.VictorOps(self)
  255. elif self.kind == "pushbullet":
  256. return transports.Pushbullet(self)
  257. elif self.kind == "po":
  258. return transports.Pushover(self)
  259. elif self.kind == "opsgenie":
  260. return transports.OpsGenie(self)
  261. elif self.kind == "discord":
  262. return transports.Discord(self)
  263. elif self.kind == "telegram":
  264. return transports.Telegram(self)
  265. elif self.kind == "sms":
  266. return transports.Sms(self)
  267. elif self.kind == "trello":
  268. return transports.Trello(self)
  269. elif self.kind == "matrix":
  270. return transports.Matrix(self)
  271. else:
  272. raise NotImplementedError("Unknown channel kind: %s" % self.kind)
  273. def notify(self, check):
  274. if self.transport.is_noop(check):
  275. return "no-op"
  276. n = Notification(owner=check, channel=self)
  277. n.check_status = check.status
  278. n.error = "Sending"
  279. n.save()
  280. if self.kind == "email":
  281. error = self.transport.notify(check, n.bounce_url()) or ""
  282. else:
  283. error = self.transport.notify(check) or ""
  284. n.error = error
  285. n.save()
  286. return error
  287. def icon_path(self):
  288. return "img/integrations/%s.png" % self.kind
  289. @property
  290. def po_priority(self):
  291. assert self.kind == "po"
  292. parts = self.value.split("|")
  293. prio = int(parts[1])
  294. return PO_PRIORITIES[prio]
  295. def webhook_spec(self, status):
  296. assert self.kind == "webhook"
  297. if not self.value.startswith("{"):
  298. parts = self.value.split("\n")
  299. url_down = parts[0]
  300. url_up = parts[1] if len(parts) > 1 else ""
  301. post_data = parts[2] if len(parts) > 2 else ""
  302. return {
  303. "method": "POST" if post_data else "GET",
  304. "url": url_down if status == "down" else url_up,
  305. "body": post_data,
  306. "headers": {},
  307. }
  308. doc = json.loads(self.value)
  309. if "post_data" in doc:
  310. # Legacy "post_data" in doc -- use the legacy fields
  311. return {
  312. "method": "POST" if doc["post_data"] else "GET",
  313. "url": doc["url_down"] if status == "down" else doc["url_up"],
  314. "body": doc["post_data"],
  315. "headers": doc["headers"],
  316. }
  317. if status == "down" and "method_down" in doc:
  318. return {
  319. "method": doc["method_down"],
  320. "url": doc["url_down"],
  321. "body": doc["body_down"],
  322. "headers": doc["headers_down"],
  323. }
  324. elif status == "up" and "method_up" in doc:
  325. return {
  326. "method": doc["method_up"],
  327. "url": doc["url_up"],
  328. "body": doc["body_up"],
  329. "headers": doc["headers_up"],
  330. }
  331. @property
  332. def down_webhook_spec(self):
  333. return self.webhook_spec("down")
  334. @property
  335. def up_webhook_spec(self):
  336. return self.webhook_spec("up")
  337. @property
  338. def url_down(self):
  339. return self.down_webhook_spec["url"]
  340. @property
  341. def url_up(self):
  342. return self.up_webhook_spec["url"]
  343. @property
  344. def slack_team(self):
  345. assert self.kind == "slack"
  346. if not self.value.startswith("{"):
  347. return None
  348. doc = json.loads(self.value)
  349. return doc["team_name"]
  350. @property
  351. def slack_channel(self):
  352. assert self.kind == "slack"
  353. if not self.value.startswith("{"):
  354. return None
  355. doc = json.loads(self.value)
  356. return doc["incoming_webhook"]["channel"]
  357. @property
  358. def slack_webhook_url(self):
  359. assert self.kind == "slack"
  360. if not self.value.startswith("{"):
  361. return self.value
  362. doc = json.loads(self.value)
  363. return doc["incoming_webhook"]["url"]
  364. @property
  365. def discord_webhook_url(self):
  366. assert self.kind == "discord"
  367. doc = json.loads(self.value)
  368. return doc["webhook"]["url"]
  369. @property
  370. def discord_webhook_id(self):
  371. assert self.kind == "discord"
  372. doc = json.loads(self.value)
  373. return doc["webhook"]["id"]
  374. @property
  375. def telegram_id(self):
  376. assert self.kind == "telegram"
  377. doc = json.loads(self.value)
  378. return doc.get("id")
  379. @property
  380. def telegram_type(self):
  381. assert self.kind == "telegram"
  382. doc = json.loads(self.value)
  383. return doc.get("type")
  384. @property
  385. def telegram_name(self):
  386. assert self.kind == "telegram"
  387. doc = json.loads(self.value)
  388. return doc.get("name")
  389. @property
  390. def pd_service_key(self):
  391. assert self.kind == "pd"
  392. if not self.value.startswith("{"):
  393. return self.value
  394. doc = json.loads(self.value)
  395. return doc["service_key"]
  396. @property
  397. def pd_account(self):
  398. assert self.kind == "pd"
  399. if self.value.startswith("{"):
  400. doc = json.loads(self.value)
  401. return doc["account"]
  402. def latest_notification(self):
  403. return Notification.objects.filter(channel=self).latest()
  404. @property
  405. def sms_number(self):
  406. assert self.kind == "sms"
  407. if self.value.startswith("{"):
  408. doc = json.loads(self.value)
  409. return doc["value"]
  410. return self.value
  411. @property
  412. def sms_label(self):
  413. assert self.kind == "sms"
  414. if self.value.startswith("{"):
  415. doc = json.loads(self.value)
  416. return doc["label"]
  417. @property
  418. def trello_token(self):
  419. assert self.kind == "trello"
  420. if self.value.startswith("{"):
  421. doc = json.loads(self.value)
  422. return doc["token"]
  423. @property
  424. def trello_board_list(self):
  425. assert self.kind == "trello"
  426. if self.value.startswith("{"):
  427. doc = json.loads(self.value)
  428. return doc["board_name"], doc["list_name"]
  429. @property
  430. def trello_list_id(self):
  431. assert self.kind == "trello"
  432. if self.value.startswith("{"):
  433. doc = json.loads(self.value)
  434. return doc["list_id"]
  435. @property
  436. def email_value(self):
  437. assert self.kind == "email"
  438. if not self.value.startswith("{"):
  439. return self.value
  440. doc = json.loads(self.value)
  441. return doc.get("value")
  442. @property
  443. def email_notify_up(self):
  444. assert self.kind == "email"
  445. if not self.value.startswith("{"):
  446. return True
  447. doc = json.loads(self.value)
  448. return doc.get("up")
  449. @property
  450. def email_notify_down(self):
  451. assert self.kind == "email"
  452. if not self.value.startswith("{"):
  453. return True
  454. doc = json.loads(self.value)
  455. return doc.get("down")
  456. class Notification(models.Model):
  457. class Meta:
  458. get_latest_by = "created"
  459. code = models.UUIDField(default=uuid.uuid4, null=True, editable=False)
  460. owner = models.ForeignKey(Check, models.CASCADE)
  461. check_status = models.CharField(max_length=6)
  462. channel = models.ForeignKey(Channel, models.CASCADE)
  463. created = models.DateTimeField(auto_now_add=True)
  464. error = models.CharField(max_length=200, blank=True)
  465. def bounce_url(self):
  466. return settings.SITE_ROOT + reverse("hc-api-bounce", args=[self.code])
  467. class Flip(models.Model):
  468. owner = models.ForeignKey(Check, models.CASCADE)
  469. created = models.DateTimeField()
  470. processed = models.DateTimeField(null=True, blank=True, db_index=True)
  471. old_status = models.CharField(max_length=8, choices=STATUSES)
  472. new_status = models.CharField(max_length=8, choices=STATUSES)
  473. def send_alerts(self):
  474. if self.new_status == "up" and self.old_status in ("new", "paused"):
  475. # Don't send alerts on new->up and paused->up transitions
  476. return []
  477. if self.new_status not in ("up", "down"):
  478. raise NotImplementedError("Unexpected status: %s" % self.status)
  479. errors = []
  480. for channel in self.owner.channel_set.all():
  481. error = channel.notify(self.owner)
  482. if error not in ("", "no-op"):
  483. errors.append((channel, error))
  484. return errors
  485. class TokenBucket(models.Model):
  486. value = models.CharField(max_length=80, unique=True)
  487. tokens = models.FloatField(default=1.0)
  488. updated = models.DateTimeField(default=timezone.now)
  489. @staticmethod
  490. def authorize(value, capacity, refill_time_secs):
  491. now = timezone.now()
  492. obj, created = TokenBucket.objects.get_or_create(value=value)
  493. if not created:
  494. # Top up the bucket:
  495. delta_secs = (now - obj.updated).total_seconds()
  496. obj.tokens = min(1.0, obj.tokens + delta_secs / refill_time_secs)
  497. obj.tokens -= 1.0 / capacity
  498. if obj.tokens < 0:
  499. # Not enough tokens
  500. return False
  501. # Race condition: two concurrent authorize calls can overwrite each
  502. # other's changes. It's OK to be a little inexact here for the sake
  503. # of simplicity.
  504. obj.updated = now
  505. obj.save()
  506. return True
  507. @staticmethod
  508. def authorize_login_email(email):
  509. # remove dots and alias:
  510. mailbox, domain = email.split("@")
  511. mailbox = mailbox.replace(".", "")
  512. mailbox = mailbox.split("+")[0]
  513. email = mailbox + "@" + domain
  514. salted_encoded = (email + settings.SECRET_KEY).encode()
  515. value = "em-%s" % hashlib.sha1(salted_encoded).hexdigest()
  516. # 20 login attempts for a single email per hour:
  517. return TokenBucket.authorize(value, 20, 3600)
  518. @staticmethod
  519. def authorize_invite(user):
  520. value = "invite-%d" % user.id
  521. # 20 invites per day
  522. return TokenBucket.authorize(value, 20, 3600 * 24)
  523. @staticmethod
  524. def authorize_login_password(email):
  525. salted_encoded = (email + settings.SECRET_KEY).encode()
  526. value = "pw-%s" % hashlib.sha1(salted_encoded).hexdigest()
  527. # 20 password attempts per day
  528. return TokenBucket.authorize(value, 20, 3600 * 24)