mirror of https://github.com/jumpserver/jumpserver
[Update] 修改一些terminal storage (#2357)
parent
eee6dd1436
commit
01a101a710
|
@ -185,7 +185,7 @@ class Asset(OrgModelMixin):
|
||||||
@property
|
@property
|
||||||
def connectivity(self):
|
def connectivity(self):
|
||||||
if not self.is_unixlike():
|
if not self.is_unixlike():
|
||||||
return self.UNKNOWN
|
return self.REACHABLE
|
||||||
key = self.CONNECTIVITY_CACHE_KEY.format(str(self.id))
|
key = self.CONNECTIVITY_CACHE_KEY.format(str(self.id))
|
||||||
cached = cache.get(key, None)
|
cached = cache.get(key, None)
|
||||||
return cached if cached is not None else self.UNKNOWN
|
return cached if cached is not None else self.UNKNOWN
|
||||||
|
|
|
@ -215,10 +215,10 @@ class LogTailApi(generics.RetrieveAPIView):
|
||||||
return Response({
|
return Response({
|
||||||
"data": 'Not found the log',
|
"data": 'Not found the log',
|
||||||
'end': True,
|
'end': True,
|
||||||
'mark': mark}
|
'mark': mark
|
||||||
)
|
})
|
||||||
else:
|
else:
|
||||||
return Response({"data": _("Waiting ...\n")}, status=200)
|
return Response({"data": "Waiting...\r\n"}, status=200)
|
||||||
|
|
||||||
with open(log_path, 'r') as f:
|
with open(log_path, 'r') as f:
|
||||||
offset = cache.get(mark, 0)
|
offset = cache.get(mark, 0)
|
||||||
|
|
|
@ -100,15 +100,18 @@ class StatusViewSet(viewsets.ModelViewSet):
|
||||||
task_serializer_class = serializers.TaskSerializer
|
task_serializer_class = serializers.TaskSerializer
|
||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
super().create(request, *args, **kwargs)
|
self.handle_status(request)
|
||||||
self.handle_sessions()
|
self.handle_sessions()
|
||||||
tasks = self.request.user.terminal.task_set.filter(is_finished=False)
|
tasks = self.request.user.terminal.task_set.filter(is_finished=False)
|
||||||
serializer = self.task_serializer_class(tasks, many=True)
|
serializer = self.task_serializer_class(tasks, many=True)
|
||||||
return Response(serializer.data, status=201)
|
return Response(serializer.data, status=201)
|
||||||
|
|
||||||
|
def handle_status(self, request):
|
||||||
|
request.user.terminal.is_alive = True
|
||||||
|
|
||||||
def handle_sessions(self):
|
def handle_sessions(self):
|
||||||
sessions_id = self.request.data.get('sessions', [])
|
sessions_id = self.request.data.get('sessions', [])
|
||||||
Session.set_active_sessions(sessions_id)
|
Session.set_sessions_active(sessions_id)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
terminal_id = self.kwargs.get("terminal", None)
|
terminal_id = self.kwargs.get("terminal", None)
|
||||||
|
|
|
@ -13,6 +13,7 @@ from django.core.cache import cache
|
||||||
from users.models import User
|
from users.models import User
|
||||||
from orgs.mixins import OrgModelMixin
|
from orgs.mixins import OrgModelMixin
|
||||||
from common.utils import get_command_storage_setting, get_replay_storage_setting
|
from common.utils import get_command_storage_setting, get_replay_storage_setting
|
||||||
|
from .backends import get_multi_command_storage
|
||||||
from .backends.command.models import AbstractSessionCommand
|
from .backends.command.models import AbstractSessionCommand
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,6 +30,17 @@ class Terminal(models.Model):
|
||||||
is_deleted = models.BooleanField(default=False)
|
is_deleted = models.BooleanField(default=False)
|
||||||
date_created = models.DateTimeField(auto_now_add=True)
|
date_created = models.DateTimeField(auto_now_add=True)
|
||||||
comment = models.TextField(blank=True, verbose_name=_('Comment'))
|
comment = models.TextField(blank=True, verbose_name=_('Comment'))
|
||||||
|
STATUS_KEY_PREFIX = 'terminal_status_'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_alive(self):
|
||||||
|
key = self.STATUS_KEY_PREFIX + str(self.id)
|
||||||
|
return bool(cache.get(key))
|
||||||
|
|
||||||
|
@is_alive.setter
|
||||||
|
def is_alive(self, value):
|
||||||
|
key = self.STATUS_KEY_PREFIX + str(self.id)
|
||||||
|
cache.set(key, value, 60)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_active(self):
|
def is_active(self):
|
||||||
|
@ -42,7 +54,7 @@ class Terminal(models.Model):
|
||||||
self.user.is_active = active
|
self.user.is_active = active
|
||||||
self.user.save()
|
self.user.save()
|
||||||
|
|
||||||
def get_common_storage(self):
|
def get_command_storage_setting(self):
|
||||||
storage_all = get_command_storage_setting()
|
storage_all = get_command_storage_setting()
|
||||||
if self.command_storage in storage_all:
|
if self.command_storage in storage_all:
|
||||||
storage = storage_all.get(self.command_storage)
|
storage = storage_all.get(self.command_storage)
|
||||||
|
@ -50,7 +62,7 @@ class Terminal(models.Model):
|
||||||
storage = storage_all.get('default')
|
storage = storage_all.get('default')
|
||||||
return {"TERMINAL_COMMAND_STORAGE": storage}
|
return {"TERMINAL_COMMAND_STORAGE": storage}
|
||||||
|
|
||||||
def get_replay_storage(self):
|
def get_replay_storage_setting(self):
|
||||||
storage_all = get_replay_storage_setting()
|
storage_all = get_replay_storage_setting()
|
||||||
if self.replay_storage in storage_all:
|
if self.replay_storage in storage_all:
|
||||||
storage = storage_all.get(self.replay_storage)
|
storage = storage_all.get(self.replay_storage)
|
||||||
|
@ -65,8 +77,8 @@ class Terminal(models.Model):
|
||||||
if not k.startswith('TERMINAL'):
|
if not k.startswith('TERMINAL'):
|
||||||
continue
|
continue
|
||||||
configs[k] = getattr(settings, k)
|
configs[k] = getattr(settings, k)
|
||||||
configs.update(self.get_common_storage())
|
configs.update(self.get_command_storage_setting())
|
||||||
configs.update(self.get_replay_storage())
|
configs.update(self.get_replay_storage_setting())
|
||||||
configs.update({
|
configs.update({
|
||||||
'SECURITY_MAX_IDLE_TIME': settings.SECURITY_MAX_IDLE_TIME
|
'SECURITY_MAX_IDLE_TIME': settings.SECURITY_MAX_IDLE_TIME
|
||||||
})
|
})
|
||||||
|
@ -185,16 +197,25 @@ class Session(OrgModelMixin):
|
||||||
return None, e
|
return None, e
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_active_sessions(cls, sessions_id):
|
def set_sessions_active(cls, sessions_id):
|
||||||
data = {cls.ACTIVE_CACHE_KEY_PREFIX.format(i): i for i in sessions_id}
|
data = {cls.ACTIVE_CACHE_KEY_PREFIX.format(i): i for i in sessions_id}
|
||||||
cache.set_many(data, timeout=5*60)
|
cache.set_many(data, timeout=5*60)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_active_sessions(cls):
|
||||||
|
return cls.objects.filter(is_finished=False)
|
||||||
|
|
||||||
def is_active(self):
|
def is_active(self):
|
||||||
if self.protocol in ['ssh', 'telnet']:
|
if self.protocol in ['ssh', 'telnet']:
|
||||||
key = self.ACTIVE_CACHE_KEY_PREFIX.format(self.id)
|
key = self.ACTIVE_CACHE_KEY_PREFIX.format(self.id)
|
||||||
return bool(cache.get(key))
|
return bool(cache.get(key))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def command_amount(self):
|
||||||
|
command_store = get_multi_command_storage()
|
||||||
|
return command_store.count(session=str(self.id))
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = "terminal_session"
|
db_table = "terminal_session"
|
||||||
ordering = ["-date_start"]
|
ordering = ["-date_start"]
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
from django.core.cache import cache
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework_bulk.serializers import BulkListSerializer
|
from rest_framework_bulk.serializers import BulkListSerializer
|
||||||
|
|
||||||
from common.mixins import BulkSerializerMixin
|
from common.mixins import BulkSerializerMixin
|
||||||
from ..models import Terminal, Status, Session, Task
|
from ..models import Terminal, Status, Session, Task
|
||||||
from ..backends import get_multi_command_storage
|
|
||||||
|
|
||||||
|
|
||||||
class TerminalSerializer(serializers.ModelSerializer):
|
class TerminalSerializer(serializers.ModelSerializer):
|
||||||
session_online = serializers.SerializerMethodField()
|
session_online = serializers.SerializerMethodField()
|
||||||
is_alive = serializers.SerializerMethodField()
|
is_alive = serializers.BooleanField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Terminal
|
model = Terminal
|
||||||
|
@ -23,40 +21,23 @@ class TerminalSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_session_online(obj):
|
def get_session_online(obj):
|
||||||
return Session.objects.filter(terminal=obj.id, is_finished=False).count()
|
return Session.objects.filter(terminal=obj, is_finished=False).count()
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_is_alive(obj):
|
|
||||||
key = StatusSerializer.CACHE_KEY_PREFIX + str(obj.id)
|
|
||||||
return cache.get(key)
|
|
||||||
|
|
||||||
|
|
||||||
class SessionSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
class SessionSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
||||||
command_amount = serializers.SerializerMethodField()
|
command_amount = serializers.IntegerField()
|
||||||
command_store = get_multi_command_storage()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Session
|
model = Session
|
||||||
list_serializer_class = BulkListSerializer
|
list_serializer_class = BulkListSerializer
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
def get_command_amount(self, obj):
|
|
||||||
return self.command_store.count(session=str(obj.id))
|
|
||||||
|
|
||||||
|
|
||||||
class StatusSerializer(serializers.ModelSerializer):
|
class StatusSerializer(serializers.ModelSerializer):
|
||||||
CACHE_KEY_PREFIX = 'terminal_status_'
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
fields = '__all__'
|
fields = ['id', 'terminal']
|
||||||
model = Status
|
model = Status
|
||||||
|
|
||||||
def create(self, validated_data):
|
|
||||||
terminal_id = str(validated_data['terminal'].id)
|
|
||||||
key = self.CACHE_KEY_PREFIX + terminal_id
|
|
||||||
cache.set(key, 1, 60)
|
|
||||||
return validated_data
|
|
||||||
|
|
||||||
|
|
||||||
class TaskSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
class TaskSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,3 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
from celery import shared_task
|
|
||||||
from django.core.cache import cache
|
|
||||||
from django.db.utils import ProgrammingError, OperationalError
|
|
||||||
|
|
||||||
from common.utils import get_logger
|
|
||||||
from .const import ASSETS_CACHE_KEY, USERS_CACHE_KEY, SYSTEM_USER_CACHE_KEY
|
|
||||||
|
|
||||||
RUNNING = False
|
|
||||||
logger = get_logger(__file__)
|
|
||||||
|
|
||||||
|
|
||||||
def set_session_info_cache():
|
|
||||||
logger.debug("")
|
|
||||||
from .utils import get_session_asset_list, get_session_user_list, \
|
|
||||||
get_session_system_user_list
|
|
||||||
|
|
||||||
try:
|
|
||||||
assets = get_session_asset_list()
|
|
||||||
users = get_session_user_list()
|
|
||||||
system_users = get_session_system_user_list()
|
|
||||||
|
|
||||||
cache.set(ASSETS_CACHE_KEY, assets)
|
|
||||||
cache.set(USERS_CACHE_KEY, users)
|
|
||||||
cache.set(SYSTEM_USER_CACHE_KEY, system_users)
|
|
||||||
except (ProgrammingError, OperationalError):
|
|
||||||
pass
|
|
||||||
|
|
|
@ -94,7 +94,7 @@
|
||||||
<td class="text-center">{{ session.remote_addr|default:"" }}</td>
|
<td class="text-center">{{ session.remote_addr|default:"" }}</td>
|
||||||
<td class="text-center">{{ session.protocol }}</td>
|
<td class="text-center">{{ session.protocol }}</td>
|
||||||
<td class="text-center">{{ session.get_login_from_display }}</td>
|
<td class="text-center">{{ session.get_login_from_display }}</td>
|
||||||
<td class="text-center">{{ session.id | get_session_command_amount }}</td>
|
<td class="text-center">{{ session.command_amount }}</td>
|
||||||
|
|
||||||
<td class="text-center">{{ session.date_start }}</td>
|
<td class="text-center">{{ session.date_start }}</td>
|
||||||
{# <td class="text-center">{{ session.date_last_active }}</td>#}
|
{# <td class="text-center">{{ session.date_last_active }}</td>#}
|
||||||
|
|
Loading…
Reference in New Issue