2 * collectd - src/disk.c
3 * Copyright (C) 2005-2007 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>
26 #if HAVE_MACH_MACH_TYPES_H
27 # include <mach/mach_types.h>
29 #if HAVE_MACH_MACH_INIT_H
30 # include <mach/mach_init.h>
32 #if HAVE_MACH_MACH_ERROR_H
33 # include <mach/mach_error.h>
35 #if HAVE_MACH_MACH_PORT_H
36 # include <mach/mach_port.h>
38 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
39 # include <CoreFoundation/CoreFoundation.h>
41 #if HAVE_IOKIT_IOKITLIB_H
42 # include <IOKit/IOKitLib.h>
44 #if HAVE_IOKIT_IOTYPES_H
45 # include <IOKit/IOTypes.h>
47 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
48 # include <IOKit/storage/IOBlockStorageDriver.h>
50 #if HAVE_IOKIT_IOBSD_H
51 # include <IOKit/IOBSD.h>
58 # define UINT_MAX 4294967295U
61 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
62 # define DISK_HAVE_READ 1
64 # define DISK_HAVE_READ 0
68 #if HAVE_IOKIT_IOKITLIB_H
69 static mach_port_t io_master_port = MACH_PORT_NULL;
70 /* #endif HAVE_IOKIT_IOKITLIB_H */
73 typedef struct diskstats
77 /* This overflows in roughly 1361 years */
78 unsigned int poll_count;
80 counter_t read_sectors;
81 counter_t write_sectors;
84 counter_t write_bytes;
91 counter_t avg_read_time;
92 counter_t avg_write_time;
94 struct diskstats *next;
97 static diskstats_t *disklist;
98 /* #endif KERNEL_LINUX */
101 #define MAX_NUMDISK 256
102 extern kstat_ctl_t *kc;
103 static kstat_t *ksp[MAX_NUMDISK];
104 static int numdisk = 0;
105 #endif /* HAVE_LIBKSTAT */
107 static int disk_init (void)
109 #if HAVE_IOKIT_IOKITLIB_H
110 kern_return_t status;
112 if (io_master_port != MACH_PORT_NULL)
114 mach_port_deallocate (mach_task_self (),
116 io_master_port = MACH_PORT_NULL;
119 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
120 if (status != kIOReturnSuccess)
122 ERROR ("IOMasterPort failed: %s",
123 mach_error_string (status));
124 io_master_port = MACH_PORT_NULL;
127 /* #endif HAVE_IOKIT_IOKITLIB_H */
131 /* #endif KERNEL_LINUX */
141 for (numdisk = 0, ksp_chain = kc->kc_chain;
142 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
143 ksp_chain = ksp_chain->ks_next)
145 if (strncmp (ksp_chain->ks_class, "disk", 4)
146 && strncmp (ksp_chain->ks_class, "partition", 9))
148 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
150 ksp[numdisk++] = ksp_chain;
152 #endif /* HAVE_LIBKSTAT */
155 } /* int disk_init */
157 static void disk_submit (const char *plugin_instance,
159 counter_t read, counter_t write)
162 value_list_t vl = VALUE_LIST_INIT;
164 values[0].counter = read;
165 values[1].counter = write;
169 vl.time = time (NULL);
170 strcpy (vl.host, hostname_g);
171 strcpy (vl.plugin, "disk");
172 strncpy (vl.plugin_instance, plugin_instance,
173 sizeof (vl.plugin_instance));
175 plugin_dispatch_values (type, &vl);
176 } /* void disk_submit */
178 #if HAVE_IOKIT_IOKITLIB_H
179 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
181 signed long long val_int;
185 /* `key_obj' needs to be released. */
186 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
187 kCFStringEncodingASCII);
190 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
194 /* get => we don't need to release (== free) the object */
195 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
201 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
205 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
207 DEBUG ("CFNumberGetValue (%s) failed.", key);
213 #endif /* HAVE_IOKIT_IOKITLIB_H */
215 static int disk_read (void)
217 #if HAVE_IOKIT_IOKITLIB_H
218 io_registry_entry_t disk;
219 io_registry_entry_t disk_child;
220 io_iterator_t disk_list;
221 CFDictionaryRef props_dict;
222 CFDictionaryRef stats_dict;
223 CFDictionaryRef child_dict;
224 kern_return_t status;
226 signed long long read_ops;
227 signed long long read_byt;
228 signed long long read_tme;
229 signed long long write_ops;
230 signed long long write_byt;
231 signed long long write_tme;
237 static complain_t complain_obj;
239 /* Get the list of all disk objects. */
240 if (IOServiceGetMatchingServices (io_master_port,
241 IOServiceMatching (kIOBlockStorageDriverClass),
242 &disk_list) != kIOReturnSuccess)
244 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
245 "IOServiceGetMatchingServices failed.");
248 else if (complain_obj.interval != 0)
250 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
251 "IOServiceGetMatchingServices succeeded.");
254 while ((disk = IOIteratorNext (disk_list)) != 0)
260 /* `disk_child' must be released */
261 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
264 /* This fails for example for DVD/CD drives.. */
265 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
266 IOObjectRelease (disk);
270 /* We create `props_dict' => we need to release it later */
271 if (IORegistryEntryCreateCFProperties (disk,
272 (CFMutableDictionaryRef *) &props_dict,
277 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
278 IOObjectRelease (disk_child);
279 IOObjectRelease (disk);
283 if (props_dict == NULL)
285 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
286 IOObjectRelease (disk_child);
287 IOObjectRelease (disk);
291 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
292 CFSTR (kIOBlockStorageDriverStatisticsKey));
294 if (stats_dict == NULL)
296 DEBUG ("CFDictionaryGetValue (%s) failed.",
297 kIOBlockStorageDriverStatisticsKey);
298 CFRelease (props_dict);
299 IOObjectRelease (disk_child);
300 IOObjectRelease (disk);
304 if (IORegistryEntryCreateCFProperties (disk_child,
305 (CFMutableDictionaryRef *) &child_dict,
310 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
311 IOObjectRelease (disk_child);
312 CFRelease (props_dict);
313 IOObjectRelease (disk);
318 disk_major = (int) dict_get_value (child_dict,
320 disk_minor = (int) dict_get_value (child_dict,
322 read_ops = dict_get_value (stats_dict,
323 kIOBlockStorageDriverStatisticsReadsKey);
324 read_byt = dict_get_value (stats_dict,
325 kIOBlockStorageDriverStatisticsBytesReadKey);
326 read_tme = dict_get_value (stats_dict,
327 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
328 write_ops = dict_get_value (stats_dict,
329 kIOBlockStorageDriverStatisticsWritesKey);
330 write_byt = dict_get_value (stats_dict,
331 kIOBlockStorageDriverStatisticsBytesWrittenKey);
332 /* This property describes the number of nanoseconds spent
333 * performing writes since the block storage driver was
334 * instantiated. It is one of the statistic entries listed
335 * under the top-level kIOBlockStorageDriverStatisticsKey
336 * property table. It has an OSNumber value. */
337 write_tme = dict_get_value (stats_dict,
338 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
340 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
342 DEBUG ("snprintf (major, minor) failed.");
343 CFRelease (child_dict);
344 IOObjectRelease (disk_child);
345 CFRelease (props_dict);
346 IOObjectRelease (disk);
349 DEBUG ("disk_name = %s", disk_name);
351 if ((read_byt != -1LL) || (write_byt != -1LL))
352 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
353 if ((read_ops != -1LL) || (write_ops != -1LL))
354 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
355 if ((read_tme != -1LL) || (write_tme != -1LL))
356 disk_submit (disk_name, "disk_time",
360 CFRelease (child_dict);
361 IOObjectRelease (disk_child);
362 CFRelease (props_dict);
363 IOObjectRelease (disk);
365 IOObjectRelease (disk_list);
366 /* #endif HAVE_IOKIT_IOKITLIB_H */
379 counter_t read_sectors = 0;
380 counter_t write_sectors = 0;
382 counter_t read_ops = 0;
383 counter_t read_merged = 0;
384 counter_t read_time = 0;
385 counter_t write_ops = 0;
386 counter_t write_merged = 0;
387 counter_t write_time = 0;
390 diskstats_t *ds, *pre_ds;
392 static complain_t complain_obj;
394 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
396 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
398 plugin_complain (LOG_ERR, &complain_obj,
399 "disk plugin: Failed to open /proc/"
400 "{diskstats,partitions}.");
404 /* Kernel is 2.4.* */
408 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
409 "Succeeded to open /proc/{diskstats,partitions}.");
411 while (fgets (buffer, sizeof (buffer), fh) != NULL)
415 numfields = strsplit (buffer, fields, 32);
417 if ((numfields != (14 + fieldshift)) && (numfields != 7))
420 major = atoll (fields[0]);
421 minor = atoll (fields[1]);
423 disk_name = fields[2];
425 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
426 if (strcmp (disk_name, ds->name) == 0)
431 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
434 if ((ds->name = strdup (disk_name)) == NULL)
449 /* Kernel 2.6, Partition */
450 read_ops = atoll (fields[3]);
451 read_sectors = atoll (fields[4]);
452 write_ops = atoll (fields[5]);
453 write_sectors = atoll (fields[6]);
455 else if (numfields == (14 + fieldshift))
457 read_ops = atoll (fields[3 + fieldshift]);
458 write_ops = atoll (fields[7 + fieldshift]);
460 read_sectors = atoll (fields[5 + fieldshift]);
461 write_sectors = atoll (fields[9 + fieldshift]);
463 if ((fieldshift == 0) || (minor == 0))
466 read_merged = atoll (fields[4 + fieldshift]);
467 read_time = atoll (fields[6 + fieldshift]);
468 write_merged = atoll (fields[8 + fieldshift]);
469 write_time = atoll (fields[10+ fieldshift]);
474 DEBUG ("numfields = %i; => unknown file format.", numfields);
479 counter_t diff_read_sectors;
480 counter_t diff_write_sectors;
482 /* If the counter wraps around, it's only 32 bits.. */
483 if (read_sectors < ds->read_sectors)
484 diff_read_sectors = 1 + read_sectors
485 + (UINT_MAX - ds->read_sectors);
487 diff_read_sectors = read_sectors - ds->read_sectors;
488 if (write_sectors < ds->write_sectors)
489 diff_write_sectors = 1 + write_sectors
490 + (UINT_MAX - ds->write_sectors);
492 diff_write_sectors = write_sectors - ds->write_sectors;
494 ds->read_bytes += 512 * diff_read_sectors;
495 ds->write_bytes += 512 * diff_write_sectors;
496 ds->read_sectors = read_sectors;
497 ds->write_sectors = write_sectors;
500 /* Calculate the average time an io-op needs to complete */
503 counter_t diff_read_ops;
504 counter_t diff_write_ops;
505 counter_t diff_read_time;
506 counter_t diff_write_time;
508 if (read_ops < ds->read_ops)
509 diff_read_ops = 1 + read_ops
510 + (UINT_MAX - ds->read_ops);
512 diff_read_ops = read_ops - ds->read_ops;
513 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
514 "ds->read_ops = %llu; diff_read_ops = %llu;",
516 read_ops, ds->read_ops, diff_read_ops);
518 if (write_ops < ds->write_ops)
519 diff_write_ops = 1 + write_ops
520 + (UINT_MAX - ds->write_ops);
522 diff_write_ops = write_ops - ds->write_ops;
524 if (read_time < ds->read_time)
525 diff_read_time = 1 + read_time
526 + (UINT_MAX - ds->read_time);
528 diff_read_time = read_time - ds->read_time;
530 if (write_time < ds->write_time)
531 diff_write_time = 1 + write_time
532 + (UINT_MAX - ds->write_time);
534 diff_write_time = write_time - ds->write_time;
536 if (diff_read_ops != 0)
537 ds->avg_read_time += (diff_read_time
538 + (diff_read_ops / 2))
540 if (diff_write_ops != 0)
541 ds->avg_write_time += (diff_write_time
542 + (diff_write_ops / 2))
545 ds->read_ops = read_ops;
546 ds->read_time = read_time;
547 ds->write_ops = write_ops;
548 ds->write_time = write_time;
551 /* Don't write to the RRDs if we've just started.. */
553 if (ds->poll_count <= 2)
555 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
556 "(min_poll_count = 2); => Not writing.",
561 if ((read_ops == 0) && (write_ops == 0))
563 DEBUG ("disk plugin: ((read_ops == 0) && "
564 "(write_ops == 0)); => Not writing.");
568 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
569 disk_submit (disk_name, "disk_octets",
570 ds->read_bytes, ds->write_bytes);
572 if ((ds->read_ops != 0) || (ds->write_ops != 0))
573 disk_submit (disk_name, "disk_ops",
574 read_ops, write_ops);
576 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
577 disk_submit (disk_name, "disk_time",
578 ds->avg_read_time, ds->avg_write_time);
582 if ((read_merged != -1LL) || (write_merged != -1LL))
583 disk_submit (disk_name, "disk_merged",
584 read_merged, write_merged);
586 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
589 /* #endif defined(KERNEL_LINUX) */
592 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
593 # define KIO_ROCTETS reads
594 # define KIO_WOCTETS writes
595 # define KIO_ROPS nreads
596 # define KIO_WOPS nwrites
597 # define KIO_RTIME rtime
598 # define KIO_WTIME wtime
599 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
600 # define KIO_ROCTETS nread
601 # define KIO_WOCTETS nwritten
602 # define KIO_ROPS reads
603 # define KIO_WOPS writes
604 # define KIO_RTIME rtime
605 # define KIO_WTIME wtime
607 # error "kstat_io_t does not have the required members"
609 static kstat_io_t kio;
615 for (i = 0; i < numdisk; i++)
617 if (kstat_read (kc, ksp[i], &kio) == -1)
620 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
622 disk_submit (ksp[i]->ks_name, "disk_octets",
623 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
624 disk_submit (ksp[i]->ks_name, "disk_ops",
625 kio.KIO_ROPS, kio.KIO_WOPS);
626 /* FIXME: Convert this to microseconds if necessary */
627 disk_submit (ksp[i]->ks_name, "disk_time",
628 kio.KIO_RTIME, kio.KIO_WTIME);
630 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
632 disk_submit (ksp[i]->ks_name, "disk_octets",
633 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
634 disk_submit (ksp[i]->ks_name, "disk_ops",
635 kio.KIO_ROPS, kio.KIO_WOPS);
638 #endif /* defined(HAVE_LIBKSTAT) */
641 } /* int disk_read */
642 #endif /* DISK_HAVE_READ */
644 void module_register (void)
647 plugin_register_init ("disk", disk_init);
648 plugin_register_read ("disk", disk_read);
649 #endif /* DISK_HAVE_READ */
650 } /* void module_register */