Code

New plugin "mbmon" to collect motherboard status information:
[collectd.git] / src / disk.c
1 /**
2  * collectd - src/disk.c
3  * Copyright (C) 2005,2006  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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "utils_debug.h"
28 #define MODULE_NAME "disk"
30 #if HAVE_MACH_MACH_TYPES_H
31 #  include <mach/mach_types.h>
32 #endif
33 #if HAVE_MACH_MACH_INIT_H
34 #  include <mach/mach_init.h>
35 #endif
36 #if HAVE_MACH_MACH_ERROR_H
37 #  include <mach/mach_error.h>
38 #endif
39 #if HAVE_MACH_MACH_PORT_H
40 #  include <mach/mach_port.h>
41 #endif
42 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
43 #  include <CoreFoundation/CoreFoundation.h>
44 #endif
45 #if HAVE_IOKIT_IOKITLIB_H
46 #  include <IOKit/IOKitLib.h>
47 #endif
48 #if HAVE_IOKIT_IOTYPES_H
49 #  include <IOKit/IOTypes.h>
50 #endif
51 #if HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDRIVER_H
52 #  include <IOKit/storage/IOBlockStorageDriver.h>
53 #endif
54 #if HAVE_IOKIT_IOBSD_H
55 #  include <IOKit/IOBSD.h>
56 #endif
58 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
59 # define DISK_HAVE_READ 1
60 #else
61 # define DISK_HAVE_READ 0
62 #endif
64 static char *disk_filename_template = "disk-%s.rrd";
65 static char *part_filename_template = "partition-%s.rrd";
67 /* 104857600 == 100 MB */
68 static char *disk_ds_def[] =
69 {
70         "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
71         "DS:rmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
72         "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
73         "DS:rtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
74         "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
75         "DS:wmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
76         "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
77         "DS:wtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
78         NULL
79 };
80 static int disk_ds_num = 8;
82 static char *part_ds_def[] =
83 {
84         "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
85         "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
86         "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
87         "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
88         NULL
89 };
90 static int part_ds_num = 4;
92 #if HAVE_IOKIT_IOKITLIB_H
93 static mach_port_t io_master_port = MACH_PORT_NULL;
94 /* #endif HAVE_IOKIT_IOKITLIB_H */
96 #elif KERNEL_LINUX
97 typedef struct diskstats
98 {
99         char *name;
101         /* This overflows in roughly 1361 year */
102         unsigned int poll_count;
104         unsigned long long read_sectors;
105         unsigned long long write_sectors;
107         unsigned long long read_bytes;
108         unsigned long long write_bytes;
110         struct diskstats *next;
111 } diskstats_t;
113 static diskstats_t *disklist;
114 static int min_poll_count;
115 /* #endif KERNEL_LINUX */
117 #elif HAVE_LIBKSTAT
118 #define MAX_NUMDISK 256
119 extern kstat_ctl_t *kc;
120 static kstat_t *ksp[MAX_NUMDISK];
121 static int numdisk = 0;
122 #endif /* HAVE_LIBKSTAT */
124 static void disk_init (void)
126 #if HAVE_IOKIT_IOKITLIB_H
127         kern_return_t status;
128         
129         if (io_master_port != MACH_PORT_NULL)
130         {
131                 mach_port_deallocate (mach_task_self (),
132                                 io_master_port);
133                 io_master_port = MACH_PORT_NULL;
134         }
136         status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
137         if (status != kIOReturnSuccess)
138         {
139                 syslog (LOG_ERR, "IOMasterPort failed: %s",
140                                 mach_error_string (status));
141                 io_master_port = MACH_PORT_NULL;
142                 return;
143         }
144 /* #endif HAVE_IOKIT_IOKITLIB_H */
146 #elif KERNEL_LINUX
147         int step;
148         int heartbeat;
150         step = atoi (COLLECTD_STEP);
151         heartbeat = atoi (COLLECTD_HEARTBEAT);
153         assert (step > 0);
154         assert (heartbeat >= step);
156         min_poll_count = 1 + (heartbeat / step);
157         DBG ("min_poll_count = %i;", min_poll_count);
158 /* #endif KERNEL_LINUX */
160 #elif HAVE_LIBKSTAT
161         kstat_t *ksp_chain;
163         numdisk = 0;
165         if (kc == NULL)
166                 return;
168         for (numdisk = 0, ksp_chain = kc->kc_chain;
169                         (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
170                         ksp_chain = ksp_chain->ks_next)
171         {
172                 if (strncmp (ksp_chain->ks_class, "disk", 4)
173                                 && strncmp (ksp_chain->ks_class, "partition", 9))
174                         continue;
175                 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
176                         continue;
177                 ksp[numdisk++] = ksp_chain;
178         }
179 #endif /* HAVE_LIBKSTAT */
181         return;
184 static void disk_write (char *host, char *inst, char *val)
186         char file[512];
187         int status;
189         status = snprintf (file, 512, disk_filename_template, inst);
190         if (status < 1)
191                 return;
192         else if (status >= 512)
193                 return;
195         rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
198 static void partition_write (char *host, char *inst, char *val)
200         char file[512];
201         int status;
203         status = snprintf (file, 512, part_filename_template, inst);
204         if (status < 1)
205                 return;
206         else if (status >= 512)
207                 return;
209         rrd_update_file (host, file, val, part_ds_def, part_ds_num);
212 #if DISK_HAVE_READ
213 #define BUFSIZE 512
214 static void disk_submit (char *disk_name,
215                 unsigned long long read_count,
216                 unsigned long long read_merged,
217                 unsigned long long read_bytes,
218                 unsigned long long read_time,
219                 unsigned long long write_count,
220                 unsigned long long write_merged,
221                 unsigned long long write_bytes,
222                 unsigned long long write_time)
224         char buf[BUFSIZE];
226         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
227                                 (unsigned int) curtime,
228                                 read_count, read_merged, read_bytes, read_time,
229                                 write_count, write_merged, write_bytes,
230                                 write_time) >= BUFSIZE)
231                 return;
233         DBG ("disk_name = %s; buf = %s;",
234                         disk_name, buf);
236         plugin_submit (MODULE_NAME, disk_name, buf);
239 #if KERNEL_LINUX || HAVE_LIBKSTAT
240 static void partition_submit (char *part_name,
241                 unsigned long long read_count,
242                 unsigned long long read_bytes,
243                 unsigned long long write_count,
244                 unsigned long long write_bytes)
246         char buf[BUFSIZE];
248         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
249                                 (unsigned int) curtime,
250                                 read_count, read_bytes, write_count,
251                                 write_bytes) >= BUFSIZE)
252                 return;
254         plugin_submit ("partition", part_name, buf);
256 #endif /* KERNEL_LINUX || HAVE_LIBKSTAT */
257 #undef BUFSIZE
259 #if HAVE_IOKIT_IOKITLIB_H
260 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
262         signed long long val_int;
263         CFNumberRef      val_obj;
264         CFStringRef      key_obj;
266         /* `key_obj' needs to be released. */
267         key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
268                         kCFStringEncodingASCII);
269         if (key_obj == NULL)
270         {
271                 DBG ("CFStringCreateWithCString (%s) failed.", key);
272                 return (-1LL);
273         }
274         
275         /* get => we don't need to release (== free) the object */
276         val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
278         CFRelease (key_obj);
280         if (val_obj == NULL)
281         {
282                 DBG ("CFDictionaryGetValue (%s) failed.", key);
283                 return (-1LL);
284         }
286         if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
287         {
288                 DBG ("CFNumberGetValue (%s) failed.", key);
289                 return (-1LL);
290         }
292         return (val_int);
294 #endif /* HAVE_IOKIT_IOKITLIB_H */
296 static void disk_read (void)
298 #if HAVE_IOKIT_IOKITLIB_H
299         io_registry_entry_t     disk;
300         io_registry_entry_t     disk_child;
301         io_iterator_t           disk_list;
302         CFDictionaryRef         props_dict;
303         CFDictionaryRef         stats_dict;
304         CFDictionaryRef         child_dict;
305         kern_return_t           status;
307         signed long long read_ops;
308         signed long long read_byt;
309         signed long long read_tme;
310         signed long long write_ops;
311         signed long long write_byt;
312         signed long long write_tme;
314         int  disk_major;
315         int  disk_minor;
316         char disk_name[64];
318         static complain_t complain_obj;
320         /* Get the list of all disk objects. */
321         if (IOServiceGetMatchingServices (io_master_port,
322                                 IOServiceMatching (kIOBlockStorageDriverClass),
323                                 &disk_list) != kIOReturnSuccess)
324         {
325                 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
326                                 "IOServiceGetMatchingServices failed.");
327                 return;
328         }
329         else if (complain_obj.interval != 0)
330         {
331                 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
332                                 "IOServiceGetMatchingServices succeeded.");
333         }
335         while ((disk = IOIteratorNext (disk_list)) != 0)
336         {
337                 props_dict = NULL;
338                 stats_dict = NULL;
339                 child_dict = NULL;
341                 /* `disk_child' must be released */
342                 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
343                                 != kIOReturnSuccess)
344                 {
345                         /* This fails for example for DVD/CD drives.. */
346                         DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
347                         IOObjectRelease (disk);
348                         continue;
349                 }
351                 /* We create `props_dict' => we need to release it later */
352                 if (IORegistryEntryCreateCFProperties (disk,
353                                         (CFMutableDictionaryRef *) &props_dict,
354                                         kCFAllocatorDefault,
355                                         kNilOptions)
356                                 != kIOReturnSuccess)
357                 {
358                         syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
359                         IOObjectRelease (disk_child);
360                         IOObjectRelease (disk);
361                         continue;
362                 }
364                 if (props_dict == NULL)
365                 {
366                         DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
367                         IOObjectRelease (disk_child);
368                         IOObjectRelease (disk);
369                         continue;
370                 }
372                 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
373                                 CFSTR (kIOBlockStorageDriverStatisticsKey));
375                 if (stats_dict == NULL)
376                 {
377                         DBG ("CFDictionaryGetValue (%s) failed.",
378                                         kIOBlockStorageDriverStatisticsKey);
379                         CFRelease (props_dict);
380                         IOObjectRelease (disk_child);
381                         IOObjectRelease (disk);
382                         continue;
383                 }
385                 if (IORegistryEntryCreateCFProperties (disk_child,
386                                         (CFMutableDictionaryRef *) &child_dict,
387                                         kCFAllocatorDefault,
388                                         kNilOptions)
389                                 != kIOReturnSuccess)
390                 {
391                         DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
392                         IOObjectRelease (disk_child);
393                         CFRelease (props_dict);
394                         IOObjectRelease (disk);
395                         continue;
396                 }
398                 disk_major = (int) dict_get_value (child_dict,
399                                 kIOBSDMajorKey);
400                 disk_minor = (int) dict_get_value (child_dict,
401                                 kIOBSDMinorKey);
402                 read_ops  = dict_get_value (stats_dict,
403                                 kIOBlockStorageDriverStatisticsReadsKey);
404                 read_byt  = dict_get_value (stats_dict,
405                                 kIOBlockStorageDriverStatisticsBytesReadKey);
406                 read_tme  = dict_get_value (stats_dict,
407                                 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
408                 write_ops = dict_get_value (stats_dict,
409                                 kIOBlockStorageDriverStatisticsWritesKey);
410                 write_byt = dict_get_value (stats_dict,
411                                 kIOBlockStorageDriverStatisticsBytesWrittenKey);
412                 write_tme = dict_get_value (stats_dict,
413                                 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
415                 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
416                 {
417                         DBG ("snprintf (major, minor) failed.");
418                         CFRelease (child_dict);
419                         IOObjectRelease (disk_child);
420                         CFRelease (props_dict);
421                         IOObjectRelease (disk);
422                         continue;
423                 }
424                 DBG ("disk_name = %s", disk_name);
426                 if ((read_ops != -1LL)
427                                 || (read_byt != -1LL)
428                                 || (read_tme != -1LL)
429                                 || (write_ops != -1LL)
430                                 || (write_byt != -1LL)
431                                 || (write_tme != -1LL))
432                         disk_submit (disk_name,
433                                         read_ops, 0ULL, read_byt, read_tme,
434                                         write_ops, 0ULL, write_byt, write_tme);
436                 CFRelease (child_dict);
437                 IOObjectRelease (disk_child);
438                 CFRelease (props_dict);
439                 IOObjectRelease (disk);
440         }
441         IOObjectRelease (disk_list);
442 /* #endif HAVE_IOKIT_IOKITLIB_H */
444 #elif KERNEL_LINUX
445         FILE *fh;
446         char buffer[1024];
447         char disk_name[128];
448         
449         char *fields[32];
450         int numfields;
451         int fieldshift = 0;
453         int major = 0;
454         int minor = 0;
456         unsigned long long read_sectors  = 0ULL;
457         unsigned long long write_sectors = 0ULL;
459         unsigned long long read_count    = 0ULL;
460         unsigned long long read_merged   = 0ULL;
461         unsigned long long read_bytes    = 0ULL;
462         unsigned long long read_time     = 0ULL;
463         unsigned long long write_count   = 0ULL;
464         unsigned long long write_merged  = 0ULL;
465         unsigned long long write_bytes   = 0ULL;
466         unsigned long long write_time    = 0ULL;
467         int is_disk = 0;
469         diskstats_t *ds, *pre_ds;
471         static complain_t complain_obj;
473         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
474         {
475                 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
476                 {
477                         plugin_complain (LOG_ERR, &complain_obj, "disk plugin: Failed to open /proc/{diskstats,partitions}.");
478                         return;
479                 }
481                 /* Kernel is 2.4.* */
482                 fieldshift = 1;
483         }
485         plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: Succeeded to open /proc/{diskstats,partitions}.");
487         while (fgets (buffer, 1024, fh) != NULL)
488         {
489                 numfields = strsplit (buffer, fields, 32);
491                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
492                         continue;
494                 major = atoll (fields[0]);
495                 minor = atoll (fields[1]);
497                 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
498                         continue;
499                 disk_name[127] = '\0';
501                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
502                         if (strcmp (disk_name, ds->name) == 0)
503                                 break;
505                 if (ds == NULL)
506                 {
507                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
508                                 continue;
510                         if ((ds->name = strdup (disk_name)) == NULL)
511                         {
512                                 free (ds);
513                                 continue;
514                         }
516                         if (pre_ds == NULL)
517                                 disklist = ds;
518                         else
519                                 pre_ds->next = ds;
520                 }
522                 is_disk = 0;
523                 if (numfields == 7)
524                 {
525                         /* Kernel 2.6, Partition */
526                         read_count    = atoll (fields[3]);
527                         read_sectors  = atoll (fields[4]);
528                         write_count   = atoll (fields[5]);
529                         write_sectors = atoll (fields[6]);
530                 }
531                 else if (numfields == (14 + fieldshift))
532                 {
533                         read_count  =  atoll (fields[3 + fieldshift]);
534                         write_count =  atoll (fields[7 + fieldshift]);
536                         read_sectors  = atoll (fields[5 + fieldshift]);
537                         write_sectors = atoll (fields[9 + fieldshift]);
539                         if ((fieldshift == 0) || (minor == 0))
540                         {
541                                 is_disk = 1;
542                                 read_merged  = atoll (fields[4 + fieldshift]);
543                                 read_time    = atoll (fields[6 + fieldshift]);
544                                 write_merged = atoll (fields[8 + fieldshift]);
545                                 write_time   = atoll (fields[10+ fieldshift]);
546                         }
547                 }
548                 else
549                 {
550                         DBG ("numfields = %i; => unknown file format.", numfields);
551                         continue;
552                 }
554                 /* If the counter wraps around, it's only 32 bits.. */
555                 if (read_sectors < ds->read_sectors)
556                         ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
557                 else
558                         ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
560                 if (write_sectors < ds->write_sectors)
561                         ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
562                 else
563                         ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
565                 ds->read_sectors  = read_sectors;
566                 ds->write_sectors = write_sectors;
567                 read_bytes  = ds->read_bytes;
568                 write_bytes = ds->write_bytes;
570                 /* Don't write to the RRDs if we've just started.. */
571                 ds->poll_count++;
572                 if (ds->poll_count <= min_poll_count)
573                 {
574                         DBG ("(ds->poll_count = %i) <= (min_poll_count = %i); => Not writing.",
575                                         ds->poll_count, min_poll_count);
576                         continue;
577                 }
579                 if ((read_count == 0) && (write_count == 0))
580                 {
581                         DBG ("((read_count == 0) && (write_count == 0)); => Not writing.");
582                         continue;
583                 }
585                 if (is_disk)
586                         disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
587                                         write_count, write_merged, write_bytes, write_time);
588                 else
589                         partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
590         }
592         fclose (fh);
593 /* #endif defined(KERNEL_LINUX) */
595 #elif HAVE_LIBKSTAT
596         static kstat_io_t kio;
597         int i;
599         if (kc == NULL)
600                 return;
602         for (i = 0; i < numdisk; i++)
603         {
604                 if (kstat_read (kc, ksp[i], &kio) == -1)
605                         continue;
607                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
608                         disk_submit (ksp[i]->ks_name,
609                                         kio.reads,  0LL, kio.nread,    kio.rtime,
610                                         kio.writes, 0LL, kio.nwritten, kio.wtime);
611                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
612                         partition_submit (ksp[i]->ks_name,
613                                         kio.reads, kio.nread,
614                                         kio.writes,kio.nwritten);
615         }
616 #endif /* defined(HAVE_LIBKSTAT) */
617 } /* static void disk_read (void) */
618 #else
619 # define disk_read NULL
620 #endif /* DISK_HAVE_READ */
622 void module_register (void)
624         plugin_register ("partition", NULL, NULL, partition_write);
625         plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
628 #undef MODULE_NAME