Router list automatically updating online status

This commit is contained in:
Eduardo Silva 2024-04-15 13:02:13 -03:00
parent 3885e8d667
commit 847775f87a
4 changed files with 86 additions and 8 deletions

View file

@ -1,7 +1,7 @@
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from monitoring.models import RouterDownTime
from router_manager.models import Router
from router_manager.models import Router, RouterStatus, RouterGroup
from django.http import JsonResponse
from django.utils import timezone
@ -17,15 +17,30 @@ def view_router_config_timestamp(request):
return JsonResponse({'router_config': webadmin_settings.router_config_last_updated.isoformat()})
def view_router_last_status_change(request):
last_router_status_change = RouterStatus.objects.filter(last_status_change__isnull=False).order_by('-last_status_change').first()
if last_router_status_change:
last_status_change_timestamp = last_router_status_change.last_status_change.isoformat()
else:
last_status_change_timestamp = "0"
return JsonResponse({'last_status_change': last_status_change_timestamp})
def view_export_router_list(request):
router_list = {}
webadmin_settings, _ = WebadminSettings.objects.get_or_create(name='webadmin_settings')
webadmin_settings.monitoring_last_run = timezone.now()
# Not updating the monitoring last run here, as this view is also used on the dashboard
if not webadmin_settings.router_config_last_updated:
webadmin_settings.router_config_last_updated = timezone.now()
webadmin_settings.save()
for router in Router.objects.filter(enabled=True, monitoring=True):
if request.GET.get('filter_group'):
router_group = get_object_or_404(RouterGroup, uuid=request.GET.get('filter_group'))
query_router_list = router_group.routers.all().filter(enabled=True, monitoring=True)
else:
query_router_list = Router.objects.filter(enabled=True, monitoring=True)
router_list = {}
for router in query_router_list:
router_list[str(router.uuid)] = {
'address': router.address,
'name': router.name,

View file

@ -16,6 +16,12 @@ from user_manager.models import UserAcl
@login_required
def view_router_list(request):
router_list = Router.objects.all().order_by('name')
last_router_status_change = RouterStatus.objects.filter(last_status_change__isnull=False).order_by('-last_status_change').first()
if last_router_status_change:
last_status_change_timestamp = last_router_status_change.last_status_change.isoformat()
else:
last_status_change_timestamp = 0
default_backup_profile, _ = BackupProfile.objects.get_or_create(name='default')
filter_group = None
if request.GET.get('filter_group'):
@ -32,6 +38,7 @@ def view_router_list(request):
'page_title': 'Router List',
'filter_group_list': RouterGroup.objects.all().order_by('name'),
'filter_group': filter_group,
'last_status_change_timestamp': last_status_change_timestamp,
}
return render(request, 'router_manager/router_list.html', context=context)

View file

@ -6,7 +6,7 @@ from user_manager.views import view_manage_user, view_user_list
from accounts.views import view_login, view_logout, view_create_first_user
from router_manager.views import view_router_list, view_manage_router, view_router_group_list, view_ssh_key_list, view_manage_router_group, view_manage_sshkey, view_router_details, view_create_instant_backup_task, view_router_availability
from backup.views import view_backup_profile_list, view_manage_backup_profile, view_backup_list, view_backup_details, view_debug_run_backups, view_compare_backups, view_backup_download, view_backup_delete
from monitoring.views import view_export_router_list, view_update_router_status, view_router_config_timestamp
from monitoring.views import view_export_router_list, view_update_router_status, view_router_config_timestamp, view_router_last_status_change
from backup_data.views import view_generate_backup_schedule, view_create_backup_tasks, view_perform_backup_tasks, view_housekeeping
from routerfleet_tools.views import cron_check_updates
@ -40,6 +40,7 @@ urlpatterns = [
path('monitoring/export_router_list/', view_export_router_list, name='export_router_list'),
path('monitoring/update_router_status/', view_update_router_status, name='update_router_status'),
path('monitoring/router_config_timestamp/', view_router_config_timestamp, name='router_config_timestamp'),
path('monitoring/last_status_change/', view_router_last_status_change, name='last_status_change'),
path('cron/generate_backup_schedule/', view_generate_backup_schedule, name='generate_backup_schedule'),
path('cron/create_backup_tasks/', view_create_backup_tasks, name='create_backup_tasks'),
path('cron/perform_backup_tasks/', view_perform_backup_tasks, name='perform_backup_tasks'),

View file

@ -37,7 +37,7 @@
<td>{{ router.get_router_type_display }}</td>
<td>{{ router.address }}</td>
<td>
<td id="status-{{ router.uuid }}">
{% if router.monitoring %}
{% if router.routerstatus.status_online %}
<span style="display: none">online</span><i class="far fa-check-circle text-success" title="Host is Online"></i>
@ -95,4 +95,59 @@
{% endblock %}
{% block custom_page_scripts %}
<script>
var last_status_change = "{{ last_status_change_timestamp }}";
var group_uuid = "{{ filter_group.uuid|default_if_none:'' }}";
var status_online_text = '<span style="display: none">online</span><i class="far fa-check-circle text-success" title="Host is Online"></i>';
var status_offline_text = '<span style="display: none">offline</span><i class="far fa-times-circle text-danger" title="Host is unavailable"></i>';
function checkStatusChange() {
$.ajax({
url: "/monitoring/last_status_change/",
success: function(data) {
if (data.last_status_change !== last_status_change) {
// Update last_status_change and fetch new router list
last_status_change = data.last_status_change;
fetchRouterList();
}
}
});
}
function fetchRouterList() {
var url = group_uuid ? "/monitoring/export_router_list/?filter_group=" + group_uuid : "/monitoring/export_router_list/";
$.ajax({
url: url,
success: function(data) {
updateRouterStatuses(data.router_list);
}
});
}
function updateRouterStatuses(routerList) {
for (var uuid in routerList) {
var router = routerList[uuid];
var $statusTd = $('#status-' + uuid);
var currentStatus = $statusTd.find('span').text().trim();
var newStatus = router.online ? 'online' : 'offline';
var newStatusHtml = router.online ? status_online_text : status_offline_text;
if (currentStatus !== newStatus) {
$statusTd.html(newStatusHtml);
$(document).Toasts('create', {
class: router.online ? 'bg-success' : 'bg-danger',
title: 'Host status updated',
body: 'Host ' + router.name + ' is now ' + newStatus,
delay: 10000,
autohide: true
});
}
}
}
setInterval(checkStatusChange, 30000);
$(document).ready(function() {
checkStatusChange();
});
</script>
{% endblock %}