10 #include <sys/statvfs.h>
11 #include <sys/types.h>
17 #include "appconfig.h"
20 #include "plugin_proc.h"
22 #include "proc_self_mountinfo.h"
24 #define RRD_TYPE_DISK "disk"
26 #define DISK_TYPE_PHYSICAL 1
27 #define DISK_TYPE_PARTITION 2
28 #define DISK_TYPE_CONTAINER 3
38 struct disk *get_disk(unsigned long major, unsigned long minor) {
39 static char path_find_block_device[FILENAME_MAX + 1] = "";
40 static struct mountinfo *mountinfo_root = NULL;
43 // search for it in our RAM list.
44 // this is sequential, but since we just walk through
45 // and the number of disks / partitions in a system
46 // should not be that many, it should be acceptable
47 for(d = disk_root; d ; d = d->next)
48 if(unlikely(d->major == major && d->minor == minor))
51 // if we found it, return it
55 if(unlikely(!path_find_block_device[0])) {
56 char dirname[FILENAME_MAX + 1];
57 snprintfz(dirname, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/dev/block/%lu:%lu/%s");
58 snprintfz(path_find_block_device, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get block device infos", dirname));
62 // create a new disk structure
63 d = (struct disk *)malloc(sizeof(struct disk));
64 if(!d) fatal("Cannot allocate memory for struct disk in proc_diskstats.");
68 d->type = DISK_TYPE_PHYSICAL; // Default type. Changed later if not correct.
71 // append it to the list
76 for(last = disk_root; last->next ;last = last->next);
80 // ------------------------------------------------------------------------
81 // find the type of the device
83 // find if it is a partition
84 // by checking if /sys/dev/block/MAJOR:MINOR/partition is readable.
85 char buffer[FILENAME_MAX + 1];
86 snprintfz(buffer, FILENAME_MAX, path_find_block_device, major, minor, "partition");
87 if(access(buffer, R_OK) == 0) {
88 d->type = DISK_TYPE_PARTITION;
90 // find if it is a container
91 // by checking if /sys/dev/block/MAJOR:MINOR/slaves has entries
93 snprintfz(buffer, FILENAME_MAX, path_find_block_device, major, minor, "slaves/");
94 DIR *dirp = opendir(buffer);
97 while(dp = readdir(dirp)) {
98 // . and .. are also files in empty folders.
99 if(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
102 d->type = DISK_TYPE_CONTAINER;
103 // Stop the loop after we found one file.
106 if(closedir(dirp) == -1)
107 error("Unable to close dir %s", buffer);
111 // ------------------------------------------------------------------------
112 // check if we can find its mount point
114 // mountinfo_find() can be called with NULL mountinfo_root
115 struct mountinfo *mi = mountinfo_find(mountinfo_root, d->major, d->minor);
117 // mountinfo_free() can be called with NULL mountinfo_root
118 mountinfo_free(mountinfo_root);
120 // re-read mountinfo in case something changed
121 mountinfo_root = mountinfo_read();
123 // search again for this disk
124 mi = mountinfo_find(mountinfo_root, d->major, d->minor);
128 d->mount_point = strdup(mi->mount_point);
129 // no need to check for NULL
131 d->mount_point = NULL;
136 int do_proc_diskstats(int update_every, unsigned long long dt) {
137 static procfile *ff = NULL;
138 static char path_to_get_hw_sector_size[FILENAME_MAX + 1] = "";
139 static int enable_autodetection;
140 static int enable_physical_disks, enable_virtual_disks, enable_partitions, enable_mountpoints, enable_space_metrics;
141 static int do_io, do_ops, do_mops, do_iotime, do_qops, do_util, do_backlog, do_space;
142 static struct statvfs buff_statvfs;
143 static struct stat buff_stat;
145 enable_autodetection = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "enable new disks detected at runtime", CONFIG_ONDEMAND_ONDEMAND);
147 enable_physical_disks = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for physical disks", CONFIG_ONDEMAND_ONDEMAND);
148 enable_virtual_disks = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for virtual disks", CONFIG_ONDEMAND_NO);
149 enable_partitions = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for partitions", CONFIG_ONDEMAND_NO);
150 enable_mountpoints = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for mounted filesystems", CONFIG_ONDEMAND_NO);
151 enable_space_metrics = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "space metrics for mounted filesystems", CONFIG_ONDEMAND_ONDEMAND);
153 do_io = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "bandwidth for all disks", CONFIG_ONDEMAND_ONDEMAND);
154 do_ops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
155 do_mops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "merged operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
156 do_iotime = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "i/o time for all disks", CONFIG_ONDEMAND_ONDEMAND);
157 do_qops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "queued operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
158 do_util = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "utilization percentage for all disks", CONFIG_ONDEMAND_ONDEMAND);
159 do_backlog = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "backlog for all disks", CONFIG_ONDEMAND_ONDEMAND);
160 do_space = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "space usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
163 char filename[FILENAME_MAX + 1];
164 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/diskstats");
165 ff = procfile_open(config_get("plugin:proc:/proc/diskstats", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
169 if(!path_to_get_hw_sector_size[0]) {
170 char filename[FILENAME_MAX + 1];
171 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/block/%s/queue/hw_sector_size");
172 snprintfz(path_to_get_hw_sector_size, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get h/w sector size", filename));
175 ff = procfile_readall(ff);
176 if(!ff) return 0; // we return 0, so that we will retry to open it next time
178 uint32_t lines = procfile_lines(ff), l;
181 for(l = 0; l < lines ;l++) {
182 // --------------------------------------------------------------------------
185 unsigned long long major = 0, minor = 0,
186 reads = 0, mreads = 0, readsectors = 0, readms = 0,
187 writes = 0, mwrites = 0, writesectors = 0, writems = 0,
188 queued_ios = 0, busy_ms = 0, backlog_ms = 0,
189 space_avail = 0, space_avail_root = 0, space_used = 0;
191 unsigned long long last_reads = 0, last_readsectors = 0, last_readms = 0,
192 last_writes = 0, last_writesectors = 0, last_writems = 0,
195 words = procfile_linewords(ff, l);
196 if(words < 14) continue;
198 major = strtoull(procfile_lineword(ff, l, 0), NULL, 10);
199 minor = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
200 disk = procfile_lineword(ff, l, 2);
202 // # of reads completed # of writes completed
203 // This is the total number of reads or writes completed successfully.
204 reads = strtoull(procfile_lineword(ff, l, 3), NULL, 10); // rd_ios
205 writes = strtoull(procfile_lineword(ff, l, 7), NULL, 10); // wr_ios
207 // # of reads merged # of writes merged
208 // Reads and writes which are adjacent to each other may be merged for
209 // efficiency. Thus two 4K reads may become one 8K read before it is
210 // ultimately handed to the disk, and so it will be counted (and queued)
211 mreads = strtoull(procfile_lineword(ff, l, 4), NULL, 10); // rd_merges_or_rd_sec
212 mwrites = strtoull(procfile_lineword(ff, l, 8), NULL, 10); // wr_merges
214 // # of sectors read # of sectors written
215 // This is the total number of sectors read or written successfully.
216 readsectors = strtoull(procfile_lineword(ff, l, 5), NULL, 10); // rd_sec_or_wr_ios
217 writesectors = strtoull(procfile_lineword(ff, l, 9), NULL, 10); // wr_sec
219 // # of milliseconds spent reading # of milliseconds spent writing
220 // This is the total number of milliseconds spent by all reads or writes (as
221 // measured from __make_request() to end_that_request_last()).
222 readms = strtoull(procfile_lineword(ff, l, 6), NULL, 10); // rd_ticks_or_wr_sec
223 writems = strtoull(procfile_lineword(ff, l, 10), NULL, 10); // wr_ticks
225 // # of I/Os currently in progress
226 // The only field that should go to zero. Incremented as requests are
227 // given to appropriate struct request_queue and decremented as they finish.
228 queued_ios = strtoull(procfile_lineword(ff, l, 11), NULL, 10); // ios_pgr
230 // # of milliseconds spent doing I/Os
231 // This field increases so long as field queued_ios is nonzero.
232 busy_ms = strtoull(procfile_lineword(ff, l, 12), NULL, 10); // tot_ticks
234 // weighted # of milliseconds spent doing I/Os
235 // This field is incremented at each I/O start, I/O completion, I/O
236 // merge, or read of these stats by the number of I/Os in progress
237 // (field queued_ios) times the number of milliseconds spent doing I/O since the
238 // last update of this field. This can provide an easy measure of both
239 // I/O completion time and the backlog that may be accumulating.
240 backlog_ms = strtoull(procfile_lineword(ff, l, 13), NULL, 10); // rq_ticks
242 // --------------------------------------------------------------------------
243 // remove slashes from disk names
245 for(s = disk; *s ;s++) if(*s == '/') *s = '_';
246 struct disk *d = get_disk(major, minor);
247 // Find mount point and family
248 char *mount_point = d->mount_point;
249 char *family = d->mount_point;
250 if(!family) family = disk;
252 // --------------------------------------------------------------------------
253 // Check if device is enabled by autodetection or config
255 char var_name[4096 + 1];
256 snprintfz(var_name, 4096, "plugin:proc:/proc/diskstats:%s", disk);
258 if(def_enable == -1) def_enable = config_get_boolean_ondemand(var_name, "enable", CONFIG_ONDEMAND_ONDEMAND);
259 if(def_enable == CONFIG_ONDEMAND_NO) continue;
261 int def_performance = CONFIG_ONDEMAND_NO, def_space = CONFIG_ONDEMAND_NO;
262 if(enable_autodetection) {
263 if(enable_physical_disks && (d->type == DISK_TYPE_PHYSICAL)) {
264 def_performance = enable_physical_disks;
265 } else if(enable_virtual_disks && (d->type == DISK_TYPE_CONTAINER)) {
266 def_performance = enable_virtual_disks;
267 } else if(enable_partitions && (d->type == DISK_TYPE_PARTITION)) {
268 def_performance = enable_partitions;
269 } else if(enable_mountpoints && (d->mount_point != NULL)) {
270 def_performance = enable_mountpoints;
273 if(enable_space_metrics && (d->mount_point != NULL)) {
274 def_space = enable_space_metrics;
280 // --------------------------------------------------------------------------
281 // Do performance metrics
282 def_performance = config_get_boolean_ondemand(var_name, "enable performance metrics", def_performance);
283 if( (def_performance == CONFIG_ONDEMAND_YES) || (def_performance == CONFIG_ONDEMAND_ONDEMAND && reads && writes) ) {
284 int ddo_io = do_io, ddo_ops = do_ops, ddo_mops = do_mops, ddo_iotime = do_iotime, ddo_qops = do_qops, ddo_util = do_util, ddo_backlog = do_backlog;
286 ddo_io = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
287 ddo_ops = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
288 ddo_mops = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
289 ddo_iotime = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
290 ddo_qops = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
291 ddo_util = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
292 ddo_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
294 // by default, do not add charts that do not have values
295 if(ddo_io == CONFIG_ONDEMAND_ONDEMAND && !reads && !writes) ddo_io = 0;
296 if(ddo_mops == CONFIG_ONDEMAND_ONDEMAND && mreads == 0 && mwrites == 0) ddo_mops = 0;
297 if(ddo_iotime == CONFIG_ONDEMAND_ONDEMAND && readms == 0 && writems == 0) ddo_iotime = 0;
298 if(ddo_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms == 0) ddo_util = 0;
299 if(ddo_backlog == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_backlog = 0;
300 if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_qops = 0;
302 // for absolute values, we need to switch the setting to 'yes'
303 // to allow it refresh from now on
304 if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND) config_set(var_name, "queued operations", "yes");
306 // --------------------------------------------------------------------
307 int sector_size = 512;
309 st = rrdset_find_bytype(RRD_TYPE_DISK, disk);
311 char tf[FILENAME_MAX + 1], *t;
312 char ssfilename[FILENAME_MAX + 1];
314 strncpyz(tf, disk, FILENAME_MAX);
316 // replace all / with !
317 while((t = strchr(tf, '/'))) *t = '!';
319 snprintfz(ssfilename, FILENAME_MAX, path_to_get_hw_sector_size, tf);
320 FILE *fpss = fopen(ssfilename, "r");
323 char *tmp = fgets(ssbuffer, 1024, fpss);
326 sector_size = atoi(tmp);
327 if(sector_size <= 0) {
328 error("Invalid sector size %d for device %s in %s. Assuming 512.", sector_size, disk, ssfilename);
332 else error("Cannot read data for sector size for device %s from %s. Assuming 512.", disk, ssfilename);
336 else error("Cannot read sector size for device %s from %s. Assuming 512.", disk, ssfilename);
338 st = rrdset_create(RRD_TYPE_DISK, disk, NULL, family, "disk.io", "Disk I/O Bandwidth", "kilobytes/s", 2000, update_every, RRDSET_TYPE_AREA);
340 rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_INCREMENTAL);
341 rrddim_add(st, "writes", NULL, sector_size * -1, 1024, RRDDIM_INCREMENTAL);
343 else rrdset_next_usec(st, dt);
345 last_readsectors = rrddim_set(st, "reads", readsectors);
346 last_writesectors = rrddim_set(st, "writes", writesectors);
350 // --------------------------------------------------------------------
352 st = rrdset_find_bytype("disk_ops", disk);
354 st = rrdset_create("disk_ops", disk, NULL, family, "disk.ops", "Disk Completed I/O Operations", "operations/s", 2001, update_every, RRDSET_TYPE_LINE);
357 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
358 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
360 else rrdset_next_usec(st, dt);
362 last_reads = rrddim_set(st, "reads", reads);
363 last_writes = rrddim_set(st, "writes", writes);
367 // --------------------------------------------------------------------
369 st = rrdset_find_bytype("disk_qops", disk);
371 st = rrdset_create("disk_qops", disk, NULL, family, "disk.qops", "Disk Current I/O Operations", "operations", 2002, update_every, RRDSET_TYPE_LINE);
374 rrddim_add(st, "operations", NULL, 1, 1, RRDDIM_ABSOLUTE);
376 else rrdset_next_usec(st, dt);
378 rrddim_set(st, "operations", queued_ios);
382 // --------------------------------------------------------------------
384 st = rrdset_find_bytype("disk_backlog", disk);
386 st = rrdset_create("disk_backlog", disk, NULL, family, "disk.backlog", "Disk Backlog", "backlog (ms)", 2003, update_every, RRDSET_TYPE_AREA);
389 rrddim_add(st, "backlog", NULL, 1, 10, RRDDIM_INCREMENTAL);
391 else rrdset_next_usec(st, dt);
393 rrddim_set(st, "backlog", backlog_ms);
397 // --------------------------------------------------------------------
399 st = rrdset_find_bytype("disk_util", disk);
401 st = rrdset_create("disk_util", disk, NULL, family, "disk.util", "Disk Utilization Time", "% of time working", 2004, update_every, RRDSET_TYPE_AREA);
404 rrddim_add(st, "utilization", NULL, 1, 10, RRDDIM_INCREMENTAL);
406 else rrdset_next_usec(st, dt);
408 last_busy_ms = rrddim_set(st, "utilization", busy_ms);
412 // --------------------------------------------------------------------
414 st = rrdset_find_bytype("disk_mops", disk);
416 st = rrdset_create("disk_mops", disk, NULL, family, "disk.mops", "Disk Merged Operations", "merged operations/s", 2021, update_every, RRDSET_TYPE_LINE);
419 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
420 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
422 else rrdset_next_usec(st, dt);
424 rrddim_set(st, "reads", mreads);
425 rrddim_set(st, "writes", mwrites);
429 // --------------------------------------------------------------------
431 st = rrdset_find_bytype("disk_iotime", disk);
433 st = rrdset_create("disk_iotime", disk, NULL, family, "disk.iotime", "Disk Total I/O Time", "milliseconds/s", 2022, update_every, RRDSET_TYPE_LINE);
436 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
437 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
439 else rrdset_next_usec(st, dt);
441 last_readms = rrddim_set(st, "reads", readms);
442 last_writems = rrddim_set(st, "writes", writems);
446 // --------------------------------------------------------------------
447 // calculate differential charts
448 // only if this is not the first time we run
450 if(ddo_iotime && ddo_ops) {
451 st = rrdset_find_bytype("disk_await", disk);
453 st = rrdset_create("disk_await", disk, NULL, family, "disk.await", "Average Completed I/O Operation Time", "ms per operation", 2005, update_every, RRDSET_TYPE_LINE);
456 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_ABSOLUTE);
457 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_ABSOLUTE);
459 else rrdset_next_usec(st, dt);
461 rrddim_set(st, "reads", (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
462 rrddim_set(st, "writes", (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
466 if(ddo_io && ddo_ops) {
467 st = rrdset_find_bytype("disk_avgsz", disk);
469 st = rrdset_create("disk_avgsz", disk, NULL, family, "disk.avgsz", "Average Completed I/O Operation Bandwidth", "kilobytes per operation", 2006, update_every, RRDSET_TYPE_AREA);
472 rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_ABSOLUTE);
473 rrddim_add(st, "writes", NULL, -sector_size, 1024, RRDDIM_ABSOLUTE);
475 else rrdset_next_usec(st, dt);
477 rrddim_set(st, "reads", (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
478 rrddim_set(st, "writes", (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
482 if(ddo_util && ddo_ops) {
483 st = rrdset_find_bytype("disk_svctm", disk);
485 st = rrdset_create("disk_svctm", disk, NULL, family, "disk.svctm", "Average Service Time", "ms per operation", 2007, update_every, RRDSET_TYPE_LINE);
488 rrddim_add(st, "svctm", NULL, 1, 1, RRDDIM_ABSOLUTE);
490 else rrdset_next_usec(st, dt);
492 rrddim_set(st, "svctm", ((reads - last_reads) + (writes - last_writes)) ? (busy_ms - last_busy_ms) / ((reads - last_reads) + (writes - last_writes)) : 0);
498 // --------------------------------------------------------------------------
500 def_space = config_get_boolean_ondemand(var_name, "enable space metrics", def_space);
501 if(def_space != CONFIG_ONDEMAND_NO) {
502 int ddo_space = do_space;
504 ddo_space = config_get_boolean_ondemand(var_name, "space", ddo_space);
505 if(ddo_space && mount_point) {
506 // collect space metrics using statvfs
507 if (statvfs(mount_point, &buff_statvfs) < 0) {
508 error("Failed checking disk space usage of %s", family);
510 // verify we collected the metrics for the right disk.
511 // if not the mountpoint has changed.
512 if(stat(mount_point, &buff_stat) == -1) {
514 if(major(buff_stat.st_dev) == major && minor(buff_stat.st_dev) == minor)
516 space_avail = buff_statvfs.f_bavail * buff_statvfs.f_bsize;
517 space_avail_root = (buff_statvfs.f_bfree - buff_statvfs.f_bavail) * buff_statvfs.f_bsize;
518 space_used = (buff_statvfs.f_blocks - buff_statvfs.f_bfree) * buff_statvfs.f_bsize;
520 st = rrdset_find_bytype("disk_space", disk);
522 st = rrdset_create("disk_space", disk, NULL, family, "disk.space", "Disk Space Usage", "GB", 2023, update_every, RRDSET_TYPE_STACKED);
525 rrddim_add(st, "avail", NULL, 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
526 rrddim_add(st, "reserved_for_root", "reserved for root", 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
527 rrddim_add(st, "used" , NULL, 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
529 else rrdset_next_usec(st, dt);
531 rrddim_set(st, "avail", space_avail);
532 rrddim_set(st, "reserved_for_root", space_avail_root);
533 rrddim_set(st, "used", space_used);