2 * collectd - src/disk.c
3 * Copyright (C) 2005 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
27 #define MODULE_NAME "disk"
29 #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT)
30 # define DISK_HAVE_READ 1
32 # define DISK_HAVE_READ 0
35 static char *disk_filename_template = "disk-%s.rrd";
36 static char *part_filename_template = "partition-%s.rrd";
38 /* 104857600 == 100 MB */
39 static char *disk_ds_def[] =
41 "DS:rcount:COUNTER:25:0:U",
42 "DS:rmerged:COUNTER:25:0:U",
43 "DS:rbytes:COUNTER:25:0:104857600",
44 "DS:rtime:COUNTER:25:0:U",
45 "DS:wcount:COUNTER:25:0:U",
46 "DS:wmerged:COUNTER:25:0:U",
47 "DS:wbytes:COUNTER:25:0:104857600",
48 "DS:wtime:COUNTER:25:0:U",
51 static int disk_ds_num = 8;
53 static char *part_ds_def[] =
55 "DS:rcount:COUNTER:25:0:U",
56 "DS:rbytes:COUNTER:25:0:104857600",
57 "DS:wcount:COUNTER:25:0:U",
58 "DS:wbytes:COUNTER:25:0:104857600",
61 static int part_ds_num = 4;
64 typedef struct diskstats
68 unsigned int read_sectors;
69 unsigned int write_sectors;
71 unsigned long long read_bytes;
72 unsigned long long write_bytes;
74 struct diskstats *next;
77 static diskstats_t *disklist;
78 /* #endif defined(KERNEL_LINUX) */
80 #elif defined(HAVE_LIBKSTAT)
81 #define MAX_NUMDISK 256
82 extern kstat_ctl_t *kc;
83 static kstat_t *ksp[MAX_NUMDISK];
84 static int numdisk = 0;
85 #endif /* HAVE_LIBKSTAT */
87 static void disk_init (void)
97 for (numdisk = 0, ksp_chain = kc->kc_chain;
98 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
99 ksp_chain = ksp_chain->ks_next)
101 if (strncmp (ksp_chain->ks_class, "disk", 4)
102 && strncmp (ksp_chain->ks_class, "partition", 9))
104 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
106 ksp[numdisk++] = ksp_chain;
113 static void disk_write (char *host, char *inst, char *val)
118 status = snprintf (file, 512, disk_filename_template, inst);
121 else if (status >= 512)
124 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
127 static void partition_write (char *host, char *inst, char *val)
132 status = snprintf (file, 512, part_filename_template, inst);
135 else if (status >= 512)
138 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
142 static void disk_submit (char *disk_name,
143 unsigned long long read_count,
144 unsigned long long read_merged,
145 unsigned long long read_bytes,
146 unsigned long long read_time,
147 unsigned long long write_count,
148 unsigned long long write_merged,
149 unsigned long long write_bytes,
150 unsigned long long write_time)
154 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
155 (unsigned int) curtime,
156 read_count, read_merged, read_bytes, read_time,
157 write_count, write_merged, write_bytes,
158 write_time) >= BUFSIZE)
161 plugin_submit (MODULE_NAME, disk_name, buf);
164 static void partition_submit (char *part_name,
165 unsigned long long read_count,
166 unsigned long long read_bytes,
167 unsigned long long write_count,
168 unsigned long long write_bytes)
172 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
173 (unsigned int) curtime,
174 read_count, read_bytes, write_count,
175 write_bytes) >= BUFSIZE)
178 plugin_submit ("partition", part_name, buf);
183 static void disk_read (void)
197 unsigned int read_sectors;
198 unsigned int write_sectors;
200 unsigned long long read_count = 0;
201 unsigned long long read_merged = 0;
202 unsigned long long read_bytes = 0;
203 unsigned long long read_time = 0;
204 unsigned long long write_count = 0;
205 unsigned long long write_merged = 0;
206 unsigned long long write_bytes = 0;
207 unsigned long long write_time = 0;
210 diskstats_t *ds, *pre_ds;
212 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
214 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
217 /* Kernel is 2.4.* */
221 while (fgets (buffer, 1024, fh) != NULL)
223 numfields = strsplit (buffer, fields, 32);
225 if ((numfields != (14 + fieldshift)) && (numfields != 7))
228 major = atoll (fields[0]);
229 minor = atoll (fields[1]);
231 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
233 disk_name[127] = '\0';
235 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
236 if (strcmp (disk_name, ds->name) == 0)
241 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
244 if ((ds->name = strdup (disk_name)) == NULL)
259 /* Kernel 2.6, Partition */
260 read_count = atoll (fields[3]);
261 read_sectors = atoi (fields[4]);
262 write_count = atoll (fields[5]);
263 write_sectors = atoi (fields[6]);
265 else if (numfields == (14 + fieldshift))
267 read_count = atoll (fields[3 + fieldshift]);
268 write_count = atoll (fields[7 + fieldshift]);
270 read_sectors = atoi (fields[5 + fieldshift]);
271 write_sectors = atoi (fields[9 + fieldshift]);
273 if ((fieldshift == 0) || (minor == 0))
276 read_merged = atoll (fields[4 + fieldshift]);
277 read_time = atoll (fields[6 + fieldshift]);
278 write_merged = atoll (fields[8 + fieldshift]);
279 write_time = atoll (fields[10+ fieldshift]);
288 if (read_sectors >= ds->read_sectors)
289 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
291 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
293 if (write_sectors >= ds->write_sectors)
294 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
296 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
298 ds->read_sectors = read_sectors;
299 ds->write_sectors = write_sectors;
300 read_bytes = ds->read_bytes;
301 write_bytes = ds->write_bytes;
304 if ((read_count == 0) && (write_count == 0))
308 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
309 write_count, write_merged, write_bytes, write_time);
311 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
315 /* #endif defined(KERNEL_LINUX) */
317 #elif defined(HAVE_LIBKSTAT)
318 static kstat_io_t kio;
324 for (i = 0; i < numdisk; i++)
326 if (kstat_read (kc, ksp[i], &kio) == -1)
329 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
330 disk_submit (ksp[i]->ks_name,
331 kio.reads, 0LL, kio.nread, kio.rtime,
332 kio.writes, 0LL, kio.nwritten, kio.wtime);
333 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
334 partition_submit (ksp[i]->ks_name,
335 kio.reads, kio.nread,
336 kio.writes,kio.nwritten);
338 #endif /* defined(HAVE_LIBKSTAT) */
339 } /* static void disk_read (void) */
341 # define disk_read NULL
342 #endif /* DISK_HAVE_READ */
344 void module_register (void)
346 plugin_register ("partition", NULL, NULL, partition_write);
347 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);