2 * collectd - src/disk.c
3 * Copyright (C) 2005,2006 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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
26 #include "utils_debug.h"
28 #define MODULE_NAME "disk"
30 #if HAVE_MACH_MACH_TYPES_H
31 # include <mach/mach_types.h>
33 #if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
39 #if HAVE_MACH_MACH_PORT_H
40 # include <mach/mach_port.h>
42 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
43 # include <CoreFoundation/CoreFoundation.h>
45 #if HAVE_IOKIT_IOKITLIB_H
46 # include <IOKit/IOKitLib.h>
48 #if HAVE_IOKIT_IOTYPES_H
49 # include <IOKit/IOTypes.h>
51 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
52 # include <IOKit/storage/IOBlockStorageDriver.h>
54 #if HAVE_IOKIT_IOBSD_H
55 # include <IOKit/IOBSD.h>
58 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
59 # define DISK_HAVE_READ 1
61 # define DISK_HAVE_READ 0
64 static char *disk_filename_template = "disk-%s.rrd";
65 static char *part_filename_template = "partition-%s.rrd";
67 /* 104857600 == 100 MB */
68 static char *disk_ds_def[] =
70 "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
71 "DS:rmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
72 "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
73 "DS:rtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
74 "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
75 "DS:wmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
76 "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
77 "DS:wtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
80 static int disk_ds_num = 8;
82 static char *part_ds_def[] =
84 "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
85 "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
86 "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
87 "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
90 static int part_ds_num = 4;
92 #if HAVE_IOKIT_IOKITLIB_H
93 static mach_port_t io_master_port = MACH_PORT_NULL;
94 /* #endif HAVE_IOKIT_IOKITLIB_H */
97 typedef struct diskstats
101 /* This overflows in roughly 1361 year */
102 unsigned int poll_count;
104 unsigned long long read_sectors;
105 unsigned long long write_sectors;
107 unsigned long long read_bytes;
108 unsigned long long write_bytes;
110 struct diskstats *next;
113 static diskstats_t *disklist;
114 static int min_poll_count;
115 /* #endif KERNEL_LINUX */
118 #define MAX_NUMDISK 256
119 extern kstat_ctl_t *kc;
120 static kstat_t *ksp[MAX_NUMDISK];
121 static int numdisk = 0;
122 #endif /* HAVE_LIBKSTAT */
124 static void disk_init (void)
126 #if HAVE_IOKIT_IOKITLIB_H
127 kern_return_t status;
129 if (io_master_port != MACH_PORT_NULL)
131 mach_port_deallocate (mach_task_self (),
133 io_master_port = MACH_PORT_NULL;
136 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
137 if (status != kIOReturnSuccess)
139 syslog (LOG_ERR, "IOMasterPort failed: %s",
140 mach_error_string (status));
141 io_master_port = MACH_PORT_NULL;
144 /* #endif HAVE_IOKIT_IOKITLIB_H */
150 step = atoi (COLLECTD_STEP);
151 heartbeat = atoi (COLLECTD_HEARTBEAT);
154 assert (heartbeat >= step);
156 min_poll_count = 1 + (heartbeat / step);
157 DBG ("min_poll_count = %i;", min_poll_count);
158 /* #endif KERNEL_LINUX */
168 for (numdisk = 0, ksp_chain = kc->kc_chain;
169 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
170 ksp_chain = ksp_chain->ks_next)
172 if (strncmp (ksp_chain->ks_class, "disk", 4)
173 && strncmp (ksp_chain->ks_class, "partition", 9))
175 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
177 ksp[numdisk++] = ksp_chain;
179 #endif /* HAVE_LIBKSTAT */
184 static void disk_write (char *host, char *inst, char *val)
189 status = snprintf (file, 512, disk_filename_template, inst);
192 else if (status >= 512)
195 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
198 static void partition_write (char *host, char *inst, char *val)
203 status = snprintf (file, 512, part_filename_template, inst);
206 else if (status >= 512)
209 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
214 static void disk_submit (char *disk_name,
215 unsigned long long read_count,
216 unsigned long long read_merged,
217 unsigned long long read_bytes,
218 unsigned long long read_time,
219 unsigned long long write_count,
220 unsigned long long write_merged,
221 unsigned long long write_bytes,
222 unsigned long long write_time)
226 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
227 (unsigned int) curtime,
228 read_count, read_merged, read_bytes, read_time,
229 write_count, write_merged, write_bytes,
230 write_time) >= BUFSIZE)
233 DBG ("disk_name = %s; buf = %s;",
236 plugin_submit (MODULE_NAME, disk_name, buf);
239 #if KERNEL_LINUX || HAVE_LIBKSTAT
240 static void partition_submit (char *part_name,
241 unsigned long long read_count,
242 unsigned long long read_bytes,
243 unsigned long long write_count,
244 unsigned long long write_bytes)
248 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
249 (unsigned int) curtime,
250 read_count, read_bytes, write_count,
251 write_bytes) >= BUFSIZE)
254 plugin_submit ("partition", part_name, buf);
256 #endif /* KERNEL_LINUX || HAVE_LIBKSTAT */
259 #if HAVE_IOKIT_IOKITLIB_H
260 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
262 signed long long val_int;
266 /* `key_obj' needs to be released. */
267 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
268 kCFStringEncodingASCII);
271 DBG ("CFStringCreateWithCString (%s) failed.", key);
275 /* get => we don't need to release (== free) the object */
276 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
282 DBG ("CFDictionaryGetValue (%s) failed.", key);
286 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
288 DBG ("CFNumberGetValue (%s) failed.", key);
294 #endif /* HAVE_IOKIT_IOKITLIB_H */
296 static void disk_read (void)
298 #if HAVE_IOKIT_IOKITLIB_H
299 io_registry_entry_t disk;
300 io_registry_entry_t disk_child;
301 io_iterator_t disk_list;
302 CFDictionaryRef props_dict;
303 CFDictionaryRef stats_dict;
304 CFDictionaryRef child_dict;
305 kern_return_t status;
307 signed long long read_ops;
308 signed long long read_byt;
309 signed long long read_tme;
310 signed long long write_ops;
311 signed long long write_byt;
312 signed long long write_tme;
318 static complain_t complain_obj;
320 /* Get the list of all disk objects. */
321 if (IOServiceGetMatchingServices (io_master_port,
322 IOServiceMatching (kIOBlockStorageDriverClass),
323 &disk_list) != kIOReturnSuccess)
325 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
326 "IOServiceGetMatchingServices failed.");
329 else if (complain_obj.interval != 0)
331 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
332 "IOServiceGetMatchingServices succeeded.");
335 while ((disk = IOIteratorNext (disk_list)) != 0)
341 /* `disk_child' must be released */
342 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
345 /* This fails for example for DVD/CD drives.. */
346 DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
347 IOObjectRelease (disk);
351 /* We create `props_dict' => we need to release it later */
352 if (IORegistryEntryCreateCFProperties (disk,
353 (CFMutableDictionaryRef *) &props_dict,
358 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
359 IOObjectRelease (disk_child);
360 IOObjectRelease (disk);
364 if (props_dict == NULL)
366 DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
367 IOObjectRelease (disk_child);
368 IOObjectRelease (disk);
372 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
373 CFSTR (kIOBlockStorageDriverStatisticsKey));
375 if (stats_dict == NULL)
377 DBG ("CFDictionaryGetValue (%s) failed.",
378 kIOBlockStorageDriverStatisticsKey);
379 CFRelease (props_dict);
380 IOObjectRelease (disk_child);
381 IOObjectRelease (disk);
385 if (IORegistryEntryCreateCFProperties (disk_child,
386 (CFMutableDictionaryRef *) &child_dict,
391 DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
392 IOObjectRelease (disk_child);
393 CFRelease (props_dict);
394 IOObjectRelease (disk);
398 disk_major = (int) dict_get_value (child_dict,
400 disk_minor = (int) dict_get_value (child_dict,
402 read_ops = dict_get_value (stats_dict,
403 kIOBlockStorageDriverStatisticsReadsKey);
404 read_byt = dict_get_value (stats_dict,
405 kIOBlockStorageDriverStatisticsBytesReadKey);
406 read_tme = dict_get_value (stats_dict,
407 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
408 write_ops = dict_get_value (stats_dict,
409 kIOBlockStorageDriverStatisticsWritesKey);
410 write_byt = dict_get_value (stats_dict,
411 kIOBlockStorageDriverStatisticsBytesWrittenKey);
412 write_tme = dict_get_value (stats_dict,
413 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
415 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
417 DBG ("snprintf (major, minor) failed.");
418 CFRelease (child_dict);
419 IOObjectRelease (disk_child);
420 CFRelease (props_dict);
421 IOObjectRelease (disk);
424 DBG ("disk_name = %s", disk_name);
426 if ((read_ops != -1LL)
427 || (read_byt != -1LL)
428 || (read_tme != -1LL)
429 || (write_ops != -1LL)
430 || (write_byt != -1LL)
431 || (write_tme != -1LL))
432 disk_submit (disk_name,
433 read_ops, 0ULL, read_byt, read_tme,
434 write_ops, 0ULL, write_byt, write_tme);
436 CFRelease (child_dict);
437 IOObjectRelease (disk_child);
438 CFRelease (props_dict);
439 IOObjectRelease (disk);
441 IOObjectRelease (disk_list);
442 /* #endif HAVE_IOKIT_IOKITLIB_H */
456 unsigned long long read_sectors = 0ULL;
457 unsigned long long write_sectors = 0ULL;
459 unsigned long long read_count = 0ULL;
460 unsigned long long read_merged = 0ULL;
461 unsigned long long read_bytes = 0ULL;
462 unsigned long long read_time = 0ULL;
463 unsigned long long write_count = 0ULL;
464 unsigned long long write_merged = 0ULL;
465 unsigned long long write_bytes = 0ULL;
466 unsigned long long write_time = 0ULL;
469 diskstats_t *ds, *pre_ds;
471 static complain_t complain_obj;
473 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
475 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
477 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: Failed to open /proc/{diskstats,partitions}.");
481 /* Kernel is 2.4.* */
485 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: Succeeded to open /proc/{diskstats,partitions}.");
487 while (fgets (buffer, 1024, fh) != NULL)
489 numfields = strsplit (buffer, fields, 32);
491 if ((numfields != (14 + fieldshift)) && (numfields != 7))
494 major = atoll (fields[0]);
495 minor = atoll (fields[1]);
497 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
499 disk_name[127] = '\0';
501 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
502 if (strcmp (disk_name, ds->name) == 0)
507 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
510 if ((ds->name = strdup (disk_name)) == NULL)
525 /* Kernel 2.6, Partition */
526 read_count = atoll (fields[3]);
527 read_sectors = atoll (fields[4]);
528 write_count = atoll (fields[5]);
529 write_sectors = atoll (fields[6]);
531 else if (numfields == (14 + fieldshift))
533 read_count = atoll (fields[3 + fieldshift]);
534 write_count = atoll (fields[7 + fieldshift]);
536 read_sectors = atoll (fields[5 + fieldshift]);
537 write_sectors = atoll (fields[9 + fieldshift]);
539 if ((fieldshift == 0) || (minor == 0))
542 read_merged = atoll (fields[4 + fieldshift]);
543 read_time = atoll (fields[6 + fieldshift]);
544 write_merged = atoll (fields[8 + fieldshift]);
545 write_time = atoll (fields[10+ fieldshift]);
550 DBG ("numfields = %i; => unknown file format.", numfields);
554 /* If the counter wraps around, it's only 32 bits.. */
555 if (read_sectors < ds->read_sectors)
556 ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
558 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
560 if (write_sectors < ds->write_sectors)
561 ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
563 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
565 ds->read_sectors = read_sectors;
566 ds->write_sectors = write_sectors;
567 read_bytes = ds->read_bytes;
568 write_bytes = ds->write_bytes;
570 /* Don't write to the RRDs if we've just started.. */
572 if (ds->poll_count <= min_poll_count)
574 DBG ("(ds->poll_count = %i) <= (min_poll_count = %i); => Not writing.",
575 ds->poll_count, min_poll_count);
579 if ((read_count == 0) && (write_count == 0))
581 DBG ("((read_count == 0) && (write_count == 0)); => Not writing.");
586 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
587 write_count, write_merged, write_bytes, write_time);
589 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
593 /* #endif defined(KERNEL_LINUX) */
596 static kstat_io_t kio;
602 for (i = 0; i < numdisk; i++)
604 if (kstat_read (kc, ksp[i], &kio) == -1)
607 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
608 disk_submit (ksp[i]->ks_name,
609 kio.reads, 0LL, kio.nread, kio.rtime,
610 kio.writes, 0LL, kio.nwritten, kio.wtime);
611 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
612 partition_submit (ksp[i]->ks_name,
613 kio.reads, kio.nread,
614 kio.writes,kio.nwritten);
616 #endif /* defined(HAVE_LIBKSTAT) */
617 } /* static void disk_read (void) */
619 # define disk_read NULL
620 #endif /* DISK_HAVE_READ */
622 void module_register (void)
624 plugin_register ("partition", NULL, NULL, partition_write);
625 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);