diff --git a/checks.d/disk.py b/checks.d/disk.py index dea997c3e0..2e4a8377fc 100644 --- a/checks.d/disk.py +++ b/checks.d/disk.py @@ -187,7 +187,19 @@ def _collect_part_metrics(self, part, usage): def _collect_inodes_metrics(self, mountpoint): metrics = {} - inodes = os.statvfs(mountpoint) + # we need to timeout this, too. + try: + inodes = timeout(5)(os.statvfs)(mountpoint) + except TimeoutException: + self.log.warn( + u"Timeout while retrieving the disk usage of `%s` mountpoint. Skipping...", + mountpoint + ) + return metrics + except Exception as e: + self.log.warn("Unable to get disk metrics for %s: %s", mountpoint, e) + return metrics + if inodes.f_files != 0: total = inodes.f_files free = inodes.f_ffree diff --git a/tests/checks/mock/test_disk.py b/tests/checks/mock/test_disk.py index 88529bf3cf..fdc4ea77c5 100644 --- a/tests/checks/mock/test_disk.py +++ b/tests/checks/mock/test_disk.py @@ -10,7 +10,6 @@ DEFAULT_DEVICE_NAME = '/dev/sda1' - class MockPart(object): def __init__(self, device=DEFAULT_DEVICE_NAME, fstype='ext4', mountpoint='/'): @@ -114,6 +113,8 @@ def test_device_exclusion_logic_no_name(self): def test_psutil(self, mock_partitions, mock_usage, mock_inodes): # Mocking mock_usage.__name__ = "foo" + mock_inodes.__name__ = "foo" + mock_partitions.__name__ = "foo" # Run check for tag_by in ['yes', 'no']: @@ -134,6 +135,8 @@ def test_psutil(self, mock_partitions, mock_usage, mock_inodes): def test_use_mount(self, mock_partitions, mock_usage, mock_inodes): # Mocking mock_usage.__name__ = "foo" + mock_inodes.__name__ = "foo" + mock_partitions.__name__ = "foo" # Run check self.run_check({'instances': [{'use_mount': 'yes'}]}) @@ -149,6 +152,9 @@ def test_use_mount(self, mock_partitions, mock_usage, mock_inodes): return_value=(Fixtures.read_file('debian-df-Tk'), "", 0)) @mock.patch('os.statvfs', return_value=MockInodesMetrics()) def test_no_psutil_debian(self, mock_df_output, mock_statvfs): + mock_statvfs.__name__ = "foo" + mock_df_output.__name__ = "foo" + self.run_check({'instances': [{'use_mount': 'no', 'excluded_filesystems': ['tmpfs']}]}, mocks={'_psutil': lambda: False}) @@ -165,6 +171,9 @@ def test_no_psutil_debian(self, mock_df_output, mock_statvfs): return_value=(Fixtures.read_file('freebsd-df-Tk'), "", 0)) @mock.patch('os.statvfs', return_value=MockInodesMetrics()) def test_no_psutil_freebsd(self, mock_df_output, mock_statvfs): + mock_statvfs.__name__ = "foo" + mock_df_output.__name__ = "foo" + self.run_check({'instances': [{'use_mount': 'no', 'excluded_filesystems': ['devfs'], 'excluded_disk_re': 'zroot/.+'}]}, @@ -180,6 +189,9 @@ def test_no_psutil_freebsd(self, mock_df_output, mock_statvfs): return_value=(Fixtures.read_file('centos-df-Tk'), "", 0)) @mock.patch('os.statvfs', return_value=MockInodesMetrics()) def test_no_psutil_centos(self, mock_df_output, mock_statvfs): + mock_statvfs.__name__ = "foo" + mock_df_output.__name__ = "foo" + self.run_check({'instances': [{'use_mount': 'no', 'excluded_filesystems': ['devfs', 'tmpfs'], 'excluded_disks': ['/dev/sda1']}]},