diff --git a/docs/AddUserAsset.py b/docs/AddUserAsset.py index 9753e49f6..1fc4ade1d 100644 --- a/docs/AddUserAsset.py +++ b/docs/AddUserAsset.py @@ -2,6 +2,8 @@ import django import os import sys +import random +import datetime sys.path.append('../') os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings' @@ -12,6 +14,7 @@ from jasset.models import Asset, IDC, BisGroup from juser.models import UserGroup from jasset.views import jasset_group_add from jperm.models import CmdGroup +from jlog.models import Log def test_add_idc(): @@ -72,6 +75,25 @@ def test_add_asset(): print "Add: %s" % ip +def test_add_log(): + li_date = [] + today = datetime.date.today() + oneday = datetime.timedelta(days=1) + for i in range(0, 7): + today = today-oneday + li_date.append(today) + user_list = ['马云', '马化腾', '丁磊', '周鸿祎', '雷军', '柳传志', '陈天桥', '李彦宏', '李开复', '罗永浩'] + for i in range(1, 1000): + user = random.choice(user_list) + ip = random.randint(1, 20) + start_time = random.choice(li_date) + end_time = datetime.datetime.now() + log_path = '/var/log/jumpserver/test.log' + host = '192.168.1.' + str(ip) + Log.objects.create(user=user, host=host, log_path=log_path, pid=168, start_time=start_time, + is_finished=1, log_finished=1, end_time=end_time) + + if __name__ == '__main__': test_add_idc() test_add_asset_group() @@ -79,6 +101,7 @@ if __name__ == '__main__': test_add_cmd_group() test_add_asset() test_add_user() + test_add_log() diff --git a/jasset/views.py b/jasset/views.py index c5edff68f..3ffb97ecd 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -50,7 +50,7 @@ def f_add_host(ip, port, idc, jtype, group, active, comment, username='', passwo def add_host(request): login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} - header_title, path1, path2 = u'添加主机 | Add Host', u'资产管理', u'添加主机' + header_title, path1, path2 = u'添加主机', u'资产管理', u'添加主机' eidc = IDC.objects.all() egroup = BisGroup.objects.filter(type='A') eusergroup = UserGroup.objects.all() @@ -80,7 +80,7 @@ def add_host(request): def add_host_multi(request): - header_title, path1, path2 = u'批量添加主机 | Add Hosts', u'资产管理', u'批量添加主机' + header_title, path1, path2 = u'批量添加主机', u'资产管理', u'批量添加主机' login_types = {'LDAP': 'L', 'SSH_KEY': 'S', 'PASSWORD': 'P', 'MAP': 'M'} j_group = [] if request.method == 'POST': @@ -141,7 +141,7 @@ def batch_host_edit(request): def list_host(request): - header_title, path1, path2 = u'查看主机 | List Host', u'资产管理', u'查看主机' + header_title, path1, path2 = u'查看主机', u'资产管理', u'查看主机' login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} posts = contact_list = Asset.objects.all().order_by('ip') p = paginator = Paginator(contact_list, 10) @@ -181,7 +181,7 @@ def host_del(request, offset): def host_edit(request): actives = {1: u'激活', 0: u'禁用'} login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} - header_title, path1, path2 = u'修改主机 | Edit Host', u'资产管理', u'修改主机' + header_title, path1, path2 = u'修改主机', u'资产管理', u'修改主机' groups, e_group = [], [] eidc = IDC.objects.all() egroup = BisGroup.objects.filter(type='A') @@ -232,7 +232,7 @@ def host_edit(request): def jlist_ip(request, offset): - header_title, path1, path2 = u'主机详细信息 | Host Detail.', u'资产管理', u'主机详情' + header_title, path1, path2 = u'主机详细信息', u'资产管理', u'主机详情' login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} post = contact_list = Asset.objects.get(ip=str(offset)) log = Log.objects.filter(host=str(offset)) @@ -240,7 +240,7 @@ def jlist_ip(request, offset): def add_idc(request): - header_title, path1, path2 = u'添加IDC | Add IDC', u'资产管理', u'添加IDC' + header_title, path1, path2 = u'添加IDC', u'资产管理', u'添加IDC' if request.method == 'POST': j_idc = request.POST.get('j_idc') j_comment = request.POST.get('j_comment') @@ -255,7 +255,7 @@ def add_idc(request): def list_idc(request): - header_title, path1, path2 = u'查看IDC | List IDC', u'资产管理', u'查看IDC' + header_title, path1, path2 = u'查看IDC', u'资产管理', u'查看IDC' posts = IDC.objects.all().order_by('id') return render_to_response('jasset/idc_list.html', locals(), context_instance=RequestContext(request)) @@ -266,7 +266,7 @@ def del_idc(request, offset): def add_group(request): - header_title, path1, path2 = u'添加主机组 | Add Group', u'资产管理', u'添加主机组' + header_title, path1, path2 = u'添加主机组', u'资产管理', u'添加主机组' posts = Asset.objects.all() if request.method == 'POST': j_group = request.POST.get('j_group') @@ -288,13 +288,13 @@ def add_group(request): def list_group(request): - header_title, path1, path2 = u'查看主机组 | List Group', u'资产管理', u'查看主机组' + header_title, path1, path2 = u'查看主机组', u'资产管理', u'查看主机组' posts = BisGroup.objects.filter(type='A').order_by('id') return render_to_response('jasset/group_list.html', locals(), context_instance=RequestContext(request)) def edit_group(request): - header_title, path1, path2 = u'编辑主机组 | Edit Group', u'资产管理', u'编辑主机组' + header_title, path1, path2 = u'编辑主机组', u'资产管理', u'编辑主机组' group_id = request.GET.get('id') group = BisGroup.objects.get(id=group_id) all = Asset.objects.all() @@ -317,7 +317,7 @@ def edit_group(request): def detail_group(request): - header_title, path1, path2 = u'主机组详情 | Group Detail', u'资产管理', u'主机组详情' + header_title, path1, path2 = u'主机组详情', u'资产管理', u'主机组详情' login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} offset = request.GET.get('id') group_name = BisGroup.objects.get(id=offset).name @@ -337,7 +337,7 @@ def detail_group(request): def detail_idc(request): - header_title, path1, path2 = u'主机组详情 | Group Detail', u'资产管理', u'主机组详情' + header_title, path1, path2 = u'主机组详情', u'资产管理', u'主机组详情' login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} offset = request.GET.get('id') idc_name = IDC.objects.get(id=offset).name diff --git a/jlog/views.py b/jlog/views.py index 243422c99..9ffff9be5 100644 --- a/jlog/views.py +++ b/jlog/views.py @@ -17,7 +17,7 @@ CONF.read('%s/jumpserver.conf' % BASE_DIR) def log_list_online(request): - header_title, path1, path2 = u'查看日志 | Log List.', u'查看日志', u'在线用户' + header_title, path1, path2 = u'查看日志', u'查看日志', u'在线用户' web_socket_host = CONF.get('websocket', 'web_socket_host') posts = Log.objects.filter(is_finished=0).order_by('-start_time') contact_list, p, contacts = pages(posts, request) @@ -26,7 +26,7 @@ def log_list_online(request): def log_list_offline(request): - header_title, path1, path2 = u'查看日志 | Log List.', u'查看日志', u'历史记录' + header_title, path1, path2 = u'查看日志', u'查看日志', u'历史记录' web_socket_host = CONF.get('websocket', 'web_socket_host') posts = Log.objects.filter(is_finished=1).order_by('-start_time') contact_list, p, contacts = pages(posts, request) diff --git a/jumpserver/views.py b/jumpserver/views.py index 3d135e3c8..3f1657e56 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -58,21 +58,33 @@ def base(request): def index(request): path1, path2 = u'仪表盘', 'Dashboard' - dic = {} + user_dic, host_dic = {}, {} li_date, li_str = getDaysByNum(7) today = datetime.datetime.now().day from_week = datetime.datetime.now() - datetime.timedelta(days=7) week_data = Log.objects.filter(start_time__range=[from_week, datetime.datetime.now()]) - top_ten = week_data.values('user').annotate(times=Count('user')).order_by('-times')[:10] - for user in top_ten: + user_top_ten = week_data.values('user').annotate(times=Count('user')).order_by('-times')[:10] + host_top_ten = week_data.values('host').annotate(times=Count('host')).order_by('-times')[:10] + for user in user_top_ten: username = user['user'] - li = [] + li_user = [] user_data = week_data.filter(user=username) for t in li_date: year, month, day = t.year, t.month, t.day times = user_data.filter(start_time__year=year, start_time__month=month, start_time__day=day).count() - li.append(times) - dic[username] = li + li_user.append(times) + user_dic[username] = li_user + + for host in host_top_ten: + ip = host['host'] + li_host = [] + host_data = week_data.filter(host=ip) + for t in li_date: + year, month, day = t.year, t.month, t.day + times = host_data.filter(start_time__year=year, start_time__month=month, start_time__day=day).count() + li_host.append(times) + host_dic[ip] = li_host + users = User.objects.all() hosts = Asset.objects.all() online_host = Log.objects.filter(is_finished=0) diff --git a/templates/index.html b/templates/index.html index a8112c2ab..086bc55e4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -58,16 +58,17 @@ -
+ + - +