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:"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 defined(KERNEL_LINUX) */
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 */
90 static void disk_init (void)
100 for (numdisk = 0, ksp_chain = kc->kc_chain;
101 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
102 ksp_chain = ksp_chain->ks_next)
104 if (strncmp (ksp_chain->ks_class, "disk", 4)
105 && strncmp (ksp_chain->ks_class, "partition", 9))
107 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
109 ksp[numdisk++] = ksp_chain;
116 static void disk_write (char *host, char *inst, char *val)
121 status = snprintf (file, 512, disk_filename_template, inst);
124 else if (status >= 512)
127 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
130 static void partition_write (char *host, char *inst, char *val)
135 status = snprintf (file, 512, part_filename_template, inst);
138 else if (status >= 512)
141 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
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)
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)
165 plugin_submit (MODULE_NAME, disk_name, buf);
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)
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)
182 plugin_submit ("partition", part_name, buf);
186 static void disk_read (void)
200 unsigned int read_sectors;
201 unsigned int write_sectors;
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;
213 diskstats_t *ds, *pre_ds;
215 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
217 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
220 /* Kernel is 2.4.* */
224 while (fgets (buffer, 1024, fh) != NULL)
226 numfields = strsplit (buffer, fields, 32);
228 if ((numfields != (14 + fieldshift)) && (numfields != 7))
231 major = atoll (fields[0]);
232 minor = atoll (fields[1]);
234 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
236 disk_name[127] = '\0';
238 for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
239 if (strcmp (disk_name, ds->name) == 0)
244 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
247 if ((ds->name = strdup (disk_name)) == NULL)
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]);
268 else if (numfields == (14 + fieldshift))
270 read_count = atoll (fields[3 + fieldshift]);
271 write_count = atoll (fields[7 + fieldshift]);
273 read_sectors = atoi (fields[5 + fieldshift]);
274 write_sectors = atoi (fields[9 + fieldshift]);
276 if ((fieldshift == 0) || (minor == 0))
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]);
291 if (read_sectors >= ds->read_sectors)
292 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
294 ds->read_bytes += 512 * ((UINT_MAX - ds->read_sectors) + read_sectors);
296 if (write_sectors >= ds->write_sectors)
297 ds->write_bytes += 512 * (write_sectors - ds->write_sectors);
299 ds->write_bytes += 512 * ((UINT_MAX - ds->write_sectors) + write_sectors);
301 ds->read_sectors = read_sectors;
302 ds->write_sectors = write_sectors;
303 read_bytes = ds->read_bytes;
304 write_bytes = ds->write_bytes;
306 /* Don't write to the RRDs if we've just started.. */
308 if (ds->poll_count <= 6)
311 if ((read_count == 0) && (write_count == 0))
315 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
316 write_count, write_merged, write_bytes, write_time);
318 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
322 /* #endif defined(KERNEL_LINUX) */
324 #elif defined(HAVE_LIBKSTAT)
325 static kstat_io_t kio;
331 for (i = 0; i < numdisk; i++)
333 if (kstat_read (kc, ksp[i], &kio) == -1)
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);
345 #endif /* defined(HAVE_LIBKSTAT) */
346 } /* static void disk_read (void) */
348 # define disk_read NULL
349 #endif /* DISK_HAVE_READ */
351 void module_register (void)
353 plugin_register ("partition", NULL, NULL, partition_write);
354 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);