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 *
|
|
|
|
from jasset.views import httperror
|
2015-04-18 05:51:47 +00:00
|
|
|
from django.http import HttpResponseNotFound
|
2015-01-25 14:20:07 +00:00
|
|
|
|
2015-04-15 10:47:02 +00:00
|
|
|
CONF = ConfigParser()
|
2015-01-25 14:20:07 +00:00
|
|
|
CONF.read('%s/jumpserver.conf' % BASE_DIR)
|
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-04-15 10:47:02 +00:00
|
|
|
def get_user_info(request, offset):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 获取用户信息及环境 """
|
2015-04-15 10:47:02 +00:00
|
|
|
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):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 获取不同类型用户日志记录 """
|
2015-04-15 10:47:02 +00:00
|
|
|
request, keyword, env, username, dept_name = ret_list
|
2015-04-16 11:03:02 +00:00
|
|
|
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')
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
if is_super_user(request):
|
|
|
|
if keyword:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_keyword_all
|
2015-03-19 10:32:10 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_all
|
2015-03-19 10:32:10 +00:00
|
|
|
|
|
|
|
elif is_group_admin(request):
|
|
|
|
if keyword:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_keyword_all.filter(dept_name=dept_name)
|
2015-03-19 10:32:10 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_all.filter(dept_name=dept_name)
|
2015-02-09 11:02:25 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
elif is_common_user(request):
|
2015-03-26 10:42:52 +00:00
|
|
|
if keyword:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_keyword_all.filter(user=username)
|
2015-03-26 10:42:52 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_all.filter(user=username)
|
|
|
|
|
2015-04-11 04:52:00 +00:00
|
|
|
return posts
|
2015-03-19 10:32:10 +00:00
|
|
|
|
2015-02-09 11:02:25 +00:00
|
|
|
|
2015-04-11 04:52:00 +00:00
|
|
|
@require_login
|
|
|
|
def log_list(request, offset):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 显示日志 """
|
2015-04-11 04:52:00 +00:00
|
|
|
header_title, path1, path2 = u'查看日志', u'查看日志', u'在线用户'
|
2015-04-16 06:32:48 +00:00
|
|
|
keyword = request.GET.get('keyword', '')
|
2015-02-01 15:00:23 +00:00
|
|
|
web_socket_host = CONF.get('websocket', 'web_socket_host')
|
2015-04-15 10:47:02 +00:00
|
|
|
posts = get_user_log(get_user_info(request, offset))
|
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
|
|
|
|
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-04-11 04:52:00 +00:00
|
|
|
@require_admin
|
2015-04-15 10:47:02 +00:00
|
|
|
def log_kill(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 杀掉connect进程 """
|
2015-04-15 10:47:02 +00:00
|
|
|
pid = request.GET.get('id', '')
|
|
|
|
log = Log.objects.filter(pid=pid)
|
|
|
|
if log:
|
2015-04-21 06:45:38 +00:00
|
|
|
log = log[0]
|
2015-04-15 10:47:02 +00:00
|
|
|
dept_name = log.dept_name
|
|
|
|
deptname = get_session_user_info(request)[4]
|
|
|
|
if is_group_admin(request) and dept_name != deptname:
|
2015-04-18 05:51:47 +00:00
|
|
|
return httperror(request, u'Kill失败, 您无权操作!')
|
2015-06-09 01:49:31 +00:00
|
|
|
try:
|
|
|
|
os.kill(int(pid), 9)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2015-04-16 06:32:48 +00:00
|
|
|
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))
|
2015-04-18 05:51:47 +00:00
|
|
|
else:
|
|
|
|
return HttpResponseNotFound(u'没有此进程!')
|
2015-02-09 11:02:25 +00:00
|
|
|
|
|
|
|
|
2015-04-11 04:52:00 +00:00
|
|
|
@require_login
|
2015-02-09 11:02:25 +00:00
|
|
|
def log_history(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 命令历史记录 """
|
2015-04-15 10:47:02 +00:00
|
|
|
log_id = request.GET.get('id', 0)
|
|
|
|
log = Log.objects.filter(id=int(log_id))
|
|
|
|
if log:
|
2015-04-21 06:45:38 +00:00
|
|
|
log = log[0]
|
2015-04-15 10:47:02 +00:00
|
|
|
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, '无日志记录, 请查看日志处理脚本是否开启!')
|
2015-02-09 11:02:25 +00:00
|
|
|
|
|
|
|
|
2015-04-11 04:52:00 +00:00
|
|
|
@require_login
|
2015-02-09 11:02:25 +00:00
|
|
|
def log_search(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 日志搜索 """
|
2015-04-15 10:47:02 +00:00
|
|
|
offset = request.GET.get('env', '')
|
2015-04-16 06:32:48 +00:00
|
|
|
keyword = request.GET.get('keyword', '')
|
2015-04-15 10:47:02 +00:00
|
|
|
posts = get_user_log(get_user_info(request, offset))
|
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-17 05:56:43 +00:00
|
|
|
return render_to_response('jlog/log_search.html', locals(), context_instance=RequestContext(request))
|