Add Slack integration

This commit is contained in:
Pēteris Caune 2015-09-30 22:19:59 +03:00
parent 09c0b3e3a7
commit 7bb17cefad
5 changed files with 34 additions and 3 deletions

View File

@ -9,6 +9,7 @@ from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
from django.template.loader import render_to_string
from django.utils import timezone from django.utils import timezone
import requests import requests
@ -19,7 +20,7 @@ STATUSES = (("up", "Up"), ("down", "Down"), ("new", "New"))
DEFAULT_TIMEOUT = td(days=1) DEFAULT_TIMEOUT = td(days=1)
DEFAULT_GRACE = td(hours=1) DEFAULT_GRACE = td(hours=1)
CHANNEL_KINDS = (("email", "Email"), ("webhook", "Webhook"), CHANNEL_KINDS = (("email", "Email"), ("webhook", "Webhook"),
("pd", "PagerDuty")) ("slack", "Slack"), ("pd", "PagerDuty"))
class Check(models.Model): class Check(models.Model):
@ -123,6 +124,19 @@ class Channel(models.Model):
pass pass
n.save() n.save()
elif self.kind == "slack":
text = render_to_string("slack_message.html", {"check": check})
payload = {
"text": text,
"username": "healthchecks.io",
"icon_url": "https://healthchecks.io/static/img/logo@2x.png"
}
r = requests.post(self.value, data=json.dumps(payload))
n.status = r.status_code
n.save()
elif self.kind == "pd": elif self.kind == "pd":
if check.status == "down": if check.status == "down":
event_type = "trigger" event_type = "trigger"

View File

@ -2,6 +2,7 @@ $(function() {
var placeholders = { var placeholders = {
email: "address@example.org", email: "address@example.org",
webhook: "http://", webhook: "http://",
slack: "https://hooks.slack.com/...",
pd: "service key" pd: "service key"
} }

View File

@ -21,6 +21,7 @@
<td> <td>
{% if ch.kind == "email" %} Email {% endif %} {% if ch.kind == "email" %} Email {% endif %}
{% if ch.kind == "webhook" %} Webhook {% endif %} {% if ch.kind == "webhook" %} Webhook {% endif %}
{% if ch.kind == "slack" %} Slack {% endif %}
{% if ch.kind == "pd" %} PagerDuty {% endif %} {% if ch.kind == "pd" %} PagerDuty {% endif %}
</td> </td>
<td> <td>
@ -68,6 +69,7 @@
<select id="add-check-kind" class="form-control" name="kind"> <select id="add-check-kind" class="form-control" name="kind">
<option value="email">Email</option> <option value="email">Email</option>
<option value="webhook">Webhook</option> <option value="webhook">Webhook</option>
<option value="slack">Slack</option>
<option value="pd">PagerDuty</option> <option value="pd">PagerDuty</option>
</select> </select>
</td> </td>
@ -103,6 +105,11 @@
<span class="word-down">down</span> and will resolve it <span class="word-down">down</span> and will resolve it
when same check goes <span class="word-up">up</span> when same check goes <span class="word-up">up</span>
</p> </p>
<p class="channels-help-hidden slack">
Healthchecks.io will post to a Slack channel when
a check goes
<span class="word-up">up</span> or <span class="word-down">down</span>.
</p>
</td> </td>
</tr> </tr>

View File

@ -222,8 +222,9 @@
</div> </div>
<div class="col-sm-4"> <div class="col-sm-4">
<p> <p>
You can specify additional email addresses, webhooks You can specify additional email addresses, webhooks,
and <a href="https://www.pagerduty.com/">PagerDuty</a> <a href="https://www.pagerduty.com/">PagerDuty</a>
and <a href="https://slack.com/">Slack</a>
accounts to send notifications to. accounts to send notifications to.
</p> </p>
</div> </div>

View File

@ -0,0 +1,8 @@
{% load humanize %}
{% if check.status == "down" %}
The check "{{ check.name_then_code }}" is DOWN.
Last ping was {{ check.last_ping|naturaltime }}
{% else %}
The check "{{ check.name_then_code }}" received a ping and is now UP.
{% endif %}