diff --git a/checks.d/disk.py b/checks.d/disk.py index e663179feb..dea997c3e0 100644 --- a/checks.d/disk.py +++ b/checks.d/disk.py @@ -12,11 +12,15 @@ except ImportError: psutil = None -# project +# datadog from checks import AgentCheck from config import _is_affirmative from util import Platform from utils.subprocess_output import get_subprocess_output +from utils.timeout import ( + timeout, + TimeoutException, +) class Disk(AgentCheck): @@ -91,12 +95,18 @@ def collect_metrics_psutil(self): # 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 = psutil.disk_usage(part.mountpoint) + 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.debug("Unable to get disk metrics for %s: %s", - part.mountpoint, 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: diff --git a/tests/checks/mock/test_disk.py b/tests/checks/mock/test_disk.py index 3243d1c2cf..88529bf3cf 100644 --- a/tests/checks/mock/test_disk.py +++ b/tests/checks/mock/test_disk.py @@ -112,6 +112,10 @@ def test_device_exclusion_logic_no_name(self): @mock.patch('psutil.disk_usage', return_value=MockDiskMetrics()) @mock.patch('os.statvfs', return_value=MockInodesMetrics()) def test_psutil(self, mock_partitions, mock_usage, mock_inodes): + # Mocking + mock_usage.__name__ = "foo" + + # Run check for tag_by in ['yes', 'no']: self.run_check({'instances': [{'tag_by_filesystem': tag_by}]}, force_reload=True) @@ -128,6 +132,10 @@ def test_psutil(self, mock_partitions, mock_usage, mock_inodes): @mock.patch('psutil.disk_usage', return_value=MockDiskMetrics()) @mock.patch('os.statvfs', return_value=MockInodesMetrics()) def test_use_mount(self, mock_partitions, mock_usage, mock_inodes): + # Mocking + mock_usage.__name__ = "foo" + + # Run check self.run_check({'instances': [{'use_mount': 'yes'}]}) # Assert metrics