def test_raw_initial_sampling(self): """ Query for initial sample for RAW Performance classes. """ wmi_sampler = WMISampler("Win32_PerfRawData_PerfOS_System", ["CounterRawCount", "CounterCounter"]) # noqa wmi_sampler.sample() # 2 queries should have been made: one for initialization, one for sampling self.assertEquals(SWbemServices.ExecQuery.call_count, 2, SWbemServices.ExecQuery.call_count) # Repeat wmi_sampler.sample() self.assertEquals(SWbemServices.ExecQuery.call_count, 3, SWbemServices.ExecQuery.call_count)
def _get_wmi_sampler(self, instance_key, wmi_class, properties, tag_by="", **kwargs): """ Create and cache a WMISampler for the given (class, properties) """ properties = properties + [tag_by] if tag_by else properties if instance_key not in self.wmi_samplers: wmi_sampler = WMISampler(self.log, wmi_class, properties, **kwargs) self.wmi_samplers[instance_key] = wmi_sampler return self.wmi_samplers[instance_key]
def __init__(self, logger): Check.__init__(self, logger) # Sampler(s) self.wmi_sampler = WMISampler( logger, "Win32_PerfRawData_PerfDisk_LogicalDisk", [ "Name", "DiskWriteBytesPerSec", "DiskWritesPerSec", "DiskReadBytesPerSec", "DiskReadsPerSec", "CurrentDiskQueueLength" ]) self.gauge('system.io.wkb_s') self.gauge('system.io.w_s') self.gauge('system.io.rkb_s') self.gauge('system.io.r_s') self.gauge('system.io.avg_q_sz')
def test_wmi_connection(self): """ Establish a WMI connection to the specified host/namespace, with the right credentials. """ wmi_sampler = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"], host="myhost", namespace="some/namespace", username="******", password="******") # Request a connection but do nothing wmi_sampler.get_connection() # Connection was established with the right parameters self.assertWMIConn(wmi_sampler, param="myhost") self.assertWMIConn(wmi_sampler, param="some/namespace")
def test_raw_cache_qualifiers(self): """ Cache the qualifiers on the first query against RAW Performance classes. """ # Append `flag_use_amended_qualifiers` flag on the first query wmi_raw_sampler = WMISampler("Win32_PerfRawData_PerfOS_System", ["CounterRawCount", "CounterCounter"]) # noqa wmi_raw_sampler._query() self.assertWMIQuery(flags=131120) wmi_raw_sampler._query() self.assertWMIQuery(flags=48) # Qualifiers are cached self.assertTrue(wmi_raw_sampler.property_counter_types) self.assertIn('CounterRawCount', wmi_raw_sampler.property_counter_types) self.assertIn('CounterCounter', wmi_raw_sampler.property_counter_types)
def test_wmi_sampler_iterator_getter(self): """ Iterate/Get on the WMISampler object iterates/gets on its current sample. """ wmi_sampler = WMISampler("Win32_PerfFormattedData_PerfDisk_LogicalDisk", ["AvgDiskBytesPerWrite", "FreeMegabytes"]) wmi_sampler.sample() self.assertEquals(len(wmi_sampler), 2) # Using an iterator for wmi_obj in wmi_sampler: self.assertWMIObject(wmi_obj, ["AvgDiskBytesPerWrite", "FreeMegabytes", "name"]) # Using an accessor for index in xrange(0, 2): self.assertWMIObject(wmi_sampler[index], ["AvgDiskBytesPerWrite", "FreeMegabytes", "name"])
def test_raw_properties_fallback(self): """ Print a warning on RAW Performance classes if the calculator is undefined. Returns the original RAW value. """ from checks.libs.wmi.sampler import WMISampler logger = Mock() wmi_raw_sampler = WMISampler(logger, "Win32_PerfRawData_PerfOS_System", ["UnknownCounter", "MissingProperty"]) # noqa wmi_raw_sampler.sample() self.assertEquals(len(wmi_raw_sampler), 2) for wmi_obj in wmi_raw_sampler: self.assertWMIObject(wmi_obj, ["UnknownCounter", "Timestamp_Sys100NS", "Frequency_Sys100NS", "name"]) # noqa self.assertEquals(wmi_obj['UnknownCounter'], 999) self.assertTrue(logger.warning.called)
def test_raw_properties_formatting(self): """ WMI Object's RAW data are returned formatted. """ wmi_raw_sampler = WMISampler("Win32_PerfRawData_PerfOS_System", ["CounterRawCount", "CounterCounter"]) # noqa wmi_raw_sampler.sample() self.assertEquals(len(wmi_raw_sampler), 2) # Using an iterator for wmi_obj in wmi_raw_sampler: self.assertWMIObject(wmi_obj, ["CounterRawCount", "CounterCounter", "Timestamp_Sys100NS", "Frequency_Sys100NS", "name"]) # noqa self.assertEquals(wmi_obj['CounterRawCount'], 500) self.assertEquals(wmi_obj['CounterCounter'], 50) # Using an accessor for index in xrange(0, 2): self.assertWMIObject(wmi_raw_sampler[index], ["CounterRawCount", "CounterCounter", "Timestamp_Sys100NS", "Frequency_Sys100NS", "name"]) # noqa self.assertEquals(wmi_raw_sampler[index]['CounterRawCount'], 500) self.assertEquals(wmi_raw_sampler[index]['CounterCounter'], 50)
def test_wmi_connection(self): """ Establish a WMI connection to the specified host/namespace, with the right credentials. """ wmi_sampler = WMISampler( "Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"], host="myhost", namespace="some/namespace", username="******", password="******" ) wmi_conn = wmi_sampler._get_connection() # WMI connection is cached self.assertIn('myhost:some/namespace:datadog', wmi_sampler._wmi_connections) # Connection was established with the right parameters self.assertWMIConnWith(wmi_sampler, "myhost") self.assertWMIConnWith(wmi_sampler, "some/namespace")
def test_wmi_parser(self): """ Parse WMI objects from WMI query results. """ wmi_sampler = WMISampler( "Win32_PerfFormattedData_PerfDisk_LogicalDisk", ["AvgDiskBytesPerWrite", "FreeMegabytes"]) wmi_sampler.sample() # Assert `results` expected_results = [{ 'freemegabytes': 19742.0, 'name': 'C:', 'avgdiskbytesperwrite': 1536.0 }, { 'freemegabytes': 19742.0, 'name': 'D:', 'avgdiskbytesperwrite': 1536.0 }] self.assertEquals(wmi_sampler, expected_results, wmi_sampler)
def _get_tag_query_tag(self, sampler, wmi_obj, tag_query): """ Design a query based on the given WMIObject to extract a tag. Returns: tag or TagQueryUniquenessFailure exception. """ self.log.debug( u"`tag_queries` parameter found." " wmi_object={wmi_obj} - query={tag_query}".format( wmi_obj=wmi_obj, tag_query=tag_query, ) ) # Extract query information target_class, target_property, filters = \ self._format_tag_query(sampler, wmi_obj, tag_query) # Create a specific sampler connection = sampler.get_connection() tag_query_sampler = WMISampler( self.log, target_class, [target_property], filters=filters, **connection ) tag_query_sampler.sample() # Extract tag self._raise_on_invalid_tag_query_result(tag_query_sampler, wmi_obj, tag_query) link_value = str(tag_query_sampler[0][target_property]).lower() tag = "{tag_name}:{tag_value}".format( tag_name=target_property.lower(), tag_value="_".join(link_value.split()) ) self.log.debug(u"Extracted `tag_queries` tag: '{tag}'".format(tag=tag)) return tag