disk plugin: Fix compatibility for Mac OS X 10.10.
[collectd.git] / src / disk.c
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  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "utils_ignorelist.h"
28
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
56
57 #if HAVE_LIMITS_H
58 # include <limits.h>
59 #endif
60 #ifndef UINT_MAX
61 #  define UINT_MAX 4294967295U
62 #endif
63
64 #if HAVE_STATGRAB_H
65 # include <statgrab.h>
66 #endif
67
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
75
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 */
82
83 #elif KERNEL_LINUX
84 typedef struct diskstats
85 {
86         char *name;
87
88         /* This overflows in roughly 1361 years */
89         unsigned int poll_count;
90
91         derive_t read_sectors;
92         derive_t write_sectors;
93
94         derive_t read_bytes;
95         derive_t write_bytes;
96
97         derive_t read_ops;
98         derive_t write_ops;
99         derive_t read_time;
100         derive_t write_time;
101
102         derive_t avg_read_time;
103         derive_t avg_write_time;
104
105         struct diskstats *next;
106 } diskstats_t;
107
108 static diskstats_t *disklist;
109 /* #endif KERNEL_LINUX */
110
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 */
117
118 #elif defined(HAVE_LIBSTATGRAB)
119 /* #endif HAVE_LIBKSTATGRAB */
120
121 #elif HAVE_PERFSTAT
122 static perfstat_disk_t * stat_disk;
123 static int numdisk;
124 static int pnumdisk;
125 /* #endif HAVE_PERFSTAT */
126
127 #else
128 # error "No applicable input method."
129 #endif
130
131 #if HAVE_LIBUDEV
132 #include <libudev.h>
133
134 static char *conf_udev_name_attr = NULL;
135 static struct udev *handle_udev;
136 #endif
137
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);
146
147 static ignorelist_t *ignorelist = NULL;
148
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);
155
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   }
195
196   return (0);
197 } /* int disk_config */
198
199 static int disk_init (void)
200 {
201 #if HAVE_IOKIT_IOKITLIB_H
202         kern_return_t status;
203
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         }
210
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 */
220
221 #elif KERNEL_LINUX
222         /* do nothing */
223 /* #endif KERNEL_LINUX */
224
225 #elif HAVE_LIBKSTAT
226         kstat_t *ksp_chain;
227
228         numdisk = 0;
229
230         if (kc == NULL)
231                 return (-1);
232
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 */
245
246         return (0);
247 } /* int disk_init */
248
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;
255
256         /* Both `ignorelist' and `plugin_instance' may be NULL. */
257         if (ignorelist_match (ignorelist, plugin_instance) != 0)
258           return;
259
260         values[0].derive = read;
261         values[1].derive = write;
262
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));
270
271         plugin_dispatch_values (&vl);
272 } /* void disk_submit */
273
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;
279
280         if (ignorelist_match (ignorelist, disk_name) != 0)
281           return;
282
283         v.gauge = in_progress;
284
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));
291
292         plugin_dispatch_values (&vl);
293 }
294
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;
299
300         if (ignorelist_match (ignorelist, plugin_instance) != 0)
301           return;
302
303         values[0].derive = io_time;
304         values[1].derive = weighted_time;
305
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));
312
313         plugin_dispatch_values (&vl);
314 }
315
316
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;
322
323         return ((counter_t) (avg_time_incr + .5));
324 }
325 #endif
326
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  */
334
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;
340
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
354
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;
361
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         }
370         
371         /* get => we don't need to release (== free) the object */
372         val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
373
374         CFRelease (key_obj);
375
376         if (val_obj == NULL)
377         {
378                 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
379                 return (-1LL);
380         }
381
382         if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
383         {
384                 DEBUG ("CFNumberGetValue (%s) failed.", key);
385                 return (-1LL);
386         }
387
388         return (val_int);
389 }
390 #endif /* HAVE_IOKIT_IOKITLIB_H */
391
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         CFMutableDictionaryRef  props_dict, child_dict;
399         CFDictionaryRef         stats_dict;
400         CFStringRef             tmp_cf_string_ref;
401         kern_return_t           status;
402
403         signed long long read_ops;
404         signed long long read_byt;
405         signed long long read_tme;
406         signed long long write_ops;
407         signed long long write_byt;
408         signed long long write_tme;
409
410         int  disk_major;
411         int  disk_minor;
412         char disk_name[DATA_MAX_NAME_LEN];
413         char child_disk_name_bsd[DATA_MAX_NAME_LEN], props_disk_name_bsd[DATA_MAX_NAME_LEN];
414
415         /* Get the list of all disk objects. */
416         if (IOServiceGetMatchingServices (io_master_port,
417                                 IOServiceMatching (kIOBlockStorageDriverClass),
418                                 &disk_list) != kIOReturnSuccess)
419         {
420                 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
421                 return (-1);
422         }
423
424         while ((disk = IOIteratorNext (disk_list)) != 0)
425         {
426                 props_dict = NULL;
427                 stats_dict = NULL;
428                 child_dict = NULL;
429
430                 /* get child of disk entry and corresponding property dictionary */
431                 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
432                                 != kIOReturnSuccess)
433                 {
434                         /* This fails for example for DVD/CD drives, which we want to ignore anyway */
435                         DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
436                         IOObjectRelease (disk);
437                         continue;
438                 }
439                 if ((IORegistryEntryCreateCFProperties (disk_child, &child_dict, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
440                                 || (child_dict == NULL)) {
441                         ERROR ("disk plugin: IORegistryEntryCreateCFProperties (disk_child) failed.");
442                         IOObjectRelease (disk_child);
443                         IOObjectRelease (disk);
444                         continue;
445                 }
446
447                 /* extract name and major/minor numbers */
448                 memset (child_disk_name_bsd, 0, sizeof (child_disk_name_bsd));
449                 /* tmp_cf_string_ref doesn't need to be released. */
450                 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (child_dict,
451                                 CFSTR(kIOBSDNameKey));
452                 if (tmp_cf_string_ref)
453                 {
454                         assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
455                         CFStringGetCString (tmp_cf_string_ref, child_disk_name_bsd, sizeof (child_disk_name_bsd), kCFStringEncodingUTF8);
456                 }
457                 disk_major = (int) dict_get_value (child_dict, kIOBSDMajorKey);
458                 disk_minor = (int) dict_get_value (child_dict, kIOBSDMinorKey);
459                 DEBUG ("disk plugin: child_disk_name_bsd=\"%s\" major=%d minor=%d", child_disk_name_bsd, disk_major, disk_minor);
460                 CFRelease (child_dict);
461                 IOObjectRelease (disk_child);
462
463                 /* get property dictionary of the disk entry itself */
464                 if ((IORegistryEntryCreateCFProperties (disk, &props_dict, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
465                                 || (props_dict == NULL)) {
466                         ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
467                         IOObjectRelease (disk);
468                         continue;
469                 }
470
471                 /* extract name and stats dictionary */
472                 memset (props_disk_name_bsd, 0, sizeof (props_disk_name_bsd));
473                 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (props_dict, CFSTR(kIOBSDNameKey));
474                 if (tmp_cf_string_ref) {
475                         assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
476                         CFStringGetCString (tmp_cf_string_ref, props_disk_name_bsd, sizeof (props_disk_name_bsd), kCFStringEncodingUTF8);
477                 }
478                 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
479                                 CFSTR (kIOBlockStorageDriverStatisticsKey));
480                 if (stats_dict == NULL)
481                 {
482                         ERROR ("disk plugin: CFDictionaryGetValue (%s) failed.", kIOBlockStorageDriverStatisticsKey);
483                         CFRelease (props_dict);
484                         IOObjectRelease (disk);
485                         continue;
486                 }
487                 DEBUG ("disk plugin: props_disk_name_bsd=\"%s\"", props_disk_name_bsd);
488
489                 /* choose name */
490                 if (use_bsd_name) {
491                         if (child_disk_name_bsd[0] != 0)
492                                 sstrncpy (disk_name, child_disk_name_bsd, sizeof (disk_name));
493                         else if (props_disk_name_bsd[0] != 0)
494                                 sstrncpy (disk_name, props_disk_name_bsd, sizeof (disk_name));
495                         else {
496                                 ERROR ("disk plugin: can't find bsd disk name.");
497                                 ssnprintf (disk_name, sizeof (disk_name), "%i-%i", disk_major, disk_minor);
498                         }
499                 }
500                 else
501                         ssnprintf (disk_name, sizeof (disk_name), "%i-%i", disk_major, disk_minor);
502
503                 /* extract the stats */
504                 read_ops  = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsReadsKey);
505                 read_byt  = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsBytesReadKey);
506                 read_tme  = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsTotalReadTimeKey);
507                 write_ops = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsWritesKey);
508                 write_byt = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsBytesWrittenKey);
509                 write_tme = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
510                 CFRelease (props_dict);
511                 IOObjectRelease (disk);
512
513                 /* and submit */
514                 DEBUG ("disk plugin: disk_name = \"%s\"", disk_name);
515                 if ((read_byt != -1LL) || (write_byt != -1LL))
516                         disk_submit (disk_name, "disk_octets", read_byt, write_byt);
517                 if ((read_ops != -1LL) || (write_ops != -1LL))
518                         disk_submit (disk_name, "disk_ops", read_ops, write_ops);
519                 if ((read_tme != -1LL) || (write_tme != -1LL))
520                         disk_submit (disk_name, "disk_time",
521                                         read_tme / 1000,
522                                         write_tme / 1000);
523
524         }
525         IOObjectRelease (disk_list);
526 /* #endif HAVE_IOKIT_IOKITLIB_H */
527
528 #elif KERNEL_LINUX
529         FILE *fh;
530         char buffer[1024];
531         
532         char *fields[32];
533         int numfields;
534         int fieldshift = 0;
535
536         int minor = 0;
537
538         derive_t read_sectors  = 0;
539         derive_t write_sectors = 0;
540
541         derive_t read_ops      = 0;
542         derive_t read_merged   = 0;
543         derive_t read_time     = 0;
544         derive_t write_ops     = 0;
545         derive_t write_merged  = 0;
546         derive_t write_time    = 0;
547         gauge_t in_progress    = NAN;
548         derive_t io_time       = 0;
549         derive_t weighted_time = 0;
550         int is_disk = 0;
551
552         diskstats_t *ds, *pre_ds;
553
554         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
555         {
556                 fh = fopen ("/proc/partitions", "r");
557                 if (fh == NULL)
558                 {
559                         ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
560                         return (-1);
561                 }
562
563                 /* Kernel is 2.4.* */
564                 fieldshift = 1;
565         }
566
567 #if HAVE_LIBUDEV
568         handle_udev = udev_new();
569 #endif
570
571         while (fgets (buffer, sizeof (buffer), fh) != NULL)
572         {
573                 char *disk_name;
574                 char *output_name;
575                 char *alt_name;
576
577                 numfields = strsplit (buffer, fields, 32);
578
579                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
580                         continue;
581
582                 minor = atoll (fields[1]);
583
584                 disk_name = fields[2 + fieldshift];
585
586                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
587                         if (strcmp (disk_name, ds->name) == 0)
588                                 break;
589
590                 if (ds == NULL)
591                 {
592                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
593                                 continue;
594
595                         if ((ds->name = strdup (disk_name)) == NULL)
596                         {
597                                 free (ds);
598                                 continue;
599                         }
600
601                         if (pre_ds == NULL)
602                                 disklist = ds;
603                         else
604                                 pre_ds->next = ds;
605                 }
606
607                 is_disk = 0;
608                 if (numfields == 7)
609                 {
610                         /* Kernel 2.6, Partition */
611                         read_ops      = atoll (fields[3]);
612                         read_sectors  = atoll (fields[4]);
613                         write_ops     = atoll (fields[5]);
614                         write_sectors = atoll (fields[6]);
615                 }
616                 else if (numfields == (14 + fieldshift))
617                 {
618                         read_ops  =  atoll (fields[3 + fieldshift]);
619                         write_ops =  atoll (fields[7 + fieldshift]);
620
621                         read_sectors  = atoll (fields[5 + fieldshift]);
622                         write_sectors = atoll (fields[9 + fieldshift]);
623
624                         if ((fieldshift == 0) || (minor == 0))
625                         {
626                                 is_disk = 1;
627                                 read_merged  = atoll (fields[4 + fieldshift]);
628                                 read_time    = atoll (fields[6 + fieldshift]);
629                                 write_merged = atoll (fields[8 + fieldshift]);
630                                 write_time   = atoll (fields[10+ fieldshift]);
631
632                                 in_progress = atof (fields[11 + fieldshift]);
633
634                                 io_time       = atof (fields[12 + fieldshift]);
635                                 weighted_time = atof (fields[13 + fieldshift]);
636                         }
637                 }
638                 else
639                 {
640                         DEBUG ("numfields = %i; => unknown file format.", numfields);
641                         continue;
642                 }
643
644                 {
645                         derive_t diff_read_sectors;
646                         derive_t diff_write_sectors;
647
648                 /* If the counter wraps around, it's only 32 bits.. */
649                         if (read_sectors < ds->read_sectors)
650                                 diff_read_sectors = 1 + read_sectors
651                                         + (UINT_MAX - ds->read_sectors);
652                         else
653                                 diff_read_sectors = read_sectors - ds->read_sectors;
654                         if (write_sectors < ds->write_sectors)
655                                 diff_write_sectors = 1 + write_sectors
656                                         + (UINT_MAX - ds->write_sectors);
657                         else
658                                 diff_write_sectors = write_sectors - ds->write_sectors;
659
660                         ds->read_bytes += 512 * diff_read_sectors;
661                         ds->write_bytes += 512 * diff_write_sectors;
662                         ds->read_sectors = read_sectors;
663                         ds->write_sectors = write_sectors;
664                 }
665
666                 /* Calculate the average time an io-op needs to complete */
667                 if (is_disk)
668                 {
669                         derive_t diff_read_ops;
670                         derive_t diff_write_ops;
671                         derive_t diff_read_time;
672                         derive_t diff_write_time;
673
674                         if (read_ops < ds->read_ops)
675                                 diff_read_ops = 1 + read_ops
676                                         + (UINT_MAX - ds->read_ops);
677                         else
678                                 diff_read_ops = read_ops - ds->read_ops;
679                         DEBUG ("disk plugin: disk_name = %s; read_ops = %"PRIi64"; "
680                                         "ds->read_ops = %"PRIi64"; diff_read_ops = %"PRIi64";",
681                                         disk_name,
682                                         read_ops, ds->read_ops, diff_read_ops);
683
684                         if (write_ops < ds->write_ops)
685                                 diff_write_ops = 1 + write_ops
686                                         + (UINT_MAX - ds->write_ops);
687                         else
688                                 diff_write_ops = write_ops - ds->write_ops;
689
690                         if (read_time < ds->read_time)
691                                 diff_read_time = 1 + read_time
692                                         + (UINT_MAX - ds->read_time);
693                         else
694                                 diff_read_time = read_time - ds->read_time;
695
696                         if (write_time < ds->write_time)
697                                 diff_write_time = 1 + write_time
698                                         + (UINT_MAX - ds->write_time);
699                         else
700                                 diff_write_time = write_time - ds->write_time;
701
702                         if (diff_read_ops != 0)
703                                 ds->avg_read_time += disk_calc_time_incr (
704                                                 diff_read_time, diff_read_ops);
705                         if (diff_write_ops != 0)
706                                 ds->avg_write_time += disk_calc_time_incr (
707                                                 diff_write_time, diff_write_ops);
708
709                         ds->read_ops = read_ops;
710                         ds->read_time = read_time;
711                         ds->write_ops = write_ops;
712                         ds->write_time = write_time;
713                 } /* if (is_disk) */
714
715                 /* Don't write to the RRDs if we've just started.. */
716                 ds->poll_count++;
717                 if (ds->poll_count <= 2)
718                 {
719                         DEBUG ("disk plugin: (ds->poll_count = %i) <= "
720                                         "(min_poll_count = 2); => Not writing.",
721                                         ds->poll_count);
722                         continue;
723                 }
724
725                 if ((read_ops == 0) && (write_ops == 0))
726                 {
727                         DEBUG ("disk plugin: ((read_ops == 0) && "
728                                         "(write_ops == 0)); => Not writing.");
729                         continue;
730                 }
731
732                 output_name = disk_name;
733
734 #if HAVE_LIBUDEV
735                 alt_name = disk_udev_attr_name (handle_udev, disk_name,
736                                 conf_udev_name_attr);
737 #else
738                 alt_name = NULL;
739 #endif
740                 if (alt_name != NULL)
741                         output_name = alt_name;
742
743                 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
744                         disk_submit (output_name, "disk_octets",
745                                         ds->read_bytes, ds->write_bytes);
746
747                 if ((ds->read_ops != 0) || (ds->write_ops != 0))
748                         disk_submit (output_name, "disk_ops",
749                                         read_ops, write_ops);
750
751                 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
752                         disk_submit (output_name, "disk_time",
753                                         ds->avg_read_time, ds->avg_write_time);
754
755                 if (is_disk)
756                 {
757                         disk_submit (output_name, "disk_merged",
758                                         read_merged, write_merged);
759                         submit_in_progress (output_name, in_progress);
760                         submit_io_time (output_name, io_time, weighted_time);
761                 } /* if (is_disk) */
762
763                 /* release udev-based alternate name, if allocated */
764                 free(alt_name);
765         } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
766
767 #if HAVE_LIBUDEV
768         udev_unref(handle_udev);
769 #endif
770
771         fclose (fh);
772 /* #endif defined(KERNEL_LINUX) */
773
774 #elif HAVE_LIBKSTAT
775 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
776 #  define KIO_ROCTETS reads
777 #  define KIO_WOCTETS writes
778 #  define KIO_ROPS    nreads
779 #  define KIO_WOPS    nwrites
780 #  define KIO_RTIME   rtime
781 #  define KIO_WTIME   wtime
782 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
783 #  define KIO_ROCTETS nread
784 #  define KIO_WOCTETS nwritten
785 #  define KIO_ROPS    reads
786 #  define KIO_WOPS    writes
787 #  define KIO_RTIME   rtime
788 #  define KIO_WTIME   wtime
789 # else
790 #  error "kstat_io_t does not have the required members"
791 # endif
792         static kstat_io_t kio;
793         int i;
794
795         if (kc == NULL)
796                 return (-1);
797
798         for (i = 0; i < numdisk; i++)
799         {
800                 if (kstat_read (kc, ksp[i], &kio) == -1)
801                         continue;
802
803                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
804                 {
805                         disk_submit (ksp[i]->ks_name, "disk_octets",
806                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
807                         disk_submit (ksp[i]->ks_name, "disk_ops",
808                                         kio.KIO_ROPS, kio.KIO_WOPS);
809                         /* FIXME: Convert this to microseconds if necessary */
810                         disk_submit (ksp[i]->ks_name, "disk_time",
811                                         kio.KIO_RTIME, kio.KIO_WTIME);
812                 }
813                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
814                 {
815                         disk_submit (ksp[i]->ks_name, "disk_octets",
816                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
817                         disk_submit (ksp[i]->ks_name, "disk_ops",
818                                         kio.KIO_ROPS, kio.KIO_WOPS);
819                 }
820         }
821 /* #endif defined(HAVE_LIBKSTAT) */
822
823 #elif defined(HAVE_LIBSTATGRAB)
824         sg_disk_io_stats *ds;
825 # if HAVE_LIBSTATGRAB_0_90
826         size_t disks;
827 # else
828         int disks;
829 #endif
830         int counter;
831         char name[DATA_MAX_NAME_LEN];
832         
833         if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
834                 return (0);
835                 
836         for (counter=0; counter < disks; counter++) {
837                 strncpy(name, ds->disk_name, sizeof(name));
838                 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
839                 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
840                 ds++;
841         }
842 /* #endif defined(HAVE_LIBSTATGRAB) */
843
844 #elif defined(HAVE_PERFSTAT)
845         derive_t read_sectors;
846         derive_t write_sectors;
847         derive_t read_time;
848         derive_t write_time;
849         derive_t read_ops;
850         derive_t write_ops;
851         perfstat_id_t firstpath;
852         int rnumdisk;
853         int i;
854
855         if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0) 
856         {
857                 char errbuf[1024];
858                 WARNING ("disk plugin: perfstat_disk: %s",
859                                 sstrerror (errno, errbuf, sizeof (errbuf)));
860                 return (-1);
861         }
862
863         if (numdisk != pnumdisk || stat_disk==NULL) {
864                 if (stat_disk!=NULL) 
865                         free(stat_disk);
866                 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
867         } 
868         pnumdisk = numdisk;
869
870         firstpath.name[0]='\0';
871         if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0) 
872         {
873                 char errbuf[1024];
874                 WARNING ("disk plugin: perfstat_disk : %s",
875                                 sstrerror (errno, errbuf, sizeof (errbuf)));
876                 return (-1);
877         }
878
879         for (i = 0; i < rnumdisk; i++) 
880         {
881                 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
882                 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
883                 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
884
885                 read_ops = stat_disk[i].xrate;
886                 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
887                 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
888
889                 read_time = stat_disk[i].rserv;
890                 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
891                 write_time = stat_disk[i].wserv;
892                 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
893                 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
894         }
895 #endif /* defined(HAVE_PERFSTAT) */
896
897         return (0);
898 } /* int disk_read */
899
900 void module_register (void)
901 {
902   plugin_register_config ("disk", disk_config,
903       config_keys, config_keys_num);
904   plugin_register_init ("disk", disk_init);
905   plugin_register_read ("disk", disk_read);
906 } /* void module_register */