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)
125 {
126 #if HAVE_IOKIT_IOKITLIB_H
127 kern_return_t status;
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;
182 }
184 static void disk_write (char *host, char *inst, char *val)
185 {
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);
196 }
198 static void partition_write (char *host, char *inst, char *val)
199 {
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);
210 }
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)
223 {
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);
237 }
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)
245 {
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);
255 }
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)
261 {
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 }
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);
293 }
294 #endif /* HAVE_IOKIT_IOKITLIB_H */
296 static void disk_read (void)
297 {
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 /* Get the list of all disk objects. */
319 if (IOServiceGetMatchingServices (io_master_port,
320 IOServiceMatching (kIOBlockStorageDriverClass),
321 &disk_list) != kIOReturnSuccess)
322 {
323 syslog (LOG_ERR, "disk-plugin: IOServiceGetMatchingServices failed.");
324 return;
325 }
327 while ((disk = IOIteratorNext (disk_list)) != 0)
328 {
329 props_dict = NULL;
330 stats_dict = NULL;
331 child_dict = NULL;
333 /* `disk_child' must be released */
334 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
335 != kIOReturnSuccess)
336 {
337 /* This fails for example for DVD/CD drives.. */
338 DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
339 IOObjectRelease (disk);
340 continue;
341 }
343 /* We create `props_dict' => we need to release it later */
344 if (IORegistryEntryCreateCFProperties (disk,
345 (CFMutableDictionaryRef *) &props_dict,
346 kCFAllocatorDefault,
347 kNilOptions)
348 != kIOReturnSuccess)
349 {
350 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
351 IOObjectRelease (disk_child);
352 IOObjectRelease (disk);
353 continue;
354 }
356 if (props_dict == NULL)
357 {
358 DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
359 IOObjectRelease (disk_child);
360 IOObjectRelease (disk);
361 continue;
362 }
364 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
365 CFSTR (kIOBlockStorageDriverStatisticsKey));
367 if (stats_dict == NULL)
368 {
369 DBG ("CFDictionaryGetValue (%s) failed.",
370 kIOBlockStorageDriverStatisticsKey);
371 CFRelease (props_dict);
372 IOObjectRelease (disk_child);
373 IOObjectRelease (disk);
374 continue;
375 }
377 if (IORegistryEntryCreateCFProperties (disk_child,
378 (CFMutableDictionaryRef *) &child_dict,
379 kCFAllocatorDefault,
380 kNilOptions)
381 != kIOReturnSuccess)
382 {
383 DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
384 IOObjectRelease (disk_child);
385 CFRelease (props_dict);
386 IOObjectRelease (disk);
387 continue;
388 }
390 disk_major = (int) dict_get_value (child_dict,
391 kIOBSDMajorKey);
392 disk_minor = (int) dict_get_value (child_dict,
393 kIOBSDMinorKey);
394 read_ops = dict_get_value (stats_dict,
395 kIOBlockStorageDriverStatisticsReadsKey);
396 read_byt = dict_get_value (stats_dict,
397 kIOBlockStorageDriverStatisticsBytesReadKey);
398 read_tme = dict_get_value (stats_dict,
399 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
400 write_ops = dict_get_value (stats_dict,
401 kIOBlockStorageDriverStatisticsWritesKey);
402 write_byt = dict_get_value (stats_dict,
403 kIOBlockStorageDriverStatisticsBytesWrittenKey);
404 write_tme = dict_get_value (stats_dict,
405 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
407 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
408 {
409 DBG ("snprintf (major, minor) failed.");
410 CFRelease (child_dict);
411 IOObjectRelease (disk_child);
412 CFRelease (props_dict);
413 IOObjectRelease (disk);
414 continue;
415 }
416 DBG ("disk_name = %s", disk_name);
418 if ((read_ops != -1LL)
419 || (read_byt != -1LL)
420 || (read_tme != -1LL)
421 || (write_ops != -1LL)
422 || (write_byt != -1LL)
423 || (write_tme != -1LL))
424 disk_submit (disk_name,
425 read_ops, 0ULL, read_byt, read_tme,
426 write_ops, 0ULL, write_byt, write_tme);
428 CFRelease (child_dict);
429 IOObjectRelease (disk_child);
430 CFRelease (props_dict);
431 IOObjectRelease (disk);
432 }
433 IOObjectRelease (disk_list);
434 /* #endif HAVE_IOKIT_IOKITLIB_H */
436 #elif KERNEL_LINUX
437 FILE *fh;
438 char buffer[1024];
439 char disk_name[128];
441 char *fields[32];
442 int numfields;
443 int fieldshift = 0;
445 int major = 0;
446 int minor = 0;
448 unsigned long long read_sectors = 0ULL;
449 unsigned long long write_sectors = 0ULL;
451 unsigned long long read_count = 0ULL;
452 unsigned long long read_merged = 0ULL;
453 unsigned long long read_bytes = 0ULL;
454 unsigned long long read_time = 0ULL;
455 unsigned long long write_count = 0ULL;
456 unsigned long long write_merged = 0ULL;
457 unsigned long long write_bytes = 0ULL;
458 unsigned long long write_time = 0ULL;
459 int is_disk = 0;
461 diskstats_t *ds, *pre_ds;
463 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
464 {
465 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
466 return;
468 /* Kernel is 2.4.* */
469 fieldshift = 1;
470 }
472 while (fgets (buffer, 1024, fh) != NULL)
473 {
474 numfields = strsplit (buffer, fields, 32);
476 if ((numfields != (14 + fieldshift)) && (numfields != 7))
477 continue;
479 major = atoll (fields[0]);
480 minor = atoll (fields[1]);
482 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
483 continue;
484 disk_name[127] = '\0';
486 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
487 if (strcmp (disk_name, ds->name) == 0)
488 break;
490 if (ds == NULL)
491 {
492 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
493 continue;
495 if ((ds->name = strdup (disk_name)) == NULL)
496 {
497 free (ds);
498 continue;
499 }
501 if (pre_ds == NULL)
502 disklist = ds;
503 else
504 pre_ds->next = ds;
505 }
507 is_disk = 0;
508 if (numfields == 7)
509 {
510 /* Kernel 2.6, Partition */
511 read_count = atoll (fields[3]);
512 read_sectors = atoll (fields[4]);
513 write_count = atoll (fields[5]);
514 write_sectors = atoll (fields[6]);
515 }
516 else if (numfields == (14 + fieldshift))
517 {
518 read_count = atoll (fields[3 + fieldshift]);
519 write_count = atoll (fields[7 + fieldshift]);
521 read_sectors = atoll (fields[5 + fieldshift]);
522 write_sectors = atoll (fields[9 + fieldshift]);
524 if ((fieldshift == 0) || (minor == 0))
525 {
526 is_disk = 1;
527 read_merged = atoll (fields[4 + fieldshift]);
528 read_time = atoll (fields[6 + fieldshift]);
529 write_merged = atoll (fields[8 + fieldshift]);
530 write_time = atoll (fields[10+ fieldshift]);
531 }
532 }
533 else
534 {
535 DBG ("numfields = %i; => unknown file format.", numfields);
536 continue;
537 }
539 /* If the counter wraps around, it's only 32 bits.. */
540 if (read_sectors < ds->read_sectors)
541 ds->read_bytes += 512 * ((0xFFFFFFFF - ds->read_sectors) + read_sectors);
542 else
543 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
545 if (write_sectors < ds->write_sectors)
546 ds->write_bytes += 512 * ((0xFFFFFFFF - ds->write_sectors) + write_sectors);
547 else
548 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
550 ds->read_sectors = read_sectors;
551 ds->write_sectors = write_sectors;
552 read_bytes = ds->read_bytes;
553 write_bytes = ds->write_bytes;
555 /* Don't write to the RRDs if we've just started.. */
556 ds->poll_count++;
557 if (ds->poll_count <= min_poll_count)
558 {
559 DBG ("(ds->poll_count = %i) <= (min_poll_count = %i); => Not writing.",
560 ds->poll_count, min_poll_count);
561 continue;
562 }
564 if ((read_count == 0) && (write_count == 0))
565 {
566 DBG ("((read_count == 0) && (write_count == 0)); => Not writing.");
567 continue;
568 }
570 if (is_disk)
571 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
572 write_count, write_merged, write_bytes, write_time);
573 else
574 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
575 }
577 fclose (fh);
578 /* #endif defined(KERNEL_LINUX) */
580 #elif HAVE_LIBKSTAT
581 static kstat_io_t kio;
582 int i;
584 if (kc == NULL)
585 return;
587 for (i = 0; i < numdisk; i++)
588 {
589 if (kstat_read (kc, ksp[i], &kio) == -1)
590 continue;
592 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
593 disk_submit (ksp[i]->ks_name,
594 kio.reads, 0LL, kio.nread, kio.rtime,
595 kio.writes, 0LL, kio.nwritten, kio.wtime);
596 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
597 partition_submit (ksp[i]->ks_name,
598 kio.reads, kio.nread,
599 kio.writes,kio.nwritten);
600 }
601 #endif /* defined(HAVE_LIBKSTAT) */
602 } /* static void disk_read (void) */
603 #else
604 # define disk_read NULL
605 #endif /* DISK_HAVE_READ */
607 void module_register (void)
608 {
609 plugin_register ("partition", NULL, NULL, partition_write);
610 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
611 }
613 #undef MODULE_NAME