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