1 /**
2 * collectd - src/disk.c
3 * Copyright (C) 2005-2012 Florian octo Forster
4 * Copyright (C) 2009 Manuel Sanmartin
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Manuel Sanmartin
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "utils_ignorelist.h"
29 #if HAVE_MACH_MACH_TYPES_H
30 # include <mach/mach_types.h>
31 #endif
32 #if HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
34 #endif
35 #if HAVE_MACH_MACH_ERROR_H
36 # include <mach/mach_error.h>
37 #endif
38 #if HAVE_MACH_MACH_PORT_H
39 # include <mach/mach_port.h>
40 #endif
41 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
42 # include <CoreFoundation/CoreFoundation.h>
43 #endif
44 #if HAVE_IOKIT_IOKITLIB_H
45 # include <IOKit/IOKitLib.h>
46 #endif
47 #if HAVE_IOKIT_IOTYPES_H
48 # include <IOKit/IOTypes.h>
49 #endif
50 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
51 # include <IOKit/storage/IOBlockStorageDriver.h>
52 #endif
53 #if HAVE_IOKIT_IOBSD_H
54 # include <IOKit/IOBSD.h>
55 #endif
57 #if HAVE_LIMITS_H
58 # include <limits.h>
59 #endif
60 #ifndef UINT_MAX
61 # define UINT_MAX 4294967295U
62 #endif
64 #if HAVE_STATGRAB_H
65 # include <statgrab.h>
66 #endif
68 #if HAVE_PERFSTAT
69 # ifndef _AIXVERSION_610
70 # include <sys/systemcfg.h>
71 # endif
72 # include <sys/protosw.h>
73 # include <libperfstat.h>
74 #endif
76 #if HAVE_IOKIT_IOKITLIB_H
77 static mach_port_t io_master_port = MACH_PORT_NULL;
78 /* This defaults to false for backwards compatibility. Please fix in the next
79 * major version. */
80 static _Bool use_bsd_name = 0;
81 /* #endif HAVE_IOKIT_IOKITLIB_H */
83 #elif KERNEL_LINUX
84 typedef struct diskstats
85 {
86 char *name;
88 /* This overflows in roughly 1361 years */
89 unsigned int poll_count;
91 derive_t read_sectors;
92 derive_t write_sectors;
94 derive_t read_bytes;
95 derive_t write_bytes;
97 derive_t read_ops;
98 derive_t write_ops;
99 derive_t read_time;
100 derive_t write_time;
102 derive_t avg_read_time;
103 derive_t avg_write_time;
105 struct diskstats *next;
106 } diskstats_t;
108 static diskstats_t *disklist;
109 /* #endif KERNEL_LINUX */
111 #elif HAVE_LIBKSTAT
112 #define MAX_NUMDISK 1024
113 extern kstat_ctl_t *kc;
114 static kstat_t *ksp[MAX_NUMDISK];
115 static int numdisk = 0;
116 /* #endif HAVE_LIBKSTAT */
118 #elif defined(HAVE_LIBSTATGRAB)
119 /* #endif HAVE_LIBKSTATGRAB */
121 #elif HAVE_PERFSTAT
122 static perfstat_disk_t * stat_disk;
123 static int numdisk;
124 static int pnumdisk;
125 /* #endif HAVE_PERFSTAT */
127 #else
128 # error "No applicable input method."
129 #endif
131 #if HAVE_LIBUDEV
132 #include <libudev.h>
134 static char *conf_udev_name_attr = NULL;
135 static struct udev *handle_udev;
136 #endif
138 static const char *config_keys[] =
139 {
140 "Disk",
141 "UseBSDName",
142 "IgnoreSelected",
143 "UdevNameAttr"
144 };
145 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
147 static ignorelist_t *ignorelist = NULL;
149 static int disk_config (const char *key, const char *value)
150 {
151 if (ignorelist == NULL)
152 ignorelist = ignorelist_create (/* invert = */ 1);
153 if (ignorelist == NULL)
154 return (1);
156 if (strcasecmp ("Disk", key) == 0)
157 {
158 ignorelist_add (ignorelist, value);
159 }
160 else if (strcasecmp ("IgnoreSelected", key) == 0)
161 {
162 int invert = 1;
163 if (IS_TRUE (value))
164 invert = 0;
165 ignorelist_set_invert (ignorelist, invert);
166 }
167 else if (strcasecmp ("UseBSDName", key) == 0)
168 {
169 #if HAVE_IOKIT_IOKITLIB_H
170 use_bsd_name = IS_TRUE (value) ? 1 : 0;
171 #else
172 WARNING ("disk plugin: The \"UseBSDName\" option is only supported "
173 "on Mach / Mac OS X and will be ignored.");
174 #endif
175 }
176 else if (strcasecmp ("UdevNameAttr", key) == 0)
177 {
178 #if HAVE_LIBUDEV
179 if (conf_udev_name_attr != NULL)
180 {
181 free (conf_udev_name_attr);
182 conf_udev_name_attr = NULL;
183 }
184 if ((conf_udev_name_attr = strdup (value)) == NULL)
185 return (1);
186 #else
187 WARNING ("disk plugin: The \"UdevNameAttr\" option is only supported "
188 "if collectd is built with libudev support");
189 #endif
190 }
191 else
192 {
193 return (-1);
194 }
196 return (0);
197 } /* int disk_config */
199 static int disk_init (void)
200 {
201 #if HAVE_IOKIT_IOKITLIB_H
202 kern_return_t status;
204 if (io_master_port != MACH_PORT_NULL)
205 {
206 mach_port_deallocate (mach_task_self (),
207 io_master_port);
208 io_master_port = MACH_PORT_NULL;
209 }
211 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
212 if (status != kIOReturnSuccess)
213 {
214 ERROR ("IOMasterPort failed: %s",
215 mach_error_string (status));
216 io_master_port = MACH_PORT_NULL;
217 return (-1);
218 }
219 /* #endif HAVE_IOKIT_IOKITLIB_H */
221 #elif KERNEL_LINUX
222 /* do nothing */
223 /* #endif KERNEL_LINUX */
225 #elif HAVE_LIBKSTAT
226 kstat_t *ksp_chain;
228 numdisk = 0;
230 if (kc == NULL)
231 return (-1);
233 for (numdisk = 0, ksp_chain = kc->kc_chain;
234 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
235 ksp_chain = ksp_chain->ks_next)
236 {
237 if (strncmp (ksp_chain->ks_class, "disk", 4)
238 && strncmp (ksp_chain->ks_class, "partition", 9))
239 continue;
240 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
241 continue;
242 ksp[numdisk++] = ksp_chain;
243 }
244 #endif /* HAVE_LIBKSTAT */
246 return (0);
247 } /* int disk_init */
249 static void disk_submit (const char *plugin_instance,
250 const char *type,
251 derive_t read, derive_t write)
252 {
253 value_t values[2];
254 value_list_t vl = VALUE_LIST_INIT;
256 /* Both `ignorelist' and `plugin_instance' may be NULL. */
257 if (ignorelist_match (ignorelist, plugin_instance) != 0)
258 return;
260 values[0].derive = read;
261 values[1].derive = write;
263 vl.values = values;
264 vl.values_len = 2;
265 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
266 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
267 sstrncpy (vl.plugin_instance, plugin_instance,
268 sizeof (vl.plugin_instance));
269 sstrncpy (vl.type, type, sizeof (vl.type));
271 plugin_dispatch_values (&vl);
272 } /* void disk_submit */
274 #if KERNEL_LINUX
275 static void submit_in_progress (char const *disk_name, gauge_t in_progress)
276 {
277 value_t v;
278 value_list_t vl = VALUE_LIST_INIT;
280 if (ignorelist_match (ignorelist, disk_name) != 0)
281 return;
283 v.gauge = in_progress;
285 vl.values = &v;
286 vl.values_len = 1;
287 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
288 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
289 sstrncpy (vl.plugin_instance, disk_name, sizeof (vl.plugin_instance));
290 sstrncpy (vl.type, "pending_operations", sizeof (vl.type));
292 plugin_dispatch_values (&vl);
293 }
295 static void submit_io_time (char const *plugin_instance, derive_t io_time, derive_t weighted_time)
296 {
297 value_t values[2];
298 value_list_t vl = VALUE_LIST_INIT;
300 if (ignorelist_match (ignorelist, plugin_instance) != 0)
301 return;
303 values[0].derive = io_time;
304 values[1].derive = weighted_time;
306 vl.values = values;
307 vl.values_len = 2;
308 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
309 sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
310 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
311 sstrncpy (vl.type, "disk_io_time", sizeof (vl.type));
313 plugin_dispatch_values (&vl);
314 }
317 static counter_t disk_calc_time_incr (counter_t delta_time, counter_t delta_ops)
318 {
319 double interval = CDTIME_T_TO_DOUBLE (plugin_get_interval ());
320 double avg_time = ((double) delta_time) / ((double) delta_ops);
321 double avg_time_incr = interval * avg_time;
323 return ((counter_t) (avg_time_incr + .5));
324 }
325 #endif
327 #if HAVE_LIBUDEV
328 /**
329 * Attempt to provide an rename disk instance from an assigned udev attribute.
330 *
331 * On success, it returns a strduped char* to the desired attribute value.
332 * Otherwise it returns NULL.
333 */
335 static char *disk_udev_attr_name (struct udev *udev, char *disk_name, const char *attr)
336 {
337 struct udev_device *dev;
338 const char *prop;
339 char *output = NULL;
341 dev = udev_device_new_from_subsystem_sysname (udev, "block", disk_name);
342 if (dev != NULL)
343 {
344 prop = udev_device_get_property_value (dev, attr);
345 if (prop) {
346 output = strdup (prop);
347 DEBUG ("disk plugin: renaming %s => %s", disk_name, output);
348 }
349 udev_device_unref (dev);
350 }
351 return output;
352 }
353 #endif
355 #if HAVE_IOKIT_IOKITLIB_H
356 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
357 {
358 signed long long val_int;
359 CFNumberRef val_obj;
360 CFStringRef key_obj;
362 /* `key_obj' needs to be released. */
363 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
364 kCFStringEncodingASCII);
365 if (key_obj == NULL)
366 {
367 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
368 return (-1LL);
369 }
371 /* get => we don't need to release (== free) the object */
372 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
374 CFRelease (key_obj);
376 if (val_obj == NULL)
377 {
378 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
379 return (-1LL);
380 }
382 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
383 {
384 DEBUG ("CFNumberGetValue (%s) failed.", key);
385 return (-1LL);
386 }
388 return (val_int);
389 }
390 #endif /* HAVE_IOKIT_IOKITLIB_H */
392 static int disk_read (void)
393 {
394 #if HAVE_IOKIT_IOKITLIB_H
395 io_registry_entry_t disk;
396 io_registry_entry_t disk_child;
397 io_iterator_t disk_list;
398 CFDictionaryRef props_dict;
399 CFDictionaryRef stats_dict;
400 CFDictionaryRef child_dict;
401 CFStringRef tmp_cf_string_ref;
402 kern_return_t status;
404 signed long long read_ops;
405 signed long long read_byt;
406 signed long long read_tme;
407 signed long long write_ops;
408 signed long long write_byt;
409 signed long long write_tme;
411 int disk_major;
412 int disk_minor;
413 char disk_name[DATA_MAX_NAME_LEN];
414 char disk_name_bsd[DATA_MAX_NAME_LEN];
416 /* Get the list of all disk objects. */
417 if (IOServiceGetMatchingServices (io_master_port,
418 IOServiceMatching (kIOBlockStorageDriverClass),
419 &disk_list) != kIOReturnSuccess)
420 {
421 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
422 return (-1);
423 }
425 while ((disk = IOIteratorNext (disk_list)) != 0)
426 {
427 props_dict = NULL;
428 stats_dict = NULL;
429 child_dict = NULL;
431 /* `disk_child' must be released */
432 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
433 != kIOReturnSuccess)
434 {
435 /* This fails for example for DVD/CD drives.. */
436 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
437 IOObjectRelease (disk);
438 continue;
439 }
441 /* We create `props_dict' => we need to release it later */
442 if (IORegistryEntryCreateCFProperties (disk,
443 (CFMutableDictionaryRef *) &props_dict,
444 kCFAllocatorDefault,
445 kNilOptions)
446 != kIOReturnSuccess)
447 {
448 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
449 IOObjectRelease (disk_child);
450 IOObjectRelease (disk);
451 continue;
452 }
454 if (props_dict == NULL)
455 {
456 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
457 IOObjectRelease (disk_child);
458 IOObjectRelease (disk);
459 continue;
460 }
462 /* tmp_cf_string_ref doesn't need to be released. */
463 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (props_dict,
464 CFSTR(kIOBSDNameKey));
465 if (!tmp_cf_string_ref)
466 {
467 DEBUG ("disk plugin: CFDictionaryGetValue("
468 "kIOBSDNameKey) failed.");
469 CFRelease (props_dict);
470 IOObjectRelease (disk_child);
471 IOObjectRelease (disk);
472 continue;
473 }
474 assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
476 memset (disk_name_bsd, 0, sizeof (disk_name_bsd));
477 CFStringGetCString (tmp_cf_string_ref,
478 disk_name_bsd, sizeof (disk_name_bsd),
479 kCFStringEncodingUTF8);
480 if (disk_name_bsd[0] == 0)
481 {
482 ERROR ("disk plugin: CFStringGetCString() failed.");
483 CFRelease (props_dict);
484 IOObjectRelease (disk_child);
485 IOObjectRelease (disk);
486 continue;
487 }
488 DEBUG ("disk plugin: disk_name_bsd = \"%s\"", disk_name_bsd);
490 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
491 CFSTR (kIOBlockStorageDriverStatisticsKey));
493 if (stats_dict == NULL)
494 {
495 DEBUG ("disk plugin: CFDictionaryGetValue ("
496 "%s) failed.",
497 kIOBlockStorageDriverStatisticsKey);
498 CFRelease (props_dict);
499 IOObjectRelease (disk_child);
500 IOObjectRelease (disk);
501 continue;
502 }
504 if (IORegistryEntryCreateCFProperties (disk_child,
505 (CFMutableDictionaryRef *) &child_dict,
506 kCFAllocatorDefault,
507 kNilOptions)
508 != kIOReturnSuccess)
509 {
510 DEBUG ("disk plugin: IORegistryEntryCreateCFProperties ("
511 "disk_child) failed.");
512 IOObjectRelease (disk_child);
513 CFRelease (props_dict);
514 IOObjectRelease (disk);
515 continue;
516 }
518 /* kIOBSDNameKey */
519 disk_major = (int) dict_get_value (child_dict,
520 kIOBSDMajorKey);
521 disk_minor = (int) dict_get_value (child_dict,
522 kIOBSDMinorKey);
523 read_ops = dict_get_value (stats_dict,
524 kIOBlockStorageDriverStatisticsReadsKey);
525 read_byt = dict_get_value (stats_dict,
526 kIOBlockStorageDriverStatisticsBytesReadKey);
527 read_tme = dict_get_value (stats_dict,
528 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
529 write_ops = dict_get_value (stats_dict,
530 kIOBlockStorageDriverStatisticsWritesKey);
531 write_byt = dict_get_value (stats_dict,
532 kIOBlockStorageDriverStatisticsBytesWrittenKey);
533 /* This property describes the number of nanoseconds spent
534 * performing writes since the block storage driver was
535 * instantiated. It is one of the statistic entries listed
536 * under the top-level kIOBlockStorageDriverStatisticsKey
537 * property table. It has an OSNumber value. */
538 write_tme = dict_get_value (stats_dict,
539 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
541 if (use_bsd_name)
542 sstrncpy (disk_name, disk_name_bsd, sizeof (disk_name));
543 else
544 ssnprintf (disk_name, sizeof (disk_name), "%i-%i",
545 disk_major, disk_minor);
546 DEBUG ("disk plugin: disk_name = \"%s\"", disk_name);
548 if ((read_byt != -1LL) || (write_byt != -1LL))
549 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
550 if ((read_ops != -1LL) || (write_ops != -1LL))
551 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
552 if ((read_tme != -1LL) || (write_tme != -1LL))
553 disk_submit (disk_name, "disk_time",
554 read_tme / 1000,
555 write_tme / 1000);
557 CFRelease (child_dict);
558 IOObjectRelease (disk_child);
559 CFRelease (props_dict);
560 IOObjectRelease (disk);
561 }
562 IOObjectRelease (disk_list);
563 /* #endif HAVE_IOKIT_IOKITLIB_H */
565 #elif KERNEL_LINUX
566 FILE *fh;
567 char buffer[1024];
569 char *fields[32];
570 int numfields;
571 int fieldshift = 0;
573 int minor = 0;
575 derive_t read_sectors = 0;
576 derive_t write_sectors = 0;
578 derive_t read_ops = 0;
579 derive_t read_merged = 0;
580 derive_t read_time = 0;
581 derive_t write_ops = 0;
582 derive_t write_merged = 0;
583 derive_t write_time = 0;
584 gauge_t in_progress = NAN;
585 derive_t io_time = 0;
586 derive_t weighted_time = 0;
587 int is_disk = 0;
589 diskstats_t *ds, *pre_ds;
591 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
592 {
593 fh = fopen ("/proc/partitions", "r");
594 if (fh == NULL)
595 {
596 ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
597 return (-1);
598 }
600 /* Kernel is 2.4.* */
601 fieldshift = 1;
602 }
604 #if HAVE_LIBUDEV
605 handle_udev = udev_new();
606 #endif
608 while (fgets (buffer, sizeof (buffer), fh) != NULL)
609 {
610 char *disk_name;
611 char *output_name;
612 char *alt_name;
614 numfields = strsplit (buffer, fields, 32);
616 if ((numfields != (14 + fieldshift)) && (numfields != 7))
617 continue;
619 minor = atoll (fields[1]);
621 disk_name = fields[2 + fieldshift];
623 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
624 if (strcmp (disk_name, ds->name) == 0)
625 break;
627 if (ds == NULL)
628 {
629 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
630 continue;
632 if ((ds->name = strdup (disk_name)) == NULL)
633 {
634 free (ds);
635 continue;
636 }
638 if (pre_ds == NULL)
639 disklist = ds;
640 else
641 pre_ds->next = ds;
642 }
644 is_disk = 0;
645 if (numfields == 7)
646 {
647 /* Kernel 2.6, Partition */
648 read_ops = atoll (fields[3]);
649 read_sectors = atoll (fields[4]);
650 write_ops = atoll (fields[5]);
651 write_sectors = atoll (fields[6]);
652 }
653 else if (numfields == (14 + fieldshift))
654 {
655 read_ops = atoll (fields[3 + fieldshift]);
656 write_ops = atoll (fields[7 + fieldshift]);
658 read_sectors = atoll (fields[5 + fieldshift]);
659 write_sectors = atoll (fields[9 + fieldshift]);
661 if ((fieldshift == 0) || (minor == 0))
662 {
663 is_disk = 1;
664 read_merged = atoll (fields[4 + fieldshift]);
665 read_time = atoll (fields[6 + fieldshift]);
666 write_merged = atoll (fields[8 + fieldshift]);
667 write_time = atoll (fields[10+ fieldshift]);
669 in_progress = atof (fields[11 + fieldshift]);
671 io_time = atof (fields[12 + fieldshift]);
672 weighted_time = atof (fields[13 + fieldshift]);
673 }
674 }
675 else
676 {
677 DEBUG ("numfields = %i; => unknown file format.", numfields);
678 continue;
679 }
681 {
682 derive_t diff_read_sectors;
683 derive_t diff_write_sectors;
685 /* If the counter wraps around, it's only 32 bits.. */
686 if (read_sectors < ds->read_sectors)
687 diff_read_sectors = 1 + read_sectors
688 + (UINT_MAX - ds->read_sectors);
689 else
690 diff_read_sectors = read_sectors - ds->read_sectors;
691 if (write_sectors < ds->write_sectors)
692 diff_write_sectors = 1 + write_sectors
693 + (UINT_MAX - ds->write_sectors);
694 else
695 diff_write_sectors = write_sectors - ds->write_sectors;
697 ds->read_bytes += 512 * diff_read_sectors;
698 ds->write_bytes += 512 * diff_write_sectors;
699 ds->read_sectors = read_sectors;
700 ds->write_sectors = write_sectors;
701 }
703 /* Calculate the average time an io-op needs to complete */
704 if (is_disk)
705 {
706 derive_t diff_read_ops;
707 derive_t diff_write_ops;
708 derive_t diff_read_time;
709 derive_t diff_write_time;
711 if (read_ops < ds->read_ops)
712 diff_read_ops = 1 + read_ops
713 + (UINT_MAX - ds->read_ops);
714 else
715 diff_read_ops = read_ops - ds->read_ops;
716 DEBUG ("disk plugin: disk_name = %s; read_ops = %"PRIi64"; "
717 "ds->read_ops = %"PRIi64"; diff_read_ops = %"PRIi64";",
718 disk_name,
719 read_ops, ds->read_ops, diff_read_ops);
721 if (write_ops < ds->write_ops)
722 diff_write_ops = 1 + write_ops
723 + (UINT_MAX - ds->write_ops);
724 else
725 diff_write_ops = write_ops - ds->write_ops;
727 if (read_time < ds->read_time)
728 diff_read_time = 1 + read_time
729 + (UINT_MAX - ds->read_time);
730 else
731 diff_read_time = read_time - ds->read_time;
733 if (write_time < ds->write_time)
734 diff_write_time = 1 + write_time
735 + (UINT_MAX - ds->write_time);
736 else
737 diff_write_time = write_time - ds->write_time;
739 if (diff_read_ops != 0)
740 ds->avg_read_time += disk_calc_time_incr (
741 diff_read_time, diff_read_ops);
742 if (diff_write_ops != 0)
743 ds->avg_write_time += disk_calc_time_incr (
744 diff_write_time, diff_write_ops);
746 ds->read_ops = read_ops;
747 ds->read_time = read_time;
748 ds->write_ops = write_ops;
749 ds->write_time = write_time;
750 } /* if (is_disk) */
752 /* Don't write to the RRDs if we've just started.. */
753 ds->poll_count++;
754 if (ds->poll_count <= 2)
755 {
756 DEBUG ("disk plugin: (ds->poll_count = %i) <= "
757 "(min_poll_count = 2); => Not writing.",
758 ds->poll_count);
759 continue;
760 }
762 if ((read_ops == 0) && (write_ops == 0))
763 {
764 DEBUG ("disk plugin: ((read_ops == 0) && "
765 "(write_ops == 0)); => Not writing.");
766 continue;
767 }
769 output_name = disk_name;
771 #if HAVE_LIBUDEV
772 alt_name = disk_udev_attr_name (handle_udev, disk_name,
773 conf_udev_name_attr);
774 #else
775 alt_name = NULL;
776 #endif
777 if (alt_name != NULL)
778 output_name = alt_name;
780 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
781 disk_submit (output_name, "disk_octets",
782 ds->read_bytes, ds->write_bytes);
784 if ((ds->read_ops != 0) || (ds->write_ops != 0))
785 disk_submit (output_name, "disk_ops",
786 read_ops, write_ops);
788 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
789 disk_submit (output_name, "disk_time",
790 ds->avg_read_time, ds->avg_write_time);
792 if (is_disk)
793 {
794 disk_submit (output_name, "disk_merged",
795 read_merged, write_merged);
796 submit_in_progress (output_name, in_progress);
797 submit_io_time (output_name, io_time, weighted_time);
798 } /* if (is_disk) */
800 /* release udev-based alternate name, if allocated */
801 free(alt_name);
802 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
804 #if HAVE_LIBUDEV
805 udev_unref(handle_udev);
806 #endif
808 fclose (fh);
809 /* #endif defined(KERNEL_LINUX) */
811 #elif HAVE_LIBKSTAT
812 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
813 # define KIO_ROCTETS reads
814 # define KIO_WOCTETS writes
815 # define KIO_ROPS nreads
816 # define KIO_WOPS nwrites
817 # define KIO_RTIME rtime
818 # define KIO_WTIME wtime
819 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
820 # define KIO_ROCTETS nread
821 # define KIO_WOCTETS nwritten
822 # define KIO_ROPS reads
823 # define KIO_WOPS writes
824 # define KIO_RTIME rtime
825 # define KIO_WTIME wtime
826 # else
827 # error "kstat_io_t does not have the required members"
828 # endif
829 static kstat_io_t kio;
830 int i;
832 if (kc == NULL)
833 return (-1);
835 for (i = 0; i < numdisk; i++)
836 {
837 if (kstat_read (kc, ksp[i], &kio) == -1)
838 continue;
840 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
841 {
842 disk_submit (ksp[i]->ks_name, "disk_octets",
843 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
844 disk_submit (ksp[i]->ks_name, "disk_ops",
845 kio.KIO_ROPS, kio.KIO_WOPS);
846 /* FIXME: Convert this to microseconds if necessary */
847 disk_submit (ksp[i]->ks_name, "disk_time",
848 kio.KIO_RTIME, kio.KIO_WTIME);
849 }
850 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
851 {
852 disk_submit (ksp[i]->ks_name, "disk_octets",
853 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
854 disk_submit (ksp[i]->ks_name, "disk_ops",
855 kio.KIO_ROPS, kio.KIO_WOPS);
856 }
857 }
858 /* #endif defined(HAVE_LIBKSTAT) */
860 #elif defined(HAVE_LIBSTATGRAB)
861 sg_disk_io_stats *ds;
862 # if HAVE_LIBSTATGRAB_0_90
863 size_t disks;
864 # else
865 int disks;
866 #endif
867 int counter;
868 char name[DATA_MAX_NAME_LEN];
870 if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
871 return (0);
873 for (counter=0; counter < disks; counter++) {
874 strncpy(name, ds->disk_name, sizeof(name));
875 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
876 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
877 ds++;
878 }
879 /* #endif defined(HAVE_LIBSTATGRAB) */
881 #elif defined(HAVE_PERFSTAT)
882 derive_t read_sectors;
883 derive_t write_sectors;
884 derive_t read_time;
885 derive_t write_time;
886 derive_t read_ops;
887 derive_t write_ops;
888 perfstat_id_t firstpath;
889 int rnumdisk;
890 int i;
892 if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0)
893 {
894 char errbuf[1024];
895 WARNING ("disk plugin: perfstat_disk: %s",
896 sstrerror (errno, errbuf, sizeof (errbuf)));
897 return (-1);
898 }
900 if (numdisk != pnumdisk || stat_disk==NULL) {
901 if (stat_disk!=NULL)
902 free(stat_disk);
903 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
904 }
905 pnumdisk = numdisk;
907 firstpath.name[0]='\0';
908 if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0)
909 {
910 char errbuf[1024];
911 WARNING ("disk plugin: perfstat_disk : %s",
912 sstrerror (errno, errbuf, sizeof (errbuf)));
913 return (-1);
914 }
916 for (i = 0; i < rnumdisk; i++)
917 {
918 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
919 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
920 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
922 read_ops = stat_disk[i].xrate;
923 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
924 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
926 read_time = stat_disk[i].rserv;
927 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
928 write_time = stat_disk[i].wserv;
929 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
930 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
931 }
932 #endif /* defined(HAVE_PERFSTAT) */
934 return (0);
935 } /* int disk_read */
937 void module_register (void)
938 {
939 plugin_register_config ("disk", disk_config,
940 config_keys, config_keys_num);
941 plugin_register_init ("disk", disk_init);
942 plugin_register_read ("disk", disk_read);
943 } /* void module_register */