jumpserver/jlog/views.py

146 lines
6.9 KiB
Python
Raw Normal View History

2015-01-25 14:20:07 +00:00
# coding:utf-8
import os
import ConfigParser
from datetime import datetime
2015-02-09 11:02:25 +00:00
from django.db.models import Q
from django.http import HttpResponse
2015-03-17 05:56:43 +00:00
from django.template import RequestContext
2015-01-25 14:20:07 +00:00
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
2014-12-26 13:57:10 +00:00
2015-01-25 14:20:07 +00:00
from connect import BASE_DIR
from jlog.models import Log
2015-02-09 11:02:25 +00:00
from jumpserver.views import pages
2015-03-19 10:32:10 +00:00
from juser.models import User, DEPT
from jumpserver.api import get_user_dept, is_super_user, is_group_admin, is_common_user
2015-01-25 14:20:07 +00:00
CONF = ConfigParser.ConfigParser()
CONF.read('%s/jumpserver.conf' % BASE_DIR)
2015-02-09 11:02:25 +00:00
def log_list_online(request):
2015-03-04 15:14:07 +00:00
header_title, path1, path2 = u'查看日志', u'查看日志', u'在线用户'
2015-03-12 03:03:53 +00:00
keyword = request.GET.get('keyword')
2015-02-09 11:02:25 +00:00
web_socket_host = CONF.get('websocket', 'web_socket_host')
2015-03-19 10:32:10 +00:00
dept_id = get_user_dept(request)
dept_name = DEPT.objects.get(id=dept_id).name
user_id = request.session.get('user_id')
username = User.objects.get(id=user_id).username
if is_super_user(request):
if keyword:
posts = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=0).order_by('-start_time')
else:
posts = Log.objects.filter(is_finished=0).order_by('-start_time')
2015-03-12 10:54:26 +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
elif is_group_admin(request):
if keyword:
posts = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=0).filter(dept_name=dept_name).order_by('-start_time')
else:
posts = Log.objects.filter(is_finished=0).filter(dept_name=dept_name).order_by('-start_time')
2015-03-12 10:54:26 +00:00
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
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:
posts = Log.objects.filter(user=username).filter(Q(user__contains=keyword) | Q(host__contains=keyword))\
.filter(is_finished=0).order_by('-start_time')
else:
posts = Log.objects.filter(is_finished=0).filter(user=username).order_by('-start_time')
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
2015-03-19 10:32:10 +00:00
2015-03-17 05:56:43 +00:00
return render_to_response('jlog/log_online.html', locals(), context_instance=RequestContext(request))
2015-02-09 11:02:25 +00:00
def log_list_offline(request):
2015-03-04 15:14:07 +00:00
header_title, path1, path2 = u'查看日志', u'查看日志', u'历史记录'
2015-03-12 03:03:53 +00:00
keyword = request.GET.get('keyword')
web_socket_host = CONF.get('websocket', 'web_socket_host')
2015-03-19 10:32:10 +00:00
dept_id = get_user_dept(request)
dept_name = DEPT.objects.get(id=dept_id).name
user_id = request.session.get('user_id')
username = User.objects.get(id=user_id).username
if is_super_user(request):
if keyword:
posts = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=1).order_by('-start_time')
else:
posts = Log.objects.filter(is_finished=1).order_by('-start_time')
2015-03-12 10:54:26 +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
elif is_group_admin(request):
if keyword:
posts = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=1).filter(dept_name=dept_name).order_by('-start_time')
else:
posts = Log.objects.filter(is_finished=1).filter(dept_name=dept_name).order_by('-start_time')
2015-03-12 10:54:26 +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
elif is_common_user(request):
2015-03-26 10:42:52 +00:00
if keyword:
posts = Log.objects.filter(user=username).filter(Q(user__contains=keyword) | Q(host__contains=keyword))\
.filter(is_finished=1).order_by('-start_time')
else:
posts = Log.objects.filter(is_finished=1).filter(user=username).order_by('-start_time')
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_offline.html', locals(), context_instance=RequestContext(request))
2015-01-25 14:20:07 +00:00
2015-02-09 11:02:25 +00:00
def log_kill(request, offset):
2015-01-25 14:20:07 +00:00
pid = offset
if pid:
os.kill(int(pid), 9)
Log.objects.filter(pid=pid).update(is_finished=1, end_time=datetime.now())
2015-03-17 05:56:43 +00:00
return HttpResponseRedirect('jlog/log_offline.html', locals(), context_instance=RequestContext(request))
2015-02-09 11:02:25 +00:00
def log_history(request):
if request.method == 'GET':
id = request.GET.get('id', 0)
log = Log.objects.get(id=int(id))
if log:
log_his = "%s.his" % log.log_path
if os.path.isfile(log_his):
f = open(log_his)
content = f.read()
return HttpResponse(content)
def log_search(request):
keyword = request.GET.get('keyword')
env = request.GET.get('env')
2015-03-19 10:32:10 +00:00
dept_id = get_user_dept(request)
dept_name = DEPT.objects.get(id=dept_id).name
2015-03-26 10:42:52 +00:00
user_id = request.session.get('user_id')
username = User.objects.get(id=user_id).username
2015-03-19 10:32:10 +00:00
if is_super_user(request):
if env == 'online':
posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=0).order_by('-start_time')
elif env == 'offline':
posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=1).order_by('-start_time')
2015-03-07 09:50:17 +00:00
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
2015-02-09 11:02:25 +00:00
2015-03-19 10:32:10 +00:00
elif is_group_admin(request):
if env == 'online':
posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=0).filter(dept_name=dept_name).order_by('-start_time')
elif env == 'offline':
posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=1).filter(dept_name=dept_name).order_by('-start_time')
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
2015-03-26 10:42:52 +00:00
elif is_common_user(request):
if env == 'online':
posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=0).filter(user=username).order_by('-start_time')
elif env == 'offline':
posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \
.filter(is_finished=1).filter(user=username).order_by('-start_time')
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))