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.

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