-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SACGF/variantgrid_private#3689 provide a page that up time robot can …
…check that checks more
- Loading branch information
Showing
16 changed files
with
195 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from dataclasses import dataclass | ||
from enum import Enum, IntEnum | ||
from functools import cached_property | ||
from typing import Optional | ||
import django | ||
from django.contrib.auth.models import User | ||
|
||
from library.utils import text_utils | ||
|
||
|
||
### | ||
# Uptime Check is similar to health check but | ||
# The results is to be displayed to a public web page, so it needs to not show sensitive information | ||
# The results are more about if critical systems are currently working or not, so we can get an alert from uptime robot if they're not | ||
### | ||
|
||
class UptimeCheckStatus(IntEnum): | ||
OKAY = 1 | ||
NON_CRITICAL_FAILURE = 2 | ||
CRITICAL_FAILURE = 3 | ||
EXCEPTION_WHEN_CHECKING = 4 | ||
|
||
@property | ||
def pretty_label(self): | ||
return text_utils.pretty_label(self.name) | ||
|
||
def severity(self) -> str: | ||
match self: | ||
case UptimeCheckStatus.OKAY: return "S" | ||
case UptimeCheckStatus.NON_CRITICAL_FAILURE: return "E" | ||
case _: return "C" | ||
|
||
@dataclass | ||
class UptimeCheckResponse: | ||
name: str | ||
status: UptimeCheckStatus | ||
note: Optional[str] = None | ||
|
||
|
||
@dataclass | ||
class UptimeCheckOverall: | ||
uptime_checks: list[UptimeCheckResponse] | ||
|
||
@cached_property | ||
def status(self) -> UptimeCheckStatus: | ||
return max(uc.status for uc in self.uptime_checks) | ||
|
||
|
||
uptime_check_signal = django.dispatch.Signal() | ||
|
||
|
||
def retrieve_uptime_response(): | ||
all_uptimes = [] | ||
|
||
status = UptimeCheckStatus.OKAY | ||
try: | ||
# note that it's quite likely if the database is down, something well before this will cause an exception | ||
# but feels dishonest to say the database is okay without checking anything | ||
User.objects.first() | ||
except: | ||
status = UptimeCheckStatus.CRITICAL_FAILURE | ||
all_uptimes.append(UptimeCheckResponse( | ||
name="Database", | ||
status=status | ||
)) | ||
|
||
for caller, result in uptime_check_signal.send_robust(sender=None): | ||
if result: | ||
if isinstance(result, Exception): | ||
all_uptimes.append(UptimeCheckResponse( | ||
name=str(caller), | ||
status=UptimeCheckStatus.EXCEPTION_WHEN_CHECKING, | ||
note=f"Exception generating health check by {caller}: {result}" | ||
)) | ||
else: | ||
all_uptimes.append(result) | ||
|
||
return UptimeCheckOverall(uptime_checks=all_uptimes) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.apps import AppConfig | ||
|
||
# TODO rename to OIDCAuth | ||
|
||
# noinspection PyUnresolvedReferences | ||
class OIDCAuthConfig(AppConfig): | ||
name = 'oidc_auth' | ||
|
||
# noinspection PyUnresolvedReferences | ||
def ready(self): | ||
# pylint: disable=import-outside-toplevel | ||
# imported to activate receivers | ||
from oidc_auth.signals import keycloak_uptime_check # pylint: disable=unused-import | ||
# pylint: enable=import-outside-toplevel |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Optional | ||
|
||
from django.dispatch import receiver | ||
from django.conf import settings | ||
|
||
from library.keycloak import Keycloak | ||
from library.uptime_check import uptime_check_signal, UptimeCheckResponse, UptimeCheckStatus | ||
|
||
|
||
@receiver(signal=uptime_check_signal) | ||
def keycloak_uptime_check(sender, **kwargs): | ||
if not settings.KEYCLOAK_SYNC_DETAILS: | ||
return None | ||
|
||
status = UptimeCheckStatus.OKAY | ||
note: Optional[str] = None | ||
try: | ||
Keycloak().ping() | ||
except Exception as ex: | ||
print(str(ex)) | ||
status = UptimeCheckStatus.CRITICAL_FAILURE | ||
note = str(ex) | ||
|
||
return UptimeCheckResponse( | ||
name="KeyCloak", | ||
status=status, | ||
note=note | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{% extends "uicore/page/base_external.html" %} | ||
{% load ui_utils %} | ||
{% block content %} | ||
<div class="container mt-4"> | ||
<div class="jumbotron"> | ||
<h1>Uptime Status</h1> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>System</th> | ||
<th>Status</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for uptime_check in uptime_response.uptime_checks %} | ||
<tr> | ||
<td>{{ uptime_check.name }}</td> | ||
<td>{{ uptime_check.status.severity | severity_icon }}{{ uptime_check.status.pretty_label }}</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
{% if uptime_response.status == 1 %} | ||
<div class="alert alert-success">Everything is OKAY</div> | ||
{% else %} | ||
<div class="alert alert-danger">{{ uptime_response.status.severity | severity_icon }}Overall Status {{ uptime_response.status.pretty_label }}</div> | ||
{% endif %} | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters