c63b49db1210b97123072ebad15004e1ad113242
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"
27 #define MODULE_NAME "disk"
29 #if HAVE_MACH_MACH_TYPES_H
30 # include <mach/mach_types.h>
31 #endif
32 #if HAVE_MACH_MACH_INIT_H
33 # include <mach/mach_init.h>
34 #endif
35 #if HAVE_MACH_MACH_ERROR_H
36 # include <mach/mach_error.h>
37 #endif
38 #if HAVE_MACH_MACH_PORT_H
39 # include <mach/mach_port.h>
40 #endif
41 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
42 # include <CoreFoundation/CoreFoundation.h>
43 #endif
44 #if HAVE_IOKIT_IOKITLIB_H
45 # include <IOKit/IOKitLib.h>
46 #endif
47 #if HAVE_IOKIT_IOTYPES_H
48 # include <IOKit/IOTypes.h>
49 #endif
51 #if HAVE_IOKIT_IOKITLIB_H || KERNEL_LINUX || HAVE_LIBKSTAT
52 # define DISK_HAVE_READ 1
53 #else
54 # define DISK_HAVE_READ 0
55 #endif
57 static char *disk_filename_template = "disk-%s.rrd";
58 static char *part_filename_template = "partition-%s.rrd";
60 /* 104857600 == 100 MB */
61 static char *disk_ds_def[] =
62 {
63 "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
64 "DS:rmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
65 "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
66 "DS:rtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
67 "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
68 "DS:wmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
69 "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
70 "DS:wtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
71 NULL
72 };
73 static int disk_ds_num = 8;
75 static char *part_ds_def[] =
76 {
77 "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
78 "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
79 "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
80 "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
81 NULL
82 };
83 static int part_ds_num = 4;
85 #if HAVE_IOKIT_IOKITLIB_H
86 static mach_port_t io_master_port = MACH_PORT_NULL;
87 /* #endif HAVE_IOKIT_IOKITLIB_H */
89 #elif KERNEL_LINUX
90 typedef struct diskstats
91 {
92 char *name;
94 /* This overflows in roughly 1361 year */
95 unsigned int poll_count;
97 unsigned int read_sectors;
98 unsigned int write_sectors;
100 unsigned long long read_bytes;
101 unsigned long long write_bytes;
103 struct diskstats *next;
104 } diskstats_t;
106 static diskstats_t *disklist;
107 /* #endif KERNEL_LINUX */
109 #elif HAVE_LIBKSTAT
110 #define MAX_NUMDISK 256
111 extern kstat_ctl_t *kc;
112 static kstat_t *ksp[MAX_NUMDISK];
113 static int numdisk = 0;
114 #endif /* HAVE_LIBKSTAT */
116 static void disk_init (void)
117 {
118 #if HAVE_IOKIT_IOKITLIB_H
119 kern_return_t status;
121 if (io_master_port != MACH_PORT_NULL)
122 {
123 mach_port_deallocate (mach_task_self (),
124 io_master_port);
125 io_master_port = MACH_PORT_NULL;
126 }
128 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
129 if (status != kIOReturnSuccess)
130 {
131 syslog (LOG_ERR, "IOMasterPort failed: %s",
132 mach_error_string (status));
133 io_master_port = MACH_PORT_NULL;
134 return;
135 }
136 /* #endif HAVE_IOKIT_IOKITLIB_H */
138 #elif KERNEL_LINUX
139 /* No init needed */
140 /* #endif KERNEL_LINUX */
142 #elif HAVE_LIBKSTAT
143 kstat_t *ksp_chain;
145 numdisk = 0;
147 if (kc == NULL)
148 return;
150 for (numdisk = 0, ksp_chain = kc->kc_chain;
151 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
152 ksp_chain = ksp_chain->ks_next)
153 {
154 if (strncmp (ksp_chain->ks_class, "disk", 4)
155 && strncmp (ksp_chain->ks_class, "partition", 9))
156 continue;
157 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
158 continue;
159 ksp[numdisk++] = ksp_chain;
160 }
161 #endif /* HAVE_LIBKSTAT */
163 return;
164 }
166 static void disk_write (char *host, char *inst, char *val)
167 {
168 char file[512];
169 int status;
171 status = snprintf (file, 512, disk_filename_template, inst);
172 if (status < 1)
173 return;
174 else if (status >= 512)
175 return;
177 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
178 }
180 static void partition_write (char *host, char *inst, char *val)
181 {
182 char file[512];
183 int status;
185 status = snprintf (file, 512, part_filename_template, inst);
186 if (status < 1)
187 return;
188 else if (status >= 512)
189 return;
191 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
192 }
194 #if DISK_HAVE_READ
195 #define BUFSIZE 512
196 static void disk_submit (char *disk_name,
197 unsigned long long read_count,
198 unsigned long long read_merged,
199 unsigned long long read_bytes,
200 unsigned long long read_time,
201 unsigned long long write_count,
202 unsigned long long write_merged,
203 unsigned long long write_bytes,
204 unsigned long long write_time)
205 {
206 char buf[BUFSIZE];
208 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
209 (unsigned int) curtime,
210 read_count, read_merged, read_bytes, read_time,
211 write_count, write_merged, write_bytes,
212 write_time) >= BUFSIZE)
213 return;
215 plugin_submit (MODULE_NAME, disk_name, buf);
216 }
218 static void partition_submit (char *part_name,
219 unsigned long long read_count,
220 unsigned long long read_bytes,
221 unsigned long long write_count,
222 unsigned long long write_bytes)
223 {
224 char buf[BUFSIZE];
226 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
227 (unsigned int) curtime,
228 read_count, read_bytes, write_count,
229 write_bytes) >= BUFSIZE)
230 return;
232 plugin_submit ("partition", part_name, buf);
233 }
234 #undef BUFSIZE
236 #if HAVE_IOKIT_IOKITLIB_H
237 static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
238 {
239 signed long long val_int;
240 CFNumberRef val_obj;
242 /* get => we don't need to release (== free) the object */
243 val_obj = (CFNumberRef) CFDictionaryGetValue (dict,
244 CFSTR (key));
246 if (number == NULL)
247 return (-1LL);
249 if (CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int)
250 != kIOReturnSuccess)
251 return (-1LL);
253 return (val_int);
254 }
255 #endif /* HAVE_IOKIT_IOKITLIB_H */
257 static void disk_read (void)
258 {
259 #if HAVE_IOKIT_IOKITLIB_H
260 CFDictionaryRef props_dict;
261 CFDictionaryRef stats_dict;
262 io_registry_entry_t disk;
263 io_iterator_t disk_list;
265 signed long long read_ops;
266 signed long long read_byt;
267 signed long long read_tme;
268 signed long long write_ops;
269 signed long long write_byt;
270 signed long long write_tme;
272 /* Get the list of all disk objects. */
273 if (IOServiceGetMatchingServices (io_master_port,
274 IOServiceMatching (kIOBlockStorageDriverClass),
275 &disk_list) != kIOReturnSuccess)
276 {
277 syslog (LOG_ERR, "disk-plugin: IOServiceGetMatchingServices failed.");
278 return;
279 }
281 while ((disk = IOIteratorNext (disk_list)) != NULL)
282 {
283 props_dict = NULL;
284 stats_dict = NULL;
286 /* We create `props_dict' => we need to release it later */
287 if (IORegistryEntryCreateCFProperties (disk,
288 (CFMutableDictionaryRef *) &props_dict,
289 kCFAllocatorDefault,
290 kNilOptions)
291 != kIOReturnSuccess)
292 {
293 syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
294 continue;
295 }
297 if (props_dict == NULL)
298 continue;
300 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
301 CFSTR (kIOBlockStorageDriverStatisticsKey));
303 if (stats_dict == NULL)
304 {
305 CFRelease (props_dict);
306 continue;
307 }
309 read_ops = dict_get_value (stats_dict,
310 kIOBlockStorageDriverStatisticsReadsKey);
311 read_byt = dict_get_value (stats_dict,
312 kIOBlockStorageDriverStatisticsBytesReadKey);
313 read_tme = dict_get_value (stats_dict,
314 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
315 write_ops = dict_get_value (stats_dict,
316 kIOBlockStorageDriverStatisticsWritesKey);
317 write_byt = dict_get_value (stats_dict,
318 kIOBlockStorageDriverStatisticsBytesWrittenKey);
319 write_tme = dict_get_value (stats_dict,
320 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
322 if ((read_ops != -1LL)
323 || (read_byt != -1LL)
324 || (read_tme != -1LL)
325 || (write_ops != -1LL)
326 || (write_byt != -1LL)
327 || (write_tme != -1LL))
328 disk_submit (kIOBlockStorageDriverClass, /* FIXME */
329 read_ops, 0ULL, read_byt, read_tme,
330 write_ops, 0ULL, write_byt, write_tme);
332 CFRelease (props_dict);
333 IOObjectRelease (disk);
334 }
335 IOObjectRelease (disk_list);
336 /* #endif HAVE_IOKIT_IOKITLIB_H */
338 #elif KERNEL_LINUX
339 FILE *fh;
340 char buffer[1024];
341 char disk_name[128];
343 char *fields[32];
344 int numfields;
345 int fieldshift = 0;
347 int major = 0;
348 int minor = 0;
350 unsigned int read_sectors;
351 unsigned int write_sectors;
353 unsigned long long read_count = 0;
354 unsigned long long read_merged = 0;
355 unsigned long long read_bytes = 0;
356 unsigned long long read_time = 0;
357 unsigned long long write_count = 0;
358 unsigned long long write_merged = 0;
359 unsigned long long write_bytes = 0;
360 unsigned long long write_time = 0;
361 int is_disk = 0;
363 diskstats_t *ds, *pre_ds;
365 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
366 {
367 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
368 return;
370 /* Kernel is 2.4.* */
371 fieldshift = 1;
372 }
374 while (fgets (buffer, 1024, fh) != NULL)
375 {
376 numfields = strsplit (buffer, fields, 32);
378 if ((numfields != (14 + fieldshift)) && (numfields != 7))
379 continue;
381 major = atoll (fields[0]);
382 minor = atoll (fields[1]);
384 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
385 continue;
386 disk_name[127] = '\0';
388 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
389 if (strcmp (disk_name, ds->name) == 0)
390 break;
392 if (ds == NULL)
393 {
394 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
395 continue;
397 if ((ds->name = strdup (disk_name)) == NULL)
398 {
399 free (ds);
400 continue;
401 }
403 if (pre_ds == NULL)
404 disklist = ds;
405 else
406 pre_ds->next = ds;
407 }
409 is_disk = 0;
410 if (numfields == 7)
411 {
412 /* Kernel 2.6, Partition */
413 read_count = atoll (fields[3]);
414 read_sectors = atoi (fields[4]);
415 write_count = atoll (fields[5]);
416 write_sectors = atoi (fields[6]);
417 }
418 else if (numfields == (14 + fieldshift))
419 {
420 read_count = atoll (fields[3 + fieldshift]);
421 write_count = atoll (fields[7 + fieldshift]);
423 read_sectors = atoi (fields[5 + fieldshift]);
424 write_sectors = atoi (fields[9 + fieldshift]);
426 if ((fieldshift == 0) || (minor == 0))
427 {
428 is_disk = 1;
429 read_merged = atoll (fields[4 + fieldshift]);
430 read_time = atoll (fields[6 + fieldshift]);
431 write_merged = atoll (fields[8 + fieldshift]);
432 write_time = atoll (fields[10+ fieldshift]);
433 }
434 }
435 else
436 {
437 continue;
438 }
441 if (read_sectors >= ds->read_sectors)
442 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
443 else
444 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
446 if (write_sectors >= ds->write_sectors)
447 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
448 else
449 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
451 ds->read_sectors = read_sectors;
452 ds->write_sectors = write_sectors;
453 read_bytes = ds->read_bytes;
454 write_bytes = ds->write_bytes;
456 /* Don't write to the RRDs if we've just started.. */
457 ds->poll_count++;
458 if (ds->poll_count <= 6)
459 continue;
461 if ((read_count == 0) && (write_count == 0))
462 continue;
464 if (is_disk)
465 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
466 write_count, write_merged, write_bytes, write_time);
467 else
468 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
469 }
471 fclose (fh);
472 /* #endif defined(KERNEL_LINUX) */
474 #elif HAVE_LIBKSTAT
475 static kstat_io_t kio;
476 int i;
478 if (kc == NULL)
479 return;
481 for (i = 0; i < numdisk; i++)
482 {
483 if (kstat_read (kc, ksp[i], &kio) == -1)
484 continue;
486 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
487 disk_submit (ksp[i]->ks_name,
488 kio.reads, 0LL, kio.nread, kio.rtime,
489 kio.writes, 0LL, kio.nwritten, kio.wtime);
490 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
491 partition_submit (ksp[i]->ks_name,
492 kio.reads, kio.nread,
493 kio.writes,kio.nwritten);
494 }
495 #endif /* defined(HAVE_LIBKSTAT) */
496 } /* static void disk_read (void) */
497 #else
498 # define disk_read NULL
499 #endif /* DISK_HAVE_READ */
501 void module_register (void)
502 {
503 plugin_register ("partition", NULL, NULL, partition_write);
504 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
505 }
507 #undef MODULE_NAME