first version should work with vm discovery and data
This commit is contained in:
parent
862dc722ec
commit
00f714cc27
|
|
@ -1,4 +1,5 @@
|
|||
from PVEApi import PVEApi
|
||||
from argparse import ArgumentParser
|
||||
import json
|
||||
|
||||
USERNAME = "zabbix@pve"
|
||||
|
|
@ -8,14 +9,72 @@ NODE='h1'
|
|||
|
||||
conn = PVEApi(HOST, USERNAME, PASSWORD)
|
||||
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("node", choices=conn.get_node_list(), help="Choose node")
|
||||
parser.add_argument("type", choices=['vm', 'cluster', 'node'], help='categories')
|
||||
parser.add_argument("-l", "--list", dest='listing', action='store_true')
|
||||
parser.add_argument("-n", "--name", dest='name')
|
||||
args = parser.parse_args()
|
||||
|
||||
def get_vmnames(vmlist):
|
||||
name_vmlist = [{"#VMNAME": vm.name} for vm in vmlist]
|
||||
|
||||
def get_vmnames(vmlist, node, **kwargs):
|
||||
"""
|
||||
Get Virtual Machine Names for discovery
|
||||
:param vmlist: list of VM class objects
|
||||
:param node: node name with to filter VMs by
|
||||
:param kwargs: used to accept command line parameters
|
||||
:return: list of dicts in [{'#VMNAME': "$name"}] format
|
||||
"""
|
||||
name_vmlist = list()
|
||||
for vm in vmlist:
|
||||
if kwargs['name'] is None:
|
||||
if vm.node == node:
|
||||
name_vmlist.append({'#VMNAME': vm.name})
|
||||
elif kwargs['name'] is not None and kwargs['listing'] is True:
|
||||
if vm.node == node and vm.name == kwargs['name']:
|
||||
name_vmlist.append({'#VMNAME': vm.name})
|
||||
return name_vmlist
|
||||
|
||||
def listofdicts_to_zabbix_json(list):
|
||||
# TODO wrap in "data:[]"
|
||||
zabbixjson=json.dumps(list)
|
||||
|
||||
def get_vminfo(vmname):
|
||||
"""
|
||||
Get Virtual Machine data
|
||||
:param vmname: Virtual machine name
|
||||
:return: list od dicts with vm params
|
||||
"""
|
||||
vm_params = list()
|
||||
vm = next(v for v in conn.get_cluster_vmlist() if v.name == vmname)
|
||||
for param in dir(vm):
|
||||
if not param.startswith('__'):
|
||||
vm_params.append({'#' + param.upper(): vm.__getattribute__(param)})
|
||||
return vm_params
|
||||
|
||||
|
||||
def listofdicts_to_zabbix_json(dlist):
|
||||
"""
|
||||
Converts list of dicts to JSON an wraps around 'data'
|
||||
:param dlist: list of dicts you want to convert
|
||||
:return: JSON string for zabbix-get
|
||||
"""
|
||||
dlist = {"data": dlist}
|
||||
zabbixjson = json.dumps(dlist)
|
||||
return zabbixjson
|
||||
|
||||
print listofdicts_to_zabbix_json(get_vmnames(conn.get_cluster_vmlist()))
|
||||
|
||||
def exec_vm(**kwargs):
|
||||
"""
|
||||
Handles vm positional parameter
|
||||
:param kwargs: handle cmd parametres
|
||||
:return:
|
||||
"""
|
||||
if kwargs['name'] is not None and kwargs['listing'] is False:
|
||||
print listofdicts_to_zabbix_json(get_vminfo(kwargs['name']))
|
||||
else:
|
||||
print listofdicts_to_zabbix_json(
|
||||
get_vmnames(conn.get_cluster_vmlist(), NODE, name=kwargs['name'], listing=kwargs['listing']))
|
||||
|
||||
|
||||
if args.type == "vm":
|
||||
exec_vm(listing=args.listing, name=args.name)
|
||||
else:
|
||||
print args
|
||||
|
|
|
|||
32
PVEApi.py
32
PVEApi.py
|
|
@ -6,6 +6,13 @@ 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)
|
||||
|
|
@ -15,29 +22,50 @@ def get_auth(host, username, password):
|
|||
|
||||
|
||||
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'])
|
||||
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'
|
||||
|
|
|
|||
Loading…
Reference in New Issue