jumpserver/jlog/views.py

119 lines
4.2 KiB
Python
Raw Normal View History

2015-01-25 14:20:07 +00:00
# coding:utf-8
2015-02-09 11:02:25 +00:00
from django.db.models import Q
2015-03-17 05:56:43 +00:00
from django.template import RequestContext
2015-01-25 14:20:07 +00:00
from django.shortcuts import render_to_response
2014-12-26 13:57:10 +00:00
2015-04-15 10:47:02 +00:00
from jumpserver.api import *
2015-11-23 10:39:38 +00:00
from jperm.perm_api import user_have_perm
2015-04-18 05:51:47 +00:00
from django.http import HttpResponseNotFound
2015-10-27 12:25:18 +00:00
from jlog.log_api import renderTemplate
2015-10-26 14:17:16 +00:00
2015-12-03 11:10:37 +00:00
from jlog.models import Log, ExecLog
from jumpserver.settings import WEB_SOCKET_HOST
2015-01-25 14:20:07 +00:00
2015-11-04 08:24:52 +00:00
@require_role('admin')
2015-04-11 04:52:00 +00:00
def log_list(request, offset):
2015-04-16 11:03:02 +00:00
""" 显示日志 """
2015-11-04 08:24:52 +00:00
header_title, path1 = u'审计', u'操作审计'
2015-10-28 14:32:16 +00:00
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', '')
2015-12-03 11:10:37 +00:00
2015-10-26 14:17:16 +00:00
if offset == 'online':
posts = Log.objects.filter(is_finished=False).order_by('-start_time')
2015-12-03 11:10:37 +00:00
if offset == 'exec':
posts = ExecLog.objects.all().order_by('-id')
2015-10-26 14:17:16 +00:00
else:
posts = Log.objects.filter(is_finished=True).order_by('-start_time')
2015-10-28 14:32:16 +00:00
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 + ' 00:00:01', '%m/%d/%Y %H:%M:%S')
datetime_end = datetime.datetime.strptime(date_now_str + ' 23:59:59', '%m/%d/%Y %H:%M:%S')
2015-10-28 14:32:16 +00:00
posts = posts.filter(start_time__gte=datetime_start).filter(start_time__lte=datetime_end)
if username_list:
posts = posts.filter(user__in=username_list)
2015-10-27 15:18:26 +00:00
2015-10-28 14:32:16 +00:00
if host_list:
posts = posts.filter(host__in=host_list)
2015-11-26 15:26:11 +00:00
2015-10-28 14:32:16 +00:00
if cmd:
log_id_list = set([log.log_id for log in TtyLog.objects.filter(cmd__contains=cmd)])
posts = posts.filter(id__in=log_id_list)
2015-11-26 15:26:11 +00:00
if not date_seven_day:
2015-10-27 15:18:26 +00:00
date_now = datetime.datetime.now()
date_now_str = date_now.strftime('%m/%d/%Y')
date_seven_day = (date_now + datetime.timedelta(days=-7)).strftime('%m/%d/%Y')
2015-10-29 03:11:05 +00:00
2015-04-11 04:52:00 +00:00
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
2015-03-19 10:32:10 +00:00
web_monitor_uri = 'ws://%s/monitor' % WEB_SOCKET_HOST
web_kill_uri = 'http://%s/kill' % WEB_SOCKET_HOST
2015-11-24 08:31:06 +00:00
session_id = request.session.session_key
2015-04-11 04:52:00 +00:00
return render_to_response('jlog/log_%s.html' % offset, locals(), context_instance=RequestContext(request))
2015-01-25 14:20:07 +00:00
2015-11-04 08:24:52 +00:00
@require_role('admin')
def log_kill(request):
""" 杀掉connect进程 """
pid = request.GET.get('id', '')
log = Log.objects.filter(pid=pid)
if log:
log = log[0]
try:
os.kill(int(pid), 9)
except OSError:
pass
Log.objects.filter(pid=pid).update(is_finished=1, end_time=datetime.datetime.now())
return render_to_response('jlog/log_offline.html', locals(), context_instance=RequestContext(request))
else:
return HttpResponseNotFound(u'没有此进程!')
2015-10-27 12:25:18 +00:00
2015-11-04 08:24:52 +00:00
@require_role('admin')
2015-10-27 12:25:18 +00:00
def log_history(request):
""" 命令历史记录 """
log_id = request.GET.get('id', 0)
2015-10-28 14:32:16 +00:00
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)
2015-11-10 08:00:05 +00:00
return HttpResponse('无日志记录!')
2015-10-27 12:25:18 +00:00
2015-11-04 08:24:52 +00:00
@require_role('admin')
2015-10-27 12:25:18 +00:00
def log_record(request):
log_id = request.GET.get('id', 0)
log = Log.objects.filter(id=int(log_id))
if log:
log = log[0]
log_file = log.log_path + '.log'
log_time = log.log_path + '.time'
if os.path.isfile(log_file) and os.path.isfile(log_time):
content = renderTemplate(log_file, log_time)
return HttpResponse(content)
else:
2015-11-10 08:00:05 +00:00
return HttpResponse('无日志记录!')
2015-10-27 12:25:18 +00:00
2015-11-24 03:01:54 +00:00
@require_role('user')
2015-11-05 10:31:00 +00:00
def web_terminal(request):
2015-11-23 10:39:38 +00:00
asset_id = request.GET.get('id')
2015-11-24 03:01:54 +00:00
role_name = request.GET.get('role')
web_terminal_uri = 'ws://%s/terminal?id=%s&role=%s' % (WEB_SOCKET_HOST, asset_id, role_name)
2015-11-05 10:31:00 +00:00
return render_to_response('jlog/web_terminal.html', locals())
2015-11-02 15:20:40 +00:00