管理员仪表盘基本完成

pull/6/head
halcyon 2015-04-05 22:52:37 +08:00
parent 17be0bb39e
commit 7de943c6e4
11 changed files with 328 additions and 113 deletions

View File

@ -13,9 +13,9 @@ urlpatterns = patterns('',
url(r'^idc_edit/$', edit_idc),
url(r'^idc_detail/$', detail_idc),
url(r'^idc_del/(\w+)/$', del_idc),
url(r'^jgroup_add/$', add_group),
url(r'^group_add/$', add_group),
url(r'^group_edit/$', edit_group),
url(r'^jgroup_list/$', list_group),
url(r'^group_list/$', list_group),
url(r'^group_detail/$', detail_group),
url(r'^group_del_host/(\w+)/$', group_del_host),
url(r'^group_del/(\w+)/$', group_del),

View File

@ -3,16 +3,13 @@
import ast
from django.db.models import Q
from django.http import Http404
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response
from models import IDC, Asset, BisGroup
from jasset.models import IDC, Asset, BisGroup
from juser.models import UserGroup, DEPT
from connect import PyCrypt, KEY
from jlog.models import Log
from jumpserver.views import jasset_host_edit, pages
from jperm.models import Perm
from jumpserver.views import pages
from jumpserver.api import *
cryptor = PyCrypt(KEY)
@ -55,6 +52,42 @@ def f_add_host(ip, port, idc, jtype, group, dept, active, comment, username='',
a.save()
def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment, j_user='', j_password=''):
groups, depts = [], []
is_active = {u'': '1', u'': '2'}
login_types = {'LDAP': 'L', 'MAP': 'M'}
for group in j_group[0].split():
c = BisGroup.objects.get(name=group.strip())
groups.append(c)
print j_dept
for d in j_dept[0].split():
p = DEPT.objects.get(name=d.strip())
depts.append(p)
j_type = login_types[j_type]
j_idc = IDC.objects.get(name=j_idc)
a = Asset.objects.get(id=j_id)
if j_type == 'M':
a.ip = j_ip
a.port = j_port
a.login_type = j_type
a.idc = j_idc
a.is_active = j_active
a.comment = j_comment
a.username = j_user
a.password = j_password
else:
a.ip = j_ip
a.port = j_port
a.idc = j_idc
a.login_type = j_type
a.is_active = is_active[j_active]
a.comment = j_comment
a.save()
a.bis_group = groups
a.dept = depts
a.save()
@require_admin
def add_host(request):
login_types = {'L': 'LDAP', 'M': 'MAP'}
@ -183,27 +216,45 @@ def list_host(request):
keyword = request.GET.get('keyword', '')
dept_id = get_user_dept(request)
dept = DEPT.objects.get(id=dept_id)
if is_super_user(request):
if keyword:
posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) |
Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).distinct().order_by('ip')
else:
posts = Asset.objects.all().order_by('ip')
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
elif is_group_admin(request):
if keyword:
posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) |
Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept).distinct().order_by('ip')
else:
posts = Asset.objects.all().filter(dept=dept).order_by('ip')
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
did = request.GET.get('did')
gid = request.GET.get('gid')
sid = request.GET.get('sid')
if did:
dept = DEPT.objects.get(id=did)
posts = dept.asset_set.all()
elif gid:
posts = []
user_group = UserGroup.objects.get(id=gid)
perms = Perm.objects.filter(user_group=user_group)
for perm in perms:
for post in perm.asset_group.asset_set.all():
posts.append(post)
posts = list(set(posts))
elif sid:
pass
else:
if is_super_user(request):
if keyword:
posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) |
Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).distinct().order_by('ip')
else:
posts = Asset.objects.all().order_by('ip')
elif is_group_admin(request):
if keyword:
posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) |
Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept).distinct().order_by('ip')
else:
posts = Asset.objects.all().filter(dept=dept).order_by('ip')
elif is_common_user(request):
user_id = request.session.get('user_id')
username = User.objects.get(id=user_id).name
posts = user_perm_asset_api(username)
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
elif is_common_user(request):
user_id = request.session.get('user_id')
username = User.objects.get(id=user_id).name
posts = user_perm_asset_api(username)
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
print posts, username
return render_to_response('jasset/host_list.html', locals(), context_instance=RequestContext(request))
@ -426,16 +477,24 @@ def list_group(request):
dept_id = get_user_dept(request)
dept = DEPT.objects.get(id=dept_id)
keyword = request.GET.get('keyword', '')
if is_super_user(request):
if keyword:
posts = BisGroup.objects.exclude(name='ALL').filter(Q(name__contains=keyword) | Q(comment__contains=keyword))
else:
posts = BisGroup.objects.exclude(name='ALL').order_by('id')
elif is_group_admin(request):
if keyword:
posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept)
else:
posts = BisGroup.objects.filter(dept=dept).order_by('id')
gid = request.GET.get('gid')
if gid:
posts = []
user_group = UserGroup.objects.get(id=gid)
perms = Perm.objects.filter(user_group=user_group)
for perm in perms:
posts.append(perm.asset_group)
else:
if is_super_user(request):
if keyword:
posts = BisGroup.objects.exclude(name='ALL').filter(Q(name__contains=keyword) | Q(comment__contains=keyword))
else:
posts = BisGroup.objects.exclude(name='ALL').order_by('id')
elif is_group_admin(request):
if keyword:
posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept)
else:
posts = BisGroup.objects.filter(dept=dept).order_by('id')
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
return render_to_response('jasset/group_list.html', locals(), context_instance=RequestContext(request))

View File

@ -9,7 +9,7 @@ database = jumpserver
[ldap]
ldap_enable = 1
host_url = ldap://127.0.0.1:389
host_url = ldap://192.168.173.129:389
base_dn = dc=jumpserver, dc=org
root_dn = cn=admin,dc=jumpserver,dc=org
root_pw = secret234

View File

@ -12,7 +12,7 @@ from ldap import modlist
import hashlib
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.http import HttpResponse, Http404
from juser.models import User, UserGroup
from juser.models import User, UserGroup, DEPT
from jasset.models import Asset, BisGroup
from jlog.models import Log
@ -345,4 +345,9 @@ def validate(request, user_group=None, user=None, asset_group=None, asset=None,
if not set(assets).issubset(eassets):
return False
return True
return True
def get_dept_asset(request):
dept_id = get_user_dept(request)
dept_asset = DEPT.objects.get(id=dept_id).asset_set.all()

View File

@ -53,6 +53,7 @@ INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'jumpserver',
'juser',
'jasset',

View File

@ -174,7 +174,12 @@ def group_type_to_str(type_name):
@register.filter(name='ast_to_list')
def ast_to_list(lis):
return ast.literal_eval(lis)[0:2]
ast_lis = ast.literal_eval(lis)
if len(ast_lis) <= 2:
return ','.join([i for i in ast_lis])
else:
restr = ','.join([i for i in ast_lis[0:2]]) + '...'
return restr
@register.filter(name='ast_to_list_1')
@ -191,6 +196,16 @@ def string_length(string, length):
return '%s ...' % string[0:length]
@register.filter(name='get_dic_user')
def get_dic_user(dic):
return dic.get('user')
@register.filter(name='get_dic_times')
def get_dic_times(dic):
return dic.get('times')
@register.filter(name='to_name')
def to_name(user_id):
try:

View File

@ -1,5 +1,7 @@
# coding: utf-8
from __future__ import division
import datetime
from django.db.models import Count
@ -7,6 +9,7 @@ from django.shortcuts import render_to_response
from django.template import RequestContext
from jasset.models import IDC
from juser.models import DEPT
from jperm.models import Apply
from jumpserver.api import *
@ -46,8 +49,18 @@ def get_data(data, items, option):
def index(request):
users = User.objects.all()
hosts = Asset.objects.all()
online_host = Log.objects.filter(is_finished=0)
online_user = online_host.distinct()
online = Log.objects.filter(is_finished=0)
online_host = online.values('host').distinct()
online_user = online.values('user').distinct()
active_users = User.objects.filter(is_active=1)
active_hosts = Asset.objects.filter(is_active=1)
# percent of dashboard
percent_user = format(active_users.count() / users.count(), '.0%')
percent_host = format(active_hosts.count() / hosts.count(), '.0%')
percent_online_user = format(online_user.count() / users.count(), '.0%')
percent_online_host = format(online_host.count() / hosts.count(), '.0%')
li_date, li_str = getDaysByNum(7)
today = datetime.datetime.now().day
from_week = datetime.datetime.now() - datetime.timedelta(days=7)
@ -56,6 +69,27 @@ def index(request):
host_top_ten = week_data.values('host').annotate(times=Count('host')).order_by('-times')[:10]
user_dic, host_dic = get_data(week_data, user_top_ten, 'user'), get_data(week_data, host_top_ten, 'host')
# a week data
week_users = week_data.values('user').distinct().count()
week_hosts = week_data.count()
user_top_five = week_data.values('user').annotate(times=Count('user')).order_by('-times')[:5]
color = ['label-success', 'label-info', 'label-primary', 'label-default', 'label-warnning']
# perm apply latest 10
perm_apply_10 = Apply.objects.order_by('-date_add')[:10]
# latest 10 login
login_10 = Log.objects.order_by('-start_time')[:10]
# a week top 10
# user_top_ten_more = []
for user_info in user_top_ten:
username = user_info.get('user')
last = Log.objects.filter(user=username).latest('start_time')
user_info['last'] = last
print user_top_ten
top = {'user': '活跃用户数', 'host': '活跃主机数', 'times': '登录次数'}
top_dic = {}
for key, value in top.items():
@ -75,51 +109,6 @@ def skin_config(request):
return render_to_response('skin_config.html')
def jasset_group_add(name, comment, jtype):
if BisGroup.objects.filter(name=name):
emg = u'该业务组已存在!'
else:
BisGroup.objects.create(name=name, comment=comment, type=jtype)
smg = u'业务组%s添加成功' % name
def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment, j_user='', j_password=''):
groups, depts = [], []
is_active = {u'': '1', u'': '2'}
login_types = {'LDAP': 'L', 'MAP': 'M'}
for group in j_group[0].split():
c = BisGroup.objects.get(name=group.strip())
groups.append(c)
print j_dept
for d in j_dept[0].split():
p = DEPT.objects.get(name=d.strip())
depts.append(p)
j_type = login_types[j_type]
j_idc = IDC.objects.get(name=j_idc)
a = Asset.objects.get(id=j_id)
if j_type == 'M':
a.ip = j_ip
a.port = j_port
a.login_type = j_type
a.idc = j_idc
a.is_active = j_active
a.comment = j_comment
a.username = j_user
a.password = j_password
else:
a.ip = j_ip
a.port = j_port
a.idc = j_idc
a.login_type = j_type
a.is_active = is_active[j_active]
a.comment = j_comment
a.save()
a.bis_group = groups
a.dept = depts
a.save()
def pages(posts, r):
"""分页公用函数"""
contact_list = posts

View File

@ -1,4 +1,6 @@
{% extends 'base.html' %}
{% load mytags %}
{% load humanize %}
{% block content %}
{% include 'nav_cat_bar.html' %}
@ -12,7 +14,7 @@
</div>
<div class="ibox-content">
<h1 class="no-margins"><a href="/juser/user_list/">{{ users.count}}</a></h1>
<div class="stat-percent font-bold text-success">98% <i class="fa fa-bolt"></i></div>
<div class="stat-percent font-bold text-success">{{ percent_user }} <i class="fa fa-bolt"></i></div>
<small>All user</small>
</div>
</div>
@ -25,7 +27,7 @@
</div>
<div class="ibox-content">
<h1 class="no-margins"><a href="/jasset/host_list/">{{ hosts.count }}</a></h1>
<div class="stat-percent font-bold text-info">20% <i class="fa fa-level-up"></i></div>
<div class="stat-percent font-bold text-info">{{ percent_host }} <i class="fa fa-level-up"></i></div>
<small>All host</small>
</div>
</div>
@ -39,7 +41,7 @@
</div>
<div class="ibox-content">
<h1 class="no-margins"><a href="/jlog/log_list/online/"> <span id="online_users"></span></a></h1>
<div class="stat-percent font-bold text-navy">44% <i class="fa fa-level-up"></i></div>
<div class="stat-percent font-bold text-navy">{{ percent_online_user }} <i class="fa fa-level-up"></i></div>
<small>Online user</small>
</div>
</div>
@ -53,28 +55,172 @@
</div>
<div class="ibox-content">
<h1 class="no-margins"><a href="/jlog/log_list/online/"> <span id="online_hosts"></span></a></h1>
<div class="stat-percent font-bold text-danger">38% <i class="fa fa-level-down"></i></div>
<div class="stat-percent font-bold text-danger">{{ percent_online_host }} <i class="fa fa-level-down"></i></div>
<small>Connected host</small>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6" id="top10" style="width:50%;height:400px;"></div>
<div class="col-lg-6" id="usertop10" style="width:50%;height:400px;"></div>
</div>
<div class="row">
<div class="col-lg-6" id="hosttop10" style="width:50%;height:400px; margin-top: 20px"></div>
</div>
</div>
<div class="row">
<div class="col-lg-3 border-bottom white-bg dashboard-header" style="margin-left:15px;">
<h2>活跃用户TOP5</h2>
<small>过去一周共有<span class="text-info">{{ week_users }}</span>位用户登录<span class="text-success">{{ week_hosts }}</span>次服务器.</small>
<ul class="list-group clear-list m-t">
{% for data in user_top_five %}
<li class="list-group-item fist-item">
<span class="pull-right">
{{ data|get_dic_times }}次/周
</span>
<span class="label {{ color|random }}">{{ forloop.counter }}</span> {{ data|get_dic_user }}
</li>
{% endfor %}
</ul>
</div>
<div class="col-lg-9" id="top10" style="margin-left: -15px;height: 345px"></div>
</div>
<br/>
<div class="row">
<div class="col-lg-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>权限申请</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
</div>
</div>
<div class="ibox-content ibox-heading">
<h3><i class="fa fa-envelope-o"></i> New messages</h3>
<small><i class="fa fa-tim"></i> You have 22 new messages and 16 waiting in draft folder.</small>
</div>
<div class="ibox-content">
<div class="feed-activity-list">
{% for perm in perm_apply_10 %}
<div class="feed-element">
<div>
{% ifequal perm.status 0 %}
<small class="pull-right text-navy">{{ perm.date_add|naturaltime }}</small>
{% else %}
<small class="pull-right">{{ perm.date_add|naturaltime }}</small>
{% endifequal %}
<strong>{{ perm.applyer }}</strong>
<div>申请 {{ perm.bisgroup|ast_to_list }} 主机组权限</div>
<div>申请 {{ perm.asset|ast_to_list }} 主机权限</div>
<small class="text-muted">{{ perm.date_add }}</small>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>最近十次登录</h5>
<div class="ibox-tools">
<span class="label label-warning-light">10 Messages</span>
</div>
</div>
<div class="ibox-content">
<div>
<div class="feed-activity-list">
{% for login in login_10 %}
<div class="feed-element">
<a href="profile.html" class="pull-left">
<img alt="image" class="img-circle" src="/static/img/root.png">
</a>
<div class="media-body ">
{% ifequal login.is_finished 0 %}
<small class="pull-right text-navy">{{ login.start_time|naturaltime }}</small>
{% else %}
<small class="pull-right">{{ login.start_time|naturaltime }}</small>
{% endifequal %}
<strong>{{ login.user }}</strong> 登录了{{ login.host }} <br>
<small class="text-muted">{{ login.start_time }}</small>
</div>
</div>
{% endfor %}
</div>
<button class="btn btn-primary btn-block m-t"><i class="fa fa-arrow-down"></i> Show More</button>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>一周Top10用户</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-wrench"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li><a href="#">Config option 1</a>
</li>
<li><a href="#">Config option 2</a>
</li>
</ul>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
</div>
</div>
<div class="ibox-content ibox-heading">
<h3>You have meeting today!</h3>
<small><i class="fa fa-map-marker"></i> Meeting is on 6:00am. Check your schedule to see detail.</small>
</div>
<div class="ibox-content inspinia-timeline">
{% for data in user_top_ten %}
<div class="timeline-item">
<div class="row">
<div class="col-xs-5 date">
<i class="fa fa-info-circle"></i>
<strong>{{ data|get_dic_user }}</strong>
<br/>
<small class="text-navy">{{ data|get_dic_times }}次</small>
</div>
<div class="col-xs-7 content no-top-border">
<p class="m-b-xs">最近一次登录</p>
<p>{{ data.last.host }}</p>
<p>于{{ data.last.start_time |date:"Y-m-d H:i:s" }}</p>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
<!--</div>-->
<!--<div class="col-xm-6" id="top10" style="width:50%;height:400px;"></div>-->
<!--<div class="col-xm-6" id="usertop10" style="width:50%;height:400px;"></div>-->
<!--<div class="row">-->
<!--<div class="col-lg-6" id="hosttop10" style="width:50%;height:400px; margin-top: 20px"></div>-->
<!--</div>-->
</div>
<script>
var cate = {{ li_str|safe }};
$(function () {
$('#top10').highcharts({
chart: {
type: 'column'
},
// chart: {
// type: 'column'
// },
title: {
text: '一周数据总览',
x: -20 //center
@ -94,7 +240,7 @@ $(function () {
yAxis:{
min: 0,
title: {
text: '登录次数'
text: ''
},
plotLines: [{
value: 0,

View File

@ -58,8 +58,8 @@
{% for dept in contacts.object_list %}
<tr class="gradeX">
<td class="text-center"> {{ dept.name }} </td>
<td class="text-center"><a href="/juser/user_list/?did={{ dept.id }}">{{ dept.id | dept_user_num }} </a> </td>
<td class="text-center"> {{ dept.id | dept_asset_num }} </td>
<td class="text-center"><a href="/juser/user_list/?did={{ dept.id }}">{{ dept.id | dept_user_num }}</a> </td>
<td class="text-center"><a href="/jasset/host_list/?did={{ dept.id }}">{{ dept.id | dept_asset_num }}</a> </td>
<td class="text-center"> {{ dept.comment }} </td>
<td class="text-center">
{# <a title="[ {{ dept.name }} ] 成员信息" href="../dept_detail/?id={{ dept.id }}" class="iframe btn btn-xs btn-primary">主机</a>#}

View File

@ -60,9 +60,9 @@
<tr class="gradeX">
<td class="text-center"> {{ group.name }} </td>
<td class="text-center"> {{ group.dept.name }} </td>
<td class="text-center"><a href="/juser/user_list/?gid={{ group.id }}">{{ group.id | member_count }} </a> </td>
<td class="text-center"> {{ group.id | ugrp_perm_agrp_count }} </td>
<td class="text-center"> {{ group.id | ugrp_perm_asset_count }} </td>
<td class="text-center"> <a href="/juser/user_list/?gid={{ group.id }}">{{ group.id | member_count }} </a> </td>
<td class="text-center"> <a href="/jasset/group_list/?gid={{ group.id }}"> {{ group.id | ugrp_perm_agrp_count }} </a> </td>
<td class="text-center"> <a href="/jasset/host_list/?gid={{ group.id }}">{{ group.id | ugrp_perm_asset_count }} </a> </td>
<td class="text-center"> {{ group.comment }} </td>
<td class="text-center">
<a href="../perm_edit/?id={{ group.id }}" class="btn btn-xs btn-danger">授权编辑</a>

View File

@ -28,8 +28,8 @@
<ul class="nav nav-second-level">
<li class="host_add host_add_multi"><a href="/jasset/host_add/">添加资产</a></li>
<li class="host_list"><a href="/jasset/host_list/">查看资产&nbsp&nbsp</span><span class="label label-info pull-right">{{ host_active_num }}/{{ host_total_num}}</span></a></li>
<li class="jgroup_add"><a href="/jasset/jgroup_add/">添加主机组</a></li>
<li class="jgroup_list group_detail"><a href="/jasset/jgroup_list/">查看主机组</a></li>
<li class="group_add"><a href="/jasset/group_add/">添加主机组</a></li>
<li class="group_list group_detail"><a href="/jasset/group_list/">查看主机组</a></li>
<li class="idc_add"><a href="/jasset/idc_add/">添加IDC</a></li>
<li class="idc_list idc_detail"><a href="/jasset/idc_list/">查看IDC</a></li>
</ul>
@ -98,8 +98,8 @@
<ul class="nav nav-second-level">
<li class="host_add host_add_multi"><a href="/jasset/host_add/">添加资产</a></li>
<li class="host_list"><a href="/jasset/host_list/">查看资产&nbsp&nbsp</span><span class="label label-info pull-right">{{ host_active_num }}/{{ host_total_num}}</span></a></li>
<li class="jgroup_add"><a href="/jasset/jgroup_add/">添加主机组</a></li>
<li class="jgroup_list group_detail"><a href="/jasset/jgroup_list/">查看主机组</a></li>
<li class="group_add"><a href="/jasset/group_add/">添加主机组</a></li>
<li class="group_list group_detail"><a href="/jasset/group_list/">查看主机组</a></li>
<li class="idc_list idc_detail"><a href="/jasset/idc_list/">查看IDC</a></li>
</ul>
</li>