dc0232d4e8af39ba132ce0ed26c9faba74a2caca
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 void disk_init (void)
84 {
85 #ifdef HAVE_LIBKSTAT
86 kstat_t *ksp_chain;
88 numdisk = 0;
90 if (kc == NULL)
91 return;
93 for (numdisk = 0, ksp_chain = kc->kc_chain;
94 (numdisk < MAX_NUMDISK) && (ksp_chain != NULL);
95 ksp_chain = ksp_chain->ks_next)
96 {
97 if (strncmp (ksp_chain->ks_class, "disk", 4)
98 && strncmp (ksp_chain->ks_class, "partition", 9))
99 continue;
100 if (ksp_chain->ks_type != KSTAT_TYPE_IO)
101 continue;
102 ksp[numdisk++] = ksp_chain;
103 }
104 #endif
106 return;
107 }
109 void disk_write (char *host, char *inst, char *val)
110 {
111 char file[512];
112 int status;
114 status = snprintf (file, 512, disk_filename_template, inst);
115 if (status < 1)
116 return;
117 else if (status >= 512)
118 return;
120 rrd_update_file (host, file, val, disk_ds_def, disk_ds_num);
121 }
123 void partition_write (char *host, char *inst, char *val)
124 {
125 char file[512];
126 int status;
128 status = snprintf (file, 512, part_filename_template, inst);
129 if (status < 1)
130 return;
131 else if (status >= 512)
132 return;
134 rrd_update_file (host, file, val, part_ds_def, part_ds_num);
135 }
137 #define BUFSIZE 512
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)
147 {
148 char buf[BUFSIZE];
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)
155 return;
157 plugin_submit (MODULE_NAME, disk_name, buf);
158 }
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)
165 {
166 char buf[BUFSIZE];
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)
172 return;
174 plugin_submit ("partition", part_name, buf);
175 }
176 #undef BUFSIZE
178 void disk_read (void)
179 {
180 #ifdef KERNEL_LINUX
181 FILE *fh;
182 char buffer[1024];
183 char disk_name[128];
185 char *fields[32];
186 int numfields;
187 int fieldshift = 0;
189 int major = 0;
190 int minor = 0;
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;
203 int is_disk = 0;
205 diskstats_t *ds, *pre_ds;
207 if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
208 {
209 if ((fh = fopen ("/proc/partitions", "r")) == NULL)
210 return;
212 /* Kernel is 2.4.* */
213 fieldshift = 1;
214 }
216 while (fgets (buffer, 1024, fh) != NULL)
217 {
218 numfields = strsplit (buffer, fields, 32);
220 if ((numfields != (14 + fieldshift)) && (numfields != 7))
221 continue;
223 major = atoll (fields[0]);
224 minor = atoll (fields[1]);
226 if (snprintf (disk_name, 128, "%i-%i", major, minor) < 1)
227 continue;
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)
232 break;
234 if (ds == NULL)
235 {
236 if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
237 continue;
239 if ((ds->name = strdup (disk_name)) == NULL)
240 {
241 free (ds);
242 continue;
243 }
245 if (pre_ds == NULL)
246 disklist = ds;
247 else
248 pre_ds->next = ds;
249 }
251 is_disk = 0;
252 if (numfields == 7)
253 {
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]);
259 }
260 else if (numfields == (14 + fieldshift))
261 {
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))
269 {
270 is_disk = 1;
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]);
275 }
276 }
277 else
278 {
279 continue;
280 }
283 if (read_sectors >= ds->read_sectors)
284 ds->read_bytes += 512 * (read_sectors - ds->read_sectors);
285 else
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);
290 else
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))
300 continue;
302 if (is_disk)
303 disk_submit (disk_name, read_count, read_merged, read_bytes, read_time,
304 write_count, write_merged, write_bytes, write_time);
305 else
306 partition_submit (disk_name, read_count, read_bytes, write_count, write_bytes);
307 }
309 fclose (fh);
310 /* #endif defined(KERNEL_LINUX) */
312 #elif defined(HAVE_LIBKSTAT)
313 static kstat_io_t kio;
314 int i;
316 if (kc == NULL)
317 return;
319 for (i = 0; i < numdisk; i++)
320 {
321 if (kstat_read (kc, ksp[i], &kio) == -1)
322 continue;
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);
332 }
333 #endif /* defined(HAVE_LIBKSTAT) */
334 }
336 void module_register (void)
337 {
338 plugin_register ("partition", NULL, NULL, partition_write);
339 plugin_register (MODULE_NAME, disk_init, disk_read, disk_write);
340 }
342 #undef MODULE_NAME
343 #endif /* COLLECT_DISK */