added detailed data for VMs

This commit is contained in:
Miłosz Stocki 2019-01-21 13:30:13 +01:00
parent 3e2e9e9b65
commit fbf27eecd0
Signed by: osiu97
GPG Key ID: E3D1D83FA04F51D6
4 changed files with 41 additions and 6 deletions

View File

@ -5,6 +5,7 @@ from VM import VM
from Storage import Storage
USERNAME = "zabbix@pve"
# TODO change password to example zabbix
PASSWORD = "zabbix"
HOST = "172.16.3.11:8006"
@ -16,13 +17,17 @@ parser.add_argument("node", help="Enter node name")
parser.add_argument("type", choices=['vm', 'storage'], help='categories')
parser.add_argument("-l", "--list", dest='listing', action='store_true')
parser.add_argument("-n", "--name", dest='name')
parser.add_argument("-d", "--details", dest='details', action='store_true')
parser.add_argument("-p", "--param", dest='param')
args = parser.parse_args()
NODE = next((n for n in nodelist if n.name == args.node), None)
if NODE is None:
raise ValueError("No such Node found", "Valid nodes:", nodelist)
def exec_vm(**kwargs):
"""
Handles vm positional parameter
@ -30,6 +35,9 @@ def exec_vm(**kwargs):
:return:
"""
# Initiate instances
if kwargs['details'] is True:
vms = conn.get_cluster_vmlist(nodelist, detailed=True)
else:
vms = conn.get_cluster_vmlist(nodelist)
if kwargs['name'] is not None and kwargs['listing'] is False:
@ -64,7 +72,7 @@ def exec_storage(**kwargs):
if args.type == "vm":
exec_vm(listing=args.listing, name=args.name, param=args.param)
exec_vm(listing=args.listing, name=args.name, param=args.param, details=args.details)
elif args.type == "storage":
exec_storage(listing=args.listing, name=args.name, param=args.param)
else:

View File

@ -65,7 +65,7 @@ class PVEApi(object):
nodes.append(nodeobj)
return nodes
def get_cluster_vmlist(self, nodelist):
def get_cluster_vmlist(self, nodelist, detailed=False):
"""
Creates Virtual Machine List
:return: list of VM class objects
@ -93,10 +93,31 @@ class PVEApi(object):
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)
template, disk, cpuusage, maxdisk, maxmem, url)
if detailed is True:
vm_dattr = self.get_details(url, vmid)
for k,v in vm_dattr.items():
vm.__setattr__('det_'+k, v)
vmlist.append(vm)
return vmlist
def get_details(self, url, vmid):
"""
Gets vm detailed data from PVE API (its slower than just basic data)
:param url: url used in vm data get
:param vmid: vmid to create new url for detailed data
:return: dict of detailed data params
"""
url = url + '/' + vmid + '/status/current'
response = json.load(urllib2.urlopen(self.get_request(url), context=ssl._create_unverified_context()))
resources = response['data']
attributedict = dict()
for param, paramvalue in resources.items():
attributedict.update({param: paramvalue})
return attributedict
def get_cluster_storagelist(self, nodelist):
"""
Creates Virtual Machine List

View File

@ -9,5 +9,5 @@ HOST = "localhost:8006"
```
<h4>Zabbix Config User Parameter:</h4>
```
UserParameter=pve-zabbix[*],/usr/bin/python /home/zabbix/PVE-Zabbix/PVE-Zabbix.py $1 $2 $3 $4 $5 $6
UserParameter=pve-zabbix[*],/usr/bin/python /home/zabbix/PVE-Zabbix/PVE-Zabbix.py $1 $2 $3 $4 $5 $6 $7
```

8
VM.py
View File

@ -8,7 +8,7 @@ class VM(object):
instances = list()
def __init__(self, node, name, status, uptime, diskread, diskwrite, memusage, pid, vmid, netin, netout, cpus,
template, disk, cpuusage, maxdisk, maxmem):
template, disk, cpuusage, maxdisk, maxmem, url, **kwargs):
self.__class__.instances.append(self)
self.name = name
self.node = node.name
@ -28,6 +28,8 @@ class VM(object):
self.cpuusage = cpuusage
self.maxdisk = maxdisk
self.maxmem = maxmem
self.__url = url
self.__dic = kwargs
def __repr__(self):
return "<%s-%s:%s>" % (self.node, self.vmid, self.name)
@ -68,3 +70,7 @@ class VM(object):
if not param.startswith('__'):
vm_params.append({'#' + param.upper(): self.__getattribute__(param)})
return listofdicts_to_zabbix_json(vm_params)
def get_detailedvminfo(self):
print self.__dic