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