@cryptotaxi247 / netdata-1 / commits / 6fd02f1c2

Disk Labels (#15949)

Costa Tsaousis committed Sep 13, 2023 at 00:25 UTC 6fd02f1c24f0e24cef22c357f0fb66410c583a56
1 file changed +129
collectors/proc.plugin/proc_diskstats.c
+129
@@ -17,6 +17,11 @@
17 static struct disk {
18 char *disk; // the name of the disk (sda, sdb, etc, after being looked up)
19 char *device; // the device of the disk (before being looked up)
20 + char *disk_by_id;
21 + char *model;
22 + char *serial;
23 +// bool rotational;
24 +// bool removable;
25 uint32_t hash;
26 unsigned long major;
27 unsigned long minor;
@@ -172,6 +177,8 @@ static char *path_to_sys_block_device = NULL;
177 static char *path_to_sys_block_device_bcache = NULL;
178 static char *path_to_sys_devices_virtual_block_device = NULL;
179 static char *path_to_device_mapper = NULL;
180 +static char *path_to_dev_disk = NULL;
181 +static char *path_to_sys_block = NULL;
182 static char *path_to_device_label = NULL;
183 static char *path_to_device_id = NULL;
184 static char *path_to_veritas_volume_groups = NULL;
@@ -469,6 +476,109 @@ static inline char *get_disk_name(unsigned long major, unsigned long minor, char
476 return strdup(result);
477 }
478
479 +static inline bool ends_with(const char *str, const char *suffix) {
480 + if (!str || !suffix)
481 + return false;
482 +
483 + size_t len_str = strlen(str);
484 + size_t len_suffix = strlen(suffix);
485 + if (len_suffix > len_str)
486 + return false;
487 +
488 + return strncmp(str + len_str - len_suffix, suffix, len_suffix) == 0;
489 +}
490 +
491 +static inline char *get_disk_by_id(char *device) {
492 + char pathname[256 + 1];
493 + snprintfz(pathname, 256, "%s/by-id", path_to_dev_disk);
494 +
495 + struct dirent *entry;
496 + DIR *dp = opendir(pathname);
497 + if (dp == NULL) {
498 + internal_error(true, "Cannot open '%s'", pathname);
499 + return NULL;
500 + }
501 +
502 + while ((entry = readdir(dp))) {
503 + // We ignore the '.' and '..' entries
504 + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
505 + continue;
506 +
507 + if(strncmp(entry->d_name, "md-uuid-", 8) == 0 ||
508 + strncmp(entry->d_name, "dm-uuid-", 8) == 0 ||
509 + strncmp(entry->d_name, "nvme-eui.", 9) == 0 ||
510 + strncmp(entry->d_name, "wwn-", 4) == 0 ||
511 + strncmp(entry->d_name, "lvm-pv-uuid-", 12) == 0)
512 + continue;
513 +
514 + char link_target[256 + 1];
515 + char full_path[256 + 1];
516 + snprintfz(full_path, 256, "%s/%s", pathname, entry->d_name);
517 +
518 + ssize_t len = readlink(full_path, link_target, 256);
519 + if (len == -1)
520 + continue;
521 +
522 + link_target[len] = '\0';
523 +
524 + if (ends_with(link_target, device)) {
525 + char *s = strdupz(entry->d_name);
526 + closedir(dp);
527 + return s;
528 + }
529 + }
530 +
531 + closedir(dp);
532 + return NULL;
533 +}
534 +
535 +static inline char *get_disk_model(char *device) {
536 + char path[256 + 1];
537 + char buffer[256 + 1];
538 +
539 + snprintfz(path, 256, "%s/%s/device/model", path_to_sys_block, device);
540 + if(read_file(path, buffer, 256) != 0) {
541 + snprintfz(path, 256, "%s/%s/device/name", path_to_sys_block, device);
542 + if(read_file(path, buffer, 256) != 0)
543 + return NULL;
544 + }
545 +
546 + return strdupz(buffer);
547 +}
548 +
549 +static inline char *get_disk_serial(char *device) {
550 + char path[256 + 1];
551 + char buffer[256 + 1];
552 +
553 + snprintfz(path, 256, "%s/%s/device/serial", path_to_sys_block, device);
554 + if(read_file(path, buffer, 256) != 0)
555 + return NULL;
556 +
557 + return strdupz(buffer);
558 +}
559 +
560 +//static inline bool get_disk_rotational(char *device) {
561 +// char path[256 + 1];
562 +// char buffer[256 + 1];
563 +//
564 +// snprintfz(path, 256, "%s/%s/queue/rotational", path_to_sys_block, device);
565 +// if(read_file(path, buffer, 256) != 0)
566 +// return false;
567 +//
568 +// return buffer[0] == '1';
569 +//}
570 +//
571 +//static inline bool get_disk_removable(char *device) {
572 +// char path[256 + 1];
573 +// char buffer[256 + 1];
574 +//
575 +// snprintfz(path, 256, "%s/%s/removable", path_to_sys_block, device);
576 +// if(read_file(path, buffer, 256) != 0)
577 +// return false;
578 +//
579 +// return buffer[0] == '1';
580 +//}
581 +
582 static void get_disk_config(struct disk *d) {
583 int def_enable = global_enable_new_disks_detected_at_runtime;
584
@@ -594,6 +704,11 @@ static struct disk *get_disk(unsigned long major, unsigned long minor, char *dis
704
705 d->disk = get_disk_name(major, minor, disk);
706 d->device = strdupz(disk);
707 + d->disk_by_id = get_disk_by_id(disk);
708 + d->model = get_disk_model(disk);
709 + d->serial = get_disk_serial(disk);
710 +// d->rotational = get_disk_rotational(disk);
711 +// d->removable = get_disk_removable(disk);
712 d->hash = simple_hash(d->device);
713 d->major = major;
714 d->minor = minor;
@@ -854,6 +969,11 @@ static struct disk *get_disk(unsigned long major, unsigned long minor, char *dis
969 static void add_labels_to_disk(struct disk *d, RRDSET *st) {
970 rrdlabels_add(st->rrdlabels, "device", d->disk, RRDLABEL_SRC_AUTO);
971 rrdlabels_add(st->rrdlabels, "mount_point", d->mount_point, RRDLABEL_SRC_AUTO);
972 + rrdlabels_add(st->rrdlabels, "id", d->disk_by_id, RRDLABEL_SRC_AUTO);
973 + rrdlabels_add(st->rrdlabels, "model", d->model, RRDLABEL_SRC_AUTO);
974 + rrdlabels_add(st->rrdlabels, "serial", d->serial, RRDLABEL_SRC_AUTO);
975 +// rrdlabels_add(st->rrdlabels, "rotational", d->rotational ? "true" : "false", RRDLABEL_SRC_AUTO);
976 +// rrdlabels_add(st->rrdlabels, "removable", d->removable ? "true" : "false", RRDLABEL_SRC_AUTO);
977
978 switch (d->type) {
979 default:
@@ -922,6 +1042,12 @@ int do_proc_diskstats(int update_every, usec_t dt) {
1042 snprintfz(buffer, FILENAME_MAX, "%s/dev/mapper", netdata_configured_host_prefix);
1043 path_to_device_mapper = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to device mapper", buffer);
1044
1045 + snprintfz(buffer, FILENAME_MAX, "%s/dev/disk", netdata_configured_host_prefix);
1046 + path_to_dev_disk = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/disk", buffer);
1047 +
1048 + snprintfz(buffer, FILENAME_MAX, "%s/sys/block", netdata_configured_host_prefix);
1049 + path_to_sys_block = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /sys/block", buffer);
1050 +
1051 snprintfz(buffer, FILENAME_MAX, "%s/dev/disk/by-label", netdata_configured_host_prefix);
1052 path_to_device_label = config_get(CONFIG_SECTION_PLUGIN_PROC_DISKSTATS, "path to /dev/disk/by-label", buffer);
1053
@@ -2026,6 +2152,9 @@ int do_proc_diskstats(int update_every, usec_t dt) {
2152
2153 freez(t->disk);
2154 freez(t->device);
2155 + freez(t->disk_by_id);
2156 + freez(t->model);
2157 + freez(t->serial);
2158 freez(t->mount_point);
2159 freez(t->chart_id);
2160 freez(t);