Merge branch 'collectd-3.11'
[collectd.git] / src / disk.c
1 /**
2  * collectd - src/disk.c
3  * Copyright (C) 2005-2007  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if HAVE_MACH_MACH_TYPES_H
27 #  include <mach/mach_types.h>
28 #endif
29 #if HAVE_MACH_MACH_INIT_H
30 #  include <mach/mach_init.h>
31 #endif
32 #if HAVE_MACH_MACH_ERROR_H
33 #  include <mach/mach_error.h>
34 #endif
35 #if HAVE_MACH_MACH_PORT_H
36 #  include <mach/mach_port.h>
37 #endif
38 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
39 #  include <CoreFoundation/CoreFoundation.h>
40 #endif
41 #if HAVE_IOKIT_IOKITLIB_H
42 #  include <IOKit/IOKitLib.h>
43 #endif
44 #if HAVE_IOKIT_IOTYPES_H
45 #  include <IOKit/IOTypes.h>
46 #endif
47 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
48 #  include <IOKit/storage/IOBlockStorageDriver.h>
49 #endif
50 #if HAVE_IOKIT_IOBSD_H
51 #  include <IOKit/IOBSD.h>
52 #endif
53
54 #if HAVE_LIMITS_H
55 # include <limits.h>
56 #endif
57 #ifndef UINT_MAX
58 #  define UINT_MAX 4294967295U
59 #endif
60
61 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
62 # define DISK_HAVE_READ 1
63 #else
64 # define DISK_HAVE_READ 0
65 #endif
66
67 #if DISK_HAVE_READ
68 #if HAVE_IOKIT_IOKITLIB_H
69 static mach_port_t io_master_port = MACH_PORT_NULL;
70 /* #endif HAVE_IOKIT_IOKITLIB_H */
71
72 #elif KERNEL_LINUX
73 typedef struct diskstats
74 {
75         char *name;
76
77         /* This overflows in roughly 1361 years */
78         unsigned int poll_count;
79
80         counter_t read_sectors;
81         counter_t write_sectors;
82
83         counter_t read_bytes;
84         counter_t write_bytes;
85
86         counter_t read_ops;
87         counter_t write_ops;
88         counter_t read_time;
89         counter_t write_time;
90
91         counter_t avg_read_time;
92         counter_t avg_write_time;
93
94         struct diskstats *next;
95 } diskstats_t;
96
97 static diskstats_t *disklist;
98 /* #endif KERNEL_LINUX */
99
100 #elif HAVE_LIBKSTAT
101 #define MAX_NUMDISK 256
102 extern kstat_ctl_t *kc;
103 static kstat_t *ksp[MAX_NUMDISK];
104 static int numdisk = 0;
105 #endif /* HAVE_LIBKSTAT */
106
107 static int disk_init (void)
108 {
109 #if HAVE_IOKIT_IOKITLIB_H
110         kern_return_t status;
111         
112         if (io_master_port != MACH_PORT_NULL)
113         {
114                 mach_port_deallocate (mach_task_self (),
115                                 io_master_port);
116                 io_master_port = MACH_PORT_NULL;
117         }
118
119         status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
120         if (status != kIOReturnSuccess)
121         {
122                 ERROR ("IOMasterPort failed: %s",
123                                 mach_error_string (status));
124                 io_master_port = MACH_PORT_NULL;
125                 return (-1);
126         }
127 /* #endif HAVE_IOKIT_IOKITLIB_H */
128
129 #elif KERNEL_LINUX
130         /* do nothing */
131 /* #endif KERNEL_LINUX */
132
133 #elif HAVE_LIBKSTAT
134         kstat_t *ksp_chain;
135
136         numdisk = 0;
137
138         if (kc == NULL)
139                 return (-1);
140
141         for (numdisk = 0, ksp_chain = kc->kc_chain;
142                         (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
143                         ksp_chain = ksp_chain->ks_next)
144         {
145                 if (strncmp (ksp_chain->ks_class, "disk", 4)
146                                 && strncmp (ksp_chain->ks_class, "partition", 9))
147                         continue;
148                 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
149                         continue;
150                 ksp[numdisk++] = ksp_chain;
151         }
152 #endif /* HAVE_LIBKSTAT */
153
154         return (0);
155 } /* int disk_init */
156
157 static void disk_submit (const char *plugin_instance,
158                 const char *type,
159                 counter_t read, counter_t write)
160 {
161         value_t values[2];
162         value_list_t vl = VALUE_LIST_INIT;
163
164         values[0].counter = read;
165         values[1].counter = write;
166
167         vl.values = values;
168         vl.values_len = 2;
169         vl.time = time (NULL);
170         strcpy (vl.host, hostname_g);
171         strcpy (vl.plugin, "disk");
172         strncpy (vl.plugin_instance, plugin_instance,
173                         sizeof (vl.plugin_instance));
174
175         plugin_dispatch_values (type, &vl);
176 } /* void disk_submit */
177
178 #if HAVE_IOKIT_IOKITLIB_H
179 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
180 {
181         signed long long val_int;
182         CFNumberRef      val_obj;
183         CFStringRef      key_obj;
184
185         /* `key_obj' needs to be released. */
186         key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
187                         kCFStringEncodingASCII);
188         if (key_obj == NULL)
189         {
190                 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
191                 return (-1LL);
192         }
193         
194         /* get => we don't need to release (== free) the object */
195         val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
196
197         CFRelease (key_obj);
198
199         if (val_obj == NULL)
200         {
201                 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
202                 return (-1LL);
203         }
204
205         if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
206         {
207                 DEBUG ("CFNumberGetValue (%s) failed.", key);
208                 return (-1LL);
209         }
210
211         return (val_int);
212 }
213 #endif /* HAVE_IOKIT_IOKITLIB_H */
214
215 static int disk_read (void)
216 {
217 #if HAVE_IOKIT_IOKITLIB_H
218         io_registry_entry_t     disk;
219         io_registry_entry_t     disk_child;
220         io_iterator_t           disk_list;
221         CFDictionaryRef         props_dict;
222         CFDictionaryRef         stats_dict;
223         CFDictionaryRef         child_dict;
224         kern_return_t           status;
225
226         signed long long read_ops;
227         signed long long read_byt;
228         signed long long read_tme;
229         signed long long write_ops;
230         signed long long write_byt;
231         signed long long write_tme;
232
233         int  disk_major;
234         int  disk_minor;
235         char disk_name[64];
236
237         static complain_t complain_obj;
238
239         /* Get the list of all disk objects. */
240         if (IOServiceGetMatchingServices (io_master_port,
241                                 IOServiceMatching (kIOBlockStorageDriverClass),
242                                 &disk_list) != kIOReturnSuccess)
243         {
244                 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
245                                 "IOServiceGetMatchingServices failed.");
246                 return (-1);
247         }
248         else if (complain_obj.interval != 0)
249         {
250                 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
251                                 "IOServiceGetMatchingServices succeeded.");
252         }
253
254         while ((disk = IOIteratorNext (disk_list)) != 0)
255         {
256                 props_dict = NULL;
257                 stats_dict = NULL;
258                 child_dict = NULL;
259
260                 /* `disk_child' must be released */
261                 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
262                                 != kIOReturnSuccess)
263                 {
264                         /* This fails for example for DVD/CD drives.. */
265                         DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
266                         IOObjectRelease (disk);
267                         continue;
268                 }
269
270                 /* We create `props_dict' => we need to release it later */
271                 if (IORegistryEntryCreateCFProperties (disk,
272                                         (CFMutableDictionaryRef *) &props_dict,
273                                         kCFAllocatorDefault,
274                                         kNilOptions)
275                                 != kIOReturnSuccess)
276                 {
277                         ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
278                         IOObjectRelease (disk_child);
279                         IOObjectRelease (disk);
280                         continue;
281                 }
282
283                 if (props_dict == NULL)
284                 {
285                         DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
286                         IOObjectRelease (disk_child);
287                         IOObjectRelease (disk);
288                         continue;
289                 }
290
291                 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
292                                 CFSTR (kIOBlockStorageDriverStatisticsKey));
293
294                 if (stats_dict == NULL)
295                 {
296                         DEBUG ("CFDictionaryGetValue (%s) failed.",
297                                         kIOBlockStorageDriverStatisticsKey);
298                         CFRelease (props_dict);
299                         IOObjectRelease (disk_child);
300                         IOObjectRelease (disk);
301                         continue;
302                 }
303
304                 if (IORegistryEntryCreateCFProperties (disk_child,
305                                         (CFMutableDictionaryRef *) &child_dict,
306                                         kCFAllocatorDefault,
307                                         kNilOptions)
308                                 != kIOReturnSuccess)
309                 {
310                         DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
311                         IOObjectRelease (disk_child);
312                         CFRelease (props_dict);
313                         IOObjectRelease (disk);
314                         continue;
315                 }
316
317                 /* kIOBSDNameKey */
318                 disk_major = (int) dict_get_value (child_dict,
319                                 kIOBSDMajorKey);
320                 disk_minor = (int) dict_get_value (child_dict,
321                                 kIOBSDMinorKey);
322                 read_ops  = dict_get_value (stats_dict,
323                                 kIOBlockStorageDriverStatisticsReadsKey);
324                 read_byt  = dict_get_value (stats_dict,
325                                 kIOBlockStorageDriverStatisticsBytesReadKey);
326                 read_tme  = dict_get_value (stats_dict,
327                                 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
328                 write_ops = dict_get_value (stats_dict,
329                                 kIOBlockStorageDriverStatisticsWritesKey);
330                 write_byt = dict_get_value (stats_dict,
331                                 kIOBlockStorageDriverStatisticsBytesWrittenKey);
332                 /* This property describes the number of nanoseconds spent
333                  * performing writes since the block storage driver was
334                  * instantiated. It is one of the statistic entries listed
335                  * under the top-level kIOBlockStorageDriverStatisticsKey
336                  * property table. It has an OSNumber value. */
337                 write_tme = dict_get_value (stats_dict,
338                                 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
339
340                 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
341                 {
342                         DEBUG ("snprintf (major, minor) failed.");
343                         CFRelease (child_dict);
344                         IOObjectRelease (disk_child);
345                         CFRelease (props_dict);
346                         IOObjectRelease (disk);
347                         continue;
348                 }
349                 DEBUG ("disk_name = %s", disk_name);
350
351                 if ((read_byt != -1LL) || (write_byt != -1LL))
352                         disk_submit (disk_name, "disk_octets", read_byt, write_byt);
353                 if ((read_ops != -1LL) || (write_ops != -1LL))
354                         disk_submit (disk_name, "disk_ops", read_ops, write_ops);
355                 if ((read_tme != -1LL) || (write_tme != -1LL))
356                         disk_submit (disk_name, "disk_time",
357                                         read_tme / 1000,
358                                         write_tme / 1000);
359
360                 CFRelease (child_dict);
361                 IOObjectRelease (disk_child);
362                 CFRelease (props_dict);
363                 IOObjectRelease (disk);
364         }
365         IOObjectRelease (disk_list);
366 /* #endif HAVE_IOKIT_IOKITLIB_H */
367
368 #elif KERNEL_LINUX
369         FILE *fh;
370         char buffer[1024];
371         
372         char *fields[32];
373         int numfields;
374         int fieldshift = 0;
375
376         int major = 0;
377         int minor = 0;
378
379         counter_t read_sectors  = 0;
380         counter_t write_sectors = 0;
381
382         counter_t read_ops      = 0;
383         counter_t read_merged   = 0;
384         counter_t read_time     = 0;
385         counter_t write_ops     = 0;
386         counter_t write_merged  = 0;
387         counter_t write_time    = 0;
388         int is_disk = 0;
389
390         diskstats_t *ds, *pre_ds;
391
392         static complain_t complain_obj;
393
394         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
395         {
396                 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
397                 {
398                         plugin_complain (LOG_ERR, &complain_obj,
399                                         "disk plugin: Failed to open /proc/"
400                                         "{diskstats,partitions}.");
401                         return (-1);
402                 }
403
404                 /* Kernel is 2.4.* */
405                 fieldshift = 1;
406         }
407
408         plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
409                         "Succeeded to open /proc/{diskstats,partitions}.");
410
411         while (fgets (buffer, sizeof (buffer), fh) != NULL)
412         {
413                 char *disk_name;
414
415                 numfields = strsplit (buffer, fields, 32);
416
417                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
418                         continue;
419
420                 major = atoll (fields[0]);
421                 minor = atoll (fields[1]);
422
423                 disk_name = fields[2];
424
425                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
426                         if (strcmp (disk_name, ds->name) == 0)
427                                 break;
428
429                 if (ds == NULL)
430                 {
431                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
432                                 continue;
433
434                         if ((ds->name = strdup (disk_name)) == NULL)
435                         {
436                                 free (ds);
437                                 continue;
438                         }
439
440                         if (pre_ds == NULL)
441                                 disklist = ds;
442                         else
443                                 pre_ds->next = ds;
444                 }
445
446                 is_disk = 0;
447                 if (numfields == 7)
448                 {
449                         /* Kernel 2.6, Partition */
450                         read_ops      = atoll (fields[3]);
451                         read_sectors  = atoll (fields[4]);
452                         write_ops     = atoll (fields[5]);
453                         write_sectors = atoll (fields[6]);
454                 }
455                 else if (numfields == (14 + fieldshift))
456                 {
457                         read_ops  =  atoll (fields[3 + fieldshift]);
458                         write_ops =  atoll (fields[7 + fieldshift]);
459
460                         read_sectors  = atoll (fields[5 + fieldshift]);
461                         write_sectors = atoll (fields[9 + fieldshift]);
462
463                         if ((fieldshift == 0) || (minor == 0))
464                         {
465                                 is_disk = 1;
466                                 read_merged  = atoll (fields[4 + fieldshift]);
467                                 read_time    = atoll (fields[6 + fieldshift]);
468                                 write_merged = atoll (fields[8 + fieldshift]);
469                                 write_time   = atoll (fields[10+ fieldshift]);
470                         }
471                 }
472                 else
473                 {
474                         DEBUG ("numfields = %i; => unknown file format.", numfields);
475                         continue;
476                 }
477
478                 {
479                         counter_t diff_read_sectors;
480                         counter_t diff_write_sectors;
481
482                 /* If the counter wraps around, it's only 32 bits.. */
483                         if (read_sectors < ds->read_sectors)
484                                 diff_read_sectors = 1 + read_sectors
485                                         + (UINT_MAX - ds->read_sectors);
486                         else
487                                 diff_read_sectors = read_sectors - ds->read_sectors;
488                         if (write_sectors < ds->write_sectors)
489                                 diff_write_sectors = 1 + write_sectors
490                                         + (UINT_MAX - ds->write_sectors);
491                         else
492                                 diff_write_sectors = write_sectors - ds->write_sectors;
493
494                         ds->read_bytes += 512 * diff_read_sectors;
495                         ds->write_bytes += 512 * diff_write_sectors;
496                         ds->read_sectors = read_sectors;
497                         ds->write_sectors = write_sectors;
498                 }
499
500                 /* Calculate the average time an io-op needs to complete */
501                 if (is_disk)
502                 {
503                         counter_t diff_read_ops;
504                         counter_t diff_write_ops;
505                         counter_t diff_read_time;
506                         counter_t diff_write_time;
507
508                         if (read_ops < ds->read_ops)
509                                 diff_read_ops = 1 + read_ops
510                                         + (UINT_MAX - ds->read_ops);
511                         else
512                                 diff_read_ops = read_ops - ds->read_ops;
513                         DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
514                                         "ds->read_ops = %llu; diff_read_ops = %llu;",
515                                         disk_name,
516                                         read_ops, ds->read_ops, diff_read_ops);
517
518                         if (write_ops < ds->write_ops)
519                                 diff_write_ops = 1 + write_ops
520                                         + (UINT_MAX - ds->write_ops);
521                         else
522                                 diff_write_ops = write_ops - ds->write_ops;
523
524                         if (read_time < ds->read_time)
525                                 diff_read_time = 1 + read_time
526                                         + (UINT_MAX - ds->read_time);
527                         else
528                                 diff_read_time = read_time - ds->read_time;
529
530                         if (write_time < ds->write_time)
531                                 diff_write_time = 1 + write_time
532                                         + (UINT_MAX - ds->write_time);
533                         else
534                                 diff_write_time = write_time - ds->write_time;
535
536                         if (diff_read_ops != 0)
537                                 ds->avg_read_time += (diff_read_time
538                                                 + (diff_read_ops / 2))
539                                         / diff_read_ops;
540                         if (diff_write_ops != 0)
541                                 ds->avg_write_time += (diff_write_time
542                                                 + (diff_write_ops / 2))
543                                         / diff_write_ops;
544
545                         ds->read_ops = read_ops;
546                         ds->read_time = read_time;
547                         ds->write_ops = write_ops;
548                         ds->write_time = write_time;
549                 } /* if (is_disk) */
550
551                 /* Don't write to the RRDs if we've just started.. */
552                 ds->poll_count++;
553                 if (ds->poll_count <= 2)
554                 {
555                         DEBUG ("disk plugin: (ds->poll_count = %i) <= "
556                                         "(min_poll_count = 2); => Not writing.",
557                                         ds->poll_count);
558                         continue;
559                 }
560
561                 if ((read_ops == 0) && (write_ops == 0))
562                 {
563                         DEBUG ("disk plugin: ((read_ops == 0) && "
564                                         "(write_ops == 0)); => Not writing.");
565                         continue;
566                 }
567
568                 if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
569                         disk_submit (disk_name, "disk_octets",
570                                         ds->read_bytes, ds->write_bytes);
571
572                 if ((ds->read_ops != 0) || (ds->write_ops != 0))
573                         disk_submit (disk_name, "disk_ops",
574                                         read_ops, write_ops);
575
576                 if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
577                         disk_submit (disk_name, "disk_time",
578                                         ds->avg_read_time, ds->avg_write_time);
579
580                 if (is_disk)
581                 {
582                         if ((read_merged != -1LL) || (write_merged != -1LL))
583                                 disk_submit (disk_name, "disk_merged",
584                                                 read_merged, write_merged);
585                 } /* if (is_disk) */
586         } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
587
588         fclose (fh);
589 /* #endif defined(KERNEL_LINUX) */
590
591 #elif HAVE_LIBKSTAT
592 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
593 #  define KIO_ROCTETS reads
594 #  define KIO_WOCTETS writes
595 #  define KIO_ROPS    nreads
596 #  define KIO_WOPS    nwrites
597 #  define KIO_RTIME   rtime
598 #  define KIO_WTIME   wtime
599 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
600 #  define KIO_ROCTETS nread
601 #  define KIO_WOCTETS nwritten
602 #  define KIO_ROPS    reads
603 #  define KIO_WOPS    writes
604 #  define KIO_RTIME   rtime
605 #  define KIO_WTIME   wtime
606 # else
607 #  error "kstat_io_t does not have the required members"
608 # endif
609         static kstat_io_t kio;
610         int i;
611
612         if (kc == NULL)
613                 return (-1);
614
615         for (i = 0; i < numdisk; i++)
616         {
617                 if (kstat_read (kc, ksp[i], &kio) == -1)
618                         continue;
619
620                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
621                 {
622                         disk_submit (ksp[i]->ks_name, "disk_octets",
623                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
624                         disk_submit (ksp[i]->ks_name, "disk_ops",
625                                         kio.KIO_ROPS, kio.KIO_WOPS);
626                         /* FIXME: Convert this to microseconds if necessary */
627                         disk_submit (ksp[i]->ks_name, "disk_time",
628                                         kio.KIO_RTIME, kio.KIO_WTIME);
629                 }
630                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
631                 {
632                         disk_submit (ksp[i]->ks_name, "disk_octets",
633                                         kio.KIO_ROCTETS, kio.KIO_WOCTETS);
634                         disk_submit (ksp[i]->ks_name, "disk_ops",
635                                         kio.KIO_ROPS, kio.KIO_WOPS);
636                 }
637         }
638 #endif /* defined(HAVE_LIBKSTAT) */
639
640         return (0);
641 } /* int disk_read */
642 #endif /* DISK_HAVE_READ */
643
644 void module_register (void)
645 {
646 #if DISK_HAVE_READ
647         plugin_register_init ("disk", disk_init);
648         plugin_register_read ("disk", disk_read);
649 #endif /* DISK_HAVE_READ */
650 } /* void module_register */