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
62 static mach_port_t io_master_port = MACH_PORT_NULL;
63 /* #endif HAVE_IOKIT_IOKITLIB_H */
66 typedef struct diskstats
70 /* This overflows in roughly 1361 years */
71 unsigned int poll_count;
73 counter_t read_sectors;
74 counter_t write_sectors;
77 counter_t write_bytes;
84 counter_t avg_read_time;
85 counter_t avg_write_time;
87 struct diskstats *next;
90 static diskstats_t *disklist;
91 /* #endif KERNEL_LINUX */
94 #define MAX_NUMDISK 256
95 extern kstat_ctl_t *kc;
96 static kstat_t *ksp[MAX_NUMDISK];
97 static int numdisk = 0;
98 /* #endif HAVE_LIBKSTAT */
101 # error "No applicable input method."
104 static int disk_init (void)
106 #if HAVE_IOKIT_IOKITLIB_H
107 kern_return_t status;
109 if (io_master_port != MACH_PORT_NULL)
111 mach_port_deallocate (mach_task_self (),
113 io_master_port = MACH_PORT_NULL;
116 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
117 if (status != kIOReturnSuccess)
119 ERROR ("IOMasterPort failed: %s",
120 mach_error_string (status));
121 io_master_port = MACH_PORT_NULL;
124 /* #endif HAVE_IOKIT_IOKITLIB_H */
128 /* #endif KERNEL_LINUX */
138 for (numdisk = 0, ksp_chain = kc->kc_chain;
139 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
140 ksp_chain = ksp_chain->ks_next)
142 if (strncmp (ksp_chain->ks_class, "disk", 4)
143 && strncmp (ksp_chain->ks_class, "partition", 9))
145 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
147 ksp[numdisk++] = ksp_chain;
149 #endif /* HAVE_LIBKSTAT */
152 } /* int disk_init */
154 static void disk_submit (const char *plugin_instance,
156 counter_t read, counter_t write)
159 value_list_t vl = VALUE_LIST_INIT;
161 values[0].counter = read;
162 values[1].counter = write;
166 vl.time = time (NULL);
167 strcpy (vl.host, hostname_g);
168 strcpy (vl.plugin, "disk");
169 strncpy (vl.plugin_instance, plugin_instance,
170 sizeof (vl.plugin_instance));
172 plugin_dispatch_values (type, &vl);
173 } /* void disk_submit */
175 #if HAVE_IOKIT_IOKITLIB_H
176 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
178 signed long long val_int;
182 /* `key_obj' needs to be released. */
183 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
184 kCFStringEncodingASCII);
187 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
191 /* get => we don't need to release (== free) the object */
192 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
198 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
202 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
204 DEBUG ("CFNumberGetValue (%s) failed.", key);
210 #endif /* HAVE_IOKIT_IOKITLIB_H */
212 static int disk_read (void)
214 #if HAVE_IOKIT_IOKITLIB_H
215 io_registry_entry_t disk;
216 io_registry_entry_t disk_child;
217 io_iterator_t disk_list;
218 CFDictionaryRef props_dict;
219 CFDictionaryRef stats_dict;
220 CFDictionaryRef child_dict;
221 kern_return_t status;
223 signed long long read_ops;
224 signed long long read_byt;
225 signed long long read_tme;
226 signed long long write_ops;
227 signed long long write_byt;
228 signed long long write_tme;
234 /* Get the list of all disk objects. */
235 if (IOServiceGetMatchingServices (io_master_port,
236 IOServiceMatching (kIOBlockStorageDriverClass),
237 &disk_list) != kIOReturnSuccess)
239 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
243 while ((disk = IOIteratorNext (disk_list)) != 0)
249 /* `disk_child' must be released */
250 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
253 /* This fails for example for DVD/CD drives.. */
254 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
255 IOObjectRelease (disk);
259 /* We create `props_dict' => we need to release it later */
260 if (IORegistryEntryCreateCFProperties (disk,
261 (CFMutableDictionaryRef *) &props_dict,
266 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
267 IOObjectRelease (disk_child);
268 IOObjectRelease (disk);
272 if (props_dict == NULL)
274 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
275 IOObjectRelease (disk_child);
276 IOObjectRelease (disk);
280 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
281 CFSTR (kIOBlockStorageDriverStatisticsKey));
283 if (stats_dict == NULL)
285 DEBUG ("CFDictionaryGetValue (%s) failed.",
286 kIOBlockStorageDriverStatisticsKey);
287 CFRelease (props_dict);
288 IOObjectRelease (disk_child);
289 IOObjectRelease (disk);
293 if (IORegistryEntryCreateCFProperties (disk_child,
294 (CFMutableDictionaryRef *) &child_dict,
299 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
300 IOObjectRelease (disk_child);
301 CFRelease (props_dict);
302 IOObjectRelease (disk);
307 disk_major = (int) dict_get_value (child_dict,
309 disk_minor = (int) dict_get_value (child_dict,
311 read_ops = dict_get_value (stats_dict,
312 kIOBlockStorageDriverStatisticsReadsKey);
313 read_byt = dict_get_value (stats_dict,
314 kIOBlockStorageDriverStatisticsBytesReadKey);
315 read_tme = dict_get_value (stats_dict,
316 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
317 write_ops = dict_get_value (stats_dict,
318 kIOBlockStorageDriverStatisticsWritesKey);
319 write_byt = dict_get_value (stats_dict,
320 kIOBlockStorageDriverStatisticsBytesWrittenKey);
321 /* This property describes the number of nanoseconds spent
322 * performing writes since the block storage driver was
323 * instantiated. It is one of the statistic entries listed
324 * under the top-level kIOBlockStorageDriverStatisticsKey
325 * property table. It has an OSNumber value. */
326 write_tme = dict_get_value (stats_dict,
327 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
329 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
331 DEBUG ("snprintf (major, minor) failed.");
332 CFRelease (child_dict);
333 IOObjectRelease (disk_child);
334 CFRelease (props_dict);
335 IOObjectRelease (disk);
338 DEBUG ("disk_name = %s", disk_name);
340 if ((read_byt != -1LL) || (write_byt != -1LL))
341 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
342 if ((read_ops != -1LL) || (write_ops != -1LL))
343 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
344 if ((read_tme != -1LL) || (write_tme != -1LL))
345 disk_submit (disk_name, "disk_time",
349 CFRelease (child_dict);
350 IOObjectRelease (disk_child);
351 CFRelease (props_dict);
352 IOObjectRelease (disk);
354 IOObjectRelease (disk_list);
355 /* #endif HAVE_IOKIT_IOKITLIB_H */
368 counter_t read_sectors = 0;
369 counter_t write_sectors = 0;
371 counter_t read_ops = 0;
372 counter_t read_merged = 0;
373 counter_t read_time = 0;
374 counter_t write_ops = 0;
375 counter_t write_merged = 0;
376 counter_t write_time = 0;
379 diskstats_t *ds, *pre_ds;
381 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
383 fh = fopen ("/proc/partitions", "r");
386 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
390 /* Kernel is 2.4.* */
394 while (fgets (buffer, sizeof (buffer), fh) != NULL)
398 numfields = strsplit (buffer, fields, 32);
400 if ((numfields != (14 + fieldshift)) && (numfields != 7))
403 major = atoll (fields[0]);
404 minor = atoll (fields[1]);
406 disk_name = fields[2];
408 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
409 if (strcmp (disk_name, ds->name) == 0)
414 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
417 if ((ds->name = strdup (disk_name)) == NULL)
432 /* Kernel 2.6, Partition */
433 read_ops = atoll (fields[3]);
434 read_sectors = atoll (fields[4]);
435 write_ops = atoll (fields[5]);
436 write_sectors = atoll (fields[6]);
438 else if (numfields == (14 + fieldshift))
440 read_ops = atoll (fields[3 + fieldshift]);
441 write_ops = atoll (fields[7 + fieldshift]);
443 read_sectors = atoll (fields[5 + fieldshift]);
444 write_sectors = atoll (fields[9 + fieldshift]);
446 if ((fieldshift == 0) || (minor == 0))
449 read_merged = atoll (fields[4 + fieldshift]);
450 read_time = atoll (fields[6 + fieldshift]);
451 write_merged = atoll (fields[8 + fieldshift]);
452 write_time = atoll (fields[10+ fieldshift]);
457 DEBUG ("numfields = %i; => unknown file format.", numfields);
462 counter_t diff_read_sectors;
463 counter_t diff_write_sectors;
465 /* If the counter wraps around, it's only 32 bits.. */
466 if (read_sectors < ds->read_sectors)
467 diff_read_sectors = 1 + read_sectors
468 + (UINT_MAX - ds->read_sectors);
470 diff_read_sectors = read_sectors - ds->read_sectors;
471 if (write_sectors < ds->write_sectors)
472 diff_write_sectors = 1 + write_sectors
473 + (UINT_MAX - ds->write_sectors);
475 diff_write_sectors = write_sectors - ds->write_sectors;
477 ds->read_bytes += 512 * diff_read_sectors;
478 ds->write_bytes += 512 * diff_write_sectors;
479 ds->read_sectors = read_sectors;
480 ds->write_sectors = write_sectors;
483 /* Calculate the average time an io-op needs to complete */
486 counter_t diff_read_ops;
487 counter_t diff_write_ops;
488 counter_t diff_read_time;
489 counter_t diff_write_time;
491 if (read_ops < ds->read_ops)
492 diff_read_ops = 1 + read_ops
493 + (UINT_MAX - ds->read_ops);
495 diff_read_ops = read_ops - ds->read_ops;
496 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
497 "ds->read_ops = %llu; diff_read_ops = %llu;",
499 read_ops, ds->read_ops, diff_read_ops);
501 if (write_ops < ds->write_ops)
502 diff_write_ops = 1 + write_ops
503 + (UINT_MAX - ds->write_ops);
505 diff_write_ops = write_ops - ds->write_ops;
507 if (read_time < ds->read_time)
508 diff_read_time = 1 + read_time
509 + (UINT_MAX - ds->read_time);
511 diff_read_time = read_time - ds->read_time;
513 if (write_time < ds->write_time)
514 diff_write_time = 1 + write_time
515 + (UINT_MAX - ds->write_time);
517 diff_write_time = write_time - ds->write_time;
519 if (diff_read_ops != 0)
520 ds->avg_read_time += (diff_read_time
521 + (diff_read_ops / 2))
523 if (diff_write_ops != 0)
524 ds->avg_write_time += (diff_write_time
525 + (diff_write_ops / 2))
528 ds->read_ops = read_ops;
529 ds->read_time = read_time;
530 ds->write_ops = write_ops;
531 ds->write_time = write_time;
534 /* Don't write to the RRDs if we've just started.. */
536 if (ds->poll_count <= 2)
538 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
539 "(min_poll_count = 2); => Not writing.",
544 if ((read_ops == 0) && (write_ops == 0))
546 DEBUG ("disk plugin: ((read_ops == 0) && "
547 "(write_ops == 0)); => Not writing.");
551 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
552 disk_submit (disk_name, "disk_octets",
553 ds->read_bytes, ds->write_bytes);
555 if ((ds->read_ops != 0) || (ds->write_ops != 0))
556 disk_submit (disk_name, "disk_ops",
557 read_ops, write_ops);
559 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
560 disk_submit (disk_name, "disk_time",
561 ds->avg_read_time, ds->avg_write_time);
565 if ((read_merged != -1LL) || (write_merged != -1LL))
566 disk_submit (disk_name, "disk_merged",
567 read_merged, write_merged);
569 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
572 /* #endif defined(KERNEL_LINUX) */
575 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
576 # define KIO_ROCTETS reads
577 # define KIO_WOCTETS writes
578 # define KIO_ROPS nreads
579 # define KIO_WOPS nwrites
580 # define KIO_RTIME rtime
581 # define KIO_WTIME wtime
582 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
583 # define KIO_ROCTETS nread
584 # define KIO_WOCTETS nwritten
585 # define KIO_ROPS reads
586 # define KIO_WOPS writes
587 # define KIO_RTIME rtime
588 # define KIO_WTIME wtime
590 # error "kstat_io_t does not have the required members"
592 static kstat_io_t kio;
598 for (i = 0; i < numdisk; i++)
600 if (kstat_read (kc, ksp[i], &kio) == -1)
603 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
605 disk_submit (ksp[i]->ks_name, "disk_octets",
606 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
607 disk_submit (ksp[i]->ks_name, "disk_ops",
608 kio.KIO_ROPS, kio.KIO_WOPS);
609 /* FIXME: Convert this to microseconds if necessary */
610 disk_submit (ksp[i]->ks_name, "disk_time",
611 kio.KIO_RTIME, kio.KIO_WTIME);
613 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
615 disk_submit (ksp[i]->ks_name, "disk_octets",
616 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
617 disk_submit (ksp[i]->ks_name, "disk_ops",
618 kio.KIO_ROPS, kio.KIO_WOPS);
621 #endif /* defined(HAVE_LIBKSTAT) */
624 } /* int disk_read */
626 void module_register (void)
628 plugin_register_init ("disk", disk_init);
629 plugin_register_read ("disk", disk_read);
630 } /* void module_register */