def psutil_wrapper(self, process, method, accessors, try_sudo, *args, **kwargs): """ A psutil wrapper that is calling * psutil.method(*args, **kwargs) and returns the result OR * psutil.method(*args, **kwargs).accessor[i] for each accessors given in a list, the result being indexed in a dictionary by the accessor name """ if accessors is None: result = None else: result = {} # Ban certain method that we know fail if method == 'memory_info' and Platform.is_win32() or Platform.is_solaris(): return result elif method == 'num_fds' and not Platform.is_unix(): return result elif method == 'num_handles' and not Platform.is_win32(): return result try: res = getattr(process, method)(*args, **kwargs) if accessors is None: result = res else: for acc in accessors: try: result[acc] = getattr(res, acc) except AttributeError: self.log.debug("psutil.{}().{} attribute does" "not exist".format(method, acc)) except (NotImplementedError, AttributeError): self.log.debug("psutil method {} not implemented".format(method)) except psutil.AccessDenied: self.log.debug("psutil was denied access for method {}".format(method)) if method == 'num_fds' and Platform.is_unix() and try_sudo: try: # It is up the agent's packager to grant # corresponding sudo policy on unix platforms ls_args = ['sudo', 'ls', '/proc/{}/fd/'.format(process.pid)] process_ls = subprocess.check_output(ls_args) result = len(process_ls.splitlines()) except subprocess.CalledProcessError as e: self.log.exception("trying to retrieve {} with sudo failed with " "return code {}".format(method, e.returncode)) except Exception: self.log.exception("trying to retrieve {} with sudo also failed".format(method)) except psutil.NoSuchProcess: self.warning("Process {} disappeared while scanning".format(process.pid)) return result
def _exclude_disk_psutil(self, part): # skip cd-rom drives with no disk in it; they may raise # ENOENT, pop-up a Windows GUI error for a non-ready # partition or just hang; # and all the other excluded disks skip_win = Platform.is_win32() and ('cdrom' in part.opts or part.fstype == '') return skip_win or self._exclude_disk(part.device, part.fstype, part.mountpoint)
def collect_metrics_psutil(self): self._valid_disks = {} for part in psutil.disk_partitions(all=True): # we check all exclude conditions if self._exclude_disk_psutil(part): continue # Get disk metrics here to be able to exclude on total usage try: disk_usage = timeout(5)(psutil.disk_usage)(part.mountpoint) except TimeoutException: self.log.warn( u"Timeout while retrieving the disk usage of `%s` mountpoint. Skipping...", part.mountpoint) continue except Exception as e: self.log.warn("Unable to get disk metrics for %s: %s", part.mountpoint, e) continue # Exclude disks with total disk size 0 if disk_usage.total == 0: continue # For later, latency metrics self._valid_disks[part.device] = (part.fstype, part.mountpoint) self.log.debug('Passed: {0}'.format(part.device)) tags = [part.fstype, 'filesystem:{}'.format(part.fstype) ] if self._tag_by_filesystem else [] device_name = part.mountpoint if self._use_mount else part.device # apply device/mountpoint specific tags for regex, device_tags in self._device_tag_re: if regex.match(device_name): tags += device_tags tags.extend(self._custom_tags) # legacy check names c: vs psutil name C:\\ if Platform.is_win32(): device_name = device_name.strip('\\').lower() for metric_name, metric_value in self._collect_part_metrics( part, disk_usage).iteritems(): self.gauge(metric_name, metric_value, tags=tags, device_name=device_name) self.collect_latency_metrics()
# All rights reserved # Licensed under a 3-clause BSD style license (see LICENSE) from __future__ import unicode_literals import time import dns.resolver from datadog_checks.checks import NetworkCheck, Status from datadog_checks.utils.platform import Platform # These imports are necessary because otherwise dynamic type # resolution will fail on windows without it. # See more here: https://github.com/rthalley/dnspython/issues/39. if Platform.is_win32(): from dns.rdtypes.ANY import * # noqa from dns.rdtypes.IN import * # noqa # for tiny time deltas, time.time on Windows reports the same value # of the clock more than once, causing the computation of response_time # to be often 0; let's use time.clock that is more precise. time_func = time.clock else: time_func = time.time class BadConfException(Exception): pass
def collect_metrics_psutil(self): self._valid_disks = {} for part in psutil.disk_partitions(all=True): # we check all exclude conditions if self._exclude_disk_psutil(part): continue # Get disk metrics here to be able to exclude on total usage try: disk_usage = timeout(5)(psutil.disk_usage)(part.mountpoint) except TimeoutException: self.log.warn( u"Timeout while retrieving the disk usage of `%s` mountpoint. Skipping...", part.mountpoint) continue except Exception as e: self.log.warn("Unable to get disk metrics for %s: %s", part.mountpoint, e) continue # Exclude disks with total disk size 0 if disk_usage.total == 0: continue # For later, latency metrics self._valid_disks[part.device] = (part.fstype, part.mountpoint) self.log.debug('Passed: {0}'.format(part.device)) tags = [part.fstype, 'filesystem:{}'.format(part.fstype) ] if self._tag_by_filesystem else [] device_name = part.mountpoint if self._use_mount else part.device # apply device/mountpoint specific tags for regex, device_tags in self._device_tag_re: if regex.match(device_name): tags += device_tags tags.extend(self._custom_tags) # legacy check names c: vs psutil name C:\\ if Platform.is_win32(): device_name = device_name.strip('\\').lower() for metric_name, metric_value in self._collect_part_metrics( part, disk_usage).iteritems(): self.gauge(metric_name, metric_value, tags=tags, device_name=device_name) # Add in a disk read write or read only check if self._service_check_rw: rwro = list(set(['rw', 'ro']) & set(part.opts.split(','))) if len(rwro) == 1: self.service_check( 'disk.read_write', AgentCheck.OK if rwro[0] == 'rw' else AgentCheck.CRITICAL, tags=tags + ['device:%s' % (device_name)]) else: self.service_check('disk.read_write', AgentCheck.UNKNOWN, tags=tags + ['device:%s' % (device_name)]) self.collect_latency_metrics()