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 /* #endif KERNEL_LINUX */
132 #define MAX_NUMDISK 256
133 extern kstat_ctl_t *kc;
134 static kstat_t *ksp[MAX_NUMDISK];
135 static int numdisk = 0;
136 #endif /* HAVE_LIBKSTAT */
138 static int disk_init (void)
140 #if HAVE_IOKIT_IOKITLIB_H
141 kern_return_t status;
143 if (io_master_port != MACH_PORT_NULL)
145 mach_port_deallocate (mach_task_self (),
147 io_master_port = MACH_PORT_NULL;
150 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
151 if (status != kIOReturnSuccess)
153 ERROR ("IOMasterPort failed: %s",
154 mach_error_string (status));
155 io_master_port = MACH_PORT_NULL;
158 /* #endif HAVE_IOKIT_IOKITLIB_H */
162 /* #endif KERNEL_LINUX */
172 for (numdisk = 0, ksp_chain = kc->kc_chain;
173 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
174 ksp_chain = ksp_chain->ks_next)
176 if (strncmp (ksp_chain->ks_class, "disk", 4)
177 && strncmp (ksp_chain->ks_class, "partition", 9))
179 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
181 ksp[numdisk++] = ksp_chain;
183 #endif /* HAVE_LIBKSTAT */
186 } /* int disk_init */
188 static void disk_submit (const char *plugin_instance,
190 counter_t read, counter_t write)
193 value_list_t vl = VALUE_LIST_INIT;
195 values[0].counter = read;
196 values[1].counter = write;
200 vl.time = time (NULL);
201 strcpy (vl.host, hostname_g);
202 strcpy (vl.plugin, "disk");
203 strncpy (vl.plugin_instance, plugin_instance,
204 sizeof (vl.plugin_instance));
206 plugin_dispatch_values (type, &vl);
207 } /* void disk_submit */
209 #if HAVE_IOKIT_IOKITLIB_H
210 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
212 signed long long val_int;
216 /* `key_obj' needs to be released. */
217 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
218 kCFStringEncodingASCII);
221 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
225 /* get => we don't need to release (== free) the object */
226 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
232 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
236 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
238 DEBUG ("CFNumberGetValue (%s) failed.", key);
244 #endif /* HAVE_IOKIT_IOKITLIB_H */
246 static int disk_read (void)
248 #if HAVE_IOKIT_IOKITLIB_H
249 io_registry_entry_t disk;
250 io_registry_entry_t disk_child;
251 io_iterator_t disk_list;
252 CFDictionaryRef props_dict;
253 CFDictionaryRef stats_dict;
254 CFDictionaryRef child_dict;
255 kern_return_t status;
257 signed long long read_ops;
258 signed long long read_byt;
259 signed long long read_tme;
260 signed long long write_ops;
261 signed long long write_byt;
262 signed long long write_tme;
268 static complain_t complain_obj;
270 /* Get the list of all disk objects. */
271 if (IOServiceGetMatchingServices (io_master_port,
272 IOServiceMatching (kIOBlockStorageDriverClass),
273 &disk_list) != kIOReturnSuccess)
275 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
276 "IOServiceGetMatchingServices failed.");
279 else if (complain_obj.interval != 0)
281 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
282 "IOServiceGetMatchingServices succeeded.");
285 while ((disk = IOIteratorNext (disk_list)) != 0)
291 /* `disk_child' must be released */
292 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
295 /* This fails for example for DVD/CD drives.. */
296 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
297 IOObjectRelease (disk);
301 /* We create `props_dict' => we need to release it later */
302 if (IORegistryEntryCreateCFProperties (disk,
303 (CFMutableDictionaryRef *) &props_dict,
308 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
309 IOObjectRelease (disk_child);
310 IOObjectRelease (disk);
314 if (props_dict == NULL)
316 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
317 IOObjectRelease (disk_child);
318 IOObjectRelease (disk);
322 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
323 CFSTR (kIOBlockStorageDriverStatisticsKey));
325 if (stats_dict == NULL)
327 DEBUG ("CFDictionaryGetValue (%s) failed.",
328 kIOBlockStorageDriverStatisticsKey);
329 CFRelease (props_dict);
330 IOObjectRelease (disk_child);
331 IOObjectRelease (disk);
335 if (IORegistryEntryCreateCFProperties (disk_child,
336 (CFMutableDictionaryRef *) &child_dict,
341 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
342 IOObjectRelease (disk_child);
343 CFRelease (props_dict);
344 IOObjectRelease (disk);
349 disk_major = (int) dict_get_value (child_dict,
351 disk_minor = (int) dict_get_value (child_dict,
353 read_ops = dict_get_value (stats_dict,
354 kIOBlockStorageDriverStatisticsReadsKey);
355 read_byt = dict_get_value (stats_dict,
356 kIOBlockStorageDriverStatisticsBytesReadKey);
357 read_tme = dict_get_value (stats_dict,
358 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
359 write_ops = dict_get_value (stats_dict,
360 kIOBlockStorageDriverStatisticsWritesKey);
361 write_byt = dict_get_value (stats_dict,
362 kIOBlockStorageDriverStatisticsBytesWrittenKey);
363 /* This property describes the number of nanoseconds spent
364 * performing writes since the block storage driver was
365 * instantiated. It is one of the statistic entries listed
366 * under the top-level kIOBlockStorageDriverStatisticsKey
367 * property table. It has an OSNumber value. */
368 write_tme = dict_get_value (stats_dict,
369 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
371 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
373 DEBUG ("snprintf (major, minor) failed.");
374 CFRelease (child_dict);
375 IOObjectRelease (disk_child);
376 CFRelease (props_dict);
377 IOObjectRelease (disk);
380 DEBUG ("disk_name = %s", disk_name);
382 if ((read_byt != -1LL) || (write_byt != -1LL))
383 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
384 if ((read_ops != -1LL) || (write_ops != -1LL))
385 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
386 if ((read_tme != -1LL) || (write_tme != -1LL))
387 disk_submit (disk_name, "disk_time",
391 CFRelease (child_dict);
392 IOObjectRelease (disk_child);
393 CFRelease (props_dict);
394 IOObjectRelease (disk);
396 IOObjectRelease (disk_list);
397 /* #endif HAVE_IOKIT_IOKITLIB_H */
410 counter_t read_sectors = 0;
411 counter_t write_sectors = 0;
413 counter_t read_count = 0;
414 counter_t read_merged = 0;
415 counter_t read_bytes = 0;
416 counter_t read_time = 0;
417 counter_t write_count = 0;
418 counter_t write_merged = 0;
419 counter_t write_bytes = 0;
420 counter_t write_time = 0;
423 diskstats_t *ds, *pre_ds;
425 static complain_t complain_obj;
427 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
429 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
431 plugin_complain (LOG_ERR, &complain_obj,
432 "disk plugin: Failed to open /proc/"
433 "{diskstats,partitions}.");
437 /* Kernel is 2.4.* */
441 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
442 "Succeeded to open /proc/{diskstats,partitions}.");
444 while (fgets (buffer, sizeof (buffer), fh) != NULL)
448 numfields = strsplit (buffer, fields, 32);
450 if ((numfields != (14 + fieldshift)) && (numfields != 7))
453 major = atoll (fields[0]);
454 minor = atoll (fields[1]);
456 disk_name = fields[2];
458 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
459 if (strcmp (disk_name, ds->name) == 0)
464 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
467 if ((ds->name = strdup (disk_name)) == NULL)
482 /* Kernel 2.6, Partition */
483 read_count = atoll (fields[3]);
484 read_sectors = atoll (fields[4]);
485 write_count = atoll (fields[5]);
486 write_sectors = atoll (fields[6]);
488 else if (numfields == (14 + fieldshift))
490 read_count = atoll (fields[3 + fieldshift]);
491 write_count = atoll (fields[7 + fieldshift]);
493 read_sectors = atoll (fields[5 + fieldshift]);
494 write_sectors = atoll (fields[9 + fieldshift]);
496 if ((fieldshift == 0) || (minor == 0))
499 read_merged = atoll (fields[4 + fieldshift]);
500 read_time = atoll (fields[6 + fieldshift]);
501 write_merged = atoll (fields[8 + fieldshift]);
502 write_time = atoll (fields[10+ fieldshift]);
507 DEBUG ("numfields = %i; => unknown file format.", numfields);
511 /* If the counter wraps around, it's only 32 bits.. */
512 if (read_sectors < ds->read_sectors)
513 ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
515 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
517 if (write_sectors < ds->write_sectors)
518 ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
520 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
522 ds->read_sectors = read_sectors;
523 ds->write_sectors = write_sectors;
524 read_bytes = ds->read_bytes;
525 write_bytes = ds->write_bytes;
527 /* Don't write to the RRDs if we've just started.. */
529 if (ds->poll_count <= 2)
531 DEBUG ("(ds->poll_count = %i) <= (min_poll_count = 2); => Not writing.",
536 if ((read_count == 0) && (write_count == 0))
538 DEBUG ("((read_count == 0) && (write_count == 0)); => Not writing.");
542 if ((read_bytes != -1LL) || (write_bytes != -1LL))
543 disk_submit (disk_name, "disk_octets", read_bytes, write_bytes);
544 if ((read_count != -1LL) || (write_count != -1LL))
545 disk_submit (disk_name, "disk_ops", read_count, write_count);
548 if ((read_merged != -1LL) || (write_merged != -1LL))
549 disk_submit (disk_name, "disk_merged",
550 read_merged, write_merged);
551 if ((read_time != -1LL) || (write_time != -1LL))
552 disk_submit (disk_name, "disk_time",
556 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
559 /* #endif defined(KERNEL_LINUX) */
562 static kstat_io_t kio;
568 for (i = 0; i < numdisk; i++)
570 if (kstat_read (kc, ksp[i], &kio) == -1)
573 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
575 disk_submit (ksp[i]->ks_name, "disk_octets", kio.reads, kio.writes);
576 disk_submit (ksp[i]->ks_name, "disk_ops", kio.nreads, kio.nwrites);
577 /* FIXME: Convert this to microseconds if necessary */
578 disk_submit (ksp[i]->ks_name, "disk_time", kio.rtime, kio.wtime);
580 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
582 disk_submit (ksp[i]->ks_name, "disk_octets", kio.reads, kio.writes);
583 disk_submit (ksp[i]->ks_name, "disk_ops", kio.nreads, kio.nwrites);
586 #endif /* defined(HAVE_LIBKSTAT) */
589 } /* int disk_read */
590 #endif /* DISK_HAVE_READ */
592 void module_register (void)
594 plugin_register_data_set (&octets_ds);
595 plugin_register_data_set (&operations_ds);
596 plugin_register_data_set (&merged_ds);
597 plugin_register_data_set (&time_ds);
600 plugin_register_init ("disk", disk_init);
601 plugin_register_read ("disk", disk_read);
602 #endif /* DISK_HAVE_READ */