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_STATGRAB_H
63 # include <statgrab.h>
64 #endif
66 #if HAVE_IOKIT_IOKITLIB_H
67 static mach_port_t io_master_port = MACH_PORT_NULL;
68 /* #endif HAVE_IOKIT_IOKITLIB_H */
70 #elif KERNEL_LINUX
71 typedef struct diskstats
72 {
73 char *name;
75 /* This overflows in roughly 1361 years */
76 unsigned int poll_count;
78 counter_t read_sectors;
79 counter_t write_sectors;
81 counter_t read_bytes;
82 counter_t write_bytes;
84 counter_t read_ops;
85 counter_t write_ops;
86 counter_t read_time;
87 counter_t write_time;
89 counter_t avg_read_time;
90 counter_t avg_write_time;
92 struct diskstats *next;
93 } diskstats_t;
95 static diskstats_t *disklist;
96 /* #endif KERNEL_LINUX */
98 #elif HAVE_LIBKSTAT
99 #define MAX_NUMDISK 256
100 extern kstat_ctl_t *kc;
101 static kstat_t *ksp[MAX_NUMDISK];
102 static int numdisk = 0;
103 /* #endif HAVE_LIBKSTAT */
105 #elif defined(HAVE_LIBSTATGRAB)
106 /* #endif HAVE_LIBKSTATGRAB */
108 #else
109 # error "No applicable input method."
110 #endif
112 static const char *config_keys[] =
113 {
114 "Disk",
115 "IgnoreSelected"
116 };
117 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
119 static ignorelist_t *ignorelist = NULL;
121 static int disk_config (const char *key, const char *value)
122 {
123 if (ignorelist == NULL)
124 ignorelist = ignorelist_create (/* invert = */ 1);
125 if (ignorelist == NULL)
126 return (1);
128 if (strcasecmp ("Disk", key) == 0)
129 {
130 ignorelist_add (ignorelist, value);
131 }
132 else if (strcasecmp ("IgnoreSelected", key) == 0)
133 {
134 int invert = 1;
135 if ((strcasecmp ("True", value) == 0)
136 || (strcasecmp ("Yes", value) == 0)
137 || (strcasecmp ("On", value) == 0))
138 invert = 0;
139 ignorelist_set_invert (ignorelist, invert);
140 }
141 else
142 {
143 return (-1);
144 }
146 return (0);
147 } /* int disk_config */
149 static int disk_init (void)
150 {
151 #if HAVE_IOKIT_IOKITLIB_H
152 kern_return_t status;
154 if (io_master_port != MACH_PORT_NULL)
155 {
156 mach_port_deallocate (mach_task_self (),
157 io_master_port);
158 io_master_port = MACH_PORT_NULL;
159 }
161 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
162 if (status != kIOReturnSuccess)
163 {
164 ERROR ("IOMasterPort failed: %s",
165 mach_error_string (status));
166 io_master_port = MACH_PORT_NULL;
167 return (-1);
168 }
169 /* #endif HAVE_IOKIT_IOKITLIB_H */
171 #elif KERNEL_LINUX
172 /* do nothing */
173 /* #endif KERNEL_LINUX */
175 #elif HAVE_LIBKSTAT
176 kstat_t *ksp_chain;
178 numdisk = 0;
180 if (kc == NULL)
181 return (-1);
183 for (numdisk = 0, ksp_chain = kc->kc_chain;
184 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
185 ksp_chain = ksp_chain->ks_next)
186 {
187 if (strncmp (ksp_chain->ks_class, "disk", 4)
188 && strncmp (ksp_chain->ks_class, "partition", 9))
189 continue;
190 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
191 continue;
192 ksp[numdisk++] = ksp_chain;
193 }
194 #endif /* HAVE_LIBKSTAT */
196 return (0);
197 } /* int disk_init */
199 static void disk_submit (const char *plugin_instance,
200 const char *type,
201 counter_t read, counter_t write)
202 {
203 value_t values[2];
204 value_list_t vl = VALUE_LIST_INIT;
206 /* Both `ignorelist' and `plugin_instance' may be NULL. */
207 if (ignorelist_match (ignorelist, plugin_instance) != 0)
208 return;
210 values[0].counter = read;
211 values[1].counter = write;
213 vl.values = values;
214 vl.values_len = 2;
215 vl.time = time (NULL);
216 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
217 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
218 sstrncpy (vl.plugin_instance, plugin_instance,
219 sizeof (vl.plugin_instance));
220 sstrncpy (vl.type, type, sizeof (vl.type));
222 plugin_dispatch_values (&vl);
223 } /* void disk_submit */
225 #if HAVE_IOKIT_IOKITLIB_H
226 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
227 {
228 signed long long val_int;
229 CFNumberRef val_obj;
230 CFStringRef key_obj;
232 /* `key_obj' needs to be released. */
233 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
234 kCFStringEncodingASCII);
235 if (key_obj == NULL)
236 {
237 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
238 return (-1LL);
239 }
241 /* get => we don't need to release (== free) the object */
242 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
244 CFRelease (key_obj);
246 if (val_obj == NULL)
247 {
248 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
249 return (-1LL);
250 }
252 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
253 {
254 DEBUG ("CFNumberGetValue (%s) failed.", key);
255 return (-1LL);
256 }
258 return (val_int);
259 }
260 #endif /* HAVE_IOKIT_IOKITLIB_H */
262 static int disk_read (void)
263 {
264 #if HAVE_IOKIT_IOKITLIB_H
265 io_registry_entry_t disk;
266 io_registry_entry_t disk_child;
267 io_iterator_t disk_list;
268 CFDictionaryRef props_dict;
269 CFDictionaryRef stats_dict;
270 CFDictionaryRef child_dict;
271 kern_return_t status;
273 signed long long read_ops;
274 signed long long read_byt;
275 signed long long read_tme;
276 signed long long write_ops;
277 signed long long write_byt;
278 signed long long write_tme;
280 int disk_major;
281 int disk_minor;
282 char disk_name[64];
284 /* Get the list of all disk objects. */
285 if (IOServiceGetMatchingServices (io_master_port,
286 IOServiceMatching (kIOBlockStorageDriverClass),
287 &disk_list) != kIOReturnSuccess)
288 {
289 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
290 return (-1);
291 }
293 while ((disk = IOIteratorNext (disk_list)) != 0)
294 {
295 props_dict = NULL;
296 stats_dict = NULL;
297 child_dict = NULL;
299 /* `disk_child' must be released */
300 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
301 != kIOReturnSuccess)
302 {
303 /* This fails for example for DVD/CD drives.. */
304 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
305 IOObjectRelease (disk);
306 continue;
307 }
309 /* We create `props_dict' => we need to release it later */
310 if (IORegistryEntryCreateCFProperties (disk,
311 (CFMutableDictionaryRef *) &props_dict,
312 kCFAllocatorDefault,
313 kNilOptions)
314 != kIOReturnSuccess)
315 {
316 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
317 IOObjectRelease (disk_child);
318 IOObjectRelease (disk);
319 continue;
320 }
322 if (props_dict == NULL)
323 {
324 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
325 IOObjectRelease (disk_child);
326 IOObjectRelease (disk);
327 continue;
328 }
330 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
331 CFSTR (kIOBlockStorageDriverStatisticsKey));
333 if (stats_dict == NULL)
334 {
335 DEBUG ("CFDictionaryGetValue (%s) failed.",
336 kIOBlockStorageDriverStatisticsKey);
337 CFRelease (props_dict);
338 IOObjectRelease (disk_child);
339 IOObjectRelease (disk);
340 continue;
341 }
343 if (IORegistryEntryCreateCFProperties (disk_child,
344 (CFMutableDictionaryRef *) &child_dict,
345 kCFAllocatorDefault,
346 kNilOptions)
347 != kIOReturnSuccess)
348 {
349 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
350 IOObjectRelease (disk_child);
351 CFRelease (props_dict);
352 IOObjectRelease (disk);
353 continue;
354 }
356 /* kIOBSDNameKey */
357 disk_major = (int) dict_get_value (child_dict,
358 kIOBSDMajorKey);
359 disk_minor = (int) dict_get_value (child_dict,
360 kIOBSDMinorKey);
361 read_ops = dict_get_value (stats_dict,
362 kIOBlockStorageDriverStatisticsReadsKey);
363 read_byt = dict_get_value (stats_dict,
364 kIOBlockStorageDriverStatisticsBytesReadKey);
365 read_tme = dict_get_value (stats_dict,
366 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
367 write_ops = dict_get_value (stats_dict,
368 kIOBlockStorageDriverStatisticsWritesKey);
369 write_byt = dict_get_value (stats_dict,
370 kIOBlockStorageDriverStatisticsBytesWrittenKey);
371 /* This property describes the number of nanoseconds spent
372 * performing writes since the block storage driver was
373 * instantiated. It is one of the statistic entries listed
374 * under the top-level kIOBlockStorageDriverStatisticsKey
375 * property table. It has an OSNumber value. */
376 write_tme = dict_get_value (stats_dict,
377 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
379 if (ssnprintf (disk_name, sizeof (disk_name),
380 "%i-%i", disk_major, disk_minor) >= sizeof (disk_name))
381 {
382 DEBUG ("snprintf (major, minor) failed.");
383 CFRelease (child_dict);
384 IOObjectRelease (disk_child);
385 CFRelease (props_dict);
386 IOObjectRelease (disk);
387 continue;
388 }
389 DEBUG ("disk_name = %s", disk_name);
391 if ((read_byt != -1LL) || (write_byt != -1LL))
392 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
393 if ((read_ops != -1LL) || (write_ops != -1LL))
394 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
395 if ((read_tme != -1LL) || (write_tme != -1LL))
396 disk_submit (disk_name, "disk_time",
397 read_tme / 1000,
398 write_tme / 1000);
400 CFRelease (child_dict);
401 IOObjectRelease (disk_child);
402 CFRelease (props_dict);
403 IOObjectRelease (disk);
404 }
405 IOObjectRelease (disk_list);
406 /* #endif HAVE_IOKIT_IOKITLIB_H */
408 #elif KERNEL_LINUX
409 FILE *fh;
410 char buffer[1024];
412 char *fields[32];
413 int numfields;
414 int fieldshift = 0;
416 int major = 0;
417 int minor = 0;
419 counter_t read_sectors = 0;
420 counter_t write_sectors = 0;
422 counter_t read_ops = 0;
423 counter_t read_merged = 0;
424 counter_t read_time = 0;
425 counter_t write_ops = 0;
426 counter_t write_merged = 0;
427 counter_t write_time = 0;
428 int is_disk = 0;
430 diskstats_t *ds, *pre_ds;
432 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
433 {
434 fh = fopen ("/proc/partitions", "r");
435 if (fh == NULL)
436 {
437 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
438 return (-1);
439 }
441 /* Kernel is 2.4.* */
442 fieldshift = 1;
443 }
445 while (fgets (buffer, sizeof (buffer), fh) != NULL)
446 {
447 char *disk_name;
449 numfields = strsplit (buffer, fields, 32);
451 if ((numfields != (14 + fieldshift)) && (numfields != 7))
452 continue;
454 major = atoll (fields[0]);
455 minor = atoll (fields[1]);
457 disk_name = fields[2 + fieldshift];
459 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
460 if (strcmp (disk_name, ds->name) == 0)
461 break;
463 if (ds == NULL)
464 {
465 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
466 continue;
468 if ((ds->name = strdup (disk_name)) == NULL)
469 {
470 free (ds);
471 continue;
472 }
474 if (pre_ds == NULL)
475 disklist = ds;
476 else
477 pre_ds->next = ds;
478 }
480 is_disk = 0;
481 if (numfields == 7)
482 {
483 /* Kernel 2.6, Partition */
484 read_ops = atoll (fields[3]);
485 read_sectors = atoll (fields[4]);
486 write_ops = atoll (fields[5]);
487 write_sectors = atoll (fields[6]);
488 }
489 else if (numfields == (14 + fieldshift))
490 {
491 read_ops = atoll (fields[3 + fieldshift]);
492 write_ops = atoll (fields[7 + fieldshift]);
494 read_sectors = atoll (fields[5 + fieldshift]);
495 write_sectors = atoll (fields[9 + fieldshift]);
497 if ((fieldshift == 0) || (minor == 0))
498 {
499 is_disk = 1;
500 read_merged = atoll (fields[4 + fieldshift]);
501 read_time = atoll (fields[6 + fieldshift]);
502 write_merged = atoll (fields[8 + fieldshift]);
503 write_time = atoll (fields[10+ fieldshift]);
504 }
505 }
506 else
507 {
508 DEBUG ("numfields = %i; => unknown file format.", numfields);
509 continue;
510 }
512 {
513 counter_t diff_read_sectors;
514 counter_t diff_write_sectors;
516 /* If the counter wraps around, it's only 32 bits.. */
517 if (read_sectors < ds->read_sectors)
518 diff_read_sectors = 1 + read_sectors
519 + (UINT_MAX - ds->read_sectors);
520 else
521 diff_read_sectors = read_sectors - ds->read_sectors;
522 if (write_sectors < ds->write_sectors)
523 diff_write_sectors = 1 + write_sectors
524 + (UINT_MAX - ds->write_sectors);
525 else
526 diff_write_sectors = write_sectors - ds->write_sectors;
528 ds->read_bytes += 512 * diff_read_sectors;
529 ds->write_bytes += 512 * diff_write_sectors;
530 ds->read_sectors = read_sectors;
531 ds->write_sectors = write_sectors;
532 }
534 /* Calculate the average time an io-op needs to complete */
535 if (is_disk)
536 {
537 counter_t diff_read_ops;
538 counter_t diff_write_ops;
539 counter_t diff_read_time;
540 counter_t diff_write_time;
542 if (read_ops < ds->read_ops)
543 diff_read_ops = 1 + read_ops
544 + (UINT_MAX - ds->read_ops);
545 else
546 diff_read_ops = read_ops - ds->read_ops;
547 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
548 "ds->read_ops = %llu; diff_read_ops = %llu;",
549 disk_name,
550 read_ops, ds->read_ops, diff_read_ops);
552 if (write_ops < ds->write_ops)
553 diff_write_ops = 1 + write_ops
554 + (UINT_MAX - ds->write_ops);
555 else
556 diff_write_ops = write_ops - ds->write_ops;
558 if (read_time < ds->read_time)
559 diff_read_time = 1 + read_time
560 + (UINT_MAX - ds->read_time);
561 else
562 diff_read_time = read_time - ds->read_time;
564 if (write_time < ds->write_time)
565 diff_write_time = 1 + write_time
566 + (UINT_MAX - ds->write_time);
567 else
568 diff_write_time = write_time - ds->write_time;
570 if (diff_read_ops != 0)
571 ds->avg_read_time += (diff_read_time
572 + (diff_read_ops / 2))
573 / diff_read_ops;
574 if (diff_write_ops != 0)
575 ds->avg_write_time += (diff_write_time
576 + (diff_write_ops / 2))
577 / diff_write_ops;
579 ds->read_ops = read_ops;
580 ds->read_time = read_time;
581 ds->write_ops = write_ops;
582 ds->write_time = write_time;
583 } /* if (is_disk) */
585 /* Don't write to the RRDs if we've just started.. */
586 ds->poll_count++;
587 if (ds->poll_count <= 2)
588 {
589 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
590 "(min_poll_count = 2); => Not writing.",
591 ds->poll_count);
592 continue;
593 }
595 if ((read_ops == 0) && (write_ops == 0))
596 {
597 DEBUG ("disk plugin: ((read_ops == 0) && "
598 "(write_ops == 0)); => Not writing.");
599 continue;
600 }
602 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
603 disk_submit (disk_name, "disk_octets",
604 ds->read_bytes, ds->write_bytes);
606 if ((ds->read_ops != 0) || (ds->write_ops != 0))
607 disk_submit (disk_name, "disk_ops",
608 read_ops, write_ops);
610 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
611 disk_submit (disk_name, "disk_time",
612 ds->avg_read_time, ds->avg_write_time);
614 if (is_disk)
615 {
616 disk_submit (disk_name, "disk_merged",
617 read_merged, write_merged);
618 } /* if (is_disk) */
619 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
621 fclose (fh);
622 /* #endif defined(KERNEL_LINUX) */
624 #elif HAVE_LIBKSTAT
625 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
626 # define KIO_ROCTETS reads
627 # define KIO_WOCTETS writes
628 # define KIO_ROPS nreads
629 # define KIO_WOPS nwrites
630 # define KIO_RTIME rtime
631 # define KIO_WTIME wtime
632 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
633 # define KIO_ROCTETS nread
634 # define KIO_WOCTETS nwritten
635 # define KIO_ROPS reads
636 # define KIO_WOPS writes
637 # define KIO_RTIME rtime
638 # define KIO_WTIME wtime
639 # else
640 # error "kstat_io_t does not have the required members"
641 # endif
642 static kstat_io_t kio;
643 int i;
645 if (kc == NULL)
646 return (-1);
648 for (i = 0; i < numdisk; i++)
649 {
650 if (kstat_read (kc, ksp[i], &kio) == -1)
651 continue;
653 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
654 {
655 disk_submit (ksp[i]->ks_name, "disk_octets",
656 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
657 disk_submit (ksp[i]->ks_name, "disk_ops",
658 kio.KIO_ROPS, kio.KIO_WOPS);
659 /* FIXME: Convert this to microseconds if necessary */
660 disk_submit (ksp[i]->ks_name, "disk_time",
661 kio.KIO_RTIME, kio.KIO_WTIME);
662 }
663 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
664 {
665 disk_submit (ksp[i]->ks_name, "disk_octets",
666 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
667 disk_submit (ksp[i]->ks_name, "disk_ops",
668 kio.KIO_ROPS, kio.KIO_WOPS);
669 }
670 }
671 /* #endif defined(HAVE_LIBKSTAT) */
673 #elif defined(HAVE_LIBSTATGRAB)
674 sg_disk_io_stats *ds;
675 int disks, counter;
676 char name[DATA_MAX_NAME_LEN];
678 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
679 return (0);
681 for (counter=0; counter < disks; counter++) {
682 strncpy(name, ds->disk_name, sizeof(name));
683 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
684 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
685 ds++;
686 }
687 #endif /* defined(HAVE_LIBSTATGRAB) */
689 return (0);
690 } /* int disk_read */
692 void module_register (void)
693 {
694 plugin_register_config ("disk", disk_config,
695 config_keys, config_keys_num);
696 plugin_register_init ("disk", disk_init);
697 plugin_register_read ("disk", disk_read);
698 } /* void module_register */