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];
424 { /* Strip slashes from disk_name */
426 for (i = 0; disk_name[i] != '\0'; i++)
427 if (disk_name[i] == '/')
432 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
433 if (strcmp (disk_name, ds->name) == 0)
438 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
441 if ((ds->name = strdup (disk_name)) == NULL)
456 /* Kernel 2.6, Partition */
457 read_ops = atoll (fields[3]);
458 read_sectors = atoll (fields[4]);
459 write_ops = atoll (fields[5]);
460 write_sectors = atoll (fields[6]);
462 else if (numfields == (14 + fieldshift))
464 read_ops = atoll (fields[3 + fieldshift]);
465 write_ops = atoll (fields[7 + fieldshift]);
467 read_sectors = atoll (fields[5 + fieldshift]);
468 write_sectors = atoll (fields[9 + fieldshift]);
470 if ((fieldshift == 0) || (minor == 0))
473 read_merged = atoll (fields[4 + fieldshift]);
474 read_time = atoll (fields[6 + fieldshift]);
475 write_merged = atoll (fields[8 + fieldshift]);
476 write_time = atoll (fields[10+ fieldshift]);
481 DEBUG ("numfields = %i; => unknown file format.", numfields);
486 counter_t diff_read_sectors;
487 counter_t diff_write_sectors;
489 /* If the counter wraps around, it's only 32 bits.. */
490 if (read_sectors < ds->read_sectors)
491 diff_read_sectors = 1 + read_sectors
492 + (UINT_MAX - ds->read_sectors);
494 diff_read_sectors = read_sectors - ds->read_sectors;
495 if (write_sectors < ds->write_sectors)
496 diff_write_sectors = 1 + write_sectors
497 + (UINT_MAX - ds->write_sectors);
499 diff_write_sectors = write_sectors - ds->write_sectors;
501 ds->read_bytes += 512 * diff_read_sectors;
502 ds->write_bytes += 512 * diff_write_sectors;
503 ds->read_sectors = read_sectors;
504 ds->write_sectors = write_sectors;
507 /* Calculate the average time an io-op needs to complete */
510 counter_t diff_read_ops;
511 counter_t diff_write_ops;
512 counter_t diff_read_time;
513 counter_t diff_write_time;
515 if (read_ops < ds->read_ops)
516 diff_read_ops = 1 + read_ops
517 + (UINT_MAX - ds->read_ops);
519 diff_read_ops = read_ops - ds->read_ops;
520 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
521 "ds->read_ops = %llu; diff_read_ops = %llu;",
523 read_ops, ds->read_ops, diff_read_ops);
525 if (write_ops < ds->write_ops)
526 diff_write_ops = 1 + write_ops
527 + (UINT_MAX - ds->write_ops);
529 diff_write_ops = write_ops - ds->write_ops;
531 if (read_time < ds->read_time)
532 diff_read_time = 1 + read_time
533 + (UINT_MAX - ds->read_time);
535 diff_read_time = read_time - ds->read_time;
537 if (write_time < ds->write_time)
538 diff_write_time = 1 + write_time
539 + (UINT_MAX - ds->write_time);
541 diff_write_time = write_time - ds->write_time;
543 if (diff_read_ops != 0)
544 ds->avg_read_time += (diff_read_time
545 + (diff_read_ops / 2))
547 if (diff_write_ops != 0)
548 ds->avg_write_time += (diff_write_time
549 + (diff_write_ops / 2))
552 ds->read_ops = read_ops;
553 ds->read_time = read_time;
554 ds->write_ops = write_ops;
555 ds->write_time = write_time;
558 /* Don't write to the RRDs if we've just started.. */
560 if (ds->poll_count <= 2)
562 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
563 "(min_poll_count = 2); => Not writing.",
568 if ((read_ops == 0) && (write_ops == 0))
570 DEBUG ("disk plugin: ((read_ops == 0) && "
571 "(write_ops == 0)); => Not writing.");
575 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
576 disk_submit (disk_name, "disk_octets",
577 ds->read_bytes, ds->write_bytes);
579 if ((ds->read_ops != 0) || (ds->write_ops != 0))
580 disk_submit (disk_name, "disk_ops",
581 read_ops, write_ops);
583 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
584 disk_submit (disk_name, "disk_time",
585 ds->avg_read_time, ds->avg_write_time);
589 if ((read_merged != -1LL) || (write_merged != -1LL))
590 disk_submit (disk_name, "disk_merged",
591 read_merged, write_merged);
593 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
596 /* #endif defined(KERNEL_LINUX) */
599 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
600 # define KIO_ROCTETS reads
601 # define KIO_WOCTETS writes
602 # define KIO_ROPS nreads
603 # define KIO_WOPS nwrites
604 # define KIO_RTIME rtime
605 # define KIO_WTIME wtime
606 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
607 # define KIO_ROCTETS nread
608 # define KIO_WOCTETS nwritten
609 # define KIO_ROPS reads
610 # define KIO_WOPS writes
611 # define KIO_RTIME rtime
612 # define KIO_WTIME wtime
614 # error "kstat_io_t does not have the required members"
616 static kstat_io_t kio;
622 for (i = 0; i < numdisk; i++)
624 if (kstat_read (kc, ksp[i], &kio) == -1)
627 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
629 disk_submit (ksp[i]->ks_name, "disk_octets",
630 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
631 disk_submit (ksp[i]->ks_name, "disk_ops",
632 kio.KIO_ROPS, kio.KIO_WOPS);
633 /* FIXME: Convert this to microseconds if necessary */
634 disk_submit (ksp[i]->ks_name, "disk_time",
635 kio.KIO_RTIME, kio.KIO_WTIME);
637 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
639 disk_submit (ksp[i]->ks_name, "disk_octets",
640 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
641 disk_submit (ksp[i]->ks_name, "disk_ops",
642 kio.KIO_ROPS, kio.KIO_WOPS);
645 #endif /* defined(HAVE_LIBKSTAT) */
648 } /* int disk_read */
649 #endif /* DISK_HAVE_READ */
651 void module_register (void)
654 plugin_register_init ("disk", disk_init);
655 plugin_register_read ("disk", disk_read);
656 #endif /* DISK_HAVE_READ */
657 } /* void module_register */