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