The defines in the `disk' plugin have been brought to the `state of the art'.. ;)
[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
27 #define MODULE_NAME "disk"
28
29 #if KERNEL_LINUX || HAVE_LIBKSTAT
30 # define DISK_HAVE_READ 1
31 #else
32 # define DISK_HAVE_READ 0
33 #endif
34
35 static char *disk_filename_template = "disk-%s.rrd";
36 static char *part_filename_template = "partition-%s.rrd";
37
38 /* 104857600 == 100 MB */
39 static char *disk_ds_def[] =
40 {
41         "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
42         "DS:rmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
43         "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
44         "DS:rtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
45         "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
46         "DS:wmerged:COUNTER:"COLLECTD_HEARTBEAT":0:U",
47         "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
48         "DS:wtime:COUNTER:"COLLECTD_HEARTBEAT":0:U",
49         NULL
50 };
51 static int disk_ds_num = 8;
52
53 static char *part_ds_def[] =
54 {
55         "DS:rcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
56         "DS:rbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
57         "DS:wcount:COUNTER:"COLLECTD_HEARTBEAT":0:U",
58         "DS:wbytes:COUNTER:"COLLECTD_HEARTBEAT":0:104857600",
59         NULL
60 };
61 static int part_ds_num = 4;
62
63 #if KERNEL_LINUX
64 typedef struct diskstats
65 {
66         char *name;
67
68         /* This overflows in roughly 1361 year */
69         unsigned int poll_count;
70
71         unsigned int read_sectors;
72         unsigned int write_sectors;
73
74         unsigned long long read_bytes;
75         unsigned long long write_bytes;
76
77         struct diskstats *next;
78 } diskstats_t;
79
80 static diskstats_t *disklist;
81 /* #endif KERNEL_LINUX */
82
83 #elif HAVE_LIBKSTAT
84 #define MAX_NUMDISK 256
85 extern kstat_ctl_t *kc;
86 static kstat_t *ksp[MAX_NUMDISK];
87 static int numdisk = 0;
88 #endif /* HAVE_LIBKSTAT */
89
90 static void disk_init (void)
91 {
92 #if KERNEL_LINUX
93         /* No init needed */
94 /* #endif KERNEL_LINUX */
95
96 #elif HAVE_LIBKSTAT
97         kstat_t *ksp_chain;
98
99         numdisk = 0;
100
101         if (kc == NULL)
102                 return;
103
104         for (numdisk = 0, ksp_chain = kc->kc_chain;
105                         (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
106                         ksp_chain = ksp_chain->ks_next)
107         {
108                 if (strncmp (ksp_chain->ks_class, "disk", 4)
109                                 && strncmp (ksp_chain->ks_class, "partition", 9))
110                         continue;
111                 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
112                         continue;
113                 ksp[numdisk++] = ksp_chain;
114         }
115 #endif /* HAVE_LIBKSTAT */
116
117         return;
118 }
119
120 static void disk_write (char *host, char *inst, char *val)
121 {
122         char file[512];
123         int status;
124
125         status = snprintf (file, 512, disk_filename_template, inst);
126         if (status < 1)
127                 return;
128         else if (status >= 512)
129                 return;
130
131         rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
132 }
133
134 static void partition_write (char *host, char *inst, char *val)
135 {
136         char file[512];
137         int status;
138
139         status = snprintf (file, 512, part_filename_template, inst);
140         if (status < 1)
141                 return;
142         else if (status >= 512)
143                 return;
144
145         rrd_update_file (host, file, val, part_ds_def, part_ds_num);
146 }
147
148 #if DISK_HAVE_READ
149 #define BUFSIZE 512
150 static void disk_submit (char *disk_name,
151                 unsigned long long read_count,
152                 unsigned long long read_merged,
153                 unsigned long long read_bytes,
154                 unsigned long long read_time,
155                 unsigned long long write_count,
156                 unsigned long long write_merged,
157                 unsigned long long write_bytes,
158                 unsigned long long write_time)
159 {
160         char buf[BUFSIZE];
161
162         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
163                                 (unsigned int) curtime,
164                                 read_count, read_merged, read_bytes, read_time,
165                                 write_count, write_merged, write_bytes,
166                                 write_time) >= BUFSIZE)
167                 return;
168
169         plugin_submit (MODULE_NAME, disk_name, buf);
170 }
171
172 static void partition_submit (char *part_name,
173                 unsigned long long read_count,
174                 unsigned long long read_bytes,
175                 unsigned long long write_count,
176                 unsigned long long write_bytes)
177 {
178         char buf[BUFSIZE];
179
180         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
181                                 (unsigned int) curtime,
182                                 read_count, read_bytes, write_count,
183                                 write_bytes) >= BUFSIZE)
184                 return;
185
186         plugin_submit ("partition", part_name, buf);
187 }
188 #undef BUFSIZE
189
190 static void disk_read (void)
191 {
192 #if KERNEL_LINUX
193         FILE *fh;
194         char buffer[1024];
195         char disk_name[128];
196         
197         char *fields[32];
198         int numfields;
199         int fieldshift = 0;
200
201         int major = 0;
202         int minor = 0;
203
204         unsigned int read_sectors;
205         unsigned int write_sectors;
206
207         unsigned long long read_count    = 0;
208         unsigned long long read_merged   = 0;
209         unsigned long long read_bytes    = 0;
210         unsigned long long read_time     = 0;
211         unsigned long long write_count   = 0;
212         unsigned long long write_merged  = 0;
213         unsigned long long write_bytes   = 0;
214         unsigned long long write_time    = 0;
215         int is_disk = 0;
216
217         diskstats_t *ds, *pre_ds;
218
219         if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
220         {
221                 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
222                         return;
223
224                 /* Kernel is 2.4.* */
225                 fieldshift = 1;
226         }
227
228         while (fgets (buffer, 1024, fh) != NULL)
229         {
230                 numfields = strsplit (buffer, fields, 32);
231
232                 if ((numfields != (14 + fieldshift)) && (numfields != 7))
233                         continue;
234
235                 major = atoll (fields[0]);
236                 minor = atoll (fields[1]);
237
238                 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
239                         continue;
240                 disk_name[127] = '\0';
241
242                 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
243                         if (strcmp (disk_name, ds->name) == 0)
244                                 break;
245
246                 if (ds == NULL)
247                 {
248                         if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
249                                 continue;
250
251                         if ((ds->name = strdup (disk_name)) == NULL)
252                         {
253                                 free (ds);
254                                 continue;
255                         }
256
257                         if (pre_ds == NULL)
258                                 disklist = ds;
259                         else
260                                 pre_ds->next = ds;
261                 }
262
263                 is_disk = 0;
264                 if (numfields == 7)
265                 {
266                         /* Kernel 2.6, Partition */
267                         read_count    = atoll (fields[3]);
268                         read_sectors  = atoi  (fields[4]);
269                         write_count   = atoll (fields[5]);
270                         write_sectors = atoi  (fields[6]);
271                 }
272                 else if (numfields == (14 + fieldshift))
273                 {
274                         read_count  =  atoll (fields[3 + fieldshift]);
275                         write_count =  atoll (fields[7 + fieldshift]);
276
277                         read_sectors  = atoi (fields[5 + fieldshift]);
278                         write_sectors = atoi (fields[9 + fieldshift]);
279
280                         if ((fieldshift == 0) || (minor == 0))
281                         {
282                                 is_disk = 1;
283                                 read_merged  = atoll (fields[4 + fieldshift]);
284                                 read_time    = atoll (fields[6 + fieldshift]);
285                                 write_merged = atoll (fields[8 + fieldshift]);
286                                 write_time   = atoll (fields[10+ fieldshift]);
287                         }
288                 }
289                 else
290                 {
291                         continue;
292                 }
293
294
295                 if (read_sectors >= ds->read_sectors)
296                         ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
297                 else
298                         ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
299
300                 if (write_sectors >= ds->write_sectors)
301                         ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
302                 else
303                         ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
304
305                 ds->read_sectors  = read_sectors;
306                 ds->write_sectors = write_sectors;
307                 read_bytes  = ds->read_bytes;
308                 write_bytes = ds->write_bytes;
309
310                 /* Don't write to the RRDs if we've just started.. */
311                 ds->poll_count++;
312                 if (ds->poll_count <= 6)
313                         continue;
314
315                 if ((read_count == 0) && (write_count == 0))
316                         continue;
317
318                 if (is_disk)
319                         disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
320                                         write_count, write_merged, write_bytes, write_time);
321                 else
322                         partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
323         }
324
325         fclose (fh);
326 /* #endif defined(KERNEL_LINUX) */
327
328 #elif HAVE_LIBKSTAT
329         static kstat_io_t kio;
330         int i;
331
332         if (kc == NULL)
333                 return;
334
335         for (i = 0; i < numdisk; i++)
336         {
337                 if (kstat_read (kc, ksp[i], &kio) == -1)
338                         continue;
339
340                 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
341                         disk_submit (ksp[i]->ks_name,
342                                         kio.reads,  0LL, kio.nread,    kio.rtime,
343                                         kio.writes, 0LL, kio.nwritten, kio.wtime);
344                 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
345                         partition_submit (ksp[i]->ks_name,
346                                         kio.reads, kio.nread,
347                                         kio.writes,kio.nwritten);
348         }
349 #endif /* defined(HAVE_LIBKSTAT) */
350 } /* static void disk_read (void) */
351 #else
352 # define disk_read NULL
353 #endif /* DISK_HAVE_READ */
354
355 void module_register (void)
356 {
357         plugin_register ("partition", NULL, NULL, partition_write);
358         plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
359 }
360
361 #undef MODULE_NAME