Skip to content

Commit 8793c78

Browse files
authored
[disk] Add include_all_devices option and improve error logs (#7378)
Using `all` option from `psutil.disk_partitions`
1 parent 14d5a79 commit 8793c78

4 files changed

Lines changed: 55 additions & 4 deletions

File tree

disk/assets/configuration/spec.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ files:
4242
start matching from the beginning and therefore to match anywhere you
4343
must prepend `.*`. For exact matches append `$`.
4444
45+
Devices from pseudo or memory-based file systems can be excluded by disabling the
46+
`include_all_devices` option.
47+
4548
When conflicts arise, this will override `file_system_whitelist`.
4649
value:
4750
example:
@@ -110,6 +113,15 @@ files:
110113
type: array
111114
items:
112115
type: string
116+
- name: include_all_devices
117+
description: |
118+
Instruct the check to collect from all devices, including non-physical devices.
119+
Set this to false to exclude pseudo, memory, duplicate or inaccessible file systems.
120+
121+
For more fine-grained control, use the inclusion and exclusion options.
122+
value:
123+
example: true
124+
type: boolean
113125
- name: service_check_rw
114126
description: |
115127
Instruct the check to notify based on partition state.

disk/datadog_checks/disk/data/conf.yaml.default

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ instances:
4444
## start matching from the beginning and therefore to match anywhere you
4545
## must prepend `.*`. For exact matches append `$`.
4646
##
47+
## Devices from pseudo or memory-based file systems can be excluded by disabling the
48+
## `include_all_devices` option.
49+
##
4750
## When conflicts arise, this will override `file_system_whitelist`.
4851
#
4952
# file_system_blacklist:
@@ -98,6 +101,14 @@ instances:
98101
# - /dev/sde
99102
# - '[FJ]:'
100103

104+
## @param include_all_devices - boolean - optional - default: true
105+
## Instruct the check to collect from all devices, including non-physical devices.
106+
## Set this to false to exclude pseudo, memory, duplicate or inaccessible file systems.
107+
##
108+
## For more fine-grained control, use the inclusion and exclusion options.
109+
#
110+
# include_all_devices: true
111+
101112
## @param service_check_rw - boolean - optional - default: false
102113
## Instruct the check to notify based on partition state.
103114
##

disk/datadog_checks/disk/disk.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def __init__(self, name, init_config, instances):
4747
self._all_partitions = is_affirmative(instance.get('all_partitions', False))
4848
self._file_system_whitelist = instance.get('file_system_whitelist', [])
4949
self._file_system_blacklist = instance.get('file_system_blacklist', [])
50+
# FIXME (8.X): Exclude special file systems by default
51+
self._include_all_devices = is_affirmative(instance.get('include_all_devices', True))
5052
self._device_whitelist = instance.get('device_whitelist', [])
5153
self._device_blacklist = instance.get('device_blacklist', [])
5254
self._mount_point_whitelist = instance.get('mount_point_whitelist', [])
@@ -71,7 +73,7 @@ def check(self, instance):
7173
self.devices_label = self._get_devices_label()
7274

7375
self._valid_disks = {}
74-
for part in psutil.disk_partitions(all=True):
76+
for part in psutil.disk_partitions(all=self._include_all_devices):
7577
# we check all exclude conditions
7678
if self.exclude_disk(part):
7779
continue
@@ -88,7 +90,12 @@ def check(self, instance):
8890
)
8991
continue
9092
except Exception as e:
91-
self.log.warning('Unable to get disk metrics for %s: %s', part.mountpoint, e)
93+
self.log.warning(
94+
u'Unable to get disk metrics for %s: %s. '
95+
u'You can exclude this mountpoint in the settings if it is invalid.',
96+
part.mountpoint,
97+
e,
98+
)
9299
continue
93100

94101
# Exclude disks with size less than min_disk_size
@@ -147,7 +154,6 @@ def _exclude_disk(self, device, file_system, mount_point):
147154
"""
148155
Return True for disks we don't want or that match regex in the config file
149156
"""
150-
self.log.debug('_exclude_disk: %s, %s, %s', device, file_system, mount_point)
151157

152158
if not device or device == 'none':
153159
device = None
@@ -245,7 +251,12 @@ def _collect_inodes_metrics(self, mountpoint):
245251
)
246252
return metrics
247253
except Exception as e:
248-
self.log.warning('Unable to get disk metrics for %s: %s', mountpoint, e)
254+
self.log.warning(
255+
u'Unable to get disk metrics for %s: %s. '
256+
u'You can exclude this mountpoint in the settings if it is invalid.',
257+
mountpoint,
258+
e,
259+
)
249260
return metrics
250261

251262
if inodes.f_files != 0:

disk/tests/test_unit.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,20 @@ def f(mountpoint):
271271
aggregator.assert_metric_has_tag(name, 'device_name:{}'.format(DEFAULT_DEVICE_BASE_NAME))
272272

273273
aggregator.assert_all_metrics_covered()
274+
275+
276+
@pytest.mark.usefixtures('psutil_mocks')
277+
def test_include_all_devices(aggregator, gauge_metrics, rate_metrics):
278+
c = Disk('disk', {}, [{}])
279+
280+
with mock.patch('psutil.disk_partitions', return_value=[]) as m:
281+
c.check({})
282+
# By default, we include all devices
283+
m.assert_called_with(all=True)
284+
285+
instance = {'include_all_devices': False}
286+
c = Disk('disk', {}, [instance])
287+
288+
with mock.patch('psutil.disk_partitions', return_value=[]) as m:
289+
c.check({})
290+
m.assert_called_with(all=False)

0 commit comments

Comments
 (0)