5739f3697f6dce2bf29ec1ec995b05fa4aa672eb
1 /**
2 * collectd - src/disk.c
3 * Copyright (C) 2005-2007 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"
26 #if HAVE_MACH_MACH_TYPES_H
27 # include <mach/mach_types.h>
28 #endif
29 #if HAVE_MACH_MACH_INIT_H
30 # include <mach/mach_init.h>
31 #endif
32 #if HAVE_MACH_MACH_ERROR_H
33 # include <mach/mach_error.h>
34 #endif
35 #if HAVE_MACH_MACH_PORT_H
36 # include <mach/mach_port.h>
37 #endif
38 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
39 # include <CoreFoundation/CoreFoundation.h>
40 #endif
41 #if HAVE_IOKIT_IOKITLIB_H
42 # include <IOKit/IOKitLib.h>
43 #endif
44 #if HAVE_IOKIT_IOTYPES_H
45 # include <IOKit/IOTypes.h>
46 #endif
47 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
48 # include <IOKit/storage/IOBlockStorageDriver.h>
49 #endif
50 #if HAVE_IOKIT_IOBSD_H
51 # include <IOKit/IOBSD.h>
52 #endif
54 #if HAVE_LIMITS_H
55 # include <limits.h>
56 #endif
57 #ifndef UINT_MAX
58 # define UINT_MAX 4294967295U
59 #endif
61 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
62 # define DISK_HAVE_READ 1
63 #else
64 # define DISK_HAVE_READ 0
65 #endif
67 #if DISK_HAVE_READ
68 #if HAVE_IOKIT_IOKITLIB_H
69 static mach_port_t io_master_port = MACH_PORT_NULL;
70 /* #endif HAVE_IOKIT_IOKITLIB_H */
72 #elif KERNEL_LINUX
73 typedef struct diskstats
74 {
75 char *name;
77 /* This overflows in roughly 1361 years */
78 unsigned int poll_count;
80 counter_t read_sectors;
81 counter_t write_sectors;
83 counter_t read_bytes;
84 counter_t write_bytes;
86 counter_t read_ops;
87 counter_t write_ops;
88 counter_t read_time;
89 counter_t write_time;
91 counter_t avg_read_time;
92 counter_t avg_write_time;
94 struct diskstats *next;
95 } diskstats_t;
97 static diskstats_t *disklist;
98 /* #endif KERNEL_LINUX */
100 #elif HAVE_LIBKSTAT
101 #define MAX_NUMDISK 256
102 extern kstat_ctl_t *kc;
103 static kstat_t *ksp[MAX_NUMDISK];
104 static int numdisk = 0;
105 #endif /* HAVE_LIBKSTAT */
107 static int disk_init (void)
108 {
109 #if HAVE_IOKIT_IOKITLIB_H
110 kern_return_t status;
112 if (io_master_port != MACH_PORT_NULL)
113 {
114 mach_port_deallocate (mach_task_self (),
115 io_master_port);
116 io_master_port = MACH_PORT_NULL;
117 }
119 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
120 if (status != kIOReturnSuccess)
121 {
122 ERROR ("IOMasterPort failed: %s",
123 mach_error_string (status));
124 io_master_port = MACH_PORT_NULL;
125 return (-1);
126 }
127 /* #endif HAVE_IOKIT_IOKITLIB_H */
129 #elif KERNEL_LINUX
130 /* do nothing */
131 /* #endif KERNEL_LINUX */
133 #elif HAVE_LIBKSTAT
134 kstat_t *ksp_chain;
136 numdisk = 0;
138 if (kc == NULL)
139 return (-1);
141 for (numdisk = 0, ksp_chain = kc->kc_chain;
142 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
143 ksp_chain = ksp_chain->ks_next)
144 {
145 if (strncmp (ksp_chain->ks_class, "disk", 4)
146 && strncmp (ksp_chain->ks_class, "partition", 9))
147 continue;
148 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
149 continue;
150 ksp[numdisk++] = ksp_chain;
151 }
152 #endif /* HAVE_LIBKSTAT */
154 return (0);
155 } /* int disk_init */
157 static void disk_submit (const char *plugin_instance,
158 const char *type,
159 counter_t read, counter_t write)
160 {
161 value_t values[2];
162 value_list_t vl = VALUE_LIST_INIT;
164 values[0].counter = read;
165 values[1].counter = write;
167 vl.values = values;
168 vl.values_len = 2;
169 vl.time = time (NULL);
170 strcpy (vl.host, hostname_g);
171 strcpy (vl.plugin, "disk");
172 strncpy (vl.plugin_instance, plugin_instance,
173 sizeof (vl.plugin_instance));
175 plugin_dispatch_values (type, &vl);
176 } /* void disk_submit */
178 #if HAVE_IOKIT_IOKITLIB_H
179 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
180 {
181 signed long long val_int;
182 CFNumberRef val_obj;
183 CFStringRef key_obj;
185 /* `key_obj' needs to be released. */
186 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
187 kCFStringEncodingASCII);
188 if (key_obj == NULL)
189 {
190 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
191 return (-1LL);
192 }
194 /* get => we don't need to release (== free) the object */
195 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
197 CFRelease (key_obj);
199 if (val_obj == NULL)
200 {
201 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
202 return (-1LL);
203 }
205 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
206 {
207 DEBUG ("CFNumberGetValue (%s) failed.", key);
208 return (-1LL);
209 }
211 return (val_int);
212 }
213 #endif /* HAVE_IOKIT_IOKITLIB_H */
215 static int disk_read (void)
216 {
217 #if HAVE_IOKIT_IOKITLIB_H
218 io_registry_entry_t disk;
219 io_registry_entry_t disk_child;
220 io_iterator_t disk_list;
221 CFDictionaryRef props_dict;
222 CFDictionaryRef stats_dict;
223 CFDictionaryRef child_dict;
224 kern_return_t status;
226 signed long long read_ops;
227 signed long long read_byt;
228 signed long long read_tme;
229 signed long long write_ops;
230 signed long long write_byt;
231 signed long long write_tme;
233 int disk_major;
234 int disk_minor;
235 char disk_name[64];
237 static complain_t complain_obj;
239 /* Get the list of all disk objects. */
240 if (IOServiceGetMatchingServices (io_master_port,
241 IOServiceMatching (kIOBlockStorageDriverClass),
242 &disk_list) != kIOReturnSuccess)
243 {
244 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
245 "IOServiceGetMatchingServices failed.");
246 return (-1);
247 }
248 else if (complain_obj.interval != 0)
249 {
250 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
251 "IOServiceGetMatchingServices succeeded.");
252 }
254 while ((disk = IOIteratorNext (disk_list)) != 0)
255 {
256 props_dict = NULL;
257 stats_dict = NULL;
258 child_dict = NULL;
260 /* `disk_child' must be released */
261 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
262 != kIOReturnSuccess)
263 {
264 /* This fails for example for DVD/CD drives.. */
265 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
266 IOObjectRelease (disk);
267 continue;
268 }
270 /* We create `props_dict' => we need to release it later */
271 if (IORegistryEntryCreateCFProperties (disk,
272 (CFMutableDictionaryRef *) &props_dict,
273 kCFAllocatorDefault,
274 kNilOptions)
275 != kIOReturnSuccess)
276 {
277 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
278 IOObjectRelease (disk_child);
279 IOObjectRelease (disk);
280 continue;
281 }
283 if (props_dict == NULL)
284 {
285 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
286 IOObjectRelease (disk_child);
287 IOObjectRelease (disk);
288 continue;
289 }
291 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
292 CFSTR (kIOBlockStorageDriverStatisticsKey));
294 if (stats_dict == NULL)
295 {
296 DEBUG ("CFDictionaryGetValue (%s) failed.",
297 kIOBlockStorageDriverStatisticsKey);
298 CFRelease (props_dict);
299 IOObjectRelease (disk_child);
300 IOObjectRelease (disk);
301 continue;
302 }
304 if (IORegistryEntryCreateCFProperties (disk_child,
305 (CFMutableDictionaryRef *) &child_dict,
306 kCFAllocatorDefault,
307 kNilOptions)
308 != kIOReturnSuccess)
309 {
310 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
311 IOObjectRelease (disk_child);
312 CFRelease (props_dict);
313 IOObjectRelease (disk);
314 continue;
315 }
317 /* kIOBSDNameKey */
318 disk_major = (int) dict_get_value (child_dict,
319 kIOBSDMajorKey);
320 disk_minor = (int) dict_get_value (child_dict,
321 kIOBSDMinorKey);
322 read_ops = dict_get_value (stats_dict,
323 kIOBlockStorageDriverStatisticsReadsKey);
324 read_byt = dict_get_value (stats_dict,
325 kIOBlockStorageDriverStatisticsBytesReadKey);
326 read_tme = dict_get_value (stats_dict,
327 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
328 write_ops = dict_get_value (stats_dict,
329 kIOBlockStorageDriverStatisticsWritesKey);
330 write_byt = dict_get_value (stats_dict,
331 kIOBlockStorageDriverStatisticsBytesWrittenKey);
332 /* This property describes the number of nanoseconds spent
333 * performing writes since the block storage driver was
334 * instantiated. It is one of the statistic entries listed
335 * under the top-level kIOBlockStorageDriverStatisticsKey
336 * property table. It has an OSNumber value. */
337 write_tme = dict_get_value (stats_dict,
338 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
340 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
341 {
342 DEBUG ("snprintf (major, minor) failed.");
343 CFRelease (child_dict);
344 IOObjectRelease (disk_child);
345 CFRelease (props_dict);
346 IOObjectRelease (disk);
347 continue;
348 }
349 DEBUG ("disk_name = %s", disk_name);
351 if ((read_byt != -1LL) || (write_byt != -1LL))
352 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
353 if ((read_ops != -1LL) || (write_ops != -1LL))
354 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
355 if ((read_tme != -1LL) || (write_tme != -1LL))
356 disk_submit (disk_name, "disk_time",
357 read_tme / 1000,
358 write_tme / 1000);
360 CFRelease (child_dict);
361 IOObjectRelease (disk_child);
362 CFRelease (props_dict);
363 IOObjectRelease (disk);
364 }
365 IOObjectRelease (disk_list);
366 /* #endif HAVE_IOKIT_IOKITLIB_H */
368 #elif KERNEL_LINUX
369 FILE *fh;
370 char buffer[1024];
372 char *fields[32];
373 int numfields;
374 int fieldshift = 0;
376 int major = 0;
377 int minor = 0;
379 counter_t read_sectors = 0;
380 counter_t write_sectors = 0;
382 counter_t read_ops = 0;
383 counter_t read_merged = 0;
384 counter_t read_time = 0;
385 counter_t write_ops = 0;
386 counter_t write_merged = 0;
387 counter_t write_time = 0;
388 int is_disk = 0;
390 diskstats_t *ds, *pre_ds;
392 static complain_t complain_obj;
394 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
395 {
396 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
397 {
398 plugin_complain (LOG_ERR, &complain_obj,
399 "disk plugin: Failed to open /proc/"
400 "{diskstats,partitions}.");
401 return (-1);
402 }
404 /* Kernel is 2.4.* */
405 fieldshift = 1;
406 }
408 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
409 "Succeeded to open /proc/{diskstats,partitions}.");
411 while (fgets (buffer, sizeof (buffer), fh) != NULL)
412 {
413 char *disk_name;
415 numfields = strsplit (buffer, fields, 32);
417 if ((numfields != (14 + fieldshift)) && (numfields != 7))
418 continue;
420 major = atoll (fields[0]);
421 minor = atoll (fields[1]);
423 disk_name = fields[2];
424 { /* Strip slashes from disk_name */
425 int i;
426 for (i = 0; disk_name[i] != '\0'; i++)
427 if (disk_name[i] == '/')
428 disk_name[i] = '_';
429 }
432 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
433 if (strcmp (disk_name, ds->name) == 0)
434 break;
436 if (ds == NULL)
437 {
438 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
439 continue;
441 if ((ds->name = strdup (disk_name)) == NULL)
442 {
443 free (ds);
444 continue;
445 }
447 if (pre_ds == NULL)
448 disklist = ds;
449 else
450 pre_ds->next = ds;
451 }
453 is_disk = 0;
454 if (numfields == 7)
455 {
456 /* Kernel 2.6, Partition */
457 read_ops = atoll (fields[3]);
458 read_sectors = atoll (fields[4]);
459 write_ops = atoll (fields[5]);
460 write_sectors = atoll (fields[6]);
461 }
462 else if (numfields == (14 + fieldshift))
463 {
464 read_ops = atoll (fields[3 + fieldshift]);
465 write_ops = atoll (fields[7 + fieldshift]);
467 read_sectors = atoll (fields[5 + fieldshift]);
468 write_sectors = atoll (fields[9 + fieldshift]);
470 if ((fieldshift == 0) || (minor == 0))
471 {
472 is_disk = 1;
473 read_merged = atoll (fields[4 + fieldshift]);
474 read_time = atoll (fields[6 + fieldshift]);
475 write_merged = atoll (fields[8 + fieldshift]);
476 write_time = atoll (fields[10+ fieldshift]);
477 }
478 }
479 else
480 {
481 DEBUG ("numfields = %i; => unknown file format.", numfields);
482 continue;
483 }
485 {
486 counter_t diff_read_sectors;
487 counter_t diff_write_sectors;
489 /* If the counter wraps around, it's only 32 bits.. */
490 if (read_sectors < ds->read_sectors)
491 diff_read_sectors = 1 + read_sectors
492 + (UINT_MAX - ds->read_sectors);
493 else
494 diff_read_sectors = read_sectors - ds->read_sectors;
495 if (write_sectors < ds->write_sectors)
496 diff_write_sectors = 1 + write_sectors
497 + (UINT_MAX - ds->write_sectors);
498 else
499 diff_write_sectors = write_sectors - ds->write_sectors;
501 ds->read_bytes += 512 * diff_read_sectors;
502 ds->write_bytes += 512 * diff_write_sectors;
503 ds->read_sectors = read_sectors;
504 ds->write_sectors = write_sectors;
505 }
507 /* Calculate the average time an io-op needs to complete */
508 if (is_disk)
509 {
510 counter_t diff_read_ops;
511 counter_t diff_write_ops;
512 counter_t diff_read_time;
513 counter_t diff_write_time;
515 if (read_ops < ds->read_ops)
516 diff_read_ops = 1 + read_ops
517 + (UINT_MAX - ds->read_ops);
518 else
519 diff_read_ops = read_ops - ds->read_ops;
520 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
521 "ds->read_ops = %llu; diff_read_ops = %llu;",
522 disk_name,
523 read_ops, ds->read_ops, diff_read_ops);
525 if (write_ops < ds->write_ops)
526 diff_write_ops = 1 + write_ops
527 + (UINT_MAX - ds->write_ops);
528 else
529 diff_write_ops = write_ops - ds->write_ops;
531 if (read_time < ds->read_time)
532 diff_read_time = 1 + read_time
533 + (UINT_MAX - ds->read_time);
534 else
535 diff_read_time = read_time - ds->read_time;
537 if (write_time < ds->write_time)
538 diff_write_time = 1 + write_time
539 + (UINT_MAX - ds->write_time);
540 else
541 diff_write_time = write_time - ds->write_time;
543 if (diff_read_ops != 0)
544 ds->avg_read_time += (diff_read_time
545 + (diff_read_ops / 2))
546 / diff_read_ops;
547 if (diff_write_ops != 0)
548 ds->avg_write_time += (diff_write_time
549 + (diff_write_ops / 2))
550 / diff_write_ops;
552 ds->read_ops = read_ops;
553 ds->read_time = read_time;
554 ds->write_ops = write_ops;
555 ds->write_time = write_time;
556 } /* if (is_disk) */
558 /* Don't write to the RRDs if we've just started.. */
559 ds->poll_count++;
560 if (ds->poll_count <= 2)
561 {
562 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
563 "(min_poll_count = 2); => Not writing.",
564 ds->poll_count);
565 continue;
566 }
568 if ((read_ops == 0) && (write_ops == 0))
569 {
570 DEBUG ("disk plugin: ((read_ops == 0) && "
571 "(write_ops == 0)); => Not writing.");
572 continue;
573 }
575 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
576 disk_submit (disk_name, "disk_octets",
577 ds->read_bytes, ds->write_bytes);
579 if ((ds->read_ops != 0) || (ds->write_ops != 0))
580 disk_submit (disk_name, "disk_ops",
581 read_ops, write_ops);
583 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
584 disk_submit (disk_name, "disk_time",
585 ds->avg_read_time, ds->avg_write_time);
587 if (is_disk)
588 {
589 if ((read_merged != -1LL) || (write_merged != -1LL))
590 disk_submit (disk_name, "disk_merged",
591 read_merged, write_merged);
592 } /* if (is_disk) */
593 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
595 fclose (fh);
596 /* #endif defined(KERNEL_LINUX) */
598 #elif HAVE_LIBKSTAT
599 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
600 # define KIO_ROCTETS reads
601 # define KIO_WOCTETS writes
602 # define KIO_ROPS nreads
603 # define KIO_WOPS nwrites
604 # define KIO_RTIME rtime
605 # define KIO_WTIME wtime
606 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
607 # define KIO_ROCTETS nread
608 # define KIO_WOCTETS nwritten
609 # define KIO_ROPS reads
610 # define KIO_WOPS writes
611 # define KIO_RTIME rtime
612 # define KIO_WTIME wtime
613 # else
614 # error "kstat_io_t does not have the required members"
615 # endif
616 static kstat_io_t kio;
617 int i;
619 if (kc == NULL)
620 return (-1);
622 for (i = 0; i < numdisk; i++)
623 {
624 if (kstat_read (kc, ksp[i], &kio) == -1)
625 continue;
627 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
628 {
629 disk_submit (ksp[i]->ks_name, "disk_octets",
630 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
631 disk_submit (ksp[i]->ks_name, "disk_ops",
632 kio.KIO_ROPS, kio.KIO_WOPS);
633 /* FIXME: Convert this to microseconds if necessary */
634 disk_submit (ksp[i]->ks_name, "disk_time",
635 kio.KIO_RTIME, kio.KIO_WTIME);
636 }
637 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
638 {
639 disk_submit (ksp[i]->ks_name, "disk_octets",
640 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
641 disk_submit (ksp[i]->ks_name, "disk_ops",
642 kio.KIO_ROPS, kio.KIO_WOPS);
643 }
644 }
645 #endif /* defined(HAVE_LIBKSTAT) */
647 return (0);
648 } /* int disk_read */
649 #endif /* DISK_HAVE_READ */
651 void module_register (void)
652 {
653 #if DISK_HAVE_READ
654 plugin_register_init ("disk", disk_init);
655 plugin_register_read ("disk", disk_read);
656 #endif /* DISK_HAVE_READ */
657 } /* void module_register */