mirror of
https://github.com/retspen/webvirtcloud
synced 2025-01-12 08:25:18 +00:00
Fixed graphs when status !=1
This commit is contained in:
parent
30db0f8e7a
commit
a2d1314a75
2 changed files with 95 additions and 71 deletions
|
@ -412,28 +412,15 @@ def instance(request, compute_id, vname):
|
||||||
return render(request, 'instance.html', locals())
|
return render(request, 'instance.html', locals())
|
||||||
|
|
||||||
|
|
||||||
def inst_graph(request, compute_id, vname):
|
def inst_status(request, compute_id, vname):
|
||||||
"""
|
"""
|
||||||
Return instance usage
|
:param request:
|
||||||
|
:return:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not request.user.is_authenticated():
|
if not request.user.is_authenticated():
|
||||||
return HttpResponseRedirect(reverse('login'))
|
return HttpResponseRedirect(reverse('login'))
|
||||||
|
|
||||||
datasets = {}
|
|
||||||
datasets_rd = []
|
|
||||||
datasets_wr = []
|
|
||||||
json_blk = []
|
|
||||||
cookie_blk = {}
|
|
||||||
blk_error = False
|
|
||||||
datasets_rx = []
|
|
||||||
datasets_tx = []
|
|
||||||
json_net = []
|
|
||||||
cookie_net = {}
|
|
||||||
net_error = False
|
|
||||||
networks = None
|
|
||||||
disks = None
|
|
||||||
points = 5
|
|
||||||
curent_time = time.strftime("%H:%M:%S")
|
|
||||||
compute = Compute.objects.get(id=compute_id)
|
compute = Compute.objects.get(id=compute_id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -443,18 +430,58 @@ def inst_graph(request, compute_id, vname):
|
||||||
compute.type,
|
compute.type,
|
||||||
vname)
|
vname)
|
||||||
status = conn.get_status()
|
status = conn.get_status()
|
||||||
|
conn.close()
|
||||||
|
except libvirtError:
|
||||||
|
status = None
|
||||||
|
|
||||||
|
data = json.dumps({'status': status})
|
||||||
|
response = HttpResponse()
|
||||||
|
response['Content-Type'] = "text/javascript"
|
||||||
|
response.write(data)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def inst_graph(request, compute_id, vname):
|
||||||
|
"""
|
||||||
|
:param request:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not request.user.is_authenticated():
|
||||||
|
return HttpResponseRedirect(reverse('login'))
|
||||||
|
|
||||||
|
datasets = {}
|
||||||
|
datasets_rd = []
|
||||||
|
datasets_wr = []
|
||||||
|
json_blk = []
|
||||||
|
cookie_blk = {}
|
||||||
|
datasets_rx = []
|
||||||
|
datasets_tx = []
|
||||||
|
json_net = []
|
||||||
|
cookie_net = {}
|
||||||
|
points = 5
|
||||||
|
curent_time = time.strftime("%H:%M:%S")
|
||||||
|
compute = Compute.objects.get(id=compute_id)
|
||||||
|
cookies = request.COOKIES
|
||||||
|
response = HttpResponse()
|
||||||
|
response['Content-Type'] = "text/javascript"
|
||||||
|
|
||||||
|
def check_points(dataset):
|
||||||
|
if len(dataset) > points:
|
||||||
|
dataset.pop(0)
|
||||||
|
return dataset
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn = wvmInstance(compute.hostname,
|
||||||
|
compute.login,
|
||||||
|
compute.password,
|
||||||
|
compute.type,
|
||||||
|
vname)
|
||||||
cpu_usage = conn.cpu_usage()
|
cpu_usage = conn.cpu_usage()
|
||||||
blk_usage = conn.disk_usage()
|
blk_usage = conn.disk_usage()
|
||||||
net_usage = conn.net_usage()
|
net_usage = conn.net_usage()
|
||||||
conn.close()
|
conn.close()
|
||||||
except libvirtError:
|
|
||||||
status = None
|
|
||||||
blk_usage = None
|
|
||||||
cpu_usage = None
|
|
||||||
net_usage = None
|
|
||||||
|
|
||||||
if status == 1:
|
|
||||||
cookies = request.COOKIES
|
|
||||||
if cookies.get('cpu') == '{}' or not cookies.get('cpu') or not cpu_usage:
|
if cookies.get('cpu') == '{}' or not cookies.get('cpu') or not cpu_usage:
|
||||||
datasets['cpu'] = [0]
|
datasets['cpu'] = [0]
|
||||||
datasets['timer'] = [curent_time]
|
datasets['timer'] = [curent_time]
|
||||||
|
@ -465,10 +492,8 @@ def inst_graph(request, compute_id, vname):
|
||||||
datasets['timer'].append(curent_time)
|
datasets['timer'].append(curent_time)
|
||||||
datasets['cpu'].append(int(cpu_usage['cpu']))
|
datasets['cpu'].append(int(cpu_usage['cpu']))
|
||||||
|
|
||||||
if len(datasets['timer']) > points:
|
datasets['timer'] = check_points(datasets['timer'])
|
||||||
datasets['timer'].pop(0)
|
datasets['cpu'] = check_points(datasets['cpu'])
|
||||||
if len(datasets['cpu']) > points:
|
|
||||||
datasets['cpu'].pop(0)
|
|
||||||
|
|
||||||
for blk in blk_usage:
|
for blk in blk_usage:
|
||||||
if cookies.get('blk') == '{}' or not cookies.get('blk') or not blk_usage:
|
if cookies.get('blk') == '{}' or not cookies.get('blk') or not blk_usage:
|
||||||
|
@ -476,20 +501,14 @@ def inst_graph(request, compute_id, vname):
|
||||||
datasets_rd.append(0)
|
datasets_rd.append(0)
|
||||||
else:
|
else:
|
||||||
datasets['blk'] = eval(cookies.get('blk'))
|
datasets['blk'] = eval(cookies.get('blk'))
|
||||||
try:
|
datasets_rd = datasets['blk'][blk['dev']][0]
|
||||||
datasets_rd = datasets['blk'][blk['dev']][0]
|
datasets_wr = datasets['blk'][blk['dev']][1]
|
||||||
datasets_wr = datasets['blk'][blk['dev']][1]
|
|
||||||
except:
|
|
||||||
blk_error = True
|
|
||||||
|
|
||||||
if not blk_error:
|
|
||||||
datasets_rd.append(int(blk['rd']) / 1048576)
|
datasets_rd.append(int(blk['rd']) / 1048576)
|
||||||
datasets_wr.append(int(blk['wr']) / 1048576)
|
datasets_wr.append(int(blk['wr']) / 1048576)
|
||||||
|
|
||||||
if len(datasets_rd) > points:
|
datasets_rd = check_points(datasets_rd)
|
||||||
datasets_rd.pop(0)
|
datasets_wr = check_points(datasets_wr)
|
||||||
if len(datasets_wr) >= points + 1:
|
|
||||||
datasets_wr.pop(0)
|
|
||||||
|
|
||||||
json_blk.append({'dev': blk['dev'], 'data': [datasets_rd, datasets_wr]})
|
json_blk.append({'dev': blk['dev'], 'data': [datasets_rd, datasets_wr]})
|
||||||
cookie_blk[blk['dev']] = [datasets_rd, datasets_wr]
|
cookie_blk[blk['dev']] = [datasets_rd, datasets_wr]
|
||||||
|
@ -500,32 +519,26 @@ def inst_graph(request, compute_id, vname):
|
||||||
datasets_tx.append(0)
|
datasets_tx.append(0)
|
||||||
else:
|
else:
|
||||||
datasets['net'] = eval(cookies.get('net'))
|
datasets['net'] = eval(cookies.get('net'))
|
||||||
try:
|
datasets_rx = datasets['net'][net['dev']][0]
|
||||||
datasets_rx = datasets['net'][net['dev']][0]
|
datasets_tx = datasets['net'][net['dev']][1]
|
||||||
datasets_tx = datasets['net'][net['dev']][1]
|
|
||||||
except:
|
|
||||||
net_error = True
|
|
||||||
|
|
||||||
if not net_error:
|
|
||||||
datasets_rx.append(int(net['rx']) / 1048576)
|
datasets_rx.append(int(net['rx']) / 1048576)
|
||||||
datasets_tx.append(int(net['tx']) / 1048576)
|
datasets_tx.append(int(net['tx']) / 1048576)
|
||||||
|
|
||||||
if len(datasets_rx) > points:
|
datasets_rx = check_points(datasets_rx)
|
||||||
datasets_rx.pop(0)
|
datasets_tx = check_points(datasets_tx)
|
||||||
if len(datasets_tx) > points:
|
|
||||||
datasets_tx.pop(0)
|
|
||||||
|
|
||||||
json_net.append({'dev': net['dev'], 'data': [datasets_rx, datasets_tx]})
|
json_net.append({'dev': net['dev'], 'data': [datasets_rx, datasets_tx]})
|
||||||
cookie_net[net['dev']] = [datasets_rx, datasets_tx]
|
cookie_net[net['dev']] = [datasets_rx, datasets_tx]
|
||||||
|
|
||||||
data = json.dumps({'status': status, 'cpudata': datasets['cpu'], 'blkdata': json_blk, 'netdata': json_net, 'timeline': datasets['timer']})
|
data = json.dumps({'cpudata': datasets['cpu'], 'blkdata': json_blk, 'netdata': json_net, 'timeline': datasets['timer']})
|
||||||
|
|
||||||
response = HttpResponse()
|
|
||||||
response['Content-Type'] = "text/javascript"
|
|
||||||
if status == 1:
|
|
||||||
response.cookies['cpu'] = datasets['cpu']
|
response.cookies['cpu'] = datasets['cpu']
|
||||||
response.cookies['timer'] = datasets['timer']
|
response.cookies['timer'] = datasets['timer']
|
||||||
response.cookies['blk'] = cookie_blk
|
response.cookies['blk'] = cookie_blk
|
||||||
response.cookies['net'] = cookie_net
|
response.cookies['net'] = cookie_net
|
||||||
|
except libvirtError:
|
||||||
|
data = json.dumps({'error': 'Error 500'})
|
||||||
|
|
||||||
response.write(data)
|
response.write(data)
|
||||||
return response
|
return response
|
|
@ -336,31 +336,42 @@ class wvmInstance(wvmConnect):
|
||||||
dev_file = dev_bus
|
dev_file = dev_bus
|
||||||
devices.append([dev_file, dev_bus])
|
devices.append([dev_file, dev_bus])
|
||||||
for dev in devices:
|
for dev in devices:
|
||||||
rd_use_ago = self.instance.blockStats(dev[0])[1]
|
if self.get_status() == 1:
|
||||||
wr_use_ago = self.instance.blockStats(dev[0])[3]
|
rd_use_ago = self.instance.blockStats(dev[0])[1]
|
||||||
time.sleep(1)
|
wr_use_ago = self.instance.blockStats(dev[0])[3]
|
||||||
rd_use_now = self.instance.blockStats(dev[0])[1]
|
time.sleep(1)
|
||||||
wr_use_now = self.instance.blockStats(dev[0])[3]
|
rd_use_now = self.instance.blockStats(dev[0])[1]
|
||||||
rd_diff_usage = rd_use_now - rd_use_ago
|
wr_use_now = self.instance.blockStats(dev[0])[3]
|
||||||
wr_diff_usage = wr_use_now - wr_use_ago
|
rd_diff_usage = rd_use_now - rd_use_ago
|
||||||
|
wr_diff_usage = wr_use_now - wr_use_ago
|
||||||
|
else:
|
||||||
|
rd_diff_usage = 0
|
||||||
|
wr_diff_usage = 0
|
||||||
|
|
||||||
dev_usage.append({'dev': dev[1], 'rd': rd_diff_usage, 'wr': wr_diff_usage})
|
dev_usage.append({'dev': dev[1], 'rd': rd_diff_usage, 'wr': wr_diff_usage})
|
||||||
|
|
||||||
return dev_usage
|
return dev_usage
|
||||||
|
|
||||||
def net_usage(self):
|
def net_usage(self):
|
||||||
devices = []
|
devices = []
|
||||||
dev_usage = []
|
dev_usage = []
|
||||||
tree = ElementTree.fromstring(self._XMLDesc(0))
|
if self.get_status() == 1:
|
||||||
for target in tree.findall("devices/interface/target"):
|
tree = ElementTree.fromstring(self._XMLDesc(0))
|
||||||
devices.append(target.get("dev"))
|
for target in tree.findall("devices/interface/target"):
|
||||||
for i, dev in enumerate(devices):
|
devices.append(target.get("dev"))
|
||||||
rx_use_ago = self.instance.interfaceStats(dev)[0]
|
for i, dev in enumerate(devices):
|
||||||
tx_use_ago = self.instance.interfaceStats(dev)[4]
|
rx_use_ago = self.instance.interfaceStats(dev)[0]
|
||||||
time.sleep(1)
|
tx_use_ago = self.instance.interfaceStats(dev)[4]
|
||||||
rx_use_now = self.instance.interfaceStats(dev)[0]
|
time.sleep(1)
|
||||||
tx_use_now = self.instance.interfaceStats(dev)[4]
|
rx_use_now = self.instance.interfaceStats(dev)[0]
|
||||||
rx_diff_usage = (rx_use_now - rx_use_ago) * 8
|
tx_use_now = self.instance.interfaceStats(dev)[4]
|
||||||
tx_diff_usage = (tx_use_now - tx_use_ago) * 8
|
rx_diff_usage = (rx_use_now - rx_use_ago) * 8
|
||||||
dev_usage.append({'dev': i, 'rx': rx_diff_usage, 'tx': tx_diff_usage})
|
tx_diff_usage = (tx_use_now - tx_use_ago) * 8
|
||||||
|
dev_usage.append({'dev': i, 'rx': rx_diff_usage, 'tx': tx_diff_usage})
|
||||||
|
else:
|
||||||
|
for i, dev in enumerate(self.get_net_device()):
|
||||||
|
dev_usage.append({'dev': i, 'rx': 0, 'tx': 0})
|
||||||
|
|
||||||
return dev_usage
|
return dev_usage
|
||||||
|
|
||||||
def get_telnet_port(self):
|
def get_telnet_port(self):
|
||||||
|
|
Loading…
Reference in a new issue