Skip to content

Commit

Permalink
[disk] timeout on disk usage
Browse files Browse the repository at this point in the history
Timeout disk usage queries after 5 seconds with a warning and no data.
Recover at next iteration retrieving the existing query.
  • Loading branch information
yannmh committed Sep 6, 2016
1 parent ce81955 commit 9db4ce4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions checks.d/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/checks/mock/test_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 9db4ce4

Please sign in to comment.