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>
27 #define MODULE_NAME "disk"
29 #if HAVE_MACH_MACH_TYPES_H
30 # include <mach/mach_types.h>
32 #if HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
35 #if HAVE_MACH_MACH_ERROR_H
36 # include <mach/mach_error.h>
38 #if HAVE_MACH_MACH_PORT_H
39 # include <mach/mach_port.h>
41 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
42 # include <CoreFoundation/CoreFoundation.h>
44 #if HAVE_IOKIT_IOKITLIB_H
45 # include <IOKit/IOKitLib.h>
47 #if HAVE_IOKIT_IOTYPES_H
48 # include <IOKit/IOTypes.h>
51 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
52 # define DISK_HAVE_READ 1
54 # define DISK_HAVE_READ 0
57 static char *disk_filename_template = "disk-%s.rrd";
58 static char *part_filename_template = "partition-%s.rrd";
60 /* 104857600 == 100 MB */
61 static char *disk_ds_def[] =
63 "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
64 "DS:rmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
65 "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
66 "DS:rtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
67 "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
68 "DS:wmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
69 "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
70 "DS:wtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
73 static int disk_ds_num = 8;
75 static char *part_ds_def[] =
77 "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
78 "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
79 "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
80 "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
83 static int part_ds_num = 4;
85 #if HAVE_IOKIT_IOKITLIB_H
86 static mach_port_t io_master_port = MACH_PORT_NULL;
87 /* #endif HAVE_IOKIT_IOKITLIB_H */
90 typedef struct diskstats
94 /* This overflows in roughly 1361 year */
95 unsigned int poll_count;
97 unsigned int read_sectors;
98 unsigned int write_sectors;
100 unsigned long long read_bytes;
101 unsigned long long write_bytes;
103 struct diskstats *next;
106 static diskstats_t *disklist;
107 /* #endif KERNEL_LINUX */
110 #define MAX_NUMDISK 256
111 extern kstat_ctl_t *kc;
112 static kstat_t *ksp[MAX_NUMDISK];
113 static int numdisk = 0;
114 #endif /* HAVE_LIBKSTAT */
116 static void disk_init (void)
118 #if HAVE_IOKIT_IOKITLIB_H
119 kern_return_t status;
121 if (io_master_port != MACH_PORT_NULL)
123 mach_port_deallocate (mach_task_self (),
125 io_master_port = MACH_PORT_NULL;
128 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
129 if (status != kIOReturnSuccess)
131 syslog (LOG_ERR, "IOMasterPort failed: %s",
132 mach_error_string (status));
133 io_master_port = MACH_PORT_NULL;
136 /* #endif HAVE_IOKIT_IOKITLIB_H */
140 /* #endif KERNEL_LINUX */
150 for (numdisk = 0, ksp_chain = kc->kc_chain;
151 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
152 ksp_chain = ksp_chain->ks_next)
154 if (strncmp (ksp_chain->ks_class, "disk", 4)
155 && strncmp (ksp_chain->ks_class, "partition", 9))
157 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
159 ksp[numdisk++] = ksp_chain;
161 #endif /* HAVE_LIBKSTAT */
166 static void disk_write (char *host, char *inst, char *val)
171 status = snprintf (file, 512, disk_filename_template, inst);
174 else if (status >= 512)
177 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
180 static void partition_write (char *host, char *inst, char *val)
185 status = snprintf (file, 512, part_filename_template, inst);
188 else if (status >= 512)
191 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
196 static void disk_submit (char *disk_name,
197 unsigned long long read_count,
198 unsigned long long read_merged,
199 unsigned long long read_bytes,
200 unsigned long long read_time,
201 unsigned long long write_count,
202 unsigned long long write_merged,
203 unsigned long long write_bytes,
204 unsigned long long write_time)
208 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
209 (unsigned int) curtime,
210 read_count, read_merged, read_bytes, read_time,
211 write_count, write_merged, write_bytes,
212 write_time) >= BUFSIZE)
215 plugin_submit (MODULE_NAME, disk_name, buf);
218 static void partition_submit (char *part_name,
219 unsigned long long read_count,
220 unsigned long long read_bytes,
221 unsigned long long write_count,
222 unsigned long long write_bytes)
226 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
227 (unsigned int) curtime,
228 read_count, read_bytes, write_count,
229 write_bytes) >= BUFSIZE)
232 plugin_submit ("partition", part_name, buf);
236 #if HAVE_IOKIT_IOKITLIB_H
237 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
239 signed long long val_int;
242 /* get => we don't need to release (== free) the object */
243 val_obj = (CFNumberRef) CFDictionaryGetValue (dict,
249 if (CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int)
255 #endif /* HAVE_IOKIT_IOKITLIB_H */
257 static void disk_read (void)
259 #if HAVE_IOKIT_IOKITLIB_H
260 CFDictionaryRef props_dict;
261 CFDictionaryRef stats_dict;
262 io_registry_entry_t disk;
263 io_iterator_t disk_list;
265 signed long long read_ops;
266 signed long long read_byt;
267 signed long long read_tme;
268 signed long long write_ops;
269 signed long long write_byt;
270 signed long long write_tme;
272 /* Get the list of all disk objects. */
273 if (IOServiceGetMatchingServices (io_master_port,
274 IOServiceMatching (kIOBlockStorageDriverClass),
275 &disk_list) != kIOReturnSuccess)
277 syslog (LOG_ERR, "disk-plugin: IOServiceGetMatchingServices failed.");
281 while ((disk = IOIteratorNext (disk_list)) != NULL)
286 /* We create `props_dict' => we need to release it later */
287 if (IORegistryEntryCreateCFProperties (disk,
288 (CFMutableDictionaryRef *) &props_dict,
293 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
297 if (props_dict == NULL)
300 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
301 CFSTR (kIOBlockStorageDriverStatisticsKey));
303 if (stats_dict == NULL)
305 CFRelease (props_dict);
309 read_ops = dict_get_value (stats_dict,
310 kIOBlockStorageDriverStatisticsReadsKey);
311 read_byt = dict_get_value (stats_dict,
312 kIOBlockStorageDriverStatisticsBytesReadKey);
313 read_tme = dict_get_value (stats_dict,
314 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
315 write_ops = dict_get_value (stats_dict,
316 kIOBlockStorageDriverStatisticsWritesKey);
317 write_byt = dict_get_value (stats_dict,
318 kIOBlockStorageDriverStatisticsBytesWrittenKey);
319 write_tme = dict_get_value (stats_dict,
320 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
322 if ((read_ops != -1LL)
323 || (read_byt != -1LL)
324 || (read_tme != -1LL)
325 || (write_ops != -1LL)
326 || (write_byt != -1LL)
327 || (write_tme != -1LL))
328 disk_submit (kIOBlockStorageDriverClass, /* FIXME */
329 read_ops, 0ULL, read_byt, read_tme,
330 write_ops, 0ULL, write_byt, write_tme);
332 CFRelease (props_dict);
333 IOObjectRelease (disk);
335 IOObjectRelease (disk_list);
336 /* #endif HAVE_IOKIT_IOKITLIB_H */
350 unsigned int read_sectors;
351 unsigned int write_sectors;
353 unsigned long long read_count = 0;
354 unsigned long long read_merged = 0;
355 unsigned long long read_bytes = 0;
356 unsigned long long read_time = 0;
357 unsigned long long write_count = 0;
358 unsigned long long write_merged = 0;
359 unsigned long long write_bytes = 0;
360 unsigned long long write_time = 0;
363 diskstats_t *ds, *pre_ds;
365 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
367 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
370 /* Kernel is 2.4.* */
374 while (fgets (buffer, 1024, fh) != NULL)
376 numfields = strsplit (buffer, fields, 32);
378 if ((numfields != (14 + fieldshift)) && (numfields != 7))
381 major = atoll (fields[0]);
382 minor = atoll (fields[1]);
384 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
386 disk_name[127] = '\0';
388 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
389 if (strcmp (disk_name, ds->name) == 0)
394 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
397 if ((ds->name = strdup (disk_name)) == NULL)
412 /* Kernel 2.6, Partition */
413 read_count = atoll (fields[3]);
414 read_sectors = atoi (fields[4]);
415 write_count = atoll (fields[5]);
416 write_sectors = atoi (fields[6]);
418 else if (numfields == (14 + fieldshift))
420 read_count = atoll (fields[3 + fieldshift]);
421 write_count = atoll (fields[7 + fieldshift]);
423 read_sectors = atoi (fields[5 + fieldshift]);
424 write_sectors = atoi (fields[9 + fieldshift]);
426 if ((fieldshift == 0) || (minor == 0))
429 read_merged = atoll (fields[4 + fieldshift]);
430 read_time = atoll (fields[6 + fieldshift]);
431 write_merged = atoll (fields[8 + fieldshift]);
432 write_time = atoll (fields[10+ fieldshift]);
441 if (read_sectors >= ds->read_sectors)
442 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
444 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
446 if (write_sectors >= ds->write_sectors)
447 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
449 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
451 ds->read_sectors = read_sectors;
452 ds->write_sectors = write_sectors;
453 read_bytes = ds->read_bytes;
454 write_bytes = ds->write_bytes;
456 /* Don't write to the RRDs if we've just started.. */
458 if (ds->poll_count <= 6)
461 if ((read_count == 0) && (write_count == 0))
465 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
466 write_count, write_merged, write_bytes, write_time);
468 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
472 /* #endif defined(KERNEL_LINUX) */
475 static kstat_io_t kio;
481 for (i = 0; i < numdisk; i++)
483 if (kstat_read (kc, ksp[i], &kio) == -1)
486 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
487 disk_submit (ksp[i]->ks_name,
488 kio.reads, 0LL, kio.nread, kio.rtime,
489 kio.writes, 0LL, kio.nwritten, kio.wtime);
490 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
491 partition_submit (ksp[i]->ks_name,
492 kio.reads, kio.nread,
493 kio.writes,kio.nwritten);
495 #endif /* defined(HAVE_LIBKSTAT) */
496 } /* static void disk_read (void) */
498 # define disk_read NULL
499 #endif /* DISK_HAVE_READ */
501 void module_register (void)
503 plugin_register ("partition", NULL, NULL, partition_write);
504 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);