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>
54 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
55 # define DISK_HAVE_READ 1
57 # define DISK_HAVE_READ 0
60 /* 2^34 = 17179869184 = ~17.2GByte/s */
61 static data_source_t octets_dsrc[2] =
63 {"read", DS_TYPE_COUNTER, 0, 17179869183.0},
64 {"write", DS_TYPE_COUNTER, 0, 17179869183.0}
67 static data_set_t octets_ds =
69 "disk_octets", 2, octets_dsrc
72 static data_source_t operations_dsrc[2] =
74 {"read", DS_TYPE_COUNTER, 0, 4294967295.0},
75 {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
78 static data_set_t operations_ds =
80 "disk_ops", 2, operations_dsrc
83 static data_source_t merged_dsrc[2] =
85 {"read", DS_TYPE_COUNTER, 0, 4294967295.0},
86 {"write", DS_TYPE_COUNTER, 0, 4294967295.0}
89 static data_set_t merged_ds =
91 "disk_merged", 2, merged_dsrc
94 /* max is 1000000us per second. */
95 static data_source_t time_dsrc[2] =
97 {"read", DS_TYPE_COUNTER, 0, 1000000.0},
98 {"write", DS_TYPE_COUNTER, 0, 1000000.0}
101 static data_set_t time_ds =
103 "disk_time", 2, time_dsrc
107 #if HAVE_IOKIT_IOKITLIB_H
108 static mach_port_t io_master_port = MACH_PORT_NULL;
109 /* #endif HAVE_IOKIT_IOKITLIB_H */
112 typedef struct diskstats
116 /* This overflows in roughly 1361 year */
117 unsigned int poll_count;
119 counter_t read_sectors;
120 counter_t write_sectors;
122 counter_t read_bytes;
123 counter_t write_bytes;
125 struct diskstats *next;
128 static diskstats_t *disklist;
129 static int min_poll_count;
130 /* #endif KERNEL_LINUX */
133 #define MAX_NUMDISK 256
134 extern kstat_ctl_t *kc;
135 static kstat_t *ksp[MAX_NUMDISK];
136 static int numdisk = 0;
137 #endif /* HAVE_LIBKSTAT */
139 static int disk_init (void)
141 #if HAVE_IOKIT_IOKITLIB_H
142 kern_return_t status;
144 if (io_master_port != MACH_PORT_NULL)
146 mach_port_deallocate (mach_task_self (),
148 io_master_port = MACH_PORT_NULL;
151 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
152 if (status != kIOReturnSuccess)
154 ERROR ("IOMasterPort failed: %s",
155 mach_error_string (status));
156 io_master_port = MACH_PORT_NULL;
159 /* #endif HAVE_IOKIT_IOKITLIB_H */
165 step = atoi (COLLECTD_STEP);
166 heartbeat = atoi (COLLECTD_HEARTBEAT);
169 assert (heartbeat >= step);
171 min_poll_count = 1 + (heartbeat / step);
172 DEBUG ("min_poll_count = %i;", min_poll_count);
173 /* #endif KERNEL_LINUX */
183 for (numdisk = 0, ksp_chain = kc->kc_chain;
184 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
185 ksp_chain = ksp_chain->ks_next)
187 if (strncmp (ksp_chain->ks_class, "disk", 4)
188 && strncmp (ksp_chain->ks_class, "partition", 9))
190 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
192 ksp[numdisk++] = ksp_chain;
194 #endif /* HAVE_LIBKSTAT */
197 } /* int disk_init */
199 static void disk_submit (const char *plugin_instance,
201 counter_t read, counter_t write)
204 value_list_t vl = VALUE_LIST_INIT;
206 values[0].counter = read;
207 values[1].counter = write;
211 vl.time = time (NULL);
212 strcpy (vl.host, hostname_g);
213 strcpy (vl.plugin, "disk");
214 strncpy (vl.plugin_instance, plugin_instance,
215 sizeof (vl.plugin_instance));
217 plugin_dispatch_values (type, &vl);
218 } /* void disk_submit */
220 #if HAVE_IOKIT_IOKITLIB_H
221 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
223 signed long long val_int;
227 /* `key_obj' needs to be released. */
228 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
229 kCFStringEncodingASCII);
232 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
236 /* get => we don't need to release (== free) the object */
237 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
243 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
247 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
249 DEBUG ("CFNumberGetValue (%s) failed.", key);
255 #endif /* HAVE_IOKIT_IOKITLIB_H */
257 static int disk_read (void)
259 #if HAVE_IOKIT_IOKITLIB_H
260 io_registry_entry_t disk;
261 io_registry_entry_t disk_child;
262 io_iterator_t disk_list;
263 CFDictionaryRef props_dict;
264 CFDictionaryRef stats_dict;
265 CFDictionaryRef child_dict;
266 kern_return_t status;
268 signed long long read_ops;
269 signed long long read_byt;
270 signed long long read_tme;
271 signed long long write_ops;
272 signed long long write_byt;
273 signed long long write_tme;
279 static complain_t complain_obj;
281 /* Get the list of all disk objects. */
282 if (IOServiceGetMatchingServices (io_master_port,
283 IOServiceMatching (kIOBlockStorageDriverClass),
284 &disk_list) != kIOReturnSuccess)
286 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
287 "IOServiceGetMatchingServices failed.");
290 else if (complain_obj.interval != 0)
292 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
293 "IOServiceGetMatchingServices succeeded.");
296 while ((disk = IOIteratorNext (disk_list)) != 0)
302 /* `disk_child' must be released */
303 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
306 /* This fails for example for DVD/CD drives.. */
307 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
308 IOObjectRelease (disk);
312 /* We create `props_dict' => we need to release it later */
313 if (IORegistryEntryCreateCFProperties (disk,
314 (CFMutableDictionaryRef *) &props_dict,
319 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
320 IOObjectRelease (disk_child);
321 IOObjectRelease (disk);
325 if (props_dict == NULL)
327 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
328 IOObjectRelease (disk_child);
329 IOObjectRelease (disk);
333 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
334 CFSTR (kIOBlockStorageDriverStatisticsKey));
336 if (stats_dict == NULL)
338 DEBUG ("CFDictionaryGetValue (%s) failed.",
339 kIOBlockStorageDriverStatisticsKey);
340 CFRelease (props_dict);
341 IOObjectRelease (disk_child);
342 IOObjectRelease (disk);
346 if (IORegistryEntryCreateCFProperties (disk_child,
347 (CFMutableDictionaryRef *) &child_dict,
352 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
353 IOObjectRelease (disk_child);
354 CFRelease (props_dict);
355 IOObjectRelease (disk);
360 disk_major = (int) dict_get_value (child_dict,
362 disk_minor = (int) dict_get_value (child_dict,
364 read_ops = dict_get_value (stats_dict,
365 kIOBlockStorageDriverStatisticsReadsKey);
366 read_byt = dict_get_value (stats_dict,
367 kIOBlockStorageDriverStatisticsBytesReadKey);
368 read_tme = dict_get_value (stats_dict,
369 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
370 write_ops = dict_get_value (stats_dict,
371 kIOBlockStorageDriverStatisticsWritesKey);
372 write_byt = dict_get_value (stats_dict,
373 kIOBlockStorageDriverStatisticsBytesWrittenKey);
374 /* This property describes the number of nanoseconds spent
375 * performing writes since the block storage driver was
376 * instantiated. It is one of the statistic entries listed
377 * under the top-level kIOBlockStorageDriverStatisticsKey
378 * property table. It has an OSNumber value. */
379 write_tme = dict_get_value (stats_dict,
380 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
382 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
384 DEBUG ("snprintf (major, minor) failed.");
385 CFRelease (child_dict);
386 IOObjectRelease (disk_child);
387 CFRelease (props_dict);
388 IOObjectRelease (disk);
391 DEBUG ("disk_name = %s", disk_name);
393 if ((read_byt != -1LL) || (write_byt != -1LL))
394 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
395 if ((read_ops != -1LL) || (write_ops != -1LL))
396 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
397 if ((read_tme != -1LL) || (write_tme != -1LL))
398 disk_submit (disk_name, "disk_time",
402 CFRelease (child_dict);
403 IOObjectRelease (disk_child);
404 CFRelease (props_dict);
405 IOObjectRelease (disk);
407 IOObjectRelease (disk_list);
408 /* #endif HAVE_IOKIT_IOKITLIB_H */
421 counter_t read_sectors = 0;
422 counter_t write_sectors = 0;
424 counter_t read_count = 0;
425 counter_t read_merged = 0;
426 counter_t read_bytes = 0;
427 counter_t read_time = 0;
428 counter_t write_count = 0;
429 counter_t write_merged = 0;
430 counter_t write_bytes = 0;
431 counter_t write_time = 0;
434 diskstats_t *ds, *pre_ds;
436 static complain_t complain_obj;
438 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
440 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
442 plugin_complain (LOG_ERR, &complain_obj,
443 "disk plugin: Failed to open /proc/"
444 "{diskstats,partitions}.");
448 /* Kernel is 2.4.* */
452 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
453 "Succeeded to open /proc/{diskstats,partitions}.");
455 while (fgets (buffer, sizeof (buffer), fh) != NULL)
459 numfields = strsplit (buffer, fields, 32);
461 if ((numfields != (14 + fieldshift)) && (numfields != 7))
464 major = atoll (fields[0]);
465 minor = atoll (fields[1]);
467 disk_name = fields[2];
469 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
470 if (strcmp (disk_name, ds->name) == 0)
475 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
478 if ((ds->name = strdup (disk_name)) == NULL)
493 /* Kernel 2.6, Partition */
494 read_count = atoll (fields[3]);
495 read_sectors = atoll (fields[4]);
496 write_count = atoll (fields[5]);
497 write_sectors = atoll (fields[6]);
499 else if (numfields == (14 + fieldshift))
501 read_count = atoll (fields[3 + fieldshift]);
502 write_count = atoll (fields[7 + fieldshift]);
504 read_sectors = atoll (fields[5 + fieldshift]);
505 write_sectors = atoll (fields[9 + fieldshift]);
507 if ((fieldshift == 0) || (minor == 0))
510 read_merged = atoll (fields[4 + fieldshift]);
511 read_time = atoll (fields[6 + fieldshift]);
512 write_merged = atoll (fields[8 + fieldshift]);
513 write_time = atoll (fields[10+ fieldshift]);
518 DEBUG ("numfields = %i; => unknown file format.", numfields);
522 /* If the counter wraps around, it's only 32 bits.. */
523 if (read_sectors < ds->read_sectors)
524 ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
526 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
528 if (write_sectors < ds->write_sectors)
529 ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
531 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
533 ds->read_sectors = read_sectors;
534 ds->write_sectors = write_sectors;
535 read_bytes = ds->read_bytes;
536 write_bytes = ds->write_bytes;
538 /* Don't write to the RRDs if we've just started.. */
540 if (ds->poll_count <= min_poll_count)
542 DEBUG ("(ds->poll_count = %i) <= (min_poll_count = %i); => Not writing.",
543 ds->poll_count, min_poll_count);
547 if ((read_count == 0) && (write_count == 0))
549 DEBUG ("((read_count == 0) && (write_count == 0)); => Not writing.");
553 if ((read_bytes != -1LL) || (write_bytes != -1LL))
554 disk_submit (disk_name, "disk_octets", read_bytes, write_bytes);
555 if ((read_count != -1LL) || (write_count != -1LL))
556 disk_submit (disk_name, "disk_ops", read_count, write_count);
559 if ((read_merged != -1LL) || (write_merged != -1LL))
560 disk_submit (disk_name, "disk_merged",
561 read_merged, write_merged);
562 if ((read_time != -1LL) || (write_time != -1LL))
563 disk_submit (disk_name, "disk_time",
567 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
570 /* #endif defined(KERNEL_LINUX) */
573 static kstat_io_t kio;
579 for (i = 0; i < numdisk; i++)
581 if (kstat_read (kc, ksp[i], &kio) == -1)
584 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
586 disk_submit (ksp[i]->ks_name, "disk_octets", kio.reads, kio.writes);
587 disk_submit (ksp[i]->ks_name, "disk_ops", kio.nreads, kio.nwrites);
588 /* FIXME: Convert this to microseconds if necessary */
589 disk_submit (ksp[i]->ks_name, "disk_time", kio.rtime, kio.wtime);
591 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
593 disk_submit (ksp[i]->ks_name, "disk_octets", kio.reads, kio.writes);
594 disk_submit (ksp[i]->ks_name, "disk_ops", kio.nreads, kio.nwrites);
597 #endif /* defined(HAVE_LIBKSTAT) */
600 } /* int disk_read */
601 #endif /* DISK_HAVE_READ */
603 void module_register (void)
605 plugin_register_data_set (&octets_ds);
606 plugin_register_data_set (&operations_ds);
607 plugin_register_data_set (&merged_ds);
608 plugin_register_data_set (&time_ds);
611 plugin_register_init ("disk", disk_init);
612 plugin_register_read ("disk", disk_read);
613 #endif /* DISK_HAVE_READ */