]> arthur.barton.de Git - netdata.git/blob - src/proc_diskstats.c
diskstats: Ensure collecting right space metrics
[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                 int is_container = 0;
93                 snprintfz(buffer, FILENAME_MAX, path_find_block_device, major, minor, "slaves/");
94                 DIR *dirp = opendir(buffer);    
95                 if (dirp != NULL) {
96                 struct dirent *dp;
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) {
100                                         continue;
101                                 }
102                                 d->type = DISK_TYPE_CONTAINER;
103                                 // Stop the loop after we found one file.
104                                 break;
105                         }
106                         if(closedir(dirp) == -1)
107                                 error("Unable to close dir %s", buffer);
108                 }
109         }
110
111         // ------------------------------------------------------------------------
112         // check if we can find its mount point
113
114         // mountinfo_find() can be called with NULL mountinfo_root
115         struct mountinfo *mi = mountinfo_find(mountinfo_root, d->major, d->minor);
116         if(unlikely(!mi)) {
117                 // mountinfo_free() can be called with NULL mountinfo_root
118                 mountinfo_free(mountinfo_root);
119
120                 // re-read mountinfo in case something changed
121                 mountinfo_root = mountinfo_read();
122
123                 // search again for this disk
124                 mi = mountinfo_find(mountinfo_root, d->major, d->minor);
125         }
126
127         if(mi)
128                 d->mount_point = strdup(mi->mount_point);
129                 // no need to check for NULL
130         else
131                 d->mount_point = NULL;
132
133         return d;
134 }
135
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_new_disks = -1;
140         static int do_io = -1, do_ops = -1, do_mops = -1, do_iotime = -1, do_qops = -1, do_util = -1, do_backlog = -1, do_space = -1;
141         static struct statvfs buff_statvfs;
142         static struct stat buff_stat;
143
144         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);
145
146         if(do_io == -1)         do_io           = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "bandwidth for all disks", CONFIG_ONDEMAND_ONDEMAND);
147         if(do_ops == -1)        do_ops          = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
148         if(do_mops == -1)       do_mops         = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "merged operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
149         if(do_iotime == -1)     do_iotime       = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "i/o time for all disks", CONFIG_ONDEMAND_ONDEMAND);
150         if(do_qops == -1)       do_qops         = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "queued operations for all disks", CONFIG_ONDEMAND_ONDEMAND);
151         if(do_util == -1)       do_util         = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "utilization percentage for all disks", CONFIG_ONDEMAND_ONDEMAND);
152         if(do_backlog == -1)do_backlog  = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "backlog for all disks", CONFIG_ONDEMAND_ONDEMAND);
153         if(do_space == -1)  do_space    = config_get_boolean_ondemand("plugin:proc:/proc/diskstats", "space usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
154
155         if(!ff) {
156                 char filename[FILENAME_MAX + 1];
157                 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/diskstats");
158                 ff = procfile_open(config_get("plugin:proc:/proc/diskstats", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
159         }
160         if(!ff) return 1;
161
162         if(!path_to_get_hw_sector_size[0]) {
163                 char filename[FILENAME_MAX + 1];
164                 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/sys/block/%s/queue/hw_sector_size");
165                 snprintfz(path_to_get_hw_sector_size, FILENAME_MAX, "%s", config_get("plugin:proc:/proc/diskstats", "path to get h/w sector size", filename));
166         }
167
168         ff = procfile_readall(ff);
169         if(!ff) return 0; // we return 0, so that we will retry to open it next time
170
171         uint32_t lines = procfile_lines(ff), l;
172         uint32_t words;
173
174         for(l = 0; l < lines ;l++) {
175                 char *disk;
176                 unsigned long long      major = 0, minor = 0,
177                                                         reads = 0,  mreads = 0,  readsectors = 0,  readms = 0,
178                                                         writes = 0, mwrites = 0, writesectors = 0, writems = 0,
179                                                         queued_ios = 0, busy_ms = 0, backlog_ms = 0,
180                                                         space_avail = 0, space_avail_root = 0, space_used = 0;
181
182                 unsigned long long      last_reads = 0,  last_readsectors = 0,  last_readms = 0,
183                                                         last_writes = 0, last_writesectors = 0, last_writems = 0,
184                                                         last_busy_ms = 0;
185
186                 words = procfile_linewords(ff, l);
187                 if(words < 14) continue;
188
189                 major                   = strtoull(procfile_lineword(ff, l, 0), NULL, 10);
190                 minor                   = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
191                 disk                    = procfile_lineword(ff, l, 2);
192
193                 // # of reads completed # of writes completed
194                 // This is the total number of reads or writes completed successfully.
195                 reads                   = strtoull(procfile_lineword(ff, l, 3), NULL, 10);      // rd_ios
196                 writes                  = strtoull(procfile_lineword(ff, l, 7), NULL, 10);      // wr_ios
197
198                 // # of reads merged # of writes merged
199                 // Reads and writes which are adjacent to each other may be merged for
200             // efficiency.  Thus two 4K reads may become one 8K read before it is
201             // ultimately handed to the disk, and so it will be counted (and queued)
202                 mreads                  = strtoull(procfile_lineword(ff, l, 4), NULL, 10);      // rd_merges_or_rd_sec
203                 mwrites                 = strtoull(procfile_lineword(ff, l, 8), NULL, 10);      // wr_merges
204
205                 // # of sectors read # of sectors written
206                 // This is the total number of sectors read or written successfully.
207                 readsectors     = strtoull(procfile_lineword(ff, l, 5), NULL, 10);      // rd_sec_or_wr_ios
208                 writesectors    = strtoull(procfile_lineword(ff, l, 9), NULL, 10);      // wr_sec
209
210                 // # of milliseconds spent reading # of milliseconds spent writing
211                 // This is the total number of milliseconds spent by all reads or writes (as
212                 // measured from __make_request() to end_that_request_last()).
213                 readms                  = strtoull(procfile_lineword(ff, l, 6), NULL, 10);      // rd_ticks_or_wr_sec
214                 writems                 = strtoull(procfile_lineword(ff, l, 10), NULL, 10);     // wr_ticks
215
216                 // # of I/Os currently in progress
217                 // The only field that should go to zero. Incremented as requests are
218                 // given to appropriate struct request_queue and decremented as they finish.
219                 queued_ios              = strtoull(procfile_lineword(ff, l, 11), NULL, 10);     // ios_pgr
220
221                 // # of milliseconds spent doing I/Os
222                 // This field increases so long as field queued_ios is nonzero.
223                 busy_ms                 = strtoull(procfile_lineword(ff, l, 12), NULL, 10);     // tot_ticks
224
225                 // weighted # of milliseconds spent doing I/Os
226                 // This field is incremented at each I/O start, I/O completion, I/O
227                 // merge, or read of these stats by the number of I/Os in progress
228                 // (field queued_ios) times the number of milliseconds spent doing I/O since the
229                 // last update of this field.  This can provide an easy measure of both
230                 // I/O completion time and the backlog that may be accumulating.
231                 backlog_ms              = strtoull(procfile_lineword(ff, l, 13), NULL, 10);     // rq_ticks
232
233                 int def_enabled = 0;
234
235                 // remove slashes from disk names
236                 char *s;
237                 for(s = disk; *s ;s++) if(*s == '/') *s = '_';
238
239                 struct disk *d = get_disk(major, minor);
240
241                 // Enable statistics for physical disks and mount points by default.
242                 if( (d->type == DISK_TYPE_PHYSICAL) || (d->mount_point != NULL) ) {
243                         def_enabled = enable_new_disks;
244                 }
245
246                 /*
247                 // Enable physical disks by default.
248                 // To fine out if it is a harddrive we use
249                 // Linux Assigned Names and Numbers Authority (http://www.lanana.org/)
250                 // We can't do that because proprietary disk drivers do not stick to
251                 // the standard. To detect re
252                 switch(major) {
253                         case 8: // SCSI disk devices (0-15)
254                                 if(!(minor % 16)) {
255                                         def_enabled = enable_new_disks;
256                                 }
257                                 break;
258                         case 21: // Acorn MFM hard drive interface
259                                 if(!(minor % 64)) {
260                                         def_enabled = enable_new_disks;
261                                 }
262                                 break;
263                         case 28: // ACSI disk (68k/Atari)
264                                 if(!(minor % 16)) {
265                                         def_enabled = enable_new_disks;
266                                 }
267                                 break;
268                         case 36: // MCA ESDI hard disk
269                                 if(!(minor % 64)) {
270                                         def_enabled = enable_new_disks;
271                                 }
272                                 break;
273                         case 48: // Mylex DAC960 PCI RAID controller; first controller
274                                 if(!(minor % 8)) {
275                                         def_enabled = enable_new_disks;
276                                 }
277                                 break;
278                         case 49: // Mylex DAC960 PCI RAID controller; second controller
279                                 if(!(minor % 8)) {
280                                         def_enabled = enable_new_disks;
281                                 }
282                                 break;
283                         case 50: // Mylex DAC960 PCI RAID controller; third controller
284                                 if(!(minor % 8)) {
285                                         def_enabled = enable_new_disks;
286                                 }
287                                 break;
288                         case 51: // Mylex DAC960 PCI RAID controller; fourth controller
289                                 if(!(minor % 8)) {
290                                         def_enabled = enable_new_disks;
291                                 }
292                                 break;
293                         case 52: // Mylex DAC960 PCI RAID controller; fifth controller
294                                 if(!(minor % 8)) {
295                                         def_enabled = enable_new_disks;
296                                 }
297                                 break;
298                         case 53: // Mylex DAC960 PCI RAID controller; sixth controller
299                                 if(!(minor % 8)) {
300                                         def_enabled = enable_new_disks;
301                                 }
302                                 break;
303                         case 54: // Mylex DAC960 PCI RAID controller; seventh controller
304                                 if(!(minor % 8)) {
305                                         def_enabled = enable_new_disks;
306                                 }
307                                 break;
308                         case 55: // Mylex DAC960 PCI RAID controller; eigth controller
309                                 if(!(minor % 8)) {
310                                         def_enabled = enable_new_disks;
311                                 }
312                                 break;
313                         case 65: // SCSI disk devices (16-31)
314                                 if(!(minor % 16)) {
315                                         def_enabled = enable_new_disks;
316                                 }
317                                 break;
318                         case 66: // SCSI disk devices (32-47)
319                                 if(!(minor % 16)) {
320                                         def_enabled = enable_new_disks;
321                                 }
322                                 break;
323                         case 67: // SCSI disk devices (48-63)
324                                 if(!(minor % 16)) {
325                                         def_enabled = enable_new_disks;
326                                 }
327                                 break;
328                         case 68: // SCSI disk devices (64-79)
329                                 if(!(minor % 16)) {
330                                         def_enabled = enable_new_disks;
331                                 }
332                                 break;
333                         case 69: // SCSI disk devices (80-95)
334                                 if(!(minor % 16)) {
335                                         def_enabled = enable_new_disks;
336                                 }
337                                 break;
338                         case 70: // SCSI disk devices (96-111)
339                                 if(!(minor % 16)) {
340                                         def_enabled = enable_new_disks;
341                                 }
342                                 break;
343                         case 71: // SCSI disk devices (112-127)
344                                 if(!(minor % 16)) {
345                                         def_enabled = enable_new_disks;
346                                 }
347                                 break;
348                         case 80: // I2O hard disk
349                                 if(!(minor % 16)) {
350                                         def_enabled = enable_new_disks;
351                                 }
352                                 break;
353                         case 81: // I2O hard disk
354                                 if(!(minor % 16)) {
355                                         def_enabled = enable_new_disks;
356                                 }
357                                 break;
358                         case 82: // I2O hard disk
359                                 if(!(minor % 16)) {
360                                         def_enabled = enable_new_disks;
361                                 }
362                                 break;
363                         case 83: // I2O hard disk
364                                 if(!(minor % 16)) {
365                                         def_enabled = enable_new_disks;
366                                 }
367                                 break;
368                         case 84: // I2O hard disk
369                                 if(!(minor % 16)) {
370                                         def_enabled = enable_new_disks;
371                                 }
372                                 break;
373                         case 85: // I2O hard disk
374                                 if(!(minor % 16)) {
375                                         def_enabled = enable_new_disks;
376                                 }
377                                 break;
378                         case 86: // I2O hard disk
379                                 if(!(minor % 16)) {
380                                         def_enabled = enable_new_disks;
381                                 }
382                                 break;
383                         case 87: // I2O hard disk
384                                 if(!(minor % 16)) {
385                                         def_enabled = enable_new_disks;
386                                 }
387                                 break;
388                         case 128: // SCSI disk devices (128-143)
389                                 if(!(minor % 16)) {
390                                         def_enabled = enable_new_disks;
391                                 }
392                                 break;
393                         case 129: // SCSI disk devices (144-159)
394                                 if(!(minor % 16)) {
395                                         def_enabled = enable_new_disks;
396                                 }
397                                 break;
398                         case 130: // SCSI disk devices (160-175)
399                                 if(!(minor % 16)) {
400                                         def_enabled = enable_new_disks;
401                                 }
402                                 break;
403                         case 131: // SCSI disk devices (176-191)
404                                 if(!(minor % 16)) {
405                                         def_enabled = enable_new_disks;
406                                 }
407                                 break;
408                         case 132: // SCSI disk devices (192-207)
409                                 if(!(minor % 16)) {
410                                         def_enabled = enable_new_disks;
411                                 }
412                                 break;
413                         case 133: // SCSI disk devices (208-223)
414                                 if(!(minor % 16)) {
415                                         def_enabled = enable_new_disks;
416                                 }
417                                 break;
418                         case 134: // SCSI disk devices (224-239)
419                                 if(!(minor % 16)) {
420                                         def_enabled = enable_new_disks;
421                                 }
422                                 break;
423                         case 135: // SCSI disk devices (240-255)
424                                 if(!(minor % 16)) {
425                                         def_enabled = enable_new_disks;
426                                 }
427                                 break;
428                         case 136: // Mylex DAC960 PCI RAID controller; nineth controller
429                                 if(!(minor % 8)) {
430                                         def_enabled = enable_new_disks;
431                                 }
432                                 break;
433                         case 137: // Mylex DAC960 PCI RAID controller; tenth controller
434                                 if(!(minor % 8)) {
435                                         def_enabled = enable_new_disks;
436                                 }
437                                 break;
438                         case 138: // Mylex DAC960 PCI RAID controller; eleventh controller
439                                 if(!(minor % 8)) {
440                                         def_enabled = enable_new_disks;
441                                 }
442                                 break;
443                         case 139: // Mylex DAC960 PCI RAID controller; twelfth controller
444                                 if(!(minor % 8)) {
445                                         def_enabled = enable_new_disks;
446                                 }
447                                 break;
448                         case 140: // Mylex DAC960 PCI RAID controller; thirteenth controller
449                                 if(!(minor % 8)) {
450                                         def_enabled = enable_new_disks;
451                                 }
452                                 break;
453                         case 141: // Mylex DAC960 PCI RAID controller; fourteenth controller
454                                 if(!(minor % 8)) {
455                                         def_enabled = enable_new_disks;
456                                 }
457                                 break;
458                         case 142: // Mylex DAC960 PCI RAID controller; fifteenth controller
459                                 if(!(minor % 8)) {
460                                         def_enabled = enable_new_disks;
461                                 }
462                                 break;
463                         case 143: // Mylex DAC960 PCI RAID controller; sixteenth controller
464                                 if(!(minor % 8)) {
465                                         def_enabled = enable_new_disks;
466                                 }
467                                 break;
468                         case 160: // Carmel 8-port SATA Disks on First Controller
469                                 if(!(minor % 32)) {
470                                         def_enabled = enable_new_disks;
471                                 }
472                                 break;
473                         case 161: // Carmel 8-port SATA Disks on Second Controller
474                                 if(!(minor % 32)) {
475                                         def_enabled = enable_new_disks;
476                                 }
477                                 break;
478                         default:
479                                 def_enabled = 0;
480                                 break;
481                 }
482                 */
483
484                 char *mount_point = d->mount_point;
485                 char *family = d->mount_point;
486                 if(!family) family = disk;
487
488                 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, ddo_space = do_space;
489
490                 // check which charts are enabled for this disk
491                 {
492                         char var_name[4096 + 1];
493                         snprintfz(var_name, 4096, "plugin:proc:/proc/diskstats:%s", disk);
494                         def_enabled = config_get_boolean_ondemand(var_name, "enabled", def_enabled);
495                         if(def_enabled == CONFIG_ONDEMAND_NO) continue;
496                         if(def_enabled == CONFIG_ONDEMAND_ONDEMAND && !reads && !writes) continue;
497
498
499                         ddo_io          = config_get_boolean_ondemand(var_name, "bandwidth", ddo_io);
500                         ddo_ops         = config_get_boolean_ondemand(var_name, "operations", ddo_ops);
501                         ddo_mops        = config_get_boolean_ondemand(var_name, "merged operations", ddo_mops);
502                         ddo_iotime      = config_get_boolean_ondemand(var_name, "i/o time", ddo_iotime);
503                         ddo_qops        = config_get_boolean_ondemand(var_name, "queued operations", ddo_qops);
504                         ddo_util        = config_get_boolean_ondemand(var_name, "utilization percentage", ddo_util);
505                         ddo_backlog = config_get_boolean_ondemand(var_name, "backlog", ddo_backlog);
506                         ddo_space   = config_get_boolean_ondemand(var_name, "space", ddo_space);
507
508                         // by default, do not add charts that do not have values
509                         if(ddo_io == CONFIG_ONDEMAND_ONDEMAND && !reads && !writes) ddo_io = 0;
510                         if(ddo_mops == CONFIG_ONDEMAND_ONDEMAND && mreads == 0 && mwrites == 0) ddo_mops = 0;
511                         if(ddo_iotime == CONFIG_ONDEMAND_ONDEMAND && readms == 0 && writems == 0) ddo_iotime = 0;
512                         if(ddo_util == CONFIG_ONDEMAND_ONDEMAND && busy_ms == 0) ddo_util = 0;
513                         if(ddo_backlog == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_backlog = 0;
514                         if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND && backlog_ms == 0) ddo_qops = 0;
515
516                         // for absolute values, we need to switch the setting to 'yes'
517                         // to allow it refresh from now on
518                         if(ddo_qops == CONFIG_ONDEMAND_ONDEMAND) config_set(var_name, "queued operations", "yes");
519                 }
520
521                 RRDSET *st;
522
523                 // --------------------------------------------------------------------
524
525                 int sector_size = 512;
526                 if(ddo_io) {
527                         st = rrdset_find_bytype(RRD_TYPE_DISK, disk);
528                         if(!st) {
529                                 char tf[FILENAME_MAX + 1], *t;
530                                 char ssfilename[FILENAME_MAX + 1];
531
532                                 strncpyz(tf, disk, FILENAME_MAX);
533
534                                 // replace all / with !
535                                 while((t = strchr(tf, '/'))) *t = '!';
536
537                                 snprintfz(ssfilename, FILENAME_MAX, path_to_get_hw_sector_size, tf);
538                                 FILE *fpss = fopen(ssfilename, "r");
539                                 if(fpss) {
540                                         char ssbuffer[1025];
541                                         char *tmp = fgets(ssbuffer, 1024, fpss);
542
543                                         if(tmp) {
544                                                 sector_size = atoi(tmp);
545                                                 if(sector_size <= 0) {
546                                                         error("Invalid sector size %d for device %s in %s. Assuming 512.", sector_size, disk, ssfilename);
547                                                         sector_size = 512;
548                                                 }
549                                         }
550                                         else error("Cannot read data for sector size for device %s from %s. Assuming 512.", disk, ssfilename);
551
552                                         fclose(fpss);
553                                 }
554                                 else error("Cannot read sector size for device %s from %s. Assuming 512.", disk, ssfilename);
555
556                                 st = rrdset_create(RRD_TYPE_DISK, disk, NULL, family, "disk.io", "Disk I/O Bandwidth", "kilobytes/s", 2000, update_every, RRDSET_TYPE_AREA);
557
558                                 rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_INCREMENTAL);
559                                 rrddim_add(st, "writes", NULL, sector_size * -1, 1024, RRDDIM_INCREMENTAL);
560                         }
561                         else rrdset_next_usec(st, dt);
562
563                         last_readsectors  = rrddim_set(st, "reads", readsectors);
564                         last_writesectors = rrddim_set(st, "writes", writesectors);
565                         rrdset_done(st);
566                 }
567
568                 // --------------------------------------------------------------------
569
570                 if(ddo_ops) {
571                         st = rrdset_find_bytype("disk_ops", disk);
572                         if(!st) {
573                                 st = rrdset_create("disk_ops", disk, NULL, family, "disk.ops", "Disk Completed I/O Operations", "operations/s", 2001, update_every, RRDSET_TYPE_LINE);
574                                 st->isdetail = 1;
575
576                                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
577                                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
578                         }
579                         else rrdset_next_usec(st, dt);
580
581                         last_reads  = rrddim_set(st, "reads", reads);
582                         last_writes = rrddim_set(st, "writes", writes);
583                         rrdset_done(st);
584                 }
585
586                 // --------------------------------------------------------------------
587
588                 if(ddo_qops) {
589                         st = rrdset_find_bytype("disk_qops", disk);
590                         if(!st) {
591                                 st = rrdset_create("disk_qops", disk, NULL, family, "disk.qops", "Disk Current I/O Operations", "operations", 2002, update_every, RRDSET_TYPE_LINE);
592                                 st->isdetail = 1;
593
594                                 rrddim_add(st, "operations", NULL, 1, 1, RRDDIM_ABSOLUTE);
595                         }
596                         else rrdset_next_usec(st, dt);
597
598                         rrddim_set(st, "operations", queued_ios);
599                         rrdset_done(st);
600                 }
601
602                 // --------------------------------------------------------------------
603
604                 if(ddo_backlog) {
605                         st = rrdset_find_bytype("disk_backlog", disk);
606                         if(!st) {
607                                 st = rrdset_create("disk_backlog", disk, NULL, family, "disk.backlog", "Disk Backlog", "backlog (ms)", 2003, update_every, RRDSET_TYPE_AREA);
608                                 st->isdetail = 1;
609
610                                 rrddim_add(st, "backlog", NULL, 1, 10, RRDDIM_INCREMENTAL);
611                         }
612                         else rrdset_next_usec(st, dt);
613
614                         rrddim_set(st, "backlog", backlog_ms);
615                         rrdset_done(st);
616                 }
617
618                 // --------------------------------------------------------------------
619
620                 if(ddo_util) {
621                         st = rrdset_find_bytype("disk_util", disk);
622                         if(!st) {
623                                 st = rrdset_create("disk_util", disk, NULL, family, "disk.util", "Disk Utilization Time", "% of time working", 2004, update_every, RRDSET_TYPE_AREA);
624                                 st->isdetail = 1;
625
626                                 rrddim_add(st, "utilization", NULL, 1, 10, RRDDIM_INCREMENTAL);
627                         }
628                         else rrdset_next_usec(st, dt);
629
630                         last_busy_ms = rrddim_set(st, "utilization", busy_ms);
631                         rrdset_done(st);
632                 }
633
634                 // --------------------------------------------------------------------
635
636                 if(ddo_mops) {
637                         st = rrdset_find_bytype("disk_mops", disk);
638                         if(!st) {
639                                 st = rrdset_create("disk_mops", disk, NULL, family, "disk.mops", "Disk Merged Operations", "merged operations/s", 2021, update_every, RRDSET_TYPE_LINE);
640                                 st->isdetail = 1;
641
642                                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
643                                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
644                         }
645                         else rrdset_next_usec(st, dt);
646
647                         rrddim_set(st, "reads", mreads);
648                         rrddim_set(st, "writes", mwrites);
649                         rrdset_done(st);
650                 }
651
652                 // --------------------------------------------------------------------
653
654                 if(ddo_iotime) {
655                         st = rrdset_find_bytype("disk_iotime", disk);
656                         if(!st) {
657                                 st = rrdset_create("disk_iotime", disk, NULL, family, "disk.iotime", "Disk Total I/O Time", "milliseconds/s", 2022, update_every, RRDSET_TYPE_LINE);
658                                 st->isdetail = 1;
659
660                                 rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_INCREMENTAL);
661                                 rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_INCREMENTAL);
662                         }
663                         else rrdset_next_usec(st, dt);
664
665                         last_readms  = rrddim_set(st, "reads", readms);
666                         last_writems = rrddim_set(st, "writes", writems);
667                         rrdset_done(st);
668                 }
669
670                 // --------------------------------------------------------------------
671
672                 if(ddo_space && mount_point) {
673                         // collect space metrics using statvfs
674                         if (statvfs(mount_point, &buff_statvfs) < 0) {
675                                 error("Failed checking disk space usage of %s", family);
676                         } else {
677                                 // verify we collected the metrics for the right disk.
678                                 // if not the mountpoint has changed.
679                                 if(stat(mount_point, &buff_stat) == -1) {
680                                 } else {
681                                         if(major(buff_stat.st_dev) == major && minor(buff_stat.st_dev) == minor)
682                                         {
683                                                 space_avail = buff_statvfs.f_bavail * buff_statvfs.f_bsize;
684                                                 space_avail_root = (buff_statvfs.f_bfree - buff_statvfs.f_bavail) * buff_statvfs.f_bsize;
685                                                 space_used = (buff_statvfs.f_blocks - buff_statvfs.f_bfree) * buff_statvfs.f_bsize;
686
687                                                 st = rrdset_find_bytype("disk_space", disk);
688                                                 if(!st) {
689                                                         st = rrdset_create("disk_space", disk, NULL, family, "disk.space", "Disk Space Usage", "GB", 2023, update_every, RRDSET_TYPE_STACKED);
690                                                         st->isdetail = 1;
691
692                                                         rrddim_add(st, "avail", NULL, 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
693                                                         rrddim_add(st, "reserved_for_root", "reserved for root", 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
694                                                         rrddim_add(st, "used" , NULL, 1, 1000*1000*1000, RRDDIM_ABSOLUTE);
695                                                 }
696                                                 else rrdset_next_usec(st, dt);
697
698                                                 rrddim_set(st, "avail", space_avail);
699                                                 rrddim_set(st, "reserved_for_root", space_avail_root);
700                                                 rrddim_set(st, "used", space_used);
701                                                 rrdset_done(st);
702                                         }
703                                 }
704                         }
705                 }
706
707                 // --------------------------------------------------------------------
708                 // calculate differential charts
709                 // only if this is not the first time we run
710
711                 if(dt) {
712                         if(ddo_iotime && ddo_ops) {
713                                 st = rrdset_find_bytype("disk_await", disk);
714                                 if(!st) {
715                                         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);
716                                         st->isdetail = 1;
717
718                                         rrddim_add(st, "reads", NULL, 1, 1, RRDDIM_ABSOLUTE);
719                                         rrddim_add(st, "writes", NULL, -1, 1, RRDDIM_ABSOLUTE);
720                                 }
721                                 else rrdset_next_usec(st, dt);
722
723                                 rrddim_set(st, "reads", (reads - last_reads) ? (readms - last_readms) / (reads - last_reads) : 0);
724                                 rrddim_set(st, "writes", (writes - last_writes) ? (writems - last_writems) / (writes - last_writes) : 0);
725                                 rrdset_done(st);
726                         }
727
728                         if(ddo_io && ddo_ops) {
729                                 st = rrdset_find_bytype("disk_avgsz", disk);
730                                 if(!st) {
731                                         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);
732                                         st->isdetail = 1;
733
734                                         rrddim_add(st, "reads", NULL, sector_size, 1024, RRDDIM_ABSOLUTE);
735                                         rrddim_add(st, "writes", NULL, -sector_size, 1024, RRDDIM_ABSOLUTE);
736                                 }
737                                 else rrdset_next_usec(st, dt);
738
739                                 rrddim_set(st, "reads", (reads - last_reads) ? (readsectors - last_readsectors) / (reads - last_reads) : 0);
740                                 rrddim_set(st, "writes", (writes - last_writes) ? (writesectors - last_writesectors) / (writes - last_writes) : 0);
741                                 rrdset_done(st);
742                         }
743
744                         if(ddo_util && ddo_ops) {
745                                 st = rrdset_find_bytype("disk_svctm", disk);
746                                 if(!st) {
747                                         st = rrdset_create("disk_svctm", disk, NULL, family, "disk.svctm", "Average Service Time", "ms per operation", 2007, update_every, RRDSET_TYPE_LINE);
748                                         st->isdetail = 1;
749
750                                         rrddim_add(st, "svctm", NULL, 1, 1, RRDDIM_ABSOLUTE);
751                                 }
752                                 else rrdset_next_usec(st, dt);
753
754                                 rrddim_set(st, "svctm", ((reads - last_reads) + (writes - last_writes)) ? (busy_ms - last_busy_ms) / ((reads - last_reads) + (writes - last_writes)) : 0);
755                                 rrdset_done(st);
756                         }
757                 }
758         }
759
760         return 0;
761 }