]> arthur.barton.de Git - netdata.git/blob - src/proc_diskstats.c
Merge remote-tracking branch 'upstream/master'
[netdata.git] / src / proc_diskstats.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <dirent.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/stat.h>
10 #include <sys/statvfs.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13
14
15 #include "common.h"
16 #include "log.h"
17 #include "appconfig.h"
18 #include "procfile.h"
19 #include "rrd.h"
20 #include "plugin_proc.h"
21
22 #include "proc_self_mountinfo.h"
23
24 #define RRD_TYPE_DISK "disk"
25
26 #define DISK_TYPE_PHYSICAL  1
27 #define DISK_TYPE_PARTITION 2
28 #define DISK_TYPE_CONTAINER 3
29
30 struct disk {
31         unsigned long major;
32         unsigned long minor;
33         int type;
34         char *mount_point;
35         struct disk *next;
36 } *disk_root = NULL;
37
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;
41         struct disk *d;
42
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))
49                         break;
50
51         // if we found it, return it
52         if(likely(d))
53                 return d;
54
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));
59         }
60
61         // not found
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.");
65
66         d->major = major;
67         d->minor = minor;
68         d->type = DISK_TYPE_PHYSICAL; // Default type. Changed later if not correct.
69         d->next = NULL;
70
71         // append it to the list
72         if(!disk_root)
73                 disk_root = d;
74         else {
75                 struct disk *last;
76                 for(last = disk_root; last->next ;last = last->next);
77                 last->next = d;
78         }
79
80         // ------------------------------------------------------------------------
81         // find the type of the device
82
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;
89         } else {
90                 // find if it is a container
91                 // by checking if /sys/dev/block/MAJOR:MINOR/slaves has entries
92                 snprintfz(buffer, FILENAME_MAX, path_find_block_device, major, minor, "slaves/");
93                 DIR *dirp = opendir(buffer);    
94                 if (dirp != NULL) {
95                 struct dirent *dp;
96                         while( (dp = readdir(dirp)) ) {
97                                 // . and .. are also files in empty folders.
98                                 if(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
99                                         continue;
100                                 }
101                                 d->type = DISK_TYPE_CONTAINER;
102                                 // Stop the loop after we found one file.
103                                 break;
104                         }
105                         if(closedir(dirp) == -1)
106                                 error("Unable to close dir %s", buffer);
107                 }
108         }
109
110         // ------------------------------------------------------------------------
111         // check if we can find its mount point
112
113         // mountinfo_find() can be called with NULL mountinfo_root
114         struct mountinfo *mi = mountinfo_find(mountinfo_root, d->major, d->minor);
115         if(unlikely(!mi)) {
116                 // mountinfo_free() can be called with NULL mountinfo_root
117                 mountinfo_free(mountinfo_root);
118
119                 // re-read mountinfo in case something changed
120                 mountinfo_root = mountinfo_read();
121
122                 // search again for this disk
123                 mi = mountinfo_find(mountinfo_root, d->major, d->minor);
124         }
125
126         if(mi)
127                 d->mount_point = strdup(mi->mount_point);
128                 // no need to check for NULL
129         else
130                 d->mount_point = NULL;
131
132         return d;
133 }
134
135 int do_proc_diskstats(int update_every, unsigned long long dt) {
136         static procfile *ff = NULL;
137         static char path_to_get_hw_sector_size[FILENAME_MAX + 1] = "";
138         static int enable_autodetection;
139         static int enable_physical_disks, enable_virtual_disks, enable_partitions, enable_mountpoints, enable_virtual_mountpoints, enable_space_metrics;
140         static int do_io, do_ops, do_mops, do_iotime, do_qops, do_util, do_backlog, do_space, do_inodes;
141         static struct statvfs buff_statvfs;
142         static struct stat buff_stat;
143
144         enable_autodetection = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "enable new disks detected at runtime", CONFIG_ONDEMAND_ONDEMAND);
145
146         enable_physical_disks = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for physical disks", CONFIG_ONDEMAND_ONDEMAND);
147         enable_virtual_disks = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for virtual disks", CONFIG_ONDEMAND_NO);
148         enable_partitions = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for partitions", CONFIG_ONDEMAND_NO);
149         enable_mountpoints = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for mounted filesystems", CONFIG_ONDEMAND_NO);
150         enable_virtual_mountpoints = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "performance metrics for mounted virtual disks", CONFIG_ONDEMAND_ONDEMAND);
151         enable_space_metrics = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "space metrics for mounted filesystems", CONFIG_ONDEMAND_ONDEMAND);
152
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);
161         do_inodes  = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "inodes usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
162
163         if(!ff) {
164                 char filename[FILENAME_MAX + 1];
165                 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/diskstats");
166                 ff = procfile_open(config_get("plugin:proc:/proc/diskstats", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
167         }
168         if(!ff) return 1;
169
170         if(!path_to_get_hw_sector_size[0]) {
171                 char filename[FILENAME_MAX + 1];
172                 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/block/%s/queue/hw_sector_size");
173                 snprintfz(path_to_get_hw_sector_size, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get h/w sector size", filename));
174         }
175
176         ff = procfile_readall(ff);
177         if(!ff) return 0; // we return 0, so that we will retry to open it next time
178
179         uint32_t lines = procfile_lines(ff), l;
180         uint32_t words;
181
182         for(l = 0; l < lines ;l++) {
183                 // --------------------------------------------------------------------------
184                 // Read parameters
185                 char *disk;
186                 unsigned long long      major = 0, minor = 0,
187                                                         reads = 0,  mreads = 0,  readsectors = 0,  readms = 0,
188                                                         writes = 0, mwrites = 0, writesectors = 0, writems = 0,
189                                                         queued_ios = 0, busy_ms = 0, backlog_ms = 0,
190                                                         space_avail = 0, space_avail_root = 0, space_used = 0,
191                                                         inodes_avail = 0, inodes_avail_root = 0, inodes_used = 0;
192
193                 unsigned long long      last_reads = 0,  last_readsectors = 0,  last_readms = 0,
194                                                         last_writes = 0, last_writesectors = 0, last_writems = 0,
195                                                         last_busy_ms = 0;
196
197                 words = procfile_linewords(ff, l);
198                 if(words < 14) continue;
199
200                 major                   = strtoull(procfile_lineword(ff, l, 0), NULL, 10);
201                 minor                   = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
202                 disk                    = procfile_lineword(ff, l, 2);
203
204                 // # of reads completed # of writes completed
205                 // This is the total number of reads or writes completed successfully.
206                 reads                   = strtoull(procfile_lineword(ff, l, 3), NULL, 10);      // rd_ios
207                 writes                  = strtoull(procfile_lineword(ff, l, 7), NULL, 10);      // wr_ios
208
209                 // # of reads merged # of writes merged
210                 // Reads and writes which are adjacent to each other may be merged for
211             // efficiency.  Thus two 4K reads may become one 8K read before it is
212             // ultimately handed to the disk, and so it will be counted (and queued)
213                 mreads                  = strtoull(procfile_lineword(ff, l, 4), NULL, 10);      // rd_merges_or_rd_sec
214                 mwrites                 = strtoull(procfile_lineword(ff, l, 8), NULL, 10);      // wr_merges
215
216                 // # of sectors read # of sectors written
217                 // This is the total number of sectors read or written successfully.
218                 readsectors     = strtoull(procfile_lineword(ff, l, 5), NULL, 10);      // rd_sec_or_wr_ios
219                 writesectors    = strtoull(procfile_lineword(ff, l, 9), NULL, 10);      // wr_sec
220
221                 // # of milliseconds spent reading # of milliseconds spent writing
222                 // This is the total number of milliseconds spent by all reads or writes (as
223                 // measured from __make_request() to end_that_request_last()).
224                 readms                  = strtoull(procfile_lineword(ff, l, 6), NULL, 10);      // rd_ticks_or_wr_sec
225                 writems                 = strtoull(procfile_lineword(ff, l, 10), NULL, 10);     // wr_ticks
226
227                 // # of I/Os currently in progress
228                 // The only field that should go to zero. Incremented as requests are
229                 // given to appropriate struct request_queue and decremented as they finish.
230                 queued_ios              = strtoull(procfile_lineword(ff, l, 11), NULL, 10);     // ios_pgr
231
232                 // # of milliseconds spent doing I/Os
233                 // This field increases so long as field queued_ios is nonzero.
234                 busy_ms                 = strtoull(procfile_lineword(ff, l, 12), NULL, 10);     // tot_ticks
235
236                 // weighted # of milliseconds spent doing I/Os
237                 // This field is incremented at each I/O start, I/O completion, I/O
238                 // merge, or read of these stats by the number of I/Os in progress
239                 // (field queued_ios) times the number of milliseconds spent doing I/O since the
240                 // last update of this field.  This can provide an easy measure of both
241                 // I/O completion time and the backlog that may be accumulating.
242                 backlog_ms              = strtoull(procfile_lineword(ff, l, 13), NULL, 10);     // rq_ticks
243
244                 // --------------------------------------------------------------------------
245                 // remove slashes from disk names
246                 char *s;
247                 for(s = disk; *s ;s++) if(*s == '/') *s = '_';
248                 struct disk *d = get_disk(major, minor);
249                 // Find mount point and family
250                 char *mount_point = d->mount_point;
251                 char *family = d->mount_point;
252                 if(!family) family = disk;
253
254                 // --------------------------------------------------------------------------
255                 // Check if device is enabled by autodetection or config
256                 int def_enable = -1;
257                 char var_name[4096 + 1];
258                 snprintfz(var_name, 4096, "plugin:proc:/proc/diskstats:%s", disk);
259
260                 if(def_enable == -1) def_enable = config_get_boolean_ondemand(var_name, "enable", CONFIG_ONDEMAND_ONDEMAND);
261                 if(def_enable == CONFIG_ONDEMAND_NO) continue;
262
263                 int def_performance = CONFIG_ONDEMAND_NO, def_space = CONFIG_ONDEMAND_NO;
264                 if(enable_autodetection)  {
265                         if(enable_physical_disks && (d->type == DISK_TYPE_PHYSICAL)) {
266                                 def_performance = enable_physical_disks;
267                         } else if(enable_virtual_disks && (d->type == DISK_TYPE_CONTAINER)) {
268                                 def_performance = enable_virtual_disks;
269                         } else if(enable_partitions && (d->type == DISK_TYPE_PARTITION)) {
270                                 def_performance = enable_partitions;
271                         } else if(enable_virtual_mountpoints && (d->type == DISK_TYPE_CONTAINER) && (d->mount_point != NULL)) {
272                                 def_performance = enable_virtual_mountpoints;
273                         } else if(enable_mountpoints && (d->mount_point != NULL)) {
274                                 def_performance = enable_mountpoints;
275                         }
276
277                         if(enable_space_metrics && (d->mount_point != NULL)) {
278                                 def_space = enable_space_metrics;
279                         }
280                 }
281
282                 RRDSET *st;
283
284                 // --------------------------------------------------------------------------
285                 // Do performance metrics
286                 def_performance = config_get_boolean_ondemand(var_name, "enable performance metrics", def_performance);
287                 if( (def_performance == CONFIG_ONDEMAND_YES) || (def_performance == CONFIG_ONDEMAND_ONDEMAND && reads && writes) ) {
288                         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;
289
290                         ddo_io          = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
291                         ddo_ops         = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
292                         ddo_mops        = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
293                         ddo_iotime      = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
294                         ddo_qops        = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
295                         ddo_util        = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
296                         ddo_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
297
298                         // by default, do not add charts that do not have values
299                         if(ddo_io == CONFIG_ONDEMAND_ONDEMAND && !reads && !writes) ddo_io = 0;
300                         if(ddo_mops == CONFIG_ONDEMAND_ONDEMAND && mreads == 0 && mwrites == 0) ddo_mops = 0;
301                         if(ddo_iotime == CONFIG_ONDEMAND_ONDEMAND && readms == 0 && writems == 0) ddo_iotime = 0;
302                         if(ddo_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms == 0) ddo_util = 0;
303                         if(ddo_backlog == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_backlog = 0;
304                         if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_qops = 0;
305
306                         // for absolute values, we need to switch the setting to 'yes'
307                         // to allow it refresh from now on
308                         if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND) config_set(var_name, "queued operations", "yes");
309
310                         // --------------------------------------------------------------------
311                         int sector_size = 512;
312                         if(ddo_io) {
313                                 st = rrdset_find_bytype(RRD_TYPE_DISK, disk);
314                                 if(!st) {
315                                         char tf[FILENAME_MAX + 1], *t;
316                                         char ssfilename[FILENAME_MAX + 1];
317
318                                         strncpyz(tf, disk, FILENAME_MAX);
319
320                                         // replace all / with !
321                                         while((t = strchr(tf, '/'))) *t = '!';
322
323                                         snprintfz(ssfilename, FILENAME_MAX, path_to_get_hw_sector_size, tf);
324                                         FILE *fpss = fopen(ssfilename, "r");
325                                         if(fpss) {
326                                                 char ssbuffer[1025];
327                                                 char *tmp = fgets(ssbuffer, 1024, fpss);
328
329                                                 if(tmp) {
330                                                         sector_size = atoi(tmp);
331                                                         if(sector_size <= 0) {
332                                                                 error("Invalid sector size %d for device %s in %s. Assuming 512.", sector_size, disk, ssfilename);
333                                                                 sector_size = 512;
334                                                         }
335                                                 }
336                                                 else error("Cannot read data for sector size for device %s from %s. Assuming 512.", disk, ssfilename);
337
338                                                 fclose(fpss);
339                                         }
340                                         else error("Cannot read sector size for device %s from %s. Assuming 512.", disk, ssfilename);
341
342                                         st = rrdset_create(RRD_TYPE_DISK, disk, NULL, family, "disk.io", "Disk I/O Bandwidth", "kilobytes/s", 2000, update_every, RRDSET_TYPE_AREA);
343
344                                         rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_INCREMENTAL);
345                                         rrddim_add(st, "writes", NULL, sector_size * -1, 1024, RRDDIM_INCREMENTAL);
346                                 }
347                                 else rrdset_next_usec(st, dt);
348
349                                 last_readsectors  = rrddim_set(st, "reads", readsectors);
350                                 last_writesectors = rrddim_set(st, "writes", writesectors);
351                                 rrdset_done(st);
352                         }
353
354                         // --------------------------------------------------------------------
355                         if(ddo_ops) {
356                                 st = rrdset_find_bytype("disk_ops", disk);
357                                 if(!st) {
358                                         st = rrdset_create("disk_ops", disk, NULL, family, "disk.ops", "Disk Completed I/O Operations", "operations/s", 2001, update_every, RRDSET_TYPE_LINE);
359                                         st->isdetail = 1;
360
361                                         rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
362                                         rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
363                                 }
364                                 else rrdset_next_usec(st, dt);
365
366                                 last_reads  = rrddim_set(st, "reads", reads);
367                                 last_writes = rrddim_set(st, "writes", writes);
368                                 rrdset_done(st);
369                         }
370
371                         // --------------------------------------------------------------------
372                         if(ddo_qops) {
373                                 st = rrdset_find_bytype("disk_qops", disk);
374                                 if(!st) {
375                                         st = rrdset_create("disk_qops", disk, NULL, family, "disk.qops", "Disk Current I/O Operations", "operations", 2002, update_every, RRDSET_TYPE_LINE);
376                                         st->isdetail = 1;
377
378                                         rrddim_add(st, "operations", NULL, 1, 1, RRDDIM_ABSOLUTE);
379                                 }
380                                 else rrdset_next_usec(st, dt);
381
382                                 rrddim_set(st, "operations", queued_ios);
383                                 rrdset_done(st);
384                         }
385
386                         // --------------------------------------------------------------------
387                         if(ddo_backlog) {
388                                 st = rrdset_find_bytype("disk_backlog", disk);
389                                 if(!st) {
390                                         st = rrdset_create("disk_backlog", disk, NULL, family, "disk.backlog", "Disk Backlog", "backlog (ms)", 2003, update_every, RRDSET_TYPE_AREA);
391                                         st->isdetail = 1;
392
393                                         rrddim_add(st, "backlog", NULL, 1, 10, RRDDIM_INCREMENTAL);
394                                 }
395                                 else rrdset_next_usec(st, dt);
396
397                                 rrddim_set(st, "backlog", backlog_ms);
398                                 rrdset_done(st);
399                         }
400
401                         // --------------------------------------------------------------------
402                         if(ddo_util) {
403                                 st = rrdset_find_bytype("disk_util", disk);
404                                 if(!st) {
405                                         st = rrdset_create("disk_util", disk, NULL, family, "disk.util", "Disk Utilization Time", "% of time working", 2004, update_every, RRDSET_TYPE_AREA);
406                                         st->isdetail = 1;
407
408                                         rrddim_add(st, "utilization", NULL, 1, 10, RRDDIM_INCREMENTAL);
409                                 }
410                                 else rrdset_next_usec(st, dt);
411
412                                 last_busy_ms = rrddim_set(st, "utilization", busy_ms);
413                                 rrdset_done(st);
414                         }
415
416                         // --------------------------------------------------------------------
417                         if(ddo_mops) {
418                                 st = rrdset_find_bytype("disk_mops", disk);
419                                 if(!st) {
420                                         st = rrdset_create("disk_mops", disk, NULL, family, "disk.mops", "Disk Merged Operations", "merged operations/s", 2021, update_every, RRDSET_TYPE_LINE);
421                                         st->isdetail = 1;
422
423                                         rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
424                                         rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
425                                 }
426                                 else rrdset_next_usec(st, dt);
427
428                                 rrddim_set(st, "reads", mreads);
429                                 rrddim_set(st, "writes", mwrites);
430                                 rrdset_done(st);
431                         }
432
433                         // --------------------------------------------------------------------
434                         if(ddo_iotime) {
435                                 st = rrdset_find_bytype("disk_iotime", disk);
436                                 if(!st) {
437                                         st = rrdset_create("disk_iotime", disk, NULL, family, "disk.iotime", "Disk Total I/O Time", "milliseconds/s", 2022, update_every, RRDSET_TYPE_LINE);
438                                         st->isdetail = 1;
439
440                                         rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
441                                         rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
442                                 }
443                                 else rrdset_next_usec(st, dt);
444
445                                 last_readms  = rrddim_set(st, "reads", readms);
446                                 last_writems = rrddim_set(st, "writes", writems);
447                                 rrdset_done(st);
448                         }
449
450                         // --------------------------------------------------------------------
451                         // calculate differential charts
452                         // only if this is not the first time we run
453                         if(dt) {
454                                 if(ddo_iotime && ddo_ops) {
455                                         st = rrdset_find_bytype("disk_await", disk);
456                                         if(!st) {
457                                                 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);
458                                                 st->isdetail = 1;
459
460                                                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_ABSOLUTE);
461                                                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_ABSOLUTE);
462                                         }
463                                         else rrdset_next_usec(st, dt);
464
465                                         rrddim_set(st, "reads", (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
466                                         rrddim_set(st, "writes", (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
467                                         rrdset_done(st);
468                                 }
469
470                                 if(ddo_io && ddo_ops) {
471                                         st = rrdset_find_bytype("disk_avgsz", disk);
472                                         if(!st) {
473                                                 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);
474                                                 st->isdetail = 1;
475
476                                                 rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_ABSOLUTE);
477                                                 rrddim_add(st, "writes", NULL, -sector_size, 1024, RRDDIM_ABSOLUTE);
478                                         }
479                                         else rrdset_next_usec(st, dt);
480
481                                         rrddim_set(st, "reads", (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
482                                         rrddim_set(st, "writes", (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
483                                         rrdset_done(st);
484                                 }
485
486                                 if(ddo_util && ddo_ops) {
487                                         st = rrdset_find_bytype("disk_svctm", disk);
488                                         if(!st) {
489                                                 st = rrdset_create("disk_svctm", disk, NULL, family, "disk.svctm", "Average Service Time", "ms per operation", 2007, update_every, RRDSET_TYPE_LINE);
490                                                 st->isdetail = 1;
491
492                                                 rrddim_add(st, "svctm", NULL, 1, 1, RRDDIM_ABSOLUTE);
493                                         }
494                                         else rrdset_next_usec(st, dt);
495
496                                         rrddim_set(st, "svctm", ((reads - last_reads) + (writes - last_writes)) ? (busy_ms - last_busy_ms) / ((reads - last_reads) + (writes - last_writes)) : 0);
497                                         rrdset_done(st);
498                                 }
499                         }
500                 }
501
502                 // --------------------------------------------------------------------------
503                 // Do space metrics
504                 def_space = config_get_boolean_ondemand(var_name, "enable space metrics", def_space);           
505                 if(def_space != CONFIG_ONDEMAND_NO) {
506                         int ddo_space = do_space, ddo_inodes = do_inodes;
507
508                         ddo_space = config_get_boolean_ondemand(var_name, "space", ddo_space);
509                         ddo_inodes = config_get_boolean_ondemand(var_name, "inodes", ddo_space);
510                         if(mount_point && (ddo_space || ddo_inodes) ) {
511                                 // collect space metrics using statvfs
512                                 if (statvfs(mount_point, &buff_statvfs) < 0) {
513                                         error("Failed checking disk space usage of %s", family);
514                                 } else {
515                                         // verify we collected the metrics for the right disk.
516                                         // if not the mountpoint has changed.
517                                         if(stat(mount_point, &buff_stat) == -1) {
518                                                 error("Failed to call stat for %s", family);
519                                         } else {
520                                                 if(major(buff_stat.st_dev) == major && minor(buff_stat.st_dev) == minor)
521                                                 {
522                                                         if(ddo_space) {
523                                                                 space_avail = buff_statvfs.f_bavail * buff_statvfs.f_bsize;
524                                                                 space_avail_root = (buff_statvfs.f_bfree - buff_statvfs.f_bavail) * buff_statvfs.f_bsize;
525                                                                 space_used = (buff_statvfs.f_blocks - buff_statvfs.f_bfree) * buff_statvfs.f_bsize;
526
527                                                                 st = rrdset_find_bytype("disk_space", disk);
528                                                                 if(!st) {
529                                                                         st = rrdset_create("disk_space", disk, NULL, family, "disk.space", "Disk Space Usage", "GB", 2023, update_every, RRDSET_TYPE_STACKED);
530                                                                         st->isdetail = 1;
531
532                                                                         rrddim_add(st, "avail", NULL, 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
533                                                                         rrddim_add(st, "reserved_for_root", "reserved for root", 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
534                                                                         rrddim_add(st, "used" , NULL, 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
535                                                                 }
536                                                                 else rrdset_next_usec(st, dt);
537
538                                                                 rrddim_set(st, "avail", space_avail);
539                                                                 rrddim_set(st, "reserved_for_root", space_avail_root);
540                                                                 rrddim_set(st, "used", space_used);
541                                                                 rrdset_done(st);
542                                                         }
543                                                         if(ddo_inodes) {
544                                                                 inodes_avail = buff_statvfs.f_favail;
545                                                                 inodes_avail_root = buff_statvfs.f_ffree - buff_statvfs.f_favail;
546                                                                 inodes_used = buff_statvfs.f_files - buff_statvfs.f_ffree;
547
548                                                                 st = rrdset_find_bytype("disk_inodes", disk);
549                                                                 if(!st) {
550                                                                         st = rrdset_create("disk_inodes", disk, NULL, family, "disk.inodes", "Disk Inodes Usage", "Inodes", 2024, update_every, RRDSET_TYPE_STACKED);
551                                                                         st->isdetail = 1;
552
553                                                                         rrddim_add(st, "avail", NULL, 1, 1, RRDDIM_ABSOLUTE);
554                                                                         rrddim_add(st, "reserved_for_root", "reserved for root", 1, 1, RRDDIM_ABSOLUTE);
555                                                                         rrddim_add(st, "used" , NULL, 1, 1, RRDDIM_ABSOLUTE);
556                                                                 }
557                                                                 else rrdset_next_usec(st, dt);
558
559                                                                 rrddim_set(st, "avail", inodes_avail);
560                                                                 rrddim_set(st, "reserved_for_root", inodes_avail_root);
561                                                                 rrddim_set(st, "used", inodes_used);
562                                                                 rrdset_done(st);
563                                                         }
564                                                 }
565                                         }
566                                 }
567                         }
568                 }
569         }
570         return 0;
571 }