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