X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fdisk.c;h=c02b6aba0b5cf61def3c48ea730cd2d686566c19;hp=c0408cecce76a6891e394bf96a00e308942ebe13;hb=48efd3deb4c9139fd060ff3d289896e9031bcc7c;hpb=75d0fe5e970f95e782784c55494b5d9274b270f0 diff --git a/src/disk.c b/src/disk.c index c0408cec..c02b6aba 100644 --- a/src/disk.c +++ b/src/disk.c @@ -23,9 +23,9 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" -#include "utils_ignorelist.h" +#include "utils/common/common.h" +#include "utils/ignorelist/ignorelist.h" #if HAVE_MACH_MACH_TYPES_H #include @@ -59,9 +59,6 @@ #include #endif -#if HAVE_LIMITS_H -#include -#endif #ifndef UINT_MAX #define UINT_MAX 4294967295U #endif @@ -209,7 +206,7 @@ static int disk_init(void) { io_master_port = MACH_PORT_NULL; return -1; } -/* #endif HAVE_IOKIT_IOKITLIB_H */ + /* #endif HAVE_IOKIT_IOKITLIB_H */ #elif KERNEL_LINUX #if HAVE_LIBUDEV_H @@ -221,7 +218,7 @@ static int disk_init(void) { } } #endif /* HAVE_LIBUDEV_H */ -/* #endif KERNEL_LINUX */ + /* #endif KERNEL_LINUX */ #elif KERNEL_FREEBSD int rv; @@ -236,7 +233,7 @@ static int disk_init(void) { ERROR("geom_stats_open() failed, returned %d", rv); return -1; } -/* #endif KERNEL_FREEBSD */ + /* #endif KERNEL_FREEBSD */ #elif HAVE_LIBKSTAT kstat_t *ksp_chain; @@ -275,7 +272,8 @@ static void disk_submit(const char *plugin_instance, const char *type, derive_t read, derive_t write) { value_list_t vl = VALUE_LIST_INIT; value_t values[] = { - {.derive = read}, {.derive = write}, + {.derive = read}, + {.derive = write}, }; vl.values = values; @@ -292,7 +290,8 @@ static void submit_io_time(char const *plugin_instance, derive_t io_time, derive_t weighted_time) { value_list_t vl = VALUE_LIST_INIT; value_t values[] = { - {.derive = io_time}, {.derive = weighted_time}, + {.derive = io_time}, + {.derive = weighted_time}, }; vl.values = values; @@ -493,10 +492,11 @@ static int disk_read(void) { sstrncpy(disk_name, props_disk_name_bsd, sizeof(disk_name)); else { ERROR("disk plugin: can't find bsd disk name."); - snprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor); + ssnprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, + disk_minor); } } else - snprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor); + ssnprintf(disk_name, sizeof(disk_name), "%i-%i", disk_major, disk_minor); DEBUG("disk plugin: disk_name = \"%s\"", disk_name); @@ -532,7 +532,7 @@ static int disk_read(void) { disk_submit(disk_name, "disk_time", read_tme / 1000, write_tme / 1000); } IOObjectRelease(disk_list); -/* #endif HAVE_IOKIT_IOKITLIB_H */ + /* #endif HAVE_IOKIT_IOKITLIB_H */ #elif KERNEL_FREEBSD int retry, dirty; @@ -662,9 +662,7 @@ static int disk_read(void) { char buffer[1024]; char *fields[32]; - int numfields; - - int minor = 0; + static unsigned int poll_count = 0; derive_t read_sectors = 0; derive_t write_sectors = 0; @@ -687,17 +685,15 @@ static int disk_read(void) { return -1; } + poll_count++; while (fgets(buffer, sizeof(buffer), fh) != NULL) { - char *disk_name; - char *output_name; + int numfields = strsplit(buffer, fields, 32); - numfields = strsplit(buffer, fields, 32); - if ((numfields != 14) && (numfields != 7)) + /* need either 7 fields (partition) or at least 14 fields */ + if ((numfields != 7) && (numfields < 14)) continue; - minor = atoll(fields[1]); - - disk_name = fields[2]; + char *disk_name = fields[2]; for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next) @@ -705,7 +701,7 @@ static int disk_read(void) { break; if (ds == NULL) { - if ((ds = (diskstats_t *)calloc(1, sizeof(diskstats_t))) == NULL) + if ((ds = calloc(1, sizeof(*ds))) == NULL) continue; if ((ds->name = strdup(disk_name)) == NULL) { @@ -726,28 +722,24 @@ static int disk_read(void) { read_sectors = atoll(fields[4]); write_ops = atoll(fields[5]); write_sectors = atoll(fields[6]); - } else if (numfields == 14) { + } else { + assert(numfields >= 14); read_ops = atoll(fields[3]); write_ops = atoll(fields[7]); read_sectors = atoll(fields[5]); write_sectors = atoll(fields[9]); - if (minor == 0) { - is_disk = 1; - read_merged = atoll(fields[4]); - read_time = atoll(fields[6]); - write_merged = atoll(fields[8]); - write_time = atoll(fields[10]); + is_disk = 1; + read_merged = atoll(fields[4]); + read_time = atoll(fields[6]); + write_merged = atoll(fields[8]); + write_time = atoll(fields[10]); - in_progress = atof(fields[11]); + in_progress = atof(fields[11]); - io_time = atof(fields[12]); - weighted_time = atof(fields[13]); - } - } else { - DEBUG("numfields = %i; => unknown file format.", numfields); - continue; + io_time = atof(fields[12]); + weighted_time = atof(fields[13]); } { @@ -822,14 +814,13 @@ static int disk_read(void) { } /* if (is_disk) */ - /* Don't write to the RRDs if we've just started.. */ - ds->poll_count++; - if (ds->poll_count <= 2) { - DEBUG("disk plugin: (ds->poll_count = %i) <= " - "(min_poll_count = 2); => Not writing.", - ds->poll_count); + /* Skip first cycle for newly-added disk */ + if (ds->poll_count == 0) { + DEBUG("disk plugin: (ds->poll_count = 0) => Skipping."); + ds->poll_count = poll_count; continue; } + ds->poll_count = poll_count; if ((read_ops == 0) && (write_ops == 0)) { DEBUG("disk plugin: ((read_ops == 0) && " @@ -837,7 +828,7 @@ static int disk_read(void) { continue; } - output_name = disk_name; + char *output_name = disk_name; #if HAVE_LIBUDEV_H char *alt_name = NULL; @@ -882,8 +873,30 @@ static int disk_read(void) { #endif } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */ + /* Remove disks that have disappeared from diskstats */ + for (ds = disklist, pre_ds = disklist; ds != NULL;) { + /* Disk exists */ + if (ds->poll_count == poll_count) { + pre_ds = ds; + ds = ds->next; + continue; + } + + /* Disk is missing, remove it */ + diskstats_t *missing_ds = ds; + if (ds == disklist) { + pre_ds = disklist = ds->next; + } else { + pre_ds->next = ds->next; + } + ds = ds->next; + + DEBUG("disk plugin: Disk %s disappeared.", missing_ds->name); + free(missing_ds->name); + free(missing_ds); + } fclose(fh); -/* #endif defined(KERNEL_LINUX) */ + /* #endif defined(KERNEL_LINUX) */ #elif HAVE_LIBKSTAT #if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME @@ -931,7 +944,7 @@ static int disk_read(void) { disk_submit(ksp[i]->ks_name, "disk_ops", kio.KIO_ROPS, kio.KIO_WOPS); } } -/* #endif defined(HAVE_LIBKSTAT) */ + /* #endif defined(HAVE_LIBKSTAT) */ #elif defined(HAVE_LIBSTATGRAB) sg_disk_io_stats *ds; @@ -958,7 +971,7 @@ static int disk_read(void) { disk_submit(name, "disk_octets", ds->read_bytes, ds->write_bytes); ds++; } -/* #endif defined(HAVE_LIBSTATGRAB) */ + /* #endif defined(HAVE_LIBSTATGRAB) */ #elif defined(HAVE_PERFSTAT) derive_t read_sectors; @@ -976,9 +989,8 @@ static int disk_read(void) { } if (numdisk != pnumdisk || stat_disk == NULL) { - if (stat_disk != NULL) - free(stat_disk); - stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t)); + free(stat_disk); + stat_disk = calloc(numdisk, sizeof(*stat_disk)); } pnumdisk = numdisk;