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
62 static mach_port_t io_master_port = MACH_PORT_NULL;
63 /* #endif HAVE_IOKIT_IOKITLIB_H */
65 #elif KERNEL_LINUX
66 typedef struct diskstats
67 {
68 char *name;
70 /* This overflows in roughly 1361 years */
71 unsigned int poll_count;
73 counter_t read_sectors;
74 counter_t write_sectors;
76 counter_t read_bytes;
77 counter_t write_bytes;
79 counter_t read_ops;
80 counter_t write_ops;
81 counter_t read_time;
82 counter_t write_time;
84 counter_t avg_read_time;
85 counter_t avg_write_time;
87 struct diskstats *next;
88 } diskstats_t;
90 static diskstats_t *disklist;
91 /* #endif KERNEL_LINUX */
93 #elif HAVE_LIBKSTAT
94 #define MAX_NUMDISK 256
95 extern kstat_ctl_t *kc;
96 static kstat_t *ksp[MAX_NUMDISK];
97 static int numdisk = 0;
98 /* #endif HAVE_LIBKSTAT */
100 #else
101 # error "No applicable input method."
102 #endif
104 static int disk_init (void)
105 {
106 #if HAVE_IOKIT_IOKITLIB_H
107 kern_return_t status;
109 if (io_master_port != MACH_PORT_NULL)
110 {
111 mach_port_deallocate (mach_task_self (),
112 io_master_port);
113 io_master_port = MACH_PORT_NULL;
114 }
116 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
117 if (status != kIOReturnSuccess)
118 {
119 ERROR ("IOMasterPort failed: %s",
120 mach_error_string (status));
121 io_master_port = MACH_PORT_NULL;
122 return (-1);
123 }
124 /* #endif HAVE_IOKIT_IOKITLIB_H */
126 #elif KERNEL_LINUX
127 /* do nothing */
128 /* #endif KERNEL_LINUX */
130 #elif HAVE_LIBKSTAT
131 kstat_t *ksp_chain;
133 numdisk = 0;
135 if (kc == NULL)
136 return (-1);
138 for (numdisk = 0, ksp_chain = kc->kc_chain;
139 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
140 ksp_chain = ksp_chain->ks_next)
141 {
142 if (strncmp (ksp_chain->ks_class, "disk", 4)
143 && strncmp (ksp_chain->ks_class, "partition", 9))
144 continue;
145 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
146 continue;
147 ksp[numdisk++] = ksp_chain;
148 }
149 #endif /* HAVE_LIBKSTAT */
151 return (0);
152 } /* int disk_init */
154 static void disk_submit (const char *plugin_instance,
155 const char *type,
156 counter_t read, counter_t write)
157 {
158 value_t values[2];
159 value_list_t vl = VALUE_LIST_INIT;
161 values[0].counter = read;
162 values[1].counter = write;
164 vl.values = values;
165 vl.values_len = 2;
166 vl.time = time (NULL);
167 strcpy (vl.host, hostname_g);
168 strcpy (vl.plugin, "disk");
169 strncpy (vl.plugin_instance, plugin_instance,
170 sizeof (vl.plugin_instance));
172 plugin_dispatch_values (type, &vl);
173 } /* void disk_submit */
175 #if HAVE_IOKIT_IOKITLIB_H
176 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
177 {
178 signed long long val_int;
179 CFNumberRef val_obj;
180 CFStringRef key_obj;
182 /* `key_obj' needs to be released. */
183 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
184 kCFStringEncodingASCII);
185 if (key_obj == NULL)
186 {
187 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
188 return (-1LL);
189 }
191 /* get => we don't need to release (== free) the object */
192 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
194 CFRelease (key_obj);
196 if (val_obj == NULL)
197 {
198 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
199 return (-1LL);
200 }
202 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
203 {
204 DEBUG ("CFNumberGetValue (%s) failed.", key);
205 return (-1LL);
206 }
208 return (val_int);
209 }
210 #endif /* HAVE_IOKIT_IOKITLIB_H */
212 static int disk_read (void)
213 {
214 #if HAVE_IOKIT_IOKITLIB_H
215 io_registry_entry_t disk;
216 io_registry_entry_t disk_child;
217 io_iterator_t disk_list;
218 CFDictionaryRef props_dict;
219 CFDictionaryRef stats_dict;
220 CFDictionaryRef child_dict;
221 kern_return_t status;
223 signed long long read_ops;
224 signed long long read_byt;
225 signed long long read_tme;
226 signed long long write_ops;
227 signed long long write_byt;
228 signed long long write_tme;
230 int disk_major;
231 int disk_minor;
232 char disk_name[64];
234 static complain_t complain_obj;
236 /* Get the list of all disk objects. */
237 if (IOServiceGetMatchingServices (io_master_port,
238 IOServiceMatching (kIOBlockStorageDriverClass),
239 &disk_list) != kIOReturnSuccess)
240 {
241 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
242 "IOServiceGetMatchingServices failed.");
243 return (-1);
244 }
245 else if (complain_obj.interval != 0)
246 {
247 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
248 "IOServiceGetMatchingServices succeeded.");
249 }
251 while ((disk = IOIteratorNext (disk_list)) != 0)
252 {
253 props_dict = NULL;
254 stats_dict = NULL;
255 child_dict = NULL;
257 /* `disk_child' must be released */
258 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
259 != kIOReturnSuccess)
260 {
261 /* This fails for example for DVD/CD drives.. */
262 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
263 IOObjectRelease (disk);
264 continue;
265 }
267 /* We create `props_dict' => we need to release it later */
268 if (IORegistryEntryCreateCFProperties (disk,
269 (CFMutableDictionaryRef *) &props_dict,
270 kCFAllocatorDefault,
271 kNilOptions)
272 != kIOReturnSuccess)
273 {
274 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
275 IOObjectRelease (disk_child);
276 IOObjectRelease (disk);
277 continue;
278 }
280 if (props_dict == NULL)
281 {
282 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
283 IOObjectRelease (disk_child);
284 IOObjectRelease (disk);
285 continue;
286 }
288 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
289 CFSTR (kIOBlockStorageDriverStatisticsKey));
291 if (stats_dict == NULL)
292 {
293 DEBUG ("CFDictionaryGetValue (%s) failed.",
294 kIOBlockStorageDriverStatisticsKey);
295 CFRelease (props_dict);
296 IOObjectRelease (disk_child);
297 IOObjectRelease (disk);
298 continue;
299 }
301 if (IORegistryEntryCreateCFProperties (disk_child,
302 (CFMutableDictionaryRef *) &child_dict,
303 kCFAllocatorDefault,
304 kNilOptions)
305 != kIOReturnSuccess)
306 {
307 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
308 IOObjectRelease (disk_child);
309 CFRelease (props_dict);
310 IOObjectRelease (disk);
311 continue;
312 }
314 /* kIOBSDNameKey */
315 disk_major = (int) dict_get_value (child_dict,
316 kIOBSDMajorKey);
317 disk_minor = (int) dict_get_value (child_dict,
318 kIOBSDMinorKey);
319 read_ops = dict_get_value (stats_dict,
320 kIOBlockStorageDriverStatisticsReadsKey);
321 read_byt = dict_get_value (stats_dict,
322 kIOBlockStorageDriverStatisticsBytesReadKey);
323 read_tme = dict_get_value (stats_dict,
324 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
325 write_ops = dict_get_value (stats_dict,
326 kIOBlockStorageDriverStatisticsWritesKey);
327 write_byt = dict_get_value (stats_dict,
328 kIOBlockStorageDriverStatisticsBytesWrittenKey);
329 /* This property describes the number of nanoseconds spent
330 * performing writes since the block storage driver was
331 * instantiated. It is one of the statistic entries listed
332 * under the top-level kIOBlockStorageDriverStatisticsKey
333 * property table. It has an OSNumber value. */
334 write_tme = dict_get_value (stats_dict,
335 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
337 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
338 {
339 DEBUG ("snprintf (major, minor) failed.");
340 CFRelease (child_dict);
341 IOObjectRelease (disk_child);
342 CFRelease (props_dict);
343 IOObjectRelease (disk);
344 continue;
345 }
346 DEBUG ("disk_name = %s", disk_name);
348 if ((read_byt != -1LL) || (write_byt != -1LL))
349 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
350 if ((read_ops != -1LL) || (write_ops != -1LL))
351 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
352 if ((read_tme != -1LL) || (write_tme != -1LL))
353 disk_submit (disk_name, "disk_time",
354 read_tme / 1000,
355 write_tme / 1000);
357 CFRelease (child_dict);
358 IOObjectRelease (disk_child);
359 CFRelease (props_dict);
360 IOObjectRelease (disk);
361 }
362 IOObjectRelease (disk_list);
363 /* #endif HAVE_IOKIT_IOKITLIB_H */
365 #elif KERNEL_LINUX
366 FILE *fh;
367 char buffer[1024];
369 char *fields[32];
370 int numfields;
371 int fieldshift = 0;
373 int major = 0;
374 int minor = 0;
376 counter_t read_sectors = 0;
377 counter_t write_sectors = 0;
379 counter_t read_ops = 0;
380 counter_t read_merged = 0;
381 counter_t read_time = 0;
382 counter_t write_ops = 0;
383 counter_t write_merged = 0;
384 counter_t write_time = 0;
385 int is_disk = 0;
387 diskstats_t *ds, *pre_ds;
389 static complain_t complain_obj;
391 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
392 {
393 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
394 {
395 plugin_complain (LOG_ERR, &complain_obj,
396 "disk plugin: Failed to open /proc/"
397 "{diskstats,partitions}.");
398 return (-1);
399 }
401 /* Kernel is 2.4.* */
402 fieldshift = 1;
403 }
405 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
406 "Succeeded to open /proc/{diskstats,partitions}.");
408 while (fgets (buffer, sizeof (buffer), fh) != NULL)
409 {
410 char *disk_name;
412 numfields = strsplit (buffer, fields, 32);
414 if ((numfields != (14 + fieldshift)) && (numfields != 7))
415 continue;
417 major = atoll (fields[0]);
418 minor = atoll (fields[1]);
420 disk_name = fields[2];
422 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
423 if (strcmp (disk_name, ds->name) == 0)
424 break;
426 if (ds == NULL)
427 {
428 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
429 continue;
431 if ((ds->name = strdup (disk_name)) == NULL)
432 {
433 free (ds);
434 continue;
435 }
437 if (pre_ds == NULL)
438 disklist = ds;
439 else
440 pre_ds->next = ds;
441 }
443 is_disk = 0;
444 if (numfields == 7)
445 {
446 /* Kernel 2.6, Partition */
447 read_ops = atoll (fields[3]);
448 read_sectors = atoll (fields[4]);
449 write_ops = atoll (fields[5]);
450 write_sectors = atoll (fields[6]);
451 }
452 else if (numfields == (14 + fieldshift))
453 {
454 read_ops = atoll (fields[3 + fieldshift]);
455 write_ops = atoll (fields[7 + fieldshift]);
457 read_sectors = atoll (fields[5 + fieldshift]);
458 write_sectors = atoll (fields[9 + fieldshift]);
460 if ((fieldshift == 0) || (minor == 0))
461 {
462 is_disk = 1;
463 read_merged = atoll (fields[4 + fieldshift]);
464 read_time = atoll (fields[6 + fieldshift]);
465 write_merged = atoll (fields[8 + fieldshift]);
466 write_time = atoll (fields[10+ fieldshift]);
467 }
468 }
469 else
470 {
471 DEBUG ("numfields = %i; => unknown file format.", numfields);
472 continue;
473 }
475 {
476 counter_t diff_read_sectors;
477 counter_t diff_write_sectors;
479 /* If the counter wraps around, it's only 32 bits.. */
480 if (read_sectors < ds->read_sectors)
481 diff_read_sectors = 1 + read_sectors
482 + (UINT_MAX - ds->read_sectors);
483 else
484 diff_read_sectors = read_sectors - ds->read_sectors;
485 if (write_sectors < ds->write_sectors)
486 diff_write_sectors = 1 + write_sectors
487 + (UINT_MAX - ds->write_sectors);
488 else
489 diff_write_sectors = write_sectors - ds->write_sectors;
491 ds->read_bytes += 512 * diff_read_sectors;
492 ds->write_bytes += 512 * diff_write_sectors;
493 ds->read_sectors = read_sectors;
494 ds->write_sectors = write_sectors;
495 }
497 /* Calculate the average time an io-op needs to complete */
498 if (is_disk)
499 {
500 counter_t diff_read_ops;
501 counter_t diff_write_ops;
502 counter_t diff_read_time;
503 counter_t diff_write_time;
505 if (read_ops < ds->read_ops)
506 diff_read_ops = 1 + read_ops
507 + (UINT_MAX - ds->read_ops);
508 else
509 diff_read_ops = read_ops - ds->read_ops;
510 DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
511 "ds->read_ops = %llu; diff_read_ops = %llu;",
512 disk_name,
513 read_ops, ds->read_ops, diff_read_ops);
515 if (write_ops < ds->write_ops)
516 diff_write_ops = 1 + write_ops
517 + (UINT_MAX - ds->write_ops);
518 else
519 diff_write_ops = write_ops - ds->write_ops;
521 if (read_time < ds->read_time)
522 diff_read_time = 1 + read_time
523 + (UINT_MAX - ds->read_time);
524 else
525 diff_read_time = read_time - ds->read_time;
527 if (write_time < ds->write_time)
528 diff_write_time = 1 + write_time
529 + (UINT_MAX - ds->write_time);
530 else
531 diff_write_time = write_time - ds->write_time;
533 if (diff_read_ops != 0)
534 ds->avg_read_time += (diff_read_time
535 + (diff_read_ops / 2))
536 / diff_read_ops;
537 if (diff_write_ops != 0)
538 ds->avg_write_time += (diff_write_time
539 + (diff_write_ops / 2))
540 / diff_write_ops;
542 ds->read_ops = read_ops;
543 ds->read_time = read_time;
544 ds->write_ops = write_ops;
545 ds->write_time = write_time;
546 } /* if (is_disk) */
548 /* Don't write to the RRDs if we've just started.. */
549 ds->poll_count++;
550 if (ds->poll_count <= 2)
551 {
552 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
553 "(min_poll_count = 2); => Not writing.",
554 ds->poll_count);
555 continue;
556 }
558 if ((read_ops == 0) && (write_ops == 0))
559 {
560 DEBUG ("disk plugin: ((read_ops == 0) && "
561 "(write_ops == 0)); => Not writing.");
562 continue;
563 }
565 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
566 disk_submit (disk_name, "disk_octets",
567 ds->read_bytes, ds->write_bytes);
569 if ((ds->read_ops != 0) || (ds->write_ops != 0))
570 disk_submit (disk_name, "disk_ops",
571 read_ops, write_ops);
573 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
574 disk_submit (disk_name, "disk_time",
575 ds->avg_read_time, ds->avg_write_time);
577 if (is_disk)
578 {
579 if ((read_merged != -1LL) || (write_merged != -1LL))
580 disk_submit (disk_name, "disk_merged",
581 read_merged, write_merged);
582 } /* if (is_disk) */
583 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
585 fclose (fh);
586 /* #endif defined(KERNEL_LINUX) */
588 #elif HAVE_LIBKSTAT
589 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
590 # define KIO_ROCTETS reads
591 # define KIO_WOCTETS writes
592 # define KIO_ROPS nreads
593 # define KIO_WOPS nwrites
594 # define KIO_RTIME rtime
595 # define KIO_WTIME wtime
596 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
597 # define KIO_ROCTETS nread
598 # define KIO_WOCTETS nwritten
599 # define KIO_ROPS reads
600 # define KIO_WOPS writes
601 # define KIO_RTIME rtime
602 # define KIO_WTIME wtime
603 # else
604 # error "kstat_io_t does not have the required members"
605 # endif
606 static kstat_io_t kio;
607 int i;
609 if (kc == NULL)
610 return (-1);
612 for (i = 0; i < numdisk; i++)
613 {
614 if (kstat_read (kc, ksp[i], &kio) == -1)
615 continue;
617 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
618 {
619 disk_submit (ksp[i]->ks_name, "disk_octets",
620 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
621 disk_submit (ksp[i]->ks_name, "disk_ops",
622 kio.KIO_ROPS, kio.KIO_WOPS);
623 /* FIXME: Convert this to microseconds if necessary */
624 disk_submit (ksp[i]->ks_name, "disk_time",
625 kio.KIO_RTIME, kio.KIO_WTIME);
626 }
627 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 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 }
634 }
635 #endif /* defined(HAVE_LIBKSTAT) */
637 return (0);
638 } /* int disk_read */
640 void module_register (void)
641 {
642 plugin_register_init ("disk", disk_init);
643 plugin_register_read ("disk", disk_read);
644 } /* void module_register */