Skip to content

Commit 20e5d20

Browse files
committed
Use all option from psutil.disk_partitions
This does the same that this PR intended but supporting all operating systems and by checking (on Linux) if the file system is backed by a block device.
1 parent 09e8990 commit 20e5d20

4 files changed

Lines changed: 40 additions & 88 deletions

File tree

disk/assets/configuration/spec.yaml

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ files:
4242
start matching from the beginning and therefore to match anywhere you
4343
must prepend `.*`. For exact matches append `$`.
4444
45-
Some common Linux special file systems can be excluded by enabling the
46-
`exclude_special_file_systems` option.
47-
45+
Devices from pseudo or memory-based file systems can be excluded by disabling the
46+
`include_all_devices` option.
47+
4848
When conflicts arise, this will override `file_system_whitelist`.
4949
value:
5050
example:
@@ -54,31 +54,6 @@ files:
5454
type: array
5555
items:
5656
type: string
57-
- name: exclude_special_file_systems
58-
description: |
59-
Instruct the check to not collect from some common special file systems.
60-
Works on Linux only.
61-
62-
The special file systems excluded with this option are:
63-
- binfmt_misc
64-
- configfs
65-
- debugfs
66-
- devtmpfs
67-
- overlay
68-
- proc
69-
- rootfs
70-
- securityfs
71-
- sysfs
72-
- tmpfs
73-
- tracefs
74-
75-
These are added to the `file_system_blacklist`.
76-
77-
For more fine-grained control, use `file_system_blacklist` and `file_system_whitelist`.
78-
value:
79-
example: true
80-
type: boolean
81-
default: false
8257
- name: device_whitelist
8358
description: |
8459
Instruct the check to only collect from matching devices.
@@ -138,6 +113,16 @@ files:
138113
type: array
139114
items:
140115
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: false
124+
type: boolean
125+
default: true
141126
- name: service_check_rw
142127
description: |
143128
Instruct the check to notify based on partition state.

disk/datadog_checks/disk/disk.py

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,13 @@
2020
# See: https://github.com/DataDog/integrations-core/pull/1109#discussion_r167133580
2121
IGNORE_CASE = re.I
2222

23-
KNOWN_SPECIAL_FILE_SYSTEMS = set()
24-
2523
def _base_device_name(device):
2624
return device.strip('\\').lower()
2725

2826

29-
elif platform.system() == "Linux":
30-
IGNORE_CASE = 0
31-
32-
# Remember to update the configuration specification
33-
# if modifying the list of special file systems.
34-
KNOWN_SPECIAL_FILE_SYSTEMS = set(
35-
[
36-
'binfmt_misc',
37-
'configfs',
38-
'debugfs',
39-
'devtmpfs',
40-
'overlay',
41-
'proc',
42-
'rootfs',
43-
'securityfs',
44-
'sysfs',
45-
'tmpfs',
46-
'tracefs',
47-
]
48-
)
49-
50-
def _base_device_name(device):
51-
return os.path.basename(device)
52-
53-
5427
else:
5528
IGNORE_CASE = 0
5629

57-
KNOWN_SPECIAL_FILE_SYSTEMS = set()
58-
5930
def _base_device_name(device):
6031
return os.path.basename(device)
6132

@@ -77,7 +48,7 @@ def __init__(self, name, init_config, instances):
7748
self._file_system_whitelist = instance.get('file_system_whitelist', [])
7849
self._file_system_blacklist = instance.get('file_system_blacklist', [])
7950
# FIXME (8.X): Exclude special file systems by default
80-
self._exclude_special_file_systems = instance.get('exclude_special_file_systems', False)
51+
self._include_all_devices = is_affirmative(instance.get('include_all_devices', True))
8152
self._device_whitelist = instance.get('device_whitelist', [])
8253
self._device_blacklist = instance.get('device_blacklist', [])
8354
self._mount_point_whitelist = instance.get('mount_point_whitelist', [])
@@ -102,7 +73,7 @@ def check(self, instance):
10273
self.devices_label = self._get_devices_label()
10374

10475
self._valid_disks = {}
105-
for part in psutil.disk_partitions(all=True):
76+
for part in psutil.disk_partitions(all=self._include_all_devices):
10677
# we check all exclude conditions
10778
if self.exclude_disk(part):
10879
continue
@@ -119,21 +90,12 @@ def check(self, instance):
11990
)
12091
continue
12192
except Exception as e:
122-
if not self._exclude_special_file_systems and part.fstype in KNOWN_SPECIAL_FILE_SYSTEMS:
123-
self.log.warning(
124-
u'Unable to get disk metrics for %s with special file system %s: %s. '
125-
u'Enable `exclude_special_file_systems` to ignore common special file systems.',
126-
part.mountpoint,
127-
part.fstype,
128-
e,
129-
)
130-
else:
131-
self.log.warning(
132-
u'Unable to get disk metrics for %s: %s. '
133-
u'You can exclude this mountpoint in the settings if it is invalid.',
134-
part.mountpoint,
135-
e,
136-
)
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+
)
13799
continue
138100

139101
# Exclude disks with size less than min_disk_size
@@ -234,9 +196,7 @@ def _file_system_blacklisted(self, file_system):
234196
if self._file_system_blacklist is None:
235197
return False
236198

237-
return (self._exclude_special_file_systems and file_system in KNOWN_SPECIAL_FILE_SYSTEMS) or bool(
238-
self._file_system_blacklist.match(file_system)
239-
)
199+
return not not self._file_system_blacklist.match(file_system)
240200

241201
def _device_whitelisted(self, device):
242202
if not device or self._device_whitelist is None:

disk/tests/test_filter.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from datadog_checks.disk.disk import IGNORE_CASE, Disk
88

99
from .mocks import MockPart
10-
from .utils import assert_regex_equal, requires_linux, requires_windows
10+
from .utils import assert_regex_equal, requires_windows
1111

1212

1313
def test_default_casing():
@@ -104,16 +104,6 @@ def test_file_system_whitelist_blacklist():
104104
assert c.exclude_disk(MockPart(fstype='NTFS')) is True
105105

106106

107-
@requires_linux
108-
def test_exclude_special_file_system():
109-
instance = {'exclude_special_file_systems': True}
110-
c = Disk('disk', {}, [instance])
111-
112-
assert c.exclude_disk(MockPart(fstype='debugfs')) is True
113-
assert c.exclude_disk(MockPart(fstype='tmpfs')) is True
114-
assert c.exclude_disk(MockPart(fstype='ext4')) is False
115-
116-
117107
def test_device_whitelist():
118108
instance = {'device_whitelist': ['/dev/sda[1-3]', 'c:']}
119109
c = Disk('disk', {}, [instance])

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)