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.

508 lines
16 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 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.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. import requests
  16. STATUSES = (
  17. ("up", "Up"),
  18. ("down", "Down"),
  19. ("new", "New"),
  20. ("paused", "Paused")
  21. )
  22. DEFAULT_TIMEOUT = td(days=1)
  23. DEFAULT_GRACE = td(hours=1)
  24. CHECK_KINDS = (("simple", "Simple"),
  25. ("cron", "Cron"))
  26. CHANNEL_KINDS = (("email", "Email"),
  27. ("webhook", "Webhook"),
  28. ("hipchat", "HipChat"),
  29. ("slack", "Slack"),
  30. ("pd", "PagerDuty"),
  31. ("pagertree", "PagerTree"),
  32. ("po", "Pushover"),
  33. ("pushbullet", "Pushbullet"),
  34. ("opsgenie", "OpsGenie"),
  35. ("victorops", "VictorOps"),
  36. ("discord", "Discord"),
  37. ("telegram", "Telegram"),
  38. ("sms", "SMS"),
  39. ("zendesk", "Zendesk"))
  40. PO_PRIORITIES = {
  41. -2: "lowest",
  42. -1: "low",
  43. 0: "normal",
  44. 1: "high",
  45. 2: "emergency"
  46. }
  47. def isostring(dt):
  48. """Convert the datetime to ISO 8601 format with no microseconds. """
  49. return dt.replace(microsecond=0).isoformat()
  50. class Check(models.Model):
  51. name = models.CharField(max_length=100, blank=True)
  52. tags = models.CharField(max_length=500, blank=True)
  53. code = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True)
  54. user = models.ForeignKey(User, models.CASCADE, blank=True, null=True)
  55. created = models.DateTimeField(auto_now_add=True)
  56. kind = models.CharField(max_length=10, default="simple",
  57. choices=CHECK_KINDS)
  58. timeout = models.DurationField(default=DEFAULT_TIMEOUT)
  59. grace = models.DurationField(default=DEFAULT_GRACE)
  60. schedule = models.CharField(max_length=100, default="* * * * *")
  61. tz = models.CharField(max_length=36, default="UTC")
  62. n_pings = models.IntegerField(default=0)
  63. last_ping = models.DateTimeField(null=True, blank=True)
  64. last_ping_was_fail = models.NullBooleanField(default=False)
  65. has_confirmation_link = models.BooleanField(default=False)
  66. alert_after = models.DateTimeField(null=True, blank=True, editable=False)
  67. status = models.CharField(max_length=6, choices=STATUSES, default="new")
  68. def name_then_code(self):
  69. if self.name:
  70. return self.name
  71. return str(self.code)
  72. def url(self):
  73. return settings.PING_ENDPOINT + str(self.code)
  74. def log_url(self):
  75. return settings.SITE_ROOT + reverse("hc-log", args=[self.code])
  76. def email(self):
  77. return "%s@%s" % (self.code, settings.PING_EMAIL_DOMAIN)
  78. def send_alert(self):
  79. if self.status not in ("up", "down"):
  80. raise NotImplementedError("Unexpected status: %s" % self.status)
  81. errors = []
  82. for channel in self.channel_set.all():
  83. error = channel.notify(self)
  84. if error not in ("", "no-op"):
  85. errors.append((channel, error))
  86. return errors
  87. def get_grace_start(self):
  88. """ Return the datetime when grace period starts. """
  89. # The common case, grace starts after timeout
  90. if self.kind == "simple":
  91. return self.last_ping + self.timeout
  92. # The complex case, next ping is expected based on cron schedule
  93. with timezone.override(self.tz):
  94. last_naive = timezone.make_naive(self.last_ping)
  95. it = croniter(self.schedule, last_naive)
  96. next_naive = it.get_next(datetime)
  97. return timezone.make_aware(next_naive, is_dst=False)
  98. def get_status(self, now=None):
  99. """ Return "up" if the check is up or in grace, otherwise "down". """
  100. if self.status in ("new", "paused"):
  101. return self.status
  102. if self.last_ping_was_fail:
  103. return "down"
  104. if now is None:
  105. now = timezone.now()
  106. return "up" if self.get_grace_start() + self.grace > now else "down"
  107. def get_alert_after(self):
  108. """ Return the datetime when check potentially goes down. """
  109. # For "fail" pings, sendalerts should the check right
  110. # after receiving the ping, without waiting for the grace time:
  111. if self.last_ping_was_fail:
  112. return self.last_ping
  113. return self.get_grace_start() + self.grace
  114. def in_grace_period(self):
  115. """ Return True if check is currently in grace period. """
  116. if self.status in ("new", "paused"):
  117. return False
  118. if self.last_ping_was_fail:
  119. return False
  120. grace_start = self.get_grace_start()
  121. grace_end = grace_start + self.grace
  122. return grace_start < timezone.now() < grace_end
  123. def assign_all_channels(self):
  124. if self.user:
  125. channels = Channel.objects.filter(user=self.user)
  126. self.channel_set.add(*channels)
  127. def tags_list(self):
  128. return [t.strip() for t in self.tags.split(" ") if t.strip()]
  129. def matches_tag_set(self, tag_set):
  130. return tag_set.issubset(self.tags_list())
  131. def to_dict(self):
  132. update_rel_url = reverse("hc-api-update", args=[self.code])
  133. pause_rel_url = reverse("hc-api-pause", args=[self.code])
  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. }
  144. if self.kind == "simple":
  145. result["timeout"] = int(self.timeout.total_seconds())
  146. elif self.kind == "cron":
  147. result["schedule"] = self.schedule
  148. result["tz"] = self.tz
  149. if self.last_ping:
  150. result["last_ping"] = isostring(self.last_ping)
  151. result["next_ping"] = isostring(self.get_grace_start())
  152. else:
  153. result["last_ping"] = None
  154. result["next_ping"] = None
  155. return result
  156. def ping(self, remote_addr, scheme, method, ua, body, is_fail=False):
  157. self.n_pings = models.F("n_pings") + 1
  158. self.last_ping = timezone.now()
  159. self.last_ping_was_fail = is_fail
  160. self.has_confirmation_link = "confirm" in str(body).lower()
  161. self.alert_after = self.get_alert_after()
  162. if self.status in ("new", "paused"):
  163. self.status = "up"
  164. self.save()
  165. self.refresh_from_db()
  166. ping = Ping(owner=self)
  167. ping.n = self.n_pings
  168. ping.fail = is_fail
  169. ping.remote_addr = remote_addr
  170. ping.scheme = scheme
  171. ping.method = method
  172. # If User-Agent is longer than 200 characters, truncate it:
  173. ping.ua = ua[:200]
  174. ping.body = body[:10000]
  175. ping.save()
  176. class Ping(models.Model):
  177. id = models.BigAutoField(primary_key=True)
  178. n = models.IntegerField(null=True)
  179. owner = models.ForeignKey(Check, models.CASCADE)
  180. created = models.DateTimeField(auto_now_add=True)
  181. fail = models.NullBooleanField(default=False)
  182. scheme = models.CharField(max_length=10, default="http")
  183. remote_addr = models.GenericIPAddressField(blank=True, null=True)
  184. method = models.CharField(max_length=10, blank=True)
  185. ua = models.CharField(max_length=200, blank=True)
  186. body = models.CharField(max_length=10000, blank=True, null=True)
  187. class Channel(models.Model):
  188. code = models.UUIDField(default=uuid.uuid4, editable=False)
  189. user = models.ForeignKey(User, models.CASCADE)
  190. created = models.DateTimeField(auto_now_add=True)
  191. kind = models.CharField(max_length=20, choices=CHANNEL_KINDS)
  192. value = models.TextField(blank=True)
  193. email_verified = models.BooleanField(default=False)
  194. checks = models.ManyToManyField(Check)
  195. def assign_all_checks(self):
  196. checks = Check.objects.filter(user=self.user)
  197. self.checks.add(*checks)
  198. def make_token(self):
  199. seed = "%s%s" % (self.code, settings.SECRET_KEY)
  200. seed = seed.encode()
  201. return hashlib.sha1(seed).hexdigest()
  202. def send_verify_link(self):
  203. args = [self.code, self.make_token()]
  204. verify_link = reverse("hc-verify-email", args=args)
  205. verify_link = settings.SITE_ROOT + verify_link
  206. emails.verify_email(self.value, {"verify_link": verify_link})
  207. def get_unsub_link(self):
  208. args = [self.code, self.make_token()]
  209. verify_link = reverse("hc-unsubscribe-alerts", args=args)
  210. return settings.SITE_ROOT + verify_link
  211. @property
  212. def transport(self):
  213. if self.kind == "email":
  214. return transports.Email(self)
  215. elif self.kind == "webhook":
  216. return transports.Webhook(self)
  217. elif self.kind == "slack":
  218. return transports.Slack(self)
  219. elif self.kind == "hipchat":
  220. return transports.HipChat(self)
  221. elif self.kind == "pd":
  222. return transports.PagerDuty(self)
  223. elif self.kind == "pagertree":
  224. return transports.PagerTree(self)
  225. elif self.kind == "victorops":
  226. return transports.VictorOps(self)
  227. elif self.kind == "pushbullet":
  228. return transports.Pushbullet(self)
  229. elif self.kind == "po":
  230. return transports.Pushover(self)
  231. elif self.kind == "opsgenie":
  232. return transports.OpsGenie(self)
  233. elif self.kind == "discord":
  234. return transports.Discord(self)
  235. elif self.kind == "telegram":
  236. return transports.Telegram(self)
  237. elif self.kind == "sms":
  238. return transports.Sms(self)
  239. elif self.kind == "zendesk":
  240. return transports.Zendesk(self)
  241. else:
  242. raise NotImplementedError("Unknown channel kind: %s" % self.kind)
  243. def notify(self, check):
  244. if self.transport.is_noop(check):
  245. return "no-op"
  246. n = Notification(owner=check, channel=self)
  247. n.check_status = check.status
  248. n.error = "Sending"
  249. n.save()
  250. if self.kind == "email":
  251. error = self.transport.notify(check, n.bounce_url()) or ""
  252. else:
  253. error = self.transport.notify(check) or ""
  254. n.error = error
  255. n.save()
  256. return error
  257. @property
  258. def po_value(self):
  259. assert self.kind == "po"
  260. user_key, prio = self.value.split("|")
  261. prio = int(prio)
  262. return user_key, prio, PO_PRIORITIES[prio]
  263. @property
  264. def url_down(self):
  265. assert self.kind == "webhook"
  266. if not self.value.startswith("{"):
  267. parts = self.value.split("\n")
  268. return parts[0]
  269. doc = json.loads(self.value)
  270. return doc.get("url_down")
  271. @property
  272. def url_up(self):
  273. assert self.kind == "webhook"
  274. if not self.value.startswith("{"):
  275. parts = self.value.split("\n")
  276. return parts[1] if len(parts) > 1 else ""
  277. doc = json.loads(self.value)
  278. return doc.get("url_up")
  279. @property
  280. def post_data(self):
  281. assert self.kind == "webhook"
  282. if not self.value.startswith("{"):
  283. parts = self.value.split("\n")
  284. return parts[2] if len(parts) > 2 else ""
  285. doc = json.loads(self.value)
  286. return doc.get("post_data")
  287. @property
  288. def headers(self):
  289. assert self.kind == "webhook"
  290. if not self.value.startswith("{"):
  291. return {}
  292. doc = json.loads(self.value)
  293. return doc.get("headers", {})
  294. @property
  295. def slack_team(self):
  296. assert self.kind == "slack"
  297. if not self.value.startswith("{"):
  298. return None
  299. doc = json.loads(self.value)
  300. return doc["team_name"]
  301. @property
  302. def slack_channel(self):
  303. assert self.kind == "slack"
  304. if not self.value.startswith("{"):
  305. return None
  306. doc = json.loads(self.value)
  307. return doc["incoming_webhook"]["channel"]
  308. @property
  309. def slack_webhook_url(self):
  310. assert self.kind == "slack"
  311. if not self.value.startswith("{"):
  312. return self.value
  313. doc = json.loads(self.value)
  314. return doc["incoming_webhook"]["url"]
  315. @property
  316. def discord_webhook_url(self):
  317. assert self.kind == "discord"
  318. doc = json.loads(self.value)
  319. return doc["webhook"]["url"]
  320. @property
  321. def discord_webhook_id(self):
  322. assert self.kind == "discord"
  323. doc = json.loads(self.value)
  324. return doc["webhook"]["id"]
  325. @property
  326. def telegram_id(self):
  327. assert self.kind == "telegram"
  328. doc = json.loads(self.value)
  329. return doc.get("id")
  330. @property
  331. def telegram_type(self):
  332. assert self.kind == "telegram"
  333. doc = json.loads(self.value)
  334. return doc.get("type")
  335. @property
  336. def telegram_name(self):
  337. assert self.kind == "telegram"
  338. doc = json.loads(self.value)
  339. return doc.get("name")
  340. def refresh_hipchat_access_token(self):
  341. assert self.kind == "hipchat"
  342. if not self.value.startswith("{"):
  343. return # Don't have OAuth credentials
  344. doc = json.loads(self.value)
  345. if time.time() < doc.get("expires_at", 0):
  346. return # Current access token is still valid
  347. url = "https://api.hipchat.com/v2/oauth/token"
  348. auth = (doc["oauthId"], doc["oauthSecret"])
  349. r = requests.post(url, auth=auth, data={
  350. "grant_type": "client_credentials",
  351. "scope": "send_notification"
  352. })
  353. doc.update(r.json())
  354. doc["expires_at"] = int(time.time()) + doc["expires_in"] - 300
  355. self.value = json.dumps(doc)
  356. self.save()
  357. @property
  358. def hipchat_webhook_url(self):
  359. assert self.kind == "hipchat"
  360. if not self.value.startswith("{"):
  361. return self.value
  362. doc = json.loads(self.value)
  363. tmpl = "https://api.hipchat.com/v2/room/%s/notification?auth_token=%s"
  364. return tmpl % (doc["roomId"], doc.get("access_token"))
  365. @property
  366. def pd_service_key(self):
  367. assert self.kind == "pd"
  368. if not self.value.startswith("{"):
  369. return self.value
  370. doc = json.loads(self.value)
  371. return doc["service_key"]
  372. @property
  373. def pd_account(self):
  374. assert self.kind == "pd"
  375. if self.value.startswith("{"):
  376. doc = json.loads(self.value)
  377. return doc["account"]
  378. @property
  379. def zendesk_token(self):
  380. assert self.kind == "zendesk"
  381. doc = json.loads(self.value)
  382. return doc["access_token"]
  383. @property
  384. def zendesk_subdomain(self):
  385. assert self.kind == "zendesk"
  386. doc = json.loads(self.value)
  387. return doc["subdomain"]
  388. def latest_notification(self):
  389. return Notification.objects.filter(channel=self).latest()
  390. @property
  391. def sms_number(self):
  392. assert self.kind == "sms"
  393. if self.value.startswith("{"):
  394. doc = json.loads(self.value)
  395. return doc["value"]
  396. return self.value
  397. @property
  398. def sms_label(self):
  399. assert self.kind == "sms"
  400. if self.value.startswith("{"):
  401. doc = json.loads(self.value)
  402. return doc["label"]
  403. class Notification(models.Model):
  404. class Meta:
  405. get_latest_by = "created"
  406. code = models.UUIDField(default=uuid.uuid4, null=True, editable=False)
  407. owner = models.ForeignKey(Check, models.CASCADE)
  408. check_status = models.CharField(max_length=6)
  409. channel = models.ForeignKey(Channel, models.CASCADE)
  410. created = models.DateTimeField(auto_now_add=True)
  411. error = models.CharField(max_length=200, blank=True)
  412. def bounce_url(self):
  413. return settings.SITE_ROOT + reverse("hc-api-bounce", args=[self.code])