find disk names by-label and by-id; fixes #3081
Costa Tsaousis (ktsaou) committed
Nov 30, 2017 at 21:46 UTC
9d0b7248a519e98a0bd45cfb1a4771b464b6d325
1 file changed
+47
-19
src/proc_diskstats.c
+47
-19
@@ -79,19 +79,19 @@ 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
+static char *path_to_device_label = NULL;
83
+static char *path_to_device_id = NULL;
84
+static int name_disks_by_id = CONFIG_BOOLEAN_NO;
85
83
-static inline char *get_disk_name(unsigned long major, unsigned long minor, char *disk) {
84
- static int enabled = 1;
85
-
86
- if(!enabled) goto cleanup;
87
-
86
+static inline int get_disk_name_from_path(const char *path, char *result, size_t result_size, unsigned long major, unsigned long minor, char *disk) {
87
char filename[FILENAME_MAX + 1];
89
- char link[FILENAME_MAX + 1];
88
+ int found = 0;
89
91
- DIR *dir = opendir(path_to_device_mapper);
90
+ result_size--;
91
+
92
+ DIR *dir = opendir(path);
93
if (!dir) {
93
- error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot open directory '%s'. Disabling device-mapper support.", disk, major, minor, path_to_device_mapper);
94
- enabled = 0;
94
+ error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot open directory '%s'. Disabling device-mapper support.", disk, major, minor, path);
95
goto cleanup;
96
}
97
@@ -99,18 +99,18 @@ static inline char *get_disk_name(unsigned long major, unsigned long minor, char
99
while ((de = readdir(dir))) {
100
if(de->d_type != DT_LNK) continue;
101
102
- snprintfz(filename, FILENAME_MAX, "%s/%s", path_to_device_mapper, de->d_name);
103
- ssize_t len = readlink(filename, link, FILENAME_MAX);
102
+ snprintfz(filename, FILENAME_MAX, "%s/%s", path, de->d_name);
103
+ ssize_t len = readlink(filename, result, result_size);
104
if(len <= 0) {
105
error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot read link '%s'.", disk, major, minor, filename);
106
continue;
107
}
108
109
- link[len] = '\0';
110
- if(link[0] != '/')
111
- snprintfz(filename, FILENAME_MAX, "%s/%s", path_to_device_mapper, link);
109
+ result[len] = '\0';
110
+ if(result[0] != '/')
111
+ snprintfz(filename, FILENAME_MAX, "%s/%s", path, result);
112
else
113
- strncpyz(filename, link, FILENAME_MAX);
113
+ strncpyz(filename, result, FILENAME_MAX);
114
115
struct stat sb;
116
if(stat(filename, &sb) == -1) {
@@ -130,17 +130,37 @@ static inline char *get_disk_name(unsigned long major, unsigned long minor, char
130
131
// info("DEVICE-MAPPER ('%s', %lu:%lu): filename '%s' matches.", disk, major, minor, filename);
132
133
- strncpy(link, de->d_name, FILENAME_MAX);
134
- netdata_fix_chart_name(link);
135
- disk = link;
133
+ strncpy(result, de->d_name, result_size);
134
+ found = 1;
135
break;
136
}
137
closedir(dir);
138
139
+
140
cleanup:
141
- return strdupz(disk);
141
+
142
+ if(!found)
143
+ result[0] = '\0';
144
+
145
+ return found;
146
}
147
148
+static inline char *get_disk_name(unsigned long major, unsigned long minor, char *disk) {
149
+ char result[FILENAME_MAX + 1] = "";
150
+
151
+ if(!path_to_device_mapper || !*path_to_device_mapper || !get_disk_name_from_path(path_to_device_mapper, result, FILENAME_MAX + 1, major, minor, disk))
152
+ if(!path_to_device_label || !*path_to_device_label || !get_disk_name_from_path(path_to_device_label, result, FILENAME_MAX + 1, major, minor, disk))
153
+ if(name_disks_by_id != CONFIG_BOOLEAN_YES || !path_to_device_id || !*path_to_device_id || !get_disk_name_from_path(path_to_device_id, result, FILENAME_MAX + 1, major, minor, disk))
154
+ strncpy(result, disk, FILENAME_MAX);
155
+
156
+ if(!result[0])
157
+ strncpy(result, disk, FILENAME_MAX);
158
+
159
+ netdata_fix_chart_name(result);
160
+ return strdup(result);
161
+}
162
+
163
+
164
static struct disk *get_disk(unsigned long major, unsigned long minor, char *disk) {
165
static struct mountinfo *disk_mountinfo_root = NULL;
166
@@ -358,6 +378,14 @@ int do_proc_diskstats(int update_every, usec_t dt) {
378
379
snprintfz(buffer, FILENAME_MAX, "%s/dev/mapper", netdata_configured_host_prefix);
380
path_to_device_mapper = config_get(CONFIG_SECTION_DISKSTATS, "path to device mapper", buffer);
381
+
382
+ snprintfz(buffer, FILENAME_MAX, "%s/dev/disk/by-label", netdata_configured_host_prefix);
383
+ path_to_device_label = config_get(CONFIG_SECTION_DISKSTATS, "path to /dev/disk/by-label", buffer);
384
+
385
+ snprintfz(buffer, FILENAME_MAX, "%s/dev/disk/by-id", netdata_configured_host_prefix);
386
+ path_to_device_id = config_get(CONFIG_SECTION_DISKSTATS, "path to /dev/disk/by-id", buffer);
387
+
388
+ name_disks_by_id = config_get_boolean(CONFIG_SECTION_DISKSTATS, "name disks by id", name_disks_by_id);
389
}
390
391
// --------------------------------------------------------------------------