2 * collectd - src/disk.c
3 * Copyright (C) 2005,2006 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 KERNEL_LINUX || 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:"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",
51 static int disk_ds_num = 8;
53 static char *part_ds_def[] =
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",
61 static int part_ds_num = 4;
64 typedef struct diskstats
68 /* This overflows in roughly 1361 year */
69 unsigned int poll_count;
71 unsigned int read_sectors;
72 unsigned int write_sectors;
74 unsigned long long read_bytes;
75 unsigned long long write_bytes;
77 struct diskstats *next;
80 static diskstats_t *disklist;
81 /* #endif KERNEL_LINUX */
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 */
90 static void disk_init (void)
94 /* #endif KERNEL_LINUX */
104 for (numdisk = 0, ksp_chain = kc->kc_chain;
105 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
106 ksp_chain = ksp_chain->ks_next)
108 if (strncmp (ksp_chain->ks_class, "disk", 4)
109 && strncmp (ksp_chain->ks_class, "partition", 9))
111 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
113 ksp[numdisk++] = ksp_chain;
115 #endif /* HAVE_LIBKSTAT */
120 static void disk_write (char *host, char *inst, char *val)
125 status = snprintf (file, 512, disk_filename_template, inst);
128 else if (status >= 512)
131 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
134 static void partition_write (char *host, char *inst, char *val)
139 status = snprintf (file, 512, part_filename_template, inst);
142 else if (status >= 512)
145 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
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)
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)
169 plugin_submit (MODULE_NAME, disk_name, buf);
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)
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)
186 plugin_submit ("partition", part_name, buf);
190 static void disk_read (void)
204 unsigned int read_sectors;
205 unsigned int write_sectors;
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;
217 diskstats_t *ds, *pre_ds;
219 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
221 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
224 /* Kernel is 2.4.* */
228 while (fgets (buffer, 1024, fh) != NULL)
230 numfields = strsplit (buffer, fields, 32);
232 if ((numfields != (14 + fieldshift)) && (numfields != 7))
235 major = atoll (fields[0]);
236 minor = atoll (fields[1]);
238 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
240 disk_name[127] = '\0';
242 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
243 if (strcmp (disk_name, ds->name) == 0)
248 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
251 if ((ds->name = strdup (disk_name)) == NULL)
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]);
272 else if (numfields == (14 + fieldshift))
274 read_count = atoll (fields[3 + fieldshift]);
275 write_count = atoll (fields[7 + fieldshift]);
277 read_sectors = atoi (fields[5 + fieldshift]);
278 write_sectors = atoi (fields[9 + fieldshift]);
280 if ((fieldshift == 0) || (minor == 0))
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]);
295 if (read_sectors >= ds->read_sectors)
296 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
298 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
300 if (write_sectors >= ds->write_sectors)
301 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
303 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
305 ds->read_sectors = read_sectors;
306 ds->write_sectors = write_sectors;
307 read_bytes = ds->read_bytes;
308 write_bytes = ds->write_bytes;
310 /* Don't write to the RRDs if we've just started.. */
312 if (ds->poll_count <= 6)
315 if ((read_count == 0) && (write_count == 0))
319 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
320 write_count, write_merged, write_bytes, write_time);
322 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
326 /* #endif defined(KERNEL_LINUX) */
329 static kstat_io_t kio;
335 for (i = 0; i < numdisk; i++)
337 if (kstat_read (kc, ksp[i], &kio) == -1)
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);
349 #endif /* defined(HAVE_LIBKSTAT) */
350 } /* static void disk_read (void) */
352 # define disk_read NULL
353 #endif /* DISK_HAVE_READ */
355 void module_register (void)
357 plugin_register ("partition", NULL, NULL, partition_write);
358 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);