Varnish plugin: Set the instance name as plugin instance.
[collectd.git] / src / disk.c
1 /**
2  * collectd - src/disk.c
3  * Copyright (C) 2005-2008  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         counter_t read_sectors;
89         counter_t write_sectors;
90
91         counter_t read_bytes;
92         counter_t write_bytes;
93
94         counter_t read_ops;
95         counter_t write_ops;
96         counter_t read_time;
97         counter_t write_time;
98
99         counter_t avg_read_time;
100         counter_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                 counter_t read, counter_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].counter = read;
225         values[1].counter = 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 HAVE_IOKIT_IOKITLIB_H
239 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
240 {
241         signed long long val_int;
242         CFNumberRef      val_obj;
243         CFStringRef      key_obj;
244
245         /* `key_obj' needs to be released. */
246         key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
247                         kCFStringEncodingASCII);
248         if (key_obj == NULL)
249         {
250                 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
251                 return (-1LL);
252         }
253         
254         /* get => we don't need to release (== free) the object */
255         val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
256
257         CFRelease (key_obj);
258
259         if (val_obj == NULL)
260         {
261                 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
262                 return (-1LL);
263         }
264
265         if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
266         {
267                 DEBUG ("CFNumberGetValue (%s) failed.", key);
268                 return (-1LL);
269         }
270
271         return (val_int);
272 }
273 #endif /* HAVE_IOKIT_IOKITLIB_H */
274
275 static int disk_read (void)
276 {
277 #if HAVE_IOKIT_IOKITLIB_H
278         io_registry_entry_t     disk;
279         io_registry_entry_t     disk_child;
280         io_iterator_t           disk_list;
281         CFDictionaryRef         props_dict;
282         CFDictionaryRef         stats_dict;
283         CFDictionaryRef         child_dict;
284         kern_return_t           status;
285
286         signed long long read_ops;
287         signed long long read_byt;
288         signed long long read_tme;
289         signed long long write_ops;
290         signed long long write_byt;
291         signed long long write_tme;
292
293         int  disk_major;
294         int  disk_minor;
295         char disk_name[64];
296
297         /* Get the list of all disk objects. */
298         if (IOServiceGetMatchingServices (io_master_port,
299                                 IOServiceMatching (kIOBlockStorageDriverClass),
300                                 &disk_list) != kIOReturnSuccess)
301         {
302                 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
303                 return (-1);
304         }
305
306         while ((disk = IOIteratorNext (disk_list)) != 0)
307         {
308                 props_dict = NULL;
309                 stats_dict = NULL;
310                 child_dict = NULL;
311
312                 /* `disk_child' must be released */
313                 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
314                                 != kIOReturnSuccess)
315                 {
316                         /* This fails for example for DVD/CD drives.. */
317                         DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
318                         IOObjectRelease (disk);
319                         continue;
320                 }
321
322                 /* We create `props_dict' => we need to release it later */
323                 if (IORegistryEntryCreateCFProperties (disk,
324                                         (CFMutableDictionaryRef *) &props_dict,
325                                         kCFAllocatorDefault,
326                                         kNilOptions)
327                                 != kIOReturnSuccess)
328                 {
329                         ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
330                         IOObjectRelease (disk_child);
331                         IOObjectRelease (disk);
332                         continue;
333                 }
334
335                 if (props_dict == NULL)
336                 {
337                         DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
338                         IOObjectRelease (disk_child);
339                         IOObjectRelease (disk);
340                         continue;
341                 }
342
343                 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
344                                 CFSTR (kIOBlockStorageDriverStatisticsKey));
345
346                 if (stats_dict == NULL)
347                 {
348                         DEBUG ("CFDictionaryGetValue (%s) failed.",
349                                         kIOBlockStorageDriverStatisticsKey);
350                         CFRelease (props_dict);
351                         IOObjectRelease (disk_child);
352                         IOObjectRelease (disk);
353                         continue;
354                 }
355
356                 if (IORegistryEntryCreateCFProperties (disk_child,
357                                         (CFMutableDictionaryRef *) &child_dict,
358                                         kCFAllocatorDefault,
359                                         kNilOptions)
360                                 != kIOReturnSuccess)
361                 {
362                         DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
363                         IOObjectRelease (disk_child);
364                         CFRelease (props_dict);
365                         IOObjectRelease (disk);
366                         continue;
367                 }
368
369                 /* kIOBSDNameKey */
370                 disk_major = (int) dict_get_value (child_dict,
371                                 kIOBSDMajorKey);
372                 disk_minor = (int) dict_get_value (child_dict,
373                                 kIOBSDMinorKey);
374                 read_ops  = dict_get_value (stats_dict,
375                                 kIOBlockStorageDriverStatisticsReadsKey);
376                 read_byt  = dict_get_value (stats_dict,
377                                 kIOBlockStorageDriverStatisticsBytesReadKey);
378                 read_tme  = dict_get_value (stats_dict,
379                                 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
380                 write_ops = dict_get_value (stats_dict,
381                                 kIOBlockStorageDriverStatisticsWritesKey);
382                 write_byt = dict_get_value (stats_dict,
383                                 kIOBlockStorageDriverStatisticsBytesWrittenKey);
384                 /* This property describes the number of nanoseconds spent
385                  * performing writes since the block storage driver was
386                  * instantiated. It is one of the statistic entries listed
387                  * under the top-level kIOBlockStorageDriverStatisticsKey
388                  * property table. It has an OSNumber value. */
389                 write_tme = dict_get_value (stats_dict,
390                                 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
391
392                 if (ssnprintf (disk_name, sizeof (disk_name),
393                                 "%i-%i", disk_major, disk_minor) >= sizeof (disk_name))
394                 {
395                         DEBUG ("snprintf (major, minor) failed.");
396                         CFRelease (child_dict);
397                         IOObjectRelease (disk_child);
398                         CFRelease (props_dict);
399                         IOObjectRelease (disk);
400                         continue;
401                 }
402                 DEBUG ("disk_name = %s", disk_name);
403
404                 if ((read_byt != -1LL) || (write_byt != -1LL))
405                         disk_submit (disk_name, "disk_octets", read_byt, write_byt);
406                 if ((read_ops != -1LL) || (write_ops != -1LL))
407                         disk_submit (disk_name, "disk_ops", read_ops, write_ops);
408                 if ((read_tme != -1LL) || (write_tme != -1LL))
409                         disk_submit (disk_name, "disk_time",
410                                         read_tme / 1000,
411                                         write_tme / 1000);
412
413                 CFRelease (child_dict);
414                 IOObjectRelease (disk_child);
415                 CFRelease (props_dict);
416                 IOObjectRelease (disk);
417         }
418         IOObjectRelease (disk_list);
419 /* #endif HAVE_IOKIT_IOKITLIB_H */
420
421 #elif KERNEL_LINUX
422         FILE *fh;
423         char buffer[1024];
424         
425         char *fields[32];
426         int numfields;
427         int fieldshift = 0;
428
429         int major = 0;
430         int minor = 0;
431
432         counter_t read_sectors  = 0;
433         counter_t write_sectors = 0;
434
435         counter_t read_ops      = 0;
436         counter_t read_merged   = 0;
437         counter_t read_time     = 0;
438         counter_t write_ops     = 0;
439         counter_t write_merged  = 0;
440         counter_t write_time    = 0;
441         int is_disk = 0;
442
443         diskstats_t *ds, *pre_ds;
444
445         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
446         {
447                 fh = fopen ("/proc/partitions", "r");
448                 if (fh == NULL)
449                 {
450                         ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
451                         return (-1);
452                 }
453
454                 /* Kernel is 2.4.* */
455                 fieldshift = 1;
456         }
457
458         while (fgets (buffer, sizeof (buffer), fh) != NULL)
459         {
460                 char *disk_name;
461
462                 numfields = strsplit (buffer, fields, 32);
463
464                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
465                         continue;
466
467                 major = atoll (fields[0]);
468                 minor = atoll (fields[1]);
469
470                 disk_name = fields[2 + fieldshift];
471
472                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
473                         if (strcmp (disk_name, ds->name) == 0)
474                                 break;
475
476                 if (ds == NULL)
477                 {
478                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
479                                 continue;
480
481                         if ((ds->name = strdup (disk_name)) == NULL)
482                         {
483                                 free (ds);
484                                 continue;
485                         }
486
487                         if (pre_ds == NULL)
488                                 disklist = ds;
489                         else
490                                 pre_ds->next = ds;
491                 }
492
493                 is_disk = 0;
494                 if (numfields == 7)
495                 {
496                         /* Kernel 2.6, Partition */
497                         read_ops      = atoll (fields[3]);
498                         read_sectors  = atoll (fields[4]);
499                         write_ops     = atoll (fields[5]);
500                         write_sectors = atoll (fields[6]);
501                 }
502                 else if (numfields == (14 + fieldshift))
503                 {
504                         read_ops  =  atoll (fields[3 + fieldshift]);
505                         write_ops =  atoll (fields[7 + fieldshift]);
506
507                         read_sectors  = atoll (fields[5 + fieldshift]);
508                         write_sectors = atoll (fields[9 + fieldshift]);
509
510                         if ((fieldshift == 0) || (minor == 0))
511                         {
512                                 is_disk = 1;
513                                 read_merged  = atoll (fields[4 + fieldshift]);
514                                 read_time    = atoll (fields[6 + fieldshift]);
515                                 write_merged = atoll (fields[8 + fieldshift]);
516                                 write_time   = atoll (fields[10+ fieldshift]);
517                         }
518                 }
519                 else
520                 {
521                         DEBUG ("numfields = %i; => unknown file format.", numfields);
522                         continue;
523                 }
524
525                 {
526                         counter_t diff_read_sectors;
527                         counter_t diff_write_sectors;
528
529                 /* If the counter wraps around, it's only 32 bits.. */
530                         if (read_sectors < ds->read_sectors)
531                                 diff_read_sectors = 1 + read_sectors
532                                         + (UINT_MAX - ds->read_sectors);
533                         else
534                                 diff_read_sectors = read_sectors - ds->read_sectors;
535                         if (write_sectors < ds->write_sectors)
536                                 diff_write_sectors = 1 + write_sectors
537                                         + (UINT_MAX - ds->write_sectors);
538                         else
539                                 diff_write_sectors = write_sectors - ds->write_sectors;
540
541                         ds->read_bytes += 512 * diff_read_sectors;
542                         ds->write_bytes += 512 * diff_write_sectors;
543                         ds->read_sectors = read_sectors;
544                         ds->write_sectors = write_sectors;
545                 }
546
547                 /* Calculate the average time an io-op needs to complete */
548                 if (is_disk)
549                 {
550                         counter_t diff_read_ops;
551                         counter_t diff_write_ops;
552                         counter_t diff_read_time;
553                         counter_t diff_write_time;
554
555                         if (read_ops < ds->read_ops)
556                                 diff_read_ops = 1 + read_ops
557                                         + (UINT_MAX - ds->read_ops);
558                         else
559                                 diff_read_ops = read_ops - ds->read_ops;
560                         DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
561                                         "ds->read_ops = %llu; diff_read_ops = %llu;",
562                                         disk_name,
563                                         read_ops, ds->read_ops, diff_read_ops);
564
565                         if (write_ops < ds->write_ops)
566                                 diff_write_ops = 1 + write_ops
567                                         + (UINT_MAX - ds->write_ops);
568                         else
569                                 diff_write_ops = write_ops - ds->write_ops;
570
571                         if (read_time < ds->read_time)
572                                 diff_read_time = 1 + read_time
573                                         + (UINT_MAX - ds->read_time);
574                         else
575                                 diff_read_time = read_time - ds->read_time;
576
577                         if (write_time < ds->write_time)
578                                 diff_write_time = 1 + write_time
579                                         + (UINT_MAX - ds->write_time);
580                         else
581                                 diff_write_time = write_time - ds->write_time;
582
583                         if (diff_read_ops != 0)
584                                 ds->avg_read_time += (diff_read_time
585                                                 + (diff_read_ops / 2))
586                                         / diff_read_ops;
587                         if (diff_write_ops != 0)
588                                 ds->avg_write_time += (diff_write_time
589                                                 + (diff_write_ops / 2))
590                                         / diff_write_ops;
591
592                         ds->read_ops = read_ops;
593                         ds->read_time = read_time;
594                         ds->write_ops = write_ops;
595                         ds->write_time = write_time;
596                 } /* if (is_disk) */
597
598                 /* Don't write to the RRDs if we've just started.. */
599                 ds->poll_count++;
600                 if (ds->poll_count <= 2)
601                 {
602                         DEBUG ("disk plugin: (ds->poll_count = %i) <= "
603                                         "(min_poll_count = 2); => Not writing.",
604                                         ds->poll_count);
605                         continue;
606                 }
607
608                 if ((read_ops == 0) && (write_ops == 0))
609                 {
610                         DEBUG ("disk plugin: ((read_ops == 0) && "
611                                         "(write_ops == 0)); => Not writing.");
612                         continue;
613                 }
614
615                 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
616                         disk_submit (disk_name, "disk_octets",
617                                         ds->read_bytes, ds->write_bytes);
618
619                 if ((ds->read_ops != 0) || (ds->write_ops != 0))
620                         disk_submit (disk_name, "disk_ops",
621                                         read_ops, write_ops);
622
623                 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
624                         disk_submit (disk_name, "disk_time",
625                                         ds->avg_read_time, ds->avg_write_time);
626
627                 if (is_disk)
628                 {
629                         disk_submit (disk_name, "disk_merged",
630                                         read_merged, write_merged);
631                 } /* if (is_disk) */
632         } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
633
634         fclose (fh);
635 /* #endif defined(KERNEL_LINUX) */
636
637 #elif HAVE_LIBKSTAT
638 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
639 #  define KIO_ROCTETS reads
640 #  define KIO_WOCTETS writes
641 #  define KIO_ROPS    nreads
642 #  define KIO_WOPS    nwrites
643 #  define KIO_RTIME   rtime
644 #  define KIO_WTIME   wtime
645 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
646 #  define KIO_ROCTETS nread
647 #  define KIO_WOCTETS nwritten
648 #  define KIO_ROPS    reads
649 #  define KIO_WOPS    writes
650 #  define KIO_RTIME   rtime
651 #  define KIO_WTIME   wtime
652 # else
653 #  error "kstat_io_t does not have the required members"
654 # endif
655         static kstat_io_t kio;
656         int i;
657
658         if (kc == NULL)
659                 return (-1);
660
661         for (i = 0; i < numdisk; i++)
662         {
663                 if (kstat_read (kc, ksp[i], &kio) == -1)
664                         continue;
665
666                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
667                 {
668                         disk_submit (ksp[i]->ks_name, "disk_octets",
669                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
670                         disk_submit (ksp[i]->ks_name, "disk_ops",
671                                         kio.KIO_ROPS, kio.KIO_WOPS);
672                         /* FIXME: Convert this to microseconds if necessary */
673                         disk_submit (ksp[i]->ks_name, "disk_time",
674                                         kio.KIO_RTIME, kio.KIO_WTIME);
675                 }
676                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
677                 {
678                         disk_submit (ksp[i]->ks_name, "disk_octets",
679                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
680                         disk_submit (ksp[i]->ks_name, "disk_ops",
681                                         kio.KIO_ROPS, kio.KIO_WOPS);
682                 }
683         }
684 /* #endif defined(HAVE_LIBKSTAT) */
685
686 #elif defined(HAVE_LIBSTATGRAB)
687         sg_disk_io_stats *ds;
688         int disks, counter;
689         char name[DATA_MAX_NAME_LEN];
690         
691         if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
692                 return (0);
693                 
694         for (counter=0; counter < disks; counter++) {
695                 strncpy(name, ds->disk_name, sizeof(name));
696                 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
697                 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
698                 ds++;
699         }
700 /* #endif defined(HAVE_LIBSTATGRAB) */
701
702 #elif defined(HAVE_PERFSTAT)
703         counter_t read_sectors;
704         counter_t write_sectors;
705         counter_t read_time;
706         counter_t write_time;
707         counter_t read_ops;
708         counter_t write_ops;
709         perfstat_id_t firstpath;
710         int rnumdisk;
711         int i;
712
713         if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0) 
714         {
715                 char errbuf[1024];
716                 WARNING ("disk plugin: perfstat_disk: %s",
717                                 sstrerror (errno, errbuf, sizeof (errbuf)));
718                 return (-1);
719         }
720
721         if (numdisk != pnumdisk || stat_disk==NULL) {
722                 if (stat_disk!=NULL) 
723                         free(stat_disk);
724                 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
725         } 
726         pnumdisk = numdisk;
727
728         firstpath.name[0]='\0';
729         if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0) 
730         {
731                 char errbuf[1024];
732                 WARNING ("disk plugin: perfstat_disk : %s",
733                                 sstrerror (errno, errbuf, sizeof (errbuf)));
734                 return (-1);
735         }
736
737         for (i = 0; i < rnumdisk; i++) 
738         {
739                 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
740                 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
741                 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
742
743                 read_ops = stat_disk[i].xrate;
744                 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
745                 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
746
747                 read_time = stat_disk[i].rserv;
748                 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
749                 write_time = stat_disk[i].wserv;
750                 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
751                 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
752         }
753 #endif /* defined(HAVE_PERFSTAT) */
754
755         return (0);
756 } /* int disk_read */
757
758 void module_register (void)
759 {
760   plugin_register_config ("disk", disk_config,
761       config_keys, config_keys_num);
762   plugin_register_init ("disk", disk_init);
763   plugin_register_read ("disk", disk_read);
764 } /* void module_register */