a6a583d294285ddd01fd412919c1e8964c29edd2
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 "disk.h"
25 #if COLLECT_DISK
26 #define MODULE_NAME "disk"
28 #include "plugin.h"
29 #include "common.h"
31 #ifdef KERNEL_LINUX
32 typedef struct diskstats
33 {
34 char *name;
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;
43 } diskstats_t;
45 static diskstats_t *disklist;
46 /* KERNEL_LINUX */
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[] =
60 {
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",
69 NULL
70 };
71 static int disk_ds_num = 8;
73 static char *part_ds_def[] =
74 {
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",
79 NULL
80 };
81 static int part_ds_num = 4;
83 extern time_t curtime;
85 void disk_init (void)
86 {
87 #ifdef HAVE_LIBKSTAT
88 kstat_t *ksp_chain;
90 numdisk = 0;
92 if (kc == NULL)
93 return;
95 for (numdisk = 0, ksp_chain = kc->kc_chain;
96 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
97 ksp_chain = ksp_chain->ks_next)
98 {
99 if (strncmp (ksp_chain->ks_class, "disk", 4)
100 && strncmp (ksp_chain->ks_class, "partition", 9))
101 continue;
102 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
103 continue;
104 ksp[numdisk++] = ksp_chain;
105 }
106 #endif
108 return;
109 }
111 void disk_write (char *host, char *inst, char *val)
112 {
113 char file[512];
114 int status;
116 status = snprintf (file, 512, disk_filename_template, inst);
117 if (status < 1)
118 return;
119 else if (status >= 512)
120 return;
122 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
123 }
125 void partition_write (char *host, char *inst, char *val)
126 {
127 char file[512];
128 int status;
130 status = snprintf (file, 512, part_filename_template, inst);
131 if (status < 1)
132 return;
133 else if (status >= 512)
134 return;
136 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
137 }
139 #define BUFSIZE 512
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)
149 {
150 char buf[BUFSIZE];
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)
157 return;
159 plugin_submit (MODULE_NAME, disk_name, buf);
160 }
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)
167 {
168 char buf[BUFSIZE];
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)
174 return;
176 plugin_submit ("partition", part_name, buf);
177 }
178 #undef BUFSIZE
180 void disk_read (void)
181 {
182 #ifdef KERNEL_LINUX
183 FILE *fh;
184 char buffer[1024];
185 char disk_name[128];
187 char *fields[32];
188 int numfields;
189 int fieldshift = 0;
191 int major = 0;
192 int minor = 0;
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;
205 int is_disk = 0;
207 diskstats_t *ds, *pre_ds;
209 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
210 {
211 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
212 return;
214 /* Kernel is 2.4.* */
215 fieldshift = 1;
216 }
218 while (fgets (buffer, 1024, fh) != NULL)
219 {
220 numfields = strsplit (buffer, fields, 32);
222 if ((numfields != (14 + fieldshift)) && (numfields != 7))
223 continue;
225 major = atoll (fields[0]);
226 minor = atoll (fields[1]);
228 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
229 continue;
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)
234 break;
236 if (ds == NULL)
237 {
238 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
239 continue;
241 if ((ds->name = strdup (disk_name)) == NULL)
242 {
243 free (ds);
244 continue;
245 }
247 if (pre_ds == NULL)
248 disklist = ds;
249 else
250 pre_ds->next = ds;
251 }
253 is_disk = 0;
254 if (numfields == 7)
255 {
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]);
261 }
262 else if (numfields == (14 + fieldshift))
263 {
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))
271 {
272 is_disk = 1;
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]);
277 }
278 }
279 else
280 {
281 continue;
282 }
285 if (read_sectors >= ds->read_sectors)
286 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
287 else
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);
292 else
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))
302 continue;
304 if (is_disk)
305 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
306 write_count, write_merged, write_bytes, write_time);
307 else
308 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
309 }
311 fclose (fh);
312 /* #endif defined(KERNEL_LINUX) */
314 #elif defined(HAVE_LIBKSTAT)
315 static kstat_io_t kio;
316 int i;
318 if (kc == NULL)
319 return;
321 for (i = 0; i < numdisk; i++)
322 {
323 if (kstat_read (kc, ksp[i], &kio) == -1)
324 continue;
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);
334 }
335 #endif /* defined(HAVE_LIBKSTAT) */
336 }
338 void module_register (void)
339 {
340 plugin_register ("partition", NULL, NULL, partition_write);
341 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
342 }
344 #undef MODULE_NAME
345 #endif /* COLLECT_DISK */