From 2962980d47a370fc78129379a3ce2cf43e5d1e65 Mon Sep 17 00:00:00 2001 From: John Zeller Date: Mon, 2 May 2016 18:21:00 +0100 Subject: [PATCH 1/2] [integration][vsphere] Add optional vm include parameter --- checks.d/vsphere.py | 30 ++++++++++++++++++++++++------ conf.d/vsphere.yaml.example | 7 +++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/checks.d/vsphere.py b/checks.d/vsphere.py index c77b3d51f2..a1161da1f6 100644 --- a/checks.d/vsphere.py +++ b/checks.d/vsphere.py @@ -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 @@ -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 @@ -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'), @@ -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: @@ -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': @@ -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': @@ -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': @@ -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': @@ -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 _is_affirmative(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) @@ -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 = 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() diff --git a/conf.d/vsphere.yaml.example b/conf.d/vsphere.yaml.example index db7a931d05..c8a50cb0ec 100644 --- a/conf.d/vsphere.yaml.example +++ b/conf.d/vsphere.yaml.example @@ -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 | 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 From 27702539da313965b854162d1640f63c030327b9 Mon Sep 17 00:00:00 2001 From: John Zeller Date: Wed, 18 May 2016 15:01:19 -0400 Subject: [PATCH 2/2] Small modification from nit --- checks.d/vsphere.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checks.d/vsphere.py b/checks.d/vsphere.py index a1161da1f6..09324000c0 100644 --- a/checks.d/vsphere.py +++ b/checks.d/vsphere.py @@ -609,7 +609,7 @@ def _cache_morlist_raw_atomic(self, i_key, obj_type, obj, tags, regexes=None, in return # Also, if include_only_marked is true, then check if there exists a # custom field with the value DatadogMonitored - if _is_affirmative(include_only_marked): + if include_only_marked: monitored = False for field in obj.customValue: if field.value == VM_MONITORING_FLAG: @@ -650,7 +650,7 @@ 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 = instance.get('include_only_marked', False) + 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, include_only_marked)