]> arthur.barton.de Git - netdata.git/blob - src/plugin_proc_diskspace.c
multi-host rrdset and rrdfamily operations
[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(1);
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
29     size_t collected; // the number of times this has been collected
30
31     RRDSET *st_space;
32     RRDDIM *rd_space_used;
33     RRDDIM *rd_space_avail;
34     RRDDIM *rd_space_reserved;
35
36     RRDSET *st_inodes;
37     RRDDIM *rd_inodes_used;
38     RRDDIM *rd_inodes_avail;
39     RRDDIM *rd_inodes_reserved;
40 };
41
42 static inline void do_disk_space_stats(struct mountinfo *mi, int update_every) {
43     const char *family = mi->mount_point;
44     const char *disk = mi->persistent_id;
45
46     static DICTIONARY *mount_points = NULL;
47     static SIMPLE_PATTERN *excluded_mountpoints = NULL;
48     int do_space, do_inodes;
49
50     if(unlikely(!mount_points)) {
51         const char *s;
52         SIMPLE_PREFIX_MODE mode = SIMPLE_PATTERN_EXACT;
53
54         if(config_exists("plugin:proc:/proc/diskstats", "exclude space metrics on paths") && !config_exists("plugin:proc:diskspace", "exclude space metrics on paths")) {
55             // the config exists in the old section
56             s = config_get("plugin:proc:/proc/diskstats", "exclude space metrics on paths", DELAULT_EXLUDED_PATHS);
57             mode = SIMPLE_PATTERN_PREFIX;
58         }
59         else
60             s = config_get("plugin:proc:diskspace", "exclude space metrics on paths", DELAULT_EXLUDED_PATHS);
61
62         mount_points = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED);
63         excluded_mountpoints = simple_pattern_create(s, mode);
64     }
65
66     struct mount_point_metadata *m = dictionary_get(mount_points, mi->mount_point);
67     if(unlikely(!m)) {
68         char var_name[4096 + 1];
69         snprintfz(var_name, 4096, "plugin:proc:diskspace:%s", mi->mount_point);
70
71         int def_space = config_get_boolean_ondemand("plugin:proc:diskspace", "space usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
72         int def_inodes = config_get_boolean_ondemand("plugin:proc:diskspace", "inodes usage for all disks", CONFIG_ONDEMAND_ONDEMAND);
73
74         if(unlikely(simple_pattern_matches(excluded_mountpoints, mi->mount_point))) {
75             def_space = CONFIG_ONDEMAND_NO;
76             def_inodes = CONFIG_ONDEMAND_NO;
77         }
78
79         do_space = config_get_boolean_ondemand(var_name, "space usage", def_space);
80         do_inodes = config_get_boolean_ondemand(var_name, "inodes usage", def_inodes);
81
82         struct mount_point_metadata mp = {
83                 .do_space = do_space,
84                 .do_inodes = do_inodes,
85
86                 .collected = 0,
87
88                 .st_space = NULL,
89                 .rd_space_avail = NULL,
90                 .rd_space_used = NULL,
91                 .rd_space_reserved = NULL,
92
93                 .st_inodes = NULL,
94                 .rd_inodes_avail = NULL,
95                 .rd_inodes_used = NULL,
96                 .rd_inodes_reserved = NULL
97         };
98
99         m = dictionary_set(mount_points, mi->mount_point, &mp, sizeof(struct mount_point_metadata));
100     }
101     else {
102         do_space = m->do_space;
103         do_inodes = m->do_inodes;
104     }
105
106     if(unlikely(do_space == CONFIG_ONDEMAND_NO && do_inodes == CONFIG_ONDEMAND_NO))
107         return;
108
109     if(unlikely(mi->flags & MOUNTINFO_READONLY && !m->collected))
110         return;
111
112     struct statvfs buff_statvfs;
113     if (statvfs(mi->mount_point, &buff_statvfs) < 0) {
114         error("Failed statvfs() for '%s' (disk '%s')", mi->mount_point, disk);
115         return;
116     }
117
118     // logic found at get_fs_usage() in coreutils
119     unsigned long bsize = (buff_statvfs.f_frsize) ? buff_statvfs.f_frsize : buff_statvfs.f_bsize;
120
121     fsblkcnt_t bavail         = buff_statvfs.f_bavail;
122     fsblkcnt_t btotal         = buff_statvfs.f_blocks;
123     fsblkcnt_t bavail_root    = buff_statvfs.f_bfree;
124     fsblkcnt_t breserved_root = bavail_root - bavail;
125     fsblkcnt_t bused;
126     if(likely(btotal >= bavail_root))
127         bused = btotal - bavail_root;
128     else
129         bused = bavail_root - btotal;
130
131 #ifdef NETDATA_INTERNAL_CHECKS
132     if(unlikely(btotal != bavail + breserved_root + bused))
133         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);
134 #endif
135
136     // --------------------------------------------------------------------------
137
138     fsfilcnt_t favail         = buff_statvfs.f_favail;
139     fsfilcnt_t ftotal         = buff_statvfs.f_files;
140     fsfilcnt_t favail_root    = buff_statvfs.f_ffree;
141     fsfilcnt_t freserved_root = favail_root - favail;
142     fsfilcnt_t fused          = ftotal - favail_root;
143
144 #ifdef NETDATA_INTERNAL_CHECKS
145     if(unlikely(btotal != bavail + breserved_root + bused))
146         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);
147 #endif
148
149     // --------------------------------------------------------------------------
150
151     int rendered = 0;
152
153     if(do_space == CONFIG_ONDEMAND_YES || (do_space == CONFIG_ONDEMAND_ONDEMAND && (bavail || breserved_root || bused))) {
154         if(unlikely(!m->st_space)) {
155             m->do_space = CONFIG_ONDEMAND_YES;
156             m->st_space = rrdset_find_bytype_localhost("disk_space", disk);
157             if(unlikely(!m->st_space)) {
158                 char title[4096 + 1];
159                 snprintfz(title, 4096, "Disk Space Usage for %s [%s]", family, mi->mount_source);
160                 m->st_space = rrdset_create("disk_space", disk, NULL, family, "disk.space", title, "GB", 2023, update_every, RRDSET_TYPE_STACKED);
161             }
162
163             m->rd_space_avail    = rrddim_add(m->st_space, "avail", NULL, bsize, 1024 * 1024 * 1024, RRDDIM_ALGORITHM_ABSOLUTE);
164             m->rd_space_used     = rrddim_add(m->st_space, "used", NULL, bsize, 1024 * 1024 * 1024, RRDDIM_ALGORITHM_ABSOLUTE);
165             m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", bsize, 1024 * 1024 * 1024, RRDDIM_ALGORITHM_ABSOLUTE);
166         }
167         else
168             rrdset_next(m->st_space);
169
170         rrddim_set_by_pointer(m->st_space, m->rd_space_avail,    (collected_number)bavail);
171         rrddim_set_by_pointer(m->st_space, m->rd_space_used,     (collected_number)bused);
172         rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root);
173         rrdset_done(m->st_space);
174
175         rendered++;
176     }
177
178     // --------------------------------------------------------------------------
179
180     if(do_inodes == CONFIG_ONDEMAND_YES || (do_inodes == CONFIG_ONDEMAND_ONDEMAND && (favail || freserved_root || fused))) {
181         if(unlikely(!m->st_inodes)) {
182             m->do_inodes = CONFIG_ONDEMAND_YES;
183             m->st_inodes = rrdset_find_bytype_localhost("disk_inodes", disk);
184             if(unlikely(!m->st_inodes)) {
185                 char title[4096 + 1];
186                 snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", family, mi->mount_source);
187                 m->st_inodes = rrdset_create("disk_inodes", disk, NULL, family, "disk.inodes", title, "Inodes", 2024, update_every, RRDSET_TYPE_STACKED);
188             }
189
190             m->rd_inodes_avail    = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
191             m->rd_inodes_used     = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
192             m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
193         }
194         else
195             rrdset_next(m->st_inodes);
196
197         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail,    (collected_number)favail);
198         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used,     (collected_number)fused);
199         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root);
200         rrdset_done(m->st_inodes);
201
202         rendered++;
203     }
204
205     // --------------------------------------------------------------------------
206
207     if(likely(rendered))
208         m->collected++;
209 }
210
211 void *proc_diskspace_main(void *ptr) {
212     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
213
214     info("DISKSPACE thread created with task id %d", gettid());
215
216     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
217         error("Cannot set pthread cancel type to DEFERRED.");
218
219     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
220         error("Cannot set pthread cancel state to ENABLE.");
221
222     int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
223
224     int update_every = (int)config_get_number("plugin:proc:diskspace", "update every", rrd_update_every);
225     if(update_every < rrd_update_every)
226         update_every = rrd_update_every;
227
228     check_for_new_mountpoints_every = (int)config_get_number("plugin:proc:diskspace", "check for new mount points every", check_for_new_mountpoints_every);
229     if(check_for_new_mountpoints_every < update_every)
230         check_for_new_mountpoints_every = update_every;
231
232     struct rusage thread;
233
234     usec_t duration = 0;
235     usec_t step = update_every * USEC_PER_SEC;
236     heartbeat_t hb;
237     heartbeat_init(&hb);
238     for(;;) {
239         duration = heartbeat_dt_usec(&hb);
240         /* usec_t hb_dt = */ heartbeat_next(&hb, step);
241
242         if(unlikely(netdata_exit)) break;
243
244
245         // --------------------------------------------------------------------------
246         // this is smart enough not to reload it every time
247
248         mountinfo_reload(0);
249
250
251         // --------------------------------------------------------------------------
252         // disk space metrics
253
254         struct mountinfo *mi;
255         for(mi = disk_mountinfo_root; mi; mi = mi->next) {
256
257             if(unlikely(mi->flags & (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND | MOUNTINFO_IS_SAME_DEV | MOUNTINFO_NO_STAT | MOUNTINFO_NO_SIZE)))
258                 continue;
259
260             do_disk_space_stats(mi, update_every);
261             if(unlikely(netdata_exit)) break;
262         }
263
264         if(unlikely(netdata_exit)) break;
265
266         if(vdo_cpu_netdata) {
267             static RRDSET *stcpu_thread = NULL, *st_duration = NULL;
268             static RRDDIM *rd_user = NULL, *rd_system = NULL, *rd_duration = NULL;
269
270             // ----------------------------------------------------------------
271
272             getrusage(RUSAGE_THREAD, &thread);
273
274             if(!stcpu_thread) {
275                 stcpu_thread = rrdset_find_localhost("netdata.plugin_diskspace");
276                 if(!stcpu_thread) stcpu_thread = rrdset_create("netdata", "plugin_diskspace", NULL, "diskspace", NULL
277                                                  , "NetData Disk Space Plugin CPU usage", "milliseconds/s", 132020
278                                                  , update_every, RRDSET_TYPE_STACKED);
279
280                 rd_user   = rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRDDIM_ALGORITHM_INCREMENTAL);
281                 rd_system = rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRDDIM_ALGORITHM_INCREMENTAL);
282             }
283             else
284                 rrdset_next(stcpu_thread);
285
286             rrddim_set_by_pointer(stcpu_thread, rd_user, thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
287             rrddim_set_by_pointer(stcpu_thread, rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
288             rrdset_done(stcpu_thread);
289
290             // ----------------------------------------------------------------
291
292             if(!st_duration) {
293                 st_duration = rrdset_find_localhost("netdata.plugin_diskspace_dt");
294                 if(!st_duration) st_duration = rrdset_create("netdata", "plugin_diskspace_dt", NULL, "diskspace", NULL
295                                                  , "NetData Disk Space Plugin Duration", "milliseconds/run", 132021
296                                                  , update_every, RRDSET_TYPE_AREA);
297
298                 rd_duration = rrddim_add(st_duration, "duration", NULL, 1, 1000, RRDDIM_ALGORITHM_ABSOLUTE);
299             }
300             else
301                 rrdset_next(st_duration);
302
303             rrddim_set_by_pointer(st_duration, rd_duration, duration);
304             rrdset_done(st_duration);
305
306             // ----------------------------------------------------------------
307
308             if(unlikely(netdata_exit)) break;
309         }
310     }
311
312     info("DISKSPACE thread exiting");
313
314     static_thread->enabled = 0;
315     pthread_exit(NULL);
316     return NULL;
317 }