VM refactor with methods
This commit is contained in:
parent
83d9d75c42
commit
b576ab275b
|
|
@ -1,6 +1,7 @@
|
|||
from PVEApi import PVEApi
|
||||
from ZabbixJSON import listofdicts_to_zabbix_json
|
||||
from argparse import ArgumentParser
|
||||
import json
|
||||
from VM import VM
|
||||
|
||||
USERNAME = "zabbix@pve"
|
||||
PASSWORD = "zabbix"
|
||||
|
|
@ -21,74 +22,24 @@ if NODE is None:
|
|||
raise ValueError("No such Node found", "Valid nodes:", nodelist)
|
||||
|
||||
|
||||
def get_vmnames(vmlist, vmnode, **kwargs):
|
||||
# TODO move to VM class
|
||||
"""
|
||||
Get Virtual Machine Names for discovery
|
||||
:param vmlist: list of VM class objects
|
||||
:param vmnode: 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 == vmnode.name:
|
||||
name_vmlist.append({'#VMNAME': vm.name})
|
||||
elif kwargs['name'] is not None and kwargs['listing'] is True:
|
||||
if vm.node == vmnode.name and vm.name == kwargs['name']:
|
||||
name_vmlist.append({'#VMNAME': vm.name})
|
||||
return name_vmlist
|
||||
|
||||
|
||||
def get_vminfo(vmname, parameter=None):
|
||||
# TODO move to VM class
|
||||
"""
|
||||
Get Virtual Machine data
|
||||
:param vmname: Virtual machine name
|
||||
:param parameter: Specific VM stat e.g.: CPU Usage, use if you want to get one parameter for Zabbix
|
||||
:return: list od dicts with vm params
|
||||
"""
|
||||
vm = next(v for v in conn.get_cluster_vmlist(nodelist) if v.name == vmname)
|
||||
if parameter is not None:
|
||||
vm_params = vm.__getattribute__(parameter)
|
||||
return vm_params
|
||||
else:
|
||||
vm_params = list()
|
||||
for param in dir(vm):
|
||||
if not param.startswith('__'):
|
||||
vm_params.append({'#' + param.upper(): vm.__getattribute__(param)})
|
||||
return listofdicts_to_zabbix_json(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
|
||||
"""
|
||||
newdlist = list()
|
||||
for d in dlist:
|
||||
for k, v in d.iteritems():
|
||||
k = '{' + k + '}'
|
||||
newdict = {k: v}
|
||||
newdlist.append(newdict)
|
||||
newdlist = {"data": newdlist}
|
||||
zabbixjson = json.dumps(newdlist, separators=(',', ':'))
|
||||
return zabbixjson
|
||||
|
||||
|
||||
def exec_vm(**kwargs):
|
||||
"""
|
||||
Handles vm positional parameter
|
||||
:param kwargs: handle cmd parametres
|
||||
:return:
|
||||
"""
|
||||
# Initiate instances
|
||||
vms = conn.get_cluster_vmlist(nodelist)
|
||||
|
||||
if kwargs['name'] is not None and kwargs['listing'] is False:
|
||||
print get_vminfo(kwargs['name'], kwargs['param'])
|
||||
try:
|
||||
vm = next(v for v in vms if v.name == kwargs['name'])
|
||||
print vm.get_vminfo(kwargs['param'])
|
||||
except StopIteration:
|
||||
print "No such VM found. Possible VMs: " + str(VM.get_vmnames(NODE))
|
||||
else:
|
||||
print listofdicts_to_zabbix_json(
|
||||
get_vmnames(conn.get_cluster_vmlist(nodelist), NODE, name=kwargs['name'], listing=kwargs['listing']))
|
||||
VM.get_vmnames(NODE, name=kwargs['name'], listing=kwargs['listing']))
|
||||
|
||||
|
||||
if args.type == "vm":
|
||||
|
|
|
|||
47
VM.py
47
VM.py
|
|
@ -1,13 +1,19 @@
|
|||
from ZabbixJSON import listofdicts_to_zabbix_json
|
||||
|
||||
|
||||
class VM(object):
|
||||
"""
|
||||
Proxmox Virtual Machine API Parameters
|
||||
"""
|
||||
instances = list()
|
||||
|
||||
def __init__(self, node, name, status, uptime, diskread, diskwrite, memusage, pid, vmid, netin, netout, cpus,
|
||||
template, disk, cpuusage, maxdisk, maxmem):
|
||||
self.__class__.instances.append(self)
|
||||
self.name = name
|
||||
self.node = node.name
|
||||
self.statusstr = status
|
||||
self.status = 1 if self.statusstr == "running" else 0
|
||||
self.__statusstr = status
|
||||
self.status = 1 if self.__statusstr == "running" else 0
|
||||
self.uptime = uptime
|
||||
self.diskread = diskread
|
||||
self.diskwrite = diskwrite
|
||||
|
|
@ -25,3 +31,40 @@ class VM(object):
|
|||
|
||||
def __repr__(self):
|
||||
return "<%s-%s:%s>" % (self.node, self.vmid, self.name)
|
||||
|
||||
@classmethod
|
||||
def get_vmnames(cls, vmnode, **kwargs):
|
||||
"""
|
||||
Get Virtual Machine Names for discovery
|
||||
:param vmnode: 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 c in cls.instances:
|
||||
try:
|
||||
if kwargs['name'] is None:
|
||||
if c.node == vmnode.name:
|
||||
name_vmlist.append({'#VMNAME': c.name})
|
||||
elif kwargs['name'] is not None and kwargs['listing'] is True:
|
||||
if c.node == vmnode.name and c.name == kwargs['name']:
|
||||
name_vmlist.append({'#VMNAME': c.name})
|
||||
except KeyError:
|
||||
return [c.name for c in cls.instances if c.node == vmnode.name]
|
||||
return name_vmlist
|
||||
|
||||
def get_vminfo(self, parameter=None):
|
||||
"""
|
||||
Get Virtual Machine data
|
||||
:param parameter: Specific VM stat e.g.: CPU Usage, use if you want to get one parameter for Zabbix
|
||||
:return: list od dicts with vm params
|
||||
"""
|
||||
if parameter is not None:
|
||||
vm_params = self.__getattribute__(parameter)
|
||||
return vm_params
|
||||
else:
|
||||
vm_params = list()
|
||||
for param in vars(self):
|
||||
if not param.startswith('__'):
|
||||
vm_params.append({'#' + param.upper(): self.__getattribute__(param)})
|
||||
return listofdicts_to_zabbix_json(vm_params)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
import json
|
||||
|
||||
|
||||
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
|
||||
"""
|
||||
newdlist = list()
|
||||
for d in dlist:
|
||||
for k, v in d.iteritems():
|
||||
k = '{' + k + '}'
|
||||
newdict = {k: v}
|
||||
newdlist.append(newdict)
|
||||
newdlist = {"data": newdlist}
|
||||
zabbixjson = json.dumps(newdlist, separators=(',', ':'))
|
||||
return zabbixjson
|
||||
Loading…
Reference in New Issue