From 97efded2d05acade8c511cfc79564db7051dd053 Mon Sep 17 00:00:00 2001 From: deflax Date: Sun, 26 Jun 2016 18:09:22 +0300 Subject: [PATCH] add vm rrd generator --- clientsdb.py | 4 ++-- plugin.py | 25 +++++++++++++++++++++++-- proxmaster.py | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/clientsdb.py b/clientsdb.py index 381230d..ecce50a 100644 --- a/clientsdb.py +++ b/clientsdb.py @@ -67,7 +67,7 @@ def setencpasswd(clientemail, newpass): def checkin(clientid): - """ return list of owned vmids if client id matches the client database. (logged-in users)""" + """ returns a list of owned vmids if client id matches the client database. (logged-in users)""" #1. search for the client try: clientsdb = readclientsdb() @@ -82,7 +82,7 @@ def checkin(clientid): def validate(clientemail, password): - """ return list of owned vmids if credentials match an user from the database. (fresh logins)""" + """ returns a list of owned vmids if credentials match an user from the database. (fresh logins)""" #1. search for the client try: clientsdb = readclientsdb() diff --git a/plugin.py b/plugin.py index 8f408bd..ed6e388 100644 --- a/plugin.py +++ b/plugin.py @@ -231,6 +231,26 @@ def vmresume(vm_id): return response +def vmrrd(vm_id): + """ retrieve rrd graphs (PNG) """ + slave_id, vm_type = grid.query_vm(vm_id) + proxobject = auth(slave_id) + vm_type = vm_type.lower() + slave_name = proxobject.cluster.status.get()[0]['name'] + ioconfig.logger.info('slave[%s]> query rrd of %s %s' % (slave_name, vm_type, vm_id)) + + result = {} + if vm_type == 'kvm': + result['cpu'] = proxobject.nodes(slave_name).qemu(vm_id).rrd.get(node=slave_name, vmid=vm_id, timeframe='day', cf='AVERAGE', ds='cpu') + result['mem'] = proxobject.nodes(slave_name).qemu(vm_id).rrd.get(node=slave_name, vmid=vm_id, timeframe='day', cf='AVERAGE', ds='mem,maxmem') + result['net'] = proxobject.nodes(slave_name).qemu(vm_id).rrd.get(node=slave_name, vmid=vm_id, timeframe='day', cf='AVERAGE', ds='netin,netout') + result['hdd'] = proxobject.nodes(slave_name).qemu(vm_id).rrd.get(node=slave_name, vmid=vm_id, timeframe='day', cf='AVERAGE', ds='diskread,diskwrite') + + if vm_type == 'lxc': + result = proxobject.nodes(slave_name).lxc(vm_id).rrd.get() + repsonse = { 'status':'RRD', 'vmid':vm_id, rrd:result } + + def vmvnc(vm_id): """ invoke vnc ticket """ slave_id, vm_type = grid.query_vm(vm_id) @@ -269,9 +289,10 @@ def vmvnc(vm_id): external_url = ioconfig.parser.get('general', 'novnc_url') prefix = external_url + "/?host=" + myip + "&port=" + listenport + "&encrypt=0&true_color=1&password=" vnc_url = prefix + ticket['ticket'] - ioconfig.logger.info('slave[{}]> {}'.format(slave_name, vnc_url)) - response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] } + ioconfig.logger.info('slave[{}]> vnc port {} ready'.format(slave_name, listenport)) + #response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] } + response = { 'status':'VNC', 'url':vnc_url } return response diff --git a/proxmaster.py b/proxmaster.py index 7fc86dc..547aa8e 100644 --- a/proxmaster.py +++ b/proxmaster.py @@ -70,6 +70,9 @@ def selector(fn, req, vmid=0): elif fn == 'vmstop': body = plugin.vmstop(vmid) + elif fn == 'vmrrd': + body = plugin.vmrrd(vmid) + elif fn == 'vmvnc': body = plugin.vmvnc(vmid) @@ -222,6 +225,14 @@ class StopResource(object): resp.status, response = selector('vmstop', req, vmid) req.context['result'] = response +class RRDResource(object): + @falcon.before(max_body(64 * 1024)) + def on_post(self, req, resp, vmid): + """ Generate rrd pngs """ + logger.info('grid> rrd ' + str(vmid)) + resp.status, response = selector('vmrrd', req, vmid) + req.context['result' = response + class VNCResource(object): @falcon.before(max_body(64 * 1024)) def on_post(self, req, resp, vmid): @@ -269,6 +280,9 @@ api.add_route('/vmshutdown/{vmid}', res_shutdown) res_stop = StopResource() api.add_route('/vmstop/{vmid}', res_stop) +res_rrd = RRDResource() +api.add_route('vmrrd/{vmid}', res_rrd) + res_vnc = VNCResource() api.add_route('/vmvnc/{vmid}', res_vnc)