]> arthur.barton.de Git - netdata.git/blob - src/plugin_proc_diskspace.c
9e11f85c6331a2320612f7ed87341691fb79b881
[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_localhost("disk_space", disk, NULL, family, "disk.space", title, "GB", 2023
161                                                       , update_every, RRDSET_TYPE_STACKED);
162             }
163
164             m->rd_space_avail    = rrddim_add(m->st_space, "avail", NULL, bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
165             m->rd_space_used     = rrddim_add(m->st_space, "used", NULL, bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
166             m->rd_space_reserved = rrddim_add(m->st_space, "reserved_for_root", "reserved for root", bsize, 1024 * 1024 * 1024, RRD_ALGORITHM_ABSOLUTE);
167         }
168         else
169             rrdset_next(m->st_space);
170
171         rrddim_set_by_pointer(m->st_space, m->rd_space_avail,    (collected_number)bavail);
172         rrddim_set_by_pointer(m->st_space, m->rd_space_used,     (collected_number)bused);
173         rrddim_set_by_pointer(m->st_space, m->rd_space_reserved, (collected_number)breserved_root);
174         rrdset_done(m->st_space);
175
176         rendered++;
177     }
178
179     // --------------------------------------------------------------------------
180
181     if(do_inodes == CONFIG_ONDEMAND_YES || (do_inodes == CONFIG_ONDEMAND_ONDEMAND && (favail || freserved_root || fused))) {
182         if(unlikely(!m->st_inodes)) {
183             m->do_inodes = CONFIG_ONDEMAND_YES;
184             m->st_inodes = rrdset_find_bytype_localhost("disk_inodes", disk);
185             if(unlikely(!m->st_inodes)) {
186                 char title[4096 + 1];
187                 snprintfz(title, 4096, "Disk Files (inodes) Usage for %s [%s]", family, mi->mount_source);
188                 m->st_inodes = rrdset_create_localhost("disk_inodes", disk, NULL, family, "disk.inodes", title, "Inodes"
189                                                        , 2024, update_every, RRDSET_TYPE_STACKED);
190             }
191
192             m->rd_inodes_avail    = rrddim_add(m->st_inodes, "avail", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
193             m->rd_inodes_used     = rrddim_add(m->st_inodes, "used", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
194             m->rd_inodes_reserved = rrddim_add(m->st_inodes, "reserved_for_root", "reserved for root", 1, 1, RRD_ALGORITHM_ABSOLUTE);
195         }
196         else
197             rrdset_next(m->st_inodes);
198
199         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_avail,    (collected_number)favail);
200         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_used,     (collected_number)fused);
201         rrddim_set_by_pointer(m->st_inodes, m->rd_inodes_reserved, (collected_number)freserved_root);
202         rrdset_done(m->st_inodes);
203
204         rendered++;
205     }
206
207     // --------------------------------------------------------------------------
208
209     if(likely(rendered))
210         m->collected++;
211 }
212
213 void *proc_diskspace_main(void *ptr) {
214     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
215
216     info("DISKSPACE thread created with task id %d", gettid());
217
218     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
219         error("Cannot set pthread cancel type to DEFERRED.");
220
221     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
222         error("Cannot set pthread cancel state to ENABLE.");
223
224     int vdo_cpu_netdata = config_get_boolean("plugin:proc", "netdata server resources", 1);
225
226     int update_every = (int)config_get_number("plugin:proc:diskspace", "update every", localhost->rrd_update_every);
227     if(update_every < localhost->rrd_update_every)
228         update_every = localhost->rrd_update_every;
229
230     check_for_new_mountpoints_every = (int)config_get_number("plugin:proc:diskspace", "check for new mount points every", check_for_new_mountpoints_every);
231     if(check_for_new_mountpoints_every < update_every)
232         check_for_new_mountpoints_every = update_every;
233
234     struct rusage thread;
235
236     usec_t duration = 0;
237     usec_t step = update_every * USEC_PER_SEC;
238     heartbeat_t hb;
239     heartbeat_init(&hb);
240     for(;;) {
241         duration = heartbeat_dt_usec(&hb);
242         /* usec_t hb_dt = */ heartbeat_next(&hb, step);
243
244         if(unlikely(netdata_exit)) break;
245
246
247         // --------------------------------------------------------------------------
248         // this is smart enough not to reload it every time
249
250         mountinfo_reload(0);
251
252
253         // --------------------------------------------------------------------------
254         // disk space metrics
255
256         struct mountinfo *mi;
257         for(mi = disk_mountinfo_root; mi; mi = mi->next) {
258
259             if(unlikely(mi->flags & (MOUNTINFO_IS_DUMMY | MOUNTINFO_IS_BIND | MOUNTINFO_IS_SAME_DEV | MOUNTINFO_NO_STAT | MOUNTINFO_NO_SIZE)))
260                 continue;
261
262             do_disk_space_stats(mi, update_every);
263             if(unlikely(netdata_exit)) break;
264         }
265
266         if(unlikely(netdata_exit)) break;
267
268         if(vdo_cpu_netdata) {
269             static RRDSET *stcpu_thread = NULL, *st_duration = NULL;
270             static RRDDIM *rd_user = NULL, *rd_system = NULL, *rd_duration = NULL;
271
272             // ----------------------------------------------------------------
273
274             getrusage(RUSAGE_THREAD, &thread);
275
276             if(!stcpu_thread) {
277                 stcpu_thread = rrdset_find_localhost("netdata.plugin_diskspace");
278                 if(!stcpu_thread) stcpu_thread = rrdset_create_localhost("netdata", "plugin_diskspace", NULL
279                                                                          , "diskspace", NULL
280                                                                          , "NetData Disk Space Plugin CPU usage"
281                                                                          , "milliseconds/s", 132020
282                                                                          , update_every, RRDSET_TYPE_STACKED);
283
284                 rd_user   = rrddim_add(stcpu_thread, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
285                 rd_system = rrddim_add(stcpu_thread, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
286             }
287             else
288                 rrdset_next(stcpu_thread);
289
290             rrddim_set_by_pointer(stcpu_thread, rd_user, thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
291             rrddim_set_by_pointer(stcpu_thread, rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
292             rrdset_done(stcpu_thread);
293
294             // ----------------------------------------------------------------
295
296             if(!st_duration) {
297                 st_duration = rrdset_find_localhost("netdata.plugin_diskspace_dt");
298                 if(!st_duration) st_duration = rrdset_create_localhost("netdata", "plugin_diskspace_dt", NULL
299                                                                        , "diskspace", NULL
300                                                                        , "NetData Disk Space Plugin Duration"
301                                                                        , "milliseconds/run", 132021
302                                                                        , update_every, RRDSET_TYPE_AREA);
303
304                 rd_duration = rrddim_add(st_duration, "duration", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
305             }
306             else
307                 rrdset_next(st_duration);
308
309             rrddim_set_by_pointer(st_duration, rd_duration, duration);
310             rrdset_done(st_duration);
311
312             // ----------------------------------------------------------------
313
314             if(unlikely(netdata_exit)) break;
315         }
316     }
317
318     info("DISKSPACE thread exiting");
319
320     static_thread->enabled = 0;
321     pthread_exit(NULL);
322     return NULL;
323 }