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.

952 lines
30 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
4 years ago
10 years ago
4 years ago
6 years ago
7 years ago
6 years ago
  1. # coding: utf-8
  2. import hashlib
  3. import json
  4. import time
  5. import uuid
  6. from datetime import datetime, timedelta as td
  7. from croniter import croniter
  8. from django.conf import settings
  9. from django.core.signing import TimestampSigner
  10. from django.db import models
  11. from django.urls import reverse
  12. from django.utils import timezone
  13. from hc.accounts.models import Project
  14. from hc.api import transports
  15. from hc.lib import emails
  16. from hc.lib.date import month_boundaries
  17. import pytz
  18. STATUSES = (("up", "Up"), ("down", "Down"), ("new", "New"), ("paused", "Paused"))
  19. DEFAULT_TIMEOUT = td(days=1)
  20. DEFAULT_GRACE = td(hours=1)
  21. NEVER = datetime(3000, 1, 1, tzinfo=pytz.UTC)
  22. CHECK_KINDS = (("simple", "Simple"), ("cron", "Cron"))
  23. # max time between start and ping where we will consider both events related:
  24. MAX_DELTA = td(hours=24)
  25. CHANNEL_KINDS = (
  26. ("email", "Email"),
  27. ("webhook", "Webhook"),
  28. ("hipchat", "HipChat"),
  29. ("slack", "Slack"),
  30. ("pd", "PagerDuty"),
  31. ("pagertree", "PagerTree"),
  32. ("pagerteam", "Pager Team"),
  33. ("po", "Pushover"),
  34. ("pushbullet", "Pushbullet"),
  35. ("opsgenie", "Opsgenie"),
  36. ("victorops", "VictorOps"),
  37. ("discord", "Discord"),
  38. ("telegram", "Telegram"),
  39. ("sms", "SMS"),
  40. ("zendesk", "Zendesk"),
  41. ("trello", "Trello"),
  42. ("matrix", "Matrix"),
  43. ("whatsapp", "WhatsApp"),
  44. ("apprise", "Apprise"),
  45. ("mattermost", "Mattermost"),
  46. ("msteams", "Microsoft Teams"),
  47. ("shell", "Shell Command"),
  48. ("zulip", "Zulip"),
  49. ("spike", "Spike"),
  50. ("call", "Phone Call"),
  51. ("linenotify", "LINE Notify"),
  52. ("signal", "Signal"),
  53. )
  54. PO_PRIORITIES = {-2: "lowest", -1: "low", 0: "normal", 1: "high", 2: "emergency"}
  55. def isostring(dt):
  56. """Convert the datetime to ISO 8601 format with no microseconds. """
  57. if dt:
  58. return dt.replace(microsecond=0).isoformat()
  59. class Check(models.Model):
  60. name = models.CharField(max_length=100, blank=True)
  61. tags = models.CharField(max_length=500, blank=True)
  62. code = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
  63. desc = models.TextField(blank=True)
  64. project = models.ForeignKey(Project, models.CASCADE)
  65. created = models.DateTimeField(auto_now_add=True)
  66. kind = models.CharField(max_length=10, default="simple", choices=CHECK_KINDS)
  67. timeout = models.DurationField(default=DEFAULT_TIMEOUT)
  68. grace = models.DurationField(default=DEFAULT_GRACE)
  69. schedule = models.CharField(max_length=100, default="* * * * *")
  70. tz = models.CharField(max_length=36, default="UTC")
  71. subject = models.CharField(max_length=100, blank=True)
  72. subject_fail = models.CharField(max_length=100, blank=True)
  73. methods = models.CharField(max_length=30, blank=True)
  74. manual_resume = models.BooleanField(default=False)
  75. n_pings = models.IntegerField(default=0)
  76. last_ping = models.DateTimeField(null=True, blank=True)
  77. last_start = models.DateTimeField(null=True, blank=True)
  78. last_duration = models.DurationField(null=True, blank=True)
  79. last_ping_was_fail = models.BooleanField(default=False)
  80. has_confirmation_link = models.BooleanField(default=False)
  81. alert_after = models.DateTimeField(null=True, blank=True, editable=False)
  82. status = models.CharField(max_length=6, choices=STATUSES, default="new")
  83. class Meta:
  84. indexes = [
  85. # Index for the alert_after field. Excludes rows with status=down.
  86. # Used in the sendalerts management command.
  87. models.Index(
  88. fields=["alert_after"],
  89. name="api_check_aa_not_down",
  90. condition=~models.Q(status="down"),
  91. )
  92. ]
  93. def __str__(self):
  94. return "%s (%d)" % (self.name or self.code, self.id)
  95. def name_then_code(self):
  96. if self.name:
  97. return self.name
  98. return str(self.code)
  99. def url(self):
  100. return settings.PING_ENDPOINT + str(self.code)
  101. def details_url(self):
  102. return settings.SITE_ROOT + reverse("hc-details", args=[self.code])
  103. def email(self):
  104. return "%s@%s" % (self.code, settings.PING_EMAIL_DOMAIN)
  105. def clamped_last_duration(self):
  106. if self.last_duration and self.last_duration < MAX_DELTA:
  107. return self.last_duration
  108. def get_grace_start(self, with_started=True):
  109. """ Return the datetime when the grace period starts.
  110. If the check is currently new, paused or down, return None.
  111. """
  112. # NEVER is a constant sentinel value (year 3000).
  113. # Using None instead would make the logic clunky.
  114. result = NEVER
  115. if self.kind == "simple" and self.status == "up":
  116. result = self.last_ping + self.timeout
  117. elif self.kind == "cron" and self.status == "up":
  118. # The complex case, next ping is expected based on cron schedule.
  119. # Don't convert to naive datetimes (and so avoid ambiguities around
  120. # DST transitions). Croniter will handle the timezone-aware datetimes.
  121. zone = pytz.timezone(self.tz)
  122. last_local = timezone.localtime(self.last_ping, zone)
  123. it = croniter(self.schedule, last_local)
  124. result = it.next(datetime)
  125. if with_started and self.last_start and self.status != "down":
  126. result = min(result, self.last_start)
  127. if result != NEVER:
  128. return result
  129. def going_down_after(self):
  130. """ Return the datetime when the check goes down.
  131. If the check is new or paused, and not currently running, return None.
  132. If the check is already down, also return None.
  133. """
  134. grace_start = self.get_grace_start()
  135. if grace_start is not None:
  136. return grace_start + self.grace
  137. def get_status(self, now=None, with_started=False):
  138. """ Return current status for display. """
  139. if now is None:
  140. now = timezone.now()
  141. if self.last_start:
  142. if now >= self.last_start + self.grace:
  143. return "down"
  144. elif with_started:
  145. return "started"
  146. if self.status in ("new", "paused", "down"):
  147. return self.status
  148. grace_start = self.get_grace_start(with_started=with_started)
  149. grace_end = grace_start + self.grace
  150. if now >= grace_end:
  151. return "down"
  152. if now >= grace_start:
  153. return "grace"
  154. return "up"
  155. def get_status_with_started(self):
  156. return self.get_status(with_started=True)
  157. def assign_all_channels(self):
  158. channels = Channel.objects.filter(project=self.project)
  159. self.channel_set.set(channels)
  160. def tags_list(self):
  161. return [t.strip() for t in self.tags.split(" ") if t.strip()]
  162. def matches_tag_set(self, tag_set):
  163. return tag_set.issubset(self.tags_list())
  164. def channels_str(self):
  165. """ Return a comma-separated string of assigned channel codes. """
  166. # self.channel_set may already be prefetched.
  167. # Sort in python to make sure we do't run additional queries
  168. codes = [str(channel.code) for channel in self.channel_set.all()]
  169. return ",".join(sorted(codes))
  170. @property
  171. def unique_key(self):
  172. code_half = self.code.hex[:16]
  173. return hashlib.sha1(code_half.encode()).hexdigest()
  174. def to_dict(self, readonly=False):
  175. result = {
  176. "name": self.name,
  177. "tags": self.tags,
  178. "desc": self.desc,
  179. "grace": int(self.grace.total_seconds()),
  180. "n_pings": self.n_pings,
  181. "status": self.get_status(with_started=True),
  182. "last_ping": isostring(self.last_ping),
  183. "next_ping": isostring(self.get_grace_start()),
  184. "manual_resume": self.manual_resume,
  185. "methods": self.methods,
  186. }
  187. if self.last_duration:
  188. result["last_duration"] = int(self.last_duration.total_seconds())
  189. if readonly:
  190. result["unique_key"] = self.unique_key
  191. else:
  192. update_rel_url = reverse("hc-api-single", args=[self.code])
  193. pause_rel_url = reverse("hc-api-pause", args=[self.code])
  194. result["ping_url"] = self.url()
  195. result["update_url"] = settings.SITE_ROOT + update_rel_url
  196. result["pause_url"] = settings.SITE_ROOT + pause_rel_url
  197. result["channels"] = self.channels_str()
  198. if self.kind == "simple":
  199. result["timeout"] = int(self.timeout.total_seconds())
  200. elif self.kind == "cron":
  201. result["schedule"] = self.schedule
  202. result["tz"] = self.tz
  203. return result
  204. def ping(self, remote_addr, scheme, method, ua, body, action, exitstatus=None):
  205. now = timezone.now()
  206. if self.status == "paused" and self.manual_resume:
  207. action = "ign"
  208. if action == "start":
  209. self.last_start = now
  210. # Don't update "last_ping" field.
  211. elif action == "ign":
  212. pass
  213. else:
  214. self.last_ping = now
  215. if self.last_start:
  216. self.last_duration = self.last_ping - self.last_start
  217. self.last_start = None
  218. else:
  219. self.last_duration = None
  220. new_status = "down" if action == "fail" else "up"
  221. if self.status != new_status:
  222. flip = Flip(owner=self)
  223. flip.created = self.last_ping
  224. flip.old_status = self.status
  225. flip.new_status = new_status
  226. flip.save()
  227. self.status = new_status
  228. self.alert_after = self.going_down_after()
  229. self.n_pings = models.F("n_pings") + 1
  230. self.has_confirmation_link = "confirm" in str(body).lower()
  231. self.save()
  232. self.refresh_from_db()
  233. ping = Ping(owner=self)
  234. ping.n = self.n_pings
  235. ping.created = now
  236. if action in ("start", "fail", "ign"):
  237. ping.kind = action
  238. ping.remote_addr = remote_addr
  239. ping.scheme = scheme
  240. ping.method = method
  241. # If User-Agent is longer than 200 characters, truncate it:
  242. ping.ua = ua[:200]
  243. ping.body = body[: settings.PING_BODY_LIMIT]
  244. ping.exitstatus = exitstatus
  245. ping.save()
  246. def downtimes(self, months=2):
  247. """ Calculate the number of downtimes and downtime minutes per month.
  248. Returns a list of (datetime, downtime_in_secs, number_of_outages) tuples.
  249. """
  250. def monthkey(dt):
  251. return dt.year, dt.month
  252. # Datetimes of the first days of months we're interested in. Ascending order.
  253. boundaries = month_boundaries(months=months)
  254. # Will accumulate totals here.
  255. # (year, month) -> [datetime, total_downtime, number_of_outages]
  256. totals = {monthkey(b): [b, td(), 0] for b in boundaries}
  257. # A list of flips and month boundaries
  258. events = [(b, "---") for b in boundaries]
  259. q = self.flip_set.filter(created__gt=min(boundaries))
  260. for pair in q.values_list("created", "old_status"):
  261. events.append(pair)
  262. # Iterate through flips and month boundaries in reverse order,
  263. # and for each "down" event increase the counters in `totals`.
  264. dt, status = timezone.now(), self.status
  265. for prev_dt, prev_status in sorted(events, reverse=True):
  266. if status == "down":
  267. delta = dt - prev_dt
  268. totals[monthkey(prev_dt)][1] += delta
  269. totals[monthkey(prev_dt)][2] += 1
  270. dt = prev_dt
  271. if prev_status != "---":
  272. status = prev_status
  273. # Set counters to None for months when the check didn't exist yet
  274. for ym in totals:
  275. if ym < monthkey(self.created):
  276. totals[ym][1] = None
  277. totals[ym][2] = None
  278. return sorted(totals.values())
  279. class Ping(models.Model):
  280. id = models.BigAutoField(primary_key=True)
  281. n = models.IntegerField(null=True)
  282. owner = models.ForeignKey(Check, models.CASCADE)
  283. created = models.DateTimeField(default=timezone.now)
  284. kind = models.CharField(max_length=6, blank=True, null=True)
  285. scheme = models.CharField(max_length=10, default="http")
  286. remote_addr = models.GenericIPAddressField(blank=True, null=True)
  287. method = models.CharField(max_length=10, blank=True)
  288. ua = models.CharField(max_length=200, blank=True)
  289. body = models.TextField(blank=True, null=True)
  290. exitstatus = models.SmallIntegerField(null=True)
  291. def to_dict(self):
  292. return {
  293. "type": self.kind or "success",
  294. "date": self.created.isoformat(),
  295. "n": self.n,
  296. "scheme": self.scheme,
  297. "remote_addr": self.remote_addr,
  298. "method": self.method,
  299. "ua": self.ua,
  300. }
  301. class Channel(models.Model):
  302. name = models.CharField(max_length=100, blank=True)
  303. code = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
  304. project = models.ForeignKey(Project, models.CASCADE)
  305. created = models.DateTimeField(auto_now_add=True)
  306. kind = models.CharField(max_length=20, choices=CHANNEL_KINDS)
  307. value = models.TextField(blank=True)
  308. email_verified = models.BooleanField(default=False)
  309. last_error = models.CharField(max_length=200, blank=True)
  310. checks = models.ManyToManyField(Check)
  311. def __str__(self):
  312. if self.name:
  313. return self.name
  314. if self.kind == "email":
  315. return "Email to %s" % self.email_value
  316. elif self.kind == "sms":
  317. return "SMS to %s" % self.phone_number
  318. elif self.kind == "slack":
  319. return "Slack %s" % self.slack_channel
  320. elif self.kind == "telegram":
  321. return "Telegram %s" % self.telegram_name
  322. elif self.kind == "zulip":
  323. if self.zulip_type == "stream":
  324. return "Zulip stream %s" % self.zulip_to
  325. if self.zulip_type == "private":
  326. return "Zulip user %s" % self.zulip_to
  327. return self.get_kind_display()
  328. def to_dict(self):
  329. return {"id": str(self.code), "name": self.name, "kind": self.kind}
  330. def assign_all_checks(self):
  331. checks = Check.objects.filter(project=self.project)
  332. self.checks.add(*checks)
  333. def make_token(self):
  334. seed = "%s%s" % (self.code, settings.SECRET_KEY)
  335. seed = seed.encode()
  336. return hashlib.sha1(seed).hexdigest()
  337. def send_verify_link(self):
  338. args = [self.code, self.make_token()]
  339. verify_link = reverse("hc-verify-email", args=args)
  340. verify_link = settings.SITE_ROOT + verify_link
  341. emails.verify_email(self.email_value, {"verify_link": verify_link})
  342. def get_unsub_link(self):
  343. signer = TimestampSigner(salt="alerts")
  344. signed_token = signer.sign(self.make_token())
  345. args = [self.code, signed_token]
  346. verify_link = reverse("hc-unsubscribe-alerts", args=args)
  347. return settings.SITE_ROOT + verify_link
  348. @property
  349. def transport(self):
  350. if self.kind == "email":
  351. return transports.Email(self)
  352. elif self.kind == "webhook":
  353. return transports.Webhook(self)
  354. elif self.kind in ("slack", "mattermost"):
  355. return transports.Slack(self)
  356. elif self.kind == "hipchat":
  357. return transports.HipChat(self)
  358. elif self.kind == "pd":
  359. return transports.PagerDuty(self)
  360. elif self.kind == "pagertree":
  361. return transports.PagerTree(self)
  362. elif self.kind == "pagerteam":
  363. return transports.PagerTeam(self)
  364. elif self.kind == "victorops":
  365. return transports.VictorOps(self)
  366. elif self.kind == "pushbullet":
  367. return transports.Pushbullet(self)
  368. elif self.kind == "po":
  369. return transports.Pushover(self)
  370. elif self.kind == "opsgenie":
  371. return transports.Opsgenie(self)
  372. elif self.kind == "discord":
  373. return transports.Discord(self)
  374. elif self.kind == "telegram":
  375. return transports.Telegram(self)
  376. elif self.kind == "sms":
  377. return transports.Sms(self)
  378. elif self.kind == "trello":
  379. return transports.Trello(self)
  380. elif self.kind == "matrix":
  381. return transports.Matrix(self)
  382. elif self.kind == "whatsapp":
  383. return transports.WhatsApp(self)
  384. elif self.kind == "apprise":
  385. return transports.Apprise(self)
  386. elif self.kind == "msteams":
  387. return transports.MsTeams(self)
  388. elif self.kind == "shell":
  389. return transports.Shell(self)
  390. elif self.kind == "zulip":
  391. return transports.Zulip(self)
  392. elif self.kind == "spike":
  393. return transports.Spike(self)
  394. elif self.kind == "call":
  395. return transports.Call(self)
  396. elif self.kind == "linenotify":
  397. return transports.LineNotify(self)
  398. elif self.kind == "signal":
  399. return transports.Signal(self)
  400. else:
  401. raise NotImplementedError("Unknown channel kind: %s" % self.kind)
  402. def notify(self, check, is_test=False):
  403. if self.transport.is_noop(check):
  404. return "no-op"
  405. n = Notification(channel=self)
  406. if is_test:
  407. # When sending a test notification we leave the owner field null.
  408. # (the passed check is a dummy, unsaved Check instance)
  409. pass
  410. else:
  411. n.owner = check
  412. n.check_status = check.status
  413. n.error = "Sending"
  414. n.save()
  415. # These are not database fields. It is just a convenient way to pass
  416. # status_url and the is_test flag to transport classes.
  417. check.is_test = is_test
  418. check.status_url = n.status_url()
  419. error = self.transport.notify(check) or ""
  420. Notification.objects.filter(id=n.id).update(error=error)
  421. Channel.objects.filter(id=self.id).update(last_error=error)
  422. return error
  423. def icon_path(self):
  424. return "img/integrations/%s.png" % self.kind
  425. @property
  426. def json(self):
  427. return json.loads(self.value)
  428. @property
  429. def po_priority(self):
  430. assert self.kind == "po"
  431. parts = self.value.split("|")
  432. prio = int(parts[1])
  433. return PO_PRIORITIES[prio]
  434. def webhook_spec(self, status):
  435. assert self.kind == "webhook"
  436. doc = json.loads(self.value)
  437. if status == "down" and "method_down" in doc:
  438. return {
  439. "method": doc["method_down"],
  440. "url": doc["url_down"],
  441. "body": doc["body_down"],
  442. "headers": doc["headers_down"],
  443. }
  444. elif status == "up" and "method_up" in doc:
  445. return {
  446. "method": doc["method_up"],
  447. "url": doc["url_up"],
  448. "body": doc["body_up"],
  449. "headers": doc["headers_up"],
  450. }
  451. @property
  452. def down_webhook_spec(self):
  453. return self.webhook_spec("down")
  454. @property
  455. def up_webhook_spec(self):
  456. return self.webhook_spec("up")
  457. @property
  458. def url_down(self):
  459. return self.down_webhook_spec["url"]
  460. @property
  461. def url_up(self):
  462. return self.up_webhook_spec["url"]
  463. @property
  464. def cmd_down(self):
  465. assert self.kind == "shell"
  466. return self.json["cmd_down"]
  467. @property
  468. def cmd_up(self):
  469. assert self.kind == "shell"
  470. return self.json["cmd_up"]
  471. @property
  472. def slack_team(self):
  473. assert self.kind == "slack"
  474. if not self.value.startswith("{"):
  475. return None
  476. doc = json.loads(self.value)
  477. if "team_name" in doc:
  478. return doc["team_name"]
  479. if "team" in doc:
  480. return doc["team"]["name"]
  481. @property
  482. def slack_channel(self):
  483. assert self.kind == "slack"
  484. if not self.value.startswith("{"):
  485. return None
  486. doc = json.loads(self.value)
  487. return doc["incoming_webhook"]["channel"]
  488. @property
  489. def slack_webhook_url(self):
  490. assert self.kind in ("slack", "mattermost")
  491. if not self.value.startswith("{"):
  492. return self.value
  493. doc = json.loads(self.value)
  494. return doc["incoming_webhook"]["url"]
  495. @property
  496. def discord_webhook_url(self):
  497. assert self.kind == "discord"
  498. doc = json.loads(self.value)
  499. url = doc["webhook"]["url"]
  500. # Discord migrated to discord.com,
  501. # and is dropping support for discordapp.com on 7 November 2020
  502. if url.startswith("https://discordapp.com/"):
  503. url = "https://discord.com/" + url[23:]
  504. return url
  505. @property
  506. def discord_webhook_id(self):
  507. assert self.kind == "discord"
  508. doc = json.loads(self.value)
  509. return doc["webhook"]["id"]
  510. @property
  511. def telegram_id(self):
  512. assert self.kind == "telegram"
  513. doc = json.loads(self.value)
  514. return doc.get("id")
  515. @property
  516. def telegram_type(self):
  517. assert self.kind == "telegram"
  518. doc = json.loads(self.value)
  519. return doc.get("type")
  520. @property
  521. def telegram_name(self):
  522. assert self.kind == "telegram"
  523. doc = json.loads(self.value)
  524. return doc.get("name")
  525. @property
  526. def pd_service_key(self):
  527. assert self.kind == "pd"
  528. if not self.value.startswith("{"):
  529. return self.value
  530. doc = json.loads(self.value)
  531. return doc["service_key"]
  532. @property
  533. def pd_account(self):
  534. assert self.kind == "pd"
  535. if self.value.startswith("{"):
  536. doc = json.loads(self.value)
  537. return doc["account"]
  538. def latest_notification(self):
  539. return Notification.objects.filter(channel=self).latest()
  540. @property
  541. def phone_number(self):
  542. assert self.kind in ("call", "sms", "whatsapp", "signal")
  543. if self.value.startswith("{"):
  544. doc = json.loads(self.value)
  545. return doc["value"]
  546. return self.value
  547. @property
  548. def trello_token(self):
  549. assert self.kind == "trello"
  550. if self.value.startswith("{"):
  551. doc = json.loads(self.value)
  552. return doc["token"]
  553. @property
  554. def trello_board_list(self):
  555. assert self.kind == "trello"
  556. if self.value.startswith("{"):
  557. doc = json.loads(self.value)
  558. return doc["board_name"], doc["list_name"]
  559. @property
  560. def trello_list_id(self):
  561. assert self.kind == "trello"
  562. if self.value.startswith("{"):
  563. doc = json.loads(self.value)
  564. return doc["list_id"]
  565. @property
  566. def email_value(self):
  567. assert self.kind == "email"
  568. if not self.value.startswith("{"):
  569. return self.value
  570. return self.json["value"]
  571. @property
  572. def email_notify_up(self):
  573. assert self.kind == "email"
  574. if not self.value.startswith("{"):
  575. return True
  576. doc = json.loads(self.value)
  577. return doc.get("up")
  578. @property
  579. def email_notify_down(self):
  580. assert self.kind == "email"
  581. if not self.value.startswith("{"):
  582. return True
  583. doc = json.loads(self.value)
  584. return doc.get("down")
  585. @property
  586. def whatsapp_notify_up(self):
  587. assert self.kind == "whatsapp"
  588. doc = json.loads(self.value)
  589. return doc["up"]
  590. @property
  591. def whatsapp_notify_down(self):
  592. assert self.kind == "whatsapp"
  593. doc = json.loads(self.value)
  594. return doc["down"]
  595. @property
  596. def signal_notify_up(self):
  597. assert self.kind == "signal"
  598. doc = json.loads(self.value)
  599. return doc["up"]
  600. @property
  601. def signal_notify_down(self):
  602. assert self.kind == "signal"
  603. doc = json.loads(self.value)
  604. return doc["down"]
  605. @property
  606. def opsgenie_key(self):
  607. assert self.kind == "opsgenie"
  608. if not self.value.startswith("{"):
  609. return self.value
  610. doc = json.loads(self.value)
  611. return doc["key"]
  612. @property
  613. def opsgenie_region(self):
  614. assert self.kind == "opsgenie"
  615. if not self.value.startswith("{"):
  616. return "us"
  617. doc = json.loads(self.value)
  618. return doc["region"]
  619. @property
  620. def zulip_bot_email(self):
  621. assert self.kind == "zulip"
  622. doc = json.loads(self.value)
  623. return doc["bot_email"]
  624. @property
  625. def zulip_site(self):
  626. assert self.kind == "zulip"
  627. doc = json.loads(self.value)
  628. if "site" in doc:
  629. return doc["site"]
  630. # Fallback if we don't have the site value:
  631. # derive it from bot's email
  632. _, domain = doc["bot_email"].split("@")
  633. return "https://" + domain
  634. @property
  635. def zulip_api_key(self):
  636. assert self.kind == "zulip"
  637. doc = json.loads(self.value)
  638. return doc["api_key"]
  639. @property
  640. def zulip_type(self):
  641. assert self.kind == "zulip"
  642. doc = json.loads(self.value)
  643. return doc["mtype"]
  644. @property
  645. def zulip_to(self):
  646. assert self.kind == "zulip"
  647. doc = json.loads(self.value)
  648. return doc["to"]
  649. @property
  650. def linenotify_token(self):
  651. assert self.kind == "linenotify"
  652. if not self.value.startswith("{"):
  653. return self.value
  654. doc = json.loads(self.value)
  655. return doc["token"]
  656. class Notification(models.Model):
  657. code = models.UUIDField(default=uuid.uuid4, null=True, editable=False)
  658. owner = models.ForeignKey(Check, models.CASCADE, null=True)
  659. check_status = models.CharField(max_length=6)
  660. channel = models.ForeignKey(Channel, models.CASCADE)
  661. created = models.DateTimeField(auto_now_add=True)
  662. error = models.CharField(max_length=200, blank=True)
  663. class Meta:
  664. get_latest_by = "created"
  665. def status_url(self):
  666. path = reverse("hc-api-notification-status", args=[self.code])
  667. return settings.SITE_ROOT + path
  668. class Flip(models.Model):
  669. owner = models.ForeignKey(Check, models.CASCADE)
  670. created = models.DateTimeField()
  671. processed = models.DateTimeField(null=True, blank=True)
  672. old_status = models.CharField(max_length=8, choices=STATUSES)
  673. new_status = models.CharField(max_length=8, choices=STATUSES)
  674. class Meta:
  675. indexes = [
  676. # For quickly looking up unprocessed flips.
  677. # Used in the sendalerts management command.
  678. models.Index(
  679. fields=["processed"],
  680. name="api_flip_not_processed",
  681. condition=models.Q(processed=None),
  682. )
  683. ]
  684. def to_dict(self):
  685. return {
  686. "timestamp": isostring(self.created),
  687. "up": 1 if self.new_status == "up" else 0,
  688. }
  689. def send_alerts(self):
  690. """Loop over the enabled channels, call notify() on each.
  691. For each channel, yield a (channel, error, send_time) triple:
  692. * channel is a Channel instance
  693. * error is an empty string ("") on success, error message otherwise
  694. * send_time is the send time in seconds (float)
  695. """
  696. # Don't send alerts on new->up and paused->up transitions
  697. if self.new_status == "up" and self.old_status in ("new", "paused"):
  698. return
  699. if self.new_status not in ("up", "down"):
  700. raise NotImplementedError("Unexpected status: %s" % self.status)
  701. for channel in self.owner.channel_set.all():
  702. start = time.time()
  703. error = channel.notify(self.owner)
  704. if error == "no-op":
  705. continue
  706. yield (channel, error, time.time() - start)
  707. class TokenBucket(models.Model):
  708. value = models.CharField(max_length=80, unique=True)
  709. tokens = models.FloatField(default=1.0)
  710. updated = models.DateTimeField(default=timezone.now)
  711. @staticmethod
  712. def authorize(value, capacity, refill_time_secs):
  713. now = timezone.now()
  714. obj, created = TokenBucket.objects.get_or_create(value=value)
  715. if not created:
  716. # Top up the bucket:
  717. delta_secs = (now - obj.updated).total_seconds()
  718. obj.tokens = min(1.0, obj.tokens + delta_secs / refill_time_secs)
  719. obj.tokens -= 1.0 / capacity
  720. if obj.tokens < 0:
  721. # Not enough tokens
  722. return False
  723. # Race condition: two concurrent authorize calls can overwrite each
  724. # other's changes. It's OK to be a little inexact here for the sake
  725. # of simplicity.
  726. obj.updated = now
  727. obj.save()
  728. return True
  729. @staticmethod
  730. def authorize_login_email(email):
  731. # remove dots and alias:
  732. mailbox, domain = email.split("@")
  733. mailbox = mailbox.replace(".", "")
  734. mailbox = mailbox.split("+")[0]
  735. email = mailbox + "@" + domain
  736. salted_encoded = (email + settings.SECRET_KEY).encode()
  737. value = "em-%s" % hashlib.sha1(salted_encoded).hexdigest()
  738. # 20 login attempts for a single email per hour:
  739. return TokenBucket.authorize(value, 20, 3600)
  740. @staticmethod
  741. def authorize_invite(user):
  742. value = "invite-%d" % user.id
  743. # 20 invites per day
  744. return TokenBucket.authorize(value, 20, 3600 * 24)
  745. @staticmethod
  746. def authorize_login_password(email):
  747. salted_encoded = (email + settings.SECRET_KEY).encode()
  748. value = "pw-%s" % hashlib.sha1(salted_encoded).hexdigest()
  749. # 20 password attempts per day
  750. return TokenBucket.authorize(value, 20, 3600 * 24)
  751. @staticmethod
  752. def authorize_telegram(telegram_id):
  753. value = "tg-%s" % telegram_id
  754. # 6 messages for a single chat per minute:
  755. return TokenBucket.authorize(value, 6, 60)
  756. @staticmethod
  757. def authorize_signal(phone):
  758. salted_encoded = (phone + settings.SECRET_KEY).encode()
  759. value = "signal-%s" % hashlib.sha1(salted_encoded).hexdigest()
  760. # 6 messages for a single recipient per minute:
  761. return TokenBucket.authorize(value, 6, 60)
  762. @staticmethod
  763. def authorize_pushover(user_key):
  764. salted_encoded = (user_key + settings.SECRET_KEY).encode()
  765. value = "po-%s" % hashlib.sha1(salted_encoded).hexdigest()
  766. # 6 messages for a single user key per minute:
  767. return TokenBucket.authorize(value, 6, 60)
  768. @staticmethod
  769. def authorize_sudo_code(user):
  770. value = "sudo-%d" % user.id
  771. # 10 sudo attempts per day
  772. return TokenBucket.authorize(value, 10, 3600 * 24)