@cryptotaxi247 / netdata-1 / commits / 542967bf7

compare major:minor for finding the proper device mapper name; #2414

Costa Tsaousis (ktsaou) committed Jul 6, 2017 at 22:32 UTC 542967bf7ae71df1e3f07c4a56dbdfd0fc035c88
1 file changed +47 -45
src/proc_diskstats.c
+47 -45
@@ -53,49 +53,56 @@ static char *path_to_find_block_device = NULL;
53 static char *path_to_device_mapper = NULL;
54
55 static inline char *get_disk_name(unsigned long major, unsigned long minor, char *disk) {
56 - (void)minor;
57 -
56 static int enabled = 1;
57
60 - // we can find names only for device mapper (253)
61 - if(!enabled || major != 253) goto cleanup;
58 + if(!enabled) goto cleanup;
59
60 char filename[FILENAME_MAX + 1];
61 char link[FILENAME_MAX + 1];
62
63 DIR *dir = opendir(path_to_device_mapper);
64 if (!dir) {
68 - error("Cannot open directory '%s'.", path_to_device_mapper);
65 + error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot open directory '%s'. Disabling device-mapper support.", disk, major, minor, path_to_device_mapper);
66 enabled = 0;
67 goto cleanup;
68 }
69
73 - ssize_t dlen = strlen(disk);
70 struct dirent *de = NULL;
71 while ((de = readdir(dir))) {
72 + if(de->d_type != DT_LNK) continue;
73
77 - if((de->d_type == DT_LNK)) {
78 - snprintfz(filename, FILENAME_MAX, "%s/%s", path_to_device_mapper, de->d_name);
79 - ssize_t len = readlink(filename, link, FILENAME_MAX);
74 + snprintfz(filename, FILENAME_MAX, "%s/%s", path_to_device_mapper, de->d_name);
75 + ssize_t len = readlink(filename, link, FILENAME_MAX);
76 + if(len <= 0) {
77 + error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot read link '%s'.", disk, major, minor, filename);
78 + continue;
79 + }
80
81 - if(len > 0) {
82 - link[len] = '\0';
81 + link[len] = '\0';
82 + snprintfz(filename, FILENAME_MAX, "%s/%s", path_to_device_mapper, link);
83
84 - if(len > dlen && link[len-dlen-1] == '/' && !strcmp(&link[len - dlen], disk)) {
85 - // we have a match
86 - // info("Disk '%s': filename '%s' ('%s') is linked to '%s'. MATCHED", disk, filename, de->d_name, link);
87 - strncpy(link, de->d_name, FILENAME_MAX);
88 - netdata_fix_chart_id(link);
89 - closedir(dir);
84 + struct stat sb;
85 + if(stat(filename, &sb) == -1) {
86 + error("DEVICE-MAPPER ('%s', %lu:%lu): Cannot stat() file '%s'.", disk, major, minor, filename);
87 + continue;
88 + }
89
91 - return strdupz(link);
92 - }
93 - //else
94 - // info("Disk '%s': filename '%s' ('%s') is linked to '%s'. NOT MATCHED", disk, filename, de->d_name, link);
95 - }
96 - //else
97 - // error("Cannot read link '%s'", filename);
90 + if((sb.st_mode & S_IFMT) != S_IFBLK) {
91 + // info("DEVICE-MAPPER ('%s', %lu:%lu): file '%s' is not a block device.", disk, major, minor, filename);
92 + continue;
93 }
94 +
95 + if(major(sb.st_rdev) != major || minor(sb.st_rdev) != minor) {
96 + // info("DEVICE-MAPPER ('%s', %lu:%lu): filename '%s' does not match %lu:%lu.", disk, major, minor, filename, (unsigned long)major(sb.st_rdev), (unsigned long)minor(sb.st_rdev));
97 + continue;
98 + }
99 +
100 + // info("DEVICE-MAPPER ('%s', %lu:%lu): filename '%s' matches.", disk, major, minor, filename);
101 +
102 + strncpy(link, de->d_name, FILENAME_MAX);
103 + netdata_fix_chart_name(link);
104 + disk = link;
105 + break;
106 }
107 closedir(dir);
108
@@ -138,28 +145,9 @@ static struct disk *get_disk(unsigned long major, unsigned long minor, char *dis
145 last->next = d;
146 }
147
141 - // ------------------------------------------------------------------------
142 - // find the type of the device
143 -
144 - char buffer[FILENAME_MAX + 1];
145 -
146 - // get the default path for finding info about the block device
147 - if(unlikely(!path_to_find_block_device)) {
148 - snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/%s");
149 - path_to_find_block_device = config_get(CONFIG_SECTION_DISKSTATS, "path to get block device infos", buffer);
150 -
151 - snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s/queue/hw_sector_size");
152 - path_to_get_hw_sector_size = config_get(CONFIG_SECTION_DISKSTATS, "path to get h/w sector size", buffer);
153 -
154 - snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/subsystem/%s/../queue/hw_sector_size");
155 - path_to_get_hw_sector_size_partitions = config_get(CONFIG_SECTION_DISKSTATS, "path to get h/w sector size for partitions", buffer);
156 -
157 - snprintfz(buffer, FILENAME_MAX, "%s/dev/mapper", netdata_configured_host_prefix);
158 - path_to_device_mapper = config_get(CONFIG_SECTION_DISKSTATS, "path to device mapper", buffer);
159 - }
160 -
148 // find if it is a partition
149 // by checking if /sys/dev/block/MAJOR:MINOR/partition is readable.
150 + char buffer[FILENAME_MAX + 1];
151 snprintfz(buffer, FILENAME_MAX, path_to_find_block_device, major, minor, "partition");
152 if(likely(access(buffer, R_OK) == 0)) {
153 d->type = DISK_TYPE_PARTITION;
@@ -285,6 +273,8 @@ int do_proc_diskstats(int update_every, usec_t dt) {
273 globals_initialized = 0;
274
275 if(unlikely(!globals_initialized)) {
276 + globals_initialized = 1;
277 +
278 global_enable_new_disks_detected_at_runtime = config_get_boolean(CONFIG_SECTION_DISKSTATS, "enable new disks detected at runtime", global_enable_new_disks_detected_at_runtime);
279 global_enable_performance_for_physical_disks = config_get_boolean_ondemand(CONFIG_SECTION_DISKSTATS, "performance metrics for physical disks", global_enable_performance_for_physical_disks);
280 global_enable_performance_for_virtual_disks = config_get_boolean_ondemand(CONFIG_SECTION_DISKSTATS, "performance metrics for virtual disks", global_enable_performance_for_virtual_disks);
@@ -298,7 +288,19 @@ int do_proc_diskstats(int update_every, usec_t dt) {
288 global_do_util = config_get_boolean_ondemand(CONFIG_SECTION_DISKSTATS, "utilization percentage for all disks", global_do_util);
289 global_do_backlog = config_get_boolean_ondemand(CONFIG_SECTION_DISKSTATS, "backlog for all disks", global_do_backlog);
290
301 - globals_initialized = 1;
291 + char buffer[FILENAME_MAX + 1];
292 +
293 + snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/%s");
294 + path_to_find_block_device = config_get(CONFIG_SECTION_DISKSTATS, "path to get block device infos", buffer);
295 +
296 + snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/block/%s/queue/hw_sector_size");
297 + path_to_get_hw_sector_size = config_get(CONFIG_SECTION_DISKSTATS, "path to get h/w sector size", buffer);
298 +
299 + snprintfz(buffer, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/dev/block/%lu:%lu/subsystem/%s/../queue/hw_sector_size");
300 + path_to_get_hw_sector_size_partitions = config_get(CONFIG_SECTION_DISKSTATS, "path to get h/w sector size for partitions", buffer);
301 +
302 + snprintfz(buffer, FILENAME_MAX, "%s/dev/mapper", netdata_configured_host_prefix);
303 + path_to_device_mapper = config_get(CONFIG_SECTION_DISKSTATS, "path to device mapper", buffer);
304 }
305
306 // --------------------------------------------------------------------------