2 * collectd - src/disk.c
3 * Copyright (C) 2005-2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_ignorelist.h"
27 #if HAVE_MACH_MACH_TYPES_H
28 # include <mach/mach_types.h>
30 #if HAVE_MACH_MACH_INIT_H
31 # include <mach/mach_init.h>
33 #if HAVE_MACH_MACH_ERROR_H
34 # include <mach/mach_error.h>
36 #if HAVE_MACH_MACH_PORT_H
37 # include <mach/mach_port.h>
39 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
40 # include <CoreFoundation/CoreFoundation.h>
42 #if HAVE_IOKIT_IOKITLIB_H
43 # include <IOKit/IOKitLib.h>
45 #if HAVE_IOKIT_IOTYPES_H
46 # include <IOKit/IOTypes.h>
48 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
49 # include <IOKit/storage/IOBlockStorageDriver.h>
51 #if HAVE_IOKIT_IOBSD_H
52 # include <IOKit/IOBSD.h>
59 # define UINT_MAX 4294967295U
63 # include <statgrab.h>
66 #if HAVE_IOKIT_IOKITLIB_H
67 static mach_port_t io_master_port = MACH_PORT_NULL;
68 /* #endif HAVE_IOKIT_IOKITLIB_H */
71 typedef struct diskstats
75 /* This overflows in roughly 1361 years */
76 unsigned int poll_count;
78 counter_t read_sectors;
79 counter_t write_sectors;
82 counter_t write_bytes;
89 counter_t avg_read_time;
90 counter_t avg_write_time;
92 struct diskstats *next;
95 static diskstats_t *disklist;
96 /* #endif KERNEL_LINUX */
99 #define MAX_NUMDISK 256
100 extern kstat_ctl_t *kc;
101 static kstat_t *ksp[MAX_NUMDISK];
102 static int numdisk = 0;
103 /* #endif HAVE_LIBKSTAT */
105 #elif defined(HAVE_LIBSTATGRAB)
106 /* #endif HAVE_LIBKSTATGRAB */
109 # error "No applicable input method."
112 static const char *config_keys[] =
117 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
119 static ignorelist_t *ignorelist = NULL;
121 static int disk_config (const char *key, const char *value)
123 if (ignorelist == NULL)
124 ignorelist = ignorelist_create (/* invert = */ 1);
125 if (ignorelist == NULL)
128 if (strcasecmp ("Disk", key) == 0)
130 ignorelist_add (ignorelist, value);
132 else if (strcasecmp ("IgnoreSelected", key) == 0)
135 if ((strcasecmp ("True", value) == 0)
136 || (strcasecmp ("Yes", value) == 0)
137 || (strcasecmp ("On", value) == 0))
139 ignorelist_set_invert (ignorelist, invert);
147 } /* int disk_config */
149 static int disk_init (void)
151 #if HAVE_IOKIT_IOKITLIB_H
152 kern_return_t status;
154 if (io_master_port != MACH_PORT_NULL)
156 mach_port_deallocate (mach_task_self (),
158 io_master_port = MACH_PORT_NULL;
161 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
162 if (status != kIOReturnSuccess)
164 ERROR ("IOMasterPort failed: %s",
165 mach_error_string (status));
166 io_master_port = MACH_PORT_NULL;
169 /* #endif HAVE_IOKIT_IOKITLIB_H */
173 /* #endif KERNEL_LINUX */
183 for (numdisk = 0, ksp_chain = kc->kc_chain;
184 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
185 ksp_chain = ksp_chain->ks_next)
187 if (strncmp (ksp_chain->ks_class, "disk", 4)
188 && strncmp (ksp_chain->ks_class, "partition", 9))
190 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
192 ksp[numdisk++] = ksp_chain;
194 #endif /* HAVE_LIBKSTAT */
197 } /* int disk_init */
199 static void disk_submit (const char *plugin_instance,
201 counter_t read, counter_t write)
204 value_list_t vl = VALUE_LIST_INIT;
206 /* Both `ignorelist' and `plugin_instance' may be NULL. */
207 if (ignorelist_match (ignorelist, plugin_instance) != 0)
210 values[0].counter = read;
211 values[1].counter = write;
215 vl.time = time (NULL);
216 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
217 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
218 strncpy (vl.plugin_instance, plugin_instance,
219 sizeof (vl.plugin_instance));
221 plugin_dispatch_values (type, &vl);
222 } /* void disk_submit */
224 #if HAVE_IOKIT_IOKITLIB_H
225 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
227 signed long long val_int;
231 /* `key_obj' needs to be released. */
232 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
233 kCFStringEncodingASCII);
236 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
240 /* get => we don't need to release (== free) the object */
241 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
247 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
251 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
253 DEBUG ("CFNumberGetValue (%s) failed.", key);
259 #endif /* HAVE_IOKIT_IOKITLIB_H */
261 static int disk_read (void)
263 #if HAVE_IOKIT_IOKITLIB_H
264 io_registry_entry_t disk;
265 io_registry_entry_t disk_child;
266 io_iterator_t disk_list;
267 CFDictionaryRef props_dict;
268 CFDictionaryRef stats_dict;
269 CFDictionaryRef child_dict;
270 kern_return_t status;
272 signed long long read_ops;
273 signed long long read_byt;
274 signed long long read_tme;
275 signed long long write_ops;
276 signed long long write_byt;
277 signed long long write_tme;
283 /* Get the list of all disk objects. */
284 if (IOServiceGetMatchingServices (io_master_port,
285 IOServiceMatching (kIOBlockStorageDriverClass),
286 &disk_list) != kIOReturnSuccess)
288 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
292 while ((disk = IOIteratorNext (disk_list)) != 0)
298 /* `disk_child' must be released */
299 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
302 /* This fails for example for DVD/CD drives.. */
303 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
304 IOObjectRelease (disk);
308 /* We create `props_dict' => we need to release it later */
309 if (IORegistryEntryCreateCFProperties (disk,
310 (CFMutableDictionaryRef *) &props_dict,
315 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
316 IOObjectRelease (disk_child);
317 IOObjectRelease (disk);
321 if (props_dict == NULL)
323 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
324 IOObjectRelease (disk_child);
325 IOObjectRelease (disk);
329 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
330 CFSTR (kIOBlockStorageDriverStatisticsKey));
332 if (stats_dict == NULL)
334 DEBUG ("CFDictionaryGetValue (%s) failed.",
335 kIOBlockStorageDriverStatisticsKey);
336 CFRelease (props_dict);
337 IOObjectRelease (disk_child);
338 IOObjectRelease (disk);
342 if (IORegistryEntryCreateCFProperties (disk_child,
343 (CFMutableDictionaryRef *) &child_dict,
348 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
349 IOObjectRelease (disk_child);
350 CFRelease (props_dict);
351 IOObjectRelease (disk);
356 disk_major = (int) dict_get_value (child_dict,
358 disk_minor = (int) dict_get_value (child_dict,
360 read_ops = dict_get_value (stats_dict,
361 kIOBlockStorageDriverStatisticsReadsKey);
362 read_byt = dict_get_value (stats_dict,
363 kIOBlockStorageDriverStatisticsBytesReadKey);
364 read_tme = dict_get_value (stats_dict,
365 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
366 write_ops = dict_get_value (stats_dict,
367 kIOBlockStorageDriverStatisticsWritesKey);
368 write_byt = dict_get_value (stats_dict,
369 kIOBlockStorageDriverStatisticsBytesWrittenKey);
370 /* This property describes the number of nanoseconds spent
371 * performing writes since the block storage driver was
372 * instantiated. It is one of the statistic entries listed
373 * under the top-level kIOBlockStorageDriverStatisticsKey
374 * property table. It has an OSNumber value. */
375 write_tme = dict_get_value (stats_dict,
376 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
378 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
380 DEBUG ("snprintf (major, minor) failed.");
381 CFRelease (child_dict);
382 IOObjectRelease (disk_child);
383 CFRelease (props_dict);
384 IOObjectRelease (disk);
387 DEBUG ("disk_name = %s", disk_name);
389 if ((read_byt != -1LL) || (write_byt != -1LL))
390 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
391 if ((read_ops != -1LL) || (write_ops != -1LL))
392 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
393 if ((read_tme != -1LL) || (write_tme != -1LL))
394 disk_submit (disk_name, "disk_time",
398 CFRelease (child_dict);
399 IOObjectRelease (disk_child);
400 CFRelease (props_dict);
401 IOObjectRelease (disk);
403 IOObjectRelease (disk_list);
404 /* #endif HAVE_IOKIT_IOKITLIB_H */
417 counter_t read_sectors = 0;
418 counter_t write_sectors = 0;
420 counter_t read_ops = 0;
421 counter_t read_merged = 0;
422 counter_t read_time = 0;
423 counter_t write_ops = 0;
424 counter_t write_merged = 0;
425 counter_t write_time = 0;
428 diskstats_t *ds, *pre_ds;
430 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
432 fh = fopen ("/proc/partitions", "r");
435 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
439 /* Kernel is 2.4.* */
443 while (fgets (buffer, sizeof (buffer), fh) != NULL)
447 numfields = strsplit (buffer, fields, 32);
449 if ((numfields != (14 + fieldshift)) && (numfields != 7))
452 major = atoll (fields[0]);
453 minor = atoll (fields[1]);
455 disk_name = fields[2 + fieldshift];
457 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
458 if (strcmp (disk_name, ds->name) == 0)
463 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
466 if ((ds->name = strdup (disk_name)) == NULL)
481 /* Kernel 2.6, Partition */
482 read_ops = atoll (fields[3]);
483 read_sectors = atoll (fields[4]);
484 write_ops = atoll (fields[5]);
485 write_sectors = atoll (fields[6]);
487 else if (numfields == (14 + fieldshift))
489 read_ops = atoll (fields[3 + fieldshift]);
490 write_ops = atoll (fields[7 + fieldshift]);
492 read_sectors = atoll (fields[5 + fieldshift]);
493 write_sectors = atoll (fields[9 + fieldshift]);
495 if ((fieldshift == 0) || (minor == 0))
498 read_merged = atoll (fields[4 + fieldshift]);
499 read_time = atoll (fields[6 + fieldshift]);
500 write_merged = atoll (fields[8 + fieldshift]);
501 write_time = atoll (fields[10+ fieldshift]);
506 DEBUG ("numfields = %i; => unknown file format.", numfields);
511 counter_t diff_read_sectors;
512 counter_t diff_write_sectors;
514 /* If the counter wraps around, it's only 32 bits.. */
515 if (read_sectors < ds->read_sectors)
516 diff_read_sectors = 1 + read_sectors
517 + (UINT_MAX - ds->read_sectors);
519 diff_read_sectors = read_sectors - ds->read_sectors;
520 if (write_sectors < ds->write_sectors)
521 diff_write_sectors = 1 + write_sectors
522 + (UINT_MAX - ds->write_sectors);
524 diff_write_sectors = write_sectors - ds->write_sectors;
526 ds->read_bytes += 512 * diff_read_sectors;
527 ds->write_bytes += 512 * diff_write_sectors;
528 ds->read_sectors = read_sectors;
529 ds->write_sectors = write_sectors;
532 /* Calculate the average time an io-op needs to complete */
535 counter_t diff_read_ops;
536 counter_t diff_write_ops;
537 counter_t diff_read_time;
538 counter_t diff_write_time;
540 if (read_ops < ds->read_ops)
541 diff_read_ops = 1 + read_ops
542 + (UINT_MAX - ds->read_ops);
544 diff_read_ops = read_ops - ds->read_ops;
545 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
546 "ds->read_ops = %llu; diff_read_ops = %llu;",
548 read_ops, ds->read_ops, diff_read_ops);
550 if (write_ops < ds->write_ops)
551 diff_write_ops = 1 + write_ops
552 + (UINT_MAX - ds->write_ops);
554 diff_write_ops = write_ops - ds->write_ops;
556 if (read_time < ds->read_time)
557 diff_read_time = 1 + read_time
558 + (UINT_MAX - ds->read_time);
560 diff_read_time = read_time - ds->read_time;
562 if (write_time < ds->write_time)
563 diff_write_time = 1 + write_time
564 + (UINT_MAX - ds->write_time);
566 diff_write_time = write_time - ds->write_time;
568 if (diff_read_ops != 0)
569 ds->avg_read_time += (diff_read_time
570 + (diff_read_ops / 2))
572 if (diff_write_ops != 0)
573 ds->avg_write_time += (diff_write_time
574 + (diff_write_ops / 2))
577 ds->read_ops = read_ops;
578 ds->read_time = read_time;
579 ds->write_ops = write_ops;
580 ds->write_time = write_time;
583 /* Don't write to the RRDs if we've just started.. */
585 if (ds->poll_count <= 2)
587 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
588 "(min_poll_count = 2); => Not writing.",
593 if ((read_ops == 0) && (write_ops == 0))
595 DEBUG ("disk plugin: ((read_ops == 0) && "
596 "(write_ops == 0)); => Not writing.");
600 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
601 disk_submit (disk_name, "disk_octets",
602 ds->read_bytes, ds->write_bytes);
604 if ((ds->read_ops != 0) || (ds->write_ops != 0))
605 disk_submit (disk_name, "disk_ops",
606 read_ops, write_ops);
608 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
609 disk_submit (disk_name, "disk_time",
610 ds->avg_read_time, ds->avg_write_time);
614 disk_submit (disk_name, "disk_merged",
615 read_merged, write_merged);
617 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
620 /* #endif defined(KERNEL_LINUX) */
623 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
624 # define KIO_ROCTETS reads
625 # define KIO_WOCTETS writes
626 # define KIO_ROPS nreads
627 # define KIO_WOPS nwrites
628 # define KIO_RTIME rtime
629 # define KIO_WTIME wtime
630 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
631 # define KIO_ROCTETS nread
632 # define KIO_WOCTETS nwritten
633 # define KIO_ROPS reads
634 # define KIO_WOPS writes
635 # define KIO_RTIME rtime
636 # define KIO_WTIME wtime
638 # error "kstat_io_t does not have the required members"
640 static kstat_io_t kio;
646 for (i = 0; i < numdisk; i++)
648 if (kstat_read (kc, ksp[i], &kio) == -1)
651 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
653 disk_submit (ksp[i]->ks_name, "disk_octets",
654 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
655 disk_submit (ksp[i]->ks_name, "disk_ops",
656 kio.KIO_ROPS, kio.KIO_WOPS);
657 /* FIXME: Convert this to microseconds if necessary */
658 disk_submit (ksp[i]->ks_name, "disk_time",
659 kio.KIO_RTIME, kio.KIO_WTIME);
661 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
663 disk_submit (ksp[i]->ks_name, "disk_octets",
664 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
665 disk_submit (ksp[i]->ks_name, "disk_ops",
666 kio.KIO_ROPS, kio.KIO_WOPS);
669 /* #endif defined(HAVE_LIBKSTAT) */
671 #elif defined(HAVE_LIBSTATGRAB)
672 sg_disk_io_stats *ds;
674 char name[DATA_MAX_NAME_LEN];
676 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
679 for (counter=0; counter < disks; counter++) {
680 strncpy(name, ds->disk_name, sizeof(name));
681 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
682 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
685 #endif /* defined(HAVE_LIBSTATGRAB) */
688 } /* int disk_read */
690 void module_register (void)
692 plugin_register_config ("disk", disk_config,
693 config_keys, config_keys_num);
694 plugin_register_init ("disk", disk_init);
695 plugin_register_read ("disk", disk_read);
696 } /* void module_register */