Fix Logging settings "Test" button functionality

This commit is contained in:
Christian Adams
2020-03-11 14:37:22 -04:00
parent 996d7ce054
commit d31c528257
6 changed files with 46 additions and 7 deletions

View File

@@ -5,6 +5,9 @@
import collections
import logging
import sys
import socket
import os
from urllib.parse import urlparse
# Django
from django.conf import settings
@@ -12,7 +15,7 @@ from django.http import Http404
from django.utils.translation import ugettext_lazy as _
# Django REST Framework
from rest_framework.exceptions import PermissionDenied, ValidationError
from rest_framework.exceptions import PermissionDenied
from rest_framework.response import Response
from rest_framework import serializers
from rest_framework import status
@@ -161,8 +164,41 @@ class SettingLoggingTest(GenericAPIView):
filter_backends = []
def post(self, request, *args, **kwargs):
logging.getLogger('awx').info('AWX Connection Test')
return Response(status=status.HTTP_202_ACCEPTED)
# Send test message to configured logger based on db settings
logging.getLogger('awx').error('AWX Connection Test Message')
hostname = getattr(settings, 'LOG_AGGREGATOR_HOST', None)
protocol = getattr(settings, 'LOG_AGGREGATOR_PROTOCOL', None)
# Check if host is reacheable
host = urlparse(hostname).netloc
response = os.system("ping -c 1 " + host)
if response != 0:
return Response({'error': 'The host is not available'}, status=status.HTTP_400_BAD_REQUEST)
# Check to ensure port is open at host
if protocol in ['udp', 'tcp']:
port = getattr(settings, 'LOG_AGGREGATOR_PORT', None)
# Error if port is not set when using UDP/TCP
if not port:
return Response({'error': 'Port required for ' + protocol}, status=status.HTTP_400_BAD_REQUEST)
else:
return Response(status=status.HTTP_202_ACCEPTED)
# Error if logging is not enabled
enabled = getattr(settings, 'LOG_AGGREGATOR_ENABLED', False)
if not enabled:
return Response({'error': 'Logging not enabled'}, status=status.HTTP_400_BAD_REQUEST)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.settimeout(.5)
s.connect((hostname, int(port)))
s.shutdown(2)
s.close()
return Response(status=status.HTTP_202_ACCEPTED)
except Exception as e:
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
# Create view functions for all of the class-based views to simplify inclusion