beee7fa690f29956cf7ab97720863427d4d0bf60
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 int read_sectors;
105 unsigned int 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 /* #endif KERNEL_LINUX */
116 #elif HAVE_LIBKSTAT
117 #define MAX_NUMDISK 256
118 extern kstat_ctl_t *kc;
119 static kstat_t *ksp[MAX_NUMDISK];
120 static int numdisk = 0;
121 #endif /* HAVE_LIBKSTAT */
123 static void disk_init (void)
124 {
125 #if HAVE_IOKIT_IOKITLIB_H
126 kern_return_t status;
128 if (io_master_port != MACH_PORT_NULL)
129 {
130 mach_port_deallocate (mach_task_self (),
131 io_master_port);
132 io_master_port = MACH_PORT_NULL;
133 }
135 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
136 if (status != kIOReturnSuccess)
137 {
138 syslog (LOG_ERR, "IOMasterPort failed: %s",
139 mach_error_string (status));
140 io_master_port = MACH_PORT_NULL;
141 return;
142 }
143 /* #endif HAVE_IOKIT_IOKITLIB_H */
145 #elif KERNEL_LINUX
146 /* No init needed */
147 /* #endif KERNEL_LINUX */
149 #elif HAVE_LIBKSTAT
150 kstat_t *ksp_chain;
152 numdisk = 0;
154 if (kc == NULL)
155 return;
157 for (numdisk = 0, ksp_chain = kc->kc_chain;
158 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
159 ksp_chain = ksp_chain->ks_next)
160 {
161 if (strncmp (ksp_chain->ks_class, "disk", 4)
162 && strncmp (ksp_chain->ks_class, "partition", 9))
163 continue;
164 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
165 continue;
166 ksp[numdisk++] = ksp_chain;
167 }
168 #endif /* HAVE_LIBKSTAT */
170 return;
171 }
173 static void disk_write (char *host, char *inst, char *val)
174 {
175 char file[512];
176 int status;
178 status = snprintf (file, 512, disk_filename_template, inst);
179 if (status < 1)
180 return;
181 else if (status >= 512)
182 return;
184 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
185 }
187 static void partition_write (char *host, char *inst, char *val)
188 {
189 char file[512];
190 int status;
192 status = snprintf (file, 512, part_filename_template, inst);
193 if (status < 1)
194 return;
195 else if (status >= 512)
196 return;
198 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
199 }
201 #if DISK_HAVE_READ
202 #define BUFSIZE 512
203 static void disk_submit (char *disk_name,
204 unsigned long long read_count,
205 unsigned long long read_merged,
206 unsigned long long read_bytes,
207 unsigned long long read_time,
208 unsigned long long write_count,
209 unsigned long long write_merged,
210 unsigned long long write_bytes,
211 unsigned long long write_time)
212 {
213 char buf[BUFSIZE];
215 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
216 (unsigned int) curtime,
217 read_count, read_merged, read_bytes, read_time,
218 write_count, write_merged, write_bytes,
219 write_time) >= BUFSIZE)
220 return;
222 plugin_submit (MODULE_NAME, disk_name, buf);
223 }
225 #if KERNEL_LINUX || HAVE_LIBKSTAT
226 static void partition_submit (char *part_name,
227 unsigned long long read_count,
228 unsigned long long read_bytes,
229 unsigned long long write_count,
230 unsigned long long write_bytes)
231 {
232 char buf[BUFSIZE];
234 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
235 (unsigned int) curtime,
236 read_count, read_bytes, write_count,
237 write_bytes) >= BUFSIZE)
238 return;
240 plugin_submit ("partition", part_name, buf);
241 }
242 #endif /* KERNEL_LINUX || HAVE_LIBKSTAT */
243 #undef BUFSIZE
245 #if HAVE_IOKIT_IOKITLIB_H
246 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
247 {
248 signed long long val_int;
249 CFNumberRef val_obj;
250 CFStringRef key_obj;
252 CFStringRef CFStringCreateWithCString (
253 CFAllocatorRef alloc,
254 const char *cStr,
255 CFStringEncoding encoding
256 );
258 /* `key_obj' needs to be released. */
259 key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
260 kCFStringEncodingASCII);
261 if (key_obj == NULL)
262 {
263 DBG ("CFStringCreateWithCString (%s) failed.", key);
264 return (-1LL);
265 }
267 /* get => we don't need to release (== free) the object */
268 val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
270 CFRelease (key_obj);
272 if (val_obj == NULL)
273 {
274 DBG ("CFDictionaryGetValue (%s) failed.", key);
275 return (-1LL);
276 }
278 if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
279 {
280 DBG ("CFNumberGetValue (%s) failed.", key);
281 return (-1LL);
282 }
284 return (val_int);
285 }
286 #endif /* HAVE_IOKIT_IOKITLIB_H */
288 static void disk_read (void)
289 {
290 #if HAVE_IOKIT_IOKITLIB_H
291 io_registry_entry_t disk;
292 io_registry_entry_t disk_child;
293 io_iterator_t disk_list;
294 CFDictionaryRef props_dict;
295 CFDictionaryRef stats_dict;
296 CFDictionaryRef child_dict;
297 kern_return_t status;
299 signed long long read_ops;
300 signed long long read_byt;
301 signed long long read_tme;
302 signed long long write_ops;
303 signed long long write_byt;
304 signed long long write_tme;
306 int disk_major;
307 int disk_minor;
308 char disk_name[64];
310 /* Get the list of all disk objects. */
311 if (IOServiceGetMatchingServices (io_master_port,
312 IOServiceMatching (kIOBlockStorageDriverClass),
313 &disk_list) != kIOReturnSuccess)
314 {
315 syslog (LOG_ERR, "disk-plugin: IOServiceGetMatchingServices failed.");
316 return;
317 }
319 while ((disk = IOIteratorNext (disk_list)) != 0)
320 {
321 props_dict = NULL;
322 stats_dict = NULL;
323 child_dict = NULL;
325 /* `disk_child' must be released */
326 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
327 != kIOReturnSuccess)
328 {
329 /* This fails for example for DVD/CD drives.. */
330 DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
331 IOObjectRelease (disk);
332 continue;
333 }
335 /* We create `props_dict' => we need to release it later */
336 if (IORegistryEntryCreateCFProperties (disk,
337 (CFMutableDictionaryRef *) &props_dict,
338 kCFAllocatorDefault,
339 kNilOptions)
340 != kIOReturnSuccess)
341 {
342 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
343 IOObjectRelease (disk_child);
344 IOObjectRelease (disk);
345 continue;
346 }
348 if (props_dict == NULL)
349 {
350 DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
351 IOObjectRelease (disk_child);
352 IOObjectRelease (disk);
353 continue;
354 }
356 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
357 CFSTR (kIOBlockStorageDriverStatisticsKey));
359 if (stats_dict == NULL)
360 {
361 DBG ("CFDictionaryGetValue (%s) failed.",
362 kIOBlockStorageDriverStatisticsKey);
363 CFRelease (props_dict);
364 IOObjectRelease (disk_child);
365 IOObjectRelease (disk);
366 continue;
367 }
369 if (IORegistryEntryCreateCFProperties (disk_child,
370 (CFMutableDictionaryRef *) &child_dict,
371 kCFAllocatorDefault,
372 kNilOptions)
373 != kIOReturnSuccess)
374 {
375 DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
376 IOObjectRelease (disk_child);
377 CFRelease (props_dict);
378 IOObjectRelease (disk);
379 continue;
380 }
382 disk_major = (int) dict_get_value (child_dict,
383 kIOBSDMajorKey);
384 disk_minor = (int) dict_get_value (child_dict,
385 kIOBSDMinorKey);
386 read_ops = dict_get_value (stats_dict,
387 kIOBlockStorageDriverStatisticsReadsKey);
388 read_byt = dict_get_value (stats_dict,
389 kIOBlockStorageDriverStatisticsBytesReadKey);
390 read_tme = dict_get_value (stats_dict,
391 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
392 write_ops = dict_get_value (stats_dict,
393 kIOBlockStorageDriverStatisticsWritesKey);
394 write_byt = dict_get_value (stats_dict,
395 kIOBlockStorageDriverStatisticsBytesWrittenKey);
396 write_tme = dict_get_value (stats_dict,
397 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
399 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
400 {
401 DBG ("snprintf (major, minor) failed.");
402 CFRelease (child_dict);
403 IOObjectRelease (disk_child);
404 CFRelease (props_dict);
405 IOObjectRelease (disk);
406 continue;
407 }
408 DBG ("disk_name = %s", disk_name);
410 if ((read_ops != -1LL)
411 || (read_byt != -1LL)
412 || (read_tme != -1LL)
413 || (write_ops != -1LL)
414 || (write_byt != -1LL)
415 || (write_tme != -1LL))
416 disk_submit (disk_name,
417 read_ops, 0ULL, read_byt, read_tme,
418 write_ops, 0ULL, write_byt, write_tme);
420 CFRelease (child_dict);
421 IOObjectRelease (disk_child);
422 CFRelease (props_dict);
423 IOObjectRelease (disk);
424 }
425 IOObjectRelease (disk_list);
426 /* #endif HAVE_IOKIT_IOKITLIB_H */
428 #elif KERNEL_LINUX
429 FILE *fh;
430 char buffer[1024];
431 char disk_name[128];
433 char *fields[32];
434 int numfields;
435 int fieldshift = 0;
437 int major = 0;
438 int minor = 0;
440 unsigned int read_sectors;
441 unsigned int write_sectors;
443 unsigned long long read_count = 0;
444 unsigned long long read_merged = 0;
445 unsigned long long read_bytes = 0;
446 unsigned long long read_time = 0;
447 unsigned long long write_count = 0;
448 unsigned long long write_merged = 0;
449 unsigned long long write_bytes = 0;
450 unsigned long long write_time = 0;
451 int is_disk = 0;
453 diskstats_t *ds, *pre_ds;
455 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
456 {
457 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
458 return;
460 /* Kernel is 2.4.* */
461 fieldshift = 1;
462 }
464 while (fgets (buffer, 1024, fh) != NULL)
465 {
466 numfields = strsplit (buffer, fields, 32);
468 if ((numfields != (14 + fieldshift)) && (numfields != 7))
469 continue;
471 major = atoll (fields[0]);
472 minor = atoll (fields[1]);
474 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
475 continue;
476 disk_name[127] = '\0';
478 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
479 if (strcmp (disk_name, ds->name) == 0)
480 break;
482 if (ds == NULL)
483 {
484 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
485 continue;
487 if ((ds->name = strdup (disk_name)) == NULL)
488 {
489 free (ds);
490 continue;
491 }
493 if (pre_ds == NULL)
494 disklist = ds;
495 else
496 pre_ds->next = ds;
497 }
499 is_disk = 0;
500 if (numfields == 7)
501 {
502 /* Kernel 2.6, Partition */
503 read_count = atoll (fields[3]);
504 read_sectors = atoi (fields[4]);
505 write_count = atoll (fields[5]);
506 write_sectors = atoi (fields[6]);
507 }
508 else if (numfields == (14 + fieldshift))
509 {
510 read_count = atoll (fields[3 + fieldshift]);
511 write_count = atoll (fields[7 + fieldshift]);
513 read_sectors = atoi (fields[5 + fieldshift]);
514 write_sectors = atoi (fields[9 + fieldshift]);
516 if ((fieldshift == 0) || (minor == 0))
517 {
518 is_disk = 1;
519 read_merged = atoll (fields[4 + fieldshift]);
520 read_time = atoll (fields[6 + fieldshift]);
521 write_merged = atoll (fields[8 + fieldshift]);
522 write_time = atoll (fields[10+ fieldshift]);
523 }
524 }
525 else
526 {
527 continue;
528 }
531 if (read_sectors >= ds->read_sectors)
532 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
533 else
534 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
536 if (write_sectors >= ds->write_sectors)
537 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
538 else
539 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
541 ds->read_sectors = read_sectors;
542 ds->write_sectors = write_sectors;
543 read_bytes = ds->read_bytes;
544 write_bytes = ds->write_bytes;
546 /* Don't write to the RRDs if we've just started.. */
547 ds->poll_count++;
548 if (ds->poll_count <= 6)
549 continue;
551 if ((read_count == 0) && (write_count == 0))
552 continue;
554 if (is_disk)
555 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
556 write_count, write_merged, write_bytes, write_time);
557 else
558 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
559 }
561 fclose (fh);
562 /* #endif defined(KERNEL_LINUX) */
564 #elif HAVE_LIBKSTAT
565 static kstat_io_t kio;
566 int i;
568 if (kc == NULL)
569 return;
571 for (i = 0; i < numdisk; i++)
572 {
573 if (kstat_read (kc, ksp[i], &kio) == -1)
574 continue;
576 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
577 disk_submit (ksp[i]->ks_name,
578 kio.reads, 0LL, kio.nread, kio.rtime,
579 kio.writes, 0LL, kio.nwritten, kio.wtime);
580 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
581 partition_submit (ksp[i]->ks_name,
582 kio.reads, kio.nread,
583 kio.writes,kio.nwritten);
584 }
585 #endif /* defined(HAVE_LIBKSTAT) */
586 } /* static void disk_read (void) */
587 #else
588 # define disk_read NULL
589 #endif /* DISK_HAVE_READ */
591 void module_register (void)
592 {
593 plugin_register ("partition", NULL, NULL, partition_write);
594 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
595 }
597 #undef MODULE_NAME