Commit 696d4d85 by tarokkk

webui: Added base statistics

parent 503aeb70
...@@ -84,4 +84,5 @@ urlpatterns = patterns('', ...@@ -84,4 +84,5 @@ urlpatterns = patterns('',
'school.views.group_ajax_remove_member', ), 'school.views.group_ajax_remove_member', ),
url(r'^ajax/group/delete/$', 'school.views.group_ajax_delete', ), url(r'^ajax/group/delete/$', 'school.views.group_ajax_delete', ),
url(r'^ajax/group/autocomplete/$', 'school.views.group_ajax_owner_autocomplete', ), url(r'^ajax/group/autocomplete/$', 'school.views.group_ajax_owner_autocomplete', ),
url(r'stat', 'one.views.stat'),
) )
#!/usr/bin/python
import xmltodict
import sys
import json
xml = sys.stdin.read()
data = xmltodict.parse(xml)
#CPU stat
cpu_usage = 0
used_cpu = 0
cpu_max = 0
#Memory stat
mem_usage = 0
used_mem = 0
mem_max = 0
#Running VMs
running_vms = 0
print "\n" .join(data["HOST_POOL"]["HOST"])
for host in data["HOST_POOL"]:
cpu_max += int(host["HOST_SHARE"]["MAX_CPU"])
used_cpu += int(host["HOST_SHARE"]["USED_CPU"])
cpu_usage += int(host["HOST_SHARE"]["CPU_USAGE"])
mem_usage += int(host["HOST_SHARE"]["MEM_USAGE"])
used_mem += int(host["HOST_SHARE"]["USED_MEM"])
mem_max += int(host["HOST_SHARE"]["MAX_MEM"])
running_vms += int(host["HOST_SHARE"]["RUNNING_VMS"])
free_cpu = cpu_max - cpu_usage - used_cpu
alloc_cpu = cpu_usage - used_cpu
free_mem = mem_max - mem_usage - used_mem
alloc_mem = mem_usage - used_mem
cpu_dict = {'FREE_CPU' : free_cpu, 'ALLOC_CPU' : alloc_cpu , 'USED_CPU' :
used_cpu}
mem_dict = {'FREE_MEM' : free_mem, 'ALLOC_MEM' : alloc_mem , 'USED_MEM' :
used_mem}
print json.dumps(cpu_dict)
print json.dumps(mem_dict)
print "Running VMs: " + str(running_vms)
print "Global MAX CPU: " + str(cpu_max)
print "Allocated CPU: " + str(cpu_usage)
print "CPU under load: " + str(used_cpu)
print "CPU diff alloc-load: " + str(cpu_usage-used_cpu)
#!/usr/bin/python
import xmltodict
import xml.dom.minidom as minidom
import sys
import json
import math
xml = sys.stdin.read()
data = minidom.parseString(xml)
node = data.documentElement
hosts = data.getElementsByTagName("HOST")
#CPU stat
cpu_usage = 0
used_cpu = 0
cpu_max = 0
#Memory stat
mem_usage = 0
used_mem = 0
mem_max = 0
#Running VMs
running_vms = 0
for host in hosts:
share = host.getElementsByTagName("HOST_SHARE")[0]
cpu_max += int(share.getElementsByTagName("MAX_CPU")[0].childNodes[0].data)
used_cpu += int(share.getElementsByTagName("USED_CPU")[0].childNodes[0].data)
cpu_usage += int(share.getElementsByTagName("CPU_USAGE")[0].childNodes[0].data)
mem_usage += int(share.getElementsByTagName("MEM_USAGE")[0].childNodes[0].data)
used_mem += int(share.getElementsByTagName("USED_MEM")[0].childNodes[0].data)
mem_max += int(share.getElementsByTagName("MAX_MEM")[0].childNodes[0].data)
running_vms += int(share.getElementsByTagName("RUNNING_VMS")[0].childNodes[0].data)
if cpu_usage < used_cpu:
alloc_cpu = 0
free_cpu = (cpu_max - used_cpu)
else:
alloc_cpu = (cpu_usage - used_cpu)
free_cpu = (cpu_max - alloc_cpu - used_cpu)
#Round memory values
mem_usage = mem_usage / 1024
used_mem = used_mem / 1024
mem_max = mem_max / 1024
if mem_max < (1024*5):
dimension = "MB"
else:
mem_usage = mem_usage / 1024
used_mem = used_mem / 1024
mem_max = mem_max / 1024
dimension = "GB"
mem_usage = round(mem_usage, 2)
used_mem = round(used_mem, 2)
mem_max = round(mem_max, 2)
if mem_usage < used_mem:
alloc_mem = 0
free_mem = (mem_max - used_mem)
else:
alloc_mem = (mem_usage - used_mem)
free_mem = (mem_max - alloc_mem - used_mem)
used_mem = used_mem
cpu_dict = {'FREE_CPU' : free_cpu, 'ALLOC_CPU' : alloc_cpu , 'USED_CPU' :
used_cpu}
mem_dict = {'FREE_MEM' : free_mem, 'ALLOC_MEM' : alloc_mem , 'USED_MEM' :
used_mem}
print json.dumps({ 'CPU' : cpu_dict, 'MEM' : mem_dict, 'VMS' : running_vms,
'DIMENSION' : dimension})
...@@ -14,4 +14,4 @@ export USER='www-data' ...@@ -14,4 +14,4 @@ export USER='www-data'
#export ONE_LOCATION='/var/lib/opennebula' #export ONE_LOCATION='/var/lib/opennebula'
export PATH="$PATH:$ONE_LOCATION/bin" export PATH="$PATH:$ONE_LOCATION/bin"
sudo -u oneadmin -i onehost list -x sudo -u oneadmin -i onehost list -x | /opt/webadmin/cloud/miscellaneous/stat/stat_dom.py
{% load l10n %}
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data_cpu = new google.visualization.DataTable();
data_cpu.addColumn('string', 'Topping');
data_cpu.addColumn('number', 'Slices');
data_cpu.addRows([
['Free CPU', {{STAT.CPU.FREE_CPU}}],
['Allocated CPU', {{STAT.CPU.ALLOC_CPU}}],
['Used CPU', {{STAT.CPU.USED_CPU}}],
]);
// Set chart options
var cpu_options = {'title':'Cloud CPU usage percent (100/CPU)',
'width':400,
'height':300,
slices: {0: {color: 'blue'}, 1:{color: 'orange'},
2:{color: 'red'}}
};
var data_mem = new google.visualization.DataTable();
data_mem.addColumn('string', 'Topping');
data_mem.addColumn('number', 'Slices');
data_mem.addRows([
['Free Memory', {{STAT.MEM.FREE_MEM|unlocalize}}],
['Allocated Memory', {{STAT.MEM.ALLOC_MEM|unlocalize}}],
['Used Memory', {{STAT.MEM.USED_MEM|unlocalize}}],
]);
// Set chart options
var mem_options = {'title':'Cloud Memory usage in {{STAT.DIMENSION}}',
'width':400,
'height':300,
slices: {0: {color: 'blue'}, 1:{color: 'orange'},
2:{color: 'red'}}
};
// Instantiate and draw our chart, passing in some options.
var chart = new
google.visualization.PieChart(document.getElementById('chart_cpu_div'));
chart.draw(data_cpu, cpu_options);
var chart = new
google.visualization.PieChart(document.getElementById('chart_mem_div'));
chart.draw(data_mem, mem_options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div>Running VMs: {{STAT.VMS}}</div>
<div id="chart_cpu_div"></div>
<div id="chart_mem_div"></div>
</body>
</html>
...@@ -26,6 +26,7 @@ from school.models import * ...@@ -26,6 +26,7 @@ from school.models import *
import django.contrib.auth as auth import django.contrib.auth as auth
import json import json
import logging import logging
import subprocess
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -566,5 +567,17 @@ def _update_keys(user): ...@@ -566,5 +567,17 @@ def _update_keys(user):
user = user.username user = user.username
StoreApi.updateauthorizationinfo(user, password, key_list) StoreApi.updateauthorizationinfo(user, password, key_list)
def stat(request):
values = subprocess.check_output(['/opt/webadmin/cloud/miscellaneous/stat/stat_wrap.sh'])
# values = '''
# {"CPU": {"USED_CPU": 2, "ALLOC_CPU": 0,
# "FREE_CPU": 98}, "MEM": {"FREE_MEM": 1685432, "ALLOC_MEM":0,
# "USED_MEM": 366284}}'''
stat_dict = json.loads(values)
return HttpResponse(render_to_response("stat.html", RequestContext(
request, {
'STAT' : stat_dict,
}
)))
# vim: et sw=4 ai fenc=utf8 smarttab : # vim: et sw=4 ai fenc=utf8 smarttab :
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment