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 static complain_t complain_obj;
236 /* Get the list of all disk objects. */
237 if (IOServiceGetMatchingServices (io_master_port,
238 IOServiceMatching (kIOBlockStorageDriverClass),
239 &disk_list) != kIOReturnSuccess)
241 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
242 "IOServiceGetMatchingServices failed.");
245 else if (complain_obj.interval != 0)
247 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
248 "IOServiceGetMatchingServices succeeded.");
251 while ((disk = IOIteratorNext (disk_list)) != 0)
257 /* `disk_child' must be released */
258 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
261 /* This fails for example for DVD/CD drives.. */
262 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
263 IOObjectRelease (disk);
267 /* We create `props_dict' => we need to release it later */
268 if (IORegistryEntryCreateCFProperties (disk,
269 (CFMutableDictionaryRef *) &props_dict,
274 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
275 IOObjectRelease (disk_child);
276 IOObjectRelease (disk);
280 if (props_dict == NULL)
282 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
283 IOObjectRelease (disk_child);
284 IOObjectRelease (disk);
288 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
289 CFSTR (kIOBlockStorageDriverStatisticsKey));
291 if (stats_dict == NULL)
293 DEBUG ("CFDictionaryGetValue (%s) failed.",
294 kIOBlockStorageDriverStatisticsKey);
295 CFRelease (props_dict);
296 IOObjectRelease (disk_child);
297 IOObjectRelease (disk);
301 if (IORegistryEntryCreateCFProperties (disk_child,
302 (CFMutableDictionaryRef *) &child_dict,
307 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
308 IOObjectRelease (disk_child);
309 CFRelease (props_dict);
310 IOObjectRelease (disk);
315 disk_major = (int) dict_get_value (child_dict,
317 disk_minor = (int) dict_get_value (child_dict,
319 read_ops = dict_get_value (stats_dict,
320 kIOBlockStorageDriverStatisticsReadsKey);
321 read_byt = dict_get_value (stats_dict,
322 kIOBlockStorageDriverStatisticsBytesReadKey);
323 read_tme = dict_get_value (stats_dict,
324 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
325 write_ops = dict_get_value (stats_dict,
326 kIOBlockStorageDriverStatisticsWritesKey);
327 write_byt = dict_get_value (stats_dict,
328 kIOBlockStorageDriverStatisticsBytesWrittenKey);
329 /* This property describes the number of nanoseconds spent
330 * performing writes since the block storage driver was
331 * instantiated. It is one of the statistic entries listed
332 * under the top-level kIOBlockStorageDriverStatisticsKey
333 * property table. It has an OSNumber value. */
334 write_tme = dict_get_value (stats_dict,
335 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
337 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
339 DEBUG ("snprintf (major, minor) failed.");
340 CFRelease (child_dict);
341 IOObjectRelease (disk_child);
342 CFRelease (props_dict);
343 IOObjectRelease (disk);
346 DEBUG ("disk_name = %s", disk_name);
348 if ((read_byt != -1LL) || (write_byt != -1LL))
349 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
350 if ((read_ops != -1LL) || (write_ops != -1LL))
351 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
352 if ((read_tme != -1LL) || (write_tme != -1LL))
353 disk_submit (disk_name, "disk_time",
357 CFRelease (child_dict);
358 IOObjectRelease (disk_child);
359 CFRelease (props_dict);
360 IOObjectRelease (disk);
362 IOObjectRelease (disk_list);
363 /* #endif HAVE_IOKIT_IOKITLIB_H */
376 counter_t read_sectors = 0;
377 counter_t write_sectors = 0;
379 counter_t read_ops = 0;
380 counter_t read_merged = 0;
381 counter_t read_time = 0;
382 counter_t write_ops = 0;
383 counter_t write_merged = 0;
384 counter_t write_time = 0;
387 diskstats_t *ds, *pre_ds;
389 static complain_t complain_obj;
391 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
393 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
395 plugin_complain (LOG_ERR, &complain_obj,
396 "disk plugin: Failed to open /proc/"
397 "{diskstats,partitions}.");
401 /* Kernel is 2.4.* */
405 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
406 "Succeeded to open /proc/{diskstats,partitions}.");
408 while (fgets (buffer, sizeof (buffer), fh) != NULL)
412 numfields = strsplit (buffer, fields, 32);
414 if ((numfields != (14 + fieldshift)) && (numfields != 7))
417 major = atoll (fields[0]);
418 minor = atoll (fields[1]);
420 disk_name = fields[2];
422 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
423 if (strcmp (disk_name, ds->name) == 0)
428 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
431 if ((ds->name = strdup (disk_name)) == NULL)
446 /* Kernel 2.6, Partition */
447 read_ops = atoll (fields[3]);
448 read_sectors = atoll (fields[4]);
449 write_ops = atoll (fields[5]);
450 write_sectors = atoll (fields[6]);
452 else if (numfields == (14 + fieldshift))
454 read_ops = atoll (fields[3 + fieldshift]);
455 write_ops = atoll (fields[7 + fieldshift]);
457 read_sectors = atoll (fields[5 + fieldshift]);
458 write_sectors = atoll (fields[9 + fieldshift]);
460 if ((fieldshift == 0) || (minor == 0))
463 read_merged = atoll (fields[4 + fieldshift]);
464 read_time = atoll (fields[6 + fieldshift]);
465 write_merged = atoll (fields[8 + fieldshift]);
466 write_time = atoll (fields[10+ fieldshift]);
471 DEBUG ("numfields = %i; => unknown file format.", numfields);
476 counter_t diff_read_sectors;
477 counter_t diff_write_sectors;
479 /* If the counter wraps around, it's only 32 bits.. */
480 if (read_sectors < ds->read_sectors)
481 diff_read_sectors = 1 + read_sectors
482 + (UINT_MAX - ds->read_sectors);
484 diff_read_sectors = read_sectors - ds->read_sectors;
485 if (write_sectors < ds->write_sectors)
486 diff_write_sectors = 1 + write_sectors
487 + (UINT_MAX - ds->write_sectors);
489 diff_write_sectors = write_sectors - ds->write_sectors;
491 ds->read_bytes += 512 * diff_read_sectors;
492 ds->write_bytes += 512 * diff_write_sectors;
493 ds->read_sectors = read_sectors;
494 ds->write_sectors = write_sectors;
497 /* Calculate the average time an io-op needs to complete */
500 counter_t diff_read_ops;
501 counter_t diff_write_ops;
502 counter_t diff_read_time;
503 counter_t diff_write_time;
505 if (read_ops < ds->read_ops)
506 diff_read_ops = 1 + read_ops
507 + (UINT_MAX - ds->read_ops);
509 diff_read_ops = read_ops - ds->read_ops;
510 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
511 "ds->read_ops = %llu; diff_read_ops = %llu;",
513 read_ops, ds->read_ops, diff_read_ops);
515 if (write_ops < ds->write_ops)
516 diff_write_ops = 1 + write_ops
517 + (UINT_MAX - ds->write_ops);
519 diff_write_ops = write_ops - ds->write_ops;
521 if (read_time < ds->read_time)
522 diff_read_time = 1 + read_time
523 + (UINT_MAX - ds->read_time);
525 diff_read_time = read_time - ds->read_time;
527 if (write_time < ds->write_time)
528 diff_write_time = 1 + write_time
529 + (UINT_MAX - ds->write_time);
531 diff_write_time = write_time - ds->write_time;
533 if (diff_read_ops != 0)
534 ds->avg_read_time += (diff_read_time
535 + (diff_read_ops / 2))
537 if (diff_write_ops != 0)
538 ds->avg_write_time += (diff_write_time
539 + (diff_write_ops / 2))
542 ds->read_ops = read_ops;
543 ds->read_time = read_time;
544 ds->write_ops = write_ops;
545 ds->write_time = write_time;
548 /* Don't write to the RRDs if we've just started.. */
550 if (ds->poll_count <= 2)
552 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
553 "(min_poll_count = 2); => Not writing.",
558 if ((read_ops == 0) && (write_ops == 0))
560 DEBUG ("disk plugin: ((read_ops == 0) && "
561 "(write_ops == 0)); => Not writing.");
565 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
566 disk_submit (disk_name, "disk_octets",
567 ds->read_bytes, ds->write_bytes);
569 if ((ds->read_ops != 0) || (ds->write_ops != 0))
570 disk_submit (disk_name, "disk_ops",
571 read_ops, write_ops);
573 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
574 disk_submit (disk_name, "disk_time",
575 ds->avg_read_time, ds->avg_write_time);
579 if ((read_merged != -1LL) || (write_merged != -1LL))
580 disk_submit (disk_name, "disk_merged",
581 read_merged, write_merged);
583 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
586 /* #endif defined(KERNEL_LINUX) */
589 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
590 # define KIO_ROCTETS reads
591 # define KIO_WOCTETS writes
592 # define KIO_ROPS nreads
593 # define KIO_WOPS nwrites
594 # define KIO_RTIME rtime
595 # define KIO_WTIME wtime
596 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
597 # define KIO_ROCTETS nread
598 # define KIO_WOCTETS nwritten
599 # define KIO_ROPS reads
600 # define KIO_WOPS writes
601 # define KIO_RTIME rtime
602 # define KIO_WTIME wtime
604 # error "kstat_io_t does not have the required members"
606 static kstat_io_t kio;
612 for (i = 0; i < numdisk; i++)
614 if (kstat_read (kc, ksp[i], &kio) == -1)
617 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
619 disk_submit (ksp[i]->ks_name, "disk_octets",
620 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
621 disk_submit (ksp[i]->ks_name, "disk_ops",
622 kio.KIO_ROPS, kio.KIO_WOPS);
623 /* FIXME: Convert this to microseconds if necessary */
624 disk_submit (ksp[i]->ks_name, "disk_time",
625 kio.KIO_RTIME, kio.KIO_WTIME);
627 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 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);
635 #endif /* defined(HAVE_LIBKSTAT) */
638 } /* int disk_read */
640 void module_register (void)
642 plugin_register_init ("disk", disk_init);
643 plugin_register_read ("disk", disk_read);
644 } /* void module_register */