2 * collectd - src/disk.c
3 * Copyright (C) 2005-2012 Florian octo Forster
4 * Copyright (C) 2009 Manuel Sanmartin
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at collectd.org>
28 #include "utils_ignorelist.h"
30 #if HAVE_MACH_MACH_TYPES_H
31 # include <mach/mach_types.h>
33 #if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
39 #if HAVE_MACH_MACH_PORT_H
40 # include <mach/mach_port.h>
42 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
43 # include <CoreFoundation/CoreFoundation.h>
45 #if HAVE_IOKIT_IOKITLIB_H
46 # include <IOKit/IOKitLib.h>
48 #if HAVE_IOKIT_IOTYPES_H
49 # include <IOKit/IOTypes.h>
51 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
52 # include <IOKit/storage/IOBlockStorageDriver.h>
54 #if HAVE_IOKIT_IOBSD_H
55 # include <IOKit/IOBSD.h>
66 # define UINT_MAX 4294967295U
70 # include <statgrab.h>
74 # ifndef _AIXVERSION_610
75 # include <sys/systemcfg.h>
77 # include <sys/protosw.h>
78 # include <libperfstat.h>
81 #if HAVE_IOKIT_IOKITLIB_H
82 static mach_port_t io_master_port = MACH_PORT_NULL;
83 /* This defaults to false for backwards compatibility. Please fix in the next
85 static _Bool use_bsd_name = 0;
86 /* #endif HAVE_IOKIT_IOKITLIB_H */
89 typedef struct diskstats
93 /* This overflows in roughly 1361 years */
94 unsigned int poll_count;
96 derive_t read_sectors;
97 derive_t write_sectors;
100 derive_t write_bytes;
107 derive_t avg_read_time;
108 derive_t avg_write_time;
111 _Bool has_in_progress;
114 struct diskstats *next;
117 static diskstats_t *disklist;
118 /* #endif KERNEL_LINUX */
120 static struct gmesh geom_tree;
121 /* #endif KERNEL_FREEBSD */
124 #define MAX_NUMDISK 1024
125 extern kstat_ctl_t *kc;
126 static kstat_t *ksp[MAX_NUMDISK];
127 static int numdisk = 0;
128 /* #endif HAVE_LIBKSTAT */
130 #elif defined(HAVE_LIBSTATGRAB)
131 /* #endif HAVE_LIBSTATGRAB */
134 static perfstat_disk_t * stat_disk;
137 /* #endif HAVE_PERFSTAT */
140 # error "No applicable input method."
146 static char *conf_udev_name_attr = NULL;
147 static struct udev *handle_udev;
150 static const char *config_keys[] =
157 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
159 static ignorelist_t *ignorelist = NULL;
161 static int disk_config (const char *key, const char *value)
163 if (ignorelist == NULL)
164 ignorelist = ignorelist_create (/* invert = */ 1);
165 if (ignorelist == NULL)
168 if (strcasecmp ("Disk", key) == 0)
170 ignorelist_add (ignorelist, value);
172 else if (strcasecmp ("IgnoreSelected", key) == 0)
177 ignorelist_set_invert (ignorelist, invert);
179 else if (strcasecmp ("UseBSDName", key) == 0)
181 #if HAVE_IOKIT_IOKITLIB_H
182 use_bsd_name = IS_TRUE (value) ? 1 : 0;
184 WARNING ("disk plugin: The \"UseBSDName\" option is only supported "
185 "on Mach / Mac OS X and will be ignored.");
188 else if (strcasecmp ("UdevNameAttr", key) == 0)
191 if (conf_udev_name_attr != NULL)
193 free (conf_udev_name_attr);
194 conf_udev_name_attr = NULL;
196 if ((conf_udev_name_attr = strdup (value)) == NULL)
199 WARNING ("disk plugin: The \"UdevNameAttr\" option is only supported "
200 "if collectd is built with libudev support");
209 } /* int disk_config */
211 static int disk_init (void)
213 #if HAVE_IOKIT_IOKITLIB_H
214 kern_return_t status;
216 if (io_master_port != MACH_PORT_NULL)
218 mach_port_deallocate (mach_task_self (),
220 io_master_port = MACH_PORT_NULL;
223 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
224 if (status != kIOReturnSuccess)
226 ERROR ("IOMasterPort failed: %s",
227 mach_error_string (status));
228 io_master_port = MACH_PORT_NULL;
231 /* #endif HAVE_IOKIT_IOKITLIB_H */
235 if (conf_udev_name_attr != NULL)
237 handle_udev = udev_new();
238 if (handle_udev == NULL) {
239 ERROR ("disk plugin: udev_new() failed!");
243 #endif /* HAVE_LIBUDEV */
244 /* #endif KERNEL_LINUX */
249 rv = geom_gettree(&geom_tree);
251 ERROR ("geom_gettree() failed, returned %d", rv);
254 rv = geom_stats_open();
256 ERROR ("geom_stats_open() failed, returned %d", rv);
259 /* #endif KERNEL_FREEBSD */
269 for (numdisk = 0, ksp_chain = kc->kc_chain;
270 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
271 ksp_chain = ksp_chain->ks_next)
273 if (strncmp (ksp_chain->ks_class, "disk", 4)
274 && strncmp (ksp_chain->ks_class, "partition", 9))
276 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
278 ksp[numdisk++] = ksp_chain;
280 #endif /* HAVE_LIBKSTAT */
283 } /* int disk_init */
285 static int disk_shutdown (void)
289 if (handle_udev != NULL)
290 udev_unref(handle_udev);
291 #endif /* HAVE_LIBUDEV */
292 #endif /* KERNEL_LINUX */
294 } /* int disk_shutdown */
296 static void disk_submit (const char *plugin_instance,
298 derive_t read, derive_t write)
300 value_list_t vl = VALUE_LIST_INIT;
307 vl.values_len = STATIC_ARRAY_SIZE (values);
308 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
309 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
310 sstrncpy (vl.plugin_instance, plugin_instance,
311 sizeof (vl.plugin_instance));
312 sstrncpy (vl.type, type, sizeof (vl.type));
314 plugin_dispatch_values (&vl);
315 } /* void disk_submit */
317 #if KERNEL_FREEBSD || KERNEL_LINUX
318 static void submit_io_time (char const *plugin_instance, derive_t io_time, derive_t weighted_time)
320 value_list_t vl = VALUE_LIST_INIT;
322 { .derive = io_time },
323 { .derive = weighted_time },
327 vl.values_len = STATIC_ARRAY_SIZE (values);
328 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
329 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
330 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
331 sstrncpy (vl.type, "disk_io_time", sizeof (vl.type));
333 plugin_dispatch_values (&vl);
334 } /* void submit_io_time */
335 #endif /* KERNEL_FREEBSD || KERNEL_LINUX */
338 static void submit_in_progress (char const *disk_name, gauge_t in_progress)
340 value_list_t vl = VALUE_LIST_INIT;
342 vl.values = &(value_t) { .gauge = in_progress };
344 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
345 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
346 sstrncpy (vl.plugin_instance, disk_name, sizeof (vl.plugin_instance));
347 sstrncpy (vl.type, "pending_operations", sizeof (vl.type));
349 plugin_dispatch_values (&vl);
352 static counter_t disk_calc_time_incr (counter_t delta_time, counter_t delta_ops)
354 double interval = CDTIME_T_TO_DOUBLE (plugin_get_interval ());
355 double avg_time = ((double) delta_time) / ((double) delta_ops);
356 double avg_time_incr = interval * avg_time;
358 return ((counter_t) (avg_time_incr + .5));
364 * Attempt to provide an rename disk instance from an assigned udev attribute.
366 * On success, it returns a strduped char* to the desired attribute value.
367 * Otherwise it returns NULL.
370 static char *disk_udev_attr_name (struct udev *udev, char *disk_name, const char *attr)
372 struct udev_device *dev;
376 dev = udev_device_new_from_subsystem_sysname (udev, "block", disk_name);
379 prop = udev_device_get_property_value (dev, attr);
381 output = strdup (prop);
382 DEBUG ("disk plugin: renaming %s => %s", disk_name, output);
384 udev_device_unref (dev);
390 #if HAVE_IOKIT_IOKITLIB_H
391 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
393 signed long long val_int;
397 /* `key_obj' needs to be released. */
398 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
399 kCFStringEncodingASCII);
402 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
406 /* get => we don't need to release (== free) the object */
407 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
413 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
417 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
419 DEBUG ("CFNumberGetValue (%s) failed.", key);
425 #endif /* HAVE_IOKIT_IOKITLIB_H */
427 static int disk_read (void)
429 #if HAVE_IOKIT_IOKITLIB_H
430 io_registry_entry_t disk;
431 io_registry_entry_t disk_child;
432 io_iterator_t disk_list;
433 CFMutableDictionaryRef props_dict, child_dict;
434 CFDictionaryRef stats_dict;
435 CFStringRef tmp_cf_string_ref;
436 kern_return_t status;
438 signed long long read_ops, read_byt, read_tme;
439 signed long long write_ops, write_byt, write_tme;
441 int disk_major, disk_minor;
442 char disk_name[DATA_MAX_NAME_LEN];
443 char child_disk_name_bsd[DATA_MAX_NAME_LEN], props_disk_name_bsd[DATA_MAX_NAME_LEN];
445 /* Get the list of all disk objects. */
446 if (IOServiceGetMatchingServices (io_master_port, IOServiceMatching (kIOBlockStorageDriverClass), &disk_list) != kIOReturnSuccess) {
447 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
451 while ((disk = IOIteratorNext (disk_list)) != 0) {
456 /* get child of disk entry and corresponding property dictionary */
457 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child)) != kIOReturnSuccess) {
458 /* This fails for example for DVD/CD drives, which we want to ignore anyway */
459 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
460 IOObjectRelease (disk);
463 if (IORegistryEntryCreateCFProperties (disk_child, (CFMutableDictionaryRef *) &child_dict, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess || child_dict == NULL) {
464 ERROR ("disk plugin: IORegistryEntryCreateCFProperties (disk_child) failed.");
465 IOObjectRelease (disk_child);
466 IOObjectRelease (disk);
470 /* extract name and major/minor numbers */
471 memset (child_disk_name_bsd, 0, sizeof (child_disk_name_bsd));
472 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (child_dict, CFSTR(kIOBSDNameKey));
473 if (tmp_cf_string_ref) {
474 assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
475 CFStringGetCString (tmp_cf_string_ref, child_disk_name_bsd, sizeof (child_disk_name_bsd), kCFStringEncodingUTF8);
477 disk_major = (int) dict_get_value (child_dict, kIOBSDMajorKey);
478 disk_minor = (int) dict_get_value (child_dict, kIOBSDMinorKey);
479 DEBUG ("disk plugin: child_disk_name_bsd=\"%s\" major=%d minor=%d", child_disk_name_bsd, disk_major, disk_minor);
480 CFRelease (child_dict);
481 IOObjectRelease (disk_child);
483 /* get property dictionary of the disk entry itself */
484 if (IORegistryEntryCreateCFProperties (disk, (CFMutableDictionaryRef *) &props_dict, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess || props_dict == NULL) {
485 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
486 IOObjectRelease (disk);
490 /* extract name and stats dictionary */
491 memset (props_disk_name_bsd, 0, sizeof (props_disk_name_bsd));
492 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (props_dict, CFSTR(kIOBSDNameKey));
493 if (tmp_cf_string_ref) {
494 assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
495 CFStringGetCString (tmp_cf_string_ref, props_disk_name_bsd, sizeof (props_disk_name_bsd), kCFStringEncodingUTF8);
497 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict, CFSTR (kIOBlockStorageDriverStatisticsKey));
498 if (stats_dict == NULL) {
499 ERROR ("disk plugin: CFDictionaryGetValue (%s) failed.", kIOBlockStorageDriverStatisticsKey);
500 CFRelease (props_dict);
501 IOObjectRelease (disk);
504 DEBUG ("disk plugin: props_disk_name_bsd=\"%s\"", props_disk_name_bsd);
508 if (child_disk_name_bsd[0] != 0)
509 sstrncpy (disk_name, child_disk_name_bsd, sizeof (disk_name));
510 else if (props_disk_name_bsd[0] != 0)
511 sstrncpy (disk_name, props_disk_name_bsd, sizeof (disk_name));
513 ERROR ("disk plugin: can't find bsd disk name.");
514 ssnprintf (disk_name, sizeof (disk_name), "%i-%i", disk_major, disk_minor);
518 ssnprintf (disk_name, sizeof (disk_name), "%i-%i", disk_major, disk_minor);
520 DEBUG ("disk plugin: disk_name = \"%s\"", disk_name);
522 /* check the name against ignore list */
523 if (ignorelist_match (ignorelist, disk_name) != 0) {
524 CFRelease (props_dict);
525 IOObjectRelease (disk);
529 /* extract the stats */
530 read_ops = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsReadsKey);
531 read_byt = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsBytesReadKey);
532 read_tme = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsTotalReadTimeKey);
533 write_ops = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsWritesKey);
534 write_byt = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsBytesWrittenKey);
535 write_tme = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
536 CFRelease (props_dict);
537 IOObjectRelease (disk);
540 if ((read_byt != -1LL) || (write_byt != -1LL))
541 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
542 if ((read_ops != -1LL) || (write_ops != -1LL))
543 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
544 if ((read_tme != -1LL) || (write_tme != -1LL))
545 disk_submit (disk_name, "disk_time", read_tme / 1000, write_tme / 1000);
548 IOObjectRelease (disk_list);
549 /* #endif HAVE_IOKIT_IOKITLIB_H */
555 struct devstat *snap_iter;
557 struct gident *geom_id;
559 const char *disk_name;
560 long double read_time, write_time, busy_time, total_duration;
562 for (retry = 0, dirty = 1; retry < 5 && dirty == 1; retry++) {
564 geom_stats_snapshot_free(snap);
566 /* Get a fresh copy of stats snapshot */
567 snap = geom_stats_snapshot_get();
569 ERROR("disk plugin: geom_stats_snapshot_get() failed.");
573 /* Check if we have dirty read from this snapshot */
575 geom_stats_snapshot_reset(snap);
576 while ((snap_iter = geom_stats_snapshot_next(snap)) != NULL) {
577 if (snap_iter->id == NULL)
579 geom_id = geom_lookupid(&geom_tree, snap_iter->id);
581 /* New device? refresh GEOM tree */
582 if (geom_id == NULL) {
583 geom_deletetree(&geom_tree);
584 if (geom_gettree(&geom_tree) != 0) {
585 ERROR("disk plugin: geom_gettree() failed");
586 geom_stats_snapshot_free(snap);
589 geom_id = geom_lookupid(&geom_tree, snap_iter->id);
592 * This should be rare: the device come right before we take the
593 * snapshot and went away right after it. We will handle this
594 * case later, so don't mark dirty but silently ignore it.
599 /* Only collect PROVIDER data */
600 if (geom_id->lg_what != ISPROVIDER)
603 /* Only collect data when rank is 1 (physical devices) */
604 if (((struct gprovider *)(geom_id->lg_ptr))->lg_geom->lg_rank != 1)
607 /* Check if this is a dirty read quit for another try */
608 if (snap_iter->sequence0 != snap_iter->sequence1) {
616 geom_stats_snapshot_reset(snap);
618 snap_iter = geom_stats_snapshot_next(snap);
619 if (snap_iter == NULL)
622 if (snap_iter->id == NULL)
624 geom_id = geom_lookupid(&geom_tree, snap_iter->id);
627 if (geom_id->lg_what != ISPROVIDER)
629 if (((struct gprovider *)(geom_id->lg_ptr))->lg_geom->lg_rank != 1)
631 /* Skip dirty reads, if present */
632 if (dirty && (snap_iter->sequence0 != snap_iter->sequence1))
635 disk_name = ((struct gprovider *)geom_id->lg_ptr)->lg_name;
637 if (ignorelist_match (ignorelist, disk_name) != 0)
640 if ((snap_iter->bytes[DEVSTAT_READ] != 0) || (snap_iter->bytes[DEVSTAT_WRITE] != 0)) {
641 disk_submit(disk_name, "disk_octets",
642 (derive_t)snap_iter->bytes[DEVSTAT_READ],
643 (derive_t)snap_iter->bytes[DEVSTAT_WRITE]);
646 if ((snap_iter->operations[DEVSTAT_READ] != 0) || (snap_iter->operations[DEVSTAT_WRITE] != 0)) {
647 disk_submit(disk_name, "disk_ops",
648 (derive_t)snap_iter->operations[DEVSTAT_READ],
649 (derive_t)snap_iter->operations[DEVSTAT_WRITE]);
652 read_time = devstat_compute_etime(&snap_iter->duration[DEVSTAT_READ], NULL);
653 write_time = devstat_compute_etime(&snap_iter->duration[DEVSTAT_WRITE], NULL);
654 if ((read_time != 0) || (write_time != 0)) {
655 disk_submit (disk_name, "disk_time",
656 (derive_t)(read_time*1000), (derive_t)(write_time*1000));
658 if (devstat_compute_statistics(snap_iter, NULL, 1.0,
659 DSM_TOTAL_BUSY_TIME, &busy_time,
660 DSM_TOTAL_DURATION, &total_duration,
662 WARNING("%s", devstat_errbuf);
666 submit_io_time(disk_name, busy_time, total_duration);
669 geom_stats_snapshot_free(snap);
681 derive_t read_sectors = 0;
682 derive_t write_sectors = 0;
684 derive_t read_ops = 0;
685 derive_t read_merged = 0;
686 derive_t read_time = 0;
687 derive_t write_ops = 0;
688 derive_t write_merged = 0;
689 derive_t write_time = 0;
690 gauge_t in_progress = NAN;
691 derive_t io_time = 0;
692 derive_t weighted_time = 0;
695 diskstats_t *ds, *pre_ds;
697 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
699 fh = fopen ("/proc/partitions", "r");
702 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
706 /* Kernel is 2.4.* */
710 while (fgets (buffer, sizeof (buffer), fh) != NULL)
715 numfields = strsplit (buffer, fields, 32);
717 if ((numfields != (14 + fieldshift)) && (numfields != 7))
720 minor = atoll (fields[1]);
722 disk_name = fields[2 + fieldshift];
724 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
725 if (strcmp (disk_name, ds->name) == 0)
730 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
733 if ((ds->name = strdup (disk_name)) == NULL)
748 /* Kernel 2.6, Partition */
749 read_ops = atoll (fields[3]);
750 read_sectors = atoll (fields[4]);
751 write_ops = atoll (fields[5]);
752 write_sectors = atoll (fields[6]);
754 else if (numfields == (14 + fieldshift))
756 read_ops = atoll (fields[3 + fieldshift]);
757 write_ops = atoll (fields[7 + fieldshift]);
759 read_sectors = atoll (fields[5 + fieldshift]);
760 write_sectors = atoll (fields[9 + fieldshift]);
762 if ((fieldshift == 0) || (minor == 0))
765 read_merged = atoll (fields[4 + fieldshift]);
766 read_time = atoll (fields[6 + fieldshift]);
767 write_merged = atoll (fields[8 + fieldshift]);
768 write_time = atoll (fields[10+ fieldshift]);
770 in_progress = atof (fields[11 + fieldshift]);
772 io_time = atof (fields[12 + fieldshift]);
773 weighted_time = atof (fields[13 + fieldshift]);
778 DEBUG ("numfields = %i; => unknown file format.", numfields);
783 derive_t diff_read_sectors;
784 derive_t diff_write_sectors;
786 /* If the counter wraps around, it's only 32 bits.. */
787 if (read_sectors < ds->read_sectors)
788 diff_read_sectors = 1 + read_sectors
789 + (UINT_MAX - ds->read_sectors);
791 diff_read_sectors = read_sectors - ds->read_sectors;
792 if (write_sectors < ds->write_sectors)
793 diff_write_sectors = 1 + write_sectors
794 + (UINT_MAX - ds->write_sectors);
796 diff_write_sectors = write_sectors - ds->write_sectors;
798 ds->read_bytes += 512 * diff_read_sectors;
799 ds->write_bytes += 512 * diff_write_sectors;
800 ds->read_sectors = read_sectors;
801 ds->write_sectors = write_sectors;
804 /* Calculate the average time an io-op needs to complete */
807 derive_t diff_read_ops;
808 derive_t diff_write_ops;
809 derive_t diff_read_time;
810 derive_t diff_write_time;
812 if (read_ops < ds->read_ops)
813 diff_read_ops = 1 + read_ops
814 + (UINT_MAX - ds->read_ops);
816 diff_read_ops = read_ops - ds->read_ops;
817 DEBUG ("disk plugin: disk_name = %s; read_ops = %"PRIi64"; "
818 "ds->read_ops = %"PRIi64"; diff_read_ops = %"PRIi64";",
820 read_ops, ds->read_ops, diff_read_ops);
822 if (write_ops < ds->write_ops)
823 diff_write_ops = 1 + write_ops
824 + (UINT_MAX - ds->write_ops);
826 diff_write_ops = write_ops - ds->write_ops;
828 if (read_time < ds->read_time)
829 diff_read_time = 1 + read_time
830 + (UINT_MAX - ds->read_time);
832 diff_read_time = read_time - ds->read_time;
834 if (write_time < ds->write_time)
835 diff_write_time = 1 + write_time
836 + (UINT_MAX - ds->write_time);
838 diff_write_time = write_time - ds->write_time;
840 if (diff_read_ops != 0)
841 ds->avg_read_time += disk_calc_time_incr (
842 diff_read_time, diff_read_ops);
843 if (diff_write_ops != 0)
844 ds->avg_write_time += disk_calc_time_incr (
845 diff_write_time, diff_write_ops);
847 ds->read_ops = read_ops;
848 ds->read_time = read_time;
849 ds->write_ops = write_ops;
850 ds->write_time = write_time;
852 if (read_merged || write_merged)
856 ds->has_in_progress = 1;
863 /* Don't write to the RRDs if we've just started.. */
865 if (ds->poll_count <= 2)
867 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
868 "(min_poll_count = 2); => Not writing.",
873 if ((read_ops == 0) && (write_ops == 0))
875 DEBUG ("disk plugin: ((read_ops == 0) && "
876 "(write_ops == 0)); => Not writing.");
880 output_name = disk_name;
883 char *alt_name = NULL;
884 if (conf_udev_name_attr != NULL)
886 alt_name = disk_udev_attr_name (handle_udev, disk_name, conf_udev_name_attr);
887 if (alt_name != NULL)
888 output_name = alt_name;
892 if (ignorelist_match (ignorelist, output_name) != 0)
895 /* release udev-based alternate name, if allocated */
901 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
902 disk_submit (output_name, "disk_octets",
903 ds->read_bytes, ds->write_bytes);
905 if ((ds->read_ops != 0) || (ds->write_ops != 0))
906 disk_submit (output_name, "disk_ops",
907 read_ops, write_ops);
909 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
910 disk_submit (output_name, "disk_time",
911 ds->avg_read_time, ds->avg_write_time);
916 disk_submit (output_name, "disk_merged",
917 read_merged, write_merged);
918 if (ds->has_in_progress)
919 submit_in_progress (output_name, in_progress);
921 submit_io_time (output_name, io_time, weighted_time);
925 /* release udev-based alternate name, if allocated */
928 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
932 /* #endif defined(KERNEL_LINUX) */
935 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
936 # define KIO_ROCTETS reads
937 # define KIO_WOCTETS writes
938 # define KIO_ROPS nreads
939 # define KIO_WOPS nwrites
940 # define KIO_RTIME rtime
941 # define KIO_WTIME wtime
942 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
943 # define KIO_ROCTETS nread
944 # define KIO_WOCTETS nwritten
945 # define KIO_ROPS reads
946 # define KIO_WOPS writes
947 # define KIO_RTIME rtime
948 # define KIO_WTIME wtime
950 # error "kstat_io_t does not have the required members"
952 static kstat_io_t kio;
957 for (int i = 0; i < numdisk; i++)
959 if (kstat_read (kc, ksp[i], &kio) == -1)
962 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
964 if (ignorelist_match (ignorelist, ksp[i]->ks_name) != 0)
967 disk_submit (ksp[i]->ks_name, "disk_octets",
968 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
969 disk_submit (ksp[i]->ks_name, "disk_ops",
970 kio.KIO_ROPS, kio.KIO_WOPS);
971 /* FIXME: Convert this to microseconds if necessary */
972 disk_submit (ksp[i]->ks_name, "disk_time",
973 kio.KIO_RTIME, kio.KIO_WTIME);
975 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
977 if (ignorelist_match (ignorelist, ksp[i]->ks_name) != 0)
980 disk_submit (ksp[i]->ks_name, "disk_octets",
981 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
982 disk_submit (ksp[i]->ks_name, "disk_ops",
983 kio.KIO_ROPS, kio.KIO_WOPS);
986 /* #endif defined(HAVE_LIBKSTAT) */
988 #elif defined(HAVE_LIBSTATGRAB)
989 sg_disk_io_stats *ds;
990 # if HAVE_LIBSTATGRAB_0_90
995 char name[DATA_MAX_NAME_LEN];
997 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
1000 for (int counter = 0; counter < disks; counter++) {
1001 strncpy(name, ds->disk_name, sizeof(name));
1002 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
1004 if (ignorelist_match (ignorelist, name) != 0) {
1009 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
1012 /* #endif defined(HAVE_LIBSTATGRAB) */
1014 #elif defined(HAVE_PERFSTAT)
1015 derive_t read_sectors;
1016 derive_t write_sectors;
1018 derive_t write_time;
1021 perfstat_id_t firstpath;
1024 if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0)
1027 WARNING ("disk plugin: perfstat_disk: %s",
1028 sstrerror (errno, errbuf, sizeof (errbuf)));
1032 if (numdisk != pnumdisk || stat_disk==NULL) {
1033 if (stat_disk!=NULL)
1035 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
1039 firstpath.name[0]='\0';
1040 if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0)
1043 WARNING ("disk plugin: perfstat_disk : %s",
1044 sstrerror (errno, errbuf, sizeof (errbuf)));
1048 for (int i = 0; i < rnumdisk; i++)
1050 if (ignorelist_match (ignorelist, stat_disk[i].name) != 0)
1053 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
1054 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
1055 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
1057 read_ops = stat_disk[i].xrate;
1058 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
1059 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
1061 read_time = stat_disk[i].rserv;
1062 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
1063 write_time = stat_disk[i].wserv;
1064 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
1065 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
1067 #endif /* defined(HAVE_PERFSTAT) */
1070 } /* int disk_read */
1072 void module_register (void)
1074 plugin_register_config ("disk", disk_config,
1075 config_keys, config_keys_num);
1076 plugin_register_init ("disk", disk_init);
1077 plugin_register_shutdown ("disk", disk_shutdown);
1078 plugin_register_read ("disk", disk_read);
1079 } /* void module_register */