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 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
216 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
217 sstrncpy (vl.plugin_instance, plugin_instance,
218 sizeof (vl.plugin_instance));
219 sstrncpy (vl.type, type, sizeof (vl.type));
221 plugin_dispatch_values (&vl);
222 } /* void disk_submit */
224 #if HAVE_IOKIT_IOKITLIB_H
225 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
226 {
227 signed long long val_int;
228 CFNumberRef val_obj;
229 CFStringRef key_obj;
231 /* `key_obj' needs to be released. */
232 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
233 kCFStringEncodingASCII);
234 if (key_obj == NULL)
235 {
236 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
237 return (-1LL);
238 }
240 /* get => we don't need to release (== free) the object */
241 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
243 CFRelease (key_obj);
245 if (val_obj == NULL)
246 {
247 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
248 return (-1LL);
249 }
251 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
252 {
253 DEBUG ("CFNumberGetValue (%s) failed.", key);
254 return (-1LL);
255 }
257 return (val_int);
258 }
259 #endif /* HAVE_IOKIT_IOKITLIB_H */
261 static int disk_read (void)
262 {
263 #if HAVE_IOKIT_IOKITLIB_H
264 io_registry_entry_t disk;
265 io_registry_entry_t disk_child;
266 io_iterator_t disk_list;
267 CFDictionaryRef props_dict;
268 CFDictionaryRef stats_dict;
269 CFDictionaryRef child_dict;
270 kern_return_t status;
272 signed long long read_ops;
273 signed long long read_byt;
274 signed long long read_tme;
275 signed long long write_ops;
276 signed long long write_byt;
277 signed long long write_tme;
279 int disk_major;
280 int disk_minor;
281 char disk_name[64];
283 /* Get the list of all disk objects. */
284 if (IOServiceGetMatchingServices (io_master_port,
285 IOServiceMatching (kIOBlockStorageDriverClass),
286 &disk_list) != kIOReturnSuccess)
287 {
288 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
289 return (-1);
290 }
292 while ((disk = IOIteratorNext (disk_list)) != 0)
293 {
294 props_dict = NULL;
295 stats_dict = NULL;
296 child_dict = NULL;
298 /* `disk_child' must be released */
299 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
300 != kIOReturnSuccess)
301 {
302 /* This fails for example for DVD/CD drives.. */
303 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
304 IOObjectRelease (disk);
305 continue;
306 }
308 /* We create `props_dict' => we need to release it later */
309 if (IORegistryEntryCreateCFProperties (disk,
310 (CFMutableDictionaryRef *) &props_dict,
311 kCFAllocatorDefault,
312 kNilOptions)
313 != kIOReturnSuccess)
314 {
315 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
316 IOObjectRelease (disk_child);
317 IOObjectRelease (disk);
318 continue;
319 }
321 if (props_dict == NULL)
322 {
323 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
324 IOObjectRelease (disk_child);
325 IOObjectRelease (disk);
326 continue;
327 }
329 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
330 CFSTR (kIOBlockStorageDriverStatisticsKey));
332 if (stats_dict == NULL)
333 {
334 DEBUG ("CFDictionaryGetValue (%s) failed.",
335 kIOBlockStorageDriverStatisticsKey);
336 CFRelease (props_dict);
337 IOObjectRelease (disk_child);
338 IOObjectRelease (disk);
339 continue;
340 }
342 if (IORegistryEntryCreateCFProperties (disk_child,
343 (CFMutableDictionaryRef *) &child_dict,
344 kCFAllocatorDefault,
345 kNilOptions)
346 != kIOReturnSuccess)
347 {
348 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
349 IOObjectRelease (disk_child);
350 CFRelease (props_dict);
351 IOObjectRelease (disk);
352 continue;
353 }
355 /* kIOBSDNameKey */
356 disk_major = (int) dict_get_value (child_dict,
357 kIOBSDMajorKey);
358 disk_minor = (int) dict_get_value (child_dict,
359 kIOBSDMinorKey);
360 read_ops = dict_get_value (stats_dict,
361 kIOBlockStorageDriverStatisticsReadsKey);
362 read_byt = dict_get_value (stats_dict,
363 kIOBlockStorageDriverStatisticsBytesReadKey);
364 read_tme = dict_get_value (stats_dict,
365 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
366 write_ops = dict_get_value (stats_dict,
367 kIOBlockStorageDriverStatisticsWritesKey);
368 write_byt = dict_get_value (stats_dict,
369 kIOBlockStorageDriverStatisticsBytesWrittenKey);
370 /* This property describes the number of nanoseconds spent
371 * performing writes since the block storage driver was
372 * instantiated. It is one of the statistic entries listed
373 * under the top-level kIOBlockStorageDriverStatisticsKey
374 * property table. It has an OSNumber value. */
375 write_tme = dict_get_value (stats_dict,
376 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
378 if (ssnprintf (disk_name, sizeof (disk_name),
379 "%i-%i", disk_major, disk_minor) >= sizeof (disk_name))
380 {
381 DEBUG ("snprintf (major, minor) failed.");
382 CFRelease (child_dict);
383 IOObjectRelease (disk_child);
384 CFRelease (props_dict);
385 IOObjectRelease (disk);
386 continue;
387 }
388 DEBUG ("disk_name = %s", disk_name);
390 if ((read_byt != -1LL) || (write_byt != -1LL))
391 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
392 if ((read_ops != -1LL) || (write_ops != -1LL))
393 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
394 if ((read_tme != -1LL) || (write_tme != -1LL))
395 disk_submit (disk_name, "disk_time",
396 read_tme / 1000,
397 write_tme / 1000);
399 CFRelease (child_dict);
400 IOObjectRelease (disk_child);
401 CFRelease (props_dict);
402 IOObjectRelease (disk);
403 }
404 IOObjectRelease (disk_list);
405 /* #endif HAVE_IOKIT_IOKITLIB_H */
407 #elif KERNEL_LINUX
408 FILE *fh;
409 char buffer[1024];
411 char *fields[32];
412 int numfields;
413 int fieldshift = 0;
415 int major = 0;
416 int minor = 0;
418 counter_t read_sectors = 0;
419 counter_t write_sectors = 0;
421 counter_t read_ops = 0;
422 counter_t read_merged = 0;
423 counter_t read_time = 0;
424 counter_t write_ops = 0;
425 counter_t write_merged = 0;
426 counter_t write_time = 0;
427 int is_disk = 0;
429 diskstats_t *ds, *pre_ds;
431 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
432 {
433 fh = fopen ("/proc/partitions", "r");
434 if (fh == NULL)
435 {
436 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
437 return (-1);
438 }
440 /* Kernel is 2.4.* */
441 fieldshift = 1;
442 }
444 while (fgets (buffer, sizeof (buffer), fh) != NULL)
445 {
446 char *disk_name;
448 numfields = strsplit (buffer, fields, 32);
450 if ((numfields != (14 + fieldshift)) && (numfields != 7))
451 continue;
453 major = atoll (fields[0]);
454 minor = atoll (fields[1]);
456 disk_name = fields[2 + fieldshift];
458 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
459 if (strcmp (disk_name, ds->name) == 0)
460 break;
462 if (ds == NULL)
463 {
464 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
465 continue;
467 if ((ds->name = strdup (disk_name)) == NULL)
468 {
469 free (ds);
470 continue;
471 }
473 if (pre_ds == NULL)
474 disklist = ds;
475 else
476 pre_ds->next = ds;
477 }
479 is_disk = 0;
480 if (numfields == 7)
481 {
482 /* Kernel 2.6, Partition */
483 read_ops = atoll (fields[3]);
484 read_sectors = atoll (fields[4]);
485 write_ops = atoll (fields[5]);
486 write_sectors = atoll (fields[6]);
487 }
488 else if (numfields == (14 + fieldshift))
489 {
490 read_ops = atoll (fields[3 + fieldshift]);
491 write_ops = atoll (fields[7 + fieldshift]);
493 read_sectors = atoll (fields[5 + fieldshift]);
494 write_sectors = atoll (fields[9 + fieldshift]);
496 if ((fieldshift == 0) || (minor == 0))
497 {
498 is_disk = 1;
499 read_merged = atoll (fields[4 + fieldshift]);
500 read_time = atoll (fields[6 + fieldshift]);
501 write_merged = atoll (fields[8 + fieldshift]);
502 write_time = atoll (fields[10+ fieldshift]);
503 }
504 }
505 else
506 {
507 DEBUG ("numfields = %i; => unknown file format.", numfields);
508 continue;
509 }
511 {
512 counter_t diff_read_sectors;
513 counter_t diff_write_sectors;
515 /* If the counter wraps around, it's only 32 bits.. */
516 if (read_sectors < ds->read_sectors)
517 diff_read_sectors = 1 + read_sectors
518 + (UINT_MAX - ds->read_sectors);
519 else
520 diff_read_sectors = read_sectors - ds->read_sectors;
521 if (write_sectors < ds->write_sectors)
522 diff_write_sectors = 1 + write_sectors
523 + (UINT_MAX - ds->write_sectors);
524 else
525 diff_write_sectors = write_sectors - ds->write_sectors;
527 ds->read_bytes += 512 * diff_read_sectors;
528 ds->write_bytes += 512 * diff_write_sectors;
529 ds->read_sectors = read_sectors;
530 ds->write_sectors = write_sectors;
531 }
533 /* Calculate the average time an io-op needs to complete */
534 if (is_disk)
535 {
536 counter_t diff_read_ops;
537 counter_t diff_write_ops;
538 counter_t diff_read_time;
539 counter_t diff_write_time;
541 if (read_ops < ds->read_ops)
542 diff_read_ops = 1 + read_ops
543 + (UINT_MAX - ds->read_ops);
544 else
545 diff_read_ops = read_ops - ds->read_ops;
546 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
547 "ds->read_ops = %llu; diff_read_ops = %llu;",
548 disk_name,
549 read_ops, ds->read_ops, diff_read_ops);
551 if (write_ops < ds->write_ops)
552 diff_write_ops = 1 + write_ops
553 + (UINT_MAX - ds->write_ops);
554 else
555 diff_write_ops = write_ops - ds->write_ops;
557 if (read_time < ds->read_time)
558 diff_read_time = 1 + read_time
559 + (UINT_MAX - ds->read_time);
560 else
561 diff_read_time = read_time - ds->read_time;
563 if (write_time < ds->write_time)
564 diff_write_time = 1 + write_time
565 + (UINT_MAX - ds->write_time);
566 else
567 diff_write_time = write_time - ds->write_time;
569 if (diff_read_ops != 0)
570 ds->avg_read_time += (diff_read_time
571 + (diff_read_ops / 2))
572 / diff_read_ops;
573 if (diff_write_ops != 0)
574 ds->avg_write_time += (diff_write_time
575 + (diff_write_ops / 2))
576 / diff_write_ops;
578 ds->read_ops = read_ops;
579 ds->read_time = read_time;
580 ds->write_ops = write_ops;
581 ds->write_time = write_time;
582 } /* if (is_disk) */
584 /* Don't write to the RRDs if we've just started.. */
585 ds->poll_count++;
586 if (ds->poll_count <= 2)
587 {
588 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
589 "(min_poll_count = 2); => Not writing.",
590 ds->poll_count);
591 continue;
592 }
594 if ((read_ops == 0) && (write_ops == 0))
595 {
596 DEBUG ("disk plugin: ((read_ops == 0) && "
597 "(write_ops == 0)); => Not writing.");
598 continue;
599 }
601 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
602 disk_submit (disk_name, "disk_octets",
603 ds->read_bytes, ds->write_bytes);
605 if ((ds->read_ops != 0) || (ds->write_ops != 0))
606 disk_submit (disk_name, "disk_ops",
607 read_ops, write_ops);
609 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
610 disk_submit (disk_name, "disk_time",
611 ds->avg_read_time, ds->avg_write_time);
613 if (is_disk)
614 {
615 disk_submit (disk_name, "disk_merged",
616 read_merged, write_merged);
617 } /* if (is_disk) */
618 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
620 fclose (fh);
621 /* #endif defined(KERNEL_LINUX) */
623 #elif HAVE_LIBKSTAT
624 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
625 # define KIO_ROCTETS reads
626 # define KIO_WOCTETS writes
627 # define KIO_ROPS nreads
628 # define KIO_WOPS nwrites
629 # define KIO_RTIME rtime
630 # define KIO_WTIME wtime
631 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
632 # define KIO_ROCTETS nread
633 # define KIO_WOCTETS nwritten
634 # define KIO_ROPS reads
635 # define KIO_WOPS writes
636 # define KIO_RTIME rtime
637 # define KIO_WTIME wtime
638 # else
639 # error "kstat_io_t does not have the required members"
640 # endif
641 static kstat_io_t kio;
642 int i;
644 if (kc == NULL)
645 return (-1);
647 for (i = 0; i < numdisk; i++)
648 {
649 if (kstat_read (kc, ksp[i], &kio) == -1)
650 continue;
652 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
653 {
654 disk_submit (ksp[i]->ks_name, "disk_octets",
655 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
656 disk_submit (ksp[i]->ks_name, "disk_ops",
657 kio.KIO_ROPS, kio.KIO_WOPS);
658 /* FIXME: Convert this to microseconds if necessary */
659 disk_submit (ksp[i]->ks_name, "disk_time",
660 kio.KIO_RTIME, kio.KIO_WTIME);
661 }
662 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
663 {
664 disk_submit (ksp[i]->ks_name, "disk_octets",
665 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
666 disk_submit (ksp[i]->ks_name, "disk_ops",
667 kio.KIO_ROPS, kio.KIO_WOPS);
668 }
669 }
670 /* #endif defined(HAVE_LIBKSTAT) */
672 #elif defined(HAVE_LIBSTATGRAB)
673 sg_disk_io_stats *ds;
674 int disks, counter;
675 char name[DATA_MAX_NAME_LEN];
677 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
678 return (0);
680 for (counter=0; counter < disks; counter++) {
681 strncpy(name, ds->disk_name, sizeof(name));
682 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
683 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
684 ds++;
685 }
686 #endif /* defined(HAVE_LIBSTATGRAB) */
688 return (0);
689 } /* int disk_read */
691 void module_register (void)
692 {
693 plugin_register_config ("disk", disk_config,
694 config_keys, config_keys_num);
695 plugin_register_init ("disk", disk_init);
696 plugin_register_read ("disk", disk_read);
697 } /* void module_register */