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