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>
26 #define MODULE_NAME "disk"
32 typedef struct diskstats
36 unsigned int read_sectors;
37 unsigned int write_sectors;
39 unsigned long long read_bytes;
40 unsigned long long write_bytes;
42 struct diskstats *next;
45 static diskstats_t *disklist;
48 #elif defined(HAVE_LIBKSTAT)
49 #define MAX_NUMDISK 256
50 extern kstat_ctl_t *kc;
51 static kstat_t *ksp[MAX_NUMDISK];
52 static int numdisk = 0;
53 #endif /* HAVE_LIBKSTAT */
55 static char *disk_filename_template = "disk-%s.rrd";
56 static char *part_filename_template = "partition-%s.rrd";
58 /* 104857600 == 100 MB */
59 static char *disk_ds_def[] =
61 "DS:rcount:COUNTER:25:0:U",
62 "DS:rmerged:COUNTER:25:0:U",
63 "DS:rbytes:COUNTER:25:0:104857600",
64 "DS:rtime:COUNTER:25:0:U",
65 "DS:wcount:COUNTER:25:0:U",
66 "DS:wmerged:COUNTER:25:0:U",
67 "DS:wbytes:COUNTER:25:0:104857600",
68 "DS:wtime:COUNTER:25:0:U",
71 static int disk_ds_num = 8;
73 static char *part_ds_def[] =
75 "DS:rcount:COUNTER:25:0:U",
76 "DS:rbytes:COUNTER:25:0:104857600",
77 "DS:wcount:COUNTER:25:0:U",
78 "DS:wbytes:COUNTER:25:0:104857600",
81 static int part_ds_num = 4;
83 extern time_t curtime;
95 for (numdisk = 0, ksp_chain = kc->kc_chain;
96 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
97 ksp_chain = ksp_chain->ks_next)
99 if (strncmp (ksp_chain->ks_class, "disk", 4)
100 && strncmp (ksp_chain->ks_class, "partition", 9))
102 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
104 ksp[numdisk++] = ksp_chain;
111 void disk_write (char *host, char *inst, char *val)
116 status = snprintf (file, 512, disk_filename_template, inst);
119 else if (status >= 512)
122 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
125 void partition_write (char *host, char *inst, char *val)
130 status = snprintf (file, 512, part_filename_template, inst);
133 else if (status >= 512)
136 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
140 void disk_submit (char *disk_name,
141 unsigned long long read_count,
142 unsigned long long read_merged,
143 unsigned long long read_bytes,
144 unsigned long long read_time,
145 unsigned long long write_count,
146 unsigned long long write_merged,
147 unsigned long long write_bytes,
148 unsigned long long write_time)
152 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
153 (unsigned int) curtime,
154 read_count, read_merged, read_bytes, read_time,
155 write_count, write_merged, write_bytes,
156 write_time) >= BUFSIZE)
159 plugin_submit (MODULE_NAME, disk_name, buf);
162 void partition_submit (char *part_name,
163 unsigned long long read_count,
164 unsigned long long read_bytes,
165 unsigned long long write_count,
166 unsigned long long write_bytes)
170 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
171 (unsigned int) curtime,
172 read_count, read_bytes, write_count,
173 write_bytes) >= BUFSIZE)
176 plugin_submit ("partition", part_name, buf);
180 void disk_read (void)
194 unsigned int read_sectors;
195 unsigned int write_sectors;
197 unsigned long long read_count = 0;
198 unsigned long long read_merged = 0;
199 unsigned long long read_bytes = 0;
200 unsigned long long read_time = 0;
201 unsigned long long write_count = 0;
202 unsigned long long write_merged = 0;
203 unsigned long long write_bytes = 0;
204 unsigned long long write_time = 0;
207 diskstats_t *ds, *pre_ds;
209 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
211 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
214 /* Kernel is 2.4.* */
218 while (fgets (buffer, 1024, fh) != NULL)
220 numfields = strsplit (buffer, fields, 32);
222 if ((numfields != (14 + fieldshift)) && (numfields != 7))
225 major = atoll (fields[0]);
226 minor = atoll (fields[1]);
228 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
230 disk_name[127] = '\0';
232 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
233 if (strcmp (disk_name, ds->name) == 0)
238 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
241 if ((ds->name = strdup (disk_name)) == NULL)
256 /* Kernel 2.6, Partition */
257 read_count = atoll (fields[3]);
258 read_sectors = atoi (fields[4]);
259 write_count = atoll (fields[5]);
260 write_sectors = atoi (fields[6]);
262 else if (numfields == (14 + fieldshift))
264 read_count = atoll (fields[3 + fieldshift]);
265 write_count = atoll (fields[7 + fieldshift]);
267 read_sectors = atoi (fields[5 + fieldshift]);
268 write_sectors = atoi (fields[9 + fieldshift]);
270 if ((fieldshift == 0) || (minor == 0))
273 read_merged = atoll (fields[4 + fieldshift]);
274 read_time = atoll (fields[6 + fieldshift]);
275 write_merged = atoll (fields[8 + fieldshift]);
276 write_time = atoll (fields[10+ fieldshift]);
285 if (read_sectors >= ds->read_sectors)
286 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
288 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
290 if (write_sectors >= ds->write_sectors)
291 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
293 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
295 ds->read_sectors = read_sectors;
296 ds->write_sectors = write_sectors;
297 read_bytes = ds->read_bytes;
298 write_bytes = ds->write_bytes;
301 if ((read_count == 0) && (write_count == 0))
305 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
306 write_count, write_merged, write_bytes, write_time);
308 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
312 /* #endif defined(KERNEL_LINUX) */
314 #elif defined(HAVE_LIBKSTAT)
315 static kstat_io_t kio;
321 for (i = 0; i < numdisk; i++)
323 if (kstat_read (kc, ksp[i], &kio) == -1)
326 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
327 disk_submit (ksp[i]->ks_name,
328 kio.reads, 0LL, kio.nread, kio.rtime,
329 kio.writes, 0LL, kio.nwritten, kio.wtime);
330 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
331 partition_submit (ksp[i]->ks_name,
332 kio.reads, kio.nread,
333 kio.writes,kio.nwritten);
335 #endif /* defined(HAVE_LIBKSTAT) */
338 void module_register (void)
340 plugin_register ("partition", NULL, NULL, partition_write);
341 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
345 #endif /* COLLECT_DISK */