Code

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