Commit b43679d3 authored by Stephan Feurer's avatar Stephan Feurer
Browse files

Add slack notification

parent bdba4d82
......@@ -7,6 +7,7 @@ from django_celery_beat.models import PeriodicTask
from django.contrib.auth.models import User
from cynomys.models import Delta
from django.core.mail import send_mail
import json
logger = logging.getLogger()
......@@ -30,8 +31,10 @@ def check_ping(host, timeout, **kwargs):
success = False
return_value = "Failed to Ping!"
## Figure out if a delta occured, if not, update the last check time
check_delta(success, return_value, parent_task)
## Figure out if a delta occured, if not, update the last check time
changed, delta_id = check_delta(success, return_value, parent_task)
if changed:
send_notification(delta_id, kwargs)
@shared_task
......@@ -52,12 +55,16 @@ def check_http(host, timeout, httpcode, **kwargs):
success = False
return_value = "Failure! Returned code: " + str(r.status_code) + ", expected code: " + str(httpcode)
check_delta(success, return_value, parent_task)
## Figure out if a delta occured, if not, update the last check time
changed, delta_id = check_delta(success, return_value, parent_task)
if changed:
send_notification(delta_id, kwargs)
## TODO
#@shared_task
# def l4_check():
# pass
@shared_task
def notify_email(delta_id, attempts):
attempts -= 1
......@@ -95,8 +102,43 @@ def notify_email(delta_id, attempts):
notify_email.delay(receivers, delta_id, attempts)
@shared_task
def notify_slack(delta_id, slack_url, attempts):
attempts -= 1
delta = Delta.objects.filter(id=delta_id).last()
task_status = "up" if delta.success else "down"
logger.debug("sending slack notification; task: {}".format(delta.task))
data = {
"text": "########\nNew Status: {}, Task: {}\nReturn Value: {}\nTimestamp: {}\nLast Check: {}\n########".format(
task_status,
delta.task,
delta.return_value,
delta.timestamp.strftime("%d.%m.%Y, %H:%M:%S"),
delta.latest_check.strftime("%d.%m.%Y, %H:%M:%S")
)
}
print(data)
# Notification was sent, exit function
response = requests.post(url=slack_url, data=json.dumps(data))
if response.status_code == 200:
logger.info("sent notification to slack")
return
# Notification was not sent, retry
logger.error("failed to send notification to slack, attempts left {}".format(attempts))
logger.error(response.text)
if attempts > 0:
notify_slack(delta_id, slack_url, attempts)
def check_delta(responsebool, return_value, parent_task):
"""Gets data from a check and compares it to the current state in the database. If a delta occurs, sends notifications"""
new_delta = Delta()
changed = False
if responsebool:
delta = Delta.objects.filter(task=parent_task).last()
if not delta:
......@@ -107,14 +149,13 @@ def check_delta(responsebool, return_value, parent_task):
delta.success = True
delta.save()
else:
new_delta = Delta()
# new_delta = Delta()
new_delta.task = parent_task
new_delta.success = True
new_delta.return_value = return_value
new_delta.save()
print("Status Changed for " + parent_task.name + " to a Success! Sending Notification")
#TODO put notification task
notify_email.delay(new_delta.pk, 3)
changed = True
else:
delta = Delta.objects.filter(task=parent_task).last()
......@@ -126,11 +167,27 @@ def check_delta(responsebool, return_value, parent_task):
delta.success = False
delta.save()
else:
new_delta = Delta()
# new_delta = Delta()
new_delta.task = parent_task
new_delta.success = False
new_delta.return_value = return_value
new_delta.save()
print("Status Changed for " + parent_task.name + " to a Failure! Sending Notification")
#TODO put notification task
notify_email.delay(new_delta.pk, 3)
changed = True
return changed, new_delta.pk
def send_notification(delta_id, kwargs):
notifier = kwargs['notifier']
if notifier == "email":
logger.debug("send_notification: email")
notify_email.delay(delta_id, 3)
elif notifier == "slack":
logger.debug("send_notification: slack")
slack_url = kwargs['url']
notify_slack.delay(delta_id, slack_url, 3)
else:
logger.debug("No notifier configured")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment