Example #1
0
class Network(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.wmi_sampler = WMISampler(
            logger,
            "Win32_PerfRawData_Tcpip_NetworkInterface",
            ["Name", "BytesReceivedPerSec", "BytesSentPerSec"]
        )

        self.gauge('system.net.bytes_rcvd')
        self.gauge('system.net.bytes_sent')

    def check(self, agentConfig):
        self.wmi_sampler.sample()

        if not (len(self.wmi_sampler)):
            self.logger.info('Missing Win32_PerfRawData_Tcpip_NetworkInterface WMI class.'
                             ' No network metrics will be returned')
            return

        for iface in self.wmi_sampler:
            name = iface.get('Name')
            bytes_received_per_sec = iface.get('BytesReceivedPerSec')
            bytes_sent_per_sec = iface.get('BytesSentPerSec')

            name = self.normalize_device_name(name)
            if bytes_received_per_sec is not None:
                self.save_sample('system.net.bytes_rcvd', bytes_received_per_sec,
                                 device_name=name)
            if bytes_sent_per_sec is not None:
                self.save_sample('system.net.bytes_sent', bytes_sent_per_sec,
                                 device_name=name)
        return self.get_metrics()
Example #2
0
    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
        tag_query_sampler = WMISampler(self.log, target_class, [target_property], filters=filters, **sampler.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
Example #3
0
    def _get_tag_query_tag(self, sampler, wmi_obj, tag_query):

        self.log.debug(u"`tag_queries` parameter found."
                       " wmi_object={wmi_obj} - query={tag_query}".format(
                           wmi_obj=wmi_obj,
                           tag_query=tag_query,
                       ))

        target_class, target_property, filters = \
            self._format_tag_query(sampler, wmi_obj, tag_query)

        tag_query_sampler = WMISampler(self.log,
                                       target_class, [target_property],
                                       filters=filters,
                                       **sampler.connection)

        tag_query_sampler.sample()

        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
Example #4
0
class Cpu(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.wmi_sampler = WMISampler(logger, "Win32_PerfRawData_PerfOS_Processor", ["Name", "PercentInterruptTime"])

        self.counter("system.cpu.user")
        self.counter("system.cpu.idle")
        self.gauge("system.cpu.interrupt")
        self.counter("system.cpu.system")

    def check(self, agentConfig):
        try:
            self.wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_PerfOS_Processor WMI class."
                u" CPU metrics will be returned at next iteration."
            )
            return []

        if not (len(self.wmi_sampler)):
            self.logger.warning(
                "Missing Win32_PerfRawData_PerfOS_Processor WMI class." " No CPU metrics will be returned"
            )
            return []

        cpu_interrupt = self._average_metric(self.wmi_sampler, "PercentInterruptTime")
        if cpu_interrupt is not None:
            self.save_sample("system.cpu.interrupt", cpu_interrupt)

        cpu_percent = psutil.cpu_times()

        self.save_sample("system.cpu.user", 100 * cpu_percent.user / psutil.cpu_count())
        self.save_sample("system.cpu.idle", 100 * cpu_percent.idle / psutil.cpu_count())
        self.save_sample("system.cpu.system", 100 * cpu_percent.system / psutil.cpu_count())

        return self.get_metrics()

    def _average_metric(self, sampler, wmi_prop):
        """ Sum all of the values of a metric from a WMI class object, excluding
            the value for "_Total"
        """
        val = 0
        counter = 0
        for wmi_object in sampler:
            if wmi_object["Name"] == "_Total":
                # Skip the _Total value
                continue

            wmi_prop_value = wmi_object.get(wmi_prop)
            if wmi_prop_value is not None:
                counter += 1
                val += float(wmi_prop_value)

        if counter > 0:
            return val / counter

        return val
Example #5
0
    def test_wmi_sampler_timeout(self):
        """
        Gracefully handle WMI query timeouts.
        """
        from checks.libs.wmi.sampler import WMISampler
        logger = Mock()

        # Create a sampler that timeouts
        wmi_sampler = WMISampler(logger, "Win32_PerfFormattedData_PerfDisk_LogicalDisk",
                                 ["AvgDiskBytesPerWrite", "FreeMegabytes"],
                                 timeout_duration=0.1)
        SWbemServices._exec_query_run_time = 0.11

        # `TimeoutException` exception is raised, DEBUG message logged
        self.assertRaises(TimeoutException, wmi_sampler.sample)
        self.assertTrue(wmi_sampler._sampling)
        self.assertTrue(logger.debug.called)

        # Cannot iterate on data
        self.assertRaises(TypeError, lambda: len(wmi_sampler))
        self.assertRaises(TypeError, lambda: sum(1 for _ in wmi_sampler))

        # Recover from timeout at next iteration
        wmi_sampler.sample()
        self.assertFalse(wmi_sampler._sampling)

        # The existing query was retrieved
        self.assertEquals(SWbemServices.ExecQuery.call_count, 1, SWbemServices.ExecQuery.call_count)

        # Data is populated
        self.assertEquals(len(wmi_sampler), 2)
        self.assertEquals(sum(1 for _ in wmi_sampler), 2)
Example #6
0
    def test_wmi_sampler_timeout(self):
        """
        Gracefully handle WMI query timeouts.
        """
        from checks.libs.wmi.sampler import WMISampler
        logger = Mock()

        # Create a sampler that timeouts
        wmi_sampler = WMISampler(
            logger,
            "Win32_PerfFormattedData_PerfDisk_LogicalDisk",
            ["AvgDiskBytesPerWrite", "FreeMegabytes"],
            timeout_duration=0.5)
        SWbemServices._exec_query_run_time = 0.5

        # `TimeoutException` exception is raised, DEBUG message logged
        self.assertRaises(TimeoutException, wmi_sampler.sample)
        self.assertTrue(wmi_sampler._sampling)
        self.assertTrue(logger.debug.called)

        # Cannot iterate on data
        self.assertRaises(TypeError, lambda: len(wmi_sampler))
        self.assertRaises(TypeError, lambda: sum(1 for _ in wmi_sampler))

        # Recover from timeout at next iteration
        wmi_sampler.sample()
        self.assertFalse(wmi_sampler._sampling)

        # The existing query was retrieved
        self.assertEquals(SWbemServices.ExecQuery.call_count, 1,
                          SWbemServices.ExecQuery.call_count)

        # Data is populated
        self.assertEquals(len(wmi_sampler), 2)
        self.assertEquals(sum(1 for _ in wmi_sampler), 2)
Example #7
0
class Processes(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.wmi_sampler = WMISampler(
            logger,
            "Win32_PerfRawData_PerfOS_System",
            ["ProcessorQueueLength", "Processes"]
        )

        self.gauge('system.proc.queue_length')
        self.gauge('system.proc.count')

    def check(self, agentConfig):
        self.wmi_sampler.sample()

        if not (len(self.wmi_sampler)):
            self.logger.info('Missing Win32_PerfRawData_PerfOS_System WMI class.'
                             ' No process metrics will be returned.')
            return

        os = self.wmi_sampler[0]
        processor_queue_length = os.get('ProcessorQueueLength')
        processes = os.get('Processes')

        if processor_queue_length is not None:
            self.save_sample('system.proc.queue_length', processor_queue_length)
        if processes is not None:
            self.save_sample('system.proc.count', processes)

        return self.get_metrics()
Example #8
0
    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)
Example #9
0
    def test_wmi_sampler_timeout(self):
        """
        Gracefully handle WMI queries' timeouts.
        """
        from checks.libs.wmi.sampler import WMISampler
        logger = Mock()

        # Create a sampler that timeouts
        wmi_sampler = WMISampler(logger, "Win32_PerfFormattedData_PerfDisk_LogicalDisk",
                                 ["AvgDiskBytesPerWrite", "FreeMegabytes"],
                                 timeout_duration=0.5)
        SWbemServices._exec_query_run_time = 0.5

        # Gracefully timeout with a warning message but no exception
        wmi_sampler.sample()
        self.assertTrue(wmi_sampler._sampling)
        self.assertTrue(logger.warning.called)

        # Show no data
        self.assertEquals(len(wmi_sampler), 0)
        self.assertEquals(sum(1 for _ in wmi_sampler), 0)

        # Recover from timeout at next iteration
        wmi_sampler.sample()
        self.assertFalse(wmi_sampler._sampling)

        # The existing query was retrieved
        self.assertEquals(SWbemServices.ExecQuery.call_count, 1, SWbemServices.ExecQuery.call_count)

        # Data is populated
        self.assertEquals(len(wmi_sampler), 2)
        self.assertEquals(sum(1 for _ in wmi_sampler), 2)
Example #10
0
    def test_missing_property(self):
        """
        Do not raise on missing properties but backfill with empty values.
        """
        wmi_raw_sampler = WMISampler("Win32_PerfRawData_PerfOS_System", ["UnknownCounter", "MissingProperty"])  # noqa
        wmi_raw_sampler.sample()

        self.assertWMISampler(wmi_raw_sampler, ["MissingProperty"], count=1)
Example #11
0
    def test_missing_property(self):
        """
        Do not raise on missing properties but backfill with empty values.
        """
        wmi_raw_sampler = WMISampler("Win32_PerfRawData_PerfOS_System", ["UnknownCounter", "MissingProperty"])  # noqa
        wmi_raw_sampler.sample()

        self.assertWMISampler(wmi_raw_sampler, ["MissingProperty"], count=1)
Example #12
0
class Cpu(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.wmi_sampler = WMISampler(
            logger,
            "Win32_PerfRawData_PerfOS_Processor",
            ["Name", "PercentInterruptTime"]
        )

        self.counter('system.cpu.user')
        self.counter('system.cpu.idle')
        self.gauge('system.cpu.interrupt')
        self.counter('system.cpu.system')

    def check(self, agentConfig):

        self.wmi_sampler.sample()

        if not (len(self.wmi_sampler)):
            self.logger.info('Missing Win32_PerfRawData_PerfOS_Processor WMI class.'
                             ' No CPU metrics will be returned')
            return

        cpu_interrupt = self._average_metric(self.wmi_sampler, 'PercentInterruptTime')
        if cpu_interrupt is not None:
            self.save_sample('system.cpu.interrupt', cpu_interrupt)

        cpu_percent = psutil.cpu_times()

        self.save_sample('system.cpu.user', 100 * cpu_percent.user / psutil.NUM_CPUS)
        self.save_sample('system.cpu.idle', 100 * cpu_percent.idle / psutil.NUM_CPUS)
        self.save_sample('system.cpu.system', 100 * cpu_percent.system / psutil.NUM_CPUS)

        return self.get_metrics()

    def _average_metric(self, sampler, wmi_prop):
        ''' Sum all of the values of a metric from a WMI class object, excluding
            the value for "_Total"
        '''
        val = 0
        counter = 0
        for wmi_object in sampler:
            if wmi_object['Name'] == '_Total':
                # Skip the _Total value
                continue

            wmi_prop_value = wmi_object.get(wmi_prop)
            if wmi_prop_value is not None:
                counter += 1
                val += float(wmi_prop_value)

        if counter > 0:
            return val / counter

        return val
Example #13
0
class IO(Check):
    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 check(self, agentConfig):
        self.wmi_sampler.sample()

        if not (len(self.wmi_sampler)):
            self.logger.info('Missing Win32_PerfRawData_PerfDisk_LogicalDiskUnable WMI class.'
                             ' No I/O metrics will be returned.')
            return

        blacklist_re = agentConfig.get('device_blacklist_re', None)
        for device in self.wmi_sampler:
            name = device.get('Name')
            disk_write_bytes_per_sec = device.get('DiskWriteBytesPerSec')
            disk_writes_per_sec = device.get('DiskWritesPerSec')
            disk_read_bytes_per_sec = device.get('DiskReadBytesPerSec')
            disk_reads_per_sec = device.get('DiskReadsPerSec')
            current_disk_queue_length = device.get('CurrentDiskQueueLength')

            name = self.normalize_device_name(name)
            if should_ignore_disk(name, blacklist_re):
                continue
            if disk_write_bytes_per_sec is not None:
                self.save_sample('system.io.wkb_s', int(disk_write_bytes_per_sec) / B2KB,
                                 device_name=name)
            if disk_writes_per_sec is not None:
                self.save_sample('system.io.w_s', int(disk_writes_per_sec),
                                 device_name=name)
            if disk_read_bytes_per_sec is not None:
                self.save_sample('system.io.rkb_s', int(disk_read_bytes_per_sec) / B2KB,
                                 device_name=name)
            if disk_reads_per_sec is not None:
                self.save_sample('system.io.r_s', int(disk_reads_per_sec),
                                 device_name=name)
            if current_disk_queue_length is not None:
                self.save_sample('system.io.avg_q_sz', current_disk_queue_length,
                                 device_name=name)
        return self.get_metrics()
Example #14
0
    def test_missing_property(self):
        """
        Do not raise on missing properties.
        """
        wmi_raw_sampler = WMISampler("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:
            # Access a non existent property
            self.assertFalse(wmi_obj['MissingProperty'])
Example #15
0
    def test_missing_property(self):
        """
        Do not raise on missing properties.
        """
        wmi_raw_sampler = WMISampler("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:
            # Access a non existent property
            self.assertFalse(wmi_obj['MissingProperty'])
Example #16
0
    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.assertWMISampler(
            wmi_raw_sampler,
            [("CounterRawCount", 500), ("CounterCounter", 50), "Timestamp_Sys100NS", "Frequency_Sys100NS", "name"],
            count=2,
        )
Example #17
0
    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)
Example #18
0
    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)
Example #19
0
    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.assertWMISampler(wmi_raw_sampler,
                              [("CounterRawCount", 500),
                               ("CounterCounter", 50), "Timestamp_Sys100NS",
                               "Frequency_Sys100NS", "name"],
                              count=2)
Example #20
0
    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)
Example #21
0
    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"])
Example #22
0
    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"])
Example #23
0
    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)
Example #24
0
    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)
Example #25
0
    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)
Example #26
0
class Network(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        self.wmi_sampler = WMISampler(
            logger, "Win32_PerfRawData_Tcpip_NetworkInterface",
            ["Name", "BytesReceivedPerSec", "BytesSentPerSec"])

        self.gauge('system.net.bytes_rcvd')
        self.gauge('system.net.bytes_sent')

    def check(self, agentConfig):
        try:
            self.wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_Tcpip_NetworkInterface WMI class."
                u" Network metrics will be returned at next iteration.")
            return []

        if not (len(self.wmi_sampler)):
            self.logger.warning(
                'Missing Win32_PerfRawData_Tcpip_NetworkInterface WMI class.'
                ' No network metrics will be returned')
            return []

        for iface in self.wmi_sampler:
            name = iface.get('Name')
            bytes_received_per_sec = iface.get('BytesReceivedPerSec')
            bytes_sent_per_sec = iface.get('BytesSentPerSec')

            name = self.normalize_device_name(name)
            if bytes_received_per_sec is not None:
                self.save_sample('system.net.bytes_rcvd',
                                 bytes_received_per_sec,
                                 device_name=name)
            if bytes_sent_per_sec is not None:
                self.save_sample('system.net.bytes_sent',
                                 bytes_sent_per_sec,
                                 device_name=name)
        return self.get_metrics()
Example #27
0
    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)
Example #28
0
    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
Example #29
0
    def test_no_wmi_connection_pooling(self):
        """
        WMI connections are not be shared among WMISampler objects.
        """
        from win32com.client import Dispatch

        wmi_sampler_1 = WMISampler("Win32_PerfRawData_PerfOS_System",
                                   ["ProcessorQueueLength"])
        wmi_sampler_2 = WMISampler("Win32_OperatingSystem",
                                   ["TotalVisibleMemorySize"])
        wmi_sampler_3 = WMISampler("Win32_PerfRawData_PerfOS_System",
                                   ["ProcessorQueueLength"],
                                   host="myhost")  # noqa

        wmi_sampler_1.sample()
        wmi_sampler_2.sample()

        # 3 conns have been opened, 2 for the raw sampler and 1 for the other sampler
        self.assertEquals(Dispatch.ConnectServer.call_count, 3,
                          Dispatch.ConnectServer.call_count)

        wmi_sampler_3.sample()

        # 5 conns now
        self.assertEquals(Dispatch.ConnectServer.call_count, 5,
                          Dispatch.ConnectServer.call_count)
Example #30
0
class Processes(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.wmi_sampler = WMISampler(
            logger,
            "Win32_PerfRawData_PerfOS_System",
            ["ProcessorQueueLength", "Processes"]
        )

        self.gauge('system.proc.queue_length')
        self.gauge('system.proc.count')

    def check(self, agentConfig):
        try:
            self.wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_PerfOS_System WMI class."
                u" Processes metrics will be returned at next iteration."
            )
            return []

        if not (len(self.wmi_sampler)):
            self.logger.warning('Missing Win32_PerfRawData_PerfOS_System WMI class.'
                             ' No process metrics will be returned.')
            return []

        os = self.wmi_sampler[0]
        processor_queue_length = os.get('ProcessorQueueLength')
        processes = os.get('Processes')

        if processor_queue_length is not None:
            self.save_sample('system.proc.queue_length', processor_queue_length)
        if processes is not None:
            self.save_sample('system.proc.count', processes)

        return self.get_metrics()
Example #31
0
class Network(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.wmi_sampler = WMISampler(
            logger, "Win32_PerfRawData_Tcpip_NetworkInterface", ["Name", "BytesReceivedPerSec", "BytesSentPerSec"]
        )

        self.gauge("system.net.bytes_rcvd")
        self.gauge("system.net.bytes_sent")

    def check(self, agentConfig):
        try:
            self.wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_Tcpip_NetworkInterface WMI class."
                u" Network metrics will be returned at next iteration."
            )
            return []

        if not (len(self.wmi_sampler)):
            self.logger.warning(
                "Missing Win32_PerfRawData_Tcpip_NetworkInterface WMI class." " No network metrics will be returned"
            )
            return []

        for iface in self.wmi_sampler:
            name = iface.get("Name")
            bytes_received_per_sec = iface.get("BytesReceivedPerSec")
            bytes_sent_per_sec = iface.get("BytesSentPerSec")

            name = self.normalize_device_name(name)
            if bytes_received_per_sec is not None:
                self.save_sample("system.net.bytes_rcvd", bytes_received_per_sec, device_name=name)
            if bytes_sent_per_sec is not None:
                self.save_sample("system.net.bytes_sent", bytes_sent_per_sec, device_name=name)
        return self.get_metrics()
Example #32
0
class Processes(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.wmi_sampler = WMISampler(
            logger,
            "Win32_PerfRawData_PerfOS_System",
            ["ProcessorQueueLength", "Processes"]
        )

        self.gauge('system.proc.queue_length')
        self.gauge('system.proc.count')

    def check(self, agentConfig):
        try:
            self.wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_PerfOS_System WMI class."
                u" Processes metrics will be returned at next iteration."
            )
            return []

        if not (len(self.wmi_sampler)):
            self.logger.warning('Missing Win32_PerfRawData_PerfOS_System WMI class.'
                             ' No process metrics will be returned.')
            return []

        os = self.wmi_sampler[0]
        processor_queue_length = os.get('ProcessorQueueLength')
        processes = os.get('Processes')

        if processor_queue_length is not None:
            self.save_sample('system.proc.queue_length', processor_queue_length)
        if processes is not None:
            self.save_sample('system.proc.count', processes)

        return self.get_metrics()
Example #33
0
    def test_wmi_connection_pooling(self):
        """
        Share WMI connections among WMISampler objects.
        """
        from win32com.client import Dispatch

        wmi_sampler_1 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"])
        wmi_sampler_2 = WMISampler("Win32_OperatingSystem", ["TotalVisibleMemorySize"])
        wmi_sampler_3 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"], host="myhost")  # noqa

        wmi_sampler_1.sample()
        wmi_sampler_2.sample()

        self.assertEquals(Dispatch.ConnectServer.call_count, 1, Dispatch.ConnectServer.call_count)

        wmi_sampler_3.sample()

        self.assertEquals(Dispatch.ConnectServer.call_count, 2, Dispatch.ConnectServer.call_count)
Example #34
0
    def test_wmi_connection_pooling(self):
        """
        Share WMI connections among WMISampler objects.
        """
        from win32com.client import Dispatch

        wmi_sampler_1 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"])
        wmi_sampler_2 = WMISampler("Win32_OperatingSystem", ["TotalVisibleMemorySize"])
        wmi_sampler_3 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"], host="myhost")  # noqa

        wmi_sampler_1.sample()
        wmi_sampler_2.sample()

        self.assertEquals(Dispatch.ConnectServer.call_count, 1, Dispatch.ConnectServer.call_count)

        wmi_sampler_3.sample()

        self.assertEquals(Dispatch.ConnectServer.call_count, 2, Dispatch.ConnectServer.call_count)
Example #35
0
    def test_wmi_query(self):
        """
        Query WMI using WMI Query Language (WQL).
        """
        # No filters
        wmi_sampler = WMISampler(
            "Win32_PerfFormattedData_PerfDisk_LogicalDisk", ["AvgDiskBytesPerWrite", "FreeMegabytes"]
        )
        wmi_sampler.sample()

        self.assertWMIQuery(
            wmi_sampler,
            "Select AvgDiskBytesPerWrite,FreeMegabytes" " from Win32_PerfFormattedData_PerfDisk_LogicalDisk",
        )

        # Single filter
        wmi_sampler = WMISampler(
            "Win32_PerfFormattedData_PerfDisk_LogicalDisk",
            ["AvgDiskBytesPerWrite", "FreeMegabytes"],
            filters=[{"Name": "C:"}],
        )
        wmi_sampler.sample()

        self.assertWMIQuery(
            wmi_sampler,
            "Select AvgDiskBytesPerWrite,FreeMegabytes"
            " from Win32_PerfFormattedData_PerfDisk_LogicalDisk"
            " WHERE ( Name = 'C:' )",
        )

        # Multiple filters
        wmi_sampler = WMISampler(
            "Win32_PerfFormattedData_PerfDisk_LogicalDisk",
            ["AvgDiskBytesPerWrite", "FreeMegabytes"],
            filters=[{"Name": "C:", "Id": "123"}],
        )
        wmi_sampler.sample()

        self.assertWMIQuery(
            wmi_sampler,
            "Select AvgDiskBytesPerWrite,FreeMegabytes"
            " from Win32_PerfFormattedData_PerfDisk_LogicalDisk"
            " WHERE ( Name = 'C:' AND Id = '123' )",
        )
Example #36
0
    def test_wmi_query(self):
        """
        Query WMI using WMI Query Language (WQL).
        """
        # No filters
        wmi_sampler = WMISampler(
            "Win32_PerfFormattedData_PerfDisk_LogicalDisk",
            ["AvgDiskBytesPerWrite", "FreeMegabytes"])
        wmi_sampler.sample()

        self.assertWMIQuery(
            wmi_sampler, "Select AvgDiskBytesPerWrite,FreeMegabytes"
            " from Win32_PerfFormattedData_PerfDisk_LogicalDisk")

        # Single filter
        wmi_sampler = WMISampler(
            "Win32_PerfFormattedData_PerfDisk_LogicalDisk",
            ["AvgDiskBytesPerWrite", "FreeMegabytes"],
            filters=[{
                'Name': "C:"
            }])
        wmi_sampler.sample()

        self.assertWMIQuery(
            wmi_sampler, "Select AvgDiskBytesPerWrite,FreeMegabytes"
            " from Win32_PerfFormattedData_PerfDisk_LogicalDisk"
            " WHERE Name = 'C:'")

        # Multiple filters
        wmi_sampler = WMISampler(
            "Win32_PerfFormattedData_PerfDisk_LogicalDisk",
            ["AvgDiskBytesPerWrite", "FreeMegabytes"],
            filters=[{
                'Name': "C:"
            }, {
                'Id': "123"
            }])
        wmi_sampler.sample()

        self.assertWMIQuery(
            wmi_sampler, "Select AvgDiskBytesPerWrite,FreeMegabytes"
            " from Win32_PerfFormattedData_PerfDisk_LogicalDisk"
            " WHERE Id = '123' AND Name = 'C:'")
Example #37
0
    def test_wmi_connection_pooling(self):
        """
        Until caching is enabled WMI connections will not be shared among WMISampler objects.
        """
        from win32com.client import Dispatch

        wmi_sampler_1 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"])
        wmi_sampler_2 = WMISampler("Win32_OperatingSystem", ["TotalVisibleMemorySize"])
        wmi_sampler_3 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"], host="myhost")  # noqa

        wmi_sampler_1.sample()
        wmi_sampler_2.sample()

        # one connection, two samples
        self.assertEquals(Dispatch.ConnectServer.call_count, 3, Dispatch.ConnectServer.call_count)

        wmi_sampler_3.sample()

        # two connection, three samples
        self.assertEquals(Dispatch.ConnectServer.call_count, 5, Dispatch.ConnectServer.call_count)
Example #38
0
    def test_no_wmi_connection_pooling(self):
        """
        WMI connections are not be shared among WMISampler objects.
        """
        from win32com.client import Dispatch

        wmi_sampler_1 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"])
        wmi_sampler_2 = WMISampler("Win32_OperatingSystem", ["TotalVisibleMemorySize"])
        wmi_sampler_3 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"], host="myhost")  # noqa

        wmi_sampler_1.sample()
        wmi_sampler_2.sample()

        # 3 conns have been opened, 2 for the raw sampler and 1 for the other sampler
        self.assertEquals(Dispatch.ConnectServer.call_count, 3, Dispatch.ConnectServer.call_count)

        wmi_sampler_3.sample()

        # 5 conns now
        self.assertEquals(Dispatch.ConnectServer.call_count, 5, Dispatch.ConnectServer.call_count)
Example #39
0
    def test_wmi_connection_pooling(self):
        """
        Until caching is enabled WMI connections will not be shared among WMISampler objects.
        """
        from win32com.client import Dispatch

        wmi_sampler_1 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"])
        wmi_sampler_2 = WMISampler("Win32_OperatingSystem", ["TotalVisibleMemorySize"])
        wmi_sampler_3 = WMISampler("Win32_PerfRawData_PerfOS_System", ["ProcessorQueueLength"], host="myhost")  # noqa

        wmi_sampler_1.sample()
        wmi_sampler_2.sample()

        # one connection, two samples
        self.assertEquals(Dispatch.ConnectServer.call_count, 3, Dispatch.ConnectServer.call_count)

        wmi_sampler_3.sample()

        # two connection, three samples
        self.assertEquals(Dispatch.ConnectServer.call_count, 5, Dispatch.ConnectServer.call_count)
Example #40
0
class Cpu(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        self.wmi_sampler = WMISampler(logger,
                                      "Win32_PerfRawData_PerfOS_Processor",
                                      ["Name", "PercentInterruptTime"])

        self.gauge('system.cpu.user')
        self.gauge('system.cpu.idle')
        self.gauge('system.cpu.interrupt')
        self.gauge('system.cpu.system')
        self.gauge('system.cpu.pct_usage')

    def check(self, agentConfig):
        try:
            self.wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_PerfOS_Processor WMI class."
                u" CPU metrics will be returned at next iteration.")
            return []

        if not (len(self.wmi_sampler)):
            self.logger.warning(
                'Missing Win32_PerfRawData_PerfOS_Processor WMI class.'
                ' No CPU metrics will be returned')
            return []

        cpu_interrupt = self._average_metric(self.wmi_sampler,
                                             'PercentInterruptTime')
        if cpu_interrupt is not None:
            self.save_sample('system.cpu.interrupt', cpu_interrupt)

        cpu_percent = psutil.cpu_times_percent()

        self.save_sample('system.cpu.user', cpu_percent.user)
        self.save_sample('system.cpu.idle', cpu_percent.idle)
        self.save_sample('system.cpu.system', cpu_percent.system)
        self.save_sample('system.cpu.pct_usage',
                         cpu_percent.system + cpu_percent.user)

        return self.get_metrics()

    def _average_metric(self, sampler, wmi_prop):

        val = 0
        counter = 0
        for wmi_object in sampler:
            if wmi_object['Name'] == '_Total':
                continue

            wmi_prop_value = wmi_object.get(wmi_prop)
            if wmi_prop_value is not None:
                counter += 1
                val += float(wmi_prop_value)

        if counter > 0:
            return val / counter

        return val
Example #41
0
class IO(Check):
    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 check(self, agentConfig):
        try:
            self.wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_PerfDisk_LogicalDiskUnable WMI class."
                u" I/O metrics will be returned at next iteration."
            )
            return []

        if not (len(self.wmi_sampler)):
            self.logger.warning(
                "Missing Win32_PerfRawData_PerfDisk_LogicalDiskUnable WMI class." " No I/O metrics will be returned."
            )
            return []

        blacklist_re = agentConfig.get("device_blacklist_re", None)
        for device in self.wmi_sampler:
            name = device.get("Name")
            disk_write_bytes_per_sec = device.get("DiskWriteBytesPerSec")
            disk_writes_per_sec = device.get("DiskWritesPerSec")
            disk_read_bytes_per_sec = device.get("DiskReadBytesPerSec")
            disk_reads_per_sec = device.get("DiskReadsPerSec")
            current_disk_queue_length = device.get("CurrentDiskQueueLength")

            name = self.normalize_device_name(name)
            if should_ignore_disk(name, blacklist_re):
                continue
            if disk_write_bytes_per_sec is not None:
                self.save_sample("system.io.wkb_s", int(disk_write_bytes_per_sec) / B2KB, device_name=name)
            if disk_writes_per_sec is not None:
                self.save_sample("system.io.w_s", int(disk_writes_per_sec), device_name=name)
            if disk_read_bytes_per_sec is not None:
                self.save_sample("system.io.rkb_s", int(disk_read_bytes_per_sec) / B2KB, device_name=name)
            if disk_reads_per_sec is not None:
                self.save_sample("system.io.r_s", int(disk_reads_per_sec), device_name=name)
            if current_disk_queue_length is not None:
                self.save_sample("system.io.avg_q_sz", current_disk_queue_length, device_name=name)
        return self.get_metrics()
Example #42
0
class Memory(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.os_wmi_sampler = WMISampler(
            logger,
            "Win32_OperatingSystem",
            ["TotalVisibleMemorySize", "FreePhysicalMemory"]
        )
        self.mem_wmi_sampler = WMISampler(
            logger,
            "Win32_PerfRawData_PerfOS_Memory",
            ["CacheBytes", "CommittedBytes", "PoolPagedBytes", "PoolNonpagedBytes"])

        self.gauge('system.mem.free')
        self.gauge('system.mem.used')
        self.gauge('system.mem.total')
        # area of physical memory that stores recently used pages of data
        # for applications
        self.gauge('system.mem.cached')
        # Committed memory is physical memory for which space has been
        # reserved on the disk paging file in case it must be written
        # back to disk
        self.gauge('system.mem.committed')
        # physical memory used by the operating system, for objects
        # that can be written to disk when they are not being used
        self.gauge('system.mem.paged')
        # physical memory used by the operating system for objects that
        # cannot be written to disk, but must remain in physical memory
        # as long as they are allocated.
        self.gauge('system.mem.nonpaged')
        # usable = free + cached
        self.gauge('system.mem.usable')
        self.gauge('system.mem.pct_usable')

    def check(self, agentConfig):
        try:
            self.os_wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_OperatingSystem WMI class."
                u" Memory metrics will be returned at next iteration."
            )
            return

        if not (len(self.os_wmi_sampler)):
            self.logger.info('Missing Win32_OperatingSystem WMI class.'
                             ' No memory metrics will be returned.')
            return

        os = self.os_wmi_sampler[0]

        total = 0
        free = 0
        cached = 0

        total_visible_memory_size = os.get('TotalVisibleMemorySize')
        free_physical_memory = os.get('FreePhysicalMemory')

        if total_visible_memory_size is not None and free_physical_memory is not None:
            total = int(total_visible_memory_size) / KB2MB
            free = int(free_physical_memory) / KB2MB
            self.save_sample('system.mem.total', total)
            self.save_sample('system.mem.free', free)
            self.save_sample('system.mem.used', total - free)

        try:
            self.mem_wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_PerfOS_Memory WMI class."
                u" Memory metrics will be returned at next iteration."
            )
            return

        if not (len(self.mem_wmi_sampler)):
            self.logger.info('Missing Win32_PerfRawData_PerfOS_Memory WMI class.'
                             ' No memory metrics will be returned.')
            return self.get_metrics()

        mem = self.mem_wmi_sampler[0]

        cache_bytes = mem.get('CacheBytes')
        committed_bytes = mem.get('CommittedBytes')
        pool_paged_bytes = mem.get('PoolPagedBytes')
        pool_non_paged_bytes = mem.get('PoolNonpagedBytes')

        if cache_bytes is not None:
            cached = int(cache_bytes) / B2MB
            self.save_sample('system.mem.cached', cached)
        if committed_bytes is not None:
            self.save_sample('system.mem.committed', int(committed_bytes) / B2MB)
        if pool_paged_bytes is not None:
            self.save_sample('system.mem.paged', int(pool_paged_bytes) / B2MB)
        if pool_non_paged_bytes is not None:
            self.save_sample('system.mem.nonpaged', int(pool_non_paged_bytes) / B2MB)

        usable = free + cached
        self.save_sample('system.mem.usable', usable)
        if total > 0:
            pct_usable = float(usable) / total
            self.save_sample('system.mem.pct_usable', pct_usable)

        return self.get_metrics()
Example #43
0
class Memory(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        # Sampler(s)
        self.os_wmi_sampler = WMISampler(
            logger,
            "Win32_OperatingSystem",
            ["TotalVisibleMemorySize", "FreePhysicalMemory"]
        )
        self.mem_wmi_sampler = WMISampler(
            logger,
            "Win32_PerfRawData_PerfOS_Memory",
            ["CacheBytes", "CommittedBytes", "PoolPagedBytes", "PoolNonpagedBytes"])

        self.gauge('system.mem.free')
        self.gauge('system.mem.used')
        self.gauge('system.mem.total')
        # area of physical memory that stores recently used pages of data
        # for applications
        self.gauge('system.mem.cached')
        # Committed memory is physical memory for which space has been
        # reserved on the disk paging file in case it must be written
        # back to disk
        self.gauge('system.mem.committed')
        # physical memory used by the operating system, for objects
        # that can be written to disk when they are not being used
        self.gauge('system.mem.paged')
        # physical memory used by the operating system for objects that
        # cannot be written to disk, but must remain in physical memory
        # as long as they are allocated.
        self.gauge('system.mem.nonpaged')
        # usable = free + cached
        self.gauge('system.mem.usable')
        self.gauge('system.mem.pct_usable')
        #  details about the usage of the pagefile.
        self.gauge('system.mem.pagefile.total')
        self.gauge('system.mem.pagefile.used')
        self.gauge('system.mem.pagefile.free')
        self.gauge('system.mem.pagefile.pct_free')

    def check(self, agentConfig):
        try:
            self.os_wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_OperatingSystem WMI class."
                u" Memory metrics will be returned at next iteration."
            )
            return []

        if not (len(self.os_wmi_sampler)):
            self.logger.warning('Missing Win32_OperatingSystem WMI class.'
                             ' No memory metrics will be returned.')
            return []

        os = self.os_wmi_sampler[0]

        total = 0
        free = 0
        cached = 0

        total_visible_memory_size = os.get('TotalVisibleMemorySize')
        free_physical_memory = os.get('FreePhysicalMemory')

        if total_visible_memory_size is not None and free_physical_memory is not None:
            total = int(total_visible_memory_size) / KB2MB
            free = int(free_physical_memory) / KB2MB
            self.save_sample('system.mem.total', total)
            self.save_sample('system.mem.free', free)
            self.save_sample('system.mem.used', total - free)

        try:
            self.mem_wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_PerfOS_Memory WMI class."
                u" Memory metrics will be returned at next iteration."
            )
            return []

        if not (len(self.mem_wmi_sampler)):
            self.logger.info('Missing Win32_PerfRawData_PerfOS_Memory WMI class.'
                             ' No memory metrics will be returned.')
            return self.get_metrics()

        mem = self.mem_wmi_sampler[0]

        cache_bytes = mem.get('CacheBytes')
        committed_bytes = mem.get('CommittedBytes')
        pool_paged_bytes = mem.get('PoolPagedBytes')
        pool_non_paged_bytes = mem.get('PoolNonpagedBytes')

        if cache_bytes is not None:
            cached = int(cache_bytes) / B2MB
            self.save_sample('system.mem.cached', cached)
        if committed_bytes is not None:
            self.save_sample('system.mem.committed', int(committed_bytes) / B2MB)
        if pool_paged_bytes is not None:
            self.save_sample('system.mem.paged', int(pool_paged_bytes) / B2MB)
        if pool_non_paged_bytes is not None:
            self.save_sample('system.mem.nonpaged', int(pool_non_paged_bytes) / B2MB)

        usable = free + cached
        self.save_sample('system.mem.usable', usable)
        if total > 0:
            pct_usable = float(usable) / total
            self.save_sample('system.mem.pct_usable', pct_usable)

        # swap_memory pulls from the pagefile data,
        # rather than from the whole virtual memory data.
        page = psutil.swap_memory()
        if page.total is not None:
            self.save_sample('system.mem.pagefile.total', page.total)
            self.save_sample('system.mem.pagefile.used', page.used)
            self.save_sample('system.mem.pagefile.free', page.free)
            self.save_sample('system.mem.pagefile.pct_free', (100 - page.percent) / 100)

        return self.get_metrics()
Example #44
0
class Memory(Check):
    def __init__(self, logger):
        Check.__init__(self, logger)

        self.os_wmi_sampler = WMISampler(
            logger, "Win32_OperatingSystem",
            ["TotalVisibleMemorySize", "FreePhysicalMemory"])
        self.mem_wmi_sampler = WMISampler(
            logger, "Win32_PerfRawData_PerfOS_Memory", [
                "CacheBytes", "CommittedBytes", "PoolPagedBytes",
                "PoolNonpagedBytes"
            ])

        self.gauge('system.mem.free')
        self.gauge('system.mem.used')
        self.gauge('system.mem.total')

        self.gauge('system.mem.cached')

        self.gauge('system.mem.committed')

        self.gauge('system.mem.paged')

        self.gauge('system.mem.nonpaged')
        self.gauge('system.mem.usable')
        self.gauge('system.mem.pct_usage')

    def check(self, agentConfig):
        try:
            self.os_wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_OperatingSystem WMI class."
                u" Memory metrics will be returned at next iteration.")
            return []

        if not (len(self.os_wmi_sampler)):
            self.logger.warning('Missing Win32_OperatingSystem WMI class.'
                                ' No memory metrics will be returned.')
            return []

        os = self.os_wmi_sampler[0]

        total = 0
        free = 0
        cached = 0

        total_visible_memory_size = os.get('TotalVisibleMemorySize')
        free_physical_memory = os.get('FreePhysicalMemory')

        if total_visible_memory_size is not None and free_physical_memory is not None:
            total = int(total_visible_memory_size) / KB2MB
            free = int(free_physical_memory) / KB2MB
            self.save_sample('system.mem.total', total)
            self.save_sample('system.mem.free', free)
            self.save_sample('system.mem.used', total - free)

        try:
            self.mem_wmi_sampler.sample()
        except TimeoutException:
            self.logger.warning(
                u"Timeout while querying Win32_PerfRawData_PerfOS_Memory WMI class."
                u" Memory metrics will be returned at next iteration.")
            return

        if not (len(self.mem_wmi_sampler)):
            self.logger.info(
                'Missing Win32_PerfRawData_PerfOS_Memory WMI class.'
                ' No memory metrics will be returned.')
            return self.get_metrics()

        mem = self.mem_wmi_sampler[0]

        cache_bytes = mem.get('CacheBytes')
        committed_bytes = mem.get('CommittedBytes')
        pool_paged_bytes = mem.get('PoolPagedBytes')
        pool_non_paged_bytes = mem.get('PoolNonpagedBytes')

        if cache_bytes is not None:
            cached = int(cache_bytes) / B2MB
            self.save_sample('system.mem.cached', cached)
        if committed_bytes is not None:
            self.save_sample('system.mem.committed',
                             int(committed_bytes) / B2MB)
        if pool_paged_bytes is not None:
            self.save_sample('system.mem.paged', int(pool_paged_bytes) / B2MB)
        if pool_non_paged_bytes is not None:
            self.save_sample('system.mem.nonpaged',
                             int(pool_non_paged_bytes) / B2MB)

        usable = free + cached
        self.save_sample('system.mem.usable', usable)
        if total > 0:
            pct_usable = float(usable) / total
            pct_usage = (1 - pct_usable) * 100
            self.save_sample('system.mem.pct_usage', pct_usage)

        return self.get_metrics()