From d5bd2143e28767513b6e7fa0c26b79a8c4a8b366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B9=BF=E5=AE=8F=E4=BC=9F?= Date: Wed, 28 Oct 2015 22:32:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=90=9C=E7=B4=A0=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jlog/models.py | 5 +-- jlog/views.py | 55 ++++++++++++++++++++++----------- jumpserver/api.py | 2 +- templates/jlog/log_offline.html | 32 ++++++------------- templates/jlog/log_online.html | 1 - 5 files changed, 48 insertions(+), 47 deletions(-) diff --git a/jlog/models.py b/jlog/models.py index 5b9c8d1f7..caae2b999 100644 --- a/jlog/models.py +++ b/jlog/models.py @@ -22,10 +22,7 @@ class Alert(models.Model): class TtyLog(models.Model): - log_id = models.IntegerField(max_length=50) - username = models.CharField(max_length=100) - host = models.CharField(max_length=100) - remote_ip = models.CharField(max_length=100) + log = models.ForeignKey(Log) datetime = models.DateTimeField() cmd = models.CharField(max_length=200) diff --git a/jlog/views.py b/jlog/views.py index d43ab3580..3a3515f9f 100644 --- a/jlog/views.py +++ b/jlog/views.py @@ -43,23 +43,38 @@ from jlog.log_api import renderTemplate def log_list(request, offset): """ 显示日志 """ header_title, path1, path2 = u'查看日志', u'查看日志', u'在线用户' - keyword = request.GET.get('keyword', None) # posts = get_user_log(get_user_info(request, offset)) - + date_seven_day = request.GET.get('start', '') + date_now_str = request.GET.get('end', '') + username_list = request.GET.getlist('username', []) + host_list = request.GET.getlist('host', []) + cmd = request.GET.get('cmd', '') + print date_seven_day, date_now_str if offset == 'online': web_socket_host = CONF.get('websocket', 'web_socket_host') posts = Log.objects.filter(is_finished=False).order_by('-start_time') else: posts = Log.objects.filter(is_finished=True).order_by('-start_time') - if keyword is not None: - date_seven_day = request.GET.get('start') - date_now_str = request.GET.get('end') - datetime_start = datetime.datetime.strptime(date_seven_day, '%m/%d/%Y') - datetime_end = datetime.datetime.strptime(date_now_str, '%m/%d/%Y') - print datetime_start, datetime_end - posts = posts.filter(start_time__gte=datetime_start).filter(start_time__lte=datetime_end).filter( - Q(user__icontains=keyword) | Q(host__icontains=keyword) | Q(remote_ip__icontains=keyword)) + username_all = set([log.user for log in Log.objects.all()]) + ip_all = set([log.host for log in Log.objects.all()]) + if date_seven_day and date_now_str: + datetime_start = datetime.datetime.strptime(date_seven_day, '%m/%d/%Y %H:%M:%S') + datetime_end = datetime.datetime.strptime(date_now_str, '%m/%d/%Y %H:%M:%S') + posts = posts.filter(start_time__gte=datetime_start).filter(start_time__lte=datetime_end) + + if username_list: + print username_list + posts = posts.filter(user__in=username_list) + + if host_list: + posts = posts.filter(host__in=host_list) + print posts + if cmd: + log_id_list = set([log.log_id for log in TtyLog.objects.filter(cmd__contains=cmd)]) + print [post.id for post in posts] + posts = posts.filter(id__in=log_id_list) + print posts else: date_now = datetime.datetime.now() date_now_str = date_now.strftime('%m/%d/%Y') @@ -92,14 +107,18 @@ def log_list(request, offset): def log_history(request): """ 命令历史记录 """ log_id = request.GET.get('id', 0) - tty_logs = TtyLog.objects.filter(log_id=int(log_id)).order_by('datetime') - if tty_logs: - content = '' - for tty_log in tty_logs: - content += '%s: %s\n' % (tty_log.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), tty_log.cmd) - return HttpResponse(content) - else: - return HttpResponse('无日志记录, 请查看日志处理脚本是否开启!') + log = Log.objects.filter(id=log_id) + if log: + log = log[0] + tty_logs = log.ttylog_set.all() + + if tty_logs: + content = '' + for tty_log in tty_logs: + content += '%s: %s\n' % (tty_log.datetime.strftime('%Y-%m-%d %H:%M:%S'), tty_log.cmd) + return HttpResponse(content) + + return HttpResponse('无日志记录, 请查看日志处理脚本是否开启!') def log_record(request): diff --git a/jumpserver/api.py b/jumpserver/api.py index 14082c726..d53732f86 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -260,7 +260,7 @@ class Jtty(object): if str(x) in ['\r', '\n', '\r\n']: input_r = remove_control_char(input_r) - TtyLog(log_id=log.id, username=self.username, host=self.ip, remote_ip=ip_list, datetime=datetime.datetime.now(), cmd=input_r).save() + TtyLog(log=log, datetime=datetime.datetime.now(), cmd=input_r).save() input_r = '' input_mode = False diff --git a/templates/jlog/log_offline.html b/templates/jlog/log_offline.html index 4ad97a165..c460af865 100644 --- a/templates/jlog/log_offline.html +++ b/templates/jlog/log_offline.html @@ -72,38 +72,24 @@
- + {% for username in username_all %} + + {% endfor %}
- + {% for ip in ip_all %} + + {% endfor %}
- +