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 CFStringRef CFStringCreateWithCString (
253 CFAllocatorRef alloc,
255 CFStringEncoding encoding
258 /* `key_obj' needs to be released. */
259 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
260 kCFStringEncodingASCII);
264 /* get => we don't need to release (== free) the object */
265 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
272 if (CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int)
278 #endif /* HAVE_IOKIT_IOKITLIB_H */
280 static void disk_read (void)
282 #if HAVE_IOKIT_IOKITLIB_H
283 io_registry_entry_t disk;
284 io_registry_entry_t disk_child;
285 io_iterator_t disk_list;
286 CFDictionaryRef props_dict;
287 CFDictionaryRef stats_dict;
288 CFDictionaryRef child_dict;
290 signed long long read_ops;
291 signed long long read_byt;
292 signed long long read_tme;
293 signed long long write_ops;
294 signed long long write_byt;
295 signed long long write_tme;
301 /* Get the list of all disk objects. */
302 if (IOServiceGetMatchingServices (io_master_port,
303 IOServiceMatching (kIOBlockStorageDriverClass),
304 &disk_list) != kIOReturnSuccess)
306 syslog (LOG_ERR, "disk-plugin: IOServiceGetMatchingServices failed.");
310 while ((disk = IOIteratorNext (disk_list)) != 0)
316 /* We create `props_dict' => we need to release it later */
317 if (IORegistryEntryCreateCFProperties (disk,
318 (CFMutableDictionaryRef *) &props_dict,
323 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
324 IOObjectRelease (disk);
328 if (props_dict == NULL)
330 DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
331 IOObjectRelease (disk);
335 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
336 CFSTR (kIOBlockStorageDriverStatisticsKey));
338 if (stats_dict == NULL)
340 DBG ("CFDictionaryGetValue (%s) failed.",
341 kIOBlockStorageDriverStatisticsKey);
342 CFRelease (props_dict);
343 IOObjectRelease (disk);
347 /* `disk_child' must be released */
348 if (IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child)
351 DBG ("IORegistryEntryGetChildEntry (disk) failed.");
352 CFRelease (props_dict);
353 IOObjectRelease (disk);
357 if (IORegistryEntryCreateCFProperties (disk_child,
358 (CFMutableDictionaryRef *) &child_dict,
363 DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
364 IOObjectRelease (disk_child);
365 CFRelease (props_dict);
366 IOObjectRelease (disk);
370 disk_major = (int) dict_get_value (child_dict,
372 disk_minor = (int) dict_get_value (child_dict,
374 read_ops = dict_get_value (stats_dict,
375 kIOBlockStorageDriverStatisticsReadsKey);
376 read_byt = dict_get_value (stats_dict,
377 kIOBlockStorageDriverStatisticsBytesReadKey);
378 read_tme = dict_get_value (stats_dict,
379 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
380 write_ops = dict_get_value (stats_dict,
381 kIOBlockStorageDriverStatisticsWritesKey);
382 write_byt = dict_get_value (stats_dict,
383 kIOBlockStorageDriverStatisticsBytesWrittenKey);
384 write_tme = dict_get_value (stats_dict,
385 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
387 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
389 DBG ("snprintf (major, minor) failed.");
390 CFRelease (child_dict);
391 IOObjectRelease (disk_child);
392 CFRelease (props_dict);
393 IOObjectRelease (disk);
397 if ((read_ops != -1LL)
398 || (read_byt != -1LL)
399 || (read_tme != -1LL)
400 || (write_ops != -1LL)
401 || (write_byt != -1LL)
402 || (write_tme != -1LL))
403 disk_submit (disk_name,
404 read_ops, 0ULL, read_byt, read_tme,
405 write_ops, 0ULL, write_byt, write_tme);
407 CFRelease (child_dict);
408 IOObjectRelease (disk_child);
409 CFRelease (props_dict);
410 IOObjectRelease (disk);
412 IOObjectRelease (disk_list);
413 /* #endif HAVE_IOKIT_IOKITLIB_H */
427 unsigned int read_sectors;
428 unsigned int write_sectors;
430 unsigned long long read_count = 0;
431 unsigned long long read_merged = 0;
432 unsigned long long read_bytes = 0;
433 unsigned long long read_time = 0;
434 unsigned long long write_count = 0;
435 unsigned long long write_merged = 0;
436 unsigned long long write_bytes = 0;
437 unsigned long long write_time = 0;
440 diskstats_t *ds, *pre_ds;
442 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
444 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
447 /* Kernel is 2.4.* */
451 while (fgets (buffer, 1024, fh) != NULL)
453 numfields = strsplit (buffer, fields, 32);
455 if ((numfields != (14 + fieldshift)) && (numfields != 7))
458 major = atoll (fields[0]);
459 minor = atoll (fields[1]);
461 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
463 disk_name[127] = '\0';
465 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
466 if (strcmp (disk_name, ds->name) == 0)
471 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
474 if ((ds->name = strdup (disk_name)) == NULL)
489 /* Kernel 2.6, Partition */
490 read_count = atoll (fields[3]);
491 read_sectors = atoi (fields[4]);
492 write_count = atoll (fields[5]);
493 write_sectors = atoi (fields[6]);
495 else if (numfields == (14 + fieldshift))
497 read_count = atoll (fields[3 + fieldshift]);
498 write_count = atoll (fields[7 + fieldshift]);
500 read_sectors = atoi (fields[5 + fieldshift]);
501 write_sectors = atoi (fields[9 + fieldshift]);
503 if ((fieldshift == 0) || (minor == 0))
506 read_merged = atoll (fields[4 + fieldshift]);
507 read_time = atoll (fields[6 + fieldshift]);
508 write_merged = atoll (fields[8 + fieldshift]);
509 write_time = atoll (fields[10+ fieldshift]);
518 if (read_sectors >= ds->read_sectors)
519 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
521 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
523 if (write_sectors >= ds->write_sectors)
524 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
526 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
528 ds->read_sectors = read_sectors;
529 ds->write_sectors = write_sectors;
530 read_bytes = ds->read_bytes;
531 write_bytes = ds->write_bytes;
533 /* Don't write to the RRDs if we've just started.. */
535 if (ds->poll_count <= 6)
538 if ((read_count == 0) && (write_count == 0))
542 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
543 write_count, write_merged, write_bytes, write_time);
545 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
549 /* #endif defined(KERNEL_LINUX) */
552 static kstat_io_t kio;
558 for (i = 0; i < numdisk; i++)
560 if (kstat_read (kc, ksp[i], &kio) == -1)
563 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
564 disk_submit (ksp[i]->ks_name,
565 kio.reads, 0LL, kio.nread, kio.rtime,
566 kio.writes, 0LL, kio.nwritten, kio.wtime);
567 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
568 partition_submit (ksp[i]->ks_name,
569 kio.reads, kio.nread,
570 kio.writes,kio.nwritten);
572 #endif /* defined(HAVE_LIBKSTAT) */
573 } /* static void disk_read (void) */
575 # define disk_read NULL
576 #endif /* DISK_HAVE_READ */
578 void module_register (void)
580 plugin_register ("partition", NULL, NULL, partition_write);
581 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);