You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/jlog/views.py

121 lines
4.1 KiB

# coding:utf-8
10 years ago
from django.db.models import Q
10 years ago
from django.template import RequestContext
from django.shortcuts import render_to_response
10 years ago
10 years ago
from jumpserver.api import *
from jasset.views import httperror
10 years ago
from django.http import HttpResponseNotFound
10 years ago
CONF = ConfigParser()
CONF.read('%s/jumpserver.conf' % BASE_DIR)
10 years ago
def get_user_info(request, offset):
10 years ago
""" 获取用户信息及环境 """
10 years ago
env_dic = {'online': 0, 'offline': 1}
env = env_dic[offset]
keyword = request.GET.get('keyword', '')
user_info = get_session_user_info(request)
user_id, username = user_info[0:2]
dept_id, dept_name = user_info[3:5]
ret = [request, keyword, env, username, dept_name]
return ret
def get_user_log(ret_list):
10 years ago
""" 获取不同类型用户日志记录 """
10 years ago
request, keyword, env, username, dept_name = ret_list
10 years ago
post_all = Log.objects.filter(is_finished=env).order_by('-start_time')
post_keyword_all = Log.objects.filter(Q(user__contains=keyword) |
Q(host__contains=keyword)) \
.filter(is_finished=env).order_by('-start_time')
10 years ago
if is_super_user(request):
if keyword:
10 years ago
posts = post_keyword_all
10 years ago
else:
10 years ago
posts = post_all
10 years ago
elif is_group_admin(request):
if keyword:
10 years ago
posts = post_keyword_all.filter(dept_name=dept_name)
10 years ago
else:
10 years ago
posts = post_all.filter(dept_name=dept_name)
10 years ago
10 years ago
elif is_common_user(request):
10 years ago
if keyword:
10 years ago
posts = post_keyword_all.filter(user=username)
10 years ago
else:
10 years ago
posts = post_all.filter(user=username)
10 years ago
return posts
10 years ago
10 years ago
10 years ago
@require_login
def log_list(request, offset):
10 years ago
""" 显示日志 """
10 years ago
header_title, path1, path2 = u'查看日志', u'查看日志', u'在线用户'
10 years ago
keyword = request.GET.get('keyword', '')
web_socket_host = CONF.get('websocket', 'web_socket_host')
10 years ago
posts = get_user_log(get_user_info(request, offset))
10 years ago
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
10 years ago
10 years ago
return render_to_response('jlog/log_%s.html' % offset, locals(), context_instance=RequestContext(request))
10 years ago
@require_admin
10 years ago
def log_kill(request):
10 years ago
""" 杀掉connect进程 """
10 years ago
pid = request.GET.get('id', '')
log = Log.objects.filter(pid=pid)
if log:
log = log[0]
10 years ago
dept_name = log.dept_name
deptname = get_session_user_info(request)[4]
if is_group_admin(request) and dept_name != deptname:
10 years ago
return httperror(request, u'Kill失败, 您无权操作!')
10 years ago
try:
os.kill(int(pid), 9)
except OSError:
pass
10 years ago
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))
10 years ago
else:
return HttpResponseNotFound(u'没有此进程!')
10 years ago
10 years ago
@require_login
10 years ago
def log_history(request):
10 years ago
""" 命令历史记录 """
10 years ago
log_id = request.GET.get('id', 0)
log = Log.objects.filter(id=int(log_id))
if log:
log = log[0]
10 years ago
dept_name = log.dept_name
deptname = get_session_user_info(request)[4]
if is_group_admin(request) and dept_name != deptname:
return httperror(request, '查看失败, 您无权查看!')
elif is_common_user(request):
return httperror(request, '查看失败, 您无权查看!')
log_his = "%s.his" % log.log_path
if os.path.isfile(log_his):
f = open(log_his)
content = f.read()
return HttpResponse(content)
else:
return httperror(request, '无日志记录, 请查看日志处理脚本是否开启!')
10 years ago
10 years ago
@require_login
10 years ago
def log_search(request):
10 years ago
""" 日志搜索 """
10 years ago
offset = request.GET.get('env', '')
10 years ago
keyword = request.GET.get('keyword', '')
10 years ago
posts = get_user_log(get_user_info(request, offset))
10 years ago
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
10 years ago
return render_to_response('jlog/log_search.html', locals(), context_instance=RequestContext(request))