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>
25 #include "utils_debug.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>
55 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
56 # define DISK_HAVE_READ 1
58 # define DISK_HAVE_READ 0
61 /* 2^34 = 17179869184 = ~17.2GByte/s */
62 static data_source_t octets_dsrc[2] =
64 {"read", DS_TYPE_COUNTER, 0, 17179869183.0},
65 {"write", DS_TYPE_COUNTER, 0, 17179869183.0}
68 static data_set_t octets_ds =
70 "disk_octets", 2, octets_dsrc
73 static data_source_t operations_dsrc[2] =
75 {"read", DS_TYPE_COUNTER, 0, 4294967295.0},
76 {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
79 static data_set_t operations_ds =
81 "disk_ops", 2, operations_dsrc
84 static data_source_t merged_dsrc[2] =
86 {"read", DS_TYPE_COUNTER, 0, 4294967295.0},
87 {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
90 static data_set_t merged_ds =
92 "disk_merged", 2, merged_dsrc
95 /* max is 1000000us per second. */
96 static data_source_t time_dsrc[2] =
98 {"read", DS_TYPE_COUNTER, 0, 1000000.0},
99 {"write", DS_TYPE_COUNTER, 0, 1000000.0}
102 static data_set_t time_ds =
104 "disk_time", 2, time_dsrc
108 #if HAVE_IOKIT_IOKITLIB_H
109 static mach_port_t io_master_port = MACH_PORT_NULL;
110 /* #endif HAVE_IOKIT_IOKITLIB_H */
113 typedef struct diskstats
117 /* This overflows in roughly 1361 year */
118 unsigned int poll_count;
120 counter_t read_sectors;
121 counter_t write_sectors;
123 counter_t read_bytes;
124 counter_t write_bytes;
126 struct diskstats *next;
129 static diskstats_t *disklist;
130 static int min_poll_count;
131 /* #endif KERNEL_LINUX */
134 #define MAX_NUMDISK 256
135 extern kstat_ctl_t *kc;
136 static kstat_t *ksp[MAX_NUMDISK];
137 static int numdisk = 0;
138 #endif /* HAVE_LIBKSTAT */
140 static int disk_init (void)
142 #if HAVE_IOKIT_IOKITLIB_H
143 kern_return_t status;
145 if (io_master_port != MACH_PORT_NULL)
147 mach_port_deallocate (mach_task_self (),
149 io_master_port = MACH_PORT_NULL;
152 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
153 if (status != kIOReturnSuccess)
155 syslog (LOG_ERR, "IOMasterPort failed: %s",
156 mach_error_string (status));
157 io_master_port = MACH_PORT_NULL;
160 /* #endif HAVE_IOKIT_IOKITLIB_H */
166 step = atoi (COLLECTD_STEP);
167 heartbeat = atoi (COLLECTD_HEARTBEAT);
170 assert (heartbeat >= step);
172 min_poll_count = 1 + (heartbeat / step);
173 DBG ("min_poll_count = %i;", min_poll_count);
174 /* #endif KERNEL_LINUX */
184 for (numdisk = 0, ksp_chain = kc->kc_chain;
185 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
186 ksp_chain = ksp_chain->ks_next)
188 if (strncmp (ksp_chain->ks_class, "disk", 4)
189 && strncmp (ksp_chain->ks_class, "partition", 9))
191 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
193 ksp[numdisk++] = ksp_chain;
195 #endif /* HAVE_LIBKSTAT */
198 } /* int disk_init */
200 static void disk_submit (const char *plugin_instance,
202 counter_t read, counter_t write)
205 value_list_t vl = VALUE_LIST_INIT;
207 values[0].counter = read;
208 values[1].counter = write;
212 vl.time = time (NULL);
213 strcpy (vl.host, hostname_g);
214 strcpy (vl.plugin, "disk");
215 strncpy (vl.plugin_instance, plugin_instance,
216 sizeof (vl.plugin_instance));
218 plugin_dispatch_values (type, &vl);
219 } /* void disk_submit */
221 #if HAVE_IOKIT_IOKITLIB_H
222 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
224 signed long long val_int;
228 /* `key_obj' needs to be released. */
229 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
230 kCFStringEncodingASCII);
233 DBG ("CFStringCreateWithCString (%s) failed.", key);
237 /* get => we don't need to release (== free) the object */
238 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
244 DBG ("CFDictionaryGetValue (%s) failed.", key);
248 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
250 DBG ("CFNumberGetValue (%s) failed.", key);
256 #endif /* HAVE_IOKIT_IOKITLIB_H */
258 static int disk_read (void)
260 #if HAVE_IOKIT_IOKITLIB_H
261 io_registry_entry_t disk;
262 io_registry_entry_t disk_child;
263 io_iterator_t disk_list;
264 CFDictionaryRef props_dict;
265 CFDictionaryRef stats_dict;
266 CFDictionaryRef child_dict;
267 kern_return_t status;
269 signed long long read_ops;
270 signed long long read_byt;
271 signed long long read_tme;
272 signed long long write_ops;
273 signed long long write_byt;
274 signed long long write_tme;
280 static complain_t complain_obj;
282 /* Get the list of all disk objects. */
283 if (IOServiceGetMatchingServices (io_master_port,
284 IOServiceMatching (kIOBlockStorageDriverClass),
285 &disk_list) != kIOReturnSuccess)
287 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
288 "IOServiceGetMatchingServices failed.");
291 else if (complain_obj.interval != 0)
293 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
294 "IOServiceGetMatchingServices succeeded.");
297 while ((disk = IOIteratorNext (disk_list)) != 0)
303 /* `disk_child' must be released */
304 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
307 /* This fails for example for DVD/CD drives.. */
308 DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
309 IOObjectRelease (disk);
313 /* We create `props_dict' => we need to release it later */
314 if (IORegistryEntryCreateCFProperties (disk,
315 (CFMutableDictionaryRef *) &props_dict,
320 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
321 IOObjectRelease (disk_child);
322 IOObjectRelease (disk);
326 if (props_dict == NULL)
328 DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
329 IOObjectRelease (disk_child);
330 IOObjectRelease (disk);
334 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
335 CFSTR (kIOBlockStorageDriverStatisticsKey));
337 if (stats_dict == NULL)
339 DBG ("CFDictionaryGetValue (%s) failed.",
340 kIOBlockStorageDriverStatisticsKey);
341 CFRelease (props_dict);
342 IOObjectRelease (disk_child);
343 IOObjectRelease (disk);
347 if (IORegistryEntryCreateCFProperties (disk_child,
348 (CFMutableDictionaryRef *) &child_dict,
353 DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
354 IOObjectRelease (disk_child);
355 CFRelease (props_dict);
356 IOObjectRelease (disk);
361 disk_major = (int) dict_get_value (child_dict,
363 disk_minor = (int) dict_get_value (child_dict,
365 read_ops = dict_get_value (stats_dict,
366 kIOBlockStorageDriverStatisticsReadsKey);
367 read_byt = dict_get_value (stats_dict,
368 kIOBlockStorageDriverStatisticsBytesReadKey);
369 read_tme = dict_get_value (stats_dict,
370 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
371 write_ops = dict_get_value (stats_dict,
372 kIOBlockStorageDriverStatisticsWritesKey);
373 write_byt = dict_get_value (stats_dict,
374 kIOBlockStorageDriverStatisticsBytesWrittenKey);
375 /* This property describes the number of nanoseconds spent
376 * performing writes since the block storage driver was
377 * instantiated. It is one of the statistic entries listed
378 * under the top-level kIOBlockStorageDriverStatisticsKey
379 * property table. It has an OSNumber value. */
380 write_tme = dict_get_value (stats_dict,
381 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
383 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
385 DBG ("snprintf (major, minor) failed.");
386 CFRelease (child_dict);
387 IOObjectRelease (disk_child);
388 CFRelease (props_dict);
389 IOObjectRelease (disk);
392 DBG ("disk_name = %s", disk_name);
394 if ((read_byt != -1LL) || (write_byt != -1LL))
395 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
396 if ((read_ops != -1LL) || (write_ops != -1LL))
397 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
398 if ((read_tme != -1LL) || (write_tme != -1LL))
399 disk_submit (disk_name, "disk_time",
403 CFRelease (child_dict);
404 IOObjectRelease (disk_child);
405 CFRelease (props_dict);
406 IOObjectRelease (disk);
408 IOObjectRelease (disk_list);
409 /* #endif HAVE_IOKIT_IOKITLIB_H */
422 counter_t read_sectors = 0;
423 counter_t write_sectors = 0;
425 counter_t read_count = 0;
426 counter_t read_merged = 0;
427 counter_t read_bytes = 0;
428 counter_t read_time = 0;
429 counter_t write_count = 0;
430 counter_t write_merged = 0;
431 counter_t write_bytes = 0;
432 counter_t write_time = 0;
435 diskstats_t *ds, *pre_ds;
437 static complain_t complain_obj;
439 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
441 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
443 plugin_complain (LOG_ERR, &complain_obj,
444 "disk plugin: Failed to open /proc/"
445 "{diskstats,partitions}.");
449 /* Kernel is 2.4.* */
453 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
454 "Succeeded to open /proc/{diskstats,partitions}.");
456 while (fgets (buffer, sizeof (buffer), fh) != NULL)
460 numfields = strsplit (buffer, fields, 32);
462 if ((numfields != (14 + fieldshift)) && (numfields != 7))
465 major = atoll (fields[0]);
466 minor = atoll (fields[1]);
468 disk_name = fields[2];
470 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
471 if (strcmp (disk_name, ds->name) == 0)
476 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
479 if ((ds->name = strdup (disk_name)) == NULL)
494 /* Kernel 2.6, Partition */
495 read_count = atoll (fields[3]);
496 read_sectors = atoll (fields[4]);
497 write_count = atoll (fields[5]);
498 write_sectors = atoll (fields[6]);
500 else if (numfields == (14 + fieldshift))
502 read_count = atoll (fields[3 + fieldshift]);
503 write_count = atoll (fields[7 + fieldshift]);
505 read_sectors = atoll (fields[5 + fieldshift]);
506 write_sectors = atoll (fields[9 + fieldshift]);
508 if ((fieldshift == 0) || (minor == 0))
511 read_merged = atoll (fields[4 + fieldshift]);
512 read_time = atoll (fields[6 + fieldshift]);
513 write_merged = atoll (fields[8 + fieldshift]);
514 write_time = atoll (fields[10+ fieldshift]);
519 DBG ("numfields = %i; => unknown file format.", numfields);
523 /* If the counter wraps around, it's only 32 bits.. */
524 if (read_sectors < ds->read_sectors)
525 ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
527 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
529 if (write_sectors < ds->write_sectors)
530 ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
532 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
534 ds->read_sectors = read_sectors;
535 ds->write_sectors = write_sectors;
536 read_bytes = ds->read_bytes;
537 write_bytes = ds->write_bytes;
539 /* Don't write to the RRDs if we've just started.. */
541 if (ds->poll_count <= min_poll_count)
543 DBG ("(ds->poll_count = %i) <= (min_poll_count = %i); => Not writing.",
544 ds->poll_count, min_poll_count);
548 if ((read_count == 0) && (write_count == 0))
550 DBG ("((read_count == 0) && (write_count == 0)); => Not writing.");
554 if ((read_bytes != -1LL) || (write_bytes != -1LL))
555 disk_submit (disk_name, "disk_octets", read_bytes, write_bytes);
556 if ((read_count != -1LL) || (write_count != -1LL))
557 disk_submit (disk_name, "disk_ops", read_count, write_count);
560 if ((read_merged != -1LL) || (write_merged != -1LL))
561 disk_submit (disk_name, "disk_merged",
562 read_merged, write_merged);
563 if ((read_time != -1LL) || (write_time != -1LL))
564 disk_submit (disk_name, "disk_time",
568 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
571 /* #endif defined(KERNEL_LINUX) */
574 static kstat_io_t kio;
580 for (i = 0; i < numdisk; i++)
582 if (kstat_read (kc, ksp[i], &kio) == -1)
585 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
587 disk_submit (ksp[i]->ks_name, "disk_octets", kio.reads, kio.writes);
588 disk_submit (ksp[i]->ks_name, "disk_ops", kio.nreads, kio.nwrites);
589 /* FIXME: Convert this to microseconds if necessary */
590 disk_submit (ksp[i]->ks_name, "disk_time", kio.rtime, kio.wtime);
592 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
594 disk_submit (ksp[i]->ks_name, "disk_octets", kio.reads, kio.writes);
595 disk_submit (ksp[i]->ks_name, "disk_ops", kio.nreads, kio.nwrites);
598 #endif /* defined(HAVE_LIBKSTAT) */
601 } /* int disk_read */
602 #endif /* DISK_HAVE_READ */
604 void module_register (void)
606 plugin_register_data_set (&octets_ds);
607 plugin_register_data_set (&operations_ds);
608 plugin_register_data_set (&merged_ds);
609 plugin_register_data_set (&time_ds);
612 plugin_register_init ("disk", disk_init);
613 plugin_register_read ("disk", disk_read);
614 #endif /* DISK_HAVE_READ */