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