13 #include "appconfig.h"
16 #include "plugin_proc.h"
18 #define RRD_TYPE_DISK "disk"
23 int partition_id; // -1 = this is not a partition
27 struct disk *get_disk(unsigned long major, unsigned long minor) {
28 static char path_find_block_device_partition[FILENAME_MAX + 1] = "";
31 // search for it in our RAM list.
32 // this is sequential, but since we just walk through
33 // and the number of disks / partitions in a system
34 // should not be that many, it should be acceptable
35 for(d = disk_root; d ; d = d->next)
36 if(unlikely(d->major == major && d->minor == minor))
39 // if we found it, return it
43 if(unlikely(!path_find_block_device_partition[0])) {
44 char filename[FILENAME_MAX + 1];
45 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/dev/block/%lu:%lu/partition");
46 snprintf(path_find_block_device_partition, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get block device partition", filename));
50 // create a new disk structure
51 d = (struct disk *)malloc(sizeof(struct disk));
52 if(!d) fatal("Cannot allocate memory for struct disk in proc_diskstats.");
59 // append it to the list
64 for(last = disk_root; last->next ;last = last->next);
68 // find if it is a partition
69 // by reading /sys/dev/block/MAJOR:MINOR/partition
70 char buffer[FILENAME_MAX + 1];
71 snprintf(buffer, FILENAME_MAX, path_find_block_device_partition, major, minor);
73 int fd = open(buffer, O_RDONLY, 0666);
74 if(likely(fd != -1)) {
76 int bytes = read(fd, buffer, FILENAME_MAX);
80 d->partition_id = strtoul(buffer, NULL, 10);
82 // if the /partition file does not exist, it is a disk, not a partition
87 int do_proc_diskstats(int update_every, unsigned long long dt) {
88 static procfile *ff = NULL;
89 static char path_to_get_hw_sector_size[FILENAME_MAX + 1] = "";
90 static int enable_new_disks = -1;
91 static int do_io = -1, do_ops = -1, do_mops = -1, do_iotime = -1, do_qops = -1, do_util = -1, do_backlog = -1;
93 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);
95 if(do_io == -1) do_io = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "bandwidth for all disks", CONFIG_ONDEMAND_ONDEMAND);
96 if(do_ops == -1) do_ops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
97 if(do_mops == -1) do_mops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "merged operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
98 if(do_iotime == -1) do_iotime = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "i/o time for all disks", CONFIG_ONDEMAND_ONDEMAND);
99 if(do_qops == -1) do_qops = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "queued operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
100 if(do_util == -1) do_util = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "utilization percentage for all disks", CONFIG_ONDEMAND_ONDEMAND);
101 if(do_backlog == -1)do_backlog = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "backlog for all disks", CONFIG_ONDEMAND_ONDEMAND);
104 char filename[FILENAME_MAX + 1];
105 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/diskstats");
106 ff = procfile_open(config_get("plugin:proc:/proc/diskstats", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
110 if(!path_to_get_hw_sector_size[0]) {
111 char filename[FILENAME_MAX + 1];
112 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/block/%s/queue/hw_sector_size");
113 snprintf(path_to_get_hw_sector_size, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get h/w sector size", filename));
116 ff = procfile_readall(ff);
117 if(!ff) return 0; // we return 0, so that we will retry to open it next time
119 uint32_t lines = procfile_lines(ff), l;
122 for(l = 0; l < lines ;l++) {
124 unsigned long long major = 0, minor = 0,
125 reads = 0, mreads = 0, readsectors = 0, readms = 0,
126 writes = 0, mwrites = 0, writesectors = 0, writems = 0,
127 queued_ios = 0, busy_ms = 0, backlog_ms = 0;
129 unsigned long long last_reads = 0, last_readsectors = 0, last_readms = 0,
130 last_writes = 0, last_writesectors = 0, last_writems = 0,
133 words = procfile_linewords(ff, l);
134 if(words < 14) continue;
136 major = strtoull(procfile_lineword(ff, l, 0), NULL, 10);
137 minor = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
138 disk = procfile_lineword(ff, l, 2);
140 // # of reads completed # of writes completed
141 // This is the total number of reads or writes completed successfully.
142 reads = strtoull(procfile_lineword(ff, l, 3), NULL, 10); // rd_ios
143 writes = strtoull(procfile_lineword(ff, l, 7), NULL, 10); // wr_ios
145 // # of reads merged # of writes merged
146 // Reads and writes which are adjacent to each other may be merged for
147 // efficiency. Thus two 4K reads may become one 8K read before it is
148 // ultimately handed to the disk, and so it will be counted (and queued)
149 mreads = strtoull(procfile_lineword(ff, l, 4), NULL, 10); // rd_merges_or_rd_sec
150 mwrites = strtoull(procfile_lineword(ff, l, 8), NULL, 10); // wr_merges
152 // # of sectors read # of sectors written
153 // This is the total number of sectors read or written successfully.
154 readsectors = strtoull(procfile_lineword(ff, l, 5), NULL, 10); // rd_sec_or_wr_ios
155 writesectors = strtoull(procfile_lineword(ff, l, 9), NULL, 10); // wr_sec
157 // # of milliseconds spent reading # of milliseconds spent writing
158 // This is the total number of milliseconds spent by all reads or writes (as
159 // measured from __make_request() to end_that_request_last()).
160 readms = strtoull(procfile_lineword(ff, l, 6), NULL, 10); // rd_ticks_or_wr_sec
161 writems = strtoull(procfile_lineword(ff, l, 10), NULL, 10); // wr_ticks
163 // # of I/Os currently in progress
164 // The only field that should go to zero. Incremented as requests are
165 // given to appropriate struct request_queue and decremented as they finish.
166 queued_ios = strtoull(procfile_lineword(ff, l, 11), NULL, 10); // ios_pgr
168 // # of milliseconds spent doing I/Os
169 // This field increases so long as field queued_ios is nonzero.
170 busy_ms = strtoull(procfile_lineword(ff, l, 12), NULL, 10); // tot_ticks
172 // weighted # of milliseconds spent doing I/Os
173 // This field is incremented at each I/O start, I/O completion, I/O
174 // merge, or read of these stats by the number of I/Os in progress
175 // (field queued_ios) times the number of milliseconds spent doing I/O since the
176 // last update of this field. This can provide an easy measure of both
177 // I/O completion time and the backlog that may be accumulating.
178 backlog_ms = strtoull(procfile_lineword(ff, l, 13), NULL, 10); // rq_ticks
182 // remove slashes from disk names
184 for(s = disk; *s ;s++) if(*s == '/') *s = '_';
186 struct disk *d = get_disk(major, minor);
187 if(d->partition_id == -1)
188 def_enabled = enable_new_disks;
195 case 43: // network block
203 def_enabled = enable_new_disks;
225 if(minor % 8) def_enabled = 0; // partitions
226 else def_enabled = enable_new_disks;
229 case 8: // scsi disks
230 case 65: // scsi disks
231 case 66: // scsi disks
232 case 67: // scsi disks
233 case 68: // scsi disks
234 case 69: // scsi disks
235 case 70: // scsi disks
236 case 71: // scsi disks
237 case 72: // scsi disks
238 case 73: // scsi disks
239 case 74: // scsi disks
240 case 75: // scsi disks
241 case 76: // scsi disks
242 case 77: // scsi disks
243 case 78: // scsi disks
244 case 79: // scsi disks
253 case 101: // hyperdisk
254 case 102: // compressed
263 case 114: // bios raid
264 case 116: // ram board
278 case 259: // nvme0n1 issue #119
279 if(minor % 16) def_enabled = 0; // partitions
280 else def_enabled = enable_new_disks;
285 if(minor % 32) def_enabled = 0; // partitions
286 else def_enabled = enable_new_disks;
300 if(minor % 64) def_enabled = 0; // partitions
301 else def_enabled = enable_new_disks;
314 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;
316 // check which charts are enabled for this disk
318 char var_name[4096 + 1];
319 snprintf(var_name, 4096, "plugin:proc:/proc/diskstats:%s", disk);
320 def_enabled = config_get_boolean_ondemand(var_name, "enabled", def_enabled);
321 if(def_enabled == CONFIG_ONDEMAND_NO) continue;
322 if(def_enabled == CONFIG_ONDEMAND_ONDEMAND && !reads && !writes) continue;
325 ddo_io = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
326 ddo_ops = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
327 ddo_mops = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
328 ddo_iotime = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
329 ddo_qops = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
330 ddo_util = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
331 ddo_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
333 // by default, do not add charts that do not have values
334 if(ddo_io == CONFIG_ONDEMAND_ONDEMAND && !reads && !writes) ddo_io = 0;
335 if(ddo_mops == CONFIG_ONDEMAND_ONDEMAND && mreads == 0 && mwrites == 0) ddo_mops = 0;
336 if(ddo_iotime == CONFIG_ONDEMAND_ONDEMAND && readms == 0 && writems == 0) ddo_iotime = 0;
337 if(ddo_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms == 0) ddo_util = 0;
338 if(ddo_backlog == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_backlog = 0;
339 if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_qops = 0;
341 // for absolute values, we need to switch the setting to 'yes'
342 // to allow it refresh from now on
343 if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND) config_set(var_name, "queued operations", "yes");
348 // --------------------------------------------------------------------
350 int sector_size = 512;
352 st = rrdset_find_bytype(RRD_TYPE_DISK, disk);
354 char tf[FILENAME_MAX + 1], *t;
355 char ssfilename[FILENAME_MAX + 1];
357 strncpy(tf, disk, FILENAME_MAX);
358 tf[FILENAME_MAX] = '\0';
360 // replace all / with !
361 while((t = strchr(tf, '/'))) *t = '!';
363 snprintf(ssfilename, FILENAME_MAX, path_to_get_hw_sector_size, tf);
364 FILE *fpss = fopen(ssfilename, "r");
367 char *tmp = fgets(ssbuffer, 1024, fpss);
370 sector_size = atoi(tmp);
371 if(sector_size <= 0) {
372 error("Invalid sector size %d for device %s in %s. Assuming 512.", sector_size, disk, ssfilename);
376 else error("Cannot read data for sector size for device %s from %s. Assuming 512.", disk, ssfilename);
380 else error("Cannot read sector size for device %s from %s. Assuming 512.", disk, ssfilename);
382 st = rrdset_create(RRD_TYPE_DISK, disk, NULL, disk, "disk.io", "Disk I/O Bandwidth", "kilobytes/s", 2000, update_every, RRDSET_TYPE_AREA);
384 rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_INCREMENTAL);
385 rrddim_add(st, "writes", NULL, sector_size * -1, 1024, RRDDIM_INCREMENTAL);
387 else rrdset_next_usec(st, dt);
389 last_readsectors = rrddim_set(st, "reads", readsectors);
390 last_writesectors = rrddim_set(st, "writes", writesectors);
394 // --------------------------------------------------------------------
397 st = rrdset_find_bytype("disk_ops", disk);
399 st = rrdset_create("disk_ops", disk, NULL, disk, "disk.ops", "Disk Completed I/O Operations", "operations/s", 2001, update_every, RRDSET_TYPE_LINE);
402 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
403 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
405 else rrdset_next_usec(st, dt);
407 last_reads = rrddim_set(st, "reads", reads);
408 last_writes = rrddim_set(st, "writes", writes);
412 // --------------------------------------------------------------------
415 st = rrdset_find_bytype("disk_qops", disk);
417 st = rrdset_create("disk_qops", disk, NULL, disk, "disk.qops", "Disk Current I/O Operations", "operations", 2002, update_every, RRDSET_TYPE_LINE);
420 rrddim_add(st, "operations", NULL, 1, 1, RRDDIM_ABSOLUTE);
422 else rrdset_next_usec(st, dt);
424 rrddim_set(st, "operations", queued_ios);
428 // --------------------------------------------------------------------
431 st = rrdset_find_bytype("disk_backlog", disk);
433 st = rrdset_create("disk_backlog", disk, NULL, disk, "disk.backlog", "Disk Backlog", "backlog (ms)", 2003, update_every, RRDSET_TYPE_AREA);
436 rrddim_add(st, "backlog", NULL, 1, 10, RRDDIM_INCREMENTAL);
438 else rrdset_next_usec(st, dt);
440 rrddim_set(st, "backlog", backlog_ms);
444 // --------------------------------------------------------------------
447 st = rrdset_find_bytype("disk_util", disk);
449 st = rrdset_create("disk_util", disk, NULL, disk, "disk.util", "Disk Utilization Time", "% of time working", 2004, update_every, RRDSET_TYPE_AREA);
452 rrddim_add(st, "utilization", NULL, 1, 10, RRDDIM_INCREMENTAL);
454 else rrdset_next_usec(st, dt);
456 last_busy_ms = rrddim_set(st, "utilization", busy_ms);
460 // --------------------------------------------------------------------
463 st = rrdset_find_bytype("disk_mops", disk);
465 st = rrdset_create("disk_mops", disk, NULL, disk, "disk.mops", "Disk Merged Operations", "merged operations/s", 2021, update_every, RRDSET_TYPE_LINE);
468 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
469 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
471 else rrdset_next_usec(st, dt);
473 rrddim_set(st, "reads", mreads);
474 rrddim_set(st, "writes", mwrites);
478 // --------------------------------------------------------------------
481 st = rrdset_find_bytype("disk_iotime", disk);
483 st = rrdset_create("disk_iotime", disk, NULL, disk, "disk.iotime", "Disk Total I/O Time", "milliseconds/s", 2022, update_every, RRDSET_TYPE_LINE);
486 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
487 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
489 else rrdset_next_usec(st, dt);
491 last_readms = rrddim_set(st, "reads", readms);
492 last_writems = rrddim_set(st, "writes", writems);
496 // --------------------------------------------------------------------
497 // calculate differential charts
498 // only if this is not the first time we run
501 if(ddo_iotime && ddo_ops) {
502 st = rrdset_find_bytype("disk_await", disk);
504 st = rrdset_create("disk_await", disk, NULL, disk, "disk.await", "Average Completed I/O Operation Time", "ms per operation", 2005, update_every, RRDSET_TYPE_LINE);
507 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_ABSOLUTE);
508 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_ABSOLUTE);
510 else rrdset_next_usec(st, dt);
512 rrddim_set(st, "reads", (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
513 rrddim_set(st, "writes", (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
517 if(ddo_io && ddo_ops) {
518 st = rrdset_find_bytype("disk_avgsz", disk);
520 st = rrdset_create("disk_avgsz", disk, NULL, disk, "disk.avgsz", "Average Completed I/O Operation Bandwidth", "kilobytes per operation", 2006, update_every, RRDSET_TYPE_AREA);
523 rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_ABSOLUTE);
524 rrddim_add(st, "writes", NULL, -sector_size, 1024, RRDDIM_ABSOLUTE);
526 else rrdset_next_usec(st, dt);
528 rrddim_set(st, "reads", (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
529 rrddim_set(st, "writes", (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
533 if(ddo_util && ddo_ops) {
534 st = rrdset_find_bytype("disk_svctm", disk);
536 st = rrdset_create("disk_svctm", disk, NULL, disk, "disk.svctm", "Average Service Time", "ms per operation", 2007, update_every, RRDSET_TYPE_LINE);
539 rrddim_add(st, "svctm", NULL, 1, 1, RRDDIM_ABSOLUTE);
541 else rrdset_next_usec(st, dt);
543 rrddim_set(st, "svctm", ((reads - last_reads) + (writes - last_writes)) ? (busy_ms - last_busy_ms) / ((reads - last_reads) + (writes - last_writes)) : 0);