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 /* Get the list of all disk objects. */
319 if (IOServiceGetMatchingServices (io_master_port,
320 IOServiceMatching (kIOBlockStorageDriverClass),
321 &disk_list) != kIOReturnSuccess)
323 syslog (LOG_ERR, "disk-plugin: IOServiceGetMatchingServices failed.");
327 while ((disk = IOIteratorNext (disk_list)) != 0)
333 /* `disk_child' must be released */
334 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
337 /* This fails for example for DVD/CD drives.. */
338 DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
339 IOObjectRelease (disk);
343 /* We create `props_dict' => we need to release it later */
344 if (IORegistryEntryCreateCFProperties (disk,
345 (CFMutableDictionaryRef *) &props_dict,
350 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
351 IOObjectRelease (disk_child);
352 IOObjectRelease (disk);
356 if (props_dict == NULL)
358 DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
359 IOObjectRelease (disk_child);
360 IOObjectRelease (disk);
364 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
365 CFSTR (kIOBlockStorageDriverStatisticsKey));
367 if (stats_dict == NULL)
369 DBG ("CFDictionaryGetValue (%s) failed.",
370 kIOBlockStorageDriverStatisticsKey);
371 CFRelease (props_dict);
372 IOObjectRelease (disk_child);
373 IOObjectRelease (disk);
377 if (IORegistryEntryCreateCFProperties (disk_child,
378 (CFMutableDictionaryRef *) &child_dict,
383 DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
384 IOObjectRelease (disk_child);
385 CFRelease (props_dict);
386 IOObjectRelease (disk);
390 disk_major = (int) dict_get_value (child_dict,
392 disk_minor = (int) dict_get_value (child_dict,
394 read_ops = dict_get_value (stats_dict,
395 kIOBlockStorageDriverStatisticsReadsKey);
396 read_byt = dict_get_value (stats_dict,
397 kIOBlockStorageDriverStatisticsBytesReadKey);
398 read_tme = dict_get_value (stats_dict,
399 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
400 write_ops = dict_get_value (stats_dict,
401 kIOBlockStorageDriverStatisticsWritesKey);
402 write_byt = dict_get_value (stats_dict,
403 kIOBlockStorageDriverStatisticsBytesWrittenKey);
404 write_tme = dict_get_value (stats_dict,
405 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
407 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
409 DBG ("snprintf (major, minor) failed.");
410 CFRelease (child_dict);
411 IOObjectRelease (disk_child);
412 CFRelease (props_dict);
413 IOObjectRelease (disk);
416 DBG ("disk_name = %s", disk_name);
418 if ((read_ops != -1LL)
419 || (read_byt != -1LL)
420 || (read_tme != -1LL)
421 || (write_ops != -1LL)
422 || (write_byt != -1LL)
423 || (write_tme != -1LL))
424 disk_submit (disk_name,
425 read_ops, 0ULL, read_byt, read_tme,
426 write_ops, 0ULL, write_byt, write_tme);
428 CFRelease (child_dict);
429 IOObjectRelease (disk_child);
430 CFRelease (props_dict);
431 IOObjectRelease (disk);
433 IOObjectRelease (disk_list);
434 /* #endif HAVE_IOKIT_IOKITLIB_H */
448 unsigned long long read_sectors = 0ULL;
449 unsigned long long write_sectors = 0ULL;
451 unsigned long long read_count = 0ULL;
452 unsigned long long read_merged = 0ULL;
453 unsigned long long read_bytes = 0ULL;
454 unsigned long long read_time = 0ULL;
455 unsigned long long write_count = 0ULL;
456 unsigned long long write_merged = 0ULL;
457 unsigned long long write_bytes = 0ULL;
458 unsigned long long write_time = 0ULL;
461 diskstats_t *ds, *pre_ds;
463 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
465 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
468 /* Kernel is 2.4.* */
472 while (fgets (buffer, 1024, fh) != NULL)
474 numfields = strsplit (buffer, fields, 32);
476 if ((numfields != (14 + fieldshift)) && (numfields != 7))
479 major = atoll (fields[0]);
480 minor = atoll (fields[1]);
482 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
484 disk_name[127] = '\0';
486 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
487 if (strcmp (disk_name, ds->name) == 0)
492 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
495 if ((ds->name = strdup (disk_name)) == NULL)
510 /* Kernel 2.6, Partition */
511 read_count = atoll (fields[3]);
512 read_sectors = atoll (fields[4]);
513 write_count = atoll (fields[5]);
514 write_sectors = atoll (fields[6]);
516 else if (numfields == (14 + fieldshift))
518 read_count = atoll (fields[3 + fieldshift]);
519 write_count = atoll (fields[7 + fieldshift]);
521 read_sectors = atoll (fields[5 + fieldshift]);
522 write_sectors = atoll (fields[9 + fieldshift]);
524 if ((fieldshift == 0) || (minor == 0))
527 read_merged = atoll (fields[4 + fieldshift]);
528 read_time = atoll (fields[6 + fieldshift]);
529 write_merged = atoll (fields[8 + fieldshift]);
530 write_time = atoll (fields[10+ fieldshift]);
535 DBG ("numfields = %i; => unknown file format.", numfields);
539 /* If the counter wraps around, it's only 32 bits.. */
540 if (read_sectors < ds->read_sectors)
541 ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
543 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
545 if (write_sectors < ds->write_sectors)
546 ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
548 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
550 ds->read_sectors = read_sectors;
551 ds->write_sectors = write_sectors;
552 read_bytes = ds->read_bytes;
553 write_bytes = ds->write_bytes;
555 /* Don't write to the RRDs if we've just started.. */
557 if (ds->poll_count <= min_poll_count)
559 DBG ("(ds->poll_count = %i) <= (min_poll_count = %i); => Not writing.",
560 ds->poll_count, min_poll_count);
564 if ((read_count == 0) && (write_count == 0))
566 DBG ("((read_count == 0) && (write_count == 0)); => Not writing.");
571 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
572 write_count, write_merged, write_bytes, write_time);
574 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
578 /* #endif defined(KERNEL_LINUX) */
581 static kstat_io_t kio;
587 for (i = 0; i < numdisk; i++)
589 if (kstat_read (kc, ksp[i], &kio) == -1)
592 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
593 disk_submit (ksp[i]->ks_name,
594 kio.reads, 0LL, kio.nread, kio.rtime,
595 kio.writes, 0LL, kio.nwritten, kio.wtime);
596 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
597 partition_submit (ksp[i]->ks_name,
598 kio.reads, kio.nread,
599 kio.writes,kio.nwritten);
601 #endif /* defined(HAVE_LIBKSTAT) */
602 } /* static void disk_read (void) */
604 # define disk_read NULL
605 #endif /* DISK_HAVE_READ */
607 void module_register (void)
609 plugin_register ("partition", NULL, NULL, partition_write);
610 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);