81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
from PVEApi import PVEApi
|
|
from argparse import ArgumentParser
|
|
import json
|
|
|
|
USERNAME = "zabbix@pve"
|
|
PASSWORD = "zabbix"
|
|
HOST = "172.16.3.11:8006"
|
|
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, 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 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
|
|
|
|
|
|
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
|