95 lines
3.4 KiB
Python
95 lines
3.4 KiB
Python
import urllib
|
|
import urllib2
|
|
import ssl
|
|
import json
|
|
from VM import VM
|
|
|
|
|
|
def get_auth(host, username, password):
|
|
"""
|
|
Handles ticket and csrf token collection for PVEApi class
|
|
:param host: ip of pve host with port
|
|
:param username: zabbix user
|
|
:param password: zabbix password
|
|
:return: list with csrf token and auth ticket
|
|
"""
|
|
postdata = urllib.urlencode({'username': username, 'password': password})
|
|
url = "https://" + host + "/api2/json/access/ticket"
|
|
req = urllib2.Request(url, postdata)
|
|
response = urllib2.urlopen(req, context=ssl._create_unverified_context())
|
|
response = json.load(response)
|
|
return response['data']['CSRFPreventionToken'], response['data']['ticket']
|
|
|
|
|
|
class PVEApi(object):
|
|
"""Handle API Connection and authorization"""
|
|
def __init__(self, host, username, password):
|
|
"""
|
|
Creates connection object
|
|
:param self:
|
|
:param host: ip of pve host with port
|
|
:param username: zabbix user
|
|
:param password: zabbix password
|
|
:return:
|
|
"""
|
|
self.host = host
|
|
self.url = "https://" + self.host + "/api2/json"
|
|
self.csrftoken = get_auth(self.host, username, password)[0]
|
|
self.ticket = get_auth(self.host, username, password)[1]
|
|
|
|
def get_request(self, url):
|
|
"""
|
|
Handles adding auth ticket to API request
|
|
:param url: url to create request to
|
|
:return: request to handle with urlopen
|
|
"""
|
|
req = urllib2.Request(url)
|
|
req.add_header('Cookie', 'PVEAuthCookie=' + self.ticket)
|
|
return req
|
|
|
|
def get_node_list(self):
|
|
"""
|
|
Gets Node names from cluster data
|
|
:return: List of node names
|
|
"""
|
|
url = self.url + '/nodes'
|
|
response = json.load(urllib2.urlopen(self.get_request(url), context=ssl._create_unverified_context()))
|
|
resources = response['data']
|
|
nodes = list()
|
|
for node in resources:
|
|
if type(node) is dict:
|
|
nodes.append(node[u'node'].encode("ascii"))
|
|
return nodes
|
|
|
|
def get_cluster_vmlist(self):
|
|
"""
|
|
Creates Virtual Machine List
|
|
:return: list of VM class objects
|
|
"""
|
|
vmlist = list()
|
|
for node in self.get_node_list():
|
|
url = self.url + '/nodes/' + node + '/qemu'
|
|
response = json.load(urllib2.urlopen(self.get_request(url), context=ssl._create_unverified_context()))
|
|
resources = response['data']
|
|
for machine in resources:
|
|
name = machine[u'name']
|
|
status = machine[u'status']
|
|
uptime = machine[u'uptime']
|
|
diskread = machine[u'diskread']
|
|
diskwrite = machine[u'diskwrite']
|
|
memusage = machine[u'mem']
|
|
pid = machine[u'pid']
|
|
vmid = machine[u'vmid']
|
|
netin = machine[u'netin']
|
|
netout = machine[u'netout']
|
|
cpus = machine[u'cpus']
|
|
template = machine[u'template']
|
|
disk = machine[u'disk']
|
|
cpuusage = machine[u'cpu']
|
|
maxdisk = machine[u'maxdisk']
|
|
maxmem = machine[u'maxmem']
|
|
vm = VM(node, name, status, uptime, diskread, diskwrite, memusage, pid, vmid, netin, netout, cpus,
|
|
template, disk, cpuusage, maxdisk, maxmem)
|
|
vmlist.append(vm)
|
|
return vmlist
|