771227064590e9f12cdf2304e52ed0ee0fdec39a
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 =
102 {
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
113 {
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 /* #endif KERNEL_LINUX */
131 #elif HAVE_LIBKSTAT
132 #define MAX_NUMDISK 256
133 extern kstat_ctl_t *kc;
134 static kstat_t *ksp[MAX_NUMDISK];
135 static int numdisk = 0;
136 #endif /* HAVE_LIBKSTAT */
138 static int disk_init (void)
139 {
140 #if HAVE_IOKIT_IOKITLIB_H
141 kern_return_t status;
143 if (io_master_port != MACH_PORT_NULL)
144 {
145 mach_port_deallocate (mach_task_self (),
146 io_master_port);
147 io_master_port = MACH_PORT_NULL;
148 }
150 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
151 if (status != kIOReturnSuccess)
152 {
153 ERROR ("IOMasterPort failed: %s",
154 mach_error_string (status));
155 io_master_port = MACH_PORT_NULL;
156 return (-1);
157 }
158 /* #endif HAVE_IOKIT_IOKITLIB_H */
160 #elif KERNEL_LINUX
161 /* do nothing */
162 /* #endif KERNEL_LINUX */
164 #elif HAVE_LIBKSTAT
165 kstat_t *ksp_chain;
167 numdisk = 0;
169 if (kc == NULL)
170 return (-1);
172 for (numdisk = 0, ksp_chain = kc->kc_chain;
173 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
174 ksp_chain = ksp_chain->ks_next)
175 {
176 if (strncmp (ksp_chain->ks_class, "disk", 4)
177 && strncmp (ksp_chain->ks_class, "partition", 9))
178 continue;
179 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
180 continue;
181 ksp[numdisk++] = ksp_chain;
182 }
183 #endif /* HAVE_LIBKSTAT */
185 return (0);
186 } /* int disk_init */
188 static void disk_submit (const char *plugin_instance,
189 const char *type,
190 counter_t read, counter_t write)
191 {
192 value_t values[2];
193 value_list_t vl = VALUE_LIST_INIT;
195 values[0].counter = read;
196 values[1].counter = write;
198 vl.values = values;
199 vl.values_len = 2;
200 vl.time = time (NULL);
201 strcpy (vl.host, hostname_g);
202 strcpy (vl.plugin, "disk");
203 strncpy (vl.plugin_instance, plugin_instance,
204 sizeof (vl.plugin_instance));
206 plugin_dispatch_values (type, &vl);
207 } /* void disk_submit */
209 #if HAVE_IOKIT_IOKITLIB_H
210 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
211 {
212 signed long long val_int;
213 CFNumberRef val_obj;
214 CFStringRef key_obj;
216 /* `key_obj' needs to be released. */
217 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
218 kCFStringEncodingASCII);
219 if (key_obj == NULL)
220 {
221 DEBUG ("CFStringCreateWithCString (%s) failed.", key);
222 return (-1LL);
223 }
225 /* get => we don't need to release (== free) the object */
226 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
228 CFRelease (key_obj);
230 if (val_obj == NULL)
231 {
232 DEBUG ("CFDictionaryGetValue (%s) failed.", key);
233 return (-1LL);
234 }
236 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
237 {
238 DEBUG ("CFNumberGetValue (%s) failed.", key);
239 return (-1LL);
240 }
242 return (val_int);
243 }
244 #endif /* HAVE_IOKIT_IOKITLIB_H */
246 static int disk_read (void)
247 {
248 #if HAVE_IOKIT_IOKITLIB_H
249 io_registry_entry_t disk;
250 io_registry_entry_t disk_child;
251 io_iterator_t disk_list;
252 CFDictionaryRef props_dict;
253 CFDictionaryRef stats_dict;
254 CFDictionaryRef child_dict;
255 kern_return_t status;
257 signed long long read_ops;
258 signed long long read_byt;
259 signed long long read_tme;
260 signed long long write_ops;
261 signed long long write_byt;
262 signed long long write_tme;
264 int disk_major;
265 int disk_minor;
266 char disk_name[64];
268 static complain_t complain_obj;
270 /* Get the list of all disk objects. */
271 if (IOServiceGetMatchingServices (io_master_port,
272 IOServiceMatching (kIOBlockStorageDriverClass),
273 &disk_list) != kIOReturnSuccess)
274 {
275 plugin_complain (LOG_ERR, &complain_obj, "disk plugin: "
276 "IOServiceGetMatchingServices failed.");
277 return (-1);
278 }
279 else if (complain_obj.interval != 0)
280 {
281 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
282 "IOServiceGetMatchingServices succeeded.");
283 }
285 while ((disk = IOIteratorNext (disk_list)) != 0)
286 {
287 props_dict = NULL;
288 stats_dict = NULL;
289 child_dict = NULL;
291 /* `disk_child' must be released */
292 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
293 != kIOReturnSuccess)
294 {
295 /* This fails for example for DVD/CD drives.. */
296 DEBUG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
297 IOObjectRelease (disk);
298 continue;
299 }
301 /* We create `props_dict' => we need to release it later */
302 if (IORegistryEntryCreateCFProperties (disk,
303 (CFMutableDictionaryRef *) &props_dict,
304 kCFAllocatorDefault,
305 kNilOptions)
306 != kIOReturnSuccess)
307 {
308 ERROR ("disk-plugin: IORegistryEntryCreateCFProperties failed.");
309 IOObjectRelease (disk_child);
310 IOObjectRelease (disk);
311 continue;
312 }
314 if (props_dict == NULL)
315 {
316 DEBUG ("IORegistryEntryCreateCFProperties (disk) failed.");
317 IOObjectRelease (disk_child);
318 IOObjectRelease (disk);
319 continue;
320 }
322 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
323 CFSTR (kIOBlockStorageDriverStatisticsKey));
325 if (stats_dict == NULL)
326 {
327 DEBUG ("CFDictionaryGetValue (%s) failed.",
328 kIOBlockStorageDriverStatisticsKey);
329 CFRelease (props_dict);
330 IOObjectRelease (disk_child);
331 IOObjectRelease (disk);
332 continue;
333 }
335 if (IORegistryEntryCreateCFProperties (disk_child,
336 (CFMutableDictionaryRef *) &child_dict,
337 kCFAllocatorDefault,
338 kNilOptions)
339 != kIOReturnSuccess)
340 {
341 DEBUG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
342 IOObjectRelease (disk_child);
343 CFRelease (props_dict);
344 IOObjectRelease (disk);
345 continue;
346 }
348 /* kIOBSDNameKey */
349 disk_major = (int) dict_get_value (child_dict,
350 kIOBSDMajorKey);
351 disk_minor = (int) dict_get_value (child_dict,
352 kIOBSDMinorKey);
353 read_ops = dict_get_value (stats_dict,
354 kIOBlockStorageDriverStatisticsReadsKey);
355 read_byt = dict_get_value (stats_dict,
356 kIOBlockStorageDriverStatisticsBytesReadKey);
357 read_tme = dict_get_value (stats_dict,
358 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
359 write_ops = dict_get_value (stats_dict,
360 kIOBlockStorageDriverStatisticsWritesKey);
361 write_byt = dict_get_value (stats_dict,
362 kIOBlockStorageDriverStatisticsBytesWrittenKey);
363 /* This property describes the number of nanoseconds spent
364 * performing writes since the block storage driver was
365 * instantiated. It is one of the statistic entries listed
366 * under the top-level kIOBlockStorageDriverStatisticsKey
367 * property table. It has an OSNumber value. */
368 write_tme = dict_get_value (stats_dict,
369 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
371 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
372 {
373 DEBUG ("snprintf (major, minor) failed.");
374 CFRelease (child_dict);
375 IOObjectRelease (disk_child);
376 CFRelease (props_dict);
377 IOObjectRelease (disk);
378 continue;
379 }
380 DEBUG ("disk_name = %s", disk_name);
382 if ((read_byt != -1LL) || (write_byt != -1LL))
383 disk_submit (disk_name, "disk_octets", read_byt, write_byt);
384 if ((read_ops != -1LL) || (write_ops != -1LL))
385 disk_submit (disk_name, "disk_ops", read_ops, write_ops);
386 if ((read_tme != -1LL) || (write_tme != -1LL))
387 disk_submit (disk_name, "disk_time",
388 read_tme / 1000,
389 write_tme / 1000);
391 CFRelease (child_dict);
392 IOObjectRelease (disk_child);
393 CFRelease (props_dict);
394 IOObjectRelease (disk);
395 }
396 IOObjectRelease (disk_list);
397 /* #endif HAVE_IOKIT_IOKITLIB_H */
399 #elif KERNEL_LINUX
400 FILE *fh;
401 char buffer[1024];
403 char *fields[32];
404 int numfields;
405 int fieldshift = 0;
407 int major = 0;
408 int minor = 0;
410 counter_t read_sectors = 0;
411 counter_t write_sectors = 0;
413 counter_t read_count = 0;
414 counter_t read_merged = 0;
415 counter_t read_bytes = 0;
416 counter_t read_time = 0;
417 counter_t write_count = 0;
418 counter_t write_merged = 0;
419 counter_t write_bytes = 0;
420 counter_t write_time = 0;
421 int is_disk = 0;
423 diskstats_t *ds, *pre_ds;
425 static complain_t complain_obj;
427 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
428 {
429 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
430 {
431 plugin_complain (LOG_ERR, &complain_obj,
432 "disk plugin: Failed to open /proc/"
433 "{diskstats,partitions}.");
434 return (-1);
435 }
437 /* Kernel is 2.4.* */
438 fieldshift = 1;
439 }
441 plugin_relief (LOG_NOTICE, &complain_obj, "disk plugin: "
442 "Succeeded to open /proc/{diskstats,partitions}.");
444 while (fgets (buffer, sizeof (buffer), fh) != NULL)
445 {
446 char *disk_name;
448 numfields = strsplit (buffer, fields, 32);
450 if ((numfields != (14 + fieldshift)) && (numfields != 7))
451 continue;
453 major = atoll (fields[0]);
454 minor = atoll (fields[1]);
456 disk_name = fields[2];
458 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
459 if (strcmp (disk_name, ds->name) == 0)
460 break;
462 if (ds == NULL)
463 {
464 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
465 continue;
467 if ((ds->name = strdup (disk_name)) == NULL)
468 {
469 free (ds);
470 continue;
471 }
473 if (pre_ds == NULL)
474 disklist = ds;
475 else
476 pre_ds->next = ds;
477 }
479 is_disk = 0;
480 if (numfields == 7)
481 {
482 /* Kernel 2.6, Partition */
483 read_count = atoll (fields[3]);
484 read_sectors = atoll (fields[4]);
485 write_count = atoll (fields[5]);
486 write_sectors = atoll (fields[6]);
487 }
488 else if (numfields == (14 + fieldshift))
489 {
490 read_count = atoll (fields[3 + fieldshift]);
491 write_count = atoll (fields[7 + fieldshift]);
493 read_sectors = atoll (fields[5 + fieldshift]);
494 write_sectors = atoll (fields[9 + fieldshift]);
496 if ((fieldshift == 0) || (minor == 0))
497 {
498 is_disk = 1;
499 read_merged = atoll (fields[4 + fieldshift]);
500 read_time = atoll (fields[6 + fieldshift]);
501 write_merged = atoll (fields[8 + fieldshift]);
502 write_time = atoll (fields[10+ fieldshift]);
503 }
504 }
505 else
506 {
507 DEBUG ("numfields = %i; => unknown file format.", numfields);
508 continue;
509 }
511 /* If the counter wraps around, it's only 32 bits.. */
512 if (read_sectors < ds->read_sectors)
513 ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
514 else
515 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
517 if (write_sectors < ds->write_sectors)
518 ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
519 else
520 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
522 ds->read_sectors = read_sectors;
523 ds->write_sectors = write_sectors;
524 read_bytes = ds->read_bytes;
525 write_bytes = ds->write_bytes;
527 /* Don't write to the RRDs if we've just started.. */
528 ds->poll_count++;
529 if (ds->poll_count <= 2)
530 {
531 DEBUG ("(ds->poll_count = %i) <= (min_poll_count = 2); => Not writing.",
532 ds->poll_count);
533 continue;
534 }
536 if ((read_count == 0) && (write_count == 0))
537 {
538 DEBUG ("((read_count == 0) && (write_count == 0)); => Not writing.");
539 continue;
540 }
542 if ((read_bytes != -1LL) || (write_bytes != -1LL))
543 disk_submit (disk_name, "disk_octets", read_bytes, write_bytes);
544 if ((read_count != -1LL) || (write_count != -1LL))
545 disk_submit (disk_name, "disk_ops", read_count, write_count);
546 if (is_disk)
547 {
548 if ((read_merged != -1LL) || (write_merged != -1LL))
549 disk_submit (disk_name, "disk_merged",
550 read_merged, write_merged);
551 if ((read_time != -1LL) || (write_time != -1LL))
552 disk_submit (disk_name, "disk_time",
553 read_time * 1000,
554 write_time * 1000);
555 }
556 } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
558 fclose (fh);
559 /* #endif defined(KERNEL_LINUX) */
561 #elif HAVE_LIBKSTAT
562 # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
563 # define KIO_ROCTETS reads
564 # define KIO_WOCTETS writes
565 # define KIO_ROPS nreads
566 # define KIO_WOPS nwrites
567 # define KIO_RTIME rtime
568 # define KIO_WTIME wtime
569 # elif HAVE_KSTAT_IO_T_NWRITTEN && HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_WTIME
570 # define KIO_ROCTETS nread
571 # define KIO_WOCTETS nwritten
572 # define KIO_ROPS reads
573 # define KIO_WOPS writes
574 # define KIO_RTIME rtime
575 # define KIO_WTIME wtime
576 # else
577 # error "kstat_io_t does not have the required members"
578 # endif
579 static kstat_io_t kio;
580 int i;
582 if (kc == NULL)
583 return (-1);
585 for (i = 0; i < numdisk; i++)
586 {
587 if (kstat_read (kc, ksp[i], &kio) == -1)
588 continue;
590 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
591 {
592 disk_submit (ksp[i]->ks_name, "disk_octets",
593 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
594 disk_submit (ksp[i]->ks_name, "disk_ops",
595 kio.KIO_ROPS, kio.KIO_WOPS);
596 /* FIXME: Convert this to microseconds if necessary */
597 disk_submit (ksp[i]->ks_name, "disk_time",
598 kio.KIO_RTIME, kio.KIO_WTIME);
599 }
600 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
601 {
602 disk_submit (ksp[i]->ks_name, "disk_octets",
603 kio.KIO_ROCTETS, kio.KIO_WOCTETS);
604 disk_submit (ksp[i]->ks_name, "disk_ops",
605 kio.KIO_ROPS, kio.KIO_WOPS);
606 }
607 }
608 #endif /* defined(HAVE_LIBKSTAT) */
610 return (0);
611 } /* int disk_read */
612 #endif /* DISK_HAVE_READ */
614 void module_register (modreg_e load)
615 {
616 if (load & MR_DATASETS)
617 {
618 plugin_register_data_set (&octets_ds);
619 plugin_register_data_set (&operations_ds);
620 plugin_register_data_set (&merged_ds);
621 plugin_register_data_set (&time_ds);
622 }
624 #if DISK_HAVE_READ
625 if (load & MR_READ)
626 {
627 plugin_register_init ("disk", disk_init);
628 plugin_register_read ("disk", disk_read);
629 }
630 #endif /* DISK_HAVE_READ */
631 } /* void module_register */