]> arthur.barton.de Git - netdata.git/blob - src/plugin_proc_diskspace.c
configurable time to check for new mount points
[netdata.git] / src / plugin_proc_diskspace.c
1 #include "common.h"
2
3 #define DELAULT_EXLUDED_PATHS "/proc/ /sys/ /var/run/user/ /run/user/"
4
5 static struct mountinfo *disk_mountinfo_root = NULL;
6 static int check_for_new_mountpoints_every = 15;
7
8 static inline void mountinfo_reload(int force) {
9     static time_t last_loaded = 0;
10     time_t now = now_realtime_sec();
11
12     if(force || now - last_loaded >= check_for_new_mountpoints_every) {
13         // mountinfo_free() can be called with NULL disk_mountinfo_root
14         mountinfo_free(disk_mountinfo_root);
15
16         // re-read mountinfo in case something changed
17         disk_mountinfo_root = mountinfo_read();
18
19         last_loaded = now;
20     }
21 }
22
23 // Data to be stored in DICTIONARY mount_points used by do_disk_space_stats().
24 // This DICTIONARY is used to lookup the settings of the mount point on each iteration.
25 struct mount_point_metadata {
26     int do_space;
27     int do_inodes;
28     int update_every;
29 };
30
31 static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
32     const char *family = mi->mount_point;
33     const char *disk = mi->persistent_id;
34
35     static DICTIONARY *mount_points = NULL;
36     static NETDATA_SIMPLE_PATTERN *excluded_mountpoints = NULL;
37     int do_space, do_inodes;
38
39     if(unlikely(!mount_points)) {
40         const char *s;
41
42         if(config_exists("plugin:proc:/proc/diskstats", "exclude space metrics on paths") && !config_exists("plugin:proc:diskspace", "exclude space metrics on paths")) {
43             // the config exists in the old section
44             s = config_get("plugin:proc:/proc/diskstats", "exclude space metrics on paths", DELAULT_EXLUDED_PATHS);
45
46             // set it to the new section
47             config_set("plugin:proc:diskspace", "exclude space metrics on paths", s);
48         }
49         else
50             s = config_get("plugin:proc:diskspace", "exclude space metrics on paths", DELAULT_EXLUDED_PATHS);
51
52         mount_points = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
53         excluded_mountpoints = netdata_simple_pattern_list_create(s, NETDATA_SIMPLE_PATTERN_MODE_PREFIX);
54     }
55
56     struct mount_point_metadata *m = dictionary_get(mount_points, mi->mount_point);
57     if(unlikely(!m)) {
58         char var_name[4096 + 1];
59         snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
60
61         int def_space = config_get_boolean_ondemand("plugin:proc:diskspace", "space usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
62         int def_inodes = config_get_boolean_ondemand("plugin:proc:diskspace", "inodes usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
63
64         if(unlikely(netdata_simple_pattern_list_matches(excluded_mountpoints, mi->mount_point))) {
65             def_space = CONFIG_ONDEMAND_NO;
66             def_inodes = CONFIG_ONDEMAND_NO;
67         }
68
69         do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
70         do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
71
72         struct mount_point_metadata mp = {
73                 .do_space = do_space,
74                 .do_inodes = do_inodes,
75                 .update_every = rrd_update_every
76         };
77
78         dictionary_set(mount_points, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
79     }
80     else {
81         do_space = m->do_space;
82         do_inodes = m->do_inodes;
83         update_every = m->update_every;
84     }
85
86     if(unlikely(do_space == CONFIG_ONDEMAND_NO && do_inodes == CONFIG_ONDEMAND_NO))
87         return;
88
89     struct statvfs buff_statvfs;
90     if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
91         error("Failed statvfs() for '%s' (disk '%s')", mi->mount_point, disk);
92         return;
93     }
94
95     // taken from get_fs_usage() found in coreutils
96     unsigned long bsize = (buff_statvfs.f_frsize) ? buff_statvfs.f_frsize : buff_statvfs.f_bsize;
97
98     fsblkcnt_t bavail         = buff_statvfs.f_bavail;
99     fsblkcnt_t btotal         = buff_statvfs.f_blocks;
100     fsblkcnt_t bavail_root    = buff_statvfs.f_bfree;
101     fsblkcnt_t breserved_root = bavail_root - bavail;
102     fsblkcnt_t bused;
103     if(likely(btotal >= bavail_root))
104         bused = btotal - bavail_root;
105     else
106         bused = bavail_root - btotal;
107
108 #ifdef NETDATA_INTERNAL_CHECKS
109     if(unlikely(btotal != bavail + breserved_root + bused))
110         error("Disk block statistics for '%s' (disk '%s') do not sum up: total = %llu, available = %llu, reserved = %llu, used = %llu", mi->mount_point, disk, (unsigned long long)btotal, (unsigned long long)bavail, (unsigned long long)breserved_root, (unsigned long long)bused);
111 #endif
112
113     // --------------------------------------------------------------------------
114
115     fsfilcnt_t favail         = buff_statvfs.f_favail;
116     fsfilcnt_t ftotal         = buff_statvfs.f_files;
117     fsfilcnt_t favail_root    = buff_statvfs.f_ffree;
118     fsfilcnt_t freserved_root = favail_root - favail;
119     fsfilcnt_t fused          = ftotal - favail_root;
120
121 #ifdef NETDATA_INTERNAL_CHECKS
122     if(unlikely(btotal != bavail + breserved_root + bused))
123         error("Disk inode statistics for '%s' (disk '%s') do not sum up: total = %llu, available = %llu, reserved = %llu, used = %llu", mi->mount_point, disk, (unsigned long long)ftotal, (unsigned long long)favail, (unsigned long long)freserved_root, (unsigned long long)fused);
124 #endif
125
126     // --------------------------------------------------------------------------
127
128     RRDSET *st;
129
130     if(do_space == CONFIG_ONDEMAND_YES || (do_space == CONFIG_ONDEMAND_ONDEMAND && (bavail || breserved_root || bused))) {
131         st = rrdset_find_bytype("disk_space", disk);
132         if(unlikely(!st)) {
133             char title[4096 + 1];
134             snprintfz(title, 4096, "Disk Space Usage for %s [%s]", family, mi->mount_source);
135             st = rrdset_create("disk_space", disk, NULL, family, "disk.space", title, "GB", 2023, update_every, RRDSET_TYPE_STACKED);
136
137             rrddim_add(st, "avail", NULL, bsize, 1024*1024*1024, RRDDIM_ABSOLUTE);
138             rrddim_add(st, "used" , NULL, bsize, 1024*1024*1024, RRDDIM_ABSOLUTE);
139             rrddim_add(st, "reserved_for_root", "reserved for root", bsize, 1024*1024*1024, RRDDIM_ABSOLUTE);
140         }
141         else rrdset_next(st);
142
143         rrddim_set(st, "avail", (collected_number)bavail);
144         rrddim_set(st, "used", (collected_number)bused);
145         rrddim_set(st, "reserved_for_root", (collected_number)breserved_root);
146         rrdset_done(st);
147     }
148
149     // --------------------------------------------------------------------------
150
151     if(do_inodes == CONFIG_ONDEMAND_YES || (do_inodes == CONFIG_ONDEMAND_ONDEMAND && (favail || freserved_root || fused))) {
152         st = rrdset_find_bytype("disk_inodes", disk);
153         if(unlikely(!st)) {
154             char title[4096 + 1];
155             snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", family, mi->mount_source);
156             st = rrdset_create("disk_inodes", disk, NULL, family, "disk.inodes", title, "Inodes", 2024, update_every, RRDSET_TYPE_STACKED);
157
158             rrddim_add(st, "avail", NULL, 1, 1, RRDDIM_ABSOLUTE);
159             rrddim_add(st, "used" , NULL, 1, 1, RRDDIM_ABSOLUTE);
160             rrddim_add(st, "reserved_for_root", "reserved for root", 1, 1, RRDDIM_ABSOLUTE);
161         }
162         else rrdset_next(st);
163
164         rrddim_set(st, "avail", (collected_number)favail);
165         rrddim_set(st, "used", (collected_number)fused);
166         rrddim_set(st, "reserved_for_root", (collected_number)freserved_root);
167         rrdset_done(st);
168     }
169 }
170
171 void *proc_diskspace_main(void *ptr) {
172     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
173
174     info("DISKSPACE thread created with task id %d", gettid());
175
176     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
177         error("Cannot set pthread cancel type to DEFERRED.");
178
179     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
180         error("Cannot set pthread cancel state to ENABLE.");
181
182     int update_every = (int)config_get_number("plugin:proc:diskspace", "update every", rrd_update_every);
183     if(update_every < rrd_update_every)
184         update_every = rrd_update_every;
185
186     check_for_new_mountpoints_every = (int)config_get_number("plugin:proc:diskspace", "check for new mount points every", check_for_new_mountpoints_every);
187     if(check_for_new_mountpoints_every < update_every)
188         check_for_new_mountpoints_every = update_every;
189
190     usec_t step = update_every * USEC_PER_SEC;
191     for(;;) {
192         usec_t now = now_monotonic_usec();
193         usec_t next = now - (now % step) + step;
194
195         while(now < next) {
196             sleep_usec(next - now);
197             now = now_monotonic_usec();
198         }
199
200         if(unlikely(netdata_exit)) break;
201
202         // --------------------------------------------------------------------------
203         // this is smart enough not to reload it every time
204
205         mountinfo_reload(0);
206
207         // --------------------------------------------------------------------------
208         // disk space metrics
209
210         struct mountinfo *mi;
211         for(mi = disk_mountinfo_root; mi; mi = mi->next) {
212
213             if(unlikely(mi->flags &
214                         (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND | MOUNTINFO_IS_SAME_DEV | MOUNTINFO_NO_STAT |
215                          MOUNTINFO_NO_SIZE | MOUNTINFO_READONLY)))
216                 continue;
217
218             do_disk_space_stats(mi, update_every);
219         }
220     }
221
222     info("DISKSPACE thread exiting");
223
224     static_thread->enabled = 0;
225     static_thread->thread = NULL;
226     pthread_exit(NULL);
227     return NULL;
228 }