added storage with bugs to fix
This commit is contained in:
parent
b576ab275b
commit
bb0083b60d
|
|
@ -2,6 +2,7 @@ from PVEApi import PVEApi
|
|||
from ZabbixJSON import listofdicts_to_zabbix_json
|
||||
from argparse import ArgumentParser
|
||||
from VM import VM
|
||||
from Storage import Storage
|
||||
|
||||
USERNAME = "zabbix@pve"
|
||||
PASSWORD = "zabbix"
|
||||
|
|
@ -12,7 +13,7 @@ nodelist = conn.get_node_list()
|
|||
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument("node", help="Enter node name")
|
||||
parser.add_argument("type", choices=['vm', 'cluster', 'node'], help='categories')
|
||||
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("-p", "--param", dest='param')
|
||||
|
|
@ -42,7 +43,30 @@ def exec_vm(**kwargs):
|
|||
VM.get_vmnames(NODE, name=kwargs['name'], listing=kwargs['listing']))
|
||||
|
||||
|
||||
def exec_storage(**kwargs):
|
||||
"""
|
||||
Handles storage positional parameter
|
||||
:param kwargs: handle cmd parametres
|
||||
:return:
|
||||
"""
|
||||
# Initiate instances
|
||||
stors = conn.get_cluster_storagelist(nodelist)
|
||||
|
||||
# FIXME consider node name to avoid random results from duplicate storage names
|
||||
if kwargs['name'] is not None and kwargs['listing'] is False:
|
||||
try:
|
||||
stor = next(v for v in stors if v.name == kwargs['name'])
|
||||
print stor.get_storageinfo(kwargs['param'])
|
||||
except StopIteration:
|
||||
print "No such VM found. Possible VMs: " + str(VM.get_vmnames(NODE))
|
||||
else:
|
||||
print listofdicts_to_zabbix_json(
|
||||
Storage.get_storagenames(NODE, name=kwargs['name'], listing=kwargs['listing']))
|
||||
|
||||
|
||||
if args.type == "vm":
|
||||
exec_vm(listing=args.listing, name=args.name, param=args.param)
|
||||
elif args.type == "storage":
|
||||
exec_storage(listing=args.listing, name=args.name, param=args.param)
|
||||
else:
|
||||
print args
|
||||
|
|
|
|||
23
PVEApi.py
23
PVEApi.py
|
|
@ -4,6 +4,7 @@ import ssl
|
|||
import json
|
||||
from VM import VM
|
||||
from Node import Node
|
||||
from Storage import Storage
|
||||
|
||||
|
||||
def get_auth(host, username, password):
|
||||
|
|
@ -95,3 +96,25 @@ class PVEApi(object):
|
|||
template, disk, cpuusage, maxdisk, maxmem)
|
||||
vmlist.append(vm)
|
||||
return vmlist
|
||||
|
||||
def get_cluster_storagelist(self, nodelist):
|
||||
"""
|
||||
Creates Virtual Machine List
|
||||
:return: list of VM class objects
|
||||
"""
|
||||
storagelist = list()
|
||||
for node in nodelist:
|
||||
url = self.url + '/nodes/' + node.name + '/storage'
|
||||
response = json.load(urllib2.urlopen(self.get_request(url), context=ssl._create_unverified_context()))
|
||||
resources = response['data']
|
||||
for storage in resources:
|
||||
name = storage[u'storage']
|
||||
total = storage[u'total']
|
||||
used = storage[u'used']
|
||||
active = storage[u'active']
|
||||
enabled = storage[u'enabled']
|
||||
avail = storage[u'avail']
|
||||
used_fraction = storage[u'used_fraction']
|
||||
stor = Storage(node, name, total, used, active, enabled, avail, used_fraction)
|
||||
storagelist.append(stor)
|
||||
return storagelist
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
from ZabbixJSON import listofdicts_to_zabbix_json
|
||||
|
||||
|
||||
class Storage(object):
|
||||
""" Storage calls can be super slow """
|
||||
instances = list()
|
||||
|
||||
def __init__(self, node, name, total, used, active, enabled, avail, used_fraction):
|
||||
self.__class__.instances.append(self)
|
||||
self.name = name
|
||||
self.node = node.name
|
||||
self.total = total
|
||||
self.used = used
|
||||
self.active = active
|
||||
self.enabled = enabled
|
||||
self.avail = avail
|
||||
self.used_fraction = used_fraction
|
||||
|
||||
def __repr__(self):
|
||||
return '%s' % self.name
|
||||
|
||||
@classmethod
|
||||
def get_storagenames(cls, storagenode, **kwargs):
|
||||
"""
|
||||
Get Virtual Machine Names for discovery
|
||||
:param storagenode: node name with to filter VMs by
|
||||
:param kwargs: used to accept command line parameters
|
||||
:return: list of dicts in [{'#STORNAME': "$name"}] format
|
||||
"""
|
||||
name_storagelist = list()
|
||||
for c in cls.instances:
|
||||
try:
|
||||
if kwargs['name'] is None:
|
||||
if c.node == storagenode.name:
|
||||
name_storagelist.append({'#STORNAME': c.name})
|
||||
elif kwargs['name'] is not None and kwargs['listing'] is True:
|
||||
if c.node == storagenode.name and c.name == kwargs['name']:
|
||||
name_storagelist.append({'#STORNAME': c.name})
|
||||
except KeyError:
|
||||
return [c.name for c in cls.instances if c.node == storagenode.name]
|
||||
return name_storagelist
|
||||
|
||||
def get_storageinfo(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:
|
||||
storage_params = self.__getattribute__(parameter)
|
||||
return storage_params
|
||||
else:
|
||||
storage_params = list()
|
||||
for param in vars(self):
|
||||
if not param.startswith('__'):
|
||||
storage_params.append({'#' + param.upper(): self.__getattribute__(param)})
|
||||
return listofdicts_to_zabbix_json(storage_params)
|
||||
Loading…
Reference in New Issue