solaris-fixes branch: Applied the swap-patch by Christophe Kalt.
[collectd.git] / src / disk.c
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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "utils_debug.h"
27
28 #define MODULE_NAME "disk"
29
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
57
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
63
64 static char *disk_filename_template = "disk-%s.rrd";
65 static char *part_filename_template = "partition-%s.rrd";
66
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;
81
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;
91
92 #if HAVE_IOKIT_IOKITLIB_H
93 static mach_port_t io_master_port = MACH_PORT_NULL;
94 /* #endif HAVE_IOKIT_IOKITLIB_H */
95
96 #elif KERNEL_LINUX
97 typedef struct diskstats
98 {
99         char *name;
100
101         /* This overflows in roughly 1361 year */
102         unsigned int poll_count;
103
104         unsigned int read_sectors;
105         unsigned int write_sectors;
106
107         unsigned long long read_bytes;
108         unsigned long long write_bytes;
109
110         struct diskstats *next;
111 } diskstats_t;
112
113 static diskstats_t *disklist;
114 /* #endif KERNEL_LINUX */
115
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 */
122
123 static void disk_init (void)
124 {
125 #if HAVE_IOKIT_IOKITLIB_H
126         kern_return_t status;
127         
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         }
134
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 */
144
145 #elif KERNEL_LINUX
146         /* No init needed */
147 /* #endif KERNEL_LINUX */
148
149 #elif HAVE_LIBKSTAT
150         kstat_t *ksp_chain;
151
152         numdisk = 0;
153
154         if (kc == NULL)
155                 return;
156
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 */
169
170         return;
171 }
172
173 static void disk_write (char *host, char *inst, char *val)
174 {
175         char file[512];
176         int status;
177
178         status = snprintf (file, 512, disk_filename_template, inst);
179         if (status < 1)
180                 return;
181         else if (status >= 512)
182                 return;
183
184         rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
185 }
186
187 static void partition_write (char *host, char *inst, char *val)
188 {
189         char file[512];
190         int status;
191
192         status = snprintf (file, 512, part_filename_template, inst);
193         if (status < 1)
194                 return;
195         else if (status >= 512)
196                 return;
197
198         rrd_update_file (host, file, val, part_ds_def, part_ds_num);
199 }
200
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];
214
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;
221
222         plugin_submit (MODULE_NAME, disk_name, buf);
223 }
224
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];
233
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;
239
240         plugin_submit ("partition", part_name, buf);
241 }
242 #endif /* KERNEL_LINUX || HAVE_LIBKSTAT */
243 #undef BUFSIZE
244
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;
251
252         /* `key_obj' needs to be released. */
253         key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key,
254                         kCFStringEncodingASCII);
255         if (key_obj == NULL)
256         {
257                 DBG ("CFStringCreateWithCString (%s) failed.", key);
258                 return (-1LL);
259         }
260         
261         /* get => we don't need to release (== free) the object */
262         val_obj = (CFNumberRef) CFDictionaryGetValue (dict, key_obj);
263
264         CFRelease (key_obj);
265
266         if (val_obj == NULL)
267         {
268                 DBG ("CFDictionaryGetValue (%s) failed.", key);
269                 return (-1LL);
270         }
271
272         if (!CFNumberGetValue (val_obj, kCFNumberSInt64Type, &val_int))
273         {
274                 DBG ("CFNumberGetValue (%s) failed.", key);
275                 return (-1LL);
276         }
277
278         return (val_int);
279 }
280 #endif /* HAVE_IOKIT_IOKITLIB_H */
281
282 static void disk_read (void)
283 {
284 #if HAVE_IOKIT_IOKITLIB_H
285         io_registry_entry_t     disk;
286         io_registry_entry_t     disk_child;
287         io_iterator_t           disk_list;
288         CFDictionaryRef         props_dict;
289         CFDictionaryRef         stats_dict;
290         CFDictionaryRef         child_dict;
291         kern_return_t           status;
292
293         signed long long read_ops;
294         signed long long read_byt;
295         signed long long read_tme;
296         signed long long write_ops;
297         signed long long write_byt;
298         signed long long write_tme;
299
300         int  disk_major;
301         int  disk_minor;
302         char disk_name[64];
303
304         /* Get the list of all disk objects. */
305         if (IOServiceGetMatchingServices (io_master_port,
306                                 IOServiceMatching (kIOBlockStorageDriverClass),
307                                 &disk_list) != kIOReturnSuccess)
308         {
309                 syslog (LOG_ERR, "disk-plugin: IOServiceGetMatchingServices failed.");
310                 return;
311         }
312
313         while ((disk = IOIteratorNext (disk_list)) != 0)
314         {
315                 props_dict = NULL;
316                 stats_dict = NULL;
317                 child_dict = NULL;
318
319                 /* `disk_child' must be released */
320                 if ((status = IORegistryEntryGetChildEntry (disk, kIOServicePlane, &disk_child))
321                                 != kIOReturnSuccess)
322                 {
323                         /* This fails for example for DVD/CD drives.. */
324                         DBG ("IORegistryEntryGetChildEntry (disk) failed: 0x%08x", status);
325                         IOObjectRelease (disk);
326                         continue;
327                 }
328
329                 /* We create `props_dict' => we need to release it later */
330                 if (IORegistryEntryCreateCFProperties (disk,
331                                         (CFMutableDictionaryRef *) &props_dict,
332                                         kCFAllocatorDefault,
333                                         kNilOptions)
334                                 != kIOReturnSuccess)
335                 {
336                         syslog (LOG_ERR, "disk-plugin: IORegistryEntryCreateCFProperties failed.");
337                         IOObjectRelease (disk_child);
338                         IOObjectRelease (disk);
339                         continue;
340                 }
341
342                 if (props_dict == NULL)
343                 {
344                         DBG ("IORegistryEntryCreateCFProperties (disk) failed.");
345                         IOObjectRelease (disk_child);
346                         IOObjectRelease (disk);
347                         continue;
348                 }
349
350                 stats_dict = (CFDictionaryRef) CFDictionaryGetValue (props_dict,
351                                 CFSTR (kIOBlockStorageDriverStatisticsKey));
352
353                 if (stats_dict == NULL)
354                 {
355                         DBG ("CFDictionaryGetValue (%s) failed.",
356                                         kIOBlockStorageDriverStatisticsKey);
357                         CFRelease (props_dict);
358                         IOObjectRelease (disk_child);
359                         IOObjectRelease (disk);
360                         continue;
361                 }
362
363                 if (IORegistryEntryCreateCFProperties (disk_child,
364                                         (CFMutableDictionaryRef *) &child_dict,
365                                         kCFAllocatorDefault,
366                                         kNilOptions)
367                                 != kIOReturnSuccess)
368                 {
369                         DBG ("IORegistryEntryCreateCFProperties (disk_child) failed.");
370                         IOObjectRelease (disk_child);
371                         CFRelease (props_dict);
372                         IOObjectRelease (disk);
373                         continue;
374                 }
375
376                 disk_major = (int) dict_get_value (child_dict,
377                                 kIOBSDMajorKey);
378                 disk_minor = (int) dict_get_value (child_dict,
379                                 kIOBSDMinorKey);
380                 read_ops  = dict_get_value (stats_dict,
381                                 kIOBlockStorageDriverStatisticsReadsKey);
382                 read_byt  = dict_get_value (stats_dict,
383                                 kIOBlockStorageDriverStatisticsBytesReadKey);
384                 read_tme  = dict_get_value (stats_dict,
385                                 kIOBlockStorageDriverStatisticsTotalReadTimeKey);
386                 write_ops = dict_get_value (stats_dict,
387                                 kIOBlockStorageDriverStatisticsWritesKey);
388                 write_byt = dict_get_value (stats_dict,
389                                 kIOBlockStorageDriverStatisticsBytesWrittenKey);
390                 write_tme = dict_get_value (stats_dict,
391                                 kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
392
393                 if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
394                 {
395                         DBG ("snprintf (major, minor) failed.");
396                         CFRelease (child_dict);
397                         IOObjectRelease (disk_child);
398                         CFRelease (props_dict);
399                         IOObjectRelease (disk);
400                         continue;
401                 }
402                 DBG ("disk_name = %s", disk_name);
403
404                 if ((read_ops != -1LL)
405                                 || (read_byt != -1LL)
406                                 || (read_tme != -1LL)
407                                 || (write_ops != -1LL)
408                                 || (write_byt != -1LL)
409                                 || (write_tme != -1LL))
410                         disk_submit (disk_name,
411                                         read_ops, 0ULL, read_byt, read_tme,
412                                         write_ops, 0ULL, write_byt, write_tme);
413
414                 CFRelease (child_dict);
415                 IOObjectRelease (disk_child);
416                 CFRelease (props_dict);
417                 IOObjectRelease (disk);
418         }
419         IOObjectRelease (disk_list);
420 /* #endif HAVE_IOKIT_IOKITLIB_H */
421
422 #elif KERNEL_LINUX
423         FILE *fh;
424         char buffer[1024];
425         char disk_name[128];
426         
427         char *fields[32];
428         int numfields;
429         int fieldshift = 0;
430
431         int major = 0;
432         int minor = 0;
433
434         unsigned int read_sectors;
435         unsigned int write_sectors;
436
437         unsigned long long read_count    = 0;
438         unsigned long long read_merged   = 0;
439         unsigned long long read_bytes    = 0;
440         unsigned long long read_time     = 0;
441         unsigned long long write_count   = 0;
442         unsigned long long write_merged  = 0;
443         unsigned long long write_bytes   = 0;
444         unsigned long long write_time    = 0;
445         int is_disk = 0;
446
447         diskstats_t *ds, *pre_ds;
448
449         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
450         {
451                 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
452                         return;
453
454                 /* Kernel is 2.4.* */
455                 fieldshift = 1;
456         }
457
458         while (fgets (buffer, 1024, fh) != NULL)
459         {
460                 numfields = strsplit (buffer, fields, 32);
461
462                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
463                         continue;
464
465                 major = atoll (fields[0]);
466                 minor = atoll (fields[1]);
467
468                 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
469                         continue;
470                 disk_name[127] = '\0';
471
472                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
473                         if (strcmp (disk_name, ds->name) == 0)
474                                 break;
475
476                 if (ds == NULL)
477                 {
478                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
479                                 continue;
480
481                         if ((ds->name = strdup (disk_name)) == NULL)
482                         {
483                                 free (ds);
484                                 continue;
485                         }
486
487                         if (pre_ds == NULL)
488                                 disklist = ds;
489                         else
490                                 pre_ds->next = ds;
491                 }
492
493                 is_disk = 0;
494                 if (numfields == 7)
495                 {
496                         /* Kernel 2.6, Partition */
497                         read_count    = atoll (fields[3]);
498                         read_sectors  = atoi  (fields[4]);
499                         write_count   = atoll (fields[5]);
500                         write_sectors = atoi  (fields[6]);
501                 }
502                 else if (numfields == (14 + fieldshift))
503                 {
504                         read_count  =  atoll (fields[3 + fieldshift]);
505                         write_count =  atoll (fields[7 + fieldshift]);
506
507                         read_sectors  = atoi (fields[5 + fieldshift]);
508                         write_sectors = atoi (fields[9 + fieldshift]);
509
510                         if ((fieldshift == 0) || (minor == 0))
511                         {
512                                 is_disk = 1;
513                                 read_merged  = atoll (fields[4 + fieldshift]);
514                                 read_time    = atoll (fields[6 + fieldshift]);
515                                 write_merged = atoll (fields[8 + fieldshift]);
516                                 write_time   = atoll (fields[10+ fieldshift]);
517                         }
518                 }
519                 else
520                 {
521                         continue;
522                 }
523
524
525                 if (read_sectors >= ds->read_sectors)
526                         ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
527                 else
528                         ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
529
530                 if (write_sectors >= ds->write_sectors)
531                         ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
532                 else
533                         ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
534
535                 ds->read_sectors  = read_sectors;
536                 ds->write_sectors = write_sectors;
537                 read_bytes  = ds->read_bytes;
538                 write_bytes = ds->write_bytes;
539
540                 /* Don't write to the RRDs if we've just started.. */
541                 ds->poll_count++;
542                 if (ds->poll_count <= 6)
543                         continue;
544
545                 if ((read_count == 0) && (write_count == 0))
546                         continue;
547
548                 if (is_disk)
549                         disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
550                                         write_count, write_merged, write_bytes, write_time);
551                 else
552                         partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
553         }
554
555         fclose (fh);
556 /* #endif defined(KERNEL_LINUX) */
557
558 #elif HAVE_LIBKSTAT
559         static kstat_io_t kio;
560         int i;
561
562         if (kc == NULL)
563                 return;
564
565         for (i = 0; i < numdisk; i++)
566         {
567                 if (kstat_read (kc, ksp[i], &kio) == -1)
568                         continue;
569
570                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
571                         disk_submit (ksp[i]->ks_name,
572                                         kio.reads,  0LL, kio.nread,    kio.rtime,
573                                         kio.writes, 0LL, kio.nwritten, kio.wtime);
574                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
575                         partition_submit (ksp[i]->ks_name,
576                                         kio.reads, kio.nread,
577                                         kio.writes,kio.nwritten);
578         }
579 #endif /* defined(HAVE_LIBKSTAT) */
580 } /* static void disk_read (void) */
581 #else
582 # define disk_read NULL
583 #endif /* DISK_HAVE_READ */
584
585 void module_register (void)
586 {
587         plugin_register ("partition", NULL, NULL, partition_write);
588         plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
589 }
590
591 #undef MODULE_NAME