Skip to content

Commit

Permalink
完善工单日志
Browse files Browse the repository at this point in the history
新增binlog2sql模块,在线解析binlog
sql_tuning展示信息优化
慢日志明细列说明调整
除上线和查询区分主从实例外,其他均展示所有实例
结构调整和其他细节优化
  • Loading branch information
hhyo authored and lihuanhuan committed Sep 7, 2018
1 parent a2bf3c6 commit bb9abfe
Show file tree
Hide file tree
Showing 64 changed files with 2,945 additions and 1,708 deletions.
279 changes: 49 additions & 230 deletions README.md

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion archer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'sql.utils.global_info.global_info',
'common.utils.global_info.global_info',
],
},
},
Expand Down Expand Up @@ -118,6 +118,10 @@
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8'
},
'TEST': {
'NAME': 'test_archer',
'CHARSET': 'utf8',
Expand Down
133 changes: 133 additions & 0 deletions common/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import datetime

import simplejson as json
from django.conf import settings
from django.contrib.auth import authenticate, login
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import Group
from django.http import HttpResponse
from django.shortcuts import render

from common.config import SysConfig
from sql.models import Users
from sql.views import logger
from sql.sql_workflow import login_failure_counter, logger


def loginAuthenticate(username, password):
"""登录认证,包含一个登录失败计数器,5分钟内连续失败5次的账号,会被锁定5分钟"""
sys_config = SysConfig().sys_config
if sys_config.get('lock_cnt_threshold'):
lockCntThreshold = int(sys_config.get('lock_cnt_threshold'))
else:
lockCntThreshold = 5
if sys_config.get('lock_time_threshold'):
lockTimeThreshold = int(sys_config.get('lock_time_threshold'))
else:
lockTimeThreshold = 300

# 服务端二次验证参数
if username == "" or password == "" or username is None or password is None:
result = {'status': 2, 'msg': '登录用户名或密码为空,请重新输入!', 'data': ''}
elif username in login_failure_counter and login_failure_counter[username]["cnt"] >= lockCntThreshold and (
datetime.datetime.now() - login_failure_counter[username][
"last_failure_time"]).seconds <= lockTimeThreshold:
result = {'status': 3, 'msg': '登录失败超过5次,该账号已被锁定5分钟!', 'data': ''}
else:
# 登录
user = authenticate(username=username, password=password)
# 登录成功
if user:
# 如果登录失败计数器中存在该用户名,则清除之
if username in login_failure_counter:
login_failure_counter.pop(username)
result = {'status': 0, 'msg': 'ok', 'data': user}
# 登录失败
else:
if username not in login_failure_counter:
# 第一次登录失败,登录失败计数器中不存在该用户,则创建一个该用户的计数器
login_failure_counter[username] = {"cnt": 1, "last_failure_time": datetime.datetime.now()}
else:
if (datetime.datetime.now() - login_failure_counter[username][
"last_failure_time"]).seconds <= lockTimeThreshold:
login_failure_counter[username]["cnt"] += 1
else:
# 上一次登录失败时间早于5分钟前,则重新计数。以达到超过5分钟自动解锁的目的。
login_failure_counter[username]["cnt"] = 1
login_failure_counter[username]["last_failure_time"] = datetime.datetime.now()
result = {'status': 1, 'msg': '用户名或密码错误,请重新输入!', 'data': ''}
return result


# ajax接口,登录页面调用,用来验证用户名密码
def authenticateEntry(request):
"""接收http请求,然后把请求中的用户名密码传给loginAuthenticate去验证"""
username = request.POST.get('username')
password = request.POST.get('password')

result = loginAuthenticate(username, password)
if result['status'] == 0:
# 开启LDAP的认证通过后更新用户密码
if settings.ENABLE_LDAP:
try:
Users.objects.get(username=username)
except Exception:
insert_info = Users()
insert_info.password = make_password(password)
insert_info.save()
else:
replace_info = Users.objects.get(username=username)
replace_info.password = make_password(password)
replace_info.save()
# 添加到默认组
default_auth_group = SysConfig().sys_config.get('default_auth_group', '')
try:
user = Users.objects.get(username=username)
group = Group.objects.get(name=default_auth_group)
user.groups.add(group)
except Exception:
logger.error('无name={}的权限组,无法默认添加'.format(default_auth_group))

# 调用了django内置登录方法,防止管理后台二次登录
user = authenticate(username=username, password=password)
if user:
login(request, user)

result = {'status': 0, 'msg': 'ok', 'data': None}

return HttpResponse(json.dumps(result), content_type='application/json')


# 注册用户
def sign_up(request):
username = request.POST.get('username')
password = request.POST.get('password')
password2 = request.POST.get('password2')
display = request.POST.get('display')
email = request.POST.get('email')

if username is None or password is None:
context = {'errMsg': '用户名和密码不能为空'}
return render(request, 'error.html', context)
if len(Users.objects.filter(username=username)) > 0:
context = {'errMsg': '用户名已存在'}
return render(request, 'error.html', context)
if password != password2:
context = {'errMsg': '两次输入密码不一致'}
return render(request, 'error.html', context)

# 添加用户并且添加到默认组
Users.objects.create_user(username=username,
password=password,
display=display,
email=email,
is_active=1,
is_staff=1)
default_auth_group = SysConfig().sys_config.get('default_auth_group', '')
try:
user = Users.objects.get(username=username)
group = Group.objects.get(name=default_auth_group)
user.groups.add(group)
except Exception:
logger.error('无name={}的权限组,无法默认添加'.format(default_auth_group))
return render(request, 'login.html')
47 changes: 47 additions & 0 deletions common/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: UTF-8 -*-
import simplejson as json
from django.http import HttpResponse

from common.utils.permission import superuser_required
from sql.models import Config
from django.db import connection, transaction


class SysConfig(object):
def __init__(self):
try:
# 获取系统配置信息
all_config = Config.objects.all().values('item', 'value')
sys_config = {}
try:
for items in all_config:
sys_config[items['item']] = items['value'].strip()
except Exception:
# 关闭后重新获取连接,防止超时
connection.close()
for items in all_config:
sys_config[items['item']] = items['value'].strip()
except Exception:
self.sys_config = {}
else:
self.sys_config = sys_config


# 修改系统配置
@superuser_required
def changeconfig(request):
configs = request.POST.get('configs')
result = {'status': 0, 'msg': 'ok', 'data': []}

# 清空并替换
try:
with transaction.atomic():
Config.objects.all().delete()
Config.objects.bulk_create(
[Config(item=items['key'], value=items['value']) for items in json.loads(configs)])
except Exception as e:
result['status'] = 1
result['msg'] = str(e)

# 返回结果
return HttpResponse(json.dumps(result), content_type='application/json')
4 changes: 2 additions & 2 deletions sql/charts.py → common/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pyecharts import Pie, Bar, Line
from pyecharts import Page
from sql.utils.inception import InceptionDao
from sql.utils.chart_dao import ChartDao
from common.utils.chart_dao import ChartDao
from datetime import date
from dateutil.relativedelta import relativedelta

Expand Down Expand Up @@ -101,4 +101,4 @@ def pyecharts(request):
myechart = page.render_embed() # 渲染配置
host = 'https://pyecharts.github.io/assets/js' # js文件源地址
script_list = page.get_js_dependencies() # 获取依赖的js文件名称(只获取当前视图需要的js)
return render(request, "charts.html", {"myechart": myechart, "host": host, "script_list": script_list})
return render(request, "dashboard.html", {"myechart": myechart, "host": host, "script_list": script_list})
35 changes: 26 additions & 9 deletions common/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<ul class="nav" id="side-menu">
{% if perms.sql.menu_dashboard %}
<li>
<a href="/charts/"><i class="fa fa-dashboard fa-fw"></i> Dashboard</a>
<a href="/dashboard/"><i class="fa fa-dashboard fa-fw"></i> Dashboard</a>
</li>
{% endif %}
{% if perms.sql.menu_sqlworkflow %}
Expand Down Expand Up @@ -115,11 +115,6 @@
<a href="/slowquery/">慢查日志</a>
</li>
{% endif %}
{% if perms.sql.menu_dbdiagnostic %}
<li>
<a href="/dbdiagnostic/">会话管理</a>
</li>
{% endif %}
</ul>
<!-- /.nav-second-level -->
</li>
Expand Down Expand Up @@ -153,16 +148,36 @@
<!-- /.nav-second-level -->
</li>
{% endif %}
{% if perms.sql.menu_instance %}
<li>
<a href="#"><i class="fa fa-database fa-fw"></i> 实例管理<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
{% if perms.sql.menu_instance %}
<li>
<a href="/instance/">实例列表</a>
</li>
{% endif %}
{% if perms.sql.menu_dbdiagnostic %}
<li>
<a href="/dbdiagnostic/">会话管理</a>
</li>
{% endif %}
{% if perms.sql.menu_binlog2sql %}
<li>
<a href="/binlog2sql/">Binlog2SQL</a>
</li>
{% endif %}
</ul>
<!-- /.nav-second-level -->
</li>
{% endif %}
{% if perms.sql.menu_system %}
<li>
<a href="#"><i class="fa fa-cogs fa-fw"></i> 系统管理<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="/config/">配置项管理</a>
</li>
<li>
<a href="/instance/">实例管理</a>
</li>
<li>
<a href="/group/">资源组管理</a>
</li>
Expand Down Expand Up @@ -210,6 +225,8 @@
<br>
{% block content %}
{% endblock content %}
<!--底部部分 -->
<hr>
</div>
</div>
<!-- /#page-wrapper -->
Expand Down
Loading

0 comments on commit bb9abfe

Please sign in to comment.