Code

Merge remote-tracking branch 'github/pr/387'
[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  **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "utils_ignorelist.h"
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
57 #if HAVE_LIMITS_H
58 # include <limits.h>
59 #endif
60 #ifndef UINT_MAX
61 #  define UINT_MAX 4294967295U
62 #endif
64 #if HAVE_STATGRAB_H
65 # include <statgrab.h>
66 #endif
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
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 */
83 #elif KERNEL_LINUX
84 typedef struct diskstats
85 {
86         char *name;
88         /* This overflows in roughly 1361 years */
89         unsigned int poll_count;
91         derive_t read_sectors;
92         derive_t write_sectors;
94         derive_t read_bytes;
95         derive_t write_bytes;
97         derive_t read_ops;
98         derive_t write_ops;
99         derive_t read_time;
100         derive_t write_time;
102         derive_t avg_read_time;
103         derive_t avg_write_time;
105         struct diskstats *next;
106 } diskstats_t;
108 static diskstats_t *disklist;
109 /* #endif KERNEL_LINUX */
111 #elif HAVE_LIBKSTAT
112 #define MAX_NUMDISK 1024
113 extern kstat_ctl_t *kc;
114 static kstat_t *ksp[MAX_NUMDISK];
115 static int numdisk = 0;
116 /* #endif HAVE_LIBKSTAT */
118 #elif defined(HAVE_LIBSTATGRAB)
119 /* #endif HAVE_LIBKSTATGRAB */
121 #elif HAVE_PERFSTAT
122 static perfstat_disk_t * stat_disk;
123 static int numdisk;
124 static int pnumdisk;
125 /* #endif HAVE_PERFSTAT */
127 #else
128 # error "No applicable input method."
129 #endif
131 static const char *config_keys[] =
133         "Disk",
134         "UseBSDName",
135         "IgnoreSelected"
136 };
137 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
139 static ignorelist_t *ignorelist = NULL;
141 static int disk_config (const char *key, const char *value)
143   if (ignorelist == NULL)
144     ignorelist = ignorelist_create (/* invert = */ 1);
145   if (ignorelist == NULL)
146     return (1);
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   }
173   return (0);
174 } /* int disk_config */
176 static int disk_init (void)
178 #if HAVE_IOKIT_IOKITLIB_H
179         kern_return_t status;
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         }
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 */
198 #elif KERNEL_LINUX
199         /* do nothing */
200 /* #endif KERNEL_LINUX */
202 #elif HAVE_LIBKSTAT
203         kstat_t *ksp_chain;
205         numdisk = 0;
207         if (kc == NULL)
208                 return (-1);
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 */
223         return (0);
224 } /* int disk_init */
226 static void disk_submit (const char *plugin_instance,
227                 const char *type,
228                 derive_t read, derive_t write)
230         value_t values[2];
231         value_list_t vl = VALUE_LIST_INIT;
233         /* Both `ignorelist' and `plugin_instance' may be NULL. */
234         if (ignorelist_match (ignorelist, plugin_instance) != 0)
235           return;
237         values[0].derive = read;
238         values[1].derive = write;
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));
248         plugin_dispatch_values (&vl);
249 } /* void disk_submit */
251 #if KERNEL_LINUX
252 static counter_t disk_calc_time_incr (counter_t delta_time, counter_t delta_ops)
254         double interval = CDTIME_T_TO_DOUBLE (plugin_get_interval ());
255         double avg_time = ((double) delta_time) / ((double) delta_ops);
256         double avg_time_incr = interval * avg_time;
258         return ((counter_t) (avg_time_incr + .5));
260 #endif
262 #if HAVE_IOKIT_IOKITLIB_H
263 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
265         signed long long val_int;
266         CFNumberRef      val_obj;
267         CFStringRef      key_obj;
269         /* `key_obj' needs to be released. */
270         key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
271                         kCFStringEncodingASCII);
272         if (key_obj == NULL)
273         {
274                 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
275                 return (-1LL);
276         }
277         
278         /* get => we don't need to release (== free) the object */
279         val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
281         CFRelease (key_obj);
283         if (val_obj == NULL)
284         {
285                 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
286                 return (-1LL);
287         }
289         if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
290         {
291                 DEBUG ("CFNumberGetValue (%s) failed.", key);
292                 return (-1LL);
293         }
295         return (val_int);
297 #endif /* HAVE_IOKIT_IOKITLIB_H */
299 static int disk_read (void)
301 #if HAVE_IOKIT_IOKITLIB_H
302         io_registry_entry_t     disk;
303         io_registry_entry_t     disk_child;
304         io_iterator_t           disk_list;
305         CFDictionaryRef         props_dict;
306         CFDictionaryRef         stats_dict;
307         CFDictionaryRef         child_dict;
308         CFStringRef             tmp_cf_string_ref;
309         kern_return_t           status;
311         signed long long read_ops;
312         signed long long read_byt;
313         signed long long read_tme;
314         signed long long write_ops;
315         signed long long write_byt;
316         signed long long write_tme;
318         int  disk_major;
319         int  disk_minor;
320         char disk_name[DATA_MAX_NAME_LEN];
321         char disk_name_bsd[DATA_MAX_NAME_LEN];
323         /* Get the list of all disk objects. */
324         if (IOServiceGetMatchingServices (io_master_port,
325                                 IOServiceMatching (kIOBlockStorageDriverClass),
326                                 &disk_list) != kIOReturnSuccess)
327         {
328                 ERROR ("disk plugin: IOServiceGetMatchingServices failed.");
329                 return (-1);
330         }
332         while ((disk = IOIteratorNext (disk_list)) != 0)
333         {
334                 props_dict = NULL;
335                 stats_dict = NULL;
336                 child_dict = NULL;
338                 /* `disk_child' must be released */
339                 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
340                                 != kIOReturnSuccess)
341                 {
342                         /* This fails for example for DVD/CD drives.. */
343                         DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
344                         IOObjectRelease (disk);
345                         continue;
346                 }
348                 /* We create `props_dict' => we need to release it later */
349                 if (IORegistryEntryCreateCFProperties (disk,
350                                         (CFMutableDictionaryRef *) &props_dict,
351                                         kCFAllocatorDefault,
352                                         kNilOptions)
353                                 != kIOReturnSuccess)
354                 {
355                         ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
356                         IOObjectRelease (disk_child);
357                         IOObjectRelease (disk);
358                         continue;
359                 }
361                 if (props_dict == NULL)
362                 {
363                         DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
364                         IOObjectRelease (disk_child);
365                         IOObjectRelease (disk);
366                         continue;
367                 }
369                 /* tmp_cf_string_ref doesn't need to be released. */
370                 tmp_cf_string_ref = (CFStringRef) CFDictionaryGetValue (props_dict,
371                                 CFSTR(kIOBSDNameKey));
372                 if (!tmp_cf_string_ref)
373                 {
374                         DEBUG ("disk plugin: CFDictionaryGetValue("
375                                         "kIOBSDNameKey) failed.");
376                         CFRelease (props_dict);
377                         IOObjectRelease (disk_child);
378                         IOObjectRelease (disk);
379                         continue;
380                 }
381                 assert (CFGetTypeID (tmp_cf_string_ref) == CFStringGetTypeID ());
383                 memset (disk_name_bsd, 0, sizeof (disk_name_bsd));
384                 CFStringGetCString (tmp_cf_string_ref,
385                                 disk_name_bsd, sizeof (disk_name_bsd),
386                                 kCFStringEncodingUTF8);
387                 if (disk_name_bsd[0] == 0)
388                 {
389                         ERROR ("disk plugin: CFStringGetCString() failed.");
390                         CFRelease (props_dict);
391                         IOObjectRelease (disk_child);
392                         IOObjectRelease (disk);
393                         continue;
394                 }
395                 DEBUG ("disk plugin: disk_name_bsd = \"%s\"", disk_name_bsd);
397                 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
398                                 CFSTR (kIOBlockStorageDriverStatisticsKey));
400                 if (stats_dict == NULL)
401                 {
402                         DEBUG ("disk plugin: CFDictionaryGetValue ("
403                                         "%s) failed.",
404                                         kIOBlockStorageDriverStatisticsKey);
405                         CFRelease (props_dict);
406                         IOObjectRelease (disk_child);
407                         IOObjectRelease (disk);
408                         continue;
409                 }
411                 if (IORegistryEntryCreateCFProperties (disk_child,
412                                         (CFMutableDictionaryRef *) &child_dict,
413                                         kCFAllocatorDefault,
414                                         kNilOptions)
415                                 != kIOReturnSuccess)
416                 {
417                         DEBUG ("disk plugin: IORegistryEntryCreateCFProperties ("
418                                         "disk_child) failed.");
419                         IOObjectRelease (disk_child);
420                         CFRelease (props_dict);
421                         IOObjectRelease (disk);
422                         continue;
423                 }
425                 /* kIOBSDNameKey */
426                 disk_major = (int) dict_get_value (child_dict,
427                                 kIOBSDMajorKey);
428                 disk_minor = (int) dict_get_value (child_dict,
429                                 kIOBSDMinorKey);
430                 read_ops  = dict_get_value (stats_dict,
431                                 kIOBlockStorageDriverStatisticsReadsKey);
432                 read_byt  = dict_get_value (stats_dict,
433                                 kIOBlockStorageDriverStatisticsBytesReadKey);
434                 read_tme  = dict_get_value (stats_dict,
435                                 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
436                 write_ops = dict_get_value (stats_dict,
437                                 kIOBlockStorageDriverStatisticsWritesKey);
438                 write_byt = dict_get_value (stats_dict,
439                                 kIOBlockStorageDriverStatisticsBytesWrittenKey);
440                 /* This property describes the number of nanoseconds spent
441                  * performing writes since the block storage driver was
442                  * instantiated. It is one of the statistic entries listed
443                  * under the top-level kIOBlockStorageDriverStatisticsKey
444                  * property table. It has an OSNumber value. */
445                 write_tme = dict_get_value (stats_dict,
446                                 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
448                 if (use_bsd_name)
449                         sstrncpy (disk_name, disk_name_bsd, sizeof (disk_name));
450                 else
451                         ssnprintf (disk_name, sizeof (disk_name), "%i-%i",
452                                         disk_major, disk_minor);
453                 DEBUG ("disk plugin: disk_name = \"%s\"", disk_name);
455                 if ((read_byt != -1LL) || (write_byt != -1LL))
456                         disk_submit (disk_name, "disk_octets", read_byt, write_byt);
457                 if ((read_ops != -1LL) || (write_ops != -1LL))
458                         disk_submit (disk_name, "disk_ops", read_ops, write_ops);
459                 if ((read_tme != -1LL) || (write_tme != -1LL))
460                         disk_submit (disk_name, "disk_time",
461                                         read_tme / 1000,
462                                         write_tme / 1000);
464                 CFRelease (child_dict);
465                 IOObjectRelease (disk_child);
466                 CFRelease (props_dict);
467                 IOObjectRelease (disk);
468         }
469         IOObjectRelease (disk_list);
470 /* #endif HAVE_IOKIT_IOKITLIB_H */
472 #elif KERNEL_LINUX
473         FILE *fh;
474         char buffer[1024];
475         
476         char *fields[32];
477         int numfields;
478         int fieldshift = 0;
480         int minor = 0;
482         derive_t read_sectors  = 0;
483         derive_t write_sectors = 0;
485         derive_t read_ops      = 0;
486         derive_t read_merged   = 0;
487         derive_t read_time     = 0;
488         derive_t write_ops     = 0;
489         derive_t write_merged  = 0;
490         derive_t write_time    = 0;
491         int is_disk = 0;
493         diskstats_t *ds, *pre_ds;
495         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
496         {
497                 fh = fopen ("/proc/partitions", "r");
498                 if (fh == NULL)
499                 {
500                         ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
501                         return (-1);
502                 }
504                 /* Kernel is 2.4.* */
505                 fieldshift = 1;
506         }
508         while (fgets (buffer, sizeof (buffer), fh) != NULL)
509         {
510                 char *disk_name;
512                 numfields = strsplit (buffer, fields, 32);
514                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
515                         continue;
517                 minor = atoll (fields[1]);
519                 disk_name = fields[2 + fieldshift];
521                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
522                         if (strcmp (disk_name, ds->name) == 0)
523                                 break;
525                 if (ds == NULL)
526                 {
527                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
528                                 continue;
530                         if ((ds->name = strdup (disk_name)) == NULL)
531                         {
532                                 free (ds);
533                                 continue;
534                         }
536                         if (pre_ds == NULL)
537                                 disklist = ds;
538                         else
539                                 pre_ds->next = ds;
540                 }
542                 is_disk = 0;
543                 if (numfields == 7)
544                 {
545                         /* Kernel 2.6, Partition */
546                         read_ops      = atoll (fields[3]);
547                         read_sectors  = atoll (fields[4]);
548                         write_ops     = atoll (fields[5]);
549                         write_sectors = atoll (fields[6]);
550                 }
551                 else if (numfields == (14 + fieldshift))
552                 {
553                         read_ops  =  atoll (fields[3 + fieldshift]);
554                         write_ops =  atoll (fields[7 + fieldshift]);
556                         read_sectors  = atoll (fields[5 + fieldshift]);
557                         write_sectors = atoll (fields[9 + fieldshift]);
559                         if ((fieldshift == 0) || (minor == 0))
560                         {
561                                 is_disk = 1;
562                                 read_merged  = atoll (fields[4 + fieldshift]);
563                                 read_time    = atoll (fields[6 + fieldshift]);
564                                 write_merged = atoll (fields[8 + fieldshift]);
565                                 write_time   = atoll (fields[10+ fieldshift]);
566                         }
567                 }
568                 else
569                 {
570                         DEBUG ("numfields = %i; => unknown file format.", numfields);
571                         continue;
572                 }
574                 {
575                         derive_t diff_read_sectors;
576                         derive_t diff_write_sectors;
578                 /* If the counter wraps around, it's only 32 bits.. */
579                         if (read_sectors < ds->read_sectors)
580                                 diff_read_sectors = 1 + read_sectors
581                                         + (UINT_MAX - ds->read_sectors);
582                         else
583                                 diff_read_sectors = read_sectors - ds->read_sectors;
584                         if (write_sectors < ds->write_sectors)
585                                 diff_write_sectors = 1 + write_sectors
586                                         + (UINT_MAX - ds->write_sectors);
587                         else
588                                 diff_write_sectors = write_sectors - ds->write_sectors;
590                         ds->read_bytes += 512 * diff_read_sectors;
591                         ds->write_bytes += 512 * diff_write_sectors;
592                         ds->read_sectors = read_sectors;
593                         ds->write_sectors = write_sectors;
594                 }
596                 /* Calculate the average time an io-op needs to complete */
597                 if (is_disk)
598                 {
599                         derive_t diff_read_ops;
600                         derive_t diff_write_ops;
601                         derive_t diff_read_time;
602                         derive_t diff_write_time;
604                         if (read_ops < ds->read_ops)
605                                 diff_read_ops = 1 + read_ops
606                                         + (UINT_MAX - ds->read_ops);
607                         else
608                                 diff_read_ops = read_ops - ds->read_ops;
609                         DEBUG ("disk plugin: disk_name = %s; read_ops = %"PRIi64"; "
610                                         "ds->read_ops = %"PRIi64"; diff_read_ops = %"PRIi64";",
611                                         disk_name,
612                                         read_ops, ds->read_ops, diff_read_ops);
614                         if (write_ops < ds->write_ops)
615                                 diff_write_ops = 1 + write_ops
616                                         + (UINT_MAX - ds->write_ops);
617                         else
618                                 diff_write_ops = write_ops - ds->write_ops;
620                         if (read_time < ds->read_time)
621                                 diff_read_time = 1 + read_time
622                                         + (UINT_MAX - ds->read_time);
623                         else
624                                 diff_read_time = read_time - ds->read_time;
626                         if (write_time < ds->write_time)
627                                 diff_write_time = 1 + write_time
628                                         + (UINT_MAX - ds->write_time);
629                         else
630                                 diff_write_time = write_time - ds->write_time;
632                         if (diff_read_ops != 0)
633                                 ds->avg_read_time += disk_calc_time_incr (
634                                                 diff_read_time, diff_read_ops);
635                         if (diff_write_ops != 0)
636                                 ds->avg_write_time += disk_calc_time_incr (
637                                                 diff_write_time, diff_write_ops);
639                         ds->read_ops = read_ops;
640                         ds->read_time = read_time;
641                         ds->write_ops = write_ops;
642                         ds->write_time = write_time;
643                 } /* if (is_disk) */
645                 /* Don't write to the RRDs if we've just started.. */
646                 ds->poll_count++;
647                 if (ds->poll_count <= 2)
648                 {
649                         DEBUG ("disk plugin: (ds->poll_count = %i) <= "
650                                         "(min_poll_count = 2); => Not writing.",
651                                         ds->poll_count);
652                         continue;
653                 }
655                 if ((read_ops == 0) && (write_ops == 0))
656                 {
657                         DEBUG ("disk plugin: ((read_ops == 0) && "
658                                         "(write_ops == 0)); => Not writing.");
659                         continue;
660                 }
662                 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
663                         disk_submit (disk_name, "disk_octets",
664                                         ds->read_bytes, ds->write_bytes);
666                 if ((ds->read_ops != 0) || (ds->write_ops != 0))
667                         disk_submit (disk_name, "disk_ops",
668                                         read_ops, write_ops);
670                 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
671                         disk_submit (disk_name, "disk_time",
672                                         ds->avg_read_time, ds->avg_write_time);
674                 if (is_disk)
675                 {
676                         disk_submit (disk_name, "disk_merged",
677                                         read_merged, write_merged);
678                 } /* if (is_disk) */
679         } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
681         fclose (fh);
682 /* #endif defined(KERNEL_LINUX) */
684 #elif HAVE_LIBKSTAT
685 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
686 #  define KIO_ROCTETS reads
687 #  define KIO_WOCTETS writes
688 #  define KIO_ROPS    nreads
689 #  define KIO_WOPS    nwrites
690 #  define KIO_RTIME   rtime
691 #  define KIO_WTIME   wtime
692 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
693 #  define KIO_ROCTETS nread
694 #  define KIO_WOCTETS nwritten
695 #  define KIO_ROPS    reads
696 #  define KIO_WOPS    writes
697 #  define KIO_RTIME   rtime
698 #  define KIO_WTIME   wtime
699 # else
700 #  error "kstat_io_t does not have the required members"
701 # endif
702         static kstat_io_t kio;
703         int i;
705         if (kc == NULL)
706                 return (-1);
708         for (i = 0; i < numdisk; i++)
709         {
710                 if (kstat_read (kc, ksp[i], &kio) == -1)
711                         continue;
713                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
714                 {
715                         disk_submit (ksp[i]->ks_name, "disk_octets",
716                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
717                         disk_submit (ksp[i]->ks_name, "disk_ops",
718                                         kio.KIO_ROPS, kio.KIO_WOPS);
719                         /* FIXME: Convert this to microseconds if necessary */
720                         disk_submit (ksp[i]->ks_name, "disk_time",
721                                         kio.KIO_RTIME, kio.KIO_WTIME);
722                 }
723                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
724                 {
725                         disk_submit (ksp[i]->ks_name, "disk_octets",
726                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
727                         disk_submit (ksp[i]->ks_name, "disk_ops",
728                                         kio.KIO_ROPS, kio.KIO_WOPS);
729                 }
730         }
731 /* #endif defined(HAVE_LIBKSTAT) */
733 #elif defined(HAVE_LIBSTATGRAB)
734         sg_disk_io_stats *ds;
735         int disks, counter;
736         char name[DATA_MAX_NAME_LEN];
737         
738         if ((ds = sg_get_disk_io_stats(&disks)) == NULL)
739                 return (0);
740                 
741         for (counter=0; counter < disks; counter++) {
742                 strncpy(name, ds->disk_name, sizeof(name));
743                 name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */
744                 disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
745                 ds++;
746         }
747 /* #endif defined(HAVE_LIBSTATGRAB) */
749 #elif defined(HAVE_PERFSTAT)
750         derive_t read_sectors;
751         derive_t write_sectors;
752         derive_t read_time;
753         derive_t write_time;
754         derive_t read_ops;
755         derive_t write_ops;
756         perfstat_id_t firstpath;
757         int rnumdisk;
758         int i;
760         if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0) 
761         {
762                 char errbuf[1024];
763                 WARNING ("disk plugin: perfstat_disk: %s",
764                                 sstrerror (errno, errbuf, sizeof (errbuf)));
765                 return (-1);
766         }
768         if (numdisk != pnumdisk || stat_disk==NULL) {
769                 if (stat_disk!=NULL) 
770                         free(stat_disk);
771                 stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
772         } 
773         pnumdisk = numdisk;
775         firstpath.name[0]='\0';
776         if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0) 
777         {
778                 char errbuf[1024];
779                 WARNING ("disk plugin: perfstat_disk : %s",
780                                 sstrerror (errno, errbuf, sizeof (errbuf)));
781                 return (-1);
782         }
784         for (i = 0; i < rnumdisk; i++) 
785         {
786                 read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
787                 write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
788                 disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
790                 read_ops = stat_disk[i].xrate;
791                 write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
792                 disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
794                 read_time = stat_disk[i].rserv;
795                 read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
796                 write_time = stat_disk[i].wserv;
797                 write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
798                 disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
799         }
800 #endif /* defined(HAVE_PERFSTAT) */
802         return (0);
803 } /* int disk_read */
805 void module_register (void)
807   plugin_register_config ("disk", disk_config,
808       config_keys, config_keys_num);
809   plugin_register_init ("disk", disk_init);
810   plugin_register_read ("disk", disk_read);
811 } /* void module_register */