2020-05-27 12:24:06 +00:00
|
|
|
import json
|
2023-08-09 06:20:18 +00:00
|
|
|
import logging
|
2020-05-27 12:24:06 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
from django.http import HttpResponse
|
2020-05-27 12:24:06 +00:00
|
|
|
|
|
|
|
from admin.decorators import superuser_only
|
2017-12-11 14:07:01 +00:00
|
|
|
from instances.models import Instance
|
2015-03-03 09:34:09 +00:00
|
|
|
from logs.models import Logs
|
|
|
|
|
|
|
|
|
2021-05-31 08:39:09 +00:00
|
|
|
def addlogmsg(user, host, instance, message):
|
2015-03-18 13:48:29 +00:00
|
|
|
"""
|
2019-09-10 06:48:31 +00:00
|
|
|
:param user:
|
2021-05-31 08:39:09 +00:00
|
|
|
:param host:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param instance:
|
|
|
|
:param message:
|
2015-03-18 13:48:29 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2021-05-31 08:39:09 +00:00
|
|
|
add_log_msg = Logs(user=user, host=host, instance=instance, message=message)
|
2015-03-18 13:48:29 +00:00
|
|
|
add_log_msg.save()
|
2023-08-09 06:20:18 +00:00
|
|
|
logger = logging.getLogger("")
|
|
|
|
logger.info(user + " " + host+ " " + instance+ " " + message)
|
2015-03-18 13:48:29 +00:00
|
|
|
|
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2017-12-11 14:07:01 +00:00
|
|
|
def vm_logs(request, vname):
|
|
|
|
"""
|
|
|
|
:param request:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param vname:
|
2017-12-11 14:07:01 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
vm = Instance.objects.get(name=vname)
|
2020-11-05 09:34:31 +00:00
|
|
|
logs_ = Logs.objects.filter(instance=vm.name, date__gte=vm.created).order_by("-date")
|
2017-12-11 14:07:01 +00:00
|
|
|
logs = []
|
|
|
|
for l in logs_:
|
2019-09-10 06:48:31 +00:00
|
|
|
log = dict()
|
2020-11-05 09:34:31 +00:00
|
|
|
log["user"] = l.user
|
2021-05-31 08:39:09 +00:00
|
|
|
log["host"] = l.host
|
2020-11-05 09:34:31 +00:00
|
|
|
log["instance"] = l.instance
|
|
|
|
log["message"] = l.message
|
|
|
|
log["date"] = l.date.strftime("%x %X")
|
2017-12-11 14:07:01 +00:00
|
|
|
logs.append(log)
|
2020-05-27 12:24:06 +00:00
|
|
|
|
2017-12-11 14:07:01 +00:00
|
|
|
return HttpResponse(json.dumps(logs))
|