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 #if DISK_HAVE_READ
61 #if HAVE_IOKIT_IOKITLIB_H
62 static mach_port_t io_master_port = MACH_PORT_NULL;
63 /* #endif HAVE_IOKIT_IOKITLIB_H */
65 #elif KERNEL_LINUX
66 typedef struct diskstats
67 {
68 char *name;
70 /* This overflows in roughly 1361 year */
71 unsigned int poll_count;
73 counter_t read_sectors;
74 counter_t write_sectors;
76 counter_t read_bytes;
77 counter_t write_bytes;
79 struct diskstats *next;
80 } diskstats_t;
82 static diskstats_t *disklist;
83 /* #endif KERNEL_LINUX */
85 #elif HAVE_LIBKSTAT
86 #define MAX_NUMDISK 256
87 extern kstat_ctl_t *kc;
88 static kstat_t *ksp[MAX_NUMDISK];
89 static int numdisk = 0;
90 #endif /* HAVE_LIBKSTAT */
92 static int disk_init (void)
93 {
94 #if HAVE_IOKIT_IOKITLIB_H
95 kern_return_t status;
97 if (io_master_port != MACH_PORT_NULL)
98 {
99 mach_port_deallocate (mach_task_self (),
100 io_master_port);
101 io_master_port = MACH_PORT_NULL;
102 }
104 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
105 if (status != kIOReturnSuccess)
106 {
107 ERROR ("IOMasterPort failed: %s",
108 mach_error_string (status));
109 io_master_port = MACH_PORT_NULL;
110 return (-1);
111 }
112 /* #endif HAVE_IOKIT_IOKITLIB_H */
114 #elif KERNEL_LINUX
115 /* do nothing */
116 /* #endif KERNEL_LINUX */
118 #elif HAVE_LIBKSTAT
119 kstat_t *ksp_chain;
121 numdisk = 0;
123 if (kc == NULL)
124 return (-1);
126 for (numdisk = 0, ksp_chain = kc->kc_chain;
127 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
128 ksp_chain = ksp_chain->ks_next)
129 {
130 if (strncmp (ksp_chain->ks_class, "disk", 4)
131 && strncmp (ksp_chain->ks_class, "partition", 9))
132 continue;
133 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
134 continue;
135 ksp[numdisk++] = ksp_chain;
136 }
137 #endif /* HAVE_LIBKSTAT */
139 return (0);
140 } /* int disk_init */
142 static void disk_submit (const char *plugin_instance,
143 const char *type,
144 counter_t read, counter_t write)
145 {
146 value_t values[2];
147 value_list_t vl = VALUE_LIST_INIT;
149 values[0].counter = read;
150 values[1].counter = write;
152 vl.values = values;
153 vl.values_len = 2;
154 vl.time = time (NULL);
155 strcpy (vl.host, hostname_g);
156 strcpy (vl.plugin, "disk");
157 strncpy (vl.plugin_instance, plugin_instance,
158 sizeof (vl.plugin_instance));
160 plugin_dispatch_values (type, &vl);
161 } /* void disk_submit */
163 #if HAVE_IOKIT_IOKITLIB_H
164 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
165 {
166 signed long long val_int;
167 CFNumberRef val_obj;
168 CFStringRef key_obj;
170 /* `key_obj' needs to be released. */
171 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
172 kCFStringEncodingASCII);
173 if (key_obj == NULL)
174 {
175 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
176 return (-1LL);
177 }
179 /* get => we don't need to release (== free) the object */
180 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
182 CFRelease (key_obj);
184 if (val_obj == NULL)
185 {
186 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
187 return (-1LL);
188 }
190 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
191 {
192 DEBUG ("CFNumberGetValue (%s) failed.", key);
193 return (-1LL);
194 }
196 return (val_int);
197 }
198 #endif /* HAVE_IOKIT_IOKITLIB_H */
200 static int disk_read (void)
201 {
202 #if HAVE_IOKIT_IOKITLIB_H
203 io_registry_entry_t disk;
204 io_registry_entry_t disk_child;
205 io_iterator_t disk_list;
206 CFDictionaryRef props_dict;
207 CFDictionaryRef stats_dict;
208 CFDictionaryRef child_dict;
209 kern_return_t status;
211 signed long long read_ops;
212 signed long long read_byt;
213 signed long long read_tme;
214 signed long long write_ops;
215 signed long long write_byt;
216 signed long long write_tme;
218 int disk_major;
219 int disk_minor;
220 char disk_name[64];
222 static complain_t complain_obj;
224 /* Get the list of all disk objects. */
225 if (IOServiceGetMatchingServices (io_master_port,
226 IOServiceMatching (kIOBlockStorageDriverClass),
227 &disk_list) != kIOReturnSuccess)
228 {
229 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
230 "IOServiceGetMatchingServices failed.");
231 return (-1);
232 }
233 else if (complain_obj.interval != 0)
234 {
235 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
236 "IOServiceGetMatchingServices succeeded.");
237 }
239 while ((disk = IOIteratorNext (disk_list)) != 0)
240 {
241 props_dict = NULL;
242 stats_dict = NULL;
243 child_dict = NULL;
245 /* `disk_child' must be released */
246 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
247 != kIOReturnSuccess)
248 {
249 /* This fails for example for DVD/CD drives.. */
250 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
251 IOObjectRelease (disk);
252 continue;
253 }
255 /* We create `props_dict' => we need to release it later */
256 if (IORegistryEntryCreateCFProperties (disk,
257 (CFMutableDictionaryRef *) &props_dict,
258 kCFAllocatorDefault,
259 kNilOptions)
260 != kIOReturnSuccess)
261 {
262 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
263 IOObjectRelease (disk_child);
264 IOObjectRelease (disk);
265 continue;
266 }
268 if (props_dict == NULL)
269 {
270 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
271 IOObjectRelease (disk_child);
272 IOObjectRelease (disk);
273 continue;
274 }
276 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
277 CFSTR (kIOBlockStorageDriverStatisticsKey));
279 if (stats_dict == NULL)
280 {
281 DEBUG ("CFDictionaryGetValue (%s) failed.",
282 kIOBlockStorageDriverStatisticsKey);
283 CFRelease (props_dict);
284 IOObjectRelease (disk_child);
285 IOObjectRelease (disk);
286 continue;
287 }
289 if (IORegistryEntryCreateCFProperties (disk_child,
290 (CFMutableDictionaryRef *) &child_dict,
291 kCFAllocatorDefault,
292 kNilOptions)
293 != kIOReturnSuccess)
294 {
295 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
296 IOObjectRelease (disk_child);
297 CFRelease (props_dict);
298 IOObjectRelease (disk);
299 continue;
300 }
302 /* kIOBSDNameKey */
303 disk_major = (int) dict_get_value (child_dict,
304 kIOBSDMajorKey);
305 disk_minor = (int) dict_get_value (child_dict,
306 kIOBSDMinorKey);
307 read_ops = dict_get_value (stats_dict,
308 kIOBlockStorageDriverStatisticsReadsKey);
309 read_byt = dict_get_value (stats_dict,
310 kIOBlockStorageDriverStatisticsBytesReadKey);
311 read_tme = dict_get_value (stats_dict,
312 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
313 write_ops = dict_get_value (stats_dict,
314 kIOBlockStorageDriverStatisticsWritesKey);
315 write_byt = dict_get_value (stats_dict,
316 kIOBlockStorageDriverStatisticsBytesWrittenKey);
317 /* This property describes the number of nanoseconds spent
318 * performing writes since the block storage driver was
319 * instantiated. It is one of the statistic entries listed
320 * under the top-level kIOBlockStorageDriverStatisticsKey
321 * property table. It has an OSNumber value. */
322 write_tme = dict_get_value (stats_dict,
323 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
325 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
326 {
327 DEBUG ("snprintf (major, minor) failed.");
328 CFRelease (child_dict);
329 IOObjectRelease (disk_child);
330 CFRelease (props_dict);
331 IOObjectRelease (disk);
332 continue;
333 }
334 DEBUG ("disk_name = %s", disk_name);
336 if ((read_byt != -1LL) || (write_byt != -1LL))
337 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
338 if ((read_ops != -1LL) || (write_ops != -1LL))
339 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
340 if ((read_tme != -1LL) || (write_tme != -1LL))
341 disk_submit (disk_name, "disk_time",
342 read_tme / 1000,
343 write_tme / 1000);
345 CFRelease (child_dict);
346 IOObjectRelease (disk_child);
347 CFRelease (props_dict);
348 IOObjectRelease (disk);
349 }
350 IOObjectRelease (disk_list);
351 /* #endif HAVE_IOKIT_IOKITLIB_H */
353 #elif KERNEL_LINUX
354 FILE *fh;
355 char buffer[1024];
357 char *fields[32];
358 int numfields;
359 int fieldshift = 0;
361 int major = 0;
362 int minor = 0;
364 counter_t read_sectors = 0;
365 counter_t write_sectors = 0;
367 counter_t read_count = 0;
368 counter_t read_merged = 0;
369 counter_t read_bytes = 0;
370 counter_t read_time = 0;
371 counter_t write_count = 0;
372 counter_t write_merged = 0;
373 counter_t write_bytes = 0;
374 counter_t write_time = 0;
375 int is_disk = 0;
377 diskstats_t *ds, *pre_ds;
379 static complain_t complain_obj;
381 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
382 {
383 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
384 {
385 plugin_complain (LOG_ERR, &complain_obj,
386 "disk plugin: Failed to open /proc/"
387 "{diskstats,partitions}.");
388 return (-1);
389 }
391 /* Kernel is 2.4.* */
392 fieldshift = 1;
393 }
395 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
396 "Succeeded to open /proc/{diskstats,partitions}.");
398 while (fgets (buffer, sizeof (buffer), fh) != NULL)
399 {
400 char *disk_name;
402 numfields = strsplit (buffer, fields, 32);
404 if ((numfields != (14 + fieldshift)) && (numfields != 7))
405 continue;
407 major = atoll (fields[0]);
408 minor = atoll (fields[1]);
410 disk_name = fields[2];
412 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
413 if (strcmp (disk_name, ds->name) == 0)
414 break;
416 if (ds == NULL)
417 {
418 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
419 continue;
421 if ((ds->name = strdup (disk_name)) == NULL)
422 {
423 free (ds);
424 continue;
425 }
427 if (pre_ds == NULL)
428 disklist = ds;
429 else
430 pre_ds->next = ds;
431 }
433 is_disk = 0;
434 if (numfields == 7)
435 {
436 /* Kernel 2.6, Partition */
437 read_count = atoll (fields[3]);
438 read_sectors = atoll (fields[4]);
439 write_count = atoll (fields[5]);
440 write_sectors = atoll (fields[6]);
441 }
442 else if (numfields == (14 + fieldshift))
443 {
444 read_count = atoll (fields[3 + fieldshift]);
445 write_count = atoll (fields[7 + fieldshift]);
447 read_sectors = atoll (fields[5 + fieldshift]);
448 write_sectors = atoll (fields[9 + fieldshift]);
450 if ((fieldshift == 0) || (minor == 0))
451 {
452 is_disk = 1;
453 read_merged = atoll (fields[4 + fieldshift]);
454 read_time = atoll (fields[6 + fieldshift]);
455 write_merged = atoll (fields[8 + fieldshift]);
456 write_time = atoll (fields[10+ fieldshift]);
457 }
458 }
459 else
460 {
461 DEBUG ("numfields = %i; => unknown file format.", numfields);
462 continue;
463 }
465 /* If the counter wraps around, it's only 32 bits.. */
466 if (read_sectors < ds->read_sectors)
467 ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
468 else
469 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
471 if (write_sectors < ds->write_sectors)
472 ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
473 else
474 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
476 ds->read_sectors = read_sectors;
477 ds->write_sectors = write_sectors;
478 read_bytes = ds->read_bytes;
479 write_bytes = ds->write_bytes;
481 /* Don't write to the RRDs if we've just started.. */
482 ds->poll_count++;
483 if (ds->poll_count <= 2)
484 {
485 DEBUG ("(ds->poll_count = %i) <= (min_poll_count = 2); => Not writing.",
486 ds->poll_count);
487 continue;
488 }
490 if ((read_count == 0) && (write_count == 0))
491 {
492 DEBUG ("((read_count == 0) && (write_count == 0)); => Not writing.");
493 continue;
494 }
496 if ((read_bytes != -1LL) || (write_bytes != -1LL))
497 disk_submit (disk_name, "disk_octets", read_bytes, write_bytes);
498 if ((read_count != -1LL) || (write_count != -1LL))
499 disk_submit (disk_name, "disk_ops", read_count, write_count);
500 if (is_disk)
501 {
502 if ((read_merged != -1LL) || (write_merged != -1LL))
503 disk_submit (disk_name, "disk_merged",
504 read_merged, write_merged);
505 if ((read_time != -1LL) || (write_time != -1LL))
506 disk_submit (disk_name, "disk_time",
507 read_time * 1000,
508 write_time * 1000);
509 }
510 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
512 fclose (fh);
513 /* #endif defined(KERNEL_LINUX) */
515 #elif HAVE_LIBKSTAT
516 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
517 # define KIO_ROCTETS reads
518 # define KIO_WOCTETS writes
519 # define KIO_ROPS nreads
520 # define KIO_WOPS nwrites
521 # define KIO_RTIME rtime
522 # define KIO_WTIME wtime
523 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
524 # define KIO_ROCTETS nread
525 # define KIO_WOCTETS nwritten
526 # define KIO_ROPS reads
527 # define KIO_WOPS writes
528 # define KIO_RTIME rtime
529 # define KIO_WTIME wtime
530 # else
531 # error "kstat_io_t does not have the required members"
532 # endif
533 static kstat_io_t kio;
534 int i;
536 if (kc == NULL)
537 return (-1);
539 for (i = 0; i < numdisk; i++)
540 {
541 if (kstat_read (kc, ksp[i], &kio) == -1)
542 continue;
544 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
545 {
546 disk_submit (ksp[i]->ks_name, "disk_octets",
547 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
548 disk_submit (ksp[i]->ks_name, "disk_ops",
549 kio.KIO_ROPS, kio.KIO_WOPS);
550 /* FIXME: Convert this to microseconds if necessary */
551 disk_submit (ksp[i]->ks_name, "disk_time",
552 kio.KIO_RTIME, kio.KIO_WTIME);
553 }
554 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
555 {
556 disk_submit (ksp[i]->ks_name, "disk_octets",
557 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
558 disk_submit (ksp[i]->ks_name, "disk_ops",
559 kio.KIO_ROPS, kio.KIO_WOPS);
560 }
561 }
562 #endif /* defined(HAVE_LIBKSTAT) */
564 return (0);
565 } /* int disk_read */
566 #endif /* DISK_HAVE_READ */
568 void module_register (void)
569 {
570 #if DISK_HAVE_READ
571 plugin_register_init ("disk", disk_init);
572 plugin_register_read ("disk", disk_read);
573 #endif /* DISK_HAVE_READ */
574 } /* void module_register */