Merge branch 'collectd-5.1'
[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 256
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 static const char *config_keys[] =
132 {
133         "Disk",
134         "UseBSDName",
135         "IgnoreSelected"
136 };
137 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
138
139 static ignorelist_t *ignorelist = NULL;
140
141 static int disk_config (const char *key, const char *value)
142 {
143   if (ignorelist == NULL)
144     ignorelist = ignorelist_create (/* invert = */ 1);
145   if (ignorelist == NULL)
146     return (1);
147
148   if (strcasecmp ("Disk", key) == 0)
149   {
150     ignorelist_add (ignorelist, value);
151   }
152   else if (strcasecmp ("IgnoreSelected", key) == 0)
153   {
154     int invert = 1;
155     if (IS_TRUE (value))
156       invert = 0;
157     ignorelist_set_invert (ignorelist, invert);
158   }
159   else if (strcasecmp ("UseBSDName", key) == 0)
160   {
161 #if HAVE_IOKIT_IOKITLIB_H
162     use_bsd_name = IS_TRUE (value) ? 1 : 0;
163 #else
164     WARNING ("disk plugin: The \"UseBSDName\" option is only supported "
165         "on Mach / Mac OS X and will be ignored.");
166 #endif
167   }
168   else
169   {
170     return (-1);
171   }
172
173   return (0);
174 } /* int disk_config */
175
176 static int disk_init (void)
177 {
178 #if HAVE_IOKIT_IOKITLIB_H
179         kern_return_t status;
180
181         if (io_master_port != MACH_PORT_NULL)
182         {
183                 mach_port_deallocate (mach_task_self (),
184                                 io_master_port);
185                 io_master_port = MACH_PORT_NULL;
186         }
187
188         status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
189         if (status != kIOReturnSuccess)
190         {
191                 ERROR ("IOMasterPort failed: %s",
192                                 mach_error_string (status));
193                 io_master_port = MACH_PORT_NULL;
194                 return (-1);
195         }
196 /* #endif HAVE_IOKIT_IOKITLIB_H */
197
198 #elif KERNEL_LINUX
199         /* do nothing */
200 /* #endif KERNEL_LINUX */
201
202 #elif HAVE_LIBKSTAT
203         kstat_t *ksp_chain;
204
205         numdisk = 0;
206
207         if (kc == NULL)
208                 return (-1);
209
210         for (numdisk = 0, ksp_chain = kc->kc_chain;
211                         (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
212                         ksp_chain = ksp_chain->ks_next)
213         {
214                 if (strncmp (ksp_chain->ks_class, "disk", 4)
215                                 && strncmp (ksp_chain->ks_class, "partition", 9))
216                         continue;
217                 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
218                         continue;
219                 ksp[numdisk++] = ksp_chain;
220         }
221 #endif /* HAVE_LIBKSTAT */
222
223         return (0);
224 } /* int disk_init */
225
226 static void disk_submit (const char *plugin_instance,
227                 const char *type,
228                 derive_t read, derive_t write)
229 {
230         value_t values[2];
231         value_list_t vl = VALUE_LIST_INIT;
232
233         /* Both `ignorelist' and `plugin_instance' may be NULL. */
234         if (ignorelist_match (ignorelist, plugin_instance) != 0)
235           return;
236
237         values[0].derive = read;
238         values[1].derive = write;
239
240         vl.values = values;
241         vl.values_len = 2;
242         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
243         sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
244         sstrncpy (vl.plugin_instance, plugin_instance,
245                         sizeof (vl.plugin_instance));
246         sstrncpy (vl.type, type, sizeof (vl.type));
247
248         plugin_dispatch_values (&vl);
249 } /* void disk_submit */
250
251 #if KERNEL_LINUX
252 static counter_t disk_calc_time_incr (counter_t delta_time, counter_t delta_ops)
253 {
254         double avg_time = ((double) delta_time) / ((double) delta_ops);
255         double avg_time_incr = CDTIME_T_TO_DOUBLE (interval_g) * avg_time;
256
257         return ((counter_t) (avg_time_incr + .5));
258 }
259 #endif
260
261 #if HAVE_IOKIT_IOKITLIB_H
262 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
263 {
264         signed long long val_int;
265         CFNumberRef      val_obj;
266         CFStringRef      key_obj;
267
268         /* `key_obj' needs to be released. */
269         key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
270                         kCFStringEncodingASCII);
271         if (key_obj == NULL)
272         {
273                 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
274                 return (-1LL);
275         }
276         
277         /* get => we don't need to release (== free) the object */
278         val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
279
280         CFRelease (key_obj);
281
282         if (val_obj == NULL)
283         {
284                 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
285                 return (-1LL);
286         }
287
288         if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
289         {
290                 DEBUG ("CFNumberGetValue (%s) failed.", key);
291                 return (-1LL);
292         }
293
294         return (val_int);
295 }
296 #endif /* HAVE_IOKIT_IOKITLIB_H */
297
298 static int disk_read (void)
299 {
300 #if HAVE_IOKIT_IOKITLIB_H
301         io_registry_entry_t     disk;
302         io_registry_entry_t     disk_child;
303         io_iterator_t           disk_list;
304         CFDictionaryRef         props_dict;
305         CFDictionaryRef         stats_dict;
306         CFDictionaryRef         child_dict;
307         CFStringRef             tmp_cf_string_ref;
308         kern_return_t           status;
309
310         signed long long read_ops;
311         signed long long read_byt;
312         signed long long read_tme;
313         signed long long write_ops;
314         signed long long write_byt;
315         signed long long write_tme;
316
317         int  disk_major;
318         int  disk_minor;
319         char disk_name[DATA_MAX_NAME_LEN];
320         char disk_name_bsd[DATA_MAX_NAME_LEN];
321
322         /* Get the list of all disk objects. */
323         if (IOServiceGetMatchingServices (io_master_port,
324                                 IOServiceMatching (kIOBlockStorageDriverClass),
325                                 &disk_list) != kIOReturnSuccess)
326         {
327                 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
328                 return (-1);
329         }
330
331         while ((disk = IOIteratorNext (disk_list)) != 0)
332         {
333                 props_dict = NULL;
334                 stats_dict = NULL;
335                 child_dict = NULL;
336
337                 /* `disk_child' must be released */
338                 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
339                                 != kIOReturnSuccess)
340                 {
341                         /* This fails for example for DVD/CD drives.. */
342                         DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
343                         IOObjectRelease (disk);
344                         continue;
345                 }
346
347                 /* We create `props_dict' => we need to release it later */
348                 if (IORegistryEntryCreateCFProperties (disk,
349                                         (CFMutableDictionaryRef *) &props_dict,
350                                         kCFAllocatorDefault,
351                                         kNilOptions)
352                                 != kIOReturnSuccess)
353                 {
354                         ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
355                         IOObjectRelease (disk_child);
356                         IOObjectRelease (disk);
357                         continue;
358                 }
359
360                 if (props_dict == NULL)
361                 {
362                         DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
363                         IOObjectRelease (disk_child);
364                         IOObjectRelease (disk);
365                         continue;
366                 }
367
368                 /* tmp_cf_string_ref doesn't need to be released. */
369                 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (props_dict,
370                                 CFSTR(kIOBSDNameKey));
371                 if (!tmp_cf_string_ref)
372                 {
373                         DEBUG ("disk plugin: CFDictionaryGetValue("
374                                         "kIOBSDNameKey) failed.");
375                         CFRelease (props_dict);
376                         IOObjectRelease (disk_child);
377                         IOObjectRelease (disk);
378                         continue;
379                 }
380                 assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
381
382                 memset (disk_name_bsd, 0, sizeof (disk_name_bsd));
383                 CFStringGetCString (tmp_cf_string_ref,
384                                 disk_name_bsd, sizeof (disk_name_bsd),
385                                 kCFStringEncodingUTF8);
386                 if (disk_name_bsd[0] == 0)
387                 {
388                         ERROR ("disk plugin: CFStringGetCString() failed.");
389                         CFRelease (props_dict);
390                         IOObjectRelease (disk_child);
391                         IOObjectRelease (disk);
392                         continue;
393                 }
394                 DEBUG ("disk plugin: disk_name_bsd = \"%s\"", disk_name_bsd);
395
396                 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
397                                 CFSTR (kIOBlockStorageDriverStatisticsKey));
398
399                 if (stats_dict == NULL)
400                 {
401                         DEBUG ("disk plugin: CFDictionaryGetValue ("
402                                         "%s) failed.",
403                                         kIOBlockStorageDriverStatisticsKey);
404                         CFRelease (props_dict);
405                         IOObjectRelease (disk_child);
406                         IOObjectRelease (disk);
407                         continue;
408                 }
409
410                 if (IORegistryEntryCreateCFProperties (disk_child,
411                                         (CFMutableDictionaryRef *) &child_dict,
412                                         kCFAllocatorDefault,
413                                         kNilOptions)
414                                 != kIOReturnSuccess)
415                 {
416                         DEBUG ("disk plugin: IORegistryEntryCreateCFProperties ("
417                                         "disk_child) failed.");
418                         IOObjectRelease (disk_child);
419                         CFRelease (props_dict);
420                         IOObjectRelease (disk);
421                         continue;
422                 }
423
424                 /* kIOBSDNameKey */
425                 disk_major = (int) dict_get_value (child_dict,
426                                 kIOBSDMajorKey);
427                 disk_minor = (int) dict_get_value (child_dict,
428                                 kIOBSDMinorKey);
429                 read_ops  = dict_get_value (stats_dict,
430                                 kIOBlockStorageDriverStatisticsReadsKey);
431                 read_byt  = dict_get_value (stats_dict,
432                                 kIOBlockStorageDriverStatisticsBytesReadKey);
433                 read_tme  = dict_get_value (stats_dict,
434                                 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
435                 write_ops = dict_get_value (stats_dict,
436                                 kIOBlockStorageDriverStatisticsWritesKey);
437                 write_byt = dict_get_value (stats_dict,
438                                 kIOBlockStorageDriverStatisticsBytesWrittenKey);
439                 /* This property describes the number of nanoseconds spent
440                  * performing writes since the block storage driver was
441                  * instantiated. It is one of the statistic entries listed
442                  * under the top-level kIOBlockStorageDriverStatisticsKey
443                  * property table. It has an OSNumber value. */
444                 write_tme = dict_get_value (stats_dict,
445                                 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
446
447                 if (use_bsd_name)
448                         sstrncpy (disk_name, disk_name_bsd, sizeof (disk_name));
449                 else
450                         ssnprintf (disk_name, sizeof (disk_name), "%i-%i",
451                                         disk_major, disk_minor);
452                 DEBUG ("disk plugin: disk_name = \"%s\"", disk_name);
453
454                 if ((read_byt != -1LL) || (write_byt != -1LL))
455                         disk_submit (disk_name, "disk_octets", read_byt, write_byt);
456                 if ((read_ops != -1LL) || (write_ops != -1LL))
457                         disk_submit (disk_name, "disk_ops", read_ops, write_ops);
458                 if ((read_tme != -1LL) || (write_tme != -1LL))
459                         disk_submit (disk_name, "disk_time",
460                                         read_tme / 1000,
461                                         write_tme / 1000);
462
463                 CFRelease (child_dict);
464                 IOObjectRelease (disk_child);
465                 CFRelease (props_dict);
466                 IOObjectRelease (disk);
467         }
468         IOObjectRelease (disk_list);
469 /* #endif HAVE_IOKIT_IOKITLIB_H */
470
471 #elif KERNEL_LINUX
472         FILE *fh;
473         char buffer[1024];
474         
475         char *fields[32];
476         int numfields;
477         int fieldshift = 0;
478
479         int minor = 0;
480
481         derive_t read_sectors  = 0;
482         derive_t write_sectors = 0;
483
484         derive_t read_ops      = 0;
485         derive_t read_merged   = 0;
486         derive_t read_time     = 0;
487         derive_t write_ops     = 0;
488         derive_t write_merged  = 0;
489         derive_t write_time    = 0;
490         int is_disk = 0;
491
492         diskstats_t *ds, *pre_ds;
493
494         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
495         {
496                 fh = fopen ("/proc/partitions", "r");
497                 if (fh == NULL)
498                 {
499                         ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
500                         return (-1);
501                 }
502
503                 /* Kernel is 2.4.* */
504                 fieldshift = 1;
505         }
506
507         while (fgets (buffer, sizeof (buffer), fh) != NULL)
508         {
509                 char *disk_name;
510
511                 numfields = strsplit (buffer, fields, 32);
512
513                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
514                         continue;
515
516                 minor = atoll (fields[1]);
517
518                 disk_name = fields[2 + fieldshift];
519
520                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
521                         if (strcmp (disk_name, ds->name) == 0)
522                                 break;
523
524                 if (ds == NULL)
525                 {
526                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
527                                 continue;
528
529                         if ((ds->name = strdup (disk_name)) == NULL)
530                         {
531                                 free (ds);
532                                 continue;
533                         }
534
535                         if (pre_ds == NULL)
536                                 disklist = ds;
537                         else
538                                 pre_ds->next = ds;
539                 }
540
541                 is_disk = 0;
542                 if (numfields == 7)
543                 {
544                         /* Kernel 2.6, Partition */
545                         read_ops      = atoll (fields[3]);
546                         read_sectors  = atoll (fields[4]);
547                         write_ops     = atoll (fields[5]);
548                         write_sectors = atoll (fields[6]);
549                 }
550                 else if (numfields == (14 + fieldshift))
551                 {
552                         read_ops  =  atoll (fields[3 + fieldshift]);
553                         write_ops =  atoll (fields[7 + fieldshift]);
554
555                         read_sectors  = atoll (fields[5 + fieldshift]);
556                         write_sectors = atoll (fields[9 + fieldshift]);
557
558                         if ((fieldshift == 0) || (minor == 0))
559                         {
560                                 is_disk = 1;
561                                 read_merged  = atoll (fields[4 + fieldshift]);
562                                 read_time    = atoll (fields[6 + fieldshift]);
563                                 write_merged = atoll (fields[8 + fieldshift]);
564                                 write_time   = atoll (fields[10+ fieldshift]);
565                         }
566                 }
567                 else
568                 {
569                         DEBUG ("numfields = %i; => unknown file format.", numfields);
570                         continue;
571                 }
572
573                 {
574                         derive_t diff_read_sectors;
575                         derive_t diff_write_sectors;
576
577                 /* If the counter wraps around, it's only 32 bits.. */
578                         if (read_sectors < ds->read_sectors)
579                                 diff_read_sectors = 1 + read_sectors
580                                         + (UINT_MAX - ds->read_sectors);
581                         else
582                                 diff_read_sectors = read_sectors - ds->read_sectors;
583                         if (write_sectors < ds->write_sectors)
584                                 diff_write_sectors = 1 + write_sectors
585                                         + (UINT_MAX - ds->write_sectors);
586                         else
587                                 diff_write_sectors = write_sectors - ds->write_sectors;
588
589                         ds->read_bytes += 512 * diff_read_sectors;
590                         ds->write_bytes += 512 * diff_write_sectors;
591                         ds->read_sectors = read_sectors;
592                         ds->write_sectors = write_sectors;
593                 }
594
595                 /* Calculate the average time an io-op needs to complete */
596                 if (is_disk)
597                 {
598                         derive_t diff_read_ops;
599                         derive_t diff_write_ops;
600                         derive_t diff_read_time;
601                         derive_t diff_write_time;
602
603                         if (read_ops < ds->read_ops)
604                                 diff_read_ops = 1 + read_ops
605                                         + (UINT_MAX - ds->read_ops);
606                         else
607                                 diff_read_ops = read_ops - ds->read_ops;
608                         DEBUG ("disk plugin: disk_name = %s; read_ops = %"PRIi64"; "
609                                         "ds->read_ops = %"PRIi64"; diff_read_ops = %"PRIi64";",
610                                         disk_name,
611                                         read_ops, ds->read_ops, diff_read_ops);
612
613                         if (write_ops < ds->write_ops)
614                                 diff_write_ops = 1 + write_ops
615                                         + (UINT_MAX - ds->write_ops);
616                         else
617                                 diff_write_ops = write_ops - ds->write_ops;
618
619                         if (read_time < ds->read_time)
620                                 diff_read_time = 1 + read_time
621                                         + (UINT_MAX - ds->read_time);
622                         else
623                                 diff_read_time = read_time - ds->read_time;
624
625                         if (write_time < ds->write_time)
626                                 diff_write_time = 1 + write_time
627                                         + (UINT_MAX - ds->write_time);
628                         else
629                                 diff_write_time = write_time - ds->write_time;
630
631                         if (diff_read_ops != 0)
632                                 ds->avg_read_time += disk_calc_time_incr (
633                                                 diff_read_time, diff_read_ops);
634                         if (diff_write_ops != 0)
635                                 ds->avg_write_time += disk_calc_time_incr (
636                                                 diff_write_time, diff_write_ops);
637
638                         ds->read_ops = read_ops;
639                         ds->read_time = read_time;
640                         ds->write_ops = write_ops;
641                         ds->write_time = write_time;
642                 } /* if (is_disk) */
643
644                 /* Don't write to the RRDs if we've just started.. */
645                 ds->poll_count++;
646                 if (ds->poll_count <= 2)
647                 {
648                         DEBUG ("disk plugin: (ds->poll_count = %i) <= "
649                                         "(min_poll_count = 2); => Not writing.",
650                                         ds->poll_count);
651                         continue;
652                 }
653
654                 if ((read_ops == 0) && (write_ops == 0))
655                 {
656                         DEBUG ("disk plugin: ((read_ops == 0) && "
657                                         "(write_ops == 0)); => Not writing.");
658                         continue;
659                 }
660
661                 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
662                         disk_submit (disk_name, "disk_octets",
663                                         ds->read_bytes, ds->write_bytes);
664
665                 if ((ds->read_ops != 0) || (ds->write_ops != 0))
666                         disk_submit (disk_name, "disk_ops",
667                                         read_ops, write_ops);
668
669                 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
670                         disk_submit (disk_name, "disk_time",
671                                         ds->avg_read_time, ds->avg_write_time);
672
673                 if (is_disk)
674                 {
675                         disk_submit (disk_name, "disk_merged",
676                                         read_merged, write_merged);
677                 } /* if (is_disk) */
678         } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
679
680         fclose (fh);
681 /* #endif defined(KERNEL_LINUX) */
682
683 #elif HAVE_LIBKSTAT
684 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
685 #  define KIO_ROCTETS reads
686 #  define KIO_WOCTETS writes
687 #  define KIO_ROPS    nreads
688 #  define KIO_WOPS    nwrites
689 #  define KIO_RTIME   rtime
690 #  define KIO_WTIME   wtime
691 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
692 #  define KIO_ROCTETS nread
693 #  define KIO_WOCTETS nwritten
694 #  define KIO_ROPS    reads
695 #  define KIO_WOPS    writes
696 #  define KIO_RTIME   rtime
697 #  define KIO_WTIME   wtime
698 # else
699 #  error "kstat_io_t does not have the required members"
700 # endif
701         static kstat_io_t kio;
702         int i;
703
704         if (kc == NULL)
705                 return (-1);
706
707         for (i = 0; i < numdisk; i++)
708         {
709                 if (kstat_read (kc, ksp[i], &kio) == -1)
710                         continue;
711
712                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
713                 {
714                         disk_submit (ksp[i]->ks_name, "disk_octets",
715                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
716                         disk_submit (ksp[i]->ks_name, "disk_ops",
717                                         kio.KIO_ROPS, kio.KIO_WOPS);
718                         /* FIXME: Convert this to microseconds if necessary */
719                         disk_submit (ksp[i]->ks_name, "disk_time",
720                                         kio.KIO_RTIME, kio.KIO_WTIME);
721                 }
722                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
723                 {
724                         disk_submit (ksp[i]->ks_name, "disk_octets",
725                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
726                         disk_submit (ksp[i]->ks_name, "disk_ops",
727                                         kio.KIO_ROPS, kio.KIO_WOPS);
728                 }
729         }
730 /* #endif defined(HAVE_LIBKSTAT) */
731
732 #elif defined(HAVE_LIBSTATGRAB)
733         sg_disk_io_stats *ds;
734         int disks, counter;
735         char name[DATA_MAX_NAME_LEN];
736         
737         if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
738                 return (0);
739                 
740         for (counter=0; counter < disks; counter++) {
741                 strncpy(name, ds->disk_name, sizeof(name));
742                 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
743                 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
744                 ds++;
745         }
746 /* #endif defined(HAVE_LIBSTATGRAB) */
747
748 #elif defined(HAVE_PERFSTAT)
749         derive_t read_sectors;
750         derive_t write_sectors;
751         derive_t read_time;
752         derive_t write_time;
753         derive_t read_ops;
754         derive_t write_ops;
755         perfstat_id_t firstpath;
756         int rnumdisk;
757         int i;
758
759         if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0) 
760         {
761                 char errbuf[1024];
762                 WARNING ("disk plugin: perfstat_disk: %s",
763                                 sstrerror (errno, errbuf, sizeof (errbuf)));
764                 return (-1);
765         }
766
767         if (numdisk != pnumdisk || stat_disk==NULL) {
768                 if (stat_disk!=NULL) 
769                         free(stat_disk);
770                 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
771         } 
772         pnumdisk = numdisk;
773
774         firstpath.name[0]='\0';
775         if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0) 
776         {
777                 char errbuf[1024];
778                 WARNING ("disk plugin: perfstat_disk : %s",
779                                 sstrerror (errno, errbuf, sizeof (errbuf)));
780                 return (-1);
781         }
782
783         for (i = 0; i < rnumdisk; i++) 
784         {
785                 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
786                 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
787                 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
788
789                 read_ops = stat_disk[i].xrate;
790                 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
791                 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
792
793                 read_time = stat_disk[i].rserv;
794                 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
795                 write_time = stat_disk[i].wserv;
796                 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
797                 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
798         }
799 #endif /* defined(HAVE_PERFSTAT) */
800
801         return (0);
802 } /* int disk_read */
803
804 void module_register (void)
805 {
806   plugin_register_config ("disk", disk_config,
807       config_keys, config_keys_num);
808   plugin_register_init ("disk", disk_init);
809   plugin_register_read ("disk", disk_read);
810 } /* void module_register */