forked from GithubBackups/healthchecks
Add the OPSGENIE_ENABLED setting, rename OpsGenie -> Opsgenie
This commit is contained in:
parent
5f31b8b873
commit
8d5890d883
@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file.
|
||||
- Add the SLACK_ENABLED setting (#471)
|
||||
- Add the MATTERMOST_ENABLED setting (#471)
|
||||
- Add the MSTEAMS_ENABLED setting (#471)
|
||||
- Add the OPSGENIE_ENABLED setting (#471)
|
||||
|
||||
## Bug Fixes
|
||||
- Fix unwanted HTML escaping in SMS and WhatsApp notifications
|
||||
|
@ -27,6 +27,7 @@ MATRIX_HOMESERVER=
|
||||
MATRIX_USER_ID=
|
||||
MATTERMOST_ENABLED=True
|
||||
MSTEAMS_ENABLED=True
|
||||
OPSGENIE_ENABLED=True
|
||||
PD_VENDOR_KEY=
|
||||
PING_BODY_LIMIT=10000
|
||||
PING_EMAIL_DOMAIN=localhost
|
||||
|
@ -36,7 +36,7 @@ CHANNEL_KINDS = (
|
||||
("pagerteam", "Pager Team"),
|
||||
("po", "Pushover"),
|
||||
("pushbullet", "Pushbullet"),
|
||||
("opsgenie", "OpsGenie"),
|
||||
("opsgenie", "Opsgenie"),
|
||||
("victorops", "VictorOps"),
|
||||
("discord", "Discord"),
|
||||
("telegram", "Telegram"),
|
||||
@ -446,7 +446,7 @@ class Channel(models.Model):
|
||||
elif self.kind == "po":
|
||||
return transports.Pushover(self)
|
||||
elif self.kind == "opsgenie":
|
||||
return transports.OpsGenie(self)
|
||||
return transports.Opsgenie(self)
|
||||
elif self.kind == "discord":
|
||||
return transports.Discord(self)
|
||||
elif self.kind == "telegram":
|
||||
|
@ -79,58 +79,6 @@ class NotifyTestCase(BaseTestCase):
|
||||
self.assertFalse(mock_post.called)
|
||||
self.assertEqual(Notification.objects.count(), 0)
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_opsgenie_with_legacy_value(self, mock_post):
|
||||
self._setup_data("opsgenie", "123")
|
||||
mock_post.return_value.status_code = 202
|
||||
|
||||
self.channel.notify(self.check)
|
||||
n = Notification.objects.first()
|
||||
self.assertEqual(n.error, "")
|
||||
|
||||
self.assertEqual(mock_post.call_count, 1)
|
||||
args, kwargs = mock_post.call_args
|
||||
self.assertIn("api.opsgenie.com", args[1])
|
||||
payload = kwargs["json"]
|
||||
self.assertIn("DOWN", payload["message"])
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_opsgenie_up(self, mock_post):
|
||||
self._setup_data("opsgenie", "123", status="up")
|
||||
mock_post.return_value.status_code = 202
|
||||
|
||||
self.channel.notify(self.check)
|
||||
n = Notification.objects.first()
|
||||
self.assertEqual(n.error, "")
|
||||
|
||||
self.assertEqual(mock_post.call_count, 1)
|
||||
args, kwargs = mock_post.call_args
|
||||
method, url = args
|
||||
self.assertTrue(str(self.check.code) in url)
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_opsgenie_with_json_value(self, mock_post):
|
||||
self._setup_data("opsgenie", json.dumps({"key": "456", "region": "eu"}))
|
||||
mock_post.return_value.status_code = 202
|
||||
|
||||
self.channel.notify(self.check)
|
||||
n = Notification.objects.first()
|
||||
self.assertEqual(n.error, "")
|
||||
|
||||
self.assertEqual(mock_post.call_count, 1)
|
||||
args, kwargs = mock_post.call_args
|
||||
self.assertIn("api.eu.opsgenie.com", args[1])
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_opsgenie_returns_error(self, mock_post):
|
||||
self._setup_data("opsgenie", "123")
|
||||
mock_post.return_value.status_code = 403
|
||||
mock_post.return_value.json.return_value = {"message": "Nice try"}
|
||||
|
||||
self.channel.notify(self.check)
|
||||
n = Notification.objects.first()
|
||||
self.assertEqual(n.error, 'Received status code 403 with a message: "Nice try"')
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_victorops(self, mock_post):
|
||||
self._setup_data("victorops", "123")
|
||||
|
85
hc/api/tests/test_notify_opsgenie.py
Normal file
85
hc/api/tests/test_notify_opsgenie.py
Normal file
@ -0,0 +1,85 @@
|
||||
# coding: utf-8
|
||||
|
||||
from datetime import timedelta as td
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test.utils import override_settings
|
||||
from django.utils.timezone import now
|
||||
from hc.api.models import Channel, Check, Notification
|
||||
from hc.test import BaseTestCase
|
||||
|
||||
|
||||
class NotifyTestCase(BaseTestCase):
|
||||
def _setup_data(self, value, status="down", email_verified=True):
|
||||
self.check = Check(project=self.project)
|
||||
self.check.status = status
|
||||
self.check.last_ping = now() - td(minutes=61)
|
||||
self.check.save()
|
||||
|
||||
self.channel = Channel(project=self.project)
|
||||
self.channel.kind = "opsgenie"
|
||||
self.channel.value = value
|
||||
self.channel.email_verified = email_verified
|
||||
self.channel.save()
|
||||
self.channel.checks.add(self.check)
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_opsgenie_with_legacy_value(self, mock_post):
|
||||
self._setup_data("123")
|
||||
mock_post.return_value.status_code = 202
|
||||
|
||||
self.channel.notify(self.check)
|
||||
n = Notification.objects.first()
|
||||
self.assertEqual(n.error, "")
|
||||
|
||||
self.assertEqual(mock_post.call_count, 1)
|
||||
args, kwargs = mock_post.call_args
|
||||
self.assertIn("api.opsgenie.com", args[1])
|
||||
payload = kwargs["json"]
|
||||
self.assertIn("DOWN", payload["message"])
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_opsgenie_up(self, mock_post):
|
||||
self._setup_data("123", status="up")
|
||||
mock_post.return_value.status_code = 202
|
||||
|
||||
self.channel.notify(self.check)
|
||||
n = Notification.objects.first()
|
||||
self.assertEqual(n.error, "")
|
||||
|
||||
self.assertEqual(mock_post.call_count, 1)
|
||||
args, kwargs = mock_post.call_args
|
||||
method, url = args
|
||||
self.assertTrue(str(self.check.code) in url)
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_opsgenie_with_json_value(self, mock_post):
|
||||
self._setup_data(json.dumps({"key": "456", "region": "eu"}))
|
||||
mock_post.return_value.status_code = 202
|
||||
|
||||
self.channel.notify(self.check)
|
||||
n = Notification.objects.first()
|
||||
self.assertEqual(n.error, "")
|
||||
|
||||
self.assertEqual(mock_post.call_count, 1)
|
||||
args, kwargs = mock_post.call_args
|
||||
self.assertIn("api.eu.opsgenie.com", args[1])
|
||||
|
||||
@patch("hc.api.transports.requests.request")
|
||||
def test_opsgenie_returns_error(self, mock_post):
|
||||
self._setup_data("123")
|
||||
mock_post.return_value.status_code = 403
|
||||
mock_post.return_value.json.return_value = {"message": "Nice try"}
|
||||
|
||||
self.channel.notify(self.check)
|
||||
n = Notification.objects.first()
|
||||
self.assertEqual(n.error, 'Received status code 403 with a message: "Nice try"')
|
||||
|
||||
@override_settings(OPSGENIE_ENABLED=False)
|
||||
def test_it_requires_opsgenie_enabled(self):
|
||||
self._setup_data("123")
|
||||
self.channel.notify(self.check)
|
||||
|
||||
n = Notification.objects.get()
|
||||
self.assertEqual(n.error, "Opsgenie notifications are not enabled.")
|
@ -279,7 +279,7 @@ class HipChat(HttpTransport):
|
||||
return True
|
||||
|
||||
|
||||
class OpsGenie(HttpTransport):
|
||||
class Opsgenie(HttpTransport):
|
||||
@classmethod
|
||||
def get_error(cls, response):
|
||||
try:
|
||||
@ -288,6 +288,9 @@ class OpsGenie(HttpTransport):
|
||||
pass
|
||||
|
||||
def notify(self, check):
|
||||
if not settings.OPSGENIE_ENABLED:
|
||||
return "Opsgenie notifications are not enabled."
|
||||
|
||||
headers = {
|
||||
"Conent-Type": "application/json",
|
||||
"Authorization": "GenieKey %s" % self.channel.opsgenie_key,
|
||||
|
@ -99,7 +99,7 @@ class CronForm(forms.Form):
|
||||
grace = forms.IntegerField(min_value=1, max_value=43200)
|
||||
|
||||
|
||||
class AddOpsGenieForm(forms.Form):
|
||||
class AddOpsgenieForm(forms.Form):
|
||||
error_css_class = "has-error"
|
||||
region = forms.ChoiceField(initial="us", choices=(("us", "US"), ("eu", "EU")))
|
||||
key = forms.CharField(max_length=40)
|
||||
|
@ -1,10 +1,11 @@
|
||||
import json
|
||||
|
||||
from django.test.utils import override_settings
|
||||
from hc.api.models import Channel
|
||||
from hc.test import BaseTestCase
|
||||
|
||||
|
||||
class AddOpsGenieTestCase(BaseTestCase):
|
||||
class AddOpsgenieTestCase(BaseTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.url = "/projects/%s/add_opsgenie/" % self.project.code
|
||||
@ -56,3 +57,9 @@ class AddOpsGenieTestCase(BaseTestCase):
|
||||
self.client.login(username="bob@example.org", password="password")
|
||||
r = self.client.get(self.url)
|
||||
self.assertEqual(r.status_code, 403)
|
||||
|
||||
@override_settings(OPSGENIE_ENABLED=False)
|
||||
def test_it_handles_disabled_integration(self):
|
||||
self.client.login(username="alice@example.org", password="password")
|
||||
r = self.client.get(self.url)
|
||||
self.assertEqual(r.status_code, 404)
|
||||
|
@ -297,6 +297,7 @@ def index(request):
|
||||
"enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None,
|
||||
"enable_mattermost": settings.MATTERMOST_ENABLED is True,
|
||||
"enable_msteams": settings.MSTEAMS_ENABLED is True,
|
||||
"enable_opsgenie": settings.OPSGENIE_ENABLED is True,
|
||||
"enable_pdc": settings.PD_VENDOR_KEY is not None,
|
||||
"enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None,
|
||||
"enable_pushover": settings.PUSHOVER_API_TOKEN is not None,
|
||||
@ -767,6 +768,7 @@ def channels(request, code):
|
||||
"enable_matrix": settings.MATRIX_ACCESS_TOKEN is not None,
|
||||
"enable_mattermost": settings.MATTERMOST_ENABLED is True,
|
||||
"enable_msteams": settings.MSTEAMS_ENABLED is True,
|
||||
"enable_opsgenie": settings.OPSGENIE_ENABLED is True,
|
||||
"enable_pdc": settings.PD_VENDOR_KEY is not None,
|
||||
"enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None,
|
||||
"enable_pushover": settings.PUSHOVER_API_TOKEN is not None,
|
||||
@ -1432,12 +1434,13 @@ def add_pushover(request, code):
|
||||
return render(request, "integrations/add_pushover.html", ctx)
|
||||
|
||||
|
||||
@require_setting("OPSGENIE_ENABLED")
|
||||
@login_required
|
||||
def add_opsgenie(request, code):
|
||||
project = _get_rw_project_for_user(request, code)
|
||||
|
||||
if request.method == "POST":
|
||||
form = forms.AddOpsGenieForm(request.POST)
|
||||
form = forms.AddOpsgenieForm(request.POST)
|
||||
if form.is_valid():
|
||||
channel = Channel(project=project, kind="opsgenie")
|
||||
v = {"region": form.cleaned_data["region"], "key": form.cleaned_data["key"]}
|
||||
@ -1447,7 +1450,7 @@ def add_opsgenie(request, code):
|
||||
channel.assign_all_checks()
|
||||
return redirect("hc-channels", project.code)
|
||||
else:
|
||||
form = forms.AddOpsGenieForm()
|
||||
form = forms.AddOpsgenieForm()
|
||||
|
||||
ctx = {"page": "channels", "project": project, "form": form}
|
||||
return render(request, "integrations/add_opsgenie.html", ctx)
|
||||
|
@ -205,6 +205,9 @@ MATTERMOST_ENABLED = envbool("MATTERMOST_ENABLED", "True")
|
||||
# MS Teams
|
||||
MSTEAMS_ENABLED = envbool("MSTEAMS_ENABLED", "True")
|
||||
|
||||
# Opsgenie
|
||||
OPSGENIE_ENABLED = envbool("OPSGENIE_ENABLED", "True")
|
||||
|
||||
# PagerDuty
|
||||
PD_VENDOR_KEY = os.getenv("PD_VENDOR_KEY")
|
||||
|
||||
|
@ -34,7 +34,7 @@ The "unused" sends from one month do not carry over to the next month.</p>
|
||||
<p>If you want to receive repeated notifications for as long as a particular check is
|
||||
down, you have a few different options:</p>
|
||||
<ul>
|
||||
<li>If you use an <strong>incident management system</strong> (PagerDuty, VictorOps, OpsGenie,
|
||||
<li>If you use an <strong>incident management system</strong> (PagerDuty, VictorOps, Opsgenie,
|
||||
PagerTree), you can set up escalation rules there.</li>
|
||||
<li>Use the <strong>Pushover</strong> integration with the "Emergency" priority. Pushover will
|
||||
play a loud notification sound on your phone every 5 minutes until the notification
|
||||
|
@ -44,7 +44,7 @@ When an account exceeds its monthly limit, SITE_NAME will:
|
||||
If you want to receive repeated notifications for as long as a particular check is
|
||||
down, you have a few different options:
|
||||
|
||||
* If you use an **incident management system** (PagerDuty, VictorOps, OpsGenie,
|
||||
* If you use an **incident management system** (PagerDuty, VictorOps, Opsgenie,
|
||||
PagerTree), you can set up escalation rules there.
|
||||
* Use the **Pushover** integration with the "Emergency" priority. Pushover will
|
||||
play a loud notification sound on your phone every 5 minutes until the notification
|
||||
|
@ -146,6 +146,9 @@ integration.</p>
|
||||
<h2 id="MSTEAMS_ENABLED"><code>MSTEAMS_ENABLED</code></h2>
|
||||
<p>Default: <code>True</code></p>
|
||||
<p>A boolean that turns on/off the MS Teams integration. Enabled by default.</p>
|
||||
<h2 id="OPSGENIE_ENABLED"><code>OPSGENIE_ENABLED</code></h2>
|
||||
<p>Default: <code>True</code></p>
|
||||
<p>A boolean that turns on/off the Opsgenie integration. Enabled by default.</p>
|
||||
<h2 id="PD_VENDOR_KEY"><code>PD_VENDOR_KEY</code></h2>
|
||||
<p>Default: <code>None</code></p>
|
||||
<p><a href="https://www.pagerduty.com/">PagerDuty</a> vendor key,
|
||||
|
@ -242,6 +242,12 @@ Default: `True`
|
||||
|
||||
A boolean that turns on/off the MS Teams integration. Enabled by default.
|
||||
|
||||
## `OPSGENIE_ENABLED` {: #OPSGENIE_ENABLED }
|
||||
|
||||
Default: `True`
|
||||
|
||||
A boolean that turns on/off the Opsgenie integration. Enabled by default.
|
||||
|
||||
## `PD_VENDOR_KEY` {: #PD_VENDOR_KEY }
|
||||
|
||||
Default: `None`
|
||||
|
@ -287,14 +287,16 @@
|
||||
</li>
|
||||
{% endif %}
|
||||
|
||||
{% if enable_opsgenie %}
|
||||
<li>
|
||||
<img src="{% static 'img/integrations/opsgenie.png' %}"
|
||||
class="icon" alt="OpsGenie icon" />
|
||||
class="icon" alt="Opsgenie icon" />
|
||||
|
||||
<h2>OpsGenie</h2>
|
||||
<h2>Opsgenie</h2>
|
||||
<p> Alerting & Incident Management Solution for Dev & Ops.</p>
|
||||
<a href="{% url 'hc-add-opsgenie' project.code %}" class="btn btn-primary">Add Integration</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
|
||||
<li>
|
||||
<img src="{% static 'img/integrations/pd.png' %}"
|
||||
|
@ -484,15 +484,17 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if enable_opsgenie %}
|
||||
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">
|
||||
<div class="integration">
|
||||
<img src="{% static 'img/integrations/opsgenie.png' %}" class="icon" alt="" />
|
||||
<h3>
|
||||
OpsGenie<br>
|
||||
Opsgenie<br>
|
||||
<small>{% trans "Incident Management" %}</small>
|
||||
</h3>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">
|
||||
{% if enable_pdc %}
|
||||
|
@ -1,15 +1,15 @@
|
||||
{% extends "base.html" %}
|
||||
{% load humanize static hc_extras %}
|
||||
|
||||
{% block title %}OpsGenie Integration for {{ site_name }}{% endblock %}
|
||||
{% block title %}Opsgenie Integration for {{ site_name }}{% endblock %}
|
||||
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col-sm-12">
|
||||
<h1>OpsGenie</h1>
|
||||
<h1>Opsgenie</h1>
|
||||
|
||||
<p><a href="https://www.opsgenie.com/">OpsGenie</a> provides
|
||||
<p><a href="https://www.opsgenie.com/">Opsgenie</a> provides
|
||||
alerting, on-call scheduling, escalation policies, and incident tracking.
|
||||
You can integrate it with your {{ site_name }} account in a few
|
||||
simple steps.</p>
|
||||
@ -19,7 +19,7 @@
|
||||
<div class="col-sm-6">
|
||||
<span class="step-no"></span>
|
||||
<p>
|
||||
Log into your OpsGenie account,
|
||||
Log into your Opsgenie account,
|
||||
select a team, and go to the team's
|
||||
<strong>Integrations › Add integration</strong> page.
|
||||
</p>
|
||||
|
Loading…
x
Reference in New Issue
Block a user