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 (IS_TRUE (value))
136 invert = 0;
137 ignorelist_set_invert (ignorelist, invert);
138 }
139 else
140 {
141 return (-1);
142 }
144 return (0);
145 } /* int disk_config */
147 static int disk_init (void)
148 {
149 #if HAVE_IOKIT_IOKITLIB_H
150 kern_return_t status;
152 if (io_master_port != MACH_PORT_NULL)
153 {
154 mach_port_deallocate (mach_task_self (),
155 io_master_port);
156 io_master_port = MACH_PORT_NULL;
157 }
159 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
160 if (status != kIOReturnSuccess)
161 {
162 ERROR ("IOMasterPort failed: %s",
163 mach_error_string (status));
164 io_master_port = MACH_PORT_NULL;
165 return (-1);
166 }
167 /* #endif HAVE_IOKIT_IOKITLIB_H */
169 #elif KERNEL_LINUX
170 /* do nothing */
171 /* #endif KERNEL_LINUX */
173 #elif HAVE_LIBKSTAT
174 kstat_t *ksp_chain;
176 numdisk = 0;
178 if (kc == NULL)
179 return (-1);
181 for (numdisk = 0, ksp_chain = kc->kc_chain;
182 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
183 ksp_chain = ksp_chain->ks_next)
184 {
185 if (strncmp (ksp_chain->ks_class, "disk", 4)
186 && strncmp (ksp_chain->ks_class, "partition", 9))
187 continue;
188 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
189 continue;
190 ksp[numdisk++] = ksp_chain;
191 }
192 #endif /* HAVE_LIBKSTAT */
194 return (0);
195 } /* int disk_init */
197 static void disk_submit (const char *plugin_instance,
198 const char *type,
199 counter_t read, counter_t write)
200 {
201 value_t values[2];
202 value_list_t vl = VALUE_LIST_INIT;
204 /* Both `ignorelist' and `plugin_instance' may be NULL. */
205 if (ignorelist_match (ignorelist, plugin_instance) != 0)
206 return;
208 values[0].counter = read;
209 values[1].counter = write;
211 vl.values = values;
212 vl.values_len = 2;
213 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
214 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
215 sstrncpy (vl.plugin_instance, plugin_instance,
216 sizeof (vl.plugin_instance));
217 sstrncpy (vl.type, type, sizeof (vl.type));
219 plugin_dispatch_values (&vl);
220 } /* void disk_submit */
222 #if HAVE_IOKIT_IOKITLIB_H
223 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
224 {
225 signed long long val_int;
226 CFNumberRef val_obj;
227 CFStringRef key_obj;
229 /* `key_obj' needs to be released. */
230 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
231 kCFStringEncodingASCII);
232 if (key_obj == NULL)
233 {
234 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
235 return (-1LL);
236 }
238 /* get => we don't need to release (== free) the object */
239 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
241 CFRelease (key_obj);
243 if (val_obj == NULL)
244 {
245 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
246 return (-1LL);
247 }
249 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
250 {
251 DEBUG ("CFNumberGetValue (%s) failed.", key);
252 return (-1LL);
253 }
255 return (val_int);
256 }
257 #endif /* HAVE_IOKIT_IOKITLIB_H */
259 static int disk_read (void)
260 {
261 #if HAVE_IOKIT_IOKITLIB_H
262 io_registry_entry_t disk;
263 io_registry_entry_t disk_child;
264 io_iterator_t disk_list;
265 CFDictionaryRef props_dict;
266 CFDictionaryRef stats_dict;
267 CFDictionaryRef child_dict;
268 kern_return_t status;
270 signed long long read_ops;
271 signed long long read_byt;
272 signed long long read_tme;
273 signed long long write_ops;
274 signed long long write_byt;
275 signed long long write_tme;
277 int disk_major;
278 int disk_minor;
279 char disk_name[64];
281 /* Get the list of all disk objects. */
282 if (IOServiceGetMatchingServices (io_master_port,
283 IOServiceMatching (kIOBlockStorageDriverClass),
284 &disk_list) != kIOReturnSuccess)
285 {
286 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
287 return (-1);
288 }
290 while ((disk = IOIteratorNext (disk_list)) != 0)
291 {
292 props_dict = NULL;
293 stats_dict = NULL;
294 child_dict = NULL;
296 /* `disk_child' must be released */
297 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
298 != kIOReturnSuccess)
299 {
300 /* This fails for example for DVD/CD drives.. */
301 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
302 IOObjectRelease (disk);
303 continue;
304 }
306 /* We create `props_dict' => we need to release it later */
307 if (IORegistryEntryCreateCFProperties (disk,
308 (CFMutableDictionaryRef *) &props_dict,
309 kCFAllocatorDefault,
310 kNilOptions)
311 != kIOReturnSuccess)
312 {
313 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
314 IOObjectRelease (disk_child);
315 IOObjectRelease (disk);
316 continue;
317 }
319 if (props_dict == NULL)
320 {
321 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
322 IOObjectRelease (disk_child);
323 IOObjectRelease (disk);
324 continue;
325 }
327 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
328 CFSTR (kIOBlockStorageDriverStatisticsKey));
330 if (stats_dict == NULL)
331 {
332 DEBUG ("CFDictionaryGetValue (%s) failed.",
333 kIOBlockStorageDriverStatisticsKey);
334 CFRelease (props_dict);
335 IOObjectRelease (disk_child);
336 IOObjectRelease (disk);
337 continue;
338 }
340 if (IORegistryEntryCreateCFProperties (disk_child,
341 (CFMutableDictionaryRef *) &child_dict,
342 kCFAllocatorDefault,
343 kNilOptions)
344 != kIOReturnSuccess)
345 {
346 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
347 IOObjectRelease (disk_child);
348 CFRelease (props_dict);
349 IOObjectRelease (disk);
350 continue;
351 }
353 /* kIOBSDNameKey */
354 disk_major = (int) dict_get_value (child_dict,
355 kIOBSDMajorKey);
356 disk_minor = (int) dict_get_value (child_dict,
357 kIOBSDMinorKey);
358 read_ops = dict_get_value (stats_dict,
359 kIOBlockStorageDriverStatisticsReadsKey);
360 read_byt = dict_get_value (stats_dict,
361 kIOBlockStorageDriverStatisticsBytesReadKey);
362 read_tme = dict_get_value (stats_dict,
363 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
364 write_ops = dict_get_value (stats_dict,
365 kIOBlockStorageDriverStatisticsWritesKey);
366 write_byt = dict_get_value (stats_dict,
367 kIOBlockStorageDriverStatisticsBytesWrittenKey);
368 /* This property describes the number of nanoseconds spent
369 * performing writes since the block storage driver was
370 * instantiated. It is one of the statistic entries listed
371 * under the top-level kIOBlockStorageDriverStatisticsKey
372 * property table. It has an OSNumber value. */
373 write_tme = dict_get_value (stats_dict,
374 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
376 if (ssnprintf (disk_name, sizeof (disk_name),
377 "%i-%i", disk_major, disk_minor) >= sizeof (disk_name))
378 {
379 DEBUG ("snprintf (major, minor) failed.");
380 CFRelease (child_dict);
381 IOObjectRelease (disk_child);
382 CFRelease (props_dict);
383 IOObjectRelease (disk);
384 continue;
385 }
386 DEBUG ("disk_name = %s", disk_name);
388 if ((read_byt != -1LL) || (write_byt != -1LL))
389 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
390 if ((read_ops != -1LL) || (write_ops != -1LL))
391 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
392 if ((read_tme != -1LL) || (write_tme != -1LL))
393 disk_submit (disk_name, "disk_time",
394 read_tme / 1000,
395 write_tme / 1000);
397 CFRelease (child_dict);
398 IOObjectRelease (disk_child);
399 CFRelease (props_dict);
400 IOObjectRelease (disk);
401 }
402 IOObjectRelease (disk_list);
403 /* #endif HAVE_IOKIT_IOKITLIB_H */
405 #elif KERNEL_LINUX
406 FILE *fh;
407 char buffer[1024];
409 char *fields[32];
410 int numfields;
411 int fieldshift = 0;
413 int major = 0;
414 int minor = 0;
416 counter_t read_sectors = 0;
417 counter_t write_sectors = 0;
419 counter_t read_ops = 0;
420 counter_t read_merged = 0;
421 counter_t read_time = 0;
422 counter_t write_ops = 0;
423 counter_t write_merged = 0;
424 counter_t write_time = 0;
425 int is_disk = 0;
427 diskstats_t *ds, *pre_ds;
429 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
430 {
431 fh = fopen ("/proc/partitions", "r");
432 if (fh == NULL)
433 {
434 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
435 return (-1);
436 }
438 /* Kernel is 2.4.* */
439 fieldshift = 1;
440 }
442 while (fgets (buffer, sizeof (buffer), fh) != NULL)
443 {
444 char *disk_name;
446 numfields = strsplit (buffer, fields, 32);
448 if ((numfields != (14 + fieldshift)) && (numfields != 7))
449 continue;
451 major = atoll (fields[0]);
452 minor = atoll (fields[1]);
454 disk_name = fields[2 + fieldshift];
456 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
457 if (strcmp (disk_name, ds->name) == 0)
458 break;
460 if (ds == NULL)
461 {
462 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
463 continue;
465 if ((ds->name = strdup (disk_name)) == NULL)
466 {
467 free (ds);
468 continue;
469 }
471 if (pre_ds == NULL)
472 disklist = ds;
473 else
474 pre_ds->next = ds;
475 }
477 is_disk = 0;
478 if (numfields == 7)
479 {
480 /* Kernel 2.6, Partition */
481 read_ops = atoll (fields[3]);
482 read_sectors = atoll (fields[4]);
483 write_ops = atoll (fields[5]);
484 write_sectors = atoll (fields[6]);
485 }
486 else if (numfields == (14 + fieldshift))
487 {
488 read_ops = atoll (fields[3 + fieldshift]);
489 write_ops = atoll (fields[7 + fieldshift]);
491 read_sectors = atoll (fields[5 + fieldshift]);
492 write_sectors = atoll (fields[9 + fieldshift]);
494 if ((fieldshift == 0) || (minor == 0))
495 {
496 is_disk = 1;
497 read_merged = atoll (fields[4 + fieldshift]);
498 read_time = atoll (fields[6 + fieldshift]);
499 write_merged = atoll (fields[8 + fieldshift]);
500 write_time = atoll (fields[10+ fieldshift]);
501 }
502 }
503 else
504 {
505 DEBUG ("numfields = %i; => unknown file format.", numfields);
506 continue;
507 }
509 {
510 counter_t diff_read_sectors;
511 counter_t diff_write_sectors;
513 /* If the counter wraps around, it's only 32 bits.. */
514 if (read_sectors < ds->read_sectors)
515 diff_read_sectors = 1 + read_sectors
516 + (UINT_MAX - ds->read_sectors);
517 else
518 diff_read_sectors = read_sectors - ds->read_sectors;
519 if (write_sectors < ds->write_sectors)
520 diff_write_sectors = 1 + write_sectors
521 + (UINT_MAX - ds->write_sectors);
522 else
523 diff_write_sectors = write_sectors - ds->write_sectors;
525 ds->read_bytes += 512 * diff_read_sectors;
526 ds->write_bytes += 512 * diff_write_sectors;
527 ds->read_sectors = read_sectors;
528 ds->write_sectors = write_sectors;
529 }
531 /* Calculate the average time an io-op needs to complete */
532 if (is_disk)
533 {
534 counter_t diff_read_ops;
535 counter_t diff_write_ops;
536 counter_t diff_read_time;
537 counter_t diff_write_time;
539 if (read_ops < ds->read_ops)
540 diff_read_ops = 1 + read_ops
541 + (UINT_MAX - ds->read_ops);
542 else
543 diff_read_ops = read_ops - ds->read_ops;
544 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
545 "ds->read_ops = %llu; diff_read_ops = %llu;",
546 disk_name,
547 read_ops, ds->read_ops, diff_read_ops);
549 if (write_ops < ds->write_ops)
550 diff_write_ops = 1 + write_ops
551 + (UINT_MAX - ds->write_ops);
552 else
553 diff_write_ops = write_ops - ds->write_ops;
555 if (read_time < ds->read_time)
556 diff_read_time = 1 + read_time
557 + (UINT_MAX - ds->read_time);
558 else
559 diff_read_time = read_time - ds->read_time;
561 if (write_time < ds->write_time)
562 diff_write_time = 1 + write_time
563 + (UINT_MAX - ds->write_time);
564 else
565 diff_write_time = write_time - ds->write_time;
567 if (diff_read_ops != 0)
568 ds->avg_read_time += (diff_read_time
569 + (diff_read_ops / 2))
570 / diff_read_ops;
571 if (diff_write_ops != 0)
572 ds->avg_write_time += (diff_write_time
573 + (diff_write_ops / 2))
574 / diff_write_ops;
576 ds->read_ops = read_ops;
577 ds->read_time = read_time;
578 ds->write_ops = write_ops;
579 ds->write_time = write_time;
580 } /* if (is_disk) */
582 /* Don't write to the RRDs if we've just started.. */
583 ds->poll_count++;
584 if (ds->poll_count <= 2)
585 {
586 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
587 "(min_poll_count = 2); => Not writing.",
588 ds->poll_count);
589 continue;
590 }
592 if ((read_ops == 0) && (write_ops == 0))
593 {
594 DEBUG ("disk plugin: ((read_ops == 0) && "
595 "(write_ops == 0)); => Not writing.");
596 continue;
597 }
599 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
600 disk_submit (disk_name, "disk_octets",
601 ds->read_bytes, ds->write_bytes);
603 if ((ds->read_ops != 0) || (ds->write_ops != 0))
604 disk_submit (disk_name, "disk_ops",
605 read_ops, write_ops);
607 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
608 disk_submit (disk_name, "disk_time",
609 ds->avg_read_time, ds->avg_write_time);
611 if (is_disk)
612 {
613 disk_submit (disk_name, "disk_merged",
614 read_merged, write_merged);
615 } /* if (is_disk) */
616 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
618 fclose (fh);
619 /* #endif defined(KERNEL_LINUX) */
621 #elif HAVE_LIBKSTAT
622 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
623 # define KIO_ROCTETS reads
624 # define KIO_WOCTETS writes
625 # define KIO_ROPS nreads
626 # define KIO_WOPS nwrites
627 # define KIO_RTIME rtime
628 # define KIO_WTIME wtime
629 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
630 # define KIO_ROCTETS nread
631 # define KIO_WOCTETS nwritten
632 # define KIO_ROPS reads
633 # define KIO_WOPS writes
634 # define KIO_RTIME rtime
635 # define KIO_WTIME wtime
636 # else
637 # error "kstat_io_t does not have the required members"
638 # endif
639 static kstat_io_t kio;
640 int i;
642 if (kc == NULL)
643 return (-1);
645 for (i = 0; i < numdisk; i++)
646 {
647 if (kstat_read (kc, ksp[i], &kio) == -1)
648 continue;
650 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
651 {
652 disk_submit (ksp[i]->ks_name, "disk_octets",
653 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
654 disk_submit (ksp[i]->ks_name, "disk_ops",
655 kio.KIO_ROPS, kio.KIO_WOPS);
656 /* FIXME: Convert this to microseconds if necessary */
657 disk_submit (ksp[i]->ks_name, "disk_time",
658 kio.KIO_RTIME, kio.KIO_WTIME);
659 }
660 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
661 {
662 disk_submit (ksp[i]->ks_name, "disk_octets",
663 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
664 disk_submit (ksp[i]->ks_name, "disk_ops",
665 kio.KIO_ROPS, kio.KIO_WOPS);
666 }
667 }
668 /* #endif defined(HAVE_LIBKSTAT) */
670 #elif defined(HAVE_LIBSTATGRAB)
671 sg_disk_io_stats *ds;
672 int disks, counter;
673 char name[DATA_MAX_NAME_LEN];
675 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
676 return (0);
678 for (counter=0; counter < disks; counter++) {
679 strncpy(name, ds->disk_name, sizeof(name));
680 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
681 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
682 ds++;
683 }
684 #endif /* defined(HAVE_LIBSTATGRAB) */
686 return (0);
687 } /* int disk_read */
689 void module_register (void)
690 {
691 plugin_register_config ("disk", disk_config,
692 config_keys, config_keys_num);
693 plugin_register_init ("disk", disk_init);
694 plugin_register_read ("disk", disk_read);
695 } /* void module_register */