Skip to content

Commit

Permalink
add request total for dashboard and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
v55448330 committed Aug 10, 2016
1 parent 3ed9b61 commit e971801
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 204 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* 新增自定义管理员用户
* 新增配置通过文件备份和还原
* 新增实时查看访问日志和错误日志
* 新增实时请求统计
* 更新 Vagrantfile
* 修复其他 Bug

Expand Down
31 changes: 25 additions & 6 deletions dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,36 @@

@login_required(login_url="/login/")
def view(request):
_sysinfo = get_sysinfo()
_user = {
sysinfo = get_sys_info()
user = {
'name':request.user,
'date':time.time()
}
return render_to_response('dashboard/view.html',{'sysinfo' : _sysinfo, 'user' : _user})
return render_to_response('dashboard/view.html',{'sysinfo' : sysinfo, 'user' : user})

@is_auth
def get_status_info(request):
content = {
req_status = get_req_status()
r_stat = []
for r in req_status:
rs = {
'req_url' : r[0],
'bytes_in' : r[2],
'bytes_out' : r[3],
'conn_total' : r[4],
'req_total' : r[5],
'http_2xx' : r[6],
'http_3xx' : r[7],
'http_4xx' : r[8],
'http_5xx' : r[9]
}
r_stat.append(rs)
print r_stat
context = {
'flag':"Success",
'content':get_statusinfo()
'context':{
"sysstatus" : get_sys_status(),
"reqstatus" : r_stat
}
}
return HttpResponse(json.dumps(content))
return HttpResponse(json.dumps(context))
2 changes: 1 addition & 1 deletion lazy_balancer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create_superuser(request):
User.objects.create_superuser(post['username'],'[email protected]',post['password'])
context = {'flag':"Success",}
except Exception,e:
content = { "flag":"Error","content":str(e) }
context = { "flag":"Error","context":str(e) }

return HttpResponse(json.dumps(context))

Expand Down
27 changes: 14 additions & 13 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def view(request):
'name':request.user,
'date':time.time()
}
print m_config

return render_to_response('main/view.html',{ 'main_config' : m_config, 'user' : user })
pass

@is_auth
def save(request):
content = {
context = {
'flag':"",
'content':""
'context':""
}
try:
post = json.loads(request.body)
Expand Down Expand Up @@ -67,27 +68,27 @@ def save(request):
'update_time' : time.time()
}
print m_config
config_content = build_main_config(m_config)
write_config(config_path,config_content)
config_context = build_main_config(m_config)
write_config(config_path,config_context)

test_ret = test_config()
if test_ret['status'] == 0:
write_config(config_path,config_content)
write_config(config_path,config_context)
main_config.objects.all().delete()
main_config.objects.create(**m_config)
content['flag'] = "Success"
context['flag'] = "Success"
else:
content['error'] = "Error"
content['content'] = test_ret['output']
context['error'] = "Error"
context['context'] = test_ret['output']

reload_config()
else:
content['flag'] = "Error"
content['content'] = "ArgsError"
context['flag'] = "Error"
context['context'] = "ArgsError"
except Exception, e:
content['flag'] = "Error"
content['content'] = str(e)
context['flag'] = "Error"
context['context'] = str(e)

return HttpResponse(json.dumps(content))
return HttpResponse(json.dumps(context))
pass
#
2 changes: 1 addition & 1 deletion nginx/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def set_firewall():
public_port = list(set(public_port))

address = ""
nics = get_sysinfo()['nic']
nics = get_sys_info()['nic']

for nic in nics:
if nic['nic'] == internal_nic:
Expand Down
26 changes: 16 additions & 10 deletions nginx/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from jinja2 import Environment, FileSystemLoader
from subprocess import check_output, CalledProcessError
from multiprocessing import cpu_count
from django.conf import settings
from proxy.models import proxy_config,upstream_config
from main.models import main_config
Expand Down Expand Up @@ -33,9 +32,6 @@ def load_template(template):
def build_main_config(config):
template = load_template('nginx.template')

if config['worker_processes'] == 0:
config['worker_processes'] = cpu_count()

return template.render(config)

def build_proxy_config(config):
Expand All @@ -48,18 +44,18 @@ def build_default_config(config):

return template.render(config)

def write_config(conf_path,conf_content):
def write_config(conf_path,conf_context):
f = open(conf_path, 'w')
f.write(conf_content)
f.write(conf_context)
f.close()

def run_shell(cmd):
(status,output) = commands.getstatusoutput(cmd)
content = {
context = {
'status':status,
'output':output,
}
return content
return context

def test_config():
return run_shell('nginx -t')
Expand Down Expand Up @@ -92,7 +88,7 @@ def reload_config():

return run_shell('nginx -s reload')

def get_statusinfo():
def get_sys_status():
phymem = psutil.virtual_memory()
disk = psutil.disk_usage('/')
conns = psutil.net_connections()
Expand Down Expand Up @@ -151,7 +147,7 @@ def get_statusinfo():
return statusinfo


def get_sysinfo():
def get_sys_info():
disk_info = psutil.disk_usage('/')
nic_info = []
for nic,addrs in psutil.net_if_addrs().items():
Expand Down Expand Up @@ -187,3 +183,13 @@ def get_proxy_http_status():
url = "http://127.0.0.1/up_status?format=json"
ret = json.loads(post_request(url))
return ret

def get_req_status():
url = "http://127.0.0.1/req_status"
req_status = post_request(url)
ret = []
for req in req_status.split('\n'):
r = req.split(',')
if r[0] != "":
ret.append(r)
return ret
11 changes: 10 additions & 1 deletion resource/nginx/nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
# Config ID : {{ config_id }}
##
user www-data;
{%- if worker_processes == 0 %}
worker_processes auto;
{%- else %}
worker_processes {{ worker_processes }};
{%- endif %}
pid /run/nginx.pid;
daemon off;

events {
use epoll;
multi_accept off;
# reuse_port on;
worker_connections {{ worker_connections }};
# multi_accept on;
}
Expand All @@ -20,6 +27,8 @@ http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;

keepalive_timeout {{ keepalive_timeout }};
types_hash_max_size 2048;
client_max_body_size {{ client_max_body_size }}m;
Expand Down Expand Up @@ -48,7 +57,7 @@ http {
# Status Settings
##

req_status_zone server "$host,$server_addr:$server_port" 10M;
req_status_zone server "$host,$server_addr:$server_port" 32M;
#req_status_zone_add_indicator server $limit;

server {
Expand Down
2 changes: 1 addition & 1 deletion settings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def view(request):
'date':time.time()
}
nic = {
'nics':get_sysinfo()['nic'],
'nics':get_sys_info()['nic'],
'internal_nic':''
}

Expand Down
Loading

0 comments on commit e971801

Please sign in to comment.