Make disk plugin compatible with FreeBSD.
[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 #if KERNEL_FREEBSD
57 #include <devstat.h>
58 #include <libgeom.h>
59 #endif
60
61 #if HAVE_LIMITS_H
62 # include <limits.h>
63 #endif
64 #ifndef UINT_MAX
65 #  define UINT_MAX 4294967295U
66 #endif
67
68 #if HAVE_STATGRAB_H
69 # include <statgrab.h>
70 #endif
71
72 #if HAVE_PERFSTAT
73 # ifndef _AIXVERSION_610
74 # include <sys/systemcfg.h>
75 # endif
76 # include <sys/protosw.h>
77 # include <libperfstat.h>
78 #endif
79
80 #if HAVE_IOKIT_IOKITLIB_H
81 static mach_port_t io_master_port = MACH_PORT_NULL;
82 /* This defaults to false for backwards compatibility. Please fix in the next
83  * major version. */
84 static _Bool use_bsd_name = 0;
85 /* #endif HAVE_IOKIT_IOKITLIB_H */
86
87 #elif KERNEL_LINUX
88 typedef struct diskstats
89 {
90         char *name;
91
92         /* This overflows in roughly 1361 years */
93         unsigned int poll_count;
94
95         derive_t read_sectors;
96         derive_t write_sectors;
97
98         derive_t read_bytes;
99         derive_t write_bytes;
100
101         derive_t read_ops;
102         derive_t write_ops;
103         derive_t read_time;
104         derive_t write_time;
105
106         derive_t avg_read_time;
107         derive_t avg_write_time;
108
109         struct diskstats *next;
110 } diskstats_t;
111
112 static diskstats_t *disklist;
113 /* #endif KERNEL_LINUX */
114 #elif KERNEL_FREEBSD
115 static void *snap_previous;
116 static struct timespec tsnap_previous;
117 static struct gmesh geom_tree;
118 /* #endif KERNEL_FREEBSD */
119
120 #elif HAVE_LIBKSTAT
121 #define MAX_NUMDISK 1024
122 extern kstat_ctl_t *kc;
123 static kstat_t *ksp[MAX_NUMDISK];
124 static int numdisk = 0;
125 /* #endif HAVE_LIBKSTAT */
126
127 #elif defined(HAVE_LIBSTATGRAB)
128 /* #endif HAVE_LIBKSTATGRAB */
129
130 #elif HAVE_PERFSTAT
131 static perfstat_disk_t * stat_disk;
132 static int numdisk;
133 static int pnumdisk;
134 /* #endif HAVE_PERFSTAT */
135
136 #else
137 # error "No applicable input method."
138 #endif
139
140 #if HAVE_LIBUDEV
141 #include <libudev.h>
142
143 static char *conf_udev_name_attr = NULL;
144 static struct udev *handle_udev;
145 #endif
146
147 static const char *config_keys[] =
148 {
149         "Disk",
150         "UseBSDName",
151         "IgnoreSelected",
152         "UdevNameAttr"
153 };
154 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
155
156 static ignorelist_t *ignorelist = NULL;
157
158 static int disk_config (const char *key, const char *value)
159 {
160   if (ignorelist == NULL)
161     ignorelist = ignorelist_create (/* invert = */ 1);
162   if (ignorelist == NULL)
163     return (1);
164
165   if (strcasecmp ("Disk", key) == 0)
166   {
167     ignorelist_add (ignorelist, value);
168   }
169   else if (strcasecmp ("IgnoreSelected", key) == 0)
170   {
171     int invert = 1;
172     if (IS_TRUE (value))
173       invert = 0;
174     ignorelist_set_invert (ignorelist, invert);
175   }
176   else if (strcasecmp ("UseBSDName", key) == 0)
177   {
178 #if HAVE_IOKIT_IOKITLIB_H
179     use_bsd_name = IS_TRUE (value) ? 1 : 0;
180 #else
181     WARNING ("disk plugin: The \"UseBSDName\" option is only supported "
182         "on Mach / Mac OS X and will be ignored.");
183 #endif
184   }
185   else if (strcasecmp ("UdevNameAttr", key) == 0)
186   {
187 #if HAVE_LIBUDEV
188     if (conf_udev_name_attr != NULL)
189     {
190       free (conf_udev_name_attr);
191       conf_udev_name_attr = NULL;
192     }
193     if ((conf_udev_name_attr = strdup (value)) == NULL)
194       return (1);
195 #else
196     WARNING ("disk plugin: The \"UdevNameAttr\" option is only supported "
197         "if collectd is built with libudev support");
198 #endif
199   }
200   else
201   {
202     return (-1);
203   }
204
205   return (0);
206 } /* int disk_config */
207
208 static int disk_init (void)
209 {
210 #if HAVE_IOKIT_IOKITLIB_H
211         kern_return_t status;
212
213         if (io_master_port != MACH_PORT_NULL)
214         {
215                 mach_port_deallocate (mach_task_self (),
216                                 io_master_port);
217                 io_master_port = MACH_PORT_NULL;
218         }
219
220         status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
221         if (status != kIOReturnSuccess)
222         {
223                 ERROR ("IOMasterPort failed: %s",
224                                 mach_error_string (status));
225                 io_master_port = MACH_PORT_NULL;
226                 return (-1);
227         }
228 /* #endif HAVE_IOKIT_IOKITLIB_H */
229
230 #elif KERNEL_LINUX
231         /* do nothing */
232 /* #endif KERNEL_LINUX */
233
234 #elif KERNEL_FREEBSD
235         int rv;
236
237         rv = geom_gettree(&geom_tree);
238         if (rv != 0) {
239                 ERROR ("geom_gettree() failed, returned %d", rv);
240                 return (-1);
241         }
242         rv = geom_stats_open();
243         if (rv != 0) {
244                 ERROR ("geom_stats_open() failed, returned %d", rv);
245                 return (-1);
246         }
247         snap_previous = geom_stats_snapshot_get();
248         if (snap_previous == NULL) {
249                 ERROR ("geom_stats_snapshot_get() failed");
250                 return (-1);
251         }
252         geom_stats_snapshot_timestamp(snap_previous, &tsnap_previous);
253 /* #endif KERNEL_FREEBSD */
254
255 #elif HAVE_LIBKSTAT
256         kstat_t *ksp_chain;
257
258         numdisk = 0;
259
260         if (kc == NULL)
261                 return (-1);
262
263         for (numdisk = 0, ksp_chain = kc->kc_chain;
264                         (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
265                         ksp_chain = ksp_chain->ks_next)
266         {
267                 if (strncmp (ksp_chain->ks_class, "disk", 4)
268                                 && strncmp (ksp_chain->ks_class, "partition", 9))
269                         continue;
270                 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
271                         continue;
272                 ksp[numdisk++] = ksp_chain;
273         }
274 #endif /* HAVE_LIBKSTAT */
275
276         return (0);
277 } /* int disk_init */
278
279 static void disk_submit (const char *plugin_instance,
280                 const char *type,
281                 derive_t read, derive_t write)
282 {
283         value_t values[2];
284         value_list_t vl = VALUE_LIST_INIT;
285
286         /* Both `ignorelist' and `plugin_instance' may be NULL. */
287         if (ignorelist_match (ignorelist, plugin_instance) != 0)
288           return;
289
290         values[0].derive = read;
291         values[1].derive = write;
292
293         vl.values = values;
294         vl.values_len = 2;
295         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
296         sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
297         sstrncpy (vl.plugin_instance, plugin_instance,
298                         sizeof (vl.plugin_instance));
299         sstrncpy (vl.type, type, sizeof (vl.type));
300
301         plugin_dispatch_values (&vl);
302 } /* void disk_submit */
303
304 #if KERNEL_LINUX
305 static void submit_in_progress (char const *disk_name, gauge_t in_progress)
306 {
307         value_t v;
308         value_list_t vl = VALUE_LIST_INIT;
309
310         if (ignorelist_match (ignorelist, disk_name) != 0)
311           return;
312
313         v.gauge = in_progress;
314
315         vl.values = &v;
316         vl.values_len = 1;
317         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
318         sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
319         sstrncpy (vl.plugin_instance, disk_name, sizeof (vl.plugin_instance));
320         sstrncpy (vl.type, "pending_operations", sizeof (vl.type));
321
322         plugin_dispatch_values (&vl);
323 }
324
325 static void submit_io_time (char const *plugin_instance, derive_t io_time, derive_t weighted_time)
326 {
327         value_t values[2];
328         value_list_t vl = VALUE_LIST_INIT;
329
330         if (ignorelist_match (ignorelist, plugin_instance) != 0)
331           return;
332
333         values[0].derive = io_time;
334         values[1].derive = weighted_time;
335
336         vl.values = values;
337         vl.values_len = 2;
338         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
339         sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
340         sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
341         sstrncpy (vl.type, "disk_io_time", sizeof (vl.type));
342
343         plugin_dispatch_values (&vl);
344 }
345
346
347 static counter_t disk_calc_time_incr (counter_t delta_time, counter_t delta_ops)
348 {
349         double interval = CDTIME_T_TO_DOUBLE (plugin_get_interval ());
350         double avg_time = ((double) delta_time) / ((double) delta_ops);
351         double avg_time_incr = interval * avg_time;
352
353         return ((counter_t) (avg_time_incr + .5));
354 }
355 #endif
356
357 #if HAVE_LIBUDEV
358 /**
359  * Attempt to provide an rename disk instance from an assigned udev attribute.
360  *
361  * On success, it returns a strduped char* to the desired attribute value.
362  * Otherwise it returns NULL.
363  */
364
365 static char *disk_udev_attr_name (struct udev *udev, char *disk_name, const char *attr)
366 {
367         struct udev_device *dev;
368         const char *prop;
369         char *output = NULL;
370
371         dev = udev_device_new_from_subsystem_sysname (udev, "block", disk_name);
372         if (dev != NULL)
373         {
374                 prop = udev_device_get_property_value (dev, attr);
375                 if (prop) {
376                         output = strdup (prop);
377                         DEBUG ("disk plugin: renaming %s => %s", disk_name, output);
378                 }
379                 udev_device_unref (dev);
380         }
381         return output;
382 }
383 #endif
384
385 #if HAVE_IOKIT_IOKITLIB_H
386 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
387 {
388         signed long long val_int;
389         CFNumberRef      val_obj;
390         CFStringRef      key_obj;
391
392         /* `key_obj' needs to be released. */
393         key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
394                         kCFStringEncodingASCII);
395         if (key_obj == NULL)
396         {
397                 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
398                 return (-1LL);
399         }
400         
401         /* get => we don't need to release (== free) the object */
402         val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
403
404         CFRelease (key_obj);
405
406         if (val_obj == NULL)
407         {
408                 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
409                 return (-1LL);
410         }
411
412         if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
413         {
414                 DEBUG ("CFNumberGetValue (%s) failed.", key);
415                 return (-1LL);
416         }
417
418         return (val_int);
419 }
420 #endif /* HAVE_IOKIT_IOKITLIB_H */
421
422 static int disk_read (void)
423 {
424 #if HAVE_IOKIT_IOKITLIB_H
425         io_registry_entry_t     disk;
426         io_registry_entry_t     disk_child;
427         io_iterator_t           disk_list;
428         CFMutableDictionaryRef  props_dict, child_dict;
429         CFDictionaryRef         stats_dict;
430         CFStringRef             tmp_cf_string_ref;
431         kern_return_t           status;
432
433         signed long long read_ops, read_byt, read_tme;
434         signed long long write_ops, write_byt, write_tme;
435
436         int  disk_major, disk_minor;
437         char disk_name[DATA_MAX_NAME_LEN];
438         char child_disk_name_bsd[DATA_MAX_NAME_LEN], props_disk_name_bsd[DATA_MAX_NAME_LEN];
439
440         /* Get the list of all disk objects. */
441         if (IOServiceGetMatchingServices (io_master_port, IOServiceMatching (kIOBlockStorageDriverClass), &disk_list) != kIOReturnSuccess) {
442                 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
443                 return (-1);
444         }
445
446         while ((disk = IOIteratorNext (disk_list)) != 0) {
447                 props_dict = NULL;
448                 stats_dict = NULL;
449                 child_dict = NULL;
450
451                 /* get child of disk entry and corresponding property dictionary */
452                 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child)) != kIOReturnSuccess) {
453                         /* This fails for example for DVD/CD drives, which we want to ignore anyway */
454                         DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
455                         IOObjectRelease (disk);
456                         continue;
457                 }
458                 if (IORegistryEntryCreateCFProperties (disk_child, (CFMutableDictionaryRef *) &child_dict, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess || child_dict == NULL) {
459                         ERROR ("disk plugin: IORegistryEntryCreateCFProperties (disk_child) failed.");
460                         IOObjectRelease (disk_child);
461                         IOObjectRelease (disk);
462                         continue;
463                 }
464
465                 /* extract name and major/minor numbers */
466                 memset (child_disk_name_bsd, 0, sizeof (child_disk_name_bsd));
467                 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (child_dict, CFSTR(kIOBSDNameKey));
468                 if (tmp_cf_string_ref) {
469                         assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
470                         CFStringGetCString (tmp_cf_string_ref, child_disk_name_bsd, sizeof (child_disk_name_bsd), kCFStringEncodingUTF8);
471                 }
472                 disk_major = (int) dict_get_value (child_dict, kIOBSDMajorKey);
473                 disk_minor = (int) dict_get_value (child_dict, kIOBSDMinorKey);
474                 DEBUG ("disk plugin: child_disk_name_bsd=\"%s\" major=%d minor=%d", child_disk_name_bsd, disk_major, disk_minor);
475                 CFRelease (child_dict);
476                 IOObjectRelease (disk_child);
477
478                 /* get property dictionary of the disk entry itself */
479                 if (IORegistryEntryCreateCFProperties (disk, (CFMutableDictionaryRef *) &props_dict, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess || props_dict == NULL) {
480                         ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
481                         IOObjectRelease (disk);
482                         continue;
483                 }
484
485                 /* extract name and stats dictionary */
486                 memset (props_disk_name_bsd, 0, sizeof (props_disk_name_bsd));
487                 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (props_dict, CFSTR(kIOBSDNameKey));
488                 if (tmp_cf_string_ref) {
489                         assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
490                         CFStringGetCString (tmp_cf_string_ref, props_disk_name_bsd, sizeof (props_disk_name_bsd), kCFStringEncodingUTF8);
491                 }
492                 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict, CFSTR (kIOBlockStorageDriverStatisticsKey));
493                 if (stats_dict == NULL) {
494                         ERROR ("disk plugin: CFDictionaryGetValue (%s) failed.", kIOBlockStorageDriverStatisticsKey);
495                         CFRelease (props_dict);
496                         IOObjectRelease (disk);
497                         continue;
498                 }
499                 DEBUG ("disk plugin: props_disk_name_bsd=\"%s\"", props_disk_name_bsd);
500
501                 /* choose name */
502                 if (use_bsd_name) {
503                         if (child_disk_name_bsd[0] != 0)
504                                 sstrncpy (disk_name, child_disk_name_bsd, sizeof (disk_name));
505                         else if (props_disk_name_bsd[0] != 0)
506                                 sstrncpy (disk_name, props_disk_name_bsd, sizeof (disk_name));
507                         else {
508                                 ERROR ("disk plugin: can't find bsd disk name.");
509                                 ssnprintf (disk_name, sizeof (disk_name), "%i-%i", disk_major, disk_minor);
510                         }
511                 }
512                 else
513                         ssnprintf (disk_name, sizeof (disk_name), "%i-%i", disk_major, disk_minor);
514
515                 /* extract the stats */
516                 read_ops  = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsReadsKey);
517                 read_byt  = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsBytesReadKey);
518                 read_tme  = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsTotalReadTimeKey);
519                 write_ops = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsWritesKey);
520                 write_byt = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsBytesWrittenKey);
521                 write_tme = dict_get_value (stats_dict, kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
522                 CFRelease (props_dict);
523                 IOObjectRelease (disk);
524
525                 /* and submit */
526                 DEBUG ("disk plugin: disk_name = \"%s\"", disk_name);
527                 if ((read_byt != -1LL) || (write_byt != -1LL))
528                         disk_submit (disk_name, "disk_octets", read_byt, write_byt);
529                 if ((read_ops != -1LL) || (write_ops != -1LL))
530                         disk_submit (disk_name, "disk_ops", read_ops, write_ops);
531                 if ((read_tme != -1LL) || (write_tme != -1LL))
532                         disk_submit (disk_name, "disk_time", read_tme / 1000, write_tme / 1000);
533
534         }
535         IOObjectRelease (disk_list);
536 /* #endif HAVE_IOKIT_IOKITLIB_H */
537
538 #elif KERNEL_FREEBSD
539         int retry, dirty;
540
541         void *snap_present = NULL;
542         struct devstat *snap_present_iter, *snap_previous_iter;
543         struct timespec tsnap_present;
544
545         struct gident *geom_id;
546         double timedelta;
547         long double ld[11];
548
549         const char *disk_name;
550         derive_t read_bytes, write_bytes;
551         derive_t read_ops, write_ops;
552         derive_t read_time, write_time;
553
554         for (retry = 0, dirty = 1; retry < 5 && dirty == 1; retry++) {
555                 if (snap_present != NULL)
556                         geom_stats_snapshot_free(snap_present);
557
558                 /* Get a fresh copy of stats snapshot */
559                 snap_present = geom_stats_snapshot_get();
560                 if (snap_present == NULL) {
561                         ERROR("disk plugin: geom_stats_snapshot_get() failed.");
562                         return (-1);
563                 }
564
565                 /* Check if we have dirty read from this snapshot */
566                 dirty = 0;
567                 geom_stats_snapshot_reset(snap_present);
568                 while ((snap_present_iter = geom_stats_snapshot_next(snap_present)) != NULL) {
569                         if (snap_present_iter->id == NULL)
570                                 continue;
571                         geom_id = geom_lookupid(&geom_tree, snap_present_iter->id);
572
573                         /* New device? refresh GEOM tree */
574                         if (geom_id == NULL) {
575                                 geom_deletetree(&geom_tree);
576                                 if (geom_gettree(&geom_tree) != 0) {
577                                         ERROR("disk plugin: geom_gettree() failed");
578                                         geom_stats_snapshot_free(snap_present);
579                                         return (-1);
580                                 }
581                                 geom_id = geom_lookupid(&geom_tree, snap_present_iter->id);
582                         }
583                         /*
584                          * This should be rare: the device come right before we take the
585                          * snapshot and went away right after it.  We will handle this
586                          * case later, so don't mark dirty but silently ignore it.
587                          */
588                         if (geom_id == NULL)
589                                 continue;
590
591                         /* Only collect PROVIDER data */
592                         if (geom_id->lg_what != ISPROVIDER)
593                                 continue;
594
595                         /* Only collect data when rank is 1 (physical devices) */
596                         if (((struct gprovider *)(geom_id->lg_ptr))->lg_geom->lg_rank != 1)
597                                 continue;
598
599                         /* Check if this is a dirty read quit for another try */
600                         if (snap_present_iter->sequence0 != snap_present_iter->sequence1) {
601                                 dirty = 1;
602                                 break;
603                         }
604                 }
605         }
606
607         /*
608          * Now we have a snapshot that is either clean, or still dirty after 5
609          * reads.
610          */
611         geom_stats_snapshot_timestamp(snap_present, &tsnap_present);
612         timedelta = tsnap_present.tv_sec - tsnap_previous.tv_sec;
613         timedelta += (tsnap_present.tv_nsec - tsnap_previous.tv_nsec) * 1e-9;
614         tsnap_previous = tsnap_present;
615
616         /* Reset iterators for both snapshots */
617         geom_stats_snapshot_reset(snap_present);
618         geom_stats_snapshot_reset(snap_previous);
619         for (;;) {
620                 snap_present_iter = geom_stats_snapshot_next(snap_present);
621                 snap_previous_iter = geom_stats_snapshot_next(snap_previous);
622                 if (snap_present_iter == NULL || snap_previous_iter == NULL)
623                         break;
624
625                 if (snap_present_iter->id == NULL)
626                         continue;
627                 geom_id = geom_lookupid(&geom_tree, snap_present_iter->id);
628                 if (geom_id == NULL)
629                         continue;
630                 if (geom_id->lg_what != ISPROVIDER)
631                         continue;
632                 if (((struct gprovider *)(geom_id->lg_ptr))->lg_geom->lg_rank != 1)
633                         continue;
634                 /* Skip dirty reads, if present */
635                 if (dirty && (snap_present_iter->sequence0 != snap_present_iter->sequence1))
636                         continue;
637
638                 devstat_compute_statistics(snap_present_iter, snap_previous_iter, timedelta,
639                     DSM_TRANSFERS_PER_SECOND, &ld[0],
640
641                     DSM_TRANSFERS_PER_SECOND_READ, &ld[1],
642                     DSM_MB_PER_SECOND_READ, &ld[2],
643                     DSM_MS_PER_TRANSACTION_READ, &ld[3],
644
645                     DSM_TRANSFERS_PER_SECOND_WRITE, &ld[4],
646                     DSM_MB_PER_SECOND_WRITE, &ld[5],
647                     DSM_MS_PER_TRANSACTION_WRITE, &ld[6],
648
649                     DSM_BUSY_PCT, &ld[7],
650
651                     DSM_TRANSFERS_PER_SECOND_FREE, &ld[8],
652                     DSM_MB_PER_SECOND_FREE, &ld[9],
653                     DSM_MS_PER_TRANSACTION_FREE, &ld[10],
654                     DSM_NONE);
655                 *snap_previous_iter = *snap_present_iter;
656
657                 /* Derive data to be submitted */
658                 read_ops = (derive_t)ld[1];
659                 write_ops = (derive_t)ld[4];
660                 read_bytes = (derive_t)(ld[2] * 1048576LL);
661                 write_bytes = (derive_t)(ld[5] * 1048576LL);
662                 read_time = (derive_t)ld[3];
663                 write_time = (derive_t)ld[6];
664
665                 disk_name = ((struct gprovider *)geom_id->lg_ptr)->lg_name;
666
667                 if ((read_bytes != 0) || (write_bytes != 0))
668                         disk_submit(disk_name, "disk_octets",
669                                         read_bytes, write_bytes);
670
671                 if ((read_ops != 0) || (write_ops != 0))
672                         disk_submit(disk_name, "disk_ops",
673                                         read_ops, write_ops);
674
675                 if ((read_time != 0) || (write_time != 0))
676                         disk_submit (disk_name, "disk_time",
677                                         read_time, write_time);
678
679                 /* TODO: TRIM statistics collected but not reported. */
680         }
681         geom_stats_snapshot_free(snap_present);
682
683 #elif KERNEL_LINUX
684         FILE *fh;
685         char buffer[1024];
686         
687         char *fields[32];
688         int numfields;
689         int fieldshift = 0;
690
691         int minor = 0;
692
693         derive_t read_sectors  = 0;
694         derive_t write_sectors = 0;
695
696         derive_t read_ops      = 0;
697         derive_t read_merged   = 0;
698         derive_t read_time     = 0;
699         derive_t write_ops     = 0;
700         derive_t write_merged  = 0;
701         derive_t write_time    = 0;
702         gauge_t in_progress    = NAN;
703         derive_t io_time       = 0;
704         derive_t weighted_time = 0;
705         int is_disk = 0;
706
707         diskstats_t *ds, *pre_ds;
708
709         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
710         {
711                 fh = fopen ("/proc/partitions", "r");
712                 if (fh == NULL)
713                 {
714                         ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
715                         return (-1);
716                 }
717
718                 /* Kernel is 2.4.* */
719                 fieldshift = 1;
720         }
721
722 #if HAVE_LIBUDEV
723         handle_udev = udev_new();
724 #endif
725
726         while (fgets (buffer, sizeof (buffer), fh) != NULL)
727         {
728                 char *disk_name;
729                 char *output_name;
730                 char *alt_name;
731
732                 numfields = strsplit (buffer, fields, 32);
733
734                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
735                         continue;
736
737                 minor = atoll (fields[1]);
738
739                 disk_name = fields[2 + fieldshift];
740
741                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
742                         if (strcmp (disk_name, ds->name) == 0)
743                                 break;
744
745                 if (ds == NULL)
746                 {
747                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
748                                 continue;
749
750                         if ((ds->name = strdup (disk_name)) == NULL)
751                         {
752                                 free (ds);
753                                 continue;
754                         }
755
756                         if (pre_ds == NULL)
757                                 disklist = ds;
758                         else
759                                 pre_ds->next = ds;
760                 }
761
762                 is_disk = 0;
763                 if (numfields == 7)
764                 {
765                         /* Kernel 2.6, Partition */
766                         read_ops      = atoll (fields[3]);
767                         read_sectors  = atoll (fields[4]);
768                         write_ops     = atoll (fields[5]);
769                         write_sectors = atoll (fields[6]);
770                 }
771                 else if (numfields == (14 + fieldshift))
772                 {
773                         read_ops  =  atoll (fields[3 + fieldshift]);
774                         write_ops =  atoll (fields[7 + fieldshift]);
775
776                         read_sectors  = atoll (fields[5 + fieldshift]);
777                         write_sectors = atoll (fields[9 + fieldshift]);
778
779                         if ((fieldshift == 0) || (minor == 0))
780                         {
781                                 is_disk = 1;
782                                 read_merged  = atoll (fields[4 + fieldshift]);
783                                 read_time    = atoll (fields[6 + fieldshift]);
784                                 write_merged = atoll (fields[8 + fieldshift]);
785                                 write_time   = atoll (fields[10+ fieldshift]);
786
787                                 in_progress = atof (fields[11 + fieldshift]);
788
789                                 io_time       = atof (fields[12 + fieldshift]);
790                                 weighted_time = atof (fields[13 + fieldshift]);
791                         }
792                 }
793                 else
794                 {
795                         DEBUG ("numfields = %i; => unknown file format.", numfields);
796                         continue;
797                 }
798
799                 {
800                         derive_t diff_read_sectors;
801                         derive_t diff_write_sectors;
802
803                 /* If the counter wraps around, it's only 32 bits.. */
804                         if (read_sectors < ds->read_sectors)
805                                 diff_read_sectors = 1 + read_sectors
806                                         + (UINT_MAX - ds->read_sectors);
807                         else
808                                 diff_read_sectors = read_sectors - ds->read_sectors;
809                         if (write_sectors < ds->write_sectors)
810                                 diff_write_sectors = 1 + write_sectors
811                                         + (UINT_MAX - ds->write_sectors);
812                         else
813                                 diff_write_sectors = write_sectors - ds->write_sectors;
814
815                         ds->read_bytes += 512 * diff_read_sectors;
816                         ds->write_bytes += 512 * diff_write_sectors;
817                         ds->read_sectors = read_sectors;
818                         ds->write_sectors = write_sectors;
819                 }
820
821                 /* Calculate the average time an io-op needs to complete */
822                 if (is_disk)
823                 {
824                         derive_t diff_read_ops;
825                         derive_t diff_write_ops;
826                         derive_t diff_read_time;
827                         derive_t diff_write_time;
828
829                         if (read_ops < ds->read_ops)
830                                 diff_read_ops = 1 + read_ops
831                                         + (UINT_MAX - ds->read_ops);
832                         else
833                                 diff_read_ops = read_ops - ds->read_ops;
834                         DEBUG ("disk plugin: disk_name = %s; read_ops = %"PRIi64"; "
835                                         "ds->read_ops = %"PRIi64"; diff_read_ops = %"PRIi64";",
836                                         disk_name,
837                                         read_ops, ds->read_ops, diff_read_ops);
838
839                         if (write_ops < ds->write_ops)
840                                 diff_write_ops = 1 + write_ops
841                                         + (UINT_MAX - ds->write_ops);
842                         else
843                                 diff_write_ops = write_ops - ds->write_ops;
844
845                         if (read_time < ds->read_time)
846                                 diff_read_time = 1 + read_time
847                                         + (UINT_MAX - ds->read_time);
848                         else
849                                 diff_read_time = read_time - ds->read_time;
850
851                         if (write_time < ds->write_time)
852                                 diff_write_time = 1 + write_time
853                                         + (UINT_MAX - ds->write_time);
854                         else
855                                 diff_write_time = write_time - ds->write_time;
856
857                         if (diff_read_ops != 0)
858                                 ds->avg_read_time += disk_calc_time_incr (
859                                                 diff_read_time, diff_read_ops);
860                         if (diff_write_ops != 0)
861                                 ds->avg_write_time += disk_calc_time_incr (
862                                                 diff_write_time, diff_write_ops);
863
864                         ds->read_ops = read_ops;
865                         ds->read_time = read_time;
866                         ds->write_ops = write_ops;
867                         ds->write_time = write_time;
868                 } /* if (is_disk) */
869
870                 /* Don't write to the RRDs if we've just started.. */
871                 ds->poll_count++;
872                 if (ds->poll_count <= 2)
873                 {
874                         DEBUG ("disk plugin: (ds->poll_count = %i) <= "
875                                         "(min_poll_count = 2); => Not writing.",
876                                         ds->poll_count);
877                         continue;
878                 }
879
880                 if ((read_ops == 0) && (write_ops == 0))
881                 {
882                         DEBUG ("disk plugin: ((read_ops == 0) && "
883                                         "(write_ops == 0)); => Not writing.");
884                         continue;
885                 }
886
887                 output_name = disk_name;
888
889 #if HAVE_LIBUDEV
890                 alt_name = disk_udev_attr_name (handle_udev, disk_name,
891                                 conf_udev_name_attr);
892 #else
893                 alt_name = NULL;
894 #endif
895                 if (alt_name != NULL)
896                         output_name = alt_name;
897
898                 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
899                         disk_submit (output_name, "disk_octets",
900                                         ds->read_bytes, ds->write_bytes);
901
902                 if ((ds->read_ops != 0) || (ds->write_ops != 0))
903                         disk_submit (output_name, "disk_ops",
904                                         read_ops, write_ops);
905
906                 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
907                         disk_submit (output_name, "disk_time",
908                                         ds->avg_read_time, ds->avg_write_time);
909
910                 if (is_disk)
911                 {
912                         disk_submit (output_name, "disk_merged",
913                                         read_merged, write_merged);
914                         submit_in_progress (output_name, in_progress);
915                         submit_io_time (output_name, io_time, weighted_time);
916                 } /* if (is_disk) */
917
918                 /* release udev-based alternate name, if allocated */
919                 free(alt_name);
920         } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
921
922 #if HAVE_LIBUDEV
923         udev_unref(handle_udev);
924 #endif
925
926         fclose (fh);
927 /* #endif defined(KERNEL_LINUX) */
928
929 #elif HAVE_LIBKSTAT
930 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
931 #  define KIO_ROCTETS reads
932 #  define KIO_WOCTETS writes
933 #  define KIO_ROPS    nreads
934 #  define KIO_WOPS    nwrites
935 #  define KIO_RTIME   rtime
936 #  define KIO_WTIME   wtime
937 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
938 #  define KIO_ROCTETS nread
939 #  define KIO_WOCTETS nwritten
940 #  define KIO_ROPS    reads
941 #  define KIO_WOPS    writes
942 #  define KIO_RTIME   rtime
943 #  define KIO_WTIME   wtime
944 # else
945 #  error "kstat_io_t does not have the required members"
946 # endif
947         static kstat_io_t kio;
948         int i;
949
950         if (kc == NULL)
951                 return (-1);
952
953         for (i = 0; i < numdisk; i++)
954         {
955                 if (kstat_read (kc, ksp[i], &kio) == -1)
956                         continue;
957
958                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
959                 {
960                         disk_submit (ksp[i]->ks_name, "disk_octets",
961                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
962                         disk_submit (ksp[i]->ks_name, "disk_ops",
963                                         kio.KIO_ROPS, kio.KIO_WOPS);
964                         /* FIXME: Convert this to microseconds if necessary */
965                         disk_submit (ksp[i]->ks_name, "disk_time",
966                                         kio.KIO_RTIME, kio.KIO_WTIME);
967                 }
968                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
969                 {
970                         disk_submit (ksp[i]->ks_name, "disk_octets",
971                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
972                         disk_submit (ksp[i]->ks_name, "disk_ops",
973                                         kio.KIO_ROPS, kio.KIO_WOPS);
974                 }
975         }
976 /* #endif defined(HAVE_LIBKSTAT) */
977
978 #elif defined(HAVE_LIBSTATGRAB)
979         sg_disk_io_stats *ds;
980 # if HAVE_LIBSTATGRAB_0_90
981         size_t disks;
982 # else
983         int disks;
984 #endif
985         int counter;
986         char name[DATA_MAX_NAME_LEN];
987         
988         if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
989                 return (0);
990                 
991         for (counter=0; counter < disks; counter++) {
992                 strncpy(name, ds->disk_name, sizeof(name));
993                 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
994                 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
995                 ds++;
996         }
997 /* #endif defined(HAVE_LIBSTATGRAB) */
998
999 #elif defined(HAVE_PERFSTAT)
1000         derive_t read_sectors;
1001         derive_t write_sectors;
1002         derive_t read_time;
1003         derive_t write_time;
1004         derive_t read_ops;
1005         derive_t write_ops;
1006         perfstat_id_t firstpath;
1007         int rnumdisk;
1008         int i;
1009
1010         if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0) 
1011         {
1012                 char errbuf[1024];
1013                 WARNING ("disk plugin: perfstat_disk: %s",
1014                                 sstrerror (errno, errbuf, sizeof (errbuf)));
1015                 return (-1);
1016         }
1017
1018         if (numdisk != pnumdisk || stat_disk==NULL) {
1019                 if (stat_disk!=NULL) 
1020                         free(stat_disk);
1021                 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
1022         } 
1023         pnumdisk = numdisk;
1024
1025         firstpath.name[0]='\0';
1026         if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0) 
1027         {
1028                 char errbuf[1024];
1029                 WARNING ("disk plugin: perfstat_disk : %s",
1030                                 sstrerror (errno, errbuf, sizeof (errbuf)));
1031                 return (-1);
1032         }
1033
1034         for (i = 0; i < rnumdisk; i++) 
1035         {
1036                 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
1037                 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
1038                 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
1039
1040                 read_ops = stat_disk[i].xrate;
1041                 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
1042                 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
1043
1044                 read_time = stat_disk[i].rserv;
1045                 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
1046                 write_time = stat_disk[i].wserv;
1047                 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
1048                 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
1049         }
1050 #endif /* defined(HAVE_PERFSTAT) */
1051
1052         return (0);
1053 } /* int disk_read */
1054
1055 void module_register (void)
1056 {
1057   plugin_register_config ("disk", disk_config,
1058       config_keys, config_keys_num);
1059   plugin_register_init ("disk", disk_init);
1060   plugin_register_read ("disk", disk_read);
1061 } /* void module_register */