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