detect virtual disks by looking up /sys/devices/virtual/block/NAME
Costa Tsaousis (ktsaou) committed
Oct 26, 2017 at 23:25 UTC
52f04c04e3e9401cf85da45863b99c5ac94caed0
1 file changed
+36
-21
src/proc_diskstats.c
+36
-21
@@ -5,7 +5,7 @@
5
#define DISK_TYPE_UNKNOWN 0
6
#define DISK_TYPE_PHYSICAL 1
7
#define DISK_TYPE_PARTITION 2
8
-#define DISK_TYPE_CONTAINER 3
8
+#define DISK_TYPE_VIRTUAL 3
9
10
#define CONFIG_SECTION_DISKSTATS "plugin:proc:/proc/diskstats"
11
#define DELAULT_EXLUDED_DISKS "loop* ram*"
@@ -77,6 +77,7 @@ static char *path_to_get_hw_sector_size = NULL;
77
static char *path_to_get_hw_sector_size_partitions = NULL;
78
static char *path_to_sys_dev_block_major_minor_string = NULL;
79
static char *path_to_sys_block_device = NULL;
80
+static char *path_to_sys_devices_virtual_block_device = NULL;
81
static char *path_to_device_mapper = NULL;
82
83
static inline char *get_disk_name(unsigned long major, unsigned long minor, char *disk) {
@@ -175,39 +176,50 @@ static struct disk *get_disk(unsigned long major, unsigned long minor, char *dis
176
last->next = d;
177
}
178
178
- // find if it is a partition
179
- // by checking if /sys/dev/block/MAJOR:MINOR/partition is readable.
179
char buffer[FILENAME_MAX + 1];
180
181
+ // find if it is a physical disk
182
+ // by checking if /sys/block/DISK is readable.
183
snprintfz(buffer, FILENAME_MAX, path_to_sys_block_device, disk);
184
if(likely(access(buffer, R_OK) == 0)) {
185
+ // assign it here, but it will be overwritten if it is not a physical disk
186
d->type = DISK_TYPE_PHYSICAL;
187
}
188
189
+ // find if it is a partition
190
+ // by checking if /sys/dev/block/MAJOR:MINOR/partition is readable.
191
snprintfz(buffer, FILENAME_MAX, path_to_sys_dev_block_major_minor_string, major, minor, "partition");
192
if(likely(access(buffer, R_OK) == 0)) {
193
d->type = DISK_TYPE_PARTITION;
194
}
195
else {
192
- // find if it is a container
193
- // by checking if /sys/dev/block/MAJOR:MINOR/slaves has entries
194
- snprintfz(buffer, FILENAME_MAX, path_to_sys_dev_block_major_minor_string, major, minor, "slaves/");
195
- DIR *dirp = opendir(buffer);
196
- if(likely(dirp != NULL)) {
197
- struct dirent *dp;
198
- while( (dp = readdir(dirp)) ) {
199
- // . and .. are also files in empty folders.
200
- if(unlikely(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)) {
201
- continue;
196
+ // find if it is a virtual disk
197
+ // by checking if /sys/devices/virtual/block/DISK is readable.
198
+ snprintfz(buffer, FILENAME_MAX, path_to_sys_devices_virtual_block_device, disk);
199
+ if(likely(access(buffer, R_OK) == 0)) {
200
+ d->type = DISK_TYPE_VIRTUAL;
201
+ }
202
+ else {
203
+ // find if it is a virtual device
204
+ // by checking if /sys/dev/block/MAJOR:MINOR/slaves has entries
205
+ snprintfz(buffer, FILENAME_MAX, path_to_sys_dev_block_major_minor_string, major, minor, "slaves/");
206
+ DIR *dirp = opendir(buffer);
207
+ if (likely(dirp != NULL)) {
208
+ struct dirent *dp;
209
+ while ((dp = readdir(dirp))) {
210
+ // . and .. are also files in empty folders.
211
+ if (unlikely(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)) {
212
+ continue;
213
+ }
214
+
215
+ d->type = DISK_TYPE_VIRTUAL;
216
+
217
+ // Stop the loop after we found one file.
218
+ break;
219
}
203
-
204
- d->type = DISK_TYPE_CONTAINER;
205
-
206
- // Stop the loop after we found one file.
207
- break;
220
+ if (unlikely(closedir(dirp) == -1))
221
+ error("Unable to close dir %s", buffer);
222
}
209
- if(unlikely(closedir(dirp) == -1))
210
- error("Unable to close dir %s", buffer);
223
}
224
}
225
@@ -332,6 +344,9 @@ int do_proc_diskstats(int update_every, usec_t dt) {
344
snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s");
345
path_to_sys_block_device = config_get(CONFIG_SECTION_DISKSTATS, "path to get block device", buffer);
346
347
+ snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/virtual/block/%s");
348
+ path_to_sys_devices_virtual_block_device = config_get(CONFIG_SECTION_DISKSTATS, "path to get virtual block device", buffer);
349
+
350
snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/%s");
351
path_to_sys_dev_block_major_minor_string = config_get(CONFIG_SECTION_DISKSTATS, "path to get block device infos", buffer);
352
@@ -504,7 +519,7 @@ int do_proc_diskstats(int update_every, usec_t dt) {
519
def_performance = global_enable_performance_for_partitions;
520
break;
521
507
- case DISK_TYPE_CONTAINER:
522
+ case DISK_TYPE_VIRTUAL:
523
def_performance = global_enable_performance_for_virtual_disks;
524
break;
525
}