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
62 #if HAVE_IOKIT_IOKITLIB_H
63 static mach_port_t io_master_port = MACH_PORT_NULL;
64 /* #endif HAVE_IOKIT_IOKITLIB_H */
67 typedef struct diskstats
71 /* This overflows in roughly 1361 years */
72 unsigned int poll_count;
74 counter_t read_sectors;
75 counter_t write_sectors;
78 counter_t write_bytes;
85 counter_t avg_read_time;
86 counter_t avg_write_time;
88 struct diskstats *next;
91 static diskstats_t *disklist;
92 /* #endif KERNEL_LINUX */
95 #define MAX_NUMDISK 256
96 extern kstat_ctl_t *kc;
97 static kstat_t *ksp[MAX_NUMDISK];
98 static int numdisk = 0;
99 /* #endif HAVE_LIBKSTAT */
102 # error "No applicable input method."
105 static const char *config_keys[] =
110 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
112 static ignorelist_t *ignorelist = NULL;
114 static int disk_config (const char *key, const char *value)
116 if (ignorelist == NULL)
117 ignorelist = ignorelist_create (/* invert = */ 1);
118 if (ignorelist == NULL)
121 if (strcasecmp ("Disk", key) == 0)
123 ignorelist_add (ignorelist, value);
125 else if (strcasecmp ("IgnoreSelected", key) == 0)
128 if ((strcasecmp ("True", value) == 0)
129 || (strcasecmp ("Yes", value) == 0)
130 || (strcasecmp ("On", value) == 0))
132 ignorelist_set_invert (ignorelist, invert);
140 } /* int disk_config */
142 static int disk_init (void)
144 #if HAVE_IOKIT_IOKITLIB_H
145 kern_return_t status;
147 if (io_master_port != MACH_PORT_NULL)
149 mach_port_deallocate (mach_task_self (),
151 io_master_port = MACH_PORT_NULL;
154 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
155 if (status != kIOReturnSuccess)
157 ERROR ("IOMasterPort failed: %s",
158 mach_error_string (status));
159 io_master_port = MACH_PORT_NULL;
162 /* #endif HAVE_IOKIT_IOKITLIB_H */
166 /* #endif KERNEL_LINUX */
176 for (numdisk = 0, ksp_chain = kc->kc_chain;
177 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
178 ksp_chain = ksp_chain->ks_next)
180 if (strncmp (ksp_chain->ks_class, "disk", 4)
181 && strncmp (ksp_chain->ks_class, "partition", 9))
183 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
185 ksp[numdisk++] = ksp_chain;
187 #endif /* HAVE_LIBKSTAT */
190 } /* int disk_init */
192 static void disk_submit (const char *plugin_instance,
194 counter_t read, counter_t write)
197 value_list_t vl = VALUE_LIST_INIT;
199 /* Both `ignorelist' and `plugin_instance' may be NULL. */
200 if (ignorelist_match (ignorelist, plugin_instance) != 0)
203 values[0].counter = read;
204 values[1].counter = write;
208 vl.time = time (NULL);
209 strcpy (vl.host, hostname_g);
210 strcpy (vl.plugin, "disk");
211 sstrncpy (vl.plugin_instance, plugin_instance,
212 sizeof (vl.plugin_instance));
213 sstrncpy (vl.type, type, sizeof (vl.type));
215 plugin_dispatch_values (&vl);
216 } /* void disk_submit */
218 #if HAVE_IOKIT_IOKITLIB_H
219 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
221 signed long long val_int;
225 /* `key_obj' needs to be released. */
226 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
227 kCFStringEncodingASCII);
230 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
234 /* get => we don't need to release (== free) the object */
235 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
241 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
245 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
247 DEBUG ("CFNumberGetValue (%s) failed.", key);
253 #endif /* HAVE_IOKIT_IOKITLIB_H */
255 static int disk_read (void)
257 #if HAVE_IOKIT_IOKITLIB_H
258 io_registry_entry_t disk;
259 io_registry_entry_t disk_child;
260 io_iterator_t disk_list;
261 CFDictionaryRef props_dict;
262 CFDictionaryRef stats_dict;
263 CFDictionaryRef child_dict;
264 kern_return_t status;
266 signed long long read_ops;
267 signed long long read_byt;
268 signed long long read_tme;
269 signed long long write_ops;
270 signed long long write_byt;
271 signed long long write_tme;
277 /* Get the list of all disk objects. */
278 if (IOServiceGetMatchingServices (io_master_port,
279 IOServiceMatching (kIOBlockStorageDriverClass),
280 &disk_list) != kIOReturnSuccess)
282 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
286 while ((disk = IOIteratorNext (disk_list)) != 0)
292 /* `disk_child' must be released */
293 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
296 /* This fails for example for DVD/CD drives.. */
297 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
298 IOObjectRelease (disk);
302 /* We create `props_dict' => we need to release it later */
303 if (IORegistryEntryCreateCFProperties (disk,
304 (CFMutableDictionaryRef *) &props_dict,
309 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
310 IOObjectRelease (disk_child);
311 IOObjectRelease (disk);
315 if (props_dict == NULL)
317 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
318 IOObjectRelease (disk_child);
319 IOObjectRelease (disk);
323 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
324 CFSTR (kIOBlockStorageDriverStatisticsKey));
326 if (stats_dict == NULL)
328 DEBUG ("CFDictionaryGetValue (%s) failed.",
329 kIOBlockStorageDriverStatisticsKey);
330 CFRelease (props_dict);
331 IOObjectRelease (disk_child);
332 IOObjectRelease (disk);
336 if (IORegistryEntryCreateCFProperties (disk_child,
337 (CFMutableDictionaryRef *) &child_dict,
342 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
343 IOObjectRelease (disk_child);
344 CFRelease (props_dict);
345 IOObjectRelease (disk);
350 disk_major = (int) dict_get_value (child_dict,
352 disk_minor = (int) dict_get_value (child_dict,
354 read_ops = dict_get_value (stats_dict,
355 kIOBlockStorageDriverStatisticsReadsKey);
356 read_byt = dict_get_value (stats_dict,
357 kIOBlockStorageDriverStatisticsBytesReadKey);
358 read_tme = dict_get_value (stats_dict,
359 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
360 write_ops = dict_get_value (stats_dict,
361 kIOBlockStorageDriverStatisticsWritesKey);
362 write_byt = dict_get_value (stats_dict,
363 kIOBlockStorageDriverStatisticsBytesWrittenKey);
364 /* This property describes the number of nanoseconds spent
365 * performing writes since the block storage driver was
366 * instantiated. It is one of the statistic entries listed
367 * under the top-level kIOBlockStorageDriverStatisticsKey
368 * property table. It has an OSNumber value. */
369 write_tme = dict_get_value (stats_dict,
370 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
372 if (ssnprintf (disk_name, sizeof (disk_name),
373 "%i-%i", disk_major, disk_minor) >= sizeof (disk_name))
375 DEBUG ("snprintf (major, minor) failed.");
376 CFRelease (child_dict);
377 IOObjectRelease (disk_child);
378 CFRelease (props_dict);
379 IOObjectRelease (disk);
382 DEBUG ("disk_name = %s", disk_name);
384 if ((read_byt != -1LL) || (write_byt != -1LL))
385 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
386 if ((read_ops != -1LL) || (write_ops != -1LL))
387 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
388 if ((read_tme != -1LL) || (write_tme != -1LL))
389 disk_submit (disk_name, "disk_time",
393 CFRelease (child_dict);
394 IOObjectRelease (disk_child);
395 CFRelease (props_dict);
396 IOObjectRelease (disk);
398 IOObjectRelease (disk_list);
399 /* #endif HAVE_IOKIT_IOKITLIB_H */
412 counter_t read_sectors = 0;
413 counter_t write_sectors = 0;
415 counter_t read_ops = 0;
416 counter_t read_merged = 0;
417 counter_t read_time = 0;
418 counter_t write_ops = 0;
419 counter_t write_merged = 0;
420 counter_t write_time = 0;
423 diskstats_t *ds, *pre_ds;
425 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
427 fh = fopen ("/proc/partitions", "r");
430 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
434 /* Kernel is 2.4.* */
438 while (fgets (buffer, sizeof (buffer), fh) != NULL)
442 numfields = strsplit (buffer, fields, 32);
444 if ((numfields != (14 + fieldshift)) && (numfields != 7))
447 major = atoll (fields[0]);
448 minor = atoll (fields[1]);
450 disk_name = fields[2];
452 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
453 if (strcmp (disk_name, ds->name) == 0)
458 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
461 if ((ds->name = strdup (disk_name)) == NULL)
476 /* Kernel 2.6, Partition */
477 read_ops = atoll (fields[3]);
478 read_sectors = atoll (fields[4]);
479 write_ops = atoll (fields[5]);
480 write_sectors = atoll (fields[6]);
482 else if (numfields == (14 + fieldshift))
484 read_ops = atoll (fields[3 + fieldshift]);
485 write_ops = atoll (fields[7 + fieldshift]);
487 read_sectors = atoll (fields[5 + fieldshift]);
488 write_sectors = atoll (fields[9 + fieldshift]);
490 if ((fieldshift == 0) || (minor == 0))
493 read_merged = atoll (fields[4 + fieldshift]);
494 read_time = atoll (fields[6 + fieldshift]);
495 write_merged = atoll (fields[8 + fieldshift]);
496 write_time = atoll (fields[10+ fieldshift]);
501 DEBUG ("numfields = %i; => unknown file format.", numfields);
506 counter_t diff_read_sectors;
507 counter_t diff_write_sectors;
509 /* If the counter wraps around, it's only 32 bits.. */
510 if (read_sectors < ds->read_sectors)
511 diff_read_sectors = 1 + read_sectors
512 + (UINT_MAX - ds->read_sectors);
514 diff_read_sectors = read_sectors - ds->read_sectors;
515 if (write_sectors < ds->write_sectors)
516 diff_write_sectors = 1 + write_sectors
517 + (UINT_MAX - ds->write_sectors);
519 diff_write_sectors = write_sectors - ds->write_sectors;
521 ds->read_bytes += 512 * diff_read_sectors;
522 ds->write_bytes += 512 * diff_write_sectors;
523 ds->read_sectors = read_sectors;
524 ds->write_sectors = write_sectors;
527 /* Calculate the average time an io-op needs to complete */
530 counter_t diff_read_ops;
531 counter_t diff_write_ops;
532 counter_t diff_read_time;
533 counter_t diff_write_time;
535 if (read_ops < ds->read_ops)
536 diff_read_ops = 1 + read_ops
537 + (UINT_MAX - ds->read_ops);
539 diff_read_ops = read_ops - ds->read_ops;
540 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
541 "ds->read_ops = %llu; diff_read_ops = %llu;",
543 read_ops, ds->read_ops, diff_read_ops);
545 if (write_ops < ds->write_ops)
546 diff_write_ops = 1 + write_ops
547 + (UINT_MAX - ds->write_ops);
549 diff_write_ops = write_ops - ds->write_ops;
551 if (read_time < ds->read_time)
552 diff_read_time = 1 + read_time
553 + (UINT_MAX - ds->read_time);
555 diff_read_time = read_time - ds->read_time;
557 if (write_time < ds->write_time)
558 diff_write_time = 1 + write_time
559 + (UINT_MAX - ds->write_time);
561 diff_write_time = write_time - ds->write_time;
563 if (diff_read_ops != 0)
564 ds->avg_read_time += (diff_read_time
565 + (diff_read_ops / 2))
567 if (diff_write_ops != 0)
568 ds->avg_write_time += (diff_write_time
569 + (diff_write_ops / 2))
572 ds->read_ops = read_ops;
573 ds->read_time = read_time;
574 ds->write_ops = write_ops;
575 ds->write_time = write_time;
578 /* Don't write to the RRDs if we've just started.. */
580 if (ds->poll_count <= 2)
582 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
583 "(min_poll_count = 2); => Not writing.",
588 if ((read_ops == 0) && (write_ops == 0))
590 DEBUG ("disk plugin: ((read_ops == 0) && "
591 "(write_ops == 0)); => Not writing.");
595 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
596 disk_submit (disk_name, "disk_octets",
597 ds->read_bytes, ds->write_bytes);
599 if ((ds->read_ops != 0) || (ds->write_ops != 0))
600 disk_submit (disk_name, "disk_ops",
601 read_ops, write_ops);
603 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
604 disk_submit (disk_name, "disk_time",
605 ds->avg_read_time, ds->avg_write_time);
609 disk_submit (disk_name, "disk_merged",
610 read_merged, write_merged);
612 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
615 /* #endif defined(KERNEL_LINUX) */
618 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
619 # define KIO_ROCTETS reads
620 # define KIO_WOCTETS writes
621 # define KIO_ROPS nreads
622 # define KIO_WOPS nwrites
623 # define KIO_RTIME rtime
624 # define KIO_WTIME wtime
625 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
626 # define KIO_ROCTETS nread
627 # define KIO_WOCTETS nwritten
628 # define KIO_ROPS reads
629 # define KIO_WOPS writes
630 # define KIO_RTIME rtime
631 # define KIO_WTIME wtime
633 # error "kstat_io_t does not have the required members"
635 static kstat_io_t kio;
641 for (i = 0; i < numdisk; i++)
643 if (kstat_read (kc, ksp[i], &kio) == -1)
646 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
648 disk_submit (ksp[i]->ks_name, "disk_octets",
649 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
650 disk_submit (ksp[i]->ks_name, "disk_ops",
651 kio.KIO_ROPS, kio.KIO_WOPS);
652 /* FIXME: Convert this to microseconds if necessary */
653 disk_submit (ksp[i]->ks_name, "disk_time",
654 kio.KIO_RTIME, kio.KIO_WTIME);
656 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
658 disk_submit (ksp[i]->ks_name, "disk_octets",
659 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
660 disk_submit (ksp[i]->ks_name, "disk_ops",
661 kio.KIO_ROPS, kio.KIO_WOPS);
664 #endif /* defined(HAVE_LIBKSTAT) */
667 } /* int disk_read */
669 void module_register (void)
671 plugin_register_config ("disk", disk_config,
672 config_keys, config_keys_num);
673 plugin_register_init ("disk", disk_init);
674 plugin_register_read ("disk", disk_read);
675 } /* void module_register */