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 sstrncpy (vl.plugin_instance, plugin_instance,
219 sizeof (vl.plugin_instance));
220 sstrncpy (vl.type, type, sizeof (vl.type));
222 plugin_dispatch_values (&vl);
223 } /* void disk_submit */
225 #if HAVE_IOKIT_IOKITLIB_H
226 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
228 signed long long val_int;
232 /* `key_obj' needs to be released. */
233 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
234 kCFStringEncodingASCII);
237 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
241 /* get => we don't need to release (== free) the object */
242 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
248 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
252 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
254 DEBUG ("CFNumberGetValue (%s) failed.", key);
260 #endif /* HAVE_IOKIT_IOKITLIB_H */
262 static int disk_read (void)
264 #if HAVE_IOKIT_IOKITLIB_H
265 io_registry_entry_t disk;
266 io_registry_entry_t disk_child;
267 io_iterator_t disk_list;
268 CFDictionaryRef props_dict;
269 CFDictionaryRef stats_dict;
270 CFDictionaryRef child_dict;
271 kern_return_t status;
273 signed long long read_ops;
274 signed long long read_byt;
275 signed long long read_tme;
276 signed long long write_ops;
277 signed long long write_byt;
278 signed long long write_tme;
284 /* Get the list of all disk objects. */
285 if (IOServiceGetMatchingServices (io_master_port,
286 IOServiceMatching (kIOBlockStorageDriverClass),
287 &disk_list) != kIOReturnSuccess)
289 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
293 while ((disk = IOIteratorNext (disk_list)) != 0)
299 /* `disk_child' must be released */
300 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
303 /* This fails for example for DVD/CD drives.. */
304 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
305 IOObjectRelease (disk);
309 /* We create `props_dict' => we need to release it later */
310 if (IORegistryEntryCreateCFProperties (disk,
311 (CFMutableDictionaryRef *) &props_dict,
316 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
317 IOObjectRelease (disk_child);
318 IOObjectRelease (disk);
322 if (props_dict == NULL)
324 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
325 IOObjectRelease (disk_child);
326 IOObjectRelease (disk);
330 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
331 CFSTR (kIOBlockStorageDriverStatisticsKey));
333 if (stats_dict == NULL)
335 DEBUG ("CFDictionaryGetValue (%s) failed.",
336 kIOBlockStorageDriverStatisticsKey);
337 CFRelease (props_dict);
338 IOObjectRelease (disk_child);
339 IOObjectRelease (disk);
343 if (IORegistryEntryCreateCFProperties (disk_child,
344 (CFMutableDictionaryRef *) &child_dict,
349 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
350 IOObjectRelease (disk_child);
351 CFRelease (props_dict);
352 IOObjectRelease (disk);
357 disk_major = (int) dict_get_value (child_dict,
359 disk_minor = (int) dict_get_value (child_dict,
361 read_ops = dict_get_value (stats_dict,
362 kIOBlockStorageDriverStatisticsReadsKey);
363 read_byt = dict_get_value (stats_dict,
364 kIOBlockStorageDriverStatisticsBytesReadKey);
365 read_tme = dict_get_value (stats_dict,
366 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
367 write_ops = dict_get_value (stats_dict,
368 kIOBlockStorageDriverStatisticsWritesKey);
369 write_byt = dict_get_value (stats_dict,
370 kIOBlockStorageDriverStatisticsBytesWrittenKey);
371 /* This property describes the number of nanoseconds spent
372 * performing writes since the block storage driver was
373 * instantiated. It is one of the statistic entries listed
374 * under the top-level kIOBlockStorageDriverStatisticsKey
375 * property table. It has an OSNumber value. */
376 write_tme = dict_get_value (stats_dict,
377 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
379 if (ssnprintf (disk_name, sizeof (disk_name),
380 "%i-%i", disk_major, disk_minor) >= sizeof (disk_name))
382 DEBUG ("snprintf (major, minor) failed.");
383 CFRelease (child_dict);
384 IOObjectRelease (disk_child);
385 CFRelease (props_dict);
386 IOObjectRelease (disk);
389 DEBUG ("disk_name = %s", disk_name);
391 if ((read_byt != -1LL) || (write_byt != -1LL))
392 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
393 if ((read_ops != -1LL) || (write_ops != -1LL))
394 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
395 if ((read_tme != -1LL) || (write_tme != -1LL))
396 disk_submit (disk_name, "disk_time",
400 CFRelease (child_dict);
401 IOObjectRelease (disk_child);
402 CFRelease (props_dict);
403 IOObjectRelease (disk);
405 IOObjectRelease (disk_list);
406 /* #endif HAVE_IOKIT_IOKITLIB_H */
419 counter_t read_sectors = 0;
420 counter_t write_sectors = 0;
422 counter_t read_ops = 0;
423 counter_t read_merged = 0;
424 counter_t read_time = 0;
425 counter_t write_ops = 0;
426 counter_t write_merged = 0;
427 counter_t write_time = 0;
430 diskstats_t *ds, *pre_ds;
432 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
434 fh = fopen ("/proc/partitions", "r");
437 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
441 /* Kernel is 2.4.* */
445 while (fgets (buffer, sizeof (buffer), fh) != NULL)
449 numfields = strsplit (buffer, fields, 32);
451 if ((numfields != (14 + fieldshift)) && (numfields != 7))
454 major = atoll (fields[0]);
455 minor = atoll (fields[1]);
457 disk_name = fields[2 + fieldshift];
459 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
460 if (strcmp (disk_name, ds->name) == 0)
465 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
468 if ((ds->name = strdup (disk_name)) == NULL)
483 /* Kernel 2.6, Partition */
484 read_ops = atoll (fields[3]);
485 read_sectors = atoll (fields[4]);
486 write_ops = atoll (fields[5]);
487 write_sectors = atoll (fields[6]);
489 else if (numfields == (14 + fieldshift))
491 read_ops = atoll (fields[3 + fieldshift]);
492 write_ops = atoll (fields[7 + fieldshift]);
494 read_sectors = atoll (fields[5 + fieldshift]);
495 write_sectors = atoll (fields[9 + fieldshift]);
497 if ((fieldshift == 0) || (minor == 0))
500 read_merged = atoll (fields[4 + fieldshift]);
501 read_time = atoll (fields[6 + fieldshift]);
502 write_merged = atoll (fields[8 + fieldshift]);
503 write_time = atoll (fields[10+ fieldshift]);
508 DEBUG ("numfields = %i; => unknown file format.", numfields);
513 counter_t diff_read_sectors;
514 counter_t diff_write_sectors;
516 /* If the counter wraps around, it's only 32 bits.. */
517 if (read_sectors < ds->read_sectors)
518 diff_read_sectors = 1 + read_sectors
519 + (UINT_MAX - ds->read_sectors);
521 diff_read_sectors = read_sectors - ds->read_sectors;
522 if (write_sectors < ds->write_sectors)
523 diff_write_sectors = 1 + write_sectors
524 + (UINT_MAX - ds->write_sectors);
526 diff_write_sectors = write_sectors - ds->write_sectors;
528 ds->read_bytes += 512 * diff_read_sectors;
529 ds->write_bytes += 512 * diff_write_sectors;
530 ds->read_sectors = read_sectors;
531 ds->write_sectors = write_sectors;
534 /* Calculate the average time an io-op needs to complete */
537 counter_t diff_read_ops;
538 counter_t diff_write_ops;
539 counter_t diff_read_time;
540 counter_t diff_write_time;
542 if (read_ops < ds->read_ops)
543 diff_read_ops = 1 + read_ops
544 + (UINT_MAX - ds->read_ops);
546 diff_read_ops = read_ops - ds->read_ops;
547 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
548 "ds->read_ops = %llu; diff_read_ops = %llu;",
550 read_ops, ds->read_ops, diff_read_ops);
552 if (write_ops < ds->write_ops)
553 diff_write_ops = 1 + write_ops
554 + (UINT_MAX - ds->write_ops);
556 diff_write_ops = write_ops - ds->write_ops;
558 if (read_time < ds->read_time)
559 diff_read_time = 1 + read_time
560 + (UINT_MAX - ds->read_time);
562 diff_read_time = read_time - ds->read_time;
564 if (write_time < ds->write_time)
565 diff_write_time = 1 + write_time
566 + (UINT_MAX - ds->write_time);
568 diff_write_time = write_time - ds->write_time;
570 if (diff_read_ops != 0)
571 ds->avg_read_time += (diff_read_time
572 + (diff_read_ops / 2))
574 if (diff_write_ops != 0)
575 ds->avg_write_time += (diff_write_time
576 + (diff_write_ops / 2))
579 ds->read_ops = read_ops;
580 ds->read_time = read_time;
581 ds->write_ops = write_ops;
582 ds->write_time = write_time;
585 /* Don't write to the RRDs if we've just started.. */
587 if (ds->poll_count <= 2)
589 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
590 "(min_poll_count = 2); => Not writing.",
595 if ((read_ops == 0) && (write_ops == 0))
597 DEBUG ("disk plugin: ((read_ops == 0) && "
598 "(write_ops == 0)); => Not writing.");
602 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
603 disk_submit (disk_name, "disk_octets",
604 ds->read_bytes, ds->write_bytes);
606 if ((ds->read_ops != 0) || (ds->write_ops != 0))
607 disk_submit (disk_name, "disk_ops",
608 read_ops, write_ops);
610 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
611 disk_submit (disk_name, "disk_time",
612 ds->avg_read_time, ds->avg_write_time);
616 disk_submit (disk_name, "disk_merged",
617 read_merged, write_merged);
619 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
622 /* #endif defined(KERNEL_LINUX) */
625 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
626 # define KIO_ROCTETS reads
627 # define KIO_WOCTETS writes
628 # define KIO_ROPS nreads
629 # define KIO_WOPS nwrites
630 # define KIO_RTIME rtime
631 # define KIO_WTIME wtime
632 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
633 # define KIO_ROCTETS nread
634 # define KIO_WOCTETS nwritten
635 # define KIO_ROPS reads
636 # define KIO_WOPS writes
637 # define KIO_RTIME rtime
638 # define KIO_WTIME wtime
640 # error "kstat_io_t does not have the required members"
642 static kstat_io_t kio;
648 for (i = 0; i < numdisk; i++)
650 if (kstat_read (kc, ksp[i], &kio) == -1)
653 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
655 disk_submit (ksp[i]->ks_name, "disk_octets",
656 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
657 disk_submit (ksp[i]->ks_name, "disk_ops",
658 kio.KIO_ROPS, kio.KIO_WOPS);
659 /* FIXME: Convert this to microseconds if necessary */
660 disk_submit (ksp[i]->ks_name, "disk_time",
661 kio.KIO_RTIME, kio.KIO_WTIME);
663 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
665 disk_submit (ksp[i]->ks_name, "disk_octets",
666 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
667 disk_submit (ksp[i]->ks_name, "disk_ops",
668 kio.KIO_ROPS, kio.KIO_WOPS);
671 /* #endif defined(HAVE_LIBKSTAT) */
673 #elif defined(HAVE_LIBSTATGRAB)
674 sg_disk_io_stats *ds;
676 char name[DATA_MAX_NAME_LEN];
678 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
681 for (counter=0; counter < disks; counter++) {
682 strncpy(name, ds->disk_name, sizeof(name));
683 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
684 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
687 #endif /* defined(HAVE_LIBSTATGRAB) */
690 } /* int disk_read */
692 void module_register (void)
694 plugin_register_config ("disk", disk_config,
695 config_keys, config_keys_num);
696 plugin_register_init ("disk", disk_init);
697 plugin_register_read ("disk", disk_read);
698 } /* void module_register */