1 /**
2 * collectd - src/disk.c
3 * Copyright (C) 2005 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #define MODULE_NAME "disk"
29 #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT)
30 # define DISK_HAVE_READ 1
31 #else
32 # define DISK_HAVE_READ 0
33 #endif
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[] =
40 {
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",
49 NULL
50 };
51 static int disk_ds_num = 8;
53 static char *part_ds_def[] =
54 {
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",
59 NULL
60 };
61 static int part_ds_num = 4;
63 #ifdef KERNEL_LINUX
64 typedef struct diskstats
65 {
66 char *name;
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;
75 } diskstats_t;
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)
88 {
89 #ifdef HAVE_LIBKSTAT
90 kstat_t *ksp_chain;
92 numdisk = 0;
94 if (kc == NULL)
95 return;
97 for (numdisk = 0, ksp_chain = kc->kc_chain;
98 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
99 ksp_chain = ksp_chain->ks_next)
100 {
101 if (strncmp (ksp_chain->ks_class, "disk", 4)
102 && strncmp (ksp_chain->ks_class, "partition", 9))
103 continue;
104 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
105 continue;
106 ksp[numdisk++] = ksp_chain;
107 }
108 #endif
110 return;
111 }
113 static void disk_write (char *host, char *inst, char *val)
114 {
115 char file[512];
116 int status;
118 status = snprintf (file, 512, disk_filename_template, inst);
119 if (status < 1)
120 return;
121 else if (status >= 512)
122 return;
124 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
125 }
127 static void partition_write (char *host, char *inst, char *val)
128 {
129 char file[512];
130 int status;
132 status = snprintf (file, 512, part_filename_template, inst);
133 if (status < 1)
134 return;
135 else if (status >= 512)
136 return;
138 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
139 }
141 #if DISK_HAVE_READ
142 #define BUFSIZE 512
143 static void disk_submit (char *disk_name,
144 unsigned long long read_count,
145 unsigned long long read_merged,
146 unsigned long long read_bytes,
147 unsigned long long read_time,
148 unsigned long long write_count,
149 unsigned long long write_merged,
150 unsigned long long write_bytes,
151 unsigned long long write_time)
152 {
153 char buf[BUFSIZE];
155 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu:%llu:%llu:%llu:%llu",
156 (unsigned int) curtime,
157 read_count, read_merged, read_bytes, read_time,
158 write_count, write_merged, write_bytes,
159 write_time) >= BUFSIZE)
160 return;
162 plugin_submit (MODULE_NAME, disk_name, buf);
163 }
165 static void partition_submit (char *part_name,
166 unsigned long long read_count,
167 unsigned long long read_bytes,
168 unsigned long long write_count,
169 unsigned long long write_bytes)
170 {
171 char buf[BUFSIZE];
173 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu:%llu:%llu",
174 (unsigned int) curtime,
175 read_count, read_bytes, write_count,
176 write_bytes) >= BUFSIZE)
177 return;
179 plugin_submit ("partition", part_name, buf);
180 }
181 #undef BUFSIZE
183 static void disk_read (void)
184 {
185 #ifdef KERNEL_LINUX
186 FILE *fh;
187 char buffer[1024];
188 char disk_name[128];
190 char *fields[32];
191 int numfields;
192 int fieldshift = 0;
194 int major = 0;
195 int minor = 0;
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;
208 int is_disk = 0;
210 diskstats_t *ds, *pre_ds;
212 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
213 {
214 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
215 return;
217 /* Kernel is 2.4.* */
218 fieldshift = 1;
219 }
221 while (fgets (buffer, 1024, fh) != NULL)
222 {
223 numfields = strsplit (buffer, fields, 32);
225 if ((numfields != (14 + fieldshift)) && (numfields != 7))
226 continue;
228 major = atoll (fields[0]);
229 minor = atoll (fields[1]);
231 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
232 continue;
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)
237 break;
239 if (ds == NULL)
240 {
241 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
242 continue;
244 if ((ds->name = strdup (disk_name)) == NULL)
245 {
246 free (ds);
247 continue;
248 }
250 if (pre_ds == NULL)
251 disklist = ds;
252 else
253 pre_ds->next = ds;
254 }
256 is_disk = 0;
257 if (numfields == 7)
258 {
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]);
264 }
265 else if (numfields == (14 + fieldshift))
266 {
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))
274 {
275 is_disk = 1;
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]);
280 }
281 }
282 else
283 {
284 continue;
285 }
288 if (read_sectors >= ds->read_sectors)
289 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
290 else
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);
295 else
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))
305 continue;
307 if (is_disk)
308 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
309 write_count, write_merged, write_bytes, write_time);
310 else
311 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
312 }
314 fclose (fh);
315 /* #endif defined(KERNEL_LINUX) */
317 #elif defined(HAVE_LIBKSTAT)
318 static kstat_io_t kio;
319 int i;
321 if (kc == NULL)
322 return;
324 for (i = 0; i < numdisk; i++)
325 {
326 if (kstat_read (kc, ksp[i], &kio) == -1)
327 continue;
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);
337 }
338 #endif /* defined(HAVE_LIBKSTAT) */
339 } /* static void disk_read (void) */
340 #else
341 # define disk_read NULL
342 #endif /* DISK_HAVE_READ */
344 void module_register (void)
345 {
346 plugin_register ("partition", NULL, NULL, partition_write);
347 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
348 }
350 #undef MODULE_NAME