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