13 #include "appconfig.h"
16 #include "plugin_proc.h"
18 #include "proc_self_mountinfo.h"
20 #define RRD_TYPE_DISK "disk"
25 int partition_id; // -1 = this is not a partition
30 struct disk *get_disk(unsigned long major, unsigned long minor) {
31 static char path_find_block_device_partition[FILENAME_MAX + 1] = "";
32 static struct mountinfo *mountinfo_root = NULL;
35 // search for it in our RAM list.
36 // this is sequential, but since we just walk through
37 // and the number of disks / partitions in a system
38 // should not be that many, it should be acceptable
39 for(d = disk_root; d ; d = d->next)
40 if(unlikely(d->major == major && d->minor == minor))
43 // if we found it, return it
47 if(unlikely(!path_find_block_device_partition[0])) {
48 char filename[FILENAME_MAX + 1];
49 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/dev/block/%lu:%lu/partition");
50 snprintf(path_find_block_device_partition, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get block device partition", filename));
54 // create a new disk structure
55 d = (struct disk *)malloc(sizeof(struct disk));
56 if(!d) fatal("Cannot allocate memory for struct disk in proc_diskstats.");
63 // append it to the list
68 for(last = disk_root; last->next ;last = last->next);
72 // find if it is a partition
73 // by reading /sys/dev/block/MAJOR:MINOR/partition
74 char buffer[FILENAME_MAX + 1];
75 snprintf(buffer, FILENAME_MAX, path_find_block_device_partition, major, minor);
77 int fd = open(buffer, O_RDONLY, 0666);
78 if(likely(fd != -1)) {
80 int bytes = read(fd, buffer, FILENAME_MAX);
84 d->partition_id = strtoul(buffer, NULL, 10);
86 // if the /partition file does not exist, it is a disk, not a partition
88 // ------------------------------------------------------------------------
89 // check if we can find its mount point
91 // mountinfo_find() can be called with NULL mountinfo_root
92 struct mountinfo *mi = mountinfo_find(mountinfo_root, d->major, d->minor);
94 // mountinfo_free() can be called with NULL mountinfo_root
95 mountinfo_free(mountinfo_root);
97 // re-read mountinfo in case something changed
98 mountinfo_root = mountinfo_read();
100 // search again for this disk
101 mi = mountinfo_find(mountinfo_root, d->major, d->minor);
105 d->family = strdup(mi->mount_point);
106 // no need to check for NULL
113 int do_proc_diskstats(int update_every, unsigned long long dt) {
114 static procfile *ff = NULL;
115 static char path_to_get_hw_sector_size[FILENAME_MAX + 1] = "";
116 static int enable_new_disks = -1;
117 static int do_io = -1, do_ops = -1, do_mops = -1, do_iotime = -1, do_qops = -1, do_util = -1, do_backlog = -1;
119 if(enable_new_disks == -1) enable_new_disks = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "enable new disks detected at runtime", CONFIG_ONDEMAND_ONDEMAND);
121 if(do_io == -1) do_io = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "bandwidth for all disks", CONFIG_ONDEMAND_ONDEMAND);
122 if(do_ops == -1) do_ops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
123 if(do_mops == -1) do_mops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "merged operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
124 if(do_iotime == -1) do_iotime = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "i/o time for all disks", CONFIG_ONDEMAND_ONDEMAND);
125 if(do_qops == -1) do_qops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "queued operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
126 if(do_util == -1) do_util = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "utilization percentage for all disks", CONFIG_ONDEMAND_ONDEMAND);
127 if(do_backlog == -1)do_backlog = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "backlog for all disks", CONFIG_ONDEMAND_ONDEMAND);
130 char filename[FILENAME_MAX + 1];
131 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/diskstats");
132 ff = procfile_open(config_get("plugin:proc:/proc/diskstats", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
136 if(!path_to_get_hw_sector_size[0]) {
137 char filename[FILENAME_MAX + 1];
138 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/block/%s/queue/hw_sector_size");
139 snprintf(path_to_get_hw_sector_size, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get h/w sector size", filename));
142 ff = procfile_readall(ff);
143 if(!ff) return 0; // we return 0, so that we will retry to open it next time
145 uint32_t lines = procfile_lines(ff), l;
148 for(l = 0; l < lines ;l++) {
150 unsigned long long major = 0, minor = 0,
151 reads = 0, mreads = 0, readsectors = 0, readms = 0,
152 writes = 0, mwrites = 0, writesectors = 0, writems = 0,
153 queued_ios = 0, busy_ms = 0, backlog_ms = 0;
155 unsigned long long last_reads = 0, last_readsectors = 0, last_readms = 0,
156 last_writes = 0, last_writesectors = 0, last_writems = 0,
159 words = procfile_linewords(ff, l);
160 if(words < 14) continue;
162 major = strtoull(procfile_lineword(ff, l, 0), NULL, 10);
163 minor = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
164 disk = procfile_lineword(ff, l, 2);
166 // # of reads completed # of writes completed
167 // This is the total number of reads or writes completed successfully.
168 reads = strtoull(procfile_lineword(ff, l, 3), NULL, 10); // rd_ios
169 writes = strtoull(procfile_lineword(ff, l, 7), NULL, 10); // wr_ios
171 // # of reads merged # of writes merged
172 // Reads and writes which are adjacent to each other may be merged for
173 // efficiency. Thus two 4K reads may become one 8K read before it is
174 // ultimately handed to the disk, and so it will be counted (and queued)
175 mreads = strtoull(procfile_lineword(ff, l, 4), NULL, 10); // rd_merges_or_rd_sec
176 mwrites = strtoull(procfile_lineword(ff, l, 8), NULL, 10); // wr_merges
178 // # of sectors read # of sectors written
179 // This is the total number of sectors read or written successfully.
180 readsectors = strtoull(procfile_lineword(ff, l, 5), NULL, 10); // rd_sec_or_wr_ios
181 writesectors = strtoull(procfile_lineword(ff, l, 9), NULL, 10); // wr_sec
183 // # of milliseconds spent reading # of milliseconds spent writing
184 // This is the total number of milliseconds spent by all reads or writes (as
185 // measured from __make_request() to end_that_request_last()).
186 readms = strtoull(procfile_lineword(ff, l, 6), NULL, 10); // rd_ticks_or_wr_sec
187 writems = strtoull(procfile_lineword(ff, l, 10), NULL, 10); // wr_ticks
189 // # of I/Os currently in progress
190 // The only field that should go to zero. Incremented as requests are
191 // given to appropriate struct request_queue and decremented as they finish.
192 queued_ios = strtoull(procfile_lineword(ff, l, 11), NULL, 10); // ios_pgr
194 // # of milliseconds spent doing I/Os
195 // This field increases so long as field queued_ios is nonzero.
196 busy_ms = strtoull(procfile_lineword(ff, l, 12), NULL, 10); // tot_ticks
198 // weighted # of milliseconds spent doing I/Os
199 // This field is incremented at each I/O start, I/O completion, I/O
200 // merge, or read of these stats by the number of I/Os in progress
201 // (field queued_ios) times the number of milliseconds spent doing I/O since the
202 // last update of this field. This can provide an easy measure of both
203 // I/O completion time and the backlog that may be accumulating.
204 backlog_ms = strtoull(procfile_lineword(ff, l, 13), NULL, 10); // rq_ticks
208 // remove slashes from disk names
210 for(s = disk; *s ;s++) if(*s == '/') *s = '_';
212 struct disk *d = get_disk(major, minor);
213 if(d->partition_id == -1)
214 def_enabled = enable_new_disks;
218 char *family = d->family;
219 if(!family) family = disk;
224 case 43: // network block
232 def_enabled = enable_new_disks;
254 if(minor % 8) def_enabled = 0; // partitions
255 else def_enabled = enable_new_disks;
258 case 8: // scsi disks
259 case 65: // scsi disks
260 case 66: // scsi disks
261 case 67: // scsi disks
262 case 68: // scsi disks
263 case 69: // scsi disks
264 case 70: // scsi disks
265 case 71: // scsi disks
266 case 72: // scsi disks
267 case 73: // scsi disks
268 case 74: // scsi disks
269 case 75: // scsi disks
270 case 76: // scsi disks
271 case 77: // scsi disks
272 case 78: // scsi disks
273 case 79: // scsi disks
282 case 101: // hyperdisk
283 case 102: // compressed
292 case 114: // bios raid
293 case 116: // ram board
307 case 259: // nvme0n1 issue #119
308 if(minor % 16) def_enabled = 0; // partitions
309 else def_enabled = enable_new_disks;
314 if(minor % 32) def_enabled = 0; // partitions
315 else def_enabled = enable_new_disks;
329 if(minor % 64) def_enabled = 0; // partitions
330 else def_enabled = enable_new_disks;
343 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;
345 // check which charts are enabled for this disk
347 char var_name[4096 + 1];
348 snprintf(var_name, 4096, "plugin:proc:/proc/diskstats:%s", disk);
349 def_enabled = config_get_boolean_ondemand(var_name, "enabled", def_enabled);
350 if(def_enabled == CONFIG_ONDEMAND_NO) continue;
351 if(def_enabled == CONFIG_ONDEMAND_ONDEMAND && !reads && !writes) continue;
354 ddo_io = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
355 ddo_ops = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
356 ddo_mops = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
357 ddo_iotime = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
358 ddo_qops = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
359 ddo_util = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
360 ddo_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
362 // by default, do not add charts that do not have values
363 if(ddo_io == CONFIG_ONDEMAND_ONDEMAND && !reads && !writes) ddo_io = 0;
364 if(ddo_mops == CONFIG_ONDEMAND_ONDEMAND && mreads == 0 && mwrites == 0) ddo_mops = 0;
365 if(ddo_iotime == CONFIG_ONDEMAND_ONDEMAND && readms == 0 && writems == 0) ddo_iotime = 0;
366 if(ddo_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms == 0) ddo_util = 0;
367 if(ddo_backlog == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_backlog = 0;
368 if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_qops = 0;
370 // for absolute values, we need to switch the setting to 'yes'
371 // to allow it refresh from now on
372 if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND) config_set(var_name, "queued operations", "yes");
377 // --------------------------------------------------------------------
379 int sector_size = 512;
381 st = rrdset_find_bytype(RRD_TYPE_DISK, disk);
383 char tf[FILENAME_MAX + 1], *t;
384 char ssfilename[FILENAME_MAX + 1];
386 strncpy(tf, disk, FILENAME_MAX);
387 tf[FILENAME_MAX] = '\0';
389 // replace all / with !
390 while((t = strchr(tf, '/'))) *t = '!';
392 snprintf(ssfilename, FILENAME_MAX, path_to_get_hw_sector_size, tf);
393 FILE *fpss = fopen(ssfilename, "r");
396 char *tmp = fgets(ssbuffer, 1024, fpss);
399 sector_size = atoi(tmp);
400 if(sector_size <= 0) {
401 error("Invalid sector size %d for device %s in %s. Assuming 512.", sector_size, disk, ssfilename);
405 else error("Cannot read data for sector size for device %s from %s. Assuming 512.", disk, ssfilename);
409 else error("Cannot read sector size for device %s from %s. Assuming 512.", disk, ssfilename);
411 st = rrdset_create(RRD_TYPE_DISK, disk, NULL, family, "disk.io", "Disk I/O Bandwidth", "kilobytes/s", 2000, update_every, RRDSET_TYPE_AREA);
413 rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_INCREMENTAL);
414 rrddim_add(st, "writes", NULL, sector_size * -1, 1024, RRDDIM_INCREMENTAL);
416 else rrdset_next_usec(st, dt);
418 last_readsectors = rrddim_set(st, "reads", readsectors);
419 last_writesectors = rrddim_set(st, "writes", writesectors);
423 // --------------------------------------------------------------------
426 st = rrdset_find_bytype("disk_ops", disk);
428 st = rrdset_create("disk_ops", disk, NULL, family, "disk.ops", "Disk Completed I/O Operations", "operations/s", 2001, update_every, RRDSET_TYPE_LINE);
431 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
432 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
434 else rrdset_next_usec(st, dt);
436 last_reads = rrddim_set(st, "reads", reads);
437 last_writes = rrddim_set(st, "writes", writes);
441 // --------------------------------------------------------------------
444 st = rrdset_find_bytype("disk_qops", disk);
446 st = rrdset_create("disk_qops", disk, NULL, family, "disk.qops", "Disk Current I/O Operations", "operations", 2002, update_every, RRDSET_TYPE_LINE);
449 rrddim_add(st, "operations", NULL, 1, 1, RRDDIM_ABSOLUTE);
451 else rrdset_next_usec(st, dt);
453 rrddim_set(st, "operations", queued_ios);
457 // --------------------------------------------------------------------
460 st = rrdset_find_bytype("disk_backlog", disk);
462 st = rrdset_create("disk_backlog", disk, NULL, family, "disk.backlog", "Disk Backlog", "backlog (ms)", 2003, update_every, RRDSET_TYPE_AREA);
465 rrddim_add(st, "backlog", NULL, 1, 10, RRDDIM_INCREMENTAL);
467 else rrdset_next_usec(st, dt);
469 rrddim_set(st, "backlog", backlog_ms);
473 // --------------------------------------------------------------------
476 st = rrdset_find_bytype("disk_util", disk);
478 st = rrdset_create("disk_util", disk, NULL, family, "disk.util", "Disk Utilization Time", "% of time working", 2004, update_every, RRDSET_TYPE_AREA);
481 rrddim_add(st, "utilization", NULL, 1, 10, RRDDIM_INCREMENTAL);
483 else rrdset_next_usec(st, dt);
485 last_busy_ms = rrddim_set(st, "utilization", busy_ms);
489 // --------------------------------------------------------------------
492 st = rrdset_find_bytype("disk_mops", disk);
494 st = rrdset_create("disk_mops", disk, NULL, family, "disk.mops", "Disk Merged Operations", "merged operations/s", 2021, update_every, RRDSET_TYPE_LINE);
497 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
498 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
500 else rrdset_next_usec(st, dt);
502 rrddim_set(st, "reads", mreads);
503 rrddim_set(st, "writes", mwrites);
507 // --------------------------------------------------------------------
510 st = rrdset_find_bytype("disk_iotime", disk);
512 st = rrdset_create("disk_iotime", disk, NULL, family, "disk.iotime", "Disk Total I/O Time", "milliseconds/s", 2022, update_every, RRDSET_TYPE_LINE);
515 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
516 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
518 else rrdset_next_usec(st, dt);
520 last_readms = rrddim_set(st, "reads", readms);
521 last_writems = rrddim_set(st, "writes", writems);
525 // --------------------------------------------------------------------
526 // calculate differential charts
527 // only if this is not the first time we run
530 if(ddo_iotime && ddo_ops) {
531 st = rrdset_find_bytype("disk_await", disk);
533 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);
536 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_ABSOLUTE);
537 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_ABSOLUTE);
539 else rrdset_next_usec(st, dt);
541 rrddim_set(st, "reads", (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
542 rrddim_set(st, "writes", (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
546 if(ddo_io && ddo_ops) {
547 st = rrdset_find_bytype("disk_avgsz", disk);
549 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);
552 rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_ABSOLUTE);
553 rrddim_add(st, "writes", NULL, -sector_size, 1024, RRDDIM_ABSOLUTE);
555 else rrdset_next_usec(st, dt);
557 rrddim_set(st, "reads", (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
558 rrddim_set(st, "writes", (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
562 if(ddo_util && ddo_ops) {
563 st = rrdset_find_bytype("disk_svctm", disk);
565 st = rrdset_create("disk_svctm", disk, NULL, family, "disk.svctm", "Average Service Time", "ms per operation", 2007, update_every, RRDSET_TYPE_LINE);
568 rrddim_add(st, "svctm", NULL, 1, 1, RRDDIM_ABSOLUTE);
570 else rrdset_next_usec(st, dt);
572 rrddim_set(st, "svctm", ((reads - last_reads) + (writes - last_writes)) ? (busy_ms - last_busy_ms) / ((reads - last_reads) + (writes - last_writes)) : 0);