1 /**
2 * collectd - src/disk.c
3 * Copyright (C) 2005-2008 Florian octo Forster
4 *
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; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_ignorelist.h"
27 #if HAVE_MACH_MACH_TYPES_H
28 # include <mach/mach_types.h>
29 #endif
30 #if HAVE_MACH_MACH_INIT_H
31 # include <mach/mach_init.h>
32 #endif
33 #if HAVE_MACH_MACH_ERROR_H
34 # include <mach/mach_error.h>
35 #endif
36 #if HAVE_MACH_MACH_PORT_H
37 # include <mach/mach_port.h>
38 #endif
39 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
40 # include <CoreFoundation/CoreFoundation.h>
41 #endif
42 #if HAVE_IOKIT_IOKITLIB_H
43 # include <IOKit/IOKitLib.h>
44 #endif
45 #if HAVE_IOKIT_IOTYPES_H
46 # include <IOKit/IOTypes.h>
47 #endif
48 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
49 # include <IOKit/storage/IOBlockStorageDriver.h>
50 #endif
51 #if HAVE_IOKIT_IOBSD_H
52 # include <IOKit/IOBSD.h>
53 #endif
55 #if HAVE_LIMITS_H
56 # include <limits.h>
57 #endif
58 #ifndef UINT_MAX
59 # define UINT_MAX 4294967295U
60 #endif
62 #if HAVE_IOKIT_IOKITLIB_H
63 static mach_port_t io_master_port = MACH_PORT_NULL;
64 /* #endif HAVE_IOKIT_IOKITLIB_H */
66 #elif KERNEL_LINUX
67 typedef struct diskstats
68 {
69 char *name;
71 /* This overflows in roughly 1361 years */
72 unsigned int poll_count;
74 counter_t read_sectors;
75 counter_t write_sectors;
77 counter_t read_bytes;
78 counter_t write_bytes;
80 counter_t read_ops;
81 counter_t write_ops;
82 counter_t read_time;
83 counter_t write_time;
85 counter_t avg_read_time;
86 counter_t avg_write_time;
88 struct diskstats *next;
89 } diskstats_t;
91 static diskstats_t *disklist;
92 /* #endif KERNEL_LINUX */
94 #elif HAVE_LIBKSTAT
95 #define MAX_NUMDISK 256
96 extern kstat_ctl_t *kc;
97 static kstat_t *ksp[MAX_NUMDISK];
98 static int numdisk = 0;
99 /* #endif HAVE_LIBKSTAT */
101 #else
102 # error "No applicable input method."
103 #endif
105 static const char *config_keys[] =
106 {
107 "Disk",
108 "IgnoreSelected"
109 };
110 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
112 static ignorelist_t *ignorelist = NULL;
114 static int disk_config (const char *key, const char *value)
115 {
116 if (ignorelist == NULL)
117 ignorelist = ignorelist_create (/* invert = */ 1);
118 if (ignorelist == NULL)
119 return (1);
121 if (strcasecmp ("Disk", key) == 0)
122 {
123 ignorelist_add (ignorelist, value);
124 }
125 else if (strcasecmp ("IgnoreSelected", key) == 0)
126 {
127 int invert = 1;
128 if ((strcasecmp ("True", value) == 0)
129 || (strcasecmp ("Yes", value) == 0)
130 || (strcasecmp ("On", value) == 0))
131 invert = 0;
132 ignorelist_set_invert (ignorelist, invert);
133 }
134 else
135 {
136 return (-1);
137 }
139 return (0);
140 } /* int disk_config */
142 static int disk_init (void)
143 {
144 #if HAVE_IOKIT_IOKITLIB_H
145 kern_return_t status;
147 if (io_master_port != MACH_PORT_NULL)
148 {
149 mach_port_deallocate (mach_task_self (),
150 io_master_port);
151 io_master_port = MACH_PORT_NULL;
152 }
154 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
155 if (status != kIOReturnSuccess)
156 {
157 ERROR ("IOMasterPort failed: %s",
158 mach_error_string (status));
159 io_master_port = MACH_PORT_NULL;
160 return (-1);
161 }
162 /* #endif HAVE_IOKIT_IOKITLIB_H */
164 #elif KERNEL_LINUX
165 /* do nothing */
166 /* #endif KERNEL_LINUX */
168 #elif HAVE_LIBKSTAT
169 kstat_t *ksp_chain;
171 numdisk = 0;
173 if (kc == NULL)
174 return (-1);
176 for (numdisk = 0, ksp_chain = kc->kc_chain;
177 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
178 ksp_chain = ksp_chain->ks_next)
179 {
180 if (strncmp (ksp_chain->ks_class, "disk", 4)
181 && strncmp (ksp_chain->ks_class, "partition", 9))
182 continue;
183 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
184 continue;
185 ksp[numdisk++] = ksp_chain;
186 }
187 #endif /* HAVE_LIBKSTAT */
189 return (0);
190 } /* int disk_init */
192 static void disk_submit (const char *plugin_instance,
193 const char *type,
194 counter_t read, counter_t write)
195 {
196 value_t values[2];
197 value_list_t vl = VALUE_LIST_INIT;
199 /* Both `ignorelist' and `plugin_instance' may be NULL. */
200 if (ignorelist_match (ignorelist, plugin_instance) != 0)
201 return;
203 values[0].counter = read;
204 values[1].counter = write;
206 vl.values = values;
207 vl.values_len = 2;
208 vl.time = time (NULL);
209 strcpy (vl.host, hostname_g);
210 strcpy (vl.plugin, "disk");
211 strncpy (vl.plugin_instance, plugin_instance,
212 sizeof (vl.plugin_instance));
213 strncpy (vl.type, type, sizeof (vl.type));
215 plugin_dispatch_values (&vl);
216 } /* void disk_submit */
218 #if HAVE_IOKIT_IOKITLIB_H
219 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
220 {
221 signed long long val_int;
222 CFNumberRef val_obj;
223 CFStringRef key_obj;
225 /* `key_obj' needs to be released. */
226 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
227 kCFStringEncodingASCII);
228 if (key_obj == NULL)
229 {
230 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
231 return (-1LL);
232 }
234 /* get => we don't need to release (== free) the object */
235 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
237 CFRelease (key_obj);
239 if (val_obj == NULL)
240 {
241 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
242 return (-1LL);
243 }
245 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
246 {
247 DEBUG ("CFNumberGetValue (%s) failed.", key);
248 return (-1LL);
249 }
251 return (val_int);
252 }
253 #endif /* HAVE_IOKIT_IOKITLIB_H */
255 static int disk_read (void)
256 {
257 #if HAVE_IOKIT_IOKITLIB_H
258 io_registry_entry_t disk;
259 io_registry_entry_t disk_child;
260 io_iterator_t disk_list;
261 CFDictionaryRef props_dict;
262 CFDictionaryRef stats_dict;
263 CFDictionaryRef child_dict;
264 kern_return_t status;
266 signed long long read_ops;
267 signed long long read_byt;
268 signed long long read_tme;
269 signed long long write_ops;
270 signed long long write_byt;
271 signed long long write_tme;
273 int disk_major;
274 int disk_minor;
275 char disk_name[64];
277 /* Get the list of all disk objects. */
278 if (IOServiceGetMatchingServices (io_master_port,
279 IOServiceMatching (kIOBlockStorageDriverClass),
280 &disk_list) != kIOReturnSuccess)
281 {
282 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
283 return (-1);
284 }
286 while ((disk = IOIteratorNext (disk_list)) != 0)
287 {
288 props_dict = NULL;
289 stats_dict = NULL;
290 child_dict = NULL;
292 /* `disk_child' must be released */
293 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
294 != kIOReturnSuccess)
295 {
296 /* This fails for example for DVD/CD drives.. */
297 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
298 IOObjectRelease (disk);
299 continue;
300 }
302 /* We create `props_dict' => we need to release it later */
303 if (IORegistryEntryCreateCFProperties (disk,
304 (CFMutableDictionaryRef *) &props_dict,
305 kCFAllocatorDefault,
306 kNilOptions)
307 != kIOReturnSuccess)
308 {
309 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
310 IOObjectRelease (disk_child);
311 IOObjectRelease (disk);
312 continue;
313 }
315 if (props_dict == NULL)
316 {
317 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
318 IOObjectRelease (disk_child);
319 IOObjectRelease (disk);
320 continue;
321 }
323 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
324 CFSTR (kIOBlockStorageDriverStatisticsKey));
326 if (stats_dict == NULL)
327 {
328 DEBUG ("CFDictionaryGetValue (%s) failed.",
329 kIOBlockStorageDriverStatisticsKey);
330 CFRelease (props_dict);
331 IOObjectRelease (disk_child);
332 IOObjectRelease (disk);
333 continue;
334 }
336 if (IORegistryEntryCreateCFProperties (disk_child,
337 (CFMutableDictionaryRef *) &child_dict,
338 kCFAllocatorDefault,
339 kNilOptions)
340 != kIOReturnSuccess)
341 {
342 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
343 IOObjectRelease (disk_child);
344 CFRelease (props_dict);
345 IOObjectRelease (disk);
346 continue;
347 }
349 /* kIOBSDNameKey */
350 disk_major = (int) dict_get_value (child_dict,
351 kIOBSDMajorKey);
352 disk_minor = (int) dict_get_value (child_dict,
353 kIOBSDMinorKey);
354 read_ops = dict_get_value (stats_dict,
355 kIOBlockStorageDriverStatisticsReadsKey);
356 read_byt = dict_get_value (stats_dict,
357 kIOBlockStorageDriverStatisticsBytesReadKey);
358 read_tme = dict_get_value (stats_dict,
359 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
360 write_ops = dict_get_value (stats_dict,
361 kIOBlockStorageDriverStatisticsWritesKey);
362 write_byt = dict_get_value (stats_dict,
363 kIOBlockStorageDriverStatisticsBytesWrittenKey);
364 /* This property describes the number of nanoseconds spent
365 * performing writes since the block storage driver was
366 * instantiated. It is one of the statistic entries listed
367 * under the top-level kIOBlockStorageDriverStatisticsKey
368 * property table. It has an OSNumber value. */
369 write_tme = dict_get_value (stats_dict,
370 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
372 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
373 {
374 DEBUG ("snprintf (major, minor) failed.");
375 CFRelease (child_dict);
376 IOObjectRelease (disk_child);
377 CFRelease (props_dict);
378 IOObjectRelease (disk);
379 continue;
380 }
381 DEBUG ("disk_name = %s", disk_name);
383 if ((read_byt != -1LL) || (write_byt != -1LL))
384 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
385 if ((read_ops != -1LL) || (write_ops != -1LL))
386 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
387 if ((read_tme != -1LL) || (write_tme != -1LL))
388 disk_submit (disk_name, "disk_time",
389 read_tme / 1000,
390 write_tme / 1000);
392 CFRelease (child_dict);
393 IOObjectRelease (disk_child);
394 CFRelease (props_dict);
395 IOObjectRelease (disk);
396 }
397 IOObjectRelease (disk_list);
398 /* #endif HAVE_IOKIT_IOKITLIB_H */
400 #elif KERNEL_LINUX
401 FILE *fh;
402 char buffer[1024];
404 char *fields[32];
405 int numfields;
406 int fieldshift = 0;
408 int major = 0;
409 int minor = 0;
411 counter_t read_sectors = 0;
412 counter_t write_sectors = 0;
414 counter_t read_ops = 0;
415 counter_t read_merged = 0;
416 counter_t read_time = 0;
417 counter_t write_ops = 0;
418 counter_t write_merged = 0;
419 counter_t write_time = 0;
420 int is_disk = 0;
422 diskstats_t *ds, *pre_ds;
424 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
425 {
426 fh = fopen ("/proc/partitions", "r");
427 if (fh == NULL)
428 {
429 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
430 return (-1);
431 }
433 /* Kernel is 2.4.* */
434 fieldshift = 1;
435 }
437 while (fgets (buffer, sizeof (buffer), fh) != NULL)
438 {
439 char *disk_name;
441 numfields = strsplit (buffer, fields, 32);
443 if ((numfields != (14 + fieldshift)) && (numfields != 7))
444 continue;
446 major = atoll (fields[0]);
447 minor = atoll (fields[1]);
449 disk_name = fields[2];
451 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
452 if (strcmp (disk_name, ds->name) == 0)
453 break;
455 if (ds == NULL)
456 {
457 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
458 continue;
460 if ((ds->name = strdup (disk_name)) == NULL)
461 {
462 free (ds);
463 continue;
464 }
466 if (pre_ds == NULL)
467 disklist = ds;
468 else
469 pre_ds->next = ds;
470 }
472 is_disk = 0;
473 if (numfields == 7)
474 {
475 /* Kernel 2.6, Partition */
476 read_ops = atoll (fields[3]);
477 read_sectors = atoll (fields[4]);
478 write_ops = atoll (fields[5]);
479 write_sectors = atoll (fields[6]);
480 }
481 else if (numfields == (14 + fieldshift))
482 {
483 read_ops = atoll (fields[3 + fieldshift]);
484 write_ops = atoll (fields[7 + fieldshift]);
486 read_sectors = atoll (fields[5 + fieldshift]);
487 write_sectors = atoll (fields[9 + fieldshift]);
489 if ((fieldshift == 0) || (minor == 0))
490 {
491 is_disk = 1;
492 read_merged = atoll (fields[4 + fieldshift]);
493 read_time = atoll (fields[6 + fieldshift]);
494 write_merged = atoll (fields[8 + fieldshift]);
495 write_time = atoll (fields[10+ fieldshift]);
496 }
497 }
498 else
499 {
500 DEBUG ("numfields = %i; => unknown file format.", numfields);
501 continue;
502 }
504 {
505 counter_t diff_read_sectors;
506 counter_t diff_write_sectors;
508 /* If the counter wraps around, it's only 32 bits.. */
509 if (read_sectors < ds->read_sectors)
510 diff_read_sectors = 1 + read_sectors
511 + (UINT_MAX - ds->read_sectors);
512 else
513 diff_read_sectors = read_sectors - ds->read_sectors;
514 if (write_sectors < ds->write_sectors)
515 diff_write_sectors = 1 + write_sectors
516 + (UINT_MAX - ds->write_sectors);
517 else
518 diff_write_sectors = write_sectors - ds->write_sectors;
520 ds->read_bytes += 512 * diff_read_sectors;
521 ds->write_bytes += 512 * diff_write_sectors;
522 ds->read_sectors = read_sectors;
523 ds->write_sectors = write_sectors;
524 }
526 /* Calculate the average time an io-op needs to complete */
527 if (is_disk)
528 {
529 counter_t diff_read_ops;
530 counter_t diff_write_ops;
531 counter_t diff_read_time;
532 counter_t diff_write_time;
534 if (read_ops < ds->read_ops)
535 diff_read_ops = 1 + read_ops
536 + (UINT_MAX - ds->read_ops);
537 else
538 diff_read_ops = read_ops - ds->read_ops;
539 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
540 "ds->read_ops = %llu; diff_read_ops = %llu;",
541 disk_name,
542 read_ops, ds->read_ops, diff_read_ops);
544 if (write_ops < ds->write_ops)
545 diff_write_ops = 1 + write_ops
546 + (UINT_MAX - ds->write_ops);
547 else
548 diff_write_ops = write_ops - ds->write_ops;
550 if (read_time < ds->read_time)
551 diff_read_time = 1 + read_time
552 + (UINT_MAX - ds->read_time);
553 else
554 diff_read_time = read_time - ds->read_time;
556 if (write_time < ds->write_time)
557 diff_write_time = 1 + write_time
558 + (UINT_MAX - ds->write_time);
559 else
560 diff_write_time = write_time - ds->write_time;
562 if (diff_read_ops != 0)
563 ds->avg_read_time += (diff_read_time
564 + (diff_read_ops / 2))
565 / diff_read_ops;
566 if (diff_write_ops != 0)
567 ds->avg_write_time += (diff_write_time
568 + (diff_write_ops / 2))
569 / diff_write_ops;
571 ds->read_ops = read_ops;
572 ds->read_time = read_time;
573 ds->write_ops = write_ops;
574 ds->write_time = write_time;
575 } /* if (is_disk) */
577 /* Don't write to the RRDs if we've just started.. */
578 ds->poll_count++;
579 if (ds->poll_count <= 2)
580 {
581 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
582 "(min_poll_count = 2); => Not writing.",
583 ds->poll_count);
584 continue;
585 }
587 if ((read_ops == 0) && (write_ops == 0))
588 {
589 DEBUG ("disk plugin: ((read_ops == 0) && "
590 "(write_ops == 0)); => Not writing.");
591 continue;
592 }
594 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
595 disk_submit (disk_name, "disk_octets",
596 ds->read_bytes, ds->write_bytes);
598 if ((ds->read_ops != 0) || (ds->write_ops != 0))
599 disk_submit (disk_name, "disk_ops",
600 read_ops, write_ops);
602 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
603 disk_submit (disk_name, "disk_time",
604 ds->avg_read_time, ds->avg_write_time);
606 if (is_disk)
607 {
608 disk_submit (disk_name, "disk_merged",
609 read_merged, write_merged);
610 } /* if (is_disk) */
611 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
613 fclose (fh);
614 /* #endif defined(KERNEL_LINUX) */
616 #elif HAVE_LIBKSTAT
617 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
618 # define KIO_ROCTETS reads
619 # define KIO_WOCTETS writes
620 # define KIO_ROPS nreads
621 # define KIO_WOPS nwrites
622 # define KIO_RTIME rtime
623 # define KIO_WTIME wtime
624 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
625 # define KIO_ROCTETS nread
626 # define KIO_WOCTETS nwritten
627 # define KIO_ROPS reads
628 # define KIO_WOPS writes
629 # define KIO_RTIME rtime
630 # define KIO_WTIME wtime
631 # else
632 # error "kstat_io_t does not have the required members"
633 # endif
634 static kstat_io_t kio;
635 int i;
637 if (kc == NULL)
638 return (-1);
640 for (i = 0; i < numdisk; i++)
641 {
642 if (kstat_read (kc, ksp[i], &kio) == -1)
643 continue;
645 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
646 {
647 disk_submit (ksp[i]->ks_name, "disk_octets",
648 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
649 disk_submit (ksp[i]->ks_name, "disk_ops",
650 kio.KIO_ROPS, kio.KIO_WOPS);
651 /* FIXME: Convert this to microseconds if necessary */
652 disk_submit (ksp[i]->ks_name, "disk_time",
653 kio.KIO_RTIME, kio.KIO_WTIME);
654 }
655 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
656 {
657 disk_submit (ksp[i]->ks_name, "disk_octets",
658 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
659 disk_submit (ksp[i]->ks_name, "disk_ops",
660 kio.KIO_ROPS, kio.KIO_WOPS);
661 }
662 }
663 #endif /* defined(HAVE_LIBKSTAT) */
665 return (0);
666 } /* int disk_read */
668 void module_register (void)
669 {
670 plugin_register_config ("disk", disk_config,
671 config_keys, config_keys_num);
672 plugin_register_init ("disk", disk_init);
673 plugin_register_read ("disk", disk_read);
674 } /* void module_register */