mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-01 15:37:19 +00:00
[Feature] 支持es存储
This commit is contained in:
@@ -3,7 +3,7 @@ from django.conf import settings
|
||||
from .command.serializers import SessionCommandSerializer
|
||||
|
||||
TYPE_ENGINE_MAPPING = {
|
||||
'elasticsearch': 'terminal.backends.command.db',
|
||||
'elasticsearch': 'terminal.backends.command.es',
|
||||
}
|
||||
|
||||
|
||||
@@ -31,4 +31,10 @@ def get_terminal_command_store():
|
||||
return storage_list
|
||||
|
||||
|
||||
def get_multi_command_store():
|
||||
from .command.multi import CommandStore
|
||||
storage_list = get_terminal_command_store().values()
|
||||
storage = CommandStore(storage_list)
|
||||
return storage
|
||||
|
||||
|
||||
|
@@ -73,7 +73,7 @@ class CommandStore(CommandBase):
|
||||
session=session,
|
||||
)
|
||||
queryset = self.model.objects.filter(**filter_kwargs)
|
||||
return queryset
|
||||
return [command.to_dict() for command in queryset]
|
||||
|
||||
def count(self, date_from=None, date_to=None,
|
||||
user=None, asset=None, system_user=None,
|
||||
|
38
apps/terminal/backends/command/es.py
Normal file
38
apps/terminal/backends/command/es.py
Normal file
@@ -0,0 +1,38 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
|
||||
from jms_es_sdk import ESStore
|
||||
from .base import CommandBase
|
||||
|
||||
|
||||
class CommandStore(CommandBase, ESStore):
|
||||
def __init__(self, params):
|
||||
hosts = params.get('HOSTS', ['http://localhost'])
|
||||
ESStore.__init__(self, hosts=hosts)
|
||||
|
||||
def save(self, command):
|
||||
return ESStore.save(self, command)
|
||||
|
||||
def bulk_save(self, commands):
|
||||
return ESStore.bulk_save(self, commands)
|
||||
|
||||
def filter(self, date_from=None, date_to=None,
|
||||
user=None, asset=None, system_user=None,
|
||||
input=None, session=None):
|
||||
|
||||
data = ESStore.filter(
|
||||
self, date_from=date_from, date_to=date_to,
|
||||
user=user, asset=asset, system_user=system_user,
|
||||
input=input, session=session
|
||||
)
|
||||
return [item["_source"] for item in data["hits"] if item]
|
||||
|
||||
def count(self, date_from=None, date_to=None,
|
||||
user=None, asset=None, system_user=None,
|
||||
input=None, session=None):
|
||||
amount = ESStore.count(
|
||||
self, date_from=date_from, date_to=date_to,
|
||||
user=user, asset=asset, system_user=system_user,
|
||||
input=input, session=session
|
||||
)
|
||||
return amount
|
@@ -33,5 +33,11 @@ class AbstractSessionCommand(models.Model):
|
||||
commands.append(command)
|
||||
return commands
|
||||
|
||||
def to_dict(self):
|
||||
d = {}
|
||||
for field in self._meta.fields:
|
||||
d[field.name] = getattr(self, field.name)
|
||||
return d
|
||||
|
||||
def __str__(self):
|
||||
return self.input
|
||||
|
27
apps/terminal/backends/command/multi.py
Normal file
27
apps/terminal/backends/command/multi.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
|
||||
from .base import CommandBase
|
||||
|
||||
|
||||
class CommandStore(CommandBase):
|
||||
def __init__(self, storage_list):
|
||||
self.storage_list = storage_list
|
||||
|
||||
def filter(self, **kwargs):
|
||||
queryset = []
|
||||
for storage in self.storage_list:
|
||||
queryset.extend(storage.filter(**kwargs))
|
||||
return sorted(queryset, key=lambda command: command["timestamp"])
|
||||
|
||||
def count(self, **kwargs):
|
||||
amount = 0
|
||||
for storage in self.storage_list:
|
||||
amount += storage.count(**kwargs)
|
||||
return amount
|
||||
|
||||
def save(self, command):
|
||||
pass
|
||||
|
||||
def bulk_save(self, commands):
|
||||
pass
|
Reference in New Issue
Block a user