def test_metric_extraction(self): """ Extract metrics from WMI query results. """ # local import to avoid pulling in pywintypes ahead of time. from checks.wmi_check import WMIMetric # noqa # Set up the check config = {'instances': [self.WMI_CONFIG]} self.run_check(config) # Retrieve the sampler wmi_sampler = self._get_wmi_sampler() # Extract metrics metrics = self.check._extract_metrics(wmi_sampler, "name", [], ["foobar"]) # Assess expected_metrics = [ WMIMetric("freemegabytes", 19742, ["foobar", "name:c:"]), WMIMetric("avgdiskbytesperwrite", 1536, ["foobar", "name:c:"]), WMIMetric("freemegabytes", 19742, ["foobar", "name:d:"]), WMIMetric("avgdiskbytesperwrite", 1536, ["foobar", "name:d:"]), ] self.assertEquals(metrics, expected_metrics)
def _extract_metrics(self, wmi_sampler, sites, tags): metrics = [] for wmi_obj in wmi_sampler: tags = list(tags) if tags else [] sitename = wmi_obj['Name'] if sitename not in sites: continue elif sitename != "_Total": tags.append("site:{0}".format(self.normalize(sitename))) for wmi_property, wmi_value in wmi_obj.iteritems(): try: metrics.append( WMIMetric(wmi_property, float(wmi_value), tags)) except ValueError: self.log.warning( u"When extracting metrics with WMI, found a non digit value" " for property '{0}'.".format(wmi_property)) continue except TypeError: self.log.warning( u"When extracting metrics with WMI, found a missing property" " '{0}'".format(wmi_property)) continue return metrics
def test_query_tags(self): """ Tag extracted metrics with `tag_queries` queries. """ # local import to avoid pulling in pywintypes ahead of time. from checks.wmi_check import WMIMetric # noqa # Set up the check tag_queries = ["IDProcess", "Win32_Process", "Handle", "CommandLine"] config = {'instances': [self._make_wmi_tag_query_config(tag_queries)]} self.run_check(config) # Retrieve the sampler wmi_sampler = self._get_wmi_sampler() # Extract metrics metrics = self.check._extract_metrics(wmi_sampler, "name", tag_queries=[tag_queries], constant_tags=["foobar"]) # Assess expected_metrics = [ WMIMetric( "ioreadbytespersec", 20455, tags=[ 'foobar', 'commandline:c:\\' 'programfiles(x86)\\google\\chrome\\application\\chrome.exe' ]), WMIMetric( 'idprocess', 4036, tags=[ 'foobar', 'commandline:c:\\' 'programfiles(x86)\\google\\chrome\\application\\chrome.exe' ]), ] self.assertEquals(metrics, expected_metrics)
def _extract_metrics(self, wmi_sampler, sites, instance_tags): """ Extract and tag metrics from the WMISampler. Returns: List of WMIMetric ``` [ WMIMetric("freemegabytes", 19742, ["name:_total"]), WMIMetric("avgdiskbytesperwrite", 1536, ["name:c:"]), ] ``` """ metrics = [] for wmi_obj in wmi_sampler: tags = list(instance_tags) if instance_tags else [] # Get site name sitename = wmi_obj['Name'] # Skip any sites we don't specifically want. if sitename not in sites: continue elif sitename != "_Total": tags.append("site:{0}".format(self.normalize(sitename))) # Tag with `tag_queries` parameter for wmi_property, wmi_value in wmi_obj.iteritems(): # No metric extraction on 'Name' property if wmi_property == 'name': continue try: metrics.append( WMIMetric(wmi_property, float(wmi_value), tags)) except ValueError: self.log.warning( u"When extracting metrics with WMI, found a non digit value" " for property '{0}'.".format(wmi_property)) continue except TypeError: self.log.warning( u"When extracting metrics with WMI, found a missing property" " '{0}'".format(wmi_property)) continue return metrics