Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using only allowed customAttributes #342

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions vmware_exporter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ def get_bool_env(key: str, default: bool):
value = os.environ.get(key, default)
return value if type(value) == bool else value.lower() == 'true'

def get_list_env(key: str):
from_env = os.environ.get(key, "")
return from_env.split(",")

def batch_fetch_properties(content, obj_type, properties):
view_ref = content.viewManager.CreateContainerView(
Expand Down
27 changes: 26 additions & 1 deletion vmware_exporter/vmware_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from prometheus_client.core import GaugeMetricFamily
from prometheus_client import CollectorRegistry, generate_latest

from .helpers import batch_fetch_properties, get_bool_env
from .helpers import batch_fetch_properties, get_bool_env, get_list_env
from .defer import parallelize, run_once_property

from .__init__ import __version__
Expand All @@ -62,6 +62,7 @@ def __init__(
collect_only,
specs_size,
fetch_custom_attributes=False,
custom_attributes_allowed=[],
ignore_ssl=False,
fetch_tags=False,
fetch_alarms=False
Expand All @@ -79,6 +80,7 @@ def __init__(
# Custom Attributes
# flag to wheter fetch custom attributes or not
self.fetch_custom_attributes = fetch_custom_attributes
self.custom_attributes_allowed = custom_attributes_allowed
# vms, hosts and datastores custom attributes must be stored by their moid
self._vmsCustomAttributes = {}
self._hostsCustomAttributes = {}
Expand Down Expand Up @@ -656,6 +658,11 @@ def datastore_inventory(self):
for ds_moId, ds in datastores.items()
]
)
if len(self.custom_attributes_allowed):
for k,v in self._datastoresCustomAttributes.items():
for attr in list(v.keys()):
if attr not in self.custom_attributes_allowed:
del v[attr]

fetch_time = datetime.datetime.utcnow() - start
logging.info("Fetched vim.Datastore inventory ({fetch_time})".format(fetch_time=fetch_time))
Expand Down Expand Up @@ -722,6 +729,11 @@ def host_system_inventory(self):
for host_moId, host in host_systems.items()
]
)
if len(self.custom_attributes_allowed):
for k,v in self._hostsCustomAttributes.items():
for attr in list(v.keys()):
if attr not in self.custom_attributes_allowed:
del v[attr]

fetch_time = datetime.datetime.utcnow() - start
logging.info("Fetched vim.HostSystem inventory ({fetch_time})".format(fetch_time=fetch_time))
Expand Down Expand Up @@ -791,6 +803,11 @@ def vm_inventory(self):
for vm_moId, vm in virtual_machines.items()
]
)
if len(self.custom_attributes_allowed):
for k,v in self._vmsCustomAttributes.items():
for attr in list(v.keys()):
if attr not in self.custom_attributes_allowed:
del v[attr]

fetch_time = datetime.datetime.utcnow() - start
logging.info("Fetched vim.VirtualMachine inventory ({fetch_time})".format(fetch_time=fetch_time))
Expand Down Expand Up @@ -1888,6 +1905,7 @@ def configure(self, args):
'ignore_ssl': get_bool_env('VSPHERE_IGNORE_SSL', False),
'specs_size': os.environ.get('VSPHERE_SPECS_SIZE', 5000),
'fetch_custom_attributes': get_bool_env('VSPHERE_FETCH_CUSTOM_ATTRIBUTES', False),
'custom_attributes_allowed': get_list_env('VSPHERE_CUSTOM_ATTRIBUTES_ALLOWED'),
'fetch_tags': get_bool_env('VSPHERE_FETCH_TAGS', False),
'fetch_alarms': get_bool_env('VSPHERE_FETCH_ALARMS', False),
'collect_only': {
Expand Down Expand Up @@ -1915,6 +1933,7 @@ def configure(self, args):
'ignore_ssl': get_bool_env('VSPHERE_{}_IGNORE_SSL'.format(section), False),
'specs_size': os.environ.get('VSPHERE_{}_SPECS_SIZE'.format(section), 5000),
'fetch_custom_attributes': get_bool_env('VSPHERE_{}_FETCH_CUSTOM_ATTRIBUTES'.format(section), False),
'custom_attributes_allowed': get_list_env('VSPHERE_{}_CUSTOM_ATTRIBUTES_ALLOWED'),
'fetch_tags': get_bool_env('VSPHERE_{}_FETCH_TAGS'.format(section), False),
'fetch_alarms': get_bool_env('VSPHERE_{}_FETCH_ALARMS'.format(section), False),
'collect_only': {
Expand Down Expand Up @@ -1966,13 +1985,19 @@ def generate_latest_metrics(self, request):
request.finish()
return

if self.config[section].get('custom_attributes_allowed') and self.config[section].get('custom_attributes_allowed') != "None":
custom_attributes_allowed = self.config[section].get('custom_attributes_allowed')
else:
custom_attributes_allowed=[]

collector = VmwareCollector(
vsphere_host,
self.config[section]['vsphere_user'],
self.config[section]['vsphere_password'],
self.config[section]['collect_only'],
self.config[section]['specs_size'],
self.config[section]['fetch_custom_attributes'],
custom_attributes_allowed,
self.config[section]['ignore_ssl'],
self.config[section]['fetch_tags'],
self.config[section]['fetch_alarms'],
Expand Down