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

[integration][vsphere] Add optional vm include parameter #2459

Merged
merged 2 commits into from
May 18, 2016
Merged
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
30 changes: 24 additions & 6 deletions checks.d/vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pyVmomi import vim # pylint: disable=E0611

# project
from config import _is_affirmative
from checks import AgentCheck
from checks.libs.thread_pool import Pool
from checks.libs.vmware.basic_metrics import BASIC_METRICS
Expand All @@ -26,6 +27,8 @@
SOURCE_TYPE = 'vsphere'
REAL_TIME_INTERVAL = 20 # Default vCenter sampling interval

# Metrics are only collected on vSphere VMs marked by custom field value
VM_MONITORING_FLAG = 'DatadogMonitored'
# The size of the ThreadPool used to process the request queue
DEFAULT_SIZE_POOL = 4
# The interval in seconds between two refresh of the entities list
Expand Down Expand Up @@ -455,6 +458,8 @@ def _get_server_instance(self, instance):

if i_key not in self.server_instances:
try:
# Object returned by SmartConnect is a ServerInstance
# https://www.vmware.com/support/developer/vc-sdk/visdk2xpubs/ReferenceGuide/vim.ServiceInstance.html
server_instance = connect.SmartConnect(
host = instance.get('host'),
user = instance.get('username'),
Expand Down Expand Up @@ -519,7 +524,7 @@ def get_external_host_tags(self):
return external_host_tags

@atomic_method
def _cache_morlist_raw_atomic(self, i_key, obj_type, obj, tags, regexes=None):
def _cache_morlist_raw_atomic(self, i_key, obj_type, obj, tags, regexes=None, include_only_marked=False):
""" Compute tags for a single node in the vCenter rootFolder
and queue other such jobs for children nodes.
Usual hierarchy:
Expand Down Expand Up @@ -549,7 +554,7 @@ def _cache_morlist_raw_atomic(self, i_key, obj_type, obj, tags, regexes=None):
continue
self.pool.apply_async(
self._cache_morlist_raw_atomic,
args=(i_key, 'datacenter', datacenter, tags_copy, regexes)
args=(i_key, 'datacenter', datacenter, tags_copy, regexes, include_only_marked)
)

elif obj_type == 'datacenter':
Expand All @@ -561,7 +566,7 @@ def _cache_morlist_raw_atomic(self, i_key, obj_type, obj, tags, regexes=None):
continue
self.pool.apply_async(
self._cache_morlist_raw_atomic,
args=(i_key, 'compute_resource', compute_resource, tags_copy, regexes)
args=(i_key, 'compute_resource', compute_resource, tags_copy, regexes, include_only_marked)
)

elif obj_type == 'compute_resource':
Expand All @@ -574,7 +579,7 @@ def _cache_morlist_raw_atomic(self, i_key, obj_type, obj, tags, regexes=None):
continue
self.pool.apply_async(
self._cache_morlist_raw_atomic,
args=(i_key, 'host', host, tags_copy, regexes)
args=(i_key, 'host', host, tags_copy, regexes, include_only_marked)
)

elif obj_type == 'host':
Expand All @@ -593,7 +598,7 @@ def _cache_morlist_raw_atomic(self, i_key, obj_type, obj, tags, regexes=None):
continue
self.pool.apply_async(
self._cache_morlist_raw_atomic,
args=(i_key, 'vm', vm, tags_copy, regexes)
args=(i_key, 'vm', vm, tags_copy, regexes, include_only_marked)
)

elif obj_type == 'vm':
Expand All @@ -602,6 +607,18 @@ def _cache_morlist_raw_atomic(self, i_key, obj_type, obj, tags, regexes=None):
if not match:
self.log.debug(u"Filtered out VM {0} because of vm_include_only_regex".format(obj.name))
return
# Also, if include_only_marked is true, then check if there exists a
# custom field with the value DatadogMonitored
if include_only_marked:
monitored = False
for field in obj.customValue:
if field.value == VM_MONITORING_FLAG:
monitored = True
break # we shall monitor
if not monitored:
self.log.debug(u"Filtered out VM {0} because of include_only_marked".format(obj.name))
return

watched_mor = dict(mor_type='vm', mor=obj, hostname=obj.name, tags=tags_copy+['vsphere_type:vm'])
self.morlist_raw[i_key].append(watched_mor)

Expand Down Expand Up @@ -633,9 +650,10 @@ def _cache_morlist_raw(self, instance):
'host_include': instance.get('host_include_only_regex'),
'vm_include': instance.get('vm_include_only_regex')
}
include_only_marked = _is_affirmative(instance.get('include_only_marked', False))
self.pool.apply_async(
self._cache_morlist_raw_atomic,
args=(i_key, 'rootFolder', root_folder, [instance_tag], regexes)
args=(i_key, 'rootFolder', root_folder, [instance_tag], regexes, include_only_marked)
)
self.cache_times[i_key][MORLIST][LAST] = time.time()

Expand Down
7 changes: 7 additions & 0 deletions conf.d/vsphere.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ instances:
# optional
# vm_include_only_regex: ".*\.sql\.datadoghq\.com"

# Set to true if you'd like to only collect metrics on vSphere VMs which
# are marked by a custom field with the value 'DatadogMonitored'
# To set this custom field with PowerCLI, use the follow command:
# Get-VM <MyVMName> | Set-CustomField -Name "DatadogMonitored" -Value "DatadogMonitored"
# optional
# include_only_marked: false

# When set to true, this will collect EVERY metric
# from vCenter, which means a LOT of metrics you probably
# do not care about. We have selected a set of metrics
Expand Down