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>
27 #include "utils_ignorelist.h"
29 #if HAVE_MACH_MACH_TYPES_H
30 # include <mach/mach_types.h>
32 #if HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
35 #if HAVE_MACH_MACH_ERROR_H
36 # include <mach/mach_error.h>
38 #if HAVE_MACH_MACH_PORT_H
39 # include <mach/mach_port.h>
41 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
42 # include <CoreFoundation/CoreFoundation.h>
44 #if HAVE_IOKIT_IOKITLIB_H
45 # include <IOKit/IOKitLib.h>
47 #if HAVE_IOKIT_IOTYPES_H
48 # include <IOKit/IOTypes.h>
50 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
51 # include <IOKit/storage/IOBlockStorageDriver.h>
53 #if HAVE_IOKIT_IOBSD_H
54 # include <IOKit/IOBSD.h>
65 # define UINT_MAX 4294967295U
69 # include <statgrab.h>
73 # ifndef _AIXVERSION_610
74 # include <sys/systemcfg.h>
76 # include <sys/protosw.h>
77 # include <libperfstat.h>
80 #if HAVE_IOKIT_IOKITLIB_H
81 static mach_port_t io_master_port = MACH_PORT_NULL;
82 /* This defaults to false for backwards compatibility. Please fix in the next
84 static _Bool use_bsd_name = 0;
85 /* #endif HAVE_IOKIT_IOKITLIB_H */
88 typedef struct diskstats
92 /* This overflows in roughly 1361 years */
93 unsigned int poll_count;
95 derive_t read_sectors;
96 derive_t write_sectors;
106 derive_t avg_read_time;
107 derive_t avg_write_time;
109 struct diskstats *next;
112 static diskstats_t *disklist;
113 /* #endif KERNEL_LINUX */
115 static struct gmesh geom_tree;
116 /* #endif KERNEL_FREEBSD */
119 #define MAX_NUMDISK 1024
120 extern kstat_ctl_t *kc;
121 static kstat_t *ksp[MAX_NUMDISK];
122 static int numdisk = 0;
123 /* #endif HAVE_LIBKSTAT */
125 #elif defined(HAVE_LIBSTATGRAB)
126 /* #endif HAVE_LIBKSTATGRAB */
129 static perfstat_disk_t * stat_disk;
132 /* #endif HAVE_PERFSTAT */
135 # error "No applicable input method."
141 static char *conf_udev_name_attr = NULL;
142 static struct udev *handle_udev;
145 static const char *config_keys[] =
152 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
154 static ignorelist_t *ignorelist = NULL;
156 static int disk_config (const char *key, const char *value)
158 if (ignorelist == NULL)
159 ignorelist = ignorelist_create (/* invert = */ 1);
160 if (ignorelist == NULL)
163 if (strcasecmp ("Disk", key) == 0)
165 ignorelist_add (ignorelist, value);
167 else if (strcasecmp ("IgnoreSelected", key) == 0)
172 ignorelist_set_invert (ignorelist, invert);
174 else if (strcasecmp ("UseBSDName", key) == 0)
176 #if HAVE_IOKIT_IOKITLIB_H
177 use_bsd_name = IS_TRUE (value) ? 1 : 0;
179 WARNING ("disk plugin: The \"UseBSDName\" option is only supported "
180 "on Mach / Mac OS X and will be ignored.");
183 else if (strcasecmp ("UdevNameAttr", key) == 0)
186 if (conf_udev_name_attr != NULL)
188 free (conf_udev_name_attr);
189 conf_udev_name_attr = NULL;
191 if ((conf_udev_name_attr = strdup (value)) == NULL)
194 WARNING ("disk plugin: The \"UdevNameAttr\" option is only supported "
195 "if collectd is built with libudev support");
204 } /* int disk_config */
206 static int disk_init (void)
208 #if HAVE_IOKIT_IOKITLIB_H
209 kern_return_t status;
211 if (io_master_port != MACH_PORT_NULL)
213 mach_port_deallocate (mach_task_self (),
215 io_master_port = MACH_PORT_NULL;
218 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
219 if (status != kIOReturnSuccess)
221 ERROR ("IOMasterPort failed: %s",
222 mach_error_string (status));
223 io_master_port = MACH_PORT_NULL;
226 /* #endif HAVE_IOKIT_IOKITLIB_H */
230 /* #endif KERNEL_LINUX */
235 rv = geom_gettree(&geom_tree);
237 ERROR ("geom_gettree() failed, returned %d", rv);
240 rv = geom_stats_open();
242 ERROR ("geom_stats_open() failed, returned %d", rv);
245 /* #endif KERNEL_FREEBSD */
255 for (numdisk = 0, ksp_chain = kc->kc_chain;
256 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
257 ksp_chain = ksp_chain->ks_next)
259 if (strncmp (ksp_chain->ks_class, "disk", 4)
260 && strncmp (ksp_chain->ks_class, "partition", 9))
262 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
264 ksp[numdisk++] = ksp_chain;
266 #endif /* HAVE_LIBKSTAT */
269 } /* int disk_init */
271 static void disk_submit (const char *plugin_instance,
273 derive_t read, derive_t write)
276 value_list_t vl = VALUE_LIST_INIT;
278 /* Both `ignorelist' and `plugin_instance' may be NULL. */
279 if (ignorelist_match (ignorelist, plugin_instance) != 0)
282 values[0].derive = read;
283 values[1].derive = write;
287 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
288 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
289 sstrncpy (vl.plugin_instance, plugin_instance,
290 sizeof (vl.plugin_instance));
291 sstrncpy (vl.type, type, sizeof (vl.type));
293 plugin_dispatch_values (&vl);
294 } /* void disk_submit */
297 static void submit_in_progress (char const *disk_name, gauge_t in_progress)
300 value_list_t vl = VALUE_LIST_INIT;
302 if (ignorelist_match (ignorelist, disk_name) != 0)
305 v.gauge = in_progress;
309 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
310 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
311 sstrncpy (vl.plugin_instance, disk_name, sizeof (vl.plugin_instance));
312 sstrncpy (vl.type, "pending_operations", sizeof (vl.type));
314 plugin_dispatch_values (&vl);
317 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 if (ignorelist_match (ignorelist, plugin_instance) != 0)
325 values[0].derive = io_time;
326 values[1].derive = weighted_time;
330 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
331 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
332 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
333 sstrncpy (vl.type, "disk_io_time", sizeof (vl.type));
335 plugin_dispatch_values (&vl);
339 static counter_t disk_calc_time_incr (counter_t delta_time, counter_t delta_ops)
341 double interval = CDTIME_T_TO_DOUBLE (plugin_get_interval ());
342 double avg_time = ((double) delta_time) / ((double) delta_ops);
343 double avg_time_incr = interval * avg_time;
345 return ((counter_t) (avg_time_incr + .5));
351 * Attempt to provide an rename disk instance from an assigned udev attribute.
353 * On success, it returns a strduped char* to the desired attribute value.
354 * Otherwise it returns NULL.
357 static char *disk_udev_attr_name (struct udev *udev, char *disk_name, const char *attr)
359 struct udev_device *dev;
363 dev = udev_device_new_from_subsystem_sysname (udev, "block", disk_name);
366 prop = udev_device_get_property_value (dev, attr);
368 output = strdup (prop);
369 DEBUG ("disk plugin: renaming %s => %s", disk_name, output);
371 udev_device_unref (dev);
377 #if HAVE_IOKIT_IOKITLIB_H
378 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
380 signed long long val_int;
384 /* `key_obj' needs to be released. */
385 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
386 kCFStringEncodingASCII);
389 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
393 /* get => we don't need to release (== free) the object */
394 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
400 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
404 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
406 DEBUG ("CFNumberGetValue (%s) failed.", key);
412 #endif /* HAVE_IOKIT_IOKITLIB_H */
414 static int disk_read (void)
416 #if HAVE_IOKIT_IOKITLIB_H
417 io_registry_entry_t disk;
418 io_registry_entry_t disk_child;
419 io_iterator_t disk_list;
420 CFMutableDictionaryRef props_dict, child_dict;
421 CFDictionaryRef stats_dict;
422 CFStringRef tmp_cf_string_ref;
423 kern_return_t status;
425 signed long long read_ops, read_byt, read_tme;
426 signed long long write_ops, write_byt, write_tme;
428 int disk_major, disk_minor;
429 char disk_name[DATA_MAX_NAME_LEN];
430 char child_disk_name_bsd[DATA_MAX_NAME_LEN], props_disk_name_bsd[DATA_MAX_NAME_LEN];
432 /* Get the list of all disk objects. */
433 if (IOServiceGetMatchingServices (io_master_port, IOServiceMatching (kIOBlockStorageDriverClass), &disk_list) != kIOReturnSuccess) {
434 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
438 while ((disk = IOIteratorNext (disk_list)) != 0) {
443 /* get child of disk entry and corresponding property dictionary */
444 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child)) != kIOReturnSuccess) {
445 /* This fails for example for DVD/CD drives, which we want to ignore anyway */
446 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
447 IOObjectRelease (disk);
450 if (IORegistryEntryCreateCFProperties (disk_child, (CFMutableDictionaryRef *) &child_dict, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess || child_dict == NULL) {
451 ERROR ("disk plugin: IORegistryEntryCreateCFProperties (disk_child) failed.");
452 IOObjectRelease (disk_child);
453 IOObjectRelease (disk);
457 /* extract name and major/minor numbers */
458 memset (child_disk_name_bsd, 0, sizeof (child_disk_name_bsd));
459 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (child_dict, CFSTR(kIOBSDNameKey));
460 if (tmp_cf_string_ref) {
461 assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
462 CFStringGetCString (tmp_cf_string_ref, child_disk_name_bsd, sizeof (child_disk_name_bsd), kCFStringEncodingUTF8);
464 disk_major = (int) dict_get_value (child_dict, kIOBSDMajorKey);
465 disk_minor = (int) dict_get_value (child_dict, kIOBSDMinorKey);
466 DEBUG ("disk plugin: child_disk_name_bsd=\"%s\" major=%d minor=%d", child_disk_name_bsd, disk_major, disk_minor);
467 CFRelease (child_dict);
468 IOObjectRelease (disk_child);
470 /* get property dictionary of the disk entry itself */
471 if (IORegistryEntryCreateCFProperties (disk, (CFMutableDictionaryRef *) &props_dict, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess || props_dict == NULL) {
472 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
473 IOObjectRelease (disk);
477 /* extract name and stats dictionary */
478 memset (props_disk_name_bsd, 0, sizeof (props_disk_name_bsd));
479 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (props_dict, CFSTR(kIOBSDNameKey));
480 if (tmp_cf_string_ref) {
481 assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
482 CFStringGetCString (tmp_cf_string_ref, props_disk_name_bsd, sizeof (props_disk_name_bsd), kCFStringEncodingUTF8);
484 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict, CFSTR (kIOBlockStorageDriverStatisticsKey));
485 if (stats_dict == NULL) {
486 ERROR ("disk plugin: CFDictionaryGetValue (%s) failed.", kIOBlockStorageDriverStatisticsKey);
487 CFRelease (props_dict);
488 IOObjectRelease (disk);
491 DEBUG ("disk plugin: props_disk_name_bsd=\"%s\"", props_disk_name_bsd);
495 if (child_disk_name_bsd[0] != 0)
496 sstrncpy (disk_name, child_disk_name_bsd, sizeof (disk_name));
497 else if (props_disk_name_bsd[0] != 0)
498 sstrncpy (disk_name, props_disk_name_bsd, sizeof (disk_name));
500 ERROR ("disk plugin: can't find bsd disk name.");
501 ssnprintf (disk_name, sizeof (disk_name), "%i-%i", disk_major, disk_minor);
505 ssnprintf (disk_name, sizeof (disk_name), "%i-%i", disk_major, disk_minor);
507 /* extract the stats */
508 read_ops = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsReadsKey);
509 read_byt = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsBytesReadKey);
510 read_tme = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsTotalReadTimeKey);
511 write_ops = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsWritesKey);
512 write_byt = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsBytesWrittenKey);
513 write_tme = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
514 CFRelease (props_dict);
515 IOObjectRelease (disk);
518 DEBUG ("disk plugin: disk_name = \"%s\"", disk_name);
519 if ((read_byt != -1LL) || (write_byt != -1LL))
520 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
521 if ((read_ops != -1LL) || (write_ops != -1LL))
522 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
523 if ((read_tme != -1LL) || (write_tme != -1LL))
524 disk_submit (disk_name, "disk_time", read_tme / 1000, write_tme / 1000);
527 IOObjectRelease (disk_list);
528 /* #endif HAVE_IOKIT_IOKITLIB_H */
534 struct devstat *snap_iter;
536 struct gident *geom_id;
538 const char *disk_name;
539 long double read_time, write_time;
541 for (retry = 0, dirty = 1; retry < 5 && dirty == 1; retry++) {
543 geom_stats_snapshot_free(snap);
545 /* Get a fresh copy of stats snapshot */
546 snap = geom_stats_snapshot_get();
548 ERROR("disk plugin: geom_stats_snapshot_get() failed.");
552 /* Check if we have dirty read from this snapshot */
554 geom_stats_snapshot_reset(snap);
555 while ((snap_iter = geom_stats_snapshot_next(snap)) != NULL) {
556 if (snap_iter->id == NULL)
558 geom_id = geom_lookupid(&geom_tree, snap_iter->id);
560 /* New device? refresh GEOM tree */
561 if (geom_id == NULL) {
562 geom_deletetree(&geom_tree);
563 if (geom_gettree(&geom_tree) != 0) {
564 ERROR("disk plugin: geom_gettree() failed");
565 geom_stats_snapshot_free(snap);
568 geom_id = geom_lookupid(&geom_tree, snap_iter->id);
571 * This should be rare: the device come right before we take the
572 * snapshot and went away right after it. We will handle this
573 * case later, so don't mark dirty but silently ignore it.
578 /* Only collect PROVIDER data */
579 if (geom_id->lg_what != ISPROVIDER)
582 /* Only collect data when rank is 1 (physical devices) */
583 if (((struct gprovider *)(geom_id->lg_ptr))->lg_geom->lg_rank != 1)
586 /* Check if this is a dirty read quit for another try */
587 if (snap_iter->sequence0 != snap_iter->sequence1) {
595 geom_stats_snapshot_reset(snap);
597 snap_iter = geom_stats_snapshot_next(snap);
598 if (snap_iter == NULL)
601 if (snap_iter->id == NULL)
603 geom_id = geom_lookupid(&geom_tree, snap_iter->id);
606 if (geom_id->lg_what != ISPROVIDER)
608 if (((struct gprovider *)(geom_id->lg_ptr))->lg_geom->lg_rank != 1)
610 /* Skip dirty reads, if present */
611 if (dirty && (snap_iter->sequence0 != snap_iter->sequence1))
614 disk_name = ((struct gprovider *)geom_id->lg_ptr)->lg_name;
616 if ((snap_iter->bytes[DEVSTAT_READ] != 0) || (snap_iter->bytes[DEVSTAT_WRITE] != 0)) {
617 disk_submit(disk_name, "disk_octets",
618 (derive_t)snap_iter->bytes[DEVSTAT_READ],
619 (derive_t)snap_iter->bytes[DEVSTAT_WRITE]);
622 if ((snap_iter->operations[DEVSTAT_READ] != 0) || (snap_iter->operations[DEVSTAT_WRITE] != 0)) {
623 disk_submit(disk_name, "disk_ops",
624 (derive_t)snap_iter->operations[DEVSTAT_READ],
625 (derive_t)snap_iter->operations[DEVSTAT_WRITE]);
628 read_time = devstat_compute_etime(&snap_iter->duration[DEVSTAT_READ], NULL);
629 write_time = devstat_compute_etime(&snap_iter->duration[DEVSTAT_WRITE], NULL);
630 if ((read_time != 0) || (write_time != 0)) {
631 disk_submit (disk_name, "disk_time",
632 (derive_t)(read_time*1000), (derive_t)(write_time*1000));
635 geom_stats_snapshot_free(snap);
647 derive_t read_sectors = 0;
648 derive_t write_sectors = 0;
650 derive_t read_ops = 0;
651 derive_t read_merged = 0;
652 derive_t read_time = 0;
653 derive_t write_ops = 0;
654 derive_t write_merged = 0;
655 derive_t write_time = 0;
656 gauge_t in_progress = NAN;
657 derive_t io_time = 0;
658 derive_t weighted_time = 0;
661 diskstats_t *ds, *pre_ds;
663 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
665 fh = fopen ("/proc/partitions", "r");
668 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
672 /* Kernel is 2.4.* */
677 handle_udev = udev_new();
680 while (fgets (buffer, sizeof (buffer), fh) != NULL)
686 numfields = strsplit (buffer, fields, 32);
688 if ((numfields != (14 + fieldshift)) && (numfields != 7))
691 minor = atoll (fields[1]);
693 disk_name = fields[2 + fieldshift];
695 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
696 if (strcmp (disk_name, ds->name) == 0)
701 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
704 if ((ds->name = strdup (disk_name)) == NULL)
719 /* Kernel 2.6, Partition */
720 read_ops = atoll (fields[3]);
721 read_sectors = atoll (fields[4]);
722 write_ops = atoll (fields[5]);
723 write_sectors = atoll (fields[6]);
725 else if (numfields == (14 + fieldshift))
727 read_ops = atoll (fields[3 + fieldshift]);
728 write_ops = atoll (fields[7 + fieldshift]);
730 read_sectors = atoll (fields[5 + fieldshift]);
731 write_sectors = atoll (fields[9 + fieldshift]);
733 if ((fieldshift == 0) || (minor == 0))
736 read_merged = atoll (fields[4 + fieldshift]);
737 read_time = atoll (fields[6 + fieldshift]);
738 write_merged = atoll (fields[8 + fieldshift]);
739 write_time = atoll (fields[10+ fieldshift]);
741 in_progress = atof (fields[11 + fieldshift]);
743 io_time = atof (fields[12 + fieldshift]);
744 weighted_time = atof (fields[13 + fieldshift]);
749 DEBUG ("numfields = %i; => unknown file format.", numfields);
754 derive_t diff_read_sectors;
755 derive_t diff_write_sectors;
757 /* If the counter wraps around, it's only 32 bits.. */
758 if (read_sectors < ds->read_sectors)
759 diff_read_sectors = 1 + read_sectors
760 + (UINT_MAX - ds->read_sectors);
762 diff_read_sectors = read_sectors - ds->read_sectors;
763 if (write_sectors < ds->write_sectors)
764 diff_write_sectors = 1 + write_sectors
765 + (UINT_MAX - ds->write_sectors);
767 diff_write_sectors = write_sectors - ds->write_sectors;
769 ds->read_bytes += 512 * diff_read_sectors;
770 ds->write_bytes += 512 * diff_write_sectors;
771 ds->read_sectors = read_sectors;
772 ds->write_sectors = write_sectors;
775 /* Calculate the average time an io-op needs to complete */
778 derive_t diff_read_ops;
779 derive_t diff_write_ops;
780 derive_t diff_read_time;
781 derive_t diff_write_time;
783 if (read_ops < ds->read_ops)
784 diff_read_ops = 1 + read_ops
785 + (UINT_MAX - ds->read_ops);
787 diff_read_ops = read_ops - ds->read_ops;
788 DEBUG ("disk plugin: disk_name = %s; read_ops = %"PRIi64"; "
789 "ds->read_ops = %"PRIi64"; diff_read_ops = %"PRIi64";",
791 read_ops, ds->read_ops, diff_read_ops);
793 if (write_ops < ds->write_ops)
794 diff_write_ops = 1 + write_ops
795 + (UINT_MAX - ds->write_ops);
797 diff_write_ops = write_ops - ds->write_ops;
799 if (read_time < ds->read_time)
800 diff_read_time = 1 + read_time
801 + (UINT_MAX - ds->read_time);
803 diff_read_time = read_time - ds->read_time;
805 if (write_time < ds->write_time)
806 diff_write_time = 1 + write_time
807 + (UINT_MAX - ds->write_time);
809 diff_write_time = write_time - ds->write_time;
811 if (diff_read_ops != 0)
812 ds->avg_read_time += disk_calc_time_incr (
813 diff_read_time, diff_read_ops);
814 if (diff_write_ops != 0)
815 ds->avg_write_time += disk_calc_time_incr (
816 diff_write_time, diff_write_ops);
818 ds->read_ops = read_ops;
819 ds->read_time = read_time;
820 ds->write_ops = write_ops;
821 ds->write_time = write_time;
824 /* Don't write to the RRDs if we've just started.. */
826 if (ds->poll_count <= 2)
828 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
829 "(min_poll_count = 2); => Not writing.",
834 if ((read_ops == 0) && (write_ops == 0))
836 DEBUG ("disk plugin: ((read_ops == 0) && "
837 "(write_ops == 0)); => Not writing.");
841 output_name = disk_name;
844 alt_name = disk_udev_attr_name (handle_udev, disk_name,
845 conf_udev_name_attr);
849 if (alt_name != NULL)
850 output_name = alt_name;
852 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
853 disk_submit (output_name, "disk_octets",
854 ds->read_bytes, ds->write_bytes);
856 if ((ds->read_ops != 0) || (ds->write_ops != 0))
857 disk_submit (output_name, "disk_ops",
858 read_ops, write_ops);
860 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
861 disk_submit (output_name, "disk_time",
862 ds->avg_read_time, ds->avg_write_time);
866 disk_submit (output_name, "disk_merged",
867 read_merged, write_merged);
868 submit_in_progress (output_name, in_progress);
869 submit_io_time (output_name, io_time, weighted_time);
872 /* release udev-based alternate name, if allocated */
874 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
877 udev_unref(handle_udev);
881 /* #endif defined(KERNEL_LINUX) */
884 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
885 # define KIO_ROCTETS reads
886 # define KIO_WOCTETS writes
887 # define KIO_ROPS nreads
888 # define KIO_WOPS nwrites
889 # define KIO_RTIME rtime
890 # define KIO_WTIME wtime
891 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
892 # define KIO_ROCTETS nread
893 # define KIO_WOCTETS nwritten
894 # define KIO_ROPS reads
895 # define KIO_WOPS writes
896 # define KIO_RTIME rtime
897 # define KIO_WTIME wtime
899 # error "kstat_io_t does not have the required members"
901 static kstat_io_t kio;
907 for (i = 0; i < numdisk; i++)
909 if (kstat_read (kc, ksp[i], &kio) == -1)
912 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
914 disk_submit (ksp[i]->ks_name, "disk_octets",
915 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
916 disk_submit (ksp[i]->ks_name, "disk_ops",
917 kio.KIO_ROPS, kio.KIO_WOPS);
918 /* FIXME: Convert this to microseconds if necessary */
919 disk_submit (ksp[i]->ks_name, "disk_time",
920 kio.KIO_RTIME, kio.KIO_WTIME);
922 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
924 disk_submit (ksp[i]->ks_name, "disk_octets",
925 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
926 disk_submit (ksp[i]->ks_name, "disk_ops",
927 kio.KIO_ROPS, kio.KIO_WOPS);
930 /* #endif defined(HAVE_LIBKSTAT) */
932 #elif defined(HAVE_LIBSTATGRAB)
933 sg_disk_io_stats *ds;
934 # if HAVE_LIBSTATGRAB_0_90
940 char name[DATA_MAX_NAME_LEN];
942 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
945 for (counter=0; counter < disks; counter++) {
946 strncpy(name, ds->disk_name, sizeof(name));
947 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
948 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
951 /* #endif defined(HAVE_LIBSTATGRAB) */
953 #elif defined(HAVE_PERFSTAT)
954 derive_t read_sectors;
955 derive_t write_sectors;
960 perfstat_id_t firstpath;
964 if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0)
967 WARNING ("disk plugin: perfstat_disk: %s",
968 sstrerror (errno, errbuf, sizeof (errbuf)));
972 if (numdisk != pnumdisk || stat_disk==NULL) {
975 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
979 firstpath.name[0]='\0';
980 if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0)
983 WARNING ("disk plugin: perfstat_disk : %s",
984 sstrerror (errno, errbuf, sizeof (errbuf)));
988 for (i = 0; i < rnumdisk; i++)
990 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
991 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
992 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
994 read_ops = stat_disk[i].xrate;
995 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
996 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
998 read_time = stat_disk[i].rserv;
999 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
1000 write_time = stat_disk[i].wserv;
1001 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
1002 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
1004 #endif /* defined(HAVE_PERFSTAT) */
1007 } /* int disk_read */
1009 void module_register (void)
1011 plugin_register_config ("disk", disk_config,
1012 config_keys, config_keys_num);
1013 plugin_register_init ("disk", disk_init);
1014 plugin_register_read ("disk", disk_read);
1015 } /* void module_register */