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>
61 # define UINT_MAX 4294967295U
65 # include <statgrab.h>
69 # ifndef _AIXVERSION_610
70 # include <sys/systemcfg.h>
72 # include <sys/protosw.h>
73 # include <libperfstat.h>
76 #if HAVE_IOKIT_IOKITLIB_H
77 static mach_port_t io_master_port = MACH_PORT_NULL;
78 /* This defaults to false for backwards compatibility. Please fix in the next
80 static _Bool use_bsd_name = 0;
81 /* #endif HAVE_IOKIT_IOKITLIB_H */
84 typedef struct diskstats
88 /* This overflows in roughly 1361 years */
89 unsigned int poll_count;
91 derive_t read_sectors;
92 derive_t write_sectors;
102 derive_t avg_read_time;
103 derive_t avg_write_time;
105 struct diskstats *next;
108 static diskstats_t *disklist;
109 /* #endif KERNEL_LINUX */
112 #define MAX_NUMDISK 1024
113 extern kstat_ctl_t *kc;
114 static kstat_t *ksp[MAX_NUMDISK];
115 static int numdisk = 0;
116 /* #endif HAVE_LIBKSTAT */
118 #elif defined(HAVE_LIBSTATGRAB)
119 /* #endif HAVE_LIBKSTATGRAB */
122 static perfstat_disk_t * stat_disk;
125 /* #endif HAVE_PERFSTAT */
128 # error "No applicable input method."
131 static const char *config_keys[] =
137 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
139 static ignorelist_t *ignorelist = NULL;
141 static int disk_config (const char *key, const char *value)
143 if (ignorelist == NULL)
144 ignorelist = ignorelist_create (/* invert = */ 1);
145 if (ignorelist == NULL)
148 if (strcasecmp ("Disk", key) == 0)
150 ignorelist_add (ignorelist, value);
152 else if (strcasecmp ("IgnoreSelected", key) == 0)
157 ignorelist_set_invert (ignorelist, invert);
159 else if (strcasecmp ("UseBSDName", key) == 0)
161 #if HAVE_IOKIT_IOKITLIB_H
162 use_bsd_name = IS_TRUE (value) ? 1 : 0;
164 WARNING ("disk plugin: The \"UseBSDName\" option is only supported "
165 "on Mach / Mac OS X and will be ignored.");
174 } /* int disk_config */
176 static int disk_init (void)
178 #if HAVE_IOKIT_IOKITLIB_H
179 kern_return_t status;
181 if (io_master_port != MACH_PORT_NULL)
183 mach_port_deallocate (mach_task_self (),
185 io_master_port = MACH_PORT_NULL;
188 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
189 if (status != kIOReturnSuccess)
191 ERROR ("IOMasterPort failed: %s",
192 mach_error_string (status));
193 io_master_port = MACH_PORT_NULL;
196 /* #endif HAVE_IOKIT_IOKITLIB_H */
200 /* #endif KERNEL_LINUX */
210 for (numdisk = 0, ksp_chain = kc->kc_chain;
211 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
212 ksp_chain = ksp_chain->ks_next)
214 if (strncmp (ksp_chain->ks_class, "disk", 4)
215 && strncmp (ksp_chain->ks_class, "partition", 9))
217 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
219 ksp[numdisk++] = ksp_chain;
221 #endif /* HAVE_LIBKSTAT */
224 } /* int disk_init */
226 static void disk_submit (const char *plugin_instance,
228 derive_t read, derive_t write)
231 value_list_t vl = VALUE_LIST_INIT;
233 /* Both `ignorelist' and `plugin_instance' may be NULL. */
234 if (ignorelist_match (ignorelist, plugin_instance) != 0)
237 values[0].derive = read;
238 values[1].derive = write;
242 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
243 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
244 sstrncpy (vl.plugin_instance, plugin_instance,
245 sizeof (vl.plugin_instance));
246 sstrncpy (vl.type, type, sizeof (vl.type));
248 plugin_dispatch_values (&vl);
249 } /* void disk_submit */
252 static counter_t disk_calc_time_incr (counter_t delta_time, counter_t delta_ops)
254 double interval = CDTIME_T_TO_DOUBLE (plugin_get_interval ());
255 double avg_time = ((double) delta_time) / ((double) delta_ops);
256 double avg_time_incr = interval * avg_time;
258 return ((counter_t) (avg_time_incr + .5));
262 #if HAVE_IOKIT_IOKITLIB_H
263 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
265 signed long long val_int;
269 /* `key_obj' needs to be released. */
270 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
271 kCFStringEncodingASCII);
274 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
278 /* get => we don't need to release (== free) the object */
279 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
285 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
289 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
291 DEBUG ("CFNumberGetValue (%s) failed.", key);
297 #endif /* HAVE_IOKIT_IOKITLIB_H */
299 static int disk_read (void)
301 #if HAVE_IOKIT_IOKITLIB_H
302 io_registry_entry_t disk;
303 io_registry_entry_t disk_child;
304 io_iterator_t disk_list;
305 CFDictionaryRef props_dict;
306 CFDictionaryRef stats_dict;
307 CFDictionaryRef child_dict;
308 CFStringRef tmp_cf_string_ref;
309 kern_return_t status;
311 signed long long read_ops;
312 signed long long read_byt;
313 signed long long read_tme;
314 signed long long write_ops;
315 signed long long write_byt;
316 signed long long write_tme;
320 char disk_name[DATA_MAX_NAME_LEN];
321 char disk_name_bsd[DATA_MAX_NAME_LEN];
323 /* Get the list of all disk objects. */
324 if (IOServiceGetMatchingServices (io_master_port,
325 IOServiceMatching (kIOBlockStorageDriverClass),
326 &disk_list) != kIOReturnSuccess)
328 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
332 while ((disk = IOIteratorNext (disk_list)) != 0)
338 /* `disk_child' must be released */
339 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
342 /* This fails for example for DVD/CD drives.. */
343 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
344 IOObjectRelease (disk);
348 /* We create `props_dict' => we need to release it later */
349 if (IORegistryEntryCreateCFProperties (disk,
350 (CFMutableDictionaryRef *) &props_dict,
355 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
356 IOObjectRelease (disk_child);
357 IOObjectRelease (disk);
361 if (props_dict == NULL)
363 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
364 IOObjectRelease (disk_child);
365 IOObjectRelease (disk);
369 /* tmp_cf_string_ref doesn't need to be released. */
370 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (props_dict,
371 CFSTR(kIOBSDNameKey));
372 if (!tmp_cf_string_ref)
374 DEBUG ("disk plugin: CFDictionaryGetValue("
375 "kIOBSDNameKey) failed.");
376 CFRelease (props_dict);
377 IOObjectRelease (disk_child);
378 IOObjectRelease (disk);
381 assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
383 memset (disk_name_bsd, 0, sizeof (disk_name_bsd));
384 CFStringGetCString (tmp_cf_string_ref,
385 disk_name_bsd, sizeof (disk_name_bsd),
386 kCFStringEncodingUTF8);
387 if (disk_name_bsd[0] == 0)
389 ERROR ("disk plugin: CFStringGetCString() failed.");
390 CFRelease (props_dict);
391 IOObjectRelease (disk_child);
392 IOObjectRelease (disk);
395 DEBUG ("disk plugin: disk_name_bsd = \"%s\"", disk_name_bsd);
397 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
398 CFSTR (kIOBlockStorageDriverStatisticsKey));
400 if (stats_dict == NULL)
402 DEBUG ("disk plugin: CFDictionaryGetValue ("
404 kIOBlockStorageDriverStatisticsKey);
405 CFRelease (props_dict);
406 IOObjectRelease (disk_child);
407 IOObjectRelease (disk);
411 if (IORegistryEntryCreateCFProperties (disk_child,
412 (CFMutableDictionaryRef *) &child_dict,
417 DEBUG ("disk plugin: IORegistryEntryCreateCFProperties ("
418 "disk_child) failed.");
419 IOObjectRelease (disk_child);
420 CFRelease (props_dict);
421 IOObjectRelease (disk);
426 disk_major = (int) dict_get_value (child_dict,
428 disk_minor = (int) dict_get_value (child_dict,
430 read_ops = dict_get_value (stats_dict,
431 kIOBlockStorageDriverStatisticsReadsKey);
432 read_byt = dict_get_value (stats_dict,
433 kIOBlockStorageDriverStatisticsBytesReadKey);
434 read_tme = dict_get_value (stats_dict,
435 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
436 write_ops = dict_get_value (stats_dict,
437 kIOBlockStorageDriverStatisticsWritesKey);
438 write_byt = dict_get_value (stats_dict,
439 kIOBlockStorageDriverStatisticsBytesWrittenKey);
440 /* This property describes the number of nanoseconds spent
441 * performing writes since the block storage driver was
442 * instantiated. It is one of the statistic entries listed
443 * under the top-level kIOBlockStorageDriverStatisticsKey
444 * property table. It has an OSNumber value. */
445 write_tme = dict_get_value (stats_dict,
446 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
449 sstrncpy (disk_name, disk_name_bsd, sizeof (disk_name));
451 ssnprintf (disk_name, sizeof (disk_name), "%i-%i",
452 disk_major, disk_minor);
453 DEBUG ("disk plugin: disk_name = \"%s\"", disk_name);
455 if ((read_byt != -1LL) || (write_byt != -1LL))
456 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
457 if ((read_ops != -1LL) || (write_ops != -1LL))
458 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
459 if ((read_tme != -1LL) || (write_tme != -1LL))
460 disk_submit (disk_name, "disk_time",
464 CFRelease (child_dict);
465 IOObjectRelease (disk_child);
466 CFRelease (props_dict);
467 IOObjectRelease (disk);
469 IOObjectRelease (disk_list);
470 /* #endif HAVE_IOKIT_IOKITLIB_H */
482 derive_t read_sectors = 0;
483 derive_t write_sectors = 0;
485 derive_t read_ops = 0;
486 derive_t read_merged = 0;
487 derive_t read_time = 0;
488 derive_t write_ops = 0;
489 derive_t write_merged = 0;
490 derive_t write_time = 0;
493 diskstats_t *ds, *pre_ds;
495 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
497 fh = fopen ("/proc/partitions", "r");
500 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
504 /* Kernel is 2.4.* */
508 while (fgets (buffer, sizeof (buffer), fh) != NULL)
512 numfields = strsplit (buffer, fields, 32);
514 if ((numfields != (14 + fieldshift)) && (numfields != 7))
517 minor = atoll (fields[1]);
519 disk_name = fields[2 + fieldshift];
521 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
522 if (strcmp (disk_name, ds->name) == 0)
527 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
530 if ((ds->name = strdup (disk_name)) == NULL)
545 /* Kernel 2.6, Partition */
546 read_ops = atoll (fields[3]);
547 read_sectors = atoll (fields[4]);
548 write_ops = atoll (fields[5]);
549 write_sectors = atoll (fields[6]);
551 else if (numfields == (14 + fieldshift))
553 read_ops = atoll (fields[3 + fieldshift]);
554 write_ops = atoll (fields[7 + fieldshift]);
556 read_sectors = atoll (fields[5 + fieldshift]);
557 write_sectors = atoll (fields[9 + fieldshift]);
559 if ((fieldshift == 0) || (minor == 0))
562 read_merged = atoll (fields[4 + fieldshift]);
563 read_time = atoll (fields[6 + fieldshift]);
564 write_merged = atoll (fields[8 + fieldshift]);
565 write_time = atoll (fields[10+ fieldshift]);
570 DEBUG ("numfields = %i; => unknown file format.", numfields);
575 derive_t diff_read_sectors;
576 derive_t diff_write_sectors;
578 /* If the counter wraps around, it's only 32 bits.. */
579 if (read_sectors < ds->read_sectors)
580 diff_read_sectors = 1 + read_sectors
581 + (UINT_MAX - ds->read_sectors);
583 diff_read_sectors = read_sectors - ds->read_sectors;
584 if (write_sectors < ds->write_sectors)
585 diff_write_sectors = 1 + write_sectors
586 + (UINT_MAX - ds->write_sectors);
588 diff_write_sectors = write_sectors - ds->write_sectors;
590 ds->read_bytes += 512 * diff_read_sectors;
591 ds->write_bytes += 512 * diff_write_sectors;
592 ds->read_sectors = read_sectors;
593 ds->write_sectors = write_sectors;
596 /* Calculate the average time an io-op needs to complete */
599 derive_t diff_read_ops;
600 derive_t diff_write_ops;
601 derive_t diff_read_time;
602 derive_t diff_write_time;
604 if (read_ops < ds->read_ops)
605 diff_read_ops = 1 + read_ops
606 + (UINT_MAX - ds->read_ops);
608 diff_read_ops = read_ops - ds->read_ops;
609 DEBUG ("disk plugin: disk_name = %s; read_ops = %"PRIi64"; "
610 "ds->read_ops = %"PRIi64"; diff_read_ops = %"PRIi64";",
612 read_ops, ds->read_ops, diff_read_ops);
614 if (write_ops < ds->write_ops)
615 diff_write_ops = 1 + write_ops
616 + (UINT_MAX - ds->write_ops);
618 diff_write_ops = write_ops - ds->write_ops;
620 if (read_time < ds->read_time)
621 diff_read_time = 1 + read_time
622 + (UINT_MAX - ds->read_time);
624 diff_read_time = read_time - ds->read_time;
626 if (write_time < ds->write_time)
627 diff_write_time = 1 + write_time
628 + (UINT_MAX - ds->write_time);
630 diff_write_time = write_time - ds->write_time;
632 if (diff_read_ops != 0)
633 ds->avg_read_time += disk_calc_time_incr (
634 diff_read_time, diff_read_ops);
635 if (diff_write_ops != 0)
636 ds->avg_write_time += disk_calc_time_incr (
637 diff_write_time, diff_write_ops);
639 ds->read_ops = read_ops;
640 ds->read_time = read_time;
641 ds->write_ops = write_ops;
642 ds->write_time = write_time;
645 /* Don't write to the RRDs if we've just started.. */
647 if (ds->poll_count <= 2)
649 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
650 "(min_poll_count = 2); => Not writing.",
655 if ((read_ops == 0) && (write_ops == 0))
657 DEBUG ("disk plugin: ((read_ops == 0) && "
658 "(write_ops == 0)); => Not writing.");
662 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
663 disk_submit (disk_name, "disk_octets",
664 ds->read_bytes, ds->write_bytes);
666 if ((ds->read_ops != 0) || (ds->write_ops != 0))
667 disk_submit (disk_name, "disk_ops",
668 read_ops, write_ops);
670 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
671 disk_submit (disk_name, "disk_time",
672 ds->avg_read_time, ds->avg_write_time);
676 disk_submit (disk_name, "disk_merged",
677 read_merged, write_merged);
679 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
682 /* #endif defined(KERNEL_LINUX) */
685 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
686 # define KIO_ROCTETS reads
687 # define KIO_WOCTETS writes
688 # define KIO_ROPS nreads
689 # define KIO_WOPS nwrites
690 # define KIO_RTIME rtime
691 # define KIO_WTIME wtime
692 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
693 # define KIO_ROCTETS nread
694 # define KIO_WOCTETS nwritten
695 # define KIO_ROPS reads
696 # define KIO_WOPS writes
697 # define KIO_RTIME rtime
698 # define KIO_WTIME wtime
700 # error "kstat_io_t does not have the required members"
702 static kstat_io_t kio;
708 for (i = 0; i < numdisk; i++)
710 if (kstat_read (kc, ksp[i], &kio) == -1)
713 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
715 disk_submit (ksp[i]->ks_name, "disk_octets",
716 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
717 disk_submit (ksp[i]->ks_name, "disk_ops",
718 kio.KIO_ROPS, kio.KIO_WOPS);
719 /* FIXME: Convert this to microseconds if necessary */
720 disk_submit (ksp[i]->ks_name, "disk_time",
721 kio.KIO_RTIME, kio.KIO_WTIME);
723 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
725 disk_submit (ksp[i]->ks_name, "disk_octets",
726 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
727 disk_submit (ksp[i]->ks_name, "disk_ops",
728 kio.KIO_ROPS, kio.KIO_WOPS);
731 /* #endif defined(HAVE_LIBKSTAT) */
733 #elif defined(HAVE_LIBSTATGRAB)
734 sg_disk_io_stats *ds;
736 char name[DATA_MAX_NAME_LEN];
738 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
741 for (counter=0; counter < disks; counter++) {
742 strncpy(name, ds->disk_name, sizeof(name));
743 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
744 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
747 /* #endif defined(HAVE_LIBSTATGRAB) */
749 #elif defined(HAVE_PERFSTAT)
750 derive_t read_sectors;
751 derive_t write_sectors;
756 perfstat_id_t firstpath;
760 if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0)
763 WARNING ("disk plugin: perfstat_disk: %s",
764 sstrerror (errno, errbuf, sizeof (errbuf)));
768 if (numdisk != pnumdisk || stat_disk==NULL) {
771 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
775 firstpath.name[0]='\0';
776 if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0)
779 WARNING ("disk plugin: perfstat_disk : %s",
780 sstrerror (errno, errbuf, sizeof (errbuf)));
784 for (i = 0; i < rnumdisk; i++)
786 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
787 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
788 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
790 read_ops = stat_disk[i].xrate;
791 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
792 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
794 read_time = stat_disk[i].rserv;
795 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
796 write_time = stat_disk[i].wserv;
797 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
798 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
800 #endif /* defined(HAVE_PERFSTAT) */
803 } /* int disk_read */
805 void module_register (void)
807 plugin_register_config ("disk", disk_config,
808 config_keys, config_keys_num);
809 plugin_register_init ("disk", disk_init);
810 plugin_register_read ("disk", disk_read);
811 } /* void module_register */