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 int read_sectors;
105 unsigned int write_sectors;
107 unsigned long long read_bytes;
108 unsigned long long write_bytes;
110 struct diskstats *next;
113 static diskstats_t *disklist;
114 /* #endif KERNEL_LINUX */
117 #define MAX_NUMDISK 256
118 extern kstat_ctl_t *kc;
119 static kstat_t *ksp[MAX_NUMDISK];
120 static int numdisk = 0;
121 #endif /* HAVE_LIBKSTAT */
123 static void disk_init (void)
125 #if HAVE_IOKIT_IOKITLIB_H
126 kern_return_t status;
128 if (io_master_port != MACH_PORT_NULL)
130 mach_port_deallocate (mach_task_self (),
132 io_master_port = MACH_PORT_NULL;
135 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
136 if (status != kIOReturnSuccess)
138 syslog (LOG_ERR, "IOMasterPort failed: %s",
139 mach_error_string (status));
140 io_master_port = MACH_PORT_NULL;
143 /* #endif HAVE_IOKIT_IOKITLIB_H */
147 /* #endif KERNEL_LINUX */
157 for (numdisk = 0, ksp_chain = kc->kc_chain;
158 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
159 ksp_chain = ksp_chain->ks_next)
161 if (strncmp (ksp_chain->ks_class, "disk", 4)
162 && strncmp (ksp_chain->ks_class, "partition", 9))
164 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
166 ksp[numdisk++] = ksp_chain;
168 #endif /* HAVE_LIBKSTAT */
173 static void disk_write (char *host, char *inst, char *val)
178 status = snprintf (file, 512, disk_filename_template, inst);
181 else if (status >= 512)
184 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
187 static void partition_write (char *host, char *inst, char *val)
192 status = snprintf (file, 512, part_filename_template, inst);
195 else if (status >= 512)
198 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
203 static void disk_submit (char *disk_name,
204 unsigned long long read_count,
205 unsigned long long read_merged,
206 unsigned long long read_bytes,
207 unsigned long long read_time,
208 unsigned long long write_count,
209 unsigned long long write_merged,
210 unsigned long long write_bytes,
211 unsigned long long write_time)
215 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
216 (unsigned int) curtime,
217 read_count, read_merged, read_bytes, read_time,
218 write_count, write_merged, write_bytes,
219 write_time) >= BUFSIZE)
222 plugin_submit (MODULE_NAME, disk_name, buf);
225 #if KERNEL_LINUX || HAVE_LIBKSTAT
226 static void partition_submit (char *part_name,
227 unsigned long long read_count,
228 unsigned long long read_bytes,
229 unsigned long long write_count,
230 unsigned long long write_bytes)
234 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
235 (unsigned int) curtime,
236 read_count, read_bytes, write_count,
237 write_bytes) >= BUFSIZE)
240 plugin_submit ("partition", part_name, buf);
242 #endif /* KERNEL_LINUX || HAVE_LIBKSTAT */
245 #if HAVE_IOKIT_IOKITLIB_H
246 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
248 signed long long val_int;
252 /* `key_obj' needs to be released. */
253 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
254 kCFStringEncodingASCII);
257 DBG ("CFStringCreateWithCString (%s) failed.", key);
261 /* get => we don't need to release (== free) the object */
262 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
268 DBG ("CFDictionaryGetValue (%s) failed.", key);
272 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
274 DBG ("CFNumberGetValue (%s) failed.", key);
280 #endif /* HAVE_IOKIT_IOKITLIB_H */
282 static void disk_read (void)
284 #if HAVE_IOKIT_IOKITLIB_H
285 io_registry_entry_t disk;
286 io_registry_entry_t disk_child;
287 io_iterator_t disk_list;
288 CFDictionaryRef props_dict;
289 CFDictionaryRef stats_dict;
290 CFDictionaryRef child_dict;
291 kern_return_t status;
293 signed long long read_ops;
294 signed long long read_byt;
295 signed long long read_tme;
296 signed long long write_ops;
297 signed long long write_byt;
298 signed long long write_tme;
304 /* Get the list of all disk objects. */
305 if (IOServiceGetMatchingServices (io_master_port,
306 IOServiceMatching (kIOBlockStorageDriverClass),
307 &disk_list) != kIOReturnSuccess)
309 syslog (LOG_ERR, "disk-plugin: IOServiceGetMatchingServices failed.");
313 while ((disk = IOIteratorNext (disk_list)) != 0)
319 /* `disk_child' must be released */
320 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
323 /* This fails for example for DVD/CD drives.. */
324 DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
325 IOObjectRelease (disk);
329 /* We create `props_dict' => we need to release it later */
330 if (IORegistryEntryCreateCFProperties (disk,
331 (CFMutableDictionaryRef *) &props_dict,
336 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
337 IOObjectRelease (disk_child);
338 IOObjectRelease (disk);
342 if (props_dict == NULL)
344 DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
345 IOObjectRelease (disk_child);
346 IOObjectRelease (disk);
350 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
351 CFSTR (kIOBlockStorageDriverStatisticsKey));
353 if (stats_dict == NULL)
355 DBG ("CFDictionaryGetValue (%s) failed.",
356 kIOBlockStorageDriverStatisticsKey);
357 CFRelease (props_dict);
358 IOObjectRelease (disk_child);
359 IOObjectRelease (disk);
363 if (IORegistryEntryCreateCFProperties (disk_child,
364 (CFMutableDictionaryRef *) &child_dict,
369 DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
370 IOObjectRelease (disk_child);
371 CFRelease (props_dict);
372 IOObjectRelease (disk);
376 disk_major = (int) dict_get_value (child_dict,
378 disk_minor = (int) dict_get_value (child_dict,
380 read_ops = dict_get_value (stats_dict,
381 kIOBlockStorageDriverStatisticsReadsKey);
382 read_byt = dict_get_value (stats_dict,
383 kIOBlockStorageDriverStatisticsBytesReadKey);
384 read_tme = dict_get_value (stats_dict,
385 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
386 write_ops = dict_get_value (stats_dict,
387 kIOBlockStorageDriverStatisticsWritesKey);
388 write_byt = dict_get_value (stats_dict,
389 kIOBlockStorageDriverStatisticsBytesWrittenKey);
390 write_tme = dict_get_value (stats_dict,
391 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
393 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
395 DBG ("snprintf (major, minor) failed.");
396 CFRelease (child_dict);
397 IOObjectRelease (disk_child);
398 CFRelease (props_dict);
399 IOObjectRelease (disk);
402 DBG ("disk_name = %s", disk_name);
404 if ((read_ops != -1LL)
405 || (read_byt != -1LL)
406 || (read_tme != -1LL)
407 || (write_ops != -1LL)
408 || (write_byt != -1LL)
409 || (write_tme != -1LL))
410 disk_submit (disk_name,
411 read_ops, 0ULL, read_byt, read_tme,
412 write_ops, 0ULL, write_byt, write_tme);
414 CFRelease (child_dict);
415 IOObjectRelease (disk_child);
416 CFRelease (props_dict);
417 IOObjectRelease (disk);
419 IOObjectRelease (disk_list);
420 /* #endif HAVE_IOKIT_IOKITLIB_H */
434 unsigned int read_sectors;
435 unsigned int write_sectors;
437 unsigned long long read_count = 0;
438 unsigned long long read_merged = 0;
439 unsigned long long read_bytes = 0;
440 unsigned long long read_time = 0;
441 unsigned long long write_count = 0;
442 unsigned long long write_merged = 0;
443 unsigned long long write_bytes = 0;
444 unsigned long long write_time = 0;
447 diskstats_t *ds, *pre_ds;
449 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
451 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
454 /* Kernel is 2.4.* */
458 while (fgets (buffer, 1024, 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 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
470 disk_name[127] = '\0';
472 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
473 if (strcmp (disk_name, ds->name) == 0)
478 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
481 if ((ds->name = strdup (disk_name)) == NULL)
496 /* Kernel 2.6, Partition */
497 read_count = atoll (fields[3]);
498 read_sectors = atoi (fields[4]);
499 write_count = atoll (fields[5]);
500 write_sectors = atoi (fields[6]);
502 else if (numfields == (14 + fieldshift))
504 read_count = atoll (fields[3 + fieldshift]);
505 write_count = atoll (fields[7 + fieldshift]);
507 read_sectors = atoi (fields[5 + fieldshift]);
508 write_sectors = atoi (fields[9 + fieldshift]);
510 if ((fieldshift == 0) || (minor == 0))
513 read_merged = atoll (fields[4 + fieldshift]);
514 read_time = atoll (fields[6 + fieldshift]);
515 write_merged = atoll (fields[8 + fieldshift]);
516 write_time = atoll (fields[10+ fieldshift]);
525 if (read_sectors >= ds->read_sectors)
526 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
528 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
530 if (write_sectors >= ds->write_sectors)
531 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
533 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
535 ds->read_sectors = read_sectors;
536 ds->write_sectors = write_sectors;
537 read_bytes = ds->read_bytes;
538 write_bytes = ds->write_bytes;
540 /* Don't write to the RRDs if we've just started.. */
542 if (ds->poll_count <= 6)
545 if ((read_count == 0) && (write_count == 0))
549 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
550 write_count, write_merged, write_bytes, write_time);
552 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
556 /* #endif defined(KERNEL_LINUX) */
559 static kstat_io_t kio;
565 for (i = 0; i < numdisk; i++)
567 if (kstat_read (kc, ksp[i], &kio) == -1)
570 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
571 disk_submit (ksp[i]->ks_name,
572 kio.reads, 0LL, kio.nread, kio.rtime,
573 kio.writes, 0LL, kio.nwritten, kio.wtime);
574 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
575 partition_submit (ksp[i]->ks_name,
576 kio.reads, kio.nread,
577 kio.writes,kio.nwritten);
579 #endif /* defined(HAVE_LIBKSTAT) */
580 } /* static void disk_read (void) */
582 # define disk_read NULL
583 #endif /* DISK_HAVE_READ */
585 void module_register (void)
587 plugin_register ("partition", NULL, NULL, partition_write);
588 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);