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;
93 for (numdisk = 0, ksp_chain = kc->kc_chain;
94 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
95 ksp_chain = ksp_chain->ks_next)
97 if (strncmp (ksp_chain->ks_class, "disk", 4)
98 && strncmp (ksp_chain->ks_class, "partition", 9))
100 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
102 ksp[numdisk++] = ksp_chain;
109 void disk_write (char *host, char *inst, char *val)
114 status = snprintf (file, 512, disk_filename_template, inst);
117 else if (status >= 512)
120 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
123 void partition_write (char *host, char *inst, char *val)
128 status = snprintf (file, 512, part_filename_template, inst);
131 else if (status >= 512)
134 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
138 void disk_submit (char *disk_name,
139 unsigned long long read_count,
140 unsigned long long read_merged,
141 unsigned long long read_bytes,
142 unsigned long long read_time,
143 unsigned long long write_count,
144 unsigned long long write_merged,
145 unsigned long long write_bytes,
146 unsigned long long write_time)
150 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
151 (unsigned int) curtime,
152 read_count, read_merged, read_bytes, read_time,
153 write_count, write_merged, write_bytes,
154 write_time) >= BUFSIZE)
157 plugin_submit (MODULE_NAME, disk_name, buf);
160 void partition_submit (char *part_name,
161 unsigned long long read_count,
162 unsigned long long read_bytes,
163 unsigned long long write_count,
164 unsigned long long write_bytes)
168 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
169 (unsigned int) curtime,
170 read_count, read_bytes, write_count,
171 write_bytes) >= BUFSIZE)
174 plugin_submit ("partition", part_name, buf);
178 void disk_read (void)
192 unsigned int read_sectors;
193 unsigned int write_sectors;
195 unsigned long long read_count = 0;
196 unsigned long long read_merged = 0;
197 unsigned long long read_bytes = 0;
198 unsigned long long read_time = 0;
199 unsigned long long write_count = 0;
200 unsigned long long write_merged = 0;
201 unsigned long long write_bytes = 0;
202 unsigned long long write_time = 0;
205 diskstats_t *ds, *pre_ds;
207 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
209 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
212 /* Kernel is 2.4.* */
216 while (fgets (buffer, 1024, fh) != NULL)
218 numfields = strsplit (buffer, fields, 32);
220 if ((numfields != (14 + fieldshift)) && (numfields != 7))
223 major = atoll (fields[0]);
224 minor = atoll (fields[1]);
226 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
228 disk_name[127] = '\0';
230 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
231 if (strcmp (disk_name, ds->name) == 0)
236 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
239 if ((ds->name = strdup (disk_name)) == NULL)
254 /* Kernel 2.6, Partition */
255 read_count = atoll (fields[3]);
256 read_sectors = atoi (fields[4]);
257 write_count = atoll (fields[5]);
258 write_sectors = atoi (fields[6]);
260 else if (numfields == (14 + fieldshift))
262 read_count = atoll (fields[3 + fieldshift]);
263 write_count = atoll (fields[7 + fieldshift]);
265 read_sectors = atoi (fields[5 + fieldshift]);
266 write_sectors = atoi (fields[9 + fieldshift]);
268 if ((fieldshift == 0) || (minor == 0))
271 read_merged = atoll (fields[4 + fieldshift]);
272 read_time = atoll (fields[6 + fieldshift]);
273 write_merged = atoll (fields[8 + fieldshift]);
274 write_time = atoll (fields[10+ fieldshift]);
283 if (read_sectors >= ds->read_sectors)
284 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
286 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
288 if (write_sectors >= ds->write_sectors)
289 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
291 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
293 ds->read_sectors = read_sectors;
294 ds->write_sectors = write_sectors;
295 read_bytes = ds->read_bytes;
296 write_bytes = ds->write_bytes;
299 if ((read_count == 0) && (write_count == 0))
303 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
304 write_count, write_merged, write_bytes, write_time);
306 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
310 /* #endif defined(KERNEL_LINUX) */
312 #elif defined(HAVE_LIBKSTAT)
313 static kstat_io_t kio;
319 for (i = 0; i < numdisk; i++)
321 if (kstat_read (kc, ksp[i], &kio) == -1)
324 if (strncmp (ksp[i]->ks_class, "disk", 4) == 0)
325 disk_submit (ksp[i]->ks_name,
326 kio.reads, 0LL, kio.nread, kio.rtime,
327 kio.writes, 0LL, kio.nwritten, kio.wtime);
328 else if (strncmp (ksp[i]->ks_class, "partition", 9) == 0)
329 partition_submit (ksp[i]->ks_name,
330 kio.reads, kio.nread,
331 kio.writes,kio.nwritten);
333 #endif /* defined(HAVE_LIBKSTAT) */
336 void module_register (void)
338 plugin_register ("partition", NULL, NULL, partition_write);
339 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
343 #endif /* COLLECT_DISK */